blob: 8488cf9aa3e2fe377fb5ec3ee2c22782b95b0da1 [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)
drh1b0bfc62010-07-25 02:39:06 +000085# define notChildEMem(E) ((E)->isAChild==0)
drh25f81882010-07-24 18:25:20 +000086# define setChildEMem(E) (E)->isAChild = 1
87# define clearChildEMem(E) (E)->isAChild = 0
88#else
drh1b0bfc62010-07-25 02:39:06 +000089# define isValidEMem(E) 1
drh25f81882010-07-24 18:25:20 +000090# define setValidEMem(E)
91# define clearValidEMem(E)
drh1b0bfc62010-07-25 02:39:06 +000092# define isChildEMem(E) 1
93# define notChildEMem(E) 1
drh25f81882010-07-24 18:25:20 +000094# define setChildEMem(E)
95# define clearChildEMem(E)
96#endif
97
98/*
drhb21c8cd2007-08-21 19:33:56 +000099** This routine runs when the memory allocator sees that the
100** total memory allocation is about to exceed the soft heap
101** limit.
102*/
103static void softHeapLimitEnforcer(
104 void *NotUsed,
danielk197762c14b32008-11-19 09:05:26 +0000105 sqlite3_int64 NotUsed2,
drh153c62c2007-08-24 03:51:33 +0000106 int allocSize
drhb21c8cd2007-08-21 19:33:56 +0000107){
danielk197762c14b32008-11-19 09:05:26 +0000108 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhb21c8cd2007-08-21 19:33:56 +0000109 sqlite3_release_memory(allocSize);
110}
111
112/*
danielk197784680242008-06-23 11:11:35 +0000113** Set the soft heap-size limit for the library. Passing a zero or
114** negative value indicates no limit.
drh25f81882010-07-24 18:25:20 +0000115**
116** If the total amount of memory allocated (by all threads) exceeds
117** the soft heap limit, then sqlite3_release_memory() is invoked to
118** try to free up some memory before proceeding.
drha3152892007-05-05 11:48:52 +0000119*/
120void sqlite3_soft_heap_limit(int n){
drhb21c8cd2007-08-21 19:33:56 +0000121 sqlite3_uint64 iLimit;
122 int overage;
123 if( n<0 ){
124 iLimit = 0;
125 }else{
126 iLimit = n;
drha3152892007-05-05 11:48:52 +0000127 }
drh9ac06502009-08-17 13:42:29 +0000128#ifndef SQLITE_OMIT_AUTOINIT
drh9ac3fe92008-06-18 18:12:04 +0000129 sqlite3_initialize();
drh9ac06502009-08-17 13:42:29 +0000130#endif
drhb21c8cd2007-08-21 19:33:56 +0000131 if( iLimit>0 ){
shane4a27a282008-09-04 04:32:49 +0000132 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
drhb21c8cd2007-08-21 19:33:56 +0000133 }else{
shane4a27a282008-09-04 04:32:49 +0000134 sqlite3MemoryAlarm(0, 0, 0);
drhb21c8cd2007-08-21 19:33:56 +0000135 }
drh1bd10f82008-12-10 21:19:56 +0000136 overage = (int)(sqlite3_memory_used() - (i64)n);
drhb21c8cd2007-08-21 19:33:56 +0000137 if( overage>0 ){
138 sqlite3_release_memory(overage);
139 }
drha3152892007-05-05 11:48:52 +0000140}
141
142/*
danielk197784680242008-06-23 11:11:35 +0000143** Attempt to release up to n bytes of non-essential memory currently
144** held by SQLite. An example of non-essential memory is memory used to
145** cache database pages that are not currently in use.
drha3152892007-05-05 11:48:52 +0000146*/
147int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +0000148#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197767e3da72008-08-21 12:19:44 +0000149 int nRet = 0;
danielk197767e3da72008-08-21 12:19:44 +0000150 nRet += sqlite3PcacheReleaseMemory(n-nRet);
danielk1977dfb316d2008-03-26 18:34:43 +0000151 return nRet;
danielk19771e536952007-08-16 10:09:01 +0000152#else
danielk197762c14b32008-11-19 09:05:26 +0000153 UNUSED_PARAMETER(n);
danielk19771e536952007-08-16 10:09:01 +0000154 return SQLITE_OK;
155#endif
drha3152892007-05-05 11:48:52 +0000156}
drha3152892007-05-05 11:48:52 +0000157
drhfec00ea2008-06-14 16:56:21 +0000158/*
159** State information local to the memory allocation subsystem.
160*/
danielk19775c8f8582008-09-02 10:22:00 +0000161static SQLITE_WSD struct Mem0Global {
danielk197723bf0f42008-09-02 17:52:51 +0000162 /* Number of free pages for scratch and page-cache memory */
163 u32 nScratchFree;
164 u32 nPageFree;
165
drhfec00ea2008-06-14 16:56:21 +0000166 sqlite3_mutex *mutex; /* Mutex to serialize access */
167
168 /*
169 ** The alarm callback and its arguments. The mem0.mutex lock will
170 ** be held while the callback is running. Recursive calls into
171 ** the memory subsystem are allowed, but no new callbacks will be
drhe64ca7b2009-07-16 18:21:17 +0000172 ** issued.
drhfec00ea2008-06-14 16:56:21 +0000173 */
174 sqlite3_int64 alarmThreshold;
175 void (*alarmCallback)(void*, sqlite3_int64,int);
176 void *alarmArg;
drhfec00ea2008-06-14 16:56:21 +0000177
178 /*
danielk1977075c23a2008-09-01 18:34:20 +0000179 ** Pointers to the end of sqlite3GlobalConfig.pScratch and
180 ** sqlite3GlobalConfig.pPage to a block of memory that records
drh9ac3fe92008-06-18 18:12:04 +0000181 ** which pages are available.
182 */
183 u32 *aScratchFree;
184 u32 *aPageFree;
drhe64ca7b2009-07-16 18:21:17 +0000185} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000186
187#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000188
189/*
190** Initialize the memory allocation subsystem.
191*/
192int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000193 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000194 sqlite3MemSetDefault();
195 }
196 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000197 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000198 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000199 }
danielk1977075c23a2008-09-01 18:34:20 +0000200 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
201 && sqlite3GlobalConfig.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000202 int i;
danielk1977bc739712009-03-23 04:33:32 +0000203 sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4);
danielk1977075c23a2008-09-01 18:34:20 +0000204 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
205 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
206 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
207 mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
drh9ac3fe92008-06-18 18:12:04 +0000208 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000209 sqlite3GlobalConfig.pScratch = 0;
210 sqlite3GlobalConfig.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000211 }
danielk1977075c23a2008-09-01 18:34:20 +0000212 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
213 && sqlite3GlobalConfig.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000214 int i;
drh0a60a382008-07-31 17:16:05 +0000215 int overhead;
danielk1977bc739712009-03-23 04:33:32 +0000216 int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage);
danielk1977075c23a2008-09-01 18:34:20 +0000217 int n = sqlite3GlobalConfig.nPage;
drh0a60a382008-07-31 17:16:05 +0000218 overhead = (4*n + sz - 1)/sz;
danielk1977075c23a2008-09-01 18:34:20 +0000219 sqlite3GlobalConfig.nPage -= overhead;
220 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
221 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
222 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
223 mem0.nPageFree = sqlite3GlobalConfig.nPage;
drh9ac3fe92008-06-18 18:12:04 +0000224 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000225 sqlite3GlobalConfig.pPage = 0;
226 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000227 }
danielk1977075c23a2008-09-01 18:34:20 +0000228 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000229}
230
231/*
232** Deinitialize the memory allocation subsystem.
233*/
234void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000235 if( sqlite3GlobalConfig.m.xShutdown ){
236 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
237 }
drh9ac3fe92008-06-18 18:12:04 +0000238 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000239}
240
241/*
242** Return the amount of memory currently checked out.
243*/
244sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000245 int n, mx;
drhc376a192008-07-14 12:30:54 +0000246 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000247 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000248 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
249 return res;
drhfec00ea2008-06-14 16:56:21 +0000250}
251
252/*
253** Return the maximum amount of memory that has ever been
254** checked out since either the beginning of this process
255** or since the most recent reset.
256*/
257sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000258 int n, mx;
drhc376a192008-07-14 12:30:54 +0000259 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000260 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000261 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000262 return res;
drhfec00ea2008-06-14 16:56:21 +0000263}
264
265/*
266** Change the alarm callback
267*/
shane4a27a282008-09-04 04:32:49 +0000268int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000269 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
270 void *pArg,
271 sqlite3_int64 iThreshold
272){
273 sqlite3_mutex_enter(mem0.mutex);
274 mem0.alarmCallback = xCallback;
275 mem0.alarmArg = pArg;
276 mem0.alarmThreshold = iThreshold;
277 sqlite3_mutex_leave(mem0.mutex);
278 return SQLITE_OK;
279}
280
shaneeec556d2008-10-12 00:27:53 +0000281#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000282/*
shane4a27a282008-09-04 04:32:49 +0000283** Deprecated external interface. Internal/core SQLite code
284** should call sqlite3MemoryAlarm.
285*/
286int sqlite3_memory_alarm(
287 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
288 void *pArg,
289 sqlite3_int64 iThreshold
290){
291 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
292}
shaneeec556d2008-10-12 00:27:53 +0000293#endif
shane4a27a282008-09-04 04:32:49 +0000294
295/*
drhfec00ea2008-06-14 16:56:21 +0000296** Trigger the alarm
297*/
298static void sqlite3MallocAlarm(int nByte){
299 void (*xCallback)(void*,sqlite3_int64,int);
300 sqlite3_int64 nowUsed;
301 void *pArg;
drhe64ca7b2009-07-16 18:21:17 +0000302 if( mem0.alarmCallback==0 ) return;
drhfec00ea2008-06-14 16:56:21 +0000303 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000304 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000305 pArg = mem0.alarmArg;
drhe64ca7b2009-07-16 18:21:17 +0000306 mem0.alarmCallback = 0;
drhfec00ea2008-06-14 16:56:21 +0000307 sqlite3_mutex_leave(mem0.mutex);
308 xCallback(pArg, nowUsed, nByte);
309 sqlite3_mutex_enter(mem0.mutex);
drhe64ca7b2009-07-16 18:21:17 +0000310 mem0.alarmCallback = xCallback;
311 mem0.alarmArg = pArg;
drhfec00ea2008-06-14 16:56:21 +0000312}
313
drhf7141992008-06-19 00:16:08 +0000314/*
315** Do a memory allocation with statistics and alarms. Assume the
316** lock is already held.
317*/
318static int mallocWithAlarm(int n, void **pp){
319 int nFull;
320 void *p;
321 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000322 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000323 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
324 if( mem0.alarmCallback!=0 ){
325 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
326 if( nUsed+nFull >= mem0.alarmThreshold ){
327 sqlite3MallocAlarm(nFull);
328 }
329 }
danielk1977075c23a2008-09-01 18:34:20 +0000330 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000331 if( p==0 && mem0.alarmCallback ){
332 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000333 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000334 }
drhc702c7c2008-07-18 18:56:16 +0000335 if( p ){
336 nFull = sqlite3MallocSize(p);
337 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
338 }
drhf7141992008-06-19 00:16:08 +0000339 *pp = p;
340 return nFull;
341}
drhfec00ea2008-06-14 16:56:21 +0000342
343/*
344** Allocate memory. This routine is like sqlite3_malloc() except that it
345** assumes the memory subsystem has already been initialized.
346*/
347void *sqlite3Malloc(int n){
348 void *p;
drhe08ed7e2009-06-26 18:35:16 +0000349 if( n<=0 || n>=0x7fffff00 ){
350 /* A memory allocation of a number of bytes which is near the maximum
351 ** signed integer value might cause an integer overflow inside of the
352 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
353 ** 255 bytes of overhead. SQLite itself will never use anything near
354 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000355 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000356 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000357 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000358 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000359 sqlite3_mutex_leave(mem0.mutex);
360 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000361 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000362 }
363 return p;
364}
365
366/*
367** This version of the memory allocation is for use by the application.
368** First make sure the memory subsystem is initialized, then do the
369** allocation.
370*/
371void *sqlite3_malloc(int n){
372#ifndef SQLITE_OMIT_AUTOINIT
373 if( sqlite3_initialize() ) return 0;
374#endif
375 return sqlite3Malloc(n);
376}
377
378/*
drhe5ae5732008-06-15 02:51:47 +0000379** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000380** xScratchMalloc(). We verify this constraint in the single-threaded
381** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000382** is outstanding clearing it when the allocation is freed.
383*/
384#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000385static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000386#endif
387
388
389/*
390** Allocate memory that is to be used and released right away.
391** This routine is similar to alloca() in that it is not intended
392** for situations where the memory might be held long-term. This
393** routine is intended to get memory to old large transient data
394** structures that would not normally fit on the stack of an
395** embedded processor.
396*/
drhfacf0302008-06-17 15:12:00 +0000397void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000398 void *p;
399 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000400
drhe5ae5732008-06-15 02:51:47 +0000401#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh37f99182010-06-26 20:25:30 +0000402 /* Verify that no more than two scratch allocation per thread
drh9ac3fe92008-06-18 18:12:04 +0000403 ** is outstanding at one time. (This is only checked in the
404 ** single-threaded case since checking in the multi-threaded case
405 ** would be much more complicated.) */
drh37f99182010-06-26 20:25:30 +0000406 assert( scratchAllocOut<=1 );
drhe5ae5732008-06-15 02:51:47 +0000407#endif
drh9ac3fe92008-06-18 18:12:04 +0000408
danielk1977075c23a2008-09-01 18:34:20 +0000409 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000410 goto scratch_overflow;
411 }else{
412 sqlite3_mutex_enter(mem0.mutex);
413 if( mem0.nScratchFree==0 ){
414 sqlite3_mutex_leave(mem0.mutex);
415 goto scratch_overflow;
416 }else{
417 int i;
418 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000419 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000420 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000421 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000422 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000423 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
shane15301592008-12-16 17:20:38 +0000424 assert( (((u8*)p - (u8*)0) & 7)==0 );
drhf7141992008-06-19 00:16:08 +0000425 }
drhe5ae5732008-06-15 02:51:47 +0000426 }
drhf7141992008-06-19 00:16:08 +0000427#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
428 scratchAllocOut = p!=0;
429#endif
430
drhe5ae5732008-06-15 02:51:47 +0000431 return p;
drhf7141992008-06-19 00:16:08 +0000432
433scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000434 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000435 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000436 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000437 n = mallocWithAlarm(n, &p);
438 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
439 sqlite3_mutex_leave(mem0.mutex);
440 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000441 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000442 }
drh107b56e2010-03-12 16:32:53 +0000443 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
drhf7141992008-06-19 00:16:08 +0000444#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
445 scratchAllocOut = p!=0;
446#endif
447 return p;
drhe5ae5732008-06-15 02:51:47 +0000448}
drhfacf0302008-06-17 15:12:00 +0000449void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000450 if( p ){
danielk1977075c23a2008-09-01 18:34:20 +0000451 if( sqlite3GlobalConfig.pScratch==0
452 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000453 || p>=(void*)mem0.aScratchFree ){
drh107b56e2010-03-12 16:32:53 +0000454 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
drh1b0bfc62010-07-25 02:39:06 +0000455 assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000456 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000457 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000458 int iSize = sqlite3MallocSize(p);
459 sqlite3_mutex_enter(mem0.mutex);
460 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
461 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000462 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000463 sqlite3_mutex_leave(mem0.mutex);
464 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000465 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000466 }
drh9ac3fe92008-06-18 18:12:04 +0000467 }else{
468 int i;
drh1bd10f82008-12-10 21:19:56 +0000469 i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
danielk1977075c23a2008-09-01 18:34:20 +0000470 i /= sqlite3GlobalConfig.szScratch;
471 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000472 sqlite3_mutex_enter(mem0.mutex);
danielk197700e13612008-11-17 19:18:54 +0000473 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000474 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000475 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000476 sqlite3_mutex_leave(mem0.mutex);
drh37f99182010-06-26 20:25:30 +0000477
478#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
479 /* Verify that no more than two scratch allocation per thread
480 ** is outstanding at one time. (This is only checked in the
481 ** single-threaded case since checking in the multi-threaded case
482 ** would be much more complicated.) */
483 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
484 scratchAllocOut = 0;
485#endif
486
drh9ac3fe92008-06-18 18:12:04 +0000487 }
drhe5ae5732008-06-15 02:51:47 +0000488 }
489}
490
491/*
drh633e6d52008-07-28 19:34:53 +0000492** TRUE if p is a lookaside memory allocation from db
493*/
drh4150ebf2008-10-11 15:38:29 +0000494#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000495static int isLookaside(sqlite3 *db, void *p){
496 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
497}
drh4150ebf2008-10-11 15:38:29 +0000498#else
499#define isLookaside(A,B) 0
500#endif
drh633e6d52008-07-28 19:34:53 +0000501
502/*
drhfec00ea2008-06-14 16:56:21 +0000503** Return the size of a memory allocation previously obtained from
504** sqlite3Malloc() or sqlite3_malloc().
drh25f81882010-07-24 18:25:20 +0000505**
506** The size returned is the usable size and does not include any
507** bookkeeping overhead or sentinals at the end of the allocation.
drhfec00ea2008-06-14 16:56:21 +0000508*/
509int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000510 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh1b0bfc62010-07-25 02:39:06 +0000511 assert( sqlite3MemdebugNoType(p, MEMTYPE_RECURSIVE) );
danielk1977075c23a2008-09-01 18:34:20 +0000512 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000513}
drh25f81882010-07-24 18:25:20 +0000514int sqlite3DbMallocSize(sqlite3 *db, void *pObj){
515 EMemHdr *p = (EMemHdr*)pObj;
drh7047e252009-03-23 17:49:14 +0000516 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh25f81882010-07-24 18:25:20 +0000517 if( p ){
518 p--;
519 assert( isValidEMem(p) );
520 }
drhf18a61d2009-07-17 11:44:07 +0000521 if( isLookaside(db, p) ){
drh25f81882010-07-24 18:25:20 +0000522 return db->lookaside.sz - sizeof(EMemHdr);
drh633e6d52008-07-28 19:34:53 +0000523 }else{
drhb9755982010-07-24 16:34:37 +0000524 assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
drh107b56e2010-03-12 16:32:53 +0000525 assert( sqlite3MemdebugHasType(p,
526 db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) );
drh25f81882010-07-24 18:25:20 +0000527 return sqlite3GlobalConfig.m.xSize(p) - sizeof(EMemHdr);
drh633e6d52008-07-28 19:34:53 +0000528 }
529}
drhfec00ea2008-06-14 16:56:21 +0000530
531/*
532** Free memory previously obtained from sqlite3Malloc().
533*/
534void sqlite3_free(void *p){
535 if( p==0 ) return;
drh1b0bfc62010-07-25 02:39:06 +0000536 assert( sqlite3MemdebugNoType(p, MEMTYPE_RECURSIVE) );
drh107b56e2010-03-12 16:32:53 +0000537 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000538 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000539 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000540 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000541 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000542 sqlite3_mutex_leave(mem0.mutex);
543 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000544 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000545 }
546}
547
548/*
drh633e6d52008-07-28 19:34:53 +0000549** Free memory that might be associated with a particular database
drh25f81882010-07-24 18:25:20 +0000550** connection. All child allocations are also freed.
drhe79ffb62010-07-24 19:08:13 +0000551**
552** pObj must be a top-level allocation in the heirarchy. It is not
553** allowed to delete a child allocation since that would leave a
554** dangling child pointer in the parent.
drh633e6d52008-07-28 19:34:53 +0000555*/
drh25f81882010-07-24 18:25:20 +0000556void sqlite3DbFree(sqlite3 *db, void *pObj){
557 EMemHdr *p = (EMemHdr*)pObj;
drh7047e252009-03-23 17:49:14 +0000558 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh25f81882010-07-24 18:25:20 +0000559 if( p ) p--;
drh1b0bfc62010-07-25 02:39:06 +0000560 assert( p==0 || notChildEMem(p) ); /* pObj is not child allocation */
drh25f81882010-07-24 18:25:20 +0000561 while( p ){
562 EMemHdr *pNext = p->pESibling;
drhe79ffb62010-07-24 19:08:13 +0000563 assert( isValidEMem(p) ); /* pObj and all siblings are valid */
564 if( p->pEChild ){
565 clearChildEMem(p->pEChild);
566 sqlite3DbFree(db, (void*)&p->pEChild[1]);
567 }
drh25f81882010-07-24 18:25:20 +0000568 if( isLookaside(db, p) ){
569 LookasideSlot *pBuf = (LookasideSlot*)p;
570 clearValidEMem(p);
571 pBuf->pNext = db->lookaside.pFree;
572 db->lookaside.pFree = pBuf;
573 db->lookaside.nOut--;
574 }else{
575 assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
576 assert( sqlite3MemdebugHasType(p,
577 db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) );
578 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
579 clearValidEMem(p);
580 sqlite3_free(p);
581 }
582 p = pNext;
drh633e6d52008-07-28 19:34:53 +0000583 }
584}
585
586/*
drh25f81882010-07-24 18:25:20 +0000587** Change the size of an existing memory allocation.
588**
589** This is the same as sqlite3_realloc() except that it assumes that
590** the memory subsystem has already been initialized.
drhfec00ea2008-06-14 16:56:21 +0000591*/
592void *sqlite3Realloc(void *pOld, int nBytes){
593 int nOld, nNew;
594 void *pNew;
595 if( pOld==0 ){
596 return sqlite3Malloc(nBytes);
597 }
drhb6063cf2009-06-27 00:48:33 +0000598 if( nBytes<=0 ){
drhfec00ea2008-06-14 16:56:21 +0000599 sqlite3_free(pOld);
600 return 0;
601 }
drhb6063cf2009-06-27 00:48:33 +0000602 if( nBytes>=0x7fffff00 ){
603 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
604 return 0;
605 }
drhfec00ea2008-06-14 16:56:21 +0000606 nOld = sqlite3MallocSize(pOld);
drh7c6791c2009-08-18 14:48:53 +0000607 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
608 if( nOld==nNew ){
609 pNew = pOld;
610 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000611 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000612 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
drh7c6791c2009-08-18 14:48:53 +0000613 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
614 mem0.alarmThreshold ){
615 sqlite3MallocAlarm(nNew-nOld);
616 }
drh107b56e2010-03-12 16:32:53 +0000617 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
drh1b0bfc62010-07-25 02:39:06 +0000618 assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
drh7c6791c2009-08-18 14:48:53 +0000619 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
620 if( pNew==0 && mem0.alarmCallback ){
621 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000622 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000623 }
624 if( pNew ){
625 nNew = sqlite3MallocSize(pNew);
626 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000627 }
628 sqlite3_mutex_leave(mem0.mutex);
629 }else{
drh7c6791c2009-08-18 14:48:53 +0000630 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000631 }
632 return pNew;
633}
634
635/*
636** The public interface to sqlite3Realloc. Make sure that the memory
637** subsystem is initialized prior to invoking sqliteRealloc.
638*/
639void *sqlite3_realloc(void *pOld, int n){
640#ifndef SQLITE_OMIT_AUTOINIT
641 if( sqlite3_initialize() ) return 0;
642#endif
643 return sqlite3Realloc(pOld, n);
644}
645
drha3152892007-05-05 11:48:52 +0000646
647/*
drh17435752007-08-16 04:30:38 +0000648** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000649*/
drhfec00ea2008-06-14 16:56:21 +0000650void *sqlite3MallocZero(int n){
651 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000652 if( p ){
653 memset(p, 0, n);
654 }
655 return p;
656}
drh17435752007-08-16 04:30:38 +0000657
658/*
drhfcc89542010-07-25 02:12:51 +0000659** Allocate and zero memory. If the allocation fails, set
drh17435752007-08-16 04:30:38 +0000660** the mallocFailed flag in the connection pointer.
661*/
drhfec00ea2008-06-14 16:56:21 +0000662void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000663 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000664 if( p ){
665 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000666 }
667 return p;
668}
669
670/*
drhfcc89542010-07-25 02:12:51 +0000671** Allocate and zero memory child memory. If the allocation fails, set
672** the mallocFailed flag in the connection pointer.
673*/
674void *sqlite3DbMallocZeroChild(sqlite3 *db, int n, void *pParent){
675 void *p = sqlite3DbMallocRaw(db, n);
676 if( p ){
677 memset(p, 0, n);
678 sqlite3MemLink(pParent, p);
679 }
680 return p;
681}
682
683
684
685/*
drh17435752007-08-16 04:30:38 +0000686** Allocate and zero memory. If the allocation fails, make
687** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000688**
689** If db!=0 and db->mallocFailed is true (indicating a prior malloc
690** failure on the same database connection) then always return 0.
691** Hence for a particular database connection, once malloc starts
692** failing, it fails consistently until mallocFailed is reset.
693** This is an important assumption. There are many places in the
694** code that do things like this:
695**
696** int *a = (int*)sqlite3DbMallocRaw(db, 100);
697** int *b = (int*)sqlite3DbMallocRaw(db, 200);
698** if( b ) a[10] = 9;
699**
700** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
701** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000702*/
drhfec00ea2008-06-14 16:56:21 +0000703void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh25f81882010-07-24 18:25:20 +0000704 EMemHdr *p;
drhd9da78a2009-03-24 15:08:09 +0000705 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh25f81882010-07-24 18:25:20 +0000706 n += sizeof(EMemHdr);
drh4150ebf2008-10-11 15:38:29 +0000707#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000708 if( db ){
709 LookasideSlot *pBuf;
710 if( db->mallocFailed ){
711 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000712 }
drh633e6d52008-07-28 19:34:53 +0000713 if( db->lookaside.bEnabled && n<=db->lookaside.sz
714 && (pBuf = db->lookaside.pFree)!=0 ){
715 db->lookaside.pFree = pBuf->pNext;
716 db->lookaside.nOut++;
717 if( db->lookaside.nOut>db->lookaside.mxOut ){
718 db->lookaside.mxOut = db->lookaside.nOut;
719 }
drh25f81882010-07-24 18:25:20 +0000720 p = (EMemHdr*)pBuf;
721 goto finish_emalloc_raw;
drh633e6d52008-07-28 19:34:53 +0000722 }
723 }
drhddecae72008-10-11 17:35:16 +0000724#else
725 if( db && db->mallocFailed ){
726 return 0;
727 }
drh4150ebf2008-10-11 15:38:29 +0000728#endif
drh633e6d52008-07-28 19:34:53 +0000729 p = sqlite3Malloc(n);
drh25f81882010-07-24 18:25:20 +0000730 if( !p ){
731 if( db ) db->mallocFailed = 1;
732 return 0;
drh17435752007-08-16 04:30:38 +0000733 }
drhb9755982010-07-24 16:34:37 +0000734 sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE |
735 ((db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP));
drh25f81882010-07-24 18:25:20 +0000736
737finish_emalloc_raw:
738 memset(p, 0, sizeof(EMemHdr));
739 setValidEMem(p);
740 return (void*)&p[1];
drh17435752007-08-16 04:30:38 +0000741}
742
danielk197726783a52007-08-29 14:06:22 +0000743/*
drhfcc89542010-07-25 02:12:51 +0000744** A convenience wrapper around sqlite3DbMallocRaw() and sqlite3MemLink().
745*/
746void *sqlite3DbMallocRawChild(sqlite3 *db, int n, void *pParent){
747 void *p = sqlite3DbMallocRaw(db, n);
748 sqlite3MemLink(pParent, p);
749 return p;
750}
751
752/*
danielk197726783a52007-08-29 14:06:22 +0000753** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000754** resize fails, set the mallocFailed flag in the connection object.
drh25f81882010-07-24 18:25:20 +0000755**
756** The pOld memory block must not be linked into an allocation hierarchy
757** as a child. It is OK for the allocation to be the root of a hierarchy
758** of allocations; the only restriction is that there must be no other
759** allocations above the pOld allocation in the hierarchy. To resize
760** an allocation that is a child within a hierarchy, first
761** unlink the allocation, resize it, then relink it.
danielk197726783a52007-08-29 14:06:22 +0000762*/
drh25f81882010-07-24 18:25:20 +0000763void *sqlite3DbRealloc(sqlite3 *db, void *pOld, int n){
764 EMemHdr *p = (EMemHdr*)pOld;
765 EMemHdr *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000766 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000767 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000768 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000769 if( p==0 ){
770 return sqlite3DbMallocRaw(db, n);
771 }
drh25f81882010-07-24 18:25:20 +0000772 p--;
773 assert( isValidEMem(p) ); /* pOld obtained from extended allocator */
drh1b0bfc62010-07-25 02:39:06 +0000774 assert( notChildEMem(p) ); /* pOld must not be a child allocation */
drh633e6d52008-07-28 19:34:53 +0000775 if( isLookaside(db, p) ){
drh25f81882010-07-24 18:25:20 +0000776 if( n+sizeof(EMemHdr)<=db->lookaside.sz ){
777 return pOld;
drh633e6d52008-07-28 19:34:53 +0000778 }
779 pNew = sqlite3DbMallocRaw(db, n);
780 if( pNew ){
drh25f81882010-07-24 18:25:20 +0000781 memcpy(pNew-1, p, db->lookaside.sz);
782 setValidEMem(pNew-1);
783 sqlite3DbFree(db, pOld);
drh633e6d52008-07-28 19:34:53 +0000784 }
785 }else{
drhb9755982010-07-24 16:34:37 +0000786 assert( sqlite3MemdebugHasType(p, MEMTYPE_RECURSIVE) );
drh107b56e2010-03-12 16:32:53 +0000787 assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) );
788 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh25f81882010-07-24 18:25:20 +0000789 pNew = sqlite3_realloc(p, n+sizeof(EMemHdr));
drh633e6d52008-07-28 19:34:53 +0000790 if( !pNew ){
drhb9755982010-07-24 16:34:37 +0000791 sqlite3MemdebugSetType(p, MEMTYPE_RECURSIVE|MEMTYPE_HEAP);
drh633e6d52008-07-28 19:34:53 +0000792 db->mallocFailed = 1;
drh25f81882010-07-24 18:25:20 +0000793 }else{
794 sqlite3MemdebugSetType(pNew, MEMTYPE_RECURSIVE |
795 (db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP));
796 setValidEMem(pNew);
797 pNew++;
drh633e6d52008-07-28 19:34:53 +0000798 }
danielk1977a1644fd2007-08-29 12:31:25 +0000799 }
800 }
drh25f81882010-07-24 18:25:20 +0000801 return (void*)pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000802}
803
drh17435752007-08-16 04:30:38 +0000804/*
805** Attempt to reallocate p. If the reallocation fails, then free p
806** and set the mallocFailed flag in the database connection.
807*/
808void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000809 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000810 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000811 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000812 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000813 }
814 return pNew;
815}
816
drha3152892007-05-05 11:48:52 +0000817/*
818** Make a copy of a string in memory obtained from sqliteMalloc(). These
819** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
820** is because when memory debugging is turned on, these two functions are
821** called via macros that record the current file and line number in the
822** ThreadData structure.
823*/
drh633e6d52008-07-28 19:34:53 +0000824char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000825 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000826 size_t n;
827 if( z==0 ){
828 return 0;
829 }
drhdee0e402009-05-03 20:23:53 +0000830 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000831 assert( (n&0x7fffffff)==n );
832 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000833 if( zNew ){
834 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000835 }
836 return zNew;
837}
838char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000839 char *zNew;
840 if( z==0 ){
841 return 0;
842 }
843 assert( (n&0x7fffffff)==n );
844 zNew = sqlite3DbMallocRaw(db, n+1);
845 if( zNew ){
846 memcpy(zNew, z, n);
847 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000848 }
849 return zNew;
850}
851
drha3152892007-05-05 11:48:52 +0000852/*
drh25f81882010-07-24 18:25:20 +0000853** Link extended allocation nodes such that deallocating the parent
854** causes the child to be automatically deallocated.
855*/
856void sqlite3MemLink(void *pParentObj, void *pChildObj){
857 EMemHdr *pParent = (EMemHdr*)pParentObj;
858 EMemHdr *pChild = (EMemHdr*)pChildObj;
859 if( pParent && pChild ){
860 pParent--;
861 assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */
862 pChild--;
863 assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */
drh1b0bfc62010-07-25 02:39:06 +0000864 assert( notChildEMem(pChild) ); /* pChildObj not a child of another obj */
drh25f81882010-07-24 18:25:20 +0000865 pChild->pESibling = pParent->pEChild;
866 pParent->pEChild = pChild;
867 setChildEMem(pChild);
868 }
869}
870
871/*
872** pChildObj is a child object of pParentObj due to a prior call
873** to sqlite3MemLink(). This routine breaks that linkage, making
874** pChildObj an independent node that is not a child of any other node.
875*/
876void sqlite3MemUnlink(void *pParentObj, void *pChildObj){
877 EMemHdr *pParent = (EMemHdr*)pParentObj;
878 EMemHdr *pChild = (EMemHdr*)pChildObj;
879 EMemHdr **pp;
880
881 assert( pParentObj!=0 );
882 assert( pChildObj!=0 );
883 pParent--;
884 assert( isValidEMem(pParent) ); /* pParentObj is an extended allocation */
885 pChild--;
886 assert( isValidEMem(pChild) ); /* pChildObj is an extended allocation */
887 assert( isChildEMem(pChild) ); /* pChildObj a child of something */
888 for(pp=&pParent->pEChild; (*pp)!=pChild; pp = &(*pp)->pESibling){
889 assert( *pp ); /* pChildObj is a child of pParentObj */
890 assert( isValidEMem(*pp) ); /* All children of pParentObj are valid */
891 assert( isChildEMem(*pp) ); /* All children of pParentObj are children */
892 }
893 *pp = pChild->pESibling;
894 pChild->pESibling = 0;
895 clearChildEMem(pChild);
896}
897
898
899/*
drhf089aa42008-07-08 19:34:06 +0000900** Create a string from the zFromat argument and the va_list that follows.
901** Store the string in memory obtained from sqliteMalloc() and make *pz
902** point to that string.
drha3152892007-05-05 11:48:52 +0000903*/
drhf089aa42008-07-08 19:34:06 +0000904void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000905 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000906 char *z;
drha3152892007-05-05 11:48:52 +0000907
drhf089aa42008-07-08 19:34:06 +0000908 va_start(ap, zFormat);
909 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000910 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000911 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000912 *pz = z;
drha3152892007-05-05 11:48:52 +0000913}
914
915
916/*
917** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000918** returning control to the user) that has called sqlite3_malloc or
919** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000920**
921** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000922** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000923** invocation SQLITE_NOMEM is returned instead.
924**
shanebe217792009-03-05 04:20:31 +0000925** If the first argument, db, is not NULL and a malloc() error has occurred,
drha3152892007-05-05 11:48:52 +0000926** then the connection error-code (the value returned by sqlite3_errcode())
927** is set to SQLITE_NOMEM.
928*/
drha3152892007-05-05 11:48:52 +0000929int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000930 /* If the db handle is not NULL, then we must hold the connection handle
931 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
932 ** is unsafe, as is the call to sqlite3Error().
933 */
934 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000935 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000936 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000937 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000938 rc = SQLITE_NOMEM;
939 }
940 return rc & (db ? db->errMask : 0xff);
941}