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*66e80084Sdrh ** $Id: malloc.c,v 1.52 2008/12/16 13:46:30 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, 2862c14b34Sdanielk1977 sqlite3_int64 NotUsed2, 29153c62c4Sdrh int allocSize 30b21c8cd4Sdrh ){ 3162c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 32b21c8cd4Sdrh sqlite3_release_memory(allocSize); 33b21c8cd4Sdrh } 34b21c8cd4Sdrh 35b21c8cd4Sdrh /* 368468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or 378468024dSdanielk1977 ** negative value indicates no limit. 38a3152895Sdrh */ 39a3152895Sdrh void sqlite3_soft_heap_limit(int n){ 40b21c8cd4Sdrh sqlite3_uint64 iLimit; 41b21c8cd4Sdrh int overage; 42b21c8cd4Sdrh if( n<0 ){ 43b21c8cd4Sdrh iLimit = 0; 44b21c8cd4Sdrh }else{ 45b21c8cd4Sdrh iLimit = n; 46a3152895Sdrh } 479ac3fe97Sdrh sqlite3_initialize(); 48b21c8cd4Sdrh if( iLimit>0 ){ 494a27a286Sshane sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit); 50b21c8cd4Sdrh }else{ 514a27a286Sshane sqlite3MemoryAlarm(0, 0, 0); 52b21c8cd4Sdrh } 531bd10f8aSdrh overage = (int)(sqlite3_memory_used() - (i64)n); 54b21c8cd4Sdrh if( overage>0 ){ 55b21c8cd4Sdrh sqlite3_release_memory(overage); 56b21c8cd4Sdrh } 57a3152895Sdrh } 58a3152895Sdrh 59a3152895Sdrh /* 608468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently 618468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to 628468024dSdanielk1977 ** cache database pages that are not currently in use. 63a3152895Sdrh */ 64a3152895Sdrh int sqlite3_release_memory(int n){ 6586f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 6667e3da7aSdanielk1977 int nRet = 0; 6767e3da7aSdanielk1977 #if 0 6867e3da7aSdanielk1977 nRet += sqlite3VdbeReleaseMemory(n); 6967e3da7aSdanielk1977 #endif 7067e3da7aSdanielk1977 nRet += sqlite3PcacheReleaseMemory(n-nRet); 71dfb316d4Sdanielk1977 return nRet; 721e536953Sdanielk1977 #else 7362c14b34Sdanielk1977 UNUSED_PARAMETER(n); 741e536953Sdanielk1977 return SQLITE_OK; 751e536953Sdanielk1977 #endif 76a3152895Sdrh } 77a3152895Sdrh 78fec00eabSdrh /* 79fec00eabSdrh ** State information local to the memory allocation subsystem. 80fec00eabSdrh */ 815c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global { 8223bf0f41Sdanielk1977 /* Number of free pages for scratch and page-cache memory */ 8323bf0f41Sdanielk1977 u32 nScratchFree; 8423bf0f41Sdanielk1977 u32 nPageFree; 8523bf0f41Sdanielk1977 86fec00eabSdrh sqlite3_mutex *mutex; /* Mutex to serialize access */ 87fec00eabSdrh 88fec00eabSdrh /* 89fec00eabSdrh ** The alarm callback and its arguments. The mem0.mutex lock will 90fec00eabSdrh ** be held while the callback is running. Recursive calls into 91fec00eabSdrh ** the memory subsystem are allowed, but no new callbacks will be 92fec00eabSdrh ** issued. The alarmBusy variable is set to prevent recursive 93fec00eabSdrh ** callbacks. 94fec00eabSdrh */ 95fec00eabSdrh sqlite3_int64 alarmThreshold; 96fec00eabSdrh void (*alarmCallback)(void*, sqlite3_int64,int); 97fec00eabSdrh void *alarmArg; 98fec00eabSdrh int alarmBusy; 99fec00eabSdrh 100fec00eabSdrh /* 101075c23afSdanielk1977 ** Pointers to the end of sqlite3GlobalConfig.pScratch and 102075c23afSdanielk1977 ** sqlite3GlobalConfig.pPage to a block of memory that records 1039ac3fe97Sdrh ** which pages are available. 1049ac3fe97Sdrh */ 1059ac3fe97Sdrh u32 *aScratchFree; 1069ac3fe97Sdrh u32 *aPageFree; 107cdcfe95cSdanielk1977 } mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 }; 1085c8f8587Sdanielk1977 1095c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0) 110fec00eabSdrh 111fec00eabSdrh /* 112fec00eabSdrh ** Initialize the memory allocation subsystem. 113fec00eabSdrh */ 114fec00eabSdrh int sqlite3MallocInit(void){ 115075c23afSdanielk1977 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 116fec00eabSdrh sqlite3MemSetDefault(); 117fec00eabSdrh } 118fec00eabSdrh memset(&mem0, 0, sizeof(mem0)); 119075c23afSdanielk1977 if( sqlite3GlobalConfig.bCoreMutex ){ 12059f8c08eSdanielk1977 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 121fec00eabSdrh } 122075c23afSdanielk1977 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 123075c23afSdanielk1977 && sqlite3GlobalConfig.nScratch>=0 ){ 1249ac3fe97Sdrh int i; 125*66e80084Sdrh sqlite3GlobalConfig.szScratch = (sqlite3GlobalConfig.szScratch - 4) & ~7; 126075c23afSdanielk1977 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch) 127075c23afSdanielk1977 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch]; 128075c23afSdanielk1977 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; } 129075c23afSdanielk1977 mem0.nScratchFree = sqlite3GlobalConfig.nScratch; 1309ac3fe97Sdrh }else{ 131075c23afSdanielk1977 sqlite3GlobalConfig.pScratch = 0; 132075c23afSdanielk1977 sqlite3GlobalConfig.szScratch = 0; 1339ac3fe97Sdrh } 134075c23afSdanielk1977 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512 135075c23afSdanielk1977 && sqlite3GlobalConfig.nPage>=1 ){ 1369ac3fe97Sdrh int i; 1370a60a384Sdrh int overhead; 138*66e80084Sdrh int sz = sqlite3GlobalConfig.szPage & ~7; 139075c23afSdanielk1977 int n = sqlite3GlobalConfig.nPage; 1400a60a384Sdrh overhead = (4*n + sz - 1)/sz; 141075c23afSdanielk1977 sqlite3GlobalConfig.nPage -= overhead; 142075c23afSdanielk1977 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage) 143075c23afSdanielk1977 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage]; 144075c23afSdanielk1977 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; } 145075c23afSdanielk1977 mem0.nPageFree = sqlite3GlobalConfig.nPage; 1469ac3fe97Sdrh }else{ 147075c23afSdanielk1977 sqlite3GlobalConfig.pPage = 0; 148075c23afSdanielk1977 sqlite3GlobalConfig.szPage = 0; 1499ac3fe97Sdrh } 150075c23afSdanielk1977 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 151fec00eabSdrh } 152fec00eabSdrh 153fec00eabSdrh /* 154fec00eabSdrh ** Deinitialize the memory allocation subsystem. 155fec00eabSdrh */ 156fec00eabSdrh void sqlite3MallocEnd(void){ 157075c23afSdanielk1977 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 1589ac3fe97Sdrh memset(&mem0, 0, sizeof(mem0)); 159fec00eabSdrh } 160fec00eabSdrh 161fec00eabSdrh /* 162fec00eabSdrh ** Return the amount of memory currently checked out. 163fec00eabSdrh */ 164fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){ 165f7141990Sdrh int n, mx; 166c376a198Sdrh sqlite3_int64 res; 167f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); 168c376a198Sdrh res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ 169c376a198Sdrh return res; 170fec00eabSdrh } 171fec00eabSdrh 172fec00eabSdrh /* 173fec00eabSdrh ** Return the maximum amount of memory that has ever been 174fec00eabSdrh ** checked out since either the beginning of this process 175fec00eabSdrh ** or since the most recent reset. 176fec00eabSdrh */ 177fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 178f7141990Sdrh int n, mx; 179c376a198Sdrh sqlite3_int64 res; 180f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); 1817986a71aSdrh res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ 182c376a198Sdrh return res; 183fec00eabSdrh } 184fec00eabSdrh 185fec00eabSdrh /* 186fec00eabSdrh ** Change the alarm callback 187fec00eabSdrh */ 1884a27a286Sshane int sqlite3MemoryAlarm( 189fec00eabSdrh void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 190fec00eabSdrh void *pArg, 191fec00eabSdrh sqlite3_int64 iThreshold 192fec00eabSdrh ){ 193fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 194fec00eabSdrh mem0.alarmCallback = xCallback; 195fec00eabSdrh mem0.alarmArg = pArg; 196fec00eabSdrh mem0.alarmThreshold = iThreshold; 197fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 198fec00eabSdrh return SQLITE_OK; 199fec00eabSdrh } 200fec00eabSdrh 201eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 202fec00eabSdrh /* 2034a27a286Sshane ** Deprecated external interface. Internal/core SQLite code 2044a27a286Sshane ** should call sqlite3MemoryAlarm. 2054a27a286Sshane */ 2064a27a286Sshane int sqlite3_memory_alarm( 2074a27a286Sshane void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 2084a27a286Sshane void *pArg, 2094a27a286Sshane sqlite3_int64 iThreshold 2104a27a286Sshane ){ 2114a27a286Sshane return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); 2124a27a286Sshane } 213eec556d3Sshane #endif 2144a27a286Sshane 2154a27a286Sshane /* 216fec00eabSdrh ** Trigger the alarm 217fec00eabSdrh */ 218fec00eabSdrh static void sqlite3MallocAlarm(int nByte){ 219fec00eabSdrh void (*xCallback)(void*,sqlite3_int64,int); 220fec00eabSdrh sqlite3_int64 nowUsed; 221fec00eabSdrh void *pArg; 222fec00eabSdrh if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; 223fec00eabSdrh mem0.alarmBusy = 1; 224fec00eabSdrh xCallback = mem0.alarmCallback; 225f7141990Sdrh nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 226fec00eabSdrh pArg = mem0.alarmArg; 227fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 228fec00eabSdrh xCallback(pArg, nowUsed, nByte); 229fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 230fec00eabSdrh mem0.alarmBusy = 0; 231fec00eabSdrh } 232fec00eabSdrh 233fec00eabSdrh /* 234f7141990Sdrh ** Do a memory allocation with statistics and alarms. Assume the 235f7141990Sdrh ** lock is already held. 236fec00eabSdrh */ 237f7141990Sdrh static int mallocWithAlarm(int n, void **pp){ 238fec00eabSdrh int nFull; 239f7141990Sdrh void *p; 240f7141990Sdrh assert( sqlite3_mutex_held(mem0.mutex) ); 241075c23afSdanielk1977 nFull = sqlite3GlobalConfig.m.xRoundup(n); 242f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 243f7141990Sdrh if( mem0.alarmCallback!=0 ){ 244f7141990Sdrh int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 245f7141990Sdrh if( nUsed+nFull >= mem0.alarmThreshold ){ 246fec00eabSdrh sqlite3MallocAlarm(nFull); 247fec00eabSdrh } 248f7141990Sdrh } 249075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(nFull); 250d09414cdSdanielk1977 if( p==0 && mem0.alarmCallback ){ 251fec00eabSdrh sqlite3MallocAlarm(nFull); 252075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(nFull); 253fec00eabSdrh } 254c702c7ccSdrh if( p ){ 255c702c7ccSdrh nFull = sqlite3MallocSize(p); 256c702c7ccSdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 257c702c7ccSdrh } 258f7141990Sdrh *pp = p; 259f7141990Sdrh return nFull; 260fec00eabSdrh } 261f7141990Sdrh 262f7141990Sdrh /* 263f7141990Sdrh ** Allocate memory. This routine is like sqlite3_malloc() except that it 264f7141990Sdrh ** assumes the memory subsystem has already been initialized. 265f7141990Sdrh */ 266f7141990Sdrh void *sqlite3Malloc(int n){ 267f7141990Sdrh void *p; 268f7141990Sdrh if( n<=0 ){ 269f7141990Sdrh p = 0; 270075c23afSdanielk1977 }else if( sqlite3GlobalConfig.bMemstat ){ 271f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 272f7141990Sdrh mallocWithAlarm(n, &p); 273fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 274fec00eabSdrh }else{ 275075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(n); 276fec00eabSdrh } 277fec00eabSdrh return p; 278fec00eabSdrh } 279fec00eabSdrh 280fec00eabSdrh /* 281fec00eabSdrh ** This version of the memory allocation is for use by the application. 282fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the 283fec00eabSdrh ** allocation. 284fec00eabSdrh */ 285fec00eabSdrh void *sqlite3_malloc(int n){ 286fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 287fec00eabSdrh if( sqlite3_initialize() ) return 0; 288fec00eabSdrh #endif 289fec00eabSdrh return sqlite3Malloc(n); 290fec00eabSdrh } 291fec00eabSdrh 292fec00eabSdrh /* 293e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from 294facf0307Sdrh ** xScratchMalloc(). We verify this constraint in the single-threaded 295facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation 296e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed. 297e5ae5735Sdrh */ 298e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 299facf0307Sdrh static int scratchAllocOut = 0; 300e5ae5735Sdrh #endif 301e5ae5735Sdrh 302e5ae5735Sdrh 303e5ae5735Sdrh /* 304e5ae5735Sdrh ** Allocate memory that is to be used and released right away. 305e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended 306e5ae5735Sdrh ** for situations where the memory might be held long-term. This 307e5ae5735Sdrh ** routine is intended to get memory to old large transient data 308e5ae5735Sdrh ** structures that would not normally fit on the stack of an 309e5ae5735Sdrh ** embedded processor. 310e5ae5735Sdrh */ 311facf0307Sdrh void *sqlite3ScratchMalloc(int n){ 312e5ae5735Sdrh void *p; 313e5ae5735Sdrh assert( n>0 ); 3149ac3fe97Sdrh 315e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 3169ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 3179ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 3189ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 3199ac3fe97Sdrh ** would be much more complicated.) */ 320facf0307Sdrh assert( scratchAllocOut==0 ); 321e5ae5735Sdrh #endif 3229ac3fe97Sdrh 323075c23afSdanielk1977 if( sqlite3GlobalConfig.szScratch<n ){ 324f7141990Sdrh goto scratch_overflow; 325f7141990Sdrh }else{ 326e5ae5735Sdrh sqlite3_mutex_enter(mem0.mutex); 327f7141990Sdrh if( mem0.nScratchFree==0 ){ 328f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 329f7141990Sdrh goto scratch_overflow; 330e5ae5735Sdrh }else{ 3319ac3fe97Sdrh int i; 3329ac3fe97Sdrh i = mem0.aScratchFree[--mem0.nScratchFree]; 333075c23afSdanielk1977 i *= sqlite3GlobalConfig.szScratch; 334f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 335e50135e2Sdrh sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 3368183e339Sdanielk1977 sqlite3_mutex_leave(mem0.mutex); 337075c23afSdanielk1977 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; 338*66e80084Sdrh assert( ((p - (void*)0) & 7)==0 ); 339e5ae5735Sdrh } 340f7141990Sdrh } 341f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 342f7141990Sdrh scratchAllocOut = p!=0; 343f7141990Sdrh #endif 344f7141990Sdrh 345f7141990Sdrh return p; 346f7141990Sdrh 347f7141990Sdrh scratch_overflow: 348075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 349f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 350e50135e2Sdrh sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 351f7141990Sdrh n = mallocWithAlarm(n, &p); 352f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); 3539ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 354f7141990Sdrh }else{ 355075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(n); 356f7141990Sdrh } 357f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 358f7141990Sdrh scratchAllocOut = p!=0; 359f7141990Sdrh #endif 360e5ae5735Sdrh return p; 361e5ae5735Sdrh } 362facf0307Sdrh void sqlite3ScratchFree(void *p){ 363e5ae5735Sdrh if( p ){ 3649ac3fe97Sdrh 365e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 3669ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 3679ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 3689ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 3699ac3fe97Sdrh ** would be much more complicated.) */ 370facf0307Sdrh assert( scratchAllocOut==1 ); 371facf0307Sdrh scratchAllocOut = 0; 372e5ae5735Sdrh #endif 3739ac3fe97Sdrh 374075c23afSdanielk1977 if( sqlite3GlobalConfig.pScratch==0 375075c23afSdanielk1977 || p<sqlite3GlobalConfig.pScratch 3769ac3fe97Sdrh || p>=(void*)mem0.aScratchFree ){ 377075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 378f7141990Sdrh int iSize = sqlite3MallocSize(p); 379f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 380f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 381f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 382075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 383f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 384f7141990Sdrh }else{ 385075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 386f7141990Sdrh } 3879ac3fe97Sdrh }else{ 3889ac3fe97Sdrh int i; 3891bd10f8aSdrh i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch); 390075c23afSdanielk1977 i /= sqlite3GlobalConfig.szScratch; 391075c23afSdanielk1977 assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); 392f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 39300e13613Sdanielk1977 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); 3949ac3fe97Sdrh mem0.aScratchFree[mem0.nScratchFree++] = i; 395f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 3969ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 3979ac3fe97Sdrh } 398e5ae5735Sdrh } 399e5ae5735Sdrh } 400e5ae5735Sdrh 401e5ae5735Sdrh /* 402f7141990Sdrh ** Allocate memory to be used by the page cache. Make use of the 403f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one 404f7141990Sdrh ** and that memory is of the right size and is not completely 405f7141990Sdrh ** consumed. Otherwise, failover to sqlite3Malloc(). 406facf0307Sdrh */ 4078c0a791aSdanielk1977 #if 0 408f7141990Sdrh void *sqlite3PageMalloc(int n){ 409f7141990Sdrh void *p; 410f7141990Sdrh assert( n>0 ); 411f7141990Sdrh assert( (n & (n-1))==0 ); 412f7141990Sdrh assert( n>=512 && n<=32768 ); 413f7141990Sdrh 414075c23afSdanielk1977 if( sqlite3GlobalConfig.szPage<n ){ 415f7141990Sdrh goto page_overflow; 416f7141990Sdrh }else{ 417f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 418f7141990Sdrh if( mem0.nPageFree==0 ){ 419f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 420f7141990Sdrh goto page_overflow; 421f7141990Sdrh }else{ 422f7141990Sdrh int i; 423f7141990Sdrh i = mem0.aPageFree[--mem0.nPageFree]; 424f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 425075c23afSdanielk1977 i *= sqlite3GlobalConfig.szPage; 426e50135e2Sdrh sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n); 427f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); 428075c23afSdanielk1977 p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i]; 429f7141990Sdrh } 430f7141990Sdrh } 431f7141990Sdrh return p; 432f7141990Sdrh 433f7141990Sdrh page_overflow: 434075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 435f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 436e50135e2Sdrh sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n); 437f7141990Sdrh n = mallocWithAlarm(n, &p); 438f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); 439f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 440f7141990Sdrh }else{ 441075c23afSdanielk1977 p = sqlite3GlobalConfig.m.xMalloc(n); 442f7141990Sdrh } 443f7141990Sdrh return p; 444f7141990Sdrh } 445f7141990Sdrh void sqlite3PageFree(void *p){ 446f7141990Sdrh if( p ){ 447075c23afSdanielk1977 if( sqlite3GlobalConfig.pPage==0 448075c23afSdanielk1977 || p<sqlite3GlobalConfig.pPage 449f7141990Sdrh || p>=(void*)mem0.aPageFree ){ 4504b9507a0Sdanielk1977 /* In this case, the page allocation was obtained from a regular 4514b9507a0Sdanielk1977 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory 4524b9507a0Sdanielk1977 ** "overflow"). Free the block with sqlite3_mem_methods.xFree(). 4534b9507a0Sdanielk1977 */ 454075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 455f7141990Sdrh int iSize = sqlite3MallocSize(p); 456f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 457f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); 458f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 459075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 460f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 461f7141990Sdrh }else{ 462075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 463f7141990Sdrh } 464f7141990Sdrh }else{ 465075c23afSdanielk1977 /* The page allocation was allocated from the sqlite3GlobalConfig.pPage 4664b9507a0Sdanielk1977 ** buffer. In this case all that is add the index of the page in 467075c23afSdanielk1977 ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored 4684b9507a0Sdanielk1977 ** in the mem0.aPageFree[] array. 4694b9507a0Sdanielk1977 */ 470f7141990Sdrh int i; 471075c23afSdanielk1977 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage; 472075c23afSdanielk1977 i /= sqlite3GlobalConfig.szPage; 473075c23afSdanielk1977 assert( i>=0 && i<sqlite3GlobalConfig.nPage ); 474f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 475075c23afSdanielk1977 assert( mem0.nPageFree<sqlite3GlobalConfig.nPage ); 476f7141990Sdrh mem0.aPageFree[mem0.nPageFree++] = i; 477f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); 478f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 4795f4bcf15Sdrh #if !defined(NDEBUG) && 0 4804b9507a0Sdanielk1977 /* Assert that a duplicate was not just inserted into aPageFree[]. */ 4814b9507a0Sdanielk1977 for(i=0; i<mem0.nPageFree-1; i++){ 4824b9507a0Sdanielk1977 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] ); 4834b9507a0Sdanielk1977 } 4844b9507a0Sdanielk1977 #endif 485f7141990Sdrh } 486f7141990Sdrh } 487facf0307Sdrh } 4888c0a791aSdanielk1977 #endif 489facf0307Sdrh 490facf0307Sdrh /* 491633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db 492633e6d57Sdrh */ 4934150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE 494633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){ 495633e6d57Sdrh return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; 496633e6d57Sdrh } 4974150ebf8Sdrh #else 4984150ebf8Sdrh #define isLookaside(A,B) 0 4994150ebf8Sdrh #endif 500633e6d57Sdrh 501633e6d57Sdrh /* 502fec00eabSdrh ** Return the size of a memory allocation previously obtained from 503fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc(). 504fec00eabSdrh */ 505fec00eabSdrh int sqlite3MallocSize(void *p){ 506075c23afSdanielk1977 return sqlite3GlobalConfig.m.xSize(p); 507fec00eabSdrh } 508633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){ 5096a1e071fSdrh if( p==0 ){ 5106a1e071fSdrh return 0; 5116a1e071fSdrh }else if( isLookaside(db, p) ){ 512633e6d57Sdrh return db->lookaside.sz; 513633e6d57Sdrh }else{ 514075c23afSdanielk1977 return sqlite3GlobalConfig.m.xSize(p); 515633e6d57Sdrh } 516633e6d57Sdrh } 517fec00eabSdrh 518fec00eabSdrh /* 519fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc(). 520fec00eabSdrh */ 521fec00eabSdrh void sqlite3_free(void *p){ 522fec00eabSdrh if( p==0 ) return; 523075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 524fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 525f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 526075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 527fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 528fec00eabSdrh }else{ 529075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p); 530fec00eabSdrh } 531fec00eabSdrh } 532fec00eabSdrh 533fec00eabSdrh /* 534633e6d57Sdrh ** Free memory that might be associated with a particular database 535633e6d57Sdrh ** connection. 536633e6d57Sdrh */ 537633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){ 538633e6d57Sdrh if( isLookaside(db, p) ){ 539633e6d57Sdrh LookasideSlot *pBuf = (LookasideSlot*)p; 540633e6d57Sdrh pBuf->pNext = db->lookaside.pFree; 541633e6d57Sdrh db->lookaside.pFree = pBuf; 542633e6d57Sdrh db->lookaside.nOut--; 543633e6d57Sdrh }else{ 544633e6d57Sdrh sqlite3_free(p); 545633e6d57Sdrh } 546633e6d57Sdrh } 547633e6d57Sdrh 548633e6d57Sdrh /* 549fec00eabSdrh ** Change the size of an existing memory allocation 550fec00eabSdrh */ 551fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){ 552fec00eabSdrh int nOld, nNew; 553fec00eabSdrh void *pNew; 554fec00eabSdrh if( pOld==0 ){ 555fec00eabSdrh return sqlite3Malloc(nBytes); 556fec00eabSdrh } 557fec00eabSdrh if( nBytes<=0 ){ 558fec00eabSdrh sqlite3_free(pOld); 559fec00eabSdrh return 0; 560fec00eabSdrh } 561fec00eabSdrh nOld = sqlite3MallocSize(pOld); 562075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){ 563fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 564f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); 565075c23afSdanielk1977 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); 566fec00eabSdrh if( nOld==nNew ){ 567fec00eabSdrh pNew = pOld; 568fec00eabSdrh }else{ 569f7141990Sdrh if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 570f7141990Sdrh mem0.alarmThreshold ){ 571fec00eabSdrh sqlite3MallocAlarm(nNew-nOld); 572fec00eabSdrh } 573075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 574d09414cdSdanielk1977 if( pNew==0 && mem0.alarmCallback ){ 575fec00eabSdrh sqlite3MallocAlarm(nBytes); 576075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 577fec00eabSdrh } 578fec00eabSdrh if( pNew ){ 579c702c7ccSdrh nNew = sqlite3MallocSize(pNew); 580f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 581fec00eabSdrh } 582fec00eabSdrh } 583fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 584fec00eabSdrh }else{ 585075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes); 586fec00eabSdrh } 587fec00eabSdrh return pNew; 588fec00eabSdrh } 589fec00eabSdrh 590fec00eabSdrh /* 591fec00eabSdrh ** The public interface to sqlite3Realloc. Make sure that the memory 592fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc. 593fec00eabSdrh */ 594fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){ 595fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 596fec00eabSdrh if( sqlite3_initialize() ) return 0; 597fec00eabSdrh #endif 598fec00eabSdrh return sqlite3Realloc(pOld, n); 599fec00eabSdrh } 600fec00eabSdrh 601a3152895Sdrh 602a3152895Sdrh /* 60317435752Sdrh ** Allocate and zero memory. 604a3152895Sdrh */ 605fec00eabSdrh void *sqlite3MallocZero(int n){ 606fec00eabSdrh void *p = sqlite3Malloc(n); 607a3152895Sdrh if( p ){ 608a3152895Sdrh memset(p, 0, n); 609a3152895Sdrh } 610a3152895Sdrh return p; 611a3152895Sdrh } 61217435752Sdrh 61317435752Sdrh /* 61417435752Sdrh ** Allocate and zero memory. If the allocation fails, make 61517435752Sdrh ** the mallocFailed flag in the connection pointer. 61617435752Sdrh */ 617fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){ 618a1644fd8Sdanielk1977 void *p = sqlite3DbMallocRaw(db, n); 61917435752Sdrh if( p ){ 62017435752Sdrh memset(p, 0, n); 62117435752Sdrh } 62217435752Sdrh return p; 62317435752Sdrh } 62417435752Sdrh 62517435752Sdrh /* 62617435752Sdrh ** Allocate and zero memory. If the allocation fails, make 62717435752Sdrh ** the mallocFailed flag in the connection pointer. 628ddecae79Sdrh ** 629ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc 630ddecae79Sdrh ** failure on the same database connection) then always return 0. 631ddecae79Sdrh ** Hence for a particular database connection, once malloc starts 632ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset. 633ddecae79Sdrh ** This is an important assumption. There are many places in the 634ddecae79Sdrh ** code that do things like this: 635ddecae79Sdrh ** 636ddecae79Sdrh ** int *a = (int*)sqlite3DbMallocRaw(db, 100); 637ddecae79Sdrh ** int *b = (int*)sqlite3DbMallocRaw(db, 200); 638ddecae79Sdrh ** if( b ) a[10] = 9; 639ddecae79Sdrh ** 640ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed 641ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too. 64217435752Sdrh */ 643fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){ 644633e6d57Sdrh void *p; 6454150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE 646633e6d57Sdrh if( db ){ 647633e6d57Sdrh LookasideSlot *pBuf; 648633e6d57Sdrh if( db->mallocFailed ){ 649633e6d57Sdrh return 0; 650633e6d57Sdrh } 651633e6d57Sdrh if( db->lookaside.bEnabled && n<=db->lookaside.sz 652633e6d57Sdrh && (pBuf = db->lookaside.pFree)!=0 ){ 653633e6d57Sdrh db->lookaside.pFree = pBuf->pNext; 654633e6d57Sdrh db->lookaside.nOut++; 655633e6d57Sdrh if( db->lookaside.nOut>db->lookaside.mxOut ){ 656633e6d57Sdrh db->lookaside.mxOut = db->lookaside.nOut; 657633e6d57Sdrh } 658633e6d57Sdrh return (void*)pBuf; 659633e6d57Sdrh } 660633e6d57Sdrh } 661ddecae79Sdrh #else 662ddecae79Sdrh if( db && db->mallocFailed ){ 663ddecae79Sdrh return 0; 664ddecae79Sdrh } 6654150ebf8Sdrh #endif 666fec00eabSdrh p = sqlite3Malloc(n); 667f3a65f7eSdrh if( !p && db ){ 66817435752Sdrh db->mallocFailed = 1; 66917435752Sdrh } 67017435752Sdrh return p; 67117435752Sdrh } 67217435752Sdrh 67326783a58Sdanielk1977 /* 67426783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the 67526783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object. 67626783a58Sdanielk1977 */ 677a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 678a1644fd8Sdanielk1977 void *pNew = 0; 679a1644fd8Sdanielk1977 if( db->mallocFailed==0 ){ 680633e6d57Sdrh if( p==0 ){ 681633e6d57Sdrh return sqlite3DbMallocRaw(db, n); 682633e6d57Sdrh } 683633e6d57Sdrh if( isLookaside(db, p) ){ 684633e6d57Sdrh if( n<=db->lookaside.sz ){ 685633e6d57Sdrh return p; 686633e6d57Sdrh } 687633e6d57Sdrh pNew = sqlite3DbMallocRaw(db, n); 688633e6d57Sdrh if( pNew ){ 689633e6d57Sdrh memcpy(pNew, p, db->lookaside.sz); 690633e6d57Sdrh sqlite3DbFree(db, p); 691633e6d57Sdrh } 692633e6d57Sdrh }else{ 693a1644fd8Sdanielk1977 pNew = sqlite3_realloc(p, n); 694a1644fd8Sdanielk1977 if( !pNew ){ 695a1644fd8Sdanielk1977 db->mallocFailed = 1; 696a1644fd8Sdanielk1977 } 697a1644fd8Sdanielk1977 } 698633e6d57Sdrh } 699a1644fd8Sdanielk1977 return pNew; 700a1644fd8Sdanielk1977 } 701a1644fd8Sdanielk1977 70217435752Sdrh /* 70317435752Sdrh ** Attempt to reallocate p. If the reallocation fails, then free p 70417435752Sdrh ** and set the mallocFailed flag in the database connection. 70517435752Sdrh */ 70617435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 707a3152895Sdrh void *pNew; 708a1644fd8Sdanielk1977 pNew = sqlite3DbRealloc(db, p, n); 709a3152895Sdrh if( !pNew ){ 710633e6d57Sdrh sqlite3DbFree(db, p); 711a3152895Sdrh } 712a3152895Sdrh return pNew; 713a3152895Sdrh } 714a3152895Sdrh 715a3152895Sdrh /* 716a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These 717a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 718a3152895Sdrh ** is because when memory debugging is turned on, these two functions are 719a3152895Sdrh ** called via macros that record the current file and line number in the 720a3152895Sdrh ** ThreadData structure. 721a3152895Sdrh */ 722633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 723a3152895Sdrh char *zNew; 724633e6d57Sdrh size_t n; 725633e6d57Sdrh if( z==0 ){ 726633e6d57Sdrh return 0; 727a3152895Sdrh } 728ea678832Sdrh n = (db ? sqlite3Strlen(db, z) : sqlite3Strlen30(z))+1; 729633e6d57Sdrh assert( (n&0x7fffffff)==n ); 730633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, (int)n); 731a3152895Sdrh if( zNew ){ 732a3152895Sdrh memcpy(zNew, z, n); 7331e536953Sdanielk1977 } 7341e536953Sdanielk1977 return zNew; 7351e536953Sdanielk1977 } 7361e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 737633e6d57Sdrh char *zNew; 738633e6d57Sdrh if( z==0 ){ 739633e6d57Sdrh return 0; 740633e6d57Sdrh } 741633e6d57Sdrh assert( (n&0x7fffffff)==n ); 742633e6d57Sdrh zNew = sqlite3DbMallocRaw(db, n+1); 743633e6d57Sdrh if( zNew ){ 744633e6d57Sdrh memcpy(zNew, z, n); 745633e6d57Sdrh zNew[n] = 0; 7461e536953Sdanielk1977 } 7471e536953Sdanielk1977 return zNew; 7481e536953Sdanielk1977 } 7491e536953Sdanielk1977 750a3152895Sdrh /* 751f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows. 752f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz 753f089aa45Sdrh ** point to that string. 754a3152895Sdrh */ 755f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ 756a3152895Sdrh va_list ap; 757f089aa45Sdrh char *z; 758a3152895Sdrh 759f089aa45Sdrh va_start(ap, zFormat); 760f089aa45Sdrh z = sqlite3VMPrintf(db, zFormat, ap); 761a3152895Sdrh va_end(ap); 762633e6d57Sdrh sqlite3DbFree(db, *pz); 763f089aa45Sdrh *pz = z; 764a3152895Sdrh } 765a3152895Sdrh 766a3152895Sdrh 767a3152895Sdrh /* 768a3152895Sdrh ** This function must be called before exiting any API function (i.e. 76917435752Sdrh ** returning control to the user) that has called sqlite3_malloc or 77017435752Sdrh ** sqlite3_realloc. 771a3152895Sdrh ** 772a3152895Sdrh ** The returned value is normally a copy of the second argument to this 773a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous 774a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead. 775a3152895Sdrh ** 776a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured, 777a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode()) 778a3152895Sdrh ** is set to SQLITE_NOMEM. 779a3152895Sdrh */ 780a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){ 781a1644fd8Sdanielk1977 /* If the db handle is not NULL, then we must hold the connection handle 782a1644fd8Sdanielk1977 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 783a1644fd8Sdanielk1977 ** is unsafe, as is the call to sqlite3Error(). 784a1644fd8Sdanielk1977 */ 785a1644fd8Sdanielk1977 assert( !db || sqlite3_mutex_held(db->mutex) ); 78698c21903Sdanielk1977 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){ 787a3152895Sdrh sqlite3Error(db, SQLITE_NOMEM, 0); 78817435752Sdrh db->mallocFailed = 0; 789a3152895Sdrh rc = SQLITE_NOMEM; 790a3152895Sdrh } 791a3152895Sdrh return rc & (db ? db->errMask : 0xff); 792a3152895Sdrh } 793