fix prototypes (for strict-prototypes)
[sqlcipher.git] / src / malloc.c
blobabaf1e9f60b86e64c36ef4b693f387dd78b3fe13
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
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"
16 #include <stdarg.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);
26 #else
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. */
30 UNUSED_PARAMETER(n);
31 return 0;
32 #endif
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.
46 int nearlyFull;
47 } mem0 = { 0, 0, 0 };
49 #define mem0 GLOBAL(struct Mem0Global, mem0)
52 ** Return the memory allocator mutex. sqlite3_status() needs it.
54 sqlite3_mutex *sqlite3MallocMutex(void){
55 return mem0.mutex;
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
62 ** no-op.
64 int sqlite3_memory_alarm(
65 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
66 void *pArg,
67 sqlite3_int64 iThreshold
69 (void)xCallback;
70 (void)pArg;
71 (void)iThreshold;
72 return SQLITE_OK;
74 #endif
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;
82 sqlite3_int64 excess;
83 sqlite3_int64 nUsed;
84 #ifndef SQLITE_OMIT_AUTOINIT
85 int rc = sqlite3_initialize();
86 if( rc ) return -1;
87 #endif
88 sqlite3_mutex_enter(mem0.mutex);
89 priorLimit = mem0.alarmThreshold;
90 if( n<0 ){
91 sqlite3_mutex_leave(mem0.mutex);
92 return priorLimit;
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));
100 return priorLimit;
102 void sqlite3_soft_heap_limit(int n){
103 if( n<0 ) n = 0;
104 sqlite3_soft_heap_limit64(n);
108 ** Initialize the memory allocation subsystem.
110 int sqlite3MallocInit(void){
111 int rc;
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
128 when freed */
129 if( rc==SQLITE_OK ) {
130 extern void sqlcipher_init_memmethods(void);
131 sqlcipher_init_memmethods();
133 #endif
134 /* END SQLCIPHER */
135 return rc;
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);
163 return res;
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);
174 return mx;
178 ** Trigger the alarm
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){
192 void *p;
193 int nFull;
194 assert( sqlite3_mutex_held(mem0.mutex) );
195 assert( n>0 );
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 ){
206 *pp = 0;
207 return;
209 #endif
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 ){
215 mem0.nearlyFull = 1;
216 sqlite3MallocAlarm(nFull);
217 }else{
218 mem0.nearlyFull = 0;
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);
227 #endif
228 if( p ){
229 nFull = sqlite3MallocSize(p);
230 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
231 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
233 *pp = p;
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){
241 void *p;
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() */
248 p = 0;
249 }else if( sqlite3GlobalConfig.bMemstat ){
250 sqlite3_mutex_enter(mem0.mutex);
251 mallocWithAlarm((int)n, &p);
252 sqlite3_mutex_leave(mem0.mutex);
253 }else{
254 p = sqlite3GlobalConfig.m.xMalloc((int)n);
256 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
257 return p;
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
263 ** allocation.
265 void *sqlite3_malloc(int n){
266 #ifndef SQLITE_OMIT_AUTOINIT
267 if( sqlite3_initialize() ) return 0;
268 #endif
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;
274 #endif
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);
285 #else
286 #define isLookaside(A,B) 0
287 #endif
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){
298 assert( p!=0 );
299 if( db==0 || !isLookaside(db,p) ){
300 #ifdef SQLITE_DEBUG
301 if( db==0 ){
302 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
303 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
304 }else{
305 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
306 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
308 #endif
309 return sqlite3GlobalConfig.m.xSize(p);
310 }else{
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);
334 }else{
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) );
354 assert( p!=0 );
355 if( db ){
356 if( db->pnBytesFreed ){
357 measureAllocationSize(db, p);
358 return;
360 if( isLookaside(db, p) ){
361 LookasideSlot *pBuf = (LookasideSlot*)p;
362 #ifdef SQLITE_DEBUG
363 /* Trash all content in the buffer being freed */
364 memset(p, 0xaa, db->lookaside.sz);
365 #endif
366 pBuf->pNext = db->lookaside.pFree;
367 db->lookaside.pFree = pBuf;
368 return;
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);
375 sqlite3_free(p);
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;
387 void *pNew;
388 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
389 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
390 if( pOld==0 ){
391 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
393 if( nBytes==0 ){
394 sqlite3_free(pOld); /* IMP: R-26507-47431 */
395 return 0;
397 if( nBytes>=0x7fffff00 ){
398 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
399 return 0;
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
404 ** xRoundup. */
405 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
406 if( nOld==nNew ){
407 pNew = pOld;
408 }else if( sqlite3GlobalConfig.bMemstat ){
409 sqlite3_mutex_enter(mem0.mutex);
410 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
411 nDiff = nNew - nOld;
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);
421 if( pNew ){
422 nNew = sqlite3MallocSize(pNew);
423 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
425 sqlite3_mutex_leave(mem0.mutex);
426 }else{
427 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
429 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
430 return pNew;
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;
440 #endif
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;
447 #endif
448 return sqlite3Realloc(pOld, n);
453 ** Allocate and zero memory.
455 void *sqlite3MallocZero(u64 n){
456 void *p = sqlite3Malloc(n);
457 if( p ){
458 memset(p, 0, (size_t)n);
460 return p;
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){
468 void *p;
469 testcase( db==0 );
470 p = sqlite3DbMallocRaw(db, n);
471 if( p ) memset(p, 0, (size_t)n);
472 return p;
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){
480 void *p;
481 assert( db!=0 );
482 p = sqlite3Malloc(n);
483 if( !p ) sqlite3OomFault(db);
484 sqlite3MemdebugSetType(p,
485 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
486 return p;
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){
512 void *p;
513 if( db ) return sqlite3DbMallocRawNN(db, n);
514 p = sqlite3Malloc(n);
515 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
516 return p;
518 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
519 #ifndef SQLITE_OMIT_LOOKASIDE
520 LookasideSlot *pBuf;
521 assert( db!=0 );
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]++;
531 return (void*)pBuf;
532 }else if( (pBuf = db->lookaside.pInit)!=0 ){
533 db->lookaside.pInit = pBuf->pNext;
534 db->lookaside.anStat[0]++;
535 return (void*)pBuf;
536 }else{
537 db->lookaside.anStat[2]++;
539 }else if( db->mallocFailed ){
540 return 0;
542 #else
543 assert( db!=0 );
544 assert( sqlite3_mutex_held(db->mutex) );
545 assert( db->pnBytesFreed==0 );
546 if( db->mallocFailed ){
547 return 0;
549 #endif
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){
561 assert( db!=0 );
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){
568 void *pNew = 0;
569 assert( db!=0 );
570 assert( p!=0 );
571 if( db->mallocFailed==0 ){
572 if( isLookaside(db, p) ){
573 pNew = sqlite3DbMallocRawNN(db, n);
574 if( pNew ){
575 memcpy(pNew, p, db->lookaside.sz);
576 sqlite3DbFree(db, p);
578 }else{
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);
583 if( !pNew ){
584 sqlite3OomFault(db);
586 sqlite3MemdebugSetType(pNew,
587 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
590 return pNew;
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){
598 void *pNew;
599 pNew = sqlite3DbRealloc(db, p, n);
600 if( !pNew ){
601 sqlite3DbFree(db, p);
603 return pNew;
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){
614 char *zNew;
615 size_t n;
616 if( z==0 ){
617 return 0;
619 n = strlen(z) + 1;
620 zNew = sqlite3DbMallocRaw(db, n);
621 if( zNew ){
622 memcpy(zNew, z, n);
624 return zNew;
626 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
627 char *zNew;
628 assert( db!=0 );
629 if( z==0 ){
630 return 0;
632 assert( (n&0x7fffffff)==n );
633 zNew = sqlite3DbMallocRawNN(db, n+1);
634 if( zNew ){
635 memcpy(zNew, z, (size_t)n);
636 zNew[n] = 0;
638 return zNew;
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){
647 int n;
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
683 ** VDBEs.
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){
698 sqlite3OomClear(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
706 ** sqlite3_realloc.
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().
720 assert( db!=0 );
721 assert( sqlite3_mutex_held(db->mutex) );
722 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
723 return apiOomError(db);
725 return rc & db->errMask;