blob: ed2bbb3836b66ba5a3d9ebe75d7d76cb009c73d8 [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**
drhc376a192008-07-14 12:30:54 +000015** $Id: malloc.c,v 1.27 2008/07/14 12:30:54 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;
118 mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
119 [sqlite3Config.szScratch*sqlite3Config.nScratch];
120 for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
121 mem0.nScratchFree = sqlite3Config.nScratch;
122 }else{
123 sqlite3Config.pScratch = 0;
drhf7141992008-06-19 00:16:08 +0000124 sqlite3Config.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000125 }
126 if( sqlite3Config.pPage && sqlite3Config.szPage>=512
127 && sqlite3Config.nPage>0 ){
128 int i;
129 mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
130 [sqlite3Config.szPage*sqlite3Config.nPage];
131 for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
132 mem0.nPageFree = sqlite3Config.nPage;
133 }else{
134 sqlite3Config.pPage = 0;
drhf7141992008-06-19 00:16:08 +0000135 sqlite3Config.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000136 }
drhfec00ea2008-06-14 16:56:21 +0000137 return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
138}
139
140/*
141** Deinitialize the memory allocation subsystem.
142*/
143void sqlite3MallocEnd(void){
drh9ac3fe92008-06-18 18:12:04 +0000144 sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
145 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000146}
147
148/*
149** Return the amount of memory currently checked out.
150*/
151sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000152 int n, mx;
drhc376a192008-07-14 12:30:54 +0000153 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000154 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000155 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
156 return res;
drhfec00ea2008-06-14 16:56:21 +0000157}
158
159/*
160** Return the maximum amount of memory that has ever been
161** checked out since either the beginning of this process
162** or since the most recent reset.
163*/
164sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000165 int n, mx;
drhc376a192008-07-14 12:30:54 +0000166 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000167 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drhc376a192008-07-14 12:30:54 +0000168 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
169 return res;
drhfec00ea2008-06-14 16:56:21 +0000170}
171
172/*
173** Change the alarm callback
174*/
175int sqlite3_memory_alarm(
176 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
177 void *pArg,
178 sqlite3_int64 iThreshold
179){
180 sqlite3_mutex_enter(mem0.mutex);
181 mem0.alarmCallback = xCallback;
182 mem0.alarmArg = pArg;
183 mem0.alarmThreshold = iThreshold;
184 sqlite3_mutex_leave(mem0.mutex);
185 return SQLITE_OK;
186}
187
188/*
189** Trigger the alarm
190*/
191static void sqlite3MallocAlarm(int nByte){
192 void (*xCallback)(void*,sqlite3_int64,int);
193 sqlite3_int64 nowUsed;
194 void *pArg;
195 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
196 mem0.alarmBusy = 1;
197 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000198 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000199 pArg = mem0.alarmArg;
200 sqlite3_mutex_leave(mem0.mutex);
201 xCallback(pArg, nowUsed, nByte);
202 sqlite3_mutex_enter(mem0.mutex);
203 mem0.alarmBusy = 0;
204}
205
drhf7141992008-06-19 00:16:08 +0000206/*
207** Do a memory allocation with statistics and alarms. Assume the
208** lock is already held.
209*/
210static int mallocWithAlarm(int n, void **pp){
211 int nFull;
212 void *p;
213 assert( sqlite3_mutex_held(mem0.mutex) );
214 nFull = sqlite3Config.m.xRoundup(n);
215 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
216 if( mem0.alarmCallback!=0 ){
217 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
218 if( nUsed+nFull >= mem0.alarmThreshold ){
219 sqlite3MallocAlarm(nFull);
220 }
221 }
danielk1977d09414c2008-06-19 18:17:49 +0000222 p = sqlite3Config.m.xMalloc(nFull);
223 if( p==0 && mem0.alarmCallback ){
224 sqlite3MallocAlarm(nFull);
drhf7141992008-06-19 00:16:08 +0000225 p = sqlite3Config.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000226 }
227 if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
228 *pp = p;
229 return nFull;
230}
drhfec00ea2008-06-14 16:56:21 +0000231
232/*
233** Allocate memory. This routine is like sqlite3_malloc() except that it
234** assumes the memory subsystem has already been initialized.
235*/
236void *sqlite3Malloc(int n){
237 void *p;
drhfec00ea2008-06-14 16:56:21 +0000238 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000239 p = 0;
drhfec00ea2008-06-14 16:56:21 +0000240 }else if( sqlite3Config.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000241 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000242 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000243 sqlite3_mutex_leave(mem0.mutex);
244 }else{
245 p = sqlite3Config.m.xMalloc(n);
246 }
247 return p;
248}
249
250/*
251** This version of the memory allocation is for use by the application.
252** First make sure the memory subsystem is initialized, then do the
253** allocation.
254*/
255void *sqlite3_malloc(int n){
256#ifndef SQLITE_OMIT_AUTOINIT
257 if( sqlite3_initialize() ) return 0;
258#endif
259 return sqlite3Malloc(n);
260}
261
262/*
drhe5ae5732008-06-15 02:51:47 +0000263** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000264** xScratchMalloc(). We verify this constraint in the single-threaded
265** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000266** is outstanding clearing it when the allocation is freed.
267*/
268#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000269static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000270#endif
271
272
273/*
274** Allocate memory that is to be used and released right away.
275** This routine is similar to alloca() in that it is not intended
276** for situations where the memory might be held long-term. This
277** routine is intended to get memory to old large transient data
278** structures that would not normally fit on the stack of an
279** embedded processor.
280*/
drhfacf0302008-06-17 15:12:00 +0000281void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000282 void *p;
283 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000284
drhe5ae5732008-06-15 02:51:47 +0000285#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000286 /* Verify that no more than one scratch allocation per thread
287 ** is outstanding at one time. (This is only checked in the
288 ** single-threaded case since checking in the multi-threaded case
289 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000290 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000291#endif
drh9ac3fe92008-06-18 18:12:04 +0000292
drhf7141992008-06-19 00:16:08 +0000293 if( sqlite3Config.szScratch<n ){
294 goto scratch_overflow;
295 }else{
296 sqlite3_mutex_enter(mem0.mutex);
297 if( mem0.nScratchFree==0 ){
298 sqlite3_mutex_leave(mem0.mutex);
299 goto scratch_overflow;
300 }else{
301 int i;
302 i = mem0.aScratchFree[--mem0.nScratchFree];
303 sqlite3_mutex_leave(mem0.mutex);
304 i *= sqlite3Config.szScratch;
305 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
306 p = (void*)&((char*)sqlite3Config.pScratch)[i];
307 }
drhe5ae5732008-06-15 02:51:47 +0000308 }
drhf7141992008-06-19 00:16:08 +0000309#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
310 scratchAllocOut = p!=0;
311#endif
312
drhe5ae5732008-06-15 02:51:47 +0000313 return p;
drhf7141992008-06-19 00:16:08 +0000314
315scratch_overflow:
316 if( sqlite3Config.bMemstat ){
317 sqlite3_mutex_enter(mem0.mutex);
318 n = mallocWithAlarm(n, &p);
319 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
320 sqlite3_mutex_leave(mem0.mutex);
321 }else{
322 p = sqlite3Config.m.xMalloc(n);
323 }
324#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
325 scratchAllocOut = p!=0;
326#endif
327 return p;
drhe5ae5732008-06-15 02:51:47 +0000328}
drhfacf0302008-06-17 15:12:00 +0000329void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000330 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000331
drhe5ae5732008-06-15 02:51:47 +0000332#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000333 /* Verify that no more than one scratch allocation per thread
334 ** is outstanding at one time. (This is only checked in the
335 ** single-threaded case since checking in the multi-threaded case
336 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000337 assert( scratchAllocOut==1 );
338 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000339#endif
drh9ac3fe92008-06-18 18:12:04 +0000340
341 if( sqlite3Config.pScratch==0
drhf7141992008-06-19 00:16:08 +0000342 || p<sqlite3Config.pScratch
343 || p>=(void*)mem0.aScratchFree ){
344 if( sqlite3Config.bMemstat ){
345 int iSize = sqlite3MallocSize(p);
346 sqlite3_mutex_enter(mem0.mutex);
347 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
348 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
349 sqlite3Config.m.xFree(p);
350 sqlite3_mutex_leave(mem0.mutex);
351 }else{
352 sqlite3Config.m.xFree(p);
353 }
drh9ac3fe92008-06-18 18:12:04 +0000354 }else{
355 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000356 i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
drh9ac3fe92008-06-18 18:12:04 +0000357 i /= sqlite3Config.szScratch;
358 assert( i>=0 && i<sqlite3Config.nScratch );
drhf7141992008-06-19 00:16:08 +0000359 sqlite3_mutex_enter(mem0.mutex);
360 assert( mem0.nScratchFree<sqlite3Config.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000361 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000362 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000363 sqlite3_mutex_leave(mem0.mutex);
364 }
drhe5ae5732008-06-15 02:51:47 +0000365 }
366}
367
368/*
drhf7141992008-06-19 00:16:08 +0000369** Allocate memory to be used by the page cache. Make use of the
370** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
371** and that memory is of the right size and is not completely
372** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000373*/
drhf7141992008-06-19 00:16:08 +0000374void *sqlite3PageMalloc(int n){
375 void *p;
376 assert( n>0 );
377 assert( (n & (n-1))==0 );
378 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000379
380 if( sqlite3Config.szPage<n ){
381 goto page_overflow;
382 }else{
383 sqlite3_mutex_enter(mem0.mutex);
384 if( mem0.nPageFree==0 ){
385 sqlite3_mutex_leave(mem0.mutex);
386 goto page_overflow;
387 }else{
388 int i;
389 i = mem0.aPageFree[--mem0.nPageFree];
390 sqlite3_mutex_leave(mem0.mutex);
391 i *= sqlite3Config.szPage;
392 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
393 p = (void*)&((char*)sqlite3Config.pPage)[i];
394 }
395 }
396 return p;
397
398page_overflow:
399 if( sqlite3Config.bMemstat ){
400 sqlite3_mutex_enter(mem0.mutex);
401 n = mallocWithAlarm(n, &p);
402 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
403 sqlite3_mutex_leave(mem0.mutex);
404 }else{
405 p = sqlite3Config.m.xMalloc(n);
406 }
407 return p;
drhfacf0302008-06-17 15:12:00 +0000408}
drhf7141992008-06-19 00:16:08 +0000409void sqlite3PageFree(void *p){
410 if( p ){
411 if( sqlite3Config.pPage==0
412 || p<sqlite3Config.pPage
413 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000414 /* In this case, the page allocation was obtained from a regular
415 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
416 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
417 */
drhf7141992008-06-19 00:16:08 +0000418 if( sqlite3Config.bMemstat ){
419 int iSize = sqlite3MallocSize(p);
420 sqlite3_mutex_enter(mem0.mutex);
421 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
422 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
423 sqlite3Config.m.xFree(p);
424 sqlite3_mutex_leave(mem0.mutex);
425 }else{
426 sqlite3Config.m.xFree(p);
427 }
428 }else{
danielk19774b9507a2008-06-21 08:12:15 +0000429 /* The page allocation was allocated from the sqlite3Config.pPage
430 ** buffer. In this case all that is add the index of the page in
431 ** the sqlite3Config.pPage array to the set of free indexes stored
432 ** in the mem0.aPageFree[] array.
433 */
drhf7141992008-06-19 00:16:08 +0000434 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000435 i = (u8 *)p - (u8 *)sqlite3Config.pPage;
drhf7141992008-06-19 00:16:08 +0000436 i /= sqlite3Config.szPage;
437 assert( i>=0 && i<sqlite3Config.nPage );
438 sqlite3_mutex_enter(mem0.mutex);
439 assert( mem0.nPageFree<sqlite3Config.nPage );
440 mem0.aPageFree[mem0.nPageFree++] = i;
441 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
442 sqlite3_mutex_leave(mem0.mutex);
danielk19774b9507a2008-06-21 08:12:15 +0000443#ifndef NDEBUG
444 /* Assert that a duplicate was not just inserted into aPageFree[]. */
445 for(i=0; i<mem0.nPageFree-1; i++){
446 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
447 }
448#endif
drhf7141992008-06-19 00:16:08 +0000449 }
450 }
drhfacf0302008-06-17 15:12:00 +0000451}
452
453/*
drhfec00ea2008-06-14 16:56:21 +0000454** Return the size of a memory allocation previously obtained from
455** sqlite3Malloc() or sqlite3_malloc().
456*/
457int sqlite3MallocSize(void *p){
458 return sqlite3Config.m.xSize(p);
459}
460
461/*
462** Free memory previously obtained from sqlite3Malloc().
463*/
464void sqlite3_free(void *p){
465 if( p==0 ) return;
466 if( sqlite3Config.bMemstat ){
467 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000468 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drhfec00ea2008-06-14 16:56:21 +0000469 sqlite3Config.m.xFree(p);
470 sqlite3_mutex_leave(mem0.mutex);
471 }else{
472 sqlite3Config.m.xFree(p);
473 }
474}
475
476/*
477** Change the size of an existing memory allocation
478*/
479void *sqlite3Realloc(void *pOld, int nBytes){
480 int nOld, nNew;
481 void *pNew;
482 if( pOld==0 ){
483 return sqlite3Malloc(nBytes);
484 }
485 if( nBytes<=0 ){
486 sqlite3_free(pOld);
487 return 0;
488 }
489 nOld = sqlite3MallocSize(pOld);
490 if( sqlite3Config.bMemstat ){
491 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000492 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000493 nNew = sqlite3Config.m.xRoundup(nBytes);
494 if( nOld==nNew ){
495 pNew = pOld;
496 }else{
drhf7141992008-06-19 00:16:08 +0000497 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
498 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000499 sqlite3MallocAlarm(nNew-nOld);
500 }
danielk1977d09414c2008-06-19 18:17:49 +0000501 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
502 if( pNew==0 && mem0.alarmCallback ){
503 sqlite3MallocAlarm(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000504 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000505 }
506 if( pNew ){
drhf7141992008-06-19 00:16:08 +0000507 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000508 }
509 }
510 sqlite3_mutex_leave(mem0.mutex);
511 }else{
512 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
513 }
514 return pNew;
515}
516
517/*
518** The public interface to sqlite3Realloc. Make sure that the memory
519** subsystem is initialized prior to invoking sqliteRealloc.
520*/
521void *sqlite3_realloc(void *pOld, int n){
522#ifndef SQLITE_OMIT_AUTOINIT
523 if( sqlite3_initialize() ) return 0;
524#endif
525 return sqlite3Realloc(pOld, n);
526}
527
drha3152892007-05-05 11:48:52 +0000528
529/*
drh17435752007-08-16 04:30:38 +0000530** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000531*/
drhfec00ea2008-06-14 16:56:21 +0000532void *sqlite3MallocZero(int n){
533 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000534 if( p ){
535 memset(p, 0, n);
536 }
537 return p;
538}
drh17435752007-08-16 04:30:38 +0000539
540/*
541** Allocate and zero memory. If the allocation fails, make
542** the mallocFailed flag in the connection pointer.
543*/
drhfec00ea2008-06-14 16:56:21 +0000544void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000545 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000546 if( p ){
547 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000548 }
549 return p;
550}
551
552/*
553** Allocate and zero memory. If the allocation fails, make
554** the mallocFailed flag in the connection pointer.
555*/
drhfec00ea2008-06-14 16:56:21 +0000556void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000557 void *p = 0;
558 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000559 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000560 if( !p && db ){
561 db->mallocFailed = 1;
562 }
drh17435752007-08-16 04:30:38 +0000563 }
564 return p;
565}
566
danielk197726783a52007-08-29 14:06:22 +0000567/*
568** Resize the block of memory pointed to by p to n bytes. If the
569** resize fails, set the mallocFailed flag inthe connection object.
570*/
danielk1977a1644fd2007-08-29 12:31:25 +0000571void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
572 void *pNew = 0;
573 if( db->mallocFailed==0 ){
574 pNew = sqlite3_realloc(p, n);
575 if( !pNew ){
576 db->mallocFailed = 1;
577 }
578 }
579 return pNew;
580}
581
drh17435752007-08-16 04:30:38 +0000582/*
583** Attempt to reallocate p. If the reallocation fails, then free p
584** and set the mallocFailed flag in the database connection.
585*/
586void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000587 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000588 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000589 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000590 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000591 }
592 return pNew;
593}
594
drha3152892007-05-05 11:48:52 +0000595/*
596** Make a copy of a string in memory obtained from sqliteMalloc(). These
597** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
598** is because when memory debugging is turned on, these two functions are
599** called via macros that record the current file and line number in the
600** ThreadData structure.
601*/
602char *sqlite3StrDup(const char *z){
603 char *zNew;
604 int n;
605 if( z==0 ) return 0;
606 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000607 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000608 if( zNew ) memcpy(zNew, z, n);
609 return zNew;
610}
611char *sqlite3StrNDup(const char *z, int n){
612 char *zNew;
613 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000614 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000615 if( zNew ){
616 memcpy(zNew, z, n);
617 zNew[n] = 0;
618 }
619 return zNew;
620}
621
danielk19771e536952007-08-16 10:09:01 +0000622char *sqlite3DbStrDup(sqlite3 *db, const char *z){
623 char *zNew = sqlite3StrDup(z);
624 if( z && !zNew ){
625 db->mallocFailed = 1;
626 }
627 return zNew;
628}
629char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
630 char *zNew = sqlite3StrNDup(z, n);
631 if( z && !zNew ){
632 db->mallocFailed = 1;
633 }
634 return zNew;
635}
636
drha3152892007-05-05 11:48:52 +0000637/*
drhf089aa42008-07-08 19:34:06 +0000638** Create a string from the zFromat argument and the va_list that follows.
639** Store the string in memory obtained from sqliteMalloc() and make *pz
640** point to that string.
drha3152892007-05-05 11:48:52 +0000641*/
drhf089aa42008-07-08 19:34:06 +0000642void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000643 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000644 char *z;
drha3152892007-05-05 11:48:52 +0000645
drhf089aa42008-07-08 19:34:06 +0000646 va_start(ap, zFormat);
647 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000648 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000649 sqlite3_free(*pz);
drhf089aa42008-07-08 19:34:06 +0000650 *pz = z;
drha3152892007-05-05 11:48:52 +0000651}
652
653
654/*
655** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000656** returning control to the user) that has called sqlite3_malloc or
657** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000658**
659** The returned value is normally a copy of the second argument to this
660** function. However, if a malloc() failure has occured since the previous
661** invocation SQLITE_NOMEM is returned instead.
662**
663** If the first argument, db, is not NULL and a malloc() error has occured,
664** then the connection error-code (the value returned by sqlite3_errcode())
665** is set to SQLITE_NOMEM.
666*/
drha3152892007-05-05 11:48:52 +0000667int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000668 /* If the db handle is not NULL, then we must hold the connection handle
669 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
670 ** is unsafe, as is the call to sqlite3Error().
671 */
672 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000673 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000674 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000675 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000676 rc = SQLITE_NOMEM;
677 }
678 return rc & (db ? db->errMask : 0xff);
679}