blob: 3e38d1df9fa60167ac1751ae8957f85195f6f1a2 [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
133 sqlite3_initialize();
134#endif
135 sqlite3_mutex_enter(mem0.mutex);
136 priorLimit = mem0.alarmThreshold;
137 sqlite3_mutex_leave(mem0.mutex);
138 if( n<0 ) return priorLimit;
139 if( n>0 ){
140 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
141 }else{
142 sqlite3MemoryAlarm(0, 0, 0);
143 }
144 excess = sqlite3_memory_used() - n;
shaneh4b03f212010-10-04 14:11:54 +0000145 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
drhf82ccf62010-09-15 17:54:31 +0000146 return priorLimit;
147}
148void sqlite3_soft_heap_limit(int n){
149 if( n<0 ) n = 0;
150 sqlite3_soft_heap_limit64(n);
151}
152
153/*
drhfec00ea2008-06-14 16:56:21 +0000154** Initialize the memory allocation subsystem.
155*/
156int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000157 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000158 sqlite3MemSetDefault();
159 }
160 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000161 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000162 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000163 }
danielk1977075c23a2008-09-01 18:34:20 +0000164 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000165 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000166 int i, n, sz;
167 ScratchFreeslot *pSlot;
168 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
169 sqlite3GlobalConfig.szScratch = sz;
170 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
171 n = sqlite3GlobalConfig.nScratch;
172 mem0.pScratchFree = pSlot;
173 mem0.nScratchFree = n;
174 for(i=0; i<n-1; i++){
175 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
176 pSlot = pSlot->pNext;
177 }
178 pSlot->pNext = 0;
179 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000180 }else{
drhbadc9802010-08-27 17:16:44 +0000181 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000182 sqlite3GlobalConfig.pScratch = 0;
183 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000184 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000185 }
drh50d1b5f2010-08-27 12:21:06 +0000186 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
187 || sqlite3GlobalConfig.nPage<1 ){
danielk1977075c23a2008-09-01 18:34:20 +0000188 sqlite3GlobalConfig.pPage = 0;
189 sqlite3GlobalConfig.szPage = 0;
drh50d1b5f2010-08-27 12:21:06 +0000190 sqlite3GlobalConfig.nPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000191 }
danielk1977075c23a2008-09-01 18:34:20 +0000192 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000193}
194
195/*
drh50d1b5f2010-08-27 12:21:06 +0000196** Return true if the heap is currently under memory pressure - in other
197** words if the amount of heap used is close to the limit set by
198** sqlite3_soft_heap_limit().
199*/
200int sqlite3HeapNearlyFull(void){
201 return mem0.nearlyFull;
202}
203
204/*
drhfec00ea2008-06-14 16:56:21 +0000205** Deinitialize the memory allocation subsystem.
206*/
207void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000208 if( sqlite3GlobalConfig.m.xShutdown ){
209 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
210 }
drh9ac3fe92008-06-18 18:12:04 +0000211 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000212}
213
214/*
215** Return the amount of memory currently checked out.
216*/
217sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000218 int n, mx;
drhc376a192008-07-14 12:30:54 +0000219 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000220 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000221 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
222 return res;
drhfec00ea2008-06-14 16:56:21 +0000223}
224
225/*
226** Return the maximum amount of memory that has ever been
227** checked out since either the beginning of this process
228** or since the most recent reset.
229*/
230sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000231 int n, mx;
drhc376a192008-07-14 12:30:54 +0000232 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000233 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000234 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000235 return res;
drhfec00ea2008-06-14 16:56:21 +0000236}
237
238/*
drhfec00ea2008-06-14 16:56:21 +0000239** Trigger the alarm
240*/
241static void sqlite3MallocAlarm(int nByte){
242 void (*xCallback)(void*,sqlite3_int64,int);
243 sqlite3_int64 nowUsed;
244 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000245 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000246 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000247 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000248 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000249 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000250 sqlite3_mutex_leave(mem0.mutex);
251 xCallback(pArg, nowUsed, nByte);
252 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000253 mem0.alarmCallback = xCallback;
254 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000255}
256
drhf7141992008-06-19 00:16:08 +0000257/*
258** Do a memory allocation with statistics and alarms. Assume the
259** lock is already held.
260*/
261static int mallocWithAlarm(int n, void **pp){
262 int nFull;
263 void *p;
264 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000265 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000266 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
267 if( mem0.alarmCallback!=0 ){
268 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drh8e1bb042011-04-15 16:39:52 +0000269 if( nUsed >= mem0.alarmThreshold - nFull ){
drh50d1b5f2010-08-27 12:21:06 +0000270 mem0.nearlyFull = 1;
drhf7141992008-06-19 00:16:08 +0000271 sqlite3MallocAlarm(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000272 }else{
273 mem0.nearlyFull = 0;
drhf7141992008-06-19 00:16:08 +0000274 }
275 }
danielk1977075c23a2008-09-01 18:34:20 +0000276 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000277#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d09414c2008-06-19 18:17:49 +0000278 if( p==0 && mem0.alarmCallback ){
279 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000280 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000281 }
drh50d1b5f2010-08-27 12:21:06 +0000282#endif
drhc702c7c2008-07-18 18:56:16 +0000283 if( p ){
284 nFull = sqlite3MallocSize(p);
285 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
drheafc43b2010-07-26 18:43:40 +0000286 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000287 }
drhf7141992008-06-19 00:16:08 +0000288 *pp = p;
289 return nFull;
290}
drhfec00ea2008-06-14 16:56:21 +0000291
292/*
293** Allocate memory. This routine is like sqlite3_malloc() except that it
294** assumes the memory subsystem has already been initialized.
295*/
296void *sqlite3Malloc(int n){
297 void *p;
drh71a1a0f2010-09-11 16:15:55 +0000298 if( n<=0 /* IMP: R-65312-04917 */
299 || n>=0x7fffff00
300 ){
drhe08ed7e2009-06-26 18:35:16 +0000301 /* A memory allocation of a number of bytes which is near the maximum
302 ** signed integer value might cause an integer overflow inside of the
303 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
304 ** 255 bytes of overhead. SQLite itself will never use anything near
305 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000306 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000307 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000308 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000309 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000310 sqlite3_mutex_leave(mem0.mutex);
311 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000312 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000313 }
drh39f67be2010-09-11 16:25:42 +0000314 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
drhfec00ea2008-06-14 16:56:21 +0000315 return p;
316}
317
318/*
319** This version of the memory allocation is for use by the application.
320** First make sure the memory subsystem is initialized, then do the
321** allocation.
322*/
323void *sqlite3_malloc(int n){
324#ifndef SQLITE_OMIT_AUTOINIT
325 if( sqlite3_initialize() ) return 0;
326#endif
327 return sqlite3Malloc(n);
328}
329
330/*
drhe5ae5732008-06-15 02:51:47 +0000331** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000332** xScratchMalloc(). We verify this constraint in the single-threaded
333** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000334** is outstanding clearing it when the allocation is freed.
335*/
336#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000337static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000338#endif
339
340
341/*
342** Allocate memory that is to be used and released right away.
343** This routine is similar to alloca() in that it is not intended
344** for situations where the memory might be held long-term. This
345** routine is intended to get memory to old large transient data
346** structures that would not normally fit on the stack of an
347** embedded processor.
348*/
drhfacf0302008-06-17 15:12:00 +0000349void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000350 void *p;
351 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000352
drhbadc9802010-08-27 17:16:44 +0000353 sqlite3_mutex_enter(mem0.mutex);
354 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
355 p = mem0.pScratchFree;
356 mem0.pScratchFree = mem0.pScratchFree->pNext;
357 mem0.nScratchFree--;
358 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
359 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danb0c6a882010-09-02 10:08:41 +0000360 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000361 }else{
362 if( sqlite3GlobalConfig.bMemstat ){
363 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
364 n = mallocWithAlarm(n, &p);
365 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
danb0c6a882010-09-02 10:08:41 +0000366 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000367 }else{
danb0c6a882010-09-02 10:08:41 +0000368 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000369 p = sqlite3GlobalConfig.m.xMalloc(n);
370 }
371 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
372 }
drh1ff6e3a2010-09-02 17:15:19 +0000373 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000374
drhbadc9802010-08-27 17:16:44 +0000375
drhe5ae5732008-06-15 02:51:47 +0000376#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhbadc9802010-08-27 17:16:44 +0000377 /* Verify that no more than two scratch allocations per thread
378 ** are outstanding at one time. (This is only checked in the
drh9ac3fe92008-06-18 18:12:04 +0000379 ** single-threaded case since checking in the multi-threaded case
380 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000381 assert( scratchAllocOut<=1 );
drhbadc9802010-08-27 17:16:44 +0000382 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000383#endif
384
drhe5ae5732008-06-15 02:51:47 +0000385 return p;
386}
drhfacf0302008-06-17 15:12:00 +0000387void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000388 if( p ){
drhbadc9802010-08-27 17:16:44 +0000389
390#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
391 /* Verify that no more than two scratch allocation per thread
392 ** is outstanding at one time. (This is only checked in the
393 ** single-threaded case since checking in the multi-threaded case
394 ** would be much more complicated.) */
395 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
396 scratchAllocOut--;
397#endif
398
399 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
400 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
401 ScratchFreeslot *pSlot;
402 pSlot = (ScratchFreeslot*)p;
403 sqlite3_mutex_enter(mem0.mutex);
404 pSlot->pNext = mem0.pScratchFree;
405 mem0.pScratchFree = pSlot;
406 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000407 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhbadc9802010-08-27 17:16:44 +0000408 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
409 sqlite3_mutex_leave(mem0.mutex);
410 }else{
411 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000412 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drh174b9a12010-07-26 11:07:20 +0000413 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000414 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000415 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000416 int iSize = sqlite3MallocSize(p);
417 sqlite3_mutex_enter(mem0.mutex);
418 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
419 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
drh81ba7d12010-07-26 19:09:31 +0000420 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000421 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000422 sqlite3_mutex_leave(mem0.mutex);
423 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000424 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000425 }
drh9ac3fe92008-06-18 18:12:04 +0000426 }
drhe5ae5732008-06-15 02:51:47 +0000427 }
428}
429
430/*
drh633e6d52008-07-28 19:34:53 +0000431** TRUE if p is a lookaside memory allocation from db
432*/
drh4150ebf2008-10-11 15:38:29 +0000433#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000434static int isLookaside(sqlite3 *db, void *p){
drh174b9a12010-07-26 11:07:20 +0000435 return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000436}
drh4150ebf2008-10-11 15:38:29 +0000437#else
438#define isLookaside(A,B) 0
439#endif
drh633e6d52008-07-28 19:34:53 +0000440
441/*
drhfec00ea2008-06-14 16:56:21 +0000442** Return the size of a memory allocation previously obtained from
443** sqlite3Malloc() or sqlite3_malloc().
444*/
445int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000446 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000447 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
danielk1977075c23a2008-09-01 18:34:20 +0000448 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000449}
drh633e6d52008-07-28 19:34:53 +0000450int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000451 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000452 if( db && 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/*
drh633e6d52008-07-28 19:34:53 +0000481** Free memory that might be associated with a particular database
482** connection.
483*/
484void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000485 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000486 if( db ){
487 if( db->pnBytesFreed ){
488 *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
489 return;
dand46def72010-07-24 11:28:28 +0000490 }
drh174b9a12010-07-26 11:07:20 +0000491 if( isLookaside(db, p) ){
492 LookasideSlot *pBuf = (LookasideSlot*)p;
493 pBuf->pNext = db->lookaside.pFree;
494 db->lookaside.pFree = pBuf;
495 db->lookaside.nOut--;
496 return;
497 }
drh633e6d52008-07-28 19:34:53 +0000498 }
drh174b9a12010-07-26 11:07:20 +0000499 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
500 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
501 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
502 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
503 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000504}
505
506/*
drhfec00ea2008-06-14 16:56:21 +0000507** Change the size of an existing memory allocation
508*/
509void *sqlite3Realloc(void *pOld, int nBytes){
shanehca591fe2011-04-15 19:30:42 +0000510 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000511 void *pNew;
512 if( pOld==0 ){
drh71a1a0f2010-09-11 16:15:55 +0000513 return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
drhfec00ea2008-06-14 16:56:21 +0000514 }
drhb6063cf2009-06-27 00:48:33 +0000515 if( nBytes<=0 ){
drh71a1a0f2010-09-11 16:15:55 +0000516 sqlite3_free(pOld); /* IMP: R-31593-10574 */
drhfec00ea2008-06-14 16:56:21 +0000517 return 0;
518 }
drhb6063cf2009-06-27 00:48:33 +0000519 if( nBytes>=0x7fffff00 ){
520 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
521 return 0;
522 }
drhfec00ea2008-06-14 16:56:21 +0000523 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000524 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
525 ** argument to xRealloc is always a value returned by a prior call to
526 ** xRoundup. */
drh7c6791c2009-08-18 14:48:53 +0000527 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
528 if( nOld==nNew ){
529 pNew = pOld;
530 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000531 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000532 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh8e1bb042011-04-15 16:39:52 +0000533 nDiff = nNew - nOld;
534 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
535 mem0.alarmThreshold-nDiff ){
drh2e5a4222011-05-05 17:00:51 +0000536 sqlite3MallocAlarm(nDiff);
drh7c6791c2009-08-18 14:48:53 +0000537 }
drh107b56e2010-03-12 16:32:53 +0000538 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000539 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000540 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
541 if( pNew==0 && mem0.alarmCallback ){
542 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000543 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000544 }
545 if( pNew ){
546 nNew = sqlite3MallocSize(pNew);
drh2e5a4222011-05-05 17:00:51 +0000547 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000548 }
549 sqlite3_mutex_leave(mem0.mutex);
550 }else{
drh7c6791c2009-08-18 14:48:53 +0000551 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000552 }
drh39f67be2010-09-11 16:25:42 +0000553 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
drhfec00ea2008-06-14 16:56:21 +0000554 return pNew;
555}
556
557/*
558** The public interface to sqlite3Realloc. Make sure that the memory
559** subsystem is initialized prior to invoking sqliteRealloc.
560*/
561void *sqlite3_realloc(void *pOld, int n){
562#ifndef SQLITE_OMIT_AUTOINIT
563 if( sqlite3_initialize() ) return 0;
564#endif
565 return sqlite3Realloc(pOld, n);
566}
567
drha3152892007-05-05 11:48:52 +0000568
569/*
drh17435752007-08-16 04:30:38 +0000570** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000571*/
drhfec00ea2008-06-14 16:56:21 +0000572void *sqlite3MallocZero(int n){
573 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000574 if( p ){
575 memset(p, 0, n);
576 }
577 return p;
578}
drh17435752007-08-16 04:30:38 +0000579
580/*
581** Allocate and zero memory. If the allocation fails, make
582** the mallocFailed flag in the connection pointer.
583*/
drhfec00ea2008-06-14 16:56:21 +0000584void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000585 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000586 if( p ){
587 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000588 }
589 return p;
590}
591
592/*
593** Allocate and zero memory. If the allocation fails, make
594** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000595**
596** If db!=0 and db->mallocFailed is true (indicating a prior malloc
597** failure on the same database connection) then always return 0.
598** Hence for a particular database connection, once malloc starts
599** failing, it fails consistently until mallocFailed is reset.
600** This is an important assumption. There are many places in the
601** code that do things like this:
602**
603** int *a = (int*)sqlite3DbMallocRaw(db, 100);
604** int *b = (int*)sqlite3DbMallocRaw(db, 200);
605** if( b ) a[10] = 9;
606**
607** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
608** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000609*/
drhfec00ea2008-06-14 16:56:21 +0000610void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000611 void *p;
drhd9da78a2009-03-24 15:08:09 +0000612 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000613 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000614#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000615 if( db ){
616 LookasideSlot *pBuf;
617 if( db->mallocFailed ){
618 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000619 }
drh0b12e7f2010-12-20 15:51:58 +0000620 if( db->lookaside.bEnabled ){
621 if( n>db->lookaside.sz ){
622 db->lookaside.anStat[1]++;
623 }else if( (pBuf = db->lookaside.pFree)==0 ){
624 db->lookaside.anStat[2]++;
625 }else{
626 db->lookaside.pFree = pBuf->pNext;
627 db->lookaside.nOut++;
628 db->lookaside.anStat[0]++;
629 if( db->lookaside.nOut>db->lookaside.mxOut ){
630 db->lookaside.mxOut = db->lookaside.nOut;
631 }
632 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000633 }
drh633e6d52008-07-28 19:34:53 +0000634 }
635 }
drhddecae72008-10-11 17:35:16 +0000636#else
637 if( db && db->mallocFailed ){
638 return 0;
639 }
drh4150ebf2008-10-11 15:38:29 +0000640#endif
drh633e6d52008-07-28 19:34:53 +0000641 p = sqlite3Malloc(n);
642 if( !p && db ){
643 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000644 }
drh174b9a12010-07-26 11:07:20 +0000645 sqlite3MemdebugSetType(p, MEMTYPE_DB |
646 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
drh17435752007-08-16 04:30:38 +0000647 return p;
648}
649
danielk197726783a52007-08-29 14:06:22 +0000650/*
651** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000652** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000653*/
danielk1977a1644fd2007-08-29 12:31:25 +0000654void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
655 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000656 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000657 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000658 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000659 if( p==0 ){
660 return sqlite3DbMallocRaw(db, n);
661 }
662 if( isLookaside(db, p) ){
663 if( n<=db->lookaside.sz ){
664 return p;
665 }
666 pNew = sqlite3DbMallocRaw(db, n);
667 if( pNew ){
668 memcpy(pNew, p, db->lookaside.sz);
669 sqlite3DbFree(db, p);
670 }
671 }else{
drh174b9a12010-07-26 11:07:20 +0000672 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
673 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
drh107b56e2010-03-12 16:32:53 +0000674 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000675 pNew = sqlite3_realloc(p, n);
676 if( !pNew ){
drh174b9a12010-07-26 11:07:20 +0000677 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000678 db->mallocFailed = 1;
679 }
drh174b9a12010-07-26 11:07:20 +0000680 sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
681 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000682 }
683 }
684 return pNew;
685}
686
drh17435752007-08-16 04:30:38 +0000687/*
688** Attempt to reallocate p. If the reallocation fails, then free p
689** and set the mallocFailed flag in the database connection.
690*/
691void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000692 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000693 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000694 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000695 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000696 }
697 return pNew;
698}
699
drha3152892007-05-05 11:48:52 +0000700/*
701** Make a copy of a string in memory obtained from sqliteMalloc(). These
702** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
703** is because when memory debugging is turned on, these two functions are
704** called via macros that record the current file and line number in the
705** ThreadData structure.
706*/
drh633e6d52008-07-28 19:34:53 +0000707char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000708 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000709 size_t n;
710 if( z==0 ){
711 return 0;
712 }
drhdee0e402009-05-03 20:23:53 +0000713 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000714 assert( (n&0x7fffffff)==n );
715 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000716 if( zNew ){
717 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000718 }
719 return zNew;
720}
721char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000722 char *zNew;
723 if( z==0 ){
724 return 0;
725 }
726 assert( (n&0x7fffffff)==n );
727 zNew = sqlite3DbMallocRaw(db, n+1);
728 if( zNew ){
729 memcpy(zNew, z, n);
730 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000731 }
732 return zNew;
733}
734
drha3152892007-05-05 11:48:52 +0000735/*
drhf089aa42008-07-08 19:34:06 +0000736** Create a string from the zFromat argument and the va_list that follows.
737** Store the string in memory obtained from sqliteMalloc() and make *pz
738** point to that string.
drha3152892007-05-05 11:48:52 +0000739*/
drhf089aa42008-07-08 19:34:06 +0000740void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000741 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000742 char *z;
drha3152892007-05-05 11:48:52 +0000743
drhf089aa42008-07-08 19:34:06 +0000744 va_start(ap, zFormat);
745 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000746 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000747 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000748 *pz = z;
drha3152892007-05-05 11:48:52 +0000749}
750
751
752/*
753** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000754** returning control to the user) that has called sqlite3_malloc or
755** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000756**
757** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000758** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000759** invocation SQLITE_NOMEM is returned instead.
760**
shanebe217792009-03-05 04:20:31 +0000761** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000762** then the connection error-code (the value returned by sqlite3_errcode())
763** is set to SQLITE_NOMEM.
764*/
drha3152892007-05-05 11:48:52 +0000765int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000766 /* If the db handle is not NULL, then we must hold the connection handle
767 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
768 ** is unsafe, as is the call to sqlite3Error().
769 */
770 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000771 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000772 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000773 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000774 rc = SQLITE_NOMEM;
775 }
776 return rc & (db ? db->errMask : 0xff);
777}