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*f7141990Sdrh ** $Id: malloc.c,v 1.21 2008/06/19 00:16:08 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 /* 35b21c8cd4Sdrh ** Set the soft heap-size limit for the current thread. Passing a 36b21c8cd4Sdrh ** zero or 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 /* 59a3152895Sdrh ** Release memory held by SQLite instances created by the current thread. 60a3152895Sdrh */ 61a3152895Sdrh int sqlite3_release_memory(int n){ 6286f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 63dfb316d4Sdanielk1977 int nRet = sqlite3VdbeReleaseMemory(n); 64dfb316d4Sdanielk1977 nRet += sqlite3PagerReleaseMemory(n-nRet); 65dfb316d4Sdanielk1977 return nRet; 661e536953Sdanielk1977 #else 671e536953Sdanielk1977 return SQLITE_OK; 681e536953Sdanielk1977 #endif 69a3152895Sdrh } 70a3152895Sdrh 71fec00eabSdrh /* 72fec00eabSdrh ** State information local to the memory allocation subsystem. 73fec00eabSdrh */ 74fec00eabSdrh static struct { 75fec00eabSdrh sqlite3_mutex *mutex; /* Mutex to serialize access */ 76fec00eabSdrh 77fec00eabSdrh /* 78fec00eabSdrh ** The alarm callback and its arguments. The mem0.mutex lock will 79fec00eabSdrh ** be held while the callback is running. Recursive calls into 80fec00eabSdrh ** the memory subsystem are allowed, but no new callbacks will be 81fec00eabSdrh ** issued. The alarmBusy variable is set to prevent recursive 82fec00eabSdrh ** callbacks. 83fec00eabSdrh */ 84fec00eabSdrh sqlite3_int64 alarmThreshold; 85fec00eabSdrh void (*alarmCallback)(void*, sqlite3_int64,int); 86fec00eabSdrh void *alarmArg; 87fec00eabSdrh int alarmBusy; 88fec00eabSdrh 89fec00eabSdrh /* 909ac3fe97Sdrh ** Pointers to the end of sqlite3Config.pScratch and 919ac3fe97Sdrh ** sqlite3Config.pPage to a block of memory that records 929ac3fe97Sdrh ** which pages are available. 939ac3fe97Sdrh */ 949ac3fe97Sdrh u32 *aScratchFree; 959ac3fe97Sdrh u32 *aPageFree; 969ac3fe97Sdrh 979ac3fe97Sdrh /* Number of free pages for scratch and page-cache memory */ 989ac3fe97Sdrh u32 nScratchFree; 999ac3fe97Sdrh u32 nPageFree; 100fec00eabSdrh } mem0; 101fec00eabSdrh 102fec00eabSdrh /* 103fec00eabSdrh ** Initialize the memory allocation subsystem. 104fec00eabSdrh */ 105fec00eabSdrh int sqlite3MallocInit(void){ 106fec00eabSdrh if( sqlite3Config.m.xMalloc==0 ){ 107fec00eabSdrh sqlite3MemSetDefault(); 108fec00eabSdrh } 109fec00eabSdrh memset(&mem0, 0, sizeof(mem0)); 1109ac3fe97Sdrh if( sqlite3Config.bCoreMutex ){ 11159f8c08eSdanielk1977 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 112fec00eabSdrh } 1139ac3fe97Sdrh if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000 1149ac3fe97Sdrh && sqlite3Config.nScratch>0 ){ 1159ac3fe97Sdrh int i; 1169ac3fe97Sdrh mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch) 1179ac3fe97Sdrh [sqlite3Config.szScratch*sqlite3Config.nScratch]; 1189ac3fe97Sdrh for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; } 1199ac3fe97Sdrh mem0.nScratchFree = sqlite3Config.nScratch; 1209ac3fe97Sdrh }else{ 1219ac3fe97Sdrh sqlite3Config.pScratch = 0; 122*f7141990Sdrh sqlite3Config.szScratch = 0; 1239ac3fe97Sdrh } 1249ac3fe97Sdrh if( sqlite3Config.pPage && sqlite3Config.szPage>=512 1259ac3fe97Sdrh && sqlite3Config.nPage>0 ){ 1269ac3fe97Sdrh int i; 1279ac3fe97Sdrh mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage) 1289ac3fe97Sdrh [sqlite3Config.szPage*sqlite3Config.nPage]; 1299ac3fe97Sdrh for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; } 1309ac3fe97Sdrh mem0.nPageFree = sqlite3Config.nPage; 1319ac3fe97Sdrh }else{ 1329ac3fe97Sdrh sqlite3Config.pPage = 0; 133*f7141990Sdrh sqlite3Config.szPage = 0; 1349ac3fe97Sdrh } 135fec00eabSdrh return sqlite3Config.m.xInit(sqlite3Config.m.pAppData); 136fec00eabSdrh } 137fec00eabSdrh 138fec00eabSdrh /* 139fec00eabSdrh ** Deinitialize the memory allocation subsystem. 140fec00eabSdrh */ 141fec00eabSdrh void sqlite3MallocEnd(void){ 142fec00eabSdrh sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData); 1439ac3fe97Sdrh memset(&mem0, 0, sizeof(mem0)); 144fec00eabSdrh } 145fec00eabSdrh 146fec00eabSdrh /* 147fec00eabSdrh ** Return the amount of memory currently checked out. 148fec00eabSdrh */ 149fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){ 150*f7141990Sdrh int n, mx; 151*f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); 152*f7141990Sdrh return (sqlite3_int64)n; 153fec00eabSdrh } 154fec00eabSdrh 155fec00eabSdrh /* 156fec00eabSdrh ** Return the maximum amount of memory that has ever been 157fec00eabSdrh ** checked out since either the beginning of this process 158fec00eabSdrh ** or since the most recent reset. 159fec00eabSdrh */ 160fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 161*f7141990Sdrh int n, mx; 162*f7141990Sdrh sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); 163*f7141990Sdrh return (sqlite3_int64)mx; 164fec00eabSdrh } 165fec00eabSdrh 166fec00eabSdrh /* 167fec00eabSdrh ** Change the alarm callback 168fec00eabSdrh */ 169fec00eabSdrh int sqlite3_memory_alarm( 170fec00eabSdrh void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 171fec00eabSdrh void *pArg, 172fec00eabSdrh sqlite3_int64 iThreshold 173fec00eabSdrh ){ 174fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 175fec00eabSdrh mem0.alarmCallback = xCallback; 176fec00eabSdrh mem0.alarmArg = pArg; 177fec00eabSdrh mem0.alarmThreshold = iThreshold; 178fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 179fec00eabSdrh return SQLITE_OK; 180fec00eabSdrh } 181fec00eabSdrh 182fec00eabSdrh /* 183fec00eabSdrh ** Trigger the alarm 184fec00eabSdrh */ 185fec00eabSdrh static void sqlite3MallocAlarm(int nByte){ 186fec00eabSdrh void (*xCallback)(void*,sqlite3_int64,int); 187fec00eabSdrh sqlite3_int64 nowUsed; 188fec00eabSdrh void *pArg; 189fec00eabSdrh if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; 190fec00eabSdrh mem0.alarmBusy = 1; 191fec00eabSdrh xCallback = mem0.alarmCallback; 192*f7141990Sdrh nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 193fec00eabSdrh pArg = mem0.alarmArg; 194fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 195fec00eabSdrh xCallback(pArg, nowUsed, nByte); 196fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 197fec00eabSdrh mem0.alarmBusy = 0; 198fec00eabSdrh } 199fec00eabSdrh 200fec00eabSdrh /* 201*f7141990Sdrh ** Do a memory allocation with statistics and alarms. Assume the 202*f7141990Sdrh ** lock is already held. 203fec00eabSdrh */ 204*f7141990Sdrh static int mallocWithAlarm(int n, void **pp){ 205fec00eabSdrh int nFull; 206*f7141990Sdrh void *p; 207*f7141990Sdrh assert( sqlite3_mutex_held(mem0.mutex) ); 208fec00eabSdrh nFull = sqlite3Config.m.xRoundup(n); 209*f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 210*f7141990Sdrh if( mem0.alarmCallback!=0 ){ 211*f7141990Sdrh int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 212*f7141990Sdrh if( nUsed+nFull >= mem0.alarmThreshold ){ 213fec00eabSdrh sqlite3MallocAlarm(nFull); 214fec00eabSdrh } 215*f7141990Sdrh } 216fec00eabSdrh if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ 217fec00eabSdrh p = 0; 218fec00eabSdrh }else{ 219fec00eabSdrh p = sqlite3Config.m.xMalloc(nFull); 220fec00eabSdrh if( p==0 ){ 221fec00eabSdrh sqlite3MallocAlarm(nFull); 222fec00eabSdrh p = malloc(nFull); 223fec00eabSdrh } 224fec00eabSdrh } 225*f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 226*f7141990Sdrh *pp = p; 227*f7141990Sdrh return nFull; 228fec00eabSdrh } 229*f7141990Sdrh 230*f7141990Sdrh /* 231*f7141990Sdrh ** Allocate memory. This routine is like sqlite3_malloc() except that it 232*f7141990Sdrh ** assumes the memory subsystem has already been initialized. 233*f7141990Sdrh */ 234*f7141990Sdrh void *sqlite3Malloc(int n){ 235*f7141990Sdrh void *p; 236*f7141990Sdrh if( n<=0 ){ 237*f7141990Sdrh p = 0; 238*f7141990Sdrh }else if( sqlite3Config.bMemstat ){ 239*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 240*f7141990Sdrh mallocWithAlarm(n, &p); 241fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 242fec00eabSdrh }else{ 243fec00eabSdrh p = sqlite3Config.m.xMalloc(n); 244fec00eabSdrh } 245fec00eabSdrh return p; 246fec00eabSdrh } 247fec00eabSdrh 248fec00eabSdrh /* 249fec00eabSdrh ** This version of the memory allocation is for use by the application. 250fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the 251fec00eabSdrh ** allocation. 252fec00eabSdrh */ 253fec00eabSdrh void *sqlite3_malloc(int n){ 254fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 255fec00eabSdrh if( sqlite3_initialize() ) return 0; 256fec00eabSdrh #endif 257fec00eabSdrh return sqlite3Malloc(n); 258fec00eabSdrh } 259fec00eabSdrh 260fec00eabSdrh /* 261e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from 262facf0307Sdrh ** xScratchMalloc(). We verify this constraint in the single-threaded 263facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation 264e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed. 265e5ae5735Sdrh */ 266e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 267facf0307Sdrh static int scratchAllocOut = 0; 268e5ae5735Sdrh #endif 269e5ae5735Sdrh 270e5ae5735Sdrh 271e5ae5735Sdrh /* 272e5ae5735Sdrh ** Allocate memory that is to be used and released right away. 273e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended 274e5ae5735Sdrh ** for situations where the memory might be held long-term. This 275e5ae5735Sdrh ** routine is intended to get memory to old large transient data 276e5ae5735Sdrh ** structures that would not normally fit on the stack of an 277e5ae5735Sdrh ** embedded processor. 278e5ae5735Sdrh */ 279facf0307Sdrh void *sqlite3ScratchMalloc(int n){ 280e5ae5735Sdrh void *p; 281e5ae5735Sdrh assert( n>0 ); 282e5ae5735Sdrh if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ 283e5ae5735Sdrh return 0; 284e5ae5735Sdrh } 2859ac3fe97Sdrh 286e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 2879ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 2889ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 2899ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 2909ac3fe97Sdrh ** would be much more complicated.) */ 291facf0307Sdrh assert( scratchAllocOut==0 ); 292e5ae5735Sdrh #endif 2939ac3fe97Sdrh 294*f7141990Sdrh if( sqlite3Config.szScratch<n ){ 295*f7141990Sdrh goto scratch_overflow; 296*f7141990Sdrh }else{ 297e5ae5735Sdrh sqlite3_mutex_enter(mem0.mutex); 298*f7141990Sdrh if( mem0.nScratchFree==0 ){ 299*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 300*f7141990Sdrh goto scratch_overflow; 301e5ae5735Sdrh }else{ 3029ac3fe97Sdrh int i; 3039ac3fe97Sdrh i = mem0.aScratchFree[--mem0.nScratchFree]; 304*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 3059ac3fe97Sdrh i *= sqlite3Config.szScratch; 306*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 3079ac3fe97Sdrh p = (void*)&((char*)sqlite3Config.pScratch)[i]; 308e5ae5735Sdrh } 309*f7141990Sdrh } 310*f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 311*f7141990Sdrh scratchAllocOut = p!=0; 312*f7141990Sdrh #endif 313*f7141990Sdrh 314*f7141990Sdrh return p; 315*f7141990Sdrh 316*f7141990Sdrh scratch_overflow: 317*f7141990Sdrh if( sqlite3Config.bMemstat ){ 318*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 319*f7141990Sdrh n = mallocWithAlarm(n, &p); 320*f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); 3219ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 322*f7141990Sdrh }else{ 323*f7141990Sdrh p = sqlite3Config.m.xMalloc(n); 324*f7141990Sdrh } 325*f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 326*f7141990Sdrh scratchAllocOut = p!=0; 327*f7141990Sdrh #endif 328e5ae5735Sdrh return p; 329e5ae5735Sdrh } 330facf0307Sdrh void sqlite3ScratchFree(void *p){ 331e5ae5735Sdrh if( p ){ 3329ac3fe97Sdrh 333e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 3349ac3fe97Sdrh /* Verify that no more than one scratch allocation per thread 3359ac3fe97Sdrh ** is outstanding at one time. (This is only checked in the 3369ac3fe97Sdrh ** single-threaded case since checking in the multi-threaded case 3379ac3fe97Sdrh ** would be much more complicated.) */ 338facf0307Sdrh assert( scratchAllocOut==1 ); 339facf0307Sdrh scratchAllocOut = 0; 340e5ae5735Sdrh #endif 3419ac3fe97Sdrh 3429ac3fe97Sdrh if( sqlite3Config.pScratch==0 3439ac3fe97Sdrh || p<sqlite3Config.pScratch 3449ac3fe97Sdrh || p>=(void*)mem0.aScratchFree ){ 345*f7141990Sdrh if( sqlite3Config.bMemstat ){ 346*f7141990Sdrh int iSize = sqlite3MallocSize(p); 347*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 348*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 349*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 350facf0307Sdrh sqlite3Config.m.xFree(p); 351*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 352*f7141990Sdrh }else{ 353*f7141990Sdrh sqlite3Config.m.xFree(p); 354*f7141990Sdrh } 3559ac3fe97Sdrh }else{ 3569ac3fe97Sdrh int i; 3579ac3fe97Sdrh i = p - sqlite3Config.pScratch; 3589ac3fe97Sdrh i /= sqlite3Config.szScratch; 3599ac3fe97Sdrh assert( i>=0 && i<sqlite3Config.nScratch ); 360*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 361*f7141990Sdrh assert( mem0.nScratchFree<sqlite3Config.nScratch ); 3629ac3fe97Sdrh mem0.aScratchFree[mem0.nScratchFree++] = i; 363*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 3649ac3fe97Sdrh sqlite3_mutex_leave(mem0.mutex); 3659ac3fe97Sdrh } 366e5ae5735Sdrh } 367e5ae5735Sdrh } 368e5ae5735Sdrh 369e5ae5735Sdrh /* 370*f7141990Sdrh ** Allocate memory to be used by the page cache. Make use of the 371*f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one 372*f7141990Sdrh ** and that memory is of the right size and is not completely 373*f7141990Sdrh ** consumed. Otherwise, failover to sqlite3Malloc(). 374facf0307Sdrh */ 375*f7141990Sdrh void *sqlite3PageMalloc(int n){ 376*f7141990Sdrh void *p; 377*f7141990Sdrh assert( n>0 ); 378*f7141990Sdrh assert( (n & (n-1))==0 ); 379*f7141990Sdrh assert( n>=512 && n<=32768 ); 380*f7141990Sdrh if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ 381*f7141990Sdrh return 0; 382facf0307Sdrh } 383*f7141990Sdrh 384*f7141990Sdrh if( sqlite3Config.szPage<n ){ 385*f7141990Sdrh goto page_overflow; 386*f7141990Sdrh }else{ 387*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 388*f7141990Sdrh if( mem0.nPageFree==0 ){ 389*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 390*f7141990Sdrh goto page_overflow; 391*f7141990Sdrh }else{ 392*f7141990Sdrh int i; 393*f7141990Sdrh i = mem0.aPageFree[--mem0.nPageFree]; 394*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 395*f7141990Sdrh i *= sqlite3Config.szPage; 396*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); 397*f7141990Sdrh p = (void*)&((char*)sqlite3Config.pPage)[i]; 398*f7141990Sdrh } 399*f7141990Sdrh } 400*f7141990Sdrh return p; 401*f7141990Sdrh 402*f7141990Sdrh page_overflow: 403*f7141990Sdrh if( sqlite3Config.bMemstat ){ 404*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 405*f7141990Sdrh n = mallocWithAlarm(n, &p); 406*f7141990Sdrh if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); 407*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 408*f7141990Sdrh }else{ 409*f7141990Sdrh p = sqlite3Config.m.xMalloc(n); 410*f7141990Sdrh } 411*f7141990Sdrh return p; 412*f7141990Sdrh } 413*f7141990Sdrh void sqlite3PageFree(void *p){ 414*f7141990Sdrh if( p ){ 415*f7141990Sdrh if( sqlite3Config.pPage==0 416*f7141990Sdrh || p<sqlite3Config.pPage 417*f7141990Sdrh || p>=(void*)mem0.aPageFree ){ 418*f7141990Sdrh if( sqlite3Config.bMemstat ){ 419*f7141990Sdrh int iSize = sqlite3MallocSize(p); 420*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 421*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); 422*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 423*f7141990Sdrh sqlite3Config.m.xFree(p); 424*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 425*f7141990Sdrh }else{ 426*f7141990Sdrh sqlite3Config.m.xFree(p); 427*f7141990Sdrh } 428*f7141990Sdrh }else{ 429*f7141990Sdrh int i; 430*f7141990Sdrh i = p - sqlite3Config.pPage; 431*f7141990Sdrh i /= sqlite3Config.szPage; 432*f7141990Sdrh assert( i>=0 && i<sqlite3Config.nPage ); 433*f7141990Sdrh sqlite3_mutex_enter(mem0.mutex); 434*f7141990Sdrh assert( mem0.nPageFree<sqlite3Config.nPage ); 435*f7141990Sdrh mem0.aPageFree[mem0.nPageFree++] = i; 436*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); 437*f7141990Sdrh sqlite3_mutex_leave(mem0.mutex); 438*f7141990Sdrh } 439*f7141990Sdrh } 440facf0307Sdrh } 441facf0307Sdrh 442facf0307Sdrh /* 443fec00eabSdrh ** Return the size of a memory allocation previously obtained from 444fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc(). 445fec00eabSdrh */ 446fec00eabSdrh int sqlite3MallocSize(void *p){ 447fec00eabSdrh return sqlite3Config.m.xSize(p); 448fec00eabSdrh } 449fec00eabSdrh 450fec00eabSdrh /* 451fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc(). 452fec00eabSdrh */ 453fec00eabSdrh void sqlite3_free(void *p){ 454fec00eabSdrh if( p==0 ) return; 455fec00eabSdrh if( sqlite3Config.bMemstat ){ 456fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 457*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 458fec00eabSdrh sqlite3Config.m.xFree(p); 459fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 460fec00eabSdrh }else{ 461fec00eabSdrh sqlite3Config.m.xFree(p); 462fec00eabSdrh } 463fec00eabSdrh } 464fec00eabSdrh 465fec00eabSdrh /* 466fec00eabSdrh ** Change the size of an existing memory allocation 467fec00eabSdrh */ 468fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){ 469fec00eabSdrh int nOld, nNew; 470fec00eabSdrh void *pNew; 471fec00eabSdrh if( pOld==0 ){ 472fec00eabSdrh return sqlite3Malloc(nBytes); 473fec00eabSdrh } 474fec00eabSdrh if( nBytes<=0 ){ 475fec00eabSdrh sqlite3_free(pOld); 476fec00eabSdrh return 0; 477fec00eabSdrh } 478fec00eabSdrh nOld = sqlite3MallocSize(pOld); 479fec00eabSdrh if( sqlite3Config.bMemstat ){ 480fec00eabSdrh sqlite3_mutex_enter(mem0.mutex); 481*f7141990Sdrh sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); 482fec00eabSdrh nNew = sqlite3Config.m.xRoundup(nBytes); 483fec00eabSdrh if( nOld==nNew ){ 484fec00eabSdrh pNew = pOld; 485fec00eabSdrh }else{ 486*f7141990Sdrh if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 487*f7141990Sdrh mem0.alarmThreshold ){ 488fec00eabSdrh sqlite3MallocAlarm(nNew-nOld); 489fec00eabSdrh } 490fec00eabSdrh if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ 491fec00eabSdrh pNew = 0; 492fec00eabSdrh }else{ 493fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nNew); 494fec00eabSdrh if( pNew==0 ){ 495fec00eabSdrh sqlite3MallocAlarm(nBytes); 496fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nNew); 497fec00eabSdrh } 498fec00eabSdrh } 499fec00eabSdrh if( pNew ){ 500*f7141990Sdrh sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 501fec00eabSdrh } 502fec00eabSdrh } 503fec00eabSdrh sqlite3_mutex_leave(mem0.mutex); 504fec00eabSdrh }else{ 505fec00eabSdrh pNew = sqlite3Config.m.xRealloc(pOld, nBytes); 506fec00eabSdrh } 507fec00eabSdrh return pNew; 508fec00eabSdrh } 509fec00eabSdrh 510fec00eabSdrh /* 511fec00eabSdrh ** The public interface to sqlite3Realloc. Make sure that the memory 512fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc. 513fec00eabSdrh */ 514fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){ 515fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT 516fec00eabSdrh if( sqlite3_initialize() ) return 0; 517fec00eabSdrh #endif 518fec00eabSdrh return sqlite3Realloc(pOld, n); 519fec00eabSdrh } 520fec00eabSdrh 521a3152895Sdrh 522a3152895Sdrh /* 52317435752Sdrh ** Allocate and zero memory. 524a3152895Sdrh */ 525fec00eabSdrh void *sqlite3MallocZero(int n){ 526fec00eabSdrh void *p = sqlite3Malloc(n); 527a3152895Sdrh if( p ){ 528a3152895Sdrh memset(p, 0, n); 529a3152895Sdrh } 530a3152895Sdrh return p; 531a3152895Sdrh } 53217435752Sdrh 53317435752Sdrh /* 53417435752Sdrh ** Allocate and zero memory. If the allocation fails, make 53517435752Sdrh ** the mallocFailed flag in the connection pointer. 53617435752Sdrh */ 537fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){ 538a1644fd8Sdanielk1977 void *p = sqlite3DbMallocRaw(db, n); 53917435752Sdrh if( p ){ 54017435752Sdrh memset(p, 0, n); 54117435752Sdrh } 54217435752Sdrh return p; 54317435752Sdrh } 54417435752Sdrh 54517435752Sdrh /* 54617435752Sdrh ** Allocate and zero memory. If the allocation fails, make 54717435752Sdrh ** the mallocFailed flag in the connection pointer. 54817435752Sdrh */ 549fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){ 550a1644fd8Sdanielk1977 void *p = 0; 551a1644fd8Sdanielk1977 if( !db || db->mallocFailed==0 ){ 552fec00eabSdrh p = sqlite3Malloc(n); 553f3a65f7eSdrh if( !p && db ){ 55417435752Sdrh db->mallocFailed = 1; 55517435752Sdrh } 556a1644fd8Sdanielk1977 } 55717435752Sdrh return p; 55817435752Sdrh } 55917435752Sdrh 56026783a58Sdanielk1977 /* 56126783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the 56226783a58Sdanielk1977 ** resize fails, set the mallocFailed flag inthe connection object. 56326783a58Sdanielk1977 */ 564a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 565a1644fd8Sdanielk1977 void *pNew = 0; 566a1644fd8Sdanielk1977 if( db->mallocFailed==0 ){ 567a1644fd8Sdanielk1977 pNew = sqlite3_realloc(p, n); 568a1644fd8Sdanielk1977 if( !pNew ){ 569a1644fd8Sdanielk1977 db->mallocFailed = 1; 570a1644fd8Sdanielk1977 } 571a1644fd8Sdanielk1977 } 572a1644fd8Sdanielk1977 return pNew; 573a1644fd8Sdanielk1977 } 574a1644fd8Sdanielk1977 57517435752Sdrh /* 57617435752Sdrh ** Attempt to reallocate p. If the reallocation fails, then free p 57717435752Sdrh ** and set the mallocFailed flag in the database connection. 57817435752Sdrh */ 57917435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 580a3152895Sdrh void *pNew; 581a1644fd8Sdanielk1977 pNew = sqlite3DbRealloc(db, p, n); 582a3152895Sdrh if( !pNew ){ 5831e536953Sdanielk1977 sqlite3_free(p); 584a3152895Sdrh } 585a3152895Sdrh return pNew; 586a3152895Sdrh } 587a3152895Sdrh 588a3152895Sdrh /* 589a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These 590a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 591a3152895Sdrh ** is because when memory debugging is turned on, these two functions are 592a3152895Sdrh ** called via macros that record the current file and line number in the 593a3152895Sdrh ** ThreadData structure. 594a3152895Sdrh */ 595a3152895Sdrh char *sqlite3StrDup(const char *z){ 596a3152895Sdrh char *zNew; 597a3152895Sdrh int n; 598a3152895Sdrh if( z==0 ) return 0; 599a3152895Sdrh n = strlen(z)+1; 600e5ae5735Sdrh zNew = sqlite3Malloc(n); 601a3152895Sdrh if( zNew ) memcpy(zNew, z, n); 602a3152895Sdrh return zNew; 603a3152895Sdrh } 604a3152895Sdrh char *sqlite3StrNDup(const char *z, int n){ 605a3152895Sdrh char *zNew; 606a3152895Sdrh if( z==0 ) return 0; 607e5ae5735Sdrh zNew = sqlite3Malloc(n+1); 608a3152895Sdrh if( zNew ){ 609a3152895Sdrh memcpy(zNew, z, n); 610a3152895Sdrh zNew[n] = 0; 611a3152895Sdrh } 612a3152895Sdrh return zNew; 613a3152895Sdrh } 614a3152895Sdrh 6151e536953Sdanielk1977 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 6161e536953Sdanielk1977 char *zNew = sqlite3StrDup(z); 6171e536953Sdanielk1977 if( z && !zNew ){ 6181e536953Sdanielk1977 db->mallocFailed = 1; 6191e536953Sdanielk1977 } 6201e536953Sdanielk1977 return zNew; 6211e536953Sdanielk1977 } 6221e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 6231e536953Sdanielk1977 char *zNew = sqlite3StrNDup(z, n); 6241e536953Sdanielk1977 if( z && !zNew ){ 6251e536953Sdanielk1977 db->mallocFailed = 1; 6261e536953Sdanielk1977 } 6271e536953Sdanielk1977 return zNew; 6281e536953Sdanielk1977 } 6291e536953Sdanielk1977 630a3152895Sdrh /* 631a3152895Sdrh ** Create a string from the 2nd and subsequent arguments (up to the 632a3152895Sdrh ** first NULL argument), store the string in memory obtained from 633a3152895Sdrh ** sqliteMalloc() and make the pointer indicated by the 1st argument 634a3152895Sdrh ** point to that string. The 1st argument must either be NULL or 635a3152895Sdrh ** point to memory obtained from sqliteMalloc(). 636a3152895Sdrh */ 637a3152895Sdrh void sqlite3SetString(char **pz, ...){ 638a3152895Sdrh va_list ap; 639a3152895Sdrh int nByte; 640a3152895Sdrh const char *z; 641a3152895Sdrh char *zResult; 642a3152895Sdrh 643a3152895Sdrh assert( pz!=0 ); 644a3152895Sdrh nByte = 1; 645a3152895Sdrh va_start(ap, pz); 646a3152895Sdrh while( (z = va_arg(ap, const char*))!=0 ){ 647a3152895Sdrh nByte += strlen(z); 648a3152895Sdrh } 649a3152895Sdrh va_end(ap); 6501e536953Sdanielk1977 sqlite3_free(*pz); 651e5ae5735Sdrh *pz = zResult = sqlite3Malloc(nByte); 652a3152895Sdrh if( zResult==0 ){ 653a3152895Sdrh return; 654a3152895Sdrh } 655a3152895Sdrh *zResult = 0; 656a3152895Sdrh va_start(ap, pz); 657a3152895Sdrh while( (z = va_arg(ap, const char*))!=0 ){ 658a3152895Sdrh int n = strlen(z); 659a3152895Sdrh memcpy(zResult, z, n); 660a3152895Sdrh zResult += n; 661a3152895Sdrh } 662a3152895Sdrh zResult[0] = 0; 663a3152895Sdrh va_end(ap); 664a3152895Sdrh } 665a3152895Sdrh 666a3152895Sdrh 667a3152895Sdrh /* 668a3152895Sdrh ** This function must be called before exiting any API function (i.e. 66917435752Sdrh ** returning control to the user) that has called sqlite3_malloc or 67017435752Sdrh ** sqlite3_realloc. 671a3152895Sdrh ** 672a3152895Sdrh ** The returned value is normally a copy of the second argument to this 673a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous 674a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead. 675a3152895Sdrh ** 676a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured, 677a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode()) 678a3152895Sdrh ** is set to SQLITE_NOMEM. 679a3152895Sdrh */ 680a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){ 681a1644fd8Sdanielk1977 /* If the db handle is not NULL, then we must hold the connection handle 682a1644fd8Sdanielk1977 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 683a1644fd8Sdanielk1977 ** is unsafe, as is the call to sqlite3Error(). 684a1644fd8Sdanielk1977 */ 685a1644fd8Sdanielk1977 assert( !db || sqlite3_mutex_held(db->mutex) ); 6861e536953Sdanielk1977 if( db && db->mallocFailed ){ 687a3152895Sdrh sqlite3Error(db, SQLITE_NOMEM, 0); 68817435752Sdrh db->mallocFailed = 0; 689a3152895Sdrh rc = SQLITE_NOMEM; 690a3152895Sdrh } 691a3152895Sdrh return rc & (db ? db->errMask : 0xff); 692a3152895Sdrh } 693