xref: /sqlite-3.40.0/src/malloc.c (revision 4ef299a3)
1a3152895Sdrh /*
2a3152895Sdrh ** 2001 September 15
3a3152895Sdrh **
4a3152895Sdrh ** The author disclaims copyright to this source code.  In place of
5a3152895Sdrh ** a legal notice, here is a blessing:
6a3152895Sdrh **
7a3152895Sdrh **    May you do good and not evil.
8a3152895Sdrh **    May you find forgiveness for yourself and forgive others.
9a3152895Sdrh **    May you share freely, never taking more than you give.
10a3152895Sdrh **
11a3152895Sdrh *************************************************************************
12fec00eabSdrh **
13a3152895Sdrh ** Memory allocation functions used throughout sqlite.
14a3152895Sdrh */
15a3152895Sdrh #include "sqliteInt.h"
16a3152895Sdrh #include <stdarg.h>
17a3152895Sdrh 
18a3152895Sdrh /*
198468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently
208468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to
218468024dSdanielk1977 ** cache database pages that are not currently in use.
22a3152895Sdrh */
23a3152895Sdrh int sqlite3_release_memory(int n){
2486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
259f129f46Sdrh   return sqlite3PcacheReleaseMemory(n);
261e536953Sdanielk1977 #else
279f129f46Sdrh   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
289f129f46Sdrh   ** is a no-op returning zero if SQLite is not compiled with
299f129f46Sdrh   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
3062c14b34Sdanielk1977   UNUSED_PARAMETER(n);
319f129f46Sdrh   return 0;
321e536953Sdanielk1977 #endif
33a3152895Sdrh }
34a3152895Sdrh 
35fec00eabSdrh /*
36badc980aSdrh ** An instance of the following object records the location of
37badc980aSdrh ** each unused scratch buffer.
38badc980aSdrh */
39badc980aSdrh typedef struct ScratchFreeslot {
40badc980aSdrh   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
41badc980aSdrh } ScratchFreeslot;
42badc980aSdrh 
43badc980aSdrh /*
44fec00eabSdrh ** State information local to the memory allocation subsystem.
45fec00eabSdrh */
465c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
47fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
48*4ef299a3Sdrh   sqlite3_int64 alarmThreshold;  /* The soft heap limit */
49fec00eabSdrh 
50fec00eabSdrh   /*
51badc980aSdrh   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
52badc980aSdrh   ** (so that a range test can be used to determine if an allocation
53badc980aSdrh   ** being freed came from pScratch) and a pointer to the list of
54badc980aSdrh   ** unused scratch allocations.
559ac3fe97Sdrh   */
56badc980aSdrh   void *pScratchEnd;
57badc980aSdrh   ScratchFreeslot *pScratchFree;
58badc980aSdrh   u32 nScratchFree;
5950d1b5f3Sdrh 
6050d1b5f3Sdrh   /*
6150d1b5f3Sdrh   ** True if heap is nearly "full" where "full" is defined by the
6250d1b5f3Sdrh   ** sqlite3_soft_heap_limit() setting.
6350d1b5f3Sdrh   */
6450d1b5f3Sdrh   int nearlyFull;
65*4ef299a3Sdrh } mem0 = { 0, 0, 0, 0, 0, 0 };
665c8f8587Sdanielk1977 
675c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
68fec00eabSdrh 
69fec00eabSdrh /*
70af89fe66Sdrh ** Return the memory allocator mutex. sqlite3_status() needs it.
71af89fe66Sdrh */
72af89fe66Sdrh sqlite3_mutex *sqlite3MallocMutex(void){
73af89fe66Sdrh   return mem0.mutex;
74af89fe66Sdrh }
75af89fe66Sdrh 
76af89fe66Sdrh /*
77*4ef299a3Sdrh ** Return the amount of memory currently in use.
78f82ccf64Sdrh */
79*4ef299a3Sdrh static sqlite3_int64 memInUse(void){
80*4ef299a3Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
81*4ef299a3Sdrh   return sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
82f82ccf64Sdrh }
83f82ccf64Sdrh 
84f82ccf64Sdrh /*
85*4ef299a3Sdrh ** Called when the soft heap limit is exceeded for an allocation
86*4ef299a3Sdrh ** of nBytes.
87f82ccf64Sdrh */
88*4ef299a3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
89*4ef299a3Sdrh static void sqlite3HeapLimitExceeded(int nByte){
90*4ef299a3Sdrh   sqlite3_int64 excess = memInUse() + nByte - mem0.alarmThreshold;
91f82ccf64Sdrh   sqlite3_mutex_leave(mem0.mutex);
92*4ef299a3Sdrh   sqlite3_release_memory((int)(excess & 0x7fffffff));
93*4ef299a3Sdrh   sqlite3_mutex_enter(mem0.mutex);
94*4ef299a3Sdrh }
95*4ef299a3Sdrh #else
96*4ef299a3Sdrh # define sqlite3HeapLimitExceeded(X)  /* no-op */
97*4ef299a3Sdrh #endif
98*4ef299a3Sdrh 
99*4ef299a3Sdrh /*
100*4ef299a3Sdrh ** Check to see if increasing the total memory usage by nNew bytes
101*4ef299a3Sdrh ** will exceed the soft heap limit.
102*4ef299a3Sdrh **
103*4ef299a3Sdrh ** If the soft heap limit is exceeded, set the mem0.nearlyFull flag
104*4ef299a3Sdrh ** and invoke sqlite3HeapLimitExceeded() to try to free up some
105*4ef299a3Sdrh ** memory.
106*4ef299a3Sdrh */
107*4ef299a3Sdrh static void sqlite3CheckSoftHeapLimit(int nNew){
108*4ef299a3Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
109*4ef299a3Sdrh   if( mem0.alarmThreshold>0 ){
110*4ef299a3Sdrh     if( mem0.alarmThreshold-nNew >= memInUse() ){
111*4ef299a3Sdrh       mem0.nearlyFull = 1;
112*4ef299a3Sdrh       sqlite3HeapLimitExceeded(nNew);
113*4ef299a3Sdrh     }else{
114*4ef299a3Sdrh       mem0.nearlyFull = 0;
115*4ef299a3Sdrh     }
116*4ef299a3Sdrh   }
117f82ccf64Sdrh }
118f82ccf64Sdrh 
119f82ccf64Sdrh #ifndef SQLITE_OMIT_DEPRECATED
120f82ccf64Sdrh /*
121*4ef299a3Sdrh ** Deprecated external interface.  First deprecated 2007-11-05.  Changed
122*4ef299a3Sdrh ** into a no-op on 2015-09-02.
123f82ccf64Sdrh */
124f82ccf64Sdrh int sqlite3_memory_alarm(
125f82ccf64Sdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
126f82ccf64Sdrh   void *pArg,
127f82ccf64Sdrh   sqlite3_int64 iThreshold
128f82ccf64Sdrh ){
129*4ef299a3Sdrh   return SQLITE_OK;
130f82ccf64Sdrh }
131f82ccf64Sdrh #endif
132f82ccf64Sdrh 
133f82ccf64Sdrh /*
134f82ccf64Sdrh ** Set the soft heap-size limit for the library. Passing a zero or
135f82ccf64Sdrh ** negative value indicates no limit.
136f82ccf64Sdrh */
137f82ccf64Sdrh sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
138f82ccf64Sdrh   sqlite3_int64 priorLimit;
139f82ccf64Sdrh #ifndef SQLITE_OMIT_AUTOINIT
140de0f1815Sdrh   int rc = sqlite3_initialize();
141de0f1815Sdrh   if( rc ) return -1;
142f82ccf64Sdrh #endif
143f82ccf64Sdrh   sqlite3_mutex_enter(mem0.mutex);
144f82ccf64Sdrh   priorLimit = mem0.alarmThreshold;
145f82ccf64Sdrh   if( n>0 ){
146*4ef299a3Sdrh     mem0.alarmThreshold = n;
147*4ef299a3Sdrh     sqlite3CheckSoftHeapLimit(0);
148*4ef299a3Sdrh   }else if( n==0 ){
149*4ef299a3Sdrh     mem0.alarmThreshold = 0;
150*4ef299a3Sdrh     mem0.nearlyFull = 0;
151f82ccf64Sdrh   }
152*4ef299a3Sdrh   sqlite3_mutex_leave(mem0.mutex);
153f82ccf64Sdrh   return priorLimit;
154f82ccf64Sdrh }
155f82ccf64Sdrh void sqlite3_soft_heap_limit(int n){
156f82ccf64Sdrh   if( n<0 ) n = 0;
157f82ccf64Sdrh   sqlite3_soft_heap_limit64(n);
158f82ccf64Sdrh }
159f82ccf64Sdrh 
160f82ccf64Sdrh /*
161fec00eabSdrh ** Initialize the memory allocation subsystem.
162fec00eabSdrh */
163fec00eabSdrh int sqlite3MallocInit(void){
164592f0cb1Sdrh   int rc;
165075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
166fec00eabSdrh     sqlite3MemSetDefault();
167fec00eabSdrh   }
168fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
169075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
17059f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
171fec00eabSdrh   }
172075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
1737ff2719eSdrh       && sqlite3GlobalConfig.nScratch>0 ){
174badc980aSdrh     int i, n, sz;
175badc980aSdrh     ScratchFreeslot *pSlot;
176badc980aSdrh     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
177badc980aSdrh     sqlite3GlobalConfig.szScratch = sz;
178badc980aSdrh     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
179badc980aSdrh     n = sqlite3GlobalConfig.nScratch;
180badc980aSdrh     mem0.pScratchFree = pSlot;
181badc980aSdrh     mem0.nScratchFree = n;
182badc980aSdrh     for(i=0; i<n-1; i++){
183badc980aSdrh       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
184badc980aSdrh       pSlot = pSlot->pNext;
185badc980aSdrh     }
186badc980aSdrh     pSlot->pNext = 0;
187badc980aSdrh     mem0.pScratchEnd = (void*)&pSlot[1];
1889ac3fe97Sdrh   }else{
189badc980aSdrh     mem0.pScratchEnd = 0;
190075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
191075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
192badc980aSdrh     sqlite3GlobalConfig.nScratch = 0;
1939ac3fe97Sdrh   }
19450d1b5f3Sdrh   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
19501c5c00cSdrh       || sqlite3GlobalConfig.nPage<=0 ){
196075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
197075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
1989ac3fe97Sdrh   }
199592f0cb1Sdrh   rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
200592f0cb1Sdrh   if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
201592f0cb1Sdrh   return rc;
202fec00eabSdrh }
203fec00eabSdrh 
204fec00eabSdrh /*
20550d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other
20650d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by
20750d1b5f3Sdrh ** sqlite3_soft_heap_limit().
20850d1b5f3Sdrh */
20950d1b5f3Sdrh int sqlite3HeapNearlyFull(void){
21050d1b5f3Sdrh   return mem0.nearlyFull;
21150d1b5f3Sdrh }
21250d1b5f3Sdrh 
21350d1b5f3Sdrh /*
214fec00eabSdrh ** Deinitialize the memory allocation subsystem.
215fec00eabSdrh */
216fec00eabSdrh void sqlite3MallocEnd(void){
2170a549071Sdanielk1977   if( sqlite3GlobalConfig.m.xShutdown ){
218075c23afSdanielk1977     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
2190a549071Sdanielk1977   }
2209ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
221fec00eabSdrh }
222fec00eabSdrh 
223fec00eabSdrh /*
224fec00eabSdrh ** Return the amount of memory currently checked out.
225fec00eabSdrh */
226fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
227df5e1a00Sdrh   sqlite3_int64 res, mx;
228df5e1a00Sdrh   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
229c376a198Sdrh   return res;
230fec00eabSdrh }
231fec00eabSdrh 
232fec00eabSdrh /*
233fec00eabSdrh ** Return the maximum amount of memory that has ever been
234fec00eabSdrh ** checked out since either the beginning of this process
235fec00eabSdrh ** or since the most recent reset.
236fec00eabSdrh */
237fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
238df5e1a00Sdrh   sqlite3_int64 res, mx;
239df5e1a00Sdrh   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
240df5e1a00Sdrh   return mx;
241fec00eabSdrh }
242fec00eabSdrh 
243fec00eabSdrh /*
244f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
245f7141990Sdrh ** lock is already held.
246fec00eabSdrh */
247f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
248fec00eabSdrh   int nFull;
249f7141990Sdrh   void *p;
250f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
251075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
252f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
253*4ef299a3Sdrh   sqlite3CheckSoftHeapLimit(nFull);
254075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
25550d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
256*4ef299a3Sdrh   if( p==0 && mem0.alarmThreshold ){
257*4ef299a3Sdrh     sqlite3HeapLimitExceeded(nFull);
258075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
259fec00eabSdrh   }
26050d1b5f3Sdrh #endif
261c702c7ccSdrh   if( p ){
262c702c7ccSdrh     nFull = sqlite3MallocSize(p);
263af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
264af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
265c702c7ccSdrh   }
266f7141990Sdrh   *pp = p;
267f7141990Sdrh   return nFull;
268fec00eabSdrh }
269f7141990Sdrh 
270f7141990Sdrh /*
271f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
272f7141990Sdrh ** assumes the memory subsystem has already been initialized.
273f7141990Sdrh */
274da4ca9d1Sdrh void *sqlite3Malloc(u64 n){
275f7141990Sdrh   void *p;
276da4ca9d1Sdrh   if( n==0 || n>=0x7fffff00 ){
277e08ed7e7Sdrh     /* A memory allocation of a number of bytes which is near the maximum
278e08ed7e7Sdrh     ** signed integer value might cause an integer overflow inside of the
279e08ed7e7Sdrh     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
280e08ed7e7Sdrh     ** 255 bytes of overhead.  SQLite itself will never use anything near
281e08ed7e7Sdrh     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
282f7141990Sdrh     p = 0;
283075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
284f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
2853329a63aSdrh     mallocWithAlarm((int)n, &p);
286fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
287fec00eabSdrh   }else{
288da4ca9d1Sdrh     p = sqlite3GlobalConfig.m.xMalloc((int)n);
289fec00eabSdrh   }
2908da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
291fec00eabSdrh   return p;
292fec00eabSdrh }
293fec00eabSdrh 
294fec00eabSdrh /*
295fec00eabSdrh ** This version of the memory allocation is for use by the application.
296fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
297fec00eabSdrh ** allocation.
298fec00eabSdrh */
299fec00eabSdrh void *sqlite3_malloc(int n){
300fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
301fec00eabSdrh   if( sqlite3_initialize() ) return 0;
302fec00eabSdrh #endif
303da4ca9d1Sdrh   return n<=0 ? 0 : sqlite3Malloc(n);
304da4ca9d1Sdrh }
305da4ca9d1Sdrh void *sqlite3_malloc64(sqlite3_uint64 n){
306da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
307da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
308da4ca9d1Sdrh #endif
309fec00eabSdrh   return sqlite3Malloc(n);
310fec00eabSdrh }
311fec00eabSdrh 
312fec00eabSdrh /*
313e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
314facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
315facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
316e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
317e5ae5735Sdrh */
318e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
319facf0307Sdrh static int scratchAllocOut = 0;
320e5ae5735Sdrh #endif
321e5ae5735Sdrh 
322e5ae5735Sdrh 
323e5ae5735Sdrh /*
324e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
325e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
326e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
327e5ae5735Sdrh ** routine is intended to get memory to old large transient data
328e5ae5735Sdrh ** structures that would not normally fit on the stack of an
329e5ae5735Sdrh ** embedded processor.
330e5ae5735Sdrh */
331facf0307Sdrh void *sqlite3ScratchMalloc(int n){
332e5ae5735Sdrh   void *p;
333e5ae5735Sdrh   assert( n>0 );
3349ac3fe97Sdrh 
335badc980aSdrh   sqlite3_mutex_enter(mem0.mutex);
3363ccd5bf8Sdrh   sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
337badc980aSdrh   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
338badc980aSdrh     p = mem0.pScratchFree;
339badc980aSdrh     mem0.pScratchFree = mem0.pScratchFree->pNext;
340badc980aSdrh     mem0.nScratchFree--;
341af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
342b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
343badc980aSdrh   }else{
344b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
3453ccd5bf8Sdrh     p = sqlite3Malloc(n);
3463ccd5bf8Sdrh     if( sqlite3GlobalConfig.bMemstat && p ){
3473ccd5bf8Sdrh       sqlite3_mutex_enter(mem0.mutex);
348af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
3493ccd5bf8Sdrh       sqlite3_mutex_leave(mem0.mutex);
350badc980aSdrh     }
351badc980aSdrh     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
352badc980aSdrh   }
3531ff6e3abSdrh   assert( sqlite3_mutex_notheld(mem0.mutex) );
354b0c6a888Sdan 
355badc980aSdrh 
356badc980aSdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
357cbd55b03Sdrh   /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
358cbd55b03Sdrh   ** buffers per thread.
359cbd55b03Sdrh   **
360cbd55b03Sdrh   ** This can only be checked in single-threaded mode.
361cbd55b03Sdrh   */
362cbd55b03Sdrh   assert( scratchAllocOut==0 );
363badc980aSdrh   if( p ) scratchAllocOut++;
364badc980aSdrh #endif
365badc980aSdrh 
366badc980aSdrh   return p;
367badc980aSdrh }
368badc980aSdrh void sqlite3ScratchFree(void *p){
369badc980aSdrh   if( p ){
370badc980aSdrh 
371e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
37237f99187Sdrh     /* Verify that no more than two scratch allocation per thread
3739ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3749ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3759ac3fe97Sdrh     ** would be much more complicated.) */
376badc980aSdrh     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
377badc980aSdrh     scratchAllocOut--;
378e5ae5735Sdrh #endif
3799ac3fe97Sdrh 
380badc980aSdrh     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
381badc980aSdrh       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
382badc980aSdrh       ScratchFreeslot *pSlot;
383badc980aSdrh       pSlot = (ScratchFreeslot*)p;
384e5ae5735Sdrh       sqlite3_mutex_enter(mem0.mutex);
385badc980aSdrh       pSlot->pNext = mem0.pScratchFree;
386badc980aSdrh       mem0.pScratchFree = pSlot;
387badc980aSdrh       mem0.nScratchFree++;
388fcd71b60Sdrh       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
389af89fe66Sdrh       sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
3909ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
391f7141990Sdrh     }else{
392badc980aSdrh       /* Release memory back to the heap */
393107b56e8Sdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
394d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
395107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
396075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
397f7141990Sdrh         int iSize = sqlite3MallocSize(p);
398f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
399af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
400af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
401af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
402075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
403f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
404f7141990Sdrh       }else{
405075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
406f7141990Sdrh       }
4079ac3fe97Sdrh     }
408e5ae5735Sdrh   }
409e5ae5735Sdrh }
410e5ae5735Sdrh 
411e5ae5735Sdrh /*
412633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
413633e6d57Sdrh */
4144150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
415633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
416b0e7704eSdrh   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
417633e6d57Sdrh }
4184150ebf8Sdrh #else
4194150ebf8Sdrh #define isLookaside(A,B) 0
4204150ebf8Sdrh #endif
421633e6d57Sdrh 
422633e6d57Sdrh /*
423fec00eabSdrh ** Return the size of a memory allocation previously obtained from
424fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
425fec00eabSdrh */
426fec00eabSdrh int sqlite3MallocSize(void *p){
427107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
428075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
429fec00eabSdrh }
430633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
431054bbabcSdrh   if( db==0 || !isLookaside(db,p) ){
432054bbabcSdrh #if SQLITE_DEBUG
43317bcb102Sdrh     if( db==0 ){
434d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
435d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
436633e6d57Sdrh     }else{
437d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
438d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
439633e6d57Sdrh     }
440054bbabcSdrh #endif
441054bbabcSdrh     return sqlite3GlobalConfig.m.xSize(p);
442054bbabcSdrh   }else{
443054bbabcSdrh     assert( sqlite3_mutex_held(db->mutex) );
444054bbabcSdrh     return db->lookaside.sz;
445633e6d57Sdrh   }
44617bcb102Sdrh }
447da4ca9d1Sdrh sqlite3_uint64 sqlite3_msize(void *p){
448d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
449d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
450da4ca9d1Sdrh   return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
451da4ca9d1Sdrh }
452fec00eabSdrh 
453fec00eabSdrh /*
454fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
455fec00eabSdrh */
456fec00eabSdrh void sqlite3_free(void *p){
45771a1a0f4Sdrh   if( p==0 ) return;  /* IMP: R-49053-54554 */
458107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
459d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
460075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
461fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
462af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
463af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
464075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
465fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
466fec00eabSdrh   }else{
467075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
468fec00eabSdrh   }
469fec00eabSdrh }
470fec00eabSdrh 
471fec00eabSdrh /*
472b4586f12Sdrh ** Add the size of memory allocation "p" to the count in
473b4586f12Sdrh ** *db->pnBytesFreed.
474b4586f12Sdrh */
475b4586f12Sdrh static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
476b4586f12Sdrh   *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
477b4586f12Sdrh }
478b4586f12Sdrh 
479b4586f12Sdrh /*
480633e6d57Sdrh ** Free memory that might be associated with a particular database
481633e6d57Sdrh ** connection.
482633e6d57Sdrh */
483633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
4847047e25cSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
4859ccd8659Sdrh   if( p==0 ) return;
486174b9a16Sdrh   if( db ){
487174b9a16Sdrh     if( db->pnBytesFreed ){
488b4586f12Sdrh       measureAllocationSize(db, p);
489174b9a16Sdrh       return;
490d46def77Sdan     }
491633e6d57Sdrh     if( isLookaside(db, p) ){
492633e6d57Sdrh       LookasideSlot *pBuf = (LookasideSlot*)p;
4933608f177Sdrh #if SQLITE_DEBUG
4943608f177Sdrh       /* Trash all content in the buffer being freed */
4953608f177Sdrh       memset(p, 0xaa, db->lookaside.sz);
4963608f177Sdrh #endif
497633e6d57Sdrh       pBuf->pNext = db->lookaside.pFree;
498633e6d57Sdrh       db->lookaside.pFree = pBuf;
499633e6d57Sdrh       db->lookaside.nOut--;
500174b9a16Sdrh       return;
501174b9a16Sdrh     }
502174b9a16Sdrh   }
503d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
504d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
505174b9a16Sdrh   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
506107b56e8Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
507633e6d57Sdrh   sqlite3_free(p);
508633e6d57Sdrh }
509633e6d57Sdrh 
510633e6d57Sdrh /*
511fec00eabSdrh ** Change the size of an existing memory allocation
512fec00eabSdrh */
513da4ca9d1Sdrh void *sqlite3Realloc(void *pOld, u64 nBytes){
514ca591febSshaneh   int nOld, nNew, nDiff;
515fec00eabSdrh   void *pNew;
516d231aa3aSdrh   assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
517d425864dSmistachkin   assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
518fec00eabSdrh   if( pOld==0 ){
5198da47419Sdrh     return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
520fec00eabSdrh   }
521da4ca9d1Sdrh   if( nBytes==0 ){
5228da47419Sdrh     sqlite3_free(pOld); /* IMP: R-26507-47431 */
523fec00eabSdrh     return 0;
524fec00eabSdrh   }
525b6063cf8Sdrh   if( nBytes>=0x7fffff00 ){
526b6063cf8Sdrh     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
527b6063cf8Sdrh     return 0;
528b6063cf8Sdrh   }
529fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
5309f129f46Sdrh   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
5319f129f46Sdrh   ** argument to xRealloc is always a value returned by a prior call to
5329f129f46Sdrh   ** xRoundup. */
533da4ca9d1Sdrh   nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
534fec00eabSdrh   if( nOld==nNew ){
535fec00eabSdrh     pNew = pOld;
5367c6791c8Sdrh   }else if( sqlite3GlobalConfig.bMemstat ){
5377c6791c8Sdrh     sqlite3_mutex_enter(mem0.mutex);
5383329a63aSdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
5398e1bb041Sdrh     nDiff = nNew - nOld;
540*4ef299a3Sdrh     sqlite3CheckSoftHeapLimit(nDiff);
541075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
542*4ef299a3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
543*4ef299a3Sdrh     if( pNew==0 && mem0.alarmThreshold ){
544*4ef299a3Sdrh       sqlite3HeapLimitExceeded((int)nBytes);
545075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
546fec00eabSdrh     }
547*4ef299a3Sdrh #endif
548fec00eabSdrh     if( pNew ){
549c702c7ccSdrh       nNew = sqlite3MallocSize(pNew);
550af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
551fec00eabSdrh     }
552fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
553fec00eabSdrh   }else{
5547c6791c8Sdrh     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
555fec00eabSdrh   }
5568da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
557fec00eabSdrh   return pNew;
558fec00eabSdrh }
559fec00eabSdrh 
560fec00eabSdrh /*
561fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
562fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
563fec00eabSdrh */
564fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
565fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
566fec00eabSdrh   if( sqlite3_initialize() ) return 0;
567fec00eabSdrh #endif
5688da47419Sdrh   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
569da4ca9d1Sdrh   return sqlite3Realloc(pOld, n);
570da4ca9d1Sdrh }
571da4ca9d1Sdrh void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
572da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
573da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
574da4ca9d1Sdrh #endif
575fec00eabSdrh   return sqlite3Realloc(pOld, n);
576fec00eabSdrh }
577fec00eabSdrh 
578a3152895Sdrh 
579a3152895Sdrh /*
58017435752Sdrh ** Allocate and zero memory.
581a3152895Sdrh */
582da4ca9d1Sdrh void *sqlite3MallocZero(u64 n){
583fec00eabSdrh   void *p = sqlite3Malloc(n);
584a3152895Sdrh   if( p ){
58520f3df04Sdrh     memset(p, 0, (size_t)n);
586a3152895Sdrh   }
587a3152895Sdrh   return p;
588a3152895Sdrh }
58917435752Sdrh 
59017435752Sdrh /*
59117435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
59217435752Sdrh ** the mallocFailed flag in the connection pointer.
59317435752Sdrh */
594da4ca9d1Sdrh void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
595a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
59617435752Sdrh   if( p ){
59720f3df04Sdrh     memset(p, 0, (size_t)n);
59817435752Sdrh   }
59917435752Sdrh   return p;
60017435752Sdrh }
60117435752Sdrh 
60217435752Sdrh /*
60317435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
60417435752Sdrh ** the mallocFailed flag in the connection pointer.
605ddecae79Sdrh **
606ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
607ddecae79Sdrh ** failure on the same database connection) then always return 0.
608ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
609ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
610ddecae79Sdrh ** This is an important assumption.  There are many places in the
611ddecae79Sdrh ** code that do things like this:
612ddecae79Sdrh **
613ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
614ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
615ddecae79Sdrh **         if( b ) a[10] = 9;
616ddecae79Sdrh **
617ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
618ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
61917435752Sdrh */
620da4ca9d1Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
621633e6d57Sdrh   void *p;
622d9da78a2Sdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
623ccd4ad3eSdan   assert( db==0 || db->pnBytesFreed==0 );
6244150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
625633e6d57Sdrh   if( db ){
626633e6d57Sdrh     LookasideSlot *pBuf;
627633e6d57Sdrh     if( db->mallocFailed ){
628633e6d57Sdrh       return 0;
629633e6d57Sdrh     }
6300b12e7f8Sdrh     if( db->lookaside.bEnabled ){
6310b12e7f8Sdrh       if( n>db->lookaside.sz ){
6320b12e7f8Sdrh         db->lookaside.anStat[1]++;
6330b12e7f8Sdrh       }else if( (pBuf = db->lookaside.pFree)==0 ){
6340b12e7f8Sdrh         db->lookaside.anStat[2]++;
6350b12e7f8Sdrh       }else{
636633e6d57Sdrh         db->lookaside.pFree = pBuf->pNext;
637633e6d57Sdrh         db->lookaside.nOut++;
6380b12e7f8Sdrh         db->lookaside.anStat[0]++;
639633e6d57Sdrh         if( db->lookaside.nOut>db->lookaside.mxOut ){
640633e6d57Sdrh           db->lookaside.mxOut = db->lookaside.nOut;
641633e6d57Sdrh         }
642633e6d57Sdrh         return (void*)pBuf;
643633e6d57Sdrh       }
644633e6d57Sdrh     }
6450b12e7f8Sdrh   }
646ddecae79Sdrh #else
647ddecae79Sdrh   if( db && db->mallocFailed ){
648ddecae79Sdrh     return 0;
649ddecae79Sdrh   }
6504150ebf8Sdrh #endif
651fec00eabSdrh   p = sqlite3Malloc(n);
652f3a65f7eSdrh   if( !p && db ){
65317435752Sdrh     db->mallocFailed = 1;
65417435752Sdrh   }
655d231aa3aSdrh   sqlite3MemdebugSetType(p,
656d231aa3aSdrh          (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
65717435752Sdrh   return p;
65817435752Sdrh }
65917435752Sdrh 
66026783a58Sdanielk1977 /*
66126783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
66226783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
66326783a58Sdanielk1977 */
664da4ca9d1Sdrh void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
665a1644fd8Sdanielk1977   void *pNew = 0;
666d9da78a2Sdrh   assert( db!=0 );
6677047e25cSdrh   assert( sqlite3_mutex_held(db->mutex) );
668a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
669633e6d57Sdrh     if( p==0 ){
670633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
671633e6d57Sdrh     }
672633e6d57Sdrh     if( isLookaside(db, p) ){
673633e6d57Sdrh       if( n<=db->lookaside.sz ){
674633e6d57Sdrh         return p;
675633e6d57Sdrh       }
676633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
677633e6d57Sdrh       if( pNew ){
678633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
679633e6d57Sdrh         sqlite3DbFree(db, p);
680633e6d57Sdrh       }
681633e6d57Sdrh     }else{
682d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
683d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
684107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
6853329a63aSdrh       pNew = sqlite3_realloc64(p, n);
686a1644fd8Sdanielk1977       if( !pNew ){
687a1644fd8Sdanielk1977         db->mallocFailed = 1;
688a1644fd8Sdanielk1977       }
689d231aa3aSdrh       sqlite3MemdebugSetType(pNew,
690174b9a16Sdrh             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
691a1644fd8Sdanielk1977     }
692633e6d57Sdrh   }
693a1644fd8Sdanielk1977   return pNew;
694a1644fd8Sdanielk1977 }
695a1644fd8Sdanielk1977 
69617435752Sdrh /*
69717435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
69817435752Sdrh ** and set the mallocFailed flag in the database connection.
69917435752Sdrh */
700da4ca9d1Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
701a3152895Sdrh   void *pNew;
702a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
703a3152895Sdrh   if( !pNew ){
704633e6d57Sdrh     sqlite3DbFree(db, p);
705a3152895Sdrh   }
706a3152895Sdrh   return pNew;
707a3152895Sdrh }
708a3152895Sdrh 
709a3152895Sdrh /*
710a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
711a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
712a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
713a3152895Sdrh ** called via macros that record the current file and line number in the
714a3152895Sdrh ** ThreadData structure.
715a3152895Sdrh */
716633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
717a3152895Sdrh   char *zNew;
718633e6d57Sdrh   size_t n;
719633e6d57Sdrh   if( z==0 ){
720633e6d57Sdrh     return 0;
721a3152895Sdrh   }
722dee0e404Sdrh   n = sqlite3Strlen30(z) + 1;
723633e6d57Sdrh   assert( (n&0x7fffffff)==n );
724633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
725a3152895Sdrh   if( zNew ){
726a3152895Sdrh     memcpy(zNew, z, n);
7271e536953Sdanielk1977   }
7281e536953Sdanielk1977   return zNew;
7291e536953Sdanielk1977 }
730da4ca9d1Sdrh char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
731633e6d57Sdrh   char *zNew;
732633e6d57Sdrh   if( z==0 ){
733633e6d57Sdrh     return 0;
734633e6d57Sdrh   }
735633e6d57Sdrh   assert( (n&0x7fffffff)==n );
736633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
737633e6d57Sdrh   if( zNew ){
73820f3df04Sdrh     memcpy(zNew, z, (size_t)n);
739633e6d57Sdrh     zNew[n] = 0;
7401e536953Sdanielk1977   }
7411e536953Sdanielk1977   return zNew;
7421e536953Sdanielk1977 }
7431e536953Sdanielk1977 
744a3152895Sdrh /*
74522c17b8bSdrh ** Free any prior content in *pz and replace it with a copy of zNew.
746a3152895Sdrh */
74722c17b8bSdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
748633e6d57Sdrh   sqlite3DbFree(db, *pz);
74922c17b8bSdrh   *pz = sqlite3DbStrDup(db, zNew);
750a3152895Sdrh }
751a3152895Sdrh 
752b50c65d5Sdrh /*
753b50c65d5Sdrh ** Take actions at the end of an API call to indicate an OOM error
754b50c65d5Sdrh */
755b50c65d5Sdrh static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
756b50c65d5Sdrh   db->mallocFailed = 0;
757b50c65d5Sdrh   sqlite3Error(db, SQLITE_NOMEM);
758b50c65d5Sdrh   return SQLITE_NOMEM;
759b50c65d5Sdrh }
760a3152895Sdrh 
761a3152895Sdrh /*
762a3152895Sdrh ** This function must be called before exiting any API function (i.e.
76317435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
76417435752Sdrh ** sqlite3_realloc.
765a3152895Sdrh **
766a3152895Sdrh ** The returned value is normally a copy of the second argument to this
767be217793Sshane ** function. However, if a malloc() failure has occurred since the previous
768a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
769a3152895Sdrh **
770597d2b64Sdrh ** If an OOM as occurred, then the connection error-code (the value
771597d2b64Sdrh ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
772a3152895Sdrh */
773a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
774597d2b64Sdrh   /* If the db handle must hold the connection handle mutex here.
775597d2b64Sdrh   ** Otherwise the read (and possible write) of db->mallocFailed
776a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
777a1644fd8Sdanielk1977   */
778597d2b64Sdrh   assert( db!=0 );
779597d2b64Sdrh   assert( sqlite3_mutex_held(db->mutex) );
780b50c65d5Sdrh   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
781b50c65d5Sdrh     return apiOomError(db);
782a3152895Sdrh   }
783b50c65d5Sdrh   return rc & db->errMask;
784a3152895Sdrh }
785