xref: /sqlite-3.40.0/src/malloc.c (revision 50b65684)
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 **
15*50b65684Sdrh ** $Id: malloc.c,v 1.56 2009/02/17 18:37:29 drh Exp $
16a3152895Sdrh */
17a3152895Sdrh #include "sqliteInt.h"
18a3152895Sdrh #include <stdarg.h>
19a3152895Sdrh 
20a3152895Sdrh /*
21b21c8cd4Sdrh ** This routine runs when the memory allocator sees that the
22b21c8cd4Sdrh ** total memory allocation is about to exceed the soft heap
23b21c8cd4Sdrh ** limit.
24b21c8cd4Sdrh */
25b21c8cd4Sdrh static void softHeapLimitEnforcer(
26b21c8cd4Sdrh   void *NotUsed,
2762c14b34Sdanielk1977   sqlite3_int64 NotUsed2,
28153c62c4Sdrh   int allocSize
29b21c8cd4Sdrh ){
3062c14b34Sdanielk1977   UNUSED_PARAMETER2(NotUsed, NotUsed2);
31b21c8cd4Sdrh   sqlite3_release_memory(allocSize);
32b21c8cd4Sdrh }
33b21c8cd4Sdrh 
34b21c8cd4Sdrh /*
358468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or
368468024dSdanielk1977 ** negative value indicates no limit.
37a3152895Sdrh */
38a3152895Sdrh void sqlite3_soft_heap_limit(int n){
39b21c8cd4Sdrh   sqlite3_uint64 iLimit;
40b21c8cd4Sdrh   int overage;
41b21c8cd4Sdrh   if( n<0 ){
42b21c8cd4Sdrh     iLimit = 0;
43b21c8cd4Sdrh   }else{
44b21c8cd4Sdrh     iLimit = n;
45a3152895Sdrh   }
469ac3fe97Sdrh   sqlite3_initialize();
47b21c8cd4Sdrh   if( iLimit>0 ){
484a27a286Sshane     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
49b21c8cd4Sdrh   }else{
504a27a286Sshane     sqlite3MemoryAlarm(0, 0, 0);
51b21c8cd4Sdrh   }
521bd10f8aSdrh   overage = (int)(sqlite3_memory_used() - (i64)n);
53b21c8cd4Sdrh   if( overage>0 ){
54b21c8cd4Sdrh     sqlite3_release_memory(overage);
55b21c8cd4Sdrh   }
56a3152895Sdrh }
57a3152895Sdrh 
58a3152895Sdrh /*
598468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently
608468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to
618468024dSdanielk1977 ** cache database pages that are not currently in use.
62a3152895Sdrh */
63a3152895Sdrh int sqlite3_release_memory(int n){
6486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
6567e3da7aSdanielk1977   int nRet = 0;
6667e3da7aSdanielk1977 #if 0
6767e3da7aSdanielk1977   nRet += sqlite3VdbeReleaseMemory(n);
6867e3da7aSdanielk1977 #endif
6967e3da7aSdanielk1977   nRet += sqlite3PcacheReleaseMemory(n-nRet);
70dfb316d4Sdanielk1977   return nRet;
711e536953Sdanielk1977 #else
7262c14b34Sdanielk1977   UNUSED_PARAMETER(n);
731e536953Sdanielk1977   return SQLITE_OK;
741e536953Sdanielk1977 #endif
75a3152895Sdrh }
76a3152895Sdrh 
77fec00eabSdrh /*
78fec00eabSdrh ** State information local to the memory allocation subsystem.
79fec00eabSdrh */
805c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
8123bf0f41Sdanielk1977   /* Number of free pages for scratch and page-cache memory */
8223bf0f41Sdanielk1977   u32 nScratchFree;
8323bf0f41Sdanielk1977   u32 nPageFree;
8423bf0f41Sdanielk1977 
85fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
86fec00eabSdrh 
87fec00eabSdrh   /*
88fec00eabSdrh   ** The alarm callback and its arguments.  The mem0.mutex lock will
89fec00eabSdrh   ** be held while the callback is running.  Recursive calls into
90fec00eabSdrh   ** the memory subsystem are allowed, but no new callbacks will be
91fec00eabSdrh   ** issued.  The alarmBusy variable is set to prevent recursive
92fec00eabSdrh   ** callbacks.
93fec00eabSdrh   */
94fec00eabSdrh   sqlite3_int64 alarmThreshold;
95fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
96fec00eabSdrh   void *alarmArg;
97fec00eabSdrh   int alarmBusy;
98fec00eabSdrh 
99fec00eabSdrh   /*
100075c23afSdanielk1977   ** Pointers to the end of sqlite3GlobalConfig.pScratch and
101075c23afSdanielk1977   ** sqlite3GlobalConfig.pPage to a block of memory that records
1029ac3fe97Sdrh   ** which pages are available.
1039ac3fe97Sdrh   */
1049ac3fe97Sdrh   u32 *aScratchFree;
1059ac3fe97Sdrh   u32 *aPageFree;
106cdcfe95cSdanielk1977 } mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 };
1075c8f8587Sdanielk1977 
1085c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
109fec00eabSdrh 
110fec00eabSdrh /*
111fec00eabSdrh ** Initialize the memory allocation subsystem.
112fec00eabSdrh */
113fec00eabSdrh int sqlite3MallocInit(void){
114075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
115fec00eabSdrh     sqlite3MemSetDefault();
116fec00eabSdrh   }
117fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
118075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
11959f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
120fec00eabSdrh   }
121075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
122075c23afSdanielk1977       && sqlite3GlobalConfig.nScratch>=0 ){
1239ac3fe97Sdrh     int i;
12466e80084Sdrh     sqlite3GlobalConfig.szScratch = (sqlite3GlobalConfig.szScratch - 4) & ~7;
125075c23afSdanielk1977     mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
126075c23afSdanielk1977                   [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
127075c23afSdanielk1977     for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
128075c23afSdanielk1977     mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
1299ac3fe97Sdrh   }else{
130075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
131075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
1329ac3fe97Sdrh   }
133075c23afSdanielk1977   if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
134075c23afSdanielk1977       && sqlite3GlobalConfig.nPage>=1 ){
1359ac3fe97Sdrh     int i;
1360a60a384Sdrh     int overhead;
13766e80084Sdrh     int sz = sqlite3GlobalConfig.szPage & ~7;
138075c23afSdanielk1977     int n = sqlite3GlobalConfig.nPage;
1390a60a384Sdrh     overhead = (4*n + sz - 1)/sz;
140075c23afSdanielk1977     sqlite3GlobalConfig.nPage -= overhead;
141075c23afSdanielk1977     mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
142075c23afSdanielk1977                   [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
143075c23afSdanielk1977     for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
144075c23afSdanielk1977     mem0.nPageFree = sqlite3GlobalConfig.nPage;
1459ac3fe97Sdrh   }else{
146075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
147075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
1489ac3fe97Sdrh   }
149075c23afSdanielk1977   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
150fec00eabSdrh }
151fec00eabSdrh 
152fec00eabSdrh /*
153fec00eabSdrh ** Deinitialize the memory allocation subsystem.
154fec00eabSdrh */
155fec00eabSdrh void sqlite3MallocEnd(void){
1560a549071Sdanielk1977   if( sqlite3GlobalConfig.m.xShutdown ){
157075c23afSdanielk1977     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
1580a549071Sdanielk1977   }
1599ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
160fec00eabSdrh }
161fec00eabSdrh 
162fec00eabSdrh /*
163fec00eabSdrh ** Return the amount of memory currently checked out.
164fec00eabSdrh */
165fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
166f7141990Sdrh   int n, mx;
167c376a198Sdrh   sqlite3_int64 res;
168f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
169c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
170c376a198Sdrh   return res;
171fec00eabSdrh }
172fec00eabSdrh 
173fec00eabSdrh /*
174fec00eabSdrh ** Return the maximum amount of memory that has ever been
175fec00eabSdrh ** checked out since either the beginning of this process
176fec00eabSdrh ** or since the most recent reset.
177fec00eabSdrh */
178fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
179f7141990Sdrh   int n, mx;
180c376a198Sdrh   sqlite3_int64 res;
181f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
1827986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
183c376a198Sdrh   return res;
184fec00eabSdrh }
185fec00eabSdrh 
186fec00eabSdrh /*
187fec00eabSdrh ** Change the alarm callback
188fec00eabSdrh */
1894a27a286Sshane int sqlite3MemoryAlarm(
190fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
191fec00eabSdrh   void *pArg,
192fec00eabSdrh   sqlite3_int64 iThreshold
193fec00eabSdrh ){
194fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
195fec00eabSdrh   mem0.alarmCallback = xCallback;
196fec00eabSdrh   mem0.alarmArg = pArg;
197fec00eabSdrh   mem0.alarmThreshold = iThreshold;
198fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
199fec00eabSdrh   return SQLITE_OK;
200fec00eabSdrh }
201fec00eabSdrh 
202eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
203fec00eabSdrh /*
2044a27a286Sshane ** Deprecated external interface.  Internal/core SQLite code
2054a27a286Sshane ** should call sqlite3MemoryAlarm.
2064a27a286Sshane */
2074a27a286Sshane int sqlite3_memory_alarm(
2084a27a286Sshane   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
2094a27a286Sshane   void *pArg,
2104a27a286Sshane   sqlite3_int64 iThreshold
2114a27a286Sshane ){
2124a27a286Sshane   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
2134a27a286Sshane }
214eec556d3Sshane #endif
2154a27a286Sshane 
2164a27a286Sshane /*
217fec00eabSdrh ** Trigger the alarm
218fec00eabSdrh */
219fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
220fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
221fec00eabSdrh   sqlite3_int64 nowUsed;
222fec00eabSdrh   void *pArg;
223fec00eabSdrh   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
224fec00eabSdrh   mem0.alarmBusy = 1;
225fec00eabSdrh   xCallback = mem0.alarmCallback;
226f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
227fec00eabSdrh   pArg = mem0.alarmArg;
228fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
229fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
230fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
231fec00eabSdrh   mem0.alarmBusy = 0;
232fec00eabSdrh }
233fec00eabSdrh 
234fec00eabSdrh /*
235f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
236f7141990Sdrh ** lock is already held.
237fec00eabSdrh */
238f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
239fec00eabSdrh   int nFull;
240f7141990Sdrh   void *p;
241f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
242075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
243f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
244f7141990Sdrh   if( mem0.alarmCallback!=0 ){
245f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
246f7141990Sdrh     if( nUsed+nFull >= mem0.alarmThreshold ){
247fec00eabSdrh       sqlite3MallocAlarm(nFull);
248fec00eabSdrh     }
249f7141990Sdrh   }
250075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
251d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
252fec00eabSdrh     sqlite3MallocAlarm(nFull);
253075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
254fec00eabSdrh   }
255c702c7ccSdrh   if( p ){
256c702c7ccSdrh     nFull = sqlite3MallocSize(p);
257c702c7ccSdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
258c702c7ccSdrh   }
259f7141990Sdrh   *pp = p;
260f7141990Sdrh   return nFull;
261fec00eabSdrh }
262f7141990Sdrh 
263f7141990Sdrh /*
264f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
265f7141990Sdrh ** assumes the memory subsystem has already been initialized.
266f7141990Sdrh */
267f7141990Sdrh void *sqlite3Malloc(int n){
268f7141990Sdrh   void *p;
269*50b65684Sdrh   if( n<=0 || NEVER(n>=0x7fffff00) ){
270*50b65684Sdrh     /* The NEVER(n>=0x7fffff00) term is added out of paranoia.  We want to make
271*50b65684Sdrh     ** absolutely sure that there is nothing within SQLite that can cause a
272*50b65684Sdrh     ** memory allocation of a number of bytes which is near the maximum signed
273*50b65684Sdrh     ** integer value and thus cause an integer overflow inside of the xMalloc()
274*50b65684Sdrh     ** implementation.  The n>=0x7fffff00 gives us 255 bytes of headroom.  The
275*50b65684Sdrh     ** test should never be true because SQLITE_MAX_LENGTH should be much
276*50b65684Sdrh     ** less than 0x7fffff00 and it should catch large memory allocations
277*50b65684Sdrh     ** before they reach this point. */
278f7141990Sdrh     p = 0;
279075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
280f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
281f7141990Sdrh     mallocWithAlarm(n, &p);
282fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
283fec00eabSdrh   }else{
284075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
285fec00eabSdrh   }
286fec00eabSdrh   return p;
287fec00eabSdrh }
288fec00eabSdrh 
289fec00eabSdrh /*
290fec00eabSdrh ** This version of the memory allocation is for use by the application.
291fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
292fec00eabSdrh ** allocation.
293fec00eabSdrh */
294fec00eabSdrh void *sqlite3_malloc(int n){
295fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
296fec00eabSdrh   if( sqlite3_initialize() ) return 0;
297fec00eabSdrh #endif
298fec00eabSdrh   return sqlite3Malloc(n);
299fec00eabSdrh }
300fec00eabSdrh 
301fec00eabSdrh /*
302e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
303facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
304facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
305e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
306e5ae5735Sdrh */
307e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
308facf0307Sdrh static int scratchAllocOut = 0;
309e5ae5735Sdrh #endif
310e5ae5735Sdrh 
311e5ae5735Sdrh 
312e5ae5735Sdrh /*
313e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
314e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
315e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
316e5ae5735Sdrh ** routine is intended to get memory to old large transient data
317e5ae5735Sdrh ** structures that would not normally fit on the stack of an
318e5ae5735Sdrh ** embedded processor.
319e5ae5735Sdrh */
320facf0307Sdrh void *sqlite3ScratchMalloc(int n){
321e5ae5735Sdrh   void *p;
322e5ae5735Sdrh   assert( n>0 );
3239ac3fe97Sdrh 
324e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3259ac3fe97Sdrh   /* Verify that no more than one scratch allocation per thread
3269ac3fe97Sdrh   ** is outstanding at one time.  (This is only checked in the
3279ac3fe97Sdrh   ** single-threaded case since checking in the multi-threaded case
3289ac3fe97Sdrh   ** would be much more complicated.) */
329facf0307Sdrh   assert( scratchAllocOut==0 );
330e5ae5735Sdrh #endif
3319ac3fe97Sdrh 
332075c23afSdanielk1977   if( sqlite3GlobalConfig.szScratch<n ){
333f7141990Sdrh     goto scratch_overflow;
334f7141990Sdrh   }else{
335e5ae5735Sdrh     sqlite3_mutex_enter(mem0.mutex);
336f7141990Sdrh     if( mem0.nScratchFree==0 ){
337f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
338f7141990Sdrh       goto scratch_overflow;
339e5ae5735Sdrh     }else{
3409ac3fe97Sdrh       int i;
3419ac3fe97Sdrh       i = mem0.aScratchFree[--mem0.nScratchFree];
342075c23afSdanielk1977       i *= sqlite3GlobalConfig.szScratch;
343f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
344e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
3458183e339Sdanielk1977       sqlite3_mutex_leave(mem0.mutex);
346075c23afSdanielk1977       p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
34715301596Sshane       assert(  (((u8*)p - (u8*)0) & 7)==0 );
348e5ae5735Sdrh     }
349f7141990Sdrh   }
350f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
351f7141990Sdrh   scratchAllocOut = p!=0;
352f7141990Sdrh #endif
353f7141990Sdrh 
354f7141990Sdrh   return p;
355f7141990Sdrh 
356f7141990Sdrh scratch_overflow:
357075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
358f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
359e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
360f7141990Sdrh     n = mallocWithAlarm(n, &p);
361f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
3629ac3fe97Sdrh     sqlite3_mutex_leave(mem0.mutex);
363f7141990Sdrh   }else{
364075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
365f7141990Sdrh   }
366f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
367f7141990Sdrh   scratchAllocOut = p!=0;
368f7141990Sdrh #endif
369e5ae5735Sdrh   return p;
370e5ae5735Sdrh }
371facf0307Sdrh void sqlite3ScratchFree(void *p){
372e5ae5735Sdrh   if( p ){
3739ac3fe97Sdrh 
374e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3759ac3fe97Sdrh     /* Verify that no more than one scratch allocation per thread
3769ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3779ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3789ac3fe97Sdrh     ** would be much more complicated.) */
379facf0307Sdrh     assert( scratchAllocOut==1 );
380facf0307Sdrh     scratchAllocOut = 0;
381e5ae5735Sdrh #endif
3829ac3fe97Sdrh 
383075c23afSdanielk1977     if( sqlite3GlobalConfig.pScratch==0
384075c23afSdanielk1977            || p<sqlite3GlobalConfig.pScratch
3859ac3fe97Sdrh            || p>=(void*)mem0.aScratchFree ){
386075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
387f7141990Sdrh         int iSize = sqlite3MallocSize(p);
388f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
389f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
390f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
391075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
392f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
393f7141990Sdrh       }else{
394075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
395f7141990Sdrh       }
3969ac3fe97Sdrh     }else{
3979ac3fe97Sdrh       int i;
3981bd10f8aSdrh       i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
399075c23afSdanielk1977       i /= sqlite3GlobalConfig.szScratch;
400075c23afSdanielk1977       assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
401f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
40200e13613Sdanielk1977       assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
4039ac3fe97Sdrh       mem0.aScratchFree[mem0.nScratchFree++] = i;
404f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
4059ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
4069ac3fe97Sdrh     }
407e5ae5735Sdrh   }
408e5ae5735Sdrh }
409e5ae5735Sdrh 
410e5ae5735Sdrh /*
411f7141990Sdrh ** Allocate memory to be used by the page cache.  Make use of the
412f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
413f7141990Sdrh ** and that memory is of the right size and is not completely
414f7141990Sdrh ** consumed.  Otherwise, failover to sqlite3Malloc().
415facf0307Sdrh */
4168c0a791aSdanielk1977 #if 0
417f7141990Sdrh void *sqlite3PageMalloc(int n){
418f7141990Sdrh   void *p;
419f7141990Sdrh   assert( n>0 );
420f7141990Sdrh   assert( (n & (n-1))==0 );
421f7141990Sdrh   assert( n>=512 && n<=32768 );
422f7141990Sdrh 
423075c23afSdanielk1977   if( sqlite3GlobalConfig.szPage<n ){
424f7141990Sdrh     goto page_overflow;
425f7141990Sdrh   }else{
426f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
427f7141990Sdrh     if( mem0.nPageFree==0 ){
428f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
429f7141990Sdrh       goto page_overflow;
430f7141990Sdrh     }else{
431f7141990Sdrh       int i;
432f7141990Sdrh       i = mem0.aPageFree[--mem0.nPageFree];
433f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
434075c23afSdanielk1977       i *= sqlite3GlobalConfig.szPage;
435e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
436f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
437075c23afSdanielk1977       p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
438f7141990Sdrh     }
439f7141990Sdrh   }
440f7141990Sdrh   return p;
441f7141990Sdrh 
442f7141990Sdrh page_overflow:
443075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
444f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
445e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
446f7141990Sdrh     n = mallocWithAlarm(n, &p);
447f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
448f7141990Sdrh     sqlite3_mutex_leave(mem0.mutex);
449f7141990Sdrh   }else{
450075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
451f7141990Sdrh   }
452f7141990Sdrh   return p;
453f7141990Sdrh }
454f7141990Sdrh void sqlite3PageFree(void *p){
455f7141990Sdrh   if( p ){
456075c23afSdanielk1977     if( sqlite3GlobalConfig.pPage==0
457075c23afSdanielk1977            || p<sqlite3GlobalConfig.pPage
458f7141990Sdrh            || p>=(void*)mem0.aPageFree ){
4594b9507a0Sdanielk1977       /* In this case, the page allocation was obtained from a regular
4604b9507a0Sdanielk1977       ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
4614b9507a0Sdanielk1977       ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
4624b9507a0Sdanielk1977       */
463075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
464f7141990Sdrh         int iSize = sqlite3MallocSize(p);
465f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
466f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
467f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
468075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
469f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
470f7141990Sdrh       }else{
471075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
472f7141990Sdrh       }
473f7141990Sdrh     }else{
474075c23afSdanielk1977       /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
4754b9507a0Sdanielk1977       ** buffer. In this case all that is add the index of the page in
476075c23afSdanielk1977       ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
4774b9507a0Sdanielk1977       ** in the mem0.aPageFree[] array.
4784b9507a0Sdanielk1977       */
479f7141990Sdrh       int i;
480075c23afSdanielk1977       i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
481075c23afSdanielk1977       i /= sqlite3GlobalConfig.szPage;
482075c23afSdanielk1977       assert( i>=0 && i<sqlite3GlobalConfig.nPage );
483f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
484075c23afSdanielk1977       assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
485f7141990Sdrh       mem0.aPageFree[mem0.nPageFree++] = i;
486f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
487f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
4885f4bcf15Sdrh #if !defined(NDEBUG) && 0
4894b9507a0Sdanielk1977       /* Assert that a duplicate was not just inserted into aPageFree[]. */
4904b9507a0Sdanielk1977       for(i=0; i<mem0.nPageFree-1; i++){
4914b9507a0Sdanielk1977         assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
4924b9507a0Sdanielk1977       }
4934b9507a0Sdanielk1977 #endif
494f7141990Sdrh     }
495f7141990Sdrh   }
496facf0307Sdrh }
4978c0a791aSdanielk1977 #endif
498facf0307Sdrh 
499facf0307Sdrh /*
500633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
501633e6d57Sdrh */
5024150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
503633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
504633e6d57Sdrh   return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
505633e6d57Sdrh }
5064150ebf8Sdrh #else
5074150ebf8Sdrh #define isLookaside(A,B) 0
5084150ebf8Sdrh #endif
509633e6d57Sdrh 
510633e6d57Sdrh /*
511fec00eabSdrh ** Return the size of a memory allocation previously obtained from
512fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
513fec00eabSdrh */
514fec00eabSdrh int sqlite3MallocSize(void *p){
515075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
516fec00eabSdrh }
517633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
5186a1e071fSdrh   if( p==0 ){
5196a1e071fSdrh     return 0;
5206a1e071fSdrh   }else if( isLookaside(db, p) ){
521633e6d57Sdrh     return db->lookaside.sz;
522633e6d57Sdrh   }else{
523075c23afSdanielk1977     return sqlite3GlobalConfig.m.xSize(p);
524633e6d57Sdrh   }
525633e6d57Sdrh }
526fec00eabSdrh 
527fec00eabSdrh /*
528fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
529fec00eabSdrh */
530fec00eabSdrh void sqlite3_free(void *p){
531fec00eabSdrh   if( p==0 ) return;
532075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
533fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
534f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
535075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
536fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
537fec00eabSdrh   }else{
538075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
539fec00eabSdrh   }
540fec00eabSdrh }
541fec00eabSdrh 
542fec00eabSdrh /*
543633e6d57Sdrh ** Free memory that might be associated with a particular database
544633e6d57Sdrh ** connection.
545633e6d57Sdrh */
546633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
547633e6d57Sdrh   if( isLookaside(db, p) ){
548633e6d57Sdrh     LookasideSlot *pBuf = (LookasideSlot*)p;
549633e6d57Sdrh     pBuf->pNext = db->lookaside.pFree;
550633e6d57Sdrh     db->lookaside.pFree = pBuf;
551633e6d57Sdrh     db->lookaside.nOut--;
552633e6d57Sdrh   }else{
553633e6d57Sdrh     sqlite3_free(p);
554633e6d57Sdrh   }
555633e6d57Sdrh }
556633e6d57Sdrh 
557633e6d57Sdrh /*
558fec00eabSdrh ** Change the size of an existing memory allocation
559fec00eabSdrh */
560fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
561fec00eabSdrh   int nOld, nNew;
562fec00eabSdrh   void *pNew;
563fec00eabSdrh   if( pOld==0 ){
564fec00eabSdrh     return sqlite3Malloc(nBytes);
565fec00eabSdrh   }
566*50b65684Sdrh   if( nBytes<=0 || NEVER(nBytes>=0x7fffff00) ){
567*50b65684Sdrh     /* The NEVER(...) term is explained in comments on sqlite3Malloc() */
568fec00eabSdrh     sqlite3_free(pOld);
569fec00eabSdrh     return 0;
570fec00eabSdrh   }
571fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
572075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
573fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
574f7141990Sdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
575075c23afSdanielk1977     nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
576fec00eabSdrh     if( nOld==nNew ){
577fec00eabSdrh       pNew = pOld;
578fec00eabSdrh     }else{
579f7141990Sdrh       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
580f7141990Sdrh             mem0.alarmThreshold ){
581fec00eabSdrh         sqlite3MallocAlarm(nNew-nOld);
582fec00eabSdrh       }
583075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
584d09414cdSdanielk1977       if( pNew==0 && mem0.alarmCallback ){
585fec00eabSdrh         sqlite3MallocAlarm(nBytes);
586075c23afSdanielk1977         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
587fec00eabSdrh       }
588fec00eabSdrh       if( pNew ){
589c702c7ccSdrh         nNew = sqlite3MallocSize(pNew);
590f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
591fec00eabSdrh       }
592fec00eabSdrh     }
593fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
594fec00eabSdrh   }else{
595075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
596fec00eabSdrh   }
597fec00eabSdrh   return pNew;
598fec00eabSdrh }
599fec00eabSdrh 
600fec00eabSdrh /*
601fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
602fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
603fec00eabSdrh */
604fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
605fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
606fec00eabSdrh   if( sqlite3_initialize() ) return 0;
607fec00eabSdrh #endif
608fec00eabSdrh   return sqlite3Realloc(pOld, n);
609fec00eabSdrh }
610fec00eabSdrh 
611a3152895Sdrh 
612a3152895Sdrh /*
61317435752Sdrh ** Allocate and zero memory.
614a3152895Sdrh */
615fec00eabSdrh void *sqlite3MallocZero(int n){
616fec00eabSdrh   void *p = sqlite3Malloc(n);
617a3152895Sdrh   if( p ){
618a3152895Sdrh     memset(p, 0, n);
619a3152895Sdrh   }
620a3152895Sdrh   return p;
621a3152895Sdrh }
62217435752Sdrh 
62317435752Sdrh /*
62417435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
62517435752Sdrh ** the mallocFailed flag in the connection pointer.
62617435752Sdrh */
627fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
628a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
62917435752Sdrh   if( p ){
63017435752Sdrh     memset(p, 0, n);
63117435752Sdrh   }
63217435752Sdrh   return p;
63317435752Sdrh }
63417435752Sdrh 
63517435752Sdrh /*
63617435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
63717435752Sdrh ** the mallocFailed flag in the connection pointer.
638ddecae79Sdrh **
639ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
640ddecae79Sdrh ** failure on the same database connection) then always return 0.
641ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
642ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
643ddecae79Sdrh ** This is an important assumption.  There are many places in the
644ddecae79Sdrh ** code that do things like this:
645ddecae79Sdrh **
646ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
647ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
648ddecae79Sdrh **         if( b ) a[10] = 9;
649ddecae79Sdrh **
650ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
651ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
65217435752Sdrh */
653fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
654633e6d57Sdrh   void *p;
6554150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
656633e6d57Sdrh   if( db ){
657633e6d57Sdrh     LookasideSlot *pBuf;
658633e6d57Sdrh     if( db->mallocFailed ){
659633e6d57Sdrh       return 0;
660633e6d57Sdrh     }
661633e6d57Sdrh     if( db->lookaside.bEnabled && n<=db->lookaside.sz
662633e6d57Sdrh          && (pBuf = db->lookaside.pFree)!=0 ){
663633e6d57Sdrh       db->lookaside.pFree = pBuf->pNext;
664633e6d57Sdrh       db->lookaside.nOut++;
665633e6d57Sdrh       if( db->lookaside.nOut>db->lookaside.mxOut ){
666633e6d57Sdrh         db->lookaside.mxOut = db->lookaside.nOut;
667633e6d57Sdrh       }
668633e6d57Sdrh       return (void*)pBuf;
669633e6d57Sdrh     }
670633e6d57Sdrh   }
671ddecae79Sdrh #else
672ddecae79Sdrh   if( db && db->mallocFailed ){
673ddecae79Sdrh     return 0;
674ddecae79Sdrh   }
6754150ebf8Sdrh #endif
676fec00eabSdrh   p = sqlite3Malloc(n);
677f3a65f7eSdrh   if( !p && db ){
67817435752Sdrh     db->mallocFailed = 1;
67917435752Sdrh   }
68017435752Sdrh   return p;
68117435752Sdrh }
68217435752Sdrh 
68326783a58Sdanielk1977 /*
68426783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
68526783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
68626783a58Sdanielk1977 */
687a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
688a1644fd8Sdanielk1977   void *pNew = 0;
689a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
690633e6d57Sdrh     if( p==0 ){
691633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
692633e6d57Sdrh     }
693633e6d57Sdrh     if( isLookaside(db, p) ){
694633e6d57Sdrh       if( n<=db->lookaside.sz ){
695633e6d57Sdrh         return p;
696633e6d57Sdrh       }
697633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
698633e6d57Sdrh       if( pNew ){
699633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
700633e6d57Sdrh         sqlite3DbFree(db, p);
701633e6d57Sdrh       }
702633e6d57Sdrh     }else{
703a1644fd8Sdanielk1977       pNew = sqlite3_realloc(p, n);
704a1644fd8Sdanielk1977       if( !pNew ){
705a1644fd8Sdanielk1977         db->mallocFailed = 1;
706a1644fd8Sdanielk1977       }
707a1644fd8Sdanielk1977     }
708633e6d57Sdrh   }
709a1644fd8Sdanielk1977   return pNew;
710a1644fd8Sdanielk1977 }
711a1644fd8Sdanielk1977 
71217435752Sdrh /*
71317435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
71417435752Sdrh ** and set the mallocFailed flag in the database connection.
71517435752Sdrh */
71617435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
717a3152895Sdrh   void *pNew;
718a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
719a3152895Sdrh   if( !pNew ){
720633e6d57Sdrh     sqlite3DbFree(db, p);
721a3152895Sdrh   }
722a3152895Sdrh   return pNew;
723a3152895Sdrh }
724a3152895Sdrh 
725a3152895Sdrh /*
726a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
727a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
728a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
729a3152895Sdrh ** called via macros that record the current file and line number in the
730a3152895Sdrh ** ThreadData structure.
731a3152895Sdrh */
732633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
733a3152895Sdrh   char *zNew;
734633e6d57Sdrh   size_t n;
735633e6d57Sdrh   if( z==0 ){
736633e6d57Sdrh     return 0;
737a3152895Sdrh   }
738ea678832Sdrh   n = (db ? sqlite3Strlen(db, z) : sqlite3Strlen30(z))+1;
739633e6d57Sdrh   assert( (n&0x7fffffff)==n );
740633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
741a3152895Sdrh   if( zNew ){
742a3152895Sdrh     memcpy(zNew, z, n);
7431e536953Sdanielk1977   }
7441e536953Sdanielk1977   return zNew;
7451e536953Sdanielk1977 }
7461e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
747633e6d57Sdrh   char *zNew;
748633e6d57Sdrh   if( z==0 ){
749633e6d57Sdrh     return 0;
750633e6d57Sdrh   }
751633e6d57Sdrh   assert( (n&0x7fffffff)==n );
752633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
753633e6d57Sdrh   if( zNew ){
754633e6d57Sdrh     memcpy(zNew, z, n);
755633e6d57Sdrh     zNew[n] = 0;
7561e536953Sdanielk1977   }
7571e536953Sdanielk1977   return zNew;
7581e536953Sdanielk1977 }
7591e536953Sdanielk1977 
760a3152895Sdrh /*
761f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
762f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
763f089aa45Sdrh ** point to that string.
764a3152895Sdrh */
765f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
766a3152895Sdrh   va_list ap;
767f089aa45Sdrh   char *z;
768a3152895Sdrh 
769f089aa45Sdrh   va_start(ap, zFormat);
770f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
771a3152895Sdrh   va_end(ap);
772633e6d57Sdrh   sqlite3DbFree(db, *pz);
773f089aa45Sdrh   *pz = z;
774a3152895Sdrh }
775a3152895Sdrh 
776a3152895Sdrh 
777a3152895Sdrh /*
778a3152895Sdrh ** This function must be called before exiting any API function (i.e.
77917435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
78017435752Sdrh ** sqlite3_realloc.
781a3152895Sdrh **
782a3152895Sdrh ** The returned value is normally a copy of the second argument to this
783a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
784a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
785a3152895Sdrh **
786a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
787a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
788a3152895Sdrh ** is set to SQLITE_NOMEM.
789a3152895Sdrh */
790a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
791a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
792a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
793a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
794a1644fd8Sdanielk1977   */
795a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
79698c21903Sdanielk1977   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
797a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
79817435752Sdrh     db->mallocFailed = 0;
799a3152895Sdrh     rc = SQLITE_NOMEM;
800a3152895Sdrh   }
801a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
802a3152895Sdrh }
803