blob: e8b244b8aa64453a71430187e04a54d835dfef87 [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.
drha3152892007-05-05 11:48:52 +000014*/
15#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000016#include <stdarg.h>
drha3152892007-05-05 11:48:52 +000017
18/*
drh25f81882010-07-24 18:25:20 +000019** There are two general-purpose memory allocators:
20**
21** Simple:
22**
23** sqlite3_malloc
24** sqlite3_free
25** sqlite3_realloc
26** sqlite3Malloc
27** sqlite3MallocSize
28** sqlite3_mprintf
29**
30** Enhanced:
31**
32** sqlite3DbMallocRaw
33** sqlite3DbMallocZero
34** sqlite3DbFree
35** sqlite3DbRealloc
36** sqlite3MPrintf
37** sqlite3DbMalloc
38**
39** All external allocations use the simple memory allocator.
40** The enhanced allocator is used internally only, and is not
41** available to extensions or applications.
42**
43** The enhanced allocator is a wrapper around the simple allocator that
44** adds the following capabilities:
45**
46** (1) Access to lookaside memory associated with a database connection.
47**
48** (2) The ability to link allocations into a hierarchy with automatic
49** deallocation of all elements of the subhierarchy whenever any
50** element within the hierarchy is deallocated.
51**
52** The two allocators are incompatible in the sense that allocations that
53** originate from the simple allocator must be deallocated using the simple
54** deallocator and allocations that originate from the enhanced allocator must
55** be deallocated using the enhanced deallocator. You cannot check-out
56** memory from one allocator then return it to the other.
57*/
58
59/*
60** The automatic hierarchical deallocation feature of the enhanced allocator
61** is implemented by adding an instance of the following structure to the
62** header of each enhanced allocation.
63**
64** In order to preserve alignment, this structure must be a multiple of
65** 8 bytes in size.
66*/
67typedef struct EMemHdr EMemHdr;
68struct EMemHdr {
69 EMemHdr *pEChild; /* List of children of this node */
70 EMemHdr *pESibling; /* Other nodes that are children of the same parent */
71#ifdef SQLITE_MEMDEBUG
72 u32 iEMemMagic; /* Magic number for sanity checking */
73 u32 isAChild; /* True if this allocate is a child of another */
74#endif
75};
76
77/*
78** Macros for querying and setting debugging fields of the EMemHdr object.
79*/
80#ifdef SQLITE_MEMDEBUG
81# define isValidEMem(E) ((E)->iEMemMagic==0xc0a43fad)
82# define setValidEMem(E) (E)->iEMemMagic = 0xc0a43fad
83# define clearValidEMem(E) (E)->iEMemMagic = 0x12345678
84# define isChildEMem(E) ((E)->isAChild!=0)
85# define setChildEMem(E) (E)->isAChild = 1
86# define clearChildEMem(E) (E)->isAChild = 0
87#else
88# define isValidEMem(E)
89# define setValidEMem(E)
90# define clearValidEMem(E)
91# define isChildEMem(E)
92# define setChildEMem(E)
93# define clearChildEMem(E)
94#endif
95
96/*
drhb21c8cd2007-08-21 19:33:56 +000097** This routine runs when the memory allocator sees that the
98** total memory allocation is about to exceed the soft heap
99** limit.
100*/
101static void softHeapLimitEnforcer(
102 void *NotUsed,
danielk197762c14b32008-11-19 09:05:26 +0000103 sqlite3_int64 NotUsed2,
drh153c62c2007-08-24 03:51:33 +0000104 int allocSize
drhb21c8cd2007-08-21 19:33:56 +0000105){
danielk197762c14b32008-11-19 09:05:26 +0000106 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhb21c8cd2007-08-21 19:33:56 +0000107 sqlite3_release_memory(allocSize);
108}
109
110/*
danielk197784680242008-06-23 11:11:35 +0000111** Set the soft heap-size limit for the library. Passing a zero or
112** negative value indicates no limit.
drh25f81882010-07-24 18:25:20 +0000113**
114** If the total amount of memory allocated (by all threads) exceeds
115** the soft heap limit, then sqlite3_release_memory() is invoked to
116** try to free up some memory before proceeding.
drha3152892007-05-05 11:48:52 +0000117*/
118void sqlite3_soft_heap_limit(int n){
drhb21c8cd2007-08-21 19:33:56 +0000119 sqlite3_uint64 iLimit;
120 int overage;
121 if( n<0 ){
122 iLimit = 0;
123 }else{
124 iLimit = n;
drha3152892007-05-05 11:48:52 +0000125 }
drh9ac06502009-08-17 13:42:29 +0000126#ifndef SQLITE_OMIT_AUTOINIT
drh9ac3fe92008-06-18 18:12:04 +0000127 sqlite3_initialize();
drh9ac06502009-08-17 13:42:29 +0000128#endif
drhb21c8cd2007-08-21 19:33:56 +0000129 if( iLimit>0 ){
shane4a27a282008-09-04 04:32:49 +0000130 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
drhb21c8cd2007-08-21 19:33:56 +0000131 }else{
shane4a27a282008-09-04 04:32:49 +0000132 sqlite3MemoryAlarm(0, 0, 0);
drhb21c8cd2007-08-21 19:33:56 +0000133 }
drh1bd10f82008-12-10 21:19:56 +0000134 overage = (int)(sqlite3_memory_used() - (i64)n);
drhb21c8cd2007-08-21 19:33:56 +0000135 if( overage>0 ){
136 sqlite3_release_memory(overage);
137 }
drha3152892007-05-05 11:48:52 +0000138}
139
140/*
danielk197784680242008-06-23 11:11:35 +0000141** Attempt to release up to n bytes of non-essential memory currently
142** held by SQLite. An example of non-essential memory is memory used to
143** cache database pages that are not currently in use.
drha3152892007-05-05 11:48:52 +0000144*/
145int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +0000146#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197767e3da72008-08-21 12:19:44 +0000147 int nRet = 0;
danielk197767e3da72008-08-21 12:19:44 +0000148 nRet += sqlite3PcacheReleaseMemory(n-nRet);
danielk1977dfb316d2008-03-26 18:34:43 +0000149 return nRet;
danielk19771e536952007-08-16 10:09:01 +0000150#else
danielk197762c14b32008-11-19 09:05:26 +0000151 UNUSED_PARAMETER(n);
danielk19771e536952007-08-16 10:09:01 +0000152 return SQLITE_OK;
153#endif
drha3152892007-05-05 11:48:52 +0000154}
drha3152892007-05-05 11:48:52 +0000155
drhfec00ea2008-06-14 16:56:21 +0000156/*
157** State information local to the memory allocation subsystem.
158*/
danielk19775c8f8582008-09-02 10:22:00 +0000159static SQLITE_WSD struct Mem0Global {
danielk197723bf0f42008-09-02 17:52:51 +0000160 /* Number of free pages for scratch and page-cache memory */
161 u32 nScratchFree;
162 u32 nPageFree;
163
drhfec00ea2008-06-14 16:56:21 +0000164 sqlite3_mutex *mutex; /* Mutex to serialize access */
165
166 /*
167 ** The alarm callback and its arguments. The mem0.mutex lock will
168 ** be held while the callback is running. Recursive calls into
169 ** the memory subsystem are allowed, but no new callbacks will be
drhe64ca7b2009-07-16 18:21:17 +0000170 ** issued.
drhfec00ea2008-06-14 16:56:21 +0000171 */
172 sqlite3_int64 alarmThreshold;
173 void (*alarmCallback)(void*, sqlite3_int64,int);
174 void *alarmArg;
drhfec00ea2008-06-14 16:56:21 +0000175
176 /*
danielk1977075c23a2008-09-01 18:34:20 +0000177 ** Pointers to the end of sqlite3GlobalConfig.pScratch and
178 ** sqlite3GlobalConfig.pPage to a block of memory that records
drh9ac3fe92008-06-18 18:12:04 +0000179 ** which pages are available.
180 */
181 u32 *aScratchFree;
182 u32 *aPageFree;
drhe64ca7b2009-07-16 18:21:17 +0000183} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000184
185#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000186
187/*
188** Initialize the memory allocation subsystem.
189*/
190int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000191 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000192 sqlite3MemSetDefault();
193 }
194 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000195 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000196 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000197 }
danielk1977075c23a2008-09-01 18:34:20 +0000198 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
199 && sqlite3GlobalConfig.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000200 int i;
danielk1977bc739712009-03-23 04:33:32 +0000201 sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4);
danielk1977075c23a2008-09-01 18:34:20 +0000202 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
203 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
204 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
205 mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
drh9ac3fe92008-06-18 18:12:04 +0000206 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000207 sqlite3GlobalConfig.pScratch = 0;
208 sqlite3GlobalConfig.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000209 }
danielk1977075c23a2008-09-01 18:34:20 +0000210 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
211 && sqlite3GlobalConfig.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000212 int i;
drh0a60a382008-07-31 17:16:05 +0000213 int overhead;
danielk1977bc739712009-03-23 04:33:32 +0000214 int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage);
danielk1977075c23a2008-09-01 18:34:20 +0000215 int n = sqlite3GlobalConfig.nPage;
drh0a60a382008-07-31 17:16:05 +0000216 overhead = (4*n + sz - 1)/sz;
danielk1977075c23a2008-09-01 18:34:20 +0000217 sqlite3GlobalConfig.nPage -= overhead;
218 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
219 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
220 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
221 mem0.nPageFree = sqlite3GlobalConfig.nPage;
drh9ac3fe92008-06-18 18:12:04 +0000222 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000223 sqlite3GlobalConfig.pPage = 0;
224 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000225 }
danielk1977075c23a2008-09-01 18:34:20 +0000226 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000227}
228
229/*
230** Deinitialize the memory allocation subsystem.
231*/
232void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000233 if( sqlite3GlobalConfig.m.xShutdown ){
234 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
235 }
drh9ac3fe92008-06-18 18:12:04 +0000236 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000237}
238
239/*
240** Return the amount of memory currently checked out.
241*/
242sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000243 int n, mx;
drhc376a192008-07-14 12:30:54 +0000244 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000245 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000246 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
247 return res;
drhfec00ea2008-06-14 16:56:21 +0000248}
249
250/*
251** Return the maximum amount of memory that has ever been
252** checked out since either the beginning of this process
253** or since the most recent reset.
254*/
255sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000256 int n, mx;
drhc376a192008-07-14 12:30:54 +0000257 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000258 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000259 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000260 return res;
drhfec00ea2008-06-14 16:56:21 +0000261}
262
263/*
264** Change the alarm callback
265*/
shane4a27a282008-09-04 04:32:49 +0000266int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000267 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
268 void *pArg,
269 sqlite3_int64 iThreshold
270){
271 sqlite3_mutex_enter(mem0.mutex);
272 mem0.alarmCallback = xCallback;
273 mem0.alarmArg = pArg;
274 mem0.alarmThreshold = iThreshold;
275 sqlite3_mutex_leave(mem0.mutex);
276 return SQLITE_OK;
277}
278
shaneeec556d2008-10-12 00:27:53 +0000279#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000280/*
shane4a27a282008-09-04 04:32:49 +0000281** Deprecated external interface. Internal/core SQLite code
282** should call sqlite3MemoryAlarm.
283*/
284int sqlite3_memory_alarm(
285 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
286 void *pArg,
287 sqlite3_int64 iThreshold
288){
289 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
290}
shaneeec556d2008-10-12 00:27:53 +0000291#endif
shane4a27a282008-09-04 04:32:49 +0000292
293/*
drhfec00ea2008-06-14 16:56:21 +0000294** Trigger the alarm
295*/
296static void sqlite3MallocAlarm(int nByte){
297 void (*xCallback)(void*,sqlite3_int64,int);
298 sqlite3_int64 nowUsed;
299 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000300 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000301 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000302 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000303 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000304 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000305 sqlite3_mutex_leave(mem0.mutex);
306 xCallback(pArg, nowUsed, nByte);
307 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000308 mem0.alarmCallback = xCallback;
309 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000310}
311
drhf7141992008-06-19 00:16:08 +0000312/*
313** Do a memory allocation with statistics and alarms. Assume the
314** lock is already held.
315*/
316static int mallocWithAlarm(int n, void **pp){
317 int nFull;
318 void *p;
319 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000320 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000321 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
322 if( mem0.alarmCallback!=0 ){
323 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
324 if( nUsed+nFull >= mem0.alarmThreshold ){
325 sqlite3MallocAlarm(nFull);
326 }
327 }
danielk1977075c23a2008-09-01 18:34:20 +0000328 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000329 if( p==0 && mem0.alarmCallback ){
330 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000331 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000332 }
drhc702c7c2008-07-18 18:56:16 +0000333 if( p ){
334 nFull = sqlite3MallocSize(p);
335 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
336 }
drhf7141992008-06-19 00:16:08 +0000337 *pp = p;
338 return nFull;
339}
drhfec00ea2008-06-14 16:56:21 +0000340
341/*
342** Allocate memory. This routine is like sqlite3_malloc() except that it
343** assumes the memory subsystem has already been initialized.
344*/
345void *sqlite3Malloc(int n){
346 void *p;
drhe08ed7e2009-06-26 18:35:16 +0000347 if( n<=0 || n>=0x7fffff00 ){
348 /* A memory allocation of a number of bytes which is near the maximum
349 ** signed integer value might cause an integer overflow inside of the
350 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
351 ** 255 bytes of overhead. SQLite itself will never use anything near
352 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000353 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000354 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000355 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000356 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000357 sqlite3_mutex_leave(mem0.mutex);
358 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000359 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000360 }
361 return p;
362}
363
364/*
365** This version of the memory allocation is for use by the application.
366** First make sure the memory subsystem is initialized, then do the
367** allocation.
368*/
369void *sqlite3_malloc(int n){
370#ifndef SQLITE_OMIT_AUTOINIT
371 if( sqlite3_initialize() ) return 0;
372#endif
373 return sqlite3Malloc(n);
374}
375
376/*
drhe5ae5732008-06-15 02:51:47 +0000377** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000378** xScratchMalloc(). We verify this constraint in the single-threaded
379** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000380** is outstanding clearing it when the allocation is freed.
381*/
382#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000383static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000384#endif
385
386
387/*
388** Allocate memory that is to be used and released right away.
389** This routine is similar to alloca() in that it is not intended
390** for situations where the memory might be held long-term. This
391** routine is intended to get memory to old large transient data
392** structures that would not normally fit on the stack of an
393** embedded processor.
394*/
drhfacf0302008-06-17 15:12:00 +0000395void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000396 void *p;
397 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000398
drhe5ae5732008-06-15 02:51:47 +0000399#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh37f99182010-06-26 20:25:30 +0000400 /* Verify that no more than two scratch allocation per thread
drh9ac3fe92008-06-18 18:12:04 +0000401 ** is outstanding at one time. (This is only checked in the
402 ** single-threaded case since checking in the multi-threaded case
403 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000404 assert( scratchAllocOut<=1 );
drhe5ae5732008-06-15 02:51:47 +0000405#endif
drh9ac3fe92008-06-18 18:12:04 +0000406
danielk1977075c23a2008-09-01 18:34:20 +0000407 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000408 goto scratch_overflow;
409 }else{
410 sqlite3_mutex_enter(mem0.mutex);
411 if( mem0.nScratchFree==0 ){
412 sqlite3_mutex_leave(mem0.mutex);
413 goto scratch_overflow;
414 }else{
415 int i;
416 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000417 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000418 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000419 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000420 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000421 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
shane15301592008-12-16 17:20:38 +0000422 assert( (((u8*)p - (u8*)0) & 7)==0 );
drhf7141992008-06-19 00:16:08 +0000423 }
drhe5ae5732008-06-15 02:51:47 +0000424 }
drhf7141992008-06-19 00:16:08 +0000425#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
426 scratchAllocOut = p!=0;
427#endif
428
drhe5ae5732008-06-15 02:51:47 +0000429 return p;
drhf7141992008-06-19 00:16:08 +0000430
431scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000432 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000433 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000434 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000435 n = mallocWithAlarm(n, &p);
436 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
437 sqlite3_mutex_leave(mem0.mutex);
438 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000439 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000440 }
drh107b56e2010-03-12 16:32:53 +0000441 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
drhf7141992008-06-19 00:16:08 +0000442#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
443 scratchAllocOut = p!=0;
444#endif
445 return p;
drhe5ae5732008-06-15 02:51:47 +0000446}
drhfacf0302008-06-17 15:12:00 +0000447void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000448 if( p ){
danielk1977075c23a2008-09-01 18:34:20 +0000449 if( sqlite3GlobalConfig.pScratch==0
450 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000451 || p>=(void*)mem0.aScratchFree ){
drh107b56e2010-03-12 16:32:53 +0000452 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drhb9755982010-07-24 16:34:37 +0000453 assert( !sqlite3MemdebugHasType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000454 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000455 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000456 int iSize = sqlite3MallocSize(p);
457 sqlite3_mutex_enter(mem0.mutex);
458 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
459 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000460 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000461 sqlite3_mutex_leave(mem0.mutex);
462 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000463 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000464 }
drh9ac3fe92008-06-18 18:12:04 +0000465 }else{
466 int i;
drh1bd10f82008-12-10 21:19:56 +0000467 i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
danielk1977075c23a2008-09-01 18:34:20 +0000468 i /= sqlite3GlobalConfig.szScratch;
469 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000470 sqlite3_mutex_enter(mem0.mutex);
danielk197700e13612008-11-17 19:18:54 +0000471 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000472 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000473 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000474 sqlite3_mutex_leave(mem0.mutex);
drh37f99182010-06-26 20:25:30 +0000475
476#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
477 /* Verify that no more than two scratch allocation per thread
478 ** is outstanding at one time. (This is only checked in the
479 ** single-threaded case since checking in the multi-threaded case
480 ** would be much more complicated.) */
481 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
482 scratchAllocOut = 0;
483#endif
484
drh9ac3fe92008-06-18 18:12:04 +0000485 }
drhe5ae5732008-06-15 02:51:47 +0000486 }
487}
488
489/*
drh633e6d52008-07-28 19:34:53 +0000490** TRUE if p is a lookaside memory allocation from db
491*/
drh4150ebf2008-10-11 15:38:29 +0000492#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000493static int isLookaside(sqlite3 *db, void *p){
494 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
495}
drh4150ebf2008-10-11 15:38:29 +0000496#else
497#define isLookaside(A,B) 0
498#endif
drh633e6d52008-07-28 19:34:53 +0000499
500/*
drhfec00ea2008-06-14 16:56:21 +0000501** Return the size of a memory allocation previously obtained from
502** sqlite3Malloc() or sqlite3_malloc().
drh25f81882010-07-24 18:25:20 +0000503**
504** The size returned is the usable size and does not include any
505** bookkeeping overhead or sentinals at the end of the allocation.
drhfec00ea2008-06-14 16:56:21 +0000506*/
507int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000508 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drhb9755982010-07-24 16:34:37 +0000509 assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
danielk1977075c23a2008-09-01 18:34:20 +0000510 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000511}
drh25f81882010-07-24 18:25:20 +0000512int sqlite3DbMallocSize(sqlite3 *db, void *pObj){
513 EMemHdr *p = (EMemHdr*)pObj;
drh7047e252009-03-23 17:49:14 +0000514 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh25f81882010-07-24 18:25:20 +0000515 if( p ){
516 p--;
517 assert( isValidEMem(p) );
518 }
drhf18a61d2009-07-17 11:44:07 +0000519 if( isLookaside(db, p) ){
drh25f81882010-07-24 18:25:20 +0000520 return db->lookaside.sz - sizeof(EMemHdr);
drh633e6d52008-07-28 19:34:53 +0000521 }else{
drhb9755982010-07-24 16:34:37 +0000522 assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
drh107b56e2010-03-12 16:32:53 +0000523 assert( sqlite3MemdebugHasType(p,
524 db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) );
drh25f81882010-07-24 18:25:20 +0000525 return sqlite3GlobalConfig.m.xSize(p) - sizeof(EMemHdr);
drh633e6d52008-07-28 19:34:53 +0000526 }
527}
drhfec00ea2008-06-14 16:56:21 +0000528
529/*
530** Free memory previously obtained from sqlite3Malloc().
531*/
532void sqlite3_free(void *p){
533 if( p==0 ) return;
drhb9755982010-07-24 16:34:37 +0000534 assert( !sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
drh107b56e2010-03-12 16:32:53 +0000535 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000536 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000537 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000538 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000539 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000540 sqlite3_mutex_leave(mem0.mutex);
541 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000542 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000543 }
544}
545
546/*
drh633e6d52008-07-28 19:34:53 +0000547** Free memory that might be associated with a particular database
drh25f81882010-07-24 18:25:20 +0000548** connection. All child allocations are also freed.
drhe79ffb62010-07-24 19:08:13 +0000549**
550** pObj must be a top-level allocation in the heirarchy. It is not
551** allowed to delete a child allocation since that would leave a
552** dangling child pointer in the parent.
drh633e6d52008-07-28 19:34:53 +0000553*/
drh25f81882010-07-24 18:25:20 +0000554void sqlite3DbFree(sqlite3 *db, void *pObj){
555 EMemHdr *p = (EMemHdr*)pObj;
drh7047e252009-03-23 17:49:14 +0000556 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh25f81882010-07-24 18:25:20 +0000557 if( p ) p--;
drhe79ffb62010-07-24 19:08:13 +0000558 assert( p==0 || !isChildEMem(p) ); /* pObj is not child allocation */
drh25f81882010-07-24 18:25:20 +0000559 while( p ){
560 EMemHdr *pNext = p->pESibling;
drhe79ffb62010-07-24 19:08:13 +0000561 assert( isValidEMem(p) ); /* pObj and all siblings are valid */
562 if( p->pEChild ){
563 clearChildEMem(p->pEChild);
564 sqlite3DbFree(db, (void*)&p->pEChild[1]);
565 }
drh25f81882010-07-24 18:25:20 +0000566 if( isLookaside(db, p) ){
567 LookasideSlot *pBuf = (LookasideSlot*)p;
568 clearValidEMem(p);
569 pBuf->pNext = db->lookaside.pFree;
570 db->lookaside.pFree = pBuf;
571 db->lookaside.nOut--;
572 }else{
573 assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
574 assert( sqlite3MemdebugHasType(p,
575 db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) );
576 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
577 clearValidEMem(p);
578 sqlite3_free(p);
579 }
580 p = pNext;
drh633e6d52008-07-28 19:34:53 +0000581 }
582}
583
584/*
drh25f81882010-07-24 18:25:20 +0000585** Change the size of an existing memory allocation.
586**
587** This is the same as sqlite3_realloc() except that it assumes that
588** the memory subsystem has already been initialized.
drhfec00ea2008-06-14 16:56:21 +0000589*/
590void *sqlite3Realloc(void *pOld, int nBytes){
591 int nOld, nNew;
592 void *pNew;
593 if( pOld==0 ){
594 return sqlite3Malloc(nBytes);
595 }
drhb6063cf2009-06-27 00:48:33 +0000596 if( nBytes<=0 ){
drhfec00ea2008-06-14 16:56:21 +0000597 sqlite3_free(pOld);
598 return 0;
599 }
drhb6063cf2009-06-27 00:48:33 +0000600 if( nBytes>=0x7fffff00 ){
601 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
602 return 0;
603 }
drhfec00ea2008-06-14 16:56:21 +0000604 nOld = sqlite3MallocSize(pOld);
drh7c6791c2009-08-18 14:48:53 +0000605 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
606 if( nOld==nNew ){
607 pNew = pOld;
608 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000609 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000610 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh7c6791c2009-08-18 14:48:53 +0000611 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
612 mem0.alarmThreshold ){
613 sqlite3MallocAlarm(nNew-nOld);
614 }
drh107b56e2010-03-12 16:32:53 +0000615 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drhb9755982010-07-24 16:34:37 +0000616 assert( !sqlite3MemdebugHasType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000617 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
618 if( pNew==0 && mem0.alarmCallback ){
619 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000620 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000621 }
622 if( pNew ){
623 nNew = sqlite3MallocSize(pNew);
624 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000625 }
626 sqlite3_mutex_leave(mem0.mutex);
627 }else{
drh7c6791c2009-08-18 14:48:53 +0000628 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000629 }
630 return pNew;
631}
632
633/*
634** The public interface to sqlite3Realloc. Make sure that the memory
635** subsystem is initialized prior to invoking sqliteRealloc.
636*/
637void *sqlite3_realloc(void *pOld, int n){
638#ifndef SQLITE_OMIT_AUTOINIT
639 if( sqlite3_initialize() ) return 0;
640#endif
641 return sqlite3Realloc(pOld, n);
642}
643
drha3152892007-05-05 11:48:52 +0000644
645/*
drh17435752007-08-16 04:30:38 +0000646** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000647*/
drhfec00ea2008-06-14 16:56:21 +0000648void *sqlite3MallocZero(int n){
649 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000650 if( p ){
651 memset(p, 0, n);
652 }
653 return p;
654}
drh17435752007-08-16 04:30:38 +0000655
656/*
657** Allocate and zero memory. If the allocation fails, make
658** the mallocFailed flag in the connection pointer.
659*/
drhfec00ea2008-06-14 16:56:21 +0000660void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000661 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000662 if( p ){
663 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000664 }
665 return p;
666}
667
668/*
669** Allocate and zero memory. If the allocation fails, make
670** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000671**
672** If db!=0 and db->mallocFailed is true (indicating a prior malloc
673** failure on the same database connection) then always return 0.
674** Hence for a particular database connection, once malloc starts
675** failing, it fails consistently until mallocFailed is reset.
676** This is an important assumption. There are many places in the
677** code that do things like this:
678**
679** int *a = (int*)sqlite3DbMallocRaw(db, 100);
680** int *b = (int*)sqlite3DbMallocRaw(db, 200);
681** if( b ) a[10] = 9;
682**
683** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
684** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000685*/
drhfec00ea2008-06-14 16:56:21 +0000686void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh25f81882010-07-24 18:25:20 +0000687 EMemHdr *p;
drhd9da78a2009-03-24 15:08:09 +0000688 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh25f81882010-07-24 18:25:20 +0000689 n += sizeof(EMemHdr);
drh4150ebf2008-10-11 15:38:29 +0000690#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000691 if( db ){
692 LookasideSlot *pBuf;
693 if( db->mallocFailed ){
694 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000695 }
drh633e6d52008-07-28 19:34:53 +0000696 if( db->lookaside.bEnabled && n<=db->lookaside.sz
697 && (pBuf = db->lookaside.pFree)!=0 ){
698 db->lookaside.pFree = pBuf->pNext;
699 db->lookaside.nOut++;
700 if( db->lookaside.nOut>db->lookaside.mxOut ){
701 db->lookaside.mxOut = db->lookaside.nOut;
702 }
drh25f81882010-07-24 18:25:20 +0000703 p = (EMemHdr*)pBuf;
704 goto finish_emalloc_raw;
drh633e6d52008-07-28 19:34:53 +0000705 }
706 }
drhddecae72008-10-11 17:35:16 +0000707#else
708 if( db && db->mallocFailed ){
709 return 0;
710 }
drh4150ebf2008-10-11 15:38:29 +0000711#endif
drh633e6d52008-07-28 19:34:53 +0000712 p = sqlite3Malloc(n);
drh25f81882010-07-24 18:25:20 +0000713 if( !p ){
714 if( db ) db->mallocFailed = 1;
715 return 0;
drh17435752007-08-16 04:30:38 +0000716 }
drhb9755982010-07-24 16:34:37 +0000717 sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE |
718 ((db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP));
drh25f81882010-07-24 18:25:20 +0000719
720finish_emalloc_raw:
721 memset(p, 0, sizeof(EMemHdr));
722 setValidEMem(p);
723 return (void*)&p[1];
drh17435752007-08-16 04:30:38 +0000724}
725
danielk197726783a52007-08-29 14:06:22 +0000726/*
727** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000728** resize fails, set the mallocFailed flag in the connection object.
drh25f81882010-07-24 18:25:20 +0000729**
730** The pOld memory block must not be linked into an allocation hierarchy
731** as a child. It is OK for the allocation to be the root of a hierarchy
732** of allocations; the only restriction is that there must be no other
733** allocations above the pOld allocation in the hierarchy. To resize
734** an allocation that is a child within a hierarchy, first
735** unlink the allocation, resize it, then relink it.
danielk197726783a52007-08-29 14:06:22 +0000736*/
drh25f81882010-07-24 18:25:20 +0000737void *sqlite3DbRealloc(sqlite3 *db, void *pOld, int n){
738 EMemHdr *p = (EMemHdr*)pOld;
739 EMemHdr *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000740 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000741 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000742 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000743 if( p==0 ){
744 return sqlite3DbMallocRaw(db, n);
745 }
drh25f81882010-07-24 18:25:20 +0000746 p--;
747 assert( isValidEMem(p) ); /* pOld obtained from extended allocator */
748 assert( !isChildEMem(p) ); /* pOld must not be a child allocation */
drh633e6d52008-07-28 19:34:53 +0000749 if( isLookaside(db, p) ){
drh25f81882010-07-24 18:25:20 +0000750 if( n+sizeof(EMemHdr)<=db->lookaside.sz ){
751 return pOld;
drh633e6d52008-07-28 19:34:53 +0000752 }
753 pNew = sqlite3DbMallocRaw(db, n);
754 if( pNew ){
drh25f81882010-07-24 18:25:20 +0000755 memcpy(pNew-1, p, db->lookaside.sz);
756 setValidEMem(pNew-1);
757 sqlite3DbFree(db, pOld);
drh633e6d52008-07-28 19:34:53 +0000758 }
759 }else{
drhb9755982010-07-24 16:34:37 +0000760 assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
drh107b56e2010-03-12 16:32:53 +0000761 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) );
762 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh25f81882010-07-24 18:25:20 +0000763 pNew = sqlite3_realloc(p, n+sizeof(EMemHdr));
drh633e6d52008-07-28 19:34:53 +0000764 if( !pNew ){
drhb9755982010-07-24 16:34:37 +0000765 sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000766 db->mallocFailed = 1;
drh25f81882010-07-24 18:25:20 +0000767 }else{
768 sqlite3MemdebugSetType(pNew, MEMTYPE_RECURSIVE |
769 (db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP));
770 setValidEMem(pNew);
771 pNew++;
drh633e6d52008-07-28 19:34:53 +0000772 }
danielk1977a1644fd2007-08-29 12:31:25 +0000773 }
774 }
drh25f81882010-07-24 18:25:20 +0000775 return (void*)pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000776}
777
drh17435752007-08-16 04:30:38 +0000778/*
779** Attempt to reallocate p. If the reallocation fails, then free p
780** and set the mallocFailed flag in the database connection.
781*/
782void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000783 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000784 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000785 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000786 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000787 }
788 return pNew;
789}
790
drha3152892007-05-05 11:48:52 +0000791/*
792** Make a copy of a string in memory obtained from sqliteMalloc(). These
793** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
794** is because when memory debugging is turned on, these two functions are
795** called via macros that record the current file and line number in the
796** ThreadData structure.
797*/
drh633e6d52008-07-28 19:34:53 +0000798char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000799 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000800 size_t n;
801 if( z==0 ){
802 return 0;
803 }
drhdee0e402009-05-03 20:23:53 +0000804 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000805 assert( (n&0x7fffffff)==n );
806 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000807 if( zNew ){
808 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000809 }
810 return zNew;
811}
812char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000813 char *zNew;
814 if( z==0 ){
815 return 0;
816 }
817 assert( (n&0x7fffffff)==n );
818 zNew = sqlite3DbMallocRaw(db, n+1);
819 if( zNew ){
820 memcpy(zNew, z, n);
821 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000822 }
823 return zNew;
824}
825
drha3152892007-05-05 11:48:52 +0000826/*
drh25f81882010-07-24 18:25:20 +0000827** Link extended allocation nodes such that deallocating the parent
828** causes the child to be automatically deallocated.
829*/
830void sqlite3MemLink(void *pParentObj, void *pChildObj){
831 EMemHdr *pParent = (EMemHdr*)pParentObj;
832 EMemHdr *pChild = (EMemHdr*)pChildObj;
833 if( pParent && pChild ){
834 pParent--;
835 assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */
836 pChild--;
837 assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */
838 assert( !isChildEMem(pChild) ); /* pChildObj not a child of another obj */
839 pChild->pESibling = pParent->pEChild;
840 pParent->pEChild = pChild;
841 setChildEMem(pChild);
842 }
843}
844
845/*
846** pChildObj is a child object of pParentObj due to a prior call
847** to sqlite3MemLink(). This routine breaks that linkage, making
848** pChildObj an independent node that is not a child of any other node.
849*/
850void sqlite3MemUnlink(void *pParentObj, void *pChildObj){
851 EMemHdr *pParent = (EMemHdr*)pParentObj;
852 EMemHdr *pChild = (EMemHdr*)pChildObj;
853 EMemHdr **pp;
854
855 assert( pParentObj!=0 );
856 assert( pChildObj!=0 );
857 pParent--;
858 assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */
859 pChild--;
860 assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */
861 assert( isChildEMem(pChild) ); /* pChildObj a child of something */
862 for(pp=&pParent->pEChild; (*pp)!=pChild; pp = &(*pp)->pESibling){
863 assert( *pp ); /* pChildObj is a child of pParentObj */
864 assert( isValidEMem(*pp) ); /* All children of pParentObj are valid */
865 assert( isChildEMem(*pp) ); /* All children of pParentObj are children */
866 }
867 *pp = pChild->pESibling;
868 pChild->pESibling = 0;
869 clearChildEMem(pChild);
870}
871
872
873/*
drhf089aa42008-07-08 19:34:06 +0000874** Create a string from the zFromat argument and the va_list that follows.
875** Store the string in memory obtained from sqliteMalloc() and make *pz
876** point to that string.
drha3152892007-05-05 11:48:52 +0000877*/
drhf089aa42008-07-08 19:34:06 +0000878void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000879 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000880 char *z;
drha3152892007-05-05 11:48:52 +0000881
drhf089aa42008-07-08 19:34:06 +0000882 va_start(ap, zFormat);
883 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000884 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000885 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000886 *pz = z;
drha3152892007-05-05 11:48:52 +0000887}
888
889
890/*
891** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000892** returning control to the user) that has called sqlite3_malloc or
893** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000894**
895** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000896** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000897** invocation SQLITE_NOMEM is returned instead.
898**
shanebe217792009-03-05 04:20:31 +0000899** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000900** then the connection error-code (the value returned by sqlite3_errcode())
901** is set to SQLITE_NOMEM.
902*/
drha3152892007-05-05 11:48:52 +0000903int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000904 /* If the db handle is not NULL, then we must hold the connection handle
905 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
906 ** is unsafe, as is the call to sqlite3Error().
907 */
908 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000909 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000910 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000911 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000912 rc = SQLITE_NOMEM;
913 }
914 return rc & (db ? db->errMask : 0xff);
915}