blob: 6d3b3002ebd7e761522acec9b7be8e7a02c8b244 [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/*
75** State information local to the memory allocation subsystem.
76*/
danielk19775c8f8582008-09-02 10:22:00 +000077static SQLITE_WSD struct Mem0Global {
danielk197723bf0f42008-09-02 17:52:51 +000078 /* Number of free pages for scratch and page-cache memory */
79 u32 nScratchFree;
80 u32 nPageFree;
81
drhfec00ea2008-06-14 16:56:21 +000082 sqlite3_mutex *mutex; /* Mutex to serialize access */
83
84 /*
85 ** The alarm callback and its arguments. The mem0.mutex lock will
86 ** be held while the callback is running. Recursive calls into
87 ** the memory subsystem are allowed, but no new callbacks will be
drhe64ca7b2009-07-16 18:21:17 +000088 ** issued.
drhfec00ea2008-06-14 16:56:21 +000089 */
90 sqlite3_int64 alarmThreshold;
91 void (*alarmCallback)(void*, sqlite3_int64,int);
92 void *alarmArg;
drhfec00ea2008-06-14 16:56:21 +000093
94 /*
danielk1977075c23a2008-09-01 18:34:20 +000095 ** Pointers to the end of sqlite3GlobalConfig.pScratch and
96 ** sqlite3GlobalConfig.pPage to a block of memory that records
drh9ac3fe92008-06-18 18:12:04 +000097 ** which pages are available.
98 */
99 u32 *aScratchFree;
100 u32 *aPageFree;
drhe64ca7b2009-07-16 18:21:17 +0000101} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000102
103#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000104
105/*
106** Initialize the memory allocation subsystem.
107*/
108int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000109 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000110 sqlite3MemSetDefault();
111 }
112 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000113 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000114 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000115 }
danielk1977075c23a2008-09-01 18:34:20 +0000116 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
117 && sqlite3GlobalConfig.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000118 int i;
danielk1977bc739712009-03-23 04:33:32 +0000119 sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4);
danielk1977075c23a2008-09-01 18:34:20 +0000120 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
121 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
122 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
123 mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
drh9ac3fe92008-06-18 18:12:04 +0000124 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000125 sqlite3GlobalConfig.pScratch = 0;
126 sqlite3GlobalConfig.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000127 }
danielk1977075c23a2008-09-01 18:34:20 +0000128 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
129 && sqlite3GlobalConfig.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000130 int i;
drh0a60a382008-07-31 17:16:05 +0000131 int overhead;
danielk1977bc739712009-03-23 04:33:32 +0000132 int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage);
danielk1977075c23a2008-09-01 18:34:20 +0000133 int n = sqlite3GlobalConfig.nPage;
drh0a60a382008-07-31 17:16:05 +0000134 overhead = (4*n + sz - 1)/sz;
danielk1977075c23a2008-09-01 18:34:20 +0000135 sqlite3GlobalConfig.nPage -= overhead;
136 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
137 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
138 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
139 mem0.nPageFree = sqlite3GlobalConfig.nPage;
drh9ac3fe92008-06-18 18:12:04 +0000140 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000141 sqlite3GlobalConfig.pPage = 0;
142 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000143 }
danielk1977075c23a2008-09-01 18:34:20 +0000144 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000145}
146
147/*
148** Deinitialize the memory allocation subsystem.
149*/
150void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000151 if( sqlite3GlobalConfig.m.xShutdown ){
152 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
153 }
drh9ac3fe92008-06-18 18:12:04 +0000154 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000155}
156
157/*
158** Return the amount of memory currently checked out.
159*/
160sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000161 int n, mx;
drhc376a192008-07-14 12:30:54 +0000162 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000163 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000164 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
165 return res;
drhfec00ea2008-06-14 16:56:21 +0000166}
167
168/*
169** Return the maximum amount of memory that has ever been
170** checked out since either the beginning of this process
171** or since the most recent reset.
172*/
173sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000174 int n, mx;
drhc376a192008-07-14 12:30:54 +0000175 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000176 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000177 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000178 return res;
drhfec00ea2008-06-14 16:56:21 +0000179}
180
181/*
182** Change the alarm callback
183*/
shane4a27a282008-09-04 04:32:49 +0000184int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000185 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
186 void *pArg,
187 sqlite3_int64 iThreshold
188){
189 sqlite3_mutex_enter(mem0.mutex);
190 mem0.alarmCallback = xCallback;
191 mem0.alarmArg = pArg;
192 mem0.alarmThreshold = iThreshold;
193 sqlite3_mutex_leave(mem0.mutex);
194 return SQLITE_OK;
195}
196
shaneeec556d2008-10-12 00:27:53 +0000197#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000198/*
shane4a27a282008-09-04 04:32:49 +0000199** Deprecated external interface. Internal/core SQLite code
200** should call sqlite3MemoryAlarm.
201*/
202int sqlite3_memory_alarm(
203 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
204 void *pArg,
205 sqlite3_int64 iThreshold
206){
207 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
208}
shaneeec556d2008-10-12 00:27:53 +0000209#endif
shane4a27a282008-09-04 04:32:49 +0000210
211/*
drhfec00ea2008-06-14 16:56:21 +0000212** Trigger the alarm
213*/
214static void sqlite3MallocAlarm(int nByte){
215 void (*xCallback)(void*,sqlite3_int64,int);
216 sqlite3_int64 nowUsed;
217 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000218 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000219 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000220 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000221 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000222 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000223 sqlite3_mutex_leave(mem0.mutex);
224 xCallback(pArg, nowUsed, nByte);
225 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000226 mem0.alarmCallback = xCallback;
227 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000228}
229
drhf7141992008-06-19 00:16:08 +0000230/*
231** Do a memory allocation with statistics and alarms. Assume the
232** lock is already held.
233*/
234static int mallocWithAlarm(int n, void **pp){
235 int nFull;
236 void *p;
237 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000238 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000239 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
240 if( mem0.alarmCallback!=0 ){
241 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
242 if( nUsed+nFull >= mem0.alarmThreshold ){
243 sqlite3MallocAlarm(nFull);
244 }
245 }
danielk1977075c23a2008-09-01 18:34:20 +0000246 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000247 if( p==0 && mem0.alarmCallback ){
248 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000249 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000250 }
drhc702c7c2008-07-18 18:56:16 +0000251 if( p ){
252 nFull = sqlite3MallocSize(p);
253 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
254 }
drhf7141992008-06-19 00:16:08 +0000255 *pp = p;
256 return nFull;
257}
drhfec00ea2008-06-14 16:56:21 +0000258
259/*
260** Allocate memory. This routine is like sqlite3_malloc() except that it
261** assumes the memory subsystem has already been initialized.
262*/
263void *sqlite3Malloc(int n){
264 void *p;
drhe08ed7e2009-06-26 18:35:16 +0000265 if( n<=0 || n>=0x7fffff00 ){
266 /* A memory allocation of a number of bytes which is near the maximum
267 ** signed integer value might cause an integer overflow inside of the
268 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
269 ** 255 bytes of overhead. SQLite itself will never use anything near
270 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000271 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000272 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000273 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000274 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000275 sqlite3_mutex_leave(mem0.mutex);
276 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000277 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000278 }
279 return p;
280}
281
282/*
283** This version of the memory allocation is for use by the application.
284** First make sure the memory subsystem is initialized, then do the
285** allocation.
286*/
287void *sqlite3_malloc(int n){
288#ifndef SQLITE_OMIT_AUTOINIT
289 if( sqlite3_initialize() ) return 0;
290#endif
291 return sqlite3Malloc(n);
292}
293
294/*
drhe5ae5732008-06-15 02:51:47 +0000295** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000296** xScratchMalloc(). We verify this constraint in the single-threaded
297** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000298** is outstanding clearing it when the allocation is freed.
299*/
300#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000301static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000302#endif
303
304
305/*
306** Allocate memory that is to be used and released right away.
307** This routine is similar to alloca() in that it is not intended
308** for situations where the memory might be held long-term. This
309** routine is intended to get memory to old large transient data
310** structures that would not normally fit on the stack of an
311** embedded processor.
312*/
drhfacf0302008-06-17 15:12:00 +0000313void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000314 void *p;
315 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000316
drhe5ae5732008-06-15 02:51:47 +0000317#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000318 /* Verify that no more than one scratch allocation per thread
319 ** is outstanding at one time. (This is only checked in the
320 ** single-threaded case since checking in the multi-threaded case
321 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000322 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000323#endif
drh9ac3fe92008-06-18 18:12:04 +0000324
danielk1977075c23a2008-09-01 18:34:20 +0000325 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000326 goto scratch_overflow;
327 }else{
328 sqlite3_mutex_enter(mem0.mutex);
329 if( mem0.nScratchFree==0 ){
330 sqlite3_mutex_leave(mem0.mutex);
331 goto scratch_overflow;
332 }else{
333 int i;
334 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000335 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000336 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000337 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000338 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000339 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
shane15301592008-12-16 17:20:38 +0000340 assert( (((u8*)p - (u8*)0) & 7)==0 );
drhf7141992008-06-19 00:16:08 +0000341 }
drhe5ae5732008-06-15 02:51:47 +0000342 }
drhf7141992008-06-19 00:16:08 +0000343#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
344 scratchAllocOut = p!=0;
345#endif
346
drhe5ae5732008-06-15 02:51:47 +0000347 return p;
drhf7141992008-06-19 00:16:08 +0000348
349scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000350 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000351 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000352 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000353 n = mallocWithAlarm(n, &p);
354 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
355 sqlite3_mutex_leave(mem0.mutex);
356 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000357 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000358 }
drh107b56e2010-03-12 16:32:53 +0000359 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
drhf7141992008-06-19 00:16:08 +0000360#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
361 scratchAllocOut = p!=0;
362#endif
363 return p;
drhe5ae5732008-06-15 02:51:47 +0000364}
drhfacf0302008-06-17 15:12:00 +0000365void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000366 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000367
drhe5ae5732008-06-15 02:51:47 +0000368#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000369 /* Verify that no more than one scratch allocation per thread
370 ** is outstanding at one time. (This is only checked in the
371 ** single-threaded case since checking in the multi-threaded case
372 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000373 assert( scratchAllocOut==1 );
374 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000375#endif
drh9ac3fe92008-06-18 18:12:04 +0000376
danielk1977075c23a2008-09-01 18:34:20 +0000377 if( sqlite3GlobalConfig.pScratch==0
378 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000379 || p>=(void*)mem0.aScratchFree ){
drh107b56e2010-03-12 16:32:53 +0000380 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
381 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
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){
drh107b56e2010-03-12 16:32:53 +0000422 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000423 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000424}
drh633e6d52008-07-28 19:34:53 +0000425int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000426 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drhf18a61d2009-07-17 11:44:07 +0000427 if( isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000428 return db->lookaside.sz;
429 }else{
drh107b56e2010-03-12 16:32:53 +0000430 assert( sqlite3MemdebugHasType(p,
431 db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000432 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000433 }
434}
drhfec00ea2008-06-14 16:56:21 +0000435
436/*
437** Free memory previously obtained from sqlite3Malloc().
438*/
439void sqlite3_free(void *p){
440 if( p==0 ) return;
drh107b56e2010-03-12 16:32:53 +0000441 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000442 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000443 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000444 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000445 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000446 sqlite3_mutex_leave(mem0.mutex);
447 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000448 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000449 }
450}
451
452/*
drh633e6d52008-07-28 19:34:53 +0000453** Free memory that might be associated with a particular database
454** connection.
455*/
456void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000457 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh633e6d52008-07-28 19:34:53 +0000458 if( isLookaside(db, p) ){
459 LookasideSlot *pBuf = (LookasideSlot*)p;
460 pBuf->pNext = db->lookaside.pFree;
461 db->lookaside.pFree = pBuf;
462 db->lookaside.nOut--;
463 }else{
drh107b56e2010-03-12 16:32:53 +0000464 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) );
465 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000466 sqlite3_free(p);
467 }
468}
469
470/*
drhfec00ea2008-06-14 16:56:21 +0000471** Change the size of an existing memory allocation
472*/
473void *sqlite3Realloc(void *pOld, int nBytes){
474 int nOld, nNew;
475 void *pNew;
476 if( pOld==0 ){
477 return sqlite3Malloc(nBytes);
478 }
drhb6063cf2009-06-27 00:48:33 +0000479 if( nBytes<=0 ){
drhfec00ea2008-06-14 16:56:21 +0000480 sqlite3_free(pOld);
481 return 0;
482 }
drhb6063cf2009-06-27 00:48:33 +0000483 if( nBytes>=0x7fffff00 ){
484 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
485 return 0;
486 }
drhfec00ea2008-06-14 16:56:21 +0000487 nOld = sqlite3MallocSize(pOld);
drh7c6791c2009-08-18 14:48:53 +0000488 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
489 if( nOld==nNew ){
490 pNew = pOld;
491 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000492 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000493 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh7c6791c2009-08-18 14:48:53 +0000494 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
495 mem0.alarmThreshold ){
496 sqlite3MallocAlarm(nNew-nOld);
497 }
drh107b56e2010-03-12 16:32:53 +0000498 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000499 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
500 if( pNew==0 && mem0.alarmCallback ){
501 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000502 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000503 }
504 if( pNew ){
505 nNew = sqlite3MallocSize(pNew);
506 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000507 }
508 sqlite3_mutex_leave(mem0.mutex);
509 }else{
drh7c6791c2009-08-18 14:48:53 +0000510 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000511 }
512 return pNew;
513}
514
515/*
516** The public interface to sqlite3Realloc. Make sure that the memory
517** subsystem is initialized prior to invoking sqliteRealloc.
518*/
519void *sqlite3_realloc(void *pOld, int n){
520#ifndef SQLITE_OMIT_AUTOINIT
521 if( sqlite3_initialize() ) return 0;
522#endif
523 return sqlite3Realloc(pOld, n);
524}
525
drha3152892007-05-05 11:48:52 +0000526
527/*
drh17435752007-08-16 04:30:38 +0000528** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000529*/
drhfec00ea2008-06-14 16:56:21 +0000530void *sqlite3MallocZero(int n){
531 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000532 if( p ){
533 memset(p, 0, n);
534 }
535 return p;
536}
drh17435752007-08-16 04:30:38 +0000537
538/*
539** Allocate and zero memory. If the allocation fails, make
540** the mallocFailed flag in the connection pointer.
541*/
drhfec00ea2008-06-14 16:56:21 +0000542void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000543 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000544 if( p ){
545 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000546 }
547 return p;
548}
549
550/*
551** Allocate and zero memory. If the allocation fails, make
552** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000553**
554** If db!=0 and db->mallocFailed is true (indicating a prior malloc
555** failure on the same database connection) then always return 0.
556** Hence for a particular database connection, once malloc starts
557** failing, it fails consistently until mallocFailed is reset.
558** This is an important assumption. There are many places in the
559** code that do things like this:
560**
561** int *a = (int*)sqlite3DbMallocRaw(db, 100);
562** int *b = (int*)sqlite3DbMallocRaw(db, 200);
563** if( b ) a[10] = 9;
564**
565** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
566** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000567*/
drhfec00ea2008-06-14 16:56:21 +0000568void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000569 void *p;
drhd9da78a2009-03-24 15:08:09 +0000570 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh4150ebf2008-10-11 15:38:29 +0000571#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000572 if( db ){
573 LookasideSlot *pBuf;
574 if( db->mallocFailed ){
575 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000576 }
drh633e6d52008-07-28 19:34:53 +0000577 if( db->lookaside.bEnabled && n<=db->lookaside.sz
578 && (pBuf = db->lookaside.pFree)!=0 ){
579 db->lookaside.pFree = pBuf->pNext;
580 db->lookaside.nOut++;
581 if( db->lookaside.nOut>db->lookaside.mxOut ){
582 db->lookaside.mxOut = db->lookaside.nOut;
583 }
584 return (void*)pBuf;
585 }
586 }
drhddecae72008-10-11 17:35:16 +0000587#else
588 if( db && db->mallocFailed ){
589 return 0;
590 }
drh4150ebf2008-10-11 15:38:29 +0000591#endif
drh633e6d52008-07-28 19:34:53 +0000592 p = sqlite3Malloc(n);
593 if( !p && db ){
594 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000595 }
drh107b56e2010-03-12 16:32:53 +0000596 sqlite3MemdebugSetType(p,
597 (db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000598 return p;
599}
600
danielk197726783a52007-08-29 14:06:22 +0000601/*
602** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000603** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000604*/
danielk1977a1644fd2007-08-29 12:31:25 +0000605void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
606 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000607 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000608 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000609 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000610 if( p==0 ){
611 return sqlite3DbMallocRaw(db, n);
612 }
613 if( isLookaside(db, p) ){
614 if( n<=db->lookaside.sz ){
615 return p;
616 }
617 pNew = sqlite3DbMallocRaw(db, n);
618 if( pNew ){
619 memcpy(pNew, p, db->lookaside.sz);
620 sqlite3DbFree(db, p);
621 }
622 }else{
drh107b56e2010-03-12 16:32:53 +0000623 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) );
624 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000625 pNew = sqlite3_realloc(p, n);
626 if( !pNew ){
627 db->mallocFailed = 1;
628 }
drh107b56e2010-03-12 16:32:53 +0000629 sqlite3MemdebugSetType(pNew,
630 db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP);
danielk1977a1644fd2007-08-29 12:31:25 +0000631 }
632 }
633 return pNew;
634}
635
drh17435752007-08-16 04:30:38 +0000636/*
637** Attempt to reallocate p. If the reallocation fails, then free p
638** and set the mallocFailed flag in the database connection.
639*/
640void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000641 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000642 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000643 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000644 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000645 }
646 return pNew;
647}
648
drha3152892007-05-05 11:48:52 +0000649/*
650** Make a copy of a string in memory obtained from sqliteMalloc(). These
651** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
652** is because when memory debugging is turned on, these two functions are
653** called via macros that record the current file and line number in the
654** ThreadData structure.
655*/
drh633e6d52008-07-28 19:34:53 +0000656char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000657 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000658 size_t n;
659 if( z==0 ){
660 return 0;
661 }
drhdee0e402009-05-03 20:23:53 +0000662 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000663 assert( (n&0x7fffffff)==n );
664 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000665 if( zNew ){
666 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000667 }
668 return zNew;
669}
670char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000671 char *zNew;
672 if( z==0 ){
673 return 0;
674 }
675 assert( (n&0x7fffffff)==n );
676 zNew = sqlite3DbMallocRaw(db, n+1);
677 if( zNew ){
678 memcpy(zNew, z, n);
679 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000680 }
681 return zNew;
682}
683
drha3152892007-05-05 11:48:52 +0000684/*
drhf089aa42008-07-08 19:34:06 +0000685** Create a string from the zFromat argument and the va_list that follows.
686** Store the string in memory obtained from sqliteMalloc() and make *pz
687** point to that string.
drha3152892007-05-05 11:48:52 +0000688*/
drhf089aa42008-07-08 19:34:06 +0000689void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000690 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000691 char *z;
drha3152892007-05-05 11:48:52 +0000692
drhf089aa42008-07-08 19:34:06 +0000693 va_start(ap, zFormat);
694 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000695 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000696 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000697 *pz = z;
drha3152892007-05-05 11:48:52 +0000698}
699
700
701/*
702** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000703** returning control to the user) that has called sqlite3_malloc or
704** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000705**
706** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000707** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000708** invocation SQLITE_NOMEM is returned instead.
709**
shanebe217792009-03-05 04:20:31 +0000710** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000711** then the connection error-code (the value returned by sqlite3_errcode())
712** is set to SQLITE_NOMEM.
713*/
drha3152892007-05-05 11:48:52 +0000714int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000715 /* If the db handle is not NULL, then we must hold the connection handle
716 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
717 ** is unsafe, as is the call to sqlite3Error().
718 */
719 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000720 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000721 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000722 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000723 rc = SQLITE_NOMEM;
724 }
725 return rc & (db ? db->errMask : 0xff);
726}