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 ** 15*5f4bcf15Sdrh ** $Id: malloc.c,v 1.31 2008/07/29 14:29:07 drh Exp $ 16a3152895Sdrh */ 17a3152895Sdrh #include "sqliteInt.h" 18a3152895Sdrh #include <stdarg.h> 19a3152895Sdrh #include <ctype.h> 20a3152895Sdrh 21a3152895Sdrh /* 22b21c8cd4Sdrh ** This routine runs when the memory allocator sees that the 23b21c8cd4Sdrh ** total memory allocation is about to exceed the soft heap 24b21c8cd4Sdrh ** limit. 25b21c8cd4Sdrh */ 26b21c8cd4Sdrh static void softHeapLimitEnforcer( 27b21c8cd4Sdrh void *NotUsed, 28153c62c4Sdrh sqlite3_int64 inUse, 29153c62c4Sdrh int allocSize 30b21c8cd4Sdrh ){ 31b21c8cd4Sdrh sqlite3_release_memory(allocSize); 32b21c8cd4Sdrh } 33b21c8cd4Sdrh 34b21c8cd4Sdrh /* 358468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or 368468024dSdanielk1977 ** negative value indicates no limit. 37a3152895Sdrh */ 38a3152895Sdrh void sqlite3_soft_heap_limit(int n){ 39b21c8cd4Sdrh sqlite3_uint64 iLimit; 40b21c8cd4Sdrh int overage; 41b21c8cd4Sdrh if( n<0 ){ 42b21c8cd4Sdrh iLimit = 0; 43b21c8cd4Sdrh }else{ 44b21c8cd4Sdrh iLimit = n; 45a3152895Sdrh } 469ac3fe97Sdrh sqlite3_initialize(); 47b21c8cd4Sdrh if( iLimit>0 ){ 48b21c8cd4Sdrh sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit); 49b21c8cd4Sdrh }else{ 50b21c8cd4Sdrh sqlite3_memory_alarm(0, 0, 0); 51b21c8cd4Sdrh } 52b21c8cd4Sdrh overage = sqlite3_memory_used() - 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 65dfb316d4Sdanielk1977 int nRet = sqlite3VdbeReleaseMemory(n); 66dfb316d4Sdanielk1977 nRet += sqlite3PagerReleaseMemory(n-nRet); 67dfb316d4Sdanielk1977 return nRet; 681e536953Sdanielk1977 #else 691e536953Sdanielk1977 return SQLITE_OK; 701e536953Sdanielk1977 #endif 71a3152895Sdrh } 72a3152895Sdrh 73fec00eabSdrh /* 74fec00eabSdrh ** State information local to the memory allocation subsystem. 75fec00eabSdrh */ 76fec00eabSdrh static struct { 77fec00eabSdrh sqlite3_mutex *mutex; /* Mutex to serialize access */ 78fec00eabSdrh 79fec00eabSdrh /* 80fec00eabSdrh ** The alarm callback and its arguments. The mem0.mutex lock will 81fec00eabSdrh ** be held while the callback is running. Recursive calls into 82fec00eabSdrh ** the memory subsystem are allowed, but no new callbacks will be 83fec00eabSdrh ** issued. The alarmBusy variable is set to prevent recursive 84fec00eabSdrh ** callbacks. 85fec00eabSdrh */ 86fec00eabSdrh sqlite3_int64 alarmThreshold; 87fec00eabSdrh void (*alarmCallback)(void*, sqlite3_int64,int); 88fec00eabSdrh void *alarmArg; 89fec00eabSdrh int alarmBusy; 90fec00eabSdrh 91fec00eabSdrh /* 929ac3fe97Sdrh ** Pointers to the end of sqlite3Config.pScratch and 939ac3fe97Sdrh ** sqlite3Config.pPage to a block of memory that records 949ac3fe97Sdrh ** which pages are available. 959ac3fe97Sdrh */ 969ac3fe97Sdrh u32 *aScratchFree; 979ac3fe97Sdrh u32 *aPageFree; 989ac3fe97Sdrh 999ac3fe97Sdrh /* Number of free pages for scratch and page-cache memory */ 1009ac3fe97Sdrh u32 nScratchFree; 1019ac3fe97Sdrh u32 nPageFree; 102fec00eabSdrh } mem0; 103fec00eabSdrh 104fec00eabSdrh /* 105fec00eabSdrh ** Initialize the memory allocation subsystem. 106fec00eabSdrh */ 107fec00eabSdrh int sqlite3MallocInit(void){ 108fec00eabSdrh if( sqlite3Config.m.xMalloc==0 ){ 109fec00eabSdrh sqlite3MemSetDefault(); 110fec00eabSdrh } 111fec00eabSdrh memset(&mem0, 0, sizeof(mem0)); 1129ac3fe97Sdrh if( sqlite3Config.bCoreMutex ){ 11359f8c08eSdanielk1977 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 114fec00eabSdrh } 1159ac3fe97Sdrh if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000 1169ac3fe97Sdrh && sqlite3Config.nScratch>0 ){ 1179ac3fe97Sdrh int i; 1189ac3fe97Sdrh mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch) 1199ac3fe97Sdrh [sqlite3Config.szScratch*sqlite3Config.nScratch]; 1209ac3fe97Sdrh for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; } 1219ac3fe97Sdrh mem0.nScratchFree = sqlite3Config.nScratch; 1229ac3fe97Sdrh }else{ 1239ac3fe97Sdrh sqlite3Config.pScratch = 0; 124f7141990Sdrh sqlite3Config.szScratch = 0; 1259ac3fe97Sdrh } 1269ac3fe97Sdrh if( sqlite3Config.pPage && sqlite3Config.szPage>=512 1279ac3fe97Sdrh && sqlite3Config.nPage>0 ){ 1289ac3fe97Sdrh int i; 1299ac3fe97Sdrh mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage) 1309ac3fe97Sdrh [sqlite3Config.szPage*sqlite3Config.nPage]; 1319ac3fe97Sdrh for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; } 1329ac3fe97Sdrh mem0.nPageFree = sqlite3Config.nPage; 1339ac3fe97Sdrh }else{ 1349ac3fe97Sdrh sqlite3Config.pPage = 0; 135f7141990Sdrh sqlite3Config.szPage = 0; 1369ac3fe97Sdrh } 137fec00eabSdrh return sqlite3Config.m.xInit(sqlite3Config.m.pAppData); 138fec00eabSdrh } 139fec00eabSdrh 140fec00eabSdrh /* 141fec00eabSdrh ** Deinitialize the memory allocation subsystem. 142fec00eabSdrh */ 143fec00eabSdrh void sqlite3MallocEnd(void){ 144fec00eabSdrh sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData); 1459ac3fe97Sdrh memset(&mem0, 0, sizeof(mem0)); 146fec00eabSdrh } 147fec00eabSdrh 148fec00eabSdrh /* 149fec00eabSdrh ** Return the amount of memory currently checked out. 150fec00eabSdrh */ 151fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){ 152f7141990Sdrh int n, mx; 153c376a198Sdrh sqlite3_int64 res; 154f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); 155c376a198Sdrh res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ 156c376a198Sdrh return res; 157fec00eabSdrh } 158fec00eabSdrh 159fec00eabSdrh /* 160fec00eabSdrh ** Return the maximum amount of memory that has ever been 161fec00eabSdrh ** checked out since either the beginning of this process 162fec00eabSdrh ** or since the most recent reset. 163fec00eabSdrh */ 164fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 165f7141990Sdrh int n, mx; 166c376a198Sdrh sqlite3_int64 res; 167f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); 1687986a71aSdrh res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ 169c376a198Sdrh return res; 170fec00eabSdrh } 171fec00eabSdrh 172fec00eabSdrh /* 173fec00eabSdrh ** Change the alarm callback 174fec00eabSdrh */ 175fec00eabSdrh int sqlite3_memory_alarm( 176fec00eabSdrh void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 177fec00eabSdrh void *pArg, 178fec00eabSdrh sqlite3_int64 iThreshold 179fec00eabSdrh ){ 180fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 181fec00eabSdrh mem0.alarmCallback = xCallback; 182fec00eabSdrh mem0.alarmArg = pArg; 183fec00eabSdrh mem0.alarmThreshold = iThreshold; 184fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 185fec00eabSdrh return SQLITE_OK; 186fec00eabSdrh } 187fec00eabSdrh 188fec00eabSdrh /* 189fec00eabSdrh ** Trigger the alarm 190fec00eabSdrh */ 191fec00eabSdrh static void sqlite3MallocAlarm(int nByte){ 192fec00eabSdrh void (*xCallback)(void*,sqlite3_int64,int); 193fec00eabSdrh sqlite3_int64 nowUsed; 194fec00eabSdrh void *pArg; 195fec00eabSdrh if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; 196fec00eabSdrh mem0.alarmBusy = 1; 197fec00eabSdrh xCallback = mem0.alarmCallback; 198f7141990Sdrh nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 199fec00eabSdrh pArg = mem0.alarmArg; 200fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 201fec00eabSdrh xCallback(pArg, nowUsed, nByte); 202fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 203fec00eabSdrh mem0.alarmBusy = 0; 204fec00eabSdrh } 205fec00eabSdrh 206fec00eabSdrh /* 207f7141990Sdrh ** Do a memory allocation with statistics and alarms. Assume the 208f7141990Sdrh ** lock is already held. 209fec00eabSdrh */ 210f7141990Sdrh static int mallocWithAlarm(int n, void **pp){ 211fec00eabSdrh int nFull; 212f7141990Sdrh void *p; 213f7141990Sdrh assert( sqlite3_mutex_held(mem0.mutex) ); 214fec00eabSdrh nFull = sqlite3Config.m.xRoundup(n); 215f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 216f7141990Sdrh if( mem0.alarmCallback!=0 ){ 217f7141990Sdrh int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 218f7141990Sdrh if( nUsed+nFull >= mem0.alarmThreshold ){ 219fec00eabSdrh sqlite3MallocAlarm(nFull); 220fec00eabSdrh } 221f7141990Sdrh } 222fec00eabSdrh p = sqlite3Config.m.xMalloc(nFull); 223d09414cdSdanielk1977 if( p==0 && mem0.alarmCallback ){ 224fec00eabSdrh sqlite3MallocAlarm(nFull); 225d09414cdSdanielk1977 p = sqlite3Config.m.xMalloc(nFull); 226fec00eabSdrh } 227c702c7ccSdrh if( p ){ 228c702c7ccSdrh nFull = sqlite3MallocSize(p); 229c702c7ccSdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 230c702c7ccSdrh } 231f7141990Sdrh *pp = p; 232f7141990Sdrh return nFull; 233fec00eabSdrh } 234f7141990Sdrh 235f7141990Sdrh /* 236f7141990Sdrh ** Allocate memory. This routine is like sqlite3_malloc() except that it 237f7141990Sdrh ** assumes the memory subsystem has already been initialized. 238f7141990Sdrh */ 239f7141990Sdrh void *sqlite3Malloc(int n){ 240f7141990Sdrh void *p; 241f7141990Sdrh if( n<=0 ){ 242f7141990Sdrh p = 0; 243f7141990Sdrh }else if( sqlite3Config.bMemstat ){ 244f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 245f7141990Sdrh mallocWithAlarm(n, &p); 246fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 247fec00eabSdrh }else{ 248fec00eabSdrh p = sqlite3Config.m.xMalloc(n); 249fec00eabSdrh } 250fec00eabSdrh return p; 251fec00eabSdrh } 252fec00eabSdrh 253fec00eabSdrh /* 254fec00eabSdrh ** This version of the memory allocation is for use by the application. 255fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the 256fec00eabSdrh ** allocation. 257fec00eabSdrh */ 258fec00eabSdrh void *sqlite3_malloc(int n){ 259fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 260fec00eabSdrh if( sqlite3_initialize() ) return 0; 261fec00eabSdrh #endif 262fec00eabSdrh return sqlite3Malloc(n); 263fec00eabSdrh } 264fec00eabSdrh 265fec00eabSdrh /* 266e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from 267facf0307Sdrh ** xScratchMalloc(). We verify this constraint in the single-threaded 268facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation 269e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed. 270e5ae5735Sdrh */ 271e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 272facf0307Sdrh static int scratchAllocOut = 0; 273e5ae5735Sdrh #endif 274e5ae5735Sdrh 275e5ae5735Sdrh 276e5ae5735Sdrh /* 277e5ae5735Sdrh ** Allocate memory that is to be used and released right away. 278e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended 279e5ae5735Sdrh ** for situations where the memory might be held long-term. This 280e5ae5735Sdrh ** routine is intended to get memory to old large transient data 281e5ae5735Sdrh ** structures that would not normally fit on the stack of an 282e5ae5735Sdrh ** embedded processor. 283e5ae5735Sdrh */ 284facf0307Sdrh void *sqlite3ScratchMalloc(int n){ 285e5ae5735Sdrh void *p; 286e5ae5735Sdrh assert( n>0 ); 2879ac3fe97Sdrh 288e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 2899ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 2909ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 2919ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 2929ac3fe97Sdrh ** would be much more complicated.) */ 293facf0307Sdrh assert( scratchAllocOut==0 ); 294e5ae5735Sdrh #endif 2959ac3fe97Sdrh 296f7141990Sdrh if( sqlite3Config.szScratch<n ){ 297f7141990Sdrh goto scratch_overflow; 298f7141990Sdrh }else{ 299e5ae5735Sdrh sqlite3_mutex_enter(mem0.mutex); 300f7141990Sdrh if( mem0.nScratchFree==0 ){ 301f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 302f7141990Sdrh goto scratch_overflow; 303e5ae5735Sdrh }else{ 3049ac3fe97Sdrh int i; 3059ac3fe97Sdrh i = mem0.aScratchFree[--mem0.nScratchFree]; 306f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 3079ac3fe97Sdrh i *= sqlite3Config.szScratch; 308f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 3099ac3fe97Sdrh p = (void*)&((char*)sqlite3Config.pScratch)[i]; 310e5ae5735Sdrh } 311f7141990Sdrh } 312f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 313f7141990Sdrh scratchAllocOut = p!=0; 314f7141990Sdrh #endif 315f7141990Sdrh 316f7141990Sdrh return p; 317f7141990Sdrh 318f7141990Sdrh scratch_overflow: 319f7141990Sdrh if( sqlite3Config.bMemstat ){ 320f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 321f7141990Sdrh n = mallocWithAlarm(n, &p); 322f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); 3239ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 324f7141990Sdrh }else{ 325f7141990Sdrh p = sqlite3Config.m.xMalloc(n); 326f7141990Sdrh } 327f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 328f7141990Sdrh scratchAllocOut = p!=0; 329f7141990Sdrh #endif 330e5ae5735Sdrh return p; 331e5ae5735Sdrh } 332facf0307Sdrh void sqlite3ScratchFree(void *p){ 333e5ae5735Sdrh if( p ){ 3349ac3fe97Sdrh 335e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 3369ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 3379ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 3389ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 3399ac3fe97Sdrh ** would be much more complicated.) */ 340facf0307Sdrh assert( scratchAllocOut==1 ); 341facf0307Sdrh scratchAllocOut = 0; 342e5ae5735Sdrh #endif 3439ac3fe97Sdrh 3449ac3fe97Sdrh if( sqlite3Config.pScratch==0 3459ac3fe97Sdrh || p<sqlite3Config.pScratch 3469ac3fe97Sdrh || p>=(void*)mem0.aScratchFree ){ 347f7141990Sdrh if( sqlite3Config.bMemstat ){ 348f7141990Sdrh int iSize = sqlite3MallocSize(p); 349f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 350f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 351f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 352facf0307Sdrh sqlite3Config.m.xFree(p); 353f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 354f7141990Sdrh }else{ 355f7141990Sdrh sqlite3Config.m.xFree(p); 356f7141990Sdrh } 3579ac3fe97Sdrh }else{ 3589ac3fe97Sdrh int i; 359867d05a0Sdanielk1977 i = (u8 *)p - (u8 *)sqlite3Config.pScratch; 3609ac3fe97Sdrh i /= sqlite3Config.szScratch; 3619ac3fe97Sdrh assert( i>=0 && i<sqlite3Config.nScratch ); 362f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 363f7141990Sdrh assert( mem0.nScratchFree<sqlite3Config.nScratch ); 3649ac3fe97Sdrh mem0.aScratchFree[mem0.nScratchFree++] = i; 365f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 3669ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 3679ac3fe97Sdrh } 368e5ae5735Sdrh } 369e5ae5735Sdrh } 370e5ae5735Sdrh 371e5ae5735Sdrh /* 372f7141990Sdrh ** Allocate memory to be used by the page cache. Make use of the 373f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one 374f7141990Sdrh ** and that memory is of the right size and is not completely 375f7141990Sdrh ** consumed. Otherwise, failover to sqlite3Malloc(). 376facf0307Sdrh */ 377f7141990Sdrh void *sqlite3PageMalloc(int n){ 378f7141990Sdrh void *p; 379f7141990Sdrh assert( n>0 ); 380f7141990Sdrh assert( (n & (n-1))==0 ); 381f7141990Sdrh assert( n>=512 && n<=32768 ); 382f7141990Sdrh 383f7141990Sdrh if( sqlite3Config.szPage<n ){ 384f7141990Sdrh goto page_overflow; 385f7141990Sdrh }else{ 386f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 387f7141990Sdrh if( mem0.nPageFree==0 ){ 388f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 389f7141990Sdrh goto page_overflow; 390f7141990Sdrh }else{ 391f7141990Sdrh int i; 392f7141990Sdrh i = mem0.aPageFree[--mem0.nPageFree]; 393f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 394f7141990Sdrh i *= sqlite3Config.szPage; 395f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); 396f7141990Sdrh p = (void*)&((char*)sqlite3Config.pPage)[i]; 397f7141990Sdrh } 398f7141990Sdrh } 399f7141990Sdrh return p; 400f7141990Sdrh 401f7141990Sdrh page_overflow: 402f7141990Sdrh if( sqlite3Config.bMemstat ){ 403f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 404f7141990Sdrh n = mallocWithAlarm(n, &p); 405f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); 406f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 407f7141990Sdrh }else{ 408f7141990Sdrh p = sqlite3Config.m.xMalloc(n); 409f7141990Sdrh } 410f7141990Sdrh return p; 411f7141990Sdrh } 412f7141990Sdrh void sqlite3PageFree(void *p){ 413f7141990Sdrh if( p ){ 414f7141990Sdrh if( sqlite3Config.pPage==0 415f7141990Sdrh || p<sqlite3Config.pPage 416f7141990Sdrh || p>=(void*)mem0.aPageFree ){ 4174b9507a0Sdanielk1977 /* In this case, the page allocation was obtained from a regular 4184b9507a0Sdanielk1977 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory 4194b9507a0Sdanielk1977 ** "overflow"). Free the block with sqlite3_mem_methods.xFree(). 4204b9507a0Sdanielk1977 */ 421f7141990Sdrh if( sqlite3Config.bMemstat ){ 422f7141990Sdrh int iSize = sqlite3MallocSize(p); 423f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 424f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); 425f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 426f7141990Sdrh sqlite3Config.m.xFree(p); 427f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 428f7141990Sdrh }else{ 429f7141990Sdrh sqlite3Config.m.xFree(p); 430f7141990Sdrh } 431f7141990Sdrh }else{ 4324b9507a0Sdanielk1977 /* The page allocation was allocated from the sqlite3Config.pPage 4334b9507a0Sdanielk1977 ** buffer. In this case all that is add the index of the page in 4344b9507a0Sdanielk1977 ** the sqlite3Config.pPage array to the set of free indexes stored 4354b9507a0Sdanielk1977 ** in the mem0.aPageFree[] array. 4364b9507a0Sdanielk1977 */ 437f7141990Sdrh int i; 438867d05a0Sdanielk1977 i = (u8 *)p - (u8 *)sqlite3Config.pPage; 439f7141990Sdrh i /= sqlite3Config.szPage; 440f7141990Sdrh assert( i>=0 && i<sqlite3Config.nPage ); 441f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 442f7141990Sdrh assert( mem0.nPageFree<sqlite3Config.nPage ); 443f7141990Sdrh mem0.aPageFree[mem0.nPageFree++] = i; 444f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); 445f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 446*5f4bcf15Sdrh #if !defined(NDEBUG) && 0 4474b9507a0Sdanielk1977 /* Assert that a duplicate was not just inserted into aPageFree[]. */ 4484b9507a0Sdanielk1977 for(i=0; i<mem0.nPageFree-1; i++){ 4494b9507a0Sdanielk1977 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] ); 4504b9507a0Sdanielk1977 } 4514b9507a0Sdanielk1977 #endif 452f7141990Sdrh } 453f7141990Sdrh } 454facf0307Sdrh } 455facf0307Sdrh 456facf0307Sdrh /* 457633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db 458633e6d57Sdrh */ 459633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){ 460633e6d57Sdrh return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; 461633e6d57Sdrh } 462633e6d57Sdrh 463633e6d57Sdrh /* 464fec00eabSdrh ** Return the size of a memory allocation previously obtained from 465fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc(). 466fec00eabSdrh */ 467fec00eabSdrh int sqlite3MallocSize(void *p){ 468fec00eabSdrh return sqlite3Config.m.xSize(p); 469fec00eabSdrh } 470633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){ 471633e6d57Sdrh if( isLookaside(db, p) ){ 472633e6d57Sdrh return db->lookaside.sz; 473633e6d57Sdrh }else{ 474633e6d57Sdrh return sqlite3Config.m.xSize(p); 475633e6d57Sdrh } 476633e6d57Sdrh } 477fec00eabSdrh 478fec00eabSdrh /* 479fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc(). 480fec00eabSdrh */ 481fec00eabSdrh void sqlite3_free(void *p){ 482fec00eabSdrh if( p==0 ) return; 483fec00eabSdrh if( sqlite3Config.bMemstat ){ 484fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 485f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 486fec00eabSdrh sqlite3Config.m.xFree(p); 487fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 488fec00eabSdrh }else{ 489fec00eabSdrh sqlite3Config.m.xFree(p); 490fec00eabSdrh } 491fec00eabSdrh } 492fec00eabSdrh 493fec00eabSdrh /* 494633e6d57Sdrh ** Free memory that might be associated with a particular database 495633e6d57Sdrh ** connection. 496633e6d57Sdrh */ 497633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){ 498633e6d57Sdrh if( isLookaside(db, p) ){ 499633e6d57Sdrh LookasideSlot *pBuf = (LookasideSlot*)p; 500633e6d57Sdrh pBuf->pNext = db->lookaside.pFree; 501633e6d57Sdrh db->lookaside.pFree = pBuf; 502633e6d57Sdrh db->lookaside.nOut--; 503633e6d57Sdrh }else{ 504633e6d57Sdrh sqlite3_free(p); 505633e6d57Sdrh } 506633e6d57Sdrh } 507633e6d57Sdrh 508633e6d57Sdrh /* 509fec00eabSdrh ** Change the size of an existing memory allocation 510fec00eabSdrh */ 511fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){ 512fec00eabSdrh int nOld, nNew; 513fec00eabSdrh void *pNew; 514fec00eabSdrh if( pOld==0 ){ 515fec00eabSdrh return sqlite3Malloc(nBytes); 516fec00eabSdrh } 517fec00eabSdrh if( nBytes<=0 ){ 518fec00eabSdrh sqlite3_free(pOld); 519fec00eabSdrh return 0; 520fec00eabSdrh } 521fec00eabSdrh nOld = sqlite3MallocSize(pOld); 522fec00eabSdrh if( sqlite3Config.bMemstat ){ 523fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 524f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); 525fec00eabSdrh nNew = sqlite3Config.m.xRoundup(nBytes); 526fec00eabSdrh if( nOld==nNew ){ 527fec00eabSdrh pNew = pOld; 528fec00eabSdrh }else{ 529f7141990Sdrh if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 530f7141990Sdrh mem0.alarmThreshold ){ 531fec00eabSdrh sqlite3MallocAlarm(nNew-nOld); 532fec00eabSdrh } 533fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nNew); 534d09414cdSdanielk1977 if( pNew==0 && mem0.alarmCallback ){ 535fec00eabSdrh sqlite3MallocAlarm(nBytes); 536fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nNew); 537fec00eabSdrh } 538fec00eabSdrh if( pNew ){ 539c702c7ccSdrh nNew = sqlite3MallocSize(pNew); 540f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 541fec00eabSdrh } 542fec00eabSdrh } 543fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 544fec00eabSdrh }else{ 545fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nBytes); 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. 58817435752Sdrh */ 589fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){ 590633e6d57Sdrh void *p; 591633e6d57Sdrh if( db ){ 592633e6d57Sdrh LookasideSlot *pBuf; 593633e6d57Sdrh if( db->mallocFailed ){ 594633e6d57Sdrh return 0; 595633e6d57Sdrh } 596633e6d57Sdrh if( db->lookaside.bEnabled && n<=db->lookaside.sz 597633e6d57Sdrh && (pBuf = db->lookaside.pFree)!=0 ){ 598633e6d57Sdrh db->lookaside.pFree = pBuf->pNext; 599633e6d57Sdrh db->lookaside.nOut++; 600633e6d57Sdrh if( db->lookaside.nOut>db->lookaside.mxOut ){ 601633e6d57Sdrh db->lookaside.mxOut = db->lookaside.nOut; 602633e6d57Sdrh } 603633e6d57Sdrh return (void*)pBuf; 604633e6d57Sdrh } 605633e6d57Sdrh } 606fec00eabSdrh p = sqlite3Malloc(n); 607f3a65f7eSdrh if( !p && db ){ 60817435752Sdrh db->mallocFailed = 1; 60917435752Sdrh } 61017435752Sdrh return p; 61117435752Sdrh } 61217435752Sdrh 61326783a58Sdanielk1977 /* 61426783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the 61526783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object. 61626783a58Sdanielk1977 */ 617a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 618a1644fd8Sdanielk1977 void *pNew = 0; 619a1644fd8Sdanielk1977 if( db->mallocFailed==0 ){ 620633e6d57Sdrh if( p==0 ){ 621633e6d57Sdrh return sqlite3DbMallocRaw(db, n); 622633e6d57Sdrh } 623633e6d57Sdrh if( isLookaside(db, p) ){ 624633e6d57Sdrh if( n<=db->lookaside.sz ){ 625633e6d57Sdrh return p; 626633e6d57Sdrh } 627633e6d57Sdrh pNew = sqlite3DbMallocRaw(db, n); 628633e6d57Sdrh if( pNew ){ 629633e6d57Sdrh memcpy(pNew, p, db->lookaside.sz); 630633e6d57Sdrh sqlite3DbFree(db, p); 631633e6d57Sdrh } 632633e6d57Sdrh }else{ 633a1644fd8Sdanielk1977 pNew = sqlite3_realloc(p, n); 634a1644fd8Sdanielk1977 if( !pNew ){ 635a1644fd8Sdanielk1977 db->mallocFailed = 1; 636a1644fd8Sdanielk1977 } 637a1644fd8Sdanielk1977 } 638633e6d57Sdrh } 639a1644fd8Sdanielk1977 return pNew; 640a1644fd8Sdanielk1977 } 641a1644fd8Sdanielk1977 64217435752Sdrh /* 64317435752Sdrh ** Attempt to reallocate p. If the reallocation fails, then free p 64417435752Sdrh ** and set the mallocFailed flag in the database connection. 64517435752Sdrh */ 64617435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 647a3152895Sdrh void *pNew; 648a1644fd8Sdanielk1977 pNew = sqlite3DbRealloc(db, p, n); 649a3152895Sdrh if( !pNew ){ 650633e6d57Sdrh sqlite3DbFree(db, p); 651a3152895Sdrh } 652a3152895Sdrh return pNew; 653a3152895Sdrh } 654a3152895Sdrh 655a3152895Sdrh /* 656a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These 657a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 658a3152895Sdrh ** is because when memory debugging is turned on, these two functions are 659a3152895Sdrh ** called via macros that record the current file and line number in the 660a3152895Sdrh ** ThreadData structure. 661a3152895Sdrh */ 662633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 663a3152895Sdrh char *zNew; 664633e6d57Sdrh size_t n; 665633e6d57Sdrh if( z==0 ){ 666633e6d57Sdrh return 0; 667a3152895Sdrh } 668633e6d57Sdrh n = strlen(z)+1; 669633e6d57Sdrh assert( (n&0x7fffffff)==n ); 670633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, (int)n); 671a3152895Sdrh if( zNew ){ 672a3152895Sdrh memcpy(zNew, z, n); 6731e536953Sdanielk1977 } 6741e536953Sdanielk1977 return zNew; 6751e536953Sdanielk1977 } 6761e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 677633e6d57Sdrh char *zNew; 678633e6d57Sdrh if( z==0 ){ 679633e6d57Sdrh return 0; 680633e6d57Sdrh } 681633e6d57Sdrh assert( (n&0x7fffffff)==n ); 682633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, n+1); 683633e6d57Sdrh if( zNew ){ 684633e6d57Sdrh memcpy(zNew, z, n); 685633e6d57Sdrh zNew[n] = 0; 6861e536953Sdanielk1977 } 6871e536953Sdanielk1977 return zNew; 6881e536953Sdanielk1977 } 6891e536953Sdanielk1977 690a3152895Sdrh /* 691f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows. 692f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz 693f089aa45Sdrh ** point to that string. 694a3152895Sdrh */ 695f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ 696a3152895Sdrh va_list ap; 697f089aa45Sdrh char *z; 698a3152895Sdrh 699f089aa45Sdrh va_start(ap, zFormat); 700f089aa45Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 701a3152895Sdrh va_end(ap); 702633e6d57Sdrh sqlite3DbFree(db, *pz); 703f089aa45Sdrh *pz = z; 704a3152895Sdrh } 705a3152895Sdrh 706a3152895Sdrh 707a3152895Sdrh /* 708a3152895Sdrh ** This function must be called before exiting any API function (i.e. 70917435752Sdrh ** returning control to the user) that has called sqlite3_malloc or 71017435752Sdrh ** sqlite3_realloc. 711a3152895Sdrh ** 712a3152895Sdrh ** The returned value is normally a copy of the second argument to this 713a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous 714a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead. 715a3152895Sdrh ** 716a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured, 717a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode()) 718a3152895Sdrh ** is set to SQLITE_NOMEM. 719a3152895Sdrh */ 720a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){ 721a1644fd8Sdanielk1977 /* If the db handle is not NULL, then we must hold the connection handle 722a1644fd8Sdanielk1977 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 723a1644fd8Sdanielk1977 ** is unsafe, as is the call to sqlite3Error(). 724a1644fd8Sdanielk1977 */ 725a1644fd8Sdanielk1977 assert( !db || sqlite3_mutex_held(db->mutex) ); 7261e536953Sdanielk1977 if( db && db->mallocFailed ){ 727a3152895Sdrh sqlite3Error(db, SQLITE_NOMEM, 0); 72817435752Sdrh db->mallocFailed = 0; 729a3152895Sdrh rc = SQLITE_NOMEM; 730a3152895Sdrh } 731a3152895Sdrh return rc & (db ? db->errMask : 0xff); 732a3152895Sdrh } 733