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 *************************************************************************
13 ** Memory allocation functions used throughout sqlite.
15 #include "sqliteInt.h"
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.
23 int sqlite3_release_memory(int n
){
24 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
25 return sqlite3PcacheReleaseMemory(n
);
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. */
36 ** An instance of the following object records the location of
37 ** each unused scratch buffer.
39 typedef struct ScratchFreeslot
{
40 struct ScratchFreeslot
*pNext
; /* Next unused scratch buffer */
44 ** State information local to the memory allocation subsystem.
46 static SQLITE_WSD
struct Mem0Global
{
47 sqlite3_mutex
*mutex
; /* Mutex to serialize access */
50 ** The alarm callback and its arguments. The mem0.mutex lock will
51 ** be held while the callback is running. Recursive calls into
52 ** the memory subsystem are allowed, but no new callbacks will be
55 sqlite3_int64 alarmThreshold
;
56 void (*alarmCallback
)(void*, sqlite3_int64
,int);
60 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
61 ** (so that a range test can be used to determine if an allocation
62 ** being freed came from pScratch) and a pointer to the list of
63 ** unused scratch allocations.
66 ScratchFreeslot
*pScratchFree
;
70 ** True if heap is nearly "full" where "full" is defined by the
71 ** sqlite3_soft_heap_limit() setting.
74 } mem0
= { 0, 0, 0, 0, 0, 0, 0, 0 };
76 #define mem0 GLOBAL(struct Mem0Global, mem0)
79 ** This routine runs when the memory allocator sees that the
80 ** total memory allocation is about to exceed the soft heap
83 static void softHeapLimitEnforcer(
85 sqlite3_int64 NotUsed2
,
88 UNUSED_PARAMETER2(NotUsed
, NotUsed2
);
89 sqlite3_release_memory(allocSize
);
93 ** Change the alarm callback
95 static int sqlite3MemoryAlarm(
96 void(*xCallback
)(void *pArg
, sqlite3_int64 used
,int N
),
98 sqlite3_int64 iThreshold
101 sqlite3_mutex_enter(mem0
.mutex
);
102 mem0
.alarmCallback
= xCallback
;
103 mem0
.alarmArg
= pArg
;
104 mem0
.alarmThreshold
= iThreshold
;
105 nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
106 mem0
.nearlyFull
= (iThreshold
>0 && iThreshold
<=nUsed
);
107 sqlite3_mutex_leave(mem0
.mutex
);
111 #ifndef SQLITE_OMIT_DEPRECATED
113 ** Deprecated external interface. Internal/core SQLite code
114 ** should call sqlite3MemoryAlarm.
116 int sqlite3_memory_alarm(
117 void(*xCallback
)(void *pArg
, sqlite3_int64 used
,int N
),
119 sqlite3_int64 iThreshold
121 return sqlite3MemoryAlarm(xCallback
, pArg
, iThreshold
);
126 ** Set the soft heap-size limit for the library. Passing a zero or
127 ** negative value indicates no limit.
129 sqlite3_int64
sqlite3_soft_heap_limit64(sqlite3_int64 n
){
130 sqlite3_int64 priorLimit
;
131 sqlite3_int64 excess
;
132 #ifndef SQLITE_OMIT_AUTOINIT
133 sqlite3_initialize();
135 sqlite3_mutex_enter(mem0
.mutex
);
136 priorLimit
= mem0
.alarmThreshold
;
137 sqlite3_mutex_leave(mem0
.mutex
);
138 if( n
<0 ) return priorLimit
;
140 sqlite3MemoryAlarm(softHeapLimitEnforcer
, 0, n
);
142 sqlite3MemoryAlarm(0, 0, 0);
144 excess
= sqlite3_memory_used() - n
;
145 if( excess
>0 ) sqlite3_release_memory((int)(excess
& 0x7fffffff));
148 void sqlite3_soft_heap_limit(int n
){
150 sqlite3_soft_heap_limit64(n
);
154 ** Initialize the memory allocation subsystem.
156 int sqlite3MallocInit(void){
157 if( sqlite3GlobalConfig
.m
.xMalloc
==0 ){
158 sqlite3MemSetDefault();
160 memset(&mem0
, 0, sizeof(mem0
));
161 if( sqlite3GlobalConfig
.bCoreMutex
){
162 mem0
.mutex
= sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM
);
164 if( sqlite3GlobalConfig
.pScratch
&& sqlite3GlobalConfig
.szScratch
>=100
165 && sqlite3GlobalConfig
.nScratch
>0 ){
167 ScratchFreeslot
*pSlot
;
168 sz
= ROUNDDOWN8(sqlite3GlobalConfig
.szScratch
);
169 sqlite3GlobalConfig
.szScratch
= sz
;
170 pSlot
= (ScratchFreeslot
*)sqlite3GlobalConfig
.pScratch
;
171 n
= sqlite3GlobalConfig
.nScratch
;
172 mem0
.pScratchFree
= pSlot
;
173 mem0
.nScratchFree
= n
;
174 for(i
=0; i
<n
-1; i
++){
175 pSlot
->pNext
= (ScratchFreeslot
*)(sz
+(char*)pSlot
);
176 pSlot
= pSlot
->pNext
;
179 mem0
.pScratchEnd
= (void*)&pSlot
[1];
181 mem0
.pScratchEnd
= 0;
182 sqlite3GlobalConfig
.pScratch
= 0;
183 sqlite3GlobalConfig
.szScratch
= 0;
184 sqlite3GlobalConfig
.nScratch
= 0;
186 if( sqlite3GlobalConfig
.pPage
==0 || sqlite3GlobalConfig
.szPage
<512
187 || sqlite3GlobalConfig
.nPage
<1 ){
188 sqlite3GlobalConfig
.pPage
= 0;
189 sqlite3GlobalConfig
.szPage
= 0;
190 sqlite3GlobalConfig
.nPage
= 0;
192 return sqlite3GlobalConfig
.m
.xInit(sqlite3GlobalConfig
.m
.pAppData
);
196 ** Return true if the heap is currently under memory pressure - in other
197 ** words if the amount of heap used is close to the limit set by
198 ** sqlite3_soft_heap_limit().
200 int sqlite3HeapNearlyFull(void){
201 return mem0
.nearlyFull
;
205 ** Deinitialize the memory allocation subsystem.
207 void sqlite3MallocEnd(void){
208 if( sqlite3GlobalConfig
.m
.xShutdown
){
209 sqlite3GlobalConfig
.m
.xShutdown(sqlite3GlobalConfig
.m
.pAppData
);
211 memset(&mem0
, 0, sizeof(mem0
));
215 ** Return the amount of memory currently checked out.
217 sqlite3_int64
sqlite3_memory_used(void){
220 sqlite3_status(SQLITE_STATUS_MEMORY_USED
, &n
, &mx
, 0);
221 res
= (sqlite3_int64
)n
; /* Work around bug in Borland C. Ticket #3216 */
226 ** Return the maximum amount of memory that has ever been
227 ** checked out since either the beginning of this process
228 ** or since the most recent reset.
230 sqlite3_int64
sqlite3_memory_highwater(int resetFlag
){
233 sqlite3_status(SQLITE_STATUS_MEMORY_USED
, &n
, &mx
, resetFlag
);
234 res
= (sqlite3_int64
)mx
; /* Work around bug in Borland C. Ticket #3216 */
241 static void sqlite3MallocAlarm(int nByte
){
242 void (*xCallback
)(void*,sqlite3_int64
,int);
243 sqlite3_int64 nowUsed
;
245 if( mem0
.alarmCallback
==0 ) return;
246 xCallback
= mem0
.alarmCallback
;
247 nowUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
248 pArg
= mem0
.alarmArg
;
249 mem0
.alarmCallback
= 0;
250 sqlite3_mutex_leave(mem0
.mutex
);
251 xCallback(pArg
, nowUsed
, nByte
);
252 sqlite3_mutex_enter(mem0
.mutex
);
253 mem0
.alarmCallback
= xCallback
;
254 mem0
.alarmArg
= pArg
;
258 ** Do a memory allocation with statistics and alarms. Assume the
259 ** lock is already held.
261 static int mallocWithAlarm(int n
, void **pp
){
264 assert( sqlite3_mutex_held(mem0
.mutex
) );
265 nFull
= sqlite3GlobalConfig
.m
.xRoundup(n
);
266 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE
, n
);
267 if( mem0
.alarmCallback
!=0 ){
268 int nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
269 if( nUsed
+nFull
>= mem0
.alarmThreshold
){
271 sqlite3MallocAlarm(nFull
);
276 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
277 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
278 if( p
==0 && mem0
.alarmCallback
){
279 sqlite3MallocAlarm(nFull
);
280 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
284 nFull
= sqlite3MallocSize(p
);
285 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, nFull
);
286 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT
, 1);
293 ** Allocate memory. This routine is like sqlite3_malloc() except that it
294 ** assumes the memory subsystem has already been initialized.
296 void *sqlite3Malloc(int n
){
298 if( n
<=0 /* IMP: R-65312-04917 */
301 /* A memory allocation of a number of bytes which is near the maximum
302 ** signed integer value might cause an integer overflow inside of the
303 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
304 ** 255 bytes of overhead. SQLite itself will never use anything near
305 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
307 }else if( sqlite3GlobalConfig
.bMemstat
){
308 sqlite3_mutex_enter(mem0
.mutex
);
309 mallocWithAlarm(n
, &p
);
310 sqlite3_mutex_leave(mem0
.mutex
);
312 p
= sqlite3GlobalConfig
.m
.xMalloc(n
);
314 assert( EIGHT_BYTE_ALIGNMENT(p
) ); /* IMP: R-04675-44850 */
319 ** This version of the memory allocation is for use by the application.
320 ** First make sure the memory subsystem is initialized, then do the
323 void *sqlite3_malloc(int n
){
324 #ifndef SQLITE_OMIT_AUTOINIT
325 if( sqlite3_initialize() ) return 0;
327 return sqlite3Malloc(n
);
331 ** Each thread may only have a single outstanding allocation from
332 ** xScratchMalloc(). We verify this constraint in the single-threaded
333 ** case by setting scratchAllocOut to 1 when an allocation
334 ** is outstanding clearing it when the allocation is freed.
336 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
337 static int scratchAllocOut
= 0;
342 ** Allocate memory that is to be used and released right away.
343 ** This routine is similar to alloca() in that it is not intended
344 ** for situations where the memory might be held long-term. This
345 ** routine is intended to get memory to old large transient data
346 ** structures that would not normally fit on the stack of an
347 ** embedded processor.
349 void *sqlite3ScratchMalloc(int n
){
353 sqlite3_mutex_enter(mem0
.mutex
);
354 if( mem0
.nScratchFree
&& sqlite3GlobalConfig
.szScratch
>=n
){
355 p
= mem0
.pScratchFree
;
356 mem0
.pScratchFree
= mem0
.pScratchFree
->pNext
;
358 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED
, 1);
359 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE
, n
);
360 sqlite3_mutex_leave(mem0
.mutex
);
362 if( sqlite3GlobalConfig
.bMemstat
){
363 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE
, n
);
364 n
= mallocWithAlarm(n
, &p
);
365 if( p
) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW
, n
);
366 sqlite3_mutex_leave(mem0
.mutex
);
368 sqlite3_mutex_leave(mem0
.mutex
);
369 p
= sqlite3GlobalConfig
.m
.xMalloc(n
);
371 sqlite3MemdebugSetType(p
, MEMTYPE_SCRATCH
);
373 assert( sqlite3_mutex_notheld(mem0
.mutex
) );
376 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
377 /* Verify that no more than two scratch allocations per thread
378 ** are outstanding at one time. (This is only checked in the
379 ** single-threaded case since checking in the multi-threaded case
380 ** would be much more complicated.) */
381 assert( scratchAllocOut
<=1 );
382 if( p
) scratchAllocOut
++;
387 void sqlite3ScratchFree(void *p
){
390 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
391 /* Verify that no more than two scratch allocation per thread
392 ** is outstanding at one time. (This is only checked in the
393 ** single-threaded case since checking in the multi-threaded case
394 ** would be much more complicated.) */
395 assert( scratchAllocOut
>=1 && scratchAllocOut
<=2 );
399 if( p
>=sqlite3GlobalConfig
.pScratch
&& p
<mem0
.pScratchEnd
){
400 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
401 ScratchFreeslot
*pSlot
;
402 pSlot
= (ScratchFreeslot
*)p
;
403 sqlite3_mutex_enter(mem0
.mutex
);
404 pSlot
->pNext
= mem0
.pScratchFree
;
405 mem0
.pScratchFree
= pSlot
;
407 assert( mem0
.nScratchFree
<= (u32
)sqlite3GlobalConfig
.nScratch
);
408 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED
, -1);
409 sqlite3_mutex_leave(mem0
.mutex
);
411 /* Release memory back to the heap */
412 assert( sqlite3MemdebugHasType(p
, MEMTYPE_SCRATCH
) );
413 assert( sqlite3MemdebugNoType(p
, ~MEMTYPE_SCRATCH
) );
414 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
415 if( sqlite3GlobalConfig
.bMemstat
){
416 int iSize
= sqlite3MallocSize(p
);
417 sqlite3_mutex_enter(mem0
.mutex
);
418 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW
, -iSize
);
419 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, -iSize
);
420 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT
, -1);
421 sqlite3GlobalConfig
.m
.xFree(p
);
422 sqlite3_mutex_leave(mem0
.mutex
);
424 sqlite3GlobalConfig
.m
.xFree(p
);
431 ** TRUE if p is a lookaside memory allocation from db
433 #ifndef SQLITE_OMIT_LOOKASIDE
434 static int isLookaside(sqlite3
*db
, void *p
){
435 return p
&& p
>=db
->lookaside
.pStart
&& p
<db
->lookaside
.pEnd
;
438 #define isLookaside(A,B) 0
442 ** Return the size of a memory allocation previously obtained from
443 ** sqlite3Malloc() or sqlite3_malloc().
445 int sqlite3MallocSize(void *p
){
446 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
447 assert( sqlite3MemdebugNoType(p
, MEMTYPE_DB
) );
448 return sqlite3GlobalConfig
.m
.xSize(p
);
450 int sqlite3DbMallocSize(sqlite3
*db
, void *p
){
451 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
452 if( db
&& isLookaside(db
, p
) ){
453 return db
->lookaside
.sz
;
455 assert( sqlite3MemdebugHasType(p
, MEMTYPE_DB
) );
456 assert( sqlite3MemdebugHasType(p
, MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
) );
457 assert( db
!=0 || sqlite3MemdebugNoType(p
, MEMTYPE_LOOKASIDE
) );
458 return sqlite3GlobalConfig
.m
.xSize(p
);
463 ** Free memory previously obtained from sqlite3Malloc().
465 void sqlite3_free(void *p
){
466 if( p
==0 ) return; /* IMP: R-49053-54554 */
467 assert( sqlite3MemdebugNoType(p
, MEMTYPE_DB
) );
468 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
469 if( sqlite3GlobalConfig
.bMemstat
){
470 sqlite3_mutex_enter(mem0
.mutex
);
471 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, -sqlite3MallocSize(p
));
472 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT
, -1);
473 sqlite3GlobalConfig
.m
.xFree(p
);
474 sqlite3_mutex_leave(mem0
.mutex
);
476 sqlite3GlobalConfig
.m
.xFree(p
);
481 ** Free memory that might be associated with a particular database
484 void sqlite3DbFree(sqlite3
*db
, void *p
){
485 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
487 if( db
->pnBytesFreed
){
488 *db
->pnBytesFreed
+= sqlite3DbMallocSize(db
, p
);
491 if( isLookaside(db
, p
) ){
492 LookasideSlot
*pBuf
= (LookasideSlot
*)p
;
493 pBuf
->pNext
= db
->lookaside
.pFree
;
494 db
->lookaside
.pFree
= pBuf
;
495 db
->lookaside
.nOut
--;
499 assert( sqlite3MemdebugHasType(p
, MEMTYPE_DB
) );
500 assert( sqlite3MemdebugHasType(p
, MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
) );
501 assert( db
!=0 || sqlite3MemdebugNoType(p
, MEMTYPE_LOOKASIDE
) );
502 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
507 ** Change the size of an existing memory allocation
509 void *sqlite3Realloc(void *pOld
, int nBytes
){
513 return sqlite3Malloc(nBytes
); /* IMP: R-28354-25769 */
516 sqlite3_free(pOld
); /* IMP: R-31593-10574 */
519 if( nBytes
>=0x7fffff00 ){
520 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
523 nOld
= sqlite3MallocSize(pOld
);
524 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
525 ** argument to xRealloc is always a value returned by a prior call to
527 nNew
= sqlite3GlobalConfig
.m
.xRoundup(nBytes
);
530 }else if( sqlite3GlobalConfig
.bMemstat
){
531 sqlite3_mutex_enter(mem0
.mutex
);
532 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE
, nBytes
);
533 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
)+nNew
-nOld
>=
534 mem0
.alarmThreshold
){
535 sqlite3MallocAlarm(nNew
-nOld
);
537 assert( sqlite3MemdebugHasType(pOld
, MEMTYPE_HEAP
) );
538 assert( sqlite3MemdebugNoType(pOld
, ~MEMTYPE_HEAP
) );
539 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
540 if( pNew
==0 && mem0
.alarmCallback
){
541 sqlite3MallocAlarm(nBytes
);
542 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
545 nNew
= sqlite3MallocSize(pNew
);
546 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, nNew
-nOld
);
548 sqlite3_mutex_leave(mem0
.mutex
);
550 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
552 assert( EIGHT_BYTE_ALIGNMENT(pNew
) ); /* IMP: R-04675-44850 */
557 ** The public interface to sqlite3Realloc. Make sure that the memory
558 ** subsystem is initialized prior to invoking sqliteRealloc.
560 void *sqlite3_realloc(void *pOld
, int n
){
561 #ifndef SQLITE_OMIT_AUTOINIT
562 if( sqlite3_initialize() ) return 0;
564 return sqlite3Realloc(pOld
, n
);
569 ** Allocate and zero memory.
571 void *sqlite3MallocZero(int n
){
572 void *p
= sqlite3Malloc(n
);
580 ** Allocate and zero memory. If the allocation fails, make
581 ** the mallocFailed flag in the connection pointer.
583 void *sqlite3DbMallocZero(sqlite3
*db
, int n
){
584 void *p
= sqlite3DbMallocRaw(db
, n
);
592 ** Allocate and zero memory. If the allocation fails, make
593 ** the mallocFailed flag in the connection pointer.
595 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
596 ** failure on the same database connection) then always return 0.
597 ** Hence for a particular database connection, once malloc starts
598 ** failing, it fails consistently until mallocFailed is reset.
599 ** This is an important assumption. There are many places in the
600 ** code that do things like this:
602 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
603 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
604 ** if( b ) a[10] = 9;
606 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
607 ** that all prior mallocs (ex: "a") worked too.
609 void *sqlite3DbMallocRaw(sqlite3
*db
, int n
){
611 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
612 assert( db
==0 || db
->pnBytesFreed
==0 );
613 #ifndef SQLITE_OMIT_LOOKASIDE
616 if( db
->mallocFailed
){
619 if( db
->lookaside
.bEnabled
){
620 if( n
>db
->lookaside
.sz
){
621 db
->lookaside
.anStat
[1]++;
622 }else if( (pBuf
= db
->lookaside
.pFree
)==0 ){
623 db
->lookaside
.anStat
[2]++;
625 db
->lookaside
.pFree
= pBuf
->pNext
;
626 db
->lookaside
.nOut
++;
627 db
->lookaside
.anStat
[0]++;
628 if( db
->lookaside
.nOut
>db
->lookaside
.mxOut
){
629 db
->lookaside
.mxOut
= db
->lookaside
.nOut
;
636 if( db
&& db
->mallocFailed
){
640 p
= sqlite3Malloc(n
);
642 db
->mallocFailed
= 1;
644 sqlite3MemdebugSetType(p
, MEMTYPE_DB
|
645 ((db
&& db
->lookaside
.bEnabled
) ? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
));
650 ** Resize the block of memory pointed to by p to n bytes. If the
651 ** resize fails, set the mallocFailed flag in the connection object.
653 void *sqlite3DbRealloc(sqlite3
*db
, void *p
, int n
){
656 assert( sqlite3_mutex_held(db
->mutex
) );
657 if( db
->mallocFailed
==0 ){
659 return sqlite3DbMallocRaw(db
, n
);
661 if( isLookaside(db
, p
) ){
662 if( n
<=db
->lookaside
.sz
){
665 pNew
= sqlite3DbMallocRaw(db
, n
);
667 memcpy(pNew
, p
, db
->lookaside
.sz
);
668 sqlite3DbFree(db
, p
);
671 assert( sqlite3MemdebugHasType(p
, MEMTYPE_DB
) );
672 assert( sqlite3MemdebugHasType(p
, MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
) );
673 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
674 pNew
= sqlite3_realloc(p
, n
);
676 sqlite3MemdebugSetType(p
, MEMTYPE_DB
|MEMTYPE_HEAP
);
677 db
->mallocFailed
= 1;
679 sqlite3MemdebugSetType(pNew
, MEMTYPE_DB
|
680 (db
->lookaside
.bEnabled
? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
));
687 ** Attempt to reallocate p. If the reallocation fails, then free p
688 ** and set the mallocFailed flag in the database connection.
690 void *sqlite3DbReallocOrFree(sqlite3
*db
, void *p
, int n
){
692 pNew
= sqlite3DbRealloc(db
, p
, n
);
694 sqlite3DbFree(db
, p
);
700 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
701 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
702 ** is because when memory debugging is turned on, these two functions are
703 ** called via macros that record the current file and line number in the
704 ** ThreadData structure.
706 char *sqlite3DbStrDup(sqlite3
*db
, const char *z
){
712 n
= sqlite3Strlen30(z
) + 1;
713 assert( (n
&0x7fffffff)==n
);
714 zNew
= sqlite3DbMallocRaw(db
, (int)n
);
720 char *sqlite3DbStrNDup(sqlite3
*db
, const char *z
, int n
){
725 assert( (n
&0x7fffffff)==n
);
726 zNew
= sqlite3DbMallocRaw(db
, n
+1);
735 ** Create a string from the zFromat argument and the va_list that follows.
736 ** Store the string in memory obtained from sqliteMalloc() and make *pz
737 ** point to that string.
739 void sqlite3SetString(char **pz
, sqlite3
*db
, const char *zFormat
, ...){
743 va_start(ap
, zFormat
);
744 z
= sqlite3VMPrintf(db
, zFormat
, ap
);
746 sqlite3DbFree(db
, *pz
);
752 ** This function must be called before exiting any API function (i.e.
753 ** returning control to the user) that has called sqlite3_malloc or
756 ** The returned value is normally a copy of the second argument to this
757 ** function. However, if a malloc() failure has occurred since the previous
758 ** invocation SQLITE_NOMEM is returned instead.
760 ** If the first argument, db, is not NULL and a malloc() error has occurred,
761 ** then the connection error-code (the value returned by sqlite3_errcode())
762 ** is set to SQLITE_NOMEM.
764 int sqlite3ApiExit(sqlite3
* db
, int rc
){
765 /* If the db handle is not NULL, then we must hold the connection handle
766 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
767 ** is unsafe, as is the call to sqlite3Error().
769 assert( !db
|| sqlite3_mutex_held(db
->mutex
) );
770 if( db
&& (db
->mallocFailed
|| rc
==SQLITE_IOERR_NOMEM
) ){
771 sqlite3Error(db
, SQLITE_NOMEM
, 0);
772 db
->mallocFailed
= 0;
775 return rc
& (db
? db
->errMask
: 0xff);