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 #ifdef SQLITE_DEBUG
000225 /*
000226 ** This routine is called whenever an out-of-memory condition is seen,
000227 ** It's only purpose to to serve as a breakpoint for gdb or similar
000228 ** code debuggers when working on out-of-memory conditions, for example
000229 ** caused by PRAGMA hard_heap_limit=N.
000230 */
000231 static SQLITE_NOINLINE void test_oom_breakpoint(u64 n){
000232 static u64 nOomFault = 0;
000233 nOomFault += n;
000234 /* The assert() is never reached in a human lifetime. It is here mostly
000235 ** to prevent code optimizers from optimizing out this function. */
000236 assert( (nOomFault>>32) < 0xffffffff );
000237 }
000238 #else
000239 # define test_oom_breakpoint(X) /* No-op for production builds */
000240 #endif
000241
000242 /*
000243 ** Do a memory allocation with statistics and alarms. Assume the
000244 ** lock is already held.
000245 */
000246 static void mallocWithAlarm(int n, void **pp){
000247 void *p;
000248 int nFull;
000249 assert( sqlite3_mutex_held(mem0.mutex) );
000250 assert( n>0 );
000251
000252 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
000253 ** implementation of malloc_good_size(), which must be called in debug
000254 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
000255 ** or else a crash results. Hence, do not attempt to optimize out the
000256 ** following xRoundup() call. */
000257 nFull = sqlite3GlobalConfig.m.xRoundup(n);
000258
000259 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
000260 if( mem0.alarmThreshold>0 ){
000261 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000262 if( nUsed >= mem0.alarmThreshold - nFull ){
000263 AtomicStore(&mem0.nearlyFull, 1);
000264 sqlite3MallocAlarm(nFull);
000265 if( mem0.hardLimit ){
000266 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
000267 if( nUsed >= mem0.hardLimit - nFull ){
000268 test_oom_breakpoint(1);
000269 *pp = 0;
000270 return;
000271 }
000272 }
000273 }else{
000274 AtomicStore(&mem0.nearlyFull, 0);
000275 }
000276 }
000277 p = sqlite3GlobalConfig.m.xMalloc(nFull);
000278 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
000279 if( p==0 && mem0.alarmThreshold>0 ){
000280 sqlite3MallocAlarm(nFull);
000281 p = sqlite3GlobalConfig.m.xMalloc(nFull);
000282 }
000283 #endif
000284 if( p ){
000285 nFull = sqlite3MallocSize(p);
000286 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
000287 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
000288 }
000289 *pp = p;
000290 }
000291
000292 /*
000293 ** Maximum size of any single memory allocation.
000294 **
000295 ** This is not a limit on the total amount of memory used. This is
000296 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
000297 **
000298 ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
000299 ** This provides a 256-byte safety margin for defense against 32-bit
000300 ** signed integer overflow bugs when computing memory allocation sizes.
000301 ** Paranoid applications might want to reduce the maximum allocation size
000302 ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
000303 ** or even smaller would be reasonable upper bounds on the size of a memory
000304 ** allocations for most applications.
000305 */
000306 #ifndef SQLITE_MAX_ALLOCATION_SIZE
000307 # define SQLITE_MAX_ALLOCATION_SIZE 2147483391
000308 #endif
000309 #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
000310 # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
000311 #endif
000312
000313 /*
000314 ** Allocate memory. This routine is like sqlite3_malloc() except that it
000315 ** assumes the memory subsystem has already been initialized.
000316 */
000317 void *sqlite3Malloc(u64 n){
000318 void *p;
000319 if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){
000320 p = 0;
000321 }else if( sqlite3GlobalConfig.bMemstat ){
000322 sqlite3_mutex_enter(mem0.mutex);
000323 mallocWithAlarm((int)n, &p);
000324 sqlite3_mutex_leave(mem0.mutex);
000325 }else{
000326 p = sqlite3GlobalConfig.m.xMalloc((int)n);
000327 }
000328 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
000329 return p;
000330 }
000331
000332 /*
000333 ** This version of the memory allocation is for use by the application.
000334 ** First make sure the memory subsystem is initialized, then do the
000335 ** allocation.
000336 */
000337 void *sqlite3_malloc(int n){
000338 #ifndef SQLITE_OMIT_AUTOINIT
000339 if( sqlite3_initialize() ) return 0;
000340 #endif
000341 return n<=0 ? 0 : sqlite3Malloc(n);
000342 }
000343 void *sqlite3_malloc64(sqlite3_uint64 n){
000344 #ifndef SQLITE_OMIT_AUTOINIT
000345 if( sqlite3_initialize() ) return 0;
000346 #endif
000347 return sqlite3Malloc(n);
000348 }
000349
000350 /*
000351 ** TRUE if p is a lookaside memory allocation from db
000352 */
000353 #ifndef SQLITE_OMIT_LOOKASIDE
000354 static int isLookaside(sqlite3 *db, const void *p){
000355 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
000356 }
000357 #else
000358 #define isLookaside(A,B) 0
000359 #endif
000360
000361 /*
000362 ** Return the size of a memory allocation previously obtained from
000363 ** sqlite3Malloc() or sqlite3_malloc().
000364 */
000365 int sqlite3MallocSize(const void *p){
000366 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000367 return sqlite3GlobalConfig.m.xSize((void*)p);
000368 }
000369 static int lookasideMallocSize(sqlite3 *db, const void *p){
000370 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000371 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
000372 #else
000373 return db->lookaside.szTrue;
000374 #endif
000375 }
000376 int sqlite3DbMallocSize(sqlite3 *db, const void *p){
000377 assert( p!=0 );
000378 #ifdef SQLITE_DEBUG
000379 if( db==0 ){
000380 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000381 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000382 }else if( !isLookaside(db,p) ){
000383 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000384 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000385 }
000386 #endif
000387 if( db ){
000388 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
000389 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000390 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
000391 assert( sqlite3_mutex_held(db->mutex) );
000392 return LOOKASIDE_SMALL;
000393 }
000394 #endif
000395 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
000396 assert( sqlite3_mutex_held(db->mutex) );
000397 return db->lookaside.szTrue;
000398 }
000399 }
000400 }
000401 return sqlite3GlobalConfig.m.xSize((void*)p);
000402 }
000403 sqlite3_uint64 sqlite3_msize(void *p){
000404 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000405 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000406 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
000407 }
000408
000409 /*
000410 ** Free memory previously obtained from sqlite3Malloc().
000411 */
000412 void sqlite3_free(void *p){
000413 if( p==0 ) return; /* IMP: R-49053-54554 */
000414 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
000415 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
000416 if( sqlite3GlobalConfig.bMemstat ){
000417 sqlite3_mutex_enter(mem0.mutex);
000418 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
000419 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
000420 sqlite3GlobalConfig.m.xFree(p);
000421 sqlite3_mutex_leave(mem0.mutex);
000422 }else{
000423 sqlite3GlobalConfig.m.xFree(p);
000424 }
000425 }
000426
000427 /*
000428 ** Add the size of memory allocation "p" to the count in
000429 ** *db->pnBytesFreed.
000430 */
000431 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
000432 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
000433 }
000434
000435 /*
000436 ** Free memory that might be associated with a particular database
000437 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
000438 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
000439 */
000440 void sqlite3DbFreeNN(sqlite3 *db, void *p){
000441 assert( db==0 || sqlite3_mutex_held(db->mutex) );
000442 assert( p!=0 );
000443 if( db ){
000444 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
000445 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000446 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
000447 LookasideSlot *pBuf = (LookasideSlot*)p;
000448 assert( db->pnBytesFreed==0 );
000449 #ifdef SQLITE_DEBUG
000450 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
000451 #endif
000452 pBuf->pNext = db->lookaside.pSmallFree;
000453 db->lookaside.pSmallFree = pBuf;
000454 return;
000455 }
000456 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
000457 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
000458 LookasideSlot *pBuf = (LookasideSlot*)p;
000459 assert( db->pnBytesFreed==0 );
000460 #ifdef SQLITE_DEBUG
000461 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
000462 #endif
000463 pBuf->pNext = db->lookaside.pFree;
000464 db->lookaside.pFree = pBuf;
000465 return;
000466 }
000467 }
000468 if( db->pnBytesFreed ){
000469 measureAllocationSize(db, p);
000470 return;
000471 }
000472 }
000473 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000474 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000475 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
000476 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000477 sqlite3_free(p);
000478 }
000479 void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
000480 assert( db!=0 );
000481 assert( sqlite3_mutex_held(db->mutex) );
000482 assert( p!=0 );
000483 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
000484 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000485 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
000486 LookasideSlot *pBuf = (LookasideSlot*)p;
000487 assert( db->pnBytesFreed==0 );
000488 #ifdef SQLITE_DEBUG
000489 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
000490 #endif
000491 pBuf->pNext = db->lookaside.pSmallFree;
000492 db->lookaside.pSmallFree = pBuf;
000493 return;
000494 }
000495 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
000496 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
000497 LookasideSlot *pBuf = (LookasideSlot*)p;
000498 assert( db->pnBytesFreed==0 );
000499 #ifdef SQLITE_DEBUG
000500 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
000501 #endif
000502 pBuf->pNext = db->lookaside.pFree;
000503 db->lookaside.pFree = pBuf;
000504 return;
000505 }
000506 }
000507 if( db->pnBytesFreed ){
000508 measureAllocationSize(db, p);
000509 return;
000510 }
000511 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000512 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000513 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000514 sqlite3_free(p);
000515 }
000516 void sqlite3DbFree(sqlite3 *db, void *p){
000517 assert( db==0 || sqlite3_mutex_held(db->mutex) );
000518 if( p ) sqlite3DbFreeNN(db, p);
000519 }
000520
000521 /*
000522 ** Change the size of an existing memory allocation
000523 */
000524 void *sqlite3Realloc(void *pOld, u64 nBytes){
000525 int nOld, nNew, nDiff;
000526 void *pNew;
000527 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
000528 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
000529 if( pOld==0 ){
000530 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
000531 }
000532 if( nBytes==0 ){
000533 sqlite3_free(pOld); /* IMP: R-26507-47431 */
000534 return 0;
000535 }
000536 if( nBytes>=0x7fffff00 ){
000537 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
000538 return 0;
000539 }
000540 nOld = sqlite3MallocSize(pOld);
000541 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
000542 ** argument to xRealloc is always a value returned by a prior call to
000543 ** xRoundup. */
000544 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
000545 if( nOld==nNew ){
000546 pNew = pOld;
000547 }else if( sqlite3GlobalConfig.bMemstat ){
000548 sqlite3_int64 nUsed;
000549 sqlite3_mutex_enter(mem0.mutex);
000550 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
000551 nDiff = nNew - nOld;
000552 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
000553 mem0.alarmThreshold-nDiff ){
000554 sqlite3MallocAlarm(nDiff);
000555 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
000556 sqlite3_mutex_leave(mem0.mutex);
000557 test_oom_breakpoint(1);
000558 return 0;
000559 }
000560 }
000561 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000562 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
000563 if( pNew==0 && mem0.alarmThreshold>0 ){
000564 sqlite3MallocAlarm((int)nBytes);
000565 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000566 }
000567 #endif
000568 if( pNew ){
000569 nNew = sqlite3MallocSize(pNew);
000570 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
000571 }
000572 sqlite3_mutex_leave(mem0.mutex);
000573 }else{
000574 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
000575 }
000576 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
000577 return pNew;
000578 }
000579
000580 /*
000581 ** The public interface to sqlite3Realloc. Make sure that the memory
000582 ** subsystem is initialized prior to invoking sqliteRealloc.
000583 */
000584 void *sqlite3_realloc(void *pOld, int n){
000585 #ifndef SQLITE_OMIT_AUTOINIT
000586 if( sqlite3_initialize() ) return 0;
000587 #endif
000588 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
000589 return sqlite3Realloc(pOld, n);
000590 }
000591 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
000592 #ifndef SQLITE_OMIT_AUTOINIT
000593 if( sqlite3_initialize() ) return 0;
000594 #endif
000595 return sqlite3Realloc(pOld, n);
000596 }
000597
000598
000599 /*
000600 ** Allocate and zero memory.
000601 */
000602 void *sqlite3MallocZero(u64 n){
000603 void *p = sqlite3Malloc(n);
000604 if( p ){
000605 memset(p, 0, (size_t)n);
000606 }
000607 return p;
000608 }
000609
000610 /*
000611 ** Allocate and zero memory. If the allocation fails, make
000612 ** the mallocFailed flag in the connection pointer.
000613 */
000614 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
000615 void *p;
000616 testcase( db==0 );
000617 p = sqlite3DbMallocRaw(db, n);
000618 if( p ) memset(p, 0, (size_t)n);
000619 return p;
000620 }
000621
000622
000623 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
000624 ** slower case when the allocation cannot be fulfilled using lookaside.
000625 */
000626 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
000627 void *p;
000628 assert( db!=0 );
000629 p = sqlite3Malloc(n);
000630 if( !p ) sqlite3OomFault(db);
000631 sqlite3MemdebugSetType(p,
000632 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
000633 return p;
000634 }
000635
000636 /*
000637 ** Allocate memory, either lookaside (if possible) or heap.
000638 ** If the allocation fails, set the mallocFailed flag in
000639 ** the connection pointer.
000640 **
000641 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
000642 ** failure on the same database connection) then always return 0.
000643 ** Hence for a particular database connection, once malloc starts
000644 ** failing, it fails consistently until mallocFailed is reset.
000645 ** This is an important assumption. There are many places in the
000646 ** code that do things like this:
000647 **
000648 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
000649 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
000650 ** if( b ) a[10] = 9;
000651 **
000652 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
000653 ** that all prior mallocs (ex: "a") worked too.
000654 **
000655 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
000656 ** not a NULL pointer.
000657 */
000658 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
000659 void *p;
000660 if( db ) return sqlite3DbMallocRawNN(db, n);
000661 p = sqlite3Malloc(n);
000662 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000663 return p;
000664 }
000665 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
000666 #ifndef SQLITE_OMIT_LOOKASIDE
000667 LookasideSlot *pBuf;
000668 assert( db!=0 );
000669 assert( sqlite3_mutex_held(db->mutex) );
000670 assert( db->pnBytesFreed==0 );
000671 if( n>db->lookaside.sz ){
000672 if( !db->lookaside.bDisable ){
000673 db->lookaside.anStat[1]++;
000674 }else if( db->mallocFailed ){
000675 return 0;
000676 }
000677 return dbMallocRawFinish(db, n);
000678 }
000679 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000680 if( n<=LOOKASIDE_SMALL ){
000681 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
000682 db->lookaside.pSmallFree = pBuf->pNext;
000683 db->lookaside.anStat[0]++;
000684 return (void*)pBuf;
000685 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
000686 db->lookaside.pSmallInit = pBuf->pNext;
000687 db->lookaside.anStat[0]++;
000688 return (void*)pBuf;
000689 }
000690 }
000691 #endif
000692 if( (pBuf = db->lookaside.pFree)!=0 ){
000693 db->lookaside.pFree = pBuf->pNext;
000694 db->lookaside.anStat[0]++;
000695 return (void*)pBuf;
000696 }else if( (pBuf = db->lookaside.pInit)!=0 ){
000697 db->lookaside.pInit = pBuf->pNext;
000698 db->lookaside.anStat[0]++;
000699 return (void*)pBuf;
000700 }else{
000701 db->lookaside.anStat[2]++;
000702 }
000703 #else
000704 assert( db!=0 );
000705 assert( sqlite3_mutex_held(db->mutex) );
000706 assert( db->pnBytesFreed==0 );
000707 if( db->mallocFailed ){
000708 return 0;
000709 }
000710 #endif
000711 return dbMallocRawFinish(db, n);
000712 }
000713
000714 /* Forward declaration */
000715 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
000716
000717 /*
000718 ** Resize the block of memory pointed to by p to n bytes. If the
000719 ** resize fails, set the mallocFailed flag in the connection object.
000720 */
000721 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
000722 assert( db!=0 );
000723 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
000724 assert( sqlite3_mutex_held(db->mutex) );
000725 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
000726 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000727 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
000728 if( n<=LOOKASIDE_SMALL ) return p;
000729 }else
000730 #endif
000731 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
000732 if( n<=db->lookaside.szTrue ) return p;
000733 }
000734 }
000735 return dbReallocFinish(db, p, n);
000736 }
000737 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
000738 void *pNew = 0;
000739 assert( db!=0 );
000740 assert( p!=0 );
000741 if( db->mallocFailed==0 ){
000742 if( isLookaside(db, p) ){
000743 pNew = sqlite3DbMallocRawNN(db, n);
000744 if( pNew ){
000745 memcpy(pNew, p, lookasideMallocSize(db, p));
000746 sqlite3DbFree(db, p);
000747 }
000748 }else{
000749 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000750 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
000751 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
000752 pNew = sqlite3Realloc(p, n);
000753 if( !pNew ){
000754 sqlite3OomFault(db);
000755 }
000756 sqlite3MemdebugSetType(pNew,
000757 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
000758 }
000759 }
000760 return pNew;
000761 }
000762
000763 /*
000764 ** Attempt to reallocate p. If the reallocation fails, then free p
000765 ** and set the mallocFailed flag in the database connection.
000766 */
000767 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
000768 void *pNew;
000769 pNew = sqlite3DbRealloc(db, p, n);
000770 if( !pNew ){
000771 sqlite3DbFree(db, p);
000772 }
000773 return pNew;
000774 }
000775
000776 /*
000777 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
000778 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
000779 ** is because when memory debugging is turned on, these two functions are
000780 ** called via macros that record the current file and line number in the
000781 ** ThreadData structure.
000782 */
000783 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
000784 char *zNew;
000785 size_t n;
000786 if( z==0 ){
000787 return 0;
000788 }
000789 n = strlen(z) + 1;
000790 zNew = sqlite3DbMallocRaw(db, n);
000791 if( zNew ){
000792 memcpy(zNew, z, n);
000793 }
000794 return zNew;
000795 }
000796 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
000797 char *zNew;
000798 assert( db!=0 );
000799 assert( z!=0 || n==0 );
000800 assert( (n&0x7fffffff)==n );
000801 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
000802 if( zNew ){
000803 memcpy(zNew, z, (size_t)n);
000804 zNew[n] = 0;
000805 }
000806 return zNew;
000807 }
000808
000809 /*
000810 ** The text between zStart and zEnd represents a phrase within a larger
000811 ** SQL statement. Make a copy of this phrase in space obtained form
000812 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
000813 */
000814 char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
000815 int n;
000816 #ifdef SQLITE_DEBUG
000817 /* Because of the way the parser works, the span is guaranteed to contain
000818 ** at least one non-space character */
000819 for(n=0; sqlite3Isspace(zStart[n]); n++){ assert( &zStart[n]<zEnd ); }
000820 #endif
000821 while( sqlite3Isspace(zStart[0]) ) zStart++;
000822 n = (int)(zEnd - zStart);
000823 while( sqlite3Isspace(zStart[n-1]) ) n--;
000824 return sqlite3DbStrNDup(db, zStart, n);
000825 }
000826
000827 /*
000828 ** Free any prior content in *pz and replace it with a copy of zNew.
000829 */
000830 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
000831 char *z = sqlite3DbStrDup(db, zNew);
000832 sqlite3DbFree(db, *pz);
000833 *pz = z;
000834 }
000835
000836 /*
000837 ** Call this routine to record the fact that an OOM (out-of-memory) error
000838 ** has happened. This routine will set db->mallocFailed, and also
000839 ** temporarily disable the lookaside memory allocator and interrupt
000840 ** any running VDBEs.
000841 **
000842 ** Always return a NULL pointer so that this routine can be invoked using
000843 **
000844 ** return sqlite3OomFault(db);
000845 **
000846 ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
000847 ** common case where no OOM occurs.
000848 */
000849 void *sqlite3OomFault(sqlite3 *db){
000850 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
000851 db->mallocFailed = 1;
000852 if( db->nVdbeExec>0 ){
000853 AtomicStore(&db->u1.isInterrupted, 1);
000854 }
000855 DisableLookaside;
000856 if( db->pParse ){
000857 Parse *pParse;
000858 sqlite3ErrorMsg(db->pParse, "out of memory");
000859 db->pParse->rc = SQLITE_NOMEM_BKPT;
000860 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
000861 pParse->nErr++;
000862 pParse->rc = SQLITE_NOMEM;
000863 }
000864 }
000865 }
000866 return 0;
000867 }
000868
000869 /*
000870 ** This routine reactivates the memory allocator and clears the
000871 ** db->mallocFailed flag as necessary.
000872 **
000873 ** The memory allocator is not restarted if there are running
000874 ** VDBEs.
000875 */
000876 void sqlite3OomClear(sqlite3 *db){
000877 if( db->mallocFailed && db->nVdbeExec==0 ){
000878 db->mallocFailed = 0;
000879 AtomicStore(&db->u1.isInterrupted, 0);
000880 assert( db->lookaside.bDisable>0 );
000881 EnableLookaside;
000882 }
000883 }
000884
000885 /*
000886 ** Take actions at the end of an API call to deal with error codes.
000887 */
000888 static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
000889 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
000890 sqlite3OomClear(db);
000891 sqlite3Error(db, SQLITE_NOMEM);
000892 return SQLITE_NOMEM_BKPT;
000893 }
000894 return rc & db->errMask;
000895 }
000896
000897 /*
000898 ** This function must be called before exiting any API function (i.e.
000899 ** returning control to the user) that has called sqlite3_malloc or
000900 ** sqlite3_realloc.
000901 **
000902 ** The returned value is normally a copy of the second argument to this
000903 ** function. However, if a malloc() failure has occurred since the previous
000904 ** invocation SQLITE_NOMEM is returned instead.
000905 **
000906 ** If an OOM as occurred, then the connection error-code (the value
000907 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
000908 */
000909 int sqlite3ApiExit(sqlite3* db, int rc){
000910 /* If the db handle must hold the connection handle mutex here.
000911 ** Otherwise the read (and possible write) of db->mallocFailed
000912 ** is unsafe, as is the call to sqlite3Error().
000913 */
000914 assert( db!=0 );
000915 assert( sqlite3_mutex_held(db->mutex) );
000916 if( db->mallocFailed || rc ){
000917 return apiHandleError(db, rc);
000918 }
000919 return 0;
000920 }