Index: src/pcache.c ================================================================== --- src/pcache.c +++ src/pcache.c @@ -9,11 +9,11 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file implements that page cache. ** -** @(#) $Id: pcache.c,v 1.12 2008/08/25 07:12:29 danielk1977 Exp $ +** @(#) $Id: pcache.c,v 1.13 2008/08/25 14:49:42 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** A complete page cache is an instance of this structure. @@ -109,30 +109,10 @@ */ #define pcacheEnterGlobal() sqlite3_mutex_enter(pcache.mutex_lru) #define pcacheExitGlobal() sqlite3_mutex_leave(pcache.mutex_lru) -/* -** Increment the reference count on both page p and its cache by n. -*/ -static void pcacheRef(PgHdr *p, int n){ - /* This next block assert()s that the number of references to the - ** PCache is the sum of the number of references to all pages in - ** the PCache. This is a bit expensive to leave turned on all the - ** time, even in debugging builds. - */ -#if 0 - PgHdr *pHdr; - int nRef = 0; - for(pHdr=p->pCache->pClean; pHdr; pHdr=pHdr->pNext) nRef += pHdr->nRef; - for(pHdr=p->pCache->pDirty; pHdr; pHdr=pHdr->pNext) nRef += pHdr->nRef; - assert( p->pCache->nRef==nRef ); -#endif - p->nRef += n; - p->pCache->nRef += n; -} - /********************************** Linked List Management ********************/ #ifndef NDEBUG /* ** This routine verifies that the number of entries in the hash table @@ -656,11 +636,13 @@ if( pPage->nRef==0 /* && (pPage->flags & PGHDR_DIRTY)==0 */ ){ pcacheEnterGlobal(); pcacheRemoveFromLruList(pPage); pcacheExitGlobal(); } - pcacheRef(pPage, 1); + if( (pPage->nRef++)==0 ){ + pCache->nRef++; + } *ppPage = pPage; return SQLITE_OK; } } } @@ -680,14 +662,14 @@ } pPage->pPager = 0; pPage->flags = 0; pPage->pDirty = 0; - pPage->nRef = 0; pPage->pgno = pgno; pPage->pCache = pCache; - pcacheRef(pPage, 1); + pPage->nRef = 1; + pCache->nRef++; pcacheAddToList(&pCache->pClean, pPage); pcacheAddToHash(pPage); }else{ *ppPage = 0; } @@ -700,26 +682,29 @@ ** move the page to the LRU list if it is clean. */ void sqlite3PcacheRelease(PgHdr *p){ assert( p->nRef>0 ); assert( p->pCache->iInUseDB || p->pCache->iInUseMM ); - pcacheRef(p, -1); - if( p->nRef!=0 ) return; - if( p->pCache->xDestroy ){ - p->pCache->xDestroy(p); - } + p->nRef--; + if( p->nRef==0 ){ + PCache *pCache = p->pCache; + pCache->nRef--; + if( p->pCache->xDestroy ){ + p->pCache->xDestroy(p); + } #if 0 - if( (p->flags & PGHDR_DIRTY)!=0 ) return; + if( (p->flags & PGHDR_DIRTY)!=0 ) return; #endif - pcacheEnterGlobal(); - pcacheAddToLruList(p); - pcacheExitGlobal(); + pcacheEnterGlobal(); + pcacheAddToLruList(p); + pcacheExitGlobal(); + } } void sqlite3PcacheRef(PgHdr *p){ - assert(p->nRef>=0); - pcacheRef(p, 1); + assert(p->nRef>0); + p->nRef++; } /* ** Drop a page from the cache. This should be the only reference to ** the page.