xref: /sqlite-3.40.0/src/malloc.c (revision cf014f6c)
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 /*
3631999c5cSdrh ** Default value of the hard heap limit.  0 means "no limit".
3731999c5cSdrh */
3831999c5cSdrh #ifndef SQLITE_MAX_MEMORY
3931999c5cSdrh # define SQLITE_MAX_MEMORY 0
4031999c5cSdrh #endif
4131999c5cSdrh 
4231999c5cSdrh /*
43fec00eabSdrh ** State information local to the memory allocation subsystem.
44fec00eabSdrh */
455c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
46fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
474ef299a3Sdrh   sqlite3_int64 alarmThreshold; /* The soft heap limit */
4810c0e711Sdrh   sqlite3_int64 hardLimit;      /* The hard upper bound on memory */
49fec00eabSdrh 
50fec00eabSdrh   /*
5150d1b5f3Sdrh   ** True if heap is nearly "full" where "full" is defined by the
5250d1b5f3Sdrh   ** sqlite3_soft_heap_limit() setting.
5350d1b5f3Sdrh   */
5450d1b5f3Sdrh   int nearlyFull;
5531999c5cSdrh } mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
565c8f8587Sdanielk1977 
575c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
58fec00eabSdrh 
59fec00eabSdrh /*
60af89fe66Sdrh ** Return the memory allocator mutex. sqlite3_status() needs it.
61af89fe66Sdrh */
62af89fe66Sdrh sqlite3_mutex *sqlite3MallocMutex(void){
63af89fe66Sdrh   return mem0.mutex;
64af89fe66Sdrh }
65af89fe66Sdrh 
66f82ccf64Sdrh #ifndef SQLITE_OMIT_DEPRECATED
67f82ccf64Sdrh /*
685fb72e5fSdrh ** Deprecated external interface.  It used to set an alarm callback
695fb72e5fSdrh ** that was invoked when memory usage grew too large.  Now it is a
705fb72e5fSdrh ** no-op.
71f82ccf64Sdrh */
72f82ccf64Sdrh int sqlite3_memory_alarm(
73f82ccf64Sdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
74f82ccf64Sdrh   void *pArg,
75f82ccf64Sdrh   sqlite3_int64 iThreshold
76f82ccf64Sdrh ){
775fb72e5fSdrh   (void)xCallback;
785fb72e5fSdrh   (void)pArg;
795fb72e5fSdrh   (void)iThreshold;
804ef299a3Sdrh   return SQLITE_OK;
81f82ccf64Sdrh }
82f82ccf64Sdrh #endif
83f82ccf64Sdrh 
84f82ccf64Sdrh /*
8510c0e711Sdrh ** Set the soft heap-size limit for the library.  An argument of
8610c0e711Sdrh ** zero disables the limit.  A negative argument is a no-op used to
8710c0e711Sdrh ** obtain the return value.
8810c0e711Sdrh **
8910c0e711Sdrh ** The return value is the value of the heap limit just before this
9010c0e711Sdrh ** interface was called.
9110c0e711Sdrh **
9210c0e711Sdrh ** If the hard heap limit is enabled, then the soft heap limit cannot
9310c0e711Sdrh ** be disabled nor raised above the hard heap limit.
94f82ccf64Sdrh */
95f82ccf64Sdrh sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
96f82ccf64Sdrh   sqlite3_int64 priorLimit;
975fb72e5fSdrh   sqlite3_int64 excess;
985fb72e5fSdrh   sqlite3_int64 nUsed;
99f82ccf64Sdrh #ifndef SQLITE_OMIT_AUTOINIT
100de0f1815Sdrh   int rc = sqlite3_initialize();
101de0f1815Sdrh   if( rc ) return -1;
102f82ccf64Sdrh #endif
103f82ccf64Sdrh   sqlite3_mutex_enter(mem0.mutex);
104f82ccf64Sdrh   priorLimit = mem0.alarmThreshold;
1055fb72e5fSdrh   if( n<0 ){
1064ef299a3Sdrh     sqlite3_mutex_leave(mem0.mutex);
107f82ccf64Sdrh     return priorLimit;
108f82ccf64Sdrh   }
10910c0e711Sdrh   if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
11010c0e711Sdrh     n = mem0.hardLimit;
11110c0e711Sdrh   }
1125fb72e5fSdrh   mem0.alarmThreshold = n;
1135fb72e5fSdrh   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
1145fb72e5fSdrh   mem0.nearlyFull = (n>0 && n<=nUsed);
1155fb72e5fSdrh   sqlite3_mutex_leave(mem0.mutex);
1165fb72e5fSdrh   excess = sqlite3_memory_used() - n;
1175fb72e5fSdrh   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
1185fb72e5fSdrh   return priorLimit;
1195fb72e5fSdrh }
120f82ccf64Sdrh void sqlite3_soft_heap_limit(int n){
121f82ccf64Sdrh   if( n<0 ) n = 0;
122f82ccf64Sdrh   sqlite3_soft_heap_limit64(n);
123f82ccf64Sdrh }
124f82ccf64Sdrh 
125f82ccf64Sdrh /*
12610c0e711Sdrh ** Set the hard heap-size limit for the library. An argument of zero
12710c0e711Sdrh ** disables the hard heap limit.  A negative argument is a no-op used
12810c0e711Sdrh ** to obtain the return value without affecting the hard heap limit.
12910c0e711Sdrh **
13010c0e711Sdrh ** The return value is the value of the hard heap limit just prior to
13110c0e711Sdrh ** calling this interface.
13210c0e711Sdrh **
13310c0e711Sdrh ** Setting the hard heap limit will also activate the soft heap limit
13410c0e711Sdrh ** and constrain the soft heap limit to be no more than the hard heap
13510c0e711Sdrh ** limit.
13610c0e711Sdrh */
13710c0e711Sdrh sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
13810c0e711Sdrh   sqlite3_int64 priorLimit;
13910c0e711Sdrh #ifndef SQLITE_OMIT_AUTOINIT
14010c0e711Sdrh   int rc = sqlite3_initialize();
14110c0e711Sdrh   if( rc ) return -1;
14210c0e711Sdrh #endif
14310c0e711Sdrh   sqlite3_mutex_enter(mem0.mutex);
14410c0e711Sdrh   priorLimit = mem0.hardLimit;
14510c0e711Sdrh   if( n>=0 ){
14610c0e711Sdrh     mem0.hardLimit = n;
14710c0e711Sdrh     if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
14810c0e711Sdrh       mem0.alarmThreshold = n;
14910c0e711Sdrh     }
15010c0e711Sdrh   }
15110c0e711Sdrh   sqlite3_mutex_leave(mem0.mutex);
15210c0e711Sdrh   return priorLimit;
15310c0e711Sdrh }
15410c0e711Sdrh 
15510c0e711Sdrh 
15610c0e711Sdrh /*
157fec00eabSdrh ** Initialize the memory allocation subsystem.
158fec00eabSdrh */
159fec00eabSdrh int sqlite3MallocInit(void){
160592f0cb1Sdrh   int rc;
161075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
162fec00eabSdrh     sqlite3MemSetDefault();
163fec00eabSdrh   }
164fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
16559f8c08eSdanielk1977   mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
16650d1b5f3Sdrh   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
16701c5c00cSdrh       || sqlite3GlobalConfig.nPage<=0 ){
168075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
169075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
1709ac3fe97Sdrh   }
171592f0cb1Sdrh   rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
172592f0cb1Sdrh   if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
173592f0cb1Sdrh   return rc;
174fec00eabSdrh }
175fec00eabSdrh 
176fec00eabSdrh /*
17750d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other
17850d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by
17950d1b5f3Sdrh ** sqlite3_soft_heap_limit().
18050d1b5f3Sdrh */
18150d1b5f3Sdrh int sqlite3HeapNearlyFull(void){
18250d1b5f3Sdrh   return mem0.nearlyFull;
18350d1b5f3Sdrh }
18450d1b5f3Sdrh 
18550d1b5f3Sdrh /*
186fec00eabSdrh ** Deinitialize the memory allocation subsystem.
187fec00eabSdrh */
188fec00eabSdrh void sqlite3MallocEnd(void){
1890a549071Sdanielk1977   if( sqlite3GlobalConfig.m.xShutdown ){
190075c23afSdanielk1977     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
1910a549071Sdanielk1977   }
1929ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
193fec00eabSdrh }
194fec00eabSdrh 
195fec00eabSdrh /*
196fec00eabSdrh ** Return the amount of memory currently checked out.
197fec00eabSdrh */
198fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
199df5e1a00Sdrh   sqlite3_int64 res, mx;
200df5e1a00Sdrh   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
201c376a198Sdrh   return res;
202fec00eabSdrh }
203fec00eabSdrh 
204fec00eabSdrh /*
205fec00eabSdrh ** Return the maximum amount of memory that has ever been
206fec00eabSdrh ** checked out since either the beginning of this process
207fec00eabSdrh ** or since the most recent reset.
208fec00eabSdrh */
209fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
210df5e1a00Sdrh   sqlite3_int64 res, mx;
211df5e1a00Sdrh   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
212df5e1a00Sdrh   return mx;
213fec00eabSdrh }
214fec00eabSdrh 
215fec00eabSdrh /*
2165fb72e5fSdrh ** Trigger the alarm
2175fb72e5fSdrh */
2185fb72e5fSdrh static void sqlite3MallocAlarm(int nByte){
2195fb72e5fSdrh   if( mem0.alarmThreshold<=0 ) return;
2205fb72e5fSdrh   sqlite3_mutex_leave(mem0.mutex);
2215fb72e5fSdrh   sqlite3_release_memory(nByte);
2225fb72e5fSdrh   sqlite3_mutex_enter(mem0.mutex);
2235fb72e5fSdrh }
2245fb72e5fSdrh 
2255fb72e5fSdrh /*
226f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
227f7141990Sdrh ** lock is already held.
228fec00eabSdrh */
2291d21bac8Sdrh static void mallocWithAlarm(int n, void **pp){
230f7141990Sdrh   void *p;
231087a29c7Sdrh   int nFull;
232f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
233087a29c7Sdrh   assert( n>0 );
234087a29c7Sdrh 
23540b84365Smistachkin   /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
236087a29c7Sdrh   ** implementation of malloc_good_size(), which must be called in debug
237087a29c7Sdrh   ** mode and specifically when the DMD "Dark Matter Detector" is enabled
23840b84365Smistachkin   ** or else a crash results.  Hence, do not attempt to optimize out the
23940b84365Smistachkin   ** following xRoundup() call. */
240087a29c7Sdrh   nFull = sqlite3GlobalConfig.m.xRoundup(n);
241087a29c7Sdrh 
242b02392e6Sdrh   sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
2435fb72e5fSdrh   if( mem0.alarmThreshold>0 ){
2445fb72e5fSdrh     sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2455fb72e5fSdrh     if( nUsed >= mem0.alarmThreshold - nFull ){
2465fb72e5fSdrh       mem0.nearlyFull = 1;
2475fb72e5fSdrh       sqlite3MallocAlarm(nFull);
24810c0e711Sdrh       if( mem0.hardLimit ){
24910c0e711Sdrh         nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
25010c0e711Sdrh         if( nUsed >= mem0.hardLimit - nFull ){
25110c0e711Sdrh           *pp = 0;
25210c0e711Sdrh           return;
25310c0e711Sdrh         }
25410c0e711Sdrh       }
2555fb72e5fSdrh     }else{
2565fb72e5fSdrh       mem0.nearlyFull = 0;
2575fb72e5fSdrh     }
2585fb72e5fSdrh   }
259087a29c7Sdrh   p = sqlite3GlobalConfig.m.xMalloc(nFull);
26050d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
2615fb72e5fSdrh   if( p==0 && mem0.alarmThreshold>0 ){
2625fb72e5fSdrh     sqlite3MallocAlarm(nFull);
263087a29c7Sdrh     p = sqlite3GlobalConfig.m.xMalloc(nFull);
264fec00eabSdrh   }
26550d1b5f3Sdrh #endif
266c702c7ccSdrh   if( p ){
267be7a0ceeSdrh     nFull = sqlite3MallocSize(p);
268af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
269af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
270c702c7ccSdrh   }
271f7141990Sdrh   *pp = p;
272fec00eabSdrh }
273f7141990Sdrh 
274f7141990Sdrh /*
275f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
276f7141990Sdrh ** assumes the memory subsystem has already been initialized.
277f7141990Sdrh */
278da4ca9d1Sdrh void *sqlite3Malloc(u64 n){
279f7141990Sdrh   void *p;
280da4ca9d1Sdrh   if( n==0 || n>=0x7fffff00 ){
281e08ed7e7Sdrh     /* A memory allocation of a number of bytes which is near the maximum
282e08ed7e7Sdrh     ** signed integer value might cause an integer overflow inside of the
283e08ed7e7Sdrh     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
284e08ed7e7Sdrh     ** 255 bytes of overhead.  SQLite itself will never use anything near
285e08ed7e7Sdrh     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
286f7141990Sdrh     p = 0;
287075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
288f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
2893329a63aSdrh     mallocWithAlarm((int)n, &p);
290fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
291fec00eabSdrh   }else{
292da4ca9d1Sdrh     p = sqlite3GlobalConfig.m.xMalloc((int)n);
293fec00eabSdrh   }
2948da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
295fec00eabSdrh   return p;
296fec00eabSdrh }
297fec00eabSdrh 
298fec00eabSdrh /*
299fec00eabSdrh ** This version of the memory allocation is for use by the application.
300fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
301fec00eabSdrh ** allocation.
302fec00eabSdrh */
303fec00eabSdrh void *sqlite3_malloc(int n){
304fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
305fec00eabSdrh   if( sqlite3_initialize() ) return 0;
306fec00eabSdrh #endif
307da4ca9d1Sdrh   return n<=0 ? 0 : sqlite3Malloc(n);
308da4ca9d1Sdrh }
309da4ca9d1Sdrh void *sqlite3_malloc64(sqlite3_uint64 n){
310da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
311da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
312da4ca9d1Sdrh #endif
313fec00eabSdrh   return sqlite3Malloc(n);
314fec00eabSdrh }
315fec00eabSdrh 
316fec00eabSdrh /*
317633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
318633e6d57Sdrh */
3194150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
320633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
321ac536e61Sdrh   return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
322633e6d57Sdrh }
3234150ebf8Sdrh #else
3244150ebf8Sdrh #define isLookaside(A,B) 0
3254150ebf8Sdrh #endif
326633e6d57Sdrh 
327633e6d57Sdrh /*
328fec00eabSdrh ** Return the size of a memory allocation previously obtained from
329fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
330fec00eabSdrh */
331fec00eabSdrh int sqlite3MallocSize(void *p){
332107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
333075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
334fec00eabSdrh }
335115d663cSnumist static int lookasideMallocSize(sqlite3 *db, void *p){
336*cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
337*cf014f6cSdrh   return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
338115d663cSnumist #else
339115d663cSnumist   return db->lookaside.szTrue;
340115d663cSnumist #endif
341115d663cSnumist }
342633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
343039ca6abSdrh   assert( p!=0 );
344d879e3ebSdrh #ifdef SQLITE_DEBUG
345e6068027Sdrh   if( db==0 || !isLookaside(db,p) ){
34617bcb102Sdrh     if( db==0 ){
347d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
348d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
349633e6d57Sdrh     }else{
350d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
351d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
352633e6d57Sdrh     }
353633e6d57Sdrh   }
354e6068027Sdrh #endif
355e6068027Sdrh   if( db ){
356e6068027Sdrh     if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
357*cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
358e6068027Sdrh       if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
359e6068027Sdrh         assert( sqlite3_mutex_held(db->mutex) );
360*cf014f6cSdrh         return LOOKASIDE_SMALL;
361e6068027Sdrh       }
362e6068027Sdrh #endif
363e6068027Sdrh       if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
364e6068027Sdrh         assert( sqlite3_mutex_held(db->mutex) );
365e6068027Sdrh         return db->lookaside.szTrue;
366e6068027Sdrh       }
367e6068027Sdrh     }
368e6068027Sdrh   }
369e6068027Sdrh   return sqlite3GlobalConfig.m.xSize(p);
37017bcb102Sdrh }
371da4ca9d1Sdrh sqlite3_uint64 sqlite3_msize(void *p){
372d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
373d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
374039ca6abSdrh   return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
375da4ca9d1Sdrh }
376fec00eabSdrh 
377fec00eabSdrh /*
378fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
379fec00eabSdrh */
380fec00eabSdrh void sqlite3_free(void *p){
38171a1a0f4Sdrh   if( p==0 ) return;  /* IMP: R-49053-54554 */
382107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
383d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
384075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
385fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
386af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
387af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
388075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
389fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
390fec00eabSdrh   }else{
391075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
392fec00eabSdrh   }
393fec00eabSdrh }
394fec00eabSdrh 
395fec00eabSdrh /*
396b4586f12Sdrh ** Add the size of memory allocation "p" to the count in
397b4586f12Sdrh ** *db->pnBytesFreed.
398b4586f12Sdrh */
399b4586f12Sdrh static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
40056d90be1Sdrh   *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
401b4586f12Sdrh }
402b4586f12Sdrh 
403b4586f12Sdrh /*
404633e6d57Sdrh ** Free memory that might be associated with a particular database
405dbd6a7dcSdrh ** connection.  Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
406dbd6a7dcSdrh ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
407633e6d57Sdrh */
408dbd6a7dcSdrh void sqlite3DbFreeNN(sqlite3 *db, void *p){
4097047e25cSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
410dbd6a7dcSdrh   assert( p!=0 );
411174b9a16Sdrh   if( db ){
412174b9a16Sdrh     if( db->pnBytesFreed ){
413b4586f12Sdrh       measureAllocationSize(db, p);
414174b9a16Sdrh       return;
415d46def77Sdan     }
416e6068027Sdrh     if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
417*cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
418e6068027Sdrh       if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
419e6068027Sdrh         LookasideSlot *pBuf = (LookasideSlot*)p;
420115d663cSnumist #ifdef SQLITE_DEBUG
421*cf014f6cSdrh         memset(p, 0xaa, LOOKASIDE_SMALL);  /* Trash freed content */
422115d663cSnumist #endif
423*cf014f6cSdrh         pBuf->pNext = db->lookaside.pSmallFree;
424*cf014f6cSdrh         db->lookaside.pSmallFree = pBuf;
425115d663cSnumist         return;
426115d663cSnumist       }
427*cf014f6cSdrh #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
428e6068027Sdrh       if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
429e6068027Sdrh         LookasideSlot *pBuf = (LookasideSlot*)p;
430d879e3ebSdrh #ifdef SQLITE_DEBUG
431e6068027Sdrh         memset(p, 0xaa, db->lookaside.szTrue);  /* Trash freed content */
4323608f177Sdrh #endif
433633e6d57Sdrh         pBuf->pNext = db->lookaside.pFree;
434633e6d57Sdrh         db->lookaside.pFree = pBuf;
435174b9a16Sdrh         return;
436174b9a16Sdrh       }
437174b9a16Sdrh     }
438e6068027Sdrh   }
439d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
440d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
441174b9a16Sdrh   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
442107b56e8Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
443633e6d57Sdrh   sqlite3_free(p);
444633e6d57Sdrh }
445dbd6a7dcSdrh void sqlite3DbFree(sqlite3 *db, void *p){
446dbd6a7dcSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
447dbd6a7dcSdrh   if( p ) sqlite3DbFreeNN(db, p);
448dbd6a7dcSdrh }
449633e6d57Sdrh 
450633e6d57Sdrh /*
451fec00eabSdrh ** Change the size of an existing memory allocation
452fec00eabSdrh */
453da4ca9d1Sdrh void *sqlite3Realloc(void *pOld, u64 nBytes){
454ca591febSshaneh   int nOld, nNew, nDiff;
455fec00eabSdrh   void *pNew;
456d231aa3aSdrh   assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
457d425864dSmistachkin   assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
458fec00eabSdrh   if( pOld==0 ){
4598da47419Sdrh     return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
460fec00eabSdrh   }
461da4ca9d1Sdrh   if( nBytes==0 ){
4628da47419Sdrh     sqlite3_free(pOld); /* IMP: R-26507-47431 */
463fec00eabSdrh     return 0;
464fec00eabSdrh   }
465b6063cf8Sdrh   if( nBytes>=0x7fffff00 ){
466b6063cf8Sdrh     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
467b6063cf8Sdrh     return 0;
468b6063cf8Sdrh   }
469fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
4709f129f46Sdrh   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
4719f129f46Sdrh   ** argument to xRealloc is always a value returned by a prior call to
4729f129f46Sdrh   ** xRoundup. */
473da4ca9d1Sdrh   nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
474fec00eabSdrh   if( nOld==nNew ){
475fec00eabSdrh     pNew = pOld;
4767c6791c8Sdrh   }else if( sqlite3GlobalConfig.bMemstat ){
4777c6791c8Sdrh     sqlite3_mutex_enter(mem0.mutex);
478b02392e6Sdrh     sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
4798e1bb041Sdrh     nDiff = nNew - nOld;
4801aa34695Sdrh     if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
4815fb72e5fSdrh           mem0.alarmThreshold-nDiff ){
4825fb72e5fSdrh       sqlite3MallocAlarm(nDiff);
4835fb72e5fSdrh     }
484075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
4855fb72e5fSdrh     if( pNew==0 && mem0.alarmThreshold>0 ){
4865fb72e5fSdrh       sqlite3MallocAlarm((int)nBytes);
487075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
488fec00eabSdrh     }
489fec00eabSdrh     if( pNew ){
490c702c7ccSdrh       nNew = sqlite3MallocSize(pNew);
491af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
492fec00eabSdrh     }
493fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
494fec00eabSdrh   }else{
4957c6791c8Sdrh     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
496fec00eabSdrh   }
4978da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
498fec00eabSdrh   return pNew;
499fec00eabSdrh }
500fec00eabSdrh 
501fec00eabSdrh /*
502fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
503fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
504fec00eabSdrh */
505fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
506fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
507fec00eabSdrh   if( sqlite3_initialize() ) return 0;
508fec00eabSdrh #endif
5098da47419Sdrh   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
510da4ca9d1Sdrh   return sqlite3Realloc(pOld, n);
511da4ca9d1Sdrh }
512da4ca9d1Sdrh void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
513da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
514da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
515da4ca9d1Sdrh #endif
516fec00eabSdrh   return sqlite3Realloc(pOld, n);
517fec00eabSdrh }
518fec00eabSdrh 
519a3152895Sdrh 
520a3152895Sdrh /*
52117435752Sdrh ** Allocate and zero memory.
522a3152895Sdrh */
523da4ca9d1Sdrh void *sqlite3MallocZero(u64 n){
524fec00eabSdrh   void *p = sqlite3Malloc(n);
525a3152895Sdrh   if( p ){
52620f3df04Sdrh     memset(p, 0, (size_t)n);
527a3152895Sdrh   }
528a3152895Sdrh   return p;
529a3152895Sdrh }
53017435752Sdrh 
53117435752Sdrh /*
53217435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
53317435752Sdrh ** the mallocFailed flag in the connection pointer.
53417435752Sdrh */
535da4ca9d1Sdrh void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
536575fad65Sdrh   void *p;
537575fad65Sdrh   testcase( db==0 );
538575fad65Sdrh   p = sqlite3DbMallocRaw(db, n);
539575fad65Sdrh   if( p ) memset(p, 0, (size_t)n);
540575fad65Sdrh   return p;
54117435752Sdrh }
542575fad65Sdrh 
543575fad65Sdrh 
544575fad65Sdrh /* Finish the work of sqlite3DbMallocRawNN for the unusual and
545575fad65Sdrh ** slower case when the allocation cannot be fulfilled using lookaside.
546575fad65Sdrh */
547575fad65Sdrh static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
548575fad65Sdrh   void *p;
549575fad65Sdrh   assert( db!=0 );
550575fad65Sdrh   p = sqlite3Malloc(n);
551575fad65Sdrh   if( !p ) sqlite3OomFault(db);
552575fad65Sdrh   sqlite3MemdebugSetType(p,
553575fad65Sdrh          (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
55417435752Sdrh   return p;
55517435752Sdrh }
55617435752Sdrh 
55717435752Sdrh /*
5581da26a48Sdrh ** Allocate memory, either lookaside (if possible) or heap.
5591da26a48Sdrh ** If the allocation fails, set the mallocFailed flag in
5601da26a48Sdrh ** the connection pointer.
561ddecae79Sdrh **
562ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
563ddecae79Sdrh ** failure on the same database connection) then always return 0.
564ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
565ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
566ddecae79Sdrh ** This is an important assumption.  There are many places in the
567ddecae79Sdrh ** code that do things like this:
568ddecae79Sdrh **
569ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
570ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
571ddecae79Sdrh **         if( b ) a[10] = 9;
572ddecae79Sdrh **
573ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
574ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
575575fad65Sdrh **
576575fad65Sdrh ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
577575fad65Sdrh ** not a NULL pointer.
57817435752Sdrh */
579da4ca9d1Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
580575fad65Sdrh   void *p;
581575fad65Sdrh   if( db ) return sqlite3DbMallocRawNN(db, n);
582575fad65Sdrh   p = sqlite3Malloc(n);
583575fad65Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
584575fad65Sdrh   return p;
585575fad65Sdrh }
586575fad65Sdrh void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
587f5818aa5Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
588f5818aa5Sdrh   LookasideSlot *pBuf;
589575fad65Sdrh   assert( db!=0 );
590575fad65Sdrh   assert( sqlite3_mutex_held(db->mutex) );
591575fad65Sdrh   assert( db->pnBytesFreed==0 );
5920b12e7f8Sdrh   if( n>db->lookaside.sz ){
5931b47c9c9Snumist     if( !db->lookaside.bDisable ){
5940b12e7f8Sdrh       db->lookaside.anStat[1]++;
5951b47c9c9Snumist     }else if( db->mallocFailed ){
5961b47c9c9Snumist       return 0;
5971b47c9c9Snumist     }
598c947d6a4Snumist     return dbMallocRawFinish(db, n);
599c947d6a4Snumist   }
600*cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
601*cf014f6cSdrh   if( n<=LOOKASIDE_SMALL ){
602*cf014f6cSdrh     if( (pBuf = db->lookaside.pSmallFree)!=0 ){
603*cf014f6cSdrh       db->lookaside.pSmallFree = pBuf->pNext;
604115d663cSnumist       db->lookaside.anStat[0]++;
605115d663cSnumist       return (void*)pBuf;
606*cf014f6cSdrh     }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
607*cf014f6cSdrh       db->lookaside.pSmallInit = pBuf->pNext;
608115d663cSnumist       db->lookaside.anStat[0]++;
609115d663cSnumist       return (void*)pBuf;
610115d663cSnumist     }
611c947d6a4Snumist   }
612115d663cSnumist #endif
613c947d6a4Snumist   if( (pBuf = db->lookaside.pFree)!=0 ){
614633e6d57Sdrh     db->lookaside.pFree = pBuf->pNext;
6150b12e7f8Sdrh     db->lookaside.anStat[0]++;
616633e6d57Sdrh     return (void*)pBuf;
61752fb8e19Sdrh   }else if( (pBuf = db->lookaside.pInit)!=0 ){
61852fb8e19Sdrh     db->lookaside.pInit = pBuf->pNext;
61952fb8e19Sdrh     db->lookaside.anStat[0]++;
62052fb8e19Sdrh     return (void*)pBuf;
62152fb8e19Sdrh   }else{
62252fb8e19Sdrh     db->lookaside.anStat[2]++;
623633e6d57Sdrh   }
624ddecae79Sdrh #else
625f5818aa5Sdrh   assert( db!=0 );
626f5818aa5Sdrh   assert( sqlite3_mutex_held(db->mutex) );
627f5818aa5Sdrh   assert( db->pnBytesFreed==0 );
628575fad65Sdrh   if( db->mallocFailed ){
629ddecae79Sdrh     return 0;
630ddecae79Sdrh   }
6314150ebf8Sdrh #endif
6321da26a48Sdrh   return dbMallocRawFinish(db, n);
6331da26a48Sdrh }
63417435752Sdrh 
635b84e574cSdrh /* Forward declaration */
636b84e574cSdrh static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
637b84e574cSdrh 
63826783a58Sdanielk1977 /*
63926783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
64026783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
64126783a58Sdanielk1977 */
642da4ca9d1Sdrh void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
643b84e574cSdrh   assert( db!=0 );
644575fad65Sdrh   if( p==0 ) return sqlite3DbMallocRawNN(db, n);
645b84e574cSdrh   assert( sqlite3_mutex_held(db->mutex) );
646e6068027Sdrh   if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
647*cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
648e6068027Sdrh     if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
649*cf014f6cSdrh       if( n<=LOOKASIDE_SMALL ) return p;
650e6068027Sdrh     }else
651e6068027Sdrh #endif
652e6068027Sdrh     if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
653e6068027Sdrh       if( n<=db->lookaside.szTrue ) return p;
654e6068027Sdrh     }
655e6068027Sdrh   }
656b84e574cSdrh   return dbReallocFinish(db, p, n);
657b84e574cSdrh }
658b84e574cSdrh static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
659a1644fd8Sdanielk1977   void *pNew = 0;
660d9da78a2Sdrh   assert( db!=0 );
661b84e574cSdrh   assert( p!=0 );
662a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
663633e6d57Sdrh     if( isLookaside(db, p) ){
664575fad65Sdrh       pNew = sqlite3DbMallocRawNN(db, n);
665633e6d57Sdrh       if( pNew ){
666115d663cSnumist         memcpy(pNew, p, lookasideMallocSize(db, p));
667633e6d57Sdrh         sqlite3DbFree(db, p);
668633e6d57Sdrh       }
669633e6d57Sdrh     }else{
670d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
671d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
672107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
6733329a63aSdrh       pNew = sqlite3_realloc64(p, n);
674a1644fd8Sdanielk1977       if( !pNew ){
6754a642b60Sdrh         sqlite3OomFault(db);
676a1644fd8Sdanielk1977       }
677d231aa3aSdrh       sqlite3MemdebugSetType(pNew,
6784a642b60Sdrh             (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
679a1644fd8Sdanielk1977     }
680633e6d57Sdrh   }
681a1644fd8Sdanielk1977   return pNew;
682a1644fd8Sdanielk1977 }
683a1644fd8Sdanielk1977 
68417435752Sdrh /*
68517435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
68617435752Sdrh ** and set the mallocFailed flag in the database connection.
68717435752Sdrh */
688da4ca9d1Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
689a3152895Sdrh   void *pNew;
690a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
691a3152895Sdrh   if( !pNew ){
692633e6d57Sdrh     sqlite3DbFree(db, p);
693a3152895Sdrh   }
694a3152895Sdrh   return pNew;
695a3152895Sdrh }
696a3152895Sdrh 
697a3152895Sdrh /*
698a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
699a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
700a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
701a3152895Sdrh ** called via macros that record the current file and line number in the
702a3152895Sdrh ** ThreadData structure.
703a3152895Sdrh */
704633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
705a3152895Sdrh   char *zNew;
706633e6d57Sdrh   size_t n;
707633e6d57Sdrh   if( z==0 ){
708633e6d57Sdrh     return 0;
709a3152895Sdrh   }
710cee11adaSdrh   n = strlen(z) + 1;
711cee11adaSdrh   zNew = sqlite3DbMallocRaw(db, n);
712a3152895Sdrh   if( zNew ){
713a3152895Sdrh     memcpy(zNew, z, n);
7141e536953Sdanielk1977   }
7151e536953Sdanielk1977   return zNew;
7161e536953Sdanielk1977 }
717da4ca9d1Sdrh char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
718633e6d57Sdrh   char *zNew;
719575fad65Sdrh   assert( db!=0 );
720633e6d57Sdrh   if( z==0 ){
721633e6d57Sdrh     return 0;
722633e6d57Sdrh   }
723633e6d57Sdrh   assert( (n&0x7fffffff)==n );
724575fad65Sdrh   zNew = sqlite3DbMallocRawNN(db, n+1);
725633e6d57Sdrh   if( zNew ){
72620f3df04Sdrh     memcpy(zNew, z, (size_t)n);
727633e6d57Sdrh     zNew[n] = 0;
7281e536953Sdanielk1977   }
7291e536953Sdanielk1977   return zNew;
7301e536953Sdanielk1977 }
7311e536953Sdanielk1977 
732a3152895Sdrh /*
7339b2e0435Sdrh ** The text between zStart and zEnd represents a phrase within a larger
7349b2e0435Sdrh ** SQL statement.  Make a copy of this phrase in space obtained form
7359b2e0435Sdrh ** sqlite3DbMalloc().  Omit leading and trailing whitespace.
7369b2e0435Sdrh */
7379b2e0435Sdrh char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
7389b2e0435Sdrh   int n;
7399b2e0435Sdrh   while( sqlite3Isspace(zStart[0]) ) zStart++;
7409b2e0435Sdrh   n = (int)(zEnd - zStart);
741e75d1f52Sdrh   while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
7429b2e0435Sdrh   return sqlite3DbStrNDup(db, zStart, n);
7439b2e0435Sdrh }
7449b2e0435Sdrh 
7459b2e0435Sdrh /*
74622c17b8bSdrh ** Free any prior content in *pz and replace it with a copy of zNew.
747a3152895Sdrh */
74822c17b8bSdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
749633e6d57Sdrh   sqlite3DbFree(db, *pz);
75022c17b8bSdrh   *pz = sqlite3DbStrDup(db, zNew);
751a3152895Sdrh }
752a3152895Sdrh 
753b50c65d5Sdrh /*
7544a642b60Sdrh ** Call this routine to record the fact that an OOM (out-of-memory) error
7554a642b60Sdrh ** has happened.  This routine will set db->mallocFailed, and also
7564a642b60Sdrh ** temporarily disable the lookaside memory allocator and interrupt
7574a642b60Sdrh ** any running VDBEs.
7584a642b60Sdrh */
7594a642b60Sdrh void sqlite3OomFault(sqlite3 *db){
7604a642b60Sdrh   if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
7614a642b60Sdrh     db->mallocFailed = 1;
7624a642b60Sdrh     if( db->nVdbeExec>0 ){
7634a642b60Sdrh       db->u1.isInterrupted = 1;
7644a642b60Sdrh     }
76531f69626Sdrh     DisableLookaside;
7661cf19758Sdrh     if( db->pParse ){
7671cf19758Sdrh       db->pParse->rc = SQLITE_NOMEM_BKPT;
7681cf19758Sdrh     }
7694a642b60Sdrh   }
7704a642b60Sdrh }
7714a642b60Sdrh 
7724a642b60Sdrh /*
7734a642b60Sdrh ** This routine reactivates the memory allocator and clears the
7744a642b60Sdrh ** db->mallocFailed flag as necessary.
7754a642b60Sdrh **
7764a642b60Sdrh ** The memory allocator is not restarted if there are running
7774a642b60Sdrh ** VDBEs.
7784a642b60Sdrh */
7794a642b60Sdrh void sqlite3OomClear(sqlite3 *db){
7804a642b60Sdrh   if( db->mallocFailed && db->nVdbeExec==0 ){
7814a642b60Sdrh     db->mallocFailed = 0;
7824a642b60Sdrh     db->u1.isInterrupted = 0;
7834a642b60Sdrh     assert( db->lookaside.bDisable>0 );
78431f69626Sdrh     EnableLookaside;
7854a642b60Sdrh   }
7864a642b60Sdrh }
7874a642b60Sdrh 
7884a642b60Sdrh /*
789b50c65d5Sdrh ** Take actions at the end of an API call to indicate an OOM error
790b50c65d5Sdrh */
791b50c65d5Sdrh static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
7924a642b60Sdrh   sqlite3OomClear(db);
793b50c65d5Sdrh   sqlite3Error(db, SQLITE_NOMEM);
794fad3039cSmistachkin   return SQLITE_NOMEM_BKPT;
795b50c65d5Sdrh }
796a3152895Sdrh 
797a3152895Sdrh /*
798a3152895Sdrh ** This function must be called before exiting any API function (i.e.
79917435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
80017435752Sdrh ** sqlite3_realloc.
801a3152895Sdrh **
802a3152895Sdrh ** The returned value is normally a copy of the second argument to this
803be217793Sshane ** function. However, if a malloc() failure has occurred since the previous
804a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
805a3152895Sdrh **
806597d2b64Sdrh ** If an OOM as occurred, then the connection error-code (the value
807597d2b64Sdrh ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
808a3152895Sdrh */
809a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
810597d2b64Sdrh   /* If the db handle must hold the connection handle mutex here.
811597d2b64Sdrh   ** Otherwise the read (and possible write) of db->mallocFailed
812a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
813a1644fd8Sdanielk1977   */
814597d2b64Sdrh   assert( db!=0 );
815597d2b64Sdrh   assert( sqlite3_mutex_held(db->mutex) );
816b50c65d5Sdrh   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
817b50c65d5Sdrh     return apiOomError(db);
818a3152895Sdrh   }
819b50c65d5Sdrh   return rc & db->errMask;
820a3152895Sdrh }
821