blob: a68378a0f8ba07d5845075cccaa74bc3a753b1a8 [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**
drh0a60a382008-07-31 17:16:05 +000015** $Id: malloc.c,v 1.32 2008/07/31 17:16:05 drh 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
danielk1977dfb316d2008-03-26 18:34:43 +000065 int nRet = sqlite3VdbeReleaseMemory(n);
66 nRet += sqlite3PagerReleaseMemory(n-nRet);
67 return nRet;
danielk19771e536952007-08-16 10:09:01 +000068#else
69 return SQLITE_OK;
70#endif
drha3152892007-05-05 11:48:52 +000071}
drha3152892007-05-05 11:48:52 +000072
drhfec00ea2008-06-14 16:56:21 +000073/*
74** State information local to the memory allocation subsystem.
75*/
76static struct {
77 sqlite3_mutex *mutex; /* Mutex to serialize access */
78
79 /*
80 ** The alarm callback and its arguments. The mem0.mutex lock will
81 ** be held while the callback is running. Recursive calls into
82 ** the memory subsystem are allowed, but no new callbacks will be
83 ** issued. The alarmBusy variable is set to prevent recursive
84 ** callbacks.
85 */
86 sqlite3_int64 alarmThreshold;
87 void (*alarmCallback)(void*, sqlite3_int64,int);
88 void *alarmArg;
89 int alarmBusy;
90
91 /*
drh9ac3fe92008-06-18 18:12:04 +000092 ** Pointers to the end of sqlite3Config.pScratch and
93 ** sqlite3Config.pPage to a block of memory that records
94 ** which pages are available.
95 */
96 u32 *aScratchFree;
97 u32 *aPageFree;
98
99 /* Number of free pages for scratch and page-cache memory */
100 u32 nScratchFree;
101 u32 nPageFree;
drhfec00ea2008-06-14 16:56:21 +0000102} mem0;
103
104/*
105** Initialize the memory allocation subsystem.
106*/
107int sqlite3MallocInit(void){
108 if( sqlite3Config.m.xMalloc==0 ){
109 sqlite3MemSetDefault();
110 }
111 memset(&mem0, 0, sizeof(mem0));
drh9ac3fe92008-06-18 18:12:04 +0000112 if( sqlite3Config.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000113 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000114 }
drh9ac3fe92008-06-18 18:12:04 +0000115 if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000
116 && sqlite3Config.nScratch>0 ){
117 int i;
drh0a60a382008-07-31 17:16:05 +0000118 sqlite3Config.szScratch -= 4;
drh9ac3fe92008-06-18 18:12:04 +0000119 mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
120 [sqlite3Config.szScratch*sqlite3Config.nScratch];
121 for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
122 mem0.nScratchFree = sqlite3Config.nScratch;
123 }else{
124 sqlite3Config.pScratch = 0;
drhf7141992008-06-19 00:16:08 +0000125 sqlite3Config.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000126 }
127 if( sqlite3Config.pPage && sqlite3Config.szPage>=512
drh0a60a382008-07-31 17:16:05 +0000128 && sqlite3Config.nPage>1 ){
drh9ac3fe92008-06-18 18:12:04 +0000129 int i;
drh0a60a382008-07-31 17:16:05 +0000130 int overhead;
131 int sz = sqlite3Config.szPage;
132 int n = sqlite3Config.nPage;
133 overhead = (4*n + sz - 1)/sz;
134 sqlite3Config.nPage -= overhead;
drh9ac3fe92008-06-18 18:12:04 +0000135 mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
136 [sqlite3Config.szPage*sqlite3Config.nPage];
137 for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
138 mem0.nPageFree = sqlite3Config.nPage;
139 }else{
140 sqlite3Config.pPage = 0;
drhf7141992008-06-19 00:16:08 +0000141 sqlite3Config.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000142 }
drhfec00ea2008-06-14 16:56:21 +0000143 return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
144}
145
146/*
147** Deinitialize the memory allocation subsystem.
148*/
149void sqlite3MallocEnd(void){
drh9ac3fe92008-06-18 18:12:04 +0000150 sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
151 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000152}
153
154/*
155** Return the amount of memory currently checked out.
156*/
157sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000158 int n, mx;
drhc376a192008-07-14 12:30:54 +0000159 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000160 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000161 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
162 return res;
drhfec00ea2008-06-14 16:56:21 +0000163}
164
165/*
166** Return the maximum amount of memory that has ever been
167** checked out since either the beginning of this process
168** or since the most recent reset.
169*/
170sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000171 int n, mx;
drhc376a192008-07-14 12:30:54 +0000172 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000173 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000174 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000175 return res;
drhfec00ea2008-06-14 16:56:21 +0000176}
177
178/*
179** Change the alarm callback
180*/
181int sqlite3_memory_alarm(
182 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
183 void *pArg,
184 sqlite3_int64 iThreshold
185){
186 sqlite3_mutex_enter(mem0.mutex);
187 mem0.alarmCallback = xCallback;
188 mem0.alarmArg = pArg;
189 mem0.alarmThreshold = iThreshold;
190 sqlite3_mutex_leave(mem0.mutex);
191 return SQLITE_OK;
192}
193
194/*
195** Trigger the alarm
196*/
197static void sqlite3MallocAlarm(int nByte){
198 void (*xCallback)(void*,sqlite3_int64,int);
199 sqlite3_int64 nowUsed;
200 void *pArg;
201 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
202 mem0.alarmBusy = 1;
203 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000204 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000205 pArg = mem0.alarmArg;
206 sqlite3_mutex_leave(mem0.mutex);
207 xCallback(pArg, nowUsed, nByte);
208 sqlite3_mutex_enter(mem0.mutex);
209 mem0.alarmBusy = 0;
210}
211
drhf7141992008-06-19 00:16:08 +0000212/*
213** Do a memory allocation with statistics and alarms. Assume the
214** lock is already held.
215*/
216static int mallocWithAlarm(int n, void **pp){
217 int nFull;
218 void *p;
219 assert( sqlite3_mutex_held(mem0.mutex) );
220 nFull = sqlite3Config.m.xRoundup(n);
221 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
222 if( mem0.alarmCallback!=0 ){
223 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
224 if( nUsed+nFull >= mem0.alarmThreshold ){
225 sqlite3MallocAlarm(nFull);
226 }
227 }
danielk1977d09414c2008-06-19 18:17:49 +0000228 p = sqlite3Config.m.xMalloc(nFull);
229 if( p==0 && mem0.alarmCallback ){
230 sqlite3MallocAlarm(nFull);
drhf7141992008-06-19 00:16:08 +0000231 p = sqlite3Config.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000232 }
drhc702c7c2008-07-18 18:56:16 +0000233 if( p ){
234 nFull = sqlite3MallocSize(p);
235 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
236 }
drhf7141992008-06-19 00:16:08 +0000237 *pp = p;
238 return nFull;
239}
drhfec00ea2008-06-14 16:56:21 +0000240
241/*
242** Allocate memory. This routine is like sqlite3_malloc() except that it
243** assumes the memory subsystem has already been initialized.
244*/
245void *sqlite3Malloc(int n){
246 void *p;
drhfec00ea2008-06-14 16:56:21 +0000247 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000248 p = 0;
drhfec00ea2008-06-14 16:56:21 +0000249 }else if( sqlite3Config.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000250 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000251 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000252 sqlite3_mutex_leave(mem0.mutex);
253 }else{
254 p = sqlite3Config.m.xMalloc(n);
255 }
256 return p;
257}
258
259/*
260** This version of the memory allocation is for use by the application.
261** First make sure the memory subsystem is initialized, then do the
262** allocation.
263*/
264void *sqlite3_malloc(int n){
265#ifndef SQLITE_OMIT_AUTOINIT
266 if( sqlite3_initialize() ) return 0;
267#endif
268 return sqlite3Malloc(n);
269}
270
271/*
drhe5ae5732008-06-15 02:51:47 +0000272** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000273** xScratchMalloc(). We verify this constraint in the single-threaded
274** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000275** is outstanding clearing it when the allocation is freed.
276*/
277#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000278static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000279#endif
280
281
282/*
283** Allocate memory that is to be used and released right away.
284** This routine is similar to alloca() in that it is not intended
285** for situations where the memory might be held long-term. This
286** routine is intended to get memory to old large transient data
287** structures that would not normally fit on the stack of an
288** embedded processor.
289*/
drhfacf0302008-06-17 15:12:00 +0000290void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000291 void *p;
292 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000293
drhe5ae5732008-06-15 02:51:47 +0000294#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000295 /* Verify that no more than one scratch allocation per thread
296 ** is outstanding at one time. (This is only checked in the
297 ** single-threaded case since checking in the multi-threaded case
298 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000299 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000300#endif
drh9ac3fe92008-06-18 18:12:04 +0000301
drhf7141992008-06-19 00:16:08 +0000302 if( sqlite3Config.szScratch<n ){
303 goto scratch_overflow;
304 }else{
305 sqlite3_mutex_enter(mem0.mutex);
306 if( mem0.nScratchFree==0 ){
307 sqlite3_mutex_leave(mem0.mutex);
308 goto scratch_overflow;
309 }else{
310 int i;
311 i = mem0.aScratchFree[--mem0.nScratchFree];
312 sqlite3_mutex_leave(mem0.mutex);
313 i *= sqlite3Config.szScratch;
314 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
315 p = (void*)&((char*)sqlite3Config.pScratch)[i];
316 }
drhe5ae5732008-06-15 02:51:47 +0000317 }
drhf7141992008-06-19 00:16:08 +0000318#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
319 scratchAllocOut = p!=0;
320#endif
321
drhe5ae5732008-06-15 02:51:47 +0000322 return p;
drhf7141992008-06-19 00:16:08 +0000323
324scratch_overflow:
325 if( sqlite3Config.bMemstat ){
326 sqlite3_mutex_enter(mem0.mutex);
327 n = mallocWithAlarm(n, &p);
328 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
329 sqlite3_mutex_leave(mem0.mutex);
330 }else{
331 p = sqlite3Config.m.xMalloc(n);
332 }
333#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
334 scratchAllocOut = p!=0;
335#endif
336 return p;
drhe5ae5732008-06-15 02:51:47 +0000337}
drhfacf0302008-06-17 15:12:00 +0000338void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000339 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000340
drhe5ae5732008-06-15 02:51:47 +0000341#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000342 /* Verify that no more than one scratch allocation per thread
343 ** is outstanding at one time. (This is only checked in the
344 ** single-threaded case since checking in the multi-threaded case
345 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000346 assert( scratchAllocOut==1 );
347 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000348#endif
drh9ac3fe92008-06-18 18:12:04 +0000349
350 if( sqlite3Config.pScratch==0
drhf7141992008-06-19 00:16:08 +0000351 || p<sqlite3Config.pScratch
352 || p>=(void*)mem0.aScratchFree ){
353 if( sqlite3Config.bMemstat ){
354 int iSize = sqlite3MallocSize(p);
355 sqlite3_mutex_enter(mem0.mutex);
356 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
357 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
358 sqlite3Config.m.xFree(p);
359 sqlite3_mutex_leave(mem0.mutex);
360 }else{
361 sqlite3Config.m.xFree(p);
362 }
drh9ac3fe92008-06-18 18:12:04 +0000363 }else{
364 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000365 i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
drh9ac3fe92008-06-18 18:12:04 +0000366 i /= sqlite3Config.szScratch;
367 assert( i>=0 && i<sqlite3Config.nScratch );
drhf7141992008-06-19 00:16:08 +0000368 sqlite3_mutex_enter(mem0.mutex);
369 assert( mem0.nScratchFree<sqlite3Config.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000370 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000371 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000372 sqlite3_mutex_leave(mem0.mutex);
373 }
drhe5ae5732008-06-15 02:51:47 +0000374 }
375}
376
377/*
drhf7141992008-06-19 00:16:08 +0000378** Allocate memory to be used by the page cache. Make use of the
379** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
380** and that memory is of the right size and is not completely
381** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000382*/
drhf7141992008-06-19 00:16:08 +0000383void *sqlite3PageMalloc(int n){
384 void *p;
385 assert( n>0 );
386 assert( (n & (n-1))==0 );
387 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000388
389 if( sqlite3Config.szPage<n ){
390 goto page_overflow;
391 }else{
392 sqlite3_mutex_enter(mem0.mutex);
393 if( mem0.nPageFree==0 ){
394 sqlite3_mutex_leave(mem0.mutex);
395 goto page_overflow;
396 }else{
397 int i;
398 i = mem0.aPageFree[--mem0.nPageFree];
399 sqlite3_mutex_leave(mem0.mutex);
400 i *= sqlite3Config.szPage;
401 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
402 p = (void*)&((char*)sqlite3Config.pPage)[i];
403 }
404 }
405 return p;
406
407page_overflow:
408 if( sqlite3Config.bMemstat ){
409 sqlite3_mutex_enter(mem0.mutex);
410 n = mallocWithAlarm(n, &p);
411 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
412 sqlite3_mutex_leave(mem0.mutex);
413 }else{
414 p = sqlite3Config.m.xMalloc(n);
415 }
416 return p;
drhfacf0302008-06-17 15:12:00 +0000417}
drhf7141992008-06-19 00:16:08 +0000418void sqlite3PageFree(void *p){
419 if( p ){
420 if( sqlite3Config.pPage==0
421 || p<sqlite3Config.pPage
422 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000423 /* In this case, the page allocation was obtained from a regular
424 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
425 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
426 */
drhf7141992008-06-19 00:16:08 +0000427 if( sqlite3Config.bMemstat ){
428 int iSize = sqlite3MallocSize(p);
429 sqlite3_mutex_enter(mem0.mutex);
430 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
431 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
432 sqlite3Config.m.xFree(p);
433 sqlite3_mutex_leave(mem0.mutex);
434 }else{
435 sqlite3Config.m.xFree(p);
436 }
437 }else{
danielk19774b9507a2008-06-21 08:12:15 +0000438 /* The page allocation was allocated from the sqlite3Config.pPage
439 ** buffer. In this case all that is add the index of the page in
440 ** the sqlite3Config.pPage array to the set of free indexes stored
441 ** in the mem0.aPageFree[] array.
442 */
drhf7141992008-06-19 00:16:08 +0000443 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000444 i = (u8 *)p - (u8 *)sqlite3Config.pPage;
drhf7141992008-06-19 00:16:08 +0000445 i /= sqlite3Config.szPage;
446 assert( i>=0 && i<sqlite3Config.nPage );
447 sqlite3_mutex_enter(mem0.mutex);
448 assert( mem0.nPageFree<sqlite3Config.nPage );
449 mem0.aPageFree[mem0.nPageFree++] = i;
450 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
451 sqlite3_mutex_leave(mem0.mutex);
drh5f4bcf12008-07-29 14:29:06 +0000452#if !defined(NDEBUG) && 0
danielk19774b9507a2008-06-21 08:12:15 +0000453 /* Assert that a duplicate was not just inserted into aPageFree[]. */
454 for(i=0; i<mem0.nPageFree-1; i++){
455 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
456 }
457#endif
drhf7141992008-06-19 00:16:08 +0000458 }
459 }
drhfacf0302008-06-17 15:12:00 +0000460}
461
462/*
drh633e6d52008-07-28 19:34:53 +0000463** TRUE if p is a lookaside memory allocation from db
464*/
465static int isLookaside(sqlite3 *db, void *p){
466 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
467}
468
469/*
drhfec00ea2008-06-14 16:56:21 +0000470** Return the size of a memory allocation previously obtained from
471** sqlite3Malloc() or sqlite3_malloc().
472*/
473int sqlite3MallocSize(void *p){
474 return sqlite3Config.m.xSize(p);
475}
drh633e6d52008-07-28 19:34:53 +0000476int sqlite3DbMallocSize(sqlite3 *db, void *p){
477 if( isLookaside(db, p) ){
478 return db->lookaside.sz;
479 }else{
480 return sqlite3Config.m.xSize(p);
481 }
482}
drhfec00ea2008-06-14 16:56:21 +0000483
484/*
485** Free memory previously obtained from sqlite3Malloc().
486*/
487void sqlite3_free(void *p){
488 if( p==0 ) return;
489 if( sqlite3Config.bMemstat ){
490 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000491 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drhfec00ea2008-06-14 16:56:21 +0000492 sqlite3Config.m.xFree(p);
493 sqlite3_mutex_leave(mem0.mutex);
494 }else{
495 sqlite3Config.m.xFree(p);
496 }
497}
498
499/*
drh633e6d52008-07-28 19:34:53 +0000500** Free memory that might be associated with a particular database
501** connection.
502*/
503void sqlite3DbFree(sqlite3 *db, void *p){
504 if( isLookaside(db, p) ){
505 LookasideSlot *pBuf = (LookasideSlot*)p;
506 pBuf->pNext = db->lookaside.pFree;
507 db->lookaside.pFree = pBuf;
508 db->lookaside.nOut--;
509 }else{
510 sqlite3_free(p);
511 }
512}
513
514/*
drhfec00ea2008-06-14 16:56:21 +0000515** Change the size of an existing memory allocation
516*/
517void *sqlite3Realloc(void *pOld, int nBytes){
518 int nOld, nNew;
519 void *pNew;
520 if( pOld==0 ){
521 return sqlite3Malloc(nBytes);
522 }
523 if( nBytes<=0 ){
524 sqlite3_free(pOld);
525 return 0;
526 }
527 nOld = sqlite3MallocSize(pOld);
528 if( sqlite3Config.bMemstat ){
529 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000530 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000531 nNew = sqlite3Config.m.xRoundup(nBytes);
532 if( nOld==nNew ){
533 pNew = pOld;
534 }else{
drhf7141992008-06-19 00:16:08 +0000535 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
536 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000537 sqlite3MallocAlarm(nNew-nOld);
538 }
danielk1977d09414c2008-06-19 18:17:49 +0000539 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
540 if( pNew==0 && mem0.alarmCallback ){
541 sqlite3MallocAlarm(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000542 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000543 }
544 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000545 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000546 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000547 }
548 }
549 sqlite3_mutex_leave(mem0.mutex);
550 }else{
551 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
552 }
553 return pNew;
554}
555
556/*
557** The public interface to sqlite3Realloc. Make sure that the memory
558** subsystem is initialized prior to invoking sqliteRealloc.
559*/
560void *sqlite3_realloc(void *pOld, int n){
561#ifndef SQLITE_OMIT_AUTOINIT
562 if( sqlite3_initialize() ) return 0;
563#endif
564 return sqlite3Realloc(pOld, n);
565}
566
drha3152892007-05-05 11:48:52 +0000567
568/*
drh17435752007-08-16 04:30:38 +0000569** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000570*/
drhfec00ea2008-06-14 16:56:21 +0000571void *sqlite3MallocZero(int n){
572 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000573 if( p ){
574 memset(p, 0, n);
575 }
576 return p;
577}
drh17435752007-08-16 04:30:38 +0000578
579/*
580** Allocate and zero memory. If the allocation fails, make
581** the mallocFailed flag in the connection pointer.
582*/
drhfec00ea2008-06-14 16:56:21 +0000583void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000584 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000585 if( p ){
586 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000587 }
588 return p;
589}
590
591/*
592** Allocate and zero memory. If the allocation fails, make
593** the mallocFailed flag in the connection pointer.
594*/
drhfec00ea2008-06-14 16:56:21 +0000595void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000596 void *p;
597 if( db ){
598 LookasideSlot *pBuf;
599 if( db->mallocFailed ){
600 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000601 }
drh633e6d52008-07-28 19:34:53 +0000602 if( db->lookaside.bEnabled && n<=db->lookaside.sz
603 && (pBuf = db->lookaside.pFree)!=0 ){
604 db->lookaside.pFree = pBuf->pNext;
605 db->lookaside.nOut++;
606 if( db->lookaside.nOut>db->lookaside.mxOut ){
607 db->lookaside.mxOut = db->lookaside.nOut;
608 }
609 return (void*)pBuf;
610 }
611 }
612 p = sqlite3Malloc(n);
613 if( !p && db ){
614 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000615 }
616 return p;
617}
618
danielk197726783a52007-08-29 14:06:22 +0000619/*
620** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000621** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000622*/
danielk1977a1644fd2007-08-29 12:31:25 +0000623void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
624 void *pNew = 0;
625 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000626 if( p==0 ){
627 return sqlite3DbMallocRaw(db, n);
628 }
629 if( isLookaside(db, p) ){
630 if( n<=db->lookaside.sz ){
631 return p;
632 }
633 pNew = sqlite3DbMallocRaw(db, n);
634 if( pNew ){
635 memcpy(pNew, p, db->lookaside.sz);
636 sqlite3DbFree(db, p);
637 }
638 }else{
639 pNew = sqlite3_realloc(p, n);
640 if( !pNew ){
641 db->mallocFailed = 1;
642 }
danielk1977a1644fd2007-08-29 12:31:25 +0000643 }
644 }
645 return pNew;
646}
647
drh17435752007-08-16 04:30:38 +0000648/*
649** Attempt to reallocate p. If the reallocation fails, then free p
650** and set the mallocFailed flag in the database connection.
651*/
652void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000653 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000654 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000655 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000656 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000657 }
658 return pNew;
659}
660
drha3152892007-05-05 11:48:52 +0000661/*
662** Make a copy of a string in memory obtained from sqliteMalloc(). These
663** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
664** is because when memory debugging is turned on, these two functions are
665** called via macros that record the current file and line number in the
666** ThreadData structure.
667*/
drh633e6d52008-07-28 19:34:53 +0000668char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000669 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000670 size_t n;
671 if( z==0 ){
672 return 0;
673 }
drha3152892007-05-05 11:48:52 +0000674 n = strlen(z)+1;
drh633e6d52008-07-28 19:34:53 +0000675 assert( (n&0x7fffffff)==n );
676 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000677 if( zNew ){
678 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000679 }
680 return zNew;
681}
682char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000683 char *zNew;
684 if( z==0 ){
685 return 0;
686 }
687 assert( (n&0x7fffffff)==n );
688 zNew = sqlite3DbMallocRaw(db, n+1);
689 if( zNew ){
690 memcpy(zNew, z, n);
691 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000692 }
693 return zNew;
694}
695
drha3152892007-05-05 11:48:52 +0000696/*
drhf089aa42008-07-08 19:34:06 +0000697** Create a string from the zFromat argument and the va_list that follows.
698** Store the string in memory obtained from sqliteMalloc() and make *pz
699** point to that string.
drha3152892007-05-05 11:48:52 +0000700*/
drhf089aa42008-07-08 19:34:06 +0000701void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000702 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000703 char *z;
drha3152892007-05-05 11:48:52 +0000704
drhf089aa42008-07-08 19:34:06 +0000705 va_start(ap, zFormat);
706 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000707 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000708 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000709 *pz = z;
drha3152892007-05-05 11:48:52 +0000710}
711
712
713/*
714** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000715** returning control to the user) that has called sqlite3_malloc or
716** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000717**
718** The returned value is normally a copy of the second argument to this
719** function. However, if a malloc() failure has occured since the previous
720** invocation SQLITE_NOMEM is returned instead.
721**
722** If the first argument, db, is not NULL and a malloc() error has occured,
723** then the connection error-code (the value returned by sqlite3_errcode())
724** is set to SQLITE_NOMEM.
725*/
drha3152892007-05-05 11:48:52 +0000726int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000727 /* If the db handle is not NULL, then we must hold the connection handle
728 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
729 ** is unsafe, as is the call to sqlite3Error().
730 */
731 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000732 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000733 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000734 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000735 rc = SQLITE_NOMEM;
736 }
737 return rc & (db ? db->errMask : 0xff);
738}