blob: a278e4816610743dd576d866a4891b552ebde45b [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**
drh9ac3fe92008-06-18 18:12:04 +000015** $Id: malloc.c,v 1.20 2008/06/18 18:12:04 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;
100
101 /*
drhfec00ea2008-06-14 16:56:21 +0000102 ** Performance statistics
103 */
104 sqlite3_int64 nowUsed; /* Main memory currently in use */
105 sqlite3_int64 mxUsed; /* Highwater mark for nowUsed */
drhe5ae5732008-06-15 02:51:47 +0000106 int mxReq; /* Max request size for ordinary mallocs */
drhfacf0302008-06-17 15:12:00 +0000107 int mxScratchReq; /* Max request size for xTemp mallocs */
drhfec00ea2008-06-14 16:56:21 +0000108} mem0;
109
110/*
111** Initialize the memory allocation subsystem.
112*/
113int sqlite3MallocInit(void){
114 if( sqlite3Config.m.xMalloc==0 ){
115 sqlite3MemSetDefault();
116 }
117 memset(&mem0, 0, sizeof(mem0));
drh9ac3fe92008-06-18 18:12:04 +0000118 if( sqlite3Config.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000119 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000120 }
drh9ac3fe92008-06-18 18:12:04 +0000121 if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000
122 && sqlite3Config.nScratch>0 ){
123 int i;
124 mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
125 [sqlite3Config.szScratch*sqlite3Config.nScratch];
126 for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
127 mem0.nScratchFree = sqlite3Config.nScratch;
128 }else{
129 sqlite3Config.pScratch = 0;
130 }
131 if( sqlite3Config.pPage && sqlite3Config.szPage>=512
132 && sqlite3Config.nPage>0 ){
133 int i;
134 mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
135 [sqlite3Config.szPage*sqlite3Config.nPage];
136 for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
137 mem0.nPageFree = sqlite3Config.nPage;
138 }else{
139 sqlite3Config.pPage = 0;
140 }
drhfec00ea2008-06-14 16:56:21 +0000141 return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
142}
143
144/*
145** Deinitialize the memory allocation subsystem.
146*/
147void sqlite3MallocEnd(void){
drh9ac3fe92008-06-18 18:12:04 +0000148 sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
149 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000150}
151
152/*
153** Return the amount of memory currently checked out.
154*/
155sqlite3_int64 sqlite3_memory_used(void){
156 sqlite3_int64 n;
drh9ac3fe92008-06-18 18:12:04 +0000157 sqlite3_initialize();
drhfec00ea2008-06-14 16:56:21 +0000158 sqlite3_mutex_enter(mem0.mutex);
159 n = mem0.nowUsed;
160 sqlite3_mutex_leave(mem0.mutex);
161 return n;
162}
163
164/*
165** Return the maximum amount of memory that has ever been
166** checked out since either the beginning of this process
167** or since the most recent reset.
168*/
169sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
170 sqlite3_int64 n;
drh9ac3fe92008-06-18 18:12:04 +0000171 sqlite3_initialize();
drhfec00ea2008-06-14 16:56:21 +0000172 sqlite3_mutex_enter(mem0.mutex);
173 n = mem0.mxUsed;
174 if( resetFlag ){
175 mem0.mxUsed = mem0.nowUsed;
176 }
177 sqlite3_mutex_leave(mem0.mutex);
178 return n;
179}
180
181/*
182** Change the alarm callback
183*/
184int sqlite3_memory_alarm(
185 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
186 void *pArg,
187 sqlite3_int64 iThreshold
188){
189 sqlite3_mutex_enter(mem0.mutex);
190 mem0.alarmCallback = xCallback;
191 mem0.alarmArg = pArg;
192 mem0.alarmThreshold = iThreshold;
193 sqlite3_mutex_leave(mem0.mutex);
194 return SQLITE_OK;
195}
196
197/*
198** Trigger the alarm
199*/
200static void sqlite3MallocAlarm(int nByte){
201 void (*xCallback)(void*,sqlite3_int64,int);
202 sqlite3_int64 nowUsed;
203 void *pArg;
204 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
205 mem0.alarmBusy = 1;
206 xCallback = mem0.alarmCallback;
207 nowUsed = mem0.nowUsed;
208 pArg = mem0.alarmArg;
209 sqlite3_mutex_leave(mem0.mutex);
210 xCallback(pArg, nowUsed, nByte);
211 sqlite3_mutex_enter(mem0.mutex);
212 mem0.alarmBusy = 0;
213}
214
215
216/*
217** Allocate memory. This routine is like sqlite3_malloc() except that it
218** assumes the memory subsystem has already been initialized.
219*/
220void *sqlite3Malloc(int n){
221 void *p;
222 int nFull;
223 if( n<=0 ){
224 return 0;
225 }else if( sqlite3Config.bMemstat ){
226 nFull = sqlite3Config.m.xRoundup(n);
227 sqlite3_mutex_enter(mem0.mutex);
228 if( n>mem0.mxReq ) mem0.mxReq = n;
229 if( mem0.alarmCallback!=0 && mem0.nowUsed+nFull>=mem0.alarmThreshold ){
230 sqlite3MallocAlarm(nFull);
231 }
232 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
233 p = 0;
234 }else{
235 p = sqlite3Config.m.xMalloc(nFull);
236 if( p==0 ){
237 sqlite3MallocAlarm(nFull);
238 p = malloc(nFull);
239 }
240 }
241 if( p ){
242 mem0.nowUsed += nFull;
243 if( mem0.nowUsed>mem0.mxUsed ){
244 mem0.mxUsed = mem0.nowUsed;
245 }
246 }
247 sqlite3_mutex_leave(mem0.mutex);
248 }else{
249 p = sqlite3Config.m.xMalloc(n);
250 }
251 return p;
252}
253
254/*
255** This version of the memory allocation is for use by the application.
256** First make sure the memory subsystem is initialized, then do the
257** allocation.
258*/
259void *sqlite3_malloc(int n){
260#ifndef SQLITE_OMIT_AUTOINIT
261 if( sqlite3_initialize() ) return 0;
262#endif
263 return sqlite3Malloc(n);
264}
265
266/*
drhe5ae5732008-06-15 02:51:47 +0000267** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000268** xScratchMalloc(). We verify this constraint in the single-threaded
269** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000270** is outstanding clearing it when the allocation is freed.
271*/
272#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000273static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000274#endif
275
276
277/*
278** Allocate memory that is to be used and released right away.
279** This routine is similar to alloca() in that it is not intended
280** for situations where the memory might be held long-term. This
281** routine is intended to get memory to old large transient data
282** structures that would not normally fit on the stack of an
283** embedded processor.
284*/
drhfacf0302008-06-17 15:12:00 +0000285void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000286 void *p;
287 assert( n>0 );
288 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
289 return 0;
290 }
drh9ac3fe92008-06-18 18:12:04 +0000291
drhe5ae5732008-06-15 02:51:47 +0000292#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000293 /* Verify that no more than one scratch allocation per thread
294 ** is outstanding at one time. (This is only checked in the
295 ** single-threaded case since checking in the multi-threaded case
296 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000297 assert( scratchAllocOut==0 );
298 scratchAllocOut = 1;
drhe5ae5732008-06-15 02:51:47 +0000299#endif
drh9ac3fe92008-06-18 18:12:04 +0000300
301 sqlite3_mutex_enter(mem0.mutex);
302 if( n>mem0.mxScratchReq ) mem0.mxScratchReq = n;
303 if( mem0.nScratchFree==0 || sqlite3Config.szScratch>=n ){
drhfacf0302008-06-17 15:12:00 +0000304 p = sqlite3Config.m.xMalloc(n);
drhe5ae5732008-06-15 02:51:47 +0000305 }else{
drh9ac3fe92008-06-18 18:12:04 +0000306 int i;
307 i = mem0.aScratchFree[--mem0.nScratchFree];
308 i *= sqlite3Config.szScratch;
309 p = (void*)&((char*)sqlite3Config.pScratch)[i];
drhe5ae5732008-06-15 02:51:47 +0000310 }
drh9ac3fe92008-06-18 18:12:04 +0000311 sqlite3_mutex_leave(mem0.mutex);
drhe5ae5732008-06-15 02:51:47 +0000312 return p;
313}
drhfacf0302008-06-17 15:12:00 +0000314void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000315 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000316
drhe5ae5732008-06-15 02:51:47 +0000317#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000318 /* Verify that no more than one scratch allocation per thread
319 ** is outstanding at one time. (This is only checked in the
320 ** single-threaded case since checking in the multi-threaded case
321 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000322 assert( scratchAllocOut==1 );
323 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000324#endif
drh9ac3fe92008-06-18 18:12:04 +0000325
326 if( sqlite3Config.pScratch==0
327 || p<sqlite3Config.pScratch
328 || p>=(void*)mem0.aScratchFree ){
329 sqlite3Config.m.xFree(p);
330 }else{
331 int i;
332 sqlite3_mutex_enter(mem0.mutex);
333 assert( mem0.nScratchFree<sqlite3Config.nScratch );
334 i = p - sqlite3Config.pScratch;
335 i /= sqlite3Config.szScratch;
336 assert( i>=0 && i<sqlite3Config.nScratch );
337 mem0.aScratchFree[mem0.nScratchFree++] = i;
338 sqlite3_mutex_leave(mem0.mutex);
339 }
drhe5ae5732008-06-15 02:51:47 +0000340 }
341}
342
343/*
drhfacf0302008-06-17 15:12:00 +0000344** Place holders for the page-cache memory allocator.
345*/
346void *sqlite3PageMalloc(int iSize){
347 return sqlite3Malloc(iSize);
348}
349void sqlite3PageFree(void *pOld){
350 sqlite3_free(pOld);
351}
352
353/*
drhfec00ea2008-06-14 16:56:21 +0000354** Return the size of a memory allocation previously obtained from
355** sqlite3Malloc() or sqlite3_malloc().
356*/
357int sqlite3MallocSize(void *p){
358 return sqlite3Config.m.xSize(p);
359}
360
361/*
362** Free memory previously obtained from sqlite3Malloc().
363*/
364void sqlite3_free(void *p){
365 if( p==0 ) return;
366 if( sqlite3Config.bMemstat ){
367 sqlite3_mutex_enter(mem0.mutex);
368 mem0.nowUsed -= sqlite3MallocSize(p);
369 sqlite3Config.m.xFree(p);
370 sqlite3_mutex_leave(mem0.mutex);
371 }else{
372 sqlite3Config.m.xFree(p);
373 }
374}
375
376/*
377** Change the size of an existing memory allocation
378*/
379void *sqlite3Realloc(void *pOld, int nBytes){
380 int nOld, nNew;
381 void *pNew;
382 if( pOld==0 ){
383 return sqlite3Malloc(nBytes);
384 }
385 if( nBytes<=0 ){
386 sqlite3_free(pOld);
387 return 0;
388 }
389 nOld = sqlite3MallocSize(pOld);
390 if( sqlite3Config.bMemstat ){
391 sqlite3_mutex_enter(mem0.mutex);
392 if( nBytes>mem0.mxReq ) mem0.mxReq = nBytes;
393 nNew = sqlite3Config.m.xRoundup(nBytes);
394 if( nOld==nNew ){
395 pNew = pOld;
396 }else{
397 if( mem0.nowUsed+nNew-nOld>=mem0.alarmThreshold ){
398 sqlite3MallocAlarm(nNew-nOld);
399 }
400 if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){
401 pNew = 0;
402 }else{
403 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
404 if( pNew==0 ){
405 sqlite3MallocAlarm(nBytes);
406 pNew = sqlite3Config.m.xRealloc(pOld, nNew);
407 }
408 }
409 if( pNew ){
410 mem0.nowUsed += nNew-nOld;
411 if( mem0.nowUsed>mem0.mxUsed ){
412 mem0.mxUsed = mem0.nowUsed;
413 }
414 }
415 }
416 sqlite3_mutex_leave(mem0.mutex);
417 }else{
418 pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
419 }
420 return pNew;
421}
422
423/*
424** The public interface to sqlite3Realloc. Make sure that the memory
425** subsystem is initialized prior to invoking sqliteRealloc.
426*/
427void *sqlite3_realloc(void *pOld, int n){
428#ifndef SQLITE_OMIT_AUTOINIT
429 if( sqlite3_initialize() ) return 0;
430#endif
431 return sqlite3Realloc(pOld, n);
432}
433
drha3152892007-05-05 11:48:52 +0000434
435/*
drh17435752007-08-16 04:30:38 +0000436** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000437*/
drhfec00ea2008-06-14 16:56:21 +0000438void *sqlite3MallocZero(int n){
439 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000440 if( p ){
441 memset(p, 0, n);
442 }
443 return p;
444}
drh17435752007-08-16 04:30:38 +0000445
446/*
447** Allocate and zero memory. If the allocation fails, make
448** the mallocFailed flag in the connection pointer.
449*/
drhfec00ea2008-06-14 16:56:21 +0000450void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000451 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000452 if( p ){
453 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000454 }
455 return p;
456}
457
458/*
459** Allocate and zero memory. If the allocation fails, make
460** the mallocFailed flag in the connection pointer.
461*/
drhfec00ea2008-06-14 16:56:21 +0000462void *sqlite3DbMallocRaw(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000463 void *p = 0;
464 if( !db || db->mallocFailed==0 ){
drhfec00ea2008-06-14 16:56:21 +0000465 p = sqlite3Malloc(n);
danielk1977a1644fd2007-08-29 12:31:25 +0000466 if( !p && db ){
467 db->mallocFailed = 1;
468 }
drh17435752007-08-16 04:30:38 +0000469 }
470 return p;
471}
472
danielk197726783a52007-08-29 14:06:22 +0000473/*
474** Resize the block of memory pointed to by p to n bytes. If the
475** resize fails, set the mallocFailed flag inthe connection object.
476*/
danielk1977a1644fd2007-08-29 12:31:25 +0000477void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
478 void *pNew = 0;
479 if( db->mallocFailed==0 ){
480 pNew = sqlite3_realloc(p, n);
481 if( !pNew ){
482 db->mallocFailed = 1;
483 }
484 }
485 return pNew;
486}
487
drh17435752007-08-16 04:30:38 +0000488/*
489** Attempt to reallocate p. If the reallocation fails, then free p
490** and set the mallocFailed flag in the database connection.
491*/
492void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000493 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000494 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000495 if( !pNew ){
danielk19771e536952007-08-16 10:09:01 +0000496 sqlite3_free(p);
drha3152892007-05-05 11:48:52 +0000497 }
498 return pNew;
499}
500
drha3152892007-05-05 11:48:52 +0000501/*
502** Make a copy of a string in memory obtained from sqliteMalloc(). These
503** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
504** is because when memory debugging is turned on, these two functions are
505** called via macros that record the current file and line number in the
506** ThreadData structure.
507*/
508char *sqlite3StrDup(const char *z){
509 char *zNew;
510 int n;
511 if( z==0 ) return 0;
512 n = strlen(z)+1;
drhe5ae5732008-06-15 02:51:47 +0000513 zNew = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000514 if( zNew ) memcpy(zNew, z, n);
515 return zNew;
516}
517char *sqlite3StrNDup(const char *z, int n){
518 char *zNew;
519 if( z==0 ) return 0;
drhe5ae5732008-06-15 02:51:47 +0000520 zNew = sqlite3Malloc(n+1);
drha3152892007-05-05 11:48:52 +0000521 if( zNew ){
522 memcpy(zNew, z, n);
523 zNew[n] = 0;
524 }
525 return zNew;
526}
527
danielk19771e536952007-08-16 10:09:01 +0000528char *sqlite3DbStrDup(sqlite3 *db, const char *z){
529 char *zNew = sqlite3StrDup(z);
530 if( z && !zNew ){
531 db->mallocFailed = 1;
532 }
533 return zNew;
534}
535char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
536 char *zNew = sqlite3StrNDup(z, n);
537 if( z && !zNew ){
538 db->mallocFailed = 1;
539 }
540 return zNew;
541}
542
drha3152892007-05-05 11:48:52 +0000543/*
544** Create a string from the 2nd and subsequent arguments (up to the
545** first NULL argument), store the string in memory obtained from
546** sqliteMalloc() and make the pointer indicated by the 1st argument
547** point to that string. The 1st argument must either be NULL or
548** point to memory obtained from sqliteMalloc().
549*/
550void sqlite3SetString(char **pz, ...){
551 va_list ap;
552 int nByte;
553 const char *z;
554 char *zResult;
555
556 assert( pz!=0 );
557 nByte = 1;
558 va_start(ap, pz);
559 while( (z = va_arg(ap, const char*))!=0 ){
560 nByte += strlen(z);
561 }
562 va_end(ap);
danielk19771e536952007-08-16 10:09:01 +0000563 sqlite3_free(*pz);
drhe5ae5732008-06-15 02:51:47 +0000564 *pz = zResult = sqlite3Malloc(nByte);
drha3152892007-05-05 11:48:52 +0000565 if( zResult==0 ){
566 return;
567 }
568 *zResult = 0;
569 va_start(ap, pz);
570 while( (z = va_arg(ap, const char*))!=0 ){
571 int n = strlen(z);
572 memcpy(zResult, z, n);
573 zResult += n;
574 }
575 zResult[0] = 0;
576 va_end(ap);
577}
578
579
580/*
581** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000582** returning control to the user) that has called sqlite3_malloc or
583** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000584**
585** The returned value is normally a copy of the second argument to this
586** function. However, if a malloc() failure has occured since the previous
587** invocation SQLITE_NOMEM is returned instead.
588**
589** If the first argument, db, is not NULL and a malloc() error has occured,
590** then the connection error-code (the value returned by sqlite3_errcode())
591** is set to SQLITE_NOMEM.
592*/
drha3152892007-05-05 11:48:52 +0000593int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000594 /* If the db handle is not NULL, then we must hold the connection handle
595 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
596 ** is unsafe, as is the call to sqlite3Error().
597 */
598 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000599 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000600 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000601 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000602 rc = SQLITE_NOMEM;
603 }
604 return rc & (db ? db->errMask : 0xff);
605}