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