rework kdf salt flags
[sqlcipher.git] / src / malloc.c
blob16e3cd3de3ffb32e0782d5655a6ee960a2159b99
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 ** Default value of the hard heap limit. 0 means "no limit".
38 #ifndef SQLITE_MAX_MEMORY
39 # define SQLITE_MAX_MEMORY 0
40 #endif
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.
54 int nearlyFull;
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){
63 return mem0.mutex;
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
70 ** no-op.
72 int sqlite3_memory_alarm(
73 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
74 void *pArg,
75 sqlite3_int64 iThreshold
77 (void)xCallback;
78 (void)pArg;
79 (void)iThreshold;
80 return SQLITE_OK;
82 #endif
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;
97 sqlite3_int64 excess;
98 sqlite3_int64 nUsed;
99 #ifndef SQLITE_OMIT_AUTOINIT
100 int rc = sqlite3_initialize();
101 if( rc ) return -1;
102 #endif
103 sqlite3_mutex_enter(mem0.mutex);
104 priorLimit = mem0.alarmThreshold;
105 if( n<0 ){
106 sqlite3_mutex_leave(mem0.mutex);
107 return priorLimit;
109 if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
110 n = mem0.hardLimit;
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));
118 return priorLimit;
120 void sqlite3_soft_heap_limit(int n){
121 if( n<0 ) n = 0;
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
135 ** limit.
137 sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
138 sqlite3_int64 priorLimit;
139 #ifndef SQLITE_OMIT_AUTOINIT
140 int rc = sqlite3_initialize();
141 if( rc ) return -1;
142 #endif
143 sqlite3_mutex_enter(mem0.mutex);
144 priorLimit = mem0.hardLimit;
145 if( n>=0 ){
146 mem0.hardLimit = n;
147 if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
148 mem0.alarmThreshold = n;
151 sqlite3_mutex_leave(mem0.mutex);
152 return priorLimit;
157 ** Initialize the memory allocation subsystem.
159 int sqlite3MallocInit(void){
160 int rc;
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));
172 /* BEGIN SQLCIPHER */
173 #ifdef SQLITE_HAS_CODEC
174 /* install wrapping functions for memory management
175 that will wipe all memory allocated by SQLite
176 when freed */
177 if( rc==SQLITE_OK ) {
178 extern void sqlcipher_init_memmethods(void);
179 sqlcipher_init_memmethods();
181 #endif
182 /* END SQLCIPHER */
183 return rc;
187 ** Return true if the heap is currently under memory pressure - in other
188 ** words if the amount of heap used is close to the limit set by
189 ** sqlite3_soft_heap_limit().
191 int sqlite3HeapNearlyFull(void){
192 return AtomicLoad(&mem0.nearlyFull);
196 ** Deinitialize the memory allocation subsystem.
198 void sqlite3MallocEnd(void){
199 if( sqlite3GlobalConfig.m.xShutdown ){
200 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
202 memset(&mem0, 0, sizeof(mem0));
206 ** Return the amount of memory currently checked out.
208 sqlite3_int64 sqlite3_memory_used(void){
209 sqlite3_int64 res, mx;
210 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
211 return res;
215 ** Return the maximum amount of memory that has ever been
216 ** checked out since either the beginning of this process
217 ** or since the most recent reset.
219 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
220 sqlite3_int64 res, mx;
221 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
222 return mx;
226 ** Trigger the alarm
228 static void sqlite3MallocAlarm(int nByte){
229 if( mem0.alarmThreshold<=0 ) return;
230 sqlite3_mutex_leave(mem0.mutex);
231 sqlite3_release_memory(nByte);
232 sqlite3_mutex_enter(mem0.mutex);
236 ** Do a memory allocation with statistics and alarms. Assume the
237 ** lock is already held.
239 static void mallocWithAlarm(int n, void **pp){
240 void *p;
241 int nFull;
242 assert( sqlite3_mutex_held(mem0.mutex) );
243 assert( n>0 );
245 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
246 ** implementation of malloc_good_size(), which must be called in debug
247 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
248 ** or else a crash results. Hence, do not attempt to optimize out the
249 ** following xRoundup() call. */
250 nFull = sqlite3GlobalConfig.m.xRoundup(n);
252 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
253 if( mem0.alarmThreshold>0 ){
254 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
255 if( nUsed >= mem0.alarmThreshold - nFull ){
256 AtomicStore(&mem0.nearlyFull, 1);
257 sqlite3MallocAlarm(nFull);
258 if( mem0.hardLimit ){
259 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
260 if( nUsed >= mem0.hardLimit - nFull ){
261 *pp = 0;
262 return;
265 }else{
266 AtomicStore(&mem0.nearlyFull, 0);
269 p = sqlite3GlobalConfig.m.xMalloc(nFull);
270 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
271 if( p==0 && mem0.alarmThreshold>0 ){
272 sqlite3MallocAlarm(nFull);
273 p = sqlite3GlobalConfig.m.xMalloc(nFull);
275 #endif
276 if( p ){
277 nFull = sqlite3MallocSize(p);
278 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
279 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
281 *pp = p;
285 ** Maximum size of any single memory allocation.
287 ** This is not a limit on the total amount of memory used. This is
288 ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
290 ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
291 ** This provides a 256-byte safety margin for defense against 32-bit
292 ** signed integer overflow bugs when computing memory allocation sizes.
293 ** Paranoid applications might want to reduce the maximum allocation size
294 ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
295 ** or even smaller would be reasonable upper bounds on the size of a memory
296 ** allocations for most applications.
298 #ifndef SQLITE_MAX_ALLOCATION_SIZE
299 # define SQLITE_MAX_ALLOCATION_SIZE 2147483391
300 #endif
301 #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
302 # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
303 #endif
306 ** Allocate memory. This routine is like sqlite3_malloc() except that it
307 ** assumes the memory subsystem has already been initialized.
309 void *sqlite3Malloc(u64 n){
310 void *p;
311 if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){
312 p = 0;
313 }else if( sqlite3GlobalConfig.bMemstat ){
314 sqlite3_mutex_enter(mem0.mutex);
315 mallocWithAlarm((int)n, &p);
316 sqlite3_mutex_leave(mem0.mutex);
317 }else{
318 p = sqlite3GlobalConfig.m.xMalloc((int)n);
320 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
321 return p;
325 ** This version of the memory allocation is for use by the application.
326 ** First make sure the memory subsystem is initialized, then do the
327 ** allocation.
329 void *sqlite3_malloc(int n){
330 #ifndef SQLITE_OMIT_AUTOINIT
331 if( sqlite3_initialize() ) return 0;
332 #endif
333 return n<=0 ? 0 : sqlite3Malloc(n);
335 void *sqlite3_malloc64(sqlite3_uint64 n){
336 #ifndef SQLITE_OMIT_AUTOINIT
337 if( sqlite3_initialize() ) return 0;
338 #endif
339 return sqlite3Malloc(n);
343 ** TRUE if p is a lookaside memory allocation from db
345 #ifndef SQLITE_OMIT_LOOKASIDE
346 static int isLookaside(sqlite3 *db, const void *p){
347 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
349 #else
350 #define isLookaside(A,B) 0
351 #endif
354 ** Return the size of a memory allocation previously obtained from
355 ** sqlite3Malloc() or sqlite3_malloc().
357 int sqlite3MallocSize(const void *p){
358 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
359 return sqlite3GlobalConfig.m.xSize((void*)p);
361 static int lookasideMallocSize(sqlite3 *db, const void *p){
362 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
363 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
364 #else
365 return db->lookaside.szTrue;
366 #endif
368 int sqlite3DbMallocSize(sqlite3 *db, const void *p){
369 assert( p!=0 );
370 #ifdef SQLITE_DEBUG
371 if( db==0 ){
372 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
373 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
374 }else if( !isLookaside(db,p) ){
375 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
376 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
378 #endif
379 if( db ){
380 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
381 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
382 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
383 assert( sqlite3_mutex_held(db->mutex) );
384 return LOOKASIDE_SMALL;
386 #endif
387 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
388 assert( sqlite3_mutex_held(db->mutex) );
389 return db->lookaside.szTrue;
393 return sqlite3GlobalConfig.m.xSize((void*)p);
395 sqlite3_uint64 sqlite3_msize(void *p){
396 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
397 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
398 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
402 ** Free memory previously obtained from sqlite3Malloc().
404 void sqlite3_free(void *p){
405 if( p==0 ) return; /* IMP: R-49053-54554 */
406 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
407 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
408 if( sqlite3GlobalConfig.bMemstat ){
409 sqlite3_mutex_enter(mem0.mutex);
410 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
411 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
412 sqlite3GlobalConfig.m.xFree(p);
413 sqlite3_mutex_leave(mem0.mutex);
414 }else{
415 sqlite3GlobalConfig.m.xFree(p);
420 ** Add the size of memory allocation "p" to the count in
421 ** *db->pnBytesFreed.
423 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
424 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
428 ** Free memory that might be associated with a particular database
429 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
430 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
432 void sqlite3DbFreeNN(sqlite3 *db, void *p){
433 assert( db==0 || sqlite3_mutex_held(db->mutex) );
434 assert( p!=0 );
435 if( db ){
436 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
437 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
438 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
439 LookasideSlot *pBuf = (LookasideSlot*)p;
440 assert( db->pnBytesFreed==0 );
441 #ifdef SQLITE_DEBUG
442 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
443 #endif
444 pBuf->pNext = db->lookaside.pSmallFree;
445 db->lookaside.pSmallFree = pBuf;
446 return;
448 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
449 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
450 LookasideSlot *pBuf = (LookasideSlot*)p;
451 assert( db->pnBytesFreed==0 );
452 #ifdef SQLITE_DEBUG
453 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
454 #endif
455 pBuf->pNext = db->lookaside.pFree;
456 db->lookaside.pFree = pBuf;
457 return;
460 if( db->pnBytesFreed ){
461 measureAllocationSize(db, p);
462 return;
465 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
466 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
467 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
468 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
469 sqlite3_free(p);
471 void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
472 assert( db!=0 );
473 assert( sqlite3_mutex_held(db->mutex) );
474 assert( p!=0 );
475 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
476 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
477 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
478 LookasideSlot *pBuf = (LookasideSlot*)p;
479 assert( db->pnBytesFreed==0 );
480 #ifdef SQLITE_DEBUG
481 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
482 #endif
483 pBuf->pNext = db->lookaside.pSmallFree;
484 db->lookaside.pSmallFree = pBuf;
485 return;
487 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
488 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
489 LookasideSlot *pBuf = (LookasideSlot*)p;
490 assert( db->pnBytesFreed==0 );
491 #ifdef SQLITE_DEBUG
492 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
493 #endif
494 pBuf->pNext = db->lookaside.pFree;
495 db->lookaside.pFree = pBuf;
496 return;
499 if( db->pnBytesFreed ){
500 measureAllocationSize(db, p);
501 return;
503 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
504 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
505 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
506 sqlite3_free(p);
508 void sqlite3DbFree(sqlite3 *db, void *p){
509 assert( db==0 || sqlite3_mutex_held(db->mutex) );
510 if( p ) sqlite3DbFreeNN(db, p);
514 ** Change the size of an existing memory allocation
516 void *sqlite3Realloc(void *pOld, u64 nBytes){
517 int nOld, nNew, nDiff;
518 void *pNew;
519 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
520 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
521 if( pOld==0 ){
522 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
524 if( nBytes==0 ){
525 sqlite3_free(pOld); /* IMP: R-26507-47431 */
526 return 0;
528 if( nBytes>=0x7fffff00 ){
529 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
530 return 0;
532 nOld = sqlite3MallocSize(pOld);
533 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
534 ** argument to xRealloc is always a value returned by a prior call to
535 ** xRoundup. */
536 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
537 if( nOld==nNew ){
538 pNew = pOld;
539 }else if( sqlite3GlobalConfig.bMemstat ){
540 sqlite3_int64 nUsed;
541 sqlite3_mutex_enter(mem0.mutex);
542 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
543 nDiff = nNew - nOld;
544 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
545 mem0.alarmThreshold-nDiff ){
546 sqlite3MallocAlarm(nDiff);
547 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
548 sqlite3_mutex_leave(mem0.mutex);
549 return 0;
552 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
553 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
554 if( pNew==0 && mem0.alarmThreshold>0 ){
555 sqlite3MallocAlarm((int)nBytes);
556 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
558 #endif
559 if( pNew ){
560 nNew = sqlite3MallocSize(pNew);
561 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
563 sqlite3_mutex_leave(mem0.mutex);
564 }else{
565 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
567 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
568 return pNew;
572 ** The public interface to sqlite3Realloc. Make sure that the memory
573 ** subsystem is initialized prior to invoking sqliteRealloc.
575 void *sqlite3_realloc(void *pOld, int n){
576 #ifndef SQLITE_OMIT_AUTOINIT
577 if( sqlite3_initialize() ) return 0;
578 #endif
579 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
580 return sqlite3Realloc(pOld, n);
582 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
583 #ifndef SQLITE_OMIT_AUTOINIT
584 if( sqlite3_initialize() ) return 0;
585 #endif
586 return sqlite3Realloc(pOld, n);
591 ** Allocate and zero memory.
593 void *sqlite3MallocZero(u64 n){
594 void *p = sqlite3Malloc(n);
595 if( p ){
596 memset(p, 0, (size_t)n);
598 return p;
602 ** Allocate and zero memory. If the allocation fails, make
603 ** the mallocFailed flag in the connection pointer.
605 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
606 void *p;
607 testcase( db==0 );
608 p = sqlite3DbMallocRaw(db, n);
609 if( p ) memset(p, 0, (size_t)n);
610 return p;
614 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
615 ** slower case when the allocation cannot be fulfilled using lookaside.
617 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
618 void *p;
619 assert( db!=0 );
620 p = sqlite3Malloc(n);
621 if( !p ) sqlite3OomFault(db);
622 sqlite3MemdebugSetType(p,
623 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
624 return p;
628 ** Allocate memory, either lookaside (if possible) or heap.
629 ** If the allocation fails, set the mallocFailed flag in
630 ** the connection pointer.
632 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
633 ** failure on the same database connection) then always return 0.
634 ** Hence for a particular database connection, once malloc starts
635 ** failing, it fails consistently until mallocFailed is reset.
636 ** This is an important assumption. There are many places in the
637 ** code that do things like this:
639 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
640 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
641 ** if( b ) a[10] = 9;
643 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
644 ** that all prior mallocs (ex: "a") worked too.
646 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
647 ** not a NULL pointer.
649 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
650 void *p;
651 if( db ) return sqlite3DbMallocRawNN(db, n);
652 p = sqlite3Malloc(n);
653 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
654 return p;
656 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
657 #ifndef SQLITE_OMIT_LOOKASIDE
658 LookasideSlot *pBuf;
659 assert( db!=0 );
660 assert( sqlite3_mutex_held(db->mutex) );
661 assert( db->pnBytesFreed==0 );
662 if( n>db->lookaside.sz ){
663 if( !db->lookaside.bDisable ){
664 db->lookaside.anStat[1]++;
665 }else if( db->mallocFailed ){
666 return 0;
668 return dbMallocRawFinish(db, n);
670 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
671 if( n<=LOOKASIDE_SMALL ){
672 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
673 db->lookaside.pSmallFree = pBuf->pNext;
674 db->lookaside.anStat[0]++;
675 return (void*)pBuf;
676 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
677 db->lookaside.pSmallInit = pBuf->pNext;
678 db->lookaside.anStat[0]++;
679 return (void*)pBuf;
682 #endif
683 if( (pBuf = db->lookaside.pFree)!=0 ){
684 db->lookaside.pFree = pBuf->pNext;
685 db->lookaside.anStat[0]++;
686 return (void*)pBuf;
687 }else if( (pBuf = db->lookaside.pInit)!=0 ){
688 db->lookaside.pInit = pBuf->pNext;
689 db->lookaside.anStat[0]++;
690 return (void*)pBuf;
691 }else{
692 db->lookaside.anStat[2]++;
694 #else
695 assert( db!=0 );
696 assert( sqlite3_mutex_held(db->mutex) );
697 assert( db->pnBytesFreed==0 );
698 if( db->mallocFailed ){
699 return 0;
701 #endif
702 return dbMallocRawFinish(db, n);
705 /* Forward declaration */
706 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
709 ** Resize the block of memory pointed to by p to n bytes. If the
710 ** resize fails, set the mallocFailed flag in the connection object.
712 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
713 assert( db!=0 );
714 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
715 assert( sqlite3_mutex_held(db->mutex) );
716 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
717 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
718 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
719 if( n<=LOOKASIDE_SMALL ) return p;
720 }else
721 #endif
722 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
723 if( n<=db->lookaside.szTrue ) return p;
726 return dbReallocFinish(db, p, n);
728 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
729 void *pNew = 0;
730 assert( db!=0 );
731 assert( p!=0 );
732 if( db->mallocFailed==0 ){
733 if( isLookaside(db, p) ){
734 pNew = sqlite3DbMallocRawNN(db, n);
735 if( pNew ){
736 memcpy(pNew, p, lookasideMallocSize(db, p));
737 sqlite3DbFree(db, p);
739 }else{
740 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
741 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
742 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
743 pNew = sqlite3Realloc(p, n);
744 if( !pNew ){
745 sqlite3OomFault(db);
747 sqlite3MemdebugSetType(pNew,
748 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
751 return pNew;
755 ** Attempt to reallocate p. If the reallocation fails, then free p
756 ** and set the mallocFailed flag in the database connection.
758 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
759 void *pNew;
760 pNew = sqlite3DbRealloc(db, p, n);
761 if( !pNew ){
762 sqlite3DbFree(db, p);
764 return pNew;
768 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
769 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
770 ** is because when memory debugging is turned on, these two functions are
771 ** called via macros that record the current file and line number in the
772 ** ThreadData structure.
774 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
775 char *zNew;
776 size_t n;
777 if( z==0 ){
778 return 0;
780 n = strlen(z) + 1;
781 zNew = sqlite3DbMallocRaw(db, n);
782 if( zNew ){
783 memcpy(zNew, z, n);
785 return zNew;
787 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
788 char *zNew;
789 assert( db!=0 );
790 assert( z!=0 || n==0 );
791 assert( (n&0x7fffffff)==n );
792 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
793 if( zNew ){
794 memcpy(zNew, z, (size_t)n);
795 zNew[n] = 0;
797 return zNew;
801 ** The text between zStart and zEnd represents a phrase within a larger
802 ** SQL statement. Make a copy of this phrase in space obtained form
803 ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
805 char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
806 int n;
807 #ifdef SQLITE_DEBUG
808 /* Because of the way the parser works, the span is guaranteed to contain
809 ** at least one non-space character */
810 for(n=0; sqlite3Isspace(zStart[n]); n++){ assert( &zStart[n]<zEnd ); }
811 #endif
812 while( sqlite3Isspace(zStart[0]) ) zStart++;
813 n = (int)(zEnd - zStart);
814 while( sqlite3Isspace(zStart[n-1]) ) n--;
815 return sqlite3DbStrNDup(db, zStart, n);
819 ** Free any prior content in *pz and replace it with a copy of zNew.
821 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
822 char *z = sqlite3DbStrDup(db, zNew);
823 sqlite3DbFree(db, *pz);
824 *pz = z;
828 ** Call this routine to record the fact that an OOM (out-of-memory) error
829 ** has happened. This routine will set db->mallocFailed, and also
830 ** temporarily disable the lookaside memory allocator and interrupt
831 ** any running VDBEs.
833 ** Always return a NULL pointer so that this routine can be invoked using
835 ** return sqlite3OomFault(db);
837 ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
838 ** common case where no OOM occurs.
840 void *sqlite3OomFault(sqlite3 *db){
841 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
842 db->mallocFailed = 1;
843 if( db->nVdbeExec>0 ){
844 AtomicStore(&db->u1.isInterrupted, 1);
846 DisableLookaside;
847 if( db->pParse ){
848 Parse *pParse;
849 sqlite3ErrorMsg(db->pParse, "out of memory");
850 db->pParse->rc = SQLITE_NOMEM_BKPT;
851 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
852 pParse->nErr++;
853 pParse->rc = SQLITE_NOMEM;
857 return 0;
861 ** This routine reactivates the memory allocator and clears the
862 ** db->mallocFailed flag as necessary.
864 ** The memory allocator is not restarted if there are running
865 ** VDBEs.
867 void sqlite3OomClear(sqlite3 *db){
868 if( db->mallocFailed && db->nVdbeExec==0 ){
869 db->mallocFailed = 0;
870 AtomicStore(&db->u1.isInterrupted, 0);
871 assert( db->lookaside.bDisable>0 );
872 EnableLookaside;
877 ** Take actions at the end of an API call to deal with error codes.
879 static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
880 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
881 sqlite3OomClear(db);
882 sqlite3Error(db, SQLITE_NOMEM);
883 return SQLITE_NOMEM_BKPT;
885 return rc & db->errMask;
889 ** This function must be called before exiting any API function (i.e.
890 ** returning control to the user) that has called sqlite3_malloc or
891 ** sqlite3_realloc.
893 ** The returned value is normally a copy of the second argument to this
894 ** function. However, if a malloc() failure has occurred since the previous
895 ** invocation SQLITE_NOMEM is returned instead.
897 ** If an OOM as occurred, then the connection error-code (the value
898 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
900 int sqlite3ApiExit(sqlite3* db, int rc){
901 /* If the db handle must hold the connection handle mutex here.
902 ** Otherwise the read (and possible write) of db->mallocFailed
903 ** is unsafe, as is the call to sqlite3Error().
905 assert( db!=0 );
906 assert( sqlite3_mutex_held(db->mutex) );
907 if( db->mallocFailed || rc ){
908 return apiHandleError(db, rc);
910 return rc & db->errMask;