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*7986a71aSdrh ** $Id: malloc.c,v 1.28 2008/07/14 12:38:21 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); 168*7986a71aSdrh 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 } 227f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 228f7141990Sdrh *pp = p; 229f7141990Sdrh return nFull; 230fec00eabSdrh } 231f7141990Sdrh 232f7141990Sdrh /* 233f7141990Sdrh ** Allocate memory. This routine is like sqlite3_malloc() except that it 234f7141990Sdrh ** assumes the memory subsystem has already been initialized. 235f7141990Sdrh */ 236f7141990Sdrh void *sqlite3Malloc(int n){ 237f7141990Sdrh void *p; 238f7141990Sdrh if( n<=0 ){ 239f7141990Sdrh p = 0; 240f7141990Sdrh }else if( sqlite3Config.bMemstat ){ 241f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 242f7141990Sdrh mallocWithAlarm(n, &p); 243fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 244fec00eabSdrh }else{ 245fec00eabSdrh p = sqlite3Config.m.xMalloc(n); 246fec00eabSdrh } 247fec00eabSdrh return p; 248fec00eabSdrh } 249fec00eabSdrh 250fec00eabSdrh /* 251fec00eabSdrh ** This version of the memory allocation is for use by the application. 252fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the 253fec00eabSdrh ** allocation. 254fec00eabSdrh */ 255fec00eabSdrh void *sqlite3_malloc(int n){ 256fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 257fec00eabSdrh if( sqlite3_initialize() ) return 0; 258fec00eabSdrh #endif 259fec00eabSdrh return sqlite3Malloc(n); 260fec00eabSdrh } 261fec00eabSdrh 262fec00eabSdrh /* 263e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from 264facf0307Sdrh ** xScratchMalloc(). We verify this constraint in the single-threaded 265facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation 266e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed. 267e5ae5735Sdrh */ 268e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 269facf0307Sdrh static int scratchAllocOut = 0; 270e5ae5735Sdrh #endif 271e5ae5735Sdrh 272e5ae5735Sdrh 273e5ae5735Sdrh /* 274e5ae5735Sdrh ** Allocate memory that is to be used and released right away. 275e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended 276e5ae5735Sdrh ** for situations where the memory might be held long-term. This 277e5ae5735Sdrh ** routine is intended to get memory to old large transient data 278e5ae5735Sdrh ** structures that would not normally fit on the stack of an 279e5ae5735Sdrh ** embedded processor. 280e5ae5735Sdrh */ 281facf0307Sdrh void *sqlite3ScratchMalloc(int n){ 282e5ae5735Sdrh void *p; 283e5ae5735Sdrh assert( n>0 ); 2849ac3fe97Sdrh 285e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 2869ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 2879ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 2889ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 2899ac3fe97Sdrh ** would be much more complicated.) */ 290facf0307Sdrh assert( scratchAllocOut==0 ); 291e5ae5735Sdrh #endif 2929ac3fe97Sdrh 293f7141990Sdrh if( sqlite3Config.szScratch<n ){ 294f7141990Sdrh goto scratch_overflow; 295f7141990Sdrh }else{ 296e5ae5735Sdrh sqlite3_mutex_enter(mem0.mutex); 297f7141990Sdrh if( mem0.nScratchFree==0 ){ 298f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 299f7141990Sdrh goto scratch_overflow; 300e5ae5735Sdrh }else{ 3019ac3fe97Sdrh int i; 3029ac3fe97Sdrh i = mem0.aScratchFree[--mem0.nScratchFree]; 303f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 3049ac3fe97Sdrh i *= sqlite3Config.szScratch; 305f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 3069ac3fe97Sdrh p = (void*)&((char*)sqlite3Config.pScratch)[i]; 307e5ae5735Sdrh } 308f7141990Sdrh } 309f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 310f7141990Sdrh scratchAllocOut = p!=0; 311f7141990Sdrh #endif 312f7141990Sdrh 313f7141990Sdrh return p; 314f7141990Sdrh 315f7141990Sdrh scratch_overflow: 316f7141990Sdrh if( sqlite3Config.bMemstat ){ 317f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 318f7141990Sdrh n = mallocWithAlarm(n, &p); 319f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); 3209ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 321f7141990Sdrh }else{ 322f7141990Sdrh p = sqlite3Config.m.xMalloc(n); 323f7141990Sdrh } 324f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 325f7141990Sdrh scratchAllocOut = p!=0; 326f7141990Sdrh #endif 327e5ae5735Sdrh return p; 328e5ae5735Sdrh } 329facf0307Sdrh void sqlite3ScratchFree(void *p){ 330e5ae5735Sdrh if( p ){ 3319ac3fe97Sdrh 332e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 3339ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 3349ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 3359ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 3369ac3fe97Sdrh ** would be much more complicated.) */ 337facf0307Sdrh assert( scratchAllocOut==1 ); 338facf0307Sdrh scratchAllocOut = 0; 339e5ae5735Sdrh #endif 3409ac3fe97Sdrh 3419ac3fe97Sdrh if( sqlite3Config.pScratch==0 3429ac3fe97Sdrh || p<sqlite3Config.pScratch 3439ac3fe97Sdrh || p>=(void*)mem0.aScratchFree ){ 344f7141990Sdrh if( sqlite3Config.bMemstat ){ 345f7141990Sdrh int iSize = sqlite3MallocSize(p); 346f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 347f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 348f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 349facf0307Sdrh sqlite3Config.m.xFree(p); 350f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 351f7141990Sdrh }else{ 352f7141990Sdrh sqlite3Config.m.xFree(p); 353f7141990Sdrh } 3549ac3fe97Sdrh }else{ 3559ac3fe97Sdrh int i; 356867d05a0Sdanielk1977 i = (u8 *)p - (u8 *)sqlite3Config.pScratch; 3579ac3fe97Sdrh i /= sqlite3Config.szScratch; 3589ac3fe97Sdrh assert( i>=0 && i<sqlite3Config.nScratch ); 359f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 360f7141990Sdrh assert( mem0.nScratchFree<sqlite3Config.nScratch ); 3619ac3fe97Sdrh mem0.aScratchFree[mem0.nScratchFree++] = i; 362f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 3639ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 3649ac3fe97Sdrh } 365e5ae5735Sdrh } 366e5ae5735Sdrh } 367e5ae5735Sdrh 368e5ae5735Sdrh /* 369f7141990Sdrh ** Allocate memory to be used by the page cache. Make use of the 370f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one 371f7141990Sdrh ** and that memory is of the right size and is not completely 372f7141990Sdrh ** consumed. Otherwise, failover to sqlite3Malloc(). 373facf0307Sdrh */ 374f7141990Sdrh void *sqlite3PageMalloc(int n){ 375f7141990Sdrh void *p; 376f7141990Sdrh assert( n>0 ); 377f7141990Sdrh assert( (n & (n-1))==0 ); 378f7141990Sdrh assert( n>=512 && n<=32768 ); 379f7141990Sdrh 380f7141990Sdrh if( sqlite3Config.szPage<n ){ 381f7141990Sdrh goto page_overflow; 382f7141990Sdrh }else{ 383f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 384f7141990Sdrh if( mem0.nPageFree==0 ){ 385f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 386f7141990Sdrh goto page_overflow; 387f7141990Sdrh }else{ 388f7141990Sdrh int i; 389f7141990Sdrh i = mem0.aPageFree[--mem0.nPageFree]; 390f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 391f7141990Sdrh i *= sqlite3Config.szPage; 392f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); 393f7141990Sdrh p = (void*)&((char*)sqlite3Config.pPage)[i]; 394f7141990Sdrh } 395f7141990Sdrh } 396f7141990Sdrh return p; 397f7141990Sdrh 398f7141990Sdrh page_overflow: 399f7141990Sdrh if( sqlite3Config.bMemstat ){ 400f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 401f7141990Sdrh n = mallocWithAlarm(n, &p); 402f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); 403f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 404f7141990Sdrh }else{ 405f7141990Sdrh p = sqlite3Config.m.xMalloc(n); 406f7141990Sdrh } 407f7141990Sdrh return p; 408f7141990Sdrh } 409f7141990Sdrh void sqlite3PageFree(void *p){ 410f7141990Sdrh if( p ){ 411f7141990Sdrh if( sqlite3Config.pPage==0 412f7141990Sdrh || p<sqlite3Config.pPage 413f7141990Sdrh || p>=(void*)mem0.aPageFree ){ 4144b9507a0Sdanielk1977 /* In this case, the page allocation was obtained from a regular 4154b9507a0Sdanielk1977 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory 4164b9507a0Sdanielk1977 ** "overflow"). Free the block with sqlite3_mem_methods.xFree(). 4174b9507a0Sdanielk1977 */ 418f7141990Sdrh if( sqlite3Config.bMemstat ){ 419f7141990Sdrh int iSize = sqlite3MallocSize(p); 420f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 421f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); 422f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 423f7141990Sdrh sqlite3Config.m.xFree(p); 424f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 425f7141990Sdrh }else{ 426f7141990Sdrh sqlite3Config.m.xFree(p); 427f7141990Sdrh } 428f7141990Sdrh }else{ 4294b9507a0Sdanielk1977 /* The page allocation was allocated from the sqlite3Config.pPage 4304b9507a0Sdanielk1977 ** buffer. In this case all that is add the index of the page in 4314b9507a0Sdanielk1977 ** the sqlite3Config.pPage array to the set of free indexes stored 4324b9507a0Sdanielk1977 ** in the mem0.aPageFree[] array. 4334b9507a0Sdanielk1977 */ 434f7141990Sdrh int i; 435867d05a0Sdanielk1977 i = (u8 *)p - (u8 *)sqlite3Config.pPage; 436f7141990Sdrh i /= sqlite3Config.szPage; 437f7141990Sdrh assert( i>=0 && i<sqlite3Config.nPage ); 438f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 439f7141990Sdrh assert( mem0.nPageFree<sqlite3Config.nPage ); 440f7141990Sdrh mem0.aPageFree[mem0.nPageFree++] = i; 441f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); 442f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 4434b9507a0Sdanielk1977 #ifndef NDEBUG 4444b9507a0Sdanielk1977 /* Assert that a duplicate was not just inserted into aPageFree[]. */ 4454b9507a0Sdanielk1977 for(i=0; i<mem0.nPageFree-1; i++){ 4464b9507a0Sdanielk1977 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] ); 4474b9507a0Sdanielk1977 } 4484b9507a0Sdanielk1977 #endif 449f7141990Sdrh } 450f7141990Sdrh } 451facf0307Sdrh } 452facf0307Sdrh 453facf0307Sdrh /* 454fec00eabSdrh ** Return the size of a memory allocation previously obtained from 455fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc(). 456fec00eabSdrh */ 457fec00eabSdrh int sqlite3MallocSize(void *p){ 458fec00eabSdrh return sqlite3Config.m.xSize(p); 459fec00eabSdrh } 460fec00eabSdrh 461fec00eabSdrh /* 462fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc(). 463fec00eabSdrh */ 464fec00eabSdrh void sqlite3_free(void *p){ 465fec00eabSdrh if( p==0 ) return; 466fec00eabSdrh if( sqlite3Config.bMemstat ){ 467fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 468f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 469fec00eabSdrh sqlite3Config.m.xFree(p); 470fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 471fec00eabSdrh }else{ 472fec00eabSdrh sqlite3Config.m.xFree(p); 473fec00eabSdrh } 474fec00eabSdrh } 475fec00eabSdrh 476fec00eabSdrh /* 477fec00eabSdrh ** Change the size of an existing memory allocation 478fec00eabSdrh */ 479fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){ 480fec00eabSdrh int nOld, nNew; 481fec00eabSdrh void *pNew; 482fec00eabSdrh if( pOld==0 ){ 483fec00eabSdrh return sqlite3Malloc(nBytes); 484fec00eabSdrh } 485fec00eabSdrh if( nBytes<=0 ){ 486fec00eabSdrh sqlite3_free(pOld); 487fec00eabSdrh return 0; 488fec00eabSdrh } 489fec00eabSdrh nOld = sqlite3MallocSize(pOld); 490fec00eabSdrh if( sqlite3Config.bMemstat ){ 491fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 492f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); 493fec00eabSdrh nNew = sqlite3Config.m.xRoundup(nBytes); 494fec00eabSdrh if( nOld==nNew ){ 495fec00eabSdrh pNew = pOld; 496fec00eabSdrh }else{ 497f7141990Sdrh if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 498f7141990Sdrh mem0.alarmThreshold ){ 499fec00eabSdrh sqlite3MallocAlarm(nNew-nOld); 500fec00eabSdrh } 501fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nNew); 502d09414cdSdanielk1977 if( pNew==0 && mem0.alarmCallback ){ 503fec00eabSdrh sqlite3MallocAlarm(nBytes); 504fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nNew); 505fec00eabSdrh } 506fec00eabSdrh if( pNew ){ 507f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 508fec00eabSdrh } 509fec00eabSdrh } 510fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 511fec00eabSdrh }else{ 512fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nBytes); 513fec00eabSdrh } 514fec00eabSdrh return pNew; 515fec00eabSdrh } 516fec00eabSdrh 517fec00eabSdrh /* 518fec00eabSdrh ** The public interface to sqlite3Realloc. Make sure that the memory 519fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc. 520fec00eabSdrh */ 521fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){ 522fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 523fec00eabSdrh if( sqlite3_initialize() ) return 0; 524fec00eabSdrh #endif 525fec00eabSdrh return sqlite3Realloc(pOld, n); 526fec00eabSdrh } 527fec00eabSdrh 528a3152895Sdrh 529a3152895Sdrh /* 53017435752Sdrh ** Allocate and zero memory. 531a3152895Sdrh */ 532fec00eabSdrh void *sqlite3MallocZero(int n){ 533fec00eabSdrh void *p = sqlite3Malloc(n); 534a3152895Sdrh if( p ){ 535a3152895Sdrh memset(p, 0, n); 536a3152895Sdrh } 537a3152895Sdrh return p; 538a3152895Sdrh } 53917435752Sdrh 54017435752Sdrh /* 54117435752Sdrh ** Allocate and zero memory. If the allocation fails, make 54217435752Sdrh ** the mallocFailed flag in the connection pointer. 54317435752Sdrh */ 544fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){ 545a1644fd8Sdanielk1977 void *p = sqlite3DbMallocRaw(db, n); 54617435752Sdrh if( p ){ 54717435752Sdrh memset(p, 0, n); 54817435752Sdrh } 54917435752Sdrh return p; 55017435752Sdrh } 55117435752Sdrh 55217435752Sdrh /* 55317435752Sdrh ** Allocate and zero memory. If the allocation fails, make 55417435752Sdrh ** the mallocFailed flag in the connection pointer. 55517435752Sdrh */ 556fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){ 557a1644fd8Sdanielk1977 void *p = 0; 558a1644fd8Sdanielk1977 if( !db || db->mallocFailed==0 ){ 559fec00eabSdrh p = sqlite3Malloc(n); 560f3a65f7eSdrh if( !p && db ){ 56117435752Sdrh db->mallocFailed = 1; 56217435752Sdrh } 563a1644fd8Sdanielk1977 } 56417435752Sdrh return p; 56517435752Sdrh } 56617435752Sdrh 56726783a58Sdanielk1977 /* 56826783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the 56926783a58Sdanielk1977 ** resize fails, set the mallocFailed flag inthe connection object. 57026783a58Sdanielk1977 */ 571a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 572a1644fd8Sdanielk1977 void *pNew = 0; 573a1644fd8Sdanielk1977 if( db->mallocFailed==0 ){ 574a1644fd8Sdanielk1977 pNew = sqlite3_realloc(p, n); 575a1644fd8Sdanielk1977 if( !pNew ){ 576a1644fd8Sdanielk1977 db->mallocFailed = 1; 577a1644fd8Sdanielk1977 } 578a1644fd8Sdanielk1977 } 579a1644fd8Sdanielk1977 return pNew; 580a1644fd8Sdanielk1977 } 581a1644fd8Sdanielk1977 58217435752Sdrh /* 58317435752Sdrh ** Attempt to reallocate p. If the reallocation fails, then free p 58417435752Sdrh ** and set the mallocFailed flag in the database connection. 58517435752Sdrh */ 58617435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 587a3152895Sdrh void *pNew; 588a1644fd8Sdanielk1977 pNew = sqlite3DbRealloc(db, p, n); 589a3152895Sdrh if( !pNew ){ 5901e536953Sdanielk1977 sqlite3_free(p); 591a3152895Sdrh } 592a3152895Sdrh return pNew; 593a3152895Sdrh } 594a3152895Sdrh 595a3152895Sdrh /* 596a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These 597a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 598a3152895Sdrh ** is because when memory debugging is turned on, these two functions are 599a3152895Sdrh ** called via macros that record the current file and line number in the 600a3152895Sdrh ** ThreadData structure. 601a3152895Sdrh */ 602a3152895Sdrh char *sqlite3StrDup(const char *z){ 603a3152895Sdrh char *zNew; 604a3152895Sdrh int n; 605a3152895Sdrh if( z==0 ) return 0; 606a3152895Sdrh n = strlen(z)+1; 607e5ae5735Sdrh zNew = sqlite3Malloc(n); 608a3152895Sdrh if( zNew ) memcpy(zNew, z, n); 609a3152895Sdrh return zNew; 610a3152895Sdrh } 611a3152895Sdrh char *sqlite3StrNDup(const char *z, int n){ 612a3152895Sdrh char *zNew; 613a3152895Sdrh if( z==0 ) return 0; 614e5ae5735Sdrh zNew = sqlite3Malloc(n+1); 615a3152895Sdrh if( zNew ){ 616a3152895Sdrh memcpy(zNew, z, n); 617a3152895Sdrh zNew[n] = 0; 618a3152895Sdrh } 619a3152895Sdrh return zNew; 620a3152895Sdrh } 621a3152895Sdrh 6221e536953Sdanielk1977 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 6231e536953Sdanielk1977 char *zNew = sqlite3StrDup(z); 6241e536953Sdanielk1977 if( z && !zNew ){ 6251e536953Sdanielk1977 db->mallocFailed = 1; 6261e536953Sdanielk1977 } 6271e536953Sdanielk1977 return zNew; 6281e536953Sdanielk1977 } 6291e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 6301e536953Sdanielk1977 char *zNew = sqlite3StrNDup(z, n); 6311e536953Sdanielk1977 if( z && !zNew ){ 6321e536953Sdanielk1977 db->mallocFailed = 1; 6331e536953Sdanielk1977 } 6341e536953Sdanielk1977 return zNew; 6351e536953Sdanielk1977 } 6361e536953Sdanielk1977 637a3152895Sdrh /* 638f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows. 639f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz 640f089aa45Sdrh ** point to that string. 641a3152895Sdrh */ 642f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ 643a3152895Sdrh va_list ap; 644f089aa45Sdrh char *z; 645a3152895Sdrh 646f089aa45Sdrh va_start(ap, zFormat); 647f089aa45Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 648a3152895Sdrh va_end(ap); 6491e536953Sdanielk1977 sqlite3_free(*pz); 650f089aa45Sdrh *pz = z; 651a3152895Sdrh } 652a3152895Sdrh 653a3152895Sdrh 654a3152895Sdrh /* 655a3152895Sdrh ** This function must be called before exiting any API function (i.e. 65617435752Sdrh ** returning control to the user) that has called sqlite3_malloc or 65717435752Sdrh ** sqlite3_realloc. 658a3152895Sdrh ** 659a3152895Sdrh ** The returned value is normally a copy of the second argument to this 660a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous 661a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead. 662a3152895Sdrh ** 663a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured, 664a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode()) 665a3152895Sdrh ** is set to SQLITE_NOMEM. 666a3152895Sdrh */ 667a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){ 668a1644fd8Sdanielk1977 /* If the db handle is not NULL, then we must hold the connection handle 669a1644fd8Sdanielk1977 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 670a1644fd8Sdanielk1977 ** is unsafe, as is the call to sqlite3Error(). 671a1644fd8Sdanielk1977 */ 672a1644fd8Sdanielk1977 assert( !db || sqlite3_mutex_held(db->mutex) ); 6731e536953Sdanielk1977 if( db && db->mallocFailed ){ 674a3152895Sdrh sqlite3Error(db, SQLITE_NOMEM, 0); 67517435752Sdrh db->mallocFailed = 0; 676a3152895Sdrh rc = SQLITE_NOMEM; 677a3152895Sdrh } 678a3152895Sdrh return rc & (db ? db->errMask : 0xff); 679a3152895Sdrh } 680