blob: 053b57b47fed330dd92470279eb9f1dcb0615077 [file] [log] [blame]
drha3152892007-05-05 11:48:52 +00001/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
drhfec00ea2008-06-14 16:56:21 +000012**
drha3152892007-05-05 11:48:52 +000013** Memory allocation functions used throughout sqlite.
drha3152892007-05-05 11:48:52 +000014*/
15#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000016#include <stdarg.h>
drha3152892007-05-05 11:48:52 +000017
18/*
danielk197784680242008-06-23 11:11:35 +000019** Attempt to release up to n bytes of non-essential memory currently
20** held by SQLite. An example of non-essential memory is memory used to
21** cache database pages that are not currently in use.
drha3152892007-05-05 11:48:52 +000022*/
23int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +000024#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh9f129f42010-08-31 15:27:32 +000025 return sqlite3PcacheReleaseMemory(n);
danielk19771e536952007-08-16 10:09:01 +000026#else
drh9f129f42010-08-31 15:27:32 +000027 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
28 ** is a no-op returning zero if SQLite is not compiled with
29 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
danielk197762c14b32008-11-19 09:05:26 +000030 UNUSED_PARAMETER(n);
drh9f129f42010-08-31 15:27:32 +000031 return 0;
danielk19771e536952007-08-16 10:09:01 +000032#endif
drha3152892007-05-05 11:48:52 +000033}
drha3152892007-05-05 11:48:52 +000034
drhfec00ea2008-06-14 16:56:21 +000035/*
drhbadc9802010-08-27 17:16:44 +000036** An instance of the following object records the location of
37** each unused scratch buffer.
38*/
39typedef struct ScratchFreeslot {
40 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
41} ScratchFreeslot;
42
43/*
drhfec00ea2008-06-14 16:56:21 +000044** State information local to the memory allocation subsystem.
45*/
danielk19775c8f8582008-09-02 10:22:00 +000046static SQLITE_WSD struct Mem0Global {
drhfec00ea2008-06-14 16:56:21 +000047 sqlite3_mutex *mutex; /* Mutex to serialize access */
drh5fb72e52015-09-10 01:22:09 +000048 sqlite3_int64 alarmThreshold; /* The soft heap limit */
drhfec00ea2008-06-14 16:56:21 +000049
50 /*
drhbadc9802010-08-27 17:16:44 +000051 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
52 ** (so that a range test can be used to determine if an allocation
53 ** being freed came from pScratch) and a pointer to the list of
54 ** unused scratch allocations.
drh9ac3fe92008-06-18 18:12:04 +000055 */
drhbadc9802010-08-27 17:16:44 +000056 void *pScratchEnd;
57 ScratchFreeslot *pScratchFree;
58 u32 nScratchFree;
drh50d1b5f2010-08-27 12:21:06 +000059
60 /*
61 ** True if heap is nearly "full" where "full" is defined by the
62 ** sqlite3_soft_heap_limit() setting.
63 */
64 int nearlyFull;
drh4ef299a2015-09-02 14:56:56 +000065} mem0 = { 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +000066
67#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +000068
69/*
drhaf89fe62015-03-23 17:25:18 +000070** Return the memory allocator mutex. sqlite3_status() needs it.
71*/
72sqlite3_mutex *sqlite3MallocMutex(void){
73 return mem0.mutex;
74}
75
drhf82ccf62010-09-15 17:54:31 +000076#ifndef SQLITE_OMIT_DEPRECATED
77/*
drh5fb72e52015-09-10 01:22:09 +000078** Deprecated external interface. It used to set an alarm callback
79** that was invoked when memory usage grew too large. Now it is a
80** no-op.
drhf82ccf62010-09-15 17:54:31 +000081*/
82int sqlite3_memory_alarm(
83 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
84 void *pArg,
85 sqlite3_int64 iThreshold
86){
drh5fb72e52015-09-10 01:22:09 +000087 (void)xCallback;
88 (void)pArg;
89 (void)iThreshold;
drh4ef299a2015-09-02 14:56:56 +000090 return SQLITE_OK;
drhf82ccf62010-09-15 17:54:31 +000091}
92#endif
93
94/*
95** Set the soft heap-size limit for the library. Passing a zero or
96** negative value indicates no limit.
97*/
98sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
99 sqlite3_int64 priorLimit;
drh5fb72e52015-09-10 01:22:09 +0000100 sqlite3_int64 excess;
101 sqlite3_int64 nUsed;
drhf82ccf62010-09-15 17:54:31 +0000102#ifndef SQLITE_OMIT_AUTOINIT
drhde0f1812011-12-22 17:10:35 +0000103 int rc = sqlite3_initialize();
104 if( rc ) return -1;
drhf82ccf62010-09-15 17:54:31 +0000105#endif
106 sqlite3_mutex_enter(mem0.mutex);
107 priorLimit = mem0.alarmThreshold;
drh5fb72e52015-09-10 01:22:09 +0000108 if( n<0 ){
109 sqlite3_mutex_leave(mem0.mutex);
110 return priorLimit;
drhf82ccf62010-09-15 17:54:31 +0000111 }
drh5fb72e52015-09-10 01:22:09 +0000112 mem0.alarmThreshold = n;
113 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
114 mem0.nearlyFull = (n>0 && n<=nUsed);
drh4ef299a2015-09-02 14:56:56 +0000115 sqlite3_mutex_leave(mem0.mutex);
drh5fb72e52015-09-10 01:22:09 +0000116 excess = sqlite3_memory_used() - n;
117 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
drhf82ccf62010-09-15 17:54:31 +0000118 return priorLimit;
119}
120void sqlite3_soft_heap_limit(int n){
121 if( n<0 ) n = 0;
122 sqlite3_soft_heap_limit64(n);
123}
124
125/*
drhfec00ea2008-06-14 16:56:21 +0000126** Initialize the memory allocation subsystem.
127*/
128int sqlite3MallocInit(void){
drh592f0cb2015-03-26 17:04:23 +0000129 int rc;
danielk1977075c23a2008-09-01 18:34:20 +0000130 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000131 sqlite3MemSetDefault();
132 }
133 memset(&mem0, 0, sizeof(mem0));
drh59a05232015-10-15 17:21:35 +0000134 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
danielk1977075c23a2008-09-01 18:34:20 +0000135 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000136 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000137 int i, n, sz;
138 ScratchFreeslot *pSlot;
139 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
140 sqlite3GlobalConfig.szScratch = sz;
141 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
142 n = sqlite3GlobalConfig.nScratch;
143 mem0.pScratchFree = pSlot;
144 mem0.nScratchFree = n;
145 for(i=0; i<n-1; i++){
146 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
147 pSlot = pSlot->pNext;
148 }
149 pSlot->pNext = 0;
150 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000151 }else{
drhbadc9802010-08-27 17:16:44 +0000152 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000153 sqlite3GlobalConfig.pScratch = 0;
154 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000155 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000156 }
drh50d1b5f2010-08-27 12:21:06 +0000157 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000158 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000159 sqlite3GlobalConfig.pPage = 0;
160 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000161 }
drh592f0cb2015-03-26 17:04:23 +0000162 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
163 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
164 return rc;
drhfec00ea2008-06-14 16:56:21 +0000165}
166
167/*
drh50d1b5f2010-08-27 12:21:06 +0000168** Return true if the heap is currently under memory pressure - in other
169** words if the amount of heap used is close to the limit set by
170** sqlite3_soft_heap_limit().
171*/
172int sqlite3HeapNearlyFull(void){
173 return mem0.nearlyFull;
174}
175
176/*
drhfec00ea2008-06-14 16:56:21 +0000177** Deinitialize the memory allocation subsystem.
178*/
179void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000180 if( sqlite3GlobalConfig.m.xShutdown ){
181 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
182 }
drh9ac3fe92008-06-18 18:12:04 +0000183 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000184}
185
186/*
187** Return the amount of memory currently checked out.
188*/
189sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000190 sqlite3_int64 res, mx;
191 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000192 return res;
drhfec00ea2008-06-14 16:56:21 +0000193}
194
195/*
196** Return the maximum amount of memory that has ever been
197** checked out since either the beginning of this process
198** or since the most recent reset.
199*/
200sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000201 sqlite3_int64 res, mx;
202 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
203 return mx;
drhfec00ea2008-06-14 16:56:21 +0000204}
205
206/*
drh5fb72e52015-09-10 01:22:09 +0000207** Trigger the alarm
208*/
209static void sqlite3MallocAlarm(int nByte){
210 if( mem0.alarmThreshold<=0 ) return;
211 sqlite3_mutex_leave(mem0.mutex);
212 sqlite3_release_memory(nByte);
213 sqlite3_mutex_enter(mem0.mutex);
214}
215
216/*
drhf7141992008-06-19 00:16:08 +0000217** Do a memory allocation with statistics and alarms. Assume the
218** lock is already held.
219*/
drh1d21bac2017-01-10 16:09:46 +0000220static void mallocWithAlarm(int n, void **pp){
drhf7141992008-06-19 00:16:08 +0000221 void *p;
222 assert( sqlite3_mutex_held(mem0.mutex) );
drhb02392e2015-10-15 15:28:56 +0000223 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
drh5fb72e52015-09-10 01:22:09 +0000224 if( mem0.alarmThreshold>0 ){
225 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drh1d21bac2017-01-10 16:09:46 +0000226 int nFull = sqlite3GlobalConfig.m.xRoundup(n);
drh5fb72e52015-09-10 01:22:09 +0000227 if( nUsed >= mem0.alarmThreshold - nFull ){
228 mem0.nearlyFull = 1;
229 sqlite3MallocAlarm(nFull);
230 }else{
231 mem0.nearlyFull = 0;
232 }
233 }
drh1d21bac2017-01-10 16:09:46 +0000234 p = sqlite3GlobalConfig.m.xMalloc(n);
drh50d1b5f2010-08-27 12:21:06 +0000235#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000236 if( p==0 && mem0.alarmThreshold>0 ){
237 sqlite3MallocAlarm(nFull);
drh1d21bac2017-01-10 16:09:46 +0000238 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000239 }
drh50d1b5f2010-08-27 12:21:06 +0000240#endif
drhc702c7c2008-07-18 18:56:16 +0000241 if( p ){
drh1d21bac2017-01-10 16:09:46 +0000242 int nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000243 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
244 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000245 }
drhf7141992008-06-19 00:16:08 +0000246 *pp = p;
drhf7141992008-06-19 00:16:08 +0000247}
drhfec00ea2008-06-14 16:56:21 +0000248
249/*
250** Allocate memory. This routine is like sqlite3_malloc() except that it
251** assumes the memory subsystem has already been initialized.
252*/
drhda4ca9d2014-09-09 17:27:35 +0000253void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000254 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000255 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000256 /* A memory allocation of a number of bytes which is near the maximum
257 ** signed integer value might cause an integer overflow inside of the
258 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
259 ** 255 bytes of overhead. SQLite itself will never use anything near
260 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000261 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000262 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000263 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000264 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000265 sqlite3_mutex_leave(mem0.mutex);
266 }else{
drhda4ca9d2014-09-09 17:27:35 +0000267 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000268 }
drh8da47412014-10-03 14:54:47 +0000269 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000270 return p;
271}
272
273/*
274** This version of the memory allocation is for use by the application.
275** First make sure the memory subsystem is initialized, then do the
276** allocation.
277*/
278void *sqlite3_malloc(int n){
279#ifndef SQLITE_OMIT_AUTOINIT
280 if( sqlite3_initialize() ) return 0;
281#endif
drhda4ca9d2014-09-09 17:27:35 +0000282 return n<=0 ? 0 : sqlite3Malloc(n);
283}
284void *sqlite3_malloc64(sqlite3_uint64 n){
285#ifndef SQLITE_OMIT_AUTOINIT
286 if( sqlite3_initialize() ) return 0;
287#endif
drhfec00ea2008-06-14 16:56:21 +0000288 return sqlite3Malloc(n);
289}
290
291/*
drhe5ae5732008-06-15 02:51:47 +0000292** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000293** xScratchMalloc(). We verify this constraint in the single-threaded
294** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000295** is outstanding clearing it when the allocation is freed.
296*/
297#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000298static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000299#endif
300
301
302/*
303** Allocate memory that is to be used and released right away.
304** This routine is similar to alloca() in that it is not intended
305** for situations where the memory might be held long-term. This
306** routine is intended to get memory to old large transient data
307** structures that would not normally fit on the stack of an
308** embedded processor.
309*/
drhfacf0302008-06-17 15:12:00 +0000310void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000311 void *p;
312 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000313
drhbadc9802010-08-27 17:16:44 +0000314 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000315 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000316 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
317 p = mem0.pScratchFree;
318 mem0.pScratchFree = mem0.pScratchFree->pNext;
319 mem0.nScratchFree--;
drhaf89fe62015-03-23 17:25:18 +0000320 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000321 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000322 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000323 sqlite3_mutex_leave(mem0.mutex);
324 p = sqlite3Malloc(n);
325 if( sqlite3GlobalConfig.bMemstat && p ){
326 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000327 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000328 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000329 }
330 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
331 }
drh1ff6e3a2010-09-02 17:15:19 +0000332 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000333
drhbadc9802010-08-27 17:16:44 +0000334
drhe5ae5732008-06-15 02:51:47 +0000335#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhcbd55b02014-11-04 14:22:27 +0000336 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
337 ** buffers per thread.
338 **
339 ** This can only be checked in single-threaded mode.
340 */
341 assert( scratchAllocOut==0 );
drhbadc9802010-08-27 17:16:44 +0000342 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000343#endif
344
drhe5ae5732008-06-15 02:51:47 +0000345 return p;
346}
drhfacf0302008-06-17 15:12:00 +0000347void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000348 if( p ){
drhbadc9802010-08-27 17:16:44 +0000349
350#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
351 /* Verify that no more than two scratch allocation per thread
352 ** is outstanding at one time. (This is only checked in the
353 ** single-threaded case since checking in the multi-threaded case
354 ** would be much more complicated.) */
355 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
356 scratchAllocOut--;
357#endif
358
drhac536e62015-12-10 15:09:17 +0000359 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
drhbadc9802010-08-27 17:16:44 +0000360 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
361 ScratchFreeslot *pSlot;
362 pSlot = (ScratchFreeslot*)p;
363 sqlite3_mutex_enter(mem0.mutex);
364 pSlot->pNext = mem0.pScratchFree;
365 mem0.pScratchFree = pSlot;
366 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000367 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhaf89fe62015-03-23 17:25:18 +0000368 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
drhbadc9802010-08-27 17:16:44 +0000369 sqlite3_mutex_leave(mem0.mutex);
370 }else{
371 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000372 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
mistachkind4258642015-03-21 23:38:59 +0000373 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000374 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000375 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000376 int iSize = sqlite3MallocSize(p);
377 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000378 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
379 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
380 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000381 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000382 sqlite3_mutex_leave(mem0.mutex);
383 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000384 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000385 }
drh9ac3fe92008-06-18 18:12:04 +0000386 }
drhe5ae5732008-06-15 02:51:47 +0000387 }
388}
389
390/*
drh633e6d52008-07-28 19:34:53 +0000391** TRUE if p is a lookaside memory allocation from db
392*/
drh4150ebf2008-10-11 15:38:29 +0000393#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000394static int isLookaside(sqlite3 *db, void *p){
drhac536e62015-12-10 15:09:17 +0000395 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
drh633e6d52008-07-28 19:34:53 +0000396}
drh4150ebf2008-10-11 15:38:29 +0000397#else
398#define isLookaside(A,B) 0
399#endif
drh633e6d52008-07-28 19:34:53 +0000400
401/*
drhfec00ea2008-06-14 16:56:21 +0000402** Return the size of a memory allocation previously obtained from
403** sqlite3Malloc() or sqlite3_malloc().
404*/
405int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000406 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000407 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000408}
drh633e6d52008-07-28 19:34:53 +0000409int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh039ca6a2015-10-15 16:20:57 +0000410 assert( p!=0 );
drh054bbab2015-09-01 20:09:33 +0000411 if( db==0 || !isLookaside(db,p) ){
412#if SQLITE_DEBUG
413 if( db==0 ){
414 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
415 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000416 }else{
drhd231aa32014-10-07 15:46:54 +0000417 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000418 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000419 }
drh054bbab2015-09-01 20:09:33 +0000420#endif
421 return sqlite3GlobalConfig.m.xSize(p);
422 }else{
423 assert( sqlite3_mutex_held(db->mutex) );
424 return db->lookaside.sz;
drh633e6d52008-07-28 19:34:53 +0000425 }
426}
drhda4ca9d2014-09-09 17:27:35 +0000427sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000428 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000429 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh039ca6a2015-10-15 16:20:57 +0000430 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
drhda4ca9d2014-09-09 17:27:35 +0000431}
drhfec00ea2008-06-14 16:56:21 +0000432
433/*
434** Free memory previously obtained from sqlite3Malloc().
435*/
436void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000437 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000438 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000439 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000440 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000441 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000442 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
443 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000444 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000445 sqlite3_mutex_leave(mem0.mutex);
446 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000447 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000448 }
449}
450
451/*
drhb4586f12014-08-23 19:42:06 +0000452** Add the size of memory allocation "p" to the count in
453** *db->pnBytesFreed.
454*/
455static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
drh56d90be2015-10-26 12:55:56 +0000456 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
drhb4586f12014-08-23 19:42:06 +0000457}
458
459/*
drh633e6d52008-07-28 19:34:53 +0000460** Free memory that might be associated with a particular database
461** connection.
462*/
463void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000464 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000465 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000466 if( db ){
467 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000468 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000469 return;
dand46def72010-07-24 11:28:28 +0000470 }
drh174b9a12010-07-26 11:07:20 +0000471 if( isLookaside(db, p) ){
472 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000473#if SQLITE_DEBUG
474 /* Trash all content in the buffer being freed */
475 memset(p, 0xaa, db->lookaside.sz);
476#endif
drh174b9a12010-07-26 11:07:20 +0000477 pBuf->pNext = db->lookaside.pFree;
478 db->lookaside.pFree = pBuf;
479 db->lookaside.nOut--;
480 return;
481 }
drh633e6d52008-07-28 19:34:53 +0000482 }
drhd231aa32014-10-07 15:46:54 +0000483 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000484 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000485 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
486 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
487 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000488}
489
490/*
drhfec00ea2008-06-14 16:56:21 +0000491** Change the size of an existing memory allocation
492*/
drhda4ca9d2014-09-09 17:27:35 +0000493void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000494 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000495 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000496 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000497 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000498 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000499 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000500 }
drhda4ca9d2014-09-09 17:27:35 +0000501 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000502 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000503 return 0;
504 }
drhb6063cf2009-06-27 00:48:33 +0000505 if( nBytes>=0x7fffff00 ){
506 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
507 return 0;
508 }
drhfec00ea2008-06-14 16:56:21 +0000509 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000510 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
511 ** argument to xRealloc is always a value returned by a prior call to
512 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000513 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000514 if( nOld==nNew ){
515 pNew = pOld;
516 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000517 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000518 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000519 nDiff = nNew - nOld;
drh1aa34692016-12-27 12:08:36 +0000520 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
drh5fb72e52015-09-10 01:22:09 +0000521 mem0.alarmThreshold-nDiff ){
522 sqlite3MallocAlarm(nDiff);
523 }
drh7c6791c2009-08-18 14:48:53 +0000524 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh5fb72e52015-09-10 01:22:09 +0000525 if( pNew==0 && mem0.alarmThreshold>0 ){
526 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000527 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000528 }
529 if( pNew ){
530 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000531 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000532 }
533 sqlite3_mutex_leave(mem0.mutex);
534 }else{
drh7c6791c2009-08-18 14:48:53 +0000535 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000536 }
drh8da47412014-10-03 14:54:47 +0000537 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000538 return pNew;
539}
540
541/*
542** The public interface to sqlite3Realloc. Make sure that the memory
543** subsystem is initialized prior to invoking sqliteRealloc.
544*/
545void *sqlite3_realloc(void *pOld, int n){
546#ifndef SQLITE_OMIT_AUTOINIT
547 if( sqlite3_initialize() ) return 0;
548#endif
drh8da47412014-10-03 14:54:47 +0000549 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000550 return sqlite3Realloc(pOld, n);
551}
552void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
553#ifndef SQLITE_OMIT_AUTOINIT
554 if( sqlite3_initialize() ) return 0;
555#endif
drhfec00ea2008-06-14 16:56:21 +0000556 return sqlite3Realloc(pOld, n);
557}
558
drha3152892007-05-05 11:48:52 +0000559
560/*
drh17435752007-08-16 04:30:38 +0000561** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000562*/
drhda4ca9d2014-09-09 17:27:35 +0000563void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000564 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000565 if( p ){
drh20f3df02014-09-18 02:20:54 +0000566 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000567 }
568 return p;
569}
drh17435752007-08-16 04:30:38 +0000570
571/*
572** Allocate and zero memory. If the allocation fails, make
573** the mallocFailed flag in the connection pointer.
574*/
drhda4ca9d2014-09-09 17:27:35 +0000575void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000576 void *p;
577 testcase( db==0 );
578 p = sqlite3DbMallocRaw(db, n);
579 if( p ) memset(p, 0, (size_t)n);
580 return p;
581}
582
583
584/* Finish the work of sqlite3DbMallocRawNN for the unusual and
585** slower case when the allocation cannot be fulfilled using lookaside.
586*/
587static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
588 void *p;
589 assert( db!=0 );
590 p = sqlite3Malloc(n);
591 if( !p ) sqlite3OomFault(db);
592 sqlite3MemdebugSetType(p,
593 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000594 return p;
595}
596
597/*
drh1da26a42016-01-20 03:36:32 +0000598** Allocate memory, either lookaside (if possible) or heap.
599** If the allocation fails, set the mallocFailed flag in
600** the connection pointer.
drhddecae72008-10-11 17:35:16 +0000601**
602** If db!=0 and db->mallocFailed is true (indicating a prior malloc
603** failure on the same database connection) then always return 0.
604** Hence for a particular database connection, once malloc starts
605** failing, it fails consistently until mallocFailed is reset.
606** This is an important assumption. There are many places in the
607** code that do things like this:
608**
609** int *a = (int*)sqlite3DbMallocRaw(db, 100);
610** int *b = (int*)sqlite3DbMallocRaw(db, 200);
611** if( b ) a[10] = 9;
612**
613** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
614** that all prior mallocs (ex: "a") worked too.
drh575fad62016-02-05 13:38:36 +0000615**
616** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
617** not a NULL pointer.
drh17435752007-08-16 04:30:38 +0000618*/
drhda4ca9d2014-09-09 17:27:35 +0000619void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000620 void *p;
621 if( db ) return sqlite3DbMallocRawNN(db, n);
622 p = sqlite3Malloc(n);
623 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
624 return p;
625}
626void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
drhf5818aa2016-02-06 19:48:50 +0000627#ifndef SQLITE_OMIT_LOOKASIDE
628 LookasideSlot *pBuf;
drh575fad62016-02-05 13:38:36 +0000629 assert( db!=0 );
630 assert( sqlite3_mutex_held(db->mutex) );
631 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000632 if( db->lookaside.bDisable==0 ){
633 assert( db->mallocFailed==0 );
634 if( n>db->lookaside.sz ){
635 db->lookaside.anStat[1]++;
636 }else if( (pBuf = db->lookaside.pFree)==0 ){
637 db->lookaside.anStat[2]++;
638 }else{
639 db->lookaside.pFree = pBuf->pNext;
640 db->lookaside.nOut++;
641 db->lookaside.anStat[0]++;
642 if( db->lookaside.nOut>db->lookaside.mxOut ){
643 db->lookaside.mxOut = db->lookaside.nOut;
drh633e6d52008-07-28 19:34:53 +0000644 }
drh575fad62016-02-05 13:38:36 +0000645 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000646 }
drh575fad62016-02-05 13:38:36 +0000647 }else if( db->mallocFailed ){
648 return 0;
drh633e6d52008-07-28 19:34:53 +0000649 }
drhddecae72008-10-11 17:35:16 +0000650#else
drhf5818aa2016-02-06 19:48:50 +0000651 assert( db!=0 );
652 assert( sqlite3_mutex_held(db->mutex) );
653 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000654 if( db->mallocFailed ){
drhddecae72008-10-11 17:35:16 +0000655 return 0;
656 }
drh4150ebf2008-10-11 15:38:29 +0000657#endif
drh1da26a42016-01-20 03:36:32 +0000658 return dbMallocRawFinish(db, n);
659}
drh17435752007-08-16 04:30:38 +0000660
drhb84e5742016-02-05 02:42:54 +0000661/* Forward declaration */
662static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
663
danielk197726783a52007-08-29 14:06:22 +0000664/*
665** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000666** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000667*/
drhda4ca9d2014-09-09 17:27:35 +0000668void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
drhb84e5742016-02-05 02:42:54 +0000669 assert( db!=0 );
drh575fad62016-02-05 13:38:36 +0000670 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
drhb84e5742016-02-05 02:42:54 +0000671 assert( sqlite3_mutex_held(db->mutex) );
672 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
673 return dbReallocFinish(db, p, n);
674}
675static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000676 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000677 assert( db!=0 );
drhb84e5742016-02-05 02:42:54 +0000678 assert( p!=0 );
danielk1977a1644fd2007-08-29 12:31:25 +0000679 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000680 if( isLookaside(db, p) ){
drh575fad62016-02-05 13:38:36 +0000681 pNew = sqlite3DbMallocRawNN(db, n);
drh633e6d52008-07-28 19:34:53 +0000682 if( pNew ){
683 memcpy(pNew, p, db->lookaside.sz);
684 sqlite3DbFree(db, p);
685 }
686 }else{
drhd231aa32014-10-07 15:46:54 +0000687 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000688 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000689 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000690 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000691 if( !pNew ){
drh4a642b62016-02-05 01:55:27 +0000692 sqlite3OomFault(db);
drh633e6d52008-07-28 19:34:53 +0000693 }
drhd231aa32014-10-07 15:46:54 +0000694 sqlite3MemdebugSetType(pNew,
drh4a642b62016-02-05 01:55:27 +0000695 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000696 }
697 }
698 return pNew;
699}
700
drh17435752007-08-16 04:30:38 +0000701/*
702** Attempt to reallocate p. If the reallocation fails, then free p
703** and set the mallocFailed flag in the database connection.
704*/
drhda4ca9d2014-09-09 17:27:35 +0000705void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000706 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000707 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000708 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000709 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000710 }
711 return pNew;
712}
713
drha3152892007-05-05 11:48:52 +0000714/*
715** Make a copy of a string in memory obtained from sqliteMalloc(). These
716** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
717** is because when memory debugging is turned on, these two functions are
718** called via macros that record the current file and line number in the
719** ThreadData structure.
720*/
drh633e6d52008-07-28 19:34:53 +0000721char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000722 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000723 size_t n;
724 if( z==0 ){
725 return 0;
726 }
drhcee11ad2016-10-17 00:48:06 +0000727 n = strlen(z) + 1;
728 zNew = sqlite3DbMallocRaw(db, n);
drha3152892007-05-05 11:48:52 +0000729 if( zNew ){
730 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000731 }
732 return zNew;
733}
drhda4ca9d2014-09-09 17:27:35 +0000734char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000735 char *zNew;
drh575fad62016-02-05 13:38:36 +0000736 assert( db!=0 );
drh633e6d52008-07-28 19:34:53 +0000737 if( z==0 ){
738 return 0;
739 }
740 assert( (n&0x7fffffff)==n );
drh575fad62016-02-05 13:38:36 +0000741 zNew = sqlite3DbMallocRawNN(db, n+1);
drh633e6d52008-07-28 19:34:53 +0000742 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000743 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000744 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000745 }
746 return zNew;
747}
748
drha3152892007-05-05 11:48:52 +0000749/*
drh22c17b82015-05-15 04:13:15 +0000750** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000751*/
drh22c17b82015-05-15 04:13:15 +0000752void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000753 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000754 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000755}
756
drhb50c65d2014-08-23 20:25:53 +0000757/*
drh4a642b62016-02-05 01:55:27 +0000758** Call this routine to record the fact that an OOM (out-of-memory) error
759** has happened. This routine will set db->mallocFailed, and also
760** temporarily disable the lookaside memory allocator and interrupt
761** any running VDBEs.
762*/
763void sqlite3OomFault(sqlite3 *db){
764 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
765 db->mallocFailed = 1;
766 if( db->nVdbeExec>0 ){
767 db->u1.isInterrupted = 1;
768 }
769 db->lookaside.bDisable++;
770 }
771}
772
773/*
774** This routine reactivates the memory allocator and clears the
775** db->mallocFailed flag as necessary.
776**
777** The memory allocator is not restarted if there are running
778** VDBEs.
779*/
780void sqlite3OomClear(sqlite3 *db){
781 if( db->mallocFailed && db->nVdbeExec==0 ){
782 db->mallocFailed = 0;
783 db->u1.isInterrupted = 0;
784 assert( db->lookaside.bDisable>0 );
785 db->lookaside.bDisable--;
786 }
787}
788
789/*
drhb50c65d2014-08-23 20:25:53 +0000790** Take actions at the end of an API call to indicate an OOM error
791*/
792static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
drh4a642b62016-02-05 01:55:27 +0000793 sqlite3OomClear(db);
drhb50c65d2014-08-23 20:25:53 +0000794 sqlite3Error(db, SQLITE_NOMEM);
mistachkinfad30392016-02-13 23:43:46 +0000795 return SQLITE_NOMEM_BKPT;
drhb50c65d2014-08-23 20:25:53 +0000796}
drha3152892007-05-05 11:48:52 +0000797
798/*
799** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000800** returning control to the user) that has called sqlite3_malloc or
801** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000802**
803** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000804** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000805** invocation SQLITE_NOMEM is returned instead.
806**
drh597d2b62015-06-30 03:13:47 +0000807** If an OOM as occurred, then the connection error-code (the value
808** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000809*/
drha3152892007-05-05 11:48:52 +0000810int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000811 /* If the db handle must hold the connection handle mutex here.
812 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000813 ** is unsafe, as is the call to sqlite3Error().
814 */
drh597d2b62015-06-30 03:13:47 +0000815 assert( db!=0 );
816 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000817 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
818 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000819 }
drhb50c65d2014-08-23 20:25:53 +0000820 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000821}