blob: 0a3d891e9ba1b3cefc25a1060c80bcf972569bce [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;
drhbe7a0ce2017-01-13 12:53:35 +0000222 int nFull = 0;
drhf7141992008-06-19 00:16:08 +0000223 assert( sqlite3_mutex_held(mem0.mutex) );
drhb02392e2015-10-15 15:28:56 +0000224 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
drh5fb72e52015-09-10 01:22:09 +0000225 if( mem0.alarmThreshold>0 ){
226 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhbe7a0ce2017-01-13 12:53:35 +0000227 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drh5fb72e52015-09-10 01:22:09 +0000228 if( nUsed >= mem0.alarmThreshold - nFull ){
229 mem0.nearlyFull = 1;
230 sqlite3MallocAlarm(nFull);
231 }else{
232 mem0.nearlyFull = 0;
233 }
234 }
drh1d21bac2017-01-10 16:09:46 +0000235 p = sqlite3GlobalConfig.m.xMalloc(n);
drh50d1b5f2010-08-27 12:21:06 +0000236#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000237 if( p==0 && mem0.alarmThreshold>0 ){
238 sqlite3MallocAlarm(nFull);
drh1d21bac2017-01-10 16:09:46 +0000239 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000240 }
drh50d1b5f2010-08-27 12:21:06 +0000241#endif
drhc702c7c2008-07-18 18:56:16 +0000242 if( p ){
drhbe7a0ce2017-01-13 12:53:35 +0000243 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000244 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
245 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000246 }
drhf7141992008-06-19 00:16:08 +0000247 *pp = p;
drhf7141992008-06-19 00:16:08 +0000248}
drhfec00ea2008-06-14 16:56:21 +0000249
250/*
251** Allocate memory. This routine is like sqlite3_malloc() except that it
252** assumes the memory subsystem has already been initialized.
253*/
drhda4ca9d2014-09-09 17:27:35 +0000254void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000255 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000256 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000257 /* A memory allocation of a number of bytes which is near the maximum
258 ** signed integer value might cause an integer overflow inside of the
259 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
260 ** 255 bytes of overhead. SQLite itself will never use anything near
261 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000262 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000263 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000264 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000265 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000266 sqlite3_mutex_leave(mem0.mutex);
267 }else{
drhda4ca9d2014-09-09 17:27:35 +0000268 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000269 }
drh8da47412014-10-03 14:54:47 +0000270 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000271 return p;
272}
273
274/*
275** This version of the memory allocation is for use by the application.
276** First make sure the memory subsystem is initialized, then do the
277** allocation.
278*/
279void *sqlite3_malloc(int n){
280#ifndef SQLITE_OMIT_AUTOINIT
281 if( sqlite3_initialize() ) return 0;
282#endif
drhda4ca9d2014-09-09 17:27:35 +0000283 return n<=0 ? 0 : sqlite3Malloc(n);
284}
285void *sqlite3_malloc64(sqlite3_uint64 n){
286#ifndef SQLITE_OMIT_AUTOINIT
287 if( sqlite3_initialize() ) return 0;
288#endif
drhfec00ea2008-06-14 16:56:21 +0000289 return sqlite3Malloc(n);
290}
291
292/*
drhe5ae5732008-06-15 02:51:47 +0000293** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000294** xScratchMalloc(). We verify this constraint in the single-threaded
295** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000296** is outstanding clearing it when the allocation is freed.
297*/
298#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000299static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000300#endif
301
302
303/*
304** Allocate memory that is to be used and released right away.
305** This routine is similar to alloca() in that it is not intended
306** for situations where the memory might be held long-term. This
307** routine is intended to get memory to old large transient data
308** structures that would not normally fit on the stack of an
309** embedded processor.
310*/
drhfacf0302008-06-17 15:12:00 +0000311void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000312 void *p;
313 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000314
drhbadc9802010-08-27 17:16:44 +0000315 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000316 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000317 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
318 p = mem0.pScratchFree;
319 mem0.pScratchFree = mem0.pScratchFree->pNext;
320 mem0.nScratchFree--;
drhaf89fe62015-03-23 17:25:18 +0000321 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000322 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000323 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000324 sqlite3_mutex_leave(mem0.mutex);
325 p = sqlite3Malloc(n);
326 if( sqlite3GlobalConfig.bMemstat && p ){
327 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000328 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000329 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000330 }
331 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
332 }
drh1ff6e3a2010-09-02 17:15:19 +0000333 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000334
drhbadc9802010-08-27 17:16:44 +0000335
drhe5ae5732008-06-15 02:51:47 +0000336#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhcbd55b02014-11-04 14:22:27 +0000337 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
338 ** buffers per thread.
339 **
340 ** This can only be checked in single-threaded mode.
341 */
342 assert( scratchAllocOut==0 );
drhbadc9802010-08-27 17:16:44 +0000343 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000344#endif
345
drhe5ae5732008-06-15 02:51:47 +0000346 return p;
347}
drhfacf0302008-06-17 15:12:00 +0000348void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000349 if( p ){
drhbadc9802010-08-27 17:16:44 +0000350
351#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
352 /* Verify that no more than two scratch allocation per thread
353 ** is outstanding at one time. (This is only checked in the
354 ** single-threaded case since checking in the multi-threaded case
355 ** would be much more complicated.) */
356 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
357 scratchAllocOut--;
358#endif
359
drhac536e62015-12-10 15:09:17 +0000360 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
drhbadc9802010-08-27 17:16:44 +0000361 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
362 ScratchFreeslot *pSlot;
363 pSlot = (ScratchFreeslot*)p;
364 sqlite3_mutex_enter(mem0.mutex);
365 pSlot->pNext = mem0.pScratchFree;
366 mem0.pScratchFree = pSlot;
367 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000368 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhaf89fe62015-03-23 17:25:18 +0000369 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
drhbadc9802010-08-27 17:16:44 +0000370 sqlite3_mutex_leave(mem0.mutex);
371 }else{
372 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000373 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
mistachkind4258642015-03-21 23:38:59 +0000374 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000375 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000376 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000377 int iSize = sqlite3MallocSize(p);
378 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000379 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
380 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
381 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000382 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000383 sqlite3_mutex_leave(mem0.mutex);
384 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000385 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000386 }
drh9ac3fe92008-06-18 18:12:04 +0000387 }
drhe5ae5732008-06-15 02:51:47 +0000388 }
389}
390
391/*
drh633e6d52008-07-28 19:34:53 +0000392** TRUE if p is a lookaside memory allocation from db
393*/
drh4150ebf2008-10-11 15:38:29 +0000394#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000395static int isLookaside(sqlite3 *db, void *p){
drhac536e62015-12-10 15:09:17 +0000396 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
drh633e6d52008-07-28 19:34:53 +0000397}
drh4150ebf2008-10-11 15:38:29 +0000398#else
399#define isLookaside(A,B) 0
400#endif
drh633e6d52008-07-28 19:34:53 +0000401
402/*
drhfec00ea2008-06-14 16:56:21 +0000403** Return the size of a memory allocation previously obtained from
404** sqlite3Malloc() or sqlite3_malloc().
405*/
406int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000407 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000408 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000409}
drh633e6d52008-07-28 19:34:53 +0000410int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh039ca6a2015-10-15 16:20:57 +0000411 assert( p!=0 );
drh054bbab2015-09-01 20:09:33 +0000412 if( db==0 || !isLookaside(db,p) ){
413#if SQLITE_DEBUG
414 if( db==0 ){
415 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
416 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000417 }else{
drhd231aa32014-10-07 15:46:54 +0000418 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000419 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000420 }
drh054bbab2015-09-01 20:09:33 +0000421#endif
422 return sqlite3GlobalConfig.m.xSize(p);
423 }else{
424 assert( sqlite3_mutex_held(db->mutex) );
425 return db->lookaside.sz;
drh633e6d52008-07-28 19:34:53 +0000426 }
427}
drhda4ca9d2014-09-09 17:27:35 +0000428sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000429 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000430 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh039ca6a2015-10-15 16:20:57 +0000431 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
drhda4ca9d2014-09-09 17:27:35 +0000432}
drhfec00ea2008-06-14 16:56:21 +0000433
434/*
435** Free memory previously obtained from sqlite3Malloc().
436*/
437void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000438 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000439 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000440 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000441 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000442 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000443 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
444 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000445 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000446 sqlite3_mutex_leave(mem0.mutex);
447 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000448 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000449 }
450}
451
452/*
drhb4586f12014-08-23 19:42:06 +0000453** Add the size of memory allocation "p" to the count in
454** *db->pnBytesFreed.
455*/
456static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
drh56d90be2015-10-26 12:55:56 +0000457 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
drhb4586f12014-08-23 19:42:06 +0000458}
459
460/*
drh633e6d52008-07-28 19:34:53 +0000461** Free memory that might be associated with a particular database
462** connection.
463*/
464void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000465 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000466 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000467 if( db ){
468 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000469 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000470 return;
dand46def72010-07-24 11:28:28 +0000471 }
drh174b9a12010-07-26 11:07:20 +0000472 if( isLookaside(db, p) ){
473 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000474#if SQLITE_DEBUG
475 /* Trash all content in the buffer being freed */
476 memset(p, 0xaa, db->lookaside.sz);
477#endif
drh174b9a12010-07-26 11:07:20 +0000478 pBuf->pNext = db->lookaside.pFree;
479 db->lookaside.pFree = pBuf;
480 db->lookaside.nOut--;
481 return;
482 }
drh633e6d52008-07-28 19:34:53 +0000483 }
drhd231aa32014-10-07 15:46:54 +0000484 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000485 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000486 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
487 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
488 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000489}
490
491/*
drhfec00ea2008-06-14 16:56:21 +0000492** Change the size of an existing memory allocation
493*/
drhda4ca9d2014-09-09 17:27:35 +0000494void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000495 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000496 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000497 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000498 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000499 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000500 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000501 }
drhda4ca9d2014-09-09 17:27:35 +0000502 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000503 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000504 return 0;
505 }
drhb6063cf2009-06-27 00:48:33 +0000506 if( nBytes>=0x7fffff00 ){
507 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
508 return 0;
509 }
drhfec00ea2008-06-14 16:56:21 +0000510 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000511 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
512 ** argument to xRealloc is always a value returned by a prior call to
513 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000514 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000515 if( nOld==nNew ){
516 pNew = pOld;
517 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000518 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000519 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000520 nDiff = nNew - nOld;
drh1aa34692016-12-27 12:08:36 +0000521 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
drh5fb72e52015-09-10 01:22:09 +0000522 mem0.alarmThreshold-nDiff ){
523 sqlite3MallocAlarm(nDiff);
524 }
drh7c6791c2009-08-18 14:48:53 +0000525 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh5fb72e52015-09-10 01:22:09 +0000526 if( pNew==0 && mem0.alarmThreshold>0 ){
527 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000528 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000529 }
530 if( pNew ){
531 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000532 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000533 }
534 sqlite3_mutex_leave(mem0.mutex);
535 }else{
drh7c6791c2009-08-18 14:48:53 +0000536 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000537 }
drh8da47412014-10-03 14:54:47 +0000538 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000539 return pNew;
540}
541
542/*
543** The public interface to sqlite3Realloc. Make sure that the memory
544** subsystem is initialized prior to invoking sqliteRealloc.
545*/
546void *sqlite3_realloc(void *pOld, int n){
547#ifndef SQLITE_OMIT_AUTOINIT
548 if( sqlite3_initialize() ) return 0;
549#endif
drh8da47412014-10-03 14:54:47 +0000550 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000551 return sqlite3Realloc(pOld, n);
552}
553void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
554#ifndef SQLITE_OMIT_AUTOINIT
555 if( sqlite3_initialize() ) return 0;
556#endif
drhfec00ea2008-06-14 16:56:21 +0000557 return sqlite3Realloc(pOld, n);
558}
559
drha3152892007-05-05 11:48:52 +0000560
561/*
drh17435752007-08-16 04:30:38 +0000562** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000563*/
drhda4ca9d2014-09-09 17:27:35 +0000564void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000565 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000566 if( p ){
drh20f3df02014-09-18 02:20:54 +0000567 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000568 }
569 return p;
570}
drh17435752007-08-16 04:30:38 +0000571
572/*
573** Allocate and zero memory. If the allocation fails, make
574** the mallocFailed flag in the connection pointer.
575*/
drhda4ca9d2014-09-09 17:27:35 +0000576void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000577 void *p;
578 testcase( db==0 );
579 p = sqlite3DbMallocRaw(db, n);
580 if( p ) memset(p, 0, (size_t)n);
581 return p;
582}
583
584
585/* Finish the work of sqlite3DbMallocRawNN for the unusual and
586** slower case when the allocation cannot be fulfilled using lookaside.
587*/
588static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
589 void *p;
590 assert( db!=0 );
591 p = sqlite3Malloc(n);
592 if( !p ) sqlite3OomFault(db);
593 sqlite3MemdebugSetType(p,
594 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000595 return p;
596}
597
598/*
drh1da26a42016-01-20 03:36:32 +0000599** Allocate memory, either lookaside (if possible) or heap.
600** If the allocation fails, set the mallocFailed flag in
601** the connection pointer.
drhddecae72008-10-11 17:35:16 +0000602**
603** If db!=0 and db->mallocFailed is true (indicating a prior malloc
604** failure on the same database connection) then always return 0.
605** Hence for a particular database connection, once malloc starts
606** failing, it fails consistently until mallocFailed is reset.
607** This is an important assumption. There are many places in the
608** code that do things like this:
609**
610** int *a = (int*)sqlite3DbMallocRaw(db, 100);
611** int *b = (int*)sqlite3DbMallocRaw(db, 200);
612** if( b ) a[10] = 9;
613**
614** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
615** that all prior mallocs (ex: "a") worked too.
drh575fad62016-02-05 13:38:36 +0000616**
617** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
618** not a NULL pointer.
drh17435752007-08-16 04:30:38 +0000619*/
drhda4ca9d2014-09-09 17:27:35 +0000620void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000621 void *p;
622 if( db ) return sqlite3DbMallocRawNN(db, n);
623 p = sqlite3Malloc(n);
624 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
625 return p;
626}
627void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
drhf5818aa2016-02-06 19:48:50 +0000628#ifndef SQLITE_OMIT_LOOKASIDE
629 LookasideSlot *pBuf;
drh575fad62016-02-05 13:38:36 +0000630 assert( db!=0 );
631 assert( sqlite3_mutex_held(db->mutex) );
632 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000633 if( db->lookaside.bDisable==0 ){
634 assert( db->mallocFailed==0 );
635 if( n>db->lookaside.sz ){
636 db->lookaside.anStat[1]++;
637 }else if( (pBuf = db->lookaside.pFree)==0 ){
638 db->lookaside.anStat[2]++;
639 }else{
640 db->lookaside.pFree = pBuf->pNext;
641 db->lookaside.nOut++;
642 db->lookaside.anStat[0]++;
643 if( db->lookaside.nOut>db->lookaside.mxOut ){
644 db->lookaside.mxOut = db->lookaside.nOut;
drh633e6d52008-07-28 19:34:53 +0000645 }
drh575fad62016-02-05 13:38:36 +0000646 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000647 }
drh575fad62016-02-05 13:38:36 +0000648 }else if( db->mallocFailed ){
649 return 0;
drh633e6d52008-07-28 19:34:53 +0000650 }
drhddecae72008-10-11 17:35:16 +0000651#else
drhf5818aa2016-02-06 19:48:50 +0000652 assert( db!=0 );
653 assert( sqlite3_mutex_held(db->mutex) );
654 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000655 if( db->mallocFailed ){
drhddecae72008-10-11 17:35:16 +0000656 return 0;
657 }
drh4150ebf2008-10-11 15:38:29 +0000658#endif
drh1da26a42016-01-20 03:36:32 +0000659 return dbMallocRawFinish(db, n);
660}
drh17435752007-08-16 04:30:38 +0000661
drhb84e5742016-02-05 02:42:54 +0000662/* Forward declaration */
663static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
664
danielk197726783a52007-08-29 14:06:22 +0000665/*
666** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000667** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000668*/
drhda4ca9d2014-09-09 17:27:35 +0000669void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
drhb84e5742016-02-05 02:42:54 +0000670 assert( db!=0 );
drh575fad62016-02-05 13:38:36 +0000671 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
drhb84e5742016-02-05 02:42:54 +0000672 assert( sqlite3_mutex_held(db->mutex) );
673 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
674 return dbReallocFinish(db, p, n);
675}
676static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000677 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000678 assert( db!=0 );
drhb84e5742016-02-05 02:42:54 +0000679 assert( p!=0 );
danielk1977a1644fd2007-08-29 12:31:25 +0000680 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000681 if( isLookaside(db, p) ){
drh575fad62016-02-05 13:38:36 +0000682 pNew = sqlite3DbMallocRawNN(db, n);
drh633e6d52008-07-28 19:34:53 +0000683 if( pNew ){
684 memcpy(pNew, p, db->lookaside.sz);
685 sqlite3DbFree(db, p);
686 }
687 }else{
drhd231aa32014-10-07 15:46:54 +0000688 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000689 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000690 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000691 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000692 if( !pNew ){
drh4a642b62016-02-05 01:55:27 +0000693 sqlite3OomFault(db);
drh633e6d52008-07-28 19:34:53 +0000694 }
drhd231aa32014-10-07 15:46:54 +0000695 sqlite3MemdebugSetType(pNew,
drh4a642b62016-02-05 01:55:27 +0000696 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000697 }
698 }
699 return pNew;
700}
701
drh17435752007-08-16 04:30:38 +0000702/*
703** Attempt to reallocate p. If the reallocation fails, then free p
704** and set the mallocFailed flag in the database connection.
705*/
drhda4ca9d2014-09-09 17:27:35 +0000706void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000707 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000708 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000709 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000710 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000711 }
712 return pNew;
713}
714
drha3152892007-05-05 11:48:52 +0000715/*
716** Make a copy of a string in memory obtained from sqliteMalloc(). These
717** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
718** is because when memory debugging is turned on, these two functions are
719** called via macros that record the current file and line number in the
720** ThreadData structure.
721*/
drh633e6d52008-07-28 19:34:53 +0000722char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000723 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000724 size_t n;
725 if( z==0 ){
726 return 0;
727 }
drhcee11ad2016-10-17 00:48:06 +0000728 n = strlen(z) + 1;
729 zNew = sqlite3DbMallocRaw(db, n);
drha3152892007-05-05 11:48:52 +0000730 if( zNew ){
731 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000732 }
733 return zNew;
734}
drhda4ca9d2014-09-09 17:27:35 +0000735char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000736 char *zNew;
drh575fad62016-02-05 13:38:36 +0000737 assert( db!=0 );
drh633e6d52008-07-28 19:34:53 +0000738 if( z==0 ){
739 return 0;
740 }
741 assert( (n&0x7fffffff)==n );
drh575fad62016-02-05 13:38:36 +0000742 zNew = sqlite3DbMallocRawNN(db, n+1);
drh633e6d52008-07-28 19:34:53 +0000743 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000744 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000745 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000746 }
747 return zNew;
748}
749
drha3152892007-05-05 11:48:52 +0000750/*
drh22c17b82015-05-15 04:13:15 +0000751** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000752*/
drh22c17b82015-05-15 04:13:15 +0000753void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000754 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000755 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000756}
757
drhb50c65d2014-08-23 20:25:53 +0000758/*
drh4a642b62016-02-05 01:55:27 +0000759** Call this routine to record the fact that an OOM (out-of-memory) error
760** has happened. This routine will set db->mallocFailed, and also
761** temporarily disable the lookaside memory allocator and interrupt
762** any running VDBEs.
763*/
764void sqlite3OomFault(sqlite3 *db){
765 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
766 db->mallocFailed = 1;
767 if( db->nVdbeExec>0 ){
768 db->u1.isInterrupted = 1;
769 }
770 db->lookaside.bDisable++;
771 }
772}
773
774/*
775** This routine reactivates the memory allocator and clears the
776** db->mallocFailed flag as necessary.
777**
778** The memory allocator is not restarted if there are running
779** VDBEs.
780*/
781void sqlite3OomClear(sqlite3 *db){
782 if( db->mallocFailed && db->nVdbeExec==0 ){
783 db->mallocFailed = 0;
784 db->u1.isInterrupted = 0;
785 assert( db->lookaside.bDisable>0 );
786 db->lookaside.bDisable--;
787 }
788}
789
790/*
drhb50c65d2014-08-23 20:25:53 +0000791** Take actions at the end of an API call to indicate an OOM error
792*/
793static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
drh4a642b62016-02-05 01:55:27 +0000794 sqlite3OomClear(db);
drhb50c65d2014-08-23 20:25:53 +0000795 sqlite3Error(db, SQLITE_NOMEM);
mistachkinfad30392016-02-13 23:43:46 +0000796 return SQLITE_NOMEM_BKPT;
drhb50c65d2014-08-23 20:25:53 +0000797}
drha3152892007-05-05 11:48:52 +0000798
799/*
800** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000801** returning control to the user) that has called sqlite3_malloc or
802** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000803**
804** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000805** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000806** invocation SQLITE_NOMEM is returned instead.
807**
drh597d2b62015-06-30 03:13:47 +0000808** If an OOM as occurred, then the connection error-code (the value
809** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000810*/
drha3152892007-05-05 11:48:52 +0000811int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000812 /* If the db handle must hold the connection handle mutex here.
813 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000814 ** is unsafe, as is the call to sqlite3Error().
815 */
drh597d2b62015-06-30 03:13:47 +0000816 assert( db!=0 );
817 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000818 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
819 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000820 }
drhb50c65d2014-08-23 20:25:53 +0000821 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000822}