blob: 3441123c69162ee9bd08899456151a759e853a57 [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**
drhf7141992008-06-19 00:16:08 +000015** $Id: malloc.c,v 1.21 2008/06/19 00:16:08 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/*
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 }
216 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
217 p = 0;
218 }else{
219 p = sqlite3Config.m.xMalloc(nFull);
220 if( p==0 ){
221 sqlite3MallocAlarm(nFull);
222 p = malloc(nFull);
223 }
224 }
225 if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
226 *pp = p;
227 return nFull;
228}
drhfec00ea2008-06-14 16:56:21 +0000229
230/*
231** Allocate memory. This routine is like sqlite3_malloc() except that it
232** assumes the memory subsystem has already been initialized.
233*/
234void *sqlite3Malloc(int n){
235 void *p;
drhfec00ea2008-06-14 16:56:21 +0000236 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000237 p = 0;
drhfec00ea2008-06-14 16:56:21 +0000238 }else if( sqlite3Config.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000239 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000240 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000241 sqlite3_mutex_leave(mem0.mutex);
242 }else{
243 p = sqlite3Config.m.xMalloc(n);
244 }
245 return p;
246}
247
248/*
249** This version of the memory allocation is for use by the application.
250** First make sure the memory subsystem is initialized, then do the
251** allocation.
252*/
253void *sqlite3_malloc(int n){
254#ifndef SQLITE_OMIT_AUTOINIT
255 if( sqlite3_initialize() ) return 0;
256#endif
257 return sqlite3Malloc(n);
258}
259
260/*
drhe5ae5732008-06-15 02:51:47 +0000261** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000262** xScratchMalloc(). We verify this constraint in the single-threaded
263** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000264** is outstanding clearing it when the allocation is freed.
265*/
266#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000267static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000268#endif
269
270
271/*
272** Allocate memory that is to be used and released right away.
273** This routine is similar to alloca() in that it is not intended
274** for situations where the memory might be held long-term. This
275** routine is intended to get memory to old large transient data
276** structures that would not normally fit on the stack of an
277** embedded processor.
278*/
drhfacf0302008-06-17 15:12:00 +0000279void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000280 void *p;
281 assert( n>0 );
282 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
283 return 0;
284 }
drh9ac3fe92008-06-18 18:12:04 +0000285
drhe5ae5732008-06-15 02:51:47 +0000286#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000287 /* Verify that no more than one scratch allocation per thread
288 ** is outstanding at one time. (This is only checked in the
289 ** single-threaded case since checking in the multi-threaded case
290 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000291 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000292#endif
drh9ac3fe92008-06-18 18:12:04 +0000293
drhf7141992008-06-19 00:16:08 +0000294 if( sqlite3Config.szScratch<n ){
295 goto scratch_overflow;
296 }else{
297 sqlite3_mutex_enter(mem0.mutex);
298 if( mem0.nScratchFree==0 ){
299 sqlite3_mutex_leave(mem0.mutex);
300 goto scratch_overflow;
301 }else{
302 int i;
303 i = mem0.aScratchFree[--mem0.nScratchFree];
304 sqlite3_mutex_leave(mem0.mutex);
305 i *= sqlite3Config.szScratch;
306 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
307 p = (void*)&((char*)sqlite3Config.pScratch)[i];
308 }
drhe5ae5732008-06-15 02:51:47 +0000309 }
drhf7141992008-06-19 00:16:08 +0000310#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
311 scratchAllocOut = p!=0;
312#endif
313
drhe5ae5732008-06-15 02:51:47 +0000314 return p;
drhf7141992008-06-19 00:16:08 +0000315
316scratch_overflow:
317 if( sqlite3Config.bMemstat ){
318 sqlite3_mutex_enter(mem0.mutex);
319 n = mallocWithAlarm(n, &p);
320 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
321 sqlite3_mutex_leave(mem0.mutex);
322 }else{
323 p = sqlite3Config.m.xMalloc(n);
324 }
325#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
326 scratchAllocOut = p!=0;
327#endif
328 return p;
drhe5ae5732008-06-15 02:51:47 +0000329}
drhfacf0302008-06-17 15:12:00 +0000330void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000331 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000332
drhe5ae5732008-06-15 02:51:47 +0000333#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000334 /* Verify that no more than one scratch allocation per thread
335 ** is outstanding at one time. (This is only checked in the
336 ** single-threaded case since checking in the multi-threaded case
337 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000338 assert( scratchAllocOut==1 );
339 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000340#endif
drh9ac3fe92008-06-18 18:12:04 +0000341
342 if( sqlite3Config.pScratch==0
drhf7141992008-06-19 00:16:08 +0000343 || p<sqlite3Config.pScratch
344 || p>=(void*)mem0.aScratchFree ){
345 if( sqlite3Config.bMemstat ){
346 int iSize = sqlite3MallocSize(p);
347 sqlite3_mutex_enter(mem0.mutex);
348 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
349 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
350 sqlite3Config.m.xFree(p);
351 sqlite3_mutex_leave(mem0.mutex);
352 }else{
353 sqlite3Config.m.xFree(p);
354 }
drh9ac3fe92008-06-18 18:12:04 +0000355 }else{
356 int i;
drh9ac3fe92008-06-18 18:12:04 +0000357 i = p - sqlite3Config.pScratch;
358 i /= sqlite3Config.szScratch;
359 assert( i>=0 && i<sqlite3Config.nScratch );
drhf7141992008-06-19 00:16:08 +0000360 sqlite3_mutex_enter(mem0.mutex);
361 assert( mem0.nScratchFree<sqlite3Config.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000362 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000363 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000364 sqlite3_mutex_leave(mem0.mutex);
365 }
drhe5ae5732008-06-15 02:51:47 +0000366 }
367}
368
369/*
drhf7141992008-06-19 00:16:08 +0000370** Allocate memory to be used by the page cache. Make use of the
371** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
372** and that memory is of the right size and is not completely
373** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000374*/
drhf7141992008-06-19 00:16:08 +0000375void *sqlite3PageMalloc(int n){
376 void *p;
377 assert( n>0 );
378 assert( (n & (n-1))==0 );
379 assert( n>=512 && n<=32768 );
380 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
381 return 0;
382 }
383
384 if( sqlite3Config.szPage<n ){
385 goto page_overflow;
386 }else{
387 sqlite3_mutex_enter(mem0.mutex);
388 if( mem0.nPageFree==0 ){
389 sqlite3_mutex_leave(mem0.mutex);
390 goto page_overflow;
391 }else{
392 int i;
393 i = mem0.aPageFree[--mem0.nPageFree];
394 sqlite3_mutex_leave(mem0.mutex);
395 i *= sqlite3Config.szPage;
396 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
397 p = (void*)&((char*)sqlite3Config.pPage)[i];
398 }
399 }
400 return p;
401
402page_overflow:
403 if( sqlite3Config.bMemstat ){
404 sqlite3_mutex_enter(mem0.mutex);
405 n = mallocWithAlarm(n, &p);
406 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
407 sqlite3_mutex_leave(mem0.mutex);
408 }else{
409 p = sqlite3Config.m.xMalloc(n);
410 }
411 return p;
drhfacf0302008-06-17 15:12:00 +0000412}
drhf7141992008-06-19 00:16:08 +0000413void sqlite3PageFree(void *p){
414 if( p ){
415 if( sqlite3Config.pPage==0
416 || p<sqlite3Config.pPage
417 || p>=(void*)mem0.aPageFree ){
418 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{
429 int i;
430 i = p - sqlite3Config.pPage;
431 i /= sqlite3Config.szPage;
432 assert( i>=0 && i<sqlite3Config.nPage );
433 sqlite3_mutex_enter(mem0.mutex);
434 assert( mem0.nPageFree<sqlite3Config.nPage );
435 mem0.aPageFree[mem0.nPageFree++] = i;
436 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
437 sqlite3_mutex_leave(mem0.mutex);
438 }
439 }
drhfacf0302008-06-17 15:12:00 +0000440}
441
442/*
drhfec00ea2008-06-14 16:56:21 +0000443** Return the size of a memory allocation previously obtained from
444** sqlite3Malloc() or sqlite3_malloc().
445*/
446int sqlite3MallocSize(void *p){
447 return sqlite3Config.m.xSize(p);
448}
449
450/*
451** Free memory previously obtained from sqlite3Malloc().
452*/
453void sqlite3_free(void *p){
454 if( p==0 ) return;
455 if( sqlite3Config.bMemstat ){
456 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000457 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drhfec00ea2008-06-14 16:56:21 +0000458 sqlite3Config.m.xFree(p);
459 sqlite3_mutex_leave(mem0.mutex);
460 }else{
461 sqlite3Config.m.xFree(p);
462 }
463}
464
465/*
466** Change the size of an existing memory allocation
467*/
468void *sqlite3Realloc(void *pOld, int nBytes){
469 int nOld, nNew;
470 void *pNew;
471 if( pOld==0 ){
472 return sqlite3Malloc(nBytes);
473 }
474 if( nBytes<=0 ){
475 sqlite3_free(pOld);
476 return 0;
477 }
478 nOld = sqlite3MallocSize(pOld);
479 if( sqlite3Config.bMemstat ){
480 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000481 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000482 nNew = sqlite3Config.m.xRoundup(nBytes);
483 if( nOld==nNew ){
484 pNew = pOld;
485 }else{
drhf7141992008-06-19 00:16:08 +0000486 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
487 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000488 sqlite3MallocAlarm(nNew-nOld);
489 }
490 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
491 pNew = 0;
492 }else{
493 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
494 if( pNew==0 ){
495 sqlite3MallocAlarm(nBytes);
496 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
497 }
498 }
499 if( pNew ){
drhf7141992008-06-19 00:16:08 +0000500 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000501 }
502 }
503 sqlite3_mutex_leave(mem0.mutex);
504 }else{
505 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
506 }
507 return pNew;
508}
509
510/*
511** The public interface to sqlite3Realloc. Make sure that the memory
512** subsystem is initialized prior to invoking sqliteRealloc.
513*/
514void *sqlite3_realloc(void *pOld, int n){
515#ifndef SQLITE_OMIT_AUTOINIT
516 if( sqlite3_initialize() ) return 0;
517#endif
518 return sqlite3Realloc(pOld, n);
519}
520
drha3152892007-05-05 11:48:52 +0000521
522/*
drh17435752007-08-16 04:30:38 +0000523** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000524*/
drhfec00ea2008-06-14 16:56:21 +0000525void *sqlite3MallocZero(int n){
526 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000527 if( p ){
528 memset(p, 0, n);
529 }
530 return p;
531}
drh17435752007-08-16 04:30:38 +0000532
533/*
534** Allocate and zero memory. If the allocation fails, make
535** the mallocFailed flag in the connection pointer.
536*/
drhfec00ea2008-06-14 16:56:21 +0000537void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000538 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000539 if( p ){
540 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000541 }
542 return p;
543}
544
545/*
546** Allocate and zero memory. If the allocation fails, make
547** the mallocFailed flag in the connection pointer.
548*/
drhfec00ea2008-06-14 16:56:21 +0000549void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000550 void *p = 0;
551 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000552 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000553 if( !p && db ){
554 db->mallocFailed = 1;
555 }
drh17435752007-08-16 04:30:38 +0000556 }
557 return p;
558}
559
danielk197726783a52007-08-29 14:06:22 +0000560/*
561** Resize the block of memory pointed to by p to n bytes. If the
562** resize fails, set the mallocFailed flag inthe connection object.
563*/
danielk1977a1644fd2007-08-29 12:31:25 +0000564void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
565 void *pNew = 0;
566 if( db->mallocFailed==0 ){
567 pNew = sqlite3_realloc(p, n);
568 if( !pNew ){
569 db->mallocFailed = 1;
570 }
571 }
572 return pNew;
573}
574
drh17435752007-08-16 04:30:38 +0000575/*
576** Attempt to reallocate p. If the reallocation fails, then free p
577** and set the mallocFailed flag in the database connection.
578*/
579void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000580 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000581 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000582 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000583 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000584 }
585 return pNew;
586}
587
drha3152892007-05-05 11:48:52 +0000588/*
589** Make a copy of a string in memory obtained from sqliteMalloc(). These
590** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
591** is because when memory debugging is turned on, these two functions are
592** called via macros that record the current file and line number in the
593** ThreadData structure.
594*/
595char *sqlite3StrDup(const char *z){
596 char *zNew;
597 int n;
598 if( z==0 ) return 0;
599 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000600 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000601 if( zNew ) memcpy(zNew, z, n);
602 return zNew;
603}
604char *sqlite3StrNDup(const char *z, int n){
605 char *zNew;
606 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000607 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000608 if( zNew ){
609 memcpy(zNew, z, n);
610 zNew[n] = 0;
611 }
612 return zNew;
613}
614
danielk19771e536952007-08-16 10:09:01 +0000615char *sqlite3DbStrDup(sqlite3 *db, const char *z){
616 char *zNew = sqlite3StrDup(z);
617 if( z && !zNew ){
618 db->mallocFailed = 1;
619 }
620 return zNew;
621}
622char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
623 char *zNew = sqlite3StrNDup(z, n);
624 if( z && !zNew ){
625 db->mallocFailed = 1;
626 }
627 return zNew;
628}
629
drha3152892007-05-05 11:48:52 +0000630/*
631** Create a string from the 2nd and subsequent arguments (up to the
632** first NULL argument), store the string in memory obtained from
633** sqliteMalloc() and make the pointer indicated by the 1st argument
634** point to that string. The 1st argument must either be NULL or
635** point to memory obtained from sqliteMalloc().
636*/
637void sqlite3SetString(char **pz, ...){
638 va_list ap;
639 int nByte;
640 const char *z;
641 char *zResult;
642
643 assert( pz!=0 );
644 nByte = 1;
645 va_start(ap, pz);
646 while( (z = va_arg(ap, const char*))!=0 ){
647 nByte += strlen(z);
648 }
649 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000650 sqlite3_free(*pz);
drhe5ae5732008-06-15 02:51:47 +0000651 *pz = zResult = sqlite3Malloc(nByte);
drha3152892007-05-05 11:48:52 +0000652 if( zResult==0 ){
653 return;
654 }
655 *zResult = 0;
656 va_start(ap, pz);
657 while( (z = va_arg(ap, const char*))!=0 ){
658 int n = strlen(z);
659 memcpy(zResult, z, n);
660 zResult += n;
661 }
662 zResult[0] = 0;
663 va_end(ap);
664}
665
666
667/*
668** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000669** returning control to the user) that has called sqlite3_malloc or
670** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000671**
672** The returned value is normally a copy of the second argument to this
673** function. However, if a malloc() failure has occured since the previous
674** invocation SQLITE_NOMEM is returned instead.
675**
676** If the first argument, db, is not NULL and a malloc() error has occured,
677** then the connection error-code (the value returned by sqlite3_errcode())
678** is set to SQLITE_NOMEM.
679*/
drha3152892007-05-05 11:48:52 +0000680int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000681 /* If the db handle is not NULL, then we must hold the connection handle
682 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
683 ** is unsafe, as is the call to sqlite3Error().
684 */
685 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000686 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000687 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000688 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000689 rc = SQLITE_NOMEM;
690 }
691 return rc & (db ? db->errMask : 0xff);
692}