1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Modify pcache.c to work with OMIT_WSD. (CVS 5659)

FossilOrigin-Name: 44def90d1bd4e37ab162e16f83e23d32e86b78ab
This commit is contained in:
danielk1977
2008-09-02 09:38:06 +00:00
parent 78f82d1e6c
commit 5b775295f0
5 changed files with 94 additions and 87 deletions

View File

@@ -1,5 +1,5 @@
C Continuing\swork\son\sadding\sfull\ssupport\sfor\sthe\sSQLITE_OMIT_WSD\r\ncompile-time\soption.\s(CVS\s5658) C Modify\spcache.c\sto\swork\swith\sOMIT_WSD.\s(CVS\s5659)
D 2008-09-02T00:52:52 D 2008-09-02T09:38:07
F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
F Makefile.in 689e14735f862a5553bceef206d8c13e29504e44 F Makefile.in 689e14735f862a5553bceef206d8c13e29504e44
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -97,7 +97,7 @@ F src/attach.c db3f4a60538733c1e4dcb9d0217a6e0d6ccd615b
F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627 F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627
F src/bitvec.c 95c86bd18d8fedf0533f5af196192546e10a7e7d F src/bitvec.c 95c86bd18d8fedf0533f5af196192546e10a7e7d
F src/btmutex.c 709cad2cdca0afd013f0f612363810e53f59ec53 F src/btmutex.c 709cad2cdca0afd013f0f612363810e53f59ec53
F src/btree.c a12044e500d4cd8d030e6ce9d59855ada7008564 F src/btree.c 3c251cac0dbf763020e43aa66afa4a79356c4482
F src/btree.h 6371c5e599fab391a150c96afbc10062b276d107 F src/btree.h 6371c5e599fab391a150c96afbc10062b276d107
F src/btreeInt.h ab18c7b4980314e9e4b402e5dcde09f3c2545576 F src/btreeInt.h ab18c7b4980314e9e4b402e5dcde09f3c2545576
F src/build.c 160c71acca8f643f436ed6c1ee2f684c88df4dfe F src/build.c 160c71acca8f643f436ed6c1ee2f684c88df4dfe
@@ -116,7 +116,7 @@ F src/insert.c 110cca7845ed5a66c08fdd413b02e706ae34455f
F src/journal.c cffd2cd214e58c0e99c3ff632b3bee6c7cbb260e F src/journal.c cffd2cd214e58c0e99c3ff632b3bee6c7cbb260e
F src/legacy.c aac57bd984e666059011ea01ec4383892a253be3 F src/legacy.c aac57bd984e666059011ea01ec4383892a253be3
F src/loadext.c 6581a5bbc4d74fcd46dbbcfd695a6a5417f778a2 F src/loadext.c 6581a5bbc4d74fcd46dbbcfd695a6a5417f778a2
F src/main.c 47ce9da041a5aa9a1adadd92be795bde887e2dc9 F src/main.c 756541b024aeaedee0c803f33e06bf16aef19ab6
F src/malloc.c 2ffd62fb4a299cf3945e50a4c53d27ec1813f8b1 F src/malloc.c 2ffd62fb4a299cf3945e50a4c53d27ec1813f8b1
F src/mem1.c 5a529ff121c55ab067be14de00f86f6dcc4f4fb9 F src/mem1.c 5a529ff121c55ab067be14de00f86f6dcc4f4fb9
F src/mem2.c f87e681d0d1ed8436870d089332ed0d27d885b5c F src/mem2.c f87e681d0d1ed8436870d089332ed0d27d885b5c
@@ -138,7 +138,7 @@ F src/os_win.c 3209dc0ed734291764393ea8d534ba0d8696a540
F src/pager.c 032d11049af4ec49bdbaa3584e7ce9887098b66a F src/pager.c 032d11049af4ec49bdbaa3584e7ce9887098b66a
F src/pager.h c45380ca9d0933ea5bc4ecb3a43958b6d2ec5a9c F src/pager.h c45380ca9d0933ea5bc4ecb3a43958b6d2ec5a9c
F src/parse.y d0f76d2cb8d6883d5600dc20beb961a6022b94b8 F src/parse.y d0f76d2cb8d6883d5600dc20beb961a6022b94b8
F src/pcache.c 200de27e85b123840a30a11f5055544e06845569 F src/pcache.c ff79cb5eb797d0f1a244b4332c2d39cb5cfd066b
F src/pcache.h 53730c33310cdf7a5c94e8333c853d59a3b30226 F src/pcache.h 53730c33310cdf7a5c94e8333c853d59a3b30226
F src/pragma.c 9d8127ac1527617cfb31392cd8832bb2c9a02115 F src/pragma.c 9d8127ac1527617cfb31392cd8832bb2c9a02115
F src/prepare.c c197041e0c4770672cda75e6bfe10242f885e510 F src/prepare.c c197041e0c4770672cda75e6bfe10242f885e510
@@ -630,7 +630,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
P e0461f8755daa9f57e056810de9ea347ff8dd986 P ef26ea5c46d3915d206f8ff7f82a24f4c8955f1f
R c8df4ae84b8bc405c32994ac1b521fda R 7736deee443b4d2230f5bd18a13532e4
U drh U danielk1977
Z 207cb363f97da2fc9bf5418da8660bf8 Z eee1cf0558cd99e310064b03fee3f582

View File

@@ -1 +1 @@
ef26ea5c46d3915d206f8ff7f82a24f4c8955f1f 44def90d1bd4e37ab162e16f83e23d32e86b78ab

View File

@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give. ** May you share freely, never taking more than you give.
** **
************************************************************************* *************************************************************************
** $Id: btree.c,v 1.506 2008/09/02 00:52:52 drh Exp $ ** $Id: btree.c,v 1.507 2008/09/02 09:38:07 danielk1977 Exp $
** **
** This file implements a external (disk-based) database using BTrees. ** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information. ** See the header comment on "btreeInt.h" for additional information.

View File

@@ -14,7 +14,7 @@
** other files are for internal use by SQLite and should not be ** other files are for internal use by SQLite and should not be
** accessed by users of the library. ** accessed by users of the library.
** **
** $Id: main.c,v 1.495 2008/09/01 18:34:20 danielk1977 Exp $ ** $Id: main.c,v 1.496 2008/09/02 09:38:07 danielk1977 Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include <ctype.h> #include <ctype.h>
@@ -160,8 +160,8 @@ int sqlite3_initialize(void){
rc = sqlite3_os_init(); rc = sqlite3_os_init();
if( rc==SQLITE_OK ){ if( rc==SQLITE_OK ){
rc = sqlite3PcacheInitialize(); rc = sqlite3PcacheInitialize();
sqlite3PCacheBufferSetup(sqlite3GlobalConfig.pPage, sqlite3GlobalConfig.szPage, sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
sqlite3GlobalConfig.nPage); sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
} }
GLOBAL(int, inProgress) = 0; GLOBAL(int, inProgress) = 0;
sqlite3GlobalConfig.isInit = (rc==SQLITE_OK ? 1 : 0); sqlite3GlobalConfig.isInit = (rc==SQLITE_OK ? 1 : 0);

View File

@@ -11,7 +11,7 @@
************************************************************************* *************************************************************************
** This file implements that page cache. ** This file implements that page cache.
** **
** @(#) $Id: pcache.c,v 1.25 2008/09/01 18:34:20 danielk1977 Exp $ ** @(#) $Id: pcache.c,v 1.26 2008/09/02 09:38:07 danielk1977 Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
@@ -68,7 +68,7 @@ struct PgFreeslot {
/* /*
** Global data for the page cache. ** Global data for the page cache.
*/ */
static struct PCacheGlobal { static SQLITE_WSD struct PCacheGlobal {
int isInit; /* True when initialized */ int isInit; /* True when initialized */
sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */ sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
@@ -83,6 +83,13 @@ static struct PCacheGlobal {
PgFreeslot *pFree; /* Free page blocks */ PgFreeslot *pFree; /* Free page blocks */
} pcache = {0}; } pcache = {0};
/*
** All code in this file should access the global pcache structure via the
** alias "pcache_g". This ensures that the WSD emulation is used when
** compiling for systems that do not support real WSD.
*/
#define pcache_g (GLOBAL(struct PCacheGlobal, pcache))
/* /*
** All global variables used by this module (all of which are grouped ** All global variables used by this module (all of which are grouped
** together in global structure "pcache" above) are protected by the static ** together in global structure "pcache" above) are protected by the static
@@ -99,9 +106,9 @@ static struct PCacheGlobal {
** entered more than once. The pcacheMutexHeld() macro should only be ** entered more than once. The pcacheMutexHeld() macro should only be
** used within an assert() to verify that the mutex is being held. ** used within an assert() to verify that the mutex is being held.
*/ */
#define pcacheEnterMutex() sqlite3_mutex_enter(pcache.mutex) #define pcacheEnterMutex() sqlite3_mutex_enter(pcache_g.mutex)
#define pcacheExitMutex() sqlite3_mutex_leave(pcache.mutex) #define pcacheExitMutex() sqlite3_mutex_leave(pcache_g.mutex)
#define pcacheMutexHeld() sqlite3_mutex_held(pcache.mutex) #define pcacheMutexHeld() sqlite3_mutex_held(pcache_g.mutex)
/* /*
** Some of the assert() macros in this code are too expensive to run ** Some of the assert() macros in this code are too expensive to run
@@ -317,22 +324,22 @@ static void pcacheAddToList(PgHdr **ppHead, PgHdr *pPage){
** Remove a page from the global LRU list ** Remove a page from the global LRU list
*/ */
static void pcacheRemoveFromLruList(PgHdr *pPage){ static void pcacheRemoveFromLruList(PgHdr *pPage){
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
assert( (pPage->flags&PGHDR_DIRTY)==0 ); assert( (pPage->flags&PGHDR_DIRTY)==0 );
if( pPage->pCache->bPurgeable==0 ) return; if( pPage->pCache->bPurgeable==0 ) return;
if( pPage->pNextLru ){ if( pPage->pNextLru ){
assert( pcache.pLruTail!=pPage ); assert( pcache_g.pLruTail!=pPage );
pPage->pNextLru->pPrevLru = pPage->pPrevLru; pPage->pNextLru->pPrevLru = pPage->pPrevLru;
}else{ }else{
assert( pcache.pLruTail==pPage ); assert( pcache_g.pLruTail==pPage );
pcache.pLruTail = pPage->pPrevLru; pcache_g.pLruTail = pPage->pPrevLru;
} }
if( pPage->pPrevLru ){ if( pPage->pPrevLru ){
assert( pcache.pLruHead!=pPage ); assert( pcache_g.pLruHead!=pPage );
pPage->pPrevLru->pNextLru = pPage->pNextLru; pPage->pPrevLru->pNextLru = pPage->pNextLru;
}else{ }else{
assert( pcache.pLruHead==pPage ); assert( pcache_g.pLruHead==pPage );
pcache.pLruHead = pPage->pNextLru; pcache_g.pLruHead = pPage->pNextLru;
} }
} }
@@ -343,31 +350,31 @@ static void pcacheRemoveFromLruList(PgHdr *pPage){
** to the end of the LRU list so that it will be the next to be recycled. ** to the end of the LRU list so that it will be the next to be recycled.
*/ */
static void pcacheAddToLruList(PgHdr *pPage){ static void pcacheAddToLruList(PgHdr *pPage){
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
assert( (pPage->flags&PGHDR_DIRTY)==0 ); assert( (pPage->flags&PGHDR_DIRTY)==0 );
if( pPage->pCache->bPurgeable==0 ) return; if( pPage->pCache->bPurgeable==0 ) return;
if( pcache.pLruTail && (pPage->flags & PGHDR_REUSE_UNLIKELY)!=0 ){ if( pcache_g.pLruTail && (pPage->flags & PGHDR_REUSE_UNLIKELY)!=0 ){
/* If reuse is unlikely. Put the page at the end of the LRU list /* If reuse is unlikely. Put the page at the end of the LRU list
** where it will be recycled sooner rather than later. ** where it will be recycled sooner rather than later.
*/ */
assert( pcache.pLruHead ); assert( pcache_g.pLruHead );
pPage->pNextLru = 0; pPage->pNextLru = 0;
pPage->pPrevLru = pcache.pLruTail; pPage->pPrevLru = pcache_g.pLruTail;
pcache.pLruTail->pNextLru = pPage; pcache_g.pLruTail->pNextLru = pPage;
pcache.pLruTail = pPage; pcache_g.pLruTail = pPage;
pPage->flags &= ~PGHDR_REUSE_UNLIKELY; pPage->flags &= ~PGHDR_REUSE_UNLIKELY;
}else{ }else{
/* If reuse is possible. the page goes at the beginning of the LRU /* If reuse is possible. the page goes at the beginning of the LRU
** list so that it will be the last to be recycled. ** list so that it will be the last to be recycled.
*/ */
if( pcache.pLruHead ){ if( pcache_g.pLruHead ){
pcache.pLruHead->pPrevLru = pPage; pcache_g.pLruHead->pPrevLru = pPage;
} }
pPage->pNextLru = pcache.pLruHead; pPage->pNextLru = pcache_g.pLruHead;
pcache.pLruHead = pPage; pcache_g.pLruHead = pPage;
pPage->pPrevLru = 0; pPage->pPrevLru = 0;
if( pcache.pLruTail==0 ){ if( pcache_g.pLruTail==0 ){
pcache.pLruTail = pPage; pcache_g.pLruTail = pPage;
} }
} }
} }
@@ -382,16 +389,16 @@ static void pcacheAddToLruList(PgHdr *pPage){
void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
PgFreeslot *p; PgFreeslot *p;
sz &= ~7; sz &= ~7;
pcache.szSlot = sz; pcache_g.szSlot = sz;
pcache.pStart = pBuf; pcache_g.pStart = pBuf;
pcache.pFree = 0; pcache_g.pFree = 0;
while( n-- ){ while( n-- ){
p = (PgFreeslot*)pBuf; p = (PgFreeslot*)pBuf;
p->pNext = pcache.pFree; p->pNext = pcache_g.pFree;
pcache.pFree = p; pcache_g.pFree = p;
pBuf = (void*)&((char*)pBuf)[sz]; pBuf = (void*)&((char*)pBuf)[sz];
} }
pcache.pEnd = pBuf; pcache_g.pEnd = pBuf;
} }
/* /*
@@ -400,10 +407,10 @@ void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
** in the page cache memory pool, go to the general purpose memory allocator. ** in the page cache memory pool, go to the general purpose memory allocator.
*/ */
void *pcacheMalloc(int sz, PCache *pCache){ void *pcacheMalloc(int sz, PCache *pCache){
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
if( sz<=pcache.szSlot && pcache.pFree ){ if( sz<=pcache_g.szSlot && pcache_g.pFree ){
PgFreeslot *p = pcache.pFree; PgFreeslot *p = pcache_g.pFree;
pcache.pFree = p->pNext; pcache_g.pFree = p->pNext;
sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, sz); sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, sz);
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
return (void*)p; return (void*)p;
@@ -439,14 +446,14 @@ void *sqlite3PageMalloc(sz){
** Release a pager memory allocation ** Release a pager memory allocation
*/ */
void pcacheFree(void *p){ void pcacheFree(void *p){
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
if( p==0 ) return; if( p==0 ) return;
if( p>=pcache.pStart && p<pcache.pEnd ){ if( p>=pcache_g.pStart && p<pcache_g.pEnd ){
PgFreeslot *pSlot; PgFreeslot *pSlot;
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
pSlot = (PgFreeslot*)p; pSlot = (PgFreeslot*)p;
pSlot->pNext = pcache.pFree; pSlot->pNext = pcache_g.pFree;
pcache.pFree = pSlot; pcache_g.pFree = pSlot;
}else{ }else{
int iSize = sqlite3MallocSize(p); int iSize = sqlite3MallocSize(p);
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
@@ -465,14 +472,14 @@ void sqlite3PageFree(void *p){
static PgHdr *pcachePageAlloc(PCache *pCache){ static PgHdr *pcachePageAlloc(PCache *pCache){
PgHdr *p; PgHdr *p;
int sz = sizeof(*p) + pCache->szPage + pCache->szExtra; int sz = sizeof(*p) + pCache->szPage + pCache->szExtra;
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
p = pcacheMalloc(sz, pCache); p = pcacheMalloc(sz, pCache);
if( p==0 ) return 0; if( p==0 ) return 0;
memset(p, 0, sizeof(PgHdr)); memset(p, 0, sizeof(PgHdr));
p->pData = (void*)&p[1]; p->pData = (void*)&p[1];
p->pExtra = (void*)&((char*)p->pData)[pCache->szPage]; p->pExtra = (void*)&((char*)p->pData)[pCache->szPage];
if( pCache->bPurgeable ){ if( pCache->bPurgeable ){
pcache.nCurrentPage++; pcache_g.nCurrentPage++;
} }
return p; return p;
} }
@@ -481,9 +488,9 @@ static PgHdr *pcachePageAlloc(PCache *pCache){
** Deallocate a page ** Deallocate a page
*/ */
static void pcachePageFree(PgHdr *p){ static void pcachePageFree(PgHdr *p){
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
if( p->pCache->bPurgeable ){ if( p->pCache->bPurgeable ){
pcache.nCurrentPage--; pcache_g.nCurrentPage--;
} }
pcacheFree(p->apSave[0]); pcacheFree(p->apSave[0]);
pcacheFree(p->apSave[1]); pcacheFree(p->apSave[1]);
@@ -496,8 +503,8 @@ static void pcachePageFree(PgHdr *p){
** the argument is passed to pcachePageFree(). ** the argument is passed to pcachePageFree().
*/ */
static int pcachePageSize(PgHdr *p){ static int pcachePageSize(PgHdr *p){
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
assert( !pcache.pStart ); assert( !pcache_g.pStart );
assert( p->apSave[0]==0 ); assert( p->apSave[0]==0 );
assert( p->apSave[1]==0 ); assert( p->apSave[1]==0 );
assert( p && p->pCache ); assert( p && p->pCache );
@@ -518,9 +525,9 @@ static int pcachePageSize(PgHdr *p){
*/ */
static PgHdr *pcacheRecyclePage(){ static PgHdr *pcacheRecyclePage(){
PgHdr *p = 0; PgHdr *p = 0;
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
if( (p=pcache.pLruTail) ){ if( (p=pcache_g.pLruTail) ){
assert( (p->flags&PGHDR_DIRTY)==0 ); assert( (p->flags&PGHDR_DIRTY)==0 );
pcacheRemoveFromLruList(p); pcacheRemoveFromLruList(p);
pcacheRemoveFromHash(p); pcacheRemoveFromHash(p);
@@ -543,8 +550,8 @@ static int pcacheRecycleOrAlloc(PCache *pCache, PgHdr **ppPage){
int szPage = pCache->szPage; int szPage = pCache->szPage;
int szExtra = pCache->szExtra; int szExtra = pCache->szExtra;
assert( pcache.isInit ); assert( pcache_g.isInit );
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
*ppPage = 0; *ppPage = 0;
@@ -556,7 +563,7 @@ static int pcacheRecycleOrAlloc(PCache *pCache, PgHdr **ppPage){
expensive_assert( pcacheCheckSynced(pCache) ); expensive_assert( pcacheCheckSynced(pCache) );
if( pCache->xStress if( pCache->xStress
&& pCache->pDirty && pCache->pDirty
&& pCache->nPinned>=(pcache.nMaxPage+pCache->nMin-pcache.nMinPage) && pCache->nPinned>=(pcache_g.nMaxPage+pCache->nMin-pcache_g.nMinPage)
){ ){
PgHdr *pPg; PgHdr *pPg;
assert(pCache->pDirtyTail); assert(pCache->pDirtyTail);
@@ -580,7 +587,7 @@ static int pcacheRecycleOrAlloc(PCache *pCache, PgHdr **ppPage){
} }
/* If the global page limit has been reached, try to recycle a page. */ /* If the global page limit has been reached, try to recycle a page. */
if( pCache->bPurgeable && pcache.nCurrentPage>=pcache.nMaxPage ){ if( pCache->bPurgeable && pcache_g.nCurrentPage>=pcache_g.nMaxPage ){
p = pcacheRecyclePage(); p = pcacheRecyclePage();
} }
@@ -604,19 +611,19 @@ static int pcacheRecycleOrAlloc(PCache *pCache, PgHdr **ppPage){
** functions are threadsafe. ** functions are threadsafe.
*/ */
int sqlite3PcacheInitialize(void){ int sqlite3PcacheInitialize(void){
assert( pcache.isInit==0 ); assert( pcache_g.isInit==0 );
memset(&pcache, 0, sizeof(pcache)); memset(&pcache_g, 0, sizeof(pcache));
if( sqlite3GlobalConfig.bCoreMutex ){ if( sqlite3GlobalConfig.bCoreMutex ){
/* No need to check the return value of sqlite3_mutex_alloc(). /* No need to check the return value of sqlite3_mutex_alloc().
** Allocating a static mutex cannot fail. ** Allocating a static mutex cannot fail.
*/ */
pcache.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU); pcache_g.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
} }
pcache.isInit = 1; pcache_g.isInit = 1;
return SQLITE_OK; return SQLITE_OK;
} }
void sqlite3PcacheShutdown(void){ void sqlite3PcacheShutdown(void){
memset(&pcache, 0, sizeof(pcache)); memset(&pcache_g, 0, sizeof(pcache));
} }
/* /*
@@ -637,7 +644,7 @@ void sqlite3PcacheOpen(
void *pStress, /* Argument to xStress */ void *pStress, /* Argument to xStress */
PCache *p /* Preallocated space for the PCache */ PCache *p /* Preallocated space for the PCache */
){ ){
assert( pcache.isInit ); assert( pcache_g.isInit );
memset(p, 0, sizeof(PCache)); memset(p, 0, sizeof(PCache));
p->szPage = szPage; p->szPage = szPage;
p->szExtra = szExtra; p->szExtra = szExtra;
@@ -650,8 +657,8 @@ void sqlite3PcacheOpen(
pcacheEnterMutex(); pcacheEnterMutex();
if( bPurgeable ){ if( bPurgeable ){
pcache.nMaxPage += p->nMax; pcache_g.nMaxPage += p->nMax;
pcache.nMinPage += p->nMin; pcache_g.nMinPage += p->nMin;
} }
pcacheExitMutex(); pcacheExitMutex();
@@ -678,7 +685,7 @@ int sqlite3PcacheFetch(
int rc = SQLITE_OK; int rc = SQLITE_OK;
PgHdr *pPage = 0; PgHdr *pPage = 0;
assert( pcache.isInit ); assert( pcache_g.isInit );
assert( pCache!=0 ); assert( pCache!=0 );
assert( pgno>0 ); assert( pgno>0 );
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) ); expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
@@ -748,7 +755,7 @@ void sqlite3PcacheRelease(PgHdr *p){
if( (p->flags&PGHDR_DIRTY)==0 ){ if( (p->flags&PGHDR_DIRTY)==0 ){
pCache->nPinned--; pCache->nPinned--;
pcacheEnterMutex(); pcacheEnterMutex();
if( pcache.nCurrentPage>pcache.nMaxPage ){ if( pcache_g.nCurrentPage>pcache_g.nMaxPage ){
pcacheRemoveFromList(&pCache->pClean, p); pcacheRemoveFromList(&pCache->pClean, p);
pcacheRemoveFromHash(p); pcacheRemoveFromHash(p);
pcachePageFree(p); pcachePageFree(p);
@@ -882,7 +889,7 @@ void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
*/ */
void pcacheClear(PCache *pCache){ void pcacheClear(PCache *pCache){
PgHdr *p, *pNext; PgHdr *p, *pNext;
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
for(p=pCache->pClean; p; p=pNext){ for(p=pCache->pClean; p; p=pNext){
pNext = p->pNext; pNext = p->pNext;
pcacheRemoveFromLruList(p); pcacheRemoveFromLruList(p);
@@ -942,8 +949,8 @@ void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
*/ */
static void pcacheEnforceMaxPage(){ static void pcacheEnforceMaxPage(){
PgHdr *p; PgHdr *p;
assert( sqlite3_mutex_held(pcache.mutex) ); assert( sqlite3_mutex_held(pcache_g.mutex) );
while( pcache.nCurrentPage>pcache.nMaxPage && (p = pcacheRecyclePage()) ){ while( pcache_g.nCurrentPage>pcache_g.nMaxPage && (p = pcacheRecyclePage()) ){
pcachePageFree(p); pcachePageFree(p);
} }
} }
@@ -957,8 +964,8 @@ void sqlite3PcacheClose(PCache *pCache){
/* Free all the pages used by this pager and remove them from the LRU list. */ /* Free all the pages used by this pager and remove them from the LRU list. */
pcacheClear(pCache); pcacheClear(pCache);
if( pCache->bPurgeable ){ if( pCache->bPurgeable ){
pcache.nMaxPage -= pCache->nMax; pcache_g.nMaxPage -= pCache->nMax;
pcache.nMinPage -= pCache->nMin; pcache_g.nMinPage -= pCache->nMin;
pcacheEnforceMaxPage(); pcacheEnforceMaxPage();
} }
sqlite3_free(pCache->apHash); sqlite3_free(pCache->apHash);
@@ -1208,8 +1215,8 @@ void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
} }
if( pCache->bPurgeable ){ if( pCache->bPurgeable ){
pcacheEnterMutex(); pcacheEnterMutex();
pcache.nMaxPage -= pCache->nMax; pcache_g.nMaxPage -= pCache->nMax;
pcache.nMaxPage += mxPage; pcache_g.nMaxPage += mxPage;
pcacheEnforceMaxPage(); pcacheEnforceMaxPage();
pcacheExitMutex(); pcacheExitMutex();
} }
@@ -1228,7 +1235,7 @@ void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
*/ */
int sqlite3PcacheReleaseMemory(int nReq){ int sqlite3PcacheReleaseMemory(int nReq){
int nFree = 0; int nFree = 0;
if( pcache.pStart==0 ){ if( pcache_g.pStart==0 ){
PgHdr *p; PgHdr *p;
pcacheEnterMutex(); pcacheEnterMutex();
while( (nReq<0 || nFree<nReq) && (p=pcacheRecyclePage()) ){ while( (nReq<0 || nFree<nReq) && (p=pcacheRecyclePage()) ){
@@ -1250,13 +1257,13 @@ void sqlite3PcacheStats(
){ ){
PgHdr *p; PgHdr *p;
int nRecyclable = 0; int nRecyclable = 0;
for(p=pcache.pLruHead; p; p=p->pNextLru){ for(p=pcache_g.pLruHead; p; p=p->pNextLru){
nRecyclable++; nRecyclable++;
} }
*pnCurrent = pcache.nCurrentPage; *pnCurrent = pcache_g.nCurrentPage;
*pnMax = pcache.nMaxPage; *pnMax = pcache_g.nMaxPage;
*pnMin = pcache.nMinPage; *pnMin = pcache_g.nMinPage;
*pnRecyclable = nRecyclable; *pnRecyclable = nRecyclable;
} }
#endif #endif