1a3152895Sdrh /* 2a3152895Sdrh ** 2001 September 15 3a3152895Sdrh ** 4a3152895Sdrh ** The author disclaims copyright to this source code. In place of 5a3152895Sdrh ** a legal notice, here is a blessing: 6a3152895Sdrh ** 7a3152895Sdrh ** May you do good and not evil. 8a3152895Sdrh ** May you find forgiveness for yourself and forgive others. 9a3152895Sdrh ** May you share freely, never taking more than you give. 10a3152895Sdrh ** 11a3152895Sdrh ************************************************************************* 12fec00eabSdrh ** 13a3152895Sdrh ** Memory allocation functions used throughout sqlite. 14a3152895Sdrh */ 15a3152895Sdrh #include "sqliteInt.h" 16a3152895Sdrh #include <stdarg.h> 17a3152895Sdrh 18a3152895Sdrh /* 198468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently 208468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to 218468024dSdanielk1977 ** cache database pages that are not currently in use. 22a3152895Sdrh */ 23a3152895Sdrh int sqlite3_release_memory(int n){ 2486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 259f129f46Sdrh return sqlite3PcacheReleaseMemory(n); 261e536953Sdanielk1977 #else 279f129f46Sdrh /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine 289f129f46Sdrh ** is a no-op returning zero if SQLite is not compiled with 299f129f46Sdrh ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ 3062c14b34Sdanielk1977 UNUSED_PARAMETER(n); 319f129f46Sdrh return 0; 321e536953Sdanielk1977 #endif 33a3152895Sdrh } 34a3152895Sdrh 35fec00eabSdrh /* 36badc980aSdrh ** An instance of the following object records the location of 37badc980aSdrh ** each unused scratch buffer. 38badc980aSdrh */ 39badc980aSdrh typedef struct ScratchFreeslot { 40badc980aSdrh struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ 41badc980aSdrh } ScratchFreeslot; 42badc980aSdrh 43badc980aSdrh /* 44fec00eabSdrh ** State information local to the memory allocation subsystem. 45fec00eabSdrh */ 465c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global { 47fec00eabSdrh sqlite3_mutex *mutex; /* Mutex to serialize access */ 484ef299a3Sdrh sqlite3_int64 alarmThreshold; /* The soft heap limit */ 49fec00eabSdrh 50fec00eabSdrh /* 51badc980aSdrh ** Pointers to the end of sqlite3GlobalConfig.pScratch memory 52badc980aSdrh ** (so that a range test can be used to determine if an allocation 53badc980aSdrh ** being freed came from pScratch) and a pointer to the list of 54badc980aSdrh ** unused scratch allocations. 559ac3fe97Sdrh */ 56badc980aSdrh void *pScratchEnd; 57badc980aSdrh ScratchFreeslot *pScratchFree; 58badc980aSdrh u32 nScratchFree; 5950d1b5f3Sdrh 6050d1b5f3Sdrh /* 6150d1b5f3Sdrh ** True if heap is nearly "full" where "full" is defined by the 6250d1b5f3Sdrh ** sqlite3_soft_heap_limit() setting. 6350d1b5f3Sdrh */ 6450d1b5f3Sdrh int nearlyFull; 654ef299a3Sdrh } mem0 = { 0, 0, 0, 0, 0, 0 }; 665c8f8587Sdanielk1977 675c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0) 68fec00eabSdrh 69fec00eabSdrh /* 70af89fe66Sdrh ** Return the memory allocator mutex. sqlite3_status() needs it. 71af89fe66Sdrh */ 72af89fe66Sdrh sqlite3_mutex *sqlite3MallocMutex(void){ 73af89fe66Sdrh return mem0.mutex; 74af89fe66Sdrh } 75af89fe66Sdrh 76f82ccf64Sdrh #ifndef SQLITE_OMIT_DEPRECATED 77f82ccf64Sdrh /* 78*5fb72e5fSdrh ** Deprecated external interface. It used to set an alarm callback 79*5fb72e5fSdrh ** that was invoked when memory usage grew too large. Now it is a 80*5fb72e5fSdrh ** no-op. 81f82ccf64Sdrh */ 82f82ccf64Sdrh int sqlite3_memory_alarm( 83f82ccf64Sdrh void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 84f82ccf64Sdrh void *pArg, 85f82ccf64Sdrh sqlite3_int64 iThreshold 86f82ccf64Sdrh ){ 87*5fb72e5fSdrh (void)xCallback; 88*5fb72e5fSdrh (void)pArg; 89*5fb72e5fSdrh (void)iThreshold; 904ef299a3Sdrh return SQLITE_OK; 91f82ccf64Sdrh } 92f82ccf64Sdrh #endif 93f82ccf64Sdrh 94f82ccf64Sdrh /* 95f82ccf64Sdrh ** Set the soft heap-size limit for the library. Passing a zero or 96f82ccf64Sdrh ** negative value indicates no limit. 97f82ccf64Sdrh */ 98f82ccf64Sdrh sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ 99f82ccf64Sdrh sqlite3_int64 priorLimit; 100*5fb72e5fSdrh sqlite3_int64 excess; 101*5fb72e5fSdrh sqlite3_int64 nUsed; 102f82ccf64Sdrh #ifndef SQLITE_OMIT_AUTOINIT 103de0f1815Sdrh int rc = sqlite3_initialize(); 104de0f1815Sdrh if( rc ) return -1; 105f82ccf64Sdrh #endif 106f82ccf64Sdrh sqlite3_mutex_enter(mem0.mutex); 107f82ccf64Sdrh priorLimit = mem0.alarmThreshold; 108*5fb72e5fSdrh if( n<0 ){ 1094ef299a3Sdrh sqlite3_mutex_leave(mem0.mutex); 110f82ccf64Sdrh return priorLimit; 111f82ccf64Sdrh } 112*5fb72e5fSdrh mem0.alarmThreshold = n; 113*5fb72e5fSdrh nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 114*5fb72e5fSdrh mem0.nearlyFull = (n>0 && n<=nUsed); 115*5fb72e5fSdrh sqlite3_mutex_leave(mem0.mutex); 116*5fb72e5fSdrh excess = sqlite3_memory_used() - n; 117*5fb72e5fSdrh if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); 118*5fb72e5fSdrh return priorLimit; 119*5fb72e5fSdrh } 120f82ccf64Sdrh void sqlite3_soft_heap_limit(int n){ 121f82ccf64Sdrh if( n<0 ) n = 0; 122f82ccf64Sdrh sqlite3_soft_heap_limit64(n); 123f82ccf64Sdrh } 124f82ccf64Sdrh 125f82ccf64Sdrh /* 126fec00eabSdrh ** Initialize the memory allocation subsystem. 127fec00eabSdrh */ 128fec00eabSdrh int sqlite3MallocInit(void){ 129592f0cb1Sdrh int rc; 130075c23afSdanielk1977 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 131fec00eabSdrh sqlite3MemSetDefault(); 132fec00eabSdrh } 133fec00eabSdrh memset(&mem0, 0, sizeof(mem0)); 134075c23afSdanielk1977 if( sqlite3GlobalConfig.bCoreMutex ){ 13559f8c08eSdanielk1977 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 136fec00eabSdrh } 137075c23afSdanielk1977 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 1387ff2719eSdrh && sqlite3GlobalConfig.nScratch>0 ){ 139badc980aSdrh int i, n, sz; 140badc980aSdrh ScratchFreeslot *pSlot; 141badc980aSdrh sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); 142badc980aSdrh sqlite3GlobalConfig.szScratch = sz; 143badc980aSdrh pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; 144badc980aSdrh n = sqlite3GlobalConfig.nScratch; 145badc980aSdrh mem0.pScratchFree = pSlot; 146badc980aSdrh mem0.nScratchFree = n; 147badc980aSdrh for(i=0; i<n-1; i++){ 148badc980aSdrh pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); 149badc980aSdrh pSlot = pSlot->pNext; 150badc980aSdrh } 151badc980aSdrh pSlot->pNext = 0; 152badc980aSdrh mem0.pScratchEnd = (void*)&pSlot[1]; 1539ac3fe97Sdrh }else{ 154badc980aSdrh mem0.pScratchEnd = 0; 155075c23afSdanielk1977 sqlite3GlobalConfig.pScratch = 0; 156075c23afSdanielk1977 sqlite3GlobalConfig.szScratch = 0; 157badc980aSdrh sqlite3GlobalConfig.nScratch = 0; 1589ac3fe97Sdrh } 15950d1b5f3Sdrh if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 16001c5c00cSdrh || sqlite3GlobalConfig.nPage<=0 ){ 161075c23afSdanielk1977 sqlite3GlobalConfig.pPage = 0; 162075c23afSdanielk1977 sqlite3GlobalConfig.szPage = 0; 1639ac3fe97Sdrh } 164592f0cb1Sdrh rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 165592f0cb1Sdrh if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); 166592f0cb1Sdrh return rc; 167fec00eabSdrh } 168fec00eabSdrh 169fec00eabSdrh /* 17050d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other 17150d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by 17250d1b5f3Sdrh ** sqlite3_soft_heap_limit(). 17350d1b5f3Sdrh */ 17450d1b5f3Sdrh int sqlite3HeapNearlyFull(void){ 17550d1b5f3Sdrh return mem0.nearlyFull; 17650d1b5f3Sdrh } 17750d1b5f3Sdrh 17850d1b5f3Sdrh /* 179fec00eabSdrh ** Deinitialize the memory allocation subsystem. 180fec00eabSdrh */ 181fec00eabSdrh void sqlite3MallocEnd(void){ 1820a549071Sdanielk1977 if( sqlite3GlobalConfig.m.xShutdown ){ 183075c23afSdanielk1977 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 1840a549071Sdanielk1977 } 1859ac3fe97Sdrh memset(&mem0, 0, sizeof(mem0)); 186fec00eabSdrh } 187fec00eabSdrh 188fec00eabSdrh /* 189fec00eabSdrh ** Return the amount of memory currently checked out. 190fec00eabSdrh */ 191fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){ 192df5e1a00Sdrh sqlite3_int64 res, mx; 193df5e1a00Sdrh sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0); 194c376a198Sdrh return res; 195fec00eabSdrh } 196fec00eabSdrh 197fec00eabSdrh /* 198fec00eabSdrh ** Return the maximum amount of memory that has ever been 199fec00eabSdrh ** checked out since either the beginning of this process 200fec00eabSdrh ** or since the most recent reset. 201fec00eabSdrh */ 202fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 203df5e1a00Sdrh sqlite3_int64 res, mx; 204df5e1a00Sdrh sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag); 205df5e1a00Sdrh return mx; 206fec00eabSdrh } 207fec00eabSdrh 208fec00eabSdrh /* 209*5fb72e5fSdrh ** Trigger the alarm 210*5fb72e5fSdrh */ 211*5fb72e5fSdrh static void sqlite3MallocAlarm(int nByte){ 212*5fb72e5fSdrh if( mem0.alarmThreshold<=0 ) return; 213*5fb72e5fSdrh sqlite3_mutex_leave(mem0.mutex); 214*5fb72e5fSdrh sqlite3_release_memory(nByte); 215*5fb72e5fSdrh sqlite3_mutex_enter(mem0.mutex); 216*5fb72e5fSdrh } 217*5fb72e5fSdrh 218*5fb72e5fSdrh /* 219f7141990Sdrh ** Do a memory allocation with statistics and alarms. Assume the 220f7141990Sdrh ** lock is already held. 221fec00eabSdrh */ 222f7141990Sdrh static int mallocWithAlarm(int n, void **pp){ 223fec00eabSdrh int nFull; 224f7141990Sdrh void *p; 225f7141990Sdrh assert( sqlite3_mutex_held(mem0.mutex) ); 226075c23afSdanielk1977 nFull = sqlite3GlobalConfig.m.xRoundup(n); 227f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 228*5fb72e5fSdrh if( mem0.alarmThreshold>0 ){ 229*5fb72e5fSdrh sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 230*5fb72e5fSdrh if( nUsed >= mem0.alarmThreshold - nFull ){ 231*5fb72e5fSdrh mem0.nearlyFull = 1; 232*5fb72e5fSdrh sqlite3MallocAlarm(nFull); 233*5fb72e5fSdrh }else{ 234*5fb72e5fSdrh mem0.nearlyFull = 0; 235*5fb72e5fSdrh } 236*5fb72e5fSdrh } 237075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(nFull); 23850d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 239*5fb72e5fSdrh if( p==0 && mem0.alarmThreshold>0 ){ 240*5fb72e5fSdrh sqlite3MallocAlarm(nFull); 241075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(nFull); 242fec00eabSdrh } 24350d1b5f3Sdrh #endif 244c702c7ccSdrh if( p ){ 245c702c7ccSdrh nFull = sqlite3MallocSize(p); 246af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); 247af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); 248c702c7ccSdrh } 249f7141990Sdrh *pp = p; 250f7141990Sdrh return nFull; 251fec00eabSdrh } 252f7141990Sdrh 253f7141990Sdrh /* 254f7141990Sdrh ** Allocate memory. This routine is like sqlite3_malloc() except that it 255f7141990Sdrh ** assumes the memory subsystem has already been initialized. 256f7141990Sdrh */ 257da4ca9d1Sdrh void *sqlite3Malloc(u64 n){ 258f7141990Sdrh void *p; 259da4ca9d1Sdrh if( n==0 || n>=0x7fffff00 ){ 260e08ed7e7Sdrh /* A memory allocation of a number of bytes which is near the maximum 261e08ed7e7Sdrh ** signed integer value might cause an integer overflow inside of the 262e08ed7e7Sdrh ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving 263e08ed7e7Sdrh ** 255 bytes of overhead. SQLite itself will never use anything near 264e08ed7e7Sdrh ** this amount. The only way to reach the limit is with sqlite3_malloc() */ 265f7141990Sdrh p = 0; 266075c23afSdanielk1977 }else if( sqlite3GlobalConfig.bMemstat ){ 267f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 2683329a63aSdrh mallocWithAlarm((int)n, &p); 269fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 270fec00eabSdrh }else{ 271da4ca9d1Sdrh p = sqlite3GlobalConfig.m.xMalloc((int)n); 272fec00eabSdrh } 2738da47419Sdrh assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ 274fec00eabSdrh return p; 275fec00eabSdrh } 276fec00eabSdrh 277fec00eabSdrh /* 278fec00eabSdrh ** This version of the memory allocation is for use by the application. 279fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the 280fec00eabSdrh ** allocation. 281fec00eabSdrh */ 282fec00eabSdrh void *sqlite3_malloc(int n){ 283fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 284fec00eabSdrh if( sqlite3_initialize() ) return 0; 285fec00eabSdrh #endif 286da4ca9d1Sdrh return n<=0 ? 0 : sqlite3Malloc(n); 287da4ca9d1Sdrh } 288da4ca9d1Sdrh void *sqlite3_malloc64(sqlite3_uint64 n){ 289da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT 290da4ca9d1Sdrh if( sqlite3_initialize() ) return 0; 291da4ca9d1Sdrh #endif 292fec00eabSdrh return sqlite3Malloc(n); 293fec00eabSdrh } 294fec00eabSdrh 295fec00eabSdrh /* 296e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from 297facf0307Sdrh ** xScratchMalloc(). We verify this constraint in the single-threaded 298facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation 299e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed. 300e5ae5735Sdrh */ 301e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 302facf0307Sdrh static int scratchAllocOut = 0; 303e5ae5735Sdrh #endif 304e5ae5735Sdrh 305e5ae5735Sdrh 306e5ae5735Sdrh /* 307e5ae5735Sdrh ** Allocate memory that is to be used and released right away. 308e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended 309e5ae5735Sdrh ** for situations where the memory might be held long-term. This 310e5ae5735Sdrh ** routine is intended to get memory to old large transient data 311e5ae5735Sdrh ** structures that would not normally fit on the stack of an 312e5ae5735Sdrh ** embedded processor. 313e5ae5735Sdrh */ 314facf0307Sdrh void *sqlite3ScratchMalloc(int n){ 315e5ae5735Sdrh void *p; 316e5ae5735Sdrh assert( n>0 ); 3179ac3fe97Sdrh 318badc980aSdrh sqlite3_mutex_enter(mem0.mutex); 3193ccd5bf8Sdrh sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 320badc980aSdrh if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ 321badc980aSdrh p = mem0.pScratchFree; 322badc980aSdrh mem0.pScratchFree = mem0.pScratchFree->pNext; 323badc980aSdrh mem0.nScratchFree--; 324af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); 325b0c6a888Sdan sqlite3_mutex_leave(mem0.mutex); 326badc980aSdrh }else{ 327b0c6a888Sdan sqlite3_mutex_leave(mem0.mutex); 3283ccd5bf8Sdrh p = sqlite3Malloc(n); 3293ccd5bf8Sdrh if( sqlite3GlobalConfig.bMemstat && p ){ 3303ccd5bf8Sdrh sqlite3_mutex_enter(mem0.mutex); 331af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); 3323ccd5bf8Sdrh sqlite3_mutex_leave(mem0.mutex); 333badc980aSdrh } 334badc980aSdrh sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); 335badc980aSdrh } 3361ff6e3abSdrh assert( sqlite3_mutex_notheld(mem0.mutex) ); 337b0c6a888Sdan 338badc980aSdrh 339badc980aSdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 340cbd55b03Sdrh /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch 341cbd55b03Sdrh ** buffers per thread. 342cbd55b03Sdrh ** 343cbd55b03Sdrh ** This can only be checked in single-threaded mode. 344cbd55b03Sdrh */ 345cbd55b03Sdrh assert( scratchAllocOut==0 ); 346badc980aSdrh if( p ) scratchAllocOut++; 347badc980aSdrh #endif 348badc980aSdrh 349badc980aSdrh return p; 350badc980aSdrh } 351badc980aSdrh void sqlite3ScratchFree(void *p){ 352badc980aSdrh if( p ){ 353badc980aSdrh 354e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 35537f99187Sdrh /* Verify that no more than two scratch allocation per thread 3569ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 3579ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 3589ac3fe97Sdrh ** would be much more complicated.) */ 359badc980aSdrh assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); 360badc980aSdrh scratchAllocOut--; 361e5ae5735Sdrh #endif 3629ac3fe97Sdrh 363badc980aSdrh if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){ 364badc980aSdrh /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ 365badc980aSdrh ScratchFreeslot *pSlot; 366badc980aSdrh pSlot = (ScratchFreeslot*)p; 367e5ae5735Sdrh sqlite3_mutex_enter(mem0.mutex); 368badc980aSdrh pSlot->pNext = mem0.pScratchFree; 369badc980aSdrh mem0.pScratchFree = pSlot; 370badc980aSdrh mem0.nScratchFree++; 371fcd71b60Sdrh assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); 372af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); 3739ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 374f7141990Sdrh }else{ 375badc980aSdrh /* Release memory back to the heap */ 376107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); 377d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); 378107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 379075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 380f7141990Sdrh int iSize = sqlite3MallocSize(p); 381f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 382af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); 383af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); 384af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); 385075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 386f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 387f7141990Sdrh }else{ 388075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 389f7141990Sdrh } 3909ac3fe97Sdrh } 391e5ae5735Sdrh } 392e5ae5735Sdrh } 393e5ae5735Sdrh 394e5ae5735Sdrh /* 395633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db 396633e6d57Sdrh */ 3974150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE 398633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){ 399b0e7704eSdrh return p>=db->lookaside.pStart && p<db->lookaside.pEnd; 400633e6d57Sdrh } 4014150ebf8Sdrh #else 4024150ebf8Sdrh #define isLookaside(A,B) 0 4034150ebf8Sdrh #endif 404633e6d57Sdrh 405633e6d57Sdrh /* 406fec00eabSdrh ** Return the size of a memory allocation previously obtained from 407fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc(). 408fec00eabSdrh */ 409fec00eabSdrh int sqlite3MallocSize(void *p){ 410107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 411075c23afSdanielk1977 return sqlite3GlobalConfig.m.xSize(p); 412fec00eabSdrh } 413633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){ 414054bbabcSdrh if( db==0 || !isLookaside(db,p) ){ 415054bbabcSdrh #if SQLITE_DEBUG 41617bcb102Sdrh if( db==0 ){ 417d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 418d231aa3aSdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 419633e6d57Sdrh }else{ 420d231aa3aSdrh assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 421d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 422633e6d57Sdrh } 423054bbabcSdrh #endif 424054bbabcSdrh return sqlite3GlobalConfig.m.xSize(p); 425054bbabcSdrh }else{ 426054bbabcSdrh assert( sqlite3_mutex_held(db->mutex) ); 427054bbabcSdrh return db->lookaside.sz; 428633e6d57Sdrh } 42917bcb102Sdrh } 430da4ca9d1Sdrh sqlite3_uint64 sqlite3_msize(void *p){ 431d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 432d231aa3aSdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 433da4ca9d1Sdrh return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); 434da4ca9d1Sdrh } 435fec00eabSdrh 436fec00eabSdrh /* 437fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc(). 438fec00eabSdrh */ 439fec00eabSdrh void sqlite3_free(void *p){ 44071a1a0f4Sdrh if( p==0 ) return; /* IMP: R-49053-54554 */ 441107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 442d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 443075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 444fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 445af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); 446af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); 447075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 448fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 449fec00eabSdrh }else{ 450075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 451fec00eabSdrh } 452fec00eabSdrh } 453fec00eabSdrh 454fec00eabSdrh /* 455b4586f12Sdrh ** Add the size of memory allocation "p" to the count in 456b4586f12Sdrh ** *db->pnBytesFreed. 457b4586f12Sdrh */ 458b4586f12Sdrh static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ 459b4586f12Sdrh *db->pnBytesFreed += sqlite3DbMallocSize(db,p); 460b4586f12Sdrh } 461b4586f12Sdrh 462b4586f12Sdrh /* 463633e6d57Sdrh ** Free memory that might be associated with a particular database 464633e6d57Sdrh ** connection. 465633e6d57Sdrh */ 466633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){ 4677047e25cSdrh assert( db==0 || sqlite3_mutex_held(db->mutex) ); 4689ccd8659Sdrh if( p==0 ) return; 469174b9a16Sdrh if( db ){ 470174b9a16Sdrh if( db->pnBytesFreed ){ 471b4586f12Sdrh measureAllocationSize(db, p); 472174b9a16Sdrh return; 473d46def77Sdan } 474633e6d57Sdrh if( isLookaside(db, p) ){ 475633e6d57Sdrh LookasideSlot *pBuf = (LookasideSlot*)p; 4763608f177Sdrh #if SQLITE_DEBUG 4773608f177Sdrh /* Trash all content in the buffer being freed */ 4783608f177Sdrh memset(p, 0xaa, db->lookaside.sz); 4793608f177Sdrh #endif 480633e6d57Sdrh pBuf->pNext = db->lookaside.pFree; 481633e6d57Sdrh db->lookaside.pFree = pBuf; 482633e6d57Sdrh db->lookaside.nOut--; 483174b9a16Sdrh return; 484174b9a16Sdrh } 485174b9a16Sdrh } 486d231aa3aSdrh assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 487d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 488174b9a16Sdrh assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 489107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 490633e6d57Sdrh sqlite3_free(p); 491633e6d57Sdrh } 492633e6d57Sdrh 493633e6d57Sdrh /* 494fec00eabSdrh ** Change the size of an existing memory allocation 495fec00eabSdrh */ 496da4ca9d1Sdrh void *sqlite3Realloc(void *pOld, u64 nBytes){ 497ca591febSshaneh int nOld, nNew, nDiff; 498fec00eabSdrh void *pNew; 499d231aa3aSdrh assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); 500d425864dSmistachkin assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); 501fec00eabSdrh if( pOld==0 ){ 5028da47419Sdrh return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ 503fec00eabSdrh } 504da4ca9d1Sdrh if( nBytes==0 ){ 5058da47419Sdrh sqlite3_free(pOld); /* IMP: R-26507-47431 */ 506fec00eabSdrh return 0; 507fec00eabSdrh } 508b6063cf8Sdrh if( nBytes>=0x7fffff00 ){ 509b6063cf8Sdrh /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ 510b6063cf8Sdrh return 0; 511b6063cf8Sdrh } 512fec00eabSdrh nOld = sqlite3MallocSize(pOld); 5139f129f46Sdrh /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second 5149f129f46Sdrh ** argument to xRealloc is always a value returned by a prior call to 5159f129f46Sdrh ** xRoundup. */ 516da4ca9d1Sdrh nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); 517fec00eabSdrh if( nOld==nNew ){ 518fec00eabSdrh pNew = pOld; 5197c6791c8Sdrh }else if( sqlite3GlobalConfig.bMemstat ){ 5207c6791c8Sdrh sqlite3_mutex_enter(mem0.mutex); 5213329a63aSdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); 5228e1bb041Sdrh nDiff = nNew - nOld; 523*5fb72e5fSdrh if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 524*5fb72e5fSdrh mem0.alarmThreshold-nDiff ){ 525*5fb72e5fSdrh sqlite3MallocAlarm(nDiff); 526*5fb72e5fSdrh } 527075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 528*5fb72e5fSdrh if( pNew==0 && mem0.alarmThreshold>0 ){ 529*5fb72e5fSdrh sqlite3MallocAlarm((int)nBytes); 530075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 531fec00eabSdrh } 532fec00eabSdrh if( pNew ){ 533c702c7ccSdrh nNew = sqlite3MallocSize(pNew); 534af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 535fec00eabSdrh } 536fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 537fec00eabSdrh }else{ 5387c6791c8Sdrh pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 539fec00eabSdrh } 5408da47419Sdrh assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ 541fec00eabSdrh return pNew; 542fec00eabSdrh } 543fec00eabSdrh 544fec00eabSdrh /* 545fec00eabSdrh ** The public interface to sqlite3Realloc. Make sure that the memory 546fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc. 547fec00eabSdrh */ 548fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){ 549fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 550fec00eabSdrh if( sqlite3_initialize() ) return 0; 551fec00eabSdrh #endif 5528da47419Sdrh if( n<0 ) n = 0; /* IMP: R-26507-47431 */ 553da4ca9d1Sdrh return sqlite3Realloc(pOld, n); 554da4ca9d1Sdrh } 555da4ca9d1Sdrh void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ 556da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT 557da4ca9d1Sdrh if( sqlite3_initialize() ) return 0; 558da4ca9d1Sdrh #endif 559fec00eabSdrh return sqlite3Realloc(pOld, n); 560fec00eabSdrh } 561fec00eabSdrh 562a3152895Sdrh 563a3152895Sdrh /* 56417435752Sdrh ** Allocate and zero memory. 565a3152895Sdrh */ 566da4ca9d1Sdrh void *sqlite3MallocZero(u64 n){ 567fec00eabSdrh void *p = sqlite3Malloc(n); 568a3152895Sdrh if( p ){ 56920f3df04Sdrh memset(p, 0, (size_t)n); 570a3152895Sdrh } 571a3152895Sdrh return p; 572a3152895Sdrh } 57317435752Sdrh 57417435752Sdrh /* 57517435752Sdrh ** Allocate and zero memory. If the allocation fails, make 57617435752Sdrh ** the mallocFailed flag in the connection pointer. 57717435752Sdrh */ 578da4ca9d1Sdrh void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ 579a1644fd8Sdanielk1977 void *p = sqlite3DbMallocRaw(db, n); 58017435752Sdrh if( p ){ 58120f3df04Sdrh memset(p, 0, (size_t)n); 58217435752Sdrh } 58317435752Sdrh return p; 58417435752Sdrh } 58517435752Sdrh 58617435752Sdrh /* 58717435752Sdrh ** Allocate and zero memory. If the allocation fails, make 58817435752Sdrh ** the mallocFailed flag in the connection pointer. 589ddecae79Sdrh ** 590ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc 591ddecae79Sdrh ** failure on the same database connection) then always return 0. 592ddecae79Sdrh ** Hence for a particular database connection, once malloc starts 593ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset. 594ddecae79Sdrh ** This is an important assumption. There are many places in the 595ddecae79Sdrh ** code that do things like this: 596ddecae79Sdrh ** 597ddecae79Sdrh ** int *a = (int*)sqlite3DbMallocRaw(db, 100); 598ddecae79Sdrh ** int *b = (int*)sqlite3DbMallocRaw(db, 200); 599ddecae79Sdrh ** if( b ) a[10] = 9; 600ddecae79Sdrh ** 601ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed 602ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too. 60317435752Sdrh */ 604da4ca9d1Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ 605633e6d57Sdrh void *p; 606d9da78a2Sdrh assert( db==0 || sqlite3_mutex_held(db->mutex) ); 607ccd4ad3eSdan assert( db==0 || db->pnBytesFreed==0 ); 6084150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE 609633e6d57Sdrh if( db ){ 610633e6d57Sdrh LookasideSlot *pBuf; 611633e6d57Sdrh if( db->mallocFailed ){ 612633e6d57Sdrh return 0; 613633e6d57Sdrh } 6140b12e7f8Sdrh if( db->lookaside.bEnabled ){ 6150b12e7f8Sdrh if( n>db->lookaside.sz ){ 6160b12e7f8Sdrh db->lookaside.anStat[1]++; 6170b12e7f8Sdrh }else if( (pBuf = db->lookaside.pFree)==0 ){ 6180b12e7f8Sdrh db->lookaside.anStat[2]++; 6190b12e7f8Sdrh }else{ 620633e6d57Sdrh db->lookaside.pFree = pBuf->pNext; 621633e6d57Sdrh db->lookaside.nOut++; 6220b12e7f8Sdrh db->lookaside.anStat[0]++; 623633e6d57Sdrh if( db->lookaside.nOut>db->lookaside.mxOut ){ 624633e6d57Sdrh db->lookaside.mxOut = db->lookaside.nOut; 625633e6d57Sdrh } 626633e6d57Sdrh return (void*)pBuf; 627633e6d57Sdrh } 628633e6d57Sdrh } 6290b12e7f8Sdrh } 630ddecae79Sdrh #else 631ddecae79Sdrh if( db && db->mallocFailed ){ 632ddecae79Sdrh return 0; 633ddecae79Sdrh } 6344150ebf8Sdrh #endif 635fec00eabSdrh p = sqlite3Malloc(n); 636f3a65f7eSdrh if( !p && db ){ 63717435752Sdrh db->mallocFailed = 1; 63817435752Sdrh } 639d231aa3aSdrh sqlite3MemdebugSetType(p, 640d231aa3aSdrh (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); 64117435752Sdrh return p; 64217435752Sdrh } 64317435752Sdrh 64426783a58Sdanielk1977 /* 64526783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the 64626783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object. 64726783a58Sdanielk1977 */ 648da4ca9d1Sdrh void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ 649a1644fd8Sdanielk1977 void *pNew = 0; 650d9da78a2Sdrh assert( db!=0 ); 6517047e25cSdrh assert( sqlite3_mutex_held(db->mutex) ); 652a1644fd8Sdanielk1977 if( db->mallocFailed==0 ){ 653633e6d57Sdrh if( p==0 ){ 654633e6d57Sdrh return sqlite3DbMallocRaw(db, n); 655633e6d57Sdrh } 656633e6d57Sdrh if( isLookaside(db, p) ){ 657633e6d57Sdrh if( n<=db->lookaside.sz ){ 658633e6d57Sdrh return p; 659633e6d57Sdrh } 660633e6d57Sdrh pNew = sqlite3DbMallocRaw(db, n); 661633e6d57Sdrh if( pNew ){ 662633e6d57Sdrh memcpy(pNew, p, db->lookaside.sz); 663633e6d57Sdrh sqlite3DbFree(db, p); 664633e6d57Sdrh } 665633e6d57Sdrh }else{ 666d231aa3aSdrh assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 667d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 668107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 6693329a63aSdrh pNew = sqlite3_realloc64(p, n); 670a1644fd8Sdanielk1977 if( !pNew ){ 671a1644fd8Sdanielk1977 db->mallocFailed = 1; 672a1644fd8Sdanielk1977 } 673d231aa3aSdrh sqlite3MemdebugSetType(pNew, 674174b9a16Sdrh (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 675a1644fd8Sdanielk1977 } 676633e6d57Sdrh } 677a1644fd8Sdanielk1977 return pNew; 678a1644fd8Sdanielk1977 } 679a1644fd8Sdanielk1977 68017435752Sdrh /* 68117435752Sdrh ** Attempt to reallocate p. If the reallocation fails, then free p 68217435752Sdrh ** and set the mallocFailed flag in the database connection. 68317435752Sdrh */ 684da4ca9d1Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){ 685a3152895Sdrh void *pNew; 686a1644fd8Sdanielk1977 pNew = sqlite3DbRealloc(db, p, n); 687a3152895Sdrh if( !pNew ){ 688633e6d57Sdrh sqlite3DbFree(db, p); 689a3152895Sdrh } 690a3152895Sdrh return pNew; 691a3152895Sdrh } 692a3152895Sdrh 693a3152895Sdrh /* 694a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These 695a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 696a3152895Sdrh ** is because when memory debugging is turned on, these two functions are 697a3152895Sdrh ** called via macros that record the current file and line number in the 698a3152895Sdrh ** ThreadData structure. 699a3152895Sdrh */ 700633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 701a3152895Sdrh char *zNew; 702633e6d57Sdrh size_t n; 703633e6d57Sdrh if( z==0 ){ 704633e6d57Sdrh return 0; 705a3152895Sdrh } 706dee0e404Sdrh n = sqlite3Strlen30(z) + 1; 707633e6d57Sdrh assert( (n&0x7fffffff)==n ); 708633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, (int)n); 709a3152895Sdrh if( zNew ){ 710a3152895Sdrh memcpy(zNew, z, n); 7111e536953Sdanielk1977 } 7121e536953Sdanielk1977 return zNew; 7131e536953Sdanielk1977 } 714da4ca9d1Sdrh char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ 715633e6d57Sdrh char *zNew; 716633e6d57Sdrh if( z==0 ){ 717633e6d57Sdrh return 0; 718633e6d57Sdrh } 719633e6d57Sdrh assert( (n&0x7fffffff)==n ); 720633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, n+1); 721633e6d57Sdrh if( zNew ){ 72220f3df04Sdrh memcpy(zNew, z, (size_t)n); 723633e6d57Sdrh zNew[n] = 0; 7241e536953Sdanielk1977 } 7251e536953Sdanielk1977 return zNew; 7261e536953Sdanielk1977 } 7271e536953Sdanielk1977 728a3152895Sdrh /* 72922c17b8bSdrh ** Free any prior content in *pz and replace it with a copy of zNew. 730a3152895Sdrh */ 73122c17b8bSdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){ 732633e6d57Sdrh sqlite3DbFree(db, *pz); 73322c17b8bSdrh *pz = sqlite3DbStrDup(db, zNew); 734a3152895Sdrh } 735a3152895Sdrh 736b50c65d5Sdrh /* 737b50c65d5Sdrh ** Take actions at the end of an API call to indicate an OOM error 738b50c65d5Sdrh */ 739b50c65d5Sdrh static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ 740b50c65d5Sdrh db->mallocFailed = 0; 741b50c65d5Sdrh sqlite3Error(db, SQLITE_NOMEM); 742b50c65d5Sdrh return SQLITE_NOMEM; 743b50c65d5Sdrh } 744a3152895Sdrh 745a3152895Sdrh /* 746a3152895Sdrh ** This function must be called before exiting any API function (i.e. 74717435752Sdrh ** returning control to the user) that has called sqlite3_malloc or 74817435752Sdrh ** sqlite3_realloc. 749a3152895Sdrh ** 750a3152895Sdrh ** The returned value is normally a copy of the second argument to this 751be217793Sshane ** function. However, if a malloc() failure has occurred since the previous 752a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead. 753a3152895Sdrh ** 754597d2b64Sdrh ** If an OOM as occurred, then the connection error-code (the value 755597d2b64Sdrh ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. 756a3152895Sdrh */ 757a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){ 758597d2b64Sdrh /* If the db handle must hold the connection handle mutex here. 759597d2b64Sdrh ** Otherwise the read (and possible write) of db->mallocFailed 760a1644fd8Sdanielk1977 ** is unsafe, as is the call to sqlite3Error(). 761a1644fd8Sdanielk1977 */ 762597d2b64Sdrh assert( db!=0 ); 763597d2b64Sdrh assert( sqlite3_mutex_held(db->mutex) ); 764b50c65d5Sdrh if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ 765b50c65d5Sdrh return apiOomError(db); 766a3152895Sdrh } 767b50c65d5Sdrh return rc & db->errMask; 768a3152895Sdrh } 769