blob: 9986f377d807951dbf189844317e4530f5db2c4f [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.
14**
drhe64ca7b2009-07-16 18:21:17 +000015** $Id: malloc.c,v 1.65 2009/07/16 18:21:18 drh Exp $
drha3152892007-05-05 11:48:52 +000016*/
17#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000018#include <stdarg.h>
drha3152892007-05-05 11:48:52 +000019
20/*
drhb21c8cd2007-08-21 19:33:56 +000021** This routine runs when the memory allocator sees that the
22** total memory allocation is about to exceed the soft heap
23** limit.
24*/
25static void softHeapLimitEnforcer(
26 void *NotUsed,
danielk197762c14b32008-11-19 09:05:26 +000027 sqlite3_int64 NotUsed2,
drh153c62c2007-08-24 03:51:33 +000028 int allocSize
drhb21c8cd2007-08-21 19:33:56 +000029){
danielk197762c14b32008-11-19 09:05:26 +000030 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhb21c8cd2007-08-21 19:33:56 +000031 sqlite3_release_memory(allocSize);
32}
33
34/*
danielk197784680242008-06-23 11:11:35 +000035** Set the soft heap-size limit for the library. Passing a zero or
36** negative value indicates no limit.
drha3152892007-05-05 11:48:52 +000037*/
38void sqlite3_soft_heap_limit(int n){
drhb21c8cd2007-08-21 19:33:56 +000039 sqlite3_uint64 iLimit;
40 int overage;
41 if( n<0 ){
42 iLimit = 0;
43 }else{
44 iLimit = n;
drha3152892007-05-05 11:48:52 +000045 }
drh9ac3fe92008-06-18 18:12:04 +000046 sqlite3_initialize();
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;
66#if 0
67 nRet += sqlite3VdbeReleaseMemory(n);
68#endif
69 nRet += sqlite3PcacheReleaseMemory(n-nRet);
danielk1977dfb316d2008-03-26 18:34:43 +000070 return nRet;
danielk19771e536952007-08-16 10:09:01 +000071#else
danielk197762c14b32008-11-19 09:05:26 +000072 UNUSED_PARAMETER(n);
danielk19771e536952007-08-16 10:09:01 +000073 return SQLITE_OK;
74#endif
drha3152892007-05-05 11:48:52 +000075}
drha3152892007-05-05 11:48:52 +000076
drhfec00ea2008-06-14 16:56:21 +000077/*
78** State information local to the memory allocation subsystem.
79*/
danielk19775c8f8582008-09-02 10:22:00 +000080static SQLITE_WSD struct Mem0Global {
danielk197723bf0f42008-09-02 17:52:51 +000081 /* Number of free pages for scratch and page-cache memory */
82 u32 nScratchFree;
83 u32 nPageFree;
84
drhfec00ea2008-06-14 16:56:21 +000085 sqlite3_mutex *mutex; /* Mutex to serialize access */
86
87 /*
88 ** The alarm callback and its arguments. The mem0.mutex lock will
89 ** be held while the callback is running. Recursive calls into
90 ** the memory subsystem are allowed, but no new callbacks will be
drhe64ca7b2009-07-16 18:21:17 +000091 ** issued.
drhfec00ea2008-06-14 16:56:21 +000092 */
93 sqlite3_int64 alarmThreshold;
94 void (*alarmCallback)(void*, sqlite3_int64,int);
95 void *alarmArg;
drhfec00ea2008-06-14 16:56:21 +000096
97 /*
danielk1977075c23a2008-09-01 18:34:20 +000098 ** Pointers to the end of sqlite3GlobalConfig.pScratch and
99 ** sqlite3GlobalConfig.pPage to a block of memory that records
drh9ac3fe92008-06-18 18:12:04 +0000100 ** which pages are available.
101 */
102 u32 *aScratchFree;
103 u32 *aPageFree;
drhe64ca7b2009-07-16 18:21:17 +0000104} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000105
106#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000107
108/*
109** Initialize the memory allocation subsystem.
110*/
111int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000112 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000113 sqlite3MemSetDefault();
114 }
115 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000116 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000117 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000118 }
danielk1977075c23a2008-09-01 18:34:20 +0000119 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
120 && sqlite3GlobalConfig.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000121 int i;
danielk1977bc739712009-03-23 04:33:32 +0000122 sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4);
danielk1977075c23a2008-09-01 18:34:20 +0000123 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
124 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
125 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
126 mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
drh9ac3fe92008-06-18 18:12:04 +0000127 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000128 sqlite3GlobalConfig.pScratch = 0;
129 sqlite3GlobalConfig.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000130 }
danielk1977075c23a2008-09-01 18:34:20 +0000131 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
132 && sqlite3GlobalConfig.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000133 int i;
drh0a60a382008-07-31 17:16:05 +0000134 int overhead;
danielk1977bc739712009-03-23 04:33:32 +0000135 int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage);
danielk1977075c23a2008-09-01 18:34:20 +0000136 int n = sqlite3GlobalConfig.nPage;
drh0a60a382008-07-31 17:16:05 +0000137 overhead = (4*n + sz - 1)/sz;
danielk1977075c23a2008-09-01 18:34:20 +0000138 sqlite3GlobalConfig.nPage -= overhead;
139 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
140 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
141 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
142 mem0.nPageFree = sqlite3GlobalConfig.nPage;
drh9ac3fe92008-06-18 18:12:04 +0000143 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000144 sqlite3GlobalConfig.pPage = 0;
145 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000146 }
danielk1977075c23a2008-09-01 18:34:20 +0000147 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000148}
149
150/*
151** Deinitialize the memory allocation subsystem.
152*/
153void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000154 if( sqlite3GlobalConfig.m.xShutdown ){
155 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
156 }
drh9ac3fe92008-06-18 18:12:04 +0000157 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000158}
159
160/*
161** Return the amount of memory currently checked out.
162*/
163sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000164 int n, mx;
drhc376a192008-07-14 12:30:54 +0000165 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000166 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000167 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
168 return res;
drhfec00ea2008-06-14 16:56:21 +0000169}
170
171/*
172** Return the maximum amount of memory that has ever been
173** checked out since either the beginning of this process
174** or since the most recent reset.
175*/
176sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000177 int n, mx;
drhc376a192008-07-14 12:30:54 +0000178 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000179 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000180 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000181 return res;
drhfec00ea2008-06-14 16:56:21 +0000182}
183
184/*
185** Change the alarm callback
186*/
shane4a27a282008-09-04 04:32:49 +0000187int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000188 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
189 void *pArg,
190 sqlite3_int64 iThreshold
191){
192 sqlite3_mutex_enter(mem0.mutex);
193 mem0.alarmCallback = xCallback;
194 mem0.alarmArg = pArg;
195 mem0.alarmThreshold = iThreshold;
196 sqlite3_mutex_leave(mem0.mutex);
197 return SQLITE_OK;
198}
199
shaneeec556d2008-10-12 00:27:53 +0000200#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000201/*
shane4a27a282008-09-04 04:32:49 +0000202** Deprecated external interface. Internal/core SQLite code
203** should call sqlite3MemoryAlarm.
204*/
205int sqlite3_memory_alarm(
206 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
207 void *pArg,
208 sqlite3_int64 iThreshold
209){
210 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
211}
shaneeec556d2008-10-12 00:27:53 +0000212#endif
shane4a27a282008-09-04 04:32:49 +0000213
214/*
drhfec00ea2008-06-14 16:56:21 +0000215** Trigger the alarm
216*/
217static void sqlite3MallocAlarm(int nByte){
218 void (*xCallback)(void*,sqlite3_int64,int);
219 sqlite3_int64 nowUsed;
220 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000221 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000222 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000223 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000224 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000225 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000226 sqlite3_mutex_leave(mem0.mutex);
227 xCallback(pArg, nowUsed, nByte);
228 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000229 mem0.alarmCallback = xCallback;
230 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000231}
232
drhf7141992008-06-19 00:16:08 +0000233/*
234** Do a memory allocation with statistics and alarms. Assume the
235** lock is already held.
236*/
237static int mallocWithAlarm(int n, void **pp){
238 int nFull;
239 void *p;
240 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000241 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000242 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
243 if( mem0.alarmCallback!=0 ){
244 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
245 if( nUsed+nFull >= mem0.alarmThreshold ){
246 sqlite3MallocAlarm(nFull);
247 }
248 }
danielk1977075c23a2008-09-01 18:34:20 +0000249 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000250 if( p==0 && mem0.alarmCallback ){
251 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000252 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000253 }
drhc702c7c2008-07-18 18:56:16 +0000254 if( p ){
255 nFull = sqlite3MallocSize(p);
256 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
257 }
drhf7141992008-06-19 00:16:08 +0000258 *pp = p;
259 return nFull;
260}
drhfec00ea2008-06-14 16:56:21 +0000261
262/*
263** Allocate memory. This routine is like sqlite3_malloc() except that it
264** assumes the memory subsystem has already been initialized.
265*/
266void *sqlite3Malloc(int n){
267 void *p;
drhe08ed7e2009-06-26 18:35:16 +0000268 if( n<=0 || n>=0x7fffff00 ){
269 /* A memory allocation of a number of bytes which is near the maximum
270 ** signed integer value might cause an integer overflow inside of the
271 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
272 ** 255 bytes of overhead. SQLite itself will never use anything near
273 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000274 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000275 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000276 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000277 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000278 sqlite3_mutex_leave(mem0.mutex);
279 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000280 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000281 }
282 return p;
283}
284
285/*
286** This version of the memory allocation is for use by the application.
287** First make sure the memory subsystem is initialized, then do the
288** allocation.
289*/
290void *sqlite3_malloc(int n){
291#ifndef SQLITE_OMIT_AUTOINIT
292 if( sqlite3_initialize() ) return 0;
293#endif
294 return sqlite3Malloc(n);
295}
296
297/*
drhe5ae5732008-06-15 02:51:47 +0000298** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000299** xScratchMalloc(). We verify this constraint in the single-threaded
300** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000301** is outstanding clearing it when the allocation is freed.
302*/
303#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000304static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000305#endif
306
307
308/*
309** Allocate memory that is to be used and released right away.
310** This routine is similar to alloca() in that it is not intended
311** for situations where the memory might be held long-term. This
312** routine is intended to get memory to old large transient data
313** structures that would not normally fit on the stack of an
314** embedded processor.
315*/
drhfacf0302008-06-17 15:12:00 +0000316void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000317 void *p;
318 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000319
drhe5ae5732008-06-15 02:51:47 +0000320#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000321 /* Verify that no more than one scratch allocation per thread
322 ** is outstanding at one time. (This is only checked in the
323 ** single-threaded case since checking in the multi-threaded case
324 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000325 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000326#endif
drh9ac3fe92008-06-18 18:12:04 +0000327
danielk1977075c23a2008-09-01 18:34:20 +0000328 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000329 goto scratch_overflow;
330 }else{
331 sqlite3_mutex_enter(mem0.mutex);
332 if( mem0.nScratchFree==0 ){
333 sqlite3_mutex_leave(mem0.mutex);
334 goto scratch_overflow;
335 }else{
336 int i;
337 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000338 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000339 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000340 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000341 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000342 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
shane15301592008-12-16 17:20:38 +0000343 assert( (((u8*)p - (u8*)0) & 7)==0 );
drhf7141992008-06-19 00:16:08 +0000344 }
drhe5ae5732008-06-15 02:51:47 +0000345 }
drhf7141992008-06-19 00:16:08 +0000346#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
347 scratchAllocOut = p!=0;
348#endif
349
drhe5ae5732008-06-15 02:51:47 +0000350 return p;
drhf7141992008-06-19 00:16:08 +0000351
352scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000353 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000354 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000355 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000356 n = mallocWithAlarm(n, &p);
357 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
358 sqlite3_mutex_leave(mem0.mutex);
359 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000360 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000361 }
362#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
363 scratchAllocOut = p!=0;
364#endif
365 return p;
drhe5ae5732008-06-15 02:51:47 +0000366}
drhfacf0302008-06-17 15:12:00 +0000367void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000368 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000369
drhe5ae5732008-06-15 02:51:47 +0000370#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000371 /* Verify that no more than one scratch allocation per thread
372 ** is outstanding at one time. (This is only checked in the
373 ** single-threaded case since checking in the multi-threaded case
374 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000375 assert( scratchAllocOut==1 );
376 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000377#endif
drh9ac3fe92008-06-18 18:12:04 +0000378
danielk1977075c23a2008-09-01 18:34:20 +0000379 if( sqlite3GlobalConfig.pScratch==0
380 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000381 || p>=(void*)mem0.aScratchFree ){
danielk1977075c23a2008-09-01 18:34:20 +0000382 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000383 int iSize = sqlite3MallocSize(p);
384 sqlite3_mutex_enter(mem0.mutex);
385 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
386 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000387 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000388 sqlite3_mutex_leave(mem0.mutex);
389 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000390 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000391 }
drh9ac3fe92008-06-18 18:12:04 +0000392 }else{
393 int i;
drh1bd10f82008-12-10 21:19:56 +0000394 i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
danielk1977075c23a2008-09-01 18:34:20 +0000395 i /= sqlite3GlobalConfig.szScratch;
396 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000397 sqlite3_mutex_enter(mem0.mutex);
danielk197700e13612008-11-17 19:18:54 +0000398 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000399 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000400 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000401 sqlite3_mutex_leave(mem0.mutex);
402 }
drhe5ae5732008-06-15 02:51:47 +0000403 }
404}
405
406/*
drh633e6d52008-07-28 19:34:53 +0000407** TRUE if p is a lookaside memory allocation from db
408*/
drh4150ebf2008-10-11 15:38:29 +0000409#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000410static int isLookaside(sqlite3 *db, void *p){
411 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
412}
drh4150ebf2008-10-11 15:38:29 +0000413#else
414#define isLookaside(A,B) 0
415#endif
drh633e6d52008-07-28 19:34:53 +0000416
417/*
drhfec00ea2008-06-14 16:56:21 +0000418** Return the size of a memory allocation previously obtained from
419** sqlite3Malloc() or sqlite3_malloc().
420*/
421int sqlite3MallocSize(void *p){
danielk1977075c23a2008-09-01 18:34:20 +0000422 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000423}
drh633e6d52008-07-28 19:34:53 +0000424int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000425 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh6a1e0712008-12-05 15:24:15 +0000426 if( p==0 ){
427 return 0;
428 }else if( isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000429 return db->lookaside.sz;
430 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000431 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000432 }
433}
drhfec00ea2008-06-14 16:56:21 +0000434
435/*
436** Free memory previously obtained from sqlite3Malloc().
437*/
438void sqlite3_free(void *p){
439 if( p==0 ) return;
danielk1977075c23a2008-09-01 18:34:20 +0000440 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000441 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000442 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000443 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000444 sqlite3_mutex_leave(mem0.mutex);
445 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000446 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000447 }
448}
449
450/*
drh633e6d52008-07-28 19:34:53 +0000451** Free memory that might be associated with a particular database
452** connection.
453*/
454void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000455 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh633e6d52008-07-28 19:34:53 +0000456 if( isLookaside(db, p) ){
457 LookasideSlot *pBuf = (LookasideSlot*)p;
458 pBuf->pNext = db->lookaside.pFree;
459 db->lookaside.pFree = pBuf;
460 db->lookaside.nOut--;
461 }else{
462 sqlite3_free(p);
463 }
464}
465
466/*
drhfec00ea2008-06-14 16:56:21 +0000467** Change the size of an existing memory allocation
468*/
469void *sqlite3Realloc(void *pOld, int nBytes){
470 int nOld, nNew;
471 void *pNew;
472 if( pOld==0 ){
473 return sqlite3Malloc(nBytes);
474 }
drhb6063cf2009-06-27 00:48:33 +0000475 if( nBytes<=0 ){
drhfec00ea2008-06-14 16:56:21 +0000476 sqlite3_free(pOld);
477 return 0;
478 }
drhb6063cf2009-06-27 00:48:33 +0000479 if( nBytes>=0x7fffff00 ){
480 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
481 return 0;
482 }
drhfec00ea2008-06-14 16:56:21 +0000483 nOld = sqlite3MallocSize(pOld);
danielk1977075c23a2008-09-01 18:34:20 +0000484 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000485 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000486 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000487 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000488 if( nOld==nNew ){
489 pNew = pOld;
490 }else{
drhf7141992008-06-19 00:16:08 +0000491 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
492 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000493 sqlite3MallocAlarm(nNew-nOld);
494 }
danielk1977075c23a2008-09-01 18:34:20 +0000495 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
danielk1977d09414c2008-06-19 18:17:49 +0000496 if( pNew==0 && mem0.alarmCallback ){
497 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000498 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000499 }
500 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000501 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000502 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000503 }
504 }
505 sqlite3_mutex_leave(mem0.mutex);
506 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000507 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000508 }
509 return pNew;
510}
511
512/*
513** The public interface to sqlite3Realloc. Make sure that the memory
514** subsystem is initialized prior to invoking sqliteRealloc.
515*/
516void *sqlite3_realloc(void *pOld, int n){
517#ifndef SQLITE_OMIT_AUTOINIT
518 if( sqlite3_initialize() ) return 0;
519#endif
520 return sqlite3Realloc(pOld, n);
521}
522
drha3152892007-05-05 11:48:52 +0000523
524/*
drh17435752007-08-16 04:30:38 +0000525** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000526*/
drhfec00ea2008-06-14 16:56:21 +0000527void *sqlite3MallocZero(int n){
528 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000529 if( p ){
530 memset(p, 0, n);
531 }
532 return p;
533}
drh17435752007-08-16 04:30:38 +0000534
535/*
536** Allocate and zero memory. If the allocation fails, make
537** the mallocFailed flag in the connection pointer.
538*/
drhfec00ea2008-06-14 16:56:21 +0000539void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000540 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000541 if( p ){
542 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000543 }
544 return p;
545}
546
547/*
548** Allocate and zero memory. If the allocation fails, make
549** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000550**
551** If db!=0 and db->mallocFailed is true (indicating a prior malloc
552** failure on the same database connection) then always return 0.
553** Hence for a particular database connection, once malloc starts
554** failing, it fails consistently until mallocFailed is reset.
555** This is an important assumption. There are many places in the
556** code that do things like this:
557**
558** int *a = (int*)sqlite3DbMallocRaw(db, 100);
559** int *b = (int*)sqlite3DbMallocRaw(db, 200);
560** if( b ) a[10] = 9;
561**
562** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
563** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000564*/
drhfec00ea2008-06-14 16:56:21 +0000565void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000566 void *p;
drhd9da78a2009-03-24 15:08:09 +0000567 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh4150ebf2008-10-11 15:38:29 +0000568#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000569 if( db ){
570 LookasideSlot *pBuf;
571 if( db->mallocFailed ){
572 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000573 }
drh633e6d52008-07-28 19:34:53 +0000574 if( db->lookaside.bEnabled && n<=db->lookaside.sz
575 && (pBuf = db->lookaside.pFree)!=0 ){
576 db->lookaside.pFree = pBuf->pNext;
577 db->lookaside.nOut++;
578 if( db->lookaside.nOut>db->lookaside.mxOut ){
579 db->lookaside.mxOut = db->lookaside.nOut;
580 }
581 return (void*)pBuf;
582 }
583 }
drhddecae72008-10-11 17:35:16 +0000584#else
585 if( db && db->mallocFailed ){
586 return 0;
587 }
drh4150ebf2008-10-11 15:38:29 +0000588#endif
drh633e6d52008-07-28 19:34:53 +0000589 p = sqlite3Malloc(n);
590 if( !p && db ){
591 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000592 }
593 return p;
594}
595
danielk197726783a52007-08-29 14:06:22 +0000596/*
597** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000598** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000599*/
danielk1977a1644fd2007-08-29 12:31:25 +0000600void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
601 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000602 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000603 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000604 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000605 if( p==0 ){
606 return sqlite3DbMallocRaw(db, n);
607 }
608 if( isLookaside(db, p) ){
609 if( n<=db->lookaside.sz ){
610 return p;
611 }
612 pNew = sqlite3DbMallocRaw(db, n);
613 if( pNew ){
614 memcpy(pNew, p, db->lookaside.sz);
615 sqlite3DbFree(db, p);
616 }
617 }else{
618 pNew = sqlite3_realloc(p, n);
619 if( !pNew ){
620 db->mallocFailed = 1;
621 }
danielk1977a1644fd2007-08-29 12:31:25 +0000622 }
623 }
624 return pNew;
625}
626
drh17435752007-08-16 04:30:38 +0000627/*
628** Attempt to reallocate p. If the reallocation fails, then free p
629** and set the mallocFailed flag in the database connection.
630*/
631void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000632 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000633 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000634 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000635 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000636 }
637 return pNew;
638}
639
drha3152892007-05-05 11:48:52 +0000640/*
641** Make a copy of a string in memory obtained from sqliteMalloc(). These
642** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
643** is because when memory debugging is turned on, these two functions are
644** called via macros that record the current file and line number in the
645** ThreadData structure.
646*/
drh633e6d52008-07-28 19:34:53 +0000647char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000648 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000649 size_t n;
650 if( z==0 ){
651 return 0;
652 }
drhdee0e402009-05-03 20:23:53 +0000653 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000654 assert( (n&0x7fffffff)==n );
655 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000656 if( zNew ){
657 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000658 }
659 return zNew;
660}
661char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000662 char *zNew;
663 if( z==0 ){
664 return 0;
665 }
666 assert( (n&0x7fffffff)==n );
667 zNew = sqlite3DbMallocRaw(db, n+1);
668 if( zNew ){
669 memcpy(zNew, z, n);
670 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000671 }
672 return zNew;
673}
674
drha3152892007-05-05 11:48:52 +0000675/*
drhf089aa42008-07-08 19:34:06 +0000676** Create a string from the zFromat argument and the va_list that follows.
677** Store the string in memory obtained from sqliteMalloc() and make *pz
678** point to that string.
drha3152892007-05-05 11:48:52 +0000679*/
drhf089aa42008-07-08 19:34:06 +0000680void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000681 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000682 char *z;
drha3152892007-05-05 11:48:52 +0000683
drhf089aa42008-07-08 19:34:06 +0000684 va_start(ap, zFormat);
685 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000686 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000687 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000688 *pz = z;
drha3152892007-05-05 11:48:52 +0000689}
690
691
692/*
693** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000694** returning control to the user) that has called sqlite3_malloc or
695** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000696**
697** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000698** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000699** invocation SQLITE_NOMEM is returned instead.
700**
shanebe217792009-03-05 04:20:31 +0000701** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000702** then the connection error-code (the value returned by sqlite3_errcode())
703** is set to SQLITE_NOMEM.
704*/
drha3152892007-05-05 11:48:52 +0000705int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000706 /* If the db handle is not NULL, then we must hold the connection handle
707 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
708 ** is unsafe, as is the call to sqlite3Error().
709 */
710 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000711 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000712 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000713 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000714 rc = SQLITE_NOMEM;
715 }
716 return rc & (db ? db->errMask : 0xff);
717}