blob: c107c36abba948d06dcb8d03f92307bd2cf817a8 [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**
danielk1977d09414c2008-06-19 18:17:49 +000015** $Id: malloc.c,v 1.22 2008/06/19 18:17:50 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 ){
408 if( sqlite3Config.bMemstat ){
409 int iSize = sqlite3MallocSize(p);
410 sqlite3_mutex_enter(mem0.mutex);
411 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
412 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
413 sqlite3Config.m.xFree(p);
414 sqlite3_mutex_leave(mem0.mutex);
415 }else{
416 sqlite3Config.m.xFree(p);
417 }
418 }else{
419 int i;
420 i = p - sqlite3Config.pPage;
421 i /= sqlite3Config.szPage;
422 assert( i>=0 && i<sqlite3Config.nPage );
423 sqlite3_mutex_enter(mem0.mutex);
424 assert( mem0.nPageFree<sqlite3Config.nPage );
425 mem0.aPageFree[mem0.nPageFree++] = i;
426 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
427 sqlite3_mutex_leave(mem0.mutex);
428 }
429 }
drhfacf0302008-06-17 15:12:00 +0000430}
431
432/*
drhfec00ea2008-06-14 16:56:21 +0000433** Return the size of a memory allocation previously obtained from
434** sqlite3Malloc() or sqlite3_malloc().
435*/
436int sqlite3MallocSize(void *p){
437 return sqlite3Config.m.xSize(p);
438}
439
440/*
441** Free memory previously obtained from sqlite3Malloc().
442*/
443void sqlite3_free(void *p){
444 if( p==0 ) return;
445 if( sqlite3Config.bMemstat ){
446 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000447 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
drhfec00ea2008-06-14 16:56:21 +0000448 sqlite3Config.m.xFree(p);
449 sqlite3_mutex_leave(mem0.mutex);
450 }else{
451 sqlite3Config.m.xFree(p);
452 }
453}
454
455/*
456** Change the size of an existing memory allocation
457*/
458void *sqlite3Realloc(void *pOld, int nBytes){
459 int nOld, nNew;
460 void *pNew;
461 if( pOld==0 ){
462 return sqlite3Malloc(nBytes);
463 }
464 if( nBytes<=0 ){
465 sqlite3_free(pOld);
466 return 0;
467 }
468 nOld = sqlite3MallocSize(pOld);
469 if( sqlite3Config.bMemstat ){
470 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000471 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000472 nNew = sqlite3Config.m.xRoundup(nBytes);
473 if( nOld==nNew ){
474 pNew = pOld;
475 }else{
drhf7141992008-06-19 00:16:08 +0000476 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
477 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000478 sqlite3MallocAlarm(nNew-nOld);
479 }
danielk1977d09414c2008-06-19 18:17:49 +0000480 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
481 if( pNew==0 && mem0.alarmCallback ){
482 sqlite3MallocAlarm(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000483 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000484 }
485 if( pNew ){
drhf7141992008-06-19 00:16:08 +0000486 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000487 }
488 }
489 sqlite3_mutex_leave(mem0.mutex);
490 }else{
491 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
492 }
493 return pNew;
494}
495
496/*
497** The public interface to sqlite3Realloc. Make sure that the memory
498** subsystem is initialized prior to invoking sqliteRealloc.
499*/
500void *sqlite3_realloc(void *pOld, int n){
501#ifndef SQLITE_OMIT_AUTOINIT
502 if( sqlite3_initialize() ) return 0;
503#endif
504 return sqlite3Realloc(pOld, n);
505}
506
drha3152892007-05-05 11:48:52 +0000507
508/*
drh17435752007-08-16 04:30:38 +0000509** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000510*/
drhfec00ea2008-06-14 16:56:21 +0000511void *sqlite3MallocZero(int n){
512 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000513 if( p ){
514 memset(p, 0, n);
515 }
516 return p;
517}
drh17435752007-08-16 04:30:38 +0000518
519/*
520** Allocate and zero memory. If the allocation fails, make
521** the mallocFailed flag in the connection pointer.
522*/
drhfec00ea2008-06-14 16:56:21 +0000523void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000524 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000525 if( p ){
526 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000527 }
528 return p;
529}
530
531/*
532** Allocate and zero memory. If the allocation fails, make
533** the mallocFailed flag in the connection pointer.
534*/
drhfec00ea2008-06-14 16:56:21 +0000535void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000536 void *p = 0;
537 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000538 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000539 if( !p && db ){
540 db->mallocFailed = 1;
541 }
drh17435752007-08-16 04:30:38 +0000542 }
543 return p;
544}
545
danielk197726783a52007-08-29 14:06:22 +0000546/*
547** Resize the block of memory pointed to by p to n bytes. If the
548** resize fails, set the mallocFailed flag inthe connection object.
549*/
danielk1977a1644fd2007-08-29 12:31:25 +0000550void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
551 void *pNew = 0;
552 if( db->mallocFailed==0 ){
553 pNew = sqlite3_realloc(p, n);
554 if( !pNew ){
555 db->mallocFailed = 1;
556 }
557 }
558 return pNew;
559}
560
drh17435752007-08-16 04:30:38 +0000561/*
562** Attempt to reallocate p. If the reallocation fails, then free p
563** and set the mallocFailed flag in the database connection.
564*/
565void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000566 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000567 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000568 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000569 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000570 }
571 return pNew;
572}
573
drha3152892007-05-05 11:48:52 +0000574/*
575** Make a copy of a string in memory obtained from sqliteMalloc(). These
576** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
577** is because when memory debugging is turned on, these two functions are
578** called via macros that record the current file and line number in the
579** ThreadData structure.
580*/
581char *sqlite3StrDup(const char *z){
582 char *zNew;
583 int n;
584 if( z==0 ) return 0;
585 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000586 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000587 if( zNew ) memcpy(zNew, z, n);
588 return zNew;
589}
590char *sqlite3StrNDup(const char *z, int n){
591 char *zNew;
592 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000593 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000594 if( zNew ){
595 memcpy(zNew, z, n);
596 zNew[n] = 0;
597 }
598 return zNew;
599}
600
danielk19771e536952007-08-16 10:09:01 +0000601char *sqlite3DbStrDup(sqlite3 *db, const char *z){
602 char *zNew = sqlite3StrDup(z);
603 if( z && !zNew ){
604 db->mallocFailed = 1;
605 }
606 return zNew;
607}
608char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
609 char *zNew = sqlite3StrNDup(z, n);
610 if( z && !zNew ){
611 db->mallocFailed = 1;
612 }
613 return zNew;
614}
615
drha3152892007-05-05 11:48:52 +0000616/*
617** Create a string from the 2nd and subsequent arguments (up to the
618** first NULL argument), store the string in memory obtained from
619** sqliteMalloc() and make the pointer indicated by the 1st argument
620** point to that string. The 1st argument must either be NULL or
621** point to memory obtained from sqliteMalloc().
622*/
623void sqlite3SetString(char **pz, ...){
624 va_list ap;
625 int nByte;
626 const char *z;
627 char *zResult;
628
629 assert( pz!=0 );
630 nByte = 1;
631 va_start(ap, pz);
632 while( (z = va_arg(ap, const char*))!=0 ){
633 nByte += strlen(z);
634 }
635 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000636 sqlite3_free(*pz);
drhe5ae5732008-06-15 02:51:47 +0000637 *pz = zResult = sqlite3Malloc(nByte);
drha3152892007-05-05 11:48:52 +0000638 if( zResult==0 ){
639 return;
640 }
641 *zResult = 0;
642 va_start(ap, pz);
643 while( (z = va_arg(ap, const char*))!=0 ){
644 int n = strlen(z);
645 memcpy(zResult, z, n);
646 zResult += n;
647 }
648 zResult[0] = 0;
649 va_end(ap);
650}
651
652
653/*
654** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000655** returning control to the user) that has called sqlite3_malloc or
656** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000657**
658** The returned value is normally a copy of the second argument to this
659** function. However, if a malloc() failure has occured since the previous
660** invocation SQLITE_NOMEM is returned instead.
661**
662** If the first argument, db, is not NULL and a malloc() error has occured,
663** then the connection error-code (the value returned by sqlite3_errcode())
664** is set to SQLITE_NOMEM.
665*/
drha3152892007-05-05 11:48:52 +0000666int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000667 /* If the db handle is not NULL, then we must hold the connection handle
668 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
669 ** is unsafe, as is the call to sqlite3Error().
670 */
671 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000672 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000673 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000674 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000675 rc = SQLITE_NOMEM;
676 }
677 return rc & (db ? db->errMask : 0xff);
678}