000001  /*
000002  ** 2001 September 15
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  **
000013  ** Memory allocation functions used throughout sqlite.
000014  */
000015  #include "sqliteInt.h"
000016  #include <stdarg.h>
000017  
000018  /*
000019  ** Attempt to release up to n bytes of non-essential memory currently
000020  ** held by SQLite. An example of non-essential memory is memory used to
000021  ** cache database pages that are not currently in use.
000022  */
000023  int sqlite3_release_memory(int n){
000024  #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
000025    return sqlite3PcacheReleaseMemory(n);
000026  #else
000027    /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
000028    ** is a no-op returning zero if SQLite is not compiled with
000029    ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
000030    UNUSED_PARAMETER(n);
000031    return 0;
000032  #endif
000033  }
000034  
000035  /*
000036  ** State information local to the memory allocation subsystem.
000037  */
000038  static SQLITE_WSD struct Mem0Global {
000039    sqlite3_mutex *mutex;         /* Mutex to serialize access */
000040    sqlite3_int64 alarmThreshold; /* The soft heap limit */
000041  
000042    /*
000043    ** True if heap is nearly "full" where "full" is defined by the
000044    ** sqlite3_soft_heap_limit() setting.
000045    */
000046    int nearlyFull;
000047  } mem0 = { 0, 0, 0 };
000048  
000049  #define mem0 GLOBAL(struct Mem0Global, mem0)
000050  
000051  /*
000052  ** Return the memory allocator mutex. sqlite3_status() needs it.
000053  */
000054  sqlite3_mutex *sqlite3MallocMutex(void){
000055    return mem0.mutex;
000056  }
000057  
000058  #ifndef SQLITE_OMIT_DEPRECATED
000059  /*
000060  ** Deprecated external interface.  It used to set an alarm callback
000061  ** that was invoked when memory usage grew too large.  Now it is a
000062  ** no-op.
000063  */
000064  int sqlite3_memory_alarm(
000065    void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
000066    void *pArg,
000067    sqlite3_int64 iThreshold
000068  ){
000069    (void)xCallback;
000070    (void)pArg;
000071    (void)iThreshold;
000072    return SQLITE_OK;
000073  }
000074  #endif
000075  
000076  /*
000077  ** Set the soft heap-size limit for the library. Passing a zero or 
000078  ** negative value indicates no limit.
000079  */
000080  sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
000081    sqlite3_int64 priorLimit;
000082    sqlite3_int64 excess;
000083    sqlite3_int64 nUsed;
000084  #ifndef SQLITE_OMIT_AUTOINIT
000085    int rc = sqlite3_initialize();
000086    if( rc ) return -1;
000087  #endif
000088    sqlite3_mutex_enter(mem0.mutex);
000089    priorLimit = mem0.alarmThreshold;
000090    if( n<0 ){
000091      sqlite3_mutex_leave(mem0.mutex);
000092      return priorLimit;
000093    }
000094    mem0.alarmThreshold = n;
000095    nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000096    mem0.nearlyFull = (n>0 && n<=nUsed);
000097    sqlite3_mutex_leave(mem0.mutex);
000098    excess = sqlite3_memory_used() - n;
000099    if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
000100    return priorLimit;
000101  }
000102  void sqlite3_soft_heap_limit(int n){
000103    if( n<0 ) n = 0;
000104    sqlite3_soft_heap_limit64(n);
000105  }
000106  
000107  /*
000108  ** Initialize the memory allocation subsystem.
000109  */
000110  int sqlite3MallocInit(void){
000111    int rc;
000112    if( sqlite3GlobalConfig.m.xMalloc==0 ){
000113      sqlite3MemSetDefault();
000114    }
000115    memset(&mem0, 0, sizeof(mem0));
000116    mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
000117    if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
000118        || sqlite3GlobalConfig.nPage<=0 ){
000119      sqlite3GlobalConfig.pPage = 0;
000120      sqlite3GlobalConfig.szPage = 0;
000121    }
000122    rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
000123    if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
000124    return rc;
000125  }
000126  
000127  /*
000128  ** Return true if the heap is currently under memory pressure - in other
000129  ** words if the amount of heap used is close to the limit set by
000130  ** sqlite3_soft_heap_limit().
000131  */
000132  int sqlite3HeapNearlyFull(void){
000133    return mem0.nearlyFull;
000134  }
000135  
000136  /*
000137  ** Deinitialize the memory allocation subsystem.
000138  */
000139  void sqlite3MallocEnd(void){
000140    if( sqlite3GlobalConfig.m.xShutdown ){
000141      sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
000142    }
000143    memset(&mem0, 0, sizeof(mem0));
000144  }
000145  
000146  /*
000147  ** Return the amount of memory currently checked out.
000148  */
000149  sqlite3_int64 sqlite3_memory_used(void){
000150    sqlite3_int64 res, mx;
000151    sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
000152    return res;
000153  }
000154  
000155  /*
000156  ** Return the maximum amount of memory that has ever been
000157  ** checked out since either the beginning of this process
000158  ** or since the most recent reset.
000159  */
000160  sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
000161    sqlite3_int64 res, mx;
000162    sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
000163    return mx;
000164  }
000165  
000166  /*
000167  ** Trigger the alarm 
000168  */
000169  static void sqlite3MallocAlarm(int nByte){
000170    if( mem0.alarmThreshold<=0 ) return;
000171    sqlite3_mutex_leave(mem0.mutex);
000172    sqlite3_release_memory(nByte);
000173    sqlite3_mutex_enter(mem0.mutex);
000174  }
000175  
000176  /*
000177  ** Do a memory allocation with statistics and alarms.  Assume the
000178  ** lock is already held.
000179  */
000180  static void mallocWithAlarm(int n, void **pp){
000181    void *p;
000182    int nFull;
000183    assert( sqlite3_mutex_held(mem0.mutex) );
000184    assert( n>0 );
000185  
000186    /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
000187    ** implementation of malloc_good_size(), which must be called in debug
000188    ** mode and specifically when the DMD "Dark Matter Detector" is enabled
000189    ** or else a crash results.  Hence, do not attempt to optimize out the
000190    ** following xRoundup() call. */
000191    nFull = sqlite3GlobalConfig.m.xRoundup(n);
000192  
000193  #ifdef SQLITE_MAX_MEMORY
000194    if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
000195      *pp = 0;
000196      return;
000197    }
000198  #endif
000199  
000200    sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
000201    if( mem0.alarmThreshold>0 ){
000202      sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000203      if( nUsed >= mem0.alarmThreshold - nFull ){
000204        mem0.nearlyFull = 1;
000205        sqlite3MallocAlarm(nFull);
000206      }else{
000207        mem0.nearlyFull = 0;
000208      }
000209    }
000210    p = sqlite3GlobalConfig.m.xMalloc(nFull);
000211  #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
000212    if( p==0 && mem0.alarmThreshold>0 ){
000213      sqlite3MallocAlarm(nFull);
000214      p = sqlite3GlobalConfig.m.xMalloc(nFull);
000215    }
000216  #endif
000217    if( p ){
000218      nFull = sqlite3MallocSize(p);
000219      sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
000220      sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
000221    }
000222    *pp = p;
000223  }
000224  
000225  /*
000226  ** Allocate memory.  This routine is like sqlite3_malloc() except that it
000227  ** assumes the memory subsystem has already been initialized.
000228  */
000229  void *sqlite3Malloc(u64 n){
000230    void *p;
000231    if( n==0 || n>=0x7fffff00 ){
000232      /* A memory allocation of a number of bytes which is near the maximum
000233      ** signed integer value might cause an integer overflow inside of the
000234      ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
000235      ** 255 bytes of overhead.  SQLite itself will never use anything near
000236      ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
000237      p = 0;
000238    }else if( sqlite3GlobalConfig.bMemstat ){
000239      sqlite3_mutex_enter(mem0.mutex);
000240      mallocWithAlarm((int)n, &p);
000241      sqlite3_mutex_leave(mem0.mutex);
000242    }else{
000243      p = sqlite3GlobalConfig.m.xMalloc((int)n);
000244    }
000245    assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
000246    return p;
000247  }
000248  
000249  /*
000250  ** This version of the memory allocation is for use by the application.
000251  ** First make sure the memory subsystem is initialized, then do the
000252  ** allocation.
000253  */
000254  void *sqlite3_malloc(int n){
000255  #ifndef SQLITE_OMIT_AUTOINIT
000256    if( sqlite3_initialize() ) return 0;
000257  #endif
000258    return n<=0 ? 0 : sqlite3Malloc(n);
000259  }
000260  void *sqlite3_malloc64(sqlite3_uint64 n){
000261  #ifndef SQLITE_OMIT_AUTOINIT
000262    if( sqlite3_initialize() ) return 0;
000263  #endif
000264    return sqlite3Malloc(n);
000265  }
000266  
000267  /*
000268  ** TRUE if p is a lookaside memory allocation from db
000269  */
000270  #ifndef SQLITE_OMIT_LOOKASIDE
000271  static int isLookaside(sqlite3 *db, void *p){
000272    return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
000273  }
000274  #else
000275  #define isLookaside(A,B) 0
000276  #endif
000277  
000278  /*
000279  ** Return the size of a memory allocation previously obtained from
000280  ** sqlite3Malloc() or sqlite3_malloc().
000281  */
000282  int sqlite3MallocSize(void *p){
000283    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000284    return sqlite3GlobalConfig.m.xSize(p);
000285  }
000286  int sqlite3DbMallocSize(sqlite3 *db, void *p){
000287    assert( p!=0 );
000288    if( db==0 || !isLookaside(db,p) ){
000289  #ifdef SQLITE_DEBUG
000290      if( db==0 ){
000291        assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000292        assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000293      }else{
000294        assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000295        assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000296      }
000297  #endif
000298      return sqlite3GlobalConfig.m.xSize(p);
000299    }else{
000300      assert( sqlite3_mutex_held(db->mutex) );
000301      return db->lookaside.sz;
000302    }
000303  }
000304  sqlite3_uint64 sqlite3_msize(void *p){
000305    assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000306    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000307    return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
000308  }
000309  
000310  /*
000311  ** Free memory previously obtained from sqlite3Malloc().
000312  */
000313  void sqlite3_free(void *p){
000314    if( p==0 ) return;  /* IMP: R-49053-54554 */
000315    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000316    assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000317    if( sqlite3GlobalConfig.bMemstat ){
000318      sqlite3_mutex_enter(mem0.mutex);
000319      sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
000320      sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
000321      sqlite3GlobalConfig.m.xFree(p);
000322      sqlite3_mutex_leave(mem0.mutex);
000323    }else{
000324      sqlite3GlobalConfig.m.xFree(p);
000325    }
000326  }
000327  
000328  /*
000329  ** Add the size of memory allocation "p" to the count in
000330  ** *db->pnBytesFreed.
000331  */
000332  static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
000333    *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
000334  }
000335  
000336  /*
000337  ** Free memory that might be associated with a particular database
000338  ** connection.  Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
000339  ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
000340  */
000341  void sqlite3DbFreeNN(sqlite3 *db, void *p){
000342    assert( db==0 || sqlite3_mutex_held(db->mutex) );
000343    assert( p!=0 );
000344    if( db ){
000345      if( db->pnBytesFreed ){
000346        measureAllocationSize(db, p);
000347        return;
000348      }
000349      if( isLookaside(db, p) ){
000350        LookasideSlot *pBuf = (LookasideSlot*)p;
000351  #ifdef SQLITE_DEBUG
000352        /* Trash all content in the buffer being freed */
000353        memset(p, 0xaa, db->lookaside.sz);
000354  #endif
000355        pBuf->pNext = db->lookaside.pFree;
000356        db->lookaside.pFree = pBuf;
000357        return;
000358      }
000359    }
000360    assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000361    assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000362    assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
000363    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000364    sqlite3_free(p);
000365  }
000366  void sqlite3DbFree(sqlite3 *db, void *p){
000367    assert( db==0 || sqlite3_mutex_held(db->mutex) );
000368    if( p ) sqlite3DbFreeNN(db, p);
000369  }
000370  
000371  /*
000372  ** Change the size of an existing memory allocation
000373  */
000374  void *sqlite3Realloc(void *pOld, u64 nBytes){
000375    int nOld, nNew, nDiff;
000376    void *pNew;
000377    assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
000378    assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
000379    if( pOld==0 ){
000380      return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
000381    }
000382    if( nBytes==0 ){
000383      sqlite3_free(pOld); /* IMP: R-26507-47431 */
000384      return 0;
000385    }
000386    if( nBytes>=0x7fffff00 ){
000387      /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
000388      return 0;
000389    }
000390    nOld = sqlite3MallocSize(pOld);
000391    /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
000392    ** argument to xRealloc is always a value returned by a prior call to
000393    ** xRoundup. */
000394    nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
000395    if( nOld==nNew ){
000396      pNew = pOld;
000397    }else if( sqlite3GlobalConfig.bMemstat ){
000398      sqlite3_mutex_enter(mem0.mutex);
000399      sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
000400      nDiff = nNew - nOld;
000401      if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
000402            mem0.alarmThreshold-nDiff ){
000403        sqlite3MallocAlarm(nDiff);
000404      }
000405      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000406      if( pNew==0 && mem0.alarmThreshold>0 ){
000407        sqlite3MallocAlarm((int)nBytes);
000408        pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000409      }
000410      if( pNew ){
000411        nNew = sqlite3MallocSize(pNew);
000412        sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
000413      }
000414      sqlite3_mutex_leave(mem0.mutex);
000415    }else{
000416      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000417    }
000418    assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
000419    return pNew;
000420  }
000421  
000422  /*
000423  ** The public interface to sqlite3Realloc.  Make sure that the memory
000424  ** subsystem is initialized prior to invoking sqliteRealloc.
000425  */
000426  void *sqlite3_realloc(void *pOld, int n){
000427  #ifndef SQLITE_OMIT_AUTOINIT
000428    if( sqlite3_initialize() ) return 0;
000429  #endif
000430    if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
000431    return sqlite3Realloc(pOld, n);
000432  }
000433  void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
000434  #ifndef SQLITE_OMIT_AUTOINIT
000435    if( sqlite3_initialize() ) return 0;
000436  #endif
000437    return sqlite3Realloc(pOld, n);
000438  }
000439  
000440  
000441  /*
000442  ** Allocate and zero memory.
000443  */ 
000444  void *sqlite3MallocZero(u64 n){
000445    void *p = sqlite3Malloc(n);
000446    if( p ){
000447      memset(p, 0, (size_t)n);
000448    }
000449    return p;
000450  }
000451  
000452  /*
000453  ** Allocate and zero memory.  If the allocation fails, make
000454  ** the mallocFailed flag in the connection pointer.
000455  */
000456  void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
000457    void *p;
000458    testcase( db==0 );
000459    p = sqlite3DbMallocRaw(db, n);
000460    if( p ) memset(p, 0, (size_t)n);
000461    return p;
000462  }
000463  
000464  
000465  /* Finish the work of sqlite3DbMallocRawNN for the unusual and
000466  ** slower case when the allocation cannot be fulfilled using lookaside.
000467  */
000468  static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
000469    void *p;
000470    assert( db!=0 );
000471    p = sqlite3Malloc(n);
000472    if( !p ) sqlite3OomFault(db);
000473    sqlite3MemdebugSetType(p, 
000474           (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
000475    return p;
000476  }
000477  
000478  /*
000479  ** Allocate memory, either lookaside (if possible) or heap.  
000480  ** If the allocation fails, set the mallocFailed flag in
000481  ** the connection pointer.
000482  **
000483  ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
000484  ** failure on the same database connection) then always return 0.
000485  ** Hence for a particular database connection, once malloc starts
000486  ** failing, it fails consistently until mallocFailed is reset.
000487  ** This is an important assumption.  There are many places in the
000488  ** code that do things like this:
000489  **
000490  **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
000491  **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
000492  **         if( b ) a[10] = 9;
000493  **
000494  ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
000495  ** that all prior mallocs (ex: "a") worked too.
000496  **
000497  ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
000498  ** not a NULL pointer.
000499  */
000500  void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
000501    void *p;
000502    if( db ) return sqlite3DbMallocRawNN(db, n);
000503    p = sqlite3Malloc(n);
000504    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000505    return p;
000506  }
000507  void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
000508  #ifndef SQLITE_OMIT_LOOKASIDE
000509    LookasideSlot *pBuf;
000510    assert( db!=0 );
000511    assert( sqlite3_mutex_held(db->mutex) );
000512    assert( db->pnBytesFreed==0 );
000513    if( db->lookaside.bDisable==0 ){
000514      assert( db->mallocFailed==0 );
000515      if( n>db->lookaside.sz ){
000516        db->lookaside.anStat[1]++;
000517      }else if( (pBuf = db->lookaside.pFree)!=0 ){
000518        db->lookaside.pFree = pBuf->pNext;
000519        db->lookaside.anStat[0]++;
000520        return (void*)pBuf;
000521      }else if( (pBuf = db->lookaside.pInit)!=0 ){
000522        db->lookaside.pInit = pBuf->pNext;
000523        db->lookaside.anStat[0]++;
000524        return (void*)pBuf;
000525      }else{
000526        db->lookaside.anStat[2]++;
000527      }
000528    }else if( db->mallocFailed ){
000529      return 0;
000530    }
000531  #else
000532    assert( db!=0 );
000533    assert( sqlite3_mutex_held(db->mutex) );
000534    assert( db->pnBytesFreed==0 );
000535    if( db->mallocFailed ){
000536      return 0;
000537    }
000538  #endif
000539    return dbMallocRawFinish(db, n);
000540  }
000541  
000542  /* Forward declaration */
000543  static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
000544  
000545  /*
000546  ** Resize the block of memory pointed to by p to n bytes. If the
000547  ** resize fails, set the mallocFailed flag in the connection object.
000548  */
000549  void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
000550    assert( db!=0 );
000551    if( p==0 ) return sqlite3DbMallocRawNN(db, n);
000552    assert( sqlite3_mutex_held(db->mutex) );
000553    if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
000554    return dbReallocFinish(db, p, n);
000555  }
000556  static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
000557    void *pNew = 0;
000558    assert( db!=0 );
000559    assert( p!=0 );
000560    if( db->mallocFailed==0 ){
000561      if( isLookaside(db, p) ){
000562        pNew = sqlite3DbMallocRawNN(db, n);
000563        if( pNew ){
000564          memcpy(pNew, p, db->lookaside.sz);
000565          sqlite3DbFree(db, p);
000566        }
000567      }else{
000568        assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000569        assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000570        sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000571        pNew = sqlite3_realloc64(p, n);
000572        if( !pNew ){
000573          sqlite3OomFault(db);
000574        }
000575        sqlite3MemdebugSetType(pNew,
000576              (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
000577      }
000578    }
000579    return pNew;
000580  }
000581  
000582  /*
000583  ** Attempt to reallocate p.  If the reallocation fails, then free p
000584  ** and set the mallocFailed flag in the database connection.
000585  */
000586  void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
000587    void *pNew;
000588    pNew = sqlite3DbRealloc(db, p, n);
000589    if( !pNew ){
000590      sqlite3DbFree(db, p);
000591    }
000592    return pNew;
000593  }
000594  
000595  /*
000596  ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
000597  ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
000598  ** is because when memory debugging is turned on, these two functions are 
000599  ** called via macros that record the current file and line number in the
000600  ** ThreadData structure.
000601  */
000602  char *sqlite3DbStrDup(sqlite3 *db, const char *z){
000603    char *zNew;
000604    size_t n;
000605    if( z==0 ){
000606      return 0;
000607    }
000608    n = strlen(z) + 1;
000609    zNew = sqlite3DbMallocRaw(db, n);
000610    if( zNew ){
000611      memcpy(zNew, z, n);
000612    }
000613    return zNew;
000614  }
000615  char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
000616    char *zNew;
000617    assert( db!=0 );
000618    if( z==0 ){
000619      return 0;
000620    }
000621    assert( (n&0x7fffffff)==n );
000622    zNew = sqlite3DbMallocRawNN(db, n+1);
000623    if( zNew ){
000624      memcpy(zNew, z, (size_t)n);
000625      zNew[n] = 0;
000626    }
000627    return zNew;
000628  }
000629  
000630  /*
000631  ** Free any prior content in *pz and replace it with a copy of zNew.
000632  */
000633  void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
000634    sqlite3DbFree(db, *pz);
000635    *pz = sqlite3DbStrDup(db, zNew);
000636  }
000637  
000638  /*
000639  ** Call this routine to record the fact that an OOM (out-of-memory) error
000640  ** has happened.  This routine will set db->mallocFailed, and also
000641  ** temporarily disable the lookaside memory allocator and interrupt
000642  ** any running VDBEs.
000643  */
000644  void sqlite3OomFault(sqlite3 *db){
000645    if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
000646      db->mallocFailed = 1;
000647      if( db->nVdbeExec>0 ){
000648        db->u1.isInterrupted = 1;
000649      }
000650      db->lookaside.bDisable++;
000651    }
000652  }
000653  
000654  /*
000655  ** This routine reactivates the memory allocator and clears the
000656  ** db->mallocFailed flag as necessary.
000657  **
000658  ** The memory allocator is not restarted if there are running
000659  ** VDBEs.
000660  */
000661  void sqlite3OomClear(sqlite3 *db){
000662    if( db->mallocFailed && db->nVdbeExec==0 ){
000663      db->mallocFailed = 0;
000664      db->u1.isInterrupted = 0;
000665      assert( db->lookaside.bDisable>0 );
000666      db->lookaside.bDisable--;
000667    }
000668  }
000669  
000670  /*
000671  ** Take actions at the end of an API call to indicate an OOM error
000672  */
000673  static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
000674    sqlite3OomClear(db);
000675    sqlite3Error(db, SQLITE_NOMEM);
000676    return SQLITE_NOMEM_BKPT;
000677  }
000678  
000679  /*
000680  ** This function must be called before exiting any API function (i.e. 
000681  ** returning control to the user) that has called sqlite3_malloc or
000682  ** sqlite3_realloc.
000683  **
000684  ** The returned value is normally a copy of the second argument to this
000685  ** function. However, if a malloc() failure has occurred since the previous
000686  ** invocation SQLITE_NOMEM is returned instead. 
000687  **
000688  ** If an OOM as occurred, then the connection error-code (the value
000689  ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
000690  */
000691  int sqlite3ApiExit(sqlite3* db, int rc){
000692    /* If the db handle must hold the connection handle mutex here.
000693    ** Otherwise the read (and possible write) of db->mallocFailed 
000694    ** is unsafe, as is the call to sqlite3Error().
000695    */
000696    assert( db!=0 );
000697    assert( sqlite3_mutex_held(db->mutex) );
000698    if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
000699      return apiOomError(db);
000700    }
000701    return rc & db->errMask;
000702  }