blob: 85ac1f133b5cc13607412d2e5f8a31d033e9af8a [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**
danielk19774b9507a2008-06-21 08:12:15 +000015** $Id: malloc.c,v 1.23 2008/06/21 08:12:15 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/*
35** Set the soft heap-size limit for the current thread. Passing a
36** zero or 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/*
59** Release memory held by SQLite instances created by the current thread.
60*/
61int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +000062#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk1977dfb316d2008-03-26 18:34:43 +000063 int nRet = sqlite3VdbeReleaseMemory(n);
64 nRet += sqlite3PagerReleaseMemory(n-nRet);
65 return nRet;
danielk19771e536952007-08-16 10:09:01 +000066#else
67 return SQLITE_OK;
68#endif
drha3152892007-05-05 11:48:52 +000069}
drha3152892007-05-05 11:48:52 +000070
drhfec00ea2008-06-14 16:56:21 +000071/*
72** State information local to the memory allocation subsystem.
73*/
74static struct {
75 sqlite3_mutex *mutex; /* Mutex to serialize access */
76
77 /*
78 ** The alarm callback and its arguments. The mem0.mutex lock will
79 ** be held while the callback is running. Recursive calls into
80 ** the memory subsystem are allowed, but no new callbacks will be
81 ** issued. The alarmBusy variable is set to prevent recursive
82 ** callbacks.
83 */
84 sqlite3_int64 alarmThreshold;
85 void (*alarmCallback)(void*, sqlite3_int64,int);
86 void *alarmArg;
87 int alarmBusy;
88
89 /*
drh9ac3fe92008-06-18 18:12:04 +000090 ** Pointers to the end of sqlite3Config.pScratch and
91 ** sqlite3Config.pPage to a block of memory that records
92 ** which pages are available.
93 */
94 u32 *aScratchFree;
95 u32 *aPageFree;
96
97 /* Number of free pages for scratch and page-cache memory */
98 u32 nScratchFree;
99 u32 nPageFree;
drhfec00ea2008-06-14 16:56:21 +0000100} mem0;
101
102/*
103** Initialize the memory allocation subsystem.
104*/
105int sqlite3MallocInit(void){
106 if( sqlite3Config.m.xMalloc==0 ){
107 sqlite3MemSetDefault();
108 }
109 memset(&mem0, 0, sizeof(mem0));
drh9ac3fe92008-06-18 18:12:04 +0000110 if( sqlite3Config.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000111 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000112 }
drh9ac3fe92008-06-18 18:12:04 +0000113 if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000
114 && sqlite3Config.nScratch>0 ){
115 int i;
116 mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
117 [sqlite3Config.szScratch*sqlite3Config.nScratch];
118 for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
119 mem0.nScratchFree = sqlite3Config.nScratch;
120 }else{
121 sqlite3Config.pScratch = 0;
drhf7141992008-06-19 00:16:08 +0000122 sqlite3Config.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000123 }
124 if( sqlite3Config.pPage && sqlite3Config.szPage>=512
125 && sqlite3Config.nPage>0 ){
126 int i;
127 mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
128 [sqlite3Config.szPage*sqlite3Config.nPage];
129 for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
130 mem0.nPageFree = sqlite3Config.nPage;
131 }else{
132 sqlite3Config.pPage = 0;
drhf7141992008-06-19 00:16:08 +0000133 sqlite3Config.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000134 }
drhfec00ea2008-06-14 16:56:21 +0000135 return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
136}
137
138/*
139** Deinitialize the memory allocation subsystem.
140*/
141void sqlite3MallocEnd(void){
drh9ac3fe92008-06-18 18:12:04 +0000142 sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
143 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000144}
145
146/*
147** Return the amount of memory currently checked out.
148*/
149sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000150 int n, mx;
151 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
152 return (sqlite3_int64)n;
drhfec00ea2008-06-14 16:56:21 +0000153}
154
155/*
156** Return the maximum amount of memory that has ever been
157** checked out since either the beginning of this process
158** or since the most recent reset.
159*/
160sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000161 int n, mx;
162 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
163 return (sqlite3_int64)mx;
drhfec00ea2008-06-14 16:56:21 +0000164}
165
166/*
167** Change the alarm callback
168*/
169int sqlite3_memory_alarm(
170 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
171 void *pArg,
172 sqlite3_int64 iThreshold
173){
174 sqlite3_mutex_enter(mem0.mutex);
175 mem0.alarmCallback = xCallback;
176 mem0.alarmArg = pArg;
177 mem0.alarmThreshold = iThreshold;
178 sqlite3_mutex_leave(mem0.mutex);
179 return SQLITE_OK;
180}
181
182/*
183** Trigger the alarm
184*/
185static void sqlite3MallocAlarm(int nByte){
186 void (*xCallback)(void*,sqlite3_int64,int);
187 sqlite3_int64 nowUsed;
188 void *pArg;
189 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
190 mem0.alarmBusy = 1;
191 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000192 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000193 pArg = mem0.alarmArg;
194 sqlite3_mutex_leave(mem0.mutex);
195 xCallback(pArg, nowUsed, nByte);
196 sqlite3_mutex_enter(mem0.mutex);
197 mem0.alarmBusy = 0;
198}
199
drhf7141992008-06-19 00:16:08 +0000200/*
201** Do a memory allocation with statistics and alarms. Assume the
202** lock is already held.
203*/
204static int mallocWithAlarm(int n, void **pp){
205 int nFull;
206 void *p;
207 assert( sqlite3_mutex_held(mem0.mutex) );
208 nFull = sqlite3Config.m.xRoundup(n);
209 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
210 if( mem0.alarmCallback!=0 ){
211 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
212 if( nUsed+nFull >= mem0.alarmThreshold ){
213 sqlite3MallocAlarm(nFull);
214 }
215 }
danielk1977d09414c2008-06-19 18:17:49 +0000216 p = sqlite3Config.m.xMalloc(nFull);
217 if( p==0 && mem0.alarmCallback ){
218 sqlite3MallocAlarm(nFull);
drhf7141992008-06-19 00:16:08 +0000219 p = sqlite3Config.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000220 }
221 if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
222 *pp = p;
223 return nFull;
224}
drhfec00ea2008-06-14 16:56:21 +0000225
226/*
227** Allocate memory. This routine is like sqlite3_malloc() except that it
228** assumes the memory subsystem has already been initialized.
229*/
230void *sqlite3Malloc(int n){
231 void *p;
drhfec00ea2008-06-14 16:56:21 +0000232 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000233 p = 0;
drhfec00ea2008-06-14 16:56:21 +0000234 }else if( sqlite3Config.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000235 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000236 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000237 sqlite3_mutex_leave(mem0.mutex);
238 }else{
239 p = sqlite3Config.m.xMalloc(n);
240 }
241 return p;
242}
243
244/*
245** This version of the memory allocation is for use by the application.
246** First make sure the memory subsystem is initialized, then do the
247** allocation.
248*/
249void *sqlite3_malloc(int n){
250#ifndef SQLITE_OMIT_AUTOINIT
251 if( sqlite3_initialize() ) return 0;
252#endif
253 return sqlite3Malloc(n);
254}
255
256/*
drhe5ae5732008-06-15 02:51:47 +0000257** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000258** xScratchMalloc(). We verify this constraint in the single-threaded
259** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000260** is outstanding clearing it when the allocation is freed.
261*/
262#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000263static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000264#endif
265
266
267/*
268** Allocate memory that is to be used and released right away.
269** This routine is similar to alloca() in that it is not intended
270** for situations where the memory might be held long-term. This
271** routine is intended to get memory to old large transient data
272** structures that would not normally fit on the stack of an
273** embedded processor.
274*/
drhfacf0302008-06-17 15:12:00 +0000275void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000276 void *p;
277 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000278
drhe5ae5732008-06-15 02:51:47 +0000279#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000280 /* Verify that no more than one scratch allocation per thread
281 ** is outstanding at one time. (This is only checked in the
282 ** single-threaded case since checking in the multi-threaded case
283 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000284 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000285#endif
drh9ac3fe92008-06-18 18:12:04 +0000286
drhf7141992008-06-19 00:16:08 +0000287 if( sqlite3Config.szScratch<n ){
288 goto scratch_overflow;
289 }else{
290 sqlite3_mutex_enter(mem0.mutex);
291 if( mem0.nScratchFree==0 ){
292 sqlite3_mutex_leave(mem0.mutex);
293 goto scratch_overflow;
294 }else{
295 int i;
296 i = mem0.aScratchFree[--mem0.nScratchFree];
297 sqlite3_mutex_leave(mem0.mutex);
298 i *= sqlite3Config.szScratch;
299 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
300 p = (void*)&((char*)sqlite3Config.pScratch)[i];
301 }
drhe5ae5732008-06-15 02:51:47 +0000302 }
drhf7141992008-06-19 00:16:08 +0000303#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
304 scratchAllocOut = p!=0;
305#endif
306
drhe5ae5732008-06-15 02:51:47 +0000307 return p;
drhf7141992008-06-19 00:16:08 +0000308
309scratch_overflow:
310 if( sqlite3Config.bMemstat ){
311 sqlite3_mutex_enter(mem0.mutex);
312 n = mallocWithAlarm(n, &p);
313 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
314 sqlite3_mutex_leave(mem0.mutex);
315 }else{
316 p = sqlite3Config.m.xMalloc(n);
317 }
318#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
319 scratchAllocOut = p!=0;
320#endif
321 return p;
drhe5ae5732008-06-15 02:51:47 +0000322}
drhfacf0302008-06-17 15:12:00 +0000323void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000324 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000325
drhe5ae5732008-06-15 02:51:47 +0000326#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000327 /* Verify that no more than one scratch allocation per thread
328 ** is outstanding at one time. (This is only checked in the
329 ** single-threaded case since checking in the multi-threaded case
330 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000331 assert( scratchAllocOut==1 );
332 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000333#endif
drh9ac3fe92008-06-18 18:12:04 +0000334
335 if( sqlite3Config.pScratch==0
drhf7141992008-06-19 00:16:08 +0000336 || p<sqlite3Config.pScratch
337 || p>=(void*)mem0.aScratchFree ){
338 if( sqlite3Config.bMemstat ){
339 int iSize = sqlite3MallocSize(p);
340 sqlite3_mutex_enter(mem0.mutex);
341 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
342 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
343 sqlite3Config.m.xFree(p);
344 sqlite3_mutex_leave(mem0.mutex);
345 }else{
346 sqlite3Config.m.xFree(p);
347 }
drh9ac3fe92008-06-18 18:12:04 +0000348 }else{
349 int i;
drh9ac3fe92008-06-18 18:12:04 +0000350 i = p - sqlite3Config.pScratch;
351 i /= sqlite3Config.szScratch;
352 assert( i>=0 && i<sqlite3Config.nScratch );
drhf7141992008-06-19 00:16:08 +0000353 sqlite3_mutex_enter(mem0.mutex);
354 assert( mem0.nScratchFree<sqlite3Config.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000355 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000356 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000357 sqlite3_mutex_leave(mem0.mutex);
358 }
drhe5ae5732008-06-15 02:51:47 +0000359 }
360}
361
362/*
drhf7141992008-06-19 00:16:08 +0000363** Allocate memory to be used by the page cache. Make use of the
364** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
365** and that memory is of the right size and is not completely
366** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000367*/
drhf7141992008-06-19 00:16:08 +0000368void *sqlite3PageMalloc(int n){
369 void *p;
370 assert( n>0 );
371 assert( (n & (n-1))==0 );
372 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000373
374 if( sqlite3Config.szPage<n ){
375 goto page_overflow;
376 }else{
377 sqlite3_mutex_enter(mem0.mutex);
378 if( mem0.nPageFree==0 ){
379 sqlite3_mutex_leave(mem0.mutex);
380 goto page_overflow;
381 }else{
382 int i;
383 i = mem0.aPageFree[--mem0.nPageFree];
384 sqlite3_mutex_leave(mem0.mutex);
385 i *= sqlite3Config.szPage;
386 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
387 p = (void*)&((char*)sqlite3Config.pPage)[i];
388 }
389 }
390 return p;
391
392page_overflow:
393 if( sqlite3Config.bMemstat ){
394 sqlite3_mutex_enter(mem0.mutex);
395 n = mallocWithAlarm(n, &p);
396 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
397 sqlite3_mutex_leave(mem0.mutex);
398 }else{
399 p = sqlite3Config.m.xMalloc(n);
400 }
401 return p;
drhfacf0302008-06-17 15:12:00 +0000402}
drhf7141992008-06-19 00:16:08 +0000403void sqlite3PageFree(void *p){
404 if( p ){
405 if( sqlite3Config.pPage==0
406 || p<sqlite3Config.pPage
407 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000408 /* In this case, the page allocation was obtained from a regular
409 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
410 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
411 */
drhf7141992008-06-19 00:16:08 +0000412 if( sqlite3Config.bMemstat ){
413 int iSize = sqlite3MallocSize(p);
414 sqlite3_mutex_enter(mem0.mutex);
415 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
416 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
417 sqlite3Config.m.xFree(p);
418 sqlite3_mutex_leave(mem0.mutex);
419 }else{
420 sqlite3Config.m.xFree(p);
421 }
422 }else{
danielk19774b9507a2008-06-21 08:12:15 +0000423 /* The page allocation was allocated from the sqlite3Config.pPage
424 ** buffer. In this case all that is add the index of the page in
425 ** the sqlite3Config.pPage array to the set of free indexes stored
426 ** in the mem0.aPageFree[] array.
427 */
drhf7141992008-06-19 00:16:08 +0000428 int i;
429 i = p - sqlite3Config.pPage;
430 i /= sqlite3Config.szPage;
431 assert( i>=0 && i<sqlite3Config.nPage );
432 sqlite3_mutex_enter(mem0.mutex);
433 assert( mem0.nPageFree<sqlite3Config.nPage );
434 mem0.aPageFree[mem0.nPageFree++] = i;
435 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
436 sqlite3_mutex_leave(mem0.mutex);
danielk19774b9507a2008-06-21 08:12:15 +0000437#ifndef NDEBUG
438 /* Assert that a duplicate was not just inserted into aPageFree[]. */
439 for(i=0; i<mem0.nPageFree-1; i++){
440 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
441 }
442#endif
drhf7141992008-06-19 00:16:08 +0000443 }
444 }
drhfacf0302008-06-17 15:12:00 +0000445}
446
447/*
drhfec00ea2008-06-14 16:56:21 +0000448** Return the size of a memory allocation previously obtained from
449** sqlite3Malloc() or sqlite3_malloc().
450*/
451int sqlite3MallocSize(void *p){
452 return sqlite3Config.m.xSize(p);
453}
454
455/*
456** Free memory previously obtained from sqlite3Malloc().
457*/
458void sqlite3_free(void *p){
459 if( p==0 ) return;
460 if( sqlite3Config.bMemstat ){
461 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000462 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drhfec00ea2008-06-14 16:56:21 +0000463 sqlite3Config.m.xFree(p);
464 sqlite3_mutex_leave(mem0.mutex);
465 }else{
466 sqlite3Config.m.xFree(p);
467 }
468}
469
470/*
471** Change the size of an existing memory allocation
472*/
473void *sqlite3Realloc(void *pOld, int nBytes){
474 int nOld, nNew;
475 void *pNew;
476 if( pOld==0 ){
477 return sqlite3Malloc(nBytes);
478 }
479 if( nBytes<=0 ){
480 sqlite3_free(pOld);
481 return 0;
482 }
483 nOld = sqlite3MallocSize(pOld);
484 if( sqlite3Config.bMemstat ){
485 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000486 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000487 nNew = sqlite3Config.m.xRoundup(nBytes);
488 if( nOld==nNew ){
489 pNew = pOld;
490 }else{
drhf7141992008-06-19 00:16:08 +0000491 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
492 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000493 sqlite3MallocAlarm(nNew-nOld);
494 }
danielk1977d09414c2008-06-19 18:17:49 +0000495 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
496 if( pNew==0 && mem0.alarmCallback ){
497 sqlite3MallocAlarm(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000498 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000499 }
500 if( pNew ){
drhf7141992008-06-19 00:16:08 +0000501 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000502 }
503 }
504 sqlite3_mutex_leave(mem0.mutex);
505 }else{
506 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
507 }
508 return pNew;
509}
510
511/*
512** The public interface to sqlite3Realloc. Make sure that the memory
513** subsystem is initialized prior to invoking sqliteRealloc.
514*/
515void *sqlite3_realloc(void *pOld, int n){
516#ifndef SQLITE_OMIT_AUTOINIT
517 if( sqlite3_initialize() ) return 0;
518#endif
519 return sqlite3Realloc(pOld, n);
520}
521
drha3152892007-05-05 11:48:52 +0000522
523/*
drh17435752007-08-16 04:30:38 +0000524** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000525*/
drhfec00ea2008-06-14 16:56:21 +0000526void *sqlite3MallocZero(int n){
527 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000528 if( p ){
529 memset(p, 0, n);
530 }
531 return p;
532}
drh17435752007-08-16 04:30:38 +0000533
534/*
535** Allocate and zero memory. If the allocation fails, make
536** the mallocFailed flag in the connection pointer.
537*/
drhfec00ea2008-06-14 16:56:21 +0000538void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000539 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000540 if( p ){
541 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000542 }
543 return p;
544}
545
546/*
547** Allocate and zero memory. If the allocation fails, make
548** the mallocFailed flag in the connection pointer.
549*/
drhfec00ea2008-06-14 16:56:21 +0000550void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000551 void *p = 0;
552 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000553 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000554 if( !p && db ){
555 db->mallocFailed = 1;
556 }
drh17435752007-08-16 04:30:38 +0000557 }
558 return p;
559}
560
danielk197726783a52007-08-29 14:06:22 +0000561/*
562** Resize the block of memory pointed to by p to n bytes. If the
563** resize fails, set the mallocFailed flag inthe connection object.
564*/
danielk1977a1644fd2007-08-29 12:31:25 +0000565void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
566 void *pNew = 0;
567 if( db->mallocFailed==0 ){
568 pNew = sqlite3_realloc(p, n);
569 if( !pNew ){
570 db->mallocFailed = 1;
571 }
572 }
573 return pNew;
574}
575
drh17435752007-08-16 04:30:38 +0000576/*
577** Attempt to reallocate p. If the reallocation fails, then free p
578** and set the mallocFailed flag in the database connection.
579*/
580void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000581 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000582 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000583 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000584 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000585 }
586 return pNew;
587}
588
drha3152892007-05-05 11:48:52 +0000589/*
590** Make a copy of a string in memory obtained from sqliteMalloc(). These
591** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
592** is because when memory debugging is turned on, these two functions are
593** called via macros that record the current file and line number in the
594** ThreadData structure.
595*/
596char *sqlite3StrDup(const char *z){
597 char *zNew;
598 int n;
599 if( z==0 ) return 0;
600 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000601 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000602 if( zNew ) memcpy(zNew, z, n);
603 return zNew;
604}
605char *sqlite3StrNDup(const char *z, int n){
606 char *zNew;
607 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000608 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000609 if( zNew ){
610 memcpy(zNew, z, n);
611 zNew[n] = 0;
612 }
613 return zNew;
614}
615
danielk19771e536952007-08-16 10:09:01 +0000616char *sqlite3DbStrDup(sqlite3 *db, const char *z){
617 char *zNew = sqlite3StrDup(z);
618 if( z && !zNew ){
619 db->mallocFailed = 1;
620 }
621 return zNew;
622}
623char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
624 char *zNew = sqlite3StrNDup(z, n);
625 if( z && !zNew ){
626 db->mallocFailed = 1;
627 }
628 return zNew;
629}
630
drha3152892007-05-05 11:48:52 +0000631/*
632** Create a string from the 2nd and subsequent arguments (up to the
633** first NULL argument), store the string in memory obtained from
634** sqliteMalloc() and make the pointer indicated by the 1st argument
635** point to that string. The 1st argument must either be NULL or
636** point to memory obtained from sqliteMalloc().
637*/
638void sqlite3SetString(char **pz, ...){
639 va_list ap;
640 int nByte;
641 const char *z;
642 char *zResult;
643
644 assert( pz!=0 );
645 nByte = 1;
646 va_start(ap, pz);
647 while( (z = va_arg(ap, const char*))!=0 ){
648 nByte += strlen(z);
649 }
650 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000651 sqlite3_free(*pz);
drhe5ae5732008-06-15 02:51:47 +0000652 *pz = zResult = sqlite3Malloc(nByte);
drha3152892007-05-05 11:48:52 +0000653 if( zResult==0 ){
654 return;
655 }
656 *zResult = 0;
657 va_start(ap, pz);
658 while( (z = va_arg(ap, const char*))!=0 ){
659 int n = strlen(z);
660 memcpy(zResult, z, n);
661 zResult += n;
662 }
663 zResult[0] = 0;
664 va_end(ap);
665}
666
667
668/*
669** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000670** returning control to the user) that has called sqlite3_malloc or
671** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000672**
673** The returned value is normally a copy of the second argument to this
674** function. However, if a malloc() failure has occured since the previous
675** invocation SQLITE_NOMEM is returned instead.
676**
677** If the first argument, db, is not NULL and a malloc() error has occured,
678** then the connection error-code (the value returned by sqlite3_errcode())
679** is set to SQLITE_NOMEM.
680*/
drha3152892007-05-05 11:48:52 +0000681int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000682 /* If the db handle is not NULL, then we must hold the connection handle
683 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
684 ** is unsafe, as is the call to sqlite3Error().
685 */
686 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000687 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000688 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000689 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000690 rc = SQLITE_NOMEM;
691 }
692 return rc & (db ? db->errMask : 0xff);
693}