blob: 1e77734ecbea63069368c5e922bf998cd42255b4 [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 */
48
49 /*
50 ** The alarm callback and its arguments. The mem0.mutex lock will
51 ** be held while the callback is running. Recursive calls into
52 ** the memory subsystem are allowed, but no new callbacks will be
drhe64ca7b2009-07-16 18:21:17 +000053 ** issued.
drhfec00ea2008-06-14 16:56:21 +000054 */
55 sqlite3_int64 alarmThreshold;
56 void (*alarmCallback)(void*, sqlite3_int64,int);
57 void *alarmArg;
drhfec00ea2008-06-14 16:56:21 +000058
59 /*
drhbadc9802010-08-27 17:16:44 +000060 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
61 ** (so that a range test can be used to determine if an allocation
62 ** being freed came from pScratch) and a pointer to the list of
63 ** unused scratch allocations.
drh9ac3fe92008-06-18 18:12:04 +000064 */
drhbadc9802010-08-27 17:16:44 +000065 void *pScratchEnd;
66 ScratchFreeslot *pScratchFree;
67 u32 nScratchFree;
drh50d1b5f2010-08-27 12:21:06 +000068
69 /*
70 ** True if heap is nearly "full" where "full" is defined by the
71 ** sqlite3_soft_heap_limit() setting.
72 */
73 int nearlyFull;
drh6ac78a02010-09-28 14:26:36 +000074} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +000075
76#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +000077
78/*
drhaf89fe62015-03-23 17:25:18 +000079** Return the memory allocator mutex. sqlite3_status() needs it.
80*/
81sqlite3_mutex *sqlite3MallocMutex(void){
82 return mem0.mutex;
83}
84
85/*
drhf82ccf62010-09-15 17:54:31 +000086** This routine runs when the memory allocator sees that the
87** total memory allocation is about to exceed the soft heap
88** limit.
89*/
90static void softHeapLimitEnforcer(
91 void *NotUsed,
92 sqlite3_int64 NotUsed2,
93 int allocSize
94){
95 UNUSED_PARAMETER2(NotUsed, NotUsed2);
96 sqlite3_release_memory(allocSize);
97}
98
99/*
100** Change the alarm callback
101*/
102static int sqlite3MemoryAlarm(
103 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
104 void *pArg,
105 sqlite3_int64 iThreshold
106){
drhaf89fe62015-03-23 17:25:18 +0000107 sqlite3_int64 nUsed;
drhf82ccf62010-09-15 17:54:31 +0000108 sqlite3_mutex_enter(mem0.mutex);
109 mem0.alarmCallback = xCallback;
110 mem0.alarmArg = pArg;
111 mem0.alarmThreshold = iThreshold;
112 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
113 mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
114 sqlite3_mutex_leave(mem0.mutex);
115 return SQLITE_OK;
116}
117
118#ifndef SQLITE_OMIT_DEPRECATED
119/*
120** Deprecated external interface. Internal/core SQLite code
121** should call sqlite3MemoryAlarm.
122*/
123int sqlite3_memory_alarm(
124 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
125 void *pArg,
126 sqlite3_int64 iThreshold
127){
128 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
129}
130#endif
131
132/*
133** Set the soft heap-size limit for the library. Passing a zero or
134** negative value indicates no limit.
135*/
136sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
137 sqlite3_int64 priorLimit;
138 sqlite3_int64 excess;
139#ifndef SQLITE_OMIT_AUTOINIT
drhde0f1812011-12-22 17:10:35 +0000140 int rc = sqlite3_initialize();
141 if( rc ) return -1;
drhf82ccf62010-09-15 17:54:31 +0000142#endif
143 sqlite3_mutex_enter(mem0.mutex);
144 priorLimit = mem0.alarmThreshold;
145 sqlite3_mutex_leave(mem0.mutex);
146 if( n<0 ) return priorLimit;
147 if( n>0 ){
148 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
149 }else{
150 sqlite3MemoryAlarm(0, 0, 0);
151 }
152 excess = sqlite3_memory_used() - n;
shaneh4b03f212010-10-04 14:11:54 +0000153 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
drhf82ccf62010-09-15 17:54:31 +0000154 return priorLimit;
155}
156void sqlite3_soft_heap_limit(int n){
157 if( n<0 ) n = 0;
158 sqlite3_soft_heap_limit64(n);
159}
160
161/*
drhfec00ea2008-06-14 16:56:21 +0000162** Initialize the memory allocation subsystem.
163*/
164int sqlite3MallocInit(void){
drh592f0cb2015-03-26 17:04:23 +0000165 int rc;
danielk1977075c23a2008-09-01 18:34:20 +0000166 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000167 sqlite3MemSetDefault();
168 }
169 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000170 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000171 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000172 }
danielk1977075c23a2008-09-01 18:34:20 +0000173 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000174 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000175 int i, n, sz;
176 ScratchFreeslot *pSlot;
177 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
178 sqlite3GlobalConfig.szScratch = sz;
179 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
180 n = sqlite3GlobalConfig.nScratch;
181 mem0.pScratchFree = pSlot;
182 mem0.nScratchFree = n;
183 for(i=0; i<n-1; i++){
184 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
185 pSlot = pSlot->pNext;
186 }
187 pSlot->pNext = 0;
188 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000189 }else{
drhbadc9802010-08-27 17:16:44 +0000190 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000191 sqlite3GlobalConfig.pScratch = 0;
192 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000193 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000194 }
drh50d1b5f2010-08-27 12:21:06 +0000195 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000196 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000197 sqlite3GlobalConfig.pPage = 0;
198 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000199 }
drh592f0cb2015-03-26 17:04:23 +0000200 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
201 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
202 return rc;
drhfec00ea2008-06-14 16:56:21 +0000203}
204
205/*
drh50d1b5f2010-08-27 12:21:06 +0000206** Return true if the heap is currently under memory pressure - in other
207** words if the amount of heap used is close to the limit set by
208** sqlite3_soft_heap_limit().
209*/
210int sqlite3HeapNearlyFull(void){
211 return mem0.nearlyFull;
212}
213
214/*
drhfec00ea2008-06-14 16:56:21 +0000215** Deinitialize the memory allocation subsystem.
216*/
217void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000218 if( sqlite3GlobalConfig.m.xShutdown ){
219 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
220 }
drh9ac3fe92008-06-18 18:12:04 +0000221 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000222}
223
224/*
225** Return the amount of memory currently checked out.
226*/
227sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000228 sqlite3_int64 res, mx;
229 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000230 return res;
drhfec00ea2008-06-14 16:56:21 +0000231}
232
233/*
234** Return the maximum amount of memory that has ever been
235** checked out since either the beginning of this process
236** or since the most recent reset.
237*/
238sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000239 sqlite3_int64 res, mx;
240 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
241 return mx;
drhfec00ea2008-06-14 16:56:21 +0000242}
243
244/*
drhfec00ea2008-06-14 16:56:21 +0000245** Trigger the alarm
246*/
247static void sqlite3MallocAlarm(int nByte){
248 void (*xCallback)(void*,sqlite3_int64,int);
249 sqlite3_int64 nowUsed;
250 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000251 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000252 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000253 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000254 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000255 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000256 sqlite3_mutex_leave(mem0.mutex);
257 xCallback(pArg, nowUsed, nByte);
258 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000259 mem0.alarmCallback = xCallback;
260 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000261}
262
drhf7141992008-06-19 00:16:08 +0000263/*
264** Do a memory allocation with statistics and alarms. Assume the
265** lock is already held.
266*/
267static int mallocWithAlarm(int n, void **pp){
268 int nFull;
269 void *p;
270 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000271 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000272 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
273 if( mem0.alarmCallback!=0 ){
drhaf89fe62015-03-23 17:25:18 +0000274 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drh8e1bb042011-04-15 16:39:52 +0000275 if( nUsed >= mem0.alarmThreshold - nFull ){
drh50d1b5f2010-08-27 12:21:06 +0000276 mem0.nearlyFull = 1;
drhf7141992008-06-19 00:16:08 +0000277 sqlite3MallocAlarm(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000278 }else{
279 mem0.nearlyFull = 0;
drhf7141992008-06-19 00:16:08 +0000280 }
281 }
danielk1977075c23a2008-09-01 18:34:20 +0000282 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000283#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d09414c2008-06-19 18:17:49 +0000284 if( p==0 && mem0.alarmCallback ){
285 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000286 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000287 }
drh50d1b5f2010-08-27 12:21:06 +0000288#endif
drhc702c7c2008-07-18 18:56:16 +0000289 if( p ){
290 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000291 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
292 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000293 }
drhf7141992008-06-19 00:16:08 +0000294 *pp = p;
295 return nFull;
296}
drhfec00ea2008-06-14 16:56:21 +0000297
298/*
299** Allocate memory. This routine is like sqlite3_malloc() except that it
300** assumes the memory subsystem has already been initialized.
301*/
drhda4ca9d2014-09-09 17:27:35 +0000302void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000303 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000304 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000305 /* A memory allocation of a number of bytes which is near the maximum
306 ** signed integer value might cause an integer overflow inside of the
307 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
308 ** 255 bytes of overhead. SQLite itself will never use anything near
309 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000310 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000311 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000312 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000313 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000314 sqlite3_mutex_leave(mem0.mutex);
315 }else{
drhda4ca9d2014-09-09 17:27:35 +0000316 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000317 }
drh8da47412014-10-03 14:54:47 +0000318 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000319 return p;
320}
321
322/*
323** This version of the memory allocation is for use by the application.
324** First make sure the memory subsystem is initialized, then do the
325** allocation.
326*/
327void *sqlite3_malloc(int n){
328#ifndef SQLITE_OMIT_AUTOINIT
329 if( sqlite3_initialize() ) return 0;
330#endif
drhda4ca9d2014-09-09 17:27:35 +0000331 return n<=0 ? 0 : sqlite3Malloc(n);
332}
333void *sqlite3_malloc64(sqlite3_uint64 n){
334#ifndef SQLITE_OMIT_AUTOINIT
335 if( sqlite3_initialize() ) return 0;
336#endif
drhfec00ea2008-06-14 16:56:21 +0000337 return sqlite3Malloc(n);
338}
339
340/*
drhe5ae5732008-06-15 02:51:47 +0000341** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000342** xScratchMalloc(). We verify this constraint in the single-threaded
343** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000344** is outstanding clearing it when the allocation is freed.
345*/
346#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000347static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000348#endif
349
350
351/*
352** Allocate memory that is to be used and released right away.
353** This routine is similar to alloca() in that it is not intended
354** for situations where the memory might be held long-term. This
355** routine is intended to get memory to old large transient data
356** structures that would not normally fit on the stack of an
357** embedded processor.
358*/
drhfacf0302008-06-17 15:12:00 +0000359void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000360 void *p;
361 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000362
drhbadc9802010-08-27 17:16:44 +0000363 sqlite3_mutex_enter(mem0.mutex);
drh3ccd5bf2014-08-23 19:04:55 +0000364 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000365 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
366 p = mem0.pScratchFree;
367 mem0.pScratchFree = mem0.pScratchFree->pNext;
368 mem0.nScratchFree--;
drhaf89fe62015-03-23 17:25:18 +0000369 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000370 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000371 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000372 sqlite3_mutex_leave(mem0.mutex);
373 p = sqlite3Malloc(n);
374 if( sqlite3GlobalConfig.bMemstat && p ){
375 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000376 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000377 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000378 }
379 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
380 }
drh1ff6e3a2010-09-02 17:15:19 +0000381 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000382
drhbadc9802010-08-27 17:16:44 +0000383
drhe5ae5732008-06-15 02:51:47 +0000384#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhcbd55b02014-11-04 14:22:27 +0000385 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
386 ** buffers per thread.
387 **
388 ** This can only be checked in single-threaded mode.
389 */
390 assert( scratchAllocOut==0 );
drhbadc9802010-08-27 17:16:44 +0000391 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000392#endif
393
drhe5ae5732008-06-15 02:51:47 +0000394 return p;
395}
drhfacf0302008-06-17 15:12:00 +0000396void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000397 if( p ){
drhbadc9802010-08-27 17:16:44 +0000398
399#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
400 /* Verify that no more than two scratch allocation per thread
401 ** is outstanding at one time. (This is only checked in the
402 ** single-threaded case since checking in the multi-threaded case
403 ** would be much more complicated.) */
404 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
405 scratchAllocOut--;
406#endif
407
408 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
409 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
410 ScratchFreeslot *pSlot;
411 pSlot = (ScratchFreeslot*)p;
412 sqlite3_mutex_enter(mem0.mutex);
413 pSlot->pNext = mem0.pScratchFree;
414 mem0.pScratchFree = pSlot;
415 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000416 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhaf89fe62015-03-23 17:25:18 +0000417 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
drhbadc9802010-08-27 17:16:44 +0000418 sqlite3_mutex_leave(mem0.mutex);
419 }else{
420 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000421 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
mistachkind4258642015-03-21 23:38:59 +0000422 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000423 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000424 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000425 int iSize = sqlite3MallocSize(p);
426 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000427 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
428 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
429 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000430 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000431 sqlite3_mutex_leave(mem0.mutex);
432 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000433 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000434 }
drh9ac3fe92008-06-18 18:12:04 +0000435 }
drhe5ae5732008-06-15 02:51:47 +0000436 }
437}
438
439/*
drh633e6d52008-07-28 19:34:53 +0000440** TRUE if p is a lookaside memory allocation from db
441*/
drh4150ebf2008-10-11 15:38:29 +0000442#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000443static int isLookaside(sqlite3 *db, void *p){
drhb0e77042013-12-10 19:49:00 +0000444 return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000445}
drh4150ebf2008-10-11 15:38:29 +0000446#else
447#define isLookaside(A,B) 0
448#endif
drh633e6d52008-07-28 19:34:53 +0000449
450/*
drhfec00ea2008-06-14 16:56:21 +0000451** Return the size of a memory allocation previously obtained from
452** sqlite3Malloc() or sqlite3_malloc().
453*/
454int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000455 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000456 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000457}
drh633e6d52008-07-28 19:34:53 +0000458int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh17bcb102014-09-18 21:25:33 +0000459 if( db==0 ){
mistachkind4258642015-03-21 23:38:59 +0000460 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000461 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000462 return sqlite3MallocSize(p);
drh633e6d52008-07-28 19:34:53 +0000463 }else{
drh17bcb102014-09-18 21:25:33 +0000464 assert( sqlite3_mutex_held(db->mutex) );
465 if( isLookaside(db, p) ){
466 return db->lookaside.sz;
467 }else{
drhd231aa32014-10-07 15:46:54 +0000468 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000469 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000470 return sqlite3GlobalConfig.m.xSize(p);
471 }
drh633e6d52008-07-28 19:34:53 +0000472 }
473}
drhda4ca9d2014-09-09 17:27:35 +0000474sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000475 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000476 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drhda4ca9d2014-09-09 17:27:35 +0000477 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
478}
drhfec00ea2008-06-14 16:56:21 +0000479
480/*
481** Free memory previously obtained from sqlite3Malloc().
482*/
483void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000484 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000485 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000486 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000487 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000488 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000489 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
490 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000491 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000492 sqlite3_mutex_leave(mem0.mutex);
493 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000494 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000495 }
496}
497
498/*
drhb4586f12014-08-23 19:42:06 +0000499** Add the size of memory allocation "p" to the count in
500** *db->pnBytesFreed.
501*/
502static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
503 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
504}
505
506/*
drh633e6d52008-07-28 19:34:53 +0000507** Free memory that might be associated with a particular database
508** connection.
509*/
510void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000511 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000512 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000513 if( db ){
514 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000515 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000516 return;
dand46def72010-07-24 11:28:28 +0000517 }
drh174b9a12010-07-26 11:07:20 +0000518 if( isLookaside(db, p) ){
519 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000520#if SQLITE_DEBUG
521 /* Trash all content in the buffer being freed */
522 memset(p, 0xaa, db->lookaside.sz);
523#endif
drh174b9a12010-07-26 11:07:20 +0000524 pBuf->pNext = db->lookaside.pFree;
525 db->lookaside.pFree = pBuf;
526 db->lookaside.nOut--;
527 return;
528 }
drh633e6d52008-07-28 19:34:53 +0000529 }
drhd231aa32014-10-07 15:46:54 +0000530 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000531 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000532 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
533 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
534 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000535}
536
537/*
drhfec00ea2008-06-14 16:56:21 +0000538** Change the size of an existing memory allocation
539*/
drhda4ca9d2014-09-09 17:27:35 +0000540void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000541 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000542 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000543 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000544 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000545 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000546 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000547 }
drhda4ca9d2014-09-09 17:27:35 +0000548 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000549 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000550 return 0;
551 }
drhb6063cf2009-06-27 00:48:33 +0000552 if( nBytes>=0x7fffff00 ){
553 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
554 return 0;
555 }
drhfec00ea2008-06-14 16:56:21 +0000556 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000557 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
558 ** argument to xRealloc is always a value returned by a prior call to
559 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000560 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000561 if( nOld==nNew ){
562 pNew = pOld;
563 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000564 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000565 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000566 nDiff = nNew - nOld;
567 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
568 mem0.alarmThreshold-nDiff ){
drh2e5a4222011-05-05 17:00:51 +0000569 sqlite3MallocAlarm(nDiff);
drh7c6791c2009-08-18 14:48:53 +0000570 }
571 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
572 if( pNew==0 && mem0.alarmCallback ){
drh3329a632014-09-18 01:21:43 +0000573 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000574 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000575 }
576 if( pNew ){
577 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000578 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000579 }
580 sqlite3_mutex_leave(mem0.mutex);
581 }else{
drh7c6791c2009-08-18 14:48:53 +0000582 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000583 }
drh8da47412014-10-03 14:54:47 +0000584 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000585 return pNew;
586}
587
588/*
589** The public interface to sqlite3Realloc. Make sure that the memory
590** subsystem is initialized prior to invoking sqliteRealloc.
591*/
592void *sqlite3_realloc(void *pOld, int n){
593#ifndef SQLITE_OMIT_AUTOINIT
594 if( sqlite3_initialize() ) return 0;
595#endif
drh8da47412014-10-03 14:54:47 +0000596 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000597 return sqlite3Realloc(pOld, n);
598}
599void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
600#ifndef SQLITE_OMIT_AUTOINIT
601 if( sqlite3_initialize() ) return 0;
602#endif
drhfec00ea2008-06-14 16:56:21 +0000603 return sqlite3Realloc(pOld, n);
604}
605
drha3152892007-05-05 11:48:52 +0000606
607/*
drh17435752007-08-16 04:30:38 +0000608** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000609*/
drhda4ca9d2014-09-09 17:27:35 +0000610void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000611 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000612 if( p ){
drh20f3df02014-09-18 02:20:54 +0000613 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000614 }
615 return p;
616}
drh17435752007-08-16 04:30:38 +0000617
618/*
619** Allocate and zero memory. If the allocation fails, make
620** the mallocFailed flag in the connection pointer.
621*/
drhda4ca9d2014-09-09 17:27:35 +0000622void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000623 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000624 if( p ){
drh20f3df02014-09-18 02:20:54 +0000625 memset(p, 0, (size_t)n);
drh17435752007-08-16 04:30:38 +0000626 }
627 return p;
628}
629
630/*
631** Allocate and zero memory. If the allocation fails, make
632** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000633**
634** If db!=0 and db->mallocFailed is true (indicating a prior malloc
635** failure on the same database connection) then always return 0.
636** Hence for a particular database connection, once malloc starts
637** failing, it fails consistently until mallocFailed is reset.
638** This is an important assumption. There are many places in the
639** code that do things like this:
640**
641** int *a = (int*)sqlite3DbMallocRaw(db, 100);
642** int *b = (int*)sqlite3DbMallocRaw(db, 200);
643** if( b ) a[10] = 9;
644**
645** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
646** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000647*/
drhda4ca9d2014-09-09 17:27:35 +0000648void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh633e6d52008-07-28 19:34:53 +0000649 void *p;
drhd9da78a2009-03-24 15:08:09 +0000650 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000651 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000652#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000653 if( db ){
654 LookasideSlot *pBuf;
655 if( db->mallocFailed ){
656 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000657 }
drh0b12e7f2010-12-20 15:51:58 +0000658 if( db->lookaside.bEnabled ){
659 if( n>db->lookaside.sz ){
660 db->lookaside.anStat[1]++;
661 }else if( (pBuf = db->lookaside.pFree)==0 ){
662 db->lookaside.anStat[2]++;
663 }else{
664 db->lookaside.pFree = pBuf->pNext;
665 db->lookaside.nOut++;
666 db->lookaside.anStat[0]++;
667 if( db->lookaside.nOut>db->lookaside.mxOut ){
668 db->lookaside.mxOut = db->lookaside.nOut;
669 }
670 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000671 }
drh633e6d52008-07-28 19:34:53 +0000672 }
673 }
drhddecae72008-10-11 17:35:16 +0000674#else
675 if( db && db->mallocFailed ){
676 return 0;
677 }
drh4150ebf2008-10-11 15:38:29 +0000678#endif
drh633e6d52008-07-28 19:34:53 +0000679 p = sqlite3Malloc(n);
680 if( !p && db ){
681 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000682 }
drhd231aa32014-10-07 15:46:54 +0000683 sqlite3MemdebugSetType(p,
684 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000685 return p;
686}
687
danielk197726783a52007-08-29 14:06:22 +0000688/*
689** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000690** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000691*/
drhda4ca9d2014-09-09 17:27:35 +0000692void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000693 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000694 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000695 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000696 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000697 if( p==0 ){
698 return sqlite3DbMallocRaw(db, n);
699 }
700 if( isLookaside(db, p) ){
701 if( n<=db->lookaside.sz ){
702 return p;
703 }
704 pNew = sqlite3DbMallocRaw(db, n);
705 if( pNew ){
706 memcpy(pNew, p, db->lookaside.sz);
707 sqlite3DbFree(db, p);
708 }
709 }else{
drhd231aa32014-10-07 15:46:54 +0000710 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000711 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000712 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000713 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000714 if( !pNew ){
715 db->mallocFailed = 1;
716 }
drhd231aa32014-10-07 15:46:54 +0000717 sqlite3MemdebugSetType(pNew,
drh174b9a12010-07-26 11:07:20 +0000718 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000719 }
720 }
721 return pNew;
722}
723
drh17435752007-08-16 04:30:38 +0000724/*
725** Attempt to reallocate p. If the reallocation fails, then free p
726** and set the mallocFailed flag in the database connection.
727*/
drhda4ca9d2014-09-09 17:27:35 +0000728void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000729 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000730 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000731 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000732 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000733 }
734 return pNew;
735}
736
drha3152892007-05-05 11:48:52 +0000737/*
738** Make a copy of a string in memory obtained from sqliteMalloc(). These
739** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
740** is because when memory debugging is turned on, these two functions are
741** called via macros that record the current file and line number in the
742** ThreadData structure.
743*/
drh633e6d52008-07-28 19:34:53 +0000744char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000745 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000746 size_t n;
747 if( z==0 ){
748 return 0;
749 }
drhdee0e402009-05-03 20:23:53 +0000750 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000751 assert( (n&0x7fffffff)==n );
752 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000753 if( zNew ){
754 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000755 }
756 return zNew;
757}
drhda4ca9d2014-09-09 17:27:35 +0000758char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000759 char *zNew;
760 if( z==0 ){
761 return 0;
762 }
763 assert( (n&0x7fffffff)==n );
764 zNew = sqlite3DbMallocRaw(db, n+1);
765 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000766 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000767 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000768 }
769 return zNew;
770}
771
drha3152892007-05-05 11:48:52 +0000772/*
drh22c17b82015-05-15 04:13:15 +0000773** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000774*/
drh22c17b82015-05-15 04:13:15 +0000775void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000776 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000777 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000778}
779
drhb50c65d2014-08-23 20:25:53 +0000780/*
781** Take actions at the end of an API call to indicate an OOM error
782*/
783static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
784 db->mallocFailed = 0;
785 sqlite3Error(db, SQLITE_NOMEM);
786 return SQLITE_NOMEM;
787}
drha3152892007-05-05 11:48:52 +0000788
789/*
790** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000791** returning control to the user) that has called sqlite3_malloc or
792** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000793**
794** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000795** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000796** invocation SQLITE_NOMEM is returned instead.
797**
drh597d2b62015-06-30 03:13:47 +0000798** If an OOM as occurred, then the connection error-code (the value
799** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000800*/
drha3152892007-05-05 11:48:52 +0000801int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000802 /* If the db handle must hold the connection handle mutex here.
803 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000804 ** is unsafe, as is the call to sqlite3Error().
805 */
drh597d2b62015-06-30 03:13:47 +0000806 assert( db!=0 );
807 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000808 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
809 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000810 }
drhb50c65d2014-08-23 20:25:53 +0000811 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000812}