1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** Memory allocation functions used throughout sqlite. 14 */ 15 #include "sqliteInt.h" 16 #include <stdarg.h> 17 18 /* 19 ** This routine runs when the memory allocator sees that the 20 ** total memory allocation is about to exceed the soft heap 21 ** limit. 22 */ 23 static void softHeapLimitEnforcer( 24 void *NotUsed, 25 sqlite3_int64 NotUsed2, 26 int allocSize 27 ){ 28 UNUSED_PARAMETER2(NotUsed, NotUsed2); 29 sqlite3_release_memory(allocSize); 30 } 31 32 /* 33 ** Set the soft heap-size limit for the library. Passing a zero or 34 ** negative value indicates no limit. 35 */ 36 void sqlite3_soft_heap_limit(int n){ 37 sqlite3_uint64 iLimit; 38 int overage; 39 if( n<0 ){ 40 iLimit = 0; 41 }else{ 42 iLimit = n; 43 } 44 #ifndef SQLITE_OMIT_AUTOINIT 45 sqlite3_initialize(); 46 #endif 47 if( iLimit>0 ){ 48 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit); 49 }else{ 50 sqlite3MemoryAlarm(0, 0, 0); 51 } 52 overage = (int)(sqlite3_memory_used() - (i64)n); 53 if( overage>0 ){ 54 sqlite3_release_memory(overage); 55 } 56 } 57 58 /* 59 ** Attempt to release up to n bytes of non-essential memory currently 60 ** held by SQLite. An example of non-essential memory is memory used to 61 ** cache database pages that are not currently in use. 62 */ 63 int sqlite3_release_memory(int n){ 64 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 65 int nRet = 0; 66 nRet += sqlite3PcacheReleaseMemory(n-nRet); 67 return nRet; 68 #else 69 UNUSED_PARAMETER(n); 70 return SQLITE_OK; 71 #endif 72 } 73 74 /* 75 ** State information local to the memory allocation subsystem. 76 */ 77 static SQLITE_WSD struct Mem0Global { 78 /* Number of free pages for scratch and page-cache memory */ 79 u32 nScratchFree; 80 u32 nPageFree; 81 82 sqlite3_mutex *mutex; /* Mutex to serialize access */ 83 84 /* 85 ** The alarm callback and its arguments. The mem0.mutex lock will 86 ** be held while the callback is running. Recursive calls into 87 ** the memory subsystem are allowed, but no new callbacks will be 88 ** issued. 89 */ 90 sqlite3_int64 alarmThreshold; 91 void (*alarmCallback)(void*, sqlite3_int64,int); 92 void *alarmArg; 93 94 /* 95 ** Pointers to the end of sqlite3GlobalConfig.pScratch and 96 ** sqlite3GlobalConfig.pPage to a block of memory that records 97 ** which pages are available. 98 */ 99 u32 *aScratchFree; 100 u32 *aPageFree; 101 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 }; 102 103 #define mem0 GLOBAL(struct Mem0Global, mem0) 104 105 /* 106 ** Initialize the memory allocation subsystem. 107 */ 108 int sqlite3MallocInit(void){ 109 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 110 sqlite3MemSetDefault(); 111 } 112 memset(&mem0, 0, sizeof(mem0)); 113 if( sqlite3GlobalConfig.bCoreMutex ){ 114 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 115 } 116 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 117 && sqlite3GlobalConfig.nScratch>=0 ){ 118 int i; 119 sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4); 120 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch) 121 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch]; 122 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; } 123 mem0.nScratchFree = sqlite3GlobalConfig.nScratch; 124 }else{ 125 sqlite3GlobalConfig.pScratch = 0; 126 sqlite3GlobalConfig.szScratch = 0; 127 } 128 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512 129 && sqlite3GlobalConfig.nPage>=1 ){ 130 int i; 131 int overhead; 132 int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage); 133 int n = sqlite3GlobalConfig.nPage; 134 overhead = (4*n + sz - 1)/sz; 135 sqlite3GlobalConfig.nPage -= overhead; 136 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage) 137 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage]; 138 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; } 139 mem0.nPageFree = sqlite3GlobalConfig.nPage; 140 }else{ 141 sqlite3GlobalConfig.pPage = 0; 142 sqlite3GlobalConfig.szPage = 0; 143 } 144 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 145 } 146 147 /* 148 ** Deinitialize the memory allocation subsystem. 149 */ 150 void sqlite3MallocEnd(void){ 151 if( sqlite3GlobalConfig.m.xShutdown ){ 152 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 153 } 154 memset(&mem0, 0, sizeof(mem0)); 155 } 156 157 /* 158 ** Return the amount of memory currently checked out. 159 */ 160 sqlite3_int64 sqlite3_memory_used(void){ 161 int n, mx; 162 sqlite3_int64 res; 163 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0); 164 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */ 165 return res; 166 } 167 168 /* 169 ** Return the maximum amount of memory that has ever been 170 ** checked out since either the beginning of this process 171 ** or since the most recent reset. 172 */ 173 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 174 int n, mx; 175 sqlite3_int64 res; 176 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag); 177 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */ 178 return res; 179 } 180 181 /* 182 ** Change the alarm callback 183 */ 184 int sqlite3MemoryAlarm( 185 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 186 void *pArg, 187 sqlite3_int64 iThreshold 188 ){ 189 sqlite3_mutex_enter(mem0.mutex); 190 mem0.alarmCallback = xCallback; 191 mem0.alarmArg = pArg; 192 mem0.alarmThreshold = iThreshold; 193 sqlite3_mutex_leave(mem0.mutex); 194 return SQLITE_OK; 195 } 196 197 #ifndef SQLITE_OMIT_DEPRECATED 198 /* 199 ** Deprecated external interface. Internal/core SQLite code 200 ** should call sqlite3MemoryAlarm. 201 */ 202 int sqlite3_memory_alarm( 203 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 204 void *pArg, 205 sqlite3_int64 iThreshold 206 ){ 207 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold); 208 } 209 #endif 210 211 /* 212 ** Trigger the alarm 213 */ 214 static void sqlite3MallocAlarm(int nByte){ 215 void (*xCallback)(void*,sqlite3_int64,int); 216 sqlite3_int64 nowUsed; 217 void *pArg; 218 if( mem0.alarmCallback==0 ) return; 219 xCallback = mem0.alarmCallback; 220 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 221 pArg = mem0.alarmArg; 222 mem0.alarmCallback = 0; 223 sqlite3_mutex_leave(mem0.mutex); 224 xCallback(pArg, nowUsed, nByte); 225 sqlite3_mutex_enter(mem0.mutex); 226 mem0.alarmCallback = xCallback; 227 mem0.alarmArg = pArg; 228 } 229 230 /* 231 ** Do a memory allocation with statistics and alarms. Assume the 232 ** lock is already held. 233 */ 234 static int mallocWithAlarm(int n, void **pp){ 235 int nFull; 236 void *p; 237 assert( sqlite3_mutex_held(mem0.mutex) ); 238 nFull = sqlite3GlobalConfig.m.xRoundup(n); 239 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n); 240 if( mem0.alarmCallback!=0 ){ 241 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 242 if( nUsed+nFull >= mem0.alarmThreshold ){ 243 sqlite3MallocAlarm(nFull); 244 } 245 } 246 p = sqlite3GlobalConfig.m.xMalloc(nFull); 247 if( p==0 && mem0.alarmCallback ){ 248 sqlite3MallocAlarm(nFull); 249 p = sqlite3GlobalConfig.m.xMalloc(nFull); 250 } 251 if( p ){ 252 nFull = sqlite3MallocSize(p); 253 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); 254 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); 255 } 256 *pp = p; 257 return nFull; 258 } 259 260 /* 261 ** Allocate memory. This routine is like sqlite3_malloc() except that it 262 ** assumes the memory subsystem has already been initialized. 263 */ 264 void *sqlite3Malloc(int n){ 265 void *p; 266 if( n<=0 || n>=0x7fffff00 ){ 267 /* A memory allocation of a number of bytes which is near the maximum 268 ** signed integer value might cause an integer overflow inside of the 269 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving 270 ** 255 bytes of overhead. SQLite itself will never use anything near 271 ** this amount. The only way to reach the limit is with sqlite3_malloc() */ 272 p = 0; 273 }else if( sqlite3GlobalConfig.bMemstat ){ 274 sqlite3_mutex_enter(mem0.mutex); 275 mallocWithAlarm(n, &p); 276 sqlite3_mutex_leave(mem0.mutex); 277 }else{ 278 p = sqlite3GlobalConfig.m.xMalloc(n); 279 } 280 return p; 281 } 282 283 /* 284 ** This version of the memory allocation is for use by the application. 285 ** First make sure the memory subsystem is initialized, then do the 286 ** allocation. 287 */ 288 void *sqlite3_malloc(int n){ 289 #ifndef SQLITE_OMIT_AUTOINIT 290 if( sqlite3_initialize() ) return 0; 291 #endif 292 return sqlite3Malloc(n); 293 } 294 295 /* 296 ** Each thread may only have a single outstanding allocation from 297 ** xScratchMalloc(). We verify this constraint in the single-threaded 298 ** case by setting scratchAllocOut to 1 when an allocation 299 ** is outstanding clearing it when the allocation is freed. 300 */ 301 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 302 static int scratchAllocOut = 0; 303 #endif 304 305 306 /* 307 ** Allocate memory that is to be used and released right away. 308 ** This routine is similar to alloca() in that it is not intended 309 ** for situations where the memory might be held long-term. This 310 ** routine is intended to get memory to old large transient data 311 ** structures that would not normally fit on the stack of an 312 ** embedded processor. 313 */ 314 void *sqlite3ScratchMalloc(int n){ 315 void *p; 316 assert( n>0 ); 317 318 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 319 /* Verify that no more than two scratch allocation per thread 320 ** is outstanding at one time. (This is only checked in the 321 ** single-threaded case since checking in the multi-threaded case 322 ** would be much more complicated.) */ 323 assert( scratchAllocOut<=1 ); 324 #endif 325 326 if( sqlite3GlobalConfig.szScratch<n ){ 327 goto scratch_overflow; 328 }else{ 329 sqlite3_mutex_enter(mem0.mutex); 330 if( mem0.nScratchFree==0 ){ 331 sqlite3_mutex_leave(mem0.mutex); 332 goto scratch_overflow; 333 }else{ 334 int i; 335 i = mem0.aScratchFree[--mem0.nScratchFree]; 336 i *= sqlite3GlobalConfig.szScratch; 337 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1); 338 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 339 sqlite3_mutex_leave(mem0.mutex); 340 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i]; 341 assert( (((u8*)p - (u8*)0) & 7)==0 ); 342 } 343 } 344 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 345 scratchAllocOut = p!=0; 346 #endif 347 348 return p; 349 350 scratch_overflow: 351 if( sqlite3GlobalConfig.bMemstat ){ 352 sqlite3_mutex_enter(mem0.mutex); 353 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n); 354 n = mallocWithAlarm(n, &p); 355 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n); 356 sqlite3_mutex_leave(mem0.mutex); 357 }else{ 358 p = sqlite3GlobalConfig.m.xMalloc(n); 359 } 360 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); 361 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 362 scratchAllocOut = p!=0; 363 #endif 364 return p; 365 } 366 void sqlite3ScratchFree(void *p){ 367 if( p ){ 368 if( sqlite3GlobalConfig.pScratch==0 369 || p<sqlite3GlobalConfig.pScratch 370 || p>=(void*)mem0.aScratchFree ){ 371 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); 372 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); 373 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 374 if( sqlite3GlobalConfig.bMemstat ){ 375 int iSize = sqlite3MallocSize(p); 376 sqlite3_mutex_enter(mem0.mutex); 377 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); 378 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); 379 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); 380 sqlite3GlobalConfig.m.xFree(p); 381 sqlite3_mutex_leave(mem0.mutex); 382 }else{ 383 sqlite3GlobalConfig.m.xFree(p); 384 } 385 }else{ 386 int i; 387 i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch); 388 i /= sqlite3GlobalConfig.szScratch; 389 assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); 390 sqlite3_mutex_enter(mem0.mutex); 391 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); 392 mem0.aScratchFree[mem0.nScratchFree++] = i; 393 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); 394 sqlite3_mutex_leave(mem0.mutex); 395 396 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 397 /* Verify that no more than two scratch allocation per thread 398 ** is outstanding at one time. (This is only checked in the 399 ** single-threaded case since checking in the multi-threaded case 400 ** would be much more complicated.) */ 401 assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); 402 scratchAllocOut = 0; 403 #endif 404 405 } 406 } 407 } 408 409 /* 410 ** TRUE if p is a lookaside memory allocation from db 411 */ 412 #ifndef SQLITE_OMIT_LOOKASIDE 413 static int isLookaside(sqlite3 *db, void *p){ 414 return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; 415 } 416 #else 417 #define isLookaside(A,B) 0 418 #endif 419 420 /* 421 ** Return the size of a memory allocation previously obtained from 422 ** sqlite3Malloc() or sqlite3_malloc(). 423 */ 424 int sqlite3MallocSize(void *p){ 425 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 426 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); 427 return sqlite3GlobalConfig.m.xSize(p); 428 } 429 int sqlite3DbMallocSize(sqlite3 *db, void *p){ 430 assert( db==0 || sqlite3_mutex_held(db->mutex) ); 431 if( db && isLookaside(db, p) ){ 432 return db->lookaside.sz; 433 }else{ 434 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); 435 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); 436 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 437 return sqlite3GlobalConfig.m.xSize(p); 438 } 439 } 440 441 /* 442 ** Free memory previously obtained from sqlite3Malloc(). 443 */ 444 void sqlite3_free(void *p){ 445 if( p==0 ) return; 446 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); 447 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 448 if( sqlite3GlobalConfig.bMemstat ){ 449 sqlite3_mutex_enter(mem0.mutex); 450 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); 451 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); 452 sqlite3GlobalConfig.m.xFree(p); 453 sqlite3_mutex_leave(mem0.mutex); 454 }else{ 455 sqlite3GlobalConfig.m.xFree(p); 456 } 457 } 458 459 /* 460 ** Free memory that might be associated with a particular database 461 ** connection. 462 */ 463 void sqlite3DbFree(sqlite3 *db, void *p){ 464 assert( db==0 || sqlite3_mutex_held(db->mutex) ); 465 if( db ){ 466 if( db->pnBytesFreed ){ 467 *db->pnBytesFreed += sqlite3DbMallocSize(db, p); 468 return; 469 } 470 if( isLookaside(db, p) ){ 471 LookasideSlot *pBuf = (LookasideSlot*)p; 472 pBuf->pNext = db->lookaside.pFree; 473 db->lookaside.pFree = pBuf; 474 db->lookaside.nOut--; 475 return; 476 } 477 } 478 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); 479 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); 480 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 481 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 482 sqlite3_free(p); 483 } 484 485 /* 486 ** Change the size of an existing memory allocation 487 */ 488 void *sqlite3Realloc(void *pOld, int nBytes){ 489 int nOld, nNew; 490 void *pNew; 491 if( pOld==0 ){ 492 return sqlite3Malloc(nBytes); 493 } 494 if( nBytes<=0 ){ 495 sqlite3_free(pOld); 496 return 0; 497 } 498 if( nBytes>=0x7fffff00 ){ 499 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ 500 return 0; 501 } 502 nOld = sqlite3MallocSize(pOld); 503 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes); 504 if( nOld==nNew ){ 505 pNew = pOld; 506 }else if( sqlite3GlobalConfig.bMemstat ){ 507 sqlite3_mutex_enter(mem0.mutex); 508 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes); 509 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 510 mem0.alarmThreshold ){ 511 sqlite3MallocAlarm(nNew-nOld); 512 } 513 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); 514 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); 515 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 516 if( pNew==0 && mem0.alarmCallback ){ 517 sqlite3MallocAlarm(nBytes); 518 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 519 } 520 if( pNew ){ 521 nNew = sqlite3MallocSize(pNew); 522 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 523 } 524 sqlite3_mutex_leave(mem0.mutex); 525 }else{ 526 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 527 } 528 return pNew; 529 } 530 531 /* 532 ** The public interface to sqlite3Realloc. Make sure that the memory 533 ** subsystem is initialized prior to invoking sqliteRealloc. 534 */ 535 void *sqlite3_realloc(void *pOld, int n){ 536 #ifndef SQLITE_OMIT_AUTOINIT 537 if( sqlite3_initialize() ) return 0; 538 #endif 539 return sqlite3Realloc(pOld, n); 540 } 541 542 543 /* 544 ** Allocate and zero memory. 545 */ 546 void *sqlite3MallocZero(int n){ 547 void *p = sqlite3Malloc(n); 548 if( p ){ 549 memset(p, 0, n); 550 } 551 return p; 552 } 553 554 /* 555 ** Allocate and zero memory. If the allocation fails, make 556 ** the mallocFailed flag in the connection pointer. 557 */ 558 void *sqlite3DbMallocZero(sqlite3 *db, int n){ 559 void *p = sqlite3DbMallocRaw(db, n); 560 if( p ){ 561 memset(p, 0, n); 562 } 563 return p; 564 } 565 566 /* 567 ** Allocate and zero memory. If the allocation fails, make 568 ** the mallocFailed flag in the connection pointer. 569 ** 570 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc 571 ** failure on the same database connection) then always return 0. 572 ** Hence for a particular database connection, once malloc starts 573 ** failing, it fails consistently until mallocFailed is reset. 574 ** This is an important assumption. There are many places in the 575 ** code that do things like this: 576 ** 577 ** int *a = (int*)sqlite3DbMallocRaw(db, 100); 578 ** int *b = (int*)sqlite3DbMallocRaw(db, 200); 579 ** if( b ) a[10] = 9; 580 ** 581 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed 582 ** that all prior mallocs (ex: "a") worked too. 583 */ 584 void *sqlite3DbMallocRaw(sqlite3 *db, int n){ 585 void *p; 586 assert( db==0 || sqlite3_mutex_held(db->mutex) ); 587 assert( db==0 || db->pnBytesFreed==0 ); 588 #ifndef SQLITE_OMIT_LOOKASIDE 589 if( db ){ 590 LookasideSlot *pBuf; 591 if( db->mallocFailed ){ 592 return 0; 593 } 594 if( db->lookaside.bEnabled && n<=db->lookaside.sz 595 && (pBuf = db->lookaside.pFree)!=0 ){ 596 db->lookaside.pFree = pBuf->pNext; 597 db->lookaside.nOut++; 598 if( db->lookaside.nOut>db->lookaside.mxOut ){ 599 db->lookaside.mxOut = db->lookaside.nOut; 600 } 601 return (void*)pBuf; 602 } 603 } 604 #else 605 if( db && db->mallocFailed ){ 606 return 0; 607 } 608 #endif 609 p = sqlite3Malloc(n); 610 if( !p && db ){ 611 db->mallocFailed = 1; 612 } 613 sqlite3MemdebugSetType(p, MEMTYPE_DB | 614 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 615 return p; 616 } 617 618 /* 619 ** Resize the block of memory pointed to by p to n bytes. If the 620 ** resize fails, set the mallocFailed flag in the connection object. 621 */ 622 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ 623 void *pNew = 0; 624 assert( db!=0 ); 625 assert( sqlite3_mutex_held(db->mutex) ); 626 if( db->mallocFailed==0 ){ 627 if( p==0 ){ 628 return sqlite3DbMallocRaw(db, n); 629 } 630 if( isLookaside(db, p) ){ 631 if( n<=db->lookaside.sz ){ 632 return p; 633 } 634 pNew = sqlite3DbMallocRaw(db, n); 635 if( pNew ){ 636 memcpy(pNew, p, db->lookaside.sz); 637 sqlite3DbFree(db, p); 638 } 639 }else{ 640 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); 641 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); 642 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 643 pNew = sqlite3_realloc(p, n); 644 if( !pNew ){ 645 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP); 646 db->mallocFailed = 1; 647 } 648 sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 649 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 650 } 651 } 652 return pNew; 653 } 654 655 /* 656 ** Attempt to reallocate p. If the reallocation fails, then free p 657 ** and set the mallocFailed flag in the database connection. 658 */ 659 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ 660 void *pNew; 661 pNew = sqlite3DbRealloc(db, p, n); 662 if( !pNew ){ 663 sqlite3DbFree(db, p); 664 } 665 return pNew; 666 } 667 668 /* 669 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 670 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 671 ** is because when memory debugging is turned on, these two functions are 672 ** called via macros that record the current file and line number in the 673 ** ThreadData structure. 674 */ 675 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 676 char *zNew; 677 size_t n; 678 if( z==0 ){ 679 return 0; 680 } 681 n = sqlite3Strlen30(z) + 1; 682 assert( (n&0x7fffffff)==n ); 683 zNew = sqlite3DbMallocRaw(db, (int)n); 684 if( zNew ){ 685 memcpy(zNew, z, n); 686 } 687 return zNew; 688 } 689 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){ 690 char *zNew; 691 if( z==0 ){ 692 return 0; 693 } 694 assert( (n&0x7fffffff)==n ); 695 zNew = sqlite3DbMallocRaw(db, n+1); 696 if( zNew ){ 697 memcpy(zNew, z, n); 698 zNew[n] = 0; 699 } 700 return zNew; 701 } 702 703 /* 704 ** Create a string from the zFromat argument and the va_list that follows. 705 ** Store the string in memory obtained from sqliteMalloc() and make *pz 706 ** point to that string. 707 */ 708 void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){ 709 va_list ap; 710 char *z; 711 712 va_start(ap, zFormat); 713 z = sqlite3VMPrintf(db, zFormat, ap); 714 va_end(ap); 715 sqlite3DbFree(db, *pz); 716 *pz = z; 717 } 718 719 720 /* 721 ** This function must be called before exiting any API function (i.e. 722 ** returning control to the user) that has called sqlite3_malloc or 723 ** sqlite3_realloc. 724 ** 725 ** The returned value is normally a copy of the second argument to this 726 ** function. However, if a malloc() failure has occurred since the previous 727 ** invocation SQLITE_NOMEM is returned instead. 728 ** 729 ** If the first argument, db, is not NULL and a malloc() error has occurred, 730 ** then the connection error-code (the value returned by sqlite3_errcode()) 731 ** is set to SQLITE_NOMEM. 732 */ 733 int sqlite3ApiExit(sqlite3* db, int rc){ 734 /* If the db handle is not NULL, then we must hold the connection handle 735 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 736 ** is unsafe, as is the call to sqlite3Error(). 737 */ 738 assert( !db || sqlite3_mutex_held(db->mutex) ); 739 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){ 740 sqlite3Error(db, SQLITE_NOMEM, 0); 741 db->mallocFailed = 0; 742 rc = SQLITE_NOMEM; 743 } 744 return rc & (db ? db->errMask : 0xff); 745 } 746