xref: /sqlite-3.40.0/src/pcache1.c (revision a002cc17)
1bc2ca9ebSdanielk1977 /*
2bc2ca9ebSdanielk1977 ** 2008 November 05
3bc2ca9ebSdanielk1977 **
4bc2ca9ebSdanielk1977 ** The author disclaims copyright to this source code.  In place of
5bc2ca9ebSdanielk1977 ** a legal notice, here is a blessing:
6bc2ca9ebSdanielk1977 **
7bc2ca9ebSdanielk1977 **    May you do good and not evil.
8bc2ca9ebSdanielk1977 **    May you find forgiveness for yourself and forgive others.
9bc2ca9ebSdanielk1977 **    May you share freely, never taking more than you give.
10bc2ca9ebSdanielk1977 **
11bc2ca9ebSdanielk1977 *************************************************************************
12bc2ca9ebSdanielk1977 **
13bc2ca9ebSdanielk1977 ** This file implements the default page cache implementation (the
14bc2ca9ebSdanielk1977 ** sqlite3_pcache interface). It also contains part of the implementation
15bc2ca9ebSdanielk1977 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
1660ec914cSpeter.d.reid ** If the default page cache implementation is overridden, then neither of
17bc2ca9ebSdanielk1977 ** these two features are available.
1801c5c00cSdrh **
1901c5c00cSdrh ** A Page cache line looks like this:
2001c5c00cSdrh **
2101c5c00cSdrh **  -------------------------------------------------------------
2201c5c00cSdrh **  |  database page content   |  PgHdr1  |  MemPage  |  PgHdr  |
2301c5c00cSdrh **  -------------------------------------------------------------
2401c5c00cSdrh **
2501c5c00cSdrh ** The database page content is up front (so that buffer overreads tend to
2601c5c00cSdrh ** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions).   MemPage
2701c5c00cSdrh ** is the extension added by the btree.c module containing information such
2801c5c00cSdrh ** as the database page number and how that database page is used.  PgHdr
2901c5c00cSdrh ** is added by the pcache.c layer and contains information used to keep track
3001c5c00cSdrh ** of which pages are "dirty".  PgHdr1 is an extension added by this
3101c5c00cSdrh ** module (pcache1.c).  The PgHdr1 header is a subclass of sqlite3_pcache_page.
3201c5c00cSdrh ** PgHdr1 contains information needed to look up a page by its page number.
3301c5c00cSdrh ** The superclass sqlite3_pcache_page.pBuf points to the start of the
3401c5c00cSdrh ** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
3501c5c00cSdrh **
3601c5c00cSdrh ** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
3701c5c00cSdrh ** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size).  The
3801c5c00cSdrh ** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
3901c5c00cSdrh ** size can vary according to architecture, compile-time options, and
4001c5c00cSdrh ** SQLite library version number.
4101c5c00cSdrh **
42c8e9f681Sdrh ** Historical note:  It used to be that if the SQLITE_PCACHE_SEPARATE_HEADER
43c8e9f681Sdrh ** was defined, then the page content would be held in a separate memory
44c8e9f681Sdrh ** allocation from the PgHdr1.  This was intended to avoid clownshoe memory
45c8e9f681Sdrh ** allocations.  However, the btree layer needs a small (16-byte) overrun
46c8e9f681Sdrh ** area after the page content buffer.  The header serves as that overrun
47c8e9f681Sdrh ** area.  Therefore SQLITE_PCACHE_SEPARATE_HEADER was discontinued to avoid
48c8e9f681Sdrh ** any possibility of a memory error.
4901c5c00cSdrh **
5001c5c00cSdrh ** This module tracks pointers to PgHdr1 objects.  Only pcache.c communicates
5101c5c00cSdrh ** with this module.  Information is passed back and forth as PgHdr1 pointers.
5201c5c00cSdrh **
5301c5c00cSdrh ** The pcache.c and pager.c modules deal pointers to PgHdr objects.
5401c5c00cSdrh ** The btree.c module deals with pointers to MemPage objects.
55ee70a84eSdrh **
56ee70a84eSdrh ** SOURCE OF PAGE CACHE MEMORY:
57ee70a84eSdrh **
58ee70a84eSdrh ** Memory for a page might come from any of three sources:
59ee70a84eSdrh **
60ee70a84eSdrh **    (1)  The general-purpose memory allocator - sqlite3Malloc()
61ee70a84eSdrh **    (2)  Global page-cache memory provided using sqlite3_config() with
62ee70a84eSdrh **         SQLITE_CONFIG_PAGECACHE.
63ee70a84eSdrh **    (3)  PCache-local bulk allocation.
64ee70a84eSdrh **
65ee70a84eSdrh ** The third case is a chunk of heap memory (defaulting to 100 pages worth)
66ee70a84eSdrh ** that is allocated when the page cache is created.  The size of the local
67ee70a84eSdrh ** bulk allocation can be adjusted using
68ee70a84eSdrh **
69a6082f69Sdrh **     sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
70ee70a84eSdrh **
71ee70a84eSdrh ** If N is positive, then N pages worth of memory are allocated using a single
72ee70a84eSdrh ** sqlite3Malloc() call and that memory is used for the first N pages allocated.
73ee70a84eSdrh ** Or if N is negative, then -1024*N bytes of memory are allocated and used
74ee70a84eSdrh ** for as many pages as can be accomodated.
75ee70a84eSdrh **
76ee70a84eSdrh ** Only one of (2) or (3) can be used.  Once the memory available to (2) or
77ee70a84eSdrh ** (3) is exhausted, subsequent allocations fail over to the general-purpose
78ee70a84eSdrh ** memory allocator (1).
79ee70a84eSdrh **
80ee70a84eSdrh ** Earlier versions of SQLite used only methods (1) and (2).  But experiments
81ee70a84eSdrh ** show that method (3) with N==100 provides about a 5% performance boost for
82ee70a84eSdrh ** common workloads.
83bc2ca9ebSdanielk1977 */
84bc2ca9ebSdanielk1977 #include "sqliteInt.h"
85bc2ca9ebSdanielk1977 
86bc2ca9ebSdanielk1977 typedef struct PCache1 PCache1;
87bc2ca9ebSdanielk1977 typedef struct PgHdr1 PgHdr1;
88bc2ca9ebSdanielk1977 typedef struct PgFreeslot PgFreeslot;
899f8cf9daSdrh typedef struct PGroup PGroup;
909f8cf9daSdrh 
9192af02c9Sdrh /*
9292af02c9Sdrh ** Each cache entry is represented by an instance of the following
93c8e9f681Sdrh ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
94c8e9f681Sdrh ** directly before this structure and is used to cache the page content.
95e385d33aSdan **
96c8e9f681Sdrh ** When reading a corrupt database file, it is possible that SQLite might
97c8e9f681Sdrh ** read a few bytes (no more than 16 bytes) past the end of the page buffer.
98c8e9f681Sdrh ** It will only read past the end of the page buffer, never write.  This
99c8e9f681Sdrh ** object is positioned immediately after the page buffer to serve as an
100c8e9f681Sdrh ** overrun area, so that overreads are harmless.
101c8e9f681Sdrh **
102c8e9f681Sdrh ** Variables isBulkLocal and isAnchor were once type "u8". That works,
103e385d33aSdan ** but causes a 2-byte gap in the structure for most architectures (since
104e385d33aSdan ** pointers must be either 4 or 8-byte aligned). As this structure is located
105e385d33aSdan ** in memory directly after the associated page data, if the database is
106e385d33aSdan ** corrupt, code at the b-tree layer may overread the page buffer and
107e385d33aSdan ** read part of this structure before the corruption is detected. This
108e385d33aSdan ** can cause a valgrind error if the unitialized gap is accessed. Using u16
10978d15f09Sdrh ** ensures there is no such gap, and therefore no bytes of uninitialized
11078d15f09Sdrh ** memory in the structure.
11178d15f09Sdrh **
11278d15f09Sdrh ** The pLruNext and pLruPrev pointers form a double-linked circular list
11378d15f09Sdrh ** of all pages that are unpinned.  The PGroup.lru element (which should be
11478d15f09Sdrh ** the only element on the list with PgHdr1.isAnchor set to 1) forms the
11578d15f09Sdrh ** beginning and the end of the list.
11692af02c9Sdrh */
11792af02c9Sdrh struct PgHdr1 {
11892af02c9Sdrh   sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
11992af02c9Sdrh   unsigned int iKey;        /* Key value (page number) */
120e385d33aSdan   u16 isBulkLocal;          /* This page from bulk local storage */
121e385d33aSdan   u16 isAnchor;             /* This is the PGroup.lru element */
12292af02c9Sdrh   PgHdr1 *pNext;            /* Next in hash table chain */
12392af02c9Sdrh   PCache1 *pCache;          /* Cache that currently owns this page */
12478d15f09Sdrh   PgHdr1 *pLruNext;         /* Next in circular LRU list of unpinned pages */
12592af02c9Sdrh   PgHdr1 *pLruPrev;         /* Previous in LRU list of unpinned pages */
126de72d2a8Sdrh                             /* NB: pLruPrev is only valid if pLruNext!=0 */
12792af02c9Sdrh };
12892af02c9Sdrh 
129eabb67fbSdrh /*
13026505e5fSdrh ** A page is pinned if it is not on the LRU list.  To be "pinned" means
13126505e5fSdrh ** that the page is in active use and must not be deallocated.
132eabb67fbSdrh */
133eabb67fbSdrh #define PAGE_IS_PINNED(p)    ((p)->pLruNext==0)
134eabb67fbSdrh #define PAGE_IS_UNPINNED(p)  ((p)->pLruNext!=0)
135eabb67fbSdrh 
1369f8cf9daSdrh /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set
13760ec914cSpeter.d.reid ** of one or more PCaches that are able to recycle each other's unpinned
1389f8cf9daSdrh ** pages when they are under memory pressure.  A PGroup is an instance of
1399f8cf9daSdrh ** the following object.
1409f8cf9daSdrh **
1419f8cf9daSdrh ** This page cache implementation works in one of two modes:
1429f8cf9daSdrh **
1439f8cf9daSdrh **   (1)  Every PCache is the sole member of its own PGroup.  There is
1449f8cf9daSdrh **        one PGroup per PCache.
1459f8cf9daSdrh **
1469f8cf9daSdrh **   (2)  There is a single global PGroup that all PCaches are a member
1479f8cf9daSdrh **        of.
1489f8cf9daSdrh **
1499f8cf9daSdrh ** Mode 1 uses more memory (since PCache instances are not able to rob
1509f8cf9daSdrh ** unused pages from other PCaches) but it also operates without a mutex,
1519f8cf9daSdrh ** and is therefore often faster.  Mode 2 requires a mutex in order to be
15245d29309Sdrh ** threadsafe, but recycles pages more efficiently.
1539f8cf9daSdrh **
1549f8cf9daSdrh ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
1559f8cf9daSdrh ** PGroup which is the pcache1.grp global variable and its mutex is
1569f8cf9daSdrh ** SQLITE_MUTEX_STATIC_LRU.
1579f8cf9daSdrh */
1589f8cf9daSdrh struct PGroup {
1599f8cf9daSdrh   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
160a69085cfSdrh   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
161a69085cfSdrh   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
162a69085cfSdrh   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
163617b7b42Sdrh   unsigned int nPurgeable;       /* Number of purgeable pages allocated */
16492af02c9Sdrh   PgHdr1 lru;                    /* The beginning and end of the LRU list */
1659f8cf9daSdrh };
166bc2ca9ebSdanielk1977 
1679d13f116Sdrh /* Each page cache is an instance of the following object.  Every
1689d13f116Sdrh ** open database file (including each in-memory database and each
1699d13f116Sdrh ** temporary or transient database) has a single page cache which
1709d13f116Sdrh ** is an instance of this object.
1719d13f116Sdrh **
1729d13f116Sdrh ** Pointers to structures of this type are cast and returned as
1739d13f116Sdrh ** opaque sqlite3_pcache* handles.
174bc2ca9ebSdanielk1977 */
175bc2ca9ebSdanielk1977 struct PCache1 {
176bc2ca9ebSdanielk1977   /* Cache configuration parameters. Page size (szPage) and the purgeable
177617b7b42Sdrh   ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
178617b7b42Sdrh   ** cache is created and are never changed thereafter. nMax may be
17945d29309Sdrh   ** modified at any time by a call to the pcache1Cachesize() method.
1809f8cf9daSdrh   ** The PGroup mutex must be held when accessing nMax.
181bc2ca9ebSdanielk1977   */
1829f8cf9daSdrh   PGroup *pGroup;                     /* PGroup this cache belongs to */
183617b7b42Sdrh   unsigned int *pnPurgeable;          /* Pointer to pGroup->nPurgeable */
184ee70a84eSdrh   int szPage;                         /* Size of database content section */
185ee70a84eSdrh   int szExtra;                        /* sizeof(MemPage)+sizeof(PgHdr) */
186ee70a84eSdrh   int szAlloc;                        /* Total size of one pcache line */
187bc2ca9ebSdanielk1977   int bPurgeable;                     /* True if cache is purgeable */
18844cd45c8Sdanielk1977   unsigned int nMin;                  /* Minimum number of pages reserved */
18944cd45c8Sdanielk1977   unsigned int nMax;                  /* Configured "cache_size" value */
19025ca5682Sdrh   unsigned int n90pct;                /* nMax*9/10 */
1912cbd78b7Sdrh   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
1921757fed8Sdrh   unsigned int nPurgeableDummy;       /* pnPurgeable points here when not used*/
193bc2ca9ebSdanielk1977 
194bc2ca9ebSdanielk1977   /* Hash table of all pages. The following variables may only be accessed
1959f8cf9daSdrh   ** when the accessor is holding the PGroup mutex.
196bc2ca9ebSdanielk1977   */
19744cd45c8Sdanielk1977   unsigned int nRecyclable;           /* Number of pages in the LRU list */
19844cd45c8Sdanielk1977   unsigned int nPage;                 /* Total number of pages in apHash */
19944cd45c8Sdanielk1977   unsigned int nHash;                 /* Number of slots in apHash[] */
200bc2ca9ebSdanielk1977   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
201ee70a84eSdrh   PgHdr1 *pFree;                      /* List of unused pcache-local pages */
202ee70a84eSdrh   void *pBulk;                        /* Bulk memory used by pcache-local */
203bc2ca9ebSdanielk1977 };
204bc2ca9ebSdanielk1977 
205bc2ca9ebSdanielk1977 /*
206ee70a84eSdrh ** Free slots in the allocator used to divide up the global page cache
207ee70a84eSdrh ** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
208bc2ca9ebSdanielk1977 */
209bc2ca9ebSdanielk1977 struct PgFreeslot {
210bc2ca9ebSdanielk1977   PgFreeslot *pNext;  /* Next free slot */
211bc2ca9ebSdanielk1977 };
212bc2ca9ebSdanielk1977 
213bc2ca9ebSdanielk1977 /*
214bc2ca9ebSdanielk1977 ** Global data used by this cache.
215bc2ca9ebSdanielk1977 */
216bc2ca9ebSdanielk1977 static SQLITE_WSD struct PCacheGlobal {
2179f8cf9daSdrh   PGroup grp;                    /* The global PGroup for mode (2) */
218bc2ca9ebSdanielk1977 
2199f8cf9daSdrh   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
2209f8cf9daSdrh   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
2219f8cf9daSdrh   ** fixed at sqlite3_initialize() time and do not require mutex protection.
2229f8cf9daSdrh   ** The nFreeSlot and pFree values do require mutex protection.
2239f8cf9daSdrh   */
2249f8cf9daSdrh   int isInit;                    /* True if initialized */
225db7ae89dSdrh   int separateCache;             /* Use a new PGroup for each PCache */
226957026acSdrh   int nInitPage;                 /* Initial bulk allocation size */
227bc2ca9ebSdanielk1977   int szSlot;                    /* Size of each free slot */
22850d1b5f3Sdrh   int nSlot;                     /* The number of pcache slots */
22950d1b5f3Sdrh   int nReserve;                  /* Try to keep nFreeSlot above this */
230ee70a84eSdrh   void *pStart, *pEnd;           /* Bounds of global page cache memory */
2319f8cf9daSdrh   /* Above requires no mutex.  Use mutex below for variable that follow. */
2329f8cf9daSdrh   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
233bc2ca9ebSdanielk1977   PgFreeslot *pFree;             /* Free page blocks */
2342cbd78b7Sdrh   int nFreeSlot;                 /* Number of unused pcache slots */
2359f8cf9daSdrh   /* The following value requires a mutex to change.  We skip the mutex on
2369f8cf9daSdrh   ** reading because (1) most platforms read a 32-bit integer atomically and
2379f8cf9daSdrh   ** (2) even if an incorrect value is read, no great harm is done since this
2389f8cf9daSdrh   ** is really just an optimization. */
2399f8cf9daSdrh   int bUnderPressure;            /* True if low on PAGECACHE memory */
24044cd45c8Sdanielk1977 } pcache1_g;
241bc2ca9ebSdanielk1977 
242bc2ca9ebSdanielk1977 /*
243bc2ca9ebSdanielk1977 ** All code in this file should access the global structure above via the
244bc2ca9ebSdanielk1977 ** alias "pcache1". This ensures that the WSD emulation is used when
245bc2ca9ebSdanielk1977 ** compiling for systems that do not support real WSD.
246bc2ca9ebSdanielk1977 */
247bc2ca9ebSdanielk1977 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
248bc2ca9ebSdanielk1977 
249bc2ca9ebSdanielk1977 /*
2509f8cf9daSdrh ** Macros to enter and leave the PCache LRU mutex.
251bc2ca9ebSdanielk1977 */
252982215a2Sdrh #if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
253982215a2Sdrh # define pcache1EnterMutex(X)  assert((X)->mutex==0)
254982215a2Sdrh # define pcache1LeaveMutex(X)  assert((X)->mutex==0)
255982215a2Sdrh # define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
256982215a2Sdrh #else
2579f8cf9daSdrh # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
2589f8cf9daSdrh # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
259982215a2Sdrh # define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
260982215a2Sdrh #endif
261bc2ca9ebSdanielk1977 
262bc2ca9ebSdanielk1977 /******************************************************************************/
263bc2ca9ebSdanielk1977 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
264bc2ca9ebSdanielk1977 
26592af02c9Sdrh 
266bc2ca9ebSdanielk1977 /*
267bc2ca9ebSdanielk1977 ** This function is called during initialization if a static buffer is
268bc2ca9ebSdanielk1977 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
269bc2ca9ebSdanielk1977 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
270bc2ca9ebSdanielk1977 ** enough to contain 'n' buffers of 'sz' bytes each.
2719f8cf9daSdrh **
2729f8cf9daSdrh ** This routine is called from sqlite3_initialize() and so it is guaranteed
2739f8cf9daSdrh ** to be serialized already.  There is no need for further mutexing.
274bc2ca9ebSdanielk1977 */
sqlite3PCacheBufferSetup(void * pBuf,int sz,int n)275bc2ca9ebSdanielk1977 void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
276f4622dc1Sdrh   if( pcache1.isInit ){
277bc2ca9ebSdanielk1977     PgFreeslot *p;
278ee70a84eSdrh     if( pBuf==0 ) sz = n = 0;
27952df6f5eSdrh     if( n==0 ) sz = 0;
280bc73971dSdanielk1977     sz = ROUNDDOWN8(sz);
281bc2ca9ebSdanielk1977     pcache1.szSlot = sz;
28250d1b5f3Sdrh     pcache1.nSlot = pcache1.nFreeSlot = n;
28350d1b5f3Sdrh     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
284bc2ca9ebSdanielk1977     pcache1.pStart = pBuf;
285bc2ca9ebSdanielk1977     pcache1.pFree = 0;
2869f8cf9daSdrh     pcache1.bUnderPressure = 0;
287bc2ca9ebSdanielk1977     while( n-- ){
288bc2ca9ebSdanielk1977       p = (PgFreeslot*)pBuf;
289bc2ca9ebSdanielk1977       p->pNext = pcache1.pFree;
290bc2ca9ebSdanielk1977       pcache1.pFree = p;
291bc2ca9ebSdanielk1977       pBuf = (void*)&((char*)pBuf)[sz];
292bc2ca9ebSdanielk1977     }
293bc2ca9ebSdanielk1977     pcache1.pEnd = pBuf;
294bc2ca9ebSdanielk1977   }
295f4622dc1Sdrh }
296bc2ca9ebSdanielk1977 
297bc2ca9ebSdanielk1977 /*
298957026acSdrh ** Try to initialize the pCache->pFree and pCache->pBulk fields.  Return
299957026acSdrh ** true if pCache->pFree ends up containing one or more free pages.
300957026acSdrh */
pcache1InitBulk(PCache1 * pCache)301957026acSdrh static int pcache1InitBulk(PCache1 *pCache){
302939d4bcdSdrh   i64 szBulk;
303957026acSdrh   char *zBulk;
304957026acSdrh   if( pcache1.nInitPage==0 ) return 0;
305957026acSdrh   /* Do not bother with a bulk allocation if the cache size very small */
306957026acSdrh   if( pCache->nMax<3 ) return 0;
307957026acSdrh   sqlite3BeginBenignMalloc();
308957026acSdrh   if( pcache1.nInitPage>0 ){
309939d4bcdSdrh     szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
310957026acSdrh   }else{
311939d4bcdSdrh     szBulk = -1024 * (i64)pcache1.nInitPage;
312957026acSdrh   }
313939d4bcdSdrh   if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
314989412a1Sdrh     szBulk = pCache->szAlloc*(i64)pCache->nMax;
315957026acSdrh   }
316957026acSdrh   zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
317957026acSdrh   sqlite3EndBenignMalloc();
318957026acSdrh   if( zBulk ){
319957026acSdrh     int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
3204eb8d7faSdrh     do{
321957026acSdrh       PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
322957026acSdrh       pX->page.pBuf = zBulk;
323957026acSdrh       pX->page.pExtra = &pX[1];
324957026acSdrh       pX->isBulkLocal = 1;
32592af02c9Sdrh       pX->isAnchor = 0;
326957026acSdrh       pX->pNext = pCache->pFree;
327e385d33aSdan       pX->pLruPrev = 0;           /* Initializing this saves a valgrind error */
328957026acSdrh       pCache->pFree = pX;
329957026acSdrh       zBulk += pCache->szAlloc;
3304eb8d7faSdrh     }while( --nBulk );
331957026acSdrh   }
332957026acSdrh   return pCache->pFree!=0;
333957026acSdrh }
334957026acSdrh 
335957026acSdrh /*
336bc2ca9ebSdanielk1977 ** Malloc function used within this file to allocate space from the buffer
337bc2ca9ebSdanielk1977 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
338bc2ca9ebSdanielk1977 ** such buffer exists or there is no space left in it, this function falls
339bc2ca9ebSdanielk1977 ** back to sqlite3Malloc().
3409f8cf9daSdrh **
3419f8cf9daSdrh ** Multiple threads can run this routine at the same time.  Global variables
3429f8cf9daSdrh ** in pcache1 need to be protected via mutex.
343bc2ca9ebSdanielk1977 */
pcache1Alloc(int nByte)344bc2ca9ebSdanielk1977 static void *pcache1Alloc(int nByte){
3459f8cf9daSdrh   void *p = 0;
3469f8cf9daSdrh   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
3479f8cf9daSdrh   if( nByte<=pcache1.szSlot ){
3489f8cf9daSdrh     sqlite3_mutex_enter(pcache1.mutex);
349bc2ca9ebSdanielk1977     p = (PgHdr1 *)pcache1.pFree;
3509f8cf9daSdrh     if( p ){
351bc2ca9ebSdanielk1977       pcache1.pFree = pcache1.pFree->pNext;
35250d1b5f3Sdrh       pcache1.nFreeSlot--;
3539f8cf9daSdrh       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
35450d1b5f3Sdrh       assert( pcache1.nFreeSlot>=0 );
355b02392e6Sdrh       sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
356af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
3579f8cf9daSdrh     }
3589f8cf9daSdrh     sqlite3_mutex_leave(pcache1.mutex);
3599f8cf9daSdrh   }
3609f8cf9daSdrh   if( p==0 ){
3619f8cf9daSdrh     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
3629f8cf9daSdrh     ** it from sqlite3Malloc instead.
363bc2ca9ebSdanielk1977     */
364bc2ca9ebSdanielk1977     p = sqlite3Malloc(nByte);
3654bd6952aSdrh #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
366bc2ca9ebSdanielk1977     if( p ){
367bc2ca9ebSdanielk1977       int sz = sqlite3MallocSize(p);
3689bf3da8eSdrh       sqlite3_mutex_enter(pcache1.mutex);
369b02392e6Sdrh       sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
370af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
3719bf3da8eSdrh       sqlite3_mutex_leave(pcache1.mutex);
372bc2ca9ebSdanielk1977     }
3734bd6952aSdrh #endif
374107b56e8Sdrh     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
375bc2ca9ebSdanielk1977   }
376bc2ca9ebSdanielk1977   return p;
377bc2ca9ebSdanielk1977 }
378bc2ca9ebSdanielk1977 
379bc2ca9ebSdanielk1977 /*
380bc2ca9ebSdanielk1977 ** Free an allocated buffer obtained from pcache1Alloc().
381bc2ca9ebSdanielk1977 */
pcache1Free(void * p)382ee70a84eSdrh static void pcache1Free(void *p){
383ee70a84eSdrh   if( p==0 ) return;
3848b0ba7b0Sdrh   if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
385bc2ca9ebSdanielk1977     PgFreeslot *pSlot;
3869f8cf9daSdrh     sqlite3_mutex_enter(pcache1.mutex);
387af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
388bc2ca9ebSdanielk1977     pSlot = (PgFreeslot*)p;
389bc2ca9ebSdanielk1977     pSlot->pNext = pcache1.pFree;
390bc2ca9ebSdanielk1977     pcache1.pFree = pSlot;
39150d1b5f3Sdrh     pcache1.nFreeSlot++;
3929f8cf9daSdrh     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
39350d1b5f3Sdrh     assert( pcache1.nFreeSlot<=pcache1.nSlot );
3949f8cf9daSdrh     sqlite3_mutex_leave(pcache1.mutex);
395bc2ca9ebSdanielk1977   }else{
396107b56e8Sdrh     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
397107b56e8Sdrh     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
3984bd6952aSdrh #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
3999493cafeSdrh     {
4009493cafeSdrh       int nFreed = 0;
401ee70a84eSdrh       nFreed = sqlite3MallocSize(p);
40215ad92f2Sdrh       sqlite3_mutex_enter(pcache1.mutex);
403af89fe66Sdrh       sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
40415ad92f2Sdrh       sqlite3_mutex_leave(pcache1.mutex);
4059493cafeSdrh     }
4064bd6952aSdrh #endif
407bc2ca9ebSdanielk1977     sqlite3_free(p);
408bc2ca9ebSdanielk1977   }
409bc2ca9ebSdanielk1977 }
410bc2ca9ebSdanielk1977 
411c8f503a8Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
412c8f503a8Sdrh /*
4139d13f116Sdrh ** Return the size of a pcache allocation
414c8f503a8Sdrh */
pcache1MemSize(void * p)415c8f503a8Sdrh static int pcache1MemSize(void *p){
416c8f503a8Sdrh   if( p>=pcache1.pStart && p<pcache1.pEnd ){
417c8f503a8Sdrh     return pcache1.szSlot;
418c8f503a8Sdrh   }else{
419c8f503a8Sdrh     int iSize;
420c8f503a8Sdrh     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
421c8f503a8Sdrh     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
422c8f503a8Sdrh     iSize = sqlite3MallocSize(p);
423c8f503a8Sdrh     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
424c8f503a8Sdrh     return iSize;
425c8f503a8Sdrh   }
426c8f503a8Sdrh }
427c8f503a8Sdrh #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
428c8f503a8Sdrh 
429d2925706Sdan /*
430bc2ca9ebSdanielk1977 ** Allocate a new page object initially associated with cache pCache.
431bc2ca9ebSdanielk1977 */
pcache1AllocPage(PCache1 * pCache,int benignMalloc)4323c0c4319Sdrh static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
433b5126ddeSdan   PgHdr1 *p = 0;
434b5126ddeSdan   void *pPg;
435d2925706Sdan 
436ee70a84eSdrh   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
437957026acSdrh   if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
43855f66b34Sdrh     assert( pCache->pFree!=0 );
439ee70a84eSdrh     p = pCache->pFree;
440ee70a84eSdrh     pCache->pFree = p->pNext;
441ee70a84eSdrh     p->pNext = 0;
442ee70a84eSdrh   }else{
443db7ae89dSdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
444d2925706Sdan     /* The group mutex must be released before pcache1Alloc() is called. This
445db7ae89dSdrh     ** is because it might call sqlite3_release_memory(), which assumes that
446d2925706Sdan     ** this mutex is not held. */
447db7ae89dSdrh     assert( pcache1.separateCache==0 );
448db7ae89dSdrh     assert( pCache->pGroup==&pcache1.grp );
449d2925706Sdan     pcache1LeaveMutex(pCache->pGroup);
450db7ae89dSdrh #endif
4518faee877Sdrh     if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
452ee70a84eSdrh     pPg = pcache1Alloc(pCache->szAlloc);
4538faee877Sdrh     if( benignMalloc ){ sqlite3EndBenignMalloc(); }
454db7ae89dSdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
455d2925706Sdan     pcache1EnterMutex(pCache->pGroup);
456db7ae89dSdrh #endif
457ee70a84eSdrh     if( pPg==0 ) return 0;
4580f1fa5deSdrh     p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
45922e21ff4Sdan     p->page.pBuf = pPg;
46022e21ff4Sdan     p->page.pExtra = &p[1];
461ee70a84eSdrh     p->isBulkLocal = 0;
46292af02c9Sdrh     p->isAnchor = 0;
4631e06c70eSdan     p->pLruPrev = 0;           /* Initializing this saves a valgrind error */
464ee70a84eSdrh   }
465617b7b42Sdrh   (*pCache->pnPurgeable)++;
466bc2ca9ebSdanielk1977   return p;
467bc2ca9ebSdanielk1977 }
468bc2ca9ebSdanielk1977 
469bc2ca9ebSdanielk1977 /*
470bc2ca9ebSdanielk1977 ** Free a page object allocated by pcache1AllocPage().
471bc2ca9ebSdanielk1977 */
pcache1FreePage(PgHdr1 * p)472bc2ca9ebSdanielk1977 static void pcache1FreePage(PgHdr1 *p){
473db7ae89dSdrh   PCache1 *pCache;
474db7ae89dSdrh   assert( p!=0 );
475db7ae89dSdrh   pCache = p->pCache;
476d2925706Sdan   assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
477ee70a84eSdrh   if( p->isBulkLocal ){
478ee70a84eSdrh     p->pNext = pCache->pFree;
479ee70a84eSdrh     pCache->pFree = p;
480ee70a84eSdrh   }else{
48122e21ff4Sdan     pcache1Free(p->page.pBuf);
482ee70a84eSdrh   }
483617b7b42Sdrh   (*pCache->pnPurgeable)--;
484bc2ca9ebSdanielk1977 }
485bc2ca9ebSdanielk1977 
486bc2ca9ebSdanielk1977 /*
487bc2ca9ebSdanielk1977 ** Malloc function used by SQLite to obtain space from the buffer configured
488bc2ca9ebSdanielk1977 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
489bc2ca9ebSdanielk1977 ** exists, this function falls back to sqlite3Malloc().
490bc2ca9ebSdanielk1977 */
sqlite3PageMalloc(int sz)491bc2ca9ebSdanielk1977 void *sqlite3PageMalloc(int sz){
492d4de9f7bSdrh   assert( sz<=65536+8 ); /* These allocations are never very large */
493d7a5e498Sdrh   return pcache1Alloc(sz);
494bc2ca9ebSdanielk1977 }
495bc2ca9ebSdanielk1977 
496bc2ca9ebSdanielk1977 /*
497bc2ca9ebSdanielk1977 ** Free an allocated buffer obtained from sqlite3PageMalloc().
498bc2ca9ebSdanielk1977 */
sqlite3PageFree(void * p)499bc2ca9ebSdanielk1977 void sqlite3PageFree(void *p){
500bc2ca9ebSdanielk1977   pcache1Free(p);
501bc2ca9ebSdanielk1977 }
502bc2ca9ebSdanielk1977 
50350d1b5f3Sdrh 
50450d1b5f3Sdrh /*
50550d1b5f3Sdrh ** Return true if it desirable to avoid allocating a new page cache
50650d1b5f3Sdrh ** entry.
50750d1b5f3Sdrh **
50850d1b5f3Sdrh ** If memory was allocated specifically to the page cache using
50950d1b5f3Sdrh ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
51050d1b5f3Sdrh ** it is desirable to avoid allocating a new page cache entry because
51150d1b5f3Sdrh ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
51250d1b5f3Sdrh ** for all page cache needs and we should not need to spill the
51350d1b5f3Sdrh ** allocation onto the heap.
51450d1b5f3Sdrh **
51545d29309Sdrh ** Or, the heap is used for all page cache memory but the heap is
51650d1b5f3Sdrh ** under memory pressure, then again it is desirable to avoid
51750d1b5f3Sdrh ** allocating a new page cache entry in order to avoid stressing
51850d1b5f3Sdrh ** the heap even further.
51950d1b5f3Sdrh */
pcache1UnderMemoryPressure(PCache1 * pCache)52050d1b5f3Sdrh static int pcache1UnderMemoryPressure(PCache1 *pCache){
52122e21ff4Sdan   if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
5229f8cf9daSdrh     return pcache1.bUnderPressure;
52350d1b5f3Sdrh   }else{
52450d1b5f3Sdrh     return sqlite3HeapNearlyFull();
52550d1b5f3Sdrh   }
52650d1b5f3Sdrh }
52750d1b5f3Sdrh 
528bc2ca9ebSdanielk1977 /******************************************************************************/
529bc2ca9ebSdanielk1977 /******** General Implementation Functions ************************************/
530bc2ca9ebSdanielk1977 
531bc2ca9ebSdanielk1977 /*
532bc2ca9ebSdanielk1977 ** This function is used to resize the hash table used by the cache passed
533bc2ca9ebSdanielk1977 ** as the first argument.
534bc2ca9ebSdanielk1977 **
5359f8cf9daSdrh ** The PCache mutex must be held when this function is called.
536bc2ca9ebSdanielk1977 */
pcache1ResizeHash(PCache1 * p)537efbf0445Sdrh static void pcache1ResizeHash(PCache1 *p){
538bc2ca9ebSdanielk1977   PgHdr1 **apNew;
53944cd45c8Sdanielk1977   unsigned int nNew;
540bc2ca9ebSdanielk1977   unsigned int i;
541bc2ca9ebSdanielk1977 
5429f8cf9daSdrh   assert( sqlite3_mutex_held(p->pGroup->mutex) );
543bc2ca9ebSdanielk1977 
544bc2ca9ebSdanielk1977   nNew = p->nHash*2;
545bc2ca9ebSdanielk1977   if( nNew<256 ){
546bc2ca9ebSdanielk1977     nNew = 256;
547bc2ca9ebSdanielk1977   }
548bc2ca9ebSdanielk1977 
5499f8cf9daSdrh   pcache1LeaveMutex(p->pGroup);
550085bb7f0Sdrh   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
5516809c96dSdan   apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
552085bb7f0Sdrh   if( p->nHash ){ sqlite3EndBenignMalloc(); }
5539f8cf9daSdrh   pcache1EnterMutex(p->pGroup);
554bc2ca9ebSdanielk1977   if( apNew ){
555bc2ca9ebSdanielk1977     for(i=0; i<p->nHash; i++){
556bc2ca9ebSdanielk1977       PgHdr1 *pPage;
557bc2ca9ebSdanielk1977       PgHdr1 *pNext = p->apHash[i];
558b27b7f5dSdrh       while( (pPage = pNext)!=0 ){
559bc2ca9ebSdanielk1977         unsigned int h = pPage->iKey % nNew;
560bc2ca9ebSdanielk1977         pNext = pPage->pNext;
561bc2ca9ebSdanielk1977         pPage->pNext = apNew[h];
562bc2ca9ebSdanielk1977         apNew[h] = pPage;
563bc2ca9ebSdanielk1977       }
564bc2ca9ebSdanielk1977     }
565bc2ca9ebSdanielk1977     sqlite3_free(p->apHash);
566bc2ca9ebSdanielk1977     p->apHash = apNew;
567bc2ca9ebSdanielk1977     p->nHash = nNew;
568bc2ca9ebSdanielk1977   }
569bc2ca9ebSdanielk1977 }
570bc2ca9ebSdanielk1977 
571bc2ca9ebSdanielk1977 /*
572bc2ca9ebSdanielk1977 ** This function is used internally to remove the page pPage from the
5739f8cf9daSdrh ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
574bc2ca9ebSdanielk1977 ** LRU list, then this function is a no-op.
575bc2ca9ebSdanielk1977 **
5769f8cf9daSdrh ** The PGroup mutex must be held when this function is called.
577bc2ca9ebSdanielk1977 */
pcache1PinPage(PgHdr1 * pPage)57855a46c9bSdrh static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
5795d56dd28Sdrh   assert( pPage!=0 );
580eabb67fbSdrh   assert( PAGE_IS_UNPINNED(pPage) );
58192af02c9Sdrh   assert( pPage->pLruNext );
58292af02c9Sdrh   assert( pPage->pLruPrev );
583eabb67fbSdrh   assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
584bc2ca9ebSdanielk1977   pPage->pLruPrev->pLruNext = pPage->pLruNext;
585bc2ca9ebSdanielk1977   pPage->pLruNext->pLruPrev = pPage->pLruPrev;
586bc2ca9ebSdanielk1977   pPage->pLruNext = 0;
587de72d2a8Sdrh   /* pPage->pLruPrev = 0;
588de72d2a8Sdrh   ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
58992af02c9Sdrh   assert( pPage->isAnchor==0 );
590eabb67fbSdrh   assert( pPage->pCache->pGroup->lru.isAnchor==1 );
591eabb67fbSdrh   pPage->pCache->nRecyclable--;
59255a46c9bSdrh   return pPage;
593bc2ca9ebSdanielk1977 }
594bc2ca9ebSdanielk1977 
595bc2ca9ebSdanielk1977 
596bc2ca9ebSdanielk1977 /*
597bc2ca9ebSdanielk1977 ** Remove the page supplied as an argument from the hash table
598bc2ca9ebSdanielk1977 ** (PCache1.apHash structure) that it is currently stored in.
59995c91e14Sdrh ** Also free the page if freePage is true.
600bc2ca9ebSdanielk1977 **
6019f8cf9daSdrh ** The PGroup mutex must be held when this function is called.
602bc2ca9ebSdanielk1977 */
pcache1RemoveFromHash(PgHdr1 * pPage,int freeFlag)60395c91e14Sdrh static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
604bc2ca9ebSdanielk1977   unsigned int h;
605bc2ca9ebSdanielk1977   PCache1 *pCache = pPage->pCache;
606bc2ca9ebSdanielk1977   PgHdr1 **pp;
607bc2ca9ebSdanielk1977 
6089f8cf9daSdrh   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
609bc2ca9ebSdanielk1977   h = pPage->iKey % pCache->nHash;
610bc2ca9ebSdanielk1977   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
611bc2ca9ebSdanielk1977   *pp = (*pp)->pNext;
612bc2ca9ebSdanielk1977 
613bc2ca9ebSdanielk1977   pCache->nPage--;
61495c91e14Sdrh   if( freeFlag ) pcache1FreePage(pPage);
615bc2ca9ebSdanielk1977 }
616bc2ca9ebSdanielk1977 
617bc2ca9ebSdanielk1977 /*
6189f8cf9daSdrh ** If there are currently more than nMaxPage pages allocated, try
6199f8cf9daSdrh ** to recycle pages to reduce the number allocated to nMaxPage.
620bc2ca9ebSdanielk1977 */
pcache1EnforceMaxPage(PCache1 * pCache)621957026acSdrh static void pcache1EnforceMaxPage(PCache1 *pCache){
622957026acSdrh   PGroup *pGroup = pCache->pGroup;
62392af02c9Sdrh   PgHdr1 *p;
6249f8cf9daSdrh   assert( sqlite3_mutex_held(pGroup->mutex) );
625617b7b42Sdrh   while( pGroup->nPurgeable>pGroup->nMaxPage
62692af02c9Sdrh       && (p=pGroup->lru.pLruPrev)->isAnchor==0
62792af02c9Sdrh   ){
6289f8cf9daSdrh     assert( p->pCache->pGroup==pGroup );
629eabb67fbSdrh     assert( PAGE_IS_UNPINNED(p) );
630bc2ca9ebSdanielk1977     pcache1PinPage(p);
63195c91e14Sdrh     pcache1RemoveFromHash(p, 1);
632bc2ca9ebSdanielk1977   }
633957026acSdrh   if( pCache->nPage==0 && pCache->pBulk ){
634957026acSdrh     sqlite3_free(pCache->pBulk);
635957026acSdrh     pCache->pBulk = pCache->pFree = 0;
636957026acSdrh   }
637bc2ca9ebSdanielk1977 }
638bc2ca9ebSdanielk1977 
639bc2ca9ebSdanielk1977 /*
640bc2ca9ebSdanielk1977 ** Discard all pages from cache pCache with a page number (key value)
641bc2ca9ebSdanielk1977 ** greater than or equal to iLimit. Any pinned pages that meet this
642bc2ca9ebSdanielk1977 ** criteria are unpinned before they are discarded.
643bc2ca9ebSdanielk1977 **
6449f8cf9daSdrh ** The PCache mutex must be held when this function is called.
645bc2ca9ebSdanielk1977 */
pcache1TruncateUnsafe(PCache1 * pCache,unsigned int iLimit)646bc2ca9ebSdanielk1977 static void pcache1TruncateUnsafe(
6479f8cf9daSdrh   PCache1 *pCache,             /* The cache to truncate */
6489f8cf9daSdrh   unsigned int iLimit          /* Drop pages with this pgno or larger */
649bc2ca9ebSdanielk1977 ){
650d9fabbccSdrh   TESTONLY( int nPage = 0; )  /* To assert pCache->nPage is correct */
651d9fabbccSdrh   unsigned int h, iStop;
6529f8cf9daSdrh   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
653d9fabbccSdrh   assert( pCache->iMaxKey >= iLimit );
654d9fabbccSdrh   assert( pCache->nHash > 0 );
655f5dbe7f8Sdrh   if( pCache->iMaxKey - iLimit < pCache->nHash ){
656d9fabbccSdrh     /* If we are just shaving the last few pages off the end of the
657d9fabbccSdrh     ** cache, then there is no point in scanning the entire hash table.
658d9fabbccSdrh     ** Only scan those hash slots that might contain pages that need to
659d9fabbccSdrh     ** be removed. */
660f5dbe7f8Sdrh     h = iLimit % pCache->nHash;
661f5dbe7f8Sdrh     iStop = pCache->iMaxKey % pCache->nHash;
662d9fabbccSdrh     TESTONLY( nPage = -10; )  /* Disable the pCache->nPage validity check */
663d9fabbccSdrh   }else{
664d9fabbccSdrh     /* This is the general case where many pages are being removed.
665d9fabbccSdrh     ** It is necessary to scan the entire hash table */
666f5dbe7f8Sdrh     h = pCache->nHash/2;
667f5dbe7f8Sdrh     iStop = h - 1;
668d9fabbccSdrh   }
669d9fabbccSdrh   for(;;){
670d9fabbccSdrh     PgHdr1 **pp;
671bc2ca9ebSdanielk1977     PgHdr1 *pPage;
672d9fabbccSdrh     assert( h<pCache->nHash );
673d9fabbccSdrh     pp = &pCache->apHash[h];
674b27b7f5dSdrh     while( (pPage = *pp)!=0 ){
675bc2ca9ebSdanielk1977       if( pPage->iKey>=iLimit ){
676ea24ac4bSdanielk1977         pCache->nPage--;
677bc2ca9ebSdanielk1977         *pp = pPage->pNext;
678eabb67fbSdrh         if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
679bc2ca9ebSdanielk1977         pcache1FreePage(pPage);
680bc2ca9ebSdanielk1977       }else{
681bc2ca9ebSdanielk1977         pp = &pPage->pNext;
682d9fabbccSdrh         TESTONLY( if( nPage>=0 ) nPage++; )
683bc2ca9ebSdanielk1977       }
684bc2ca9ebSdanielk1977     }
685d9fabbccSdrh     if( h==iStop ) break;
686f5dbe7f8Sdrh     h = (h+1) % pCache->nHash;
687bc2ca9ebSdanielk1977   }
688d9fabbccSdrh   assert( nPage<0 || pCache->nPage==(unsigned)nPage );
689bc2ca9ebSdanielk1977 }
690bc2ca9ebSdanielk1977 
691bc2ca9ebSdanielk1977 /******************************************************************************/
692bc2ca9ebSdanielk1977 /******** sqlite3_pcache Methods **********************************************/
693bc2ca9ebSdanielk1977 
694bc2ca9ebSdanielk1977 /*
695bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xInit method.
696bc2ca9ebSdanielk1977 */
pcache1Init(void * NotUsed)69762c14b34Sdanielk1977 static int pcache1Init(void *NotUsed){
69862c14b34Sdanielk1977   UNUSED_PARAMETER(NotUsed);
699f4622dc1Sdrh   assert( pcache1.isInit==0 );
700bc2ca9ebSdanielk1977   memset(&pcache1, 0, sizeof(pcache1));
701db7ae89dSdrh 
702db7ae89dSdrh 
703db7ae89dSdrh   /*
704db7ae89dSdrh   ** The pcache1.separateCache variable is true if each PCache has its own
705db7ae89dSdrh   ** private PGroup (mode-1).  pcache1.separateCache is false if the single
706db7ae89dSdrh   ** PGroup in pcache1.grp is used for all page caches (mode-2).
707db7ae89dSdrh   **
708db7ae89dSdrh   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
709db7ae89dSdrh   **
710db7ae89dSdrh   **   *  Use a unified cache in single-threaded applications that have
711db7ae89dSdrh   **      configured a start-time buffer for use as page-cache memory using
712db7ae89dSdrh   **      sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
713db7ae89dSdrh   **      pBuf argument.
714db7ae89dSdrh   **
715db7ae89dSdrh   **   *  Otherwise use separate caches (mode-1)
716db7ae89dSdrh   */
717db7ae89dSdrh #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
718db7ae89dSdrh   pcache1.separateCache = 0;
719fd5ae964Sdrh #elif SQLITE_THREADSAFE
720db7ae89dSdrh   pcache1.separateCache = sqlite3GlobalConfig.pPage==0
721db7ae89dSdrh                           || sqlite3GlobalConfig.bCoreMutex>0;
722fd5ae964Sdrh #else
723fd5ae964Sdrh   pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
724db7ae89dSdrh #endif
725db7ae89dSdrh 
726982215a2Sdrh #if SQLITE_THREADSAFE
727bc2ca9ebSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
72897a7e5e6Sdrh     pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
72997a7e5e6Sdrh     pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
730bc2ca9ebSdanielk1977   }
731982215a2Sdrh #endif
732957026acSdrh   if( pcache1.separateCache
733957026acSdrh    && sqlite3GlobalConfig.nPage!=0
734957026acSdrh    && sqlite3GlobalConfig.pPage==0
735957026acSdrh   ){
736957026acSdrh     pcache1.nInitPage = sqlite3GlobalConfig.nPage;
737957026acSdrh   }else{
738957026acSdrh     pcache1.nInitPage = 0;
739957026acSdrh   }
74041692e9dSdrh   pcache1.grp.mxPinned = 10;
741f4622dc1Sdrh   pcache1.isInit = 1;
742bc2ca9ebSdanielk1977   return SQLITE_OK;
743bc2ca9ebSdanielk1977 }
744bc2ca9ebSdanielk1977 
745bc2ca9ebSdanielk1977 /*
746bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xShutdown method.
7477c7c311dSshane ** Note that the static mutex allocated in xInit does
7487c7c311dSshane ** not need to be freed.
749bc2ca9ebSdanielk1977 */
pcache1Shutdown(void * NotUsed)75062c14b34Sdanielk1977 static void pcache1Shutdown(void *NotUsed){
75162c14b34Sdanielk1977   UNUSED_PARAMETER(NotUsed);
752f4622dc1Sdrh   assert( pcache1.isInit!=0 );
753b093719aSdrh   memset(&pcache1, 0, sizeof(pcache1));
754bc2ca9ebSdanielk1977 }
755bc2ca9ebSdanielk1977 
756efbf0445Sdrh /* forward declaration */
757efbf0445Sdrh static void pcache1Destroy(sqlite3_pcache *p);
758efbf0445Sdrh 
759bc2ca9ebSdanielk1977 /*
760bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xCreate method.
761bc2ca9ebSdanielk1977 **
762bc2ca9ebSdanielk1977 ** Allocate a new cache.
763bc2ca9ebSdanielk1977 */
pcache1Create(int szPage,int szExtra,int bPurgeable)764e5c40b18Sdrh static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
7659f8cf9daSdrh   PCache1 *pCache;      /* The newly created page cache */
7669f8cf9daSdrh   PGroup *pGroup;       /* The group the new page cache will belong to */
7679f8cf9daSdrh   int sz;               /* Bytes of memory required to allocate the new cache */
768bc2ca9ebSdanielk1977 
769e73c9149Sdrh   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
770e73c9149Sdrh   assert( szExtra < 300 );
771e73c9149Sdrh 
772db7ae89dSdrh   sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
7736809c96dSdan   pCache = (PCache1 *)sqlite3MallocZero(sz);
774bc2ca9ebSdanielk1977   if( pCache ){
775db7ae89dSdrh     if( pcache1.separateCache ){
7769f8cf9daSdrh       pGroup = (PGroup*)&pCache[1];
77741692e9dSdrh       pGroup->mxPinned = 10;
7789f8cf9daSdrh     }else{
7799dde7cbbSdan       pGroup = &pcache1.grp;
7809f8cf9daSdrh     }
781a082cd75Sdan     pcache1EnterMutex(pGroup);
78292af02c9Sdrh     if( pGroup->lru.isAnchor==0 ){
78392af02c9Sdrh       pGroup->lru.isAnchor = 1;
78492af02c9Sdrh       pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
78592af02c9Sdrh     }
7869f8cf9daSdrh     pCache->pGroup = pGroup;
787bc2ca9ebSdanielk1977     pCache->szPage = szPage;
78822e21ff4Sdan     pCache->szExtra = szExtra;
789ee70a84eSdrh     pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
790bc2ca9ebSdanielk1977     pCache->bPurgeable = (bPurgeable ? 1 : 0);
791efbf0445Sdrh     pcache1ResizeHash(pCache);
792bc2ca9ebSdanielk1977     if( bPurgeable ){
793bc2ca9ebSdanielk1977       pCache->nMin = 10;
7949f8cf9daSdrh       pGroup->nMinPage += pCache->nMin;
79541692e9dSdrh       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
796617b7b42Sdrh       pCache->pnPurgeable = &pGroup->nPurgeable;
797617b7b42Sdrh     }else{
7981757fed8Sdrh       pCache->pnPurgeable = &pCache->nPurgeableDummy;
799efbf0445Sdrh     }
8009f8cf9daSdrh     pcache1LeaveMutex(pGroup);
801efbf0445Sdrh     if( pCache->nHash==0 ){
802efbf0445Sdrh       pcache1Destroy((sqlite3_pcache*)pCache);
803efbf0445Sdrh       pCache = 0;
804bc2ca9ebSdanielk1977     }
805bc2ca9ebSdanielk1977   }
806bc2ca9ebSdanielk1977   return (sqlite3_pcache *)pCache;
807bc2ca9ebSdanielk1977 }
808bc2ca9ebSdanielk1977 
809bc2ca9ebSdanielk1977 /*
810bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xCachesize method.
811bc2ca9ebSdanielk1977 **
812bc2ca9ebSdanielk1977 ** Configure the cache_size limit for a cache.
813bc2ca9ebSdanielk1977 */
pcache1Cachesize(sqlite3_pcache * p,int nMax)814bc2ca9ebSdanielk1977 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
815bc2ca9ebSdanielk1977   PCache1 *pCache = (PCache1 *)p;
816f9d349a8Sdrh   u32 n;
817f9d349a8Sdrh   assert( nMax>=0 );
818bc2ca9ebSdanielk1977   if( pCache->bPurgeable ){
8199f8cf9daSdrh     PGroup *pGroup = pCache->pGroup;
8209f8cf9daSdrh     pcache1EnterMutex(pGroup);
821f9d349a8Sdrh     n = (u32)nMax;
822f9d349a8Sdrh     if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
823f9d349a8Sdrh       n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
8248a728824Sdrh     }
825f9d349a8Sdrh     pGroup->nMaxPage += (n - pCache->nMax);
82641692e9dSdrh     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
827f9d349a8Sdrh     pCache->nMax = n;
82825ca5682Sdrh     pCache->n90pct = pCache->nMax*9/10;
829957026acSdrh     pcache1EnforceMaxPage(pCache);
8309f8cf9daSdrh     pcache1LeaveMutex(pGroup);
831bc2ca9ebSdanielk1977   }
832bc2ca9ebSdanielk1977 }
833bc2ca9ebSdanielk1977 
834bc2ca9ebSdanielk1977 /*
83509419b4bSdrh ** Implementation of the sqlite3_pcache.xShrink method.
83609419b4bSdrh **
83709419b4bSdrh ** Free up as much memory as possible.
83809419b4bSdrh */
pcache1Shrink(sqlite3_pcache * p)83909419b4bSdrh static void pcache1Shrink(sqlite3_pcache *p){
84009419b4bSdrh   PCache1 *pCache = (PCache1*)p;
84109419b4bSdrh   if( pCache->bPurgeable ){
84209419b4bSdrh     PGroup *pGroup = pCache->pGroup;
8438a728824Sdrh     unsigned int savedMaxPage;
84409419b4bSdrh     pcache1EnterMutex(pGroup);
84509419b4bSdrh     savedMaxPage = pGroup->nMaxPage;
84609419b4bSdrh     pGroup->nMaxPage = 0;
847957026acSdrh     pcache1EnforceMaxPage(pCache);
84809419b4bSdrh     pGroup->nMaxPage = savedMaxPage;
84909419b4bSdrh     pcache1LeaveMutex(pGroup);
85009419b4bSdrh   }
85109419b4bSdrh }
85209419b4bSdrh 
85309419b4bSdrh /*
854bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xPagecount method.
855bc2ca9ebSdanielk1977 */
pcache1Pagecount(sqlite3_pcache * p)856bc2ca9ebSdanielk1977 static int pcache1Pagecount(sqlite3_pcache *p){
857bc2ca9ebSdanielk1977   int n;
8589f8cf9daSdrh   PCache1 *pCache = (PCache1*)p;
8599f8cf9daSdrh   pcache1EnterMutex(pCache->pGroup);
8609f8cf9daSdrh   n = pCache->nPage;
8619f8cf9daSdrh   pcache1LeaveMutex(pCache->pGroup);
862bc2ca9ebSdanielk1977   return n;
863bc2ca9ebSdanielk1977 }
864bc2ca9ebSdanielk1977 
865efbf0445Sdrh 
866efbf0445Sdrh /*
867efbf0445Sdrh ** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
868efbf0445Sdrh ** in the header of the pcache1Fetch() procedure.
869efbf0445Sdrh **
870efbf0445Sdrh ** This steps are broken out into a separate procedure because they are
871efbf0445Sdrh ** usually not needed, and by avoiding the stack initialization required
872efbf0445Sdrh ** for these steps, the main pcache1Fetch() procedure can run faster.
873efbf0445Sdrh */
pcache1FetchStage2(PCache1 * pCache,unsigned int iKey,int createFlag)874efbf0445Sdrh static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
875efbf0445Sdrh   PCache1 *pCache,
876efbf0445Sdrh   unsigned int iKey,
877efbf0445Sdrh   int createFlag
878efbf0445Sdrh ){
879efbf0445Sdrh   unsigned int nPinned;
880efbf0445Sdrh   PGroup *pGroup = pCache->pGroup;
881efbf0445Sdrh   PgHdr1 *pPage = 0;
882efbf0445Sdrh 
883efbf0445Sdrh   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
884efbf0445Sdrh   assert( pCache->nPage >= pCache->nRecyclable );
885efbf0445Sdrh   nPinned = pCache->nPage - pCache->nRecyclable;
886efbf0445Sdrh   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
887efbf0445Sdrh   assert( pCache->n90pct == pCache->nMax*9/10 );
888efbf0445Sdrh   if( createFlag==1 && (
889efbf0445Sdrh         nPinned>=pGroup->mxPinned
890efbf0445Sdrh      || nPinned>=pCache->n90pct
8915bd8af7cSdan      || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
892efbf0445Sdrh   )){
893efbf0445Sdrh     return 0;
894efbf0445Sdrh   }
895efbf0445Sdrh 
896efbf0445Sdrh   if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
897efbf0445Sdrh   assert( pCache->nHash>0 && pCache->apHash );
898efbf0445Sdrh 
899efbf0445Sdrh   /* Step 4. Try to recycle a page. */
900c54357ccSdrh   if( pCache->bPurgeable
90192af02c9Sdrh    && !pGroup->lru.pLruPrev->isAnchor
902c54357ccSdrh    && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
903c54357ccSdrh   ){
904efbf0445Sdrh     PCache1 *pOther;
90592af02c9Sdrh     pPage = pGroup->lru.pLruPrev;
906eabb67fbSdrh     assert( PAGE_IS_UNPINNED(pPage) );
90795c91e14Sdrh     pcache1RemoveFromHash(pPage, 0);
908efbf0445Sdrh     pcache1PinPage(pPage);
909efbf0445Sdrh     pOther = pPage->pCache;
910ee70a84eSdrh     if( pOther->szAlloc != pCache->szAlloc ){
911efbf0445Sdrh       pcache1FreePage(pPage);
912efbf0445Sdrh       pPage = 0;
913efbf0445Sdrh     }else{
914617b7b42Sdrh       pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
915efbf0445Sdrh     }
916efbf0445Sdrh   }
917efbf0445Sdrh 
918efbf0445Sdrh   /* Step 5. If a usable page buffer has still not been found,
919efbf0445Sdrh   ** attempt to allocate a new one.
920efbf0445Sdrh   */
921efbf0445Sdrh   if( !pPage ){
9223c0c4319Sdrh     pPage = pcache1AllocPage(pCache, createFlag==1);
923efbf0445Sdrh   }
924efbf0445Sdrh 
925efbf0445Sdrh   if( pPage ){
926efbf0445Sdrh     unsigned int h = iKey % pCache->nHash;
927efbf0445Sdrh     pCache->nPage++;
928efbf0445Sdrh     pPage->iKey = iKey;
929efbf0445Sdrh     pPage->pNext = pCache->apHash[h];
930efbf0445Sdrh     pPage->pCache = pCache;
931efbf0445Sdrh     pPage->pLruNext = 0;
932de72d2a8Sdrh     /* pPage->pLruPrev = 0;
933de72d2a8Sdrh     ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
934efbf0445Sdrh     *(void **)pPage->page.pExtra = 0;
935efbf0445Sdrh     pCache->apHash[h] = pPage;
936efbf0445Sdrh     if( iKey>pCache->iMaxKey ){
937efbf0445Sdrh       pCache->iMaxKey = iKey;
938efbf0445Sdrh     }
939efbf0445Sdrh   }
940efbf0445Sdrh   return pPage;
941efbf0445Sdrh }
942efbf0445Sdrh 
943bc2ca9ebSdanielk1977 /*
944bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xFetch method.
945bc2ca9ebSdanielk1977 **
946bc2ca9ebSdanielk1977 ** Fetch a page by key value.
947bc2ca9ebSdanielk1977 **
948bc2ca9ebSdanielk1977 ** Whether or not a new page may be allocated by this function depends on
949f18a61ddSdrh ** the value of the createFlag argument.  0 means do not allocate a new
950f18a61ddSdrh ** page.  1 means allocate a new page if space is easily available.  2
951f18a61ddSdrh ** means to try really hard to allocate a new page.
952f18a61ddSdrh **
953f18a61ddSdrh ** For a non-purgeable cache (a cache used as the storage for an in-memory
954f18a61ddSdrh ** database) there is really no difference between createFlag 1 and 2.  So
955f18a61ddSdrh ** the calling function (pcache.c) will never have a createFlag of 1 on
95645d29309Sdrh ** a non-purgeable cache.
957bc2ca9ebSdanielk1977 **
958bc2ca9ebSdanielk1977 ** There are three different approaches to obtaining space for a page,
959bc2ca9ebSdanielk1977 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
960bc2ca9ebSdanielk1977 **
961bc2ca9ebSdanielk1977 **   1. Regardless of the value of createFlag, the cache is searched for a
962bc2ca9ebSdanielk1977 **      copy of the requested page. If one is found, it is returned.
963bc2ca9ebSdanielk1977 **
964bc2ca9ebSdanielk1977 **   2. If createFlag==0 and the page is not already in the cache, NULL is
965bc2ca9ebSdanielk1977 **      returned.
966bc2ca9ebSdanielk1977 **
96750d1b5f3Sdrh **   3. If createFlag is 1, and the page is not already in the cache, then
96850d1b5f3Sdrh **      return NULL (do not allocate a new page) if any of the following
96950d1b5f3Sdrh **      conditions are true:
970bc2ca9ebSdanielk1977 **
971bc2ca9ebSdanielk1977 **       (a) the number of pages pinned by the cache is greater than
972bc2ca9ebSdanielk1977 **           PCache1.nMax, or
97350d1b5f3Sdrh **
974bc2ca9ebSdanielk1977 **       (b) the number of pages pinned by the cache is greater than
975bc2ca9ebSdanielk1977 **           the sum of nMax for all purgeable caches, less the sum of
97650d1b5f3Sdrh **           nMin for all other purgeable caches, or
977bc2ca9ebSdanielk1977 **
978bc2ca9ebSdanielk1977 **   4. If none of the first three conditions apply and the cache is marked
979bc2ca9ebSdanielk1977 **      as purgeable, and if one of the following is true:
980bc2ca9ebSdanielk1977 **
981bc2ca9ebSdanielk1977 **       (a) The number of pages allocated for the cache is already
982bc2ca9ebSdanielk1977 **           PCache1.nMax, or
983bc2ca9ebSdanielk1977 **
984bc2ca9ebSdanielk1977 **       (b) The number of pages allocated for all purgeable caches is
985bc2ca9ebSdanielk1977 **           already equal to or greater than the sum of nMax for all
986bc2ca9ebSdanielk1977 **           purgeable caches,
987bc2ca9ebSdanielk1977 **
98850d1b5f3Sdrh **       (c) The system is under memory pressure and wants to avoid
98950d1b5f3Sdrh **           unnecessary pages cache entry allocations
99050d1b5f3Sdrh **
991bc2ca9ebSdanielk1977 **      then attempt to recycle a page from the LRU list. If it is the right
992bc2ca9ebSdanielk1977 **      size, return the recycled buffer. Otherwise, free the buffer and
993bc2ca9ebSdanielk1977 **      proceed to step 5.
994bc2ca9ebSdanielk1977 **
995bc2ca9ebSdanielk1977 **   5. Otherwise, allocate and return a new page buffer.
99655a46c9bSdrh **
99755a46c9bSdrh ** There are two versions of this routine.  pcache1FetchWithMutex() is
99855a46c9bSdrh ** the general case.  pcache1FetchNoMutex() is a faster implementation for
99955a46c9bSdrh ** the common case where pGroup->mutex is NULL.  The pcache1Fetch() wrapper
100055a46c9bSdrh ** invokes the appropriate routine.
1001bc2ca9ebSdanielk1977 */
pcache1FetchNoMutex(sqlite3_pcache * p,unsigned int iKey,int createFlag)100255a46c9bSdrh static PgHdr1 *pcache1FetchNoMutex(
100322e21ff4Sdan   sqlite3_pcache *p,
100422e21ff4Sdan   unsigned int iKey,
100522e21ff4Sdan   int createFlag
100622e21ff4Sdan ){
1007bc2ca9ebSdanielk1977   PCache1 *pCache = (PCache1 *)p;
1008bc2ca9ebSdanielk1977   PgHdr1 *pPage = 0;
1009bc2ca9ebSdanielk1977 
10103a5676ceSdrh   /* Step 1: Search the hash table for an existing entry. */
1011efbf0445Sdrh   pPage = pCache->apHash[iKey % pCache->nHash];
1012efbf0445Sdrh   while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
1013bc2ca9ebSdanielk1977 
101495a0b371Sdrh   /* Step 2: If the page was found in the hash table, then return it.
101595a0b371Sdrh   ** If the page was not in the hash table and createFlag is 0, abort.
101695a0b371Sdrh   ** Otherwise (page not in hash and createFlag!=0) continue with
101795a0b371Sdrh   ** subsequent steps to try to create the page. */
10185d56dd28Sdrh   if( pPage ){
1019eabb67fbSdrh     if( PAGE_IS_UNPINNED(pPage) ){
102055a46c9bSdrh       return pcache1PinPage(pPage);
102155a46c9bSdrh     }else{
102255a46c9bSdrh       return pPage;
102355a46c9bSdrh     }
1024efbf0445Sdrh   }else if( createFlag ){
1025efbf0445Sdrh     /* Steps 3, 4, and 5 implemented by this subroutine */
102655a46c9bSdrh     return pcache1FetchStage2(pCache, iKey, createFlag);
102755a46c9bSdrh   }else{
102855a46c9bSdrh     return 0;
10295d56dd28Sdrh   }
103055a46c9bSdrh }
1031982215a2Sdrh #if PCACHE1_MIGHT_USE_GROUP_MUTEX
pcache1FetchWithMutex(sqlite3_pcache * p,unsigned int iKey,int createFlag)103255a46c9bSdrh static PgHdr1 *pcache1FetchWithMutex(
103355a46c9bSdrh   sqlite3_pcache *p,
103455a46c9bSdrh   unsigned int iKey,
103555a46c9bSdrh   int createFlag
103655a46c9bSdrh ){
103755a46c9bSdrh   PCache1 *pCache = (PCache1 *)p;
103855a46c9bSdrh   PgHdr1 *pPage;
103955a46c9bSdrh 
104055a46c9bSdrh   pcache1EnterMutex(pCache->pGroup);
104155a46c9bSdrh   pPage = pcache1FetchNoMutex(p, iKey, createFlag);
1042efbf0445Sdrh   assert( pPage==0 || pCache->iMaxKey>=iKey );
1043efbf0445Sdrh   pcache1LeaveMutex(pCache->pGroup);
104455a46c9bSdrh   return pPage;
104555a46c9bSdrh }
1046982215a2Sdrh #endif
pcache1Fetch(sqlite3_pcache * p,unsigned int iKey,int createFlag)104755a46c9bSdrh static sqlite3_pcache_page *pcache1Fetch(
104855a46c9bSdrh   sqlite3_pcache *p,
104955a46c9bSdrh   unsigned int iKey,
105055a46c9bSdrh   int createFlag
105155a46c9bSdrh ){
1052982215a2Sdrh #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
105355a46c9bSdrh   PCache1 *pCache = (PCache1 *)p;
1054982215a2Sdrh #endif
105555a46c9bSdrh 
105655a46c9bSdrh   assert( offsetof(PgHdr1,page)==0 );
105755a46c9bSdrh   assert( pCache->bPurgeable || createFlag!=1 );
105855a46c9bSdrh   assert( pCache->bPurgeable || pCache->nMin==0 );
105955a46c9bSdrh   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
106055a46c9bSdrh   assert( pCache->nMin==0 || pCache->bPurgeable );
106155a46c9bSdrh   assert( pCache->nHash>0 );
1062982215a2Sdrh #if PCACHE1_MIGHT_USE_GROUP_MUTEX
106355a46c9bSdrh   if( pCache->pGroup->mutex ){
106455a46c9bSdrh     return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
1065982215a2Sdrh   }else
1066982215a2Sdrh #endif
1067982215a2Sdrh   {
106855a46c9bSdrh     return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
106955a46c9bSdrh   }
1070bc2ca9ebSdanielk1977 }
1071bc2ca9ebSdanielk1977 
1072bc2ca9ebSdanielk1977 
1073bc2ca9ebSdanielk1977 /*
1074bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xUnpin method.
1075bc2ca9ebSdanielk1977 **
1076bc2ca9ebSdanielk1977 ** Mark a page as unpinned (eligible for asynchronous recycling).
1077bc2ca9ebSdanielk1977 */
pcache1Unpin(sqlite3_pcache * p,sqlite3_pcache_page * pPg,int reuseUnlikely)107822e21ff4Sdan static void pcache1Unpin(
107922e21ff4Sdan   sqlite3_pcache *p,
108022e21ff4Sdan   sqlite3_pcache_page *pPg,
108122e21ff4Sdan   int reuseUnlikely
108222e21ff4Sdan ){
1083bc2ca9ebSdanielk1977   PCache1 *pCache = (PCache1 *)p;
108422e21ff4Sdan   PgHdr1 *pPage = (PgHdr1 *)pPg;
10859f8cf9daSdrh   PGroup *pGroup = pCache->pGroup;
1086bc2ca9ebSdanielk1977 
108769e931e7Sdrh   assert( pPage->pCache==pCache );
10889f8cf9daSdrh   pcache1EnterMutex(pGroup);
1089bc2ca9ebSdanielk1977 
1090bc2ca9ebSdanielk1977   /* It is an error to call this function if the page is already
10919f8cf9daSdrh   ** part of the PGroup LRU list.
1092bc2ca9ebSdanielk1977   */
1093de72d2a8Sdrh   assert( pPage->pLruNext==0 );
1094eabb67fbSdrh   assert( PAGE_IS_PINNED(pPage) );
1095bc2ca9ebSdanielk1977 
1096617b7b42Sdrh   if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
109795c91e14Sdrh     pcache1RemoveFromHash(pPage, 1);
1098bc2ca9ebSdanielk1977   }else{
10999f8cf9daSdrh     /* Add the page to the PGroup LRU list. */
110092af02c9Sdrh     PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
110192af02c9Sdrh     pPage->pLruPrev = &pGroup->lru;
110292af02c9Sdrh     (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
110392af02c9Sdrh     *ppFirst = pPage;
1104bc2ca9ebSdanielk1977     pCache->nRecyclable++;
1105bc2ca9ebSdanielk1977   }
1106bc2ca9ebSdanielk1977 
11079f8cf9daSdrh   pcache1LeaveMutex(pCache->pGroup);
1108bc2ca9ebSdanielk1977 }
1109bc2ca9ebSdanielk1977 
1110bc2ca9ebSdanielk1977 /*
1111bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xRekey method.
1112bc2ca9ebSdanielk1977 */
pcache1Rekey(sqlite3_pcache * p,sqlite3_pcache_page * pPg,unsigned int iOld,unsigned int iNew)1113bc2ca9ebSdanielk1977 static void pcache1Rekey(
1114bc2ca9ebSdanielk1977   sqlite3_pcache *p,
111522e21ff4Sdan   sqlite3_pcache_page *pPg,
1116bc2ca9ebSdanielk1977   unsigned int iOld,
1117bc2ca9ebSdanielk1977   unsigned int iNew
1118bc2ca9ebSdanielk1977 ){
1119bc2ca9ebSdanielk1977   PCache1 *pCache = (PCache1 *)p;
112022e21ff4Sdan   PgHdr1 *pPage = (PgHdr1 *)pPg;
1121bc2ca9ebSdanielk1977   PgHdr1 **pp;
11224e9bf5acSdrh   unsigned int hOld, hNew;
1123bc2ca9ebSdanielk1977   assert( pPage->iKey==iOld );
112469e931e7Sdrh   assert( pPage->pCache==pCache );
11259c3a114cSdrh   assert( iOld!=iNew );               /* The page number really is changing */
1126bc2ca9ebSdanielk1977 
11279f8cf9daSdrh   pcache1EnterMutex(pCache->pGroup);
1128bc2ca9ebSdanielk1977 
11299c3a114cSdrh   assert( pcache1FetchNoMutex(p, iOld, 0)==pPage ); /* pPg really is iOld */
11304e9bf5acSdrh   hOld = iOld%pCache->nHash;
11314e9bf5acSdrh   pp = &pCache->apHash[hOld];
1132bc2ca9ebSdanielk1977   while( (*pp)!=pPage ){
1133bc2ca9ebSdanielk1977     pp = &(*pp)->pNext;
1134bc2ca9ebSdanielk1977   }
1135bc2ca9ebSdanielk1977   *pp = pPage->pNext;
1136bc2ca9ebSdanielk1977 
1137*a002cc17Sstephan   assert( pcache1FetchNoMutex(p, iNew, 0)==0 ); /* iNew not in cache */
11384e9bf5acSdrh   hNew = iNew%pCache->nHash;
1139bc2ca9ebSdanielk1977   pPage->iKey = iNew;
11404e9bf5acSdrh   pPage->pNext = pCache->apHash[hNew];
11414e9bf5acSdrh   pCache->apHash[hNew] = pPage;
114298829a65Sdrh   if( iNew>pCache->iMaxKey ){
1143f90b7260Sdanielk1977     pCache->iMaxKey = iNew;
1144f90b7260Sdanielk1977   }
1145f90b7260Sdanielk1977 
11469f8cf9daSdrh   pcache1LeaveMutex(pCache->pGroup);
1147bc2ca9ebSdanielk1977 }
1148bc2ca9ebSdanielk1977 
1149bc2ca9ebSdanielk1977 /*
1150bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xTruncate method.
1151bc2ca9ebSdanielk1977 **
1152bc2ca9ebSdanielk1977 ** Discard all unpinned pages in the cache with a page number equal to
1153bc2ca9ebSdanielk1977 ** or greater than parameter iLimit. Any pinned pages with a page number
1154bc2ca9ebSdanielk1977 ** equal to or greater than iLimit are implicitly unpinned.
1155bc2ca9ebSdanielk1977 */
pcache1Truncate(sqlite3_pcache * p,unsigned int iLimit)1156bc2ca9ebSdanielk1977 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
1157bc2ca9ebSdanielk1977   PCache1 *pCache = (PCache1 *)p;
11589f8cf9daSdrh   pcache1EnterMutex(pCache->pGroup);
1159f90b7260Sdanielk1977   if( iLimit<=pCache->iMaxKey ){
1160bc2ca9ebSdanielk1977     pcache1TruncateUnsafe(pCache, iLimit);
1161f90b7260Sdanielk1977     pCache->iMaxKey = iLimit-1;
1162f90b7260Sdanielk1977   }
11639f8cf9daSdrh   pcache1LeaveMutex(pCache->pGroup);
1164bc2ca9ebSdanielk1977 }
1165bc2ca9ebSdanielk1977 
1166bc2ca9ebSdanielk1977 /*
1167bc2ca9ebSdanielk1977 ** Implementation of the sqlite3_pcache.xDestroy method.
1168bc2ca9ebSdanielk1977 **
1169bc2ca9ebSdanielk1977 ** Destroy a cache allocated using pcache1Create().
1170bc2ca9ebSdanielk1977 */
pcache1Destroy(sqlite3_pcache * p)1171bc2ca9ebSdanielk1977 static void pcache1Destroy(sqlite3_pcache *p){
1172bc2ca9ebSdanielk1977   PCache1 *pCache = (PCache1 *)p;
11739f8cf9daSdrh   PGroup *pGroup = pCache->pGroup;
1174b51d2fa8Sdan   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
11759f8cf9daSdrh   pcache1EnterMutex(pGroup);
1176d9fabbccSdrh   if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
1177a69085cfSdrh   assert( pGroup->nMaxPage >= pCache->nMax );
11789f8cf9daSdrh   pGroup->nMaxPage -= pCache->nMax;
1179a69085cfSdrh   assert( pGroup->nMinPage >= pCache->nMin );
11809f8cf9daSdrh   pGroup->nMinPage -= pCache->nMin;
118141692e9dSdrh   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
1182957026acSdrh   pcache1EnforceMaxPage(pCache);
11839f8cf9daSdrh   pcache1LeaveMutex(pGroup);
1184ee70a84eSdrh   sqlite3_free(pCache->pBulk);
1185bc2ca9ebSdanielk1977   sqlite3_free(pCache->apHash);
1186bc2ca9ebSdanielk1977   sqlite3_free(pCache);
1187bc2ca9ebSdanielk1977 }
1188bc2ca9ebSdanielk1977 
1189bc2ca9ebSdanielk1977 /*
1190bc2ca9ebSdanielk1977 ** This function is called during initialization (sqlite3_initialize()) to
1191bc2ca9ebSdanielk1977 ** install the default pluggable cache module, assuming the user has not
1192bc2ca9ebSdanielk1977 ** already provided an alternative.
1193bc2ca9ebSdanielk1977 */
sqlite3PCacheSetDefault(void)1194bc2ca9ebSdanielk1977 void sqlite3PCacheSetDefault(void){
119522e21ff4Sdan   static const sqlite3_pcache_methods2 defaultMethods = {
119681ef0f97Sdrh     1,                       /* iVersion */
1197bc2ca9ebSdanielk1977     0,                       /* pArg */
1198bc2ca9ebSdanielk1977     pcache1Init,             /* xInit */
1199bc2ca9ebSdanielk1977     pcache1Shutdown,         /* xShutdown */
1200bc2ca9ebSdanielk1977     pcache1Create,           /* xCreate */
1201bc2ca9ebSdanielk1977     pcache1Cachesize,        /* xCachesize */
1202bc2ca9ebSdanielk1977     pcache1Pagecount,        /* xPagecount */
1203bc2ca9ebSdanielk1977     pcache1Fetch,            /* xFetch */
1204bc2ca9ebSdanielk1977     pcache1Unpin,            /* xUnpin */
1205bc2ca9ebSdanielk1977     pcache1Rekey,            /* xRekey */
1206bc2ca9ebSdanielk1977     pcache1Truncate,         /* xTruncate */
120709419b4bSdrh     pcache1Destroy,          /* xDestroy */
120809419b4bSdrh     pcache1Shrink            /* xShrink */
1209bc2ca9ebSdanielk1977   };
121022e21ff4Sdan   sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
1211bc2ca9ebSdanielk1977 }
1212bc2ca9ebSdanielk1977 
1213def6889dSdrh /*
1214def6889dSdrh ** Return the size of the header on each page of this PCACHE implementation.
1215def6889dSdrh */
sqlite3HeaderSizePcache1(void)121637c057b8Sdrh int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
1217def6889dSdrh 
1218af89fe66Sdrh /*
1219af89fe66Sdrh ** Return the global mutex used by this PCACHE implementation.  The
1220af89fe66Sdrh ** sqlite3_status() routine needs access to this mutex.
1221af89fe66Sdrh */
sqlite3Pcache1Mutex(void)1222af89fe66Sdrh sqlite3_mutex *sqlite3Pcache1Mutex(void){
1223af89fe66Sdrh   return pcache1.mutex;
1224af89fe66Sdrh }
1225af89fe66Sdrh 
1226bc2ca9ebSdanielk1977 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
1227bc2ca9ebSdanielk1977 /*
1228bc2ca9ebSdanielk1977 ** This function is called to free superfluous dynamically allocated memory
1229bc2ca9ebSdanielk1977 ** held by the pager system. Memory in use by any SQLite pager allocated
1230bc2ca9ebSdanielk1977 ** by the current thread may be sqlite3_free()ed.
1231bc2ca9ebSdanielk1977 **
1232bc2ca9ebSdanielk1977 ** nReq is the number of bytes of memory required. Once this much has
1233bc2ca9ebSdanielk1977 ** been released, the function returns. The return value is the total number
1234bc2ca9ebSdanielk1977 ** of bytes of memory released.
1235bc2ca9ebSdanielk1977 */
sqlite3PcacheReleaseMemory(int nReq)1236bc2ca9ebSdanielk1977 int sqlite3PcacheReleaseMemory(int nReq){
1237bc2ca9ebSdanielk1977   int nFree = 0;
12389f8cf9daSdrh   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
12399f8cf9daSdrh   assert( sqlite3_mutex_notheld(pcache1.mutex) );
1240bf962287Sdrh   if( sqlite3GlobalConfig.pPage==0 ){
1241bc2ca9ebSdanielk1977     PgHdr1 *p;
12429f8cf9daSdrh     pcache1EnterMutex(&pcache1.grp);
124392af02c9Sdrh     while( (nReq<0 || nFree<nReq)
12448820250eSdrh        &&  (p=pcache1.grp.lru.pLruPrev)!=0
12458820250eSdrh        &&  p->isAnchor==0
124692af02c9Sdrh     ){
124722e21ff4Sdan       nFree += pcache1MemSize(p->page.pBuf);
1248eabb67fbSdrh       assert( PAGE_IS_UNPINNED(p) );
1249bc2ca9ebSdanielk1977       pcache1PinPage(p);
125095c91e14Sdrh       pcache1RemoveFromHash(p, 1);
1251bc2ca9ebSdanielk1977     }
12529f8cf9daSdrh     pcache1LeaveMutex(&pcache1.grp);
1253bc2ca9ebSdanielk1977   }
1254bc2ca9ebSdanielk1977   return nFree;
1255bc2ca9ebSdanielk1977 }
1256bc2ca9ebSdanielk1977 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
1257bc2ca9ebSdanielk1977 
1258bc2ca9ebSdanielk1977 #ifdef SQLITE_TEST
1259bc2ca9ebSdanielk1977 /*
1260bc2ca9ebSdanielk1977 ** This function is used by test procedures to inspect the internal state
1261bc2ca9ebSdanielk1977 ** of the global cache.
1262bc2ca9ebSdanielk1977 */
sqlite3PcacheStats(int * pnCurrent,int * pnMax,int * pnMin,int * pnRecyclable)1263bc2ca9ebSdanielk1977 void sqlite3PcacheStats(
1264bc2ca9ebSdanielk1977   int *pnCurrent,      /* OUT: Total number of pages cached */
1265bc2ca9ebSdanielk1977   int *pnMax,          /* OUT: Global maximum cache size */
1266bc2ca9ebSdanielk1977   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
1267bc2ca9ebSdanielk1977   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
1268bc2ca9ebSdanielk1977 ){
1269bc2ca9ebSdanielk1977   PgHdr1 *p;
1270bc2ca9ebSdanielk1977   int nRecyclable = 0;
12710b19c969Sdrh   for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
1272eabb67fbSdrh     assert( PAGE_IS_UNPINNED(p) );
1273bc2ca9ebSdanielk1977     nRecyclable++;
1274bc2ca9ebSdanielk1977   }
1275617b7b42Sdrh   *pnCurrent = pcache1.grp.nPurgeable;
1276a69085cfSdrh   *pnMax = (int)pcache1.grp.nMaxPage;
1277a69085cfSdrh   *pnMin = (int)pcache1.grp.nMinPage;
1278bc2ca9ebSdanielk1977   *pnRecyclable = nRecyclable;
1279bc2ca9ebSdanielk1977 }
1280bc2ca9ebSdanielk1977 #endif
1281