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 ** Default value of the hard heap limit. 0 means "no limit".
38 #ifndef SQLITE_MAX_MEMORY
39 # define SQLITE_MAX_MEMORY 0
43 ** State information local to the memory allocation subsystem.
45 static SQLITE_WSD
struct Mem0Global
{
46 sqlite3_mutex
*mutex
; /* Mutex to serialize access */
47 sqlite3_int64 alarmThreshold
; /* The soft heap limit */
48 sqlite3_int64 hardLimit
; /* The hard upper bound on memory */
51 ** True if heap is nearly "full" where "full" is defined by the
52 ** sqlite3_soft_heap_limit() setting.
55 } mem0
= { 0, SQLITE_MAX_MEMORY
, SQLITE_MAX_MEMORY
, 0 };
57 #define mem0 GLOBAL(struct Mem0Global, mem0)
60 ** Return the memory allocator mutex. sqlite3_status() needs it.
62 sqlite3_mutex
*sqlite3MallocMutex(void){
66 #ifndef SQLITE_OMIT_DEPRECATED
68 ** Deprecated external interface. It used to set an alarm callback
69 ** that was invoked when memory usage grew too large. Now it is a
72 int sqlite3_memory_alarm(
73 void(*xCallback
)(void *pArg
, sqlite3_int64 used
,int N
),
75 sqlite3_int64 iThreshold
85 ** Set the soft heap-size limit for the library. An argument of
86 ** zero disables the limit. A negative argument is a no-op used to
87 ** obtain the return value.
89 ** The return value is the value of the heap limit just before this
90 ** interface was called.
92 ** If the hard heap limit is enabled, then the soft heap limit cannot
93 ** be disabled nor raised above the hard heap limit.
95 sqlite3_int64
sqlite3_soft_heap_limit64(sqlite3_int64 n
){
96 sqlite3_int64 priorLimit
;
99 #ifndef SQLITE_OMIT_AUTOINIT
100 int rc
= sqlite3_initialize();
103 sqlite3_mutex_enter(mem0
.mutex
);
104 priorLimit
= mem0
.alarmThreshold
;
106 sqlite3_mutex_leave(mem0
.mutex
);
109 if( mem0
.hardLimit
>0 && (n
>mem0
.hardLimit
|| n
==0) ){
112 mem0
.alarmThreshold
= n
;
113 nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
114 AtomicStore(&mem0
.nearlyFull
, n
>0 && n
<=nUsed
);
115 sqlite3_mutex_leave(mem0
.mutex
);
116 excess
= sqlite3_memory_used() - n
;
117 if( excess
>0 ) sqlite3_release_memory((int)(excess
& 0x7fffffff));
120 void sqlite3_soft_heap_limit(int n
){
122 sqlite3_soft_heap_limit64(n
);
126 ** Set the hard heap-size limit for the library. An argument of zero
127 ** disables the hard heap limit. A negative argument is a no-op used
128 ** to obtain the return value without affecting the hard heap limit.
130 ** The return value is the value of the hard heap limit just prior to
131 ** calling this interface.
133 ** Setting the hard heap limit will also activate the soft heap limit
134 ** and constrain the soft heap limit to be no more than the hard heap
137 sqlite3_int64
sqlite3_hard_heap_limit64(sqlite3_int64 n
){
138 sqlite3_int64 priorLimit
;
139 #ifndef SQLITE_OMIT_AUTOINIT
140 int rc
= sqlite3_initialize();
143 sqlite3_mutex_enter(mem0
.mutex
);
144 priorLimit
= mem0
.hardLimit
;
147 if( n
<mem0
.alarmThreshold
|| mem0
.alarmThreshold
==0 ){
148 mem0
.alarmThreshold
= n
;
151 sqlite3_mutex_leave(mem0
.mutex
);
157 ** Initialize the memory allocation subsystem.
159 int sqlite3MallocInit(void){
161 if( sqlite3GlobalConfig
.m
.xMalloc
==0 ){
162 sqlite3MemSetDefault();
164 mem0
.mutex
= sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM
);
165 if( sqlite3GlobalConfig
.pPage
==0 || sqlite3GlobalConfig
.szPage
<512
166 || sqlite3GlobalConfig
.nPage
<=0 ){
167 sqlite3GlobalConfig
.pPage
= 0;
168 sqlite3GlobalConfig
.szPage
= 0;
170 rc
= sqlite3GlobalConfig
.m
.xInit(sqlite3GlobalConfig
.m
.pAppData
);
171 if( rc
!=SQLITE_OK
) memset(&mem0
, 0, sizeof(mem0
));
176 ** Return true if the heap is currently under memory pressure - in other
177 ** words if the amount of heap used is close to the limit set by
178 ** sqlite3_soft_heap_limit().
180 int sqlite3HeapNearlyFull(void){
181 return AtomicLoad(&mem0
.nearlyFull
);
185 ** Deinitialize the memory allocation subsystem.
187 void sqlite3MallocEnd(void){
188 if( sqlite3GlobalConfig
.m
.xShutdown
){
189 sqlite3GlobalConfig
.m
.xShutdown(sqlite3GlobalConfig
.m
.pAppData
);
191 memset(&mem0
, 0, sizeof(mem0
));
195 ** Return the amount of memory currently checked out.
197 sqlite3_int64
sqlite3_memory_used(void){
198 sqlite3_int64 res
, mx
;
199 sqlite3_status64(SQLITE_STATUS_MEMORY_USED
, &res
, &mx
, 0);
204 ** Return the maximum amount of memory that has ever been
205 ** checked out since either the beginning of this process
206 ** or since the most recent reset.
208 sqlite3_int64
sqlite3_memory_highwater(int resetFlag
){
209 sqlite3_int64 res
, mx
;
210 sqlite3_status64(SQLITE_STATUS_MEMORY_USED
, &res
, &mx
, resetFlag
);
217 static void sqlite3MallocAlarm(int nByte
){
218 if( mem0
.alarmThreshold
<=0 ) return;
219 sqlite3_mutex_leave(mem0
.mutex
);
220 sqlite3_release_memory(nByte
);
221 sqlite3_mutex_enter(mem0
.mutex
);
225 ** Do a memory allocation with statistics and alarms. Assume the
226 ** lock is already held.
228 static void mallocWithAlarm(int n
, void **pp
){
231 assert( sqlite3_mutex_held(mem0
.mutex
) );
234 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
235 ** implementation of malloc_good_size(), which must be called in debug
236 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
237 ** or else a crash results. Hence, do not attempt to optimize out the
238 ** following xRoundup() call. */
239 nFull
= sqlite3GlobalConfig
.m
.xRoundup(n
);
241 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE
, n
);
242 if( mem0
.alarmThreshold
>0 ){
243 sqlite3_int64 nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
244 if( nUsed
>= mem0
.alarmThreshold
- nFull
){
245 AtomicStore(&mem0
.nearlyFull
, 1);
246 sqlite3MallocAlarm(nFull
);
247 if( mem0
.hardLimit
){
248 nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
249 if( nUsed
>= mem0
.hardLimit
- nFull
){
255 AtomicStore(&mem0
.nearlyFull
, 0);
258 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
259 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
260 if( p
==0 && mem0
.alarmThreshold
>0 ){
261 sqlite3MallocAlarm(nFull
);
262 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
266 nFull
= sqlite3MallocSize(p
);
267 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED
, nFull
);
268 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT
, 1);
274 ** Maximum size of any single memory allocation.
276 ** This is not a limit on the total amount of memory used. This is
277 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
279 ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
280 ** This provides a 256-byte safety margin for defense against 32-bit
281 ** signed integer overflow bugs when computing memory allocation sizes.
282 ** Paranoid applications might want to reduce the maximum allocation size
283 ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
284 ** or even smaller would be reasonable upper bounds on the size of a memory
285 ** allocations for most applications.
287 #ifndef SQLITE_MAX_ALLOCATION_SIZE
288 # define SQLITE_MAX_ALLOCATION_SIZE 2147483391
290 #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
291 # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
295 ** Allocate memory. This routine is like sqlite3_malloc() except that it
296 ** assumes the memory subsystem has already been initialized.
298 void *sqlite3Malloc(u64 n
){
300 if( n
==0 || n
>SQLITE_MAX_ALLOCATION_SIZE
){
302 }else if( sqlite3GlobalConfig
.bMemstat
){
303 sqlite3_mutex_enter(mem0
.mutex
);
304 mallocWithAlarm((int)n
, &p
);
305 sqlite3_mutex_leave(mem0
.mutex
);
307 p
= sqlite3GlobalConfig
.m
.xMalloc((int)n
);
309 assert( EIGHT_BYTE_ALIGNMENT(p
) ); /* IMP: R-11148-40995 */
314 ** This version of the memory allocation is for use by the application.
315 ** First make sure the memory subsystem is initialized, then do the
318 void *sqlite3_malloc(int n
){
319 #ifndef SQLITE_OMIT_AUTOINIT
320 if( sqlite3_initialize() ) return 0;
322 return n
<=0 ? 0 : sqlite3Malloc(n
);
324 void *sqlite3_malloc64(sqlite3_uint64 n
){
325 #ifndef SQLITE_OMIT_AUTOINIT
326 if( sqlite3_initialize() ) return 0;
328 return sqlite3Malloc(n
);
332 ** TRUE if p is a lookaside memory allocation from db
334 #ifndef SQLITE_OMIT_LOOKASIDE
335 static int isLookaside(sqlite3
*db
, const void *p
){
336 return SQLITE_WITHIN(p
, db
->lookaside
.pStart
, db
->lookaside
.pTrueEnd
);
339 #define isLookaside(A,B) 0
343 ** Return the size of a memory allocation previously obtained from
344 ** sqlite3Malloc() or sqlite3_malloc().
346 int sqlite3MallocSize(const void *p
){
347 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
348 return sqlite3GlobalConfig
.m
.xSize((void*)p
);
350 static int lookasideMallocSize(sqlite3
*db
, const void *p
){
351 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
352 return p
<db
->lookaside
.pMiddle
? db
->lookaside
.szTrue
: LOOKASIDE_SMALL
;
354 return db
->lookaside
.szTrue
;
357 int sqlite3DbMallocSize(sqlite3
*db
, const void *p
){
361 assert( sqlite3MemdebugNoType(p
, (u8
)~MEMTYPE_HEAP
) );
362 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
363 }else if( !isLookaside(db
,p
) ){
364 assert( sqlite3MemdebugHasType(p
, (MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
365 assert( sqlite3MemdebugNoType(p
, (u8
)~(MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
369 if( ((uptr
)p
)<(uptr
)(db
->lookaside
.pTrueEnd
) ){
370 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
371 if( ((uptr
)p
)>=(uptr
)(db
->lookaside
.pMiddle
) ){
372 assert( sqlite3_mutex_held(db
->mutex
) );
373 return LOOKASIDE_SMALL
;
376 if( ((uptr
)p
)>=(uptr
)(db
->lookaside
.pStart
) ){
377 assert( sqlite3_mutex_held(db
->mutex
) );
378 return db
->lookaside
.szTrue
;
382 return sqlite3GlobalConfig
.m
.xSize((void*)p
);
384 sqlite3_uint64
sqlite3_msize(void *p
){
385 assert( sqlite3MemdebugNoType(p
, (u8
)~MEMTYPE_HEAP
) );
386 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
387 return p
? sqlite3GlobalConfig
.m
.xSize(p
) : 0;
391 ** Free memory previously obtained from sqlite3Malloc().
393 void sqlite3_free(void *p
){
394 if( p
==0 ) return; /* IMP: R-49053-54554 */
395 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
396 assert( sqlite3MemdebugNoType(p
, (u8
)~MEMTYPE_HEAP
) );
397 if( sqlite3GlobalConfig
.bMemstat
){
398 sqlite3_mutex_enter(mem0
.mutex
);
399 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED
, sqlite3MallocSize(p
));
400 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT
, 1);
401 sqlite3GlobalConfig
.m
.xFree(p
);
402 sqlite3_mutex_leave(mem0
.mutex
);
404 sqlite3GlobalConfig
.m
.xFree(p
);
409 ** Add the size of memory allocation "p" to the count in
410 ** *db->pnBytesFreed.
412 static SQLITE_NOINLINE
void measureAllocationSize(sqlite3
*db
, void *p
){
413 *db
->pnBytesFreed
+= sqlite3DbMallocSize(db
,p
);
417 ** Free memory that might be associated with a particular database
418 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
419 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
421 void sqlite3DbFreeNN(sqlite3
*db
, void *p
){
422 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
425 if( ((uptr
)p
)<(uptr
)(db
->lookaside
.pEnd
) ){
426 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
427 if( ((uptr
)p
)>=(uptr
)(db
->lookaside
.pMiddle
) ){
428 LookasideSlot
*pBuf
= (LookasideSlot
*)p
;
429 assert( db
->pnBytesFreed
==0 );
431 memset(p
, 0xaa, LOOKASIDE_SMALL
); /* Trash freed content */
433 pBuf
->pNext
= db
->lookaside
.pSmallFree
;
434 db
->lookaside
.pSmallFree
= pBuf
;
437 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
438 if( ((uptr
)p
)>=(uptr
)(db
->lookaside
.pStart
) ){
439 LookasideSlot
*pBuf
= (LookasideSlot
*)p
;
440 assert( db
->pnBytesFreed
==0 );
442 memset(p
, 0xaa, db
->lookaside
.szTrue
); /* Trash freed content */
444 pBuf
->pNext
= db
->lookaside
.pFree
;
445 db
->lookaside
.pFree
= pBuf
;
449 if( db
->pnBytesFreed
){
450 measureAllocationSize(db
, p
);
454 assert( sqlite3MemdebugHasType(p
, (MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
455 assert( sqlite3MemdebugNoType(p
, (u8
)~(MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
456 assert( db
!=0 || sqlite3MemdebugNoType(p
, MEMTYPE_LOOKASIDE
) );
457 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
460 void sqlite3DbNNFreeNN(sqlite3
*db
, void *p
){
462 assert( sqlite3_mutex_held(db
->mutex
) );
464 if( ((uptr
)p
)<(uptr
)(db
->lookaside
.pEnd
) ){
465 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
466 if( ((uptr
)p
)>=(uptr
)(db
->lookaside
.pMiddle
) ){
467 LookasideSlot
*pBuf
= (LookasideSlot
*)p
;
468 assert( db
->pnBytesFreed
==0 );
470 memset(p
, 0xaa, LOOKASIDE_SMALL
); /* Trash freed content */
472 pBuf
->pNext
= db
->lookaside
.pSmallFree
;
473 db
->lookaside
.pSmallFree
= pBuf
;
476 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
477 if( ((uptr
)p
)>=(uptr
)(db
->lookaside
.pStart
) ){
478 LookasideSlot
*pBuf
= (LookasideSlot
*)p
;
479 assert( db
->pnBytesFreed
==0 );
481 memset(p
, 0xaa, db
->lookaside
.szTrue
); /* Trash freed content */
483 pBuf
->pNext
= db
->lookaside
.pFree
;
484 db
->lookaside
.pFree
= pBuf
;
488 if( db
->pnBytesFreed
){
489 measureAllocationSize(db
, p
);
492 assert( sqlite3MemdebugHasType(p
, (MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
493 assert( sqlite3MemdebugNoType(p
, (u8
)~(MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
494 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
497 void sqlite3DbFree(sqlite3
*db
, void *p
){
498 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
499 if( p
) sqlite3DbFreeNN(db
, p
);
503 ** Change the size of an existing memory allocation
505 void *sqlite3Realloc(void *pOld
, u64 nBytes
){
506 int nOld
, nNew
, nDiff
;
508 assert( sqlite3MemdebugHasType(pOld
, MEMTYPE_HEAP
) );
509 assert( sqlite3MemdebugNoType(pOld
, (u8
)~MEMTYPE_HEAP
) );
511 return sqlite3Malloc(nBytes
); /* IMP: R-04300-56712 */
514 sqlite3_free(pOld
); /* IMP: R-26507-47431 */
517 if( nBytes
>=0x7fffff00 ){
518 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
521 nOld
= sqlite3MallocSize(pOld
);
522 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
523 ** argument to xRealloc is always a value returned by a prior call to
525 nNew
= sqlite3GlobalConfig
.m
.xRoundup((int)nBytes
);
528 }else if( sqlite3GlobalConfig
.bMemstat
){
530 sqlite3_mutex_enter(mem0
.mutex
);
531 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE
, (int)nBytes
);
533 if( nDiff
>0 && (nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
)) >=
534 mem0
.alarmThreshold
-nDiff
){
535 sqlite3MallocAlarm(nDiff
);
536 if( mem0
.hardLimit
>0 && nUsed
>= mem0
.hardLimit
- nDiff
){
537 sqlite3_mutex_leave(mem0
.mutex
);
541 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
542 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
543 if( pNew
==0 && mem0
.alarmThreshold
>0 ){
544 sqlite3MallocAlarm((int)nBytes
);
545 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
549 nNew
= sqlite3MallocSize(pNew
);
550 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED
, nNew
-nOld
);
552 sqlite3_mutex_leave(mem0
.mutex
);
554 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
556 assert( EIGHT_BYTE_ALIGNMENT(pNew
) ); /* IMP: R-11148-40995 */
561 ** The public interface to sqlite3Realloc. Make sure that the memory
562 ** subsystem is initialized prior to invoking sqliteRealloc.
564 void *sqlite3_realloc(void *pOld
, int n
){
565 #ifndef SQLITE_OMIT_AUTOINIT
566 if( sqlite3_initialize() ) return 0;
568 if( n
<0 ) n
= 0; /* IMP: R-26507-47431 */
569 return sqlite3Realloc(pOld
, n
);
571 void *sqlite3_realloc64(void *pOld
, sqlite3_uint64 n
){
572 #ifndef SQLITE_OMIT_AUTOINIT
573 if( sqlite3_initialize() ) return 0;
575 return sqlite3Realloc(pOld
, n
);
580 ** Allocate and zero memory.
582 void *sqlite3MallocZero(u64 n
){
583 void *p
= sqlite3Malloc(n
);
585 memset(p
, 0, (size_t)n
);
591 ** Allocate and zero memory. If the allocation fails, make
592 ** the mallocFailed flag in the connection pointer.
594 void *sqlite3DbMallocZero(sqlite3
*db
, u64 n
){
597 p
= sqlite3DbMallocRaw(db
, n
);
598 if( p
) memset(p
, 0, (size_t)n
);
603 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
604 ** slower case when the allocation cannot be fulfilled using lookaside.
606 static SQLITE_NOINLINE
void *dbMallocRawFinish(sqlite3
*db
, u64 n
){
609 p
= sqlite3Malloc(n
);
610 if( !p
) sqlite3OomFault(db
);
611 sqlite3MemdebugSetType(p
,
612 (db
->lookaside
.bDisable
==0) ? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
);
617 ** Allocate memory, either lookaside (if possible) or heap.
618 ** If the allocation fails, set the mallocFailed flag in
619 ** the connection pointer.
621 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
622 ** failure on the same database connection) then always return 0.
623 ** Hence for a particular database connection, once malloc starts
624 ** failing, it fails consistently until mallocFailed is reset.
625 ** This is an important assumption. There are many places in the
626 ** code that do things like this:
628 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
629 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
630 ** if( b ) a[10] = 9;
632 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
633 ** that all prior mallocs (ex: "a") worked too.
635 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
636 ** not a NULL pointer.
638 void *sqlite3DbMallocRaw(sqlite3
*db
, u64 n
){
640 if( db
) return sqlite3DbMallocRawNN(db
, n
);
641 p
= sqlite3Malloc(n
);
642 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
645 void *sqlite3DbMallocRawNN(sqlite3
*db
, u64 n
){
646 #ifndef SQLITE_OMIT_LOOKASIDE
649 assert( sqlite3_mutex_held(db
->mutex
) );
650 assert( db
->pnBytesFreed
==0 );
651 if( n
>db
->lookaside
.sz
){
652 if( !db
->lookaside
.bDisable
){
653 db
->lookaside
.anStat
[1]++;
654 }else if( db
->mallocFailed
){
657 return dbMallocRawFinish(db
, n
);
659 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
660 if( n
<=LOOKASIDE_SMALL
){
661 if( (pBuf
= db
->lookaside
.pSmallFree
)!=0 ){
662 db
->lookaside
.pSmallFree
= pBuf
->pNext
;
663 db
->lookaside
.anStat
[0]++;
665 }else if( (pBuf
= db
->lookaside
.pSmallInit
)!=0 ){
666 db
->lookaside
.pSmallInit
= pBuf
->pNext
;
667 db
->lookaside
.anStat
[0]++;
672 if( (pBuf
= db
->lookaside
.pFree
)!=0 ){
673 db
->lookaside
.pFree
= pBuf
->pNext
;
674 db
->lookaside
.anStat
[0]++;
676 }else if( (pBuf
= db
->lookaside
.pInit
)!=0 ){
677 db
->lookaside
.pInit
= pBuf
->pNext
;
678 db
->lookaside
.anStat
[0]++;
681 db
->lookaside
.anStat
[2]++;
685 assert( sqlite3_mutex_held(db
->mutex
) );
686 assert( db
->pnBytesFreed
==0 );
687 if( db
->mallocFailed
){
691 return dbMallocRawFinish(db
, n
);
694 /* Forward declaration */
695 static SQLITE_NOINLINE
void *dbReallocFinish(sqlite3
*db
, void *p
, u64 n
);
698 ** Resize the block of memory pointed to by p to n bytes. If the
699 ** resize fails, set the mallocFailed flag in the connection object.
701 void *sqlite3DbRealloc(sqlite3
*db
, void *p
, u64 n
){
703 if( p
==0 ) return sqlite3DbMallocRawNN(db
, n
);
704 assert( sqlite3_mutex_held(db
->mutex
) );
705 if( ((uptr
)p
)<(uptr
)db
->lookaside
.pEnd
){
706 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
707 if( ((uptr
)p
)>=(uptr
)db
->lookaside
.pMiddle
){
708 if( n
<=LOOKASIDE_SMALL
) return p
;
711 if( ((uptr
)p
)>=(uptr
)db
->lookaside
.pStart
){
712 if( n
<=db
->lookaside
.szTrue
) return p
;
715 return dbReallocFinish(db
, p
, n
);
717 static SQLITE_NOINLINE
void *dbReallocFinish(sqlite3
*db
, void *p
, u64 n
){
721 if( db
->mallocFailed
==0 ){
722 if( isLookaside(db
, p
) ){
723 pNew
= sqlite3DbMallocRawNN(db
, n
);
725 memcpy(pNew
, p
, lookasideMallocSize(db
, p
));
726 sqlite3DbFree(db
, p
);
729 assert( sqlite3MemdebugHasType(p
, (MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
730 assert( sqlite3MemdebugNoType(p
, (u8
)~(MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
731 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
732 pNew
= sqlite3Realloc(p
, n
);
736 sqlite3MemdebugSetType(pNew
,
737 (db
->lookaside
.bDisable
==0 ? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
));
744 ** Attempt to reallocate p. If the reallocation fails, then free p
745 ** and set the mallocFailed flag in the database connection.
747 void *sqlite3DbReallocOrFree(sqlite3
*db
, void *p
, u64 n
){
749 pNew
= sqlite3DbRealloc(db
, p
, n
);
751 sqlite3DbFree(db
, p
);
757 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
758 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
759 ** is because when memory debugging is turned on, these two functions are
760 ** called via macros that record the current file and line number in the
761 ** ThreadData structure.
763 char *sqlite3DbStrDup(sqlite3
*db
, const char *z
){
770 zNew
= sqlite3DbMallocRaw(db
, n
);
776 char *sqlite3DbStrNDup(sqlite3
*db
, const char *z
, u64 n
){
779 assert( z
!=0 || n
==0 );
780 assert( (n
&0x7fffffff)==n
);
781 zNew
= z
? sqlite3DbMallocRawNN(db
, n
+1) : 0;
783 memcpy(zNew
, z
, (size_t)n
);
790 ** The text between zStart and zEnd represents a phrase within a larger
791 ** SQL statement. Make a copy of this phrase in space obtained form
792 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
794 char *sqlite3DbSpanDup(sqlite3
*db
, const char *zStart
, const char *zEnd
){
797 /* Because of the way the parser works, the span is guaranteed to contain
798 ** at least one non-space character */
799 for(n
=0; sqlite3Isspace(zStart
[n
]); n
++){ assert( &zStart
[n
]<zEnd
); }
801 while( sqlite3Isspace(zStart
[0]) ) zStart
++;
802 n
= (int)(zEnd
- zStart
);
803 while( sqlite3Isspace(zStart
[n
-1]) ) n
--;
804 return sqlite3DbStrNDup(db
, zStart
, n
);
808 ** Free any prior content in *pz and replace it with a copy of zNew.
810 void sqlite3SetString(char **pz
, sqlite3
*db
, const char *zNew
){
811 char *z
= sqlite3DbStrDup(db
, zNew
);
812 sqlite3DbFree(db
, *pz
);
817 ** Call this routine to record the fact that an OOM (out-of-memory) error
818 ** has happened. This routine will set db->mallocFailed, and also
819 ** temporarily disable the lookaside memory allocator and interrupt
820 ** any running VDBEs.
822 ** Always return a NULL pointer so that this routine can be invoked using
824 ** return sqlite3OomFault(db);
826 ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
827 ** common case where no OOM occurs.
829 void *sqlite3OomFault(sqlite3
*db
){
830 if( db
->mallocFailed
==0 && db
->bBenignMalloc
==0 ){
831 db
->mallocFailed
= 1;
832 if( db
->nVdbeExec
>0 ){
833 AtomicStore(&db
->u1
.isInterrupted
, 1);
838 sqlite3ErrorMsg(db
->pParse
, "out of memory");
839 db
->pParse
->rc
= SQLITE_NOMEM_BKPT
;
840 for(pParse
=db
->pParse
->pOuterParse
; pParse
; pParse
= pParse
->pOuterParse
){
842 pParse
->rc
= SQLITE_NOMEM
;
850 ** This routine reactivates the memory allocator and clears the
851 ** db->mallocFailed flag as necessary.
853 ** The memory allocator is not restarted if there are running
856 void sqlite3OomClear(sqlite3
*db
){
857 if( db
->mallocFailed
&& db
->nVdbeExec
==0 ){
858 db
->mallocFailed
= 0;
859 AtomicStore(&db
->u1
.isInterrupted
, 0);
860 assert( db
->lookaside
.bDisable
>0 );
866 ** Take actions at the end of an API call to deal with error codes.
868 static SQLITE_NOINLINE
int apiHandleError(sqlite3
*db
, int rc
){
869 if( db
->mallocFailed
|| rc
==SQLITE_IOERR_NOMEM
){
871 sqlite3Error(db
, SQLITE_NOMEM
);
872 return SQLITE_NOMEM_BKPT
;
874 return rc
& db
->errMask
;
878 ** This function must be called before exiting any API function (i.e.
879 ** returning control to the user) that has called sqlite3_malloc or
882 ** The returned value is normally a copy of the second argument to this
883 ** function. However, if a malloc() failure has occurred since the previous
884 ** invocation SQLITE_NOMEM is returned instead.
886 ** If an OOM as occurred, then the connection error-code (the value
887 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
889 int sqlite3ApiExit(sqlite3
* db
, int rc
){
890 /* If the db handle must hold the connection handle mutex here.
891 ** Otherwise the read (and possible write) of db->mallocFailed
892 ** is unsafe, as is the call to sqlite3Error().
895 assert( sqlite3_mutex_held(db
->mutex
) );
896 if( db
->mallocFailed
|| rc
){
897 return apiHandleError(db
, rc
);
899 return rc
& db
->errMask
;