xref: /sqlite-3.40.0/src/malloc.c (revision 8773b858)
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.
10 **
11 *************************************************************************
12 **
13 ** Memory allocation functions used throughout sqlite.
14 */
15 #include "sqliteInt.h"
16 #include <stdarg.h>
17 
18 /*
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.
22 */
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
33 }
34 
35 /*
36 ** An instance of the following object records the location of
37 ** each unused scratch buffer.
38 */
39 typedef struct ScratchFreeslot {
40   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
41 } ScratchFreeslot;
42 
43 /*
44 ** State information local to the memory allocation subsystem.
45 */
46 static SQLITE_WSD struct Mem0Global {
47   sqlite3_mutex *mutex;         /* Mutex to serialize access */
48 
49   /*
50   ** The alarm callback and its arguments.  The mem0.mutex lock will
51   ** be held while the callback is running.  Recursive calls into
52   ** the memory subsystem are allowed, but no new callbacks will be
53   ** issued.
54   */
55   sqlite3_int64 alarmThreshold;
56   void (*alarmCallback)(void*, sqlite3_int64,int);
57   void *alarmArg;
58 
59   /*
60   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
61   ** (so that a range test can be used to determine if an allocation
62   ** being freed came from pScratch) and a pointer to the list of
63   ** unused scratch allocations.
64   */
65   void *pScratchEnd;
66   ScratchFreeslot *pScratchFree;
67   u32 nScratchFree;
68 
69   /*
70   ** True if heap is nearly "full" where "full" is defined by the
71   ** sqlite3_soft_heap_limit() setting.
72   */
73   int nearlyFull;
74 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
75 
76 #define mem0 GLOBAL(struct Mem0Global, mem0)
77 
78 /*
79 ** Return the memory allocator mutex. sqlite3_status() needs it.
80 */
81 sqlite3_mutex *sqlite3MallocMutex(void){
82   return mem0.mutex;
83 }
84 
85 /*
86 ** This routine runs when the memory allocator sees that the
87 ** total memory allocation is about to exceed the soft heap
88 ** limit.
89 */
90 static void softHeapLimitEnforcer(
91   void *NotUsed,
92   sqlite3_int64 NotUsed2,
93   int allocSize
94 ){
95   UNUSED_PARAMETER2(NotUsed, NotUsed2);
96   sqlite3_release_memory(allocSize);
97 }
98 
99 /*
100 ** Change the alarm callback
101 */
102 static int sqlite3MemoryAlarm(
103   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
104   void *pArg,
105   sqlite3_int64 iThreshold
106 ){
107   sqlite3_int64 nUsed;
108   sqlite3_mutex_enter(mem0.mutex);
109   mem0.alarmCallback = xCallback;
110   mem0.alarmArg = pArg;
111   mem0.alarmThreshold = iThreshold;
112   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
113   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
114   sqlite3_mutex_leave(mem0.mutex);
115   return SQLITE_OK;
116 }
117 
118 #ifndef SQLITE_OMIT_DEPRECATED
119 /*
120 ** Deprecated external interface.  Internal/core SQLite code
121 ** should call sqlite3MemoryAlarm.
122 */
123 int sqlite3_memory_alarm(
124   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
125   void *pArg,
126   sqlite3_int64 iThreshold
127 ){
128   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
129 }
130 #endif
131 
132 /*
133 ** Set the soft heap-size limit for the library. Passing a zero or
134 ** negative value indicates no limit.
135 */
136 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
137   sqlite3_int64 priorLimit;
138   sqlite3_int64 excess;
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.alarmThreshold;
145   sqlite3_mutex_leave(mem0.mutex);
146   if( n<0 ) return priorLimit;
147   if( n>0 ){
148     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
149   }else{
150     sqlite3MemoryAlarm(0, 0, 0);
151   }
152   excess = sqlite3_memory_used() - n;
153   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
154   return priorLimit;
155 }
156 void sqlite3_soft_heap_limit(int n){
157   if( n<0 ) n = 0;
158   sqlite3_soft_heap_limit64(n);
159 }
160 
161 /*
162 ** Initialize the memory allocation subsystem.
163 */
164 int sqlite3MallocInit(void){
165   int rc;
166   if( sqlite3GlobalConfig.m.xMalloc==0 ){
167     sqlite3MemSetDefault();
168   }
169   memset(&mem0, 0, sizeof(mem0));
170   if( sqlite3GlobalConfig.bCoreMutex ){
171     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
172   }
173   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
174       && sqlite3GlobalConfig.nScratch>0 ){
175     int i, n, sz;
176     ScratchFreeslot *pSlot;
177     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
178     sqlite3GlobalConfig.szScratch = sz;
179     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
180     n = sqlite3GlobalConfig.nScratch;
181     mem0.pScratchFree = pSlot;
182     mem0.nScratchFree = n;
183     for(i=0; i<n-1; i++){
184       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
185       pSlot = pSlot->pNext;
186     }
187     pSlot->pNext = 0;
188     mem0.pScratchEnd = (void*)&pSlot[1];
189   }else{
190     mem0.pScratchEnd = 0;
191     sqlite3GlobalConfig.pScratch = 0;
192     sqlite3GlobalConfig.szScratch = 0;
193     sqlite3GlobalConfig.nScratch = 0;
194   }
195   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
196       || sqlite3GlobalConfig.nPage<1 ){
197     sqlite3GlobalConfig.pPage = 0;
198     sqlite3GlobalConfig.szPage = 0;
199     sqlite3GlobalConfig.nPage = 0;
200   }
201   rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
202   if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
203   return rc;
204 }
205 
206 /*
207 ** Return true if the heap is currently under memory pressure - in other
208 ** words if the amount of heap used is close to the limit set by
209 ** sqlite3_soft_heap_limit().
210 */
211 int sqlite3HeapNearlyFull(void){
212   return mem0.nearlyFull;
213 }
214 
215 /*
216 ** Deinitialize the memory allocation subsystem.
217 */
218 void sqlite3MallocEnd(void){
219   if( sqlite3GlobalConfig.m.xShutdown ){
220     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
221   }
222   memset(&mem0, 0, sizeof(mem0));
223 }
224 
225 /*
226 ** Return the amount of memory currently checked out.
227 */
228 sqlite3_int64 sqlite3_memory_used(void){
229   int n, mx;
230   sqlite3_int64 res;
231   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
232   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
233   return res;
234 }
235 
236 /*
237 ** Return the maximum amount of memory that has ever been
238 ** checked out since either the beginning of this process
239 ** or since the most recent reset.
240 */
241 sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
242   int n, mx;
243   sqlite3_int64 res;
244   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
245   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
246   return res;
247 }
248 
249 /*
250 ** Trigger the alarm
251 */
252 static void sqlite3MallocAlarm(int nByte){
253   void (*xCallback)(void*,sqlite3_int64,int);
254   sqlite3_int64 nowUsed;
255   void *pArg;
256   if( mem0.alarmCallback==0 ) return;
257   xCallback = mem0.alarmCallback;
258   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
259   pArg = mem0.alarmArg;
260   mem0.alarmCallback = 0;
261   sqlite3_mutex_leave(mem0.mutex);
262   xCallback(pArg, nowUsed, nByte);
263   sqlite3_mutex_enter(mem0.mutex);
264   mem0.alarmCallback = xCallback;
265   mem0.alarmArg = pArg;
266 }
267 
268 /*
269 ** Do a memory allocation with statistics and alarms.  Assume the
270 ** lock is already held.
271 */
272 static int mallocWithAlarm(int n, void **pp){
273   int nFull;
274   void *p;
275   assert( sqlite3_mutex_held(mem0.mutex) );
276   nFull = sqlite3GlobalConfig.m.xRoundup(n);
277   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
278   if( mem0.alarmCallback!=0 ){
279     sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
280     if( nUsed >= mem0.alarmThreshold - nFull ){
281       mem0.nearlyFull = 1;
282       sqlite3MallocAlarm(nFull);
283     }else{
284       mem0.nearlyFull = 0;
285     }
286   }
287   p = sqlite3GlobalConfig.m.xMalloc(nFull);
288 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
289   if( p==0 && mem0.alarmCallback ){
290     sqlite3MallocAlarm(nFull);
291     p = sqlite3GlobalConfig.m.xMalloc(nFull);
292   }
293 #endif
294   if( p ){
295     nFull = sqlite3MallocSize(p);
296     sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
297     sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
298   }
299   *pp = p;
300   return nFull;
301 }
302 
303 /*
304 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
305 ** assumes the memory subsystem has already been initialized.
306 */
307 void *sqlite3Malloc(u64 n){
308   void *p;
309   if( n==0 || n>=0x7fffff00 ){
310     /* A memory allocation of a number of bytes which is near the maximum
311     ** signed integer value might cause an integer overflow inside of the
312     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
313     ** 255 bytes of overhead.  SQLite itself will never use anything near
314     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
315     p = 0;
316   }else if( sqlite3GlobalConfig.bMemstat ){
317     sqlite3_mutex_enter(mem0.mutex);
318     mallocWithAlarm((int)n, &p);
319     sqlite3_mutex_leave(mem0.mutex);
320   }else{
321     p = sqlite3GlobalConfig.m.xMalloc((int)n);
322   }
323   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
324   return p;
325 }
326 
327 /*
328 ** This version of the memory allocation is for use by the application.
329 ** First make sure the memory subsystem is initialized, then do the
330 ** allocation.
331 */
332 void *sqlite3_malloc(int n){
333 #ifndef SQLITE_OMIT_AUTOINIT
334   if( sqlite3_initialize() ) return 0;
335 #endif
336   return n<=0 ? 0 : sqlite3Malloc(n);
337 }
338 void *sqlite3_malloc64(sqlite3_uint64 n){
339 #ifndef SQLITE_OMIT_AUTOINIT
340   if( sqlite3_initialize() ) return 0;
341 #endif
342   return sqlite3Malloc(n);
343 }
344 
345 /*
346 ** Each thread may only have a single outstanding allocation from
347 ** xScratchMalloc().  We verify this constraint in the single-threaded
348 ** case by setting scratchAllocOut to 1 when an allocation
349 ** is outstanding clearing it when the allocation is freed.
350 */
351 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
352 static int scratchAllocOut = 0;
353 #endif
354 
355 
356 /*
357 ** Allocate memory that is to be used and released right away.
358 ** This routine is similar to alloca() in that it is not intended
359 ** for situations where the memory might be held long-term.  This
360 ** routine is intended to get memory to old large transient data
361 ** structures that would not normally fit on the stack of an
362 ** embedded processor.
363 */
364 void *sqlite3ScratchMalloc(int n){
365   void *p;
366   assert( n>0 );
367 
368   sqlite3_mutex_enter(mem0.mutex);
369   sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
370   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
371     p = mem0.pScratchFree;
372     mem0.pScratchFree = mem0.pScratchFree->pNext;
373     mem0.nScratchFree--;
374     sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
375     sqlite3_mutex_leave(mem0.mutex);
376   }else{
377     sqlite3_mutex_leave(mem0.mutex);
378     p = sqlite3Malloc(n);
379     if( sqlite3GlobalConfig.bMemstat && p ){
380       sqlite3_mutex_enter(mem0.mutex);
381       sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
382       sqlite3_mutex_leave(mem0.mutex);
383     }
384     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
385   }
386   assert( sqlite3_mutex_notheld(mem0.mutex) );
387 
388 
389 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
390   /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
391   ** buffers per thread.
392   **
393   ** This can only be checked in single-threaded mode.
394   */
395   assert( scratchAllocOut==0 );
396   if( p ) scratchAllocOut++;
397 #endif
398 
399   return p;
400 }
401 void sqlite3ScratchFree(void *p){
402   if( p ){
403 
404 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
405     /* Verify that no more than two scratch allocation per thread
406     ** is outstanding at one time.  (This is only checked in the
407     ** single-threaded case since checking in the multi-threaded case
408     ** would be much more complicated.) */
409     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
410     scratchAllocOut--;
411 #endif
412 
413     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
414       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
415       ScratchFreeslot *pSlot;
416       pSlot = (ScratchFreeslot*)p;
417       sqlite3_mutex_enter(mem0.mutex);
418       pSlot->pNext = mem0.pScratchFree;
419       mem0.pScratchFree = pSlot;
420       mem0.nScratchFree++;
421       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
422       sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
423       sqlite3_mutex_leave(mem0.mutex);
424     }else{
425       /* Release memory back to the heap */
426       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
427       assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
428       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
429       if( sqlite3GlobalConfig.bMemstat ){
430         int iSize = sqlite3MallocSize(p);
431         sqlite3_mutex_enter(mem0.mutex);
432         sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
433         sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
434         sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
435         sqlite3GlobalConfig.m.xFree(p);
436         sqlite3_mutex_leave(mem0.mutex);
437       }else{
438         sqlite3GlobalConfig.m.xFree(p);
439       }
440     }
441   }
442 }
443 
444 /*
445 ** TRUE if p is a lookaside memory allocation from db
446 */
447 #ifndef SQLITE_OMIT_LOOKASIDE
448 static int isLookaside(sqlite3 *db, void *p){
449   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
450 }
451 #else
452 #define isLookaside(A,B) 0
453 #endif
454 
455 /*
456 ** Return the size of a memory allocation previously obtained from
457 ** sqlite3Malloc() or sqlite3_malloc().
458 */
459 int sqlite3MallocSize(void *p){
460   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
461   return sqlite3GlobalConfig.m.xSize(p);
462 }
463 int sqlite3DbMallocSize(sqlite3 *db, void *p){
464   if( db==0 ){
465     assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
466     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
467     return sqlite3MallocSize(p);
468   }else{
469     assert( sqlite3_mutex_held(db->mutex) );
470     if( isLookaside(db, p) ){
471       return db->lookaside.sz;
472     }else{
473       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
474       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
475       return sqlite3GlobalConfig.m.xSize(p);
476     }
477   }
478 }
479 sqlite3_uint64 sqlite3_msize(void *p){
480   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
481   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
482   return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
483 }
484 
485 /*
486 ** Free memory previously obtained from sqlite3Malloc().
487 */
488 void sqlite3_free(void *p){
489   if( p==0 ) return;  /* IMP: R-49053-54554 */
490   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
491   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
492   if( sqlite3GlobalConfig.bMemstat ){
493     sqlite3_mutex_enter(mem0.mutex);
494     sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
495     sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
496     sqlite3GlobalConfig.m.xFree(p);
497     sqlite3_mutex_leave(mem0.mutex);
498   }else{
499     sqlite3GlobalConfig.m.xFree(p);
500   }
501 }
502 
503 /*
504 ** Add the size of memory allocation "p" to the count in
505 ** *db->pnBytesFreed.
506 */
507 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
508   *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
509 }
510 
511 /*
512 ** Free memory that might be associated with a particular database
513 ** connection.
514 */
515 void sqlite3DbFree(sqlite3 *db, void *p){
516   assert( db==0 || sqlite3_mutex_held(db->mutex) );
517   if( p==0 ) return;
518   if( db ){
519     if( db->pnBytesFreed ){
520       measureAllocationSize(db, p);
521       return;
522     }
523     if( isLookaside(db, p) ){
524       LookasideSlot *pBuf = (LookasideSlot*)p;
525 #if SQLITE_DEBUG
526       /* Trash all content in the buffer being freed */
527       memset(p, 0xaa, db->lookaside.sz);
528 #endif
529       pBuf->pNext = db->lookaside.pFree;
530       db->lookaside.pFree = pBuf;
531       db->lookaside.nOut--;
532       return;
533     }
534   }
535   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
536   assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
537   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
538   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
539   sqlite3_free(p);
540 }
541 
542 /*
543 ** Change the size of an existing memory allocation
544 */
545 void *sqlite3Realloc(void *pOld, u64 nBytes){
546   int nOld, nNew, nDiff;
547   void *pNew;
548   assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
549   assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
550   if( pOld==0 ){
551     return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
552   }
553   if( nBytes==0 ){
554     sqlite3_free(pOld); /* IMP: R-26507-47431 */
555     return 0;
556   }
557   if( nBytes>=0x7fffff00 ){
558     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
559     return 0;
560   }
561   nOld = sqlite3MallocSize(pOld);
562   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
563   ** argument to xRealloc is always a value returned by a prior call to
564   ** xRoundup. */
565   nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
566   if( nOld==nNew ){
567     pNew = pOld;
568   }else if( sqlite3GlobalConfig.bMemstat ){
569     sqlite3_mutex_enter(mem0.mutex);
570     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
571     nDiff = nNew - nOld;
572     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
573           mem0.alarmThreshold-nDiff ){
574       sqlite3MallocAlarm(nDiff);
575     }
576     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
577     if( pNew==0 && mem0.alarmCallback ){
578       sqlite3MallocAlarm((int)nBytes);
579       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
580     }
581     if( pNew ){
582       nNew = sqlite3MallocSize(pNew);
583       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
584     }
585     sqlite3_mutex_leave(mem0.mutex);
586   }else{
587     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
588   }
589   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
590   return pNew;
591 }
592 
593 /*
594 ** The public interface to sqlite3Realloc.  Make sure that the memory
595 ** subsystem is initialized prior to invoking sqliteRealloc.
596 */
597 void *sqlite3_realloc(void *pOld, int n){
598 #ifndef SQLITE_OMIT_AUTOINIT
599   if( sqlite3_initialize() ) return 0;
600 #endif
601   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
602   return sqlite3Realloc(pOld, n);
603 }
604 void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
605 #ifndef SQLITE_OMIT_AUTOINIT
606   if( sqlite3_initialize() ) return 0;
607 #endif
608   return sqlite3Realloc(pOld, n);
609 }
610 
611 
612 /*
613 ** Allocate and zero memory.
614 */
615 void *sqlite3MallocZero(u64 n){
616   void *p = sqlite3Malloc(n);
617   if( p ){
618     memset(p, 0, (size_t)n);
619   }
620   return p;
621 }
622 
623 /*
624 ** Allocate and zero memory.  If the allocation fails, make
625 ** the mallocFailed flag in the connection pointer.
626 */
627 void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
628   void *p = sqlite3DbMallocRaw(db, n);
629   if( p ){
630     memset(p, 0, (size_t)n);
631   }
632   return p;
633 }
634 
635 /*
636 ** Allocate and zero memory.  If the allocation fails, make
637 ** the mallocFailed flag in the connection pointer.
638 **
639 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
640 ** failure on the same database connection) then always return 0.
641 ** Hence for a particular database connection, once malloc starts
642 ** failing, it fails consistently until mallocFailed is reset.
643 ** This is an important assumption.  There are many places in the
644 ** code that do things like this:
645 **
646 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
647 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
648 **         if( b ) a[10] = 9;
649 **
650 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
651 ** that all prior mallocs (ex: "a") worked too.
652 */
653 void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
654   void *p;
655   assert( db==0 || sqlite3_mutex_held(db->mutex) );
656   assert( db==0 || db->pnBytesFreed==0 );
657 #ifndef SQLITE_OMIT_LOOKASIDE
658   if( db ){
659     LookasideSlot *pBuf;
660     if( db->mallocFailed ){
661       return 0;
662     }
663     if( db->lookaside.bEnabled ){
664       if( n>db->lookaside.sz ){
665         db->lookaside.anStat[1]++;
666       }else if( (pBuf = db->lookaside.pFree)==0 ){
667         db->lookaside.anStat[2]++;
668       }else{
669         db->lookaside.pFree = pBuf->pNext;
670         db->lookaside.nOut++;
671         db->lookaside.anStat[0]++;
672         if( db->lookaside.nOut>db->lookaside.mxOut ){
673           db->lookaside.mxOut = db->lookaside.nOut;
674         }
675         return (void*)pBuf;
676       }
677     }
678   }
679 #else
680   if( db && db->mallocFailed ){
681     return 0;
682   }
683 #endif
684   p = sqlite3Malloc(n);
685   if( !p && db ){
686     db->mallocFailed = 1;
687   }
688   sqlite3MemdebugSetType(p,
689          (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
690   return p;
691 }
692 
693 /*
694 ** Resize the block of memory pointed to by p to n bytes. If the
695 ** resize fails, set the mallocFailed flag in the connection object.
696 */
697 void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
698   void *pNew = 0;
699   assert( db!=0 );
700   assert( sqlite3_mutex_held(db->mutex) );
701   if( db->mallocFailed==0 ){
702     if( p==0 ){
703       return sqlite3DbMallocRaw(db, n);
704     }
705     if( isLookaside(db, p) ){
706       if( n<=db->lookaside.sz ){
707         return p;
708       }
709       pNew = sqlite3DbMallocRaw(db, n);
710       if( pNew ){
711         memcpy(pNew, p, db->lookaside.sz);
712         sqlite3DbFree(db, p);
713       }
714     }else{
715       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
716       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
717       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
718       pNew = sqlite3_realloc64(p, n);
719       if( !pNew ){
720         db->mallocFailed = 1;
721       }
722       sqlite3MemdebugSetType(pNew,
723             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
724     }
725   }
726   return pNew;
727 }
728 
729 /*
730 ** Attempt to reallocate p.  If the reallocation fails, then free p
731 ** and set the mallocFailed flag in the database connection.
732 */
733 void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
734   void *pNew;
735   pNew = sqlite3DbRealloc(db, p, n);
736   if( !pNew ){
737     sqlite3DbFree(db, p);
738   }
739   return pNew;
740 }
741 
742 /*
743 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
744 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
745 ** is because when memory debugging is turned on, these two functions are
746 ** called via macros that record the current file and line number in the
747 ** ThreadData structure.
748 */
749 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
750   char *zNew;
751   size_t n;
752   if( z==0 ){
753     return 0;
754   }
755   n = sqlite3Strlen30(z) + 1;
756   assert( (n&0x7fffffff)==n );
757   zNew = sqlite3DbMallocRaw(db, (int)n);
758   if( zNew ){
759     memcpy(zNew, z, n);
760   }
761   return zNew;
762 }
763 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
764   char *zNew;
765   if( z==0 ){
766     return 0;
767   }
768   assert( (n&0x7fffffff)==n );
769   zNew = sqlite3DbMallocRaw(db, n+1);
770   if( zNew ){
771     memcpy(zNew, z, (size_t)n);
772     zNew[n] = 0;
773   }
774   return zNew;
775 }
776 
777 /*
778 ** Create a string from the zFromat argument and the va_list that follows.
779 ** Store the string in memory obtained from sqliteMalloc() and make *pz
780 ** point to that string.
781 */
782 void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
783   va_list ap;
784   char *z;
785 
786   va_start(ap, zFormat);
787   z = sqlite3VMPrintf(db, zFormat, ap);
788   va_end(ap);
789   sqlite3DbFree(db, *pz);
790   *pz = z;
791 }
792 
793 /*
794 ** Take actions at the end of an API call to indicate an OOM error
795 */
796 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
797   db->mallocFailed = 0;
798   sqlite3Error(db, SQLITE_NOMEM);
799   return SQLITE_NOMEM;
800 }
801 
802 /*
803 ** This function must be called before exiting any API function (i.e.
804 ** returning control to the user) that has called sqlite3_malloc or
805 ** sqlite3_realloc.
806 **
807 ** The returned value is normally a copy of the second argument to this
808 ** function. However, if a malloc() failure has occurred since the previous
809 ** invocation SQLITE_NOMEM is returned instead.
810 **
811 ** If the first argument, db, is not NULL and a malloc() error has occurred,
812 ** then the connection error-code (the value returned by sqlite3_errcode())
813 ** is set to SQLITE_NOMEM.
814 */
815 int sqlite3ApiExit(sqlite3* db, int rc){
816   /* If the db handle is not NULL, then we must hold the connection handle
817   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
818   ** is unsafe, as is the call to sqlite3Error().
819   */
820   assert( !db || sqlite3_mutex_held(db->mutex) );
821   if( db==0 ) return rc & 0xff;
822   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
823     return apiOomError(db);
824   }
825   return rc & db->errMask;
826 }
827