blob: 1b219604bc2fed56cb8be1dae634965bbcf626cb [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/*
drhf82ccf62010-09-15 17:54:31 +000079** This routine runs when the memory allocator sees that the
80** total memory allocation is about to exceed the soft heap
81** limit.
82*/
83static void softHeapLimitEnforcer(
84 void *NotUsed,
85 sqlite3_int64 NotUsed2,
86 int allocSize
87){
88 UNUSED_PARAMETER2(NotUsed, NotUsed2);
89 sqlite3_release_memory(allocSize);
90}
91
92/*
93** Change the alarm callback
94*/
95static int sqlite3MemoryAlarm(
96 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
97 void *pArg,
98 sqlite3_int64 iThreshold
99){
100 int nUsed;
101 sqlite3_mutex_enter(mem0.mutex);
102 mem0.alarmCallback = xCallback;
103 mem0.alarmArg = pArg;
104 mem0.alarmThreshold = iThreshold;
105 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
106 mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
107 sqlite3_mutex_leave(mem0.mutex);
108 return SQLITE_OK;
109}
110
111#ifndef SQLITE_OMIT_DEPRECATED
112/*
113** Deprecated external interface. Internal/core SQLite code
114** should call sqlite3MemoryAlarm.
115*/
116int sqlite3_memory_alarm(
117 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
118 void *pArg,
119 sqlite3_int64 iThreshold
120){
121 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
122}
123#endif
124
125/*
126** Set the soft heap-size limit for the library. Passing a zero or
127** negative value indicates no limit.
128*/
129sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
130 sqlite3_int64 priorLimit;
131 sqlite3_int64 excess;
132#ifndef SQLITE_OMIT_AUTOINIT
drhde0f1812011-12-22 17:10:35 +0000133 int rc = sqlite3_initialize();
134 if( rc ) return -1;
drhf82ccf62010-09-15 17:54:31 +0000135#endif
136 sqlite3_mutex_enter(mem0.mutex);
137 priorLimit = mem0.alarmThreshold;
138 sqlite3_mutex_leave(mem0.mutex);
139 if( n<0 ) return priorLimit;
140 if( n>0 ){
141 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
142 }else{
143 sqlite3MemoryAlarm(0, 0, 0);
144 }
145 excess = sqlite3_memory_used() - n;
shaneh4b03f212010-10-04 14:11:54 +0000146 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
drhf82ccf62010-09-15 17:54:31 +0000147 return priorLimit;
148}
149void sqlite3_soft_heap_limit(int n){
150 if( n<0 ) n = 0;
151 sqlite3_soft_heap_limit64(n);
152}
153
154/*
drhfec00ea2008-06-14 16:56:21 +0000155** Initialize the memory allocation subsystem.
156*/
157int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000158 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000159 sqlite3MemSetDefault();
160 }
161 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000162 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000163 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000164 }
danielk1977075c23a2008-09-01 18:34:20 +0000165 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000166 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000167 int i, n, sz;
168 ScratchFreeslot *pSlot;
169 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
170 sqlite3GlobalConfig.szScratch = sz;
171 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
172 n = sqlite3GlobalConfig.nScratch;
173 mem0.pScratchFree = pSlot;
174 mem0.nScratchFree = n;
175 for(i=0; i<n-1; i++){
176 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
177 pSlot = pSlot->pNext;
178 }
179 pSlot->pNext = 0;
180 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000181 }else{
drhbadc9802010-08-27 17:16:44 +0000182 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000183 sqlite3GlobalConfig.pScratch = 0;
184 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000185 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000186 }
drh50d1b5f2010-08-27 12:21:06 +0000187 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
188 || sqlite3GlobalConfig.nPage<1 ){
danielk1977075c23a2008-09-01 18:34:20 +0000189 sqlite3GlobalConfig.pPage = 0;
190 sqlite3GlobalConfig.szPage = 0;
drh50d1b5f2010-08-27 12:21:06 +0000191 sqlite3GlobalConfig.nPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000192 }
danielk1977075c23a2008-09-01 18:34:20 +0000193 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000194}
195
196/*
drh50d1b5f2010-08-27 12:21:06 +0000197** Return true if the heap is currently under memory pressure - in other
198** words if the amount of heap used is close to the limit set by
199** sqlite3_soft_heap_limit().
200*/
201int sqlite3HeapNearlyFull(void){
202 return mem0.nearlyFull;
203}
204
205/*
drhfec00ea2008-06-14 16:56:21 +0000206** Deinitialize the memory allocation subsystem.
207*/
208void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000209 if( sqlite3GlobalConfig.m.xShutdown ){
210 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
211 }
drh9ac3fe92008-06-18 18:12:04 +0000212 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000213}
214
215/*
216** Return the amount of memory currently checked out.
217*/
218sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000219 int n, mx;
drhc376a192008-07-14 12:30:54 +0000220 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000221 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000222 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
223 return res;
drhfec00ea2008-06-14 16:56:21 +0000224}
225
226/*
227** Return the maximum amount of memory that has ever been
228** checked out since either the beginning of this process
229** or since the most recent reset.
230*/
231sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000232 int n, mx;
drhc376a192008-07-14 12:30:54 +0000233 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000234 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000235 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000236 return res;
drhfec00ea2008-06-14 16:56:21 +0000237}
238
239/*
drhfec00ea2008-06-14 16:56:21 +0000240** Trigger the alarm
241*/
242static void sqlite3MallocAlarm(int nByte){
243 void (*xCallback)(void*,sqlite3_int64,int);
244 sqlite3_int64 nowUsed;
245 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000246 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000247 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000248 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000249 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000250 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000251 sqlite3_mutex_leave(mem0.mutex);
252 xCallback(pArg, nowUsed, nByte);
253 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000254 mem0.alarmCallback = xCallback;
255 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000256}
257
drhf7141992008-06-19 00:16:08 +0000258/*
259** Do a memory allocation with statistics and alarms. Assume the
260** lock is already held.
261*/
262static int mallocWithAlarm(int n, void **pp){
263 int nFull;
264 void *p;
265 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000266 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000267 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
268 if( mem0.alarmCallback!=0 ){
269 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drh8e1bb042011-04-15 16:39:52 +0000270 if( nUsed >= mem0.alarmThreshold - nFull ){
drh50d1b5f2010-08-27 12:21:06 +0000271 mem0.nearlyFull = 1;
drhf7141992008-06-19 00:16:08 +0000272 sqlite3MallocAlarm(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000273 }else{
274 mem0.nearlyFull = 0;
drhf7141992008-06-19 00:16:08 +0000275 }
276 }
danielk1977075c23a2008-09-01 18:34:20 +0000277 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000278#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d09414c2008-06-19 18:17:49 +0000279 if( p==0 && mem0.alarmCallback ){
280 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000281 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000282 }
drh50d1b5f2010-08-27 12:21:06 +0000283#endif
drhc702c7c2008-07-18 18:56:16 +0000284 if( p ){
285 nFull = sqlite3MallocSize(p);
286 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
drheafc43b2010-07-26 18:43:40 +0000287 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000288 }
drhf7141992008-06-19 00:16:08 +0000289 *pp = p;
290 return nFull;
291}
drhfec00ea2008-06-14 16:56:21 +0000292
293/*
294** Allocate memory. This routine is like sqlite3_malloc() except that it
295** assumes the memory subsystem has already been initialized.
296*/
297void *sqlite3Malloc(int n){
298 void *p;
drh71a1a0f2010-09-11 16:15:55 +0000299 if( n<=0 /* IMP: R-65312-04917 */
300 || n>=0x7fffff00
301 ){
drhe08ed7e2009-06-26 18:35:16 +0000302 /* A memory allocation of a number of bytes which is near the maximum
303 ** signed integer value might cause an integer overflow inside of the
304 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
305 ** 255 bytes of overhead. SQLite itself will never use anything near
306 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000307 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000308 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000309 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000310 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000311 sqlite3_mutex_leave(mem0.mutex);
312 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000313 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000314 }
drh39f67be2010-09-11 16:25:42 +0000315 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
drhfec00ea2008-06-14 16:56:21 +0000316 return p;
317}
318
319/*
320** This version of the memory allocation is for use by the application.
321** First make sure the memory subsystem is initialized, then do the
322** allocation.
323*/
324void *sqlite3_malloc(int n){
325#ifndef SQLITE_OMIT_AUTOINIT
326 if( sqlite3_initialize() ) return 0;
327#endif
328 return sqlite3Malloc(n);
329}
330
331/*
drhe5ae5732008-06-15 02:51:47 +0000332** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000333** xScratchMalloc(). We verify this constraint in the single-threaded
334** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000335** is outstanding clearing it when the allocation is freed.
336*/
337#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000338static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000339#endif
340
341
342/*
343** Allocate memory that is to be used and released right away.
344** This routine is similar to alloca() in that it is not intended
345** for situations where the memory might be held long-term. This
346** routine is intended to get memory to old large transient data
347** structures that would not normally fit on the stack of an
348** embedded processor.
349*/
drhfacf0302008-06-17 15:12:00 +0000350void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000351 void *p;
352 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000353
drhbadc9802010-08-27 17:16:44 +0000354 sqlite3_mutex_enter(mem0.mutex);
drh3ccd5bf2014-08-23 19:04:55 +0000355 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000356 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
357 p = mem0.pScratchFree;
358 mem0.pScratchFree = mem0.pScratchFree->pNext;
359 mem0.nScratchFree--;
360 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000361 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000362 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000363 sqlite3_mutex_leave(mem0.mutex);
364 p = sqlite3Malloc(n);
365 if( sqlite3GlobalConfig.bMemstat && p ){
366 sqlite3_mutex_enter(mem0.mutex);
367 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000368 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000369 }
370 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
371 }
drh1ff6e3a2010-09-02 17:15:19 +0000372 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000373
drhbadc9802010-08-27 17:16:44 +0000374
drhe5ae5732008-06-15 02:51:47 +0000375#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhbadc9802010-08-27 17:16:44 +0000376 /* Verify that no more than two scratch allocations per thread
377 ** are outstanding at one time. (This is only checked in the
drh9ac3fe92008-06-18 18:12:04 +0000378 ** single-threaded case since checking in the multi-threaded case
379 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000380 assert( scratchAllocOut<=1 );
drhbadc9802010-08-27 17:16:44 +0000381 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000382#endif
383
drhe5ae5732008-06-15 02:51:47 +0000384 return p;
385}
drhfacf0302008-06-17 15:12:00 +0000386void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000387 if( p ){
drhbadc9802010-08-27 17:16:44 +0000388
389#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
390 /* Verify that no more than two scratch allocation per thread
391 ** is outstanding at one time. (This is only checked in the
392 ** single-threaded case since checking in the multi-threaded case
393 ** would be much more complicated.) */
394 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
395 scratchAllocOut--;
396#endif
397
398 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
399 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
400 ScratchFreeslot *pSlot;
401 pSlot = (ScratchFreeslot*)p;
402 sqlite3_mutex_enter(mem0.mutex);
403 pSlot->pNext = mem0.pScratchFree;
404 mem0.pScratchFree = pSlot;
405 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000406 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhbadc9802010-08-27 17:16:44 +0000407 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
408 sqlite3_mutex_leave(mem0.mutex);
409 }else{
410 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000411 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drh174b9a12010-07-26 11:07:20 +0000412 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000413 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000414 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000415 int iSize = sqlite3MallocSize(p);
416 sqlite3_mutex_enter(mem0.mutex);
417 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
418 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
drh81ba7d12010-07-26 19:09:31 +0000419 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000420 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000421 sqlite3_mutex_leave(mem0.mutex);
422 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000423 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000424 }
drh9ac3fe92008-06-18 18:12:04 +0000425 }
drhe5ae5732008-06-15 02:51:47 +0000426 }
427}
428
429/*
drh633e6d52008-07-28 19:34:53 +0000430** TRUE if p is a lookaside memory allocation from db
431*/
drh4150ebf2008-10-11 15:38:29 +0000432#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000433static int isLookaside(sqlite3 *db, void *p){
drhb0e77042013-12-10 19:49:00 +0000434 return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000435}
drh4150ebf2008-10-11 15:38:29 +0000436#else
437#define isLookaside(A,B) 0
438#endif
drh633e6d52008-07-28 19:34:53 +0000439
440/*
drhfec00ea2008-06-14 16:56:21 +0000441** Return the size of a memory allocation previously obtained from
442** sqlite3Malloc() or sqlite3_malloc().
443*/
444int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000445 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000446 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
danielk1977075c23a2008-09-01 18:34:20 +0000447 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000448}
drh633e6d52008-07-28 19:34:53 +0000449int sqlite3DbMallocSize(sqlite3 *db, void *p){
drhb0e77042013-12-10 19:49:00 +0000450 assert( db!=0 );
451 assert( sqlite3_mutex_held(db->mutex) );
452 if( isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000453 return db->lookaside.sz;
454 }else{
drh174b9a12010-07-26 11:07:20 +0000455 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
456 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
457 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
danielk1977075c23a2008-09-01 18:34:20 +0000458 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000459 }
460}
drhfec00ea2008-06-14 16:56:21 +0000461
462/*
463** Free memory previously obtained from sqlite3Malloc().
464*/
465void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000466 if( p==0 ) return; /* IMP: R-49053-54554 */
drh174b9a12010-07-26 11:07:20 +0000467 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
drh107b56e2010-03-12 16:32:53 +0000468 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000469 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000470 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000471 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drheafc43b2010-07-26 18:43:40 +0000472 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000473 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000474 sqlite3_mutex_leave(mem0.mutex);
475 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000476 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000477 }
478}
479
480/*
drhb4586f12014-08-23 19:42:06 +0000481** Add the size of memory allocation "p" to the count in
482** *db->pnBytesFreed.
483*/
484static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
485 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
486}
487
488/*
drh633e6d52008-07-28 19:34:53 +0000489** Free memory that might be associated with a particular database
490** connection.
491*/
492void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000493 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000494 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000495 if( db ){
496 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000497 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000498 return;
dand46def72010-07-24 11:28:28 +0000499 }
drh174b9a12010-07-26 11:07:20 +0000500 if( isLookaside(db, p) ){
501 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000502#if SQLITE_DEBUG
503 /* Trash all content in the buffer being freed */
504 memset(p, 0xaa, db->lookaside.sz);
505#endif
drh174b9a12010-07-26 11:07:20 +0000506 pBuf->pNext = db->lookaside.pFree;
507 db->lookaside.pFree = pBuf;
508 db->lookaside.nOut--;
509 return;
510 }
drh633e6d52008-07-28 19:34:53 +0000511 }
drh174b9a12010-07-26 11:07:20 +0000512 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
513 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
514 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
515 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
516 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000517}
518
519/*
drhfec00ea2008-06-14 16:56:21 +0000520** Change the size of an existing memory allocation
521*/
522void *sqlite3Realloc(void *pOld, int nBytes){
shanehca591fe2011-04-15 19:30:42 +0000523 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000524 void *pNew;
525 if( pOld==0 ){
drh71a1a0f2010-09-11 16:15:55 +0000526 return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
drhfec00ea2008-06-14 16:56:21 +0000527 }
drhb6063cf2009-06-27 00:48:33 +0000528 if( nBytes<=0 ){
drh71a1a0f2010-09-11 16:15:55 +0000529 sqlite3_free(pOld); /* IMP: R-31593-10574 */
drhfec00ea2008-06-14 16:56:21 +0000530 return 0;
531 }
drhb6063cf2009-06-27 00:48:33 +0000532 if( nBytes>=0x7fffff00 ){
533 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
534 return 0;
535 }
drhfec00ea2008-06-14 16:56:21 +0000536 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000537 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
538 ** argument to xRealloc is always a value returned by a prior call to
539 ** xRoundup. */
drh7c6791c2009-08-18 14:48:53 +0000540 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
541 if( nOld==nNew ){
542 pNew = pOld;
543 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000544 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000545 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh8e1bb042011-04-15 16:39:52 +0000546 nDiff = nNew - nOld;
547 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
548 mem0.alarmThreshold-nDiff ){
drh2e5a4222011-05-05 17:00:51 +0000549 sqlite3MallocAlarm(nDiff);
drh7c6791c2009-08-18 14:48:53 +0000550 }
drh107b56e2010-03-12 16:32:53 +0000551 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000552 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000553 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
554 if( pNew==0 && mem0.alarmCallback ){
555 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000556 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000557 }
558 if( pNew ){
559 nNew = sqlite3MallocSize(pNew);
drh2e5a4222011-05-05 17:00:51 +0000560 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000561 }
562 sqlite3_mutex_leave(mem0.mutex);
563 }else{
drh7c6791c2009-08-18 14:48:53 +0000564 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000565 }
drh39f67be2010-09-11 16:25:42 +0000566 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
drhfec00ea2008-06-14 16:56:21 +0000567 return pNew;
568}
569
570/*
571** The public interface to sqlite3Realloc. Make sure that the memory
572** subsystem is initialized prior to invoking sqliteRealloc.
573*/
574void *sqlite3_realloc(void *pOld, int n){
575#ifndef SQLITE_OMIT_AUTOINIT
576 if( sqlite3_initialize() ) return 0;
577#endif
578 return sqlite3Realloc(pOld, n);
579}
580
drha3152892007-05-05 11:48:52 +0000581
582/*
drh17435752007-08-16 04:30:38 +0000583** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000584*/
drhfec00ea2008-06-14 16:56:21 +0000585void *sqlite3MallocZero(int n){
586 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000587 if( p ){
588 memset(p, 0, n);
589 }
590 return p;
591}
drh17435752007-08-16 04:30:38 +0000592
593/*
594** Allocate and zero memory. If the allocation fails, make
595** the mallocFailed flag in the connection pointer.
596*/
drhfec00ea2008-06-14 16:56:21 +0000597void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000598 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000599 if( p ){
600 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000601 }
602 return p;
603}
604
605/*
606** Allocate and zero memory. If the allocation fails, make
607** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000608**
609** If db!=0 and db->mallocFailed is true (indicating a prior malloc
610** failure on the same database connection) then always return 0.
611** Hence for a particular database connection, once malloc starts
612** failing, it fails consistently until mallocFailed is reset.
613** This is an important assumption. There are many places in the
614** code that do things like this:
615**
616** int *a = (int*)sqlite3DbMallocRaw(db, 100);
617** int *b = (int*)sqlite3DbMallocRaw(db, 200);
618** if( b ) a[10] = 9;
619**
620** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
621** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000622*/
drhfec00ea2008-06-14 16:56:21 +0000623void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000624 void *p;
drhd9da78a2009-03-24 15:08:09 +0000625 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000626 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000627#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000628 if( db ){
629 LookasideSlot *pBuf;
630 if( db->mallocFailed ){
631 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000632 }
drh0b12e7f2010-12-20 15:51:58 +0000633 if( db->lookaside.bEnabled ){
634 if( n>db->lookaside.sz ){
635 db->lookaside.anStat[1]++;
636 }else if( (pBuf = db->lookaside.pFree)==0 ){
637 db->lookaside.anStat[2]++;
638 }else{
639 db->lookaside.pFree = pBuf->pNext;
640 db->lookaside.nOut++;
641 db->lookaside.anStat[0]++;
642 if( db->lookaside.nOut>db->lookaside.mxOut ){
643 db->lookaside.mxOut = db->lookaside.nOut;
644 }
645 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000646 }
drh633e6d52008-07-28 19:34:53 +0000647 }
648 }
drhddecae72008-10-11 17:35:16 +0000649#else
650 if( db && db->mallocFailed ){
651 return 0;
652 }
drh4150ebf2008-10-11 15:38:29 +0000653#endif
drh633e6d52008-07-28 19:34:53 +0000654 p = sqlite3Malloc(n);
655 if( !p && db ){
656 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000657 }
drh174b9a12010-07-26 11:07:20 +0000658 sqlite3MemdebugSetType(p, MEMTYPE_DB |
659 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
drh17435752007-08-16 04:30:38 +0000660 return p;
661}
662
danielk197726783a52007-08-29 14:06:22 +0000663/*
664** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000665** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000666*/
danielk1977a1644fd2007-08-29 12:31:25 +0000667void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
668 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000669 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000670 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000671 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000672 if( p==0 ){
673 return sqlite3DbMallocRaw(db, n);
674 }
675 if( isLookaside(db, p) ){
676 if( n<=db->lookaside.sz ){
677 return p;
678 }
679 pNew = sqlite3DbMallocRaw(db, n);
680 if( pNew ){
681 memcpy(pNew, p, db->lookaside.sz);
682 sqlite3DbFree(db, p);
683 }
684 }else{
drh174b9a12010-07-26 11:07:20 +0000685 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
686 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
drh107b56e2010-03-12 16:32:53 +0000687 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000688 pNew = sqlite3_realloc(p, n);
689 if( !pNew ){
drh174b9a12010-07-26 11:07:20 +0000690 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000691 db->mallocFailed = 1;
692 }
drh174b9a12010-07-26 11:07:20 +0000693 sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
694 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000695 }
696 }
697 return pNew;
698}
699
drh17435752007-08-16 04:30:38 +0000700/*
701** Attempt to reallocate p. If the reallocation fails, then free p
702** and set the mallocFailed flag in the database connection.
703*/
704void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000705 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000706 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000707 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000708 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000709 }
710 return pNew;
711}
712
drha3152892007-05-05 11:48:52 +0000713/*
714** Make a copy of a string in memory obtained from sqliteMalloc(). These
715** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
716** is because when memory debugging is turned on, these two functions are
717** called via macros that record the current file and line number in the
718** ThreadData structure.
719*/
drh633e6d52008-07-28 19:34:53 +0000720char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000721 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000722 size_t n;
723 if( z==0 ){
724 return 0;
725 }
drhdee0e402009-05-03 20:23:53 +0000726 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000727 assert( (n&0x7fffffff)==n );
728 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000729 if( zNew ){
730 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000731 }
732 return zNew;
733}
734char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000735 char *zNew;
736 if( z==0 ){
737 return 0;
738 }
739 assert( (n&0x7fffffff)==n );
740 zNew = sqlite3DbMallocRaw(db, n+1);
741 if( zNew ){
742 memcpy(zNew, z, n);
743 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000744 }
745 return zNew;
746}
747
drha3152892007-05-05 11:48:52 +0000748/*
drhf089aa42008-07-08 19:34:06 +0000749** Create a string from the zFromat argument and the va_list that follows.
750** Store the string in memory obtained from sqliteMalloc() and make *pz
751** point to that string.
drha3152892007-05-05 11:48:52 +0000752*/
drhf089aa42008-07-08 19:34:06 +0000753void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000754 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000755 char *z;
drha3152892007-05-05 11:48:52 +0000756
drhf089aa42008-07-08 19:34:06 +0000757 va_start(ap, zFormat);
758 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000759 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000760 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000761 *pz = z;
drha3152892007-05-05 11:48:52 +0000762}
763
764
765/*
766** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000767** returning control to the user) that has called sqlite3_malloc or
768** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000769**
770** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000771** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000772** invocation SQLITE_NOMEM is returned instead.
773**
shanebe217792009-03-05 04:20:31 +0000774** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000775** then the connection error-code (the value returned by sqlite3_errcode())
776** is set to SQLITE_NOMEM.
777*/
drha3152892007-05-05 11:48:52 +0000778int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000779 /* If the db handle is not NULL, then we must hold the connection handle
780 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
781 ** is unsafe, as is the call to sqlite3Error().
782 */
783 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000784 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drh13f40da2014-08-22 18:00:11 +0000785 sqlite3Error(db, SQLITE_NOMEM);
drh17435752007-08-16 04:30:38 +0000786 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000787 rc = SQLITE_NOMEM;
788 }
789 return rc & (db ? db->errMask : 0xff);
790}