4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** Memory allocation functions used throughout sqlite.
17 #include "sqliteInt.h"
23 ** MALLOC WRAPPER ARCHITECTURE
25 ** The sqlite code accesses dynamic memory allocation/deallocation by invoking
26 ** the following six APIs (which may be implemented as macros).
31 ** sqlite3ReallocOrFree()
35 ** The function sqlite3FreeX performs the same task as sqlite3Free and is
36 ** guaranteed to be a real function. The same holds for sqlite3MallocX
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.
45 ** sqlite3OsAllocationSize()
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().
53 ** MALLOC TEST WRAPPER ARCHITECTURE
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.
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).
65 #define MAX(x,y) ((x)>(y)?(x):(y))
67 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
69 ** Set the soft heap-size limit for the current thread. Passing a negative
70 ** value indicates no limit.
72 void sqlite3_soft_heap_limit(int n){
73 ThreadData *pTd = sqlite3ThreadData();
75 pTd->nSoftHeapLimit = n;
77 sqlite3ReleaseThreadData();
81 ** Release memory held by SQLite instances created by the current thread.
83 int sqlite3_release_memory(int n){
84 return sqlite3PagerReleaseMemory(n);
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.
92 #define sqlite3_release_memory(x) 0 /* 0 == no memory freed */
95 #ifdef SQLITE_MEMDEBUG
96 /*--------------------------------------------------------------------------
97 ** Begin code for memory allocation system test layer.
99 ** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro.
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)
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.
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*))
116 #define backtrace(x, y)
117 #define TESTALLOC_STACKSIZE 0
118 #define TESTALLOC_STACKFRAMES 0
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.
126 #ifndef TESTALLOC_NGUARD
127 # define TESTALLOC_NGUARD 2
131 ** Size reserved for storing file-name along with each malloc()ed blob.
133 #define TESTALLOC_FILESIZE 64
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.
140 #define TESTALLOC_USERSIZE 64
141 const char *sqlite3_malloc_id = 0;
144 ** Blocks used by the test layer have the following format:
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>
156 #define TESTALLOC_OFFSET_GUARD1(p) (sizeof(void *) * 2)
157 #define TESTALLOC_OFFSET_DATA(p) ( \
158 TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \
160 #define TESTALLOC_OFFSET_GUARD2(p) ( \
161 TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \
163 #define TESTALLOC_OFFSET_LINENUMBER(p) ( \
164 TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \
166 #define TESTALLOC_OFFSET_FILENAME(p) ( \
167 TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \
169 #define TESTALLOC_OFFSET_USER(p) ( \
170 TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \
172 #define TESTALLOC_OFFSET_STACK(p) ( \
173 TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \
174 (TESTALLOC_OFFSET_USER(p) % 8) \
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 */ \
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
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 */
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 */
209 ** Check for a simulated memory allocation failure. Return true if
210 ** the failure should be simulated. Return false to proceed as normal.
212 int sqlite3TestMallocFail(){
213 if( sqlite3_isFail ){
216 if( sqlite3_iMallocFail>=0 ){
217 sqlite3_iMallocFail--;
218 if( sqlite3_iMallocFail==0 ){
219 sqlite3_iMallocFail = sqlite3_iMallocReset;
221 if( sqlite3_mallocfail_trace ){
222 sqlite3DebugPrintf("###_malloc_fails_###\n");
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.
235 static void checkGuards(u32 *p)
238 char *zAlloc = (char *)p;
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);
247 /* Second set of guard words */
248 z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
249 for(i=0; i<TESTALLOC_NGUARD; i++){
251 memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32));
252 assert(guard==0xdead3344);
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
261 static void applyGuards(u32 *p)
265 char *zAlloc = (char *)p;
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;
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));
281 z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)]; /* Guard words */
282 z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
283 memcpy(z, &sqlite3_iLine, sizeof(u32));
286 z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
287 strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE);
288 z[TESTALLOC_FILESIZE - 1] = '\0';
291 z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
293 if( sqlite3_malloc_id ){
294 strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE);
295 z[TESTALLOC_USERSIZE-1] = 0;
298 /* backtrace() stack */
299 z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
300 backtrace((void **)z, TESTALLOC_STACKFRAMES);
302 /* Sanity check to make sure checkGuards() is working */
307 ** The argument is a malloc()ed pointer as returned by the test-wrapper.
308 ** Return a pointer to the Os level allocation.
310 static void *getOsPointer(void *p)
313 return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]);
317 #if SQLITE_MEMDEBUG>1
319 ** The argument points to an Os level allocation. Link it into the threads list
322 static void linkAlloc(void *p){
323 void **pp = (void **)p;
325 pp[1] = sqlite3_pFirst;
326 if( sqlite3_pFirst ){
327 ((void **)sqlite3_pFirst)[0] = p;
333 ** The argument points to an Os level allocation. Unlinke it from the threads
334 ** list of allocations.
336 static void unlinkAlloc(void *p)
338 void **pp = (void **)p;
339 if( p==sqlite3_pFirst ){
341 assert(!pp[1] || ((void **)(pp[1]))[0]==p);
342 sqlite3_pFirst = pp[1];
343 if( sqlite3_pFirst ){
344 ((void **)sqlite3_pFirst)[0] = 0;
347 void **pprev = pp[0];
348 void **pnext = pp[1];
351 pprev[1] = (void *)pnext;
354 pnext[0] = (void *)pprev;
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
364 static void relinkAlloc(void *p)
366 void **pp = (void **)p;
368 ((void **)(pp[0]))[1] = p;
373 ((void **)(pp[1]))[0] = p;
378 #define relinkAlloc(x)
379 #define unlinkAlloc(x)
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):
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) ...
394 ** Todo: We could have a version of this function that outputs to stdout,
395 ** to debug memory leaks when Tcl is not available.
397 #if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1
399 int sqlite3OutstandingMallocs(Tcl_Interp *interp){
401 Tcl_Obj *pRes = Tcl_NewObj();
402 Tcl_IncrRefCount(pRes);
405 for(p=sqlite3_pFirst; p; p=((void **)p)[1]){
406 Tcl_Obj *pEntry = Tcl_NewObj();
407 Tcl_Obj *pStack = Tcl_NewObj();
410 int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD;
411 char *zAlloc = (char *)p;
414 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes));
416 z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
417 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
419 z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
420 memcpy(&iLine, z, sizeof(u32));
421 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine));
423 z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
424 Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
426 z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
427 for(i=0; i<TESTALLOC_STACKFRAMES; i++){
429 sqlite3_snprintf(sizeof(zHex), zHex, "%p", ((void **)z)[i]);
430 Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1));
433 Tcl_ListObjAppendElement(0, pEntry, pStack);
434 Tcl_ListObjAppendElement(0, pRes, pEntry);
437 Tcl_ResetResult(interp);
438 Tcl_SetObjResult(interp, pRes);
439 Tcl_DecrRefCount(pRes);
445 ** This is the test layer's wrapper around sqlite3OsMalloc().
447 static void * OSMALLOC(int n){
448 sqlite3OsEnterMutex();
449 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
451 MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
453 assert( !sqlite3_mallocDisallowed );
454 if( !sqlite3TestMallocFail() ){
456 p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
461 sqlite3OsLeaveMutex();
462 return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
464 sqlite3OsLeaveMutex();
468 static int OSSIZEOF(void *p){
470 u32 *pOs = (u32 *)getOsPointer(p);
471 return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD;
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.
480 static void OSFREE(void *pFree){
481 u32 *p; /* Pointer to the OS-layer allocation */
482 sqlite3OsEnterMutex();
483 p = (u32 *)getOsPointer(pFree);
486 memset(pFree, 0x55, OSSIZEOF(pFree));
489 sqlite3OsLeaveMutex();
493 ** This is the test layer's wrapper around sqlite3OsRealloc().
495 static void * OSREALLOC(void *pRealloc, int n){
496 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
498 MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
500 assert( !sqlite3_mallocDisallowed );
501 if( !sqlite3TestMallocFail() ){
502 u32 *p = (u32 *)getOsPointer(pRealloc);
504 p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
507 return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
512 static void OSMALLOC_FAILED(){
517 /* Define macros to call the sqlite3OsXXX interface directly if
518 ** the SQLITE_MEMDEBUG macro is not defined.
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()
526 #endif /* SQLITE_MEMDEBUG */
528 ** End code for memory allocation system test layer.
529 **--------------------------------------------------------------------------*/
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.
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.
542 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
545 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
546 static int enforceSoftLimit(int n){
547 ThreadData *pTsd = sqlite3ThreadData();
551 assert( pTsd->nAlloc>=0 );
552 if( n>0 && pTsd->nSoftHeapLimit>0 ){
553 while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){}
558 # define enforceSoftLimit(X) 1
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.
566 ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
569 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
570 static void updateMemoryUsedCount(int n){
571 ThreadData *pTsd = sqlite3ThreadData();
574 assert( pTsd->nAlloc>=0 );
575 if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
576 sqlite3ReleaseThreadData();
581 #define updateMemoryUsedCount(x) /* no-op */
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().
589 void *sqlite3MallocRaw(int n, int doMemManage){
591 if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){
592 while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}
594 sqlite3FailedMalloc();
596 }else if( doMemManage ){
597 updateMemoryUsedCount(OSSIZEOF(p));
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().
608 void *sqlite3Realloc(void *p, int n){
609 if( sqlite3MallocFailed() ){
614 return sqlite3Malloc(n, 1);
617 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
618 int origSize = OSSIZEOF(p);
620 if( enforceSoftLimit(n - origSize) ){
621 while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}
623 sqlite3FailedMalloc();
626 updateMemoryUsedCount(OSSIZEOF(np) - origSize);
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().
637 void sqlite3FreeX(void *p){
639 updateMemoryUsedCount(0 - OSSIZEOF(p));
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.
648 void *sqlite3MallocX(int n){
649 return sqliteMalloc(n);
654 ** sqlite3ReallocOrFree
656 ** These two are implemented as wrappers around sqlite3MallocRaw(),
657 ** sqlite3Realloc() and sqlite3Free().
659 void *sqlite3Malloc(int n, int doMemManage){
660 void *p = sqlite3MallocRaw(n, doMemManage);
666 void *sqlite3ReallocOrFree(void *p, int n){
668 pNew = sqlite3Realloc(p, n);
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:
681 ** * The allocated memory is not included in any calculations with
682 ** respect to the soft-heap-limit, and
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.
688 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
689 void *sqlite3ThreadSafeMalloc(int n){
691 return sqlite3Malloc(n, 0);
693 void sqlite3ThreadSafeFree(void *p){
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().
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.
712 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
713 int sqlite3AllocSize(void *p){
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.
725 char *sqlite3StrDup(const char *z){
730 zNew = sqlite3MallocRaw(n, 1);
731 if( zNew ) memcpy(zNew, z, n);
734 char *sqlite3StrNDup(const char *z, int n){
737 zNew = sqlite3MallocRaw(n+1, 1);
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().
752 void sqlite3SetString(char **pz, ...){
761 while( (z = va_arg(ap, const char*))!=0 ){
766 *pz = zResult = sqliteMallocRaw( nByte );
772 while( (z = va_arg(ap, const char*))!=0 ){
774 memcpy(zResult, z, n);
783 ** This function must be called before exiting any API function (i.e.
784 ** returning control to the user) that has called sqlite3Malloc or
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.
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.
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);
803 return rc & (db ? db->errMask : 0xff);
807 ** Set the "malloc has failed" condition to true for this thread.
809 void sqlite3FailedMalloc(){
810 if( !sqlite3MallocFailed() ){
811 sqlite3OsEnterMutex();
812 assert( sqlite3_mallocHasFailed==0 );
813 sqlite3_mallocHasFailed = 1;
817 #ifdef SQLITE_MEMDEBUG
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.
822 void sqlite3MallocDisallow(){
823 assert( sqlite3_mallocDisallowed>=0 );
824 sqlite3_mallocDisallowed++;
828 ** This function clears the flag set in the thread-specific-data structure set
829 ** by sqlite3MallocDisallow().
831 void sqlite3MallocAllow(){
832 assert( sqlite3_mallocDisallowed>0 );
833 sqlite3_mallocDisallowed--;