blob: f6f75565f3be6c964337208c833c24482f3efe16 [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);
danb0c6a882010-09-02 10:08:41 +0000355 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000356 }else{
357 if( sqlite3GlobalConfig.bMemstat ){
358 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
359 n = mallocWithAlarm(n, &p);
360 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
danb0c6a882010-09-02 10:08:41 +0000361 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000362 }else{
danb0c6a882010-09-02 10:08:41 +0000363 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000364 p = sqlite3GlobalConfig.m.xMalloc(n);
365 }
366 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
367 }
danb0c6a882010-09-02 10:08:41 +0000368 assert( !sqlite3_mutex_held(mem0.mutex) );
369
drhbadc9802010-08-27 17:16:44 +0000370
drhe5ae5732008-06-15 02:51:47 +0000371#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhbadc9802010-08-27 17:16:44 +0000372 /* Verify that no more than two scratch allocations per thread
373 ** are outstanding at one time. (This is only checked in the
drh9ac3fe92008-06-18 18:12:04 +0000374 ** single-threaded case since checking in the multi-threaded case
375 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000376 assert( scratchAllocOut<=1 );
drhbadc9802010-08-27 17:16:44 +0000377 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000378#endif
379
drhe5ae5732008-06-15 02:51:47 +0000380 return p;
381}
drhfacf0302008-06-17 15:12:00 +0000382void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000383 if( p ){
drhbadc9802010-08-27 17:16:44 +0000384
385#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
386 /* Verify that no more than two scratch allocation per thread
387 ** is outstanding at one time. (This is only checked in the
388 ** single-threaded case since checking in the multi-threaded case
389 ** would be much more complicated.) */
390 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
391 scratchAllocOut--;
392#endif
393
394 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
395 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
396 ScratchFreeslot *pSlot;
397 pSlot = (ScratchFreeslot*)p;
398 sqlite3_mutex_enter(mem0.mutex);
399 pSlot->pNext = mem0.pScratchFree;
400 mem0.pScratchFree = pSlot;
401 mem0.nScratchFree++;
402 assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch );
403 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
404 sqlite3_mutex_leave(mem0.mutex);
405 }else{
406 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000407 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drh174b9a12010-07-26 11:07:20 +0000408 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000409 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000410 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000411 int iSize = sqlite3MallocSize(p);
412 sqlite3_mutex_enter(mem0.mutex);
413 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
414 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
drh81ba7d12010-07-26 19:09:31 +0000415 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000416 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000417 sqlite3_mutex_leave(mem0.mutex);
418 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000419 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000420 }
drh9ac3fe92008-06-18 18:12:04 +0000421 }
drhe5ae5732008-06-15 02:51:47 +0000422 }
423}
424
425/*
drh633e6d52008-07-28 19:34:53 +0000426** TRUE if p is a lookaside memory allocation from db
427*/
drh4150ebf2008-10-11 15:38:29 +0000428#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000429static int isLookaside(sqlite3 *db, void *p){
drh174b9a12010-07-26 11:07:20 +0000430 return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000431}
drh4150ebf2008-10-11 15:38:29 +0000432#else
433#define isLookaside(A,B) 0
434#endif
drh633e6d52008-07-28 19:34:53 +0000435
436/*
drhfec00ea2008-06-14 16:56:21 +0000437** Return the size of a memory allocation previously obtained from
438** sqlite3Malloc() or sqlite3_malloc().
439*/
440int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000441 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000442 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
danielk1977075c23a2008-09-01 18:34:20 +0000443 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000444}
drh633e6d52008-07-28 19:34:53 +0000445int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000446 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000447 if( db && isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000448 return db->lookaside.sz;
449 }else{
drh174b9a12010-07-26 11:07:20 +0000450 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
451 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
452 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
danielk1977075c23a2008-09-01 18:34:20 +0000453 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000454 }
455}
drhfec00ea2008-06-14 16:56:21 +0000456
457/*
458** Free memory previously obtained from sqlite3Malloc().
459*/
460void sqlite3_free(void *p){
461 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000462 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
drh107b56e2010-03-12 16:32:53 +0000463 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000464 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000465 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000466 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drheafc43b2010-07-26 18:43:40 +0000467 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000468 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000469 sqlite3_mutex_leave(mem0.mutex);
470 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000471 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000472 }
473}
474
475/*
drh633e6d52008-07-28 19:34:53 +0000476** Free memory that might be associated with a particular database
477** connection.
478*/
479void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000480 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000481 if( db ){
482 if( db->pnBytesFreed ){
483 *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
484 return;
dand46def72010-07-24 11:28:28 +0000485 }
drh174b9a12010-07-26 11:07:20 +0000486 if( isLookaside(db, p) ){
487 LookasideSlot *pBuf = (LookasideSlot*)p;
488 pBuf->pNext = db->lookaside.pFree;
489 db->lookaside.pFree = pBuf;
490 db->lookaside.nOut--;
491 return;
492 }
drh633e6d52008-07-28 19:34:53 +0000493 }
drh174b9a12010-07-26 11:07:20 +0000494 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
495 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
496 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
497 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
498 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000499}
500
501/*
drhfec00ea2008-06-14 16:56:21 +0000502** Change the size of an existing memory allocation
503*/
504void *sqlite3Realloc(void *pOld, int nBytes){
505 int nOld, nNew;
506 void *pNew;
507 if( pOld==0 ){
508 return sqlite3Malloc(nBytes);
509 }
drhb6063cf2009-06-27 00:48:33 +0000510 if( nBytes<=0 ){
drhfec00ea2008-06-14 16:56:21 +0000511 sqlite3_free(pOld);
512 return 0;
513 }
drhb6063cf2009-06-27 00:48:33 +0000514 if( nBytes>=0x7fffff00 ){
515 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
516 return 0;
517 }
drhfec00ea2008-06-14 16:56:21 +0000518 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000519 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
520 ** argument to xRealloc is always a value returned by a prior call to
521 ** xRoundup. */
drh7c6791c2009-08-18 14:48:53 +0000522 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
523 if( nOld==nNew ){
524 pNew = pOld;
525 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000526 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000527 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh7c6791c2009-08-18 14:48:53 +0000528 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
529 mem0.alarmThreshold ){
530 sqlite3MallocAlarm(nNew-nOld);
531 }
drh107b56e2010-03-12 16:32:53 +0000532 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000533 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000534 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
535 if( pNew==0 && mem0.alarmCallback ){
536 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000537 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000538 }
539 if( pNew ){
540 nNew = sqlite3MallocSize(pNew);
541 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000542 }
543 sqlite3_mutex_leave(mem0.mutex);
544 }else{
drh7c6791c2009-08-18 14:48:53 +0000545 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000546 }
547 return pNew;
548}
549
550/*
551** The public interface to sqlite3Realloc. Make sure that the memory
552** subsystem is initialized prior to invoking sqliteRealloc.
553*/
554void *sqlite3_realloc(void *pOld, int n){
555#ifndef SQLITE_OMIT_AUTOINIT
556 if( sqlite3_initialize() ) return 0;
557#endif
558 return sqlite3Realloc(pOld, n);
559}
560
drha3152892007-05-05 11:48:52 +0000561
562/*
drh17435752007-08-16 04:30:38 +0000563** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000564*/
drhfec00ea2008-06-14 16:56:21 +0000565void *sqlite3MallocZero(int n){
566 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000567 if( p ){
568 memset(p, 0, n);
569 }
570 return p;
571}
drh17435752007-08-16 04:30:38 +0000572
573/*
574** Allocate and zero memory. If the allocation fails, make
575** the mallocFailed flag in the connection pointer.
576*/
drhfec00ea2008-06-14 16:56:21 +0000577void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000578 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000579 if( p ){
580 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000581 }
582 return p;
583}
584
585/*
586** Allocate and zero memory. If the allocation fails, make
587** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000588**
589** If db!=0 and db->mallocFailed is true (indicating a prior malloc
590** failure on the same database connection) then always return 0.
591** Hence for a particular database connection, once malloc starts
592** failing, it fails consistently until mallocFailed is reset.
593** This is an important assumption. There are many places in the
594** code that do things like this:
595**
596** int *a = (int*)sqlite3DbMallocRaw(db, 100);
597** int *b = (int*)sqlite3DbMallocRaw(db, 200);
598** if( b ) a[10] = 9;
599**
600** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
601** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000602*/
drhfec00ea2008-06-14 16:56:21 +0000603void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000604 void *p;
drhd9da78a2009-03-24 15:08:09 +0000605 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000606 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000607#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000608 if( db ){
609 LookasideSlot *pBuf;
610 if( db->mallocFailed ){
611 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000612 }
drh633e6d52008-07-28 19:34:53 +0000613 if( db->lookaside.bEnabled && n<=db->lookaside.sz
614 && (pBuf = db->lookaside.pFree)!=0 ){
615 db->lookaside.pFree = pBuf->pNext;
616 db->lookaside.nOut++;
617 if( db->lookaside.nOut>db->lookaside.mxOut ){
618 db->lookaside.mxOut = db->lookaside.nOut;
619 }
620 return (void*)pBuf;
621 }
622 }
drhddecae72008-10-11 17:35:16 +0000623#else
624 if( db && db->mallocFailed ){
625 return 0;
626 }
drh4150ebf2008-10-11 15:38:29 +0000627#endif
drh633e6d52008-07-28 19:34:53 +0000628 p = sqlite3Malloc(n);
629 if( !p && db ){
630 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000631 }
drh174b9a12010-07-26 11:07:20 +0000632 sqlite3MemdebugSetType(p, MEMTYPE_DB |
633 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
drh17435752007-08-16 04:30:38 +0000634 return p;
635}
636
danielk197726783a52007-08-29 14:06:22 +0000637/*
638** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000639** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000640*/
danielk1977a1644fd2007-08-29 12:31:25 +0000641void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
642 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000643 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000644 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000645 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000646 if( p==0 ){
647 return sqlite3DbMallocRaw(db, n);
648 }
649 if( isLookaside(db, p) ){
650 if( n<=db->lookaside.sz ){
651 return p;
652 }
653 pNew = sqlite3DbMallocRaw(db, n);
654 if( pNew ){
655 memcpy(pNew, p, db->lookaside.sz);
656 sqlite3DbFree(db, p);
657 }
658 }else{
drh174b9a12010-07-26 11:07:20 +0000659 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
660 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
drh107b56e2010-03-12 16:32:53 +0000661 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000662 pNew = sqlite3_realloc(p, n);
663 if( !pNew ){
drh174b9a12010-07-26 11:07:20 +0000664 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000665 db->mallocFailed = 1;
666 }
drh174b9a12010-07-26 11:07:20 +0000667 sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
668 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000669 }
670 }
671 return pNew;
672}
673
drh17435752007-08-16 04:30:38 +0000674/*
675** Attempt to reallocate p. If the reallocation fails, then free p
676** and set the mallocFailed flag in the database connection.
677*/
678void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000679 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000680 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000681 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000682 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000683 }
684 return pNew;
685}
686
drha3152892007-05-05 11:48:52 +0000687/*
688** Make a copy of a string in memory obtained from sqliteMalloc(). These
689** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
690** is because when memory debugging is turned on, these two functions are
691** called via macros that record the current file and line number in the
692** ThreadData structure.
693*/
drh633e6d52008-07-28 19:34:53 +0000694char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000695 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000696 size_t n;
697 if( z==0 ){
698 return 0;
699 }
drhdee0e402009-05-03 20:23:53 +0000700 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000701 assert( (n&0x7fffffff)==n );
702 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000703 if( zNew ){
704 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000705 }
706 return zNew;
707}
708char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000709 char *zNew;
710 if( z==0 ){
711 return 0;
712 }
713 assert( (n&0x7fffffff)==n );
714 zNew = sqlite3DbMallocRaw(db, n+1);
715 if( zNew ){
716 memcpy(zNew, z, n);
717 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000718 }
719 return zNew;
720}
721
drha3152892007-05-05 11:48:52 +0000722/*
drhf089aa42008-07-08 19:34:06 +0000723** Create a string from the zFromat argument and the va_list that follows.
724** Store the string in memory obtained from sqliteMalloc() and make *pz
725** point to that string.
drha3152892007-05-05 11:48:52 +0000726*/
drhf089aa42008-07-08 19:34:06 +0000727void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000728 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000729 char *z;
drha3152892007-05-05 11:48:52 +0000730
drhf089aa42008-07-08 19:34:06 +0000731 va_start(ap, zFormat);
732 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000733 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000734 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000735 *pz = z;
drha3152892007-05-05 11:48:52 +0000736}
737
738
739/*
740** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000741** returning control to the user) that has called sqlite3_malloc or
742** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000743**
744** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000745** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000746** invocation SQLITE_NOMEM is returned instead.
747**
shanebe217792009-03-05 04:20:31 +0000748** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000749** then the connection error-code (the value returned by sqlite3_errcode())
750** is set to SQLITE_NOMEM.
751*/
drha3152892007-05-05 11:48:52 +0000752int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000753 /* If the db handle is not NULL, then we must hold the connection handle
754 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
755 ** is unsafe, as is the call to sqlite3Error().
756 */
757 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000758 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000759 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000760 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000761 rc = SQLITE_NOMEM;
762 }
763 return rc & (db ? db->errMask : 0xff);
764}