Adjust path to test databases for tests
[sqlcipher.git] / src / malloc.c
blob462d78e68cb6e716780fc9d190e1665f2271cd54
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 ** An instance of the following object records the location of
37 ** each unused scratch buffer.
39 typedef struct ScratchFreeslot {
40 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
41 } ScratchFreeslot;
44 ** State information local to the memory allocation subsystem.
46 static SQLITE_WSD struct Mem0Global {
47 sqlite3_mutex *mutex; /* Mutex to serialize access */
48 sqlite3_int64 alarmThreshold; /* The soft heap limit */
51 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
52 ** (so that a range test can be used to determine if an allocation
53 ** being freed came from pScratch) and a pointer to the list of
54 ** unused scratch allocations.
56 void *pScratchEnd;
57 ScratchFreeslot *pScratchFree;
58 u32 nScratchFree;
61 ** True if heap is nearly "full" where "full" is defined by the
62 ** sqlite3_soft_heap_limit() setting.
64 int nearlyFull;
65 } mem0 = { 0, 0, 0, 0, 0, 0 };
67 #define mem0 GLOBAL(struct Mem0Global, mem0)
70 ** Return the memory allocator mutex. sqlite3_status() needs it.
72 sqlite3_mutex *sqlite3MallocMutex(void){
73 return mem0.mutex;
76 #ifndef SQLITE_OMIT_DEPRECATED
78 ** Deprecated external interface. It used to set an alarm callback
79 ** that was invoked when memory usage grew too large. Now it is a
80 ** no-op.
82 int sqlite3_memory_alarm(
83 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
84 void *pArg,
85 sqlite3_int64 iThreshold
87 (void)xCallback;
88 (void)pArg;
89 (void)iThreshold;
90 return SQLITE_OK;
92 #endif
95 ** Set the soft heap-size limit for the library. Passing a zero or
96 ** negative value indicates no limit.
98 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
99 sqlite3_int64 priorLimit;
100 sqlite3_int64 excess;
101 sqlite3_int64 nUsed;
102 #ifndef SQLITE_OMIT_AUTOINIT
103 int rc = sqlite3_initialize();
104 if( rc ) return -1;
105 #endif
106 sqlite3_mutex_enter(mem0.mutex);
107 priorLimit = mem0.alarmThreshold;
108 if( n<0 ){
109 sqlite3_mutex_leave(mem0.mutex);
110 return priorLimit;
112 mem0.alarmThreshold = n;
113 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
114 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 ** Initialize the memory allocation subsystem.
128 int sqlite3MallocInit(void){
129 int rc;
130 if( sqlite3GlobalConfig.m.xMalloc==0 ){
131 sqlite3MemSetDefault();
133 memset(&mem0, 0, sizeof(mem0));
134 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
135 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
136 && sqlite3GlobalConfig.nScratch>0 ){
137 int i, n, sz;
138 ScratchFreeslot *pSlot;
139 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
140 sqlite3GlobalConfig.szScratch = sz;
141 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
142 n = sqlite3GlobalConfig.nScratch;
143 mem0.pScratchFree = pSlot;
144 mem0.nScratchFree = n;
145 for(i=0; i<n-1; i++){
146 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
147 pSlot = pSlot->pNext;
149 pSlot->pNext = 0;
150 mem0.pScratchEnd = (void*)&pSlot[1];
151 }else{
152 mem0.pScratchEnd = 0;
153 sqlite3GlobalConfig.pScratch = 0;
154 sqlite3GlobalConfig.szScratch = 0;
155 sqlite3GlobalConfig.nScratch = 0;
157 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
158 || sqlite3GlobalConfig.nPage<=0 ){
159 sqlite3GlobalConfig.pPage = 0;
160 sqlite3GlobalConfig.szPage = 0;
162 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
163 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
164 return rc;
168 ** Return true if the heap is currently under memory pressure - in other
169 ** words if the amount of heap used is close to the limit set by
170 ** sqlite3_soft_heap_limit().
172 int sqlite3HeapNearlyFull(void){
173 return mem0.nearlyFull;
177 ** Deinitialize the memory allocation subsystem.
179 void sqlite3MallocEnd(void){
180 if( sqlite3GlobalConfig.m.xShutdown ){
181 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
183 memset(&mem0, 0, sizeof(mem0));
187 ** Return the amount of memory currently checked out.
189 sqlite3_int64 sqlite3_memory_used(void){
190 sqlite3_int64 res, mx;
191 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
192 return res;
196 ** Return the maximum amount of memory that has ever been
197 ** checked out since either the beginning of this process
198 ** or since the most recent reset.
200 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
201 sqlite3_int64 res, mx;
202 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
203 return mx;
207 ** Trigger the alarm
209 static void sqlite3MallocAlarm(int nByte){
210 if( mem0.alarmThreshold<=0 ) return;
211 sqlite3_mutex_leave(mem0.mutex);
212 sqlite3_release_memory(nByte);
213 sqlite3_mutex_enter(mem0.mutex);
217 ** Do a memory allocation with statistics and alarms. Assume the
218 ** lock is already held.
220 static int mallocWithAlarm(int n, void **pp){
221 int nFull;
222 void *p;
223 assert( sqlite3_mutex_held(mem0.mutex) );
224 nFull = sqlite3GlobalConfig.m.xRoundup(n);
225 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
226 if( mem0.alarmThreshold>0 ){
227 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
228 if( nUsed >= mem0.alarmThreshold - nFull ){
229 mem0.nearlyFull = 1;
230 sqlite3MallocAlarm(nFull);
231 }else{
232 mem0.nearlyFull = 0;
235 p = sqlite3GlobalConfig.m.xMalloc(nFull);
236 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
237 if( p==0 && mem0.alarmThreshold>0 ){
238 sqlite3MallocAlarm(nFull);
239 p = sqlite3GlobalConfig.m.xMalloc(nFull);
241 #endif
242 if( p ){
243 nFull = sqlite3MallocSize(p);
244 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
245 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
247 *pp = p;
248 return nFull;
252 ** Allocate memory. This routine is like sqlite3_malloc() except that it
253 ** assumes the memory subsystem has already been initialized.
255 void *sqlite3Malloc(u64 n){
256 void *p;
257 if( n==0 || n>=0x7fffff00 ){
258 /* A memory allocation of a number of bytes which is near the maximum
259 ** signed integer value might cause an integer overflow inside of the
260 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
261 ** 255 bytes of overhead. SQLite itself will never use anything near
262 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
263 p = 0;
264 }else if( sqlite3GlobalConfig.bMemstat ){
265 sqlite3_mutex_enter(mem0.mutex);
266 mallocWithAlarm((int)n, &p);
267 sqlite3_mutex_leave(mem0.mutex);
268 }else{
269 p = sqlite3GlobalConfig.m.xMalloc((int)n);
271 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
272 return p;
276 ** This version of the memory allocation is for use by the application.
277 ** First make sure the memory subsystem is initialized, then do the
278 ** allocation.
280 void *sqlite3_malloc(int n){
281 #ifndef SQLITE_OMIT_AUTOINIT
282 if( sqlite3_initialize() ) return 0;
283 #endif
284 return n<=0 ? 0 : sqlite3Malloc(n);
286 void *sqlite3_malloc64(sqlite3_uint64 n){
287 #ifndef SQLITE_OMIT_AUTOINIT
288 if( sqlite3_initialize() ) return 0;
289 #endif
290 return sqlite3Malloc(n);
294 ** Each thread may only have a single outstanding allocation from
295 ** xScratchMalloc(). We verify this constraint in the single-threaded
296 ** case by setting scratchAllocOut to 1 when an allocation
297 ** is outstanding clearing it when the allocation is freed.
299 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
300 static int scratchAllocOut = 0;
301 #endif
305 ** Allocate memory that is to be used and released right away.
306 ** This routine is similar to alloca() in that it is not intended
307 ** for situations where the memory might be held long-term. This
308 ** routine is intended to get memory to old large transient data
309 ** structures that would not normally fit on the stack of an
310 ** embedded processor.
312 void *sqlite3ScratchMalloc(int n){
313 void *p;
314 assert( n>0 );
316 sqlite3_mutex_enter(mem0.mutex);
317 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
318 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
319 p = mem0.pScratchFree;
320 mem0.pScratchFree = mem0.pScratchFree->pNext;
321 mem0.nScratchFree--;
322 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
323 sqlite3_mutex_leave(mem0.mutex);
324 }else{
325 sqlite3_mutex_leave(mem0.mutex);
326 p = sqlite3Malloc(n);
327 if( sqlite3GlobalConfig.bMemstat && p ){
328 sqlite3_mutex_enter(mem0.mutex);
329 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
330 sqlite3_mutex_leave(mem0.mutex);
332 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
334 assert( sqlite3_mutex_notheld(mem0.mutex) );
337 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
338 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
339 ** buffers per thread.
341 ** This can only be checked in single-threaded mode.
343 assert( scratchAllocOut==0 );
344 if( p ) scratchAllocOut++;
345 #endif
347 return p;
349 void sqlite3ScratchFree(void *p){
350 if( p ){
352 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
353 /* Verify that no more than two scratch allocation per thread
354 ** is outstanding at one time. (This is only checked in the
355 ** single-threaded case since checking in the multi-threaded case
356 ** would be much more complicated.) */
357 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
358 scratchAllocOut--;
359 #endif
361 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
362 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
363 ScratchFreeslot *pSlot;
364 pSlot = (ScratchFreeslot*)p;
365 sqlite3_mutex_enter(mem0.mutex);
366 pSlot->pNext = mem0.pScratchFree;
367 mem0.pScratchFree = pSlot;
368 mem0.nScratchFree++;
369 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
370 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
371 sqlite3_mutex_leave(mem0.mutex);
372 }else{
373 /* Release memory back to the heap */
374 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
375 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
376 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
377 if( sqlite3GlobalConfig.bMemstat ){
378 int iSize = sqlite3MallocSize(p);
379 sqlite3_mutex_enter(mem0.mutex);
380 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
381 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
382 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
383 sqlite3GlobalConfig.m.xFree(p);
384 sqlite3_mutex_leave(mem0.mutex);
385 }else{
386 sqlite3GlobalConfig.m.xFree(p);
393 ** TRUE if p is a lookaside memory allocation from db
395 #ifndef SQLITE_OMIT_LOOKASIDE
396 static int isLookaside(sqlite3 *db, void *p){
397 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
399 #else
400 #define isLookaside(A,B) 0
401 #endif
404 ** Return the size of a memory allocation previously obtained from
405 ** sqlite3Malloc() or sqlite3_malloc().
407 int sqlite3MallocSize(void *p){
408 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
409 return sqlite3GlobalConfig.m.xSize(p);
411 int sqlite3DbMallocSize(sqlite3 *db, void *p){
412 assert( p!=0 );
413 if( db==0 || !isLookaside(db,p) ){
414 #if SQLITE_DEBUG
415 if( db==0 ){
416 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
417 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
418 }else{
419 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
420 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
422 #endif
423 return sqlite3GlobalConfig.m.xSize(p);
424 }else{
425 assert( sqlite3_mutex_held(db->mutex) );
426 return db->lookaside.sz;
429 sqlite3_uint64 sqlite3_msize(void *p){
430 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
431 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
432 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
436 ** Free memory previously obtained from sqlite3Malloc().
438 void sqlite3_free(void *p){
439 if( p==0 ) return; /* IMP: R-49053-54554 */
440 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
441 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
442 if( sqlite3GlobalConfig.bMemstat ){
443 sqlite3_mutex_enter(mem0.mutex);
444 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
445 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
446 sqlite3GlobalConfig.m.xFree(p);
447 sqlite3_mutex_leave(mem0.mutex);
448 }else{
449 sqlite3GlobalConfig.m.xFree(p);
454 ** Add the size of memory allocation "p" to the count in
455 ** *db->pnBytesFreed.
457 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
458 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
462 ** Free memory that might be associated with a particular database
463 ** connection.
465 void sqlite3DbFree(sqlite3 *db, void *p){
466 assert( db==0 || sqlite3_mutex_held(db->mutex) );
467 if( p==0 ) return;
468 if( db ){
469 if( db->pnBytesFreed ){
470 measureAllocationSize(db, p);
471 return;
473 if( isLookaside(db, p) ){
474 LookasideSlot *pBuf = (LookasideSlot*)p;
475 #if SQLITE_DEBUG
476 /* Trash all content in the buffer being freed */
477 memset(p, 0xaa, db->lookaside.sz);
478 #endif
479 pBuf->pNext = db->lookaside.pFree;
480 db->lookaside.pFree = pBuf;
481 db->lookaside.nOut--;
482 return;
485 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
486 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
487 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
488 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
489 sqlite3_free(p);
493 ** Change the size of an existing memory allocation
495 void *sqlite3Realloc(void *pOld, u64 nBytes){
496 int nOld, nNew, nDiff;
497 void *pNew;
498 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
499 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
500 if( pOld==0 ){
501 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
503 if( nBytes==0 ){
504 sqlite3_free(pOld); /* IMP: R-26507-47431 */
505 return 0;
507 if( nBytes>=0x7fffff00 ){
508 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
509 return 0;
511 nOld = sqlite3MallocSize(pOld);
512 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
513 ** argument to xRealloc is always a value returned by a prior call to
514 ** xRoundup. */
515 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
516 if( nOld==nNew ){
517 pNew = pOld;
518 }else if( sqlite3GlobalConfig.bMemstat ){
519 sqlite3_mutex_enter(mem0.mutex);
520 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
521 nDiff = nNew - nOld;
522 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
523 mem0.alarmThreshold-nDiff ){
524 sqlite3MallocAlarm(nDiff);
526 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
527 if( pNew==0 && mem0.alarmThreshold>0 ){
528 sqlite3MallocAlarm((int)nBytes);
529 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
531 if( pNew ){
532 nNew = sqlite3MallocSize(pNew);
533 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
535 sqlite3_mutex_leave(mem0.mutex);
536 }else{
537 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
539 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
540 return pNew;
544 ** The public interface to sqlite3Realloc. Make sure that the memory
545 ** subsystem is initialized prior to invoking sqliteRealloc.
547 void *sqlite3_realloc(void *pOld, int n){
548 #ifndef SQLITE_OMIT_AUTOINIT
549 if( sqlite3_initialize() ) return 0;
550 #endif
551 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
552 return sqlite3Realloc(pOld, n);
554 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
555 #ifndef SQLITE_OMIT_AUTOINIT
556 if( sqlite3_initialize() ) return 0;
557 #endif
558 return sqlite3Realloc(pOld, n);
563 ** Allocate and zero memory.
565 void *sqlite3MallocZero(u64 n){
566 void *p = sqlite3Malloc(n);
567 if( p ){
568 memset(p, 0, (size_t)n);
570 return p;
574 ** Allocate and zero memory. If the allocation fails, make
575 ** the mallocFailed flag in the connection pointer.
577 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
578 void *p;
579 testcase( db==0 );
580 p = sqlite3DbMallocRaw(db, n);
581 if( p ) memset(p, 0, (size_t)n);
582 return p;
586 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
587 ** slower case when the allocation cannot be fulfilled using lookaside.
589 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
590 void *p;
591 assert( db!=0 );
592 p = sqlite3Malloc(n);
593 if( !p ) sqlite3OomFault(db);
594 sqlite3MemdebugSetType(p,
595 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
596 return p;
600 ** Allocate memory, either lookaside (if possible) or heap.
601 ** If the allocation fails, set the mallocFailed flag in
602 ** the connection pointer.
604 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
605 ** failure on the same database connection) then always return 0.
606 ** Hence for a particular database connection, once malloc starts
607 ** failing, it fails consistently until mallocFailed is reset.
608 ** This is an important assumption. There are many places in the
609 ** code that do things like this:
611 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
612 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
613 ** if( b ) a[10] = 9;
615 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
616 ** that all prior mallocs (ex: "a") worked too.
618 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
619 ** not a NULL pointer.
621 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
622 void *p;
623 if( db ) return sqlite3DbMallocRawNN(db, n);
624 p = sqlite3Malloc(n);
625 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
626 return p;
628 void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
629 #ifndef SQLITE_OMIT_LOOKASIDE
630 LookasideSlot *pBuf;
631 assert( db!=0 );
632 assert( sqlite3_mutex_held(db->mutex) );
633 assert( db->pnBytesFreed==0 );
634 if( db->lookaside.bDisable==0 ){
635 assert( db->mallocFailed==0 );
636 if( n>db->lookaside.sz ){
637 db->lookaside.anStat[1]++;
638 }else if( (pBuf = db->lookaside.pFree)==0 ){
639 db->lookaside.anStat[2]++;
640 }else{
641 db->lookaside.pFree = pBuf->pNext;
642 db->lookaside.nOut++;
643 db->lookaside.anStat[0]++;
644 if( db->lookaside.nOut>db->lookaside.mxOut ){
645 db->lookaside.mxOut = db->lookaside.nOut;
647 return (void*)pBuf;
649 }else if( db->mallocFailed ){
650 return 0;
652 #else
653 assert( db!=0 );
654 assert( sqlite3_mutex_held(db->mutex) );
655 assert( db->pnBytesFreed==0 );
656 if( db->mallocFailed ){
657 return 0;
659 #endif
660 return dbMallocRawFinish(db, n);
663 /* Forward declaration */
664 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
667 ** Resize the block of memory pointed to by p to n bytes. If the
668 ** resize fails, set the mallocFailed flag in the connection object.
670 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
671 assert( db!=0 );
672 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
673 assert( sqlite3_mutex_held(db->mutex) );
674 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
675 return dbReallocFinish(db, p, n);
677 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
678 void *pNew = 0;
679 assert( db!=0 );
680 assert( p!=0 );
681 if( db->mallocFailed==0 ){
682 if( isLookaside(db, p) ){
683 pNew = sqlite3DbMallocRawNN(db, n);
684 if( pNew ){
685 memcpy(pNew, p, db->lookaside.sz);
686 sqlite3DbFree(db, p);
688 }else{
689 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
690 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
691 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
692 pNew = sqlite3_realloc64(p, n);
693 if( !pNew ){
694 sqlite3OomFault(db);
696 sqlite3MemdebugSetType(pNew,
697 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
700 return pNew;
704 ** Attempt to reallocate p. If the reallocation fails, then free p
705 ** and set the mallocFailed flag in the database connection.
707 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
708 void *pNew;
709 pNew = sqlite3DbRealloc(db, p, n);
710 if( !pNew ){
711 sqlite3DbFree(db, p);
713 return pNew;
717 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
718 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
719 ** is because when memory debugging is turned on, these two functions are
720 ** called via macros that record the current file and line number in the
721 ** ThreadData structure.
723 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
724 char *zNew;
725 size_t n;
726 if( z==0 ){
727 return 0;
729 n = sqlite3Strlen30(z) + 1;
730 assert( (n&0x7fffffff)==n );
731 zNew = sqlite3DbMallocRaw(db, (int)n);
732 if( zNew ){
733 memcpy(zNew, z, n);
735 return zNew;
737 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
738 char *zNew;
739 assert( db!=0 );
740 if( z==0 ){
741 return 0;
743 assert( (n&0x7fffffff)==n );
744 zNew = sqlite3DbMallocRawNN(db, n+1);
745 if( zNew ){
746 memcpy(zNew, z, (size_t)n);
747 zNew[n] = 0;
749 return zNew;
753 ** Free any prior content in *pz and replace it with a copy of zNew.
755 void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
756 sqlite3DbFree(db, *pz);
757 *pz = sqlite3DbStrDup(db, zNew);
761 ** Call this routine to record the fact that an OOM (out-of-memory) error
762 ** has happened. This routine will set db->mallocFailed, and also
763 ** temporarily disable the lookaside memory allocator and interrupt
764 ** any running VDBEs.
766 void sqlite3OomFault(sqlite3 *db){
767 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
768 db->mallocFailed = 1;
769 if( db->nVdbeExec>0 ){
770 db->u1.isInterrupted = 1;
772 db->lookaside.bDisable++;
777 ** This routine reactivates the memory allocator and clears the
778 ** db->mallocFailed flag as necessary.
780 ** The memory allocator is not restarted if there are running
781 ** VDBEs.
783 void sqlite3OomClear(sqlite3 *db){
784 if( db->mallocFailed && db->nVdbeExec==0 ){
785 db->mallocFailed = 0;
786 db->u1.isInterrupted = 0;
787 assert( db->lookaside.bDisable>0 );
788 db->lookaside.bDisable--;
793 ** Take actions at the end of an API call to indicate an OOM error
795 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
796 sqlite3OomClear(db);
797 sqlite3Error(db, SQLITE_NOMEM);
798 return SQLITE_NOMEM_BKPT;
802 ** This function must be called before exiting any API function (i.e.
803 ** returning control to the user) that has called sqlite3_malloc or
804 ** sqlite3_realloc.
806 ** The returned value is normally a copy of the second argument to this
807 ** function. However, if a malloc() failure has occurred since the previous
808 ** invocation SQLITE_NOMEM is returned instead.
810 ** If an OOM as occurred, then the connection error-code (the value
811 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
813 int sqlite3ApiExit(sqlite3* db, int rc){
814 /* If the db handle must hold the connection handle mutex here.
815 ** Otherwise the read (and possible write) of db->mallocFailed
816 ** is unsafe, as is the call to sqlite3Error().
818 assert( db!=0 );
819 assert( sqlite3_mutex_held(db->mutex) );
820 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
821 return apiOomError(db);
823 return rc & db->errMask;