| Index: third_party/sqlite/src/src/pcache1.c
 | 
| diff --git a/third_party/sqlite/src/src/pcache1.c b/third_party/sqlite/src/src/pcache1.c
 | 
| index 036af54a23246ee0190a693b520cdf99dfdcd2aa..abf57dc34d0e29800acdcd3ff6781277083d8e61 100644
 | 
| --- a/third_party/sqlite/src/src/pcache1.c
 | 
| +++ b/third_party/sqlite/src/src/pcache1.c
 | 
| @@ -15,8 +15,6 @@
 | 
|  ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
 | 
|  ** If the default page cache implementation is overriden, then neither of
 | 
|  ** these two features are available.
 | 
| -**
 | 
| -** @(#) $Id: pcache1.c,v 1.19 2009/07/17 11:44:07 drh Exp $
 | 
|  */
 | 
|  
 | 
|  #include "sqliteInt.h"
 | 
| @@ -25,8 +23,13 @@ typedef struct PCache1 PCache1;
 | 
|  typedef struct PgHdr1 PgHdr1;
 | 
|  typedef struct PgFreeslot PgFreeslot;
 | 
|  
 | 
| -/* Pointers to structures of this type are cast and returned as 
 | 
| -** opaque sqlite3_pcache* handles
 | 
| +/* Each page cache is an instance of the following object.  Every
 | 
| +** open database file (including each in-memory database and each
 | 
| +** temporary or transient database) has a single page cache which
 | 
| +** is an instance of this object.
 | 
| +**
 | 
| +** Pointers to structures of this type are cast and returned as 
 | 
| +** opaque sqlite3_pcache* handles.
 | 
|  */
 | 
|  struct PCache1 {
 | 
|    /* Cache configuration parameters. Page size (szPage) and the purgeable
 | 
| @@ -86,6 +89,9 @@ static SQLITE_WSD struct PCacheGlobal {
 | 
|  
 | 
|    /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
 | 
|    int szSlot;                         /* Size of each free slot */
 | 
| +  int nSlot;                          /* The number of pcache slots */
 | 
| +  int nFreeSlot;                      /* Number of unused pcache slots */
 | 
| +  int nReserve;                       /* Try to keep nFreeSlot above this */
 | 
|    void *pStart, *pEnd;                /* Bounds of pagecache malloc range */
 | 
|    PgFreeslot *pFree;                  /* Free page blocks */
 | 
|    int isInit;                         /* True if initialized */
 | 
| @@ -133,6 +139,8 @@ void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
 | 
|      PgFreeslot *p;
 | 
|      sz = ROUNDDOWN8(sz);
 | 
|      pcache1.szSlot = sz;
 | 
| +    pcache1.nSlot = pcache1.nFreeSlot = n;
 | 
| +    pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
 | 
|      pcache1.pStart = pBuf;
 | 
|      pcache1.pFree = 0;
 | 
|      while( n-- ){
 | 
| @@ -154,11 +162,13 @@ void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
 | 
|  static void *pcache1Alloc(int nByte){
 | 
|    void *p;
 | 
|    assert( sqlite3_mutex_held(pcache1.mutex) );
 | 
| +  sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
 | 
|    if( nByte<=pcache1.szSlot && pcache1.pFree ){
 | 
|      assert( pcache1.isInit );
 | 
|      p = (PgHdr1 *)pcache1.pFree;
 | 
|      pcache1.pFree = pcache1.pFree->pNext;
 | 
| -    sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
 | 
| +    pcache1.nFreeSlot--;
 | 
| +    assert( pcache1.nFreeSlot>=0 );
 | 
|      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
 | 
|    }else{
 | 
|  
 | 
| @@ -175,6 +185,7 @@ static void *pcache1Alloc(int nByte){
 | 
|        int sz = sqlite3MallocSize(p);
 | 
|        sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
 | 
|      }
 | 
| +    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 | 
|    }
 | 
|    return p;
 | 
|  }
 | 
| @@ -191,13 +202,37 @@ static void pcache1Free(void *p){
 | 
|      pSlot = (PgFreeslot*)p;
 | 
|      pSlot->pNext = pcache1.pFree;
 | 
|      pcache1.pFree = pSlot;
 | 
| +    pcache1.nFreeSlot++;
 | 
| +    assert( pcache1.nFreeSlot<=pcache1.nSlot );
 | 
|    }else{
 | 
| -    int iSize = sqlite3MallocSize(p);
 | 
| +    int iSize;
 | 
| +    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 | 
| +    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 | 
| +    iSize = sqlite3MallocSize(p);
 | 
|      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
 | 
|      sqlite3_free(p);
 | 
|    }
 | 
|  }
 | 
|  
 | 
| +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 | 
| +/*
 | 
| +** Return the size of a pcache allocation
 | 
| +*/
 | 
| +static int pcache1MemSize(void *p){
 | 
| +  assert( sqlite3_mutex_held(pcache1.mutex) );
 | 
| +  if( p>=pcache1.pStart && p<pcache1.pEnd ){
 | 
| +    return pcache1.szSlot;
 | 
| +  }else{
 | 
| +    int iSize;
 | 
| +    assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
 | 
| +    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
 | 
| +    iSize = sqlite3MallocSize(p);
 | 
| +    sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
 | 
| +    return iSize;
 | 
| +  }
 | 
| +}
 | 
| +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 | 
| +
 | 
|  /*
 | 
|  ** Allocate a new page object initially associated with cache pCache.
 | 
|  */
 | 
| @@ -254,6 +289,32 @@ void sqlite3PageFree(void *p){
 | 
|    pcache1LeaveMutex();
 | 
|  }
 | 
|  
 | 
| +
 | 
| +/*
 | 
| +** Return true if it desirable to avoid allocating a new page cache
 | 
| +** entry.
 | 
| +**
 | 
| +** If memory was allocated specifically to the page cache using
 | 
| +** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
 | 
| +** it is desirable to avoid allocating a new page cache entry because
 | 
| +** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
 | 
| +** for all page cache needs and we should not need to spill the
 | 
| +** allocation onto the heap.
 | 
| +**
 | 
| +** Or, the heap is used for all page cache memory put the heap is
 | 
| +** under memory pressure, then again it is desirable to avoid
 | 
| +** allocating a new page cache entry in order to avoid stressing
 | 
| +** the heap even further.
 | 
| +*/
 | 
| +static int pcache1UnderMemoryPressure(PCache1 *pCache){
 | 
| +  assert( sqlite3_mutex_held(pcache1.mutex) );
 | 
| +  if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
 | 
| +    return pcache1.nFreeSlot<pcache1.nReserve;
 | 
| +  }else{
 | 
| +    return sqlite3HeapNearlyFull();
 | 
| +  }
 | 
| +}
 | 
| +
 | 
|  /******************************************************************************/
 | 
|  /******** General Implementation Functions ************************************/
 | 
|  
 | 
| @@ -495,14 +556,16 @@ static int pcache1Pagecount(sqlite3_pcache *p){
 | 
|  **   2. If createFlag==0 and the page is not already in the cache, NULL is
 | 
|  **      returned.
 | 
|  **
 | 
| -**   3. If createFlag is 1, and the page is not already in the cache,
 | 
| -**      and if either of the following are true, return NULL:
 | 
| +**   3. If createFlag is 1, and the page is not already in the cache, then
 | 
| +**      return NULL (do not allocate a new page) if any of the following
 | 
| +**      conditions are true:
 | 
|  **
 | 
|  **       (a) the number of pages pinned by the cache is greater than
 | 
|  **           PCache1.nMax, or
 | 
| +**
 | 
|  **       (b) the number of pages pinned by the cache is greater than
 | 
|  **           the sum of nMax for all purgeable caches, less the sum of 
 | 
| -**           nMin for all other purgeable caches. 
 | 
| +**           nMin for all other purgeable caches, or
 | 
|  **
 | 
|  **   4. If none of the first three conditions apply and the cache is marked
 | 
|  **      as purgeable, and if one of the following is true:
 | 
| @@ -514,6 +577,9 @@ static int pcache1Pagecount(sqlite3_pcache *p){
 | 
|  **           already equal to or greater than the sum of nMax for all
 | 
|  **           purgeable caches,
 | 
|  **
 | 
| +**       (c) The system is under memory pressure and wants to avoid
 | 
| +**           unnecessary pages cache entry allocations
 | 
| +**
 | 
|  **      then attempt to recycle a page from the LRU list. If it is the right
 | 
|  **      size, return the recycled buffer. Otherwise, free the buffer and
 | 
|  **      proceed to step 5. 
 | 
| @@ -545,6 +611,7 @@ static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
 | 
|    if( createFlag==1 && (
 | 
|          nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
 | 
|       || nPinned>=(pCache->nMax * 9 / 10)
 | 
| +     || pcache1UnderMemoryPressure(pCache)
 | 
|    )){
 | 
|      goto fetch_out;
 | 
|    }
 | 
| @@ -555,7 +622,9 @@ static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
 | 
|  
 | 
|    /* Step 4. Try to recycle a page buffer if appropriate. */
 | 
|    if( pCache->bPurgeable && pcache1.pLruTail && (
 | 
| -     (pCache->nPage+1>=pCache->nMax) || pcache1.nCurrentPage>=pcache1.nMaxPage
 | 
| +         (pCache->nPage+1>=pCache->nMax)
 | 
| +      || pcache1.nCurrentPage>=pcache1.nMaxPage
 | 
| +      || pcache1UnderMemoryPressure(pCache)
 | 
|    )){
 | 
|      pPage = pcache1.pLruTail;
 | 
|      pcache1RemoveFromHash(pPage);
 | 
| @@ -667,15 +736,7 @@ static void pcache1Rekey(
 | 
|    pPage->iKey = iNew;
 | 
|    pPage->pNext = pCache->apHash[h];
 | 
|    pCache->apHash[h] = pPage;
 | 
| -
 | 
| -  /* The xRekey() interface is only used to move pages earlier in the
 | 
| -  ** database file (in order to move all free pages to the end of the
 | 
| -  ** file where they can be truncated off.)  Hence, it is not possible
 | 
| -  ** for the new page number to be greater than the largest previously
 | 
| -  ** fetched page.  But we retain the following test in case xRekey()
 | 
| -  ** begins to be used in different ways in the future.
 | 
| -  */
 | 
| -  if( NEVER(iNew>pCache->iMaxKey) ){
 | 
| +  if( iNew>pCache->iMaxKey ){
 | 
|      pCache->iMaxKey = iNew;
 | 
|    }
 | 
|  
 | 
| @@ -706,6 +767,7 @@ static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
 | 
|  */
 | 
|  static void pcache1Destroy(sqlite3_pcache *p){
 | 
|    PCache1 *pCache = (PCache1 *)p;
 | 
| +  assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
 | 
|    pcache1EnterMutex();
 | 
|    pcache1TruncateUnsafe(pCache, 0);
 | 
|    pcache1.nMaxPage -= pCache->nMax;
 | 
| @@ -722,7 +784,7 @@ static void pcache1Destroy(sqlite3_pcache *p){
 | 
|  ** already provided an alternative.
 | 
|  */
 | 
|  void sqlite3PCacheSetDefault(void){
 | 
| -  static sqlite3_pcache_methods defaultMethods = {
 | 
| +  static const sqlite3_pcache_methods defaultMethods = {
 | 
|      0,                       /* pArg */
 | 
|      pcache1Init,             /* xInit */
 | 
|      pcache1Shutdown,         /* xShutdown */
 | 
| @@ -753,8 +815,8 @@ int sqlite3PcacheReleaseMemory(int nReq){
 | 
|    if( pcache1.pStart==0 ){
 | 
|      PgHdr1 *p;
 | 
|      pcache1EnterMutex();
 | 
| -    while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){
 | 
| -      nFree += sqlite3MallocSize(PGHDR1_TO_PAGE(p));
 | 
| +    while( (nReq<0 || nFree<nReq) && ((p=pcache1.pLruTail)!=0) ){
 | 
| +      nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
 | 
|        pcache1PinPage(p);
 | 
|        pcache1RemoveFromHash(p);
 | 
|        pcache1FreePage(p);
 | 
| 
 |