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 /* 19b21c8cd4Sdrh ** This routine runs when the memory allocator sees that the 20b21c8cd4Sdrh ** total memory allocation is about to exceed the soft heap 21b21c8cd4Sdrh ** limit. 22b21c8cd4Sdrh */ 23b21c8cd4Sdrh static void softHeapLimitEnforcer( 24b21c8cd4Sdrh void *NotUsed, 2562c14b34Sdanielk1977 sqlite3_int64 NotUsed2, 26153c62c4Sdrh int allocSize 27b21c8cd4Sdrh ){ 2862c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 29b21c8cd4Sdrh sqlite3_release_memory(allocSize); 30b21c8cd4Sdrh } 31b21c8cd4Sdrh 32b21c8cd4Sdrh /* 338468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or 348468024dSdanielk1977 ** negative value indicates no limit. 35a3152895Sdrh */ 36a3152895Sdrh void sqlite3_soft_heap_limit(int n){ 37b21c8cd4Sdrh sqlite3_uint64 iLimit; 38b21c8cd4Sdrh int overage; 39b21c8cd4Sdrh if( n<0 ){ 40b21c8cd4Sdrh iLimit = 0; 41b21c8cd4Sdrh }else{ 42b21c8cd4Sdrh iLimit = n; 43a3152895Sdrh } 449ac06509Sdrh #ifndef SQLITE_OMIT_AUTOINIT 459ac3fe97Sdrh sqlite3_initialize(); 469ac06509Sdrh #endif 47b21c8cd4Sdrh if( iLimit>0 ){ 484a27a286Sshane sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit); 49b21c8cd4Sdrh }else{ 504a27a286Sshane sqlite3MemoryAlarm(0, 0, 0); 51b21c8cd4Sdrh } 521bd10f8aSdrh overage = (int)(sqlite3_memory_used() - (i64)n); 53b21c8cd4Sdrh if( overage>0 ){ 54b21c8cd4Sdrh sqlite3_release_memory(overage); 55b21c8cd4Sdrh } 56a3152895Sdrh } 57a3152895Sdrh 58a3152895Sdrh /* 598468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently 608468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to 618468024dSdanielk1977 ** cache database pages that are not currently in use. 62a3152895Sdrh */ 63a3152895Sdrh int sqlite3_release_memory(int n){ 6486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 659f129f46Sdrh return sqlite3PcacheReleaseMemory(n); 661e536953Sdanielk1977 #else 679f129f46Sdrh /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine 689f129f46Sdrh ** is a no-op returning zero if SQLite is not compiled with 699f129f46Sdrh ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ 7062c14b34Sdanielk1977 UNUSED_PARAMETER(n); 719f129f46Sdrh return 0; 721e536953Sdanielk1977 #endif 73a3152895Sdrh } 74a3152895Sdrh 75fec00eabSdrh /* 76badc980aSdrh ** An instance of the following object records the location of 77badc980aSdrh ** each unused scratch buffer. 78badc980aSdrh */ 79badc980aSdrh typedef struct ScratchFreeslot { 80badc980aSdrh struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ 81badc980aSdrh } ScratchFreeslot; 82badc980aSdrh 83badc980aSdrh /* 84fec00eabSdrh ** State information local to the memory allocation subsystem. 85fec00eabSdrh */ 865c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global { 87fec00eabSdrh sqlite3_mutex *mutex; /* Mutex to serialize access */ 88fec00eabSdrh 89fec00eabSdrh /* 90fec00eabSdrh ** The alarm callback and its arguments. The mem0.mutex lock will 91fec00eabSdrh ** be held while the callback is running. Recursive calls into 92fec00eabSdrh ** the memory subsystem are allowed, but no new callbacks will be 93e64ca7baSdrh ** issued. 94fec00eabSdrh */ 95fec00eabSdrh sqlite3_int64 alarmThreshold; 96fec00eabSdrh void (*alarmCallback)(void*, sqlite3_int64,int); 97fec00eabSdrh void *alarmArg; 98fec00eabSdrh 99fec00eabSdrh /* 100badc980aSdrh ** Pointers to the end of sqlite3GlobalConfig.pScratch memory 101badc980aSdrh ** (so that a range test can be used to determine if an allocation 102badc980aSdrh ** being freed came from pScratch) and a pointer to the list of 103badc980aSdrh ** unused scratch allocations. 1049ac3fe97Sdrh */ 105badc980aSdrh void *pScratchEnd; 106badc980aSdrh ScratchFreeslot *pScratchFree; 107badc980aSdrh u32 nScratchFree; 10850d1b5f3Sdrh 10950d1b5f3Sdrh /* 11050d1b5f3Sdrh ** True if heap is nearly "full" where "full" is defined by the 11150d1b5f3Sdrh ** sqlite3_soft_heap_limit() setting. 11250d1b5f3Sdrh */ 11350d1b5f3Sdrh int nearlyFull; 114badc980aSdrh } mem0 = { 0, 0, 0, 0, 0, 0, 0 }; 1155c8f8587Sdanielk1977 1165c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0) 117fec00eabSdrh 118fec00eabSdrh /* 119fec00eabSdrh ** Initialize the memory allocation subsystem. 120fec00eabSdrh */ 121fec00eabSdrh int sqlite3MallocInit(void){ 122075c23afSdanielk1977 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 123fec00eabSdrh sqlite3MemSetDefault(); 124fec00eabSdrh } 125fec00eabSdrh memset(&mem0, 0, sizeof(mem0)); 126075c23afSdanielk1977 if( sqlite3GlobalConfig.bCoreMutex ){ 12759f8c08eSdanielk1977 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 128fec00eabSdrh } 129075c23afSdanielk1977 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 130*7ff2719eSdrh && sqlite3GlobalConfig.nScratch>0 ){ 131badc980aSdrh int i, n, sz; 132badc980aSdrh ScratchFreeslot *pSlot; 133badc980aSdrh sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); 134badc980aSdrh sqlite3GlobalConfig.szScratch = sz; 135badc980aSdrh pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; 136badc980aSdrh n = sqlite3GlobalConfig.nScratch; 137badc980aSdrh mem0.pScratchFree = pSlot; 138badc980aSdrh mem0.nScratchFree = n; 139badc980aSdrh for(i=0; i<n-1; i++){ 140badc980aSdrh pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); 141badc980aSdrh pSlot = pSlot->pNext; 142badc980aSdrh } 143badc980aSdrh pSlot->pNext = 0; 144badc980aSdrh mem0.pScratchEnd = (void*)&pSlot[1]; 1459ac3fe97Sdrh }else{ 146badc980aSdrh mem0.pScratchEnd = 0; 147075c23afSdanielk1977 sqlite3GlobalConfig.pScratch = 0; 148075c23afSdanielk1977 sqlite3GlobalConfig.szScratch = 0; 149badc980aSdrh sqlite3GlobalConfig.nScratch = 0; 1509ac3fe97Sdrh } 15150d1b5f3Sdrh if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 15250d1b5f3Sdrh || sqlite3GlobalConfig.nPage<1 ){ 153075c23afSdanielk1977 sqlite3GlobalConfig.pPage = 0; 154075c23afSdanielk1977 sqlite3GlobalConfig.szPage = 0; 15550d1b5f3Sdrh sqlite3GlobalConfig.nPage = 0; 1569ac3fe97Sdrh } 157075c23afSdanielk1977 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 158fec00eabSdrh } 159fec00eabSdrh 160fec00eabSdrh /* 16150d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other 16250d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by 16350d1b5f3Sdrh ** sqlite3_soft_heap_limit(). 16450d1b5f3Sdrh */ 16550d1b5f3Sdrh int sqlite3HeapNearlyFull(void){ 16650d1b5f3Sdrh return mem0.nearlyFull; 16750d1b5f3Sdrh } 16850d1b5f3Sdrh 16950d1b5f3Sdrh /* 170fec00eabSdrh ** Deinitialize the memory allocation subsystem. 171fec00eabSdrh */ 172fec00eabSdrh void sqlite3MallocEnd(void){ 1730a549071Sdanielk1977 if( sqlite3GlobalConfig.m.xShutdown ){ 174075c23afSdanielk1977 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 1750a549071Sdanielk1977 } 1769ac3fe97Sdrh memset(&mem0, 0, sizeof(mem0)); 177fec00eabSdrh } 178fec00eabSdrh 179fec00eabSdrh /* 180fec00eabSdrh ** Return the amount of memory currently checked out. 181fec00eabSdrh */ 182fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){ 183f7141990Sdrh int n, mx; 184c376a198Sdrh sqlite3_int64 res; 185f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); 186c376a198Sdrh res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ 187c376a198Sdrh return res; 188fec00eabSdrh } 189fec00eabSdrh 190fec00eabSdrh /* 191fec00eabSdrh ** Return the maximum amount of memory that has ever been 192fec00eabSdrh ** checked out since either the beginning of this process 193fec00eabSdrh ** or since the most recent reset. 194fec00eabSdrh */ 195fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 196f7141990Sdrh int n, mx; 197c376a198Sdrh sqlite3_int64 res; 198f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); 1997986a71aSdrh res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ 200c376a198Sdrh return res; 201fec00eabSdrh } 202fec00eabSdrh 203fec00eabSdrh /* 204fec00eabSdrh ** Change the alarm callback 205fec00eabSdrh */ 2064a27a286Sshane int sqlite3MemoryAlarm( 207fec00eabSdrh void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 208fec00eabSdrh void *pArg, 209fec00eabSdrh sqlite3_int64 iThreshold 210fec00eabSdrh ){ 21150d1b5f3Sdrh int nUsed; 212fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 213fec00eabSdrh mem0.alarmCallback = xCallback; 214fec00eabSdrh mem0.alarmArg = pArg; 215fec00eabSdrh mem0.alarmThreshold = iThreshold; 21650d1b5f3Sdrh nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 21750d1b5f3Sdrh mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed); 218fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 219fec00eabSdrh return SQLITE_OK; 220fec00eabSdrh } 221fec00eabSdrh 222eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 223fec00eabSdrh /* 2244a27a286Sshane ** Deprecated external interface. Internal/core SQLite code 2254a27a286Sshane ** should call sqlite3MemoryAlarm. 2264a27a286Sshane */ 2274a27a286Sshane int sqlite3_memory_alarm( 2284a27a286Sshane void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 2294a27a286Sshane void *pArg, 2304a27a286Sshane sqlite3_int64 iThreshold 2314a27a286Sshane ){ 2324a27a286Sshane return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); 2334a27a286Sshane } 234eec556d3Sshane #endif 2354a27a286Sshane 2364a27a286Sshane /* 237fec00eabSdrh ** Trigger the alarm 238fec00eabSdrh */ 239fec00eabSdrh static void sqlite3MallocAlarm(int nByte){ 240fec00eabSdrh void (*xCallback)(void*,sqlite3_int64,int); 241fec00eabSdrh sqlite3_int64 nowUsed; 242fec00eabSdrh void *pArg; 243e64ca7baSdrh if( mem0.alarmCallback==0 ) return; 244fec00eabSdrh xCallback = mem0.alarmCallback; 245f7141990Sdrh nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 246fec00eabSdrh pArg = mem0.alarmArg; 247e64ca7baSdrh mem0.alarmCallback = 0; 248fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 249fec00eabSdrh xCallback(pArg, nowUsed, nByte); 250fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 251e64ca7baSdrh mem0.alarmCallback = xCallback; 252e64ca7baSdrh mem0.alarmArg = pArg; 253fec00eabSdrh } 254fec00eabSdrh 255fec00eabSdrh /* 256f7141990Sdrh ** Do a memory allocation with statistics and alarms. Assume the 257f7141990Sdrh ** lock is already held. 258fec00eabSdrh */ 259f7141990Sdrh static int mallocWithAlarm(int n, void **pp){ 260fec00eabSdrh int nFull; 261f7141990Sdrh void *p; 262f7141990Sdrh assert( sqlite3_mutex_held(mem0.mutex) ); 263075c23afSdanielk1977 nFull = sqlite3GlobalConfig.m.xRoundup(n); 264f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 265f7141990Sdrh if( mem0.alarmCallback!=0 ){ 266f7141990Sdrh int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 267f7141990Sdrh if( nUsed+nFull >= mem0.alarmThreshold ){ 26850d1b5f3Sdrh mem0.nearlyFull = 1; 269fec00eabSdrh sqlite3MallocAlarm(nFull); 27050d1b5f3Sdrh }else{ 27150d1b5f3Sdrh mem0.nearlyFull = 0; 272fec00eabSdrh } 273f7141990Sdrh } 274075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(nFull); 27550d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 276d09414cdSdanielk1977 if( p==0 && mem0.alarmCallback ){ 277fec00eabSdrh sqlite3MallocAlarm(nFull); 278075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(nFull); 279fec00eabSdrh } 28050d1b5f3Sdrh #endif 281c702c7ccSdrh if( p ){ 282c702c7ccSdrh nFull = sqlite3MallocSize(p); 283c702c7ccSdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 284eafc43b1Sdrh sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); 285c702c7ccSdrh } 286f7141990Sdrh *pp = p; 287f7141990Sdrh return nFull; 288fec00eabSdrh } 289f7141990Sdrh 290f7141990Sdrh /* 291f7141990Sdrh ** Allocate memory. This routine is like sqlite3_malloc() except that it 292f7141990Sdrh ** assumes the memory subsystem has already been initialized. 293f7141990Sdrh */ 294f7141990Sdrh void *sqlite3Malloc(int n){ 295f7141990Sdrh void *p; 296e08ed7e7Sdrh if( n<=0 || n>=0x7fffff00 ){ 297e08ed7e7Sdrh /* A memory allocation of a number of bytes which is near the maximum 298e08ed7e7Sdrh ** signed integer value might cause an integer overflow inside of the 299e08ed7e7Sdrh ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving 300e08ed7e7Sdrh ** 255 bytes of overhead. SQLite itself will never use anything near 301e08ed7e7Sdrh ** this amount. The only way to reach the limit is with sqlite3_malloc() */ 302f7141990Sdrh p = 0; 303075c23afSdanielk1977 }else if( sqlite3GlobalConfig.bMemstat ){ 304f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 305f7141990Sdrh mallocWithAlarm(n, &p); 306fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 307fec00eabSdrh }else{ 308075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(n); 309fec00eabSdrh } 310fec00eabSdrh return p; 311fec00eabSdrh } 312fec00eabSdrh 313fec00eabSdrh /* 314fec00eabSdrh ** This version of the memory allocation is for use by the application. 315fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the 316fec00eabSdrh ** allocation. 317fec00eabSdrh */ 318fec00eabSdrh void *sqlite3_malloc(int n){ 319fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 320fec00eabSdrh if( sqlite3_initialize() ) return 0; 321fec00eabSdrh #endif 322fec00eabSdrh return sqlite3Malloc(n); 323fec00eabSdrh } 324fec00eabSdrh 325fec00eabSdrh /* 326e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from 327facf0307Sdrh ** xScratchMalloc(). We verify this constraint in the single-threaded 328facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation 329e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed. 330e5ae5735Sdrh */ 331e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 332facf0307Sdrh static int scratchAllocOut = 0; 333e5ae5735Sdrh #endif 334e5ae5735Sdrh 335e5ae5735Sdrh 336e5ae5735Sdrh /* 337e5ae5735Sdrh ** Allocate memory that is to be used and released right away. 338e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended 339e5ae5735Sdrh ** for situations where the memory might be held long-term. This 340e5ae5735Sdrh ** routine is intended to get memory to old large transient data 341e5ae5735Sdrh ** structures that would not normally fit on the stack of an 342e5ae5735Sdrh ** embedded processor. 343e5ae5735Sdrh */ 344facf0307Sdrh void *sqlite3ScratchMalloc(int n){ 345e5ae5735Sdrh void *p; 346e5ae5735Sdrh assert( n>0 ); 3479ac3fe97Sdrh 348badc980aSdrh sqlite3_mutex_enter(mem0.mutex); 349badc980aSdrh if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ 350badc980aSdrh p = mem0.pScratchFree; 351badc980aSdrh mem0.pScratchFree = mem0.pScratchFree->pNext; 352badc980aSdrh mem0.nScratchFree--; 353badc980aSdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 354badc980aSdrh sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 355b0c6a888Sdan sqlite3_mutex_leave(mem0.mutex); 356badc980aSdrh }else{ 357badc980aSdrh if( sqlite3GlobalConfig.bMemstat ){ 358badc980aSdrh sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 359badc980aSdrh n = mallocWithAlarm(n, &p); 360badc980aSdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); 361b0c6a888Sdan sqlite3_mutex_leave(mem0.mutex); 362badc980aSdrh }else{ 363b0c6a888Sdan sqlite3_mutex_leave(mem0.mutex); 364badc980aSdrh p = sqlite3GlobalConfig.m.xMalloc(n); 365badc980aSdrh } 366badc980aSdrh sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); 367badc980aSdrh } 3681ff6e3abSdrh assert( sqlite3_mutex_notheld(mem0.mutex) ); 369b0c6a888Sdan 370badc980aSdrh 371badc980aSdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 372badc980aSdrh /* Verify that no more than two scratch allocations per thread 373badc980aSdrh ** are outstanding at one time. (This is only checked in the 374badc980aSdrh ** single-threaded case since checking in the multi-threaded case 375badc980aSdrh ** would be much more complicated.) */ 376badc980aSdrh assert( scratchAllocOut<=1 ); 377badc980aSdrh if( p ) scratchAllocOut++; 378badc980aSdrh #endif 379badc980aSdrh 380badc980aSdrh return p; 381badc980aSdrh } 382badc980aSdrh void sqlite3ScratchFree(void *p){ 383badc980aSdrh if( p ){ 384badc980aSdrh 385e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 38637f99187Sdrh /* Verify that no more than two scratch allocation per thread 3879ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 3889ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 3899ac3fe97Sdrh ** would be much more complicated.) */ 390badc980aSdrh assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); 391badc980aSdrh scratchAllocOut--; 392e5ae5735Sdrh #endif 3939ac3fe97Sdrh 394badc980aSdrh if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){ 395badc980aSdrh /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ 396badc980aSdrh ScratchFreeslot *pSlot; 397badc980aSdrh pSlot = (ScratchFreeslot*)p; 398e5ae5735Sdrh sqlite3_mutex_enter(mem0.mutex); 399badc980aSdrh pSlot->pNext = mem0.pScratchFree; 400badc980aSdrh mem0.pScratchFree = pSlot; 401badc980aSdrh mem0.nScratchFree++; 402badc980aSdrh assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch ); 403badc980aSdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 4049ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 405f7141990Sdrh }else{ 406badc980aSdrh /* Release memory back to the heap */ 407107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); 408174b9a16Sdrh assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); 409107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 410075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 411f7141990Sdrh int iSize = sqlite3MallocSize(p); 412f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 413f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 414f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 41581ba7d16Sdrh sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); 416075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 417f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 418f7141990Sdrh }else{ 419075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 420f7141990Sdrh } 4219ac3fe97Sdrh } 422e5ae5735Sdrh } 423e5ae5735Sdrh } 424e5ae5735Sdrh 425e5ae5735Sdrh /* 426633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db 427633e6d57Sdrh */ 4284150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE 429633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){ 430174b9a16Sdrh return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; 431633e6d57Sdrh } 4324150ebf8Sdrh #else 4334150ebf8Sdrh #define isLookaside(A,B) 0 4344150ebf8Sdrh #endif 435633e6d57Sdrh 436633e6d57Sdrh /* 437fec00eabSdrh ** Return the size of a memory allocation previously obtained from 438fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc(). 439fec00eabSdrh */ 440fec00eabSdrh int sqlite3MallocSize(void *p){ 441107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 442174b9a16Sdrh assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); 443075c23afSdanielk1977 return sqlite3GlobalConfig.m.xSize(p); 444fec00eabSdrh } 445633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){ 4467047e25cSdrh assert( db==0 || sqlite3_mutex_held(db->mutex) ); 447174b9a16Sdrh if( db && isLookaside(db, p) ){ 448633e6d57Sdrh return db->lookaside.sz; 449633e6d57Sdrh }else{ 450174b9a16Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); 451174b9a16Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); 452174b9a16Sdrh assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 453075c23afSdanielk1977 return sqlite3GlobalConfig.m.xSize(p); 454633e6d57Sdrh } 455633e6d57Sdrh } 456fec00eabSdrh 457fec00eabSdrh /* 458fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc(). 459fec00eabSdrh */ 460fec00eabSdrh void sqlite3_free(void *p){ 461fec00eabSdrh if( p==0 ) return; 462174b9a16Sdrh assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); 463107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 464075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 465fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 466f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 467eafc43b1Sdrh sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); 468075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 469fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 470fec00eabSdrh }else{ 471075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 472fec00eabSdrh } 473fec00eabSdrh } 474fec00eabSdrh 475fec00eabSdrh /* 476633e6d57Sdrh ** Free memory that might be associated with a particular database 477633e6d57Sdrh ** connection. 478633e6d57Sdrh */ 479633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){ 4807047e25cSdrh assert( db==0 || sqlite3_mutex_held(db->mutex) ); 481174b9a16Sdrh if( db ){ 482174b9a16Sdrh if( db->pnBytesFreed ){ 483174b9a16Sdrh *db->pnBytesFreed += sqlite3DbMallocSize(db, p); 484174b9a16Sdrh return; 485d46def77Sdan } 486633e6d57Sdrh if( isLookaside(db, p) ){ 487633e6d57Sdrh LookasideSlot *pBuf = (LookasideSlot*)p; 488633e6d57Sdrh pBuf->pNext = db->lookaside.pFree; 489633e6d57Sdrh db->lookaside.pFree = pBuf; 490633e6d57Sdrh db->lookaside.nOut--; 491174b9a16Sdrh return; 492174b9a16Sdrh } 493174b9a16Sdrh } 494174b9a16Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); 495174b9a16Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); 496174b9a16Sdrh assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 497107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 498633e6d57Sdrh sqlite3_free(p); 499633e6d57Sdrh } 500633e6d57Sdrh 501633e6d57Sdrh /* 502fec00eabSdrh ** Change the size of an existing memory allocation 503fec00eabSdrh */ 504fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){ 505fec00eabSdrh int nOld, nNew; 506fec00eabSdrh void *pNew; 507fec00eabSdrh if( pOld==0 ){ 508fec00eabSdrh return sqlite3Malloc(nBytes); 509fec00eabSdrh } 510b6063cf8Sdrh if( nBytes<=0 ){ 511fec00eabSdrh sqlite3_free(pOld); 512fec00eabSdrh return 0; 513fec00eabSdrh } 514b6063cf8Sdrh if( nBytes>=0x7fffff00 ){ 515b6063cf8Sdrh /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ 516b6063cf8Sdrh return 0; 517b6063cf8Sdrh } 518fec00eabSdrh nOld = sqlite3MallocSize(pOld); 5199f129f46Sdrh /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second 5209f129f46Sdrh ** argument to xRealloc is always a value returned by a prior call to 5219f129f46Sdrh ** xRoundup. */ 522075c23afSdanielk1977 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); 523fec00eabSdrh if( nOld==nNew ){ 524fec00eabSdrh pNew = pOld; 5257c6791c8Sdrh }else if( sqlite3GlobalConfig.bMemstat ){ 5267c6791c8Sdrh sqlite3_mutex_enter(mem0.mutex); 5277c6791c8Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); 528f7141990Sdrh if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 529f7141990Sdrh mem0.alarmThreshold ){ 530fec00eabSdrh sqlite3MallocAlarm(nNew-nOld); 531fec00eabSdrh } 532107b56e8Sdrh assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); 533174b9a16Sdrh assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); 534075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 535d09414cdSdanielk1977 if( pNew==0 && mem0.alarmCallback ){ 536fec00eabSdrh sqlite3MallocAlarm(nBytes); 537075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 538fec00eabSdrh } 539fec00eabSdrh if( pNew ){ 540c702c7ccSdrh nNew = sqlite3MallocSize(pNew); 541f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 542fec00eabSdrh } 543fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 544fec00eabSdrh }else{ 5457c6791c8Sdrh pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 546fec00eabSdrh } 547fec00eabSdrh return pNew; 548fec00eabSdrh } 549fec00eabSdrh 550fec00eabSdrh /* 551fec00eabSdrh ** The public interface to sqlite3Realloc. Make sure that the memory 552fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc. 553fec00eabSdrh */ 554fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){ 555fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 556fec00eabSdrh if( sqlite3_initialize() ) return 0; 557fec00eabSdrh #endif 558fec00eabSdrh return sqlite3Realloc(pOld, n); 559fec00eabSdrh } 560fec00eabSdrh 561a3152895Sdrh 562a3152895Sdrh /* 56317435752Sdrh ** Allocate and zero memory. 564a3152895Sdrh */ 565fec00eabSdrh void *sqlite3MallocZero(int n){ 566fec00eabSdrh void *p = sqlite3Malloc(n); 567a3152895Sdrh if( p ){ 568a3152895Sdrh memset(p, 0, n); 569a3152895Sdrh } 570a3152895Sdrh return p; 571a3152895Sdrh } 57217435752Sdrh 57317435752Sdrh /* 57417435752Sdrh ** Allocate and zero memory. If the allocation fails, make 57517435752Sdrh ** the mallocFailed flag in the connection pointer. 57617435752Sdrh */ 577fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){ 578a1644fd8Sdanielk1977 void *p = sqlite3DbMallocRaw(db, n); 57917435752Sdrh if( p ){ 58017435752Sdrh memset(p, 0, n); 58117435752Sdrh } 58217435752Sdrh return p; 58317435752Sdrh } 58417435752Sdrh 58517435752Sdrh /* 58617435752Sdrh ** Allocate and zero memory. If the allocation fails, make 58717435752Sdrh ** the mallocFailed flag in the connection pointer. 588ddecae79Sdrh ** 589ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc 590ddecae79Sdrh ** failure on the same database connection) then always return 0. 591ddecae79Sdrh ** Hence for a particular database connection, once malloc starts 592ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset. 593ddecae79Sdrh ** This is an important assumption. There are many places in the 594ddecae79Sdrh ** code that do things like this: 595ddecae79Sdrh ** 596ddecae79Sdrh ** int *a = (int*)sqlite3DbMallocRaw(db, 100); 597ddecae79Sdrh ** int *b = (int*)sqlite3DbMallocRaw(db, 200); 598ddecae79Sdrh ** if( b ) a[10] = 9; 599ddecae79Sdrh ** 600ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed 601ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too. 60217435752Sdrh */ 603fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){ 604633e6d57Sdrh void *p; 605d9da78a2Sdrh assert( db==0 || sqlite3_mutex_held(db->mutex) ); 606ccd4ad3eSdan assert( db==0 || db->pnBytesFreed==0 ); 6074150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE 608633e6d57Sdrh if( db ){ 609633e6d57Sdrh LookasideSlot *pBuf; 610633e6d57Sdrh if( db->mallocFailed ){ 611633e6d57Sdrh return 0; 612633e6d57Sdrh } 613633e6d57Sdrh if( db->lookaside.bEnabled && n<=db->lookaside.sz 614633e6d57Sdrh && (pBuf = db->lookaside.pFree)!=0 ){ 615633e6d57Sdrh db->lookaside.pFree = pBuf->pNext; 616633e6d57Sdrh db->lookaside.nOut++; 617633e6d57Sdrh if( db->lookaside.nOut>db->lookaside.mxOut ){ 618633e6d57Sdrh db->lookaside.mxOut = db->lookaside.nOut; 619633e6d57Sdrh } 620633e6d57Sdrh return (void*)pBuf; 621633e6d57Sdrh } 622633e6d57Sdrh } 623ddecae79Sdrh #else 624ddecae79Sdrh if( db && db->mallocFailed ){ 625ddecae79Sdrh return 0; 626ddecae79Sdrh } 6274150ebf8Sdrh #endif 628fec00eabSdrh p = sqlite3Malloc(n); 629f3a65f7eSdrh if( !p && db ){ 63017435752Sdrh db->mallocFailed = 1; 63117435752Sdrh } 632174b9a16Sdrh sqlite3MemdebugSetType(p, MEMTYPE_DB | 633174b9a16Sdrh ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 63417435752Sdrh return p; 63517435752Sdrh } 63617435752Sdrh 63726783a58Sdanielk1977 /* 63826783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the 63926783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object. 64026783a58Sdanielk1977 */ 641a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 642a1644fd8Sdanielk1977 void *pNew = 0; 643d9da78a2Sdrh assert( db!=0 ); 6447047e25cSdrh assert( sqlite3_mutex_held(db->mutex) ); 645a1644fd8Sdanielk1977 if( db->mallocFailed==0 ){ 646633e6d57Sdrh if( p==0 ){ 647633e6d57Sdrh return sqlite3DbMallocRaw(db, n); 648633e6d57Sdrh } 649633e6d57Sdrh if( isLookaside(db, p) ){ 650633e6d57Sdrh if( n<=db->lookaside.sz ){ 651633e6d57Sdrh return p; 652633e6d57Sdrh } 653633e6d57Sdrh pNew = sqlite3DbMallocRaw(db, n); 654633e6d57Sdrh if( pNew ){ 655633e6d57Sdrh memcpy(pNew, p, db->lookaside.sz); 656633e6d57Sdrh sqlite3DbFree(db, p); 657633e6d57Sdrh } 658633e6d57Sdrh }else{ 659174b9a16Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); 660174b9a16Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); 661107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 662a1644fd8Sdanielk1977 pNew = sqlite3_realloc(p, n); 663a1644fd8Sdanielk1977 if( !pNew ){ 664174b9a16Sdrh sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP); 665a1644fd8Sdanielk1977 db->mallocFailed = 1; 666a1644fd8Sdanielk1977 } 667174b9a16Sdrh sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 668174b9a16Sdrh (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 669a1644fd8Sdanielk1977 } 670633e6d57Sdrh } 671a1644fd8Sdanielk1977 return pNew; 672a1644fd8Sdanielk1977 } 673a1644fd8Sdanielk1977 67417435752Sdrh /* 67517435752Sdrh ** Attempt to reallocate p. If the reallocation fails, then free p 67617435752Sdrh ** and set the mallocFailed flag in the database connection. 67717435752Sdrh */ 67817435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 679a3152895Sdrh void *pNew; 680a1644fd8Sdanielk1977 pNew = sqlite3DbRealloc(db, p, n); 681a3152895Sdrh if( !pNew ){ 682633e6d57Sdrh sqlite3DbFree(db, p); 683a3152895Sdrh } 684a3152895Sdrh return pNew; 685a3152895Sdrh } 686a3152895Sdrh 687a3152895Sdrh /* 688a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These 689a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 690a3152895Sdrh ** is because when memory debugging is turned on, these two functions are 691a3152895Sdrh ** called via macros that record the current file and line number in the 692a3152895Sdrh ** ThreadData structure. 693a3152895Sdrh */ 694633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 695a3152895Sdrh char *zNew; 696633e6d57Sdrh size_t n; 697633e6d57Sdrh if( z==0 ){ 698633e6d57Sdrh return 0; 699a3152895Sdrh } 700dee0e404Sdrh n = sqlite3Strlen30(z) + 1; 701633e6d57Sdrh assert( (n&0x7fffffff)==n ); 702633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, (int)n); 703a3152895Sdrh if( zNew ){ 704a3152895Sdrh memcpy(zNew, z, n); 7051e536953Sdanielk1977 } 7061e536953Sdanielk1977 return zNew; 7071e536953Sdanielk1977 } 7081e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 709633e6d57Sdrh char *zNew; 710633e6d57Sdrh if( z==0 ){ 711633e6d57Sdrh return 0; 712633e6d57Sdrh } 713633e6d57Sdrh assert( (n&0x7fffffff)==n ); 714633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, n+1); 715633e6d57Sdrh if( zNew ){ 716633e6d57Sdrh memcpy(zNew, z, n); 717633e6d57Sdrh zNew[n] = 0; 7181e536953Sdanielk1977 } 7191e536953Sdanielk1977 return zNew; 7201e536953Sdanielk1977 } 7211e536953Sdanielk1977 722a3152895Sdrh /* 723f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows. 724f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz 725f089aa45Sdrh ** point to that string. 726a3152895Sdrh */ 727f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ 728a3152895Sdrh va_list ap; 729f089aa45Sdrh char *z; 730a3152895Sdrh 731f089aa45Sdrh va_start(ap, zFormat); 732f089aa45Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 733a3152895Sdrh va_end(ap); 734633e6d57Sdrh sqlite3DbFree(db, *pz); 735f089aa45Sdrh *pz = z; 736a3152895Sdrh } 737a3152895Sdrh 738a3152895Sdrh 739a3152895Sdrh /* 740a3152895Sdrh ** This function must be called before exiting any API function (i.e. 74117435752Sdrh ** returning control to the user) that has called sqlite3_malloc or 74217435752Sdrh ** sqlite3_realloc. 743a3152895Sdrh ** 744a3152895Sdrh ** The returned value is normally a copy of the second argument to this 745be217793Sshane ** function. However, if a malloc() failure has occurred since the previous 746a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead. 747a3152895Sdrh ** 748be217793Sshane ** If the first argument, db, is not NULL and a malloc() error has occurred, 749a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode()) 750a3152895Sdrh ** is set to SQLITE_NOMEM. 751a3152895Sdrh */ 752a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){ 753a1644fd8Sdanielk1977 /* If the db handle is not NULL, then we must hold the connection handle 754a1644fd8Sdanielk1977 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 755a1644fd8Sdanielk1977 ** is unsafe, as is the call to sqlite3Error(). 756a1644fd8Sdanielk1977 */ 757a1644fd8Sdanielk1977 assert( !db || sqlite3_mutex_held(db->mutex) ); 75898c21903Sdanielk1977 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){ 759a3152895Sdrh sqlite3Error(db, SQLITE_NOMEM, 0); 76017435752Sdrh db->mallocFailed = 0; 761a3152895Sdrh rc = SQLITE_NOMEM; 762a3152895Sdrh } 763a3152895Sdrh return rc & (db ? db->errMask : 0xff); 764a3152895Sdrh } 765