blob: 0b19cff8b7ed5817d27bdef2e015b31c3bd073df [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
danielk197767e3da72008-08-21 12:19:44 +000065 int nRet = 0;
danielk197767e3da72008-08-21 12:19:44 +000066 nRet += sqlite3PcacheReleaseMemory(n-nRet);
danielk1977dfb316d2008-03-26 18:34:43 +000067 return nRet;
danielk19771e536952007-08-16 10:09:01 +000068#else
danielk197762c14b32008-11-19 09:05:26 +000069 UNUSED_PARAMETER(n);
danielk19771e536952007-08-16 10:09:01 +000070 return SQLITE_OK;
71#endif
drha3152892007-05-05 11:48:52 +000072}
drha3152892007-05-05 11:48:52 +000073
drhfec00ea2008-06-14 16:56:21 +000074/*
drhbadc9802010-08-27 17:16:44 +000075** An instance of the following object records the location of
76** each unused scratch buffer.
77*/
78typedef struct ScratchFreeslot {
79 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
80} ScratchFreeslot;
81
82/*
drhfec00ea2008-06-14 16:56:21 +000083** State information local to the memory allocation subsystem.
84*/
danielk19775c8f8582008-09-02 10:22:00 +000085static SQLITE_WSD struct Mem0Global {
drhfec00ea2008-06-14 16:56:21 +000086 sqlite3_mutex *mutex; /* Mutex to serialize access */
87
88 /*
89 ** The alarm callback and its arguments. The mem0.mutex lock will
90 ** be held while the callback is running. Recursive calls into
91 ** the memory subsystem are allowed, but no new callbacks will be
drhe64ca7b2009-07-16 18:21:17 +000092 ** issued.
drhfec00ea2008-06-14 16:56:21 +000093 */
94 sqlite3_int64 alarmThreshold;
95 void (*alarmCallback)(void*, sqlite3_int64,int);
96 void *alarmArg;
drhfec00ea2008-06-14 16:56:21 +000097
98 /*
drhbadc9802010-08-27 17:16:44 +000099 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
100 ** (so that a range test can be used to determine if an allocation
101 ** being freed came from pScratch) and a pointer to the list of
102 ** unused scratch allocations.
drh9ac3fe92008-06-18 18:12:04 +0000103 */
drhbadc9802010-08-27 17:16:44 +0000104 void *pScratchEnd;
105 ScratchFreeslot *pScratchFree;
106 u32 nScratchFree;
drh50d1b5f2010-08-27 12:21:06 +0000107
108 /*
109 ** True if heap is nearly "full" where "full" is defined by the
110 ** sqlite3_soft_heap_limit() setting.
111 */
112 int nearlyFull;
drhbadc9802010-08-27 17:16:44 +0000113} mem0 = { 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000114
115#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000116
117/*
118** Initialize the memory allocation subsystem.
119*/
120int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000121 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000122 sqlite3MemSetDefault();
123 }
124 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000125 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000126 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000127 }
danielk1977075c23a2008-09-01 18:34:20 +0000128 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
129 && sqlite3GlobalConfig.nScratch>=0 ){
drhbadc9802010-08-27 17:16:44 +0000130 int i, n, sz;
131 ScratchFreeslot *pSlot;
132 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
133 sqlite3GlobalConfig.szScratch = sz;
134 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
135 n = sqlite3GlobalConfig.nScratch;
136 mem0.pScratchFree = pSlot;
137 mem0.nScratchFree = n;
138 for(i=0; i<n-1; i++){
139 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
140 pSlot = pSlot->pNext;
141 }
142 pSlot->pNext = 0;
143 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000144 }else{
drhbadc9802010-08-27 17:16:44 +0000145 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000146 sqlite3GlobalConfig.pScratch = 0;
147 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000148 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000149 }
drh50d1b5f2010-08-27 12:21:06 +0000150 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
151 || sqlite3GlobalConfig.nPage<1 ){
danielk1977075c23a2008-09-01 18:34:20 +0000152 sqlite3GlobalConfig.pPage = 0;
153 sqlite3GlobalConfig.szPage = 0;
drh50d1b5f2010-08-27 12:21:06 +0000154 sqlite3GlobalConfig.nPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000155 }
danielk1977075c23a2008-09-01 18:34:20 +0000156 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000157}
158
159/*
drh50d1b5f2010-08-27 12:21:06 +0000160** Return true if the heap is currently under memory pressure - in other
161** words if the amount of heap used is close to the limit set by
162** sqlite3_soft_heap_limit().
163*/
164int sqlite3HeapNearlyFull(void){
165 return mem0.nearlyFull;
166}
167
168/*
drhfec00ea2008-06-14 16:56:21 +0000169** Deinitialize the memory allocation subsystem.
170*/
171void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000172 if( sqlite3GlobalConfig.m.xShutdown ){
173 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
174 }
drh9ac3fe92008-06-18 18:12:04 +0000175 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000176}
177
178/*
179** Return the amount of memory currently checked out.
180*/
181sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000182 int n, mx;
drhc376a192008-07-14 12:30:54 +0000183 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000184 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000185 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
186 return res;
drhfec00ea2008-06-14 16:56:21 +0000187}
188
189/*
190** Return the maximum amount of memory that has ever been
191** checked out since either the beginning of this process
192** or since the most recent reset.
193*/
194sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000195 int n, mx;
drhc376a192008-07-14 12:30:54 +0000196 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000197 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000198 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000199 return res;
drhfec00ea2008-06-14 16:56:21 +0000200}
201
202/*
203** Change the alarm callback
204*/
shane4a27a282008-09-04 04:32:49 +0000205int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000206 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
207 void *pArg,
208 sqlite3_int64 iThreshold
209){
drh50d1b5f2010-08-27 12:21:06 +0000210 int nUsed;
drhfec00ea2008-06-14 16:56:21 +0000211 sqlite3_mutex_enter(mem0.mutex);
212 mem0.alarmCallback = xCallback;
213 mem0.alarmArg = pArg;
214 mem0.alarmThreshold = iThreshold;
drh50d1b5f2010-08-27 12:21:06 +0000215 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
216 mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
drhfec00ea2008-06-14 16:56:21 +0000217 sqlite3_mutex_leave(mem0.mutex);
218 return SQLITE_OK;
219}
220
shaneeec556d2008-10-12 00:27:53 +0000221#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000222/*
shane4a27a282008-09-04 04:32:49 +0000223** Deprecated external interface. Internal/core SQLite code
224** should call sqlite3MemoryAlarm.
225*/
226int sqlite3_memory_alarm(
227 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
228 void *pArg,
229 sqlite3_int64 iThreshold
230){
231 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
232}
shaneeec556d2008-10-12 00:27:53 +0000233#endif
shane4a27a282008-09-04 04:32:49 +0000234
235/*
drhfec00ea2008-06-14 16:56:21 +0000236** Trigger the alarm
237*/
238static void sqlite3MallocAlarm(int nByte){
239 void (*xCallback)(void*,sqlite3_int64,int);
240 sqlite3_int64 nowUsed;
241 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000242 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000243 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000244 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000245 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000246 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000247 sqlite3_mutex_leave(mem0.mutex);
248 xCallback(pArg, nowUsed, nByte);
249 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000250 mem0.alarmCallback = xCallback;
251 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000252}
253
drhf7141992008-06-19 00:16:08 +0000254/*
255** Do a memory allocation with statistics and alarms. Assume the
256** lock is already held.
257*/
258static int mallocWithAlarm(int n, void **pp){
259 int nFull;
260 void *p;
261 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000262 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000263 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
264 if( mem0.alarmCallback!=0 ){
265 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
266 if( nUsed+nFull >= mem0.alarmThreshold ){
drh50d1b5f2010-08-27 12:21:06 +0000267 mem0.nearlyFull = 1;
drhf7141992008-06-19 00:16:08 +0000268 sqlite3MallocAlarm(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000269 }else{
270 mem0.nearlyFull = 0;
drhf7141992008-06-19 00:16:08 +0000271 }
272 }
danielk1977075c23a2008-09-01 18:34:20 +0000273 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000274#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977d09414c2008-06-19 18:17:49 +0000275 if( p==0 && mem0.alarmCallback ){
276 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000277 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000278 }
drh50d1b5f2010-08-27 12:21:06 +0000279#endif
drhc702c7c2008-07-18 18:56:16 +0000280 if( p ){
281 nFull = sqlite3MallocSize(p);
282 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
drheafc43b2010-07-26 18:43:40 +0000283 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000284 }
drhf7141992008-06-19 00:16:08 +0000285 *pp = p;
286 return nFull;
287}
drhfec00ea2008-06-14 16:56:21 +0000288
289/*
290** Allocate memory. This routine is like sqlite3_malloc() except that it
291** assumes the memory subsystem has already been initialized.
292*/
293void *sqlite3Malloc(int n){
294 void *p;
drhe08ed7e2009-06-26 18:35:16 +0000295 if( n<=0 || n>=0x7fffff00 ){
296 /* A memory allocation of a number of bytes which is near the maximum
297 ** signed integer value might cause an integer overflow inside of the
298 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
299 ** 255 bytes of overhead. SQLite itself will never use anything near
300 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000301 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000302 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000303 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000304 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000305 sqlite3_mutex_leave(mem0.mutex);
306 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000307 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000308 }
309 return p;
310}
311
312/*
313** This version of the memory allocation is for use by the application.
314** First make sure the memory subsystem is initialized, then do the
315** allocation.
316*/
317void *sqlite3_malloc(int n){
318#ifndef SQLITE_OMIT_AUTOINIT
319 if( sqlite3_initialize() ) return 0;
320#endif
321 return sqlite3Malloc(n);
322}
323
324/*
drhe5ae5732008-06-15 02:51:47 +0000325** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000326** xScratchMalloc(). We verify this constraint in the single-threaded
327** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000328** is outstanding clearing it when the allocation is freed.
329*/
330#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000331static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000332#endif
333
334
335/*
336** Allocate memory that is to be used and released right away.
337** This routine is similar to alloca() in that it is not intended
338** for situations where the memory might be held long-term. This
339** routine is intended to get memory to old large transient data
340** structures that would not normally fit on the stack of an
341** embedded processor.
342*/
drhfacf0302008-06-17 15:12:00 +0000343void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000344 void *p;
345 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000346
drhbadc9802010-08-27 17:16:44 +0000347 sqlite3_mutex_enter(mem0.mutex);
348 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
349 p = mem0.pScratchFree;
350 mem0.pScratchFree = mem0.pScratchFree->pNext;
351 mem0.nScratchFree--;
352 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
353 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
354 }else{
355 if( sqlite3GlobalConfig.bMemstat ){
356 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
357 n = mallocWithAlarm(n, &p);
358 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
359 }else{
360 p = sqlite3GlobalConfig.m.xMalloc(n);
361 }
362 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
363 }
364 sqlite3_mutex_leave(mem0.mutex);
365
drhe5ae5732008-06-15 02:51:47 +0000366#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhbadc9802010-08-27 17:16:44 +0000367 /* Verify that no more than two scratch allocations per thread
368 ** are outstanding at one time. (This is only checked in the
drh9ac3fe92008-06-18 18:12:04 +0000369 ** single-threaded case since checking in the multi-threaded case
370 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000371 assert( scratchAllocOut<=1 );
drhbadc9802010-08-27 17:16:44 +0000372 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000373#endif
374
drhe5ae5732008-06-15 02:51:47 +0000375 return p;
376}
drhfacf0302008-06-17 15:12:00 +0000377void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000378 if( p ){
drhbadc9802010-08-27 17:16:44 +0000379
380#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
381 /* Verify that no more than two scratch allocation per thread
382 ** is outstanding at one time. (This is only checked in the
383 ** single-threaded case since checking in the multi-threaded case
384 ** would be much more complicated.) */
385 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
386 scratchAllocOut--;
387#endif
388
389 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
390 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
391 ScratchFreeslot *pSlot;
392 pSlot = (ScratchFreeslot*)p;
393 sqlite3_mutex_enter(mem0.mutex);
394 pSlot->pNext = mem0.pScratchFree;
395 mem0.pScratchFree = pSlot;
396 mem0.nScratchFree++;
397 assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch );
398 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
399 sqlite3_mutex_leave(mem0.mutex);
400 }else{
401 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000402 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drh174b9a12010-07-26 11:07:20 +0000403 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000404 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000405 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000406 int iSize = sqlite3MallocSize(p);
407 sqlite3_mutex_enter(mem0.mutex);
408 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
409 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
drh81ba7d12010-07-26 19:09:31 +0000410 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000411 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000412 sqlite3_mutex_leave(mem0.mutex);
413 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000414 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000415 }
drh9ac3fe92008-06-18 18:12:04 +0000416 }
drhe5ae5732008-06-15 02:51:47 +0000417 }
418}
419
420/*
drh633e6d52008-07-28 19:34:53 +0000421** TRUE if p is a lookaside memory allocation from db
422*/
drh4150ebf2008-10-11 15:38:29 +0000423#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000424static int isLookaside(sqlite3 *db, void *p){
drh174b9a12010-07-26 11:07:20 +0000425 return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000426}
drh4150ebf2008-10-11 15:38:29 +0000427#else
428#define isLookaside(A,B) 0
429#endif
drh633e6d52008-07-28 19:34:53 +0000430
431/*
drhfec00ea2008-06-14 16:56:21 +0000432** Return the size of a memory allocation previously obtained from
433** sqlite3Malloc() or sqlite3_malloc().
434*/
435int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000436 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000437 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
danielk1977075c23a2008-09-01 18:34:20 +0000438 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000439}
drh633e6d52008-07-28 19:34:53 +0000440int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000441 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000442 if( db && isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000443 return db->lookaside.sz;
444 }else{
drh174b9a12010-07-26 11:07:20 +0000445 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
446 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
447 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
danielk1977075c23a2008-09-01 18:34:20 +0000448 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000449 }
450}
drhfec00ea2008-06-14 16:56:21 +0000451
452/*
453** Free memory previously obtained from sqlite3Malloc().
454*/
455void sqlite3_free(void *p){
456 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000457 assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
drh107b56e2010-03-12 16:32:53 +0000458 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000459 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000460 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000461 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drheafc43b2010-07-26 18:43:40 +0000462 sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
danielk1977075c23a2008-09-01 18:34:20 +0000463 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000464 sqlite3_mutex_leave(mem0.mutex);
465 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000466 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000467 }
468}
469
470/*
drh633e6d52008-07-28 19:34:53 +0000471** Free memory that might be associated with a particular database
472** connection.
473*/
474void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000475 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh174b9a12010-07-26 11:07:20 +0000476 if( db ){
477 if( db->pnBytesFreed ){
478 *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
479 return;
dand46def72010-07-24 11:28:28 +0000480 }
drh174b9a12010-07-26 11:07:20 +0000481 if( isLookaside(db, p) ){
482 LookasideSlot *pBuf = (LookasideSlot*)p;
483 pBuf->pNext = db->lookaside.pFree;
484 db->lookaside.pFree = pBuf;
485 db->lookaside.nOut--;
486 return;
487 }
drh633e6d52008-07-28 19:34:53 +0000488 }
drh174b9a12010-07-26 11:07:20 +0000489 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
490 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
491 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
492 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
493 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000494}
495
496/*
drhfec00ea2008-06-14 16:56:21 +0000497** Change the size of an existing memory allocation
498*/
499void *sqlite3Realloc(void *pOld, int nBytes){
500 int nOld, nNew;
501 void *pNew;
502 if( pOld==0 ){
503 return sqlite3Malloc(nBytes);
504 }
drhb6063cf2009-06-27 00:48:33 +0000505 if( nBytes<=0 ){
drhfec00ea2008-06-14 16:56:21 +0000506 sqlite3_free(pOld);
507 return 0;
508 }
drhb6063cf2009-06-27 00:48:33 +0000509 if( nBytes>=0x7fffff00 ){
510 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
511 return 0;
512 }
drhfec00ea2008-06-14 16:56:21 +0000513 nOld = sqlite3MallocSize(pOld);
drh7c6791c2009-08-18 14:48:53 +0000514 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
515 if( nOld==nNew ){
516 pNew = pOld;
517 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000518 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000519 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh7c6791c2009-08-18 14:48:53 +0000520 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
521 mem0.alarmThreshold ){
522 sqlite3MallocAlarm(nNew-nOld);
523 }
drh107b56e2010-03-12 16:32:53 +0000524 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh174b9a12010-07-26 11:07:20 +0000525 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000526 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
527 if( pNew==0 && mem0.alarmCallback ){
528 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000529 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000530 }
531 if( pNew ){
532 nNew = sqlite3MallocSize(pNew);
533 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000534 }
535 sqlite3_mutex_leave(mem0.mutex);
536 }else{
drh7c6791c2009-08-18 14:48:53 +0000537 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000538 }
539 return pNew;
540}
541
542/*
543** The public interface to sqlite3Realloc. Make sure that the memory
544** subsystem is initialized prior to invoking sqliteRealloc.
545*/
546void *sqlite3_realloc(void *pOld, int n){
547#ifndef SQLITE_OMIT_AUTOINIT
548 if( sqlite3_initialize() ) return 0;
549#endif
550 return sqlite3Realloc(pOld, n);
551}
552
drha3152892007-05-05 11:48:52 +0000553
554/*
drh17435752007-08-16 04:30:38 +0000555** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000556*/
drhfec00ea2008-06-14 16:56:21 +0000557void *sqlite3MallocZero(int n){
558 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000559 if( p ){
560 memset(p, 0, n);
561 }
562 return p;
563}
drh17435752007-08-16 04:30:38 +0000564
565/*
566** Allocate and zero memory. If the allocation fails, make
567** the mallocFailed flag in the connection pointer.
568*/
drhfec00ea2008-06-14 16:56:21 +0000569void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000570 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000571 if( p ){
572 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000573 }
574 return p;
575}
576
577/*
578** Allocate and zero memory. If the allocation fails, make
579** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000580**
581** If db!=0 and db->mallocFailed is true (indicating a prior malloc
582** failure on the same database connection) then always return 0.
583** Hence for a particular database connection, once malloc starts
584** failing, it fails consistently until mallocFailed is reset.
585** This is an important assumption. There are many places in the
586** code that do things like this:
587**
588** int *a = (int*)sqlite3DbMallocRaw(db, 100);
589** int *b = (int*)sqlite3DbMallocRaw(db, 200);
590** if( b ) a[10] = 9;
591**
592** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
593** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000594*/
drhfec00ea2008-06-14 16:56:21 +0000595void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000596 void *p;
drhd9da78a2009-03-24 15:08:09 +0000597 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000598 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000599#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000600 if( db ){
601 LookasideSlot *pBuf;
602 if( db->mallocFailed ){
603 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000604 }
drh633e6d52008-07-28 19:34:53 +0000605 if( db->lookaside.bEnabled && n<=db->lookaside.sz
606 && (pBuf = db->lookaside.pFree)!=0 ){
607 db->lookaside.pFree = pBuf->pNext;
608 db->lookaside.nOut++;
609 if( db->lookaside.nOut>db->lookaside.mxOut ){
610 db->lookaside.mxOut = db->lookaside.nOut;
611 }
612 return (void*)pBuf;
613 }
614 }
drhddecae72008-10-11 17:35:16 +0000615#else
616 if( db && db->mallocFailed ){
617 return 0;
618 }
drh4150ebf2008-10-11 15:38:29 +0000619#endif
drh633e6d52008-07-28 19:34:53 +0000620 p = sqlite3Malloc(n);
621 if( !p && db ){
622 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000623 }
drh174b9a12010-07-26 11:07:20 +0000624 sqlite3MemdebugSetType(p, MEMTYPE_DB |
625 ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
drh17435752007-08-16 04:30:38 +0000626 return p;
627}
628
danielk197726783a52007-08-29 14:06:22 +0000629/*
630** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000631** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000632*/
danielk1977a1644fd2007-08-29 12:31:25 +0000633void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
634 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000635 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000636 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000637 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000638 if( p==0 ){
639 return sqlite3DbMallocRaw(db, n);
640 }
641 if( isLookaside(db, p) ){
642 if( n<=db->lookaside.sz ){
643 return p;
644 }
645 pNew = sqlite3DbMallocRaw(db, n);
646 if( pNew ){
647 memcpy(pNew, p, db->lookaside.sz);
648 sqlite3DbFree(db, p);
649 }
650 }else{
drh174b9a12010-07-26 11:07:20 +0000651 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
652 assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
drh107b56e2010-03-12 16:32:53 +0000653 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000654 pNew = sqlite3_realloc(p, n);
655 if( !pNew ){
drh174b9a12010-07-26 11:07:20 +0000656 sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000657 db->mallocFailed = 1;
658 }
drh174b9a12010-07-26 11:07:20 +0000659 sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
660 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000661 }
662 }
663 return pNew;
664}
665
drh17435752007-08-16 04:30:38 +0000666/*
667** Attempt to reallocate p. If the reallocation fails, then free p
668** and set the mallocFailed flag in the database connection.
669*/
670void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000671 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000672 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000673 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000674 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000675 }
676 return pNew;
677}
678
drha3152892007-05-05 11:48:52 +0000679/*
680** Make a copy of a string in memory obtained from sqliteMalloc(). These
681** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
682** is because when memory debugging is turned on, these two functions are
683** called via macros that record the current file and line number in the
684** ThreadData structure.
685*/
drh633e6d52008-07-28 19:34:53 +0000686char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000687 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000688 size_t n;
689 if( z==0 ){
690 return 0;
691 }
drhdee0e402009-05-03 20:23:53 +0000692 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000693 assert( (n&0x7fffffff)==n );
694 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000695 if( zNew ){
696 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000697 }
698 return zNew;
699}
700char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000701 char *zNew;
702 if( z==0 ){
703 return 0;
704 }
705 assert( (n&0x7fffffff)==n );
706 zNew = sqlite3DbMallocRaw(db, n+1);
707 if( zNew ){
708 memcpy(zNew, z, n);
709 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000710 }
711 return zNew;
712}
713
drha3152892007-05-05 11:48:52 +0000714/*
drhf089aa42008-07-08 19:34:06 +0000715** Create a string from the zFromat argument and the va_list that follows.
716** Store the string in memory obtained from sqliteMalloc() and make *pz
717** point to that string.
drha3152892007-05-05 11:48:52 +0000718*/
drhf089aa42008-07-08 19:34:06 +0000719void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000720 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000721 char *z;
drha3152892007-05-05 11:48:52 +0000722
drhf089aa42008-07-08 19:34:06 +0000723 va_start(ap, zFormat);
724 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000725 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000726 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000727 *pz = z;
drha3152892007-05-05 11:48:52 +0000728}
729
730
731/*
732** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000733** returning control to the user) that has called sqlite3_malloc or
734** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000735**
736** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000737** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000738** invocation SQLITE_NOMEM is returned instead.
739**
shanebe217792009-03-05 04:20:31 +0000740** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000741** then the connection error-code (the value returned by sqlite3_errcode())
742** is set to SQLITE_NOMEM.
743*/
drha3152892007-05-05 11:48:52 +0000744int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000745 /* If the db handle is not NULL, then we must hold the connection handle
746 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
747 ** is unsafe, as is the call to sqlite3Error().
748 */
749 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000750 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000751 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000752 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000753 rc = SQLITE_NOMEM;
754 }
755 return rc & (db ? db->errMask : 0xff);
756}