blob: a1727e60327be6b36e9cc8837ea5732abe7623c2 [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**
danielk197767e3da72008-08-21 12:19:44 +000015** $Id: malloc.c,v 1.36 2008/08/21 12:19:44 danielk1977 Exp $
drha3152892007-05-05 11:48:52 +000016*/
17#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000018#include <stdarg.h>
19#include <ctype.h>
20
21/*
drhb21c8cd2007-08-21 19:33:56 +000022** This routine runs when the memory allocator sees that the
23** total memory allocation is about to exceed the soft heap
24** limit.
25*/
26static void softHeapLimitEnforcer(
27 void *NotUsed,
drh153c62c2007-08-24 03:51:33 +000028 sqlite3_int64 inUse,
29 int allocSize
drhb21c8cd2007-08-21 19:33:56 +000030){
31 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 ){
48 sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
49 }else{
50 sqlite3_memory_alarm(0, 0, 0);
51 }
52 overage = sqlite3_memory_used() - n;
53 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
72 return SQLITE_OK;
73#endif
drha3152892007-05-05 11:48:52 +000074}
drha3152892007-05-05 11:48:52 +000075
drhfec00ea2008-06-14 16:56:21 +000076/*
77** State information local to the memory allocation subsystem.
78*/
79static struct {
80 sqlite3_mutex *mutex; /* Mutex to serialize access */
81
82 /*
83 ** The alarm callback and its arguments. The mem0.mutex lock will
84 ** be held while the callback is running. Recursive calls into
85 ** the memory subsystem are allowed, but no new callbacks will be
86 ** issued. The alarmBusy variable is set to prevent recursive
87 ** callbacks.
88 */
89 sqlite3_int64 alarmThreshold;
90 void (*alarmCallback)(void*, sqlite3_int64,int);
91 void *alarmArg;
92 int alarmBusy;
93
94 /*
drh9ac3fe92008-06-18 18:12:04 +000095 ** Pointers to the end of sqlite3Config.pScratch and
96 ** sqlite3Config.pPage to a block of memory that records
97 ** which pages are available.
98 */
99 u32 *aScratchFree;
100 u32 *aPageFree;
101
102 /* Number of free pages for scratch and page-cache memory */
103 u32 nScratchFree;
104 u32 nPageFree;
drhfec00ea2008-06-14 16:56:21 +0000105} mem0;
106
107/*
108** Initialize the memory allocation subsystem.
109*/
110int sqlite3MallocInit(void){
111 if( sqlite3Config.m.xMalloc==0 ){
112 sqlite3MemSetDefault();
113 }
114 memset(&mem0, 0, sizeof(mem0));
drh9ac3fe92008-06-18 18:12:04 +0000115 if( sqlite3Config.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000116 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000117 }
drh6480aad2008-08-01 16:31:14 +0000118 if( sqlite3Config.pScratch && sqlite3Config.szScratch>=100
119 && sqlite3Config.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000120 int i;
drh0a60a382008-07-31 17:16:05 +0000121 sqlite3Config.szScratch -= 4;
drh9ac3fe92008-06-18 18:12:04 +0000122 mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
123 [sqlite3Config.szScratch*sqlite3Config.nScratch];
124 for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
125 mem0.nScratchFree = sqlite3Config.nScratch;
126 }else{
127 sqlite3Config.pScratch = 0;
drhf7141992008-06-19 00:16:08 +0000128 sqlite3Config.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000129 }
130 if( sqlite3Config.pPage && sqlite3Config.szPage>=512
drh6480aad2008-08-01 16:31:14 +0000131 && sqlite3Config.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000132 int i;
drh0a60a382008-07-31 17:16:05 +0000133 int overhead;
134 int sz = sqlite3Config.szPage;
135 int n = sqlite3Config.nPage;
136 overhead = (4*n + sz - 1)/sz;
137 sqlite3Config.nPage -= overhead;
drh9ac3fe92008-06-18 18:12:04 +0000138 mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
139 [sqlite3Config.szPage*sqlite3Config.nPage];
140 for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
141 mem0.nPageFree = sqlite3Config.nPage;
142 }else{
143 sqlite3Config.pPage = 0;
drhf7141992008-06-19 00:16:08 +0000144 sqlite3Config.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000145 }
drhfec00ea2008-06-14 16:56:21 +0000146 return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
147}
148
149/*
150** Deinitialize the memory allocation subsystem.
151*/
152void sqlite3MallocEnd(void){
drh9ac3fe92008-06-18 18:12:04 +0000153 sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
154 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*/
184int sqlite3_memory_alarm(
185 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
197/*
198** Trigger the alarm
199*/
200static void sqlite3MallocAlarm(int nByte){
201 void (*xCallback)(void*,sqlite3_int64,int);
202 sqlite3_int64 nowUsed;
203 void *pArg;
204 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
205 mem0.alarmBusy = 1;
206 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000207 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000208 pArg = mem0.alarmArg;
209 sqlite3_mutex_leave(mem0.mutex);
210 xCallback(pArg, nowUsed, nByte);
211 sqlite3_mutex_enter(mem0.mutex);
212 mem0.alarmBusy = 0;
213}
214
drhf7141992008-06-19 00:16:08 +0000215/*
216** Do a memory allocation with statistics and alarms. Assume the
217** lock is already held.
218*/
219static int mallocWithAlarm(int n, void **pp){
220 int nFull;
221 void *p;
222 assert( sqlite3_mutex_held(mem0.mutex) );
223 nFull = sqlite3Config.m.xRoundup(n);
224 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
225 if( mem0.alarmCallback!=0 ){
226 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
227 if( nUsed+nFull >= mem0.alarmThreshold ){
228 sqlite3MallocAlarm(nFull);
229 }
230 }
danielk1977d09414c2008-06-19 18:17:49 +0000231 p = sqlite3Config.m.xMalloc(nFull);
232 if( p==0 && mem0.alarmCallback ){
233 sqlite3MallocAlarm(nFull);
drhf7141992008-06-19 00:16:08 +0000234 p = sqlite3Config.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000235 }
drhc702c7c2008-07-18 18:56:16 +0000236 if( p ){
237 nFull = sqlite3MallocSize(p);
238 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
239 }
drhf7141992008-06-19 00:16:08 +0000240 *pp = p;
241 return nFull;
242}
drhfec00ea2008-06-14 16:56:21 +0000243
244/*
245** Allocate memory. This routine is like sqlite3_malloc() except that it
246** assumes the memory subsystem has already been initialized.
247*/
248void *sqlite3Malloc(int n){
249 void *p;
drhfec00ea2008-06-14 16:56:21 +0000250 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000251 p = 0;
drhfec00ea2008-06-14 16:56:21 +0000252 }else if( sqlite3Config.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000253 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000254 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000255 sqlite3_mutex_leave(mem0.mutex);
256 }else{
257 p = sqlite3Config.m.xMalloc(n);
258 }
259 return p;
260}
261
262/*
263** This version of the memory allocation is for use by the application.
264** First make sure the memory subsystem is initialized, then do the
265** allocation.
266*/
267void *sqlite3_malloc(int n){
268#ifndef SQLITE_OMIT_AUTOINIT
269 if( sqlite3_initialize() ) return 0;
270#endif
271 return sqlite3Malloc(n);
272}
273
274/*
drhe5ae5732008-06-15 02:51:47 +0000275** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000276** xScratchMalloc(). We verify this constraint in the single-threaded
277** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000278** is outstanding clearing it when the allocation is freed.
279*/
280#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000281static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000282#endif
283
284
285/*
286** Allocate memory that is to be used and released right away.
287** This routine is similar to alloca() in that it is not intended
288** for situations where the memory might be held long-term. This
289** routine is intended to get memory to old large transient data
290** structures that would not normally fit on the stack of an
291** embedded processor.
292*/
drhfacf0302008-06-17 15:12:00 +0000293void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000294 void *p;
295 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000296
drhe5ae5732008-06-15 02:51:47 +0000297#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000298 /* Verify that no more than one scratch allocation per thread
299 ** is outstanding at one time. (This is only checked in the
300 ** single-threaded case since checking in the multi-threaded case
301 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000302 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000303#endif
drh9ac3fe92008-06-18 18:12:04 +0000304
drhf7141992008-06-19 00:16:08 +0000305 if( sqlite3Config.szScratch<n ){
306 goto scratch_overflow;
307 }else{
308 sqlite3_mutex_enter(mem0.mutex);
309 if( mem0.nScratchFree==0 ){
310 sqlite3_mutex_leave(mem0.mutex);
311 goto scratch_overflow;
312 }else{
313 int i;
314 i = mem0.aScratchFree[--mem0.nScratchFree];
315 sqlite3_mutex_leave(mem0.mutex);
316 i *= sqlite3Config.szScratch;
317 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000318 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000319 p = (void*)&((char*)sqlite3Config.pScratch)[i];
320 }
drhe5ae5732008-06-15 02:51:47 +0000321 }
drhf7141992008-06-19 00:16:08 +0000322#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
323 scratchAllocOut = p!=0;
324#endif
325
drhe5ae5732008-06-15 02:51:47 +0000326 return p;
drhf7141992008-06-19 00:16:08 +0000327
328scratch_overflow:
329 if( sqlite3Config.bMemstat ){
330 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000331 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000332 n = mallocWithAlarm(n, &p);
333 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
334 sqlite3_mutex_leave(mem0.mutex);
335 }else{
336 p = sqlite3Config.m.xMalloc(n);
337 }
338#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
339 scratchAllocOut = p!=0;
340#endif
341 return p;
drhe5ae5732008-06-15 02:51:47 +0000342}
drhfacf0302008-06-17 15:12:00 +0000343void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000344 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000345
drhe5ae5732008-06-15 02:51:47 +0000346#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000347 /* Verify that no more than one scratch allocation per thread
348 ** is outstanding at one time. (This is only checked in the
349 ** single-threaded case since checking in the multi-threaded case
350 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000351 assert( scratchAllocOut==1 );
352 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000353#endif
drh9ac3fe92008-06-18 18:12:04 +0000354
355 if( sqlite3Config.pScratch==0
drhf7141992008-06-19 00:16:08 +0000356 || p<sqlite3Config.pScratch
357 || p>=(void*)mem0.aScratchFree ){
358 if( sqlite3Config.bMemstat ){
359 int iSize = sqlite3MallocSize(p);
360 sqlite3_mutex_enter(mem0.mutex);
361 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
362 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
363 sqlite3Config.m.xFree(p);
364 sqlite3_mutex_leave(mem0.mutex);
365 }else{
366 sqlite3Config.m.xFree(p);
367 }
drh9ac3fe92008-06-18 18:12:04 +0000368 }else{
369 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000370 i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
drh9ac3fe92008-06-18 18:12:04 +0000371 i /= sqlite3Config.szScratch;
372 assert( i>=0 && i<sqlite3Config.nScratch );
drhf7141992008-06-19 00:16:08 +0000373 sqlite3_mutex_enter(mem0.mutex);
374 assert( mem0.nScratchFree<sqlite3Config.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000375 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000376 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000377 sqlite3_mutex_leave(mem0.mutex);
378 }
drhe5ae5732008-06-15 02:51:47 +0000379 }
380}
381
382/*
drhf7141992008-06-19 00:16:08 +0000383** Allocate memory to be used by the page cache. Make use of the
384** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
385** and that memory is of the right size and is not completely
386** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000387*/
danielk19778c0a7912008-08-20 14:49:23 +0000388#if 0
drhf7141992008-06-19 00:16:08 +0000389void *sqlite3PageMalloc(int n){
390 void *p;
391 assert( n>0 );
392 assert( (n & (n-1))==0 );
393 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000394
395 if( sqlite3Config.szPage<n ){
396 goto page_overflow;
397 }else{
398 sqlite3_mutex_enter(mem0.mutex);
399 if( mem0.nPageFree==0 ){
400 sqlite3_mutex_leave(mem0.mutex);
401 goto page_overflow;
402 }else{
403 int i;
404 i = mem0.aPageFree[--mem0.nPageFree];
405 sqlite3_mutex_leave(mem0.mutex);
406 i *= sqlite3Config.szPage;
drhe50135e2008-08-05 17:53:22 +0000407 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000408 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
409 p = (void*)&((char*)sqlite3Config.pPage)[i];
410 }
411 }
412 return p;
413
414page_overflow:
415 if( sqlite3Config.bMemstat ){
416 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000417 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000418 n = mallocWithAlarm(n, &p);
419 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
420 sqlite3_mutex_leave(mem0.mutex);
421 }else{
422 p = sqlite3Config.m.xMalloc(n);
423 }
424 return p;
drhfacf0302008-06-17 15:12:00 +0000425}
drhf7141992008-06-19 00:16:08 +0000426void sqlite3PageFree(void *p){
427 if( p ){
428 if( sqlite3Config.pPage==0
429 || p<sqlite3Config.pPage
430 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000431 /* In this case, the page allocation was obtained from a regular
432 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
433 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
434 */
drhf7141992008-06-19 00:16:08 +0000435 if( sqlite3Config.bMemstat ){
436 int iSize = sqlite3MallocSize(p);
437 sqlite3_mutex_enter(mem0.mutex);
438 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
439 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
440 sqlite3Config.m.xFree(p);
441 sqlite3_mutex_leave(mem0.mutex);
442 }else{
443 sqlite3Config.m.xFree(p);
444 }
445 }else{
danielk19774b9507a2008-06-21 08:12:15 +0000446 /* The page allocation was allocated from the sqlite3Config.pPage
447 ** buffer. In this case all that is add the index of the page in
448 ** the sqlite3Config.pPage array to the set of free indexes stored
449 ** in the mem0.aPageFree[] array.
450 */
drhf7141992008-06-19 00:16:08 +0000451 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000452 i = (u8 *)p - (u8 *)sqlite3Config.pPage;
drhf7141992008-06-19 00:16:08 +0000453 i /= sqlite3Config.szPage;
454 assert( i>=0 && i<sqlite3Config.nPage );
455 sqlite3_mutex_enter(mem0.mutex);
456 assert( mem0.nPageFree<sqlite3Config.nPage );
457 mem0.aPageFree[mem0.nPageFree++] = i;
458 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
459 sqlite3_mutex_leave(mem0.mutex);
drh5f4bcf12008-07-29 14:29:06 +0000460#if !defined(NDEBUG) && 0
danielk19774b9507a2008-06-21 08:12:15 +0000461 /* Assert that a duplicate was not just inserted into aPageFree[]. */
462 for(i=0; i<mem0.nPageFree-1; i++){
463 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
464 }
465#endif
drhf7141992008-06-19 00:16:08 +0000466 }
467 }
drhfacf0302008-06-17 15:12:00 +0000468}
danielk19778c0a7912008-08-20 14:49:23 +0000469#endif
drhfacf0302008-06-17 15:12:00 +0000470
471/*
drh633e6d52008-07-28 19:34:53 +0000472** TRUE if p is a lookaside memory allocation from db
473*/
474static int isLookaside(sqlite3 *db, void *p){
475 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
476}
477
478/*
drhfec00ea2008-06-14 16:56:21 +0000479** Return the size of a memory allocation previously obtained from
480** sqlite3Malloc() or sqlite3_malloc().
481*/
482int sqlite3MallocSize(void *p){
483 return sqlite3Config.m.xSize(p);
484}
drh633e6d52008-07-28 19:34:53 +0000485int sqlite3DbMallocSize(sqlite3 *db, void *p){
486 if( isLookaside(db, p) ){
487 return db->lookaside.sz;
488 }else{
489 return sqlite3Config.m.xSize(p);
490 }
491}
drhfec00ea2008-06-14 16:56:21 +0000492
493/*
494** Free memory previously obtained from sqlite3Malloc().
495*/
496void sqlite3_free(void *p){
497 if( p==0 ) return;
498 if( sqlite3Config.bMemstat ){
499 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000500 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drhfec00ea2008-06-14 16:56:21 +0000501 sqlite3Config.m.xFree(p);
502 sqlite3_mutex_leave(mem0.mutex);
503 }else{
504 sqlite3Config.m.xFree(p);
505 }
506}
507
508/*
drh633e6d52008-07-28 19:34:53 +0000509** Free memory that might be associated with a particular database
510** connection.
511*/
512void sqlite3DbFree(sqlite3 *db, void *p){
513 if( isLookaside(db, p) ){
514 LookasideSlot *pBuf = (LookasideSlot*)p;
515 pBuf->pNext = db->lookaside.pFree;
516 db->lookaside.pFree = pBuf;
517 db->lookaside.nOut--;
518 }else{
519 sqlite3_free(p);
520 }
521}
522
523/*
drhfec00ea2008-06-14 16:56:21 +0000524** Change the size of an existing memory allocation
525*/
526void *sqlite3Realloc(void *pOld, int nBytes){
527 int nOld, nNew;
528 void *pNew;
529 if( pOld==0 ){
530 return sqlite3Malloc(nBytes);
531 }
532 if( nBytes<=0 ){
533 sqlite3_free(pOld);
534 return 0;
535 }
536 nOld = sqlite3MallocSize(pOld);
537 if( sqlite3Config.bMemstat ){
538 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000539 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000540 nNew = sqlite3Config.m.xRoundup(nBytes);
541 if( nOld==nNew ){
542 pNew = pOld;
543 }else{
drhf7141992008-06-19 00:16:08 +0000544 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
545 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000546 sqlite3MallocAlarm(nNew-nOld);
547 }
danielk1977d09414c2008-06-19 18:17:49 +0000548 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
549 if( pNew==0 && mem0.alarmCallback ){
550 sqlite3MallocAlarm(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000551 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000552 }
553 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000554 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000555 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000556 }
557 }
558 sqlite3_mutex_leave(mem0.mutex);
559 }else{
560 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
561 }
562 return pNew;
563}
564
565/*
566** The public interface to sqlite3Realloc. Make sure that the memory
567** subsystem is initialized prior to invoking sqliteRealloc.
568*/
569void *sqlite3_realloc(void *pOld, int n){
570#ifndef SQLITE_OMIT_AUTOINIT
571 if( sqlite3_initialize() ) return 0;
572#endif
573 return sqlite3Realloc(pOld, n);
574}
575
drha3152892007-05-05 11:48:52 +0000576
577/*
drh17435752007-08-16 04:30:38 +0000578** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000579*/
drhfec00ea2008-06-14 16:56:21 +0000580void *sqlite3MallocZero(int n){
581 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000582 if( p ){
583 memset(p, 0, n);
584 }
585 return p;
586}
drh17435752007-08-16 04:30:38 +0000587
588/*
589** Allocate and zero memory. If the allocation fails, make
590** the mallocFailed flag in the connection pointer.
591*/
drhfec00ea2008-06-14 16:56:21 +0000592void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000593 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000594 if( p ){
595 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000596 }
597 return p;
598}
599
600/*
601** Allocate and zero memory. If the allocation fails, make
602** the mallocFailed flag in the connection pointer.
603*/
drhfec00ea2008-06-14 16:56:21 +0000604void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000605 void *p;
606 if( db ){
607 LookasideSlot *pBuf;
608 if( db->mallocFailed ){
609 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000610 }
drh633e6d52008-07-28 19:34:53 +0000611 if( db->lookaside.bEnabled && n<=db->lookaside.sz
612 && (pBuf = db->lookaside.pFree)!=0 ){
613 db->lookaside.pFree = pBuf->pNext;
614 db->lookaside.nOut++;
615 if( db->lookaside.nOut>db->lookaside.mxOut ){
616 db->lookaside.mxOut = db->lookaside.nOut;
617 }
618 return (void*)pBuf;
619 }
620 }
621 p = sqlite3Malloc(n);
622 if( !p && db ){
623 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000624 }
625 return p;
626}
627
danielk197726783a52007-08-29 14:06:22 +0000628/*
629** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000630** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000631*/
danielk1977a1644fd2007-08-29 12:31:25 +0000632void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
633 void *pNew = 0;
634 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000635 if( p==0 ){
636 return sqlite3DbMallocRaw(db, n);
637 }
638 if( isLookaside(db, p) ){
639 if( n<=db->lookaside.sz ){
640 return p;
641 }
642 pNew = sqlite3DbMallocRaw(db, n);
643 if( pNew ){
644 memcpy(pNew, p, db->lookaside.sz);
645 sqlite3DbFree(db, p);
646 }
647 }else{
648 pNew = sqlite3_realloc(p, n);
649 if( !pNew ){
650 db->mallocFailed = 1;
651 }
danielk1977a1644fd2007-08-29 12:31:25 +0000652 }
653 }
654 return pNew;
655}
656
drh17435752007-08-16 04:30:38 +0000657/*
658** Attempt to reallocate p. If the reallocation fails, then free p
659** and set the mallocFailed flag in the database connection.
660*/
661void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000662 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000663 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000664 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000665 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000666 }
667 return pNew;
668}
669
drha3152892007-05-05 11:48:52 +0000670/*
671** Make a copy of a string in memory obtained from sqliteMalloc(). These
672** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
673** is because when memory debugging is turned on, these two functions are
674** called via macros that record the current file and line number in the
675** ThreadData structure.
676*/
drh633e6d52008-07-28 19:34:53 +0000677char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000678 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000679 size_t n;
680 if( z==0 ){
681 return 0;
682 }
drha3152892007-05-05 11:48:52 +0000683 n = strlen(z)+1;
drh633e6d52008-07-28 19:34:53 +0000684 assert( (n&0x7fffffff)==n );
685 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000686 if( zNew ){
687 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000688 }
689 return zNew;
690}
691char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000692 char *zNew;
693 if( z==0 ){
694 return 0;
695 }
696 assert( (n&0x7fffffff)==n );
697 zNew = sqlite3DbMallocRaw(db, n+1);
698 if( zNew ){
699 memcpy(zNew, z, n);
700 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000701 }
702 return zNew;
703}
704
drha3152892007-05-05 11:48:52 +0000705/*
drhf089aa42008-07-08 19:34:06 +0000706** Create a string from the zFromat argument and the va_list that follows.
707** Store the string in memory obtained from sqliteMalloc() and make *pz
708** point to that string.
drha3152892007-05-05 11:48:52 +0000709*/
drhf089aa42008-07-08 19:34:06 +0000710void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000711 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000712 char *z;
drha3152892007-05-05 11:48:52 +0000713
drhf089aa42008-07-08 19:34:06 +0000714 va_start(ap, zFormat);
715 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000716 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000717 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000718 *pz = z;
drha3152892007-05-05 11:48:52 +0000719}
720
721
722/*
723** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000724** returning control to the user) that has called sqlite3_malloc or
725** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000726**
727** The returned value is normally a copy of the second argument to this
728** function. However, if a malloc() failure has occured since the previous
729** invocation SQLITE_NOMEM is returned instead.
730**
731** If the first argument, db, is not NULL and a malloc() error has occured,
732** then the connection error-code (the value returned by sqlite3_errcode())
733** is set to SQLITE_NOMEM.
734*/
drha3152892007-05-05 11:48:52 +0000735int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000736 /* If the db handle is not NULL, then we must hold the connection handle
737 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
738 ** is unsafe, as is the call to sqlite3Error().
739 */
740 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000741 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000742 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000743 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000744 rc = SQLITE_NOMEM;
745 }
746 return rc & (db ? db->errMask : 0xff);
747}