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 int rc
= sqlite3_initialize();
136 sqlite3_mutex_enter(mem0
.mutex
);
137 priorLimit
= mem0
.alarmThreshold
;
138 sqlite3_mutex_leave(mem0
.mutex
);
139 if( n
<0 ) return priorLimit
;
141 sqlite3MemoryAlarm(softHeapLimitEnforcer
, 0, n
);
143 sqlite3MemoryAlarm(0, 0, 0);
145 excess
= sqlite3_memory_used() - n
;
146 if( excess
>0 ) sqlite3_release_memory((int)(excess
& 0x7fffffff));
149 void sqlite3_soft_heap_limit(int n
){
151 sqlite3_soft_heap_limit64(n
);
155 ** Initialize the memory allocation subsystem.
157 int sqlite3MallocInit(void){
158 if( sqlite3GlobalConfig
.m
.xMalloc
==0 ){
159 sqlite3MemSetDefault();
161 memset(&mem0
, 0, sizeof(mem0
));
162 if( sqlite3GlobalConfig
.bCoreMutex
){
163 mem0
.mutex
= sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM
);
165 if( sqlite3GlobalConfig
.pScratch
&& sqlite3GlobalConfig
.szScratch
>=100
166 && sqlite3GlobalConfig
.nScratch
>0 ){
168 ScratchFreeslot
*pSlot
;
169 sz
= ROUNDDOWN8(sqlite3GlobalConfig
.szScratch
);
170 sqlite3GlobalConfig
.szScratch
= sz
;
171 pSlot
= (ScratchFreeslot
*)sqlite3GlobalConfig
.pScratch
;
172 n
= sqlite3GlobalConfig
.nScratch
;
173 mem0
.pScratchFree
= pSlot
;
174 mem0
.nScratchFree
= n
;
175 for(i
=0; i
<n
-1; i
++){
176 pSlot
->pNext
= (ScratchFreeslot
*)(sz
+(char*)pSlot
);
177 pSlot
= pSlot
->pNext
;
180 mem0
.pScratchEnd
= (void*)&pSlot
[1];
182 mem0
.pScratchEnd
= 0;
183 sqlite3GlobalConfig
.pScratch
= 0;
184 sqlite3GlobalConfig
.szScratch
= 0;
185 sqlite3GlobalConfig
.nScratch
= 0;
187 if( sqlite3GlobalConfig
.pPage
==0 || sqlite3GlobalConfig
.szPage
<512
188 || sqlite3GlobalConfig
.nPage
<1 ){
189 sqlite3GlobalConfig
.pPage
= 0;
190 sqlite3GlobalConfig
.szPage
= 0;
191 sqlite3GlobalConfig
.nPage
= 0;
193 return sqlite3GlobalConfig
.m
.xInit(sqlite3GlobalConfig
.m
.pAppData
);
197 ** Return true if the heap is currently under memory pressure - in other
198 ** words if the amount of heap used is close to the limit set by
199 ** sqlite3_soft_heap_limit().
201 int sqlite3HeapNearlyFull(void){
202 return mem0
.nearlyFull
;
206 ** Deinitialize the memory allocation subsystem.
208 void sqlite3MallocEnd(void){
209 if( sqlite3GlobalConfig
.m
.xShutdown
){
210 sqlite3GlobalConfig
.m
.xShutdown(sqlite3GlobalConfig
.m
.pAppData
);
212 memset(&mem0
, 0, sizeof(mem0
));
216 ** Return the amount of memory currently checked out.
218 sqlite3_int64
sqlite3_memory_used(void){
221 sqlite3_status(SQLITE_STATUS_MEMORY_USED
, &n
, &mx
, 0);
222 res
= (sqlite3_int64
)n
; /* Work around bug in Borland C. Ticket #3216 */
227 ** Return the maximum amount of memory that has ever been
228 ** checked out since either the beginning of this process
229 ** or since the most recent reset.
231 sqlite3_int64
sqlite3_memory_highwater(int resetFlag
){
234 sqlite3_status(SQLITE_STATUS_MEMORY_USED
, &n
, &mx
, resetFlag
);
235 res
= (sqlite3_int64
)mx
; /* Work around bug in Borland C. Ticket #3216 */
242 static void sqlite3MallocAlarm(int nByte
){
243 void (*xCallback
)(void*,sqlite3_int64
,int);
244 sqlite3_int64 nowUsed
;
246 if( mem0
.alarmCallback
==0 ) return;
247 xCallback
= mem0
.alarmCallback
;
248 nowUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
249 pArg
= mem0
.alarmArg
;
250 mem0
.alarmCallback
= 0;
251 sqlite3_mutex_leave(mem0
.mutex
);
252 xCallback(pArg
, nowUsed
, nByte
);
253 sqlite3_mutex_enter(mem0
.mutex
);
254 mem0
.alarmCallback
= xCallback
;
255 mem0
.alarmArg
= pArg
;
259 ** Do a memory allocation with statistics and alarms. Assume the
260 ** lock is already held.
262 static int mallocWithAlarm(int n
, void **pp
){
265 assert( sqlite3_mutex_held(mem0
.mutex
) );
266 nFull
= sqlite3GlobalConfig
.m
.xRoundup(n
);
267 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE
, n
);
268 if( mem0
.alarmCallback
!=0 ){
269 int nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
270 if( nUsed
>= mem0
.alarmThreshold
- nFull
){
272 sqlite3MallocAlarm(nFull
);
277 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
278 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
279 if( p
==0 && mem0
.alarmCallback
){
280 sqlite3MallocAlarm(nFull
);
281 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
285 nFull
= sqlite3MallocSize(p
);
286 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, nFull
);
287 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT
, 1);
294 ** Allocate memory. This routine is like sqlite3_malloc() except that it
295 ** assumes the memory subsystem has already been initialized.
297 void *sqlite3Malloc(int n
){
299 if( n
<=0 /* IMP: R-65312-04917 */
302 /* A memory allocation of a number of bytes which is near the maximum
303 ** signed integer value might cause an integer overflow inside of the
304 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
305 ** 255 bytes of overhead. SQLite itself will never use anything near
306 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
308 }else if( sqlite3GlobalConfig
.bMemstat
){
309 sqlite3_mutex_enter(mem0
.mutex
);
310 mallocWithAlarm(n
, &p
);
311 sqlite3_mutex_leave(mem0
.mutex
);
313 p
= sqlite3GlobalConfig
.m
.xMalloc(n
);
315 assert( EIGHT_BYTE_ALIGNMENT(p
) ); /* IMP: R-04675-44850 */
320 ** This version of the memory allocation is for use by the application.
321 ** First make sure the memory subsystem is initialized, then do the
324 void *sqlite3_malloc(int n
){
325 #ifndef SQLITE_OMIT_AUTOINIT
326 if( sqlite3_initialize() ) return 0;
328 return sqlite3Malloc(n
);
332 ** Each thread may only have a single outstanding allocation from
333 ** xScratchMalloc(). We verify this constraint in the single-threaded
334 ** case by setting scratchAllocOut to 1 when an allocation
335 ** is outstanding clearing it when the allocation is freed.
337 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
338 static int scratchAllocOut
= 0;
343 ** Allocate memory that is to be used and released right away.
344 ** This routine is similar to alloca() in that it is not intended
345 ** for situations where the memory might be held long-term. This
346 ** routine is intended to get memory to old large transient data
347 ** structures that would not normally fit on the stack of an
348 ** embedded processor.
350 void *sqlite3ScratchMalloc(int n
){
354 sqlite3_mutex_enter(mem0
.mutex
);
355 if( mem0
.nScratchFree
&& sqlite3GlobalConfig
.szScratch
>=n
){
356 p
= mem0
.pScratchFree
;
357 mem0
.pScratchFree
= mem0
.pScratchFree
->pNext
;
359 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED
, 1);
360 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE
, n
);
361 sqlite3_mutex_leave(mem0
.mutex
);
363 if( sqlite3GlobalConfig
.bMemstat
){
364 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE
, n
);
365 n
= mallocWithAlarm(n
, &p
);
366 if( p
) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW
, n
);
367 sqlite3_mutex_leave(mem0
.mutex
);
369 sqlite3_mutex_leave(mem0
.mutex
);
370 p
= sqlite3GlobalConfig
.m
.xMalloc(n
);
372 sqlite3MemdebugSetType(p
, MEMTYPE_SCRATCH
);
374 assert( sqlite3_mutex_notheld(mem0
.mutex
) );
377 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
378 /* Verify that no more than two scratch allocations per thread
379 ** are outstanding at one time. (This is only checked in the
380 ** single-threaded case since checking in the multi-threaded case
381 ** would be much more complicated.) */
382 assert( scratchAllocOut
<=1 );
383 if( p
) scratchAllocOut
++;
388 void sqlite3ScratchFree(void *p
){
391 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
392 /* Verify that no more than two scratch allocation per thread
393 ** is outstanding at one time. (This is only checked in the
394 ** single-threaded case since checking in the multi-threaded case
395 ** would be much more complicated.) */
396 assert( scratchAllocOut
>=1 && scratchAllocOut
<=2 );
400 if( p
>=sqlite3GlobalConfig
.pScratch
&& p
<mem0
.pScratchEnd
){
401 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
402 ScratchFreeslot
*pSlot
;
403 pSlot
= (ScratchFreeslot
*)p
;
404 sqlite3_mutex_enter(mem0
.mutex
);
405 pSlot
->pNext
= mem0
.pScratchFree
;
406 mem0
.pScratchFree
= pSlot
;
408 assert( mem0
.nScratchFree
<= (u32
)sqlite3GlobalConfig
.nScratch
);
409 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED
, -1);
410 sqlite3_mutex_leave(mem0
.mutex
);
412 /* Release memory back to the heap */
413 assert( sqlite3MemdebugHasType(p
, MEMTYPE_SCRATCH
) );
414 assert( sqlite3MemdebugNoType(p
, ~MEMTYPE_SCRATCH
) );
415 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
416 if( sqlite3GlobalConfig
.bMemstat
){
417 int iSize
= sqlite3MallocSize(p
);
418 sqlite3_mutex_enter(mem0
.mutex
);
419 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW
, -iSize
);
420 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, -iSize
);
421 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT
, -1);
422 sqlite3GlobalConfig
.m
.xFree(p
);
423 sqlite3_mutex_leave(mem0
.mutex
);
425 sqlite3GlobalConfig
.m
.xFree(p
);
432 ** TRUE if p is a lookaside memory allocation from db
434 #ifndef SQLITE_OMIT_LOOKASIDE
435 static int isLookaside(sqlite3
*db
, void *p
){
436 return p
&& p
>=db
->lookaside
.pStart
&& p
<db
->lookaside
.pEnd
;
439 #define isLookaside(A,B) 0
443 ** Return the size of a memory allocation previously obtained from
444 ** sqlite3Malloc() or sqlite3_malloc().
446 int sqlite3MallocSize(void *p
){
447 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
448 assert( sqlite3MemdebugNoType(p
, MEMTYPE_DB
) );
449 return sqlite3GlobalConfig
.m
.xSize(p
);
451 int sqlite3DbMallocSize(sqlite3
*db
, void *p
){
452 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
453 if( db
&& isLookaside(db
, p
) ){
454 return db
->lookaside
.sz
;
456 assert( sqlite3MemdebugHasType(p
, MEMTYPE_DB
) );
457 assert( sqlite3MemdebugHasType(p
, MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
) );
458 assert( db
!=0 || sqlite3MemdebugNoType(p
, MEMTYPE_LOOKASIDE
) );
459 return sqlite3GlobalConfig
.m
.xSize(p
);
464 ** Free memory previously obtained from sqlite3Malloc().
466 void sqlite3_free(void *p
){
467 if( p
==0 ) return; /* IMP: R-49053-54554 */
468 assert( sqlite3MemdebugNoType(p
, MEMTYPE_DB
) );
469 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
470 if( sqlite3GlobalConfig
.bMemstat
){
471 sqlite3_mutex_enter(mem0
.mutex
);
472 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, -sqlite3MallocSize(p
));
473 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT
, -1);
474 sqlite3GlobalConfig
.m
.xFree(p
);
475 sqlite3_mutex_leave(mem0
.mutex
);
477 sqlite3GlobalConfig
.m
.xFree(p
);
482 ** Free memory that might be associated with a particular database
485 void sqlite3DbFree(sqlite3
*db
, void *p
){
486 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
488 if( db
->pnBytesFreed
){
489 *db
->pnBytesFreed
+= sqlite3DbMallocSize(db
, p
);
492 if( isLookaside(db
, p
) ){
493 LookasideSlot
*pBuf
= (LookasideSlot
*)p
;
495 /* Trash all content in the buffer being freed */
496 memset(p
, 0xaa, db
->lookaside
.sz
);
498 pBuf
->pNext
= db
->lookaside
.pFree
;
499 db
->lookaside
.pFree
= pBuf
;
500 db
->lookaside
.nOut
--;
504 assert( sqlite3MemdebugHasType(p
, MEMTYPE_DB
) );
505 assert( sqlite3MemdebugHasType(p
, MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
) );
506 assert( db
!=0 || sqlite3MemdebugNoType(p
, MEMTYPE_LOOKASIDE
) );
507 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
512 ** Change the size of an existing memory allocation
514 void *sqlite3Realloc(void *pOld
, int nBytes
){
515 int nOld
, nNew
, nDiff
;
518 return sqlite3Malloc(nBytes
); /* IMP: R-28354-25769 */
521 sqlite3_free(pOld
); /* IMP: R-31593-10574 */
524 if( nBytes
>=0x7fffff00 ){
525 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
528 nOld
= sqlite3MallocSize(pOld
);
529 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
530 ** argument to xRealloc is always a value returned by a prior call to
532 nNew
= sqlite3GlobalConfig
.m
.xRoundup(nBytes
);
535 }else if( sqlite3GlobalConfig
.bMemstat
){
536 sqlite3_mutex_enter(mem0
.mutex
);
537 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE
, nBytes
);
539 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
) >=
540 mem0
.alarmThreshold
-nDiff
){
541 sqlite3MallocAlarm(nDiff
);
543 assert( sqlite3MemdebugHasType(pOld
, MEMTYPE_HEAP
) );
544 assert( sqlite3MemdebugNoType(pOld
, ~MEMTYPE_HEAP
) );
545 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
546 if( pNew
==0 && mem0
.alarmCallback
){
547 sqlite3MallocAlarm(nBytes
);
548 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
551 nNew
= sqlite3MallocSize(pNew
);
552 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED
, nNew
-nOld
);
554 sqlite3_mutex_leave(mem0
.mutex
);
556 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
558 assert( EIGHT_BYTE_ALIGNMENT(pNew
) ); /* IMP: R-04675-44850 */
563 ** The public interface to sqlite3Realloc. Make sure that the memory
564 ** subsystem is initialized prior to invoking sqliteRealloc.
566 void *sqlite3_realloc(void *pOld
, int n
){
567 #ifndef SQLITE_OMIT_AUTOINIT
568 if( sqlite3_initialize() ) return 0;
570 return sqlite3Realloc(pOld
, n
);
575 ** Allocate and zero memory.
577 void *sqlite3MallocZero(int n
){
578 void *p
= sqlite3Malloc(n
);
586 ** Allocate and zero memory. If the allocation fails, make
587 ** the mallocFailed flag in the connection pointer.
589 void *sqlite3DbMallocZero(sqlite3
*db
, int n
){
590 void *p
= sqlite3DbMallocRaw(db
, n
);
598 ** Allocate and zero memory. If the allocation fails, make
599 ** the mallocFailed flag in the connection pointer.
601 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
602 ** failure on the same database connection) then always return 0.
603 ** Hence for a particular database connection, once malloc starts
604 ** failing, it fails consistently until mallocFailed is reset.
605 ** This is an important assumption. There are many places in the
606 ** code that do things like this:
608 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
609 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
610 ** if( b ) a[10] = 9;
612 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
613 ** that all prior mallocs (ex: "a") worked too.
615 void *sqlite3DbMallocRaw(sqlite3
*db
, int n
){
617 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
618 assert( db
==0 || db
->pnBytesFreed
==0 );
619 #ifndef SQLITE_OMIT_LOOKASIDE
622 if( db
->mallocFailed
){
625 if( db
->lookaside
.bEnabled
){
626 if( n
>db
->lookaside
.sz
){
627 db
->lookaside
.anStat
[1]++;
628 }else if( (pBuf
= db
->lookaside
.pFree
)==0 ){
629 db
->lookaside
.anStat
[2]++;
631 db
->lookaside
.pFree
= pBuf
->pNext
;
632 db
->lookaside
.nOut
++;
633 db
->lookaside
.anStat
[0]++;
634 if( db
->lookaside
.nOut
>db
->lookaside
.mxOut
){
635 db
->lookaside
.mxOut
= db
->lookaside
.nOut
;
642 if( db
&& db
->mallocFailed
){
646 p
= sqlite3Malloc(n
);
648 db
->mallocFailed
= 1;
650 sqlite3MemdebugSetType(p
, MEMTYPE_DB
|
651 ((db
&& db
->lookaside
.bEnabled
) ? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
));
656 ** Resize the block of memory pointed to by p to n bytes. If the
657 ** resize fails, set the mallocFailed flag in the connection object.
659 void *sqlite3DbRealloc(sqlite3
*db
, void *p
, int n
){
662 assert( sqlite3_mutex_held(db
->mutex
) );
663 if( db
->mallocFailed
==0 ){
665 return sqlite3DbMallocRaw(db
, n
);
667 if( isLookaside(db
, p
) ){
668 if( n
<=db
->lookaside
.sz
){
671 pNew
= sqlite3DbMallocRaw(db
, n
);
673 memcpy(pNew
, p
, db
->lookaside
.sz
);
674 sqlite3DbFree(db
, p
);
677 assert( sqlite3MemdebugHasType(p
, MEMTYPE_DB
) );
678 assert( sqlite3MemdebugHasType(p
, MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
) );
679 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
680 pNew
= sqlite3_realloc(p
, n
);
682 sqlite3MemdebugSetType(p
, MEMTYPE_DB
|MEMTYPE_HEAP
);
683 db
->mallocFailed
= 1;
685 sqlite3MemdebugSetType(pNew
, MEMTYPE_DB
|
686 (db
->lookaside
.bEnabled
? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
));
693 ** Attempt to reallocate p. If the reallocation fails, then free p
694 ** and set the mallocFailed flag in the database connection.
696 void *sqlite3DbReallocOrFree(sqlite3
*db
, void *p
, int n
){
698 pNew
= sqlite3DbRealloc(db
, p
, n
);
700 sqlite3DbFree(db
, p
);
706 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
707 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
708 ** is because when memory debugging is turned on, these two functions are
709 ** called via macros that record the current file and line number in the
710 ** ThreadData structure.
712 char *sqlite3DbStrDup(sqlite3
*db
, const char *z
){
718 n
= sqlite3Strlen30(z
) + 1;
719 assert( (n
&0x7fffffff)==n
);
720 zNew
= sqlite3DbMallocRaw(db
, (int)n
);
726 char *sqlite3DbStrNDup(sqlite3
*db
, const char *z
, int n
){
731 assert( (n
&0x7fffffff)==n
);
732 zNew
= sqlite3DbMallocRaw(db
, n
+1);
741 ** Create a string from the zFromat argument and the va_list that follows.
742 ** Store the string in memory obtained from sqliteMalloc() and make *pz
743 ** point to that string.
745 void sqlite3SetString(char **pz
, sqlite3
*db
, const char *zFormat
, ...){
749 va_start(ap
, zFormat
);
750 z
= sqlite3VMPrintf(db
, zFormat
, ap
);
752 sqlite3DbFree(db
, *pz
);
758 ** This function must be called before exiting any API function (i.e.
759 ** returning control to the user) that has called sqlite3_malloc or
762 ** The returned value is normally a copy of the second argument to this
763 ** function. However, if a malloc() failure has occurred since the previous
764 ** invocation SQLITE_NOMEM is returned instead.
766 ** If the first argument, db, is not NULL and a malloc() error has occurred,
767 ** then the connection error-code (the value returned by sqlite3_errcode())
768 ** is set to SQLITE_NOMEM.
770 int sqlite3ApiExit(sqlite3
* db
, int rc
){
771 /* If the db handle is not NULL, then we must hold the connection handle
772 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
773 ** is unsafe, as is the call to sqlite3Error().
775 assert( !db
|| sqlite3_mutex_held(db
->mutex
) );
776 if( db
&& (db
->mallocFailed
|| rc
==SQLITE_IOERR_NOMEM
) ){
777 sqlite3Error(db
, SQLITE_NOMEM
, 0);
778 db
->mallocFailed
= 0;
781 return rc
& (db
? db
->errMask
: 0xff);