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  ** Default value of the hard heap limit.  0 means "no limit".
000037  */
000038  #ifndef SQLITE_MAX_MEMORY
000039  # define SQLITE_MAX_MEMORY 0
000040  #endif
000041  
000042  /*
000043  ** State information local to the memory allocation subsystem.
000044  */
000045  static SQLITE_WSD struct Mem0Global {
000046    sqlite3_mutex *mutex;         /* Mutex to serialize access */
000047    sqlite3_int64 alarmThreshold; /* The soft heap limit */
000048    sqlite3_int64 hardLimit;      /* The hard upper bound on memory */
000049  
000050    /*
000051    ** True if heap is nearly "full" where "full" is defined by the
000052    ** sqlite3_soft_heap_limit() setting.
000053    */
000054    int nearlyFull;
000055  } mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
000056  
000057  #define mem0 GLOBAL(struct Mem0Global, mem0)
000058  
000059  /*
000060  ** Return the memory allocator mutex. sqlite3_status() needs it.
000061  */
000062  sqlite3_mutex *sqlite3MallocMutex(void){
000063    return mem0.mutex;
000064  }
000065  
000066  #ifndef SQLITE_OMIT_DEPRECATED
000067  /*
000068  ** Deprecated external interface.  It used to set an alarm callback
000069  ** that was invoked when memory usage grew too large.  Now it is a
000070  ** no-op.
000071  */
000072  int sqlite3_memory_alarm(
000073    void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
000074    void *pArg,
000075    sqlite3_int64 iThreshold
000076  ){
000077    (void)xCallback;
000078    (void)pArg;
000079    (void)iThreshold;
000080    return SQLITE_OK;
000081  }
000082  #endif
000083  
000084  /*
000085  ** Set the soft heap-size limit for the library.  An argument of
000086  ** zero disables the limit.  A negative argument is a no-op used to
000087  ** obtain the return value.
000088  **
000089  ** The return value is the value of the heap limit just before this
000090  ** interface was called.
000091  **
000092  ** If the hard heap limit is enabled, then the soft heap limit cannot
000093  ** be disabled nor raised above the hard heap limit.
000094  */
000095  sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
000096    sqlite3_int64 priorLimit;
000097    sqlite3_int64 excess;
000098    sqlite3_int64 nUsed;
000099  #ifndef SQLITE_OMIT_AUTOINIT
000100    int rc = sqlite3_initialize();
000101    if( rc ) return -1;
000102  #endif
000103    sqlite3_mutex_enter(mem0.mutex);
000104    priorLimit = mem0.alarmThreshold;
000105    if( n<0 ){
000106      sqlite3_mutex_leave(mem0.mutex);
000107      return priorLimit;
000108    }
000109    if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
000110      n = mem0.hardLimit;
000111    }
000112    mem0.alarmThreshold = n;
000113    nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000114    AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed);
000115    sqlite3_mutex_leave(mem0.mutex);
000116    excess = sqlite3_memory_used() - n;
000117    if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
000118    return priorLimit;
000119  }
000120  void sqlite3_soft_heap_limit(int n){
000121    if( n<0 ) n = 0;
000122    sqlite3_soft_heap_limit64(n);
000123  }
000124  
000125  /*
000126  ** Set the hard heap-size limit for the library. An argument of zero
000127  ** disables the hard heap limit.  A negative argument is a no-op used
000128  ** to obtain the return value without affecting the hard heap limit.
000129  **
000130  ** The return value is the value of the hard heap limit just prior to
000131  ** calling this interface.
000132  **
000133  ** Setting the hard heap limit will also activate the soft heap limit
000134  ** and constrain the soft heap limit to be no more than the hard heap
000135  ** limit.
000136  */
000137  sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
000138    sqlite3_int64 priorLimit;
000139  #ifndef SQLITE_OMIT_AUTOINIT
000140    int rc = sqlite3_initialize();
000141    if( rc ) return -1;
000142  #endif
000143    sqlite3_mutex_enter(mem0.mutex);
000144    priorLimit = mem0.hardLimit;
000145    if( n>=0 ){
000146      mem0.hardLimit = n;
000147      if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
000148        mem0.alarmThreshold = n;
000149      }
000150    }
000151    sqlite3_mutex_leave(mem0.mutex);
000152    return priorLimit;
000153  }
000154  
000155  
000156  /*
000157  ** Initialize the memory allocation subsystem.
000158  */
000159  int sqlite3MallocInit(void){
000160    int rc;
000161    if( sqlite3GlobalConfig.m.xMalloc==0 ){
000162      sqlite3MemSetDefault();
000163    }
000164    mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
000165    if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
000166        || sqlite3GlobalConfig.nPage<=0 ){
000167      sqlite3GlobalConfig.pPage = 0;
000168      sqlite3GlobalConfig.szPage = 0;
000169    }
000170    rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
000171    if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
000172    return rc;
000173  }
000174  
000175  /*
000176  ** Return true if the heap is currently under memory pressure - in other
000177  ** words if the amount of heap used is close to the limit set by
000178  ** sqlite3_soft_heap_limit().
000179  */
000180  int sqlite3HeapNearlyFull(void){
000181    return AtomicLoad(&mem0.nearlyFull);
000182  }
000183  
000184  /*
000185  ** Deinitialize the memory allocation subsystem.
000186  */
000187  void sqlite3MallocEnd(void){
000188    if( sqlite3GlobalConfig.m.xShutdown ){
000189      sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
000190    }
000191    memset(&mem0, 0, sizeof(mem0));
000192  }
000193  
000194  /*
000195  ** Return the amount of memory currently checked out.
000196  */
000197  sqlite3_int64 sqlite3_memory_used(void){
000198    sqlite3_int64 res, mx;
000199    sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
000200    return res;
000201  }
000202  
000203  /*
000204  ** Return the maximum amount of memory that has ever been
000205  ** checked out since either the beginning of this process
000206  ** or since the most recent reset.
000207  */
000208  sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
000209    sqlite3_int64 res, mx;
000210    sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
000211    return mx;
000212  }
000213  
000214  /*
000215  ** Trigger the alarm 
000216  */
000217  static void sqlite3MallocAlarm(int nByte){
000218    if( mem0.alarmThreshold<=0 ) return;
000219    sqlite3_mutex_leave(mem0.mutex);
000220    sqlite3_release_memory(nByte);
000221    sqlite3_mutex_enter(mem0.mutex);
000222  }
000223  
000224  /*
000225  ** Do a memory allocation with statistics and alarms.  Assume the
000226  ** lock is already held.
000227  */
000228  static void mallocWithAlarm(int n, void **pp){
000229    void *p;
000230    int nFull;
000231    assert( sqlite3_mutex_held(mem0.mutex) );
000232    assert( n>0 );
000233  
000234    /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
000235    ** implementation of malloc_good_size(), which must be called in debug
000236    ** mode and specifically when the DMD "Dark Matter Detector" is enabled
000237    ** or else a crash results.  Hence, do not attempt to optimize out the
000238    ** following xRoundup() call. */
000239    nFull = sqlite3GlobalConfig.m.xRoundup(n);
000240  
000241    sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
000242    if( mem0.alarmThreshold>0 ){
000243      sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000244      if( nUsed >= mem0.alarmThreshold - nFull ){
000245        AtomicStore(&mem0.nearlyFull, 1);
000246        sqlite3MallocAlarm(nFull);
000247        if( mem0.hardLimit ){
000248          nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000249          if( nUsed >= mem0.hardLimit - nFull ){
000250            *pp = 0;
000251            return;
000252          }
000253        }
000254      }else{
000255        AtomicStore(&mem0.nearlyFull, 0);
000256      }
000257    }
000258    p = sqlite3GlobalConfig.m.xMalloc(nFull);
000259  #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
000260    if( p==0 && mem0.alarmThreshold>0 ){
000261      sqlite3MallocAlarm(nFull);
000262      p = sqlite3GlobalConfig.m.xMalloc(nFull);
000263    }
000264  #endif
000265    if( p ){
000266      nFull = sqlite3MallocSize(p);
000267      sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
000268      sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
000269    }
000270    *pp = p;
000271  }
000272  
000273  /*
000274  ** Maximum size of any single memory allocation.
000275  **
000276  ** This is not a limit on the total amount of memory used.  This is
000277  ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
000278  **
000279  ** The upper bound is slightly less than 2GiB:  0x7ffffeff == 2,147,483,391
000280  ** This provides a 256-byte safety margin for defense against 32-bit 
000281  ** signed integer overflow bugs when computing memory allocation sizes.
000282  ** Paranoid applications might want to reduce the maximum allocation size
000283  ** further for an even larger safety margin.  0x3fffffff or 0x0fffffff
000284  ** or even smaller would be reasonable upper bounds on the size of a memory
000285  ** allocations for most applications.
000286  */
000287  #ifndef SQLITE_MAX_ALLOCATION_SIZE
000288  # define SQLITE_MAX_ALLOCATION_SIZE  2147483391
000289  #endif
000290  #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
000291  # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
000292  #endif
000293  
000294  /*
000295  ** Allocate memory.  This routine is like sqlite3_malloc() except that it
000296  ** assumes the memory subsystem has already been initialized.
000297  */
000298  void *sqlite3Malloc(u64 n){
000299    void *p;
000300    if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){
000301      p = 0;
000302    }else if( sqlite3GlobalConfig.bMemstat ){
000303      sqlite3_mutex_enter(mem0.mutex);
000304      mallocWithAlarm((int)n, &p);
000305      sqlite3_mutex_leave(mem0.mutex);
000306    }else{
000307      p = sqlite3GlobalConfig.m.xMalloc((int)n);
000308    }
000309    assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
000310    return p;
000311  }
000312  
000313  /*
000314  ** This version of the memory allocation is for use by the application.
000315  ** First make sure the memory subsystem is initialized, then do the
000316  ** allocation.
000317  */
000318  void *sqlite3_malloc(int n){
000319  #ifndef SQLITE_OMIT_AUTOINIT
000320    if( sqlite3_initialize() ) return 0;
000321  #endif
000322    return n<=0 ? 0 : sqlite3Malloc(n);
000323  }
000324  void *sqlite3_malloc64(sqlite3_uint64 n){
000325  #ifndef SQLITE_OMIT_AUTOINIT
000326    if( sqlite3_initialize() ) return 0;
000327  #endif
000328    return sqlite3Malloc(n);
000329  }
000330  
000331  /*
000332  ** TRUE if p is a lookaside memory allocation from db
000333  */
000334  #ifndef SQLITE_OMIT_LOOKASIDE
000335  static int isLookaside(sqlite3 *db, const void *p){
000336    return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
000337  }
000338  #else
000339  #define isLookaside(A,B) 0
000340  #endif
000341  
000342  /*
000343  ** Return the size of a memory allocation previously obtained from
000344  ** sqlite3Malloc() or sqlite3_malloc().
000345  */
000346  int sqlite3MallocSize(const void *p){
000347    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000348    return sqlite3GlobalConfig.m.xSize((void*)p);
000349  }
000350  static int lookasideMallocSize(sqlite3 *db, const void *p){
000351  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE    
000352    return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
000353  #else
000354    return db->lookaside.szTrue;
000355  #endif  
000356  }
000357  int sqlite3DbMallocSize(sqlite3 *db, const void *p){
000358    assert( p!=0 );
000359  #ifdef SQLITE_DEBUG
000360    if( db==0 ){
000361      assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000362      assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000363    }else if( !isLookaside(db,p) ){
000364      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000365      assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000366    }
000367  #endif
000368    if( db ){
000369      if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
000370  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000371        if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
000372          assert( sqlite3_mutex_held(db->mutex) );
000373          return LOOKASIDE_SMALL;
000374        }
000375  #endif
000376        if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
000377          assert( sqlite3_mutex_held(db->mutex) );
000378          return db->lookaside.szTrue;
000379        }
000380      }
000381    }
000382    return sqlite3GlobalConfig.m.xSize((void*)p);
000383  }
000384  sqlite3_uint64 sqlite3_msize(void *p){
000385    assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000386    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000387    return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
000388  }
000389  
000390  /*
000391  ** Free memory previously obtained from sqlite3Malloc().
000392  */
000393  void sqlite3_free(void *p){
000394    if( p==0 ) return;  /* IMP: R-49053-54554 */
000395    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000396    assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000397    if( sqlite3GlobalConfig.bMemstat ){
000398      sqlite3_mutex_enter(mem0.mutex);
000399      sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
000400      sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
000401      sqlite3GlobalConfig.m.xFree(p);
000402      sqlite3_mutex_leave(mem0.mutex);
000403    }else{
000404      sqlite3GlobalConfig.m.xFree(p);
000405    }
000406  }
000407  
000408  /*
000409  ** Add the size of memory allocation "p" to the count in
000410  ** *db->pnBytesFreed.
000411  */
000412  static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
000413    *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
000414  }
000415  
000416  /*
000417  ** Free memory that might be associated with a particular database
000418  ** connection.  Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
000419  ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
000420  */
000421  void sqlite3DbFreeNN(sqlite3 *db, void *p){
000422    assert( db==0 || sqlite3_mutex_held(db->mutex) );
000423    assert( p!=0 );
000424    if( db ){
000425      if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
000426  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000427        if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
000428          LookasideSlot *pBuf = (LookasideSlot*)p;
000429          assert( db->pnBytesFreed==0 );
000430  #ifdef SQLITE_DEBUG
000431          memset(p, 0xaa, LOOKASIDE_SMALL);  /* Trash freed content */
000432  #endif
000433          pBuf->pNext = db->lookaside.pSmallFree;
000434          db->lookaside.pSmallFree = pBuf;
000435          return;
000436        }
000437  #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
000438        if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
000439          LookasideSlot *pBuf = (LookasideSlot*)p;
000440          assert( db->pnBytesFreed==0 );
000441  #ifdef SQLITE_DEBUG
000442          memset(p, 0xaa, db->lookaside.szTrue);  /* Trash freed content */
000443  #endif
000444          pBuf->pNext = db->lookaside.pFree;
000445          db->lookaside.pFree = pBuf;
000446          return;
000447        }
000448      }
000449      if( db->pnBytesFreed ){
000450        measureAllocationSize(db, p);
000451        return;
000452      }
000453    }
000454    assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000455    assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000456    assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
000457    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000458    sqlite3_free(p);
000459  }
000460  void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
000461    assert( db!=0 );
000462    assert( sqlite3_mutex_held(db->mutex) );
000463    assert( p!=0 );
000464    if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
000465  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000466      if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
000467        LookasideSlot *pBuf = (LookasideSlot*)p;
000468        assert( db->pnBytesFreed==0 );
000469  #ifdef SQLITE_DEBUG
000470        memset(p, 0xaa, LOOKASIDE_SMALL);  /* Trash freed content */
000471  #endif
000472        pBuf->pNext = db->lookaside.pSmallFree;
000473        db->lookaside.pSmallFree = pBuf;
000474        return;
000475      }
000476  #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
000477      if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
000478        LookasideSlot *pBuf = (LookasideSlot*)p;
000479        assert( db->pnBytesFreed==0 );
000480  #ifdef SQLITE_DEBUG
000481        memset(p, 0xaa, db->lookaside.szTrue);  /* Trash freed content */
000482  #endif
000483        pBuf->pNext = db->lookaside.pFree;
000484        db->lookaside.pFree = pBuf;
000485        return;
000486      }
000487    }
000488    if( db->pnBytesFreed ){
000489      measureAllocationSize(db, p);
000490      return;
000491    }
000492    assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000493    assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000494    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000495    sqlite3_free(p);
000496  }
000497  void sqlite3DbFree(sqlite3 *db, void *p){
000498    assert( db==0 || sqlite3_mutex_held(db->mutex) );
000499    if( p ) sqlite3DbFreeNN(db, p);
000500  }
000501  
000502  /*
000503  ** Change the size of an existing memory allocation
000504  */
000505  void *sqlite3Realloc(void *pOld, u64 nBytes){
000506    int nOld, nNew, nDiff;
000507    void *pNew;
000508    assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
000509    assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
000510    if( pOld==0 ){
000511      return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
000512    }
000513    if( nBytes==0 ){
000514      sqlite3_free(pOld); /* IMP: R-26507-47431 */
000515      return 0;
000516    }
000517    if( nBytes>=0x7fffff00 ){
000518      /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
000519      return 0;
000520    }
000521    nOld = sqlite3MallocSize(pOld);
000522    /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
000523    ** argument to xRealloc is always a value returned by a prior call to
000524    ** xRoundup. */
000525    nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
000526    if( nOld==nNew ){
000527      pNew = pOld;
000528    }else if( sqlite3GlobalConfig.bMemstat ){
000529      sqlite3_int64 nUsed;
000530      sqlite3_mutex_enter(mem0.mutex);
000531      sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
000532      nDiff = nNew - nOld;
000533      if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >= 
000534            mem0.alarmThreshold-nDiff ){
000535        sqlite3MallocAlarm(nDiff);
000536        if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
000537          sqlite3_mutex_leave(mem0.mutex);
000538          return 0;
000539        }
000540      }
000541      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000542  #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
000543      if( pNew==0 && mem0.alarmThreshold>0 ){
000544        sqlite3MallocAlarm((int)nBytes);
000545        pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000546      }
000547  #endif
000548      if( pNew ){
000549        nNew = sqlite3MallocSize(pNew);
000550        sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
000551      }
000552      sqlite3_mutex_leave(mem0.mutex);
000553    }else{
000554      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000555    }
000556    assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
000557    return pNew;
000558  }
000559  
000560  /*
000561  ** The public interface to sqlite3Realloc.  Make sure that the memory
000562  ** subsystem is initialized prior to invoking sqliteRealloc.
000563  */
000564  void *sqlite3_realloc(void *pOld, int n){
000565  #ifndef SQLITE_OMIT_AUTOINIT
000566    if( sqlite3_initialize() ) return 0;
000567  #endif
000568    if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
000569    return sqlite3Realloc(pOld, n);
000570  }
000571  void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
000572  #ifndef SQLITE_OMIT_AUTOINIT
000573    if( sqlite3_initialize() ) return 0;
000574  #endif
000575    return sqlite3Realloc(pOld, n);
000576  }
000577  
000578  
000579  /*
000580  ** Allocate and zero memory.
000581  */ 
000582  void *sqlite3MallocZero(u64 n){
000583    void *p = sqlite3Malloc(n);
000584    if( p ){
000585      memset(p, 0, (size_t)n);
000586    }
000587    return p;
000588  }
000589  
000590  /*
000591  ** Allocate and zero memory.  If the allocation fails, make
000592  ** the mallocFailed flag in the connection pointer.
000593  */
000594  void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
000595    void *p;
000596    testcase( db==0 );
000597    p = sqlite3DbMallocRaw(db, n);
000598    if( p ) memset(p, 0, (size_t)n);
000599    return p;
000600  }
000601  
000602  
000603  /* Finish the work of sqlite3DbMallocRawNN for the unusual and
000604  ** slower case when the allocation cannot be fulfilled using lookaside.
000605  */
000606  static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
000607    void *p;
000608    assert( db!=0 );
000609    p = sqlite3Malloc(n);
000610    if( !p ) sqlite3OomFault(db);
000611    sqlite3MemdebugSetType(p, 
000612           (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
000613    return p;
000614  }
000615  
000616  /*
000617  ** Allocate memory, either lookaside (if possible) or heap.  
000618  ** If the allocation fails, set the mallocFailed flag in
000619  ** the connection pointer.
000620  **
000621  ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
000622  ** failure on the same database connection) then always return 0.
000623  ** Hence for a particular database connection, once malloc starts
000624  ** failing, it fails consistently until mallocFailed is reset.
000625  ** This is an important assumption.  There are many places in the
000626  ** code that do things like this:
000627  **
000628  **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
000629  **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
000630  **         if( b ) a[10] = 9;
000631  **
000632  ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
000633  ** that all prior mallocs (ex: "a") worked too.
000634  **
000635  ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
000636  ** not a NULL pointer.
000637  */
000638  void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
000639    void *p;
000640    if( db ) return sqlite3DbMallocRawNN(db, n);
000641    p = sqlite3Malloc(n);
000642    sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000643    return p;
000644  }
000645  void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
000646  #ifndef SQLITE_OMIT_LOOKASIDE
000647    LookasideSlot *pBuf;
000648    assert( db!=0 );
000649    assert( sqlite3_mutex_held(db->mutex) );
000650    assert( db->pnBytesFreed==0 );
000651    if( n>db->lookaside.sz ){
000652      if( !db->lookaside.bDisable ){
000653        db->lookaside.anStat[1]++;      
000654      }else if( db->mallocFailed ){
000655        return 0;
000656      }
000657      return dbMallocRawFinish(db, n);
000658    }
000659  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000660    if( n<=LOOKASIDE_SMALL ){
000661      if( (pBuf = db->lookaside.pSmallFree)!=0 ){
000662        db->lookaside.pSmallFree = pBuf->pNext;
000663        db->lookaside.anStat[0]++;
000664        return (void*)pBuf;
000665      }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
000666        db->lookaside.pSmallInit = pBuf->pNext;
000667        db->lookaside.anStat[0]++;
000668        return (void*)pBuf;
000669      }
000670    }
000671  #endif
000672    if( (pBuf = db->lookaside.pFree)!=0 ){
000673      db->lookaside.pFree = pBuf->pNext;
000674      db->lookaside.anStat[0]++;
000675      return (void*)pBuf;
000676    }else if( (pBuf = db->lookaside.pInit)!=0 ){
000677      db->lookaside.pInit = pBuf->pNext;
000678      db->lookaside.anStat[0]++;
000679      return (void*)pBuf;
000680    }else{
000681      db->lookaside.anStat[2]++;
000682    }
000683  #else
000684    assert( db!=0 );
000685    assert( sqlite3_mutex_held(db->mutex) );
000686    assert( db->pnBytesFreed==0 );
000687    if( db->mallocFailed ){
000688      return 0;
000689    }
000690  #endif
000691    return dbMallocRawFinish(db, n);
000692  }
000693  
000694  /* Forward declaration */
000695  static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
000696  
000697  /*
000698  ** Resize the block of memory pointed to by p to n bytes. If the
000699  ** resize fails, set the mallocFailed flag in the connection object.
000700  */
000701  void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
000702    assert( db!=0 );
000703    if( p==0 ) return sqlite3DbMallocRawNN(db, n);
000704    assert( sqlite3_mutex_held(db->mutex) );
000705    if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
000706  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000707      if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
000708        if( n<=LOOKASIDE_SMALL ) return p;
000709      }else
000710  #endif
000711      if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
000712        if( n<=db->lookaside.szTrue ) return p;
000713      }
000714    }
000715    return dbReallocFinish(db, p, n);
000716  }
000717  static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
000718    void *pNew = 0;
000719    assert( db!=0 );
000720    assert( p!=0 );
000721    if( db->mallocFailed==0 ){
000722      if( isLookaside(db, p) ){
000723        pNew = sqlite3DbMallocRawNN(db, n);
000724        if( pNew ){
000725          memcpy(pNew, p, lookasideMallocSize(db, p));
000726          sqlite3DbFree(db, p);
000727        }
000728      }else{
000729        assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000730        assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000731        sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000732        pNew = sqlite3Realloc(p, n);
000733        if( !pNew ){
000734          sqlite3OomFault(db);
000735        }
000736        sqlite3MemdebugSetType(pNew,
000737              (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
000738      }
000739    }
000740    return pNew;
000741  }
000742  
000743  /*
000744  ** Attempt to reallocate p.  If the reallocation fails, then free p
000745  ** and set the mallocFailed flag in the database connection.
000746  */
000747  void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
000748    void *pNew;
000749    pNew = sqlite3DbRealloc(db, p, n);
000750    if( !pNew ){
000751      sqlite3DbFree(db, p);
000752    }
000753    return pNew;
000754  }
000755  
000756  /*
000757  ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
000758  ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
000759  ** is because when memory debugging is turned on, these two functions are 
000760  ** called via macros that record the current file and line number in the
000761  ** ThreadData structure.
000762  */
000763  char *sqlite3DbStrDup(sqlite3 *db, const char *z){
000764    char *zNew;
000765    size_t n;
000766    if( z==0 ){
000767      return 0;
000768    }
000769    n = strlen(z) + 1;
000770    zNew = sqlite3DbMallocRaw(db, n);
000771    if( zNew ){
000772      memcpy(zNew, z, n);
000773    }
000774    return zNew;
000775  }
000776  char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
000777    char *zNew;
000778    assert( db!=0 );
000779    assert( z!=0 || n==0 );
000780    assert( (n&0x7fffffff)==n );
000781    zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
000782    if( zNew ){
000783      memcpy(zNew, z, (size_t)n);
000784      zNew[n] = 0;
000785    }
000786    return zNew;
000787  }
000788  
000789  /*
000790  ** The text between zStart and zEnd represents a phrase within a larger
000791  ** SQL statement.  Make a copy of this phrase in space obtained form
000792  ** sqlite3DbMalloc().  Omit leading and trailing whitespace.
000793  */
000794  char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
000795    int n;
000796  #ifdef SQLITE_DEBUG
000797    /* Because of the way the parser works, the span is guaranteed to contain
000798    ** at least one non-space character */
000799    for(n=0; sqlite3Isspace(zStart[n]); n++){ assert( &zStart[n]<zEnd ); }
000800  #endif
000801    while( sqlite3Isspace(zStart[0]) ) zStart++;
000802    n = (int)(zEnd - zStart);
000803    while( sqlite3Isspace(zStart[n-1]) ) n--;
000804    return sqlite3DbStrNDup(db, zStart, n);
000805  }
000806  
000807  /*
000808  ** Free any prior content in *pz and replace it with a copy of zNew.
000809  */
000810  void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
000811    char *z = sqlite3DbStrDup(db, zNew);
000812    sqlite3DbFree(db, *pz);
000813    *pz = z;
000814  }
000815  
000816  /*
000817  ** Call this routine to record the fact that an OOM (out-of-memory) error
000818  ** has happened.  This routine will set db->mallocFailed, and also
000819  ** temporarily disable the lookaside memory allocator and interrupt
000820  ** any running VDBEs.
000821  **
000822  ** Always return a NULL pointer so that this routine can be invoked using
000823  **
000824  **      return sqlite3OomFault(db);
000825  **
000826  ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
000827  ** common case where no OOM occurs.
000828  */
000829  void *sqlite3OomFault(sqlite3 *db){
000830    if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
000831      db->mallocFailed = 1;
000832      if( db->nVdbeExec>0 ){
000833        AtomicStore(&db->u1.isInterrupted, 1);
000834      }
000835      DisableLookaside;
000836      if( db->pParse ){
000837        Parse *pParse;
000838        sqlite3ErrorMsg(db->pParse, "out of memory");
000839        db->pParse->rc = SQLITE_NOMEM_BKPT;
000840        for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
000841          pParse->nErr++;
000842          pParse->rc = SQLITE_NOMEM;
000843        } 
000844      }
000845    }
000846    return 0;
000847  }
000848  
000849  /*
000850  ** This routine reactivates the memory allocator and clears the
000851  ** db->mallocFailed flag as necessary.
000852  **
000853  ** The memory allocator is not restarted if there are running
000854  ** VDBEs.
000855  */
000856  void sqlite3OomClear(sqlite3 *db){
000857    if( db->mallocFailed && db->nVdbeExec==0 ){
000858      db->mallocFailed = 0;
000859      AtomicStore(&db->u1.isInterrupted, 0);
000860      assert( db->lookaside.bDisable>0 );
000861      EnableLookaside;
000862    }
000863  }
000864  
000865  /*
000866  ** Take actions at the end of an API call to deal with error codes.
000867  */
000868  static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
000869    if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
000870      sqlite3OomClear(db);
000871      sqlite3Error(db, SQLITE_NOMEM);
000872      return SQLITE_NOMEM_BKPT;
000873    }
000874    return rc & db->errMask;
000875  }
000876  
000877  /*
000878  ** This function must be called before exiting any API function (i.e. 
000879  ** returning control to the user) that has called sqlite3_malloc or
000880  ** sqlite3_realloc.
000881  **
000882  ** The returned value is normally a copy of the second argument to this
000883  ** function. However, if a malloc() failure has occurred since the previous
000884  ** invocation SQLITE_NOMEM is returned instead. 
000885  **
000886  ** If an OOM as occurred, then the connection error-code (the value
000887  ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
000888  */
000889  int sqlite3ApiExit(sqlite3* db, int rc){
000890    /* If the db handle must hold the connection handle mutex here.
000891    ** Otherwise the read (and possible write) of db->mallocFailed 
000892    ** is unsafe, as is the call to sqlite3Error().
000893    */
000894    assert( db!=0 );
000895    assert( sqlite3_mutex_held(db->mutex) );
000896    if( db->mallocFailed || rc ){
000897      return apiHandleError(db, rc);
000898    }
000899    return 0;
000900  }