blob: ac1910389b1d1ff35dd0d543468a210dc27733d5 [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/*
drhb21c8cd2007-08-21 19:33:56 +000019** This routine runs when the memory allocator sees that the
20** total memory allocation is about to exceed the soft heap
21** limit.
22*/
23static void softHeapLimitEnforcer(
24 void *NotUsed,
danielk197762c14b32008-11-19 09:05:26 +000025 sqlite3_int64 NotUsed2,
drh153c62c2007-08-24 03:51:33 +000026 int allocSize
drhb21c8cd2007-08-21 19:33:56 +000027){
danielk197762c14b32008-11-19 09:05:26 +000028 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhb21c8cd2007-08-21 19:33:56 +000029 sqlite3_release_memory(allocSize);
30}
31
32/*
danielk197784680242008-06-23 11:11:35 +000033** Set the soft heap-size limit for the library. Passing a zero or
34** negative value indicates no limit.
drha3152892007-05-05 11:48:52 +000035*/
36void sqlite3_soft_heap_limit(int n){
drhb21c8cd2007-08-21 19:33:56 +000037 sqlite3_uint64 iLimit;
38 int overage;
39 if( n<0 ){
40 iLimit = 0;
41 }else{
42 iLimit = n;
drha3152892007-05-05 11:48:52 +000043 }
drh9ac06502009-08-17 13:42:29 +000044#ifndef SQLITE_OMIT_AUTOINIT
drh9ac3fe92008-06-18 18:12:04 +000045 sqlite3_initialize();
drh9ac06502009-08-17 13:42:29 +000046#endif
drhb21c8cd2007-08-21 19:33:56 +000047 if( iLimit>0 ){
shane4a27a282008-09-04 04:32:49 +000048 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
drhb21c8cd2007-08-21 19:33:56 +000049 }else{
shane4a27a282008-09-04 04:32:49 +000050 sqlite3MemoryAlarm(0, 0, 0);
drhb21c8cd2007-08-21 19:33:56 +000051 }
drh1bd10f82008-12-10 21:19:56 +000052 overage = (int)(sqlite3_memory_used() - (i64)n);
drhb21c8cd2007-08-21 19:33:56 +000053 if( overage>0 ){
54 sqlite3_release_memory(overage);
55 }
drha3152892007-05-05 11:48:52 +000056}
57
58/*
danielk197784680242008-06-23 11:11:35 +000059** Attempt to release up to n bytes of non-essential memory currently
60** held by SQLite. An example of non-essential memory is memory used to
61** cache database pages that are not currently in use.
drha3152892007-05-05 11:48:52 +000062*/
63int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +000064#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh9f129f42010-08-31 15:27:32 +000065 return sqlite3PcacheReleaseMemory(n);
danielk19771e536952007-08-16 10:09:01 +000066#else
drh9f129f42010-08-31 15:27:32 +000067 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
68 ** is a no-op returning zero if SQLite is not compiled with
69 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
danielk197762c14b32008-11-19 09:05:26 +000070 UNUSED_PARAMETER(n);
drh9f129f42010-08-31 15:27:32 +000071 return 0;
danielk19771e536952007-08-16 10:09:01 +000072#endif
drha3152892007-05-05 11:48:52 +000073}
drha3152892007-05-05 11:48:52 +000074
drhfec00ea2008-06-14 16:56:21 +000075/*
drhbadc9802010-08-27 17:16:44 +000076** An instance of the following object records the location of
77** each unused scratch buffer.
78*/
79typedef struct ScratchFreeslot {
80 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
81} ScratchFreeslot;
82
83/*
drhfec00ea2008-06-14 16:56:21 +000084** State information local to the memory allocation subsystem.
85*/
danielk19775c8f8582008-09-02 10:22:00 +000086static SQLITE_WSD struct Mem0Global {
drhfec00ea2008-06-14 16:56:21 +000087 sqlite3_mutex *mutex; /* Mutex to serialize access */
88
89 /*
90 ** The alarm callback and its arguments. The mem0.mutex lock will
91 ** be held while the callback is running. Recursive calls into
92 ** the memory subsystem are allowed, but no new callbacks will be
drhe64ca7b2009-07-16 18:21:17 +000093 ** issued.
drhfec00ea2008-06-14 16:56:21 +000094 */
95 sqlite3_int64 alarmThreshold;
96 void (*alarmCallback)(void*, sqlite3_int64,int);
97 void *alarmArg;
drhfec00ea2008-06-14 16:56:21 +000098
99 /*
drhbadc9802010-08-27 17:16:44 +0000100 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
101 ** (so that a range test can be used to determine if an allocation
102 ** being freed came from pScratch) and a pointer to the list of
103 ** unused scratch allocations.
drh9ac3fe92008-06-18 18:12:04 +0000104 */
drhbadc9802010-08-27 17:16:44 +0000105 void *pScratchEnd;
106 ScratchFreeslot *pScratchFree;
107 u32 nScratchFree;
drh50d1b5f2010-08-27 12:21:06 +0000108
109 /*
110 ** True if heap is nearly "full" where "full" is defined by the
111 ** sqlite3_soft_heap_limit() setting.
112 */
113 int nearlyFull;
drhbadc9802010-08-27 17:16:44 +0000114} mem0 = { 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000115
116#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000117
118/*
119** Initialize the memory allocation subsystem.
120*/
121int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000122 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000123 sqlite3MemSetDefault();
124 }
125 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000126 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000127 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000128 }
danielk1977075c23a2008-09-01 18:34:20 +0000129 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
130 && sqlite3GlobalConfig.nScratch>=0 ){
drhbadc9802010-08-27 17:16:44 +0000131 int i, n, sz;
132 ScratchFreeslot *pSlot;
133 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
134 sqlite3GlobalConfig.szScratch = sz;
135 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
136 n = sqlite3GlobalConfig.nScratch;
137 mem0.pScratchFree = pSlot;
138 mem0.nScratchFree = n;
139 for(i=0; i<n-1; i++){
140 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
141 pSlot = pSlot->pNext;
142 }
143 pSlot->pNext = 0;
144 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000145 }else{
drhbadc9802010-08-27 17:16:44 +0000146 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000147 sqlite3GlobalConfig.pScratch = 0;
148 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000149 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000150 }
drh50d1b5f2010-08-27 12:21:06 +0000151 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
152 || sqlite3GlobalConfig.nPage<1 ){
danielk1977075c23a2008-09-01 18:34:20 +0000153 sqlite3GlobalConfig.pPage = 0;
154 sqlite3GlobalConfig.szPage = 0;
drh50d1b5f2010-08-27 12:21:06 +0000155 sqlite3GlobalConfig.nPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000156 }
danielk1977075c23a2008-09-01 18:34:20 +0000157 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000158}
159
160/*
drh50d1b5f2010-08-27 12:21:06 +0000161** Return true if the heap is currently under memory pressure - in other
162** words if the amount of heap used is close to the limit set by
163** sqlite3_soft_heap_limit().
164*/
165int sqlite3HeapNearlyFull(void){
166 return mem0.nearlyFull;
167}
168
169/*
drhfec00ea2008-06-14 16:56:21 +0000170** Deinitialize the memory allocation subsystem.
171*/
172void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000173 if( sqlite3GlobalConfig.m.xShutdown ){
174 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
175 }
drh9ac3fe92008-06-18 18:12:04 +0000176 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000177}
178
179/*
180** Return the amount of memory currently checked out.
181*/
182sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000183 int n, mx;
drhc376a192008-07-14 12:30:54 +0000184 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000185 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000186 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
187 return res;
drhfec00ea2008-06-14 16:56:21 +0000188}
189
190/*
191** Return the maximum amount of memory that has ever been
192** checked out since either the beginning of this process
193** or since the most recent reset.
194*/
195sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000196 int n, mx;
drhc376a192008-07-14 12:30:54 +0000197 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000198 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000199 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000200 return res;
drhfec00ea2008-06-14 16:56:21 +0000201}
202
203/*
204** Change the alarm callback
205*/
shane4a27a282008-09-04 04:32:49 +0000206int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000207 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
208 void *pArg,
209 sqlite3_int64 iThreshold
210){
drh50d1b5f2010-08-27 12:21:06 +0000211 int nUsed;
drhfec00ea2008-06-14 16:56:21 +0000212 sqlite3_mutex_enter(mem0.mutex);
213 mem0.alarmCallback = xCallback;
214 mem0.alarmArg = pArg;
215 mem0.alarmThreshold = iThreshold;
drh50d1b5f2010-08-27 12:21:06 +0000216 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
217 mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
drhfec00ea2008-06-14 16:56:21 +0000218 sqlite3_mutex_leave(mem0.mutex);
219 return SQLITE_OK;
220}
221
shaneeec556d2008-10-12 00:27:53 +0000222#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000223/*
shane4a27a282008-09-04 04:32:49 +0000224** Deprecated external interface. Internal/core SQLite code
225** should call sqlite3MemoryAlarm.
226*/
227int sqlite3_memory_alarm(
228 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
229 void *pArg,
230 sqlite3_int64 iThreshold
231){
232 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
233}
shaneeec556d2008-10-12 00:27:53 +0000234#endif
shane4a27a282008-09-04 04:32:49 +0000235
236/*
drhfec00ea2008-06-14 16:56:21 +0000237** Trigger the alarm
238*/
239static void sqlite3MallocAlarm(int nByte){
240 void (*xCallback)(void*,sqlite3_int64,int);
241 sqlite3_int64 nowUsed;
242 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000243 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000244 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000245 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000246 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000247 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000248 sqlite3_mutex_leave(mem0.mutex);
249 xCallback(pArg, nowUsed, nByte);
250 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000251 mem0.alarmCallback = xCallback;
252 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000253}
254
drhf7141992008-06-19 00:16:08 +0000255/*
256** Do a memory allocation with statistics and alarms. Assume the
257** lock is already held.
258*/
259static int mallocWithAlarm(int n, void **pp){
260 int nFull;
261 void *p;
262 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000263 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000264 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
265 if( mem0.alarmCallback!=0 ){
266 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
267 if( nUsed+nFull >= mem0.alarmThreshold ){
drh50d1b5f2010-08-27 12:21:06 +0000268 mem0.nearlyFull = 1;
drhf7141992008-06-19 00:16:08 +0000269 sqlite3MallocAlarm(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000270 }else{
271 mem0.nearlyFull = 0;
drhf7141992008-06-19 00:16:08 +0000272 }
273 }
danielk1977075c23a2008-09-01 18:34:20 +0000274 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000275#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d09414c2008-06-19 18:17:49 +0000276 if( p==0 && mem0.alarmCallback ){
277 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000278 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000279 }
drh50d1b5f2010-08-27 12:21:06 +0000280#endif
drhc702c7c2008-07-18 18:56:16 +0000281 if( p ){
282 nFull = sqlite3MallocSize(p);
283 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
drheafc43b2010-07-26 18:43:40 +0000284 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000285 }
drhf7141992008-06-19 00:16:08 +0000286 *pp = p;
287 return nFull;
288}
drhfec00ea2008-06-14 16:56:21 +0000289
290/*
291** Allocate memory. This routine is like sqlite3_malloc() except that it
292** assumes the memory subsystem has already been initialized.
293*/
294void *sqlite3Malloc(int n){
295 void *p;
drhe08ed7e2009-06-26 18:35:16 +0000296 if( n<=0 || n>=0x7fffff00 ){
297 /* A memory allocation of a number of bytes which is near the maximum
298 ** signed integer value might cause an integer overflow inside of the
299 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
300 ** 255 bytes of overhead. SQLite itself will never use anything near
301 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000302 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000303 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000304 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000305 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000306 sqlite3_mutex_leave(mem0.mutex);
307 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000308 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000309 }
310 return p;
311}
312
313/*
314** This version of the memory allocation is for use by the application.
315** First make sure the memory subsystem is initialized, then do the
316** allocation.
317*/
318void *sqlite3_malloc(int n){
319#ifndef SQLITE_OMIT_AUTOINIT
320 if( sqlite3_initialize() ) return 0;
321#endif
322 return sqlite3Malloc(n);
323}
324
325/*
drhe5ae5732008-06-15 02:51:47 +0000326** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000327** xScratchMalloc(). We verify this constraint in the single-threaded
328** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000329** is outstanding clearing it when the allocation is freed.
330*/
331#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000332static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000333#endif
334
335
336/*
337** Allocate memory that is to be used and released right away.
338** This routine is similar to alloca() in that it is not intended
339** for situations where the memory might be held long-term. This
340** routine is intended to get memory to old large transient data
341** structures that would not normally fit on the stack of an
342** embedded processor.
343*/
drhfacf0302008-06-17 15:12:00 +0000344void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000345 void *p;
346 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000347
drhbadc9802010-08-27 17:16:44 +0000348 sqlite3_mutex_enter(mem0.mutex);
349 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
350 p = mem0.pScratchFree;
351 mem0.pScratchFree = mem0.pScratchFree->pNext;
352 mem0.nScratchFree--;
353 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
354 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
355 }else{
356 if( sqlite3GlobalConfig.bMemstat ){
357 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
358 n = mallocWithAlarm(n, &p);
359 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
360 }else{
361 p = sqlite3GlobalConfig.m.xMalloc(n);
362 }
363 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
364 }
365 sqlite3_mutex_leave(mem0.mutex);
366
drhe5ae5732008-06-15 02:51:47 +0000367#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhbadc9802010-08-27 17:16:44 +0000368 /* Verify that no more than two scratch allocations per thread
369 ** are outstanding at one time. (This is only checked in the
drh9ac3fe92008-06-18 18:12:04 +0000370 ** single-threaded case since checking in the multi-threaded case
371 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000372 assert( scratchAllocOut<=1 );
drhbadc9802010-08-27 17:16:44 +0000373 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000374#endif
375
drhe5ae5732008-06-15 02:51:47 +0000376 return p;
377}
drhfacf0302008-06-17 15:12:00 +0000378void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000379 if( p ){
drhbadc9802010-08-27 17:16:44 +0000380
381#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
382 /* Verify that no more than two scratch allocation per thread
383 ** is outstanding at one time. (This is only checked in the
384 ** single-threaded case since checking in the multi-threaded case
385 ** would be much more complicated.) */
386 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
387 scratchAllocOut--;
388#endif
389
390 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
391 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
392 ScratchFreeslot *pSlot;
393 pSlot = (ScratchFreeslot*)p;
394 sqlite3_mutex_enter(mem0.mutex);
395 pSlot->pNext = mem0.pScratchFree;
396 mem0.pScratchFree = pSlot;
397 mem0.nScratchFree++;
398 assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch );
399 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
400 sqlite3_mutex_leave(mem0.mutex);
401 }else{
402 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000403 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drh174b9a12010-07-26 11:07:20 +0000404 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000405 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000406 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000407 int iSize = sqlite3MallocSize(p);
408 sqlite3_mutex_enter(mem0.mutex);
409 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
410 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
drh81ba7d12010-07-26 19:09:31 +0000411 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000412 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000413 sqlite3_mutex_leave(mem0.mutex);
414 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000415 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000416 }
drh9ac3fe92008-06-18 18:12:04 +0000417 }
drhe5ae5732008-06-15 02:51:47 +0000418 }
419}
420
421/*
drh633e6d52008-07-28 19:34:53 +0000422** TRUE if p is a lookaside memory allocation from db
423*/
drh4150ebf2008-10-11 15:38:29 +0000424#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000425static int isLookaside(sqlite3 *db, void *p){
drh174b9a12010-07-26 11:07:20 +0000426 return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000427}
drh4150ebf2008-10-11 15:38:29 +0000428#else
429#define isLookaside(A,B) 0
430#endif
drh633e6d52008-07-28 19:34:53 +0000431
432/*
drhfec00ea2008-06-14 16:56:21 +0000433** Return the size of a memory allocation previously obtained from
434** sqlite3Malloc() or sqlite3_malloc().
435*/
436int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000437 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000438 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
danielk1977075c23a2008-09-01 18:34:20 +0000439 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000440}
drh633e6d52008-07-28 19:34:53 +0000441int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000442 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000443 if( db && isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000444 return db->lookaside.sz;
445 }else{
drh174b9a12010-07-26 11:07:20 +0000446 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
447 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
448 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
danielk1977075c23a2008-09-01 18:34:20 +0000449 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000450 }
451}
drhfec00ea2008-06-14 16:56:21 +0000452
453/*
454** Free memory previously obtained from sqlite3Malloc().
455*/
456void sqlite3_free(void *p){
457 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000458 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
drh107b56e2010-03-12 16:32:53 +0000459 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000460 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000461 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000462 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drheafc43b2010-07-26 18:43:40 +0000463 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000464 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000465 sqlite3_mutex_leave(mem0.mutex);
466 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000467 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000468 }
469}
470
471/*
drh633e6d52008-07-28 19:34:53 +0000472** Free memory that might be associated with a particular database
473** connection.
474*/
475void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000476 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000477 if( db ){
478 if( db->pnBytesFreed ){
479 *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
480 return;
dand46def72010-07-24 11:28:28 +0000481 }
drh174b9a12010-07-26 11:07:20 +0000482 if( isLookaside(db, p) ){
483 LookasideSlot *pBuf = (LookasideSlot*)p;
484 pBuf->pNext = db->lookaside.pFree;
485 db->lookaside.pFree = pBuf;
486 db->lookaside.nOut--;
487 return;
488 }
drh633e6d52008-07-28 19:34:53 +0000489 }
drh174b9a12010-07-26 11:07:20 +0000490 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
491 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
492 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
493 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
494 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000495}
496
497/*
drhfec00ea2008-06-14 16:56:21 +0000498** Change the size of an existing memory allocation
499*/
500void *sqlite3Realloc(void *pOld, int nBytes){
501 int nOld, nNew;
502 void *pNew;
503 if( pOld==0 ){
504 return sqlite3Malloc(nBytes);
505 }
drhb6063cf2009-06-27 00:48:33 +0000506 if( nBytes<=0 ){
drhfec00ea2008-06-14 16:56:21 +0000507 sqlite3_free(pOld);
508 return 0;
509 }
drhb6063cf2009-06-27 00:48:33 +0000510 if( nBytes>=0x7fffff00 ){
511 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
512 return 0;
513 }
drhfec00ea2008-06-14 16:56:21 +0000514 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000515 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
516 ** argument to xRealloc is always a value returned by a prior call to
517 ** xRoundup. */
drh7c6791c2009-08-18 14:48:53 +0000518 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
519 if( nOld==nNew ){
520 pNew = pOld;
521 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000522 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000523 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh7c6791c2009-08-18 14:48:53 +0000524 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
525 mem0.alarmThreshold ){
526 sqlite3MallocAlarm(nNew-nOld);
527 }
drh107b56e2010-03-12 16:32:53 +0000528 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000529 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000530 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
531 if( pNew==0 && mem0.alarmCallback ){
532 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000533 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000534 }
535 if( pNew ){
536 nNew = sqlite3MallocSize(pNew);
537 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000538 }
539 sqlite3_mutex_leave(mem0.mutex);
540 }else{
drh7c6791c2009-08-18 14:48:53 +0000541 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000542 }
543 return pNew;
544}
545
546/*
547** The public interface to sqlite3Realloc. Make sure that the memory
548** subsystem is initialized prior to invoking sqliteRealloc.
549*/
550void *sqlite3_realloc(void *pOld, int n){
551#ifndef SQLITE_OMIT_AUTOINIT
552 if( sqlite3_initialize() ) return 0;
553#endif
554 return sqlite3Realloc(pOld, n);
555}
556
drha3152892007-05-05 11:48:52 +0000557
558/*
drh17435752007-08-16 04:30:38 +0000559** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000560*/
drhfec00ea2008-06-14 16:56:21 +0000561void *sqlite3MallocZero(int n){
562 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000563 if( p ){
564 memset(p, 0, n);
565 }
566 return p;
567}
drh17435752007-08-16 04:30:38 +0000568
569/*
570** Allocate and zero memory. If the allocation fails, make
571** the mallocFailed flag in the connection pointer.
572*/
drhfec00ea2008-06-14 16:56:21 +0000573void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000574 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000575 if( p ){
576 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000577 }
578 return p;
579}
580
581/*
582** Allocate and zero memory. If the allocation fails, make
583** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000584**
585** If db!=0 and db->mallocFailed is true (indicating a prior malloc
586** failure on the same database connection) then always return 0.
587** Hence for a particular database connection, once malloc starts
588** failing, it fails consistently until mallocFailed is reset.
589** This is an important assumption. There are many places in the
590** code that do things like this:
591**
592** int *a = (int*)sqlite3DbMallocRaw(db, 100);
593** int *b = (int*)sqlite3DbMallocRaw(db, 200);
594** if( b ) a[10] = 9;
595**
596** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
597** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000598*/
drhfec00ea2008-06-14 16:56:21 +0000599void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000600 void *p;
drhd9da78a2009-03-24 15:08:09 +0000601 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000602 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000603#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000604 if( db ){
605 LookasideSlot *pBuf;
606 if( db->mallocFailed ){
607 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000608 }
drh633e6d52008-07-28 19:34:53 +0000609 if( db->lookaside.bEnabled && n<=db->lookaside.sz
610 && (pBuf = db->lookaside.pFree)!=0 ){
611 db->lookaside.pFree = pBuf->pNext;
612 db->lookaside.nOut++;
613 if( db->lookaside.nOut>db->lookaside.mxOut ){
614 db->lookaside.mxOut = db->lookaside.nOut;
615 }
616 return (void*)pBuf;
617 }
618 }
drhddecae72008-10-11 17:35:16 +0000619#else
620 if( db && db->mallocFailed ){
621 return 0;
622 }
drh4150ebf2008-10-11 15:38:29 +0000623#endif
drh633e6d52008-07-28 19:34:53 +0000624 p = sqlite3Malloc(n);
625 if( !p && db ){
626 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000627 }
drh174b9a12010-07-26 11:07:20 +0000628 sqlite3MemdebugSetType(p, MEMTYPE_DB |
629 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
drh17435752007-08-16 04:30:38 +0000630 return p;
631}
632
danielk197726783a52007-08-29 14:06:22 +0000633/*
634** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000635** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000636*/
danielk1977a1644fd2007-08-29 12:31:25 +0000637void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
638 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000639 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000640 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000641 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000642 if( p==0 ){
643 return sqlite3DbMallocRaw(db, n);
644 }
645 if( isLookaside(db, p) ){
646 if( n<=db->lookaside.sz ){
647 return p;
648 }
649 pNew = sqlite3DbMallocRaw(db, n);
650 if( pNew ){
651 memcpy(pNew, p, db->lookaside.sz);
652 sqlite3DbFree(db, p);
653 }
654 }else{
drh174b9a12010-07-26 11:07:20 +0000655 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
656 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
drh107b56e2010-03-12 16:32:53 +0000657 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000658 pNew = sqlite3_realloc(p, n);
659 if( !pNew ){
drh174b9a12010-07-26 11:07:20 +0000660 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000661 db->mallocFailed = 1;
662 }
drh174b9a12010-07-26 11:07:20 +0000663 sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
664 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000665 }
666 }
667 return pNew;
668}
669
drh17435752007-08-16 04:30:38 +0000670/*
671** Attempt to reallocate p. If the reallocation fails, then free p
672** and set the mallocFailed flag in the database connection.
673*/
674void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000675 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000676 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000677 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000678 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000679 }
680 return pNew;
681}
682
drha3152892007-05-05 11:48:52 +0000683/*
684** Make a copy of a string in memory obtained from sqliteMalloc(). These
685** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
686** is because when memory debugging is turned on, these two functions are
687** called via macros that record the current file and line number in the
688** ThreadData structure.
689*/
drh633e6d52008-07-28 19:34:53 +0000690char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000691 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000692 size_t n;
693 if( z==0 ){
694 return 0;
695 }
drhdee0e402009-05-03 20:23:53 +0000696 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000697 assert( (n&0x7fffffff)==n );
698 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000699 if( zNew ){
700 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000701 }
702 return zNew;
703}
704char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000705 char *zNew;
706 if( z==0 ){
707 return 0;
708 }
709 assert( (n&0x7fffffff)==n );
710 zNew = sqlite3DbMallocRaw(db, n+1);
711 if( zNew ){
712 memcpy(zNew, z, n);
713 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000714 }
715 return zNew;
716}
717
drha3152892007-05-05 11:48:52 +0000718/*
drhf089aa42008-07-08 19:34:06 +0000719** Create a string from the zFromat argument and the va_list that follows.
720** Store the string in memory obtained from sqliteMalloc() and make *pz
721** point to that string.
drha3152892007-05-05 11:48:52 +0000722*/
drhf089aa42008-07-08 19:34:06 +0000723void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000724 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000725 char *z;
drha3152892007-05-05 11:48:52 +0000726
drhf089aa42008-07-08 19:34:06 +0000727 va_start(ap, zFormat);
728 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000729 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000730 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000731 *pz = z;
drha3152892007-05-05 11:48:52 +0000732}
733
734
735/*
736** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000737** returning control to the user) that has called sqlite3_malloc or
738** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000739**
740** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000741** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000742** invocation SQLITE_NOMEM is returned instead.
743**
shanebe217792009-03-05 04:20:31 +0000744** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000745** then the connection error-code (the value returned by sqlite3_errcode())
746** is set to SQLITE_NOMEM.
747*/
drha3152892007-05-05 11:48:52 +0000748int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000749 /* If the db handle is not NULL, then we must hold the connection handle
750 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
751 ** is unsafe, as is the call to sqlite3Error().
752 */
753 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000754 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000755 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000756 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000757 rc = SQLITE_NOMEM;
758 }
759 return rc & (db ? db->errMask : 0xff);
760}