00001 /* 00002 ** 2001 September 15 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** 00013 ** Memory allocation functions used throughout sqlite. 00014 ** 00015 ** $Id: malloc.c,v 1.45 2008/10/12 00:27:53 shane Exp $ 00016 */ 00017 #include "sqliteInt.h" 00018 #include <stdarg.h> 00019 #include <ctype.h> 00020 00021 /* 00022 ** This routine runs when the memory allocator sees that the 00023 ** total memory allocation is about to exceed the soft heap 00024 ** limit. 00025 */ 00026 static void softHeapLimitEnforcer( 00027 void *NotUsed, 00028 sqlite3_int64 inUse, 00029 int allocSize 00030 ){ 00031 sqlite3_release_memory(allocSize); 00032 } 00033 00034 /* 00035 ** Set the soft heap-size limit for the library. Passing a zero or 00036 ** negative value indicates no limit. 00037 */ 00038 void sqlite3_soft_heap_limit(int n){ 00039 sqlite3_uint64 iLimit; 00040 int overage; 00041 if( n<0 ){ 00042 iLimit = 0; 00043 }else{ 00044 iLimit = n; 00045 } 00046 sqlite3_initialize(); 00047 if( iLimit>0 ){ 00048 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit); 00049 }else{ 00050 sqlite3MemoryAlarm(0, 0, 0); 00051 } 00052 overage = sqlite3_memory_used() - n; 00053 if( overage>0 ){ 00054 sqlite3_release_memory(overage); 00055 } 00056 } 00057 00058 /* 00059 ** Attempt to release up to n bytes of non-essential memory currently 00060 ** held by SQLite. An example of non-essential memory is memory used to 00061 ** cache database pages that are not currently in use. 00062 */ 00063 int sqlite3_release_memory(int n){ 00064 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 00065 int nRet = 0; 00066 #if 0 00067 nRet += sqlite3VdbeReleaseMemory(n); 00068 #endif 00069 nRet += sqlite3PcacheReleaseMemory(n-nRet); 00070 return nRet; 00071 #else 00072 return SQLITE_OK; 00073 #endif 00074 } 00075 00076 /* 00077 ** State information local to the memory allocation subsystem. 00078 */ 00079 static SQLITE_WSD struct Mem0Global { 00080 /* Number of free pages for scratch and page-cache memory */ 00081 u32 nScratchFree; 00082 u32 nPageFree; 00083 00084 sqlite3_mutex *mutex; /* Mutex to serialize access */ 00085 00086 /* 00087 ** The alarm callback and its arguments. The mem0.mutex lock will 00088 ** be held while the callback is running. Recursive calls into 00089 ** the memory subsystem are allowed, but no new callbacks will be 00090 ** issued. The alarmBusy variable is set to prevent recursive 00091 ** callbacks. 00092 */ 00093 sqlite3_int64 alarmThreshold; 00094 void (*alarmCallback)(void*, sqlite3_int64,int); 00095 void *alarmArg; 00096 int alarmBusy; 00097 00098 /* 00099 ** Pointers to the end of sqlite3GlobalConfig.pScratch and 00100 ** sqlite3GlobalConfig.pPage to a block of memory that records 00101 ** which pages are available. 00102 */ 00103 u32 *aScratchFree; 00104 u32 *aPageFree; 00105 } mem0 = { 62560955 }; 00106 00107 #define mem0 GLOBAL(struct Mem0Global, mem0) 00108 00109 /* 00110 ** Initialize the memory allocation subsystem. 00111 */ 00112 int sqlite3MallocInit(void){ 00113 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 00114 sqlite3MemSetDefault(); 00115 } 00116 memset(&mem0, 0, sizeof(mem0)); 00117 if( sqlite3GlobalConfig.bCoreMutex ){ 00118 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 00119 } 00120 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 00121 && sqlite3GlobalConfig.nScratch>=0 ){ 00122 int i; 00123 sqlite3GlobalConfig.szScratch -= 4; 00124 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch) 00125 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch]; 00126 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; } 00127 mem0.nScratchFree = sqlite3GlobalConfig.nScratch; 00128 }else{ 00129 sqlite3GlobalConfig.pScratch = 0; 00130 sqlite3GlobalConfig.szScratch = 0; 00131 } 00132 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512 00133 && sqlite3GlobalConfig.nPage>=1 ){ 00134 int i; 00135 int overhead; 00136 int sz = sqlite3GlobalConfig.szPage; 00137 int n = sqlite3GlobalConfig.nPage; 00138 overhead = (4*n + sz - 1)/sz; 00139 sqlite3GlobalConfig.nPage -= overhead; 00140 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage) 00141 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage]; 00142 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; } 00143 mem0.nPageFree = sqlite3GlobalConfig.nPage; 00144 }else{ 00145 sqlite3GlobalConfig.pPage = 0; 00146 sqlite3GlobalConfig.szPage = 0; 00147 } 00148 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 00149 } 00150 00151 /* 00152 ** Deinitialize the memory allocation subsystem. 00153 */ 00154 void sqlite3MallocEnd(void){ 00155 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 00156 memset(&mem0, 0, sizeof(mem0)); 00157 } 00158 00159 /* 00160 ** Return the amount of memory currently checked out. 00161 */ 00162 sqlite3_int64 sqlite3_memory_used(void){ 00163 int n, mx; 00164 sqlite3_int64 res; 00165 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); 00166 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ 00167 return res; 00168 } 00169 00170 /* 00171 ** Return the maximum amount of memory that has ever been 00172 ** checked out since either the beginning of this process 00173 ** or since the most recent reset. 00174 */ 00175 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 00176 int n, mx; 00177 sqlite3_int64 res; 00178 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); 00179 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ 00180 return res; 00181 } 00182 00183 /* 00184 ** Change the alarm callback 00185 */ 00186 int sqlite3MemoryAlarm( 00187 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 00188 void *pArg, 00189 sqlite3_int64 iThreshold 00190 ){ 00191 sqlite3_mutex_enter(mem0.mutex); 00192 mem0.alarmCallback = xCallback; 00193 mem0.alarmArg = pArg; 00194 mem0.alarmThreshold = iThreshold; 00195 sqlite3_mutex_leave(mem0.mutex); 00196 return SQLITE_OK; 00197 } 00198 00199 #ifndef SQLITE_OMIT_DEPRECATED 00200 /* 00201 ** Deprecated external interface. Internal/core SQLite code 00202 ** should call sqlite3MemoryAlarm. 00203 */ 00204 int sqlite3_memory_alarm( 00205 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 00206 void *pArg, 00207 sqlite3_int64 iThreshold 00208 ){ 00209 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); 00210 } 00211 #endif 00212 00213 /* 00214 ** Trigger the alarm 00215 */ 00216 static void sqlite3MallocAlarm(int nByte){ 00217 void (*xCallback)(void*,sqlite3_int64,int); 00218 sqlite3_int64 nowUsed; 00219 void *pArg; 00220 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return; 00221 mem0.alarmBusy = 1; 00222 xCallback = mem0.alarmCallback; 00223 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 00224 pArg = mem0.alarmArg; 00225 sqlite3_mutex_leave(mem0.mutex); 00226 xCallback(pArg, nowUsed, nByte); 00227 sqlite3_mutex_enter(mem0.mutex); 00228 mem0.alarmBusy = 0; 00229 } 00230 00231 /* 00232 ** Do a memory allocation with statistics and alarms. Assume the 00233 ** lock is already held. 00234 */ 00235 static int mallocWithAlarm(int n, void **pp){ 00236 int nFull; 00237 void *p; 00238 assert( sqlite3_mutex_held(mem0.mutex) ); 00239 nFull = sqlite3GlobalConfig.m.xRoundup(n); 00240 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 00241 if( mem0.alarmCallback!=0 ){ 00242 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 00243 if( nUsed+nFull >= mem0.alarmThreshold ){ 00244 sqlite3MallocAlarm(nFull); 00245 } 00246 } 00247 p = sqlite3GlobalConfig.m.xMalloc(nFull); 00248 if( p==0 && mem0.alarmCallback ){ 00249 sqlite3MallocAlarm(nFull); 00250 p = sqlite3GlobalConfig.m.xMalloc(nFull); 00251 } 00252 if( p ){ 00253 nFull = sqlite3MallocSize(p); 00254 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 00255 } 00256 *pp = p; 00257 return nFull; 00258 } 00259 00260 /* 00261 ** Allocate memory. This routine is like sqlite3_malloc() except that it 00262 ** assumes the memory subsystem has already been initialized. 00263 */ 00264 void *sqlite3Malloc(int n){ 00265 void *p; 00266 if( n<=0 ){ 00267 p = 0; 00268 }else if( sqlite3GlobalConfig.bMemstat ){ 00269 sqlite3_mutex_enter(mem0.mutex); 00270 mallocWithAlarm(n, &p); 00271 sqlite3_mutex_leave(mem0.mutex); 00272 }else{ 00273 p = sqlite3GlobalConfig.m.xMalloc(n); 00274 } 00275 return p; 00276 } 00277 00278 /* 00279 ** This version of the memory allocation is for use by the application. 00280 ** First make sure the memory subsystem is initialized, then do the 00281 ** allocation. 00282 */ 00283 EXPORT_C void *sqlite3_malloc(int n){ 00284 #ifndef SQLITE_OMIT_AUTOINIT 00285 if( sqlite3_initialize() ) return 0; 00286 #endif 00287 return sqlite3Malloc(n); 00288 } 00289 00290 /* 00291 ** Each thread may only have a single outstanding allocation from 00292 ** xScratchMalloc(). We verify this constraint in the single-threaded 00293 ** case by setting scratchAllocOut to 1 when an allocation 00294 ** is outstanding clearing it when the allocation is freed. 00295 */ 00296 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 00297 static int scratchAllocOut = 0; 00298 #endif 00299 00300 00301 /* 00302 ** Allocate memory that is to be used and released right away. 00303 ** This routine is similar to alloca() in that it is not intended 00304 ** for situations where the memory might be held long-term. This 00305 ** routine is intended to get memory to old large transient data 00306 ** structures that would not normally fit on the stack of an 00307 ** embedded processor. 00308 */ 00309 void *sqlite3ScratchMalloc(int n){ 00310 void *p; 00311 assert( n>0 ); 00312 00313 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 00314 /* Verify that no more than one scratch allocation per thread 00315 ** is outstanding at one time. (This is only checked in the 00316 ** single-threaded case since checking in the multi-threaded case 00317 ** would be much more complicated.) */ 00318 assert( scratchAllocOut==0 ); 00319 #endif 00320 00321 if( sqlite3GlobalConfig.szScratch<n ){ 00322 goto scratch_overflow; 00323 }else{ 00324 sqlite3_mutex_enter(mem0.mutex); 00325 if( mem0.nScratchFree==0 ){ 00326 sqlite3_mutex_leave(mem0.mutex); 00327 goto scratch_overflow; 00328 }else{ 00329 int i; 00330 i = mem0.aScratchFree[--mem0.nScratchFree]; 00331 i *= sqlite3GlobalConfig.szScratch; 00332 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 00333 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 00334 sqlite3_mutex_leave(mem0.mutex); 00335 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; 00336 } 00337 } 00338 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 00339 scratchAllocOut = p!=0; 00340 #endif 00341 00342 return p; 00343 00344 scratch_overflow: 00345 if( sqlite3GlobalConfig.bMemstat ){ 00346 sqlite3_mutex_enter(mem0.mutex); 00347 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 00348 n = mallocWithAlarm(n, &p); 00349 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); 00350 sqlite3_mutex_leave(mem0.mutex); 00351 }else{ 00352 p = sqlite3GlobalConfig.m.xMalloc(n); 00353 } 00354 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 00355 scratchAllocOut = p!=0; 00356 #endif 00357 return p; 00358 } 00359 void sqlite3ScratchFree(void *p){ 00360 if( p ){ 00361 00362 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 00363 /* Verify that no more than one scratch allocation per thread 00364 ** is outstanding at one time. (This is only checked in the 00365 ** single-threaded case since checking in the multi-threaded case 00366 ** would be much more complicated.) */ 00367 assert( scratchAllocOut==1 ); 00368 scratchAllocOut = 0; 00369 #endif 00370 00371 if( sqlite3GlobalConfig.pScratch==0 00372 || p<sqlite3GlobalConfig.pScratch 00373 || p>=(void*)mem0.aScratchFree ){ 00374 if( sqlite3GlobalConfig.bMemstat ){ 00375 int iSize = sqlite3MallocSize(p); 00376 sqlite3_mutex_enter(mem0.mutex); 00377 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 00378 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 00379 sqlite3GlobalConfig.m.xFree(p); 00380 sqlite3_mutex_leave(mem0.mutex); 00381 }else{ 00382 sqlite3GlobalConfig.m.xFree(p); 00383 } 00384 }else{ 00385 int i; 00386 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch; 00387 i /= sqlite3GlobalConfig.szScratch; 00388 assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); 00389 sqlite3_mutex_enter(mem0.mutex); 00390 assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch ); 00391 mem0.aScratchFree[mem0.nScratchFree++] = i; 00392 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 00393 sqlite3_mutex_leave(mem0.mutex); 00394 } 00395 } 00396 } 00397 00398 /* 00399 ** Allocate memory to be used by the page cache. Make use of the 00400 ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one 00401 ** and that memory is of the right size and is not completely 00402 ** consumed. Otherwise, failover to sqlite3Malloc(). 00403 */ 00404 #if 0 00405 void *sqlite3PageMalloc(int n){ 00406 void *p; 00407 assert( n>0 ); 00408 assert( (n & (n-1))==0 ); 00409 assert( n>=512 && n<=32768 ); 00410 00411 if( sqlite3GlobalConfig.szPage<n ){ 00412 goto page_overflow; 00413 }else{ 00414 sqlite3_mutex_enter(mem0.mutex); 00415 if( mem0.nPageFree==0 ){ 00416 sqlite3_mutex_leave(mem0.mutex); 00417 goto page_overflow; 00418 }else{ 00419 int i; 00420 i = mem0.aPageFree[--mem0.nPageFree]; 00421 sqlite3_mutex_leave(mem0.mutex); 00422 i *= sqlite3GlobalConfig.szPage; 00423 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n); 00424 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); 00425 p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i]; 00426 } 00427 } 00428 return p; 00429 00430 page_overflow: 00431 if( sqlite3GlobalConfig.bMemstat ){ 00432 sqlite3_mutex_enter(mem0.mutex); 00433 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n); 00434 n = mallocWithAlarm(n, &p); 00435 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n); 00436 sqlite3_mutex_leave(mem0.mutex); 00437 }else{ 00438 p = sqlite3GlobalConfig.m.xMalloc(n); 00439 } 00440 return p; 00441 } 00442 void sqlite3PageFree(void *p){ 00443 if( p ){ 00444 if( sqlite3GlobalConfig.pPage==0 00445 || p<sqlite3GlobalConfig.pPage 00446 || p>=(void*)mem0.aPageFree ){ 00447 /* In this case, the page allocation was obtained from a regular 00448 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory 00449 ** "overflow"). Free the block with sqlite3_mem_methods.xFree(). 00450 */ 00451 if( sqlite3GlobalConfig.bMemstat ){ 00452 int iSize = sqlite3MallocSize(p); 00453 sqlite3_mutex_enter(mem0.mutex); 00454 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); 00455 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 00456 sqlite3GlobalConfig.m.xFree(p); 00457 sqlite3_mutex_leave(mem0.mutex); 00458 }else{ 00459 sqlite3GlobalConfig.m.xFree(p); 00460 } 00461 }else{ 00462 /* The page allocation was allocated from the sqlite3GlobalConfig.pPage 00463 ** buffer. In this case all that is add the index of the page in 00464 ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored 00465 ** in the mem0.aPageFree[] array. 00466 */ 00467 int i; 00468 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage; 00469 i /= sqlite3GlobalConfig.szPage; 00470 assert( i>=0 && i<sqlite3GlobalConfig.nPage ); 00471 sqlite3_mutex_enter(mem0.mutex); 00472 assert( mem0.nPageFree<sqlite3GlobalConfig.nPage ); 00473 mem0.aPageFree[mem0.nPageFree++] = i; 00474 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); 00475 sqlite3_mutex_leave(mem0.mutex); 00476 #if !defined(NDEBUG) && 0 00477 /* Assert that a duplicate was not just inserted into aPageFree[]. */ 00478 for(i=0; i<mem0.nPageFree-1; i++){ 00479 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] ); 00480 } 00481 #endif 00482 } 00483 } 00484 } 00485 #endif 00486 00487 /* 00488 ** TRUE if p is a lookaside memory allocation from db 00489 */ 00490 #ifndef SQLITE_OMIT_LOOKASIDE 00491 static int isLookaside(sqlite3 *db, void *p){ 00492 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; 00493 } 00494 #else 00495 #define isLookaside(A,B) 0 00496 #endif 00497 00498 /* 00499 ** Return the size of a memory allocation previously obtained from 00500 ** sqlite3Malloc() or sqlite3_malloc(). 00501 */ 00502 int sqlite3MallocSize(void *p){ 00503 return sqlite3GlobalConfig.m.xSize(p); 00504 } 00505 int sqlite3DbMallocSize(sqlite3 *db, void *p){ 00506 if( isLookaside(db, p) ){ 00507 return db->lookaside.sz; 00508 }else{ 00509 return sqlite3GlobalConfig.m.xSize(p); 00510 } 00511 } 00512 00513 /* 00514 ** Free memory previously obtained from sqlite3Malloc(). 00515 */ 00516 EXPORT_C void sqlite3_free(void *p){ 00517 if( p==0 ) return; 00518 if( sqlite3GlobalConfig.bMemstat ){ 00519 sqlite3_mutex_enter(mem0.mutex); 00520 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 00521 sqlite3GlobalConfig.m.xFree(p); 00522 sqlite3_mutex_leave(mem0.mutex); 00523 }else{ 00524 sqlite3GlobalConfig.m.xFree(p); 00525 } 00526 } 00527 00528 /* 00529 ** Free memory that might be associated with a particular database 00530 ** connection. 00531 */ 00532 void sqlite3DbFree(sqlite3 *db, void *p){ 00533 if( isLookaside(db, p) ){ 00534 LookasideSlot *pBuf = (LookasideSlot*)p; 00535 pBuf->pNext = db->lookaside.pFree; 00536 db->lookaside.pFree = pBuf; 00537 db->lookaside.nOut--; 00538 }else{ 00539 sqlite3_free(p); 00540 } 00541 } 00542 00543 /* 00544 ** Change the size of an existing memory allocation 00545 */ 00546 void *sqlite3Realloc(void *pOld, int nBytes){ 00547 int nOld, nNew; 00548 void *pNew; 00549 if( pOld==0 ){ 00550 return sqlite3Malloc(nBytes); 00551 } 00552 if( nBytes<=0 ){ 00553 sqlite3_free(pOld); 00554 return 0; 00555 } 00556 nOld = sqlite3MallocSize(pOld); 00557 if( sqlite3GlobalConfig.bMemstat ){ 00558 sqlite3_mutex_enter(mem0.mutex); 00559 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); 00560 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); 00561 if( nOld==nNew ){ 00562 pNew = pOld; 00563 }else{ 00564 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 00565 mem0.alarmThreshold ){ 00566 sqlite3MallocAlarm(nNew-nOld); 00567 } 00568 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 00569 if( pNew==0 && mem0.alarmCallback ){ 00570 sqlite3MallocAlarm(nBytes); 00571 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 00572 } 00573 if( pNew ){ 00574 nNew = sqlite3MallocSize(pNew); 00575 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 00576 } 00577 } 00578 sqlite3_mutex_leave(mem0.mutex); 00579 }else{ 00580 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes); 00581 } 00582 return pNew; 00583 } 00584 00585 /* 00586 ** The public interface to sqlite3Realloc. Make sure that the memory 00587 ** subsystem is initialized prior to invoking sqliteRealloc. 00588 */ 00589 void *sqlite3_realloc(void *pOld, int n){ 00590 #ifndef SQLITE_OMIT_AUTOINIT 00591 if( sqlite3_initialize() ) return 0; 00592 #endif 00593 return sqlite3Realloc(pOld, n); 00594 } 00595 00596 00597 /* 00598 ** Allocate and zero memory. 00599 */ 00600 void *sqlite3MallocZero(int n){ 00601 void *p = sqlite3Malloc(n); 00602 if( p ){ 00603 memset(p, 0, n); 00604 } 00605 return p; 00606 } 00607 00608 /* 00609 ** Allocate and zero memory. If the allocation fails, make 00610 ** the mallocFailed flag in the connection pointer. 00611 */ 00612 void *sqlite3DbMallocZero(sqlite3 *db, int n){ 00613 void *p = sqlite3DbMallocRaw(db, n); 00614 if( p ){ 00615 memset(p, 0, n); 00616 } 00617 return p; 00618 } 00619 00620 /* 00621 ** Allocate and zero memory. If the allocation fails, make 00622 ** the mallocFailed flag in the connection pointer. 00623 ** 00624 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc 00625 ** failure on the same database connection) then always return 0. 00626 ** Hence for a particular database connection, once malloc starts 00627 ** failing, it fails consistently until mallocFailed is reset. 00628 ** This is an important assumption. There are many places in the 00629 ** code that do things like this: 00630 ** 00631 ** int *a = (int*)sqlite3DbMallocRaw(db, 100); 00632 ** int *b = (int*)sqlite3DbMallocRaw(db, 200); 00633 ** if( b ) a[10] = 9; 00634 ** 00635 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed 00636 ** that all prior mallocs (ex: "a") worked too. 00637 */ 00638 void *sqlite3DbMallocRaw(sqlite3 *db, int n){ 00639 void *p; 00640 #ifndef SQLITE_OMIT_LOOKASIDE 00641 if( db ){ 00642 LookasideSlot *pBuf; 00643 if( db->mallocFailed ){ 00644 return 0; 00645 } 00646 if( db->lookaside.bEnabled && n<=db->lookaside.sz 00647 && (pBuf = db->lookaside.pFree)!=0 ){ 00648 db->lookaside.pFree = pBuf->pNext; 00649 db->lookaside.nOut++; 00650 if( db->lookaside.nOut>db->lookaside.mxOut ){ 00651 db->lookaside.mxOut = db->lookaside.nOut; 00652 } 00653 return (void*)pBuf; 00654 } 00655 } 00656 #else 00657 if( db && db->mallocFailed ){ 00658 return 0; 00659 } 00660 #endif 00661 p = sqlite3Malloc(n); 00662 if( !p && db ){ 00663 db->mallocFailed = 1; 00664 } 00665 return p; 00666 } 00667 00668 /* 00669 ** Resize the block of memory pointed to by p to n bytes. If the 00670 ** resize fails, set the mallocFailed flag in the connection object. 00671 */ 00672 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 00673 void *pNew = 0; 00674 if( db->mallocFailed==0 ){ 00675 if( p==0 ){ 00676 return sqlite3DbMallocRaw(db, n); 00677 } 00678 if( isLookaside(db, p) ){ 00679 if( n<=db->lookaside.sz ){ 00680 return p; 00681 } 00682 pNew = sqlite3DbMallocRaw(db, n); 00683 if( pNew ){ 00684 memcpy(pNew, p, db->lookaside.sz); 00685 sqlite3DbFree(db, p); 00686 } 00687 }else{ 00688 pNew = sqlite3_realloc(p, n); 00689 if( !pNew ){ 00690 db->mallocFailed = 1; 00691 } 00692 } 00693 } 00694 return pNew; 00695 } 00696 00697 /* 00698 ** Attempt to reallocate p. If the reallocation fails, then free p 00699 ** and set the mallocFailed flag in the database connection. 00700 */ 00701 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 00702 void *pNew; 00703 pNew = sqlite3DbRealloc(db, p, n); 00704 if( !pNew ){ 00705 sqlite3DbFree(db, p); 00706 } 00707 return pNew; 00708 } 00709 00710 /* 00711 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 00712 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 00713 ** is because when memory debugging is turned on, these two functions are 00714 ** called via macros that record the current file and line number in the 00715 ** ThreadData structure. 00716 */ 00717 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 00718 char *zNew; 00719 size_t n; 00720 if( z==0 ){ 00721 return 0; 00722 } 00723 n = strlen(z)+1; 00724 assert( (n&0x7fffffff)==n ); 00725 zNew = sqlite3DbMallocRaw(db, (int)n); 00726 if( zNew ){ 00727 memcpy(zNew, z, n); 00728 } 00729 return zNew; 00730 } 00731 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 00732 char *zNew; 00733 if( z==0 ){ 00734 return 0; 00735 } 00736 assert( (n&0x7fffffff)==n ); 00737 zNew = sqlite3DbMallocRaw(db, n+1); 00738 if( zNew ){ 00739 memcpy(zNew, z, n); 00740 zNew[n] = 0; 00741 } 00742 return zNew; 00743 } 00744 00745 /* 00746 ** Create a string from the zFromat argument and the va_list that follows. 00747 ** Store the string in memory obtained from sqliteMalloc() and make *pz 00748 ** point to that string. 00749 */ 00750 void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ 00751 va_list ap; 00752 char *z; 00753 00754 va_start(ap, zFormat); 00755 z = sqlite3VMPrintf(db, zFormat, ap); 00756 va_end(ap); 00757 sqlite3DbFree(db, *pz); 00758 *pz = z; 00759 } 00760 00761 00762 /* 00763 ** This function must be called before exiting any API function (i.e. 00764 ** returning control to the user) that has called sqlite3_malloc or 00765 ** sqlite3_realloc. 00766 ** 00767 ** The returned value is normally a copy of the second argument to this 00768 ** function. However, if a malloc() failure has occured since the previous 00769 ** invocation SQLITE_NOMEM is returned instead. 00770 ** 00771 ** If the first argument, db, is not NULL and a malloc() error has occured, 00772 ** then the connection error-code (the value returned by sqlite3_errcode()) 00773 ** is set to SQLITE_NOMEM. 00774 */ 00775 int sqlite3ApiExit(sqlite3* db, int rc){ 00776 /* If the db handle is not NULL, then we must hold the connection handle 00777 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 00778 ** is unsafe, as is the call to sqlite3Error(). 00779 */ 00780 assert( !db || sqlite3_mutex_held(db->mutex) ); 00781 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){ 00782 sqlite3Error(db, SQLITE_NOMEM, 0); 00783 db->mallocFailed = 0; 00784 rc = SQLITE_NOMEM; 00785 } 00786 return rc & (db ? db->errMask : 0xff); 00787 }
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1