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 ** State information local to the memory allocation subsystem.
38 static SQLITE_WSD
struct Mem0Global
{
39 sqlite3_mutex
*mutex
; /* Mutex to serialize access */
40 sqlite3_int64 alarmThreshold
; /* The soft heap limit */
43 ** True if heap is nearly "full" where "full" is defined by the
44 ** sqlite3_soft_heap_limit() setting.
49 #define mem0 GLOBAL(struct Mem0Global, mem0)
52 ** Return the memory allocator mutex. sqlite3_status() needs it.
54 sqlite3_mutex
*sqlite3MallocMutex(void){
58 #ifndef SQLITE_OMIT_DEPRECATED
60 ** Deprecated external interface. It used to set an alarm callback
61 ** that was invoked when memory usage grew too large. Now it is a
64 int sqlite3_memory_alarm(
65 void(*xCallback
)(void *pArg
, sqlite3_int64 used
,int N
),
67 sqlite3_int64 iThreshold
77 ** Set the soft heap-size limit for the library. Passing a zero or
78 ** negative value indicates no limit.
80 sqlite3_int64
sqlite3_soft_heap_limit64(sqlite3_int64 n
){
81 sqlite3_int64 priorLimit
;
84 #ifndef SQLITE_OMIT_AUTOINIT
85 int rc
= sqlite3_initialize();
88 sqlite3_mutex_enter(mem0
.mutex
);
89 priorLimit
= mem0
.alarmThreshold
;
91 sqlite3_mutex_leave(mem0
.mutex
);
94 mem0
.alarmThreshold
= n
;
95 nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
96 mem0
.nearlyFull
= (n
>0 && n
<=nUsed
);
97 sqlite3_mutex_leave(mem0
.mutex
);
98 excess
= sqlite3_memory_used() - n
;
99 if( excess
>0 ) sqlite3_release_memory((int)(excess
& 0x7fffffff));
102 void sqlite3_soft_heap_limit(int n
){
104 sqlite3_soft_heap_limit64(n
);
108 ** Initialize the memory allocation subsystem.
110 int sqlite3MallocInit(void){
112 if( sqlite3GlobalConfig
.m
.xMalloc
==0 ){
113 sqlite3MemSetDefault();
115 memset(&mem0
, 0, sizeof(mem0
));
116 mem0
.mutex
= sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM
);
117 if( sqlite3GlobalConfig
.pPage
==0 || sqlite3GlobalConfig
.szPage
<512
118 || sqlite3GlobalConfig
.nPage
<=0 ){
119 sqlite3GlobalConfig
.pPage
= 0;
120 sqlite3GlobalConfig
.szPage
= 0;
122 rc
= sqlite3GlobalConfig
.m
.xInit(sqlite3GlobalConfig
.m
.pAppData
);
123 if( rc
!=SQLITE_OK
) memset(&mem0
, 0, sizeof(mem0
));
124 /* BEGIN SQLCIPHER */
125 #ifdef SQLITE_HAS_CODEC
126 /* install wrapping functions for memory management
127 that will wipe all memory allocated by SQLite
129 if( rc
==SQLITE_OK
) {
130 extern void sqlcipher_init_memmethods(void);
131 sqlcipher_init_memmethods();
139 ** Return true if the heap is currently under memory pressure - in other
140 ** words if the amount of heap used is close to the limit set by
141 ** sqlite3_soft_heap_limit().
143 int sqlite3HeapNearlyFull(void){
144 return mem0
.nearlyFull
;
148 ** Deinitialize the memory allocation subsystem.
150 void sqlite3MallocEnd(void){
151 if( sqlite3GlobalConfig
.m
.xShutdown
){
152 sqlite3GlobalConfig
.m
.xShutdown(sqlite3GlobalConfig
.m
.pAppData
);
154 memset(&mem0
, 0, sizeof(mem0
));
158 ** Return the amount of memory currently checked out.
160 sqlite3_int64
sqlite3_memory_used(void){
161 sqlite3_int64 res
, mx
;
162 sqlite3_status64(SQLITE_STATUS_MEMORY_USED
, &res
, &mx
, 0);
167 ** Return the maximum amount of memory that has ever been
168 ** checked out since either the beginning of this process
169 ** or since the most recent reset.
171 sqlite3_int64
sqlite3_memory_highwater(int resetFlag
){
172 sqlite3_int64 res
, mx
;
173 sqlite3_status64(SQLITE_STATUS_MEMORY_USED
, &res
, &mx
, resetFlag
);
180 static void sqlite3MallocAlarm(int nByte
){
181 if( mem0
.alarmThreshold
<=0 ) return;
182 sqlite3_mutex_leave(mem0
.mutex
);
183 sqlite3_release_memory(nByte
);
184 sqlite3_mutex_enter(mem0
.mutex
);
188 ** Do a memory allocation with statistics and alarms. Assume the
189 ** lock is already held.
191 static void mallocWithAlarm(int n
, void **pp
){
194 assert( sqlite3_mutex_held(mem0
.mutex
) );
197 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
198 ** implementation of malloc_good_size(), which must be called in debug
199 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
200 ** or else a crash results. Hence, do not attempt to optimize out the
201 ** following xRoundup() call. */
202 nFull
= sqlite3GlobalConfig
.m
.xRoundup(n
);
204 #ifdef SQLITE_MAX_MEMORY
205 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
)+nFull
>SQLITE_MAX_MEMORY
){
211 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE
, n
);
212 if( mem0
.alarmThreshold
>0 ){
213 sqlite3_int64 nUsed
= sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
);
214 if( nUsed
>= mem0
.alarmThreshold
- nFull
){
216 sqlite3MallocAlarm(nFull
);
221 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
222 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
223 if( p
==0 && mem0
.alarmThreshold
>0 ){
224 sqlite3MallocAlarm(nFull
);
225 p
= sqlite3GlobalConfig
.m
.xMalloc(nFull
);
229 nFull
= sqlite3MallocSize(p
);
230 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED
, nFull
);
231 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT
, 1);
237 ** Allocate memory. This routine is like sqlite3_malloc() except that it
238 ** assumes the memory subsystem has already been initialized.
240 void *sqlite3Malloc(u64 n
){
242 if( n
==0 || n
>=0x7fffff00 ){
243 /* A memory allocation of a number of bytes which is near the maximum
244 ** signed integer value might cause an integer overflow inside of the
245 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
246 ** 255 bytes of overhead. SQLite itself will never use anything near
247 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
249 }else if( sqlite3GlobalConfig
.bMemstat
){
250 sqlite3_mutex_enter(mem0
.mutex
);
251 mallocWithAlarm((int)n
, &p
);
252 sqlite3_mutex_leave(mem0
.mutex
);
254 p
= sqlite3GlobalConfig
.m
.xMalloc((int)n
);
256 assert( EIGHT_BYTE_ALIGNMENT(p
) ); /* IMP: R-11148-40995 */
261 ** This version of the memory allocation is for use by the application.
262 ** First make sure the memory subsystem is initialized, then do the
265 void *sqlite3_malloc(int n
){
266 #ifndef SQLITE_OMIT_AUTOINIT
267 if( sqlite3_initialize() ) return 0;
269 return n
<=0 ? 0 : sqlite3Malloc(n
);
271 void *sqlite3_malloc64(sqlite3_uint64 n
){
272 #ifndef SQLITE_OMIT_AUTOINIT
273 if( sqlite3_initialize() ) return 0;
275 return sqlite3Malloc(n
);
279 ** TRUE if p is a lookaside memory allocation from db
281 #ifndef SQLITE_OMIT_LOOKASIDE
282 static int isLookaside(sqlite3
*db
, void *p
){
283 return SQLITE_WITHIN(p
, db
->lookaside
.pStart
, db
->lookaside
.pEnd
);
286 #define isLookaside(A,B) 0
290 ** Return the size of a memory allocation previously obtained from
291 ** sqlite3Malloc() or sqlite3_malloc().
293 int sqlite3MallocSize(void *p
){
294 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
295 return sqlite3GlobalConfig
.m
.xSize(p
);
297 int sqlite3DbMallocSize(sqlite3
*db
, void *p
){
299 if( db
==0 || !isLookaside(db
,p
) ){
302 assert( sqlite3MemdebugNoType(p
, (u8
)~MEMTYPE_HEAP
) );
303 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
305 assert( sqlite3MemdebugHasType(p
, (MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
306 assert( sqlite3MemdebugNoType(p
, (u8
)~(MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
309 return sqlite3GlobalConfig
.m
.xSize(p
);
311 assert( sqlite3_mutex_held(db
->mutex
) );
312 return db
->lookaside
.sz
;
315 sqlite3_uint64
sqlite3_msize(void *p
){
316 assert( sqlite3MemdebugNoType(p
, (u8
)~MEMTYPE_HEAP
) );
317 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
318 return p
? sqlite3GlobalConfig
.m
.xSize(p
) : 0;
322 ** Free memory previously obtained from sqlite3Malloc().
324 void sqlite3_free(void *p
){
325 if( p
==0 ) return; /* IMP: R-49053-54554 */
326 assert( sqlite3MemdebugHasType(p
, MEMTYPE_HEAP
) );
327 assert( sqlite3MemdebugNoType(p
, (u8
)~MEMTYPE_HEAP
) );
328 if( sqlite3GlobalConfig
.bMemstat
){
329 sqlite3_mutex_enter(mem0
.mutex
);
330 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED
, sqlite3MallocSize(p
));
331 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT
, 1);
332 sqlite3GlobalConfig
.m
.xFree(p
);
333 sqlite3_mutex_leave(mem0
.mutex
);
335 sqlite3GlobalConfig
.m
.xFree(p
);
340 ** Add the size of memory allocation "p" to the count in
341 ** *db->pnBytesFreed.
343 static SQLITE_NOINLINE
void measureAllocationSize(sqlite3
*db
, void *p
){
344 *db
->pnBytesFreed
+= sqlite3DbMallocSize(db
,p
);
348 ** Free memory that might be associated with a particular database
349 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
350 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
352 void sqlite3DbFreeNN(sqlite3
*db
, void *p
){
353 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
356 if( db
->pnBytesFreed
){
357 measureAllocationSize(db
, p
);
360 if( isLookaside(db
, p
) ){
361 LookasideSlot
*pBuf
= (LookasideSlot
*)p
;
363 /* Trash all content in the buffer being freed */
364 memset(p
, 0xaa, db
->lookaside
.sz
);
366 pBuf
->pNext
= db
->lookaside
.pFree
;
367 db
->lookaside
.pFree
= pBuf
;
371 assert( sqlite3MemdebugHasType(p
, (MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
372 assert( sqlite3MemdebugNoType(p
, (u8
)~(MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
373 assert( db
!=0 || sqlite3MemdebugNoType(p
, MEMTYPE_LOOKASIDE
) );
374 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
377 void sqlite3DbFree(sqlite3
*db
, void *p
){
378 assert( db
==0 || sqlite3_mutex_held(db
->mutex
) );
379 if( p
) sqlite3DbFreeNN(db
, p
);
383 ** Change the size of an existing memory allocation
385 void *sqlite3Realloc(void *pOld
, u64 nBytes
){
386 int nOld
, nNew
, nDiff
;
388 assert( sqlite3MemdebugHasType(pOld
, MEMTYPE_HEAP
) );
389 assert( sqlite3MemdebugNoType(pOld
, (u8
)~MEMTYPE_HEAP
) );
391 return sqlite3Malloc(nBytes
); /* IMP: R-04300-56712 */
394 sqlite3_free(pOld
); /* IMP: R-26507-47431 */
397 if( nBytes
>=0x7fffff00 ){
398 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
401 nOld
= sqlite3MallocSize(pOld
);
402 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
403 ** argument to xRealloc is always a value returned by a prior call to
405 nNew
= sqlite3GlobalConfig
.m
.xRoundup((int)nBytes
);
408 }else if( sqlite3GlobalConfig
.bMemstat
){
409 sqlite3_mutex_enter(mem0
.mutex
);
410 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE
, (int)nBytes
);
412 if( nDiff
>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED
) >=
413 mem0
.alarmThreshold
-nDiff
){
414 sqlite3MallocAlarm(nDiff
);
416 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
417 if( pNew
==0 && mem0
.alarmThreshold
>0 ){
418 sqlite3MallocAlarm((int)nBytes
);
419 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
422 nNew
= sqlite3MallocSize(pNew
);
423 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED
, nNew
-nOld
);
425 sqlite3_mutex_leave(mem0
.mutex
);
427 pNew
= sqlite3GlobalConfig
.m
.xRealloc(pOld
, nNew
);
429 assert( EIGHT_BYTE_ALIGNMENT(pNew
) ); /* IMP: R-11148-40995 */
434 ** The public interface to sqlite3Realloc. Make sure that the memory
435 ** subsystem is initialized prior to invoking sqliteRealloc.
437 void *sqlite3_realloc(void *pOld
, int n
){
438 #ifndef SQLITE_OMIT_AUTOINIT
439 if( sqlite3_initialize() ) return 0;
441 if( n
<0 ) n
= 0; /* IMP: R-26507-47431 */
442 return sqlite3Realloc(pOld
, n
);
444 void *sqlite3_realloc64(void *pOld
, sqlite3_uint64 n
){
445 #ifndef SQLITE_OMIT_AUTOINIT
446 if( sqlite3_initialize() ) return 0;
448 return sqlite3Realloc(pOld
, n
);
453 ** Allocate and zero memory.
455 void *sqlite3MallocZero(u64 n
){
456 void *p
= sqlite3Malloc(n
);
458 memset(p
, 0, (size_t)n
);
464 ** Allocate and zero memory. If the allocation fails, make
465 ** the mallocFailed flag in the connection pointer.
467 void *sqlite3DbMallocZero(sqlite3
*db
, u64 n
){
470 p
= sqlite3DbMallocRaw(db
, n
);
471 if( p
) memset(p
, 0, (size_t)n
);
476 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
477 ** slower case when the allocation cannot be fulfilled using lookaside.
479 static SQLITE_NOINLINE
void *dbMallocRawFinish(sqlite3
*db
, u64 n
){
482 p
= sqlite3Malloc(n
);
483 if( !p
) sqlite3OomFault(db
);
484 sqlite3MemdebugSetType(p
,
485 (db
->lookaside
.bDisable
==0) ? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
);
490 ** Allocate memory, either lookaside (if possible) or heap.
491 ** If the allocation fails, set the mallocFailed flag in
492 ** the connection pointer.
494 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
495 ** failure on the same database connection) then always return 0.
496 ** Hence for a particular database connection, once malloc starts
497 ** failing, it fails consistently until mallocFailed is reset.
498 ** This is an important assumption. There are many places in the
499 ** code that do things like this:
501 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
502 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
503 ** if( b ) a[10] = 9;
505 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
506 ** that all prior mallocs (ex: "a") worked too.
508 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
509 ** not a NULL pointer.
511 void *sqlite3DbMallocRaw(sqlite3
*db
, u64 n
){
513 if( db
) return sqlite3DbMallocRawNN(db
, n
);
514 p
= sqlite3Malloc(n
);
515 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
518 void *sqlite3DbMallocRawNN(sqlite3
*db
, u64 n
){
519 #ifndef SQLITE_OMIT_LOOKASIDE
522 assert( sqlite3_mutex_held(db
->mutex
) );
523 assert( db
->pnBytesFreed
==0 );
524 if( db
->lookaside
.bDisable
==0 ){
525 assert( db
->mallocFailed
==0 );
526 if( n
>db
->lookaside
.sz
){
527 db
->lookaside
.anStat
[1]++;
528 }else if( (pBuf
= db
->lookaside
.pFree
)!=0 ){
529 db
->lookaside
.pFree
= pBuf
->pNext
;
530 db
->lookaside
.anStat
[0]++;
532 }else if( (pBuf
= db
->lookaside
.pInit
)!=0 ){
533 db
->lookaside
.pInit
= pBuf
->pNext
;
534 db
->lookaside
.anStat
[0]++;
537 db
->lookaside
.anStat
[2]++;
539 }else if( db
->mallocFailed
){
544 assert( sqlite3_mutex_held(db
->mutex
) );
545 assert( db
->pnBytesFreed
==0 );
546 if( db
->mallocFailed
){
550 return dbMallocRawFinish(db
, n
);
553 /* Forward declaration */
554 static SQLITE_NOINLINE
void *dbReallocFinish(sqlite3
*db
, void *p
, u64 n
);
557 ** Resize the block of memory pointed to by p to n bytes. If the
558 ** resize fails, set the mallocFailed flag in the connection object.
560 void *sqlite3DbRealloc(sqlite3
*db
, void *p
, u64 n
){
562 if( p
==0 ) return sqlite3DbMallocRawNN(db
, n
);
563 assert( sqlite3_mutex_held(db
->mutex
) );
564 if( isLookaside(db
,p
) && n
<=db
->lookaside
.sz
) return p
;
565 return dbReallocFinish(db
, p
, n
);
567 static SQLITE_NOINLINE
void *dbReallocFinish(sqlite3
*db
, void *p
, u64 n
){
571 if( db
->mallocFailed
==0 ){
572 if( isLookaside(db
, p
) ){
573 pNew
= sqlite3DbMallocRawNN(db
, n
);
575 memcpy(pNew
, p
, db
->lookaside
.sz
);
576 sqlite3DbFree(db
, p
);
579 assert( sqlite3MemdebugHasType(p
, (MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
580 assert( sqlite3MemdebugNoType(p
, (u8
)~(MEMTYPE_LOOKASIDE
|MEMTYPE_HEAP
)) );
581 sqlite3MemdebugSetType(p
, MEMTYPE_HEAP
);
582 pNew
= sqlite3_realloc64(p
, n
);
586 sqlite3MemdebugSetType(pNew
,
587 (db
->lookaside
.bDisable
==0 ? MEMTYPE_LOOKASIDE
: MEMTYPE_HEAP
));
594 ** Attempt to reallocate p. If the reallocation fails, then free p
595 ** and set the mallocFailed flag in the database connection.
597 void *sqlite3DbReallocOrFree(sqlite3
*db
, void *p
, u64 n
){
599 pNew
= sqlite3DbRealloc(db
, p
, n
);
601 sqlite3DbFree(db
, p
);
607 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
608 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
609 ** is because when memory debugging is turned on, these two functions are
610 ** called via macros that record the current file and line number in the
611 ** ThreadData structure.
613 char *sqlite3DbStrDup(sqlite3
*db
, const char *z
){
620 zNew
= sqlite3DbMallocRaw(db
, n
);
626 char *sqlite3DbStrNDup(sqlite3
*db
, const char *z
, u64 n
){
632 assert( (n
&0x7fffffff)==n
);
633 zNew
= sqlite3DbMallocRawNN(db
, n
+1);
635 memcpy(zNew
, z
, (size_t)n
);
642 ** The text between zStart and zEnd represents a phrase within a larger
643 ** SQL statement. Make a copy of this phrase in space obtained form
644 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
646 char *sqlite3DbSpanDup(sqlite3
*db
, const char *zStart
, const char *zEnd
){
648 while( sqlite3Isspace(zStart
[0]) ) zStart
++;
649 n
= (int)(zEnd
- zStart
);
650 while( ALWAYS(n
>0) && sqlite3Isspace(zStart
[n
-1]) ) n
--;
651 return sqlite3DbStrNDup(db
, zStart
, n
);
655 ** Free any prior content in *pz and replace it with a copy of zNew.
657 void sqlite3SetString(char **pz
, sqlite3
*db
, const char *zNew
){
658 sqlite3DbFree(db
, *pz
);
659 *pz
= sqlite3DbStrDup(db
, zNew
);
663 ** Call this routine to record the fact that an OOM (out-of-memory) error
664 ** has happened. This routine will set db->mallocFailed, and also
665 ** temporarily disable the lookaside memory allocator and interrupt
666 ** any running VDBEs.
668 void sqlite3OomFault(sqlite3
*db
){
669 if( db
->mallocFailed
==0 && db
->bBenignMalloc
==0 ){
670 db
->mallocFailed
= 1;
671 if( db
->nVdbeExec
>0 ){
672 db
->u1
.isInterrupted
= 1;
674 db
->lookaside
.bDisable
++;
679 ** This routine reactivates the memory allocator and clears the
680 ** db->mallocFailed flag as necessary.
682 ** The memory allocator is not restarted if there are running
685 void sqlite3OomClear(sqlite3
*db
){
686 if( db
->mallocFailed
&& db
->nVdbeExec
==0 ){
687 db
->mallocFailed
= 0;
688 db
->u1
.isInterrupted
= 0;
689 assert( db
->lookaside
.bDisable
>0 );
690 db
->lookaside
.bDisable
--;
695 ** Take actions at the end of an API call to indicate an OOM error
697 static SQLITE_NOINLINE
int apiOomError(sqlite3
*db
){
699 sqlite3Error(db
, SQLITE_NOMEM
);
700 return SQLITE_NOMEM_BKPT
;
704 ** This function must be called before exiting any API function (i.e.
705 ** returning control to the user) that has called sqlite3_malloc or
708 ** The returned value is normally a copy of the second argument to this
709 ** function. However, if a malloc() failure has occurred since the previous
710 ** invocation SQLITE_NOMEM is returned instead.
712 ** If an OOM as occurred, then the connection error-code (the value
713 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
715 int sqlite3ApiExit(sqlite3
* db
, int rc
){
716 /* If the db handle must hold the connection handle mutex here.
717 ** Otherwise the read (and possible write) of db->mallocFailed
718 ** is unsafe, as is the call to sqlite3Error().
721 assert( sqlite3_mutex_held(db
->mutex
) );
722 if( db
->mallocFailed
|| rc
==SQLITE_IOERR_NOMEM
){
723 return apiOomError(db
);
725 return rc
& db
->errMask
;