blob: 6c07b20f5708422bea28e3b0cccef64b5fc0ac6b [file] [log] [blame]
danielk19778c0a7912008-08-20 14:49:23 +00001/*
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.
danielk19778c0a7912008-08-20 14:49:23 +000013*/
14#include "sqliteInt.h"
15
16/*
drh72e6a392016-05-11 23:54:14 +000017** A complete page cache is an instance of this structure. Every
18** entry in the cache holds a single page of the database file. The
19** btree layer only operates on the cached copy of the database pages.
20**
21** A page cache entry is "clean" if it exactly matches what is currently
22** on disk. A page is "dirty" if it has been modified and needs to be
23** persisted to disk.
danb2ef9002016-05-11 15:41:15 +000024**
25** pDirty, pDirtyTail, pSynced:
26** All dirty pages are linked into the doubly linked list using
27** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
28** such that p was added to the list more recently than p->pDirtyNext.
29** PCache.pDirty points to the first (newest) element in the list and
30** pDirtyTail to the last (oldest).
31**
32** The PCache.pSynced variable is used to optimize searching for a dirty
33** page to eject from the cache mid-transaction. It is better to eject
34** a page that does not require a journal sync than one that does.
35** Therefore, pSynced is maintained to that it *almost* always points
36** to either the oldest page in the pDirty/pDirtyTail list that has a
37** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
38** (so that the right page to eject can be found by following pDirtyPrev
39** pointers).
danielk19778c0a7912008-08-20 14:49:23 +000040*/
41struct PCache {
danielk1977d491e1b2008-08-26 18:05:48 +000042 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
43 PgHdr *pSynced; /* Last synced page in dirty page list */
drh95a0b372015-09-03 20:43:55 +000044 int nRefSum; /* Sum of ref counts over all pages */
drh3b42abb2011-11-09 14:23:04 +000045 int szCache; /* Configured cache size */
drh9b0cf342015-11-12 14:57:19 +000046 int szSpill; /* Size before spilling occurs */
drha85f7e32008-08-28 02:26:07 +000047 int szPage; /* Size of every page in this cache */
48 int szExtra; /* Size of extra space for each page */
drhfe21a792014-02-03 17:04:29 +000049 u8 bPurgeable; /* True if pages are on backing store */
50 u8 eCreate; /* eCreate value for for xFetch() */
drha85f7e32008-08-28 02:26:07 +000051 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
52 void *pStress; /* Argument to xStress */
danielk1977bc2ca9e2008-11-13 14:28:28 +000053 sqlite3_pcache *pCache; /* Pluggable cache module */
danielk19778c0a7912008-08-20 14:49:23 +000054};
55
drh5c8e0922016-05-11 10:57:04 +000056/*
57** Debug tracing macros
58*/
59#if defined(SQLITE_DEBUG) && 0
drh7aeb2162016-05-13 04:24:25 +000060 int sqlite3PcacheTrace = 2;
drh5c8e0922016-05-11 10:57:04 +000061# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
drh7aeb2162016-05-13 04:24:25 +000062 void pcacheDump(PCache *pCache){
63 int N;
64 int i, j;
65 sqlite3_pcache_page *pLower;
66 PgHdr *pPg;
67 unsigned char *a;
68
69 if( sqlite3PcacheTrace<2 ) return;
70 if( pCache->pCache==0 ) return;
71 N = sqlite3PcachePagecount(pCache);
72 if( N>5 ) N = 5;
73 for(i=1; i<=N; i++){
74 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
75 if( pLower==0 ) continue;
76 pPg = (PgHdr*)pLower->pExtra;
77 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
78 a = (unsigned char *)pLower->pBuf;
79 for(j=0; j<12; j++) printf("%02x", a[j]);
80 printf("\n");
81 if( pPg->pPage==0 ){
82 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
83 }
84 }
85 }
86 #else
drh5c8e0922016-05-11 10:57:04 +000087# define pcacheTrace(X)
drh7aeb2162016-05-13 04:24:25 +000088# define pcacheDump(X)
drh5c8e0922016-05-11 10:57:04 +000089#endif
90
danielk19778c0a7912008-08-20 14:49:23 +000091/********************************** Linked List Management ********************/
92
drha8dcba92014-08-22 20:35:29 +000093/* Allowed values for second argument to pcacheManageDirtyList() */
94#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
95#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
96#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
danielk19778c0a7912008-08-20 14:49:23 +000097
98/*
drha8dcba92014-08-22 20:35:29 +000099** Manage pPage's participation on the dirty list. Bits of the addRemove
100** argument determines what operation to do. The 0x01 bit means first
101** remove pPage from the dirty list. The 0x02 means add pPage back to
102** the dirty list. Doing both moves pPage to the front of the dirty list.
danielk19778c0a7912008-08-20 14:49:23 +0000103*/
drha8dcba92014-08-22 20:35:29 +0000104static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000105 PCache *p = pPage->pCache;
danielk19778c0a7912008-08-20 14:49:23 +0000106
drh5c8e0922016-05-11 10:57:04 +0000107 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
108 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
109 pPage->pgno));
drha8dcba92014-08-22 20:35:29 +0000110 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
111 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
112 assert( pPage->pDirtyPrev || pPage==p->pDirty );
113
114 /* Update the PCache1.pSynced variable if necessary. */
115 if( p->pSynced==pPage ){
danb2ef9002016-05-11 15:41:15 +0000116 p->pSynced = pPage->pDirtyPrev;
drha8dcba92014-08-22 20:35:29 +0000117 }
118
119 if( pPage->pDirtyNext ){
120 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
121 }else{
122 assert( pPage==p->pDirtyTail );
123 p->pDirtyTail = pPage->pDirtyPrev;
124 }
125 if( pPage->pDirtyPrev ){
126 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
127 }else{
dan401907e2016-05-11 20:03:23 +0000128 /* If there are now no dirty pages in the cache, set eCreate to 2.
129 ** This is an optimization that allows sqlite3PcacheFetch() to skip
130 ** searching for a dirty page to eject from the cache when it might
131 ** otherwise have to. */
drha8dcba92014-08-22 20:35:29 +0000132 assert( pPage==p->pDirty );
133 p->pDirty = pPage->pDirtyNext;
dan401907e2016-05-11 20:03:23 +0000134 assert( p->bPurgeable || p->eCreate==2 );
135 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
136 assert( p->bPurgeable==0 || p->eCreate==1 );
drha8dcba92014-08-22 20:35:29 +0000137 p->eCreate = 2;
138 }
139 }
140 pPage->pDirtyNext = 0;
141 pPage->pDirtyPrev = 0;
danielk19778c0a7912008-08-20 14:49:23 +0000142 }
drha8dcba92014-08-22 20:35:29 +0000143 if( addRemove & PCACHE_DIRTYLIST_ADD ){
144 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
145
146 pPage->pDirtyNext = p->pDirty;
147 if( pPage->pDirtyNext ){
148 assert( pPage->pDirtyNext->pDirtyPrev==0 );
149 pPage->pDirtyNext->pDirtyPrev = pPage;
drh36ce9192014-09-12 20:30:59 +0000150 }else{
151 p->pDirtyTail = pPage;
152 if( p->bPurgeable ){
153 assert( p->eCreate==2 );
154 p->eCreate = 1;
155 }
drha8dcba92014-08-22 20:35:29 +0000156 }
157 p->pDirty = pPage;
dan613723d2016-05-12 09:48:23 +0000158
159 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
160 ** pSynced to point to it. Checking the NEED_SYNC flag is an
161 ** optimization, as if pSynced points to a page with the NEED_SYNC
162 ** flag set sqlite3PcacheFetchStress() searches through all newer
163 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
164 if( !p->pSynced
165 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
166 ){
drha8dcba92014-08-22 20:35:29 +0000167 p->pSynced = pPage;
168 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000169 }
drh7aeb2162016-05-13 04:24:25 +0000170 pcacheDump(p);
danielk19778c0a7912008-08-20 14:49:23 +0000171}
172
173/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000174** Wrapper around the pluggable caches xUnpin method. If the cache is
175** being used for an in-memory database, this function is a no-op.
danielk19778c0a7912008-08-20 14:49:23 +0000176*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000177static void pcacheUnpin(PgHdr *p){
drha8dcba92014-08-22 20:35:29 +0000178 if( p->pCache->bPurgeable ){
drh5c8e0922016-05-11 10:57:04 +0000179 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
drha8dcba92014-08-22 20:35:29 +0000180 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
drh7aeb2162016-05-13 04:24:25 +0000181 pcacheDump(p->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000182 }
danielk19778c0a7912008-08-20 14:49:23 +0000183}
184
drhc3031c62014-08-26 15:06:49 +0000185/*
drh9b0cf342015-11-12 14:57:19 +0000186** Compute the number of pages of cache requested. p->szCache is the
drhe0e84292015-02-27 21:53:35 +0000187** cache size requested by the "PRAGMA cache_size" statement.
drhc3031c62014-08-26 15:06:49 +0000188*/
189static int numberOfCachePages(PCache *p){
190 if( p->szCache>=0 ){
drhe0e84292015-02-27 21:53:35 +0000191 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
192 ** suggested cache size is set to N. */
drhc3031c62014-08-26 15:06:49 +0000193 return p->szCache;
194 }else{
drhe0e84292015-02-27 21:53:35 +0000195 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
196 ** the number of cache pages is adjusted to use approximately abs(N*1024)
197 ** bytes of memory. */
drhc3031c62014-08-26 15:06:49 +0000198 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
199 }
200}
201
danielk19778c0a7912008-08-20 14:49:23 +0000202/*************************************************** General Interfaces ******
203**
204** Initialize and shutdown the page cache subsystem. Neither of these
205** functions are threadsafe.
206*/
207int sqlite3PcacheInitialize(void){
dan22e21ff2011-11-08 20:08:44 +0000208 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
drhf759bb82010-09-09 18:25:34 +0000209 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
210 ** built-in default page cache is used instead of the application defined
211 ** page cache. */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000212 sqlite3PCacheSetDefault();
danielk19778c0a7912008-08-20 14:49:23 +0000213 }
dan22e21ff2011-11-08 20:08:44 +0000214 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
danielk19778c0a7912008-08-20 14:49:23 +0000215}
216void sqlite3PcacheShutdown(void){
dan22e21ff2011-11-08 20:08:44 +0000217 if( sqlite3GlobalConfig.pcache2.xShutdown ){
drhf759bb82010-09-09 18:25:34 +0000218 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
dan22e21ff2011-11-08 20:08:44 +0000219 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000220 }
danielk19778c0a7912008-08-20 14:49:23 +0000221}
222
223/*
224** Return the size in bytes of a PCache object.
225*/
226int sqlite3PcacheSize(void){ return sizeof(PCache); }
227
228/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000229** Create a new PCache object. Storage space to hold the object
230** has already been allocated and is passed in as the p pointer.
231** The caller discovers how much space needs to be allocated by
232** calling sqlite3PcacheSize().
danielk19778c0a7912008-08-20 14:49:23 +0000233*/
drhc3031c62014-08-26 15:06:49 +0000234int sqlite3PcacheOpen(
danielk19778c0a7912008-08-20 14:49:23 +0000235 int szPage, /* Size of every page */
236 int szExtra, /* Extra space associated with each page */
237 int bPurgeable, /* True if pages are on backing store */
danielk1977a858aa22008-08-22 16:22:17 +0000238 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
danielk19778c0a7912008-08-20 14:49:23 +0000239 void *pStress, /* Argument to xStress */
240 PCache *p /* Preallocated space for the PCache */
241){
danielk19778c0a7912008-08-20 14:49:23 +0000242 memset(p, 0, sizeof(PCache));
drhc3031c62014-08-26 15:06:49 +0000243 p->szPage = 1;
danielk19778c0a7912008-08-20 14:49:23 +0000244 p->szExtra = szExtra;
245 p->bPurgeable = bPurgeable;
drhfe21a792014-02-03 17:04:29 +0000246 p->eCreate = 2;
danielk19778c0a7912008-08-20 14:49:23 +0000247 p->xStress = xStress;
248 p->pStress = pStress;
drh3b42abb2011-11-09 14:23:04 +0000249 p->szCache = 100;
drh9b0cf342015-11-12 14:57:19 +0000250 p->szSpill = 1;
drh5c8e0922016-05-11 10:57:04 +0000251 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
drhc3031c62014-08-26 15:06:49 +0000252 return sqlite3PcacheSetPageSize(p, szPage);
danielk19778c0a7912008-08-20 14:49:23 +0000253}
254
drh41d30272008-08-20 21:47:45 +0000255/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000256** Change the page size for PCache object. The caller must ensure that there
257** are no outstanding page references when this function is called.
drh41d30272008-08-20 21:47:45 +0000258*/
drhc3031c62014-08-26 15:06:49 +0000259int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
drh95a0b372015-09-03 20:43:55 +0000260 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
drhc3031c62014-08-26 15:06:49 +0000261 if( pCache->szPage ){
262 sqlite3_pcache *pNew;
263 pNew = sqlite3GlobalConfig.pcache2.xCreate(
drh51dc84e2014-12-30 13:04:25 +0000264 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
265 pCache->bPurgeable
drhc3031c62014-08-26 15:06:49 +0000266 );
mistachkinfad30392016-02-13 23:43:46 +0000267 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
drhc3031c62014-08-26 15:06:49 +0000268 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
269 if( pCache->pCache ){
270 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
271 }
272 pCache->pCache = pNew;
drhc3031c62014-08-26 15:06:49 +0000273 pCache->szPage = szPage;
drh5c8e0922016-05-11 10:57:04 +0000274 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000275 }
drhc3031c62014-08-26 15:06:49 +0000276 return SQLITE_OK;
drh3b42abb2011-11-09 14:23:04 +0000277}
278
279/*
danielk19778c0a7912008-08-20 14:49:23 +0000280** Try to obtain a page from the cache.
drhbc59ac02014-08-27 23:18:01 +0000281**
282** This routine returns a pointer to an sqlite3_pcache_page object if
283** such an object is already in cache, or if a new one is created.
284** This routine returns a NULL pointer if the object was not in cache
285** and could not be created.
286**
287** The createFlags should be 0 to check for existing pages and should
288** be 3 (not 1, but 3) to try to create a new page.
289**
290** If the createFlag is 0, then NULL is always returned if the page
291** is not already in the cache. If createFlag is 1, then a new page
292** is created only if that can be done without spilling dirty pages
293** and without exceeding the cache size limit.
294**
295** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
296** initialize the sqlite3_pcache_page object and convert it into a
297** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
298** routines are split this way for performance reasons. When separated
299** they can both (usually) operate without having to push values to
300** the stack on entry and pop them back off on exit, which saves a
301** lot of pushing and popping.
danielk19778c0a7912008-08-20 14:49:23 +0000302*/
drhbc59ac02014-08-27 23:18:01 +0000303sqlite3_pcache_page *sqlite3PcacheFetch(
danielk19778c0a7912008-08-20 14:49:23 +0000304 PCache *pCache, /* Obtain the page from this cache */
305 Pgno pgno, /* Page number to obtain */
drhbc59ac02014-08-27 23:18:01 +0000306 int createFlag /* If true, create page if it does not exist already */
danielk19778c0a7912008-08-20 14:49:23 +0000307){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000308 int eCreate;
drh5c8e0922016-05-11 10:57:04 +0000309 sqlite3_pcache_page *pRes;
danielk1977f599a192008-08-28 10:21:16 +0000310
danielk19778c0a7912008-08-20 14:49:23 +0000311 assert( pCache!=0 );
drhc3031c62014-08-26 15:06:49 +0000312 assert( pCache->pCache!=0 );
313 assert( createFlag==3 || createFlag==0 );
danielk19778c0a7912008-08-20 14:49:23 +0000314 assert( pgno>0 );
dan401907e2016-05-11 20:03:23 +0000315 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
danielk19778c0a7912008-08-20 14:49:23 +0000316
drhfe21a792014-02-03 17:04:29 +0000317 /* eCreate defines what to do if the page does not exist.
318 ** 0 Do not allocate a new page. (createFlag==0)
319 ** 1 Allocate a new page if doing so is inexpensive.
320 ** (createFlag==1 AND bPurgeable AND pDirty)
321 ** 2 Allocate a new page even it doing so is difficult.
322 ** (createFlag==1 AND !(bPurgeable AND pDirty)
323 */
drhc3031c62014-08-26 15:06:49 +0000324 eCreate = createFlag & pCache->eCreate;
325 assert( eCreate==0 || eCreate==1 || eCreate==2 );
326 assert( createFlag==0 || pCache->eCreate==eCreate );
327 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
drh5c8e0922016-05-11 10:57:04 +0000328 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
329 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
330 createFlag?" create":"",pRes));
331 return pRes;
drhbc59ac02014-08-27 23:18:01 +0000332}
danielk1977bc2ca9e2008-11-13 14:28:28 +0000333
drhbc59ac02014-08-27 23:18:01 +0000334/*
335** If the sqlite3PcacheFetch() routine is unable to allocate a new
dan41113b62016-04-05 21:07:58 +0000336** page because no clean pages are available for reuse and the cache
drhbc59ac02014-08-27 23:18:01 +0000337** size limit has been reached, then this routine can be invoked to
338** try harder to allocate a page. This routine might invoke the stress
339** callback to spill dirty pages to the journal. It will then try to
340** allocate the new page and will only fail to allocate a new page on
341** an OOM error.
342**
343** This routine should be invoked only after sqlite3PcacheFetch() fails.
344*/
345int sqlite3PcacheFetchStress(
346 PCache *pCache, /* Obtain the page from this cache */
347 Pgno pgno, /* Page number to obtain */
348 sqlite3_pcache_page **ppPage /* Write result here */
349){
350 PgHdr *pPg;
351 if( pCache->eCreate==2 ) return 0;
352
drh9b0cf342015-11-12 14:57:19 +0000353 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
354 /* Find a dirty page to write-out and recycle. First try to find a
355 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
356 ** cleared), but if that is not possible settle for any other
357 ** unreferenced dirty page.
danb2ef9002016-05-11 15:41:15 +0000358 **
359 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
360 ** flag is currently referenced, then the following may leave pSynced
361 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
362 ** cleared). This is Ok, as pSynced is just an optimization. */
drh9b0cf342015-11-12 14:57:19 +0000363 for(pPg=pCache->pSynced;
364 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
365 pPg=pPg->pDirtyPrev
366 );
367 pCache->pSynced = pPg;
368 if( !pPg ){
369 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
370 }
371 if( pPg ){
372 int rc;
drhc97125e2011-05-28 15:53:07 +0000373#ifdef SQLITE_LOG_CACHE_SPILL
drh9b0cf342015-11-12 14:57:19 +0000374 sqlite3_log(SQLITE_FULL,
375 "spill page %d making room for %d - cache used: %d/%d",
376 pPg->pgno, pgno,
377 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
drhbc59ac02014-08-27 23:18:01 +0000378 numberOfCachePages(pCache));
drhc97125e2011-05-28 15:53:07 +0000379#endif
drh5c8e0922016-05-11 10:57:04 +0000380 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
drh9b0cf342015-11-12 14:57:19 +0000381 rc = pCache->xStress(pCache->pStress, pPg);
drh7aeb2162016-05-13 04:24:25 +0000382 pcacheDump(pCache);
drh9b0cf342015-11-12 14:57:19 +0000383 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
384 return rc;
385 }
danielk1977f599a192008-08-28 10:21:16 +0000386 }
danielk19778c0a7912008-08-20 14:49:23 +0000387 }
drhbc59ac02014-08-27 23:18:01 +0000388 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
mistachkinfad30392016-02-13 23:43:46 +0000389 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
drhbc59ac02014-08-27 23:18:01 +0000390}
391
392/*
393** This is a helper routine for sqlite3PcacheFetchFinish()
394**
395** In the uncommon case where the page being fetched has not been
396** initialized, this routine is invoked to do the initialization.
397** This routine is broken out into a separate function since it
398** requires extra stack manipulation that can be avoided in the common
399** case.
400*/
401static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
402 PCache *pCache, /* Obtain the page from this cache */
403 Pgno pgno, /* Page number obtained */
404 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
405){
406 PgHdr *pPgHdr;
407 assert( pPage!=0 );
408 pPgHdr = (PgHdr*)pPage->pExtra;
409 assert( pPgHdr->pPage==0 );
drhd8c0ba32015-06-30 03:57:59 +0000410 memset(pPgHdr, 0, sizeof(PgHdr));
drhbc59ac02014-08-27 23:18:01 +0000411 pPgHdr->pPage = pPage;
412 pPgHdr->pData = pPage->pBuf;
413 pPgHdr->pExtra = (void *)&pPgHdr[1];
414 memset(pPgHdr->pExtra, 0, pCache->szExtra);
415 pPgHdr->pCache = pCache;
416 pPgHdr->pgno = pgno;
drhc78ae912015-06-29 04:21:15 +0000417 pPgHdr->flags = PGHDR_CLEAN;
drhbc59ac02014-08-27 23:18:01 +0000418 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
419}
420
421/*
422** This routine converts the sqlite3_pcache_page object returned by
423** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
424** must be called after sqlite3PcacheFetch() in order to get a usable
425** result.
426*/
427PgHdr *sqlite3PcacheFetchFinish(
428 PCache *pCache, /* Obtain the page from this cache */
429 Pgno pgno, /* Page number obtained */
430 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
431){
432 PgHdr *pPgHdr;
433
drhd8c0ba32015-06-30 03:57:59 +0000434 assert( pPage!=0 );
drhbc59ac02014-08-27 23:18:01 +0000435 pPgHdr = (PgHdr *)pPage->pExtra;
436
437 if( !pPgHdr->pPage ){
438 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
439 }
drh95a0b372015-09-03 20:43:55 +0000440 pCache->nRefSum++;
drhbc59ac02014-08-27 23:18:01 +0000441 pPgHdr->nRef++;
drhbc59ac02014-08-27 23:18:01 +0000442 return pPgHdr;
danielk19778c0a7912008-08-20 14:49:23 +0000443}
444
445/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000446** Decrement the reference count on a page. If the page is clean and the
peter.d.reid60ec9142014-09-06 16:39:46 +0000447** reference count drops to 0, then it is made eligible for recycling.
danielk19778c0a7912008-08-20 14:49:23 +0000448*/
drha8dcba92014-08-22 20:35:29 +0000449void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000450 assert( p->nRef>0 );
drh95a0b372015-09-03 20:43:55 +0000451 p->pCache->nRefSum--;
452 if( (--p->nRef)==0 ){
drhc78ae912015-06-29 04:21:15 +0000453 if( p->flags&PGHDR_CLEAN ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000454 pcacheUnpin(p);
dan82c04472016-05-12 17:06:04 +0000455 }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
456 /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
457 ** then page p is already at the head of the dirty list and the
458 ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
459 ** tag above. */
drha8dcba92014-08-22 20:35:29 +0000460 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
danielk1977d491e1b2008-08-26 18:05:48 +0000461 }
danielk1977502b7432008-08-25 14:49:42 +0000462 }
danielk19778c0a7912008-08-20 14:49:23 +0000463}
464
danielk1977bc2ca9e2008-11-13 14:28:28 +0000465/*
466** Increase the reference count of a supplied page by 1.
467*/
danielk19778c0a7912008-08-20 14:49:23 +0000468void sqlite3PcacheRef(PgHdr *p){
danielk1977502b7432008-08-25 14:49:42 +0000469 assert(p->nRef>0);
470 p->nRef++;
drh95a0b372015-09-03 20:43:55 +0000471 p->pCache->nRefSum++;
danielk19778c0a7912008-08-20 14:49:23 +0000472}
473
474/*
danielk19774abdfa42008-08-27 09:44:39 +0000475** Drop a page from the cache. There must be exactly one reference to the
476** page. This function deletes that reference, so after it returns the
477** page pointed to by p is invalid.
danielk19778c0a7912008-08-20 14:49:23 +0000478*/
479void sqlite3PcacheDrop(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000480 assert( p->nRef==1 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000481 if( p->flags&PGHDR_DIRTY ){
drha8dcba92014-08-22 20:35:29 +0000482 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
danielk1977bc2ca9e2008-11-13 14:28:28 +0000483 }
drh95a0b372015-09-03 20:43:55 +0000484 p->pCache->nRefSum--;
drha8dcba92014-08-22 20:35:29 +0000485 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
danielk19778c0a7912008-08-20 14:49:23 +0000486}
487
488/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000489** Make sure the page is marked as dirty. If it isn't dirty already,
danielk19778c0a7912008-08-20 14:49:23 +0000490** make it so.
491*/
492void sqlite3PcacheMakeDirty(PgHdr *p){
danielk19778c0a7912008-08-20 14:49:23 +0000493 assert( p->nRef>0 );
dan82c04472016-05-12 17:06:04 +0000494 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
drhc78ae912015-06-29 04:21:15 +0000495 p->flags &= ~PGHDR_DONT_WRITE;
496 if( p->flags & PGHDR_CLEAN ){
497 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
drh5c8e0922016-05-11 10:57:04 +0000498 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
drhc78ae912015-06-29 04:21:15 +0000499 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
500 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
501 }
danielk1977d491e1b2008-08-26 18:05:48 +0000502 }
danielk1977f599a192008-08-28 10:21:16 +0000503}
504
505/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000506** Make sure the page is marked as clean. If it isn't clean already,
danielk1977f599a192008-08-28 10:21:16 +0000507** make it so.
508*/
509void sqlite3PcacheMakeClean(PgHdr *p){
drh42bee5f2016-05-12 12:01:20 +0000510 if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
drhc78ae912015-06-29 04:21:15 +0000511 assert( (p->flags & PGHDR_CLEAN)==0 );
drha8dcba92014-08-22 20:35:29 +0000512 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
drh1aacbdb2015-06-29 18:29:10 +0000513 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
drhc78ae912015-06-29 04:21:15 +0000514 p->flags |= PGHDR_CLEAN;
drh5c8e0922016-05-11 10:57:04 +0000515 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000516 if( p->nRef==0 ){
517 pcacheUnpin(p);
518 }
danielk1977f599a192008-08-28 10:21:16 +0000519 }
danielk19778c0a7912008-08-20 14:49:23 +0000520}
521
522/*
523** Make every page in the cache clean.
524*/
525void sqlite3PcacheCleanAll(PCache *pCache){
526 PgHdr *p;
drh7aeb2162016-05-13 04:24:25 +0000527 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
danielk19778c0a7912008-08-20 14:49:23 +0000528 while( (p = pCache->pDirty)!=0 ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000529 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000530 }
531}
532
533/*
dan41113b62016-04-05 21:07:58 +0000534** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
535*/
536void sqlite3PcacheClearWritable(PCache *pCache){
537 PgHdr *p;
drh7aeb2162016-05-13 04:24:25 +0000538 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
dan41113b62016-04-05 21:07:58 +0000539 for(p=pCache->pDirty; p; p=p->pDirtyNext){
540 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
541 }
542 pCache->pSynced = pCache->pDirtyTail;
543}
544
545/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000546** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
547*/
548void sqlite3PcacheClearSyncFlags(PCache *pCache){
549 PgHdr *p;
550 for(p=pCache->pDirty; p; p=p->pDirtyNext){
551 p->flags &= ~PGHDR_NEED_SYNC;
552 }
553 pCache->pSynced = pCache->pDirtyTail;
554}
555
556/*
557** Change the page number of page p to newPgno.
danielk19778c0a7912008-08-20 14:49:23 +0000558*/
559void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000560 PCache *pCache = p->pCache;
danielk1977d491e1b2008-08-26 18:05:48 +0000561 assert( p->nRef>0 );
danielk1977bc2ca9e2008-11-13 14:28:28 +0000562 assert( newPgno>0 );
drh5c8e0922016-05-11 10:57:04 +0000563 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
dan22e21ff2011-11-08 20:08:44 +0000564 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
danielk19778c0a7912008-08-20 14:49:23 +0000565 p->pgno = newPgno;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000566 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
drha8dcba92014-08-22 20:35:29 +0000567 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
danielk19778c0a7912008-08-20 14:49:23 +0000568 }
danielk19778c0a7912008-08-20 14:49:23 +0000569}
570
571/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000572** Drop every cache entry whose page number is greater than "pgno". The
573** caller must ensure that there are no outstanding references to any pages
574** other than page 1 with a page number greater than pgno.
575**
576** If there is a reference to page 1 and the pgno parameter passed to this
577** function is 0, then the data area associated with page 1 is zeroed, but
578** the page object is not dropped.
danielk19778c0a7912008-08-20 14:49:23 +0000579*/
580void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000581 if( pCache->pCache ){
582 PgHdr *p;
583 PgHdr *pNext;
drh5c8e0922016-05-11 10:57:04 +0000584 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
danielk1977bc2ca9e2008-11-13 14:28:28 +0000585 for(p=pCache->pDirty; p; p=pNext){
586 pNext = p->pDirtyNext;
drhf3609ee2010-03-19 19:23:51 +0000587 /* This routine never gets call with a positive pgno except right
588 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
589 ** it must be that pgno==0.
590 */
591 assert( p->pgno>0 );
dan41113b62016-04-05 21:07:58 +0000592 if( p->pgno>pgno ){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000593 assert( p->flags&PGHDR_DIRTY );
594 sqlite3PcacheMakeClean(p);
danielk19778c0a7912008-08-20 14:49:23 +0000595 }
596 }
drh95a0b372015-09-03 20:43:55 +0000597 if( pgno==0 && pCache->nRefSum ){
drh39065c62015-06-26 02:41:31 +0000598 sqlite3_pcache_page *pPage1;
599 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
600 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
drh95a0b372015-09-03 20:43:55 +0000601 ** pCache->nRefSum>0 */
drh39065c62015-06-26 02:41:31 +0000602 memset(pPage1->pBuf, 0, pCache->szPage);
603 pgno = 1;
604 }
danielk1977bc2ca9e2008-11-13 14:28:28 +0000605 }
dan22e21ff2011-11-08 20:08:44 +0000606 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
danielk1977062d4cb2008-08-29 09:10:02 +0000607 }
608}
danielk19778c0a7912008-08-20 14:49:23 +0000609
610/*
611** Close a cache.
612*/
613void sqlite3PcacheClose(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000614 assert( pCache->pCache!=0 );
drh5c8e0922016-05-11 10:57:04 +0000615 pcacheTrace(("%p.CLOSE\n",pCache));
drhc3031c62014-08-26 15:06:49 +0000616 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000617}
618
619/*
620** Discard the contents of the cache.
621*/
danielk1977bea2a942009-01-20 17:06:27 +0000622void sqlite3PcacheClear(PCache *pCache){
danielk1977bc2ca9e2008-11-13 14:28:28 +0000623 sqlite3PcacheTruncate(pCache, 0);
danielk19778c0a7912008-08-20 14:49:23 +0000624}
625
626/*
627** Merge two lists of pages connected by pDirty and in pgno order.
danielk1977bc2ca9e2008-11-13 14:28:28 +0000628** Do not both fixing the pDirtyPrev pointers.
danielk19778c0a7912008-08-20 14:49:23 +0000629*/
630static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
631 PgHdr result, *pTail;
632 pTail = &result;
633 while( pA && pB ){
634 if( pA->pgno<pB->pgno ){
635 pTail->pDirty = pA;
636 pTail = pA;
637 pA = pA->pDirty;
638 }else{
639 pTail->pDirty = pB;
640 pTail = pB;
641 pB = pB->pDirty;
642 }
643 }
644 if( pA ){
645 pTail->pDirty = pA;
646 }else if( pB ){
647 pTail->pDirty = pB;
648 }else{
649 pTail->pDirty = 0;
650 }
651 return result.pDirty;
652}
653
654/*
655** Sort the list of pages in accending order by pgno. Pages are
danielk1977bc2ca9e2008-11-13 14:28:28 +0000656** connected by pDirty pointers. The pDirtyPrev pointers are
danielk19778c0a7912008-08-20 14:49:23 +0000657** corrupted by this sort.
drhe64ca7b2009-07-16 18:21:17 +0000658**
659** Since there cannot be more than 2^31 distinct pages in a database,
660** there cannot be more than 31 buckets required by the merge sorter.
661** One extra bucket is added to catch overflow in case something
662** ever changes to make the previous sentence incorrect.
danielk19778c0a7912008-08-20 14:49:23 +0000663*/
drhe64ca7b2009-07-16 18:21:17 +0000664#define N_SORT_BUCKET 32
danielk19778c0a7912008-08-20 14:49:23 +0000665static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
drhe64ca7b2009-07-16 18:21:17 +0000666 PgHdr *a[N_SORT_BUCKET], *p;
danielk19778c0a7912008-08-20 14:49:23 +0000667 int i;
668 memset(a, 0, sizeof(a));
669 while( pIn ){
670 p = pIn;
671 pIn = p->pDirty;
672 p->pDirty = 0;
drhe64ca7b2009-07-16 18:21:17 +0000673 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
danielk19778c0a7912008-08-20 14:49:23 +0000674 if( a[i]==0 ){
675 a[i] = p;
676 break;
677 }else{
678 p = pcacheMergeDirtyList(a[i], p);
679 a[i] = 0;
680 }
681 }
drhe64ca7b2009-07-16 18:21:17 +0000682 if( NEVER(i==N_SORT_BUCKET-1) ){
683 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
684 ** the input list. But that is impossible.
danielk19778c0a7912008-08-20 14:49:23 +0000685 */
686 a[i] = pcacheMergeDirtyList(a[i], p);
687 }
688 }
689 p = a[0];
690 for(i=1; i<N_SORT_BUCKET; i++){
691 p = pcacheMergeDirtyList(p, a[i]);
692 }
693 return p;
694}
695
696/*
697** Return a list of all dirty pages in the cache, sorted by page number.
698*/
699PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
700 PgHdr *p;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000701 for(p=pCache->pDirty; p; p=p->pDirtyNext){
702 p->pDirty = p->pDirtyNext;
danielk19778c0a7912008-08-20 14:49:23 +0000703 }
704 return pcacheSortDirtyList(pCache->pDirty);
705}
706
danielk19778c0a7912008-08-20 14:49:23 +0000707/*
drh95a0b372015-09-03 20:43:55 +0000708** Return the total number of references to all pages held by the cache.
709**
710** This is not the total number of pages referenced, but the sum of the
711** reference count for all pages.
danielk19778c0a7912008-08-20 14:49:23 +0000712*/
713int sqlite3PcacheRefCount(PCache *pCache){
drh95a0b372015-09-03 20:43:55 +0000714 return pCache->nRefSum;
danielk19778c0a7912008-08-20 14:49:23 +0000715}
716
danielk1977bc2ca9e2008-11-13 14:28:28 +0000717/*
718** Return the number of references to the page supplied as an argument.
719*/
danielk197771d5d2c2008-09-29 11:49:47 +0000720int sqlite3PcachePageRefcount(PgHdr *p){
721 return p->nRef;
722}
723
danielk19778c0a7912008-08-20 14:49:23 +0000724/*
725** Return the total number of pages in the cache.
726*/
727int sqlite3PcachePagecount(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000728 assert( pCache->pCache!=0 );
729 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000730}
731
danielk1977f3d3c272008-11-19 16:52:44 +0000732#ifdef SQLITE_TEST
danielk19778c0a7912008-08-20 14:49:23 +0000733/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000734** Get the suggested cache-size value.
danielk19778c0a7912008-08-20 14:49:23 +0000735*/
736int sqlite3PcacheGetCachesize(PCache *pCache){
drh3b42abb2011-11-09 14:23:04 +0000737 return numberOfCachePages(pCache);
danielk19778c0a7912008-08-20 14:49:23 +0000738}
danielk1977f3d3c272008-11-19 16:52:44 +0000739#endif
danielk19778c0a7912008-08-20 14:49:23 +0000740
741/*
742** Set the suggested cache-size value.
743*/
744void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
drhc3031c62014-08-26 15:06:49 +0000745 assert( pCache->pCache!=0 );
drh3b42abb2011-11-09 14:23:04 +0000746 pCache->szCache = mxPage;
drhc3031c62014-08-26 15:06:49 +0000747 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
748 numberOfCachePages(pCache));
danielk19778c0a7912008-08-20 14:49:23 +0000749}
750
drh09419b42011-11-16 19:29:17 +0000751/*
drh9b0cf342015-11-12 14:57:19 +0000752** Set the suggested cache-spill value. Make no changes if if the
753** argument is zero. Return the effective cache-spill size, which will
754** be the larger of the szSpill and szCache.
755*/
756int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
757 int res;
758 assert( p->pCache!=0 );
759 if( mxPage ){
760 if( mxPage<0 ){
drh4f9c8ec2015-11-12 15:47:48 +0000761 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
drh9b0cf342015-11-12 14:57:19 +0000762 }
763 p->szSpill = mxPage;
764 }
765 res = numberOfCachePages(p);
766 if( res<p->szSpill ) res = p->szSpill;
767 return res;
768}
769
770/*
drh09419b42011-11-16 19:29:17 +0000771** Free up as much memory as possible from the page cache.
772*/
773void sqlite3PcacheShrink(PCache *pCache){
drhc3031c62014-08-26 15:06:49 +0000774 assert( pCache->pCache!=0 );
775 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
drh09419b42011-11-16 19:29:17 +0000776}
777
drhdef68892014-11-04 12:11:23 +0000778/*
779** Return the size of the header added by this middleware layer
780** in the page-cache hierarchy.
781*/
drh37c057b2014-12-30 00:57:29 +0000782int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
drhdef68892014-11-04 12:11:23 +0000783
dan0f524552016-04-13 16:52:11 +0000784/*
785** Return the number of dirty pages currently in the cache, as a percentage
786** of the configured cache size.
787*/
788int sqlite3PCachePercentDirty(PCache *pCache){
789 PgHdr *pDirty;
790 int nDirty = 0;
791 int nCache = numberOfCachePages(pCache);
792 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
drhb5895e52016-04-18 13:30:50 +0000793 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
dan0f524552016-04-13 16:52:11 +0000794}
drhdef68892014-11-04 12:11:23 +0000795
danielk1977750e87d2009-07-25 11:46:48 +0000796#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
danielk197767e3da72008-08-21 12:19:44 +0000797/*
danielk1977bc2ca9e2008-11-13 14:28:28 +0000798** For all dirty pages currently in the cache, invoke the specified
799** callback. This is only used if the SQLITE_CHECK_PAGES macro is
800** defined.
danielk197767e3da72008-08-21 12:19:44 +0000801*/
danielk1977bc2ca9e2008-11-13 14:28:28 +0000802void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
803 PgHdr *pDirty;
804 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
805 xIter(pDirty);
danielk197767e3da72008-08-21 12:19:44 +0000806 }
danielk1977062d4cb2008-08-29 09:10:02 +0000807}
808#endif