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 ** Memory allocation functions used throughout sqlite. 13 ** 14 ** 15 ** $Id: malloc.c,v 1.3 2007/06/15 20:29:20 drh Exp $ 16 */ 17 #include "sqliteInt.h" 18 #include "os.h" 19 #include <stdarg.h> 20 #include <ctype.h> 21 22 /* 23 ** MALLOC WRAPPER ARCHITECTURE 24 ** 25 ** The sqlite code accesses dynamic memory allocation/deallocation by invoking 26 ** the following six APIs (which may be implemented as macros). 27 ** 28 ** sqlite3Malloc() 29 ** sqlite3MallocRaw() 30 ** sqlite3Realloc() 31 ** sqlite3ReallocOrFree() 32 ** sqlite3Free() 33 ** sqlite3AllocSize() 34 ** 35 ** The function sqlite3FreeX performs the same task as sqlite3Free and is 36 ** guaranteed to be a real function. The same holds for sqlite3MallocX 37 ** 38 ** The above APIs are implemented in terms of the functions provided in the 39 ** operating-system interface. The OS interface is never accessed directly 40 ** by code outside of this file. 41 ** 42 ** sqlite3OsMalloc() 43 ** sqlite3OsRealloc() 44 ** sqlite3OsFree() 45 ** sqlite3OsAllocationSize() 46 ** 47 ** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke 48 ** sqlite3_release_memory() if a call to sqlite3OsMalloc() or 49 ** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is 50 ** exceeded). Function sqlite3Malloc() usually invokes 51 ** sqlite3MallocRaw(). 52 ** 53 ** MALLOC TEST WRAPPER ARCHITECTURE 54 ** 55 ** The test wrapper provides extra test facilities to ensure the library 56 ** does not leak memory and handles the failure of the underlying OS level 57 ** allocation system correctly. It is only present if the library is 58 ** compiled with the SQLITE_MEMDEBUG macro set. 59 ** 60 ** * Guardposts to detect overwrites. 61 ** * Ability to cause a specific Malloc() or Realloc() to fail. 62 ** * Audit outstanding memory allocations (i.e check for leaks). 63 */ 64 65 #define MAX(x,y) ((x)>(y)?(x):(y)) 66 67 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) 68 /* 69 ** Set the soft heap-size limit for the current thread. Passing a negative 70 ** value indicates no limit. 71 */ 72 void sqlite3_soft_heap_limit(int n){ 73 ThreadData *pTd = sqlite3ThreadData(); 74 if( pTd ){ 75 pTd->nSoftHeapLimit = n; 76 } 77 sqlite3ReleaseThreadData(); 78 } 79 80 /* 81 ** Release memory held by SQLite instances created by the current thread. 82 */ 83 int sqlite3_release_memory(int n){ 84 return sqlite3PagerReleaseMemory(n); 85 } 86 #else 87 /* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version 88 ** of sqlite3_release_memory() to be used by other code in this file. 89 ** This is done for no better reason than to reduce the number of 90 ** pre-processor #ifndef statements. 91 */ 92 #define sqlite3_release_memory(x) 0 /* 0 == no memory freed */ 93 #endif 94 95 #ifdef SQLITE_MEMDEBUG 96 /*-------------------------------------------------------------------------- 97 ** Begin code for memory allocation system test layer. 98 ** 99 ** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro. 100 ** 101 ** SQLITE_MEMDEBUG==1 -> Fence-posting only (thread safe) 102 ** SQLITE_MEMDEBUG==2 -> Fence-posting + linked list of allocations (not ts) 103 ** SQLITE_MEMDEBUG==3 -> Above + backtraces (not thread safe, req. glibc) 104 */ 105 106 /* Figure out whether or not to store backtrace() information for each malloc. 107 ** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or 108 ** greater and glibc is in use. If we don't want to use backtrace(), then just 109 ** define it as an empty macro and set the amount of space reserved to 0. 110 */ 111 #if defined(__GLIBC__) && SQLITE_MEMDEBUG>2 112 extern int backtrace(void **, int); 113 #define TESTALLOC_STACKSIZE 128 114 #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*)) 115 #else 116 #define backtrace(x, y) 117 #define TESTALLOC_STACKSIZE 0 118 #define TESTALLOC_STACKFRAMES 0 119 #endif 120 121 /* 122 ** Number of 32-bit guard words. This should probably be a multiple of 123 ** 2 since on 64-bit machines we want the value returned by sqliteMalloc() 124 ** to be 8-byte aligned. 125 */ 126 #ifndef TESTALLOC_NGUARD 127 # define TESTALLOC_NGUARD 2 128 #endif 129 130 /* 131 ** Size reserved for storing file-name along with each malloc()ed blob. 132 */ 133 #define TESTALLOC_FILESIZE 64 134 135 /* 136 ** Size reserved for storing the user string. Each time a Malloc() or Realloc() 137 ** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by 138 ** sqlite3_malloc_id are stored along with the other test system metadata. 139 */ 140 #define TESTALLOC_USERSIZE 64 141 const char *sqlite3_malloc_id = 0; 142 143 /* 144 ** Blocks used by the test layer have the following format: 145 ** 146 ** <sizeof(void *) pNext pointer> 147 ** <sizeof(void *) pPrev pointer> 148 ** <TESTALLOC_NGUARD 32-bit guard words> 149 ** <The application level allocation> 150 ** <TESTALLOC_NGUARD 32-bit guard words> 151 ** <32-bit line number> 152 ** <TESTALLOC_FILESIZE bytes containing null-terminated file name> 153 ** <TESTALLOC_STACKSIZE bytes of backtrace() output> 154 */ 155 156 #define TESTALLOC_OFFSET_GUARD1(p) (sizeof(void *) * 2) 157 #define TESTALLOC_OFFSET_DATA(p) ( \ 158 TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \ 159 ) 160 #define TESTALLOC_OFFSET_GUARD2(p) ( \ 161 TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \ 162 ) 163 #define TESTALLOC_OFFSET_LINENUMBER(p) ( \ 164 TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \ 165 ) 166 #define TESTALLOC_OFFSET_FILENAME(p) ( \ 167 TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \ 168 ) 169 #define TESTALLOC_OFFSET_USER(p) ( \ 170 TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \ 171 ) 172 #define TESTALLOC_OFFSET_STACK(p) ( \ 173 TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \ 174 (TESTALLOC_OFFSET_USER(p) % 8) \ 175 ) 176 177 #define TESTALLOC_OVERHEAD ( \ 178 sizeof(void *)*2 + /* pPrev and pNext pointers */ \ 179 TESTALLOC_NGUARD*sizeof(u32)*2 + /* Guard words */ \ 180 sizeof(u32) + TESTALLOC_FILESIZE + /* File and line number */ \ 181 TESTALLOC_USERSIZE + /* User string */ \ 182 TESTALLOC_STACKSIZE /* backtrace() stack */ \ 183 ) 184 185 186 /* 187 ** For keeping track of the number of mallocs and frees. This 188 ** is used to check for memory leaks. The iMallocFail and iMallocReset 189 ** values are used to simulate malloc() failures during testing in 190 ** order to verify that the library correctly handles an out-of-memory 191 ** condition. 192 */ 193 int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ 194 int sqlite3_nFree; /* Number of sqliteFree() calls */ 195 int sqlite3_memUsed; /* TODO Total memory obtained from malloc */ 196 int sqlite3_memMax; /* TODO Mem usage high-water mark */ 197 int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ 198 int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */ 199 200 void *sqlite3_pFirst = 0; /* Pointer to linked list of allocations */ 201 int sqlite3_nMaxAlloc = 0; /* High water mark of ThreadData.nAlloc */ 202 int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */ 203 int sqlite3_isFail = 0; /* True if all malloc calls should fail */ 204 const char *sqlite3_zFile = 0; /* Filename to associate debug info with */ 205 int sqlite3_iLine = 0; /* Line number for debug info */ 206 int sqlite3_mallocfail_trace = 0; /* Print a msg on malloc fail if true */ 207 208 /* 209 ** Check for a simulated memory allocation failure. Return true if 210 ** the failure should be simulated. Return false to proceed as normal. 211 */ 212 int sqlite3TestMallocFail(){ 213 if( sqlite3_isFail ){ 214 return 1; 215 } 216 if( sqlite3_iMallocFail>=0 ){ 217 sqlite3_iMallocFail--; 218 if( sqlite3_iMallocFail==0 ){ 219 sqlite3_iMallocFail = sqlite3_iMallocReset; 220 sqlite3_isFail = 1; 221 if( sqlite3_mallocfail_trace ){ 222 sqlite3DebugPrintf("###_malloc_fails_###\n"); 223 } 224 return 1; 225 } 226 } 227 return 0; 228 } 229 230 /* 231 ** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc(). 232 ** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the 233 ** values set by the applyGuards() function. 234 */ 235 static void checkGuards(u32 *p) 236 { 237 int i; 238 char *zAlloc = (char *)p; 239 char *z; 240 241 /* First set of guard words */ 242 z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)]; 243 for(i=0; i<TESTALLOC_NGUARD; i++){ 244 assert(((u32 *)z)[i]==0xdead1122); 245 } 246 247 /* Second set of guard words */ 248 z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)]; 249 for(i=0; i<TESTALLOC_NGUARD; i++){ 250 u32 guard = 0; 251 memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32)); 252 assert(guard==0xdead3344); 253 } 254 } 255 256 /* 257 ** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The 258 ** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as 259 ** guard-posts. 260 */ 261 static void applyGuards(u32 *p) 262 { 263 int i; 264 char *z; 265 char *zAlloc = (char *)p; 266 267 /* First set of guard words */ 268 z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)]; 269 for(i=0; i<TESTALLOC_NGUARD; i++){ 270 ((u32 *)z)[i] = 0xdead1122; 271 } 272 273 /* Second set of guard words */ 274 z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)]; 275 for(i=0; i<TESTALLOC_NGUARD; i++){ 276 static const int guard = 0xdead3344; 277 memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32)); 278 } 279 280 /* Line number */ 281 z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)]; /* Guard words */ 282 z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)]; 283 memcpy(z, &sqlite3_iLine, sizeof(u32)); 284 285 /* File name */ 286 z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)]; 287 strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE); 288 z[TESTALLOC_FILESIZE - 1] = '\0'; 289 290 /* User string */ 291 z = &zAlloc[TESTALLOC_OFFSET_USER(p)]; 292 z[0] = 0; 293 if( sqlite3_malloc_id ){ 294 strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE); 295 z[TESTALLOC_USERSIZE-1] = 0; 296 } 297 298 /* backtrace() stack */ 299 z = &zAlloc[TESTALLOC_OFFSET_STACK(p)]; 300 backtrace((void **)z, TESTALLOC_STACKFRAMES); 301 302 /* Sanity check to make sure checkGuards() is working */ 303 checkGuards(p); 304 } 305 306 /* 307 ** The argument is a malloc()ed pointer as returned by the test-wrapper. 308 ** Return a pointer to the Os level allocation. 309 */ 310 static void *getOsPointer(void *p) 311 { 312 char *z = (char *)p; 313 return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]); 314 } 315 316 317 #if SQLITE_MEMDEBUG>1 318 /* 319 ** The argument points to an Os level allocation. Link it into the threads list 320 ** of allocations. 321 */ 322 static void linkAlloc(void *p){ 323 void **pp = (void **)p; 324 pp[0] = 0; 325 pp[1] = sqlite3_pFirst; 326 if( sqlite3_pFirst ){ 327 ((void **)sqlite3_pFirst)[0] = p; 328 } 329 sqlite3_pFirst = p; 330 } 331 332 /* 333 ** The argument points to an Os level allocation. Unlinke it from the threads 334 ** list of allocations. 335 */ 336 static void unlinkAlloc(void *p) 337 { 338 void **pp = (void **)p; 339 if( p==sqlite3_pFirst ){ 340 assert(!pp[0]); 341 assert(!pp[1] || ((void **)(pp[1]))[0]==p); 342 sqlite3_pFirst = pp[1]; 343 if( sqlite3_pFirst ){ 344 ((void **)sqlite3_pFirst)[0] = 0; 345 } 346 }else{ 347 void **pprev = pp[0]; 348 void **pnext = pp[1]; 349 assert(pprev); 350 assert(pprev[1]==p); 351 pprev[1] = (void *)pnext; 352 if( pnext ){ 353 assert(pnext[0]==p); 354 pnext[0] = (void *)pprev; 355 } 356 } 357 } 358 359 /* 360 ** Pointer p is a pointer to an OS level allocation that has just been 361 ** realloc()ed. Set the list pointers that point to this entry to it's new 362 ** location. 363 */ 364 static void relinkAlloc(void *p) 365 { 366 void **pp = (void **)p; 367 if( pp[0] ){ 368 ((void **)(pp[0]))[1] = p; 369 }else{ 370 sqlite3_pFirst = p; 371 } 372 if( pp[1] ){ 373 ((void **)(pp[1]))[0] = p; 374 } 375 } 376 #else 377 #define linkAlloc(x) 378 #define relinkAlloc(x) 379 #define unlinkAlloc(x) 380 #endif 381 382 /* 383 ** This function sets the result of the Tcl interpreter passed as an argument 384 ** to a list containing an entry for each currently outstanding call made to 385 ** sqliteMalloc and friends by the current thread. Each list entry is itself a 386 ** list, consisting of the following (in order): 387 ** 388 ** * The number of bytes allocated 389 ** * The __FILE__ macro at the time of the sqliteMalloc() call. 390 ** * The __LINE__ macro ... 391 ** * The value of the sqlite3_malloc_id variable ... 392 ** * The output of backtrace() (if available) ... 393 ** 394 ** Todo: We could have a version of this function that outputs to stdout, 395 ** to debug memory leaks when Tcl is not available. 396 */ 397 #if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1 398 #include <tcl.h> 399 int sqlite3OutstandingMallocs(Tcl_Interp *interp){ 400 void *p; 401 Tcl_Obj *pRes = Tcl_NewObj(); 402 Tcl_IncrRefCount(pRes); 403 404 405 for(p=sqlite3_pFirst; p; p=((void **)p)[1]){ 406 Tcl_Obj *pEntry = Tcl_NewObj(); 407 Tcl_Obj *pStack = Tcl_NewObj(); 408 char *z; 409 u32 iLine; 410 int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD; 411 char *zAlloc = (char *)p; 412 int i; 413 414 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes)); 415 416 z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)]; 417 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1)); 418 419 z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)]; 420 memcpy(&iLine, z, sizeof(u32)); 421 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine)); 422 423 z = &zAlloc[TESTALLOC_OFFSET_USER(p)]; 424 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1)); 425 426 z = &zAlloc[TESTALLOC_OFFSET_STACK(p)]; 427 for(i=0; i<TESTALLOC_STACKFRAMES; i++){ 428 char zHex[128]; 429 sqlite3_snprintf(sizeof(zHex), zHex, "%p", ((void **)z)[i]); 430 Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1)); 431 } 432 433 Tcl_ListObjAppendElement(0, pEntry, pStack); 434 Tcl_ListObjAppendElement(0, pRes, pEntry); 435 } 436 437 Tcl_ResetResult(interp); 438 Tcl_SetObjResult(interp, pRes); 439 Tcl_DecrRefCount(pRes); 440 return TCL_OK; 441 } 442 #endif 443 444 /* 445 ** This is the test layer's wrapper around sqlite3OsMalloc(). 446 */ 447 static void * OSMALLOC(int n){ 448 sqlite3OsEnterMutex(); 449 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 450 sqlite3_nMaxAlloc = 451 MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc); 452 #endif 453 assert( !sqlite3_mallocDisallowed ); 454 if( !sqlite3TestMallocFail() ){ 455 u32 *p; 456 p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD); 457 assert(p); 458 sqlite3_nMalloc++; 459 applyGuards(p); 460 linkAlloc(p); 461 sqlite3OsLeaveMutex(); 462 return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); 463 } 464 sqlite3OsLeaveMutex(); 465 return 0; 466 } 467 468 static int OSSIZEOF(void *p){ 469 if( p ){ 470 u32 *pOs = (u32 *)getOsPointer(p); 471 return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD; 472 } 473 return 0; 474 } 475 476 /* 477 ** This is the test layer's wrapper around sqlite3OsFree(). The argument is a 478 ** pointer to the space allocated for the application to use. 479 */ 480 static void OSFREE(void *pFree){ 481 u32 *p; /* Pointer to the OS-layer allocation */ 482 sqlite3OsEnterMutex(); 483 p = (u32 *)getOsPointer(pFree); 484 checkGuards(p); 485 unlinkAlloc(p); 486 memset(pFree, 0x55, OSSIZEOF(pFree)); 487 sqlite3OsFree(p); 488 sqlite3_nFree++; 489 sqlite3OsLeaveMutex(); 490 } 491 492 /* 493 ** This is the test layer's wrapper around sqlite3OsRealloc(). 494 */ 495 static void * OSREALLOC(void *pRealloc, int n){ 496 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 497 sqlite3_nMaxAlloc = 498 MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc); 499 #endif 500 assert( !sqlite3_mallocDisallowed ); 501 if( !sqlite3TestMallocFail() ){ 502 u32 *p = (u32 *)getOsPointer(pRealloc); 503 checkGuards(p); 504 p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD); 505 applyGuards(p); 506 relinkAlloc(p); 507 return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); 508 } 509 return 0; 510 } 511 512 static void OSMALLOC_FAILED(){ 513 sqlite3_isFail = 0; 514 } 515 516 #else 517 /* Define macros to call the sqlite3OsXXX interface directly if 518 ** the SQLITE_MEMDEBUG macro is not defined. 519 */ 520 #define OSMALLOC(x) sqlite3OsMalloc(x) 521 #define OSREALLOC(x,y) sqlite3OsRealloc(x,y) 522 #define OSFREE(x) sqlite3OsFree(x) 523 #define OSSIZEOF(x) sqlite3OsAllocationSize(x) 524 #define OSMALLOC_FAILED() 525 526 #endif /* SQLITE_MEMDEBUG */ 527 /* 528 ** End code for memory allocation system test layer. 529 **--------------------------------------------------------------------------*/ 530 531 /* 532 ** This routine is called when we are about to allocate n additional bytes 533 ** of memory. If the new allocation will put is over the soft allocation 534 ** limit, then invoke sqlite3_release_memory() to try to release some 535 ** memory before continuing with the allocation. 536 ** 537 ** This routine also makes sure that the thread-specific-data (TSD) has 538 ** be allocated. If it has not and can not be allocated, then return 539 ** false. The updateMemoryUsedCount() routine below will deallocate 540 ** the TSD if it ought to be. 541 ** 542 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is 543 ** a no-op 544 */ 545 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 546 static int enforceSoftLimit(int n){ 547 ThreadData *pTsd = sqlite3ThreadData(); 548 if( pTsd==0 ){ 549 return 0; 550 } 551 assert( pTsd->nAlloc>=0 ); 552 if( n>0 && pTsd->nSoftHeapLimit>0 ){ 553 while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){} 554 } 555 return 1; 556 } 557 #else 558 # define enforceSoftLimit(X) 1 559 #endif 560 561 /* 562 ** Update the count of total outstanding memory that is held in 563 ** thread-specific-data (TSD). If after this update the TSD is 564 ** no longer being used, then deallocate it. 565 ** 566 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is 567 ** a no-op 568 */ 569 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 570 static void updateMemoryUsedCount(int n){ 571 ThreadData *pTsd = sqlite3ThreadData(); 572 if( pTsd ){ 573 pTsd->nAlloc += n; 574 assert( pTsd->nAlloc>=0 ); 575 if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){ 576 sqlite3ReleaseThreadData(); 577 } 578 } 579 } 580 #else 581 #define updateMemoryUsedCount(x) /* no-op */ 582 #endif 583 584 /* 585 ** Allocate and return N bytes of uninitialised memory by calling 586 ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory 587 ** by calling sqlite3_release_memory(). 588 */ 589 void *sqlite3MallocRaw(int n, int doMemManage){ 590 void *p = 0; 591 if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){ 592 while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){} 593 if( !p ){ 594 sqlite3FailedMalloc(); 595 OSMALLOC_FAILED(); 596 }else if( doMemManage ){ 597 updateMemoryUsedCount(OSSIZEOF(p)); 598 } 599 } 600 return p; 601 } 602 603 /* 604 ** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The 605 ** pointer to the new allocation is returned. If the Realloc() call fails, 606 ** attempt to free memory by calling sqlite3_release_memory(). 607 */ 608 void *sqlite3Realloc(void *p, int n){ 609 if( sqlite3MallocFailed() ){ 610 return 0; 611 } 612 613 if( !p ){ 614 return sqlite3Malloc(n, 1); 615 }else{ 616 void *np = 0; 617 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 618 int origSize = OSSIZEOF(p); 619 #endif 620 if( enforceSoftLimit(n - origSize) ){ 621 while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){} 622 if( !np ){ 623 sqlite3FailedMalloc(); 624 OSMALLOC_FAILED(); 625 }else{ 626 updateMemoryUsedCount(OSSIZEOF(np) - origSize); 627 } 628 } 629 return np; 630 } 631 } 632 633 /* 634 ** Free the memory pointed to by p. p must be either a NULL pointer or a 635 ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc(). 636 */ 637 void sqlite3FreeX(void *p){ 638 if( p ){ 639 updateMemoryUsedCount(0 - OSSIZEOF(p)); 640 OSFREE(p); 641 } 642 } 643 644 /* 645 ** A version of sqliteMalloc() that is always a function, not a macro. 646 ** Currently, this is used only to alloc to allocate the parser engine. 647 */ 648 void *sqlite3MallocX(int n){ 649 return sqliteMalloc(n); 650 } 651 652 /* 653 ** sqlite3Malloc 654 ** sqlite3ReallocOrFree 655 ** 656 ** These two are implemented as wrappers around sqlite3MallocRaw(), 657 ** sqlite3Realloc() and sqlite3Free(). 658 */ 659 void *sqlite3Malloc(int n, int doMemManage){ 660 void *p = sqlite3MallocRaw(n, doMemManage); 661 if( p ){ 662 memset(p, 0, n); 663 } 664 return p; 665 } 666 void *sqlite3ReallocOrFree(void *p, int n){ 667 void *pNew; 668 pNew = sqlite3Realloc(p, n); 669 if( !pNew ){ 670 sqlite3FreeX(p); 671 } 672 return pNew; 673 } 674 675 /* 676 ** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those 677 ** rare scenarios where sqlite may allocate memory in one thread and free 678 ** it in another. They are exactly the same as sqlite3Malloc() and 679 ** sqlite3Free() except that: 680 ** 681 ** * The allocated memory is not included in any calculations with 682 ** respect to the soft-heap-limit, and 683 ** 684 ** * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(), 685 ** not sqlite3Free(). Calling sqlite3Free() on memory obtained from 686 ** ThreadSafeMalloc() will cause an error somewhere down the line. 687 */ 688 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 689 void *sqlite3ThreadSafeMalloc(int n){ 690 (void)ENTER_MALLOC; 691 return sqlite3Malloc(n, 0); 692 } 693 void sqlite3ThreadSafeFree(void *p){ 694 (void)ENTER_MALLOC; 695 if( p ){ 696 OSFREE(p); 697 } 698 } 699 #endif 700 701 702 /* 703 ** Return the number of bytes allocated at location p. p must be either 704 ** a NULL pointer (in which case 0 is returned) or a pointer returned by 705 ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree(). 706 ** 707 ** The number of bytes allocated does not include any overhead inserted by 708 ** any malloc() wrapper functions that may be called. So the value returned 709 ** is the number of bytes that were available to SQLite using pointer p, 710 ** regardless of how much memory was actually allocated. 711 */ 712 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT 713 int sqlite3AllocSize(void *p){ 714 return OSSIZEOF(p); 715 } 716 #endif 717 718 /* 719 ** Make a copy of a string in memory obtained from sqliteMalloc(). These 720 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This 721 ** is because when memory debugging is turned on, these two functions are 722 ** called via macros that record the current file and line number in the 723 ** ThreadData structure. 724 */ 725 char *sqlite3StrDup(const char *z){ 726 char *zNew; 727 int n; 728 if( z==0 ) return 0; 729 n = strlen(z)+1; 730 zNew = sqlite3MallocRaw(n, 1); 731 if( zNew ) memcpy(zNew, z, n); 732 return zNew; 733 } 734 char *sqlite3StrNDup(const char *z, int n){ 735 char *zNew; 736 if( z==0 ) return 0; 737 zNew = sqlite3MallocRaw(n+1, 1); 738 if( zNew ){ 739 memcpy(zNew, z, n); 740 zNew[n] = 0; 741 } 742 return zNew; 743 } 744 745 /* 746 ** Create a string from the 2nd and subsequent arguments (up to the 747 ** first NULL argument), store the string in memory obtained from 748 ** sqliteMalloc() and make the pointer indicated by the 1st argument 749 ** point to that string. The 1st argument must either be NULL or 750 ** point to memory obtained from sqliteMalloc(). 751 */ 752 void sqlite3SetString(char **pz, ...){ 753 va_list ap; 754 int nByte; 755 const char *z; 756 char *zResult; 757 758 assert( pz!=0 ); 759 nByte = 1; 760 va_start(ap, pz); 761 while( (z = va_arg(ap, const char*))!=0 ){ 762 nByte += strlen(z); 763 } 764 va_end(ap); 765 sqliteFree(*pz); 766 *pz = zResult = sqliteMallocRaw( nByte ); 767 if( zResult==0 ){ 768 return; 769 } 770 *zResult = 0; 771 va_start(ap, pz); 772 while( (z = va_arg(ap, const char*))!=0 ){ 773 int n = strlen(z); 774 memcpy(zResult, z, n); 775 zResult += n; 776 } 777 zResult[0] = 0; 778 va_end(ap); 779 } 780 781 782 /* 783 ** This function must be called before exiting any API function (i.e. 784 ** returning control to the user) that has called sqlite3Malloc or 785 ** sqlite3Realloc. 786 ** 787 ** The returned value is normally a copy of the second argument to this 788 ** function. However, if a malloc() failure has occured since the previous 789 ** invocation SQLITE_NOMEM is returned instead. 790 ** 791 ** If the first argument, db, is not NULL and a malloc() error has occured, 792 ** then the connection error-code (the value returned by sqlite3_errcode()) 793 ** is set to SQLITE_NOMEM. 794 */ 795 int sqlite3_mallocHasFailed = 0; 796 int sqlite3ApiExit(sqlite3* db, int rc){ 797 if( sqlite3MallocFailed() ){ 798 sqlite3_mallocHasFailed = 0; 799 sqlite3OsLeaveMutex(); 800 sqlite3Error(db, SQLITE_NOMEM, 0); 801 rc = SQLITE_NOMEM; 802 } 803 return rc & (db ? db->errMask : 0xff); 804 } 805 806 /* 807 ** Set the "malloc has failed" condition to true for this thread. 808 */ 809 void sqlite3FailedMalloc(){ 810 if( !sqlite3MallocFailed() ){ 811 sqlite3OsEnterMutex(); 812 assert( sqlite3_mallocHasFailed==0 ); 813 sqlite3_mallocHasFailed = 1; 814 } 815 } 816 817 #ifdef SQLITE_MEMDEBUG 818 /* 819 ** This function sets a flag in the thread-specific-data structure that will 820 ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called. 821 */ 822 void sqlite3MallocDisallow(){ 823 assert( sqlite3_mallocDisallowed>=0 ); 824 sqlite3_mallocDisallowed++; 825 } 826 827 /* 828 ** This function clears the flag set in the thread-specific-data structure set 829 ** by sqlite3MallocDisallow(). 830 */ 831 void sqlite3MallocAllow(){ 832 assert( sqlite3_mallocDisallowed>0 ); 833 sqlite3_mallocDisallowed--; 834 } 835 #endif 836