xref: /sqlite-3.40.0/src/pcache1.c (revision 60176fa9)
1 /*
2 ** 2008 November 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 **
13 ** This file implements the default page cache implementation (the
14 ** sqlite3_pcache interface). It also contains part of the implementation
15 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
16 ** If the default page cache implementation is overriden, then neither of
17 ** these two features are available.
18 */
19 
20 #include "sqliteInt.h"
21 
22 typedef struct PCache1 PCache1;
23 typedef struct PgHdr1 PgHdr1;
24 typedef struct PgFreeslot PgFreeslot;
25 
26 /* Pointers to structures of this type are cast and returned as
27 ** opaque sqlite3_pcache* handles
28 */
29 struct PCache1 {
30   /* Cache configuration parameters. Page size (szPage) and the purgeable
31   ** flag (bPurgeable) are set when the cache is created. nMax may be
32   ** modified at any time by a call to the pcache1CacheSize() method.
33   ** The global mutex must be held when accessing nMax.
34   */
35   int szPage;                         /* Size of allocated pages in bytes */
36   int bPurgeable;                     /* True if cache is purgeable */
37   unsigned int nMin;                  /* Minimum number of pages reserved */
38   unsigned int nMax;                  /* Configured "cache_size" value */
39 
40   /* Hash table of all pages. The following variables may only be accessed
41   ** when the accessor is holding the global mutex (see pcache1EnterMutex()
42   ** and pcache1LeaveMutex()).
43   */
44   unsigned int nRecyclable;           /* Number of pages in the LRU list */
45   unsigned int nPage;                 /* Total number of pages in apHash */
46   unsigned int nHash;                 /* Number of slots in apHash[] */
47   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
48 
49   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
50 };
51 
52 /*
53 ** Each cache entry is represented by an instance of the following
54 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
55 ** directly before this structure in memory (see the PGHDR1_TO_PAGE()
56 ** macro below).
57 */
58 struct PgHdr1 {
59   unsigned int iKey;             /* Key value (page number) */
60   PgHdr1 *pNext;                 /* Next in hash table chain */
61   PCache1 *pCache;               /* Cache that currently owns this page */
62   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
63   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
64 };
65 
66 /*
67 ** Free slots in the allocator used to divide up the buffer provided using
68 ** the SQLITE_CONFIG_PAGECACHE mechanism.
69 */
70 struct PgFreeslot {
71   PgFreeslot *pNext;  /* Next free slot */
72 };
73 
74 /*
75 ** Global data used by this cache.
76 */
77 static SQLITE_WSD struct PCacheGlobal {
78   sqlite3_mutex *mutex;               /* static mutex MUTEX_STATIC_LRU */
79 
80   int nMaxPage;                       /* Sum of nMaxPage for purgeable caches */
81   int nMinPage;                       /* Sum of nMinPage for purgeable caches */
82   int nCurrentPage;                   /* Number of purgeable pages allocated */
83   PgHdr1 *pLruHead, *pLruTail;        /* LRU list of unpinned pages */
84 
85   /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
86   int szSlot;                         /* Size of each free slot */
87   void *pStart, *pEnd;                /* Bounds of pagecache malloc range */
88   PgFreeslot *pFree;                  /* Free page blocks */
89   int isInit;                         /* True if initialized */
90 } pcache1_g;
91 
92 /*
93 ** All code in this file should access the global structure above via the
94 ** alias "pcache1". This ensures that the WSD emulation is used when
95 ** compiling for systems that do not support real WSD.
96 */
97 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
98 
99 /*
100 ** When a PgHdr1 structure is allocated, the associated PCache1.szPage
101 ** bytes of data are located directly before it in memory (i.e. the total
102 ** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
103 ** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
104 ** an argument and returns a pointer to the associated block of szPage
105 ** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
106 ** a pointer to a block of szPage bytes of data and the return value is
107 ** a pointer to the associated PgHdr1 structure.
108 **
109 **   assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
110 */
111 #define PGHDR1_TO_PAGE(p)    (void*)(((char*)p) - p->pCache->szPage)
112 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
113 
114 /*
115 ** Macros to enter and leave the global LRU mutex.
116 */
117 #define pcache1EnterMutex() sqlite3_mutex_enter(pcache1.mutex)
118 #define pcache1LeaveMutex() sqlite3_mutex_leave(pcache1.mutex)
119 
120 /******************************************************************************/
121 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
122 
123 /*
124 ** This function is called during initialization if a static buffer is
125 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
126 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
127 ** enough to contain 'n' buffers of 'sz' bytes each.
128 */
129 void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
130   if( pcache1.isInit ){
131     PgFreeslot *p;
132     sz = ROUNDDOWN8(sz);
133     pcache1.szSlot = sz;
134     pcache1.pStart = pBuf;
135     pcache1.pFree = 0;
136     while( n-- ){
137       p = (PgFreeslot*)pBuf;
138       p->pNext = pcache1.pFree;
139       pcache1.pFree = p;
140       pBuf = (void*)&((char*)pBuf)[sz];
141     }
142     pcache1.pEnd = pBuf;
143   }
144 }
145 
146 /*
147 ** Malloc function used within this file to allocate space from the buffer
148 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
149 ** such buffer exists or there is no space left in it, this function falls
150 ** back to sqlite3Malloc().
151 */
152 static void *pcache1Alloc(int nByte){
153   void *p;
154   assert( sqlite3_mutex_held(pcache1.mutex) );
155   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
156   if( nByte<=pcache1.szSlot && pcache1.pFree ){
157     assert( pcache1.isInit );
158     p = (PgHdr1 *)pcache1.pFree;
159     pcache1.pFree = pcache1.pFree->pNext;
160     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
161   }else{
162 
163     /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
164     ** global pcache mutex and unlock the pager-cache object pCache. This is
165     ** so that if the attempt to allocate a new buffer causes the the
166     ** configured soft-heap-limit to be breached, it will be possible to
167     ** reclaim memory from this pager-cache.
168     */
169     pcache1LeaveMutex();
170     p = sqlite3Malloc(nByte);
171     pcache1EnterMutex();
172     if( p ){
173       int sz = sqlite3MallocSize(p);
174       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
175     }
176     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
177   }
178   return p;
179 }
180 
181 /*
182 ** Free an allocated buffer obtained from pcache1Alloc().
183 */
184 static void pcache1Free(void *p){
185   assert( sqlite3_mutex_held(pcache1.mutex) );
186   if( p==0 ) return;
187   if( p>=pcache1.pStart && p<pcache1.pEnd ){
188     PgFreeslot *pSlot;
189     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
190     pSlot = (PgFreeslot*)p;
191     pSlot->pNext = pcache1.pFree;
192     pcache1.pFree = pSlot;
193   }else{
194     int iSize;
195     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
196     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
197     iSize = sqlite3MallocSize(p);
198     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
199     sqlite3_free(p);
200   }
201 }
202 
203 /*
204 ** Allocate a new page object initially associated with cache pCache.
205 */
206 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
207   int nByte = sizeof(PgHdr1) + pCache->szPage;
208   void *pPg = pcache1Alloc(nByte);
209   PgHdr1 *p;
210   if( pPg ){
211     p = PAGE_TO_PGHDR1(pCache, pPg);
212     if( pCache->bPurgeable ){
213       pcache1.nCurrentPage++;
214     }
215   }else{
216     p = 0;
217   }
218   return p;
219 }
220 
221 /*
222 ** Free a page object allocated by pcache1AllocPage().
223 **
224 ** The pointer is allowed to be NULL, which is prudent.  But it turns out
225 ** that the current implementation happens to never call this routine
226 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
227 */
228 static void pcache1FreePage(PgHdr1 *p){
229   if( ALWAYS(p) ){
230     if( p->pCache->bPurgeable ){
231       pcache1.nCurrentPage--;
232     }
233     pcache1Free(PGHDR1_TO_PAGE(p));
234   }
235 }
236 
237 /*
238 ** Malloc function used by SQLite to obtain space from the buffer configured
239 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
240 ** exists, this function falls back to sqlite3Malloc().
241 */
242 void *sqlite3PageMalloc(int sz){
243   void *p;
244   pcache1EnterMutex();
245   p = pcache1Alloc(sz);
246   pcache1LeaveMutex();
247   return p;
248 }
249 
250 /*
251 ** Free an allocated buffer obtained from sqlite3PageMalloc().
252 */
253 void sqlite3PageFree(void *p){
254   pcache1EnterMutex();
255   pcache1Free(p);
256   pcache1LeaveMutex();
257 }
258 
259 /******************************************************************************/
260 /******** General Implementation Functions ************************************/
261 
262 /*
263 ** This function is used to resize the hash table used by the cache passed
264 ** as the first argument.
265 **
266 ** The global mutex must be held when this function is called.
267 */
268 static int pcache1ResizeHash(PCache1 *p){
269   PgHdr1 **apNew;
270   unsigned int nNew;
271   unsigned int i;
272 
273   assert( sqlite3_mutex_held(pcache1.mutex) );
274 
275   nNew = p->nHash*2;
276   if( nNew<256 ){
277     nNew = 256;
278   }
279 
280   pcache1LeaveMutex();
281   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
282   apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
283   if( p->nHash ){ sqlite3EndBenignMalloc(); }
284   pcache1EnterMutex();
285   if( apNew ){
286     memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
287     for(i=0; i<p->nHash; i++){
288       PgHdr1 *pPage;
289       PgHdr1 *pNext = p->apHash[i];
290       while( (pPage = pNext)!=0 ){
291         unsigned int h = pPage->iKey % nNew;
292         pNext = pPage->pNext;
293         pPage->pNext = apNew[h];
294         apNew[h] = pPage;
295       }
296     }
297     sqlite3_free(p->apHash);
298     p->apHash = apNew;
299     p->nHash = nNew;
300   }
301 
302   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
303 }
304 
305 /*
306 ** This function is used internally to remove the page pPage from the
307 ** global LRU list, if is part of it. If pPage is not part of the global
308 ** LRU list, then this function is a no-op.
309 **
310 ** The global mutex must be held when this function is called.
311 */
312 static void pcache1PinPage(PgHdr1 *pPage){
313   assert( sqlite3_mutex_held(pcache1.mutex) );
314   if( pPage && (pPage->pLruNext || pPage==pcache1.pLruTail) ){
315     if( pPage->pLruPrev ){
316       pPage->pLruPrev->pLruNext = pPage->pLruNext;
317     }
318     if( pPage->pLruNext ){
319       pPage->pLruNext->pLruPrev = pPage->pLruPrev;
320     }
321     if( pcache1.pLruHead==pPage ){
322       pcache1.pLruHead = pPage->pLruNext;
323     }
324     if( pcache1.pLruTail==pPage ){
325       pcache1.pLruTail = pPage->pLruPrev;
326     }
327     pPage->pLruNext = 0;
328     pPage->pLruPrev = 0;
329     pPage->pCache->nRecyclable--;
330   }
331 }
332 
333 
334 /*
335 ** Remove the page supplied as an argument from the hash table
336 ** (PCache1.apHash structure) that it is currently stored in.
337 **
338 ** The global mutex must be held when this function is called.
339 */
340 static void pcache1RemoveFromHash(PgHdr1 *pPage){
341   unsigned int h;
342   PCache1 *pCache = pPage->pCache;
343   PgHdr1 **pp;
344 
345   h = pPage->iKey % pCache->nHash;
346   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
347   *pp = (*pp)->pNext;
348 
349   pCache->nPage--;
350 }
351 
352 /*
353 ** If there are currently more than pcache.nMaxPage pages allocated, try
354 ** to recycle pages to reduce the number allocated to pcache.nMaxPage.
355 */
356 static void pcache1EnforceMaxPage(void){
357   assert( sqlite3_mutex_held(pcache1.mutex) );
358   while( pcache1.nCurrentPage>pcache1.nMaxPage && pcache1.pLruTail ){
359     PgHdr1 *p = pcache1.pLruTail;
360     pcache1PinPage(p);
361     pcache1RemoveFromHash(p);
362     pcache1FreePage(p);
363   }
364 }
365 
366 /*
367 ** Discard all pages from cache pCache with a page number (key value)
368 ** greater than or equal to iLimit. Any pinned pages that meet this
369 ** criteria are unpinned before they are discarded.
370 **
371 ** The global mutex must be held when this function is called.
372 */
373 static void pcache1TruncateUnsafe(
374   PCache1 *pCache,
375   unsigned int iLimit
376 ){
377   TESTONLY( unsigned int nPage = 0; )      /* Used to assert pCache->nPage is correct */
378   unsigned int h;
379   assert( sqlite3_mutex_held(pcache1.mutex) );
380   for(h=0; h<pCache->nHash; h++){
381     PgHdr1 **pp = &pCache->apHash[h];
382     PgHdr1 *pPage;
383     while( (pPage = *pp)!=0 ){
384       if( pPage->iKey>=iLimit ){
385         pCache->nPage--;
386         *pp = pPage->pNext;
387         pcache1PinPage(pPage);
388         pcache1FreePage(pPage);
389       }else{
390         pp = &pPage->pNext;
391         TESTONLY( nPage++; )
392       }
393     }
394   }
395   assert( pCache->nPage==nPage );
396 }
397 
398 /******************************************************************************/
399 /******** sqlite3_pcache Methods **********************************************/
400 
401 /*
402 ** Implementation of the sqlite3_pcache.xInit method.
403 */
404 static int pcache1Init(void *NotUsed){
405   UNUSED_PARAMETER(NotUsed);
406   assert( pcache1.isInit==0 );
407   memset(&pcache1, 0, sizeof(pcache1));
408   if( sqlite3GlobalConfig.bCoreMutex ){
409     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
410   }
411   pcache1.isInit = 1;
412   return SQLITE_OK;
413 }
414 
415 /*
416 ** Implementation of the sqlite3_pcache.xShutdown method.
417 ** Note that the static mutex allocated in xInit does
418 ** not need to be freed.
419 */
420 static void pcache1Shutdown(void *NotUsed){
421   UNUSED_PARAMETER(NotUsed);
422   assert( pcache1.isInit!=0 );
423   memset(&pcache1, 0, sizeof(pcache1));
424 }
425 
426 /*
427 ** Implementation of the sqlite3_pcache.xCreate method.
428 **
429 ** Allocate a new cache.
430 */
431 static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
432   PCache1 *pCache;
433 
434   pCache = (PCache1 *)sqlite3_malloc(sizeof(PCache1));
435   if( pCache ){
436     memset(pCache, 0, sizeof(PCache1));
437     pCache->szPage = szPage;
438     pCache->bPurgeable = (bPurgeable ? 1 : 0);
439     if( bPurgeable ){
440       pCache->nMin = 10;
441       pcache1EnterMutex();
442       pcache1.nMinPage += pCache->nMin;
443       pcache1LeaveMutex();
444     }
445   }
446   return (sqlite3_pcache *)pCache;
447 }
448 
449 /*
450 ** Implementation of the sqlite3_pcache.xCachesize method.
451 **
452 ** Configure the cache_size limit for a cache.
453 */
454 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
455   PCache1 *pCache = (PCache1 *)p;
456   if( pCache->bPurgeable ){
457     pcache1EnterMutex();
458     pcache1.nMaxPage += (nMax - pCache->nMax);
459     pCache->nMax = nMax;
460     pcache1EnforceMaxPage();
461     pcache1LeaveMutex();
462   }
463 }
464 
465 /*
466 ** Implementation of the sqlite3_pcache.xPagecount method.
467 */
468 static int pcache1Pagecount(sqlite3_pcache *p){
469   int n;
470   pcache1EnterMutex();
471   n = ((PCache1 *)p)->nPage;
472   pcache1LeaveMutex();
473   return n;
474 }
475 
476 /*
477 ** Implementation of the sqlite3_pcache.xFetch method.
478 **
479 ** Fetch a page by key value.
480 **
481 ** Whether or not a new page may be allocated by this function depends on
482 ** the value of the createFlag argument.  0 means do not allocate a new
483 ** page.  1 means allocate a new page if space is easily available.  2
484 ** means to try really hard to allocate a new page.
485 **
486 ** For a non-purgeable cache (a cache used as the storage for an in-memory
487 ** database) there is really no difference between createFlag 1 and 2.  So
488 ** the calling function (pcache.c) will never have a createFlag of 1 on
489 ** a non-purgable cache.
490 **
491 ** There are three different approaches to obtaining space for a page,
492 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
493 **
494 **   1. Regardless of the value of createFlag, the cache is searched for a
495 **      copy of the requested page. If one is found, it is returned.
496 **
497 **   2. If createFlag==0 and the page is not already in the cache, NULL is
498 **      returned.
499 **
500 **   3. If createFlag is 1, and the page is not already in the cache,
501 **      and if either of the following are true, return NULL:
502 **
503 **       (a) the number of pages pinned by the cache is greater than
504 **           PCache1.nMax, or
505 **       (b) the number of pages pinned by the cache is greater than
506 **           the sum of nMax for all purgeable caches, less the sum of
507 **           nMin for all other purgeable caches.
508 **
509 **   4. If none of the first three conditions apply and the cache is marked
510 **      as purgeable, and if one of the following is true:
511 **
512 **       (a) The number of pages allocated for the cache is already
513 **           PCache1.nMax, or
514 **
515 **       (b) The number of pages allocated for all purgeable caches is
516 **           already equal to or greater than the sum of nMax for all
517 **           purgeable caches,
518 **
519 **      then attempt to recycle a page from the LRU list. If it is the right
520 **      size, return the recycled buffer. Otherwise, free the buffer and
521 **      proceed to step 5.
522 **
523 **   5. Otherwise, allocate and return a new page buffer.
524 */
525 static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
526   unsigned int nPinned;
527   PCache1 *pCache = (PCache1 *)p;
528   PgHdr1 *pPage = 0;
529 
530   assert( pCache->bPurgeable || createFlag!=1 );
531   pcache1EnterMutex();
532   if( createFlag==1 ) sqlite3BeginBenignMalloc();
533 
534   /* Search the hash table for an existing entry. */
535   if( pCache->nHash>0 ){
536     unsigned int h = iKey % pCache->nHash;
537     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
538   }
539 
540   if( pPage || createFlag==0 ){
541     pcache1PinPage(pPage);
542     goto fetch_out;
543   }
544 
545   /* Step 3 of header comment. */
546   nPinned = pCache->nPage - pCache->nRecyclable;
547   if( createFlag==1 && (
548         nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
549      || nPinned>=(pCache->nMax * 9 / 10)
550   )){
551     goto fetch_out;
552   }
553 
554   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
555     goto fetch_out;
556   }
557 
558   /* Step 4. Try to recycle a page buffer if appropriate. */
559   if( pCache->bPurgeable && pcache1.pLruTail && (
560      (pCache->nPage+1>=pCache->nMax) || pcache1.nCurrentPage>=pcache1.nMaxPage
561   )){
562     pPage = pcache1.pLruTail;
563     pcache1RemoveFromHash(pPage);
564     pcache1PinPage(pPage);
565     if( pPage->pCache->szPage!=pCache->szPage ){
566       pcache1FreePage(pPage);
567       pPage = 0;
568     }else{
569       pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable);
570     }
571   }
572 
573   /* Step 5. If a usable page buffer has still not been found,
574   ** attempt to allocate a new one.
575   */
576   if( !pPage ){
577     pPage = pcache1AllocPage(pCache);
578   }
579 
580   if( pPage ){
581     unsigned int h = iKey % pCache->nHash;
582     pCache->nPage++;
583     pPage->iKey = iKey;
584     pPage->pNext = pCache->apHash[h];
585     pPage->pCache = pCache;
586     pPage->pLruPrev = 0;
587     pPage->pLruNext = 0;
588     *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
589     pCache->apHash[h] = pPage;
590   }
591 
592 fetch_out:
593   if( pPage && iKey>pCache->iMaxKey ){
594     pCache->iMaxKey = iKey;
595   }
596   if( createFlag==1 ) sqlite3EndBenignMalloc();
597   pcache1LeaveMutex();
598   return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
599 }
600 
601 
602 /*
603 ** Implementation of the sqlite3_pcache.xUnpin method.
604 **
605 ** Mark a page as unpinned (eligible for asynchronous recycling).
606 */
607 static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
608   PCache1 *pCache = (PCache1 *)p;
609   PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
610 
611   assert( pPage->pCache==pCache );
612   pcache1EnterMutex();
613 
614   /* It is an error to call this function if the page is already
615   ** part of the global LRU list.
616   */
617   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
618   assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage );
619 
620   if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){
621     pcache1RemoveFromHash(pPage);
622     pcache1FreePage(pPage);
623   }else{
624     /* Add the page to the global LRU list. Normally, the page is added to
625     ** the head of the list (last page to be recycled). However, if the
626     ** reuseUnlikely flag passed to this function is true, the page is added
627     ** to the tail of the list (first page to be recycled).
628     */
629     if( pcache1.pLruHead ){
630       pcache1.pLruHead->pLruPrev = pPage;
631       pPage->pLruNext = pcache1.pLruHead;
632       pcache1.pLruHead = pPage;
633     }else{
634       pcache1.pLruTail = pPage;
635       pcache1.pLruHead = pPage;
636     }
637     pCache->nRecyclable++;
638   }
639 
640   pcache1LeaveMutex();
641 }
642 
643 /*
644 ** Implementation of the sqlite3_pcache.xRekey method.
645 */
646 static void pcache1Rekey(
647   sqlite3_pcache *p,
648   void *pPg,
649   unsigned int iOld,
650   unsigned int iNew
651 ){
652   PCache1 *pCache = (PCache1 *)p;
653   PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
654   PgHdr1 **pp;
655   unsigned int h;
656   assert( pPage->iKey==iOld );
657   assert( pPage->pCache==pCache );
658 
659   pcache1EnterMutex();
660 
661   h = iOld%pCache->nHash;
662   pp = &pCache->apHash[h];
663   while( (*pp)!=pPage ){
664     pp = &(*pp)->pNext;
665   }
666   *pp = pPage->pNext;
667 
668   h = iNew%pCache->nHash;
669   pPage->iKey = iNew;
670   pPage->pNext = pCache->apHash[h];
671   pCache->apHash[h] = pPage;
672   if( iNew>pCache->iMaxKey ){
673     pCache->iMaxKey = iNew;
674   }
675 
676   pcache1LeaveMutex();
677 }
678 
679 /*
680 ** Implementation of the sqlite3_pcache.xTruncate method.
681 **
682 ** Discard all unpinned pages in the cache with a page number equal to
683 ** or greater than parameter iLimit. Any pinned pages with a page number
684 ** equal to or greater than iLimit are implicitly unpinned.
685 */
686 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
687   PCache1 *pCache = (PCache1 *)p;
688   pcache1EnterMutex();
689   if( iLimit<=pCache->iMaxKey ){
690     pcache1TruncateUnsafe(pCache, iLimit);
691     pCache->iMaxKey = iLimit-1;
692   }
693   pcache1LeaveMutex();
694 }
695 
696 /*
697 ** Implementation of the sqlite3_pcache.xDestroy method.
698 **
699 ** Destroy a cache allocated using pcache1Create().
700 */
701 static void pcache1Destroy(sqlite3_pcache *p){
702   PCache1 *pCache = (PCache1 *)p;
703   pcache1EnterMutex();
704   pcache1TruncateUnsafe(pCache, 0);
705   pcache1.nMaxPage -= pCache->nMax;
706   pcache1.nMinPage -= pCache->nMin;
707   pcache1EnforceMaxPage();
708   pcache1LeaveMutex();
709   sqlite3_free(pCache->apHash);
710   sqlite3_free(pCache);
711 }
712 
713 /*
714 ** This function is called during initialization (sqlite3_initialize()) to
715 ** install the default pluggable cache module, assuming the user has not
716 ** already provided an alternative.
717 */
718 void sqlite3PCacheSetDefault(void){
719   static const sqlite3_pcache_methods defaultMethods = {
720     0,                       /* pArg */
721     pcache1Init,             /* xInit */
722     pcache1Shutdown,         /* xShutdown */
723     pcache1Create,           /* xCreate */
724     pcache1Cachesize,        /* xCachesize */
725     pcache1Pagecount,        /* xPagecount */
726     pcache1Fetch,            /* xFetch */
727     pcache1Unpin,            /* xUnpin */
728     pcache1Rekey,            /* xRekey */
729     pcache1Truncate,         /* xTruncate */
730     pcache1Destroy           /* xDestroy */
731   };
732   sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
733 }
734 
735 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
736 /*
737 ** This function is called to free superfluous dynamically allocated memory
738 ** held by the pager system. Memory in use by any SQLite pager allocated
739 ** by the current thread may be sqlite3_free()ed.
740 **
741 ** nReq is the number of bytes of memory required. Once this much has
742 ** been released, the function returns. The return value is the total number
743 ** of bytes of memory released.
744 */
745 int sqlite3PcacheReleaseMemory(int nReq){
746   int nFree = 0;
747   if( pcache1.pStart==0 ){
748     PgHdr1 *p;
749     pcache1EnterMutex();
750     while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){
751       nFree += sqlite3MallocSize(PGHDR1_TO_PAGE(p));
752       pcache1PinPage(p);
753       pcache1RemoveFromHash(p);
754       pcache1FreePage(p);
755     }
756     pcache1LeaveMutex();
757   }
758   return nFree;
759 }
760 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
761 
762 #ifdef SQLITE_TEST
763 /*
764 ** This function is used by test procedures to inspect the internal state
765 ** of the global cache.
766 */
767 void sqlite3PcacheStats(
768   int *pnCurrent,      /* OUT: Total number of pages cached */
769   int *pnMax,          /* OUT: Global maximum cache size */
770   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
771   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
772 ){
773   PgHdr1 *p;
774   int nRecyclable = 0;
775   for(p=pcache1.pLruHead; p; p=p->pLruNext){
776     nRecyclable++;
777   }
778   *pnCurrent = pcache1.nCurrentPage;
779   *pnMax = pcache1.nMaxPage;
780   *pnMin = pcache1.nMinPage;
781   *pnRecyclable = nRecyclable;
782 }
783 #endif
784