blob: 351a38ce3792dd11989e020326e76fa21533f6ef [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**
drhf089aa42008-07-08 19:34:06 +000015** $Id: malloc.c,v 1.26 2008/07/08 19:34:07 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;
153 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
154 return (sqlite3_int64)n;
drhfec00ea2008-06-14 16:56:21 +0000155}
156
157/*
158** Return the maximum amount of memory that has ever been
159** checked out since either the beginning of this process
160** or since the most recent reset.
161*/
162sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000163 int n, mx;
164 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
165 return (sqlite3_int64)mx;
drhfec00ea2008-06-14 16:56:21 +0000166}
167
168/*
169** Change the alarm callback
170*/
171int sqlite3_memory_alarm(
172 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
173 void *pArg,
174 sqlite3_int64 iThreshold
175){
176 sqlite3_mutex_enter(mem0.mutex);
177 mem0.alarmCallback = xCallback;
178 mem0.alarmArg = pArg;
179 mem0.alarmThreshold = iThreshold;
180 sqlite3_mutex_leave(mem0.mutex);
181 return SQLITE_OK;
182}
183
184/*
185** Trigger the alarm
186*/
187static void sqlite3MallocAlarm(int nByte){
188 void (*xCallback)(void*,sqlite3_int64,int);
189 sqlite3_int64 nowUsed;
190 void *pArg;
191 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
192 mem0.alarmBusy = 1;
193 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000194 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000195 pArg = mem0.alarmArg;
196 sqlite3_mutex_leave(mem0.mutex);
197 xCallback(pArg, nowUsed, nByte);
198 sqlite3_mutex_enter(mem0.mutex);
199 mem0.alarmBusy = 0;
200}
201
drhf7141992008-06-19 00:16:08 +0000202/*
203** Do a memory allocation with statistics and alarms. Assume the
204** lock is already held.
205*/
206static int mallocWithAlarm(int n, void **pp){
207 int nFull;
208 void *p;
209 assert( sqlite3_mutex_held(mem0.mutex) );
210 nFull = sqlite3Config.m.xRoundup(n);
211 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
212 if( mem0.alarmCallback!=0 ){
213 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
214 if( nUsed+nFull >= mem0.alarmThreshold ){
215 sqlite3MallocAlarm(nFull);
216 }
217 }
danielk1977d09414c2008-06-19 18:17:49 +0000218 p = sqlite3Config.m.xMalloc(nFull);
219 if( p==0 && mem0.alarmCallback ){
220 sqlite3MallocAlarm(nFull);
drhf7141992008-06-19 00:16:08 +0000221 p = sqlite3Config.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000222 }
223 if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
224 *pp = p;
225 return nFull;
226}
drhfec00ea2008-06-14 16:56:21 +0000227
228/*
229** Allocate memory. This routine is like sqlite3_malloc() except that it
230** assumes the memory subsystem has already been initialized.
231*/
232void *sqlite3Malloc(int n){
233 void *p;
drhfec00ea2008-06-14 16:56:21 +0000234 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000235 p = 0;
drhfec00ea2008-06-14 16:56:21 +0000236 }else if( sqlite3Config.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000237 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000238 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000239 sqlite3_mutex_leave(mem0.mutex);
240 }else{
241 p = sqlite3Config.m.xMalloc(n);
242 }
243 return p;
244}
245
246/*
247** This version of the memory allocation is for use by the application.
248** First make sure the memory subsystem is initialized, then do the
249** allocation.
250*/
251void *sqlite3_malloc(int n){
252#ifndef SQLITE_OMIT_AUTOINIT
253 if( sqlite3_initialize() ) return 0;
254#endif
255 return sqlite3Malloc(n);
256}
257
258/*
drhe5ae5732008-06-15 02:51:47 +0000259** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000260** xScratchMalloc(). We verify this constraint in the single-threaded
261** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000262** is outstanding clearing it when the allocation is freed.
263*/
264#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000265static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000266#endif
267
268
269/*
270** Allocate memory that is to be used and released right away.
271** This routine is similar to alloca() in that it is not intended
272** for situations where the memory might be held long-term. This
273** routine is intended to get memory to old large transient data
274** structures that would not normally fit on the stack of an
275** embedded processor.
276*/
drhfacf0302008-06-17 15:12:00 +0000277void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000278 void *p;
279 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000280
drhe5ae5732008-06-15 02:51:47 +0000281#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000282 /* Verify that no more than one scratch allocation per thread
283 ** is outstanding at one time. (This is only checked in the
284 ** single-threaded case since checking in the multi-threaded case
285 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000286 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000287#endif
drh9ac3fe92008-06-18 18:12:04 +0000288
drhf7141992008-06-19 00:16:08 +0000289 if( sqlite3Config.szScratch<n ){
290 goto scratch_overflow;
291 }else{
292 sqlite3_mutex_enter(mem0.mutex);
293 if( mem0.nScratchFree==0 ){
294 sqlite3_mutex_leave(mem0.mutex);
295 goto scratch_overflow;
296 }else{
297 int i;
298 i = mem0.aScratchFree[--mem0.nScratchFree];
299 sqlite3_mutex_leave(mem0.mutex);
300 i *= sqlite3Config.szScratch;
301 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
302 p = (void*)&((char*)sqlite3Config.pScratch)[i];
303 }
drhe5ae5732008-06-15 02:51:47 +0000304 }
drhf7141992008-06-19 00:16:08 +0000305#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
306 scratchAllocOut = p!=0;
307#endif
308
drhe5ae5732008-06-15 02:51:47 +0000309 return p;
drhf7141992008-06-19 00:16:08 +0000310
311scratch_overflow:
312 if( sqlite3Config.bMemstat ){
313 sqlite3_mutex_enter(mem0.mutex);
314 n = mallocWithAlarm(n, &p);
315 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
316 sqlite3_mutex_leave(mem0.mutex);
317 }else{
318 p = sqlite3Config.m.xMalloc(n);
319 }
320#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
321 scratchAllocOut = p!=0;
322#endif
323 return p;
drhe5ae5732008-06-15 02:51:47 +0000324}
drhfacf0302008-06-17 15:12:00 +0000325void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000326 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000327
drhe5ae5732008-06-15 02:51:47 +0000328#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000329 /* Verify that no more than one scratch allocation per thread
330 ** is outstanding at one time. (This is only checked in the
331 ** single-threaded case since checking in the multi-threaded case
332 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000333 assert( scratchAllocOut==1 );
334 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000335#endif
drh9ac3fe92008-06-18 18:12:04 +0000336
337 if( sqlite3Config.pScratch==0
drhf7141992008-06-19 00:16:08 +0000338 || p<sqlite3Config.pScratch
339 || p>=(void*)mem0.aScratchFree ){
340 if( sqlite3Config.bMemstat ){
341 int iSize = sqlite3MallocSize(p);
342 sqlite3_mutex_enter(mem0.mutex);
343 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
344 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
345 sqlite3Config.m.xFree(p);
346 sqlite3_mutex_leave(mem0.mutex);
347 }else{
348 sqlite3Config.m.xFree(p);
349 }
drh9ac3fe92008-06-18 18:12:04 +0000350 }else{
351 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000352 i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
drh9ac3fe92008-06-18 18:12:04 +0000353 i /= sqlite3Config.szScratch;
354 assert( i>=0 && i<sqlite3Config.nScratch );
drhf7141992008-06-19 00:16:08 +0000355 sqlite3_mutex_enter(mem0.mutex);
356 assert( mem0.nScratchFree<sqlite3Config.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000357 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000358 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000359 sqlite3_mutex_leave(mem0.mutex);
360 }
drhe5ae5732008-06-15 02:51:47 +0000361 }
362}
363
364/*
drhf7141992008-06-19 00:16:08 +0000365** Allocate memory to be used by the page cache. Make use of the
366** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
367** and that memory is of the right size and is not completely
368** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000369*/
drhf7141992008-06-19 00:16:08 +0000370void *sqlite3PageMalloc(int n){
371 void *p;
372 assert( n>0 );
373 assert( (n & (n-1))==0 );
374 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000375
376 if( sqlite3Config.szPage<n ){
377 goto page_overflow;
378 }else{
379 sqlite3_mutex_enter(mem0.mutex);
380 if( mem0.nPageFree==0 ){
381 sqlite3_mutex_leave(mem0.mutex);
382 goto page_overflow;
383 }else{
384 int i;
385 i = mem0.aPageFree[--mem0.nPageFree];
386 sqlite3_mutex_leave(mem0.mutex);
387 i *= sqlite3Config.szPage;
388 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
389 p = (void*)&((char*)sqlite3Config.pPage)[i];
390 }
391 }
392 return p;
393
394page_overflow:
395 if( sqlite3Config.bMemstat ){
396 sqlite3_mutex_enter(mem0.mutex);
397 n = mallocWithAlarm(n, &p);
398 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
399 sqlite3_mutex_leave(mem0.mutex);
400 }else{
401 p = sqlite3Config.m.xMalloc(n);
402 }
403 return p;
drhfacf0302008-06-17 15:12:00 +0000404}
drhf7141992008-06-19 00:16:08 +0000405void sqlite3PageFree(void *p){
406 if( p ){
407 if( sqlite3Config.pPage==0
408 || p<sqlite3Config.pPage
409 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000410 /* In this case, the page allocation was obtained from a regular
411 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
412 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
413 */
drhf7141992008-06-19 00:16:08 +0000414 if( sqlite3Config.bMemstat ){
415 int iSize = sqlite3MallocSize(p);
416 sqlite3_mutex_enter(mem0.mutex);
417 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
418 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
419 sqlite3Config.m.xFree(p);
420 sqlite3_mutex_leave(mem0.mutex);
421 }else{
422 sqlite3Config.m.xFree(p);
423 }
424 }else{
danielk19774b9507a2008-06-21 08:12:15 +0000425 /* The page allocation was allocated from the sqlite3Config.pPage
426 ** buffer. In this case all that is add the index of the page in
427 ** the sqlite3Config.pPage array to the set of free indexes stored
428 ** in the mem0.aPageFree[] array.
429 */
drhf7141992008-06-19 00:16:08 +0000430 int i;
danielk1977867d05a2008-06-23 14:03:45 +0000431 i = (u8 *)p - (u8 *)sqlite3Config.pPage;
drhf7141992008-06-19 00:16:08 +0000432 i /= sqlite3Config.szPage;
433 assert( i>=0 && i<sqlite3Config.nPage );
434 sqlite3_mutex_enter(mem0.mutex);
435 assert( mem0.nPageFree<sqlite3Config.nPage );
436 mem0.aPageFree[mem0.nPageFree++] = i;
437 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
438 sqlite3_mutex_leave(mem0.mutex);
danielk19774b9507a2008-06-21 08:12:15 +0000439#ifndef NDEBUG
440 /* Assert that a duplicate was not just inserted into aPageFree[]. */
441 for(i=0; i<mem0.nPageFree-1; i++){
442 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
443 }
444#endif
drhf7141992008-06-19 00:16:08 +0000445 }
446 }
drhfacf0302008-06-17 15:12:00 +0000447}
448
449/*
drhfec00ea2008-06-14 16:56:21 +0000450** Return the size of a memory allocation previously obtained from
451** sqlite3Malloc() or sqlite3_malloc().
452*/
453int sqlite3MallocSize(void *p){
454 return sqlite3Config.m.xSize(p);
455}
456
457/*
458** Free memory previously obtained from sqlite3Malloc().
459*/
460void sqlite3_free(void *p){
461 if( p==0 ) return;
462 if( sqlite3Config.bMemstat ){
463 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000464 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drhfec00ea2008-06-14 16:56:21 +0000465 sqlite3Config.m.xFree(p);
466 sqlite3_mutex_leave(mem0.mutex);
467 }else{
468 sqlite3Config.m.xFree(p);
469 }
470}
471
472/*
473** Change the size of an existing memory allocation
474*/
475void *sqlite3Realloc(void *pOld, int nBytes){
476 int nOld, nNew;
477 void *pNew;
478 if( pOld==0 ){
479 return sqlite3Malloc(nBytes);
480 }
481 if( nBytes<=0 ){
482 sqlite3_free(pOld);
483 return 0;
484 }
485 nOld = sqlite3MallocSize(pOld);
486 if( sqlite3Config.bMemstat ){
487 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000488 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000489 nNew = sqlite3Config.m.xRoundup(nBytes);
490 if( nOld==nNew ){
491 pNew = pOld;
492 }else{
drhf7141992008-06-19 00:16:08 +0000493 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
494 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000495 sqlite3MallocAlarm(nNew-nOld);
496 }
danielk1977d09414c2008-06-19 18:17:49 +0000497 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
498 if( pNew==0 && mem0.alarmCallback ){
499 sqlite3MallocAlarm(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000500 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000501 }
502 if( pNew ){
drhf7141992008-06-19 00:16:08 +0000503 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000504 }
505 }
506 sqlite3_mutex_leave(mem0.mutex);
507 }else{
508 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
509 }
510 return pNew;
511}
512
513/*
514** The public interface to sqlite3Realloc. Make sure that the memory
515** subsystem is initialized prior to invoking sqliteRealloc.
516*/
517void *sqlite3_realloc(void *pOld, int n){
518#ifndef SQLITE_OMIT_AUTOINIT
519 if( sqlite3_initialize() ) return 0;
520#endif
521 return sqlite3Realloc(pOld, n);
522}
523
drha3152892007-05-05 11:48:52 +0000524
525/*
drh17435752007-08-16 04:30:38 +0000526** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000527*/
drhfec00ea2008-06-14 16:56:21 +0000528void *sqlite3MallocZero(int n){
529 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000530 if( p ){
531 memset(p, 0, n);
532 }
533 return p;
534}
drh17435752007-08-16 04:30:38 +0000535
536/*
537** Allocate and zero memory. If the allocation fails, make
538** the mallocFailed flag in the connection pointer.
539*/
drhfec00ea2008-06-14 16:56:21 +0000540void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000541 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000542 if( p ){
543 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000544 }
545 return p;
546}
547
548/*
549** Allocate and zero memory. If the allocation fails, make
550** the mallocFailed flag in the connection pointer.
551*/
drhfec00ea2008-06-14 16:56:21 +0000552void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000553 void *p = 0;
554 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000555 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000556 if( !p && db ){
557 db->mallocFailed = 1;
558 }
drh17435752007-08-16 04:30:38 +0000559 }
560 return p;
561}
562
danielk197726783a52007-08-29 14:06:22 +0000563/*
564** Resize the block of memory pointed to by p to n bytes. If the
565** resize fails, set the mallocFailed flag inthe connection object.
566*/
danielk1977a1644fd2007-08-29 12:31:25 +0000567void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
568 void *pNew = 0;
569 if( db->mallocFailed==0 ){
570 pNew = sqlite3_realloc(p, n);
571 if( !pNew ){
572 db->mallocFailed = 1;
573 }
574 }
575 return pNew;
576}
577
drh17435752007-08-16 04:30:38 +0000578/*
579** Attempt to reallocate p. If the reallocation fails, then free p
580** and set the mallocFailed flag in the database connection.
581*/
582void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000583 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000584 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000585 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000586 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000587 }
588 return pNew;
589}
590
drha3152892007-05-05 11:48:52 +0000591/*
592** Make a copy of a string in memory obtained from sqliteMalloc(). These
593** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
594** is because when memory debugging is turned on, these two functions are
595** called via macros that record the current file and line number in the
596** ThreadData structure.
597*/
598char *sqlite3StrDup(const char *z){
599 char *zNew;
600 int n;
601 if( z==0 ) return 0;
602 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000603 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000604 if( zNew ) memcpy(zNew, z, n);
605 return zNew;
606}
607char *sqlite3StrNDup(const char *z, int n){
608 char *zNew;
609 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000610 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000611 if( zNew ){
612 memcpy(zNew, z, n);
613 zNew[n] = 0;
614 }
615 return zNew;
616}
617
danielk19771e536952007-08-16 10:09:01 +0000618char *sqlite3DbStrDup(sqlite3 *db, const char *z){
619 char *zNew = sqlite3StrDup(z);
620 if( z && !zNew ){
621 db->mallocFailed = 1;
622 }
623 return zNew;
624}
625char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
626 char *zNew = sqlite3StrNDup(z, n);
627 if( z && !zNew ){
628 db->mallocFailed = 1;
629 }
630 return zNew;
631}
632
drha3152892007-05-05 11:48:52 +0000633/*
drhf089aa42008-07-08 19:34:06 +0000634** Create a string from the zFromat argument and the va_list that follows.
635** Store the string in memory obtained from sqliteMalloc() and make *pz
636** point to that string.
drha3152892007-05-05 11:48:52 +0000637*/
drhf089aa42008-07-08 19:34:06 +0000638void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000639 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000640 char *z;
drha3152892007-05-05 11:48:52 +0000641
drhf089aa42008-07-08 19:34:06 +0000642 va_start(ap, zFormat);
643 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000644 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000645 sqlite3_free(*pz);
drhf089aa42008-07-08 19:34:06 +0000646 *pz = z;
drha3152892007-05-05 11:48:52 +0000647}
648
649
650/*
651** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000652** returning control to the user) that has called sqlite3_malloc or
653** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000654**
655** The returned value is normally a copy of the second argument to this
656** function. However, if a malloc() failure has occured since the previous
657** invocation SQLITE_NOMEM is returned instead.
658**
659** If the first argument, db, is not NULL and a malloc() error has occured,
660** then the connection error-code (the value returned by sqlite3_errcode())
661** is set to SQLITE_NOMEM.
662*/
drha3152892007-05-05 11:48:52 +0000663int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000664 /* If the db handle is not NULL, then we must hold the connection handle
665 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
666 ** is unsafe, as is the call to sqlite3Error().
667 */
668 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000669 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000670 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000671 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000672 rc = SQLITE_NOMEM;
673 }
674 return rc & (db ? db->errMask : 0xff);
675}