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 ** An instance of the following object records the location of 000037 ** each unused scratch buffer. 000038 */ 000039 typedef struct ScratchFreeslot { 000040 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ 000041 } ScratchFreeslot; 000042 000043 /* 000044 ** State information local to the memory allocation subsystem. 000045 */ 000046 static SQLITE_WSD struct Mem0Global { 000047 sqlite3_mutex *mutex; /* Mutex to serialize access */ 000048 sqlite3_int64 alarmThreshold; /* The soft heap limit */ 000049 000050 /* 000051 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory 000052 ** (so that a range test can be used to determine if an allocation 000053 ** being freed came from pScratch) and a pointer to the list of 000054 ** unused scratch allocations. 000055 */ 000056 void *pScratchEnd; 000057 ScratchFreeslot *pScratchFree; 000058 u32 nScratchFree; 000059 000060 /* 000061 ** True if heap is nearly "full" where "full" is defined by the 000062 ** sqlite3_soft_heap_limit() setting. 000063 */ 000064 int nearlyFull; 000065 } mem0 = { 0, 0, 0, 0, 0, 0 }; 000066 000067 #define mem0 GLOBAL(struct Mem0Global, mem0) 000068 000069 /* 000070 ** Return the memory allocator mutex. sqlite3_status() needs it. 000071 */ 000072 sqlite3_mutex *sqlite3MallocMutex(void){ 000073 return mem0.mutex; 000074 } 000075 000076 #ifndef SQLITE_OMIT_DEPRECATED 000077 /* 000078 ** Deprecated external interface. It used to set an alarm callback 000079 ** that was invoked when memory usage grew too large. Now it is a 000080 ** no-op. 000081 */ 000082 int sqlite3_memory_alarm( 000083 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 000084 void *pArg, 000085 sqlite3_int64 iThreshold 000086 ){ 000087 (void)xCallback; 000088 (void)pArg; 000089 (void)iThreshold; 000090 return SQLITE_OK; 000091 } 000092 #endif 000093 000094 /* 000095 ** Set the soft heap-size limit for the library. Passing a zero or 000096 ** negative value indicates no limit. 000097 */ 000098 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ 000099 sqlite3_int64 priorLimit; 000100 sqlite3_int64 excess; 000101 sqlite3_int64 nUsed; 000102 #ifndef SQLITE_OMIT_AUTOINIT 000103 int rc = sqlite3_initialize(); 000104 if( rc ) return -1; 000105 #endif 000106 sqlite3_mutex_enter(mem0.mutex); 000107 priorLimit = mem0.alarmThreshold; 000108 if( n<0 ){ 000109 sqlite3_mutex_leave(mem0.mutex); 000110 return priorLimit; 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 ** Initialize the memory allocation subsystem. 000127 */ 000128 int sqlite3MallocInit(void){ 000129 int rc; 000130 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 000131 sqlite3MemSetDefault(); 000132 } 000133 memset(&mem0, 0, sizeof(mem0)); 000134 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 000135 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 000136 && sqlite3GlobalConfig.nScratch>0 ){ 000137 int i, n, sz; 000138 ScratchFreeslot *pSlot; 000139 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); 000140 sqlite3GlobalConfig.szScratch = sz; 000141 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; 000142 n = sqlite3GlobalConfig.nScratch; 000143 mem0.pScratchFree = pSlot; 000144 mem0.nScratchFree = n; 000145 for(i=0; i<n-1; i++){ 000146 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); 000147 pSlot = pSlot->pNext; 000148 } 000149 pSlot->pNext = 0; 000150 mem0.pScratchEnd = (void*)&pSlot[1]; 000151 }else{ 000152 mem0.pScratchEnd = 0; 000153 sqlite3GlobalConfig.pScratch = 0; 000154 sqlite3GlobalConfig.szScratch = 0; 000155 sqlite3GlobalConfig.nScratch = 0; 000156 } 000157 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 000158 || sqlite3GlobalConfig.nPage<=0 ){ 000159 sqlite3GlobalConfig.pPage = 0; 000160 sqlite3GlobalConfig.szPage = 0; 000161 } 000162 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 000163 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); 000164 return rc; 000165 } 000166 000167 /* 000168 ** Return true if the heap is currently under memory pressure - in other 000169 ** words if the amount of heap used is close to the limit set by 000170 ** sqlite3_soft_heap_limit(). 000171 */ 000172 int sqlite3HeapNearlyFull(void){ 000173 return mem0.nearlyFull; 000174 } 000175 000176 /* 000177 ** Deinitialize the memory allocation subsystem. 000178 */ 000179 void sqlite3MallocEnd(void){ 000180 if( sqlite3GlobalConfig.m.xShutdown ){ 000181 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 000182 } 000183 memset(&mem0, 0, sizeof(mem0)); 000184 } 000185 000186 /* 000187 ** Return the amount of memory currently checked out. 000188 */ 000189 sqlite3_int64 sqlite3_memory_used(void){ 000190 sqlite3_int64 res, mx; 000191 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0); 000192 return res; 000193 } 000194 000195 /* 000196 ** Return the maximum amount of memory that has ever been 000197 ** checked out since either the beginning of this process 000198 ** or since the most recent reset. 000199 */ 000200 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 000201 sqlite3_int64 res, mx; 000202 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag); 000203 return mx; 000204 } 000205 000206 /* 000207 ** Trigger the alarm 000208 */ 000209 static void sqlite3MallocAlarm(int nByte){ 000210 if( mem0.alarmThreshold<=0 ) return; 000211 sqlite3_mutex_leave(mem0.mutex); 000212 sqlite3_release_memory(nByte); 000213 sqlite3_mutex_enter(mem0.mutex); 000214 } 000215 000216 /* 000217 ** Do a memory allocation with statistics and alarms. Assume the 000218 ** lock is already held. 000219 */ 000220 static int mallocWithAlarm(int n, void **pp){ 000221 int nFull; 000222 void *p; 000223 assert( sqlite3_mutex_held(mem0.mutex) ); 000224 nFull = sqlite3GlobalConfig.m.xRoundup(n); 000225 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); 000226 if( mem0.alarmThreshold>0 ){ 000227 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 000228 if( nUsed >= mem0.alarmThreshold - nFull ){ 000229 mem0.nearlyFull = 1; 000230 sqlite3MallocAlarm(nFull); 000231 }else{ 000232 mem0.nearlyFull = 0; 000233 } 000234 } 000235 p = sqlite3GlobalConfig.m.xMalloc(nFull); 000236 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 000237 if( p==0 && mem0.alarmThreshold>0 ){ 000238 sqlite3MallocAlarm(nFull); 000239 p = sqlite3GlobalConfig.m.xMalloc(nFull); 000240 } 000241 #endif 000242 if( p ){ 000243 nFull = sqlite3MallocSize(p); 000244 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); 000245 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); 000246 } 000247 *pp = p; 000248 return nFull; 000249 } 000250 000251 /* 000252 ** Allocate memory. This routine is like sqlite3_malloc() except that it 000253 ** assumes the memory subsystem has already been initialized. 000254 */ 000255 void *sqlite3Malloc(u64 n){ 000256 void *p; 000257 if( n==0 || n>=0x7fffff00 ){ 000258 /* A memory allocation of a number of bytes which is near the maximum 000259 ** signed integer value might cause an integer overflow inside of the 000260 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving 000261 ** 255 bytes of overhead. SQLite itself will never use anything near 000262 ** this amount. The only way to reach the limit is with sqlite3_malloc() */ 000263 p = 0; 000264 }else if( sqlite3GlobalConfig.bMemstat ){ 000265 sqlite3_mutex_enter(mem0.mutex); 000266 mallocWithAlarm((int)n, &p); 000267 sqlite3_mutex_leave(mem0.mutex); 000268 }else{ 000269 p = sqlite3GlobalConfig.m.xMalloc((int)n); 000270 } 000271 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ 000272 return p; 000273 } 000274 000275 /* 000276 ** This version of the memory allocation is for use by the application. 000277 ** First make sure the memory subsystem is initialized, then do the 000278 ** allocation. 000279 */ 000280 void *sqlite3_malloc(int n){ 000281 #ifndef SQLITE_OMIT_AUTOINIT 000282 if( sqlite3_initialize() ) return 0; 000283 #endif 000284 return n<=0 ? 0 : sqlite3Malloc(n); 000285 } 000286 void *sqlite3_malloc64(sqlite3_uint64 n){ 000287 #ifndef SQLITE_OMIT_AUTOINIT 000288 if( sqlite3_initialize() ) return 0; 000289 #endif 000290 return sqlite3Malloc(n); 000291 } 000292 000293 /* 000294 ** Each thread may only have a single outstanding allocation from 000295 ** xScratchMalloc(). We verify this constraint in the single-threaded 000296 ** case by setting scratchAllocOut to 1 when an allocation 000297 ** is outstanding clearing it when the allocation is freed. 000298 */ 000299 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 000300 static int scratchAllocOut = 0; 000301 #endif 000302 000303 000304 /* 000305 ** Allocate memory that is to be used and released right away. 000306 ** This routine is similar to alloca() in that it is not intended 000307 ** for situations where the memory might be held long-term. This 000308 ** routine is intended to get memory to old large transient data 000309 ** structures that would not normally fit on the stack of an 000310 ** embedded processor. 000311 */ 000312 void *sqlite3ScratchMalloc(int n){ 000313 void *p; 000314 assert( n>0 ); 000315 000316 sqlite3_mutex_enter(mem0.mutex); 000317 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n); 000318 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ 000319 p = mem0.pScratchFree; 000320 mem0.pScratchFree = mem0.pScratchFree->pNext; 000321 mem0.nScratchFree--; 000322 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); 000323 sqlite3_mutex_leave(mem0.mutex); 000324 }else{ 000325 sqlite3_mutex_leave(mem0.mutex); 000326 p = sqlite3Malloc(n); 000327 if( sqlite3GlobalConfig.bMemstat && p ){ 000328 sqlite3_mutex_enter(mem0.mutex); 000329 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); 000330 sqlite3_mutex_leave(mem0.mutex); 000331 } 000332 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); 000333 } 000334 assert( sqlite3_mutex_notheld(mem0.mutex) ); 000335 000336 000337 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 000338 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch 000339 ** buffers per thread. 000340 ** 000341 ** This can only be checked in single-threaded mode. 000342 */ 000343 assert( scratchAllocOut==0 ); 000344 if( p ) scratchAllocOut++; 000345 #endif 000346 000347 return p; 000348 } 000349 void sqlite3ScratchFree(void *p){ 000350 if( p ){ 000351 000352 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 000353 /* Verify that no more than two scratch allocation per thread 000354 ** is outstanding at one time. (This is only checked in the 000355 ** single-threaded case since checking in the multi-threaded case 000356 ** would be much more complicated.) */ 000357 assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); 000358 scratchAllocOut--; 000359 #endif 000360 000361 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){ 000362 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ 000363 ScratchFreeslot *pSlot; 000364 pSlot = (ScratchFreeslot*)p; 000365 sqlite3_mutex_enter(mem0.mutex); 000366 pSlot->pNext = mem0.pScratchFree; 000367 mem0.pScratchFree = pSlot; 000368 mem0.nScratchFree++; 000369 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); 000370 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); 000371 sqlite3_mutex_leave(mem0.mutex); 000372 }else{ 000373 /* Release memory back to the heap */ 000374 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); 000375 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); 000376 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 000377 if( sqlite3GlobalConfig.bMemstat ){ 000378 int iSize = sqlite3MallocSize(p); 000379 sqlite3_mutex_enter(mem0.mutex); 000380 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); 000381 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); 000382 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); 000383 sqlite3GlobalConfig.m.xFree(p); 000384 sqlite3_mutex_leave(mem0.mutex); 000385 }else{ 000386 sqlite3GlobalConfig.m.xFree(p); 000387 } 000388 } 000389 } 000390 } 000391 000392 /* 000393 ** TRUE if p is a lookaside memory allocation from db 000394 */ 000395 #ifndef SQLITE_OMIT_LOOKASIDE 000396 static int isLookaside(sqlite3 *db, void *p){ 000397 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); 000398 } 000399 #else 000400 #define isLookaside(A,B) 0 000401 #endif 000402 000403 /* 000404 ** Return the size of a memory allocation previously obtained from 000405 ** sqlite3Malloc() or sqlite3_malloc(). 000406 */ 000407 int sqlite3MallocSize(void *p){ 000408 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 000409 return sqlite3GlobalConfig.m.xSize(p); 000410 } 000411 int sqlite3DbMallocSize(sqlite3 *db, void *p){ 000412 assert( p!=0 ); 000413 if( db==0 || !isLookaside(db,p) ){ 000414 #if SQLITE_DEBUG 000415 if( db==0 ){ 000416 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 000417 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 000418 }else{ 000419 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 000420 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 000421 } 000422 #endif 000423 return sqlite3GlobalConfig.m.xSize(p); 000424 }else{ 000425 assert( sqlite3_mutex_held(db->mutex) ); 000426 return db->lookaside.sz; 000427 } 000428 } 000429 sqlite3_uint64 sqlite3_msize(void *p){ 000430 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 000431 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 000432 return p ? sqlite3GlobalConfig.m.xSize(p) : 0; 000433 } 000434 000435 /* 000436 ** Free memory previously obtained from sqlite3Malloc(). 000437 */ 000438 void sqlite3_free(void *p){ 000439 if( p==0 ) return; /* IMP: R-49053-54554 */ 000440 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 000441 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 000442 if( sqlite3GlobalConfig.bMemstat ){ 000443 sqlite3_mutex_enter(mem0.mutex); 000444 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); 000445 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); 000446 sqlite3GlobalConfig.m.xFree(p); 000447 sqlite3_mutex_leave(mem0.mutex); 000448 }else{ 000449 sqlite3GlobalConfig.m.xFree(p); 000450 } 000451 } 000452 000453 /* 000454 ** Add the size of memory allocation "p" to the count in 000455 ** *db->pnBytesFreed. 000456 */ 000457 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ 000458 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); 000459 } 000460 000461 /* 000462 ** Free memory that might be associated with a particular database 000463 ** connection. 000464 */ 000465 void sqlite3DbFree(sqlite3 *db, void *p){ 000466 assert( db==0 || sqlite3_mutex_held(db->mutex) ); 000467 if( p==0 ) return; 000468 if( db ){ 000469 if( db->pnBytesFreed ){ 000470 measureAllocationSize(db, p); 000471 return; 000472 } 000473 if( isLookaside(db, p) ){ 000474 LookasideSlot *pBuf = (LookasideSlot*)p; 000475 #if SQLITE_DEBUG 000476 /* Trash all content in the buffer being freed */ 000477 memset(p, 0xaa, db->lookaside.sz); 000478 #endif 000479 pBuf->pNext = db->lookaside.pFree; 000480 db->lookaside.pFree = pBuf; 000481 db->lookaside.nOut--; 000482 return; 000483 } 000484 } 000485 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 000486 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 000487 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 000488 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 000489 sqlite3_free(p); 000490 } 000491 000492 /* 000493 ** Change the size of an existing memory allocation 000494 */ 000495 void *sqlite3Realloc(void *pOld, u64 nBytes){ 000496 int nOld, nNew, nDiff; 000497 void *pNew; 000498 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); 000499 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); 000500 if( pOld==0 ){ 000501 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ 000502 } 000503 if( nBytes==0 ){ 000504 sqlite3_free(pOld); /* IMP: R-26507-47431 */ 000505 return 0; 000506 } 000507 if( nBytes>=0x7fffff00 ){ 000508 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ 000509 return 0; 000510 } 000511 nOld = sqlite3MallocSize(pOld); 000512 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second 000513 ** argument to xRealloc is always a value returned by a prior call to 000514 ** xRoundup. */ 000515 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); 000516 if( nOld==nNew ){ 000517 pNew = pOld; 000518 }else if( sqlite3GlobalConfig.bMemstat ){ 000519 sqlite3_mutex_enter(mem0.mutex); 000520 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); 000521 nDiff = nNew - nOld; 000522 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 000523 mem0.alarmThreshold-nDiff ){ 000524 sqlite3MallocAlarm(nDiff); 000525 } 000526 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 000527 if( pNew==0 && mem0.alarmThreshold>0 ){ 000528 sqlite3MallocAlarm((int)nBytes); 000529 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 000530 } 000531 if( pNew ){ 000532 nNew = sqlite3MallocSize(pNew); 000533 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 000534 } 000535 sqlite3_mutex_leave(mem0.mutex); 000536 }else{ 000537 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 000538 } 000539 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ 000540 return pNew; 000541 } 000542 000543 /* 000544 ** The public interface to sqlite3Realloc. Make sure that the memory 000545 ** subsystem is initialized prior to invoking sqliteRealloc. 000546 */ 000547 void *sqlite3_realloc(void *pOld, int n){ 000548 #ifndef SQLITE_OMIT_AUTOINIT 000549 if( sqlite3_initialize() ) return 0; 000550 #endif 000551 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ 000552 return sqlite3Realloc(pOld, n); 000553 } 000554 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ 000555 #ifndef SQLITE_OMIT_AUTOINIT 000556 if( sqlite3_initialize() ) return 0; 000557 #endif 000558 return sqlite3Realloc(pOld, n); 000559 } 000560 000561 000562 /* 000563 ** Allocate and zero memory. 000564 */ 000565 void *sqlite3MallocZero(u64 n){ 000566 void *p = sqlite3Malloc(n); 000567 if( p ){ 000568 memset(p, 0, (size_t)n); 000569 } 000570 return p; 000571 } 000572 000573 /* 000574 ** Allocate and zero memory. If the allocation fails, make 000575 ** the mallocFailed flag in the connection pointer. 000576 */ 000577 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ 000578 void *p; 000579 testcase( db==0 ); 000580 p = sqlite3DbMallocRaw(db, n); 000581 if( p ) memset(p, 0, (size_t)n); 000582 return p; 000583 } 000584 000585 000586 /* Finish the work of sqlite3DbMallocRawNN for the unusual and 000587 ** slower case when the allocation cannot be fulfilled using lookaside. 000588 */ 000589 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){ 000590 void *p; 000591 assert( db!=0 ); 000592 p = sqlite3Malloc(n); 000593 if( !p ) sqlite3OomFault(db); 000594 sqlite3MemdebugSetType(p, 000595 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); 000596 return p; 000597 } 000598 000599 /* 000600 ** Allocate memory, either lookaside (if possible) or heap. 000601 ** If the allocation fails, set the mallocFailed flag in 000602 ** the connection pointer. 000603 ** 000604 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc 000605 ** failure on the same database connection) then always return 0. 000606 ** Hence for a particular database connection, once malloc starts 000607 ** failing, it fails consistently until mallocFailed is reset. 000608 ** This is an important assumption. There are many places in the 000609 ** code that do things like this: 000610 ** 000611 ** int *a = (int*)sqlite3DbMallocRaw(db, 100); 000612 ** int *b = (int*)sqlite3DbMallocRaw(db, 200); 000613 ** if( b ) a[10] = 9; 000614 ** 000615 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed 000616 ** that all prior mallocs (ex: "a") worked too. 000617 ** 000618 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is 000619 ** not a NULL pointer. 000620 */ 000621 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ 000622 void *p; 000623 if( db ) return sqlite3DbMallocRawNN(db, n); 000624 p = sqlite3Malloc(n); 000625 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 000626 return p; 000627 } 000628 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){ 000629 #ifndef SQLITE_OMIT_LOOKASIDE 000630 LookasideSlot *pBuf; 000631 assert( db!=0 ); 000632 assert( sqlite3_mutex_held(db->mutex) ); 000633 assert( db->pnBytesFreed==0 ); 000634 if( db->lookaside.bDisable==0 ){ 000635 assert( db->mallocFailed==0 ); 000636 if( n>db->lookaside.sz ){ 000637 db->lookaside.anStat[1]++; 000638 }else if( (pBuf = db->lookaside.pFree)==0 ){ 000639 db->lookaside.anStat[2]++; 000640 }else{ 000641 db->lookaside.pFree = pBuf->pNext; 000642 db->lookaside.nOut++; 000643 db->lookaside.anStat[0]++; 000644 if( db->lookaside.nOut>db->lookaside.mxOut ){ 000645 db->lookaside.mxOut = db->lookaside.nOut; 000646 } 000647 return (void*)pBuf; 000648 } 000649 }else if( db->mallocFailed ){ 000650 return 0; 000651 } 000652 #else 000653 assert( db!=0 ); 000654 assert( sqlite3_mutex_held(db->mutex) ); 000655 assert( db->pnBytesFreed==0 ); 000656 if( db->mallocFailed ){ 000657 return 0; 000658 } 000659 #endif 000660 return dbMallocRawFinish(db, n); 000661 } 000662 000663 /* Forward declaration */ 000664 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n); 000665 000666 /* 000667 ** Resize the block of memory pointed to by p to n bytes. If the 000668 ** resize fails, set the mallocFailed flag in the connection object. 000669 */ 000670 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ 000671 assert( db!=0 ); 000672 if( p==0 ) return sqlite3DbMallocRawNN(db, n); 000673 assert( sqlite3_mutex_held(db->mutex) ); 000674 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p; 000675 return dbReallocFinish(db, p, n); 000676 } 000677 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){ 000678 void *pNew = 0; 000679 assert( db!=0 ); 000680 assert( p!=0 ); 000681 if( db->mallocFailed==0 ){ 000682 if( isLookaside(db, p) ){ 000683 pNew = sqlite3DbMallocRawNN(db, n); 000684 if( pNew ){ 000685 memcpy(pNew, p, db->lookaside.sz); 000686 sqlite3DbFree(db, p); 000687 } 000688 }else{ 000689 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 000690 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 000691 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 000692 pNew = sqlite3_realloc64(p, n); 000693 if( !pNew ){ 000694 sqlite3OomFault(db); 000695 } 000696 sqlite3MemdebugSetType(pNew, 000697 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 000698 } 000699 } 000700 return pNew; 000701 } 000702 000703 /* 000704 ** Attempt to reallocate p. If the reallocation fails, then free p 000705 ** and set the mallocFailed flag in the database connection. 000706 */ 000707 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){ 000708 void *pNew; 000709 pNew = sqlite3DbRealloc(db, p, n); 000710 if( !pNew ){ 000711 sqlite3DbFree(db, p); 000712 } 000713 return pNew; 000714 } 000715 000716 /* 000717 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 000718 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 000719 ** is because when memory debugging is turned on, these two functions are 000720 ** called via macros that record the current file and line number in the 000721 ** ThreadData structure. 000722 */ 000723 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 000724 char *zNew; 000725 size_t n; 000726 if( z==0 ){ 000727 return 0; 000728 } 000729 n = strlen(z) + 1; 000730 zNew = sqlite3DbMallocRaw(db, n); 000731 if( zNew ){ 000732 memcpy(zNew, z, n); 000733 } 000734 return zNew; 000735 } 000736 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ 000737 char *zNew; 000738 assert( db!=0 ); 000739 if( z==0 ){ 000740 return 0; 000741 } 000742 assert( (n&0x7fffffff)==n ); 000743 zNew = sqlite3DbMallocRawNN(db, n+1); 000744 if( zNew ){ 000745 memcpy(zNew, z, (size_t)n); 000746 zNew[n] = 0; 000747 } 000748 return zNew; 000749 } 000750 000751 /* 000752 ** Free any prior content in *pz and replace it with a copy of zNew. 000753 */ 000754 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){ 000755 sqlite3DbFree(db, *pz); 000756 *pz = sqlite3DbStrDup(db, zNew); 000757 } 000758 000759 /* 000760 ** Call this routine to record the fact that an OOM (out-of-memory) error 000761 ** has happened. This routine will set db->mallocFailed, and also 000762 ** temporarily disable the lookaside memory allocator and interrupt 000763 ** any running VDBEs. 000764 */ 000765 void sqlite3OomFault(sqlite3 *db){ 000766 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ 000767 db->mallocFailed = 1; 000768 if( db->nVdbeExec>0 ){ 000769 db->u1.isInterrupted = 1; 000770 } 000771 db->lookaside.bDisable++; 000772 } 000773 } 000774 000775 /* 000776 ** This routine reactivates the memory allocator and clears the 000777 ** db->mallocFailed flag as necessary. 000778 ** 000779 ** The memory allocator is not restarted if there are running 000780 ** VDBEs. 000781 */ 000782 void sqlite3OomClear(sqlite3 *db){ 000783 if( db->mallocFailed && db->nVdbeExec==0 ){ 000784 db->mallocFailed = 0; 000785 db->u1.isInterrupted = 0; 000786 assert( db->lookaside.bDisable>0 ); 000787 db->lookaside.bDisable--; 000788 } 000789 } 000790 000791 /* 000792 ** Take actions at the end of an API call to indicate an OOM error 000793 */ 000794 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ 000795 sqlite3OomClear(db); 000796 sqlite3Error(db, SQLITE_NOMEM); 000797 return SQLITE_NOMEM_BKPT; 000798 } 000799 000800 /* 000801 ** This function must be called before exiting any API function (i.e. 000802 ** returning control to the user) that has called sqlite3_malloc or 000803 ** sqlite3_realloc. 000804 ** 000805 ** The returned value is normally a copy of the second argument to this 000806 ** function. However, if a malloc() failure has occurred since the previous 000807 ** invocation SQLITE_NOMEM is returned instead. 000808 ** 000809 ** If an OOM as occurred, then the connection error-code (the value 000810 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. 000811 */ 000812 int sqlite3ApiExit(sqlite3* db, int rc){ 000813 /* If the db handle must hold the connection handle mutex here. 000814 ** Otherwise the read (and possible write) of db->mallocFailed 000815 ** is unsafe, as is the call to sqlite3Error(). 000816 */ 000817 assert( db!=0 ); 000818 assert( sqlite3_mutex_held(db->mutex) ); 000819 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ 000820 return apiOomError(db); 000821 } 000822 return rc & db->errMask; 000823 }