blob: c48cbd7ed559401ae8f399300babc4a5948f55d6 [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
drh7ff27192010-09-02 18:13:00 +0000130 && 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;
drh71a1a0f2010-09-11 16:15:55 +0000296 if( n<=0 /* IMP: R-65312-04917 */
297 || n>=0x7fffff00
298 ){
drhe08ed7e2009-06-26 18:35:16 +0000299 /* A memory allocation of a number of bytes which is near the maximum
300 ** signed integer value might cause an integer overflow inside of the
301 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
302 ** 255 bytes of overhead. SQLite itself will never use anything near
303 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000304 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000305 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000306 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000307 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000308 sqlite3_mutex_leave(mem0.mutex);
309 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000310 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000311 }
drh39f67be2010-09-11 16:25:42 +0000312 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
drhfec00ea2008-06-14 16:56:21 +0000313 return p;
314}
315
316/*
317** This version of the memory allocation is for use by the application.
318** First make sure the memory subsystem is initialized, then do the
319** allocation.
320*/
321void *sqlite3_malloc(int n){
322#ifndef SQLITE_OMIT_AUTOINIT
323 if( sqlite3_initialize() ) return 0;
324#endif
325 return sqlite3Malloc(n);
326}
327
328/*
drhe5ae5732008-06-15 02:51:47 +0000329** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000330** xScratchMalloc(). We verify this constraint in the single-threaded
331** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000332** is outstanding clearing it when the allocation is freed.
333*/
334#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000335static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000336#endif
337
338
339/*
340** Allocate memory that is to be used and released right away.
341** This routine is similar to alloca() in that it is not intended
342** for situations where the memory might be held long-term. This
343** routine is intended to get memory to old large transient data
344** structures that would not normally fit on the stack of an
345** embedded processor.
346*/
drhfacf0302008-06-17 15:12:00 +0000347void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000348 void *p;
349 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000350
drhbadc9802010-08-27 17:16:44 +0000351 sqlite3_mutex_enter(mem0.mutex);
352 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
353 p = mem0.pScratchFree;
354 mem0.pScratchFree = mem0.pScratchFree->pNext;
355 mem0.nScratchFree--;
356 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
357 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danb0c6a882010-09-02 10:08:41 +0000358 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000359 }else{
360 if( sqlite3GlobalConfig.bMemstat ){
361 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
362 n = mallocWithAlarm(n, &p);
363 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
danb0c6a882010-09-02 10:08:41 +0000364 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000365 }else{
danb0c6a882010-09-02 10:08:41 +0000366 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000367 p = sqlite3GlobalConfig.m.xMalloc(n);
368 }
369 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
370 }
drh1ff6e3a2010-09-02 17:15:19 +0000371 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000372
drhbadc9802010-08-27 17:16:44 +0000373
drhe5ae5732008-06-15 02:51:47 +0000374#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhbadc9802010-08-27 17:16:44 +0000375 /* Verify that no more than two scratch allocations per thread
376 ** are outstanding at one time. (This is only checked in the
drh9ac3fe92008-06-18 18:12:04 +0000377 ** single-threaded case since checking in the multi-threaded case
378 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000379 assert( scratchAllocOut<=1 );
drhbadc9802010-08-27 17:16:44 +0000380 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000381#endif
382
drhe5ae5732008-06-15 02:51:47 +0000383 return p;
384}
drhfacf0302008-06-17 15:12:00 +0000385void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000386 if( p ){
drhbadc9802010-08-27 17:16:44 +0000387
388#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
389 /* Verify that no more than two scratch allocation per thread
390 ** is outstanding at one time. (This is only checked in the
391 ** single-threaded case since checking in the multi-threaded case
392 ** would be much more complicated.) */
393 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
394 scratchAllocOut--;
395#endif
396
397 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
398 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
399 ScratchFreeslot *pSlot;
400 pSlot = (ScratchFreeslot*)p;
401 sqlite3_mutex_enter(mem0.mutex);
402 pSlot->pNext = mem0.pScratchFree;
403 mem0.pScratchFree = pSlot;
404 mem0.nScratchFree++;
405 assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch );
406 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
407 sqlite3_mutex_leave(mem0.mutex);
408 }else{
409 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000410 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drh174b9a12010-07-26 11:07:20 +0000411 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000412 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000413 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000414 int iSize = sqlite3MallocSize(p);
415 sqlite3_mutex_enter(mem0.mutex);
416 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
417 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
drh81ba7d12010-07-26 19:09:31 +0000418 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000419 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000420 sqlite3_mutex_leave(mem0.mutex);
421 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000422 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000423 }
drh9ac3fe92008-06-18 18:12:04 +0000424 }
drhe5ae5732008-06-15 02:51:47 +0000425 }
426}
427
428/*
drh633e6d52008-07-28 19:34:53 +0000429** TRUE if p is a lookaside memory allocation from db
430*/
drh4150ebf2008-10-11 15:38:29 +0000431#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000432static int isLookaside(sqlite3 *db, void *p){
drh174b9a12010-07-26 11:07:20 +0000433 return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000434}
drh4150ebf2008-10-11 15:38:29 +0000435#else
436#define isLookaside(A,B) 0
437#endif
drh633e6d52008-07-28 19:34:53 +0000438
439/*
drhfec00ea2008-06-14 16:56:21 +0000440** Return the size of a memory allocation previously obtained from
441** sqlite3Malloc() or sqlite3_malloc().
442*/
443int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000444 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000445 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
danielk1977075c23a2008-09-01 18:34:20 +0000446 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000447}
drh633e6d52008-07-28 19:34:53 +0000448int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000449 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000450 if( db && isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000451 return db->lookaside.sz;
452 }else{
drh174b9a12010-07-26 11:07:20 +0000453 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
454 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
455 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
danielk1977075c23a2008-09-01 18:34:20 +0000456 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000457 }
458}
drhfec00ea2008-06-14 16:56:21 +0000459
460/*
461** Free memory previously obtained from sqlite3Malloc().
462*/
463void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000464 if( p==0 ) return; /* IMP: R-49053-54554 */
drh174b9a12010-07-26 11:07:20 +0000465 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
drh107b56e2010-03-12 16:32:53 +0000466 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000467 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000468 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000469 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drheafc43b2010-07-26 18:43:40 +0000470 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000471 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000472 sqlite3_mutex_leave(mem0.mutex);
473 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000474 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000475 }
476}
477
478/*
drh633e6d52008-07-28 19:34:53 +0000479** Free memory that might be associated with a particular database
480** connection.
481*/
482void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000483 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000484 if( db ){
485 if( db->pnBytesFreed ){
486 *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
487 return;
dand46def72010-07-24 11:28:28 +0000488 }
drh174b9a12010-07-26 11:07:20 +0000489 if( isLookaside(db, p) ){
490 LookasideSlot *pBuf = (LookasideSlot*)p;
491 pBuf->pNext = db->lookaside.pFree;
492 db->lookaside.pFree = pBuf;
493 db->lookaside.nOut--;
494 return;
495 }
drh633e6d52008-07-28 19:34:53 +0000496 }
drh174b9a12010-07-26 11:07:20 +0000497 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
498 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
499 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
500 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
501 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000502}
503
504/*
drhfec00ea2008-06-14 16:56:21 +0000505** Change the size of an existing memory allocation
506*/
507void *sqlite3Realloc(void *pOld, int nBytes){
508 int nOld, nNew;
509 void *pNew;
510 if( pOld==0 ){
drh71a1a0f2010-09-11 16:15:55 +0000511 return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
drhfec00ea2008-06-14 16:56:21 +0000512 }
drhb6063cf2009-06-27 00:48:33 +0000513 if( nBytes<=0 ){
drh71a1a0f2010-09-11 16:15:55 +0000514 sqlite3_free(pOld); /* IMP: R-31593-10574 */
drhfec00ea2008-06-14 16:56:21 +0000515 return 0;
516 }
drhb6063cf2009-06-27 00:48:33 +0000517 if( nBytes>=0x7fffff00 ){
518 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
519 return 0;
520 }
drhfec00ea2008-06-14 16:56:21 +0000521 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000522 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
523 ** argument to xRealloc is always a value returned by a prior call to
524 ** xRoundup. */
drh7c6791c2009-08-18 14:48:53 +0000525 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
526 if( nOld==nNew ){
527 pNew = pOld;
528 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000529 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000530 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh7c6791c2009-08-18 14:48:53 +0000531 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
532 mem0.alarmThreshold ){
533 sqlite3MallocAlarm(nNew-nOld);
534 }
drh107b56e2010-03-12 16:32:53 +0000535 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000536 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000537 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
538 if( pNew==0 && mem0.alarmCallback ){
539 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000540 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000541 }
542 if( pNew ){
543 nNew = sqlite3MallocSize(pNew);
544 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000545 }
546 sqlite3_mutex_leave(mem0.mutex);
547 }else{
drh7c6791c2009-08-18 14:48:53 +0000548 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000549 }
drh39f67be2010-09-11 16:25:42 +0000550 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
drhfec00ea2008-06-14 16:56:21 +0000551 return pNew;
552}
553
554/*
555** The public interface to sqlite3Realloc. Make sure that the memory
556** subsystem is initialized prior to invoking sqliteRealloc.
557*/
558void *sqlite3_realloc(void *pOld, int n){
559#ifndef SQLITE_OMIT_AUTOINIT
560 if( sqlite3_initialize() ) return 0;
561#endif
562 return sqlite3Realloc(pOld, n);
563}
564
drha3152892007-05-05 11:48:52 +0000565
566/*
drh17435752007-08-16 04:30:38 +0000567** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000568*/
drhfec00ea2008-06-14 16:56:21 +0000569void *sqlite3MallocZero(int n){
570 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000571 if( p ){
572 memset(p, 0, n);
573 }
574 return p;
575}
drh17435752007-08-16 04:30:38 +0000576
577/*
578** Allocate and zero memory. If the allocation fails, make
579** the mallocFailed flag in the connection pointer.
580*/
drhfec00ea2008-06-14 16:56:21 +0000581void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000582 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000583 if( p ){
584 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000585 }
586 return p;
587}
588
589/*
590** Allocate and zero memory. If the allocation fails, make
591** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000592**
593** If db!=0 and db->mallocFailed is true (indicating a prior malloc
594** failure on the same database connection) then always return 0.
595** Hence for a particular database connection, once malloc starts
596** failing, it fails consistently until mallocFailed is reset.
597** This is an important assumption. There are many places in the
598** code that do things like this:
599**
600** int *a = (int*)sqlite3DbMallocRaw(db, 100);
601** int *b = (int*)sqlite3DbMallocRaw(db, 200);
602** if( b ) a[10] = 9;
603**
604** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
605** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000606*/
drhfec00ea2008-06-14 16:56:21 +0000607void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000608 void *p;
drhd9da78a2009-03-24 15:08:09 +0000609 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000610 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000611#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000612 if( db ){
613 LookasideSlot *pBuf;
614 if( db->mallocFailed ){
615 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000616 }
drh633e6d52008-07-28 19:34:53 +0000617 if( db->lookaside.bEnabled && n<=db->lookaside.sz
618 && (pBuf = db->lookaside.pFree)!=0 ){
619 db->lookaside.pFree = pBuf->pNext;
620 db->lookaside.nOut++;
621 if( db->lookaside.nOut>db->lookaside.mxOut ){
622 db->lookaside.mxOut = db->lookaside.nOut;
623 }
624 return (void*)pBuf;
625 }
626 }
drhddecae72008-10-11 17:35:16 +0000627#else
628 if( db && db->mallocFailed ){
629 return 0;
630 }
drh4150ebf2008-10-11 15:38:29 +0000631#endif
drh633e6d52008-07-28 19:34:53 +0000632 p = sqlite3Malloc(n);
633 if( !p && db ){
634 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000635 }
drh174b9a12010-07-26 11:07:20 +0000636 sqlite3MemdebugSetType(p, MEMTYPE_DB |
637 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
drh17435752007-08-16 04:30:38 +0000638 return p;
639}
640
danielk197726783a52007-08-29 14:06:22 +0000641/*
642** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000643** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000644*/
danielk1977a1644fd2007-08-29 12:31:25 +0000645void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
646 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000647 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000648 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000649 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000650 if( p==0 ){
651 return sqlite3DbMallocRaw(db, n);
652 }
653 if( isLookaside(db, p) ){
654 if( n<=db->lookaside.sz ){
655 return p;
656 }
657 pNew = sqlite3DbMallocRaw(db, n);
658 if( pNew ){
659 memcpy(pNew, p, db->lookaside.sz);
660 sqlite3DbFree(db, p);
661 }
662 }else{
drh174b9a12010-07-26 11:07:20 +0000663 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
664 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
drh107b56e2010-03-12 16:32:53 +0000665 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000666 pNew = sqlite3_realloc(p, n);
667 if( !pNew ){
drh174b9a12010-07-26 11:07:20 +0000668 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000669 db->mallocFailed = 1;
670 }
drh174b9a12010-07-26 11:07:20 +0000671 sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
672 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000673 }
674 }
675 return pNew;
676}
677
drh17435752007-08-16 04:30:38 +0000678/*
679** Attempt to reallocate p. If the reallocation fails, then free p
680** and set the mallocFailed flag in the database connection.
681*/
682void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000683 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000684 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000685 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000686 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000687 }
688 return pNew;
689}
690
drha3152892007-05-05 11:48:52 +0000691/*
692** Make a copy of a string in memory obtained from sqliteMalloc(). These
693** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
694** is because when memory debugging is turned on, these two functions are
695** called via macros that record the current file and line number in the
696** ThreadData structure.
697*/
drh633e6d52008-07-28 19:34:53 +0000698char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000699 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000700 size_t n;
701 if( z==0 ){
702 return 0;
703 }
drhdee0e402009-05-03 20:23:53 +0000704 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000705 assert( (n&0x7fffffff)==n );
706 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000707 if( zNew ){
708 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000709 }
710 return zNew;
711}
712char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000713 char *zNew;
714 if( z==0 ){
715 return 0;
716 }
717 assert( (n&0x7fffffff)==n );
718 zNew = sqlite3DbMallocRaw(db, n+1);
719 if( zNew ){
720 memcpy(zNew, z, n);
721 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000722 }
723 return zNew;
724}
725
drha3152892007-05-05 11:48:52 +0000726/*
drhf089aa42008-07-08 19:34:06 +0000727** Create a string from the zFromat argument and the va_list that follows.
728** Store the string in memory obtained from sqliteMalloc() and make *pz
729** point to that string.
drha3152892007-05-05 11:48:52 +0000730*/
drhf089aa42008-07-08 19:34:06 +0000731void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000732 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000733 char *z;
drha3152892007-05-05 11:48:52 +0000734
drhf089aa42008-07-08 19:34:06 +0000735 va_start(ap, zFormat);
736 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000737 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000738 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000739 *pz = z;
drha3152892007-05-05 11:48:52 +0000740}
741
742
743/*
744** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000745** returning control to the user) that has called sqlite3_malloc or
746** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000747**
748** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000749** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000750** invocation SQLITE_NOMEM is returned instead.
751**
shanebe217792009-03-05 04:20:31 +0000752** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000753** then the connection error-code (the value returned by sqlite3_errcode())
754** is set to SQLITE_NOMEM.
755*/
drha3152892007-05-05 11:48:52 +0000756int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000757 /* If the db handle is not NULL, then we must hold the connection handle
758 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
759 ** is unsafe, as is the call to sqlite3Error().
760 */
761 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000762 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000763 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000764 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000765 rc = SQLITE_NOMEM;
766 }
767 return rc & (db ? db->errMask : 0xff);
768}