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