blob: 264d046ec8eb02b382ce04e741eb812f35863fde [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){
danielk1977075c23a2008-09-01 18:34:20 +0000165 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000166 sqlite3MemSetDefault();
167 }
168 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000169 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000170 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000171 }
danielk1977075c23a2008-09-01 18:34:20 +0000172 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000173 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000174 int i, n, sz;
175 ScratchFreeslot *pSlot;
176 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
177 sqlite3GlobalConfig.szScratch = sz;
178 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
179 n = sqlite3GlobalConfig.nScratch;
180 mem0.pScratchFree = pSlot;
181 mem0.nScratchFree = n;
182 for(i=0; i<n-1; i++){
183 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
184 pSlot = pSlot->pNext;
185 }
186 pSlot->pNext = 0;
187 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000188 }else{
drhbadc9802010-08-27 17:16:44 +0000189 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000190 sqlite3GlobalConfig.pScratch = 0;
191 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000192 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000193 }
drh50d1b5f2010-08-27 12:21:06 +0000194 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
195 || sqlite3GlobalConfig.nPage<1 ){
danielk1977075c23a2008-09-01 18:34:20 +0000196 sqlite3GlobalConfig.pPage = 0;
197 sqlite3GlobalConfig.szPage = 0;
drh50d1b5f2010-08-27 12:21:06 +0000198 sqlite3GlobalConfig.nPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000199 }
danielk1977075c23a2008-09-01 18:34:20 +0000200 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000201}
202
203/*
drh50d1b5f2010-08-27 12:21:06 +0000204** Return true if the heap is currently under memory pressure - in other
205** words if the amount of heap used is close to the limit set by
206** sqlite3_soft_heap_limit().
207*/
208int sqlite3HeapNearlyFull(void){
209 return mem0.nearlyFull;
210}
211
212/*
drhfec00ea2008-06-14 16:56:21 +0000213** Deinitialize the memory allocation subsystem.
214*/
215void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000216 if( sqlite3GlobalConfig.m.xShutdown ){
217 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
218 }
drh9ac3fe92008-06-18 18:12:04 +0000219 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000220}
221
222/*
223** Return the amount of memory currently checked out.
224*/
225sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000226 int n, mx;
drhc376a192008-07-14 12:30:54 +0000227 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000228 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000229 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
230 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){
drhf7141992008-06-19 00:16:08 +0000239 int n, mx;
drhc376a192008-07-14 12:30:54 +0000240 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000241 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000242 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000243 return res;
drhfec00ea2008-06-14 16:56:21 +0000244}
245
246/*
drhfec00ea2008-06-14 16:56:21 +0000247** Trigger the alarm
248*/
249static void sqlite3MallocAlarm(int nByte){
250 void (*xCallback)(void*,sqlite3_int64,int);
251 sqlite3_int64 nowUsed;
252 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000253 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000254 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000255 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000256 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000257 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000258 sqlite3_mutex_leave(mem0.mutex);
259 xCallback(pArg, nowUsed, nByte);
260 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000261 mem0.alarmCallback = xCallback;
262 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000263}
264
drhf7141992008-06-19 00:16:08 +0000265/*
266** Do a memory allocation with statistics and alarms. Assume the
267** lock is already held.
268*/
269static int mallocWithAlarm(int n, void **pp){
270 int nFull;
271 void *p;
272 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000273 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000274 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
275 if( mem0.alarmCallback!=0 ){
drhaf89fe62015-03-23 17:25:18 +0000276 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drh8e1bb042011-04-15 16:39:52 +0000277 if( nUsed >= mem0.alarmThreshold - nFull ){
drh50d1b5f2010-08-27 12:21:06 +0000278 mem0.nearlyFull = 1;
drhf7141992008-06-19 00:16:08 +0000279 sqlite3MallocAlarm(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000280 }else{
281 mem0.nearlyFull = 0;
drhf7141992008-06-19 00:16:08 +0000282 }
283 }
danielk1977075c23a2008-09-01 18:34:20 +0000284 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000285#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d09414c2008-06-19 18:17:49 +0000286 if( p==0 && mem0.alarmCallback ){
287 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000288 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000289 }
drh50d1b5f2010-08-27 12:21:06 +0000290#endif
drhc702c7c2008-07-18 18:56:16 +0000291 if( p ){
292 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000293 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
294 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000295 }
drhf7141992008-06-19 00:16:08 +0000296 *pp = p;
297 return nFull;
298}
drhfec00ea2008-06-14 16:56:21 +0000299
300/*
301** Allocate memory. This routine is like sqlite3_malloc() except that it
302** assumes the memory subsystem has already been initialized.
303*/
drhda4ca9d2014-09-09 17:27:35 +0000304void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000305 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000306 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000307 /* A memory allocation of a number of bytes which is near the maximum
308 ** signed integer value might cause an integer overflow inside of the
309 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
310 ** 255 bytes of overhead. SQLite itself will never use anything near
311 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000312 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000313 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000314 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000315 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000316 sqlite3_mutex_leave(mem0.mutex);
317 }else{
drhda4ca9d2014-09-09 17:27:35 +0000318 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000319 }
drh8da47412014-10-03 14:54:47 +0000320 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000321 return p;
322}
323
324/*
325** This version of the memory allocation is for use by the application.
326** First make sure the memory subsystem is initialized, then do the
327** allocation.
328*/
329void *sqlite3_malloc(int n){
330#ifndef SQLITE_OMIT_AUTOINIT
331 if( sqlite3_initialize() ) return 0;
332#endif
drhda4ca9d2014-09-09 17:27:35 +0000333 return n<=0 ? 0 : sqlite3Malloc(n);
334}
335void *sqlite3_malloc64(sqlite3_uint64 n){
336#ifndef SQLITE_OMIT_AUTOINIT
337 if( sqlite3_initialize() ) return 0;
338#endif
drhfec00ea2008-06-14 16:56:21 +0000339 return sqlite3Malloc(n);
340}
341
342/*
drhe5ae5732008-06-15 02:51:47 +0000343** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000344** xScratchMalloc(). We verify this constraint in the single-threaded
345** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000346** is outstanding clearing it when the allocation is freed.
347*/
348#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000349static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000350#endif
351
352
353/*
354** Allocate memory that is to be used and released right away.
355** This routine is similar to alloca() in that it is not intended
356** for situations where the memory might be held long-term. This
357** routine is intended to get memory to old large transient data
358** structures that would not normally fit on the stack of an
359** embedded processor.
360*/
drhfacf0302008-06-17 15:12:00 +0000361void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000362 void *p;
363 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000364
drhbadc9802010-08-27 17:16:44 +0000365 sqlite3_mutex_enter(mem0.mutex);
drh3ccd5bf2014-08-23 19:04:55 +0000366 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000367 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
368 p = mem0.pScratchFree;
369 mem0.pScratchFree = mem0.pScratchFree->pNext;
370 mem0.nScratchFree--;
drhaf89fe62015-03-23 17:25:18 +0000371 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000372 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000373 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000374 sqlite3_mutex_leave(mem0.mutex);
375 p = sqlite3Malloc(n);
376 if( sqlite3GlobalConfig.bMemstat && p ){
377 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000378 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000379 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000380 }
381 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
382 }
drh1ff6e3a2010-09-02 17:15:19 +0000383 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000384
drhbadc9802010-08-27 17:16:44 +0000385
drhe5ae5732008-06-15 02:51:47 +0000386#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhcbd55b02014-11-04 14:22:27 +0000387 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
388 ** buffers per thread.
389 **
390 ** This can only be checked in single-threaded mode.
391 */
392 assert( scratchAllocOut==0 );
drhbadc9802010-08-27 17:16:44 +0000393 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000394#endif
395
drhe5ae5732008-06-15 02:51:47 +0000396 return p;
397}
drhfacf0302008-06-17 15:12:00 +0000398void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000399 if( p ){
drhbadc9802010-08-27 17:16:44 +0000400
401#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
402 /* Verify that no more than two scratch allocation per thread
403 ** is outstanding at one time. (This is only checked in the
404 ** single-threaded case since checking in the multi-threaded case
405 ** would be much more complicated.) */
406 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
407 scratchAllocOut--;
408#endif
409
410 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
411 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
412 ScratchFreeslot *pSlot;
413 pSlot = (ScratchFreeslot*)p;
414 sqlite3_mutex_enter(mem0.mutex);
415 pSlot->pNext = mem0.pScratchFree;
416 mem0.pScratchFree = pSlot;
417 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000418 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhaf89fe62015-03-23 17:25:18 +0000419 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
drhbadc9802010-08-27 17:16:44 +0000420 sqlite3_mutex_leave(mem0.mutex);
421 }else{
422 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000423 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
mistachkind4258642015-03-21 23:38:59 +0000424 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000425 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000426 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000427 int iSize = sqlite3MallocSize(p);
428 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000429 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
430 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
431 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000432 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000433 sqlite3_mutex_leave(mem0.mutex);
434 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000435 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000436 }
drh9ac3fe92008-06-18 18:12:04 +0000437 }
drhe5ae5732008-06-15 02:51:47 +0000438 }
439}
440
441/*
drh633e6d52008-07-28 19:34:53 +0000442** TRUE if p is a lookaside memory allocation from db
443*/
drh4150ebf2008-10-11 15:38:29 +0000444#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000445static int isLookaside(sqlite3 *db, void *p){
drhb0e77042013-12-10 19:49:00 +0000446 return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000447}
drh4150ebf2008-10-11 15:38:29 +0000448#else
449#define isLookaside(A,B) 0
450#endif
drh633e6d52008-07-28 19:34:53 +0000451
452/*
drhfec00ea2008-06-14 16:56:21 +0000453** Return the size of a memory allocation previously obtained from
454** sqlite3Malloc() or sqlite3_malloc().
455*/
456int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000457 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000458 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000459}
drh633e6d52008-07-28 19:34:53 +0000460int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh17bcb102014-09-18 21:25:33 +0000461 if( db==0 ){
mistachkind4258642015-03-21 23:38:59 +0000462 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000463 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000464 return sqlite3MallocSize(p);
drh633e6d52008-07-28 19:34:53 +0000465 }else{
drh17bcb102014-09-18 21:25:33 +0000466 assert( sqlite3_mutex_held(db->mutex) );
467 if( isLookaside(db, p) ){
468 return db->lookaside.sz;
469 }else{
drhd231aa32014-10-07 15:46:54 +0000470 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000471 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000472 return sqlite3GlobalConfig.m.xSize(p);
473 }
drh633e6d52008-07-28 19:34:53 +0000474 }
475}
drhda4ca9d2014-09-09 17:27:35 +0000476sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000477 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000478 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drhda4ca9d2014-09-09 17:27:35 +0000479 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
480}
drhfec00ea2008-06-14 16:56:21 +0000481
482/*
483** Free memory previously obtained from sqlite3Malloc().
484*/
485void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000486 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000487 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000488 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000489 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000490 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000491 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
492 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000493 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000494 sqlite3_mutex_leave(mem0.mutex);
495 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000496 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000497 }
498}
499
500/*
drhb4586f12014-08-23 19:42:06 +0000501** Add the size of memory allocation "p" to the count in
502** *db->pnBytesFreed.
503*/
504static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
505 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
506}
507
508/*
drh633e6d52008-07-28 19:34:53 +0000509** Free memory that might be associated with a particular database
510** connection.
511*/
512void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000513 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000514 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000515 if( db ){
516 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000517 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000518 return;
dand46def72010-07-24 11:28:28 +0000519 }
drh174b9a12010-07-26 11:07:20 +0000520 if( isLookaside(db, p) ){
521 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000522#if SQLITE_DEBUG
523 /* Trash all content in the buffer being freed */
524 memset(p, 0xaa, db->lookaside.sz);
525#endif
drh174b9a12010-07-26 11:07:20 +0000526 pBuf->pNext = db->lookaside.pFree;
527 db->lookaside.pFree = pBuf;
528 db->lookaside.nOut--;
529 return;
530 }
drh633e6d52008-07-28 19:34:53 +0000531 }
drhd231aa32014-10-07 15:46:54 +0000532 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000533 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000534 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
535 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
536 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000537}
538
539/*
drhfec00ea2008-06-14 16:56:21 +0000540** Change the size of an existing memory allocation
541*/
drhda4ca9d2014-09-09 17:27:35 +0000542void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000543 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000544 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000545 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000546 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000547 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000548 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000549 }
drhda4ca9d2014-09-09 17:27:35 +0000550 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000551 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000552 return 0;
553 }
drhb6063cf2009-06-27 00:48:33 +0000554 if( nBytes>=0x7fffff00 ){
555 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
556 return 0;
557 }
drhfec00ea2008-06-14 16:56:21 +0000558 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000559 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
560 ** argument to xRealloc is always a value returned by a prior call to
561 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000562 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000563 if( nOld==nNew ){
564 pNew = pOld;
565 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000566 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000567 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000568 nDiff = nNew - nOld;
569 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
570 mem0.alarmThreshold-nDiff ){
drh2e5a4222011-05-05 17:00:51 +0000571 sqlite3MallocAlarm(nDiff);
drh7c6791c2009-08-18 14:48:53 +0000572 }
573 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
574 if( pNew==0 && mem0.alarmCallback ){
drh3329a632014-09-18 01:21:43 +0000575 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000576 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000577 }
578 if( pNew ){
579 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000580 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000581 }
582 sqlite3_mutex_leave(mem0.mutex);
583 }else{
drh7c6791c2009-08-18 14:48:53 +0000584 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000585 }
drh8da47412014-10-03 14:54:47 +0000586 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000587 return pNew;
588}
589
590/*
591** The public interface to sqlite3Realloc. Make sure that the memory
592** subsystem is initialized prior to invoking sqliteRealloc.
593*/
594void *sqlite3_realloc(void *pOld, int n){
595#ifndef SQLITE_OMIT_AUTOINIT
596 if( sqlite3_initialize() ) return 0;
597#endif
drh8da47412014-10-03 14:54:47 +0000598 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000599 return sqlite3Realloc(pOld, n);
600}
601void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
602#ifndef SQLITE_OMIT_AUTOINIT
603 if( sqlite3_initialize() ) return 0;
604#endif
drhfec00ea2008-06-14 16:56:21 +0000605 return sqlite3Realloc(pOld, n);
606}
607
drha3152892007-05-05 11:48:52 +0000608
609/*
drh17435752007-08-16 04:30:38 +0000610** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000611*/
drhda4ca9d2014-09-09 17:27:35 +0000612void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000613 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000614 if( p ){
drh20f3df02014-09-18 02:20:54 +0000615 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000616 }
617 return p;
618}
drh17435752007-08-16 04:30:38 +0000619
620/*
621** Allocate and zero memory. If the allocation fails, make
622** the mallocFailed flag in the connection pointer.
623*/
drhda4ca9d2014-09-09 17:27:35 +0000624void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000625 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000626 if( p ){
drh20f3df02014-09-18 02:20:54 +0000627 memset(p, 0, (size_t)n);
drh17435752007-08-16 04:30:38 +0000628 }
629 return p;
630}
631
632/*
633** Allocate and zero memory. If the allocation fails, make
634** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000635**
636** If db!=0 and db->mallocFailed is true (indicating a prior malloc
637** failure on the same database connection) then always return 0.
638** Hence for a particular database connection, once malloc starts
639** failing, it fails consistently until mallocFailed is reset.
640** This is an important assumption. There are many places in the
641** code that do things like this:
642**
643** int *a = (int*)sqlite3DbMallocRaw(db, 100);
644** int *b = (int*)sqlite3DbMallocRaw(db, 200);
645** if( b ) a[10] = 9;
646**
647** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
648** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000649*/
drhda4ca9d2014-09-09 17:27:35 +0000650void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh633e6d52008-07-28 19:34:53 +0000651 void *p;
drhd9da78a2009-03-24 15:08:09 +0000652 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000653 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000654#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000655 if( db ){
656 LookasideSlot *pBuf;
657 if( db->mallocFailed ){
658 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000659 }
drh0b12e7f2010-12-20 15:51:58 +0000660 if( db->lookaside.bEnabled ){
661 if( n>db->lookaside.sz ){
662 db->lookaside.anStat[1]++;
663 }else if( (pBuf = db->lookaside.pFree)==0 ){
664 db->lookaside.anStat[2]++;
665 }else{
666 db->lookaside.pFree = pBuf->pNext;
667 db->lookaside.nOut++;
668 db->lookaside.anStat[0]++;
669 if( db->lookaside.nOut>db->lookaside.mxOut ){
670 db->lookaside.mxOut = db->lookaside.nOut;
671 }
672 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000673 }
drh633e6d52008-07-28 19:34:53 +0000674 }
675 }
drhddecae72008-10-11 17:35:16 +0000676#else
677 if( db && db->mallocFailed ){
678 return 0;
679 }
drh4150ebf2008-10-11 15:38:29 +0000680#endif
drh633e6d52008-07-28 19:34:53 +0000681 p = sqlite3Malloc(n);
682 if( !p && db ){
683 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000684 }
drhd231aa32014-10-07 15:46:54 +0000685 sqlite3MemdebugSetType(p,
686 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000687 return p;
688}
689
danielk197726783a52007-08-29 14:06:22 +0000690/*
691** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000692** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000693*/
drhda4ca9d2014-09-09 17:27:35 +0000694void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000695 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000696 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000697 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000698 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000699 if( p==0 ){
700 return sqlite3DbMallocRaw(db, n);
701 }
702 if( isLookaside(db, p) ){
703 if( n<=db->lookaside.sz ){
704 return p;
705 }
706 pNew = sqlite3DbMallocRaw(db, n);
707 if( pNew ){
708 memcpy(pNew, p, db->lookaside.sz);
709 sqlite3DbFree(db, p);
710 }
711 }else{
drhd231aa32014-10-07 15:46:54 +0000712 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000713 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000714 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000715 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000716 if( !pNew ){
717 db->mallocFailed = 1;
718 }
drhd231aa32014-10-07 15:46:54 +0000719 sqlite3MemdebugSetType(pNew,
drh174b9a12010-07-26 11:07:20 +0000720 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000721 }
722 }
723 return pNew;
724}
725
drh17435752007-08-16 04:30:38 +0000726/*
727** Attempt to reallocate p. If the reallocation fails, then free p
728** and set the mallocFailed flag in the database connection.
729*/
drhda4ca9d2014-09-09 17:27:35 +0000730void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000731 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000732 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000733 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000734 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000735 }
736 return pNew;
737}
738
drha3152892007-05-05 11:48:52 +0000739/*
740** Make a copy of a string in memory obtained from sqliteMalloc(). These
741** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
742** is because when memory debugging is turned on, these two functions are
743** called via macros that record the current file and line number in the
744** ThreadData structure.
745*/
drh633e6d52008-07-28 19:34:53 +0000746char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000747 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000748 size_t n;
749 if( z==0 ){
750 return 0;
751 }
drhdee0e402009-05-03 20:23:53 +0000752 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000753 assert( (n&0x7fffffff)==n );
754 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000755 if( zNew ){
756 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000757 }
758 return zNew;
759}
drhda4ca9d2014-09-09 17:27:35 +0000760char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000761 char *zNew;
762 if( z==0 ){
763 return 0;
764 }
765 assert( (n&0x7fffffff)==n );
766 zNew = sqlite3DbMallocRaw(db, n+1);
767 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000768 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000769 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000770 }
771 return zNew;
772}
773
drha3152892007-05-05 11:48:52 +0000774/*
drhf089aa42008-07-08 19:34:06 +0000775** Create a string from the zFromat argument and the va_list that follows.
776** Store the string in memory obtained from sqliteMalloc() and make *pz
777** point to that string.
drha3152892007-05-05 11:48:52 +0000778*/
drhf089aa42008-07-08 19:34:06 +0000779void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000780 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000781 char *z;
drha3152892007-05-05 11:48:52 +0000782
drhf089aa42008-07-08 19:34:06 +0000783 va_start(ap, zFormat);
784 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000785 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000786 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000787 *pz = z;
drha3152892007-05-05 11:48:52 +0000788}
789
drhb50c65d2014-08-23 20:25:53 +0000790/*
791** Take actions at the end of an API call to indicate an OOM error
792*/
793static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
794 db->mallocFailed = 0;
795 sqlite3Error(db, SQLITE_NOMEM);
796 return SQLITE_NOMEM;
797}
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**
shanebe217792009-03-05 04:20:31 +0000808** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000809** then the connection error-code (the value returned by sqlite3_errcode())
810** is set to SQLITE_NOMEM.
811*/
drha3152892007-05-05 11:48:52 +0000812int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000813 /* If the db handle is not NULL, then we must hold the connection handle
814 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
815 ** is unsafe, as is the call to sqlite3Error().
816 */
817 assert( !db || sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000818 if( db==0 ) return rc & 0xff;
819 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
820 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000821 }
drhb50c65d2014-08-23 20:25:53 +0000822 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000823}