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 ** Attempt to release up to n bytes of non-essential memory currently 20 ** held by SQLite. An example of non-essential memory is memory used to 21 ** cache database pages that are not currently in use. 22 */ 23 int sqlite3_release_memory(int n){ 24 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 25 return sqlite3PcacheReleaseMemory(n); 26 #else 27 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine 28 ** is a no-op returning zero if SQLite is not compiled with 29 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ 30 UNUSED_PARAMETER(n); 31 return 0; 32 #endif 33 } 34 35 /* 36 ** An instance of the following object records the location of 37 ** each unused scratch buffer. 38 */ 39 typedef struct ScratchFreeslot { 40 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ 41 } ScratchFreeslot; 42 43 /* 44 ** State information local to the memory allocation subsystem. 45 */ 46 static SQLITE_WSD struct Mem0Global { 47 sqlite3_mutex *mutex; /* Mutex to serialize access */ 48 sqlite3_int64 alarmThreshold; /* The soft heap limit */ 49 50 /* 51 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory 52 ** (so that a range test can be used to determine if an allocation 53 ** being freed came from pScratch) and a pointer to the list of 54 ** unused scratch allocations. 55 */ 56 void *pScratchEnd; 57 ScratchFreeslot *pScratchFree; 58 u32 nScratchFree; 59 60 /* 61 ** True if heap is nearly "full" where "full" is defined by the 62 ** sqlite3_soft_heap_limit() setting. 63 */ 64 int nearlyFull; 65 } mem0 = { 0, 0, 0, 0, 0, 0 }; 66 67 #define mem0 GLOBAL(struct Mem0Global, mem0) 68 69 /* 70 ** Return the memory allocator mutex. sqlite3_status() needs it. 71 */ 72 sqlite3_mutex *sqlite3MallocMutex(void){ 73 return mem0.mutex; 74 } 75 76 #ifndef SQLITE_OMIT_DEPRECATED 77 /* 78 ** Deprecated external interface. It used to set an alarm callback 79 ** that was invoked when memory usage grew too large. Now it is a 80 ** no-op. 81 */ 82 int sqlite3_memory_alarm( 83 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), 84 void *pArg, 85 sqlite3_int64 iThreshold 86 ){ 87 (void)xCallback; 88 (void)pArg; 89 (void)iThreshold; 90 return SQLITE_OK; 91 } 92 #endif 93 94 /* 95 ** Set the soft heap-size limit for the library. Passing a zero or 96 ** negative value indicates no limit. 97 */ 98 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){ 99 sqlite3_int64 priorLimit; 100 sqlite3_int64 excess; 101 sqlite3_int64 nUsed; 102 #ifndef SQLITE_OMIT_AUTOINIT 103 int rc = sqlite3_initialize(); 104 if( rc ) return -1; 105 #endif 106 sqlite3_mutex_enter(mem0.mutex); 107 priorLimit = mem0.alarmThreshold; 108 if( n<0 ){ 109 sqlite3_mutex_leave(mem0.mutex); 110 return priorLimit; 111 } 112 mem0.alarmThreshold = n; 113 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 114 mem0.nearlyFull = (n>0 && n<=nUsed); 115 sqlite3_mutex_leave(mem0.mutex); 116 excess = sqlite3_memory_used() - n; 117 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); 118 return priorLimit; 119 } 120 void sqlite3_soft_heap_limit(int n){ 121 if( n<0 ) n = 0; 122 sqlite3_soft_heap_limit64(n); 123 } 124 125 /* 126 ** Initialize the memory allocation subsystem. 127 */ 128 int sqlite3MallocInit(void){ 129 int rc; 130 if( sqlite3GlobalConfig.m.xMalloc==0 ){ 131 sqlite3MemSetDefault(); 132 } 133 memset(&mem0, 0, sizeof(mem0)); 134 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); 135 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 136 && sqlite3GlobalConfig.nScratch>0 ){ 137 int i, n, sz; 138 ScratchFreeslot *pSlot; 139 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); 140 sqlite3GlobalConfig.szScratch = sz; 141 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; 142 n = sqlite3GlobalConfig.nScratch; 143 mem0.pScratchFree = pSlot; 144 mem0.nScratchFree = n; 145 for(i=0; i<n-1; i++){ 146 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); 147 pSlot = pSlot->pNext; 148 } 149 pSlot->pNext = 0; 150 mem0.pScratchEnd = (void*)&pSlot[1]; 151 }else{ 152 mem0.pScratchEnd = 0; 153 sqlite3GlobalConfig.pScratch = 0; 154 sqlite3GlobalConfig.szScratch = 0; 155 sqlite3GlobalConfig.nScratch = 0; 156 } 157 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 158 || sqlite3GlobalConfig.nPage<=0 ){ 159 sqlite3GlobalConfig.pPage = 0; 160 sqlite3GlobalConfig.szPage = 0; 161 } 162 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); 163 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); 164 return rc; 165 } 166 167 /* 168 ** Return true if the heap is currently under memory pressure - in other 169 ** words if the amount of heap used is close to the limit set by 170 ** sqlite3_soft_heap_limit(). 171 */ 172 int sqlite3HeapNearlyFull(void){ 173 return mem0.nearlyFull; 174 } 175 176 /* 177 ** Deinitialize the memory allocation subsystem. 178 */ 179 void sqlite3MallocEnd(void){ 180 if( sqlite3GlobalConfig.m.xShutdown ){ 181 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); 182 } 183 memset(&mem0, 0, sizeof(mem0)); 184 } 185 186 /* 187 ** Return the amount of memory currently checked out. 188 */ 189 sqlite3_int64 sqlite3_memory_used(void){ 190 sqlite3_int64 res, mx; 191 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0); 192 return res; 193 } 194 195 /* 196 ** Return the maximum amount of memory that has ever been 197 ** checked out since either the beginning of this process 198 ** or since the most recent reset. 199 */ 200 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ 201 sqlite3_int64 res, mx; 202 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag); 203 return mx; 204 } 205 206 /* 207 ** Trigger the alarm 208 */ 209 static void sqlite3MallocAlarm(int nByte){ 210 if( mem0.alarmThreshold<=0 ) return; 211 sqlite3_mutex_leave(mem0.mutex); 212 sqlite3_release_memory(nByte); 213 sqlite3_mutex_enter(mem0.mutex); 214 } 215 216 /* 217 ** Do a memory allocation with statistics and alarms. Assume the 218 ** lock is already held. 219 */ 220 static void mallocWithAlarm(int n, void **pp){ 221 void *p; 222 int nFull; 223 assert( sqlite3_mutex_held(mem0.mutex) ); 224 assert( n>0 ); 225 226 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal 227 ** implementation of malloc_good_size(), which must be called in debug 228 ** mode and specifically when the DMD "Dark Matter Detector" is enabled 229 ** or else a crash results. Hence, do not attempt to optimize out the 230 ** following xRoundup() call. */ 231 nFull = sqlite3GlobalConfig.m.xRoundup(n); 232 233 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); 234 if( mem0.alarmThreshold>0 ){ 235 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); 236 if( nUsed >= mem0.alarmThreshold - nFull ){ 237 mem0.nearlyFull = 1; 238 sqlite3MallocAlarm(nFull); 239 }else{ 240 mem0.nearlyFull = 0; 241 } 242 } 243 p = sqlite3GlobalConfig.m.xMalloc(nFull); 244 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 245 if( p==0 && mem0.alarmThreshold>0 ){ 246 sqlite3MallocAlarm(nFull); 247 p = sqlite3GlobalConfig.m.xMalloc(nFull); 248 } 249 #endif 250 if( p ){ 251 nFull = sqlite3MallocSize(p); 252 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); 253 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); 254 } 255 *pp = p; 256 } 257 258 /* 259 ** Allocate memory. This routine is like sqlite3_malloc() except that it 260 ** assumes the memory subsystem has already been initialized. 261 */ 262 void *sqlite3Malloc(u64 n){ 263 void *p; 264 if( n==0 || n>=0x7fffff00 ){ 265 /* A memory allocation of a number of bytes which is near the maximum 266 ** signed integer value might cause an integer overflow inside of the 267 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving 268 ** 255 bytes of overhead. SQLite itself will never use anything near 269 ** this amount. The only way to reach the limit is with sqlite3_malloc() */ 270 p = 0; 271 }else if( sqlite3GlobalConfig.bMemstat ){ 272 sqlite3_mutex_enter(mem0.mutex); 273 mallocWithAlarm((int)n, &p); 274 sqlite3_mutex_leave(mem0.mutex); 275 }else{ 276 p = sqlite3GlobalConfig.m.xMalloc((int)n); 277 } 278 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ 279 return p; 280 } 281 282 /* 283 ** This version of the memory allocation is for use by the application. 284 ** First make sure the memory subsystem is initialized, then do the 285 ** allocation. 286 */ 287 void *sqlite3_malloc(int n){ 288 #ifndef SQLITE_OMIT_AUTOINIT 289 if( sqlite3_initialize() ) return 0; 290 #endif 291 return n<=0 ? 0 : sqlite3Malloc(n); 292 } 293 void *sqlite3_malloc64(sqlite3_uint64 n){ 294 #ifndef SQLITE_OMIT_AUTOINIT 295 if( sqlite3_initialize() ) return 0; 296 #endif 297 return sqlite3Malloc(n); 298 } 299 300 /* 301 ** Each thread may only have a single outstanding allocation from 302 ** xScratchMalloc(). We verify this constraint in the single-threaded 303 ** case by setting scratchAllocOut to 1 when an allocation 304 ** is outstanding clearing it when the allocation is freed. 305 */ 306 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 307 static int scratchAllocOut = 0; 308 #endif 309 310 311 /* 312 ** Allocate memory that is to be used and released right away. 313 ** This routine is similar to alloca() in that it is not intended 314 ** for situations where the memory might be held long-term. This 315 ** routine is intended to get memory to old large transient data 316 ** structures that would not normally fit on the stack of an 317 ** embedded processor. 318 */ 319 void *sqlite3ScratchMalloc(int n){ 320 void *p; 321 assert( n>0 ); 322 323 sqlite3_mutex_enter(mem0.mutex); 324 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n); 325 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ 326 p = mem0.pScratchFree; 327 mem0.pScratchFree = mem0.pScratchFree->pNext; 328 mem0.nScratchFree--; 329 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); 330 sqlite3_mutex_leave(mem0.mutex); 331 }else{ 332 sqlite3_mutex_leave(mem0.mutex); 333 p = sqlite3Malloc(n); 334 if( sqlite3GlobalConfig.bMemstat && p ){ 335 sqlite3_mutex_enter(mem0.mutex); 336 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); 337 sqlite3_mutex_leave(mem0.mutex); 338 } 339 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); 340 } 341 assert( sqlite3_mutex_notheld(mem0.mutex) ); 342 343 344 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 345 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch 346 ** buffers per thread. 347 ** 348 ** This can only be checked in single-threaded mode. 349 */ 350 assert( scratchAllocOut==0 ); 351 if( p ) scratchAllocOut++; 352 #endif 353 354 return p; 355 } 356 void sqlite3ScratchFree(void *p){ 357 if( p ){ 358 359 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) 360 /* Verify that no more than two scratch allocation per thread 361 ** is outstanding at one time. (This is only checked in the 362 ** single-threaded case since checking in the multi-threaded case 363 ** would be much more complicated.) */ 364 assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); 365 scratchAllocOut--; 366 #endif 367 368 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){ 369 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ 370 ScratchFreeslot *pSlot; 371 pSlot = (ScratchFreeslot*)p; 372 sqlite3_mutex_enter(mem0.mutex); 373 pSlot->pNext = mem0.pScratchFree; 374 mem0.pScratchFree = pSlot; 375 mem0.nScratchFree++; 376 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); 377 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); 378 sqlite3_mutex_leave(mem0.mutex); 379 }else{ 380 /* Release memory back to the heap */ 381 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); 382 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); 383 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 384 if( sqlite3GlobalConfig.bMemstat ){ 385 int iSize = sqlite3MallocSize(p); 386 sqlite3_mutex_enter(mem0.mutex); 387 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); 388 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); 389 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); 390 sqlite3GlobalConfig.m.xFree(p); 391 sqlite3_mutex_leave(mem0.mutex); 392 }else{ 393 sqlite3GlobalConfig.m.xFree(p); 394 } 395 } 396 } 397 } 398 399 /* 400 ** TRUE if p is a lookaside memory allocation from db 401 */ 402 #ifndef SQLITE_OMIT_LOOKASIDE 403 static int isLookaside(sqlite3 *db, void *p){ 404 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); 405 } 406 #else 407 #define isLookaside(A,B) 0 408 #endif 409 410 /* 411 ** Return the size of a memory allocation previously obtained from 412 ** sqlite3Malloc() or sqlite3_malloc(). 413 */ 414 int sqlite3MallocSize(void *p){ 415 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 416 return sqlite3GlobalConfig.m.xSize(p); 417 } 418 int sqlite3DbMallocSize(sqlite3 *db, void *p){ 419 assert( p!=0 ); 420 if( db==0 || !isLookaside(db,p) ){ 421 #ifdef SQLITE_DEBUG 422 if( db==0 ){ 423 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 424 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 425 }else{ 426 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 427 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 428 } 429 #endif 430 return sqlite3GlobalConfig.m.xSize(p); 431 }else{ 432 assert( sqlite3_mutex_held(db->mutex) ); 433 return db->lookaside.sz; 434 } 435 } 436 sqlite3_uint64 sqlite3_msize(void *p){ 437 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 438 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 439 return p ? sqlite3GlobalConfig.m.xSize(p) : 0; 440 } 441 442 /* 443 ** Free memory previously obtained from sqlite3Malloc(). 444 */ 445 void sqlite3_free(void *p){ 446 if( p==0 ) return; /* IMP: R-49053-54554 */ 447 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); 448 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); 449 if( sqlite3GlobalConfig.bMemstat ){ 450 sqlite3_mutex_enter(mem0.mutex); 451 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); 452 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); 453 sqlite3GlobalConfig.m.xFree(p); 454 sqlite3_mutex_leave(mem0.mutex); 455 }else{ 456 sqlite3GlobalConfig.m.xFree(p); 457 } 458 } 459 460 /* 461 ** Add the size of memory allocation "p" to the count in 462 ** *db->pnBytesFreed. 463 */ 464 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ 465 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); 466 } 467 468 /* 469 ** Free memory that might be associated with a particular database 470 ** connection. 471 */ 472 void sqlite3DbFree(sqlite3 *db, void *p){ 473 assert( db==0 || sqlite3_mutex_held(db->mutex) ); 474 if( p==0 ) return; 475 if( db ){ 476 if( db->pnBytesFreed ){ 477 measureAllocationSize(db, p); 478 return; 479 } 480 if( isLookaside(db, p) ){ 481 LookasideSlot *pBuf = (LookasideSlot*)p; 482 #ifdef SQLITE_DEBUG 483 /* Trash all content in the buffer being freed */ 484 memset(p, 0xaa, db->lookaside.sz); 485 #endif 486 pBuf->pNext = db->lookaside.pFree; 487 db->lookaside.pFree = pBuf; 488 db->lookaside.nOut--; 489 return; 490 } 491 } 492 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 493 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 494 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); 495 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 496 sqlite3_free(p); 497 } 498 499 /* 500 ** Change the size of an existing memory allocation 501 */ 502 void *sqlite3Realloc(void *pOld, u64 nBytes){ 503 int nOld, nNew, nDiff; 504 void *pNew; 505 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); 506 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); 507 if( pOld==0 ){ 508 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ 509 } 510 if( nBytes==0 ){ 511 sqlite3_free(pOld); /* IMP: R-26507-47431 */ 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((int)nBytes); 523 if( nOld==nNew ){ 524 pNew = pOld; 525 }else if( sqlite3GlobalConfig.bMemstat ){ 526 sqlite3_mutex_enter(mem0.mutex); 527 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); 528 nDiff = nNew - nOld; 529 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 530 mem0.alarmThreshold-nDiff ){ 531 sqlite3MallocAlarm(nDiff); 532 } 533 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 534 if( pNew==0 && mem0.alarmThreshold>0 ){ 535 sqlite3MallocAlarm((int)nBytes); 536 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 537 } 538 if( pNew ){ 539 nNew = sqlite3MallocSize(pNew); 540 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); 541 } 542 sqlite3_mutex_leave(mem0.mutex); 543 }else{ 544 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); 545 } 546 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ 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 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ 559 return sqlite3Realloc(pOld, n); 560 } 561 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ 562 #ifndef SQLITE_OMIT_AUTOINIT 563 if( sqlite3_initialize() ) return 0; 564 #endif 565 return sqlite3Realloc(pOld, n); 566 } 567 568 569 /* 570 ** Allocate and zero memory. 571 */ 572 void *sqlite3MallocZero(u64 n){ 573 void *p = sqlite3Malloc(n); 574 if( p ){ 575 memset(p, 0, (size_t)n); 576 } 577 return p; 578 } 579 580 /* 581 ** Allocate and zero memory. If the allocation fails, make 582 ** the mallocFailed flag in the connection pointer. 583 */ 584 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ 585 void *p; 586 testcase( db==0 ); 587 p = sqlite3DbMallocRaw(db, n); 588 if( p ) memset(p, 0, (size_t)n); 589 return p; 590 } 591 592 593 /* Finish the work of sqlite3DbMallocRawNN for the unusual and 594 ** slower case when the allocation cannot be fulfilled using lookaside. 595 */ 596 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){ 597 void *p; 598 assert( db!=0 ); 599 p = sqlite3Malloc(n); 600 if( !p ) sqlite3OomFault(db); 601 sqlite3MemdebugSetType(p, 602 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); 603 return p; 604 } 605 606 /* 607 ** Allocate memory, either lookaside (if possible) or heap. 608 ** If the allocation fails, set the mallocFailed flag in 609 ** the connection pointer. 610 ** 611 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc 612 ** failure on the same database connection) then always return 0. 613 ** Hence for a particular database connection, once malloc starts 614 ** failing, it fails consistently until mallocFailed is reset. 615 ** This is an important assumption. There are many places in the 616 ** code that do things like this: 617 ** 618 ** int *a = (int*)sqlite3DbMallocRaw(db, 100); 619 ** int *b = (int*)sqlite3DbMallocRaw(db, 200); 620 ** if( b ) a[10] = 9; 621 ** 622 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed 623 ** that all prior mallocs (ex: "a") worked too. 624 ** 625 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is 626 ** not a NULL pointer. 627 */ 628 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ 629 void *p; 630 if( db ) return sqlite3DbMallocRawNN(db, n); 631 p = sqlite3Malloc(n); 632 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 633 return p; 634 } 635 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){ 636 #ifndef SQLITE_OMIT_LOOKASIDE 637 LookasideSlot *pBuf; 638 assert( db!=0 ); 639 assert( sqlite3_mutex_held(db->mutex) ); 640 assert( db->pnBytesFreed==0 ); 641 if( db->lookaside.bDisable==0 ){ 642 assert( db->mallocFailed==0 ); 643 if( n>db->lookaside.sz ){ 644 db->lookaside.anStat[1]++; 645 }else if( (pBuf = db->lookaside.pFree)==0 ){ 646 db->lookaside.anStat[2]++; 647 }else{ 648 db->lookaside.pFree = pBuf->pNext; 649 db->lookaside.nOut++; 650 db->lookaside.anStat[0]++; 651 if( db->lookaside.nOut>db->lookaside.mxOut ){ 652 db->lookaside.mxOut = db->lookaside.nOut; 653 } 654 return (void*)pBuf; 655 } 656 }else if( db->mallocFailed ){ 657 return 0; 658 } 659 #else 660 assert( db!=0 ); 661 assert( sqlite3_mutex_held(db->mutex) ); 662 assert( db->pnBytesFreed==0 ); 663 if( db->mallocFailed ){ 664 return 0; 665 } 666 #endif 667 return dbMallocRawFinish(db, n); 668 } 669 670 /* Forward declaration */ 671 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n); 672 673 /* 674 ** Resize the block of memory pointed to by p to n bytes. If the 675 ** resize fails, set the mallocFailed flag in the connection object. 676 */ 677 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ 678 assert( db!=0 ); 679 if( p==0 ) return sqlite3DbMallocRawNN(db, n); 680 assert( sqlite3_mutex_held(db->mutex) ); 681 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p; 682 return dbReallocFinish(db, p, n); 683 } 684 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){ 685 void *pNew = 0; 686 assert( db!=0 ); 687 assert( p!=0 ); 688 if( db->mallocFailed==0 ){ 689 if( isLookaside(db, p) ){ 690 pNew = sqlite3DbMallocRawNN(db, n); 691 if( pNew ){ 692 memcpy(pNew, p, db->lookaside.sz); 693 sqlite3DbFree(db, p); 694 } 695 }else{ 696 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 697 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); 698 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); 699 pNew = sqlite3_realloc64(p, n); 700 if( !pNew ){ 701 sqlite3OomFault(db); 702 } 703 sqlite3MemdebugSetType(pNew, 704 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); 705 } 706 } 707 return pNew; 708 } 709 710 /* 711 ** Attempt to reallocate p. If the reallocation fails, then free p 712 ** and set the mallocFailed flag in the database connection. 713 */ 714 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){ 715 void *pNew; 716 pNew = sqlite3DbRealloc(db, p, n); 717 if( !pNew ){ 718 sqlite3DbFree(db, p); 719 } 720 return pNew; 721 } 722 723 /* 724 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 725 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 726 ** is because when memory debugging is turned on, these two functions are 727 ** called via macros that record the current file and line number in the 728 ** ThreadData structure. 729 */ 730 char *sqlite3DbStrDup(sqlite3 *db, const char *z){ 731 char *zNew; 732 size_t n; 733 if( z==0 ){ 734 return 0; 735 } 736 n = strlen(z) + 1; 737 zNew = sqlite3DbMallocRaw(db, n); 738 if( zNew ){ 739 memcpy(zNew, z, n); 740 } 741 return zNew; 742 } 743 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ 744 char *zNew; 745 assert( db!=0 ); 746 if( z==0 ){ 747 return 0; 748 } 749 assert( (n&0x7fffffff)==n ); 750 zNew = sqlite3DbMallocRawNN(db, n+1); 751 if( zNew ){ 752 memcpy(zNew, z, (size_t)n); 753 zNew[n] = 0; 754 } 755 return zNew; 756 } 757 758 /* 759 ** Free any prior content in *pz and replace it with a copy of zNew. 760 */ 761 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){ 762 sqlite3DbFree(db, *pz); 763 *pz = sqlite3DbStrDup(db, zNew); 764 } 765 766 /* 767 ** Call this routine to record the fact that an OOM (out-of-memory) error 768 ** has happened. This routine will set db->mallocFailed, and also 769 ** temporarily disable the lookaside memory allocator and interrupt 770 ** any running VDBEs. 771 */ 772 void sqlite3OomFault(sqlite3 *db){ 773 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ 774 db->mallocFailed = 1; 775 if( db->nVdbeExec>0 ){ 776 db->u1.isInterrupted = 1; 777 } 778 db->lookaside.bDisable++; 779 } 780 } 781 782 /* 783 ** This routine reactivates the memory allocator and clears the 784 ** db->mallocFailed flag as necessary. 785 ** 786 ** The memory allocator is not restarted if there are running 787 ** VDBEs. 788 */ 789 void sqlite3OomClear(sqlite3 *db){ 790 if( db->mallocFailed && db->nVdbeExec==0 ){ 791 db->mallocFailed = 0; 792 db->u1.isInterrupted = 0; 793 assert( db->lookaside.bDisable>0 ); 794 db->lookaside.bDisable--; 795 } 796 } 797 798 /* 799 ** Take actions at the end of an API call to indicate an OOM error 800 */ 801 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ 802 sqlite3OomClear(db); 803 sqlite3Error(db, SQLITE_NOMEM); 804 return SQLITE_NOMEM_BKPT; 805 } 806 807 /* 808 ** This function must be called before exiting any API function (i.e. 809 ** returning control to the user) that has called sqlite3_malloc or 810 ** sqlite3_realloc. 811 ** 812 ** The returned value is normally a copy of the second argument to this 813 ** function. However, if a malloc() failure has occurred since the previous 814 ** invocation SQLITE_NOMEM is returned instead. 815 ** 816 ** If an OOM as occurred, then the connection error-code (the value 817 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. 818 */ 819 int sqlite3ApiExit(sqlite3* db, int rc){ 820 /* If the db handle must hold the connection handle mutex here. 821 ** Otherwise the read (and possible write) of db->mallocFailed 822 ** is unsafe, as is the call to sqlite3Error(). 823 */ 824 assert( db!=0 ); 825 assert( sqlite3_mutex_held(db->mutex) ); 826 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ 827 return apiOomError(db); 828 } 829 return rc & db->errMask; 830 } 831