pcache.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. ** 2008 August 05
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** This file implements that page cache.
  13. */
  14. #include "sqliteInt.h"
  15. /*
  16. ** A complete page cache is an instance of this structure.
  17. */
  18. struct PCache {
  19. PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
  20. PgHdr *pSynced; /* Last synced page in dirty page list */
  21. int nRef; /* Number of referenced pages */
  22. int szCache; /* Configured cache size */
  23. int szPage; /* Size of every page in this cache */
  24. int szExtra; /* Size of extra space for each page */
  25. int bPurgeable; /* True if pages are on backing store */
  26. int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
  27. void *pStress; /* Argument to xStress */
  28. sqlite3_pcache *pCache; /* Pluggable cache module */
  29. PgHdr *pPage1; /* Reference to page 1 */
  30. };
  31. /*
  32. ** Some of the assert() macros in this code are too expensive to run
  33. ** even during normal debugging. Use them only rarely on long-running
  34. ** tests. Enable the expensive asserts using the
  35. ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
  36. */
  37. #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
  38. # define expensive_assert(X) assert(X)
  39. #else
  40. # define expensive_assert(X)
  41. #endif
  42. /********************************** Linked List Management ********************/
  43. #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
  44. /*
  45. ** Check that the pCache->pSynced variable is set correctly. If it
  46. ** is not, either fail an assert or return zero. Otherwise, return
  47. ** non-zero. This is only used in debugging builds, as follows:
  48. **
  49. ** expensive_assert( pcacheCheckSynced(pCache) );
  50. */
  51. static int pcacheCheckSynced(PCache *pCache){
  52. PgHdr *p;
  53. for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
  54. assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
  55. }
  56. return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
  57. }
  58. #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
  59. /*
  60. ** Remove page pPage from the list of dirty pages.
  61. */
  62. static void pcacheRemoveFromDirtyList(PgHdr *pPage){
  63. PCache *p = pPage->pCache;
  64. assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
  65. assert( pPage->pDirtyPrev || pPage==p->pDirty );
  66. /* Update the PCache1.pSynced variable if necessary. */
  67. if( p->pSynced==pPage ){
  68. PgHdr *pSynced = pPage->pDirtyPrev;
  69. while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
  70. pSynced = pSynced->pDirtyPrev;
  71. }
  72. p->pSynced = pSynced;
  73. }
  74. if( pPage->pDirtyNext ){
  75. pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
  76. }else{
  77. assert( pPage==p->pDirtyTail );
  78. p->pDirtyTail = pPage->pDirtyPrev;
  79. }
  80. if( pPage->pDirtyPrev ){
  81. pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
  82. }else{
  83. assert( pPage==p->pDirty );
  84. p->pDirty = pPage->pDirtyNext;
  85. }
  86. pPage->pDirtyNext = 0;
  87. pPage->pDirtyPrev = 0;
  88. expensive_assert( pcacheCheckSynced(p) );
  89. }
  90. /*
  91. ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
  92. ** pPage).
  93. */
  94. static void pcacheAddToDirtyList(PgHdr *pPage){
  95. PCache *p = pPage->pCache;
  96. assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
  97. pPage->pDirtyNext = p->pDirty;
  98. if( pPage->pDirtyNext ){
  99. assert( pPage->pDirtyNext->pDirtyPrev==0 );
  100. pPage->pDirtyNext->pDirtyPrev = pPage;
  101. }
  102. p->pDirty = pPage;
  103. if( !p->pDirtyTail ){
  104. p->pDirtyTail = pPage;
  105. }
  106. if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
  107. p->pSynced = pPage;
  108. }
  109. expensive_assert( pcacheCheckSynced(p) );
  110. }
  111. /*
  112. ** Wrapper around the pluggable caches xUnpin method. If the cache is
  113. ** being used for an in-memory database, this function is a no-op.
  114. */
  115. static void pcacheUnpin(PgHdr *p){
  116. PCache *pCache = p->pCache;
  117. if( pCache->bPurgeable ){
  118. if( p->pgno==1 ){
  119. pCache->pPage1 = 0;
  120. }
  121. sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
  122. }
  123. }
  124. /*************************************************** General Interfaces ******
  125. **
  126. ** Initialize and shutdown the page cache subsystem. Neither of these
  127. ** functions are threadsafe.
  128. */
  129. int sqlite3PcacheInitialize(void){
  130. if( sqlite3GlobalConfig.pcache2.xInit==0 ){
  131. /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
  132. ** built-in default page cache is used instead of the application defined
  133. ** page cache. */
  134. sqlite3PCacheSetDefault();
  135. }
  136. return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
  137. }
  138. void sqlite3PcacheShutdown(void){
  139. if( sqlite3GlobalConfig.pcache2.xShutdown ){
  140. /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
  141. sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
  142. }
  143. }
  144. /*
  145. ** Return the size in bytes of a PCache object.
  146. */
  147. int sqlite3PcacheSize(void){ return sizeof(PCache); }
  148. /*
  149. ** Create a new PCache object. Storage space to hold the object
  150. ** has already been allocated and is passed in as the p pointer.
  151. ** The caller discovers how much space needs to be allocated by
  152. ** calling sqlite3PcacheSize().
  153. */
  154. void sqlite3PcacheOpen(
  155. int szPage, /* Size of every page */
  156. int szExtra, /* Extra space associated with each page */
  157. int bPurgeable, /* True if pages are on backing store */
  158. int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
  159. void *pStress, /* Argument to xStress */
  160. PCache *p /* Preallocated space for the PCache */
  161. ){
  162. memset(p, 0, sizeof(PCache));
  163. p->szPage = szPage;
  164. p->szExtra = szExtra;
  165. p->bPurgeable = bPurgeable;
  166. p->xStress = xStress;
  167. p->pStress = pStress;
  168. p->szCache = 100;
  169. }
  170. /*
  171. ** Change the page size for PCache object. The caller must ensure that there
  172. ** are no outstanding page references when this function is called.
  173. */
  174. void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
  175. assert( pCache->nRef==0 && pCache->pDirty==0 );
  176. if( pCache->pCache ){
  177. sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
  178. pCache->pCache = 0;
  179. pCache->pPage1 = 0;
  180. }
  181. pCache->szPage = szPage;
  182. }
  183. /*
  184. ** Compute the number of pages of cache requested.
  185. */
  186. static int numberOfCachePages(PCache *p){
  187. if( p->szCache>=0 ){
  188. return p->szCache;
  189. }else{
  190. return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
  191. }
  192. }
  193. /*
  194. ** Try to obtain a page from the cache.
  195. */
  196. int sqlite3PcacheFetch(
  197. PCache *pCache, /* Obtain the page from this cache */
  198. Pgno pgno, /* Page number to obtain */
  199. int createFlag, /* If true, create page if it does not exist already */
  200. PgHdr **ppPage /* Write the page here */
  201. ){
  202. sqlite3_pcache_page *pPage = 0;
  203. PgHdr *pPgHdr = 0;
  204. int eCreate;
  205. assert( pCache!=0 );
  206. assert( createFlag==1 || createFlag==0 );
  207. assert( pgno>0 );
  208. /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
  209. ** allocate it now.
  210. */
  211. if( !pCache->pCache && createFlag ){
  212. sqlite3_pcache *p;
  213. p = sqlite3GlobalConfig.pcache2.xCreate(
  214. pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
  215. );
  216. if( !p ){
  217. return SQLITE_NOMEM;
  218. }
  219. sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
  220. pCache->pCache = p;
  221. }
  222. eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
  223. if( pCache->pCache ){
  224. pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
  225. }
  226. if( !pPage && eCreate==1 ){
  227. PgHdr *pPg;
  228. /* Find a dirty page to write-out and recycle. First try to find a
  229. ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
  230. ** cleared), but if that is not possible settle for any other
  231. ** unreferenced dirty page.
  232. */
  233. expensive_assert( pcacheCheckSynced(pCache) );
  234. for(pPg=pCache->pSynced;
  235. pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
  236. pPg=pPg->pDirtyPrev
  237. );
  238. pCache->pSynced = pPg;
  239. if( !pPg ){
  240. for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
  241. }
  242. if( pPg ){
  243. int rc;
  244. #ifdef SQLITE_LOG_CACHE_SPILL
  245. sqlite3_log(SQLITE_FULL,
  246. "spill page %d making room for %d - cache used: %d/%d",
  247. pPg->pgno, pgno,
  248. sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
  249. numberOfCachePages(pCache));
  250. #endif
  251. rc = pCache->xStress(pCache->pStress, pPg);
  252. if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
  253. return rc;
  254. }
  255. }
  256. pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
  257. }
  258. if( pPage ){
  259. pPgHdr = (PgHdr *)pPage->pExtra;
  260. if( !pPgHdr->pPage ){
  261. memset(pPgHdr, 0, sizeof(PgHdr));
  262. pPgHdr->pPage = pPage;
  263. pPgHdr->pData = pPage->pBuf;
  264. pPgHdr->pExtra = (void *)&pPgHdr[1];
  265. memset(pPgHdr->pExtra, 0, pCache->szExtra);
  266. pPgHdr->pCache = pCache;
  267. pPgHdr->pgno = pgno;
  268. }
  269. assert( pPgHdr->pCache==pCache );
  270. assert( pPgHdr->pgno==pgno );
  271. assert( pPgHdr->pData==pPage->pBuf );
  272. assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
  273. if( 0==pPgHdr->nRef ){
  274. pCache->nRef++;
  275. }
  276. pPgHdr->nRef++;
  277. if( pgno==1 ){
  278. pCache->pPage1 = pPgHdr;
  279. }
  280. }
  281. *ppPage = pPgHdr;
  282. return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
  283. }
  284. /*
  285. ** Decrement the reference count on a page. If the page is clean and the
  286. ** reference count drops to 0, then it is made elible for recycling.
  287. */
  288. void sqlite3PcacheRelease(PgHdr *p){
  289. assert( p->nRef>0 );
  290. p->nRef--;
  291. if( p->nRef==0 ){
  292. PCache *pCache = p->pCache;
  293. pCache->nRef--;
  294. if( (p->flags&PGHDR_DIRTY)==0 ){
  295. pcacheUnpin(p);
  296. }else{
  297. /* Move the page to the head of the dirty list. */
  298. pcacheRemoveFromDirtyList(p);
  299. pcacheAddToDirtyList(p);
  300. }
  301. }
  302. }
  303. /*
  304. ** Increase the reference count of a supplied page by 1.
  305. */
  306. void sqlite3PcacheRef(PgHdr *p){
  307. assert(p->nRef>0);
  308. p->nRef++;
  309. }
  310. /*
  311. ** Drop a page from the cache. There must be exactly one reference to the
  312. ** page. This function deletes that reference, so after it returns the
  313. ** page pointed to by p is invalid.
  314. */
  315. void sqlite3PcacheDrop(PgHdr *p){
  316. PCache *pCache;
  317. assert( p->nRef==1 );
  318. if( p->flags&PGHDR_DIRTY ){
  319. pcacheRemoveFromDirtyList(p);
  320. }
  321. pCache = p->pCache;
  322. pCache->nRef--;
  323. if( p->pgno==1 ){
  324. pCache->pPage1 = 0;
  325. }
  326. sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
  327. }
  328. /*
  329. ** Make sure the page is marked as dirty. If it isn't dirty already,
  330. ** make it so.
  331. */
  332. void sqlite3PcacheMakeDirty(PgHdr *p){
  333. p->flags &= ~PGHDR_DONT_WRITE;
  334. assert( p->nRef>0 );
  335. if( 0==(p->flags & PGHDR_DIRTY) ){
  336. p->flags |= PGHDR_DIRTY;
  337. pcacheAddToDirtyList( p);
  338. }
  339. }
  340. /*
  341. ** Make sure the page is marked as clean. If it isn't clean already,
  342. ** make it so.
  343. */
  344. void sqlite3PcacheMakeClean(PgHdr *p){
  345. if( (p->flags & PGHDR_DIRTY) ){
  346. pcacheRemoveFromDirtyList(p);
  347. p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
  348. if( p->nRef==0 ){
  349. pcacheUnpin(p);
  350. }
  351. }
  352. }
  353. /*
  354. ** Make every page in the cache clean.
  355. */
  356. void sqlite3PcacheCleanAll(PCache *pCache){
  357. PgHdr *p;
  358. while( (p = pCache->pDirty)!=0 ){
  359. sqlite3PcacheMakeClean(p);
  360. }
  361. }
  362. /*
  363. ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
  364. */
  365. void sqlite3PcacheClearSyncFlags(PCache *pCache){
  366. PgHdr *p;
  367. for(p=pCache->pDirty; p; p=p->pDirtyNext){
  368. p->flags &= ~PGHDR_NEED_SYNC;
  369. }
  370. pCache->pSynced = pCache->pDirtyTail;
  371. }
  372. /*
  373. ** Change the page number of page p to newPgno.
  374. */
  375. void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
  376. PCache *pCache = p->pCache;
  377. assert( p->nRef>0 );
  378. assert( newPgno>0 );
  379. sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
  380. p->pgno = newPgno;
  381. if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
  382. pcacheRemoveFromDirtyList(p);
  383. pcacheAddToDirtyList(p);
  384. }
  385. }
  386. /*
  387. ** Drop every cache entry whose page number is greater than "pgno". The
  388. ** caller must ensure that there are no outstanding references to any pages
  389. ** other than page 1 with a page number greater than pgno.
  390. **
  391. ** If there is a reference to page 1 and the pgno parameter passed to this
  392. ** function is 0, then the data area associated with page 1 is zeroed, but
  393. ** the page object is not dropped.
  394. */
  395. void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
  396. if( pCache->pCache ){
  397. PgHdr *p;
  398. PgHdr *pNext;
  399. for(p=pCache->pDirty; p; p=pNext){
  400. pNext = p->pDirtyNext;
  401. /* This routine never gets call with a positive pgno except right
  402. ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
  403. ** it must be that pgno==0.
  404. */
  405. assert( p->pgno>0 );
  406. if( ALWAYS(p->pgno>pgno) ){
  407. assert( p->flags&PGHDR_DIRTY );
  408. sqlite3PcacheMakeClean(p);
  409. }
  410. }
  411. if( pgno==0 && pCache->pPage1 ){
  412. memset(pCache->pPage1->pData, 0, pCache->szPage);
  413. pgno = 1;
  414. }
  415. sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
  416. }
  417. }
  418. /*
  419. ** Close a cache.
  420. */
  421. void sqlite3PcacheClose(PCache *pCache){
  422. if( pCache->pCache ){
  423. sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
  424. }
  425. }
  426. /*
  427. ** Discard the contents of the cache.
  428. */
  429. void sqlite3PcacheClear(PCache *pCache){
  430. sqlite3PcacheTruncate(pCache, 0);
  431. }
  432. /*
  433. ** Merge two lists of pages connected by pDirty and in pgno order.
  434. ** Do not both fixing the pDirtyPrev pointers.
  435. */
  436. static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
  437. PgHdr result, *pTail;
  438. pTail = &result;
  439. while( pA && pB ){
  440. if( pA->pgno<pB->pgno ){
  441. pTail->pDirty = pA;
  442. pTail = pA;
  443. pA = pA->pDirty;
  444. }else{
  445. pTail->pDirty = pB;
  446. pTail = pB;
  447. pB = pB->pDirty;
  448. }
  449. }
  450. if( pA ){
  451. pTail->pDirty = pA;
  452. }else if( pB ){
  453. pTail->pDirty = pB;
  454. }else{
  455. pTail->pDirty = 0;
  456. }
  457. return result.pDirty;
  458. }
  459. /*
  460. ** Sort the list of pages in accending order by pgno. Pages are
  461. ** connected by pDirty pointers. The pDirtyPrev pointers are
  462. ** corrupted by this sort.
  463. **
  464. ** Since there cannot be more than 2^31 distinct pages in a database,
  465. ** there cannot be more than 31 buckets required by the merge sorter.
  466. ** One extra bucket is added to catch overflow in case something
  467. ** ever changes to make the previous sentence incorrect.
  468. */
  469. #define N_SORT_BUCKET 32
  470. static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
  471. PgHdr *a[N_SORT_BUCKET], *p;
  472. int i;
  473. memset(a, 0, sizeof(a));
  474. while( pIn ){
  475. p = pIn;
  476. pIn = p->pDirty;
  477. p->pDirty = 0;
  478. for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
  479. if( a[i]==0 ){
  480. a[i] = p;
  481. break;
  482. }else{
  483. p = pcacheMergeDirtyList(a[i], p);
  484. a[i] = 0;
  485. }
  486. }
  487. if( NEVER(i==N_SORT_BUCKET-1) ){
  488. /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
  489. ** the input list. But that is impossible.
  490. */
  491. a[i] = pcacheMergeDirtyList(a[i], p);
  492. }
  493. }
  494. p = a[0];
  495. for(i=1; i<N_SORT_BUCKET; i++){
  496. p = pcacheMergeDirtyList(p, a[i]);
  497. }
  498. return p;
  499. }
  500. /*
  501. ** Return a list of all dirty pages in the cache, sorted by page number.
  502. */
  503. PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
  504. PgHdr *p;
  505. for(p=pCache->pDirty; p; p=p->pDirtyNext){
  506. p->pDirty = p->pDirtyNext;
  507. }
  508. return pcacheSortDirtyList(pCache->pDirty);
  509. }
  510. /*
  511. ** Return the total number of referenced pages held by the cache.
  512. */
  513. int sqlite3PcacheRefCount(PCache *pCache){
  514. return pCache->nRef;
  515. }
  516. /*
  517. ** Return the number of references to the page supplied as an argument.
  518. */
  519. int sqlite3PcachePageRefcount(PgHdr *p){
  520. return p->nRef;
  521. }
  522. /*
  523. ** Return the total number of pages in the cache.
  524. */
  525. int sqlite3PcachePagecount(PCache *pCache){
  526. int nPage = 0;
  527. if( pCache->pCache ){
  528. nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
  529. }
  530. return nPage;
  531. }
  532. #ifdef SQLITE_TEST
  533. /*
  534. ** Get the suggested cache-size value.
  535. */
  536. int sqlite3PcacheGetCachesize(PCache *pCache){
  537. return numberOfCachePages(pCache);
  538. }
  539. #endif
  540. /*
  541. ** Set the suggested cache-size value.
  542. */
  543. void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
  544. pCache->szCache = mxPage;
  545. if( pCache->pCache ){
  546. sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
  547. numberOfCachePages(pCache));
  548. }
  549. }
  550. /*
  551. ** Free up as much memory as possible from the page cache.
  552. */
  553. void sqlite3PcacheShrink(PCache *pCache){
  554. if( pCache->pCache ){
  555. sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
  556. }
  557. }
  558. #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  559. /*
  560. ** For all dirty pages currently in the cache, invoke the specified
  561. ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
  562. ** defined.
  563. */
  564. void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
  565. PgHdr *pDirty;
  566. for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
  567. xIter(pDirty);
  568. }
  569. }
  570. #endif