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 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 memset(&mem0, 0, sizeof(mem0));
000165 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
000166 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
000167 || sqlite3GlobalConfig.nPage<=0 ){
000168 sqlite3GlobalConfig.pPage = 0;
000169 sqlite3GlobalConfig.szPage = 0;
000170 }
000171 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
000172 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
000173 return rc;
000174 }
000175
000176 /*
000177 ** Return true if the heap is currently under memory pressure - in other
000178 ** words if the amount of heap used is close to the limit set by
000179 ** sqlite3_soft_heap_limit().
000180 */
000181 int sqlite3HeapNearlyFull(void){
000182 return mem0.nearlyFull;
000183 }
000184
000185 /*
000186 ** Deinitialize the memory allocation subsystem.
000187 */
000188 void sqlite3MallocEnd(void){
000189 if( sqlite3GlobalConfig.m.xShutdown ){
000190 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
000191 }
000192 memset(&mem0, 0, sizeof(mem0));
000193 }
000194
000195 /*
000196 ** Return the amount of memory currently checked out.
000197 */
000198 sqlite3_int64 sqlite3_memory_used(void){
000199 sqlite3_int64 res, mx;
000200 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
000201 return res;
000202 }
000203
000204 /*
000205 ** Return the maximum amount of memory that has ever been
000206 ** checked out since either the beginning of this process
000207 ** or since the most recent reset.
000208 */
000209 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
000210 sqlite3_int64 res, mx;
000211 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
000212 return mx;
000213 }
000214
000215 /*
000216 ** Trigger the alarm
000217 */
000218 static void sqlite3MallocAlarm(int nByte){
000219 if( mem0.alarmThreshold<=0 ) return;
000220 sqlite3_mutex_leave(mem0.mutex);
000221 sqlite3_release_memory(nByte);
000222 sqlite3_mutex_enter(mem0.mutex);
000223 }
000224
000225 /*
000226 ** Do a memory allocation with statistics and alarms. Assume the
000227 ** lock is already held.
000228 */
000229 static void mallocWithAlarm(int n, void **pp){
000230 void *p;
000231 int nFull;
000232 assert( sqlite3_mutex_held(mem0.mutex) );
000233 assert( n>0 );
000234
000235 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
000236 ** implementation of malloc_good_size(), which must be called in debug
000237 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
000238 ** or else a crash results. Hence, do not attempt to optimize out the
000239 ** following xRoundup() call. */
000240 nFull = sqlite3GlobalConfig.m.xRoundup(n);
000241
000242 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
000243 if( mem0.alarmThreshold>0 ){
000244 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000245 if( nUsed >= mem0.alarmThreshold - nFull ){
000246 mem0.nearlyFull = 1;
000247 sqlite3MallocAlarm(nFull);
000248 if( mem0.hardLimit ){
000249 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000250 if( nUsed >= mem0.hardLimit - nFull ){
000251 *pp = 0;
000252 return;
000253 }
000254 }
000255 }else{
000256 mem0.nearlyFull = 0;
000257 }
000258 }
000259 p = sqlite3GlobalConfig.m.xMalloc(nFull);
000260 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
000261 if( p==0 && mem0.alarmThreshold>0 ){
000262 sqlite3MallocAlarm(nFull);
000263 p = sqlite3GlobalConfig.m.xMalloc(nFull);
000264 }
000265 #endif
000266 if( p ){
000267 nFull = sqlite3MallocSize(p);
000268 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
000269 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
000270 }
000271 *pp = p;
000272 }
000273
000274 /*
000275 ** Allocate memory. This routine is like sqlite3_malloc() except that it
000276 ** assumes the memory subsystem has already been initialized.
000277 */
000278 void *sqlite3Malloc(u64 n){
000279 void *p;
000280 if( n==0 || n>=0x7fffff00 ){
000281 /* A memory allocation of a number of bytes which is near the maximum
000282 ** signed integer value might cause an integer overflow inside of the
000283 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
000284 ** 255 bytes of overhead. SQLite itself will never use anything near
000285 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
000286 p = 0;
000287 }else if( sqlite3GlobalConfig.bMemstat ){
000288 sqlite3_mutex_enter(mem0.mutex);
000289 mallocWithAlarm((int)n, &p);
000290 sqlite3_mutex_leave(mem0.mutex);
000291 }else{
000292 p = sqlite3GlobalConfig.m.xMalloc((int)n);
000293 }
000294 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
000295 return p;
000296 }
000297
000298 /*
000299 ** This version of the memory allocation is for use by the application.
000300 ** First make sure the memory subsystem is initialized, then do the
000301 ** allocation.
000302 */
000303 void *sqlite3_malloc(int n){
000304 #ifndef SQLITE_OMIT_AUTOINIT
000305 if( sqlite3_initialize() ) return 0;
000306 #endif
000307 return n<=0 ? 0 : sqlite3Malloc(n);
000308 }
000309 void *sqlite3_malloc64(sqlite3_uint64 n){
000310 #ifndef SQLITE_OMIT_AUTOINIT
000311 if( sqlite3_initialize() ) return 0;
000312 #endif
000313 return sqlite3Malloc(n);
000314 }
000315
000316 /*
000317 ** TRUE if p is a lookaside memory allocation from db
000318 */
000319 #ifndef SQLITE_OMIT_LOOKASIDE
000320 static int isLookaside(sqlite3 *db, void *p){
000321 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
000322 }
000323 #else
000324 #define isLookaside(A,B) 0
000325 #endif
000326
000327 /*
000328 ** Return the size of a memory allocation previously obtained from
000329 ** sqlite3Malloc() or sqlite3_malloc().
000330 */
000331 int sqlite3MallocSize(void *p){
000332 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000333 return sqlite3GlobalConfig.m.xSize(p);
000334 }
000335 int sqlite3DbMallocSize(sqlite3 *db, void *p){
000336 assert( p!=0 );
000337 if( db==0 || !isLookaside(db,p) ){
000338 #ifdef SQLITE_DEBUG
000339 if( db==0 ){
000340 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000341 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000342 }else{
000343 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000344 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000345 }
000346 #endif
000347 return sqlite3GlobalConfig.m.xSize(p);
000348 }else{
000349 assert( sqlite3_mutex_held(db->mutex) );
000350 return db->lookaside.szTrue;
000351 }
000352 }
000353 sqlite3_uint64 sqlite3_msize(void *p){
000354 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000355 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000356 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
000357 }
000358
000359 /*
000360 ** Free memory previously obtained from sqlite3Malloc().
000361 */
000362 void sqlite3_free(void *p){
000363 if( p==0 ) return; /* IMP: R-49053-54554 */
000364 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000365 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000366 if( sqlite3GlobalConfig.bMemstat ){
000367 sqlite3_mutex_enter(mem0.mutex);
000368 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
000369 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
000370 sqlite3GlobalConfig.m.xFree(p);
000371 sqlite3_mutex_leave(mem0.mutex);
000372 }else{
000373 sqlite3GlobalConfig.m.xFree(p);
000374 }
000375 }
000376
000377 /*
000378 ** Add the size of memory allocation "p" to the count in
000379 ** *db->pnBytesFreed.
000380 */
000381 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
000382 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
000383 }
000384
000385 /*
000386 ** Free memory that might be associated with a particular database
000387 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
000388 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
000389 */
000390 void sqlite3DbFreeNN(sqlite3 *db, void *p){
000391 assert( db==0 || sqlite3_mutex_held(db->mutex) );
000392 assert( p!=0 );
000393 if( db ){
000394 if( db->pnBytesFreed ){
000395 measureAllocationSize(db, p);
000396 return;
000397 }
000398 if( isLookaside(db, p) ){
000399 LookasideSlot *pBuf = (LookasideSlot*)p;
000400 #ifdef SQLITE_DEBUG
000401 /* Trash all content in the buffer being freed */
000402 memset(p, 0xaa, db->lookaside.szTrue);
000403 #endif
000404 pBuf->pNext = db->lookaside.pFree;
000405 db->lookaside.pFree = pBuf;
000406 return;
000407 }
000408 }
000409 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000410 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000411 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
000412 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000413 sqlite3_free(p);
000414 }
000415 void sqlite3DbFree(sqlite3 *db, void *p){
000416 assert( db==0 || sqlite3_mutex_held(db->mutex) );
000417 if( p ) sqlite3DbFreeNN(db, p);
000418 }
000419
000420 /*
000421 ** Change the size of an existing memory allocation
000422 */
000423 void *sqlite3Realloc(void *pOld, u64 nBytes){
000424 int nOld, nNew, nDiff;
000425 void *pNew;
000426 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
000427 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
000428 if( pOld==0 ){
000429 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
000430 }
000431 if( nBytes==0 ){
000432 sqlite3_free(pOld); /* IMP: R-26507-47431 */
000433 return 0;
000434 }
000435 if( nBytes>=0x7fffff00 ){
000436 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
000437 return 0;
000438 }
000439 nOld = sqlite3MallocSize(pOld);
000440 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
000441 ** argument to xRealloc is always a value returned by a prior call to
000442 ** xRoundup. */
000443 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
000444 if( nOld==nNew ){
000445 pNew = pOld;
000446 }else if( sqlite3GlobalConfig.bMemstat ){
000447 sqlite3_mutex_enter(mem0.mutex);
000448 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
000449 nDiff = nNew - nOld;
000450 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
000451 mem0.alarmThreshold-nDiff ){
000452 sqlite3MallocAlarm(nDiff);
000453 }
000454 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000455 if( pNew==0 && mem0.alarmThreshold>0 ){
000456 sqlite3MallocAlarm((int)nBytes);
000457 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000458 }
000459 if( pNew ){
000460 nNew = sqlite3MallocSize(pNew);
000461 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
000462 }
000463 sqlite3_mutex_leave(mem0.mutex);
000464 }else{
000465 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000466 }
000467 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
000468 return pNew;
000469 }
000470
000471 /*
000472 ** The public interface to sqlite3Realloc. Make sure that the memory
000473 ** subsystem is initialized prior to invoking sqliteRealloc.
000474 */
000475 void *sqlite3_realloc(void *pOld, int n){
000476 #ifndef SQLITE_OMIT_AUTOINIT
000477 if( sqlite3_initialize() ) return 0;
000478 #endif
000479 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
000480 return sqlite3Realloc(pOld, n);
000481 }
000482 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
000483 #ifndef SQLITE_OMIT_AUTOINIT
000484 if( sqlite3_initialize() ) return 0;
000485 #endif
000486 return sqlite3Realloc(pOld, n);
000487 }
000488
000489
000490 /*
000491 ** Allocate and zero memory.
000492 */
000493 void *sqlite3MallocZero(u64 n){
000494 void *p = sqlite3Malloc(n);
000495 if( p ){
000496 memset(p, 0, (size_t)n);
000497 }
000498 return p;
000499 }
000500
000501 /*
000502 ** Allocate and zero memory. If the allocation fails, make
000503 ** the mallocFailed flag in the connection pointer.
000504 */
000505 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
000506 void *p;
000507 testcase( db==0 );
000508 p = sqlite3DbMallocRaw(db, n);
000509 if( p ) memset(p, 0, (size_t)n);
000510 return p;
000511 }
000512
000513
000514 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
000515 ** slower case when the allocation cannot be fulfilled using lookaside.
000516 */
000517 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
000518 void *p;
000519 assert( db!=0 );
000520 p = sqlite3Malloc(n);
000521 if( !p ) sqlite3OomFault(db);
000522 sqlite3MemdebugSetType(p,
000523 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
000524 return p;
000525 }
000526
000527 /*
000528 ** Allocate memory, either lookaside (if possible) or heap.
000529 ** If the allocation fails, set the mallocFailed flag in
000530 ** the connection pointer.
000531 **
000532 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
000533 ** failure on the same database connection) then always return 0.
000534 ** Hence for a particular database connection, once malloc starts
000535 ** failing, it fails consistently until mallocFailed is reset.
000536 ** This is an important assumption. There are many places in the
000537 ** code that do things like this:
000538 **
000539 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
000540 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
000541 ** if( b ) a[10] = 9;
000542 **
000543 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
000544 ** that all prior mallocs (ex: "a") worked too.
000545 **
000546 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
000547 ** not a NULL pointer.
000548 */
000549 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
000550 void *p;
000551 if( db ) return sqlite3DbMallocRawNN(db, n);
000552 p = sqlite3Malloc(n);
000553 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000554 return p;
000555 }
000556 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
000557 #ifndef SQLITE_OMIT_LOOKASIDE
000558 LookasideSlot *pBuf;
000559 assert( db!=0 );
000560 assert( sqlite3_mutex_held(db->mutex) );
000561 assert( db->pnBytesFreed==0 );
000562 if( n>db->lookaside.sz ){
000563 if( db->lookaside.bDisable ){
000564 return db->mallocFailed ? 0 : dbMallocRawFinish(db, n);
000565 }
000566 db->lookaside.anStat[1]++;
000567 }else if( (pBuf = db->lookaside.pFree)!=0 ){
000568 db->lookaside.pFree = pBuf->pNext;
000569 db->lookaside.anStat[0]++;
000570 return (void*)pBuf;
000571 }else if( (pBuf = db->lookaside.pInit)!=0 ){
000572 db->lookaside.pInit = pBuf->pNext;
000573 db->lookaside.anStat[0]++;
000574 return (void*)pBuf;
000575 }else{
000576 db->lookaside.anStat[2]++;
000577 }
000578 #else
000579 assert( db!=0 );
000580 assert( sqlite3_mutex_held(db->mutex) );
000581 assert( db->pnBytesFreed==0 );
000582 if( db->mallocFailed ){
000583 return 0;
000584 }
000585 #endif
000586 return dbMallocRawFinish(db, n);
000587 }
000588
000589 /* Forward declaration */
000590 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
000591
000592 /*
000593 ** Resize the block of memory pointed to by p to n bytes. If the
000594 ** resize fails, set the mallocFailed flag in the connection object.
000595 */
000596 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
000597 assert( db!=0 );
000598 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
000599 assert( sqlite3_mutex_held(db->mutex) );
000600 if( isLookaside(db,p) && n<=db->lookaside.szTrue ) return p;
000601 return dbReallocFinish(db, p, n);
000602 }
000603 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
000604 void *pNew = 0;
000605 assert( db!=0 );
000606 assert( p!=0 );
000607 if( db->mallocFailed==0 ){
000608 if( isLookaside(db, p) ){
000609 pNew = sqlite3DbMallocRawNN(db, n);
000610 if( pNew ){
000611 memcpy(pNew, p, db->lookaside.szTrue);
000612 sqlite3DbFree(db, p);
000613 }
000614 }else{
000615 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000616 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000617 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000618 pNew = sqlite3_realloc64(p, n);
000619 if( !pNew ){
000620 sqlite3OomFault(db);
000621 }
000622 sqlite3MemdebugSetType(pNew,
000623 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
000624 }
000625 }
000626 return pNew;
000627 }
000628
000629 /*
000630 ** Attempt to reallocate p. If the reallocation fails, then free p
000631 ** and set the mallocFailed flag in the database connection.
000632 */
000633 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
000634 void *pNew;
000635 pNew = sqlite3DbRealloc(db, p, n);
000636 if( !pNew ){
000637 sqlite3DbFree(db, p);
000638 }
000639 return pNew;
000640 }
000641
000642 /*
000643 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
000644 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
000645 ** is because when memory debugging is turned on, these two functions are
000646 ** called via macros that record the current file and line number in the
000647 ** ThreadData structure.
000648 */
000649 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
000650 char *zNew;
000651 size_t n;
000652 if( z==0 ){
000653 return 0;
000654 }
000655 n = strlen(z) + 1;
000656 zNew = sqlite3DbMallocRaw(db, n);
000657 if( zNew ){
000658 memcpy(zNew, z, n);
000659 }
000660 return zNew;
000661 }
000662 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
000663 char *zNew;
000664 assert( db!=0 );
000665 if( z==0 ){
000666 return 0;
000667 }
000668 assert( (n&0x7fffffff)==n );
000669 zNew = sqlite3DbMallocRawNN(db, n+1);
000670 if( zNew ){
000671 memcpy(zNew, z, (size_t)n);
000672 zNew[n] = 0;
000673 }
000674 return zNew;
000675 }
000676
000677 /*
000678 ** The text between zStart and zEnd represents a phrase within a larger
000679 ** SQL statement. Make a copy of this phrase in space obtained form
000680 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
000681 */
000682 char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
000683 int n;
000684 while( sqlite3Isspace(zStart[0]) ) zStart++;
000685 n = (int)(zEnd - zStart);
000686 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
000687 return sqlite3DbStrNDup(db, zStart, n);
000688 }
000689
000690 /*
000691 ** Free any prior content in *pz and replace it with a copy of zNew.
000692 */
000693 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
000694 sqlite3DbFree(db, *pz);
000695 *pz = sqlite3DbStrDup(db, zNew);
000696 }
000697
000698 /*
000699 ** Call this routine to record the fact that an OOM (out-of-memory) error
000700 ** has happened. This routine will set db->mallocFailed, and also
000701 ** temporarily disable the lookaside memory allocator and interrupt
000702 ** any running VDBEs.
000703 */
000704 void sqlite3OomFault(sqlite3 *db){
000705 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
000706 db->mallocFailed = 1;
000707 if( db->nVdbeExec>0 ){
000708 db->u1.isInterrupted = 1;
000709 }
000710 DisableLookaside;
000711 if( db->pParse ){
000712 db->pParse->rc = SQLITE_NOMEM_BKPT;
000713 }
000714 }
000715 }
000716
000717 /*
000718 ** This routine reactivates the memory allocator and clears the
000719 ** db->mallocFailed flag as necessary.
000720 **
000721 ** The memory allocator is not restarted if there are running
000722 ** VDBEs.
000723 */
000724 void sqlite3OomClear(sqlite3 *db){
000725 if( db->mallocFailed && db->nVdbeExec==0 ){
000726 db->mallocFailed = 0;
000727 db->u1.isInterrupted = 0;
000728 assert( db->lookaside.bDisable>0 );
000729 EnableLookaside;
000730 }
000731 }
000732
000733 /*
000734 ** Take actions at the end of an API call to indicate an OOM error
000735 */
000736 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
000737 sqlite3OomClear(db);
000738 sqlite3Error(db, SQLITE_NOMEM);
000739 return SQLITE_NOMEM_BKPT;
000740 }
000741
000742 /*
000743 ** This function must be called before exiting any API function (i.e.
000744 ** returning control to the user) that has called sqlite3_malloc or
000745 ** sqlite3_realloc.
000746 **
000747 ** The returned value is normally a copy of the second argument to this
000748 ** function. However, if a malloc() failure has occurred since the previous
000749 ** invocation SQLITE_NOMEM is returned instead.
000750 **
000751 ** If an OOM as occurred, then the connection error-code (the value
000752 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
000753 */
000754 int sqlite3ApiExit(sqlite3* db, int rc){
000755 /* If the db handle must hold the connection handle mutex here.
000756 ** Otherwise the read (and possible write) of db->mallocFailed
000757 ** is unsafe, as is the call to sqlite3Error().
000758 */
000759 assert( db!=0 );
000760 assert( sqlite3_mutex_held(db->mutex) );
000761 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
000762 return apiOomError(db);
000763 }
000764 return rc & db->errMask;
000765 }