xref: /sqlite-3.40.0/src/malloc.c (revision 054bbabc)
1a3152895Sdrh /*
2a3152895Sdrh ** 2001 September 15
3a3152895Sdrh **
4a3152895Sdrh ** The author disclaims copyright to this source code.  In place of
5a3152895Sdrh ** a legal notice, here is a blessing:
6a3152895Sdrh **
7a3152895Sdrh **    May you do good and not evil.
8a3152895Sdrh **    May you find forgiveness for yourself and forgive others.
9a3152895Sdrh **    May you share freely, never taking more than you give.
10a3152895Sdrh **
11a3152895Sdrh *************************************************************************
12fec00eabSdrh **
13a3152895Sdrh ** Memory allocation functions used throughout sqlite.
14a3152895Sdrh */
15a3152895Sdrh #include "sqliteInt.h"
16a3152895Sdrh #include <stdarg.h>
17a3152895Sdrh 
18a3152895Sdrh /*
198468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently
208468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to
218468024dSdanielk1977 ** cache database pages that are not currently in use.
22a3152895Sdrh */
23a3152895Sdrh int sqlite3_release_memory(int n){
2486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
259f129f46Sdrh   return sqlite3PcacheReleaseMemory(n);
261e536953Sdanielk1977 #else
279f129f46Sdrh   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
289f129f46Sdrh   ** is a no-op returning zero if SQLite is not compiled with
299f129f46Sdrh   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
3062c14b34Sdanielk1977   UNUSED_PARAMETER(n);
319f129f46Sdrh   return 0;
321e536953Sdanielk1977 #endif
33a3152895Sdrh }
34a3152895Sdrh 
35fec00eabSdrh /*
36badc980aSdrh ** An instance of the following object records the location of
37badc980aSdrh ** each unused scratch buffer.
38badc980aSdrh */
39badc980aSdrh typedef struct ScratchFreeslot {
40badc980aSdrh   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
41badc980aSdrh } ScratchFreeslot;
42badc980aSdrh 
43badc980aSdrh /*
44fec00eabSdrh ** State information local to the memory allocation subsystem.
45fec00eabSdrh */
465c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
47fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
48fec00eabSdrh 
49fec00eabSdrh   /*
50fec00eabSdrh   ** The alarm callback and its arguments.  The mem0.mutex lock will
51fec00eabSdrh   ** be held while the callback is running.  Recursive calls into
52fec00eabSdrh   ** the memory subsystem are allowed, but no new callbacks will be
53e64ca7baSdrh   ** issued.
54fec00eabSdrh   */
55fec00eabSdrh   sqlite3_int64 alarmThreshold;
56fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
57fec00eabSdrh   void *alarmArg;
58fec00eabSdrh 
59fec00eabSdrh   /*
60badc980aSdrh   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
61badc980aSdrh   ** (so that a range test can be used to determine if an allocation
62badc980aSdrh   ** being freed came from pScratch) and a pointer to the list of
63badc980aSdrh   ** unused scratch allocations.
649ac3fe97Sdrh   */
65badc980aSdrh   void *pScratchEnd;
66badc980aSdrh   ScratchFreeslot *pScratchFree;
67badc980aSdrh   u32 nScratchFree;
6850d1b5f3Sdrh 
6950d1b5f3Sdrh   /*
7050d1b5f3Sdrh   ** True if heap is nearly "full" where "full" is defined by the
7150d1b5f3Sdrh   ** sqlite3_soft_heap_limit() setting.
7250d1b5f3Sdrh   */
7350d1b5f3Sdrh   int nearlyFull;
746ac78a0dSdrh } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
755c8f8587Sdanielk1977 
765c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
77fec00eabSdrh 
78fec00eabSdrh /*
79af89fe66Sdrh ** Return the memory allocator mutex. sqlite3_status() needs it.
80af89fe66Sdrh */
81af89fe66Sdrh sqlite3_mutex *sqlite3MallocMutex(void){
82af89fe66Sdrh   return mem0.mutex;
83af89fe66Sdrh }
84af89fe66Sdrh 
85af89fe66Sdrh /*
86f82ccf64Sdrh ** This routine runs when the memory allocator sees that the
87f82ccf64Sdrh ** total memory allocation is about to exceed the soft heap
88f82ccf64Sdrh ** limit.
89f82ccf64Sdrh */
90f82ccf64Sdrh static void softHeapLimitEnforcer(
91f82ccf64Sdrh   void *NotUsed,
92f82ccf64Sdrh   sqlite3_int64 NotUsed2,
93f82ccf64Sdrh   int allocSize
94f82ccf64Sdrh ){
95f82ccf64Sdrh   UNUSED_PARAMETER2(NotUsed, NotUsed2);
96f82ccf64Sdrh   sqlite3_release_memory(allocSize);
97f82ccf64Sdrh }
98f82ccf64Sdrh 
99f82ccf64Sdrh /*
100f82ccf64Sdrh ** Change the alarm callback
101f82ccf64Sdrh */
102f82ccf64Sdrh static int sqlite3MemoryAlarm(
103f82ccf64Sdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
104f82ccf64Sdrh   void *pArg,
105f82ccf64Sdrh   sqlite3_int64 iThreshold
106f82ccf64Sdrh ){
107af89fe66Sdrh   sqlite3_int64 nUsed;
108f82ccf64Sdrh   sqlite3_mutex_enter(mem0.mutex);
109f82ccf64Sdrh   mem0.alarmCallback = xCallback;
110f82ccf64Sdrh   mem0.alarmArg = pArg;
111f82ccf64Sdrh   mem0.alarmThreshold = iThreshold;
112f82ccf64Sdrh   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
113f82ccf64Sdrh   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
114f82ccf64Sdrh   sqlite3_mutex_leave(mem0.mutex);
115f82ccf64Sdrh   return SQLITE_OK;
116f82ccf64Sdrh }
117f82ccf64Sdrh 
118f82ccf64Sdrh #ifndef SQLITE_OMIT_DEPRECATED
119f82ccf64Sdrh /*
120f82ccf64Sdrh ** Deprecated external interface.  Internal/core SQLite code
121f82ccf64Sdrh ** should call sqlite3MemoryAlarm.
122f82ccf64Sdrh */
123f82ccf64Sdrh int sqlite3_memory_alarm(
124f82ccf64Sdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
125f82ccf64Sdrh   void *pArg,
126f82ccf64Sdrh   sqlite3_int64 iThreshold
127f82ccf64Sdrh ){
128f82ccf64Sdrh   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
129f82ccf64Sdrh }
130f82ccf64Sdrh #endif
131f82ccf64Sdrh 
132f82ccf64Sdrh /*
133f82ccf64Sdrh ** Set the soft heap-size limit for the library. Passing a zero or
134f82ccf64Sdrh ** negative value indicates no limit.
135f82ccf64Sdrh */
136f82ccf64Sdrh sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
137f82ccf64Sdrh   sqlite3_int64 priorLimit;
138f82ccf64Sdrh   sqlite3_int64 excess;
139f82ccf64Sdrh #ifndef SQLITE_OMIT_AUTOINIT
140de0f1815Sdrh   int rc = sqlite3_initialize();
141de0f1815Sdrh   if( rc ) return -1;
142f82ccf64Sdrh #endif
143f82ccf64Sdrh   sqlite3_mutex_enter(mem0.mutex);
144f82ccf64Sdrh   priorLimit = mem0.alarmThreshold;
145f82ccf64Sdrh   sqlite3_mutex_leave(mem0.mutex);
146f82ccf64Sdrh   if( n<0 ) return priorLimit;
147f82ccf64Sdrh   if( n>0 ){
148f82ccf64Sdrh     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
149f82ccf64Sdrh   }else{
150f82ccf64Sdrh     sqlite3MemoryAlarm(0, 0, 0);
151f82ccf64Sdrh   }
152f82ccf64Sdrh   excess = sqlite3_memory_used() - n;
1534b03f21eSshaneh   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
154f82ccf64Sdrh   return priorLimit;
155f82ccf64Sdrh }
156f82ccf64Sdrh void sqlite3_soft_heap_limit(int n){
157f82ccf64Sdrh   if( n<0 ) n = 0;
158f82ccf64Sdrh   sqlite3_soft_heap_limit64(n);
159f82ccf64Sdrh }
160f82ccf64Sdrh 
161f82ccf64Sdrh /*
162fec00eabSdrh ** Initialize the memory allocation subsystem.
163fec00eabSdrh */
164fec00eabSdrh int sqlite3MallocInit(void){
165592f0cb1Sdrh   int rc;
166075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
167fec00eabSdrh     sqlite3MemSetDefault();
168fec00eabSdrh   }
169fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
170075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
17159f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
172fec00eabSdrh   }
173075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
1747ff2719eSdrh       && sqlite3GlobalConfig.nScratch>0 ){
175badc980aSdrh     int i, n, sz;
176badc980aSdrh     ScratchFreeslot *pSlot;
177badc980aSdrh     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
178badc980aSdrh     sqlite3GlobalConfig.szScratch = sz;
179badc980aSdrh     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
180badc980aSdrh     n = sqlite3GlobalConfig.nScratch;
181badc980aSdrh     mem0.pScratchFree = pSlot;
182badc980aSdrh     mem0.nScratchFree = n;
183badc980aSdrh     for(i=0; i<n-1; i++){
184badc980aSdrh       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
185badc980aSdrh       pSlot = pSlot->pNext;
186badc980aSdrh     }
187badc980aSdrh     pSlot->pNext = 0;
188badc980aSdrh     mem0.pScratchEnd = (void*)&pSlot[1];
1899ac3fe97Sdrh   }else{
190badc980aSdrh     mem0.pScratchEnd = 0;
191075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
192075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
193badc980aSdrh     sqlite3GlobalConfig.nScratch = 0;
1949ac3fe97Sdrh   }
19550d1b5f3Sdrh   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
19601c5c00cSdrh       || sqlite3GlobalConfig.nPage<=0 ){
197075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
198075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
1999ac3fe97Sdrh   }
200592f0cb1Sdrh   rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
201592f0cb1Sdrh   if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
202592f0cb1Sdrh   return rc;
203fec00eabSdrh }
204fec00eabSdrh 
205fec00eabSdrh /*
20650d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other
20750d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by
20850d1b5f3Sdrh ** sqlite3_soft_heap_limit().
20950d1b5f3Sdrh */
21050d1b5f3Sdrh int sqlite3HeapNearlyFull(void){
21150d1b5f3Sdrh   return mem0.nearlyFull;
21250d1b5f3Sdrh }
21350d1b5f3Sdrh 
21450d1b5f3Sdrh /*
215fec00eabSdrh ** Deinitialize the memory allocation subsystem.
216fec00eabSdrh */
217fec00eabSdrh void sqlite3MallocEnd(void){
2180a549071Sdanielk1977   if( sqlite3GlobalConfig.m.xShutdown ){
219075c23afSdanielk1977     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
2200a549071Sdanielk1977   }
2219ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
222fec00eabSdrh }
223fec00eabSdrh 
224fec00eabSdrh /*
225fec00eabSdrh ** Return the amount of memory currently checked out.
226fec00eabSdrh */
227fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
228df5e1a00Sdrh   sqlite3_int64 res, mx;
229df5e1a00Sdrh   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
230c376a198Sdrh   return res;
231fec00eabSdrh }
232fec00eabSdrh 
233fec00eabSdrh /*
234fec00eabSdrh ** Return the maximum amount of memory that has ever been
235fec00eabSdrh ** checked out since either the beginning of this process
236fec00eabSdrh ** or since the most recent reset.
237fec00eabSdrh */
238fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
239df5e1a00Sdrh   sqlite3_int64 res, mx;
240df5e1a00Sdrh   sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
241df5e1a00Sdrh   return mx;
242fec00eabSdrh }
243fec00eabSdrh 
244fec00eabSdrh /*
245fec00eabSdrh ** Trigger the alarm
246fec00eabSdrh */
247fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
248fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
249fec00eabSdrh   sqlite3_int64 nowUsed;
250fec00eabSdrh   void *pArg;
251e64ca7baSdrh   if( mem0.alarmCallback==0 ) return;
252fec00eabSdrh   xCallback = mem0.alarmCallback;
253f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
254fec00eabSdrh   pArg = mem0.alarmArg;
255e64ca7baSdrh   mem0.alarmCallback = 0;
256fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
257fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
258fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
259e64ca7baSdrh   mem0.alarmCallback = xCallback;
260e64ca7baSdrh   mem0.alarmArg = pArg;
261fec00eabSdrh }
262fec00eabSdrh 
263fec00eabSdrh /*
264f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
265f7141990Sdrh ** lock is already held.
266fec00eabSdrh */
267f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
268fec00eabSdrh   int nFull;
269f7141990Sdrh   void *p;
270f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
271075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
272f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
273f7141990Sdrh   if( mem0.alarmCallback!=0 ){
274af89fe66Sdrh     sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2758e1bb041Sdrh     if( nUsed >= mem0.alarmThreshold - nFull ){
27650d1b5f3Sdrh       mem0.nearlyFull = 1;
277fec00eabSdrh       sqlite3MallocAlarm(nFull);
27850d1b5f3Sdrh     }else{
27950d1b5f3Sdrh       mem0.nearlyFull = 0;
280fec00eabSdrh     }
281f7141990Sdrh   }
282075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
28350d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
284d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
285fec00eabSdrh     sqlite3MallocAlarm(nFull);
286075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
287fec00eabSdrh   }
28850d1b5f3Sdrh #endif
289c702c7ccSdrh   if( p ){
290c702c7ccSdrh     nFull = sqlite3MallocSize(p);
291af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
292af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
293c702c7ccSdrh   }
294f7141990Sdrh   *pp = p;
295f7141990Sdrh   return nFull;
296fec00eabSdrh }
297f7141990Sdrh 
298f7141990Sdrh /*
299f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
300f7141990Sdrh ** assumes the memory subsystem has already been initialized.
301f7141990Sdrh */
302da4ca9d1Sdrh void *sqlite3Malloc(u64 n){
303f7141990Sdrh   void *p;
304da4ca9d1Sdrh   if( n==0 || n>=0x7fffff00 ){
305e08ed7e7Sdrh     /* A memory allocation of a number of bytes which is near the maximum
306e08ed7e7Sdrh     ** signed integer value might cause an integer overflow inside of the
307e08ed7e7Sdrh     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
308e08ed7e7Sdrh     ** 255 bytes of overhead.  SQLite itself will never use anything near
309e08ed7e7Sdrh     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
310f7141990Sdrh     p = 0;
311075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
312f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
3133329a63aSdrh     mallocWithAlarm((int)n, &p);
314fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
315fec00eabSdrh   }else{
316da4ca9d1Sdrh     p = sqlite3GlobalConfig.m.xMalloc((int)n);
317fec00eabSdrh   }
3188da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
319fec00eabSdrh   return p;
320fec00eabSdrh }
321fec00eabSdrh 
322fec00eabSdrh /*
323fec00eabSdrh ** This version of the memory allocation is for use by the application.
324fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
325fec00eabSdrh ** allocation.
326fec00eabSdrh */
327fec00eabSdrh void *sqlite3_malloc(int n){
328fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
329fec00eabSdrh   if( sqlite3_initialize() ) return 0;
330fec00eabSdrh #endif
331da4ca9d1Sdrh   return n<=0 ? 0 : sqlite3Malloc(n);
332da4ca9d1Sdrh }
333da4ca9d1Sdrh void *sqlite3_malloc64(sqlite3_uint64 n){
334da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
335da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
336da4ca9d1Sdrh #endif
337fec00eabSdrh   return sqlite3Malloc(n);
338fec00eabSdrh }
339fec00eabSdrh 
340fec00eabSdrh /*
341e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
342facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
343facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
344e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
345e5ae5735Sdrh */
346e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
347facf0307Sdrh static int scratchAllocOut = 0;
348e5ae5735Sdrh #endif
349e5ae5735Sdrh 
350e5ae5735Sdrh 
351e5ae5735Sdrh /*
352e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
353e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
354e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
355e5ae5735Sdrh ** routine is intended to get memory to old large transient data
356e5ae5735Sdrh ** structures that would not normally fit on the stack of an
357e5ae5735Sdrh ** embedded processor.
358e5ae5735Sdrh */
359facf0307Sdrh void *sqlite3ScratchMalloc(int n){
360e5ae5735Sdrh   void *p;
361e5ae5735Sdrh   assert( n>0 );
3629ac3fe97Sdrh 
363badc980aSdrh   sqlite3_mutex_enter(mem0.mutex);
3643ccd5bf8Sdrh   sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
365badc980aSdrh   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
366badc980aSdrh     p = mem0.pScratchFree;
367badc980aSdrh     mem0.pScratchFree = mem0.pScratchFree->pNext;
368badc980aSdrh     mem0.nScratchFree--;
369af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
370b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
371badc980aSdrh   }else{
372b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
3733ccd5bf8Sdrh     p = sqlite3Malloc(n);
3743ccd5bf8Sdrh     if( sqlite3GlobalConfig.bMemstat && p ){
3753ccd5bf8Sdrh       sqlite3_mutex_enter(mem0.mutex);
376af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
3773ccd5bf8Sdrh       sqlite3_mutex_leave(mem0.mutex);
378badc980aSdrh     }
379badc980aSdrh     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
380badc980aSdrh   }
3811ff6e3abSdrh   assert( sqlite3_mutex_notheld(mem0.mutex) );
382b0c6a888Sdan 
383badc980aSdrh 
384badc980aSdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
385cbd55b03Sdrh   /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
386cbd55b03Sdrh   ** buffers per thread.
387cbd55b03Sdrh   **
388cbd55b03Sdrh   ** This can only be checked in single-threaded mode.
389cbd55b03Sdrh   */
390cbd55b03Sdrh   assert( scratchAllocOut==0 );
391badc980aSdrh   if( p ) scratchAllocOut++;
392badc980aSdrh #endif
393badc980aSdrh 
394badc980aSdrh   return p;
395badc980aSdrh }
396badc980aSdrh void sqlite3ScratchFree(void *p){
397badc980aSdrh   if( p ){
398badc980aSdrh 
399e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
40037f99187Sdrh     /* Verify that no more than two scratch allocation per thread
4019ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
4029ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
4039ac3fe97Sdrh     ** would be much more complicated.) */
404badc980aSdrh     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
405badc980aSdrh     scratchAllocOut--;
406e5ae5735Sdrh #endif
4079ac3fe97Sdrh 
408badc980aSdrh     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
409badc980aSdrh       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
410badc980aSdrh       ScratchFreeslot *pSlot;
411badc980aSdrh       pSlot = (ScratchFreeslot*)p;
412e5ae5735Sdrh       sqlite3_mutex_enter(mem0.mutex);
413badc980aSdrh       pSlot->pNext = mem0.pScratchFree;
414badc980aSdrh       mem0.pScratchFree = pSlot;
415badc980aSdrh       mem0.nScratchFree++;
416fcd71b60Sdrh       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
417af89fe66Sdrh       sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
4189ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
419f7141990Sdrh     }else{
420badc980aSdrh       /* Release memory back to the heap */
421107b56e8Sdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
422d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
423107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
424075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
425f7141990Sdrh         int iSize = sqlite3MallocSize(p);
426f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
427af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
428af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
429af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
430075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
431f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
432f7141990Sdrh       }else{
433075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
434f7141990Sdrh       }
4359ac3fe97Sdrh     }
436e5ae5735Sdrh   }
437e5ae5735Sdrh }
438e5ae5735Sdrh 
439e5ae5735Sdrh /*
440633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
441633e6d57Sdrh */
4424150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
443633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
444b0e7704eSdrh   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
445633e6d57Sdrh }
4464150ebf8Sdrh #else
4474150ebf8Sdrh #define isLookaside(A,B) 0
4484150ebf8Sdrh #endif
449633e6d57Sdrh 
450633e6d57Sdrh /*
451fec00eabSdrh ** Return the size of a memory allocation previously obtained from
452fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
453fec00eabSdrh */
454fec00eabSdrh int sqlite3MallocSize(void *p){
455107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
456075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
457fec00eabSdrh }
458633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
459*054bbabcSdrh   if( db==0 || !isLookaside(db,p) ){
460*054bbabcSdrh #if SQLITE_DEBUG
46117bcb102Sdrh     if( db==0 ){
462d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
463d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
464633e6d57Sdrh     }else{
465d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
466d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
467633e6d57Sdrh     }
468*054bbabcSdrh #endif
469*054bbabcSdrh     return sqlite3GlobalConfig.m.xSize(p);
470*054bbabcSdrh   }else{
471*054bbabcSdrh     assert( sqlite3_mutex_held(db->mutex) );
472*054bbabcSdrh     return db->lookaside.sz;
473633e6d57Sdrh   }
47417bcb102Sdrh }
475da4ca9d1Sdrh sqlite3_uint64 sqlite3_msize(void *p){
476d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
477d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
478da4ca9d1Sdrh   return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
479da4ca9d1Sdrh }
480fec00eabSdrh 
481fec00eabSdrh /*
482fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
483fec00eabSdrh */
484fec00eabSdrh void sqlite3_free(void *p){
48571a1a0f4Sdrh   if( p==0 ) return;  /* IMP: R-49053-54554 */
486107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
487d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
488075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
489fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
490af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
491af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
492075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
493fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
494fec00eabSdrh   }else{
495075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
496fec00eabSdrh   }
497fec00eabSdrh }
498fec00eabSdrh 
499fec00eabSdrh /*
500b4586f12Sdrh ** Add the size of memory allocation "p" to the count in
501b4586f12Sdrh ** *db->pnBytesFreed.
502b4586f12Sdrh */
503b4586f12Sdrh static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
504b4586f12Sdrh   *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
505b4586f12Sdrh }
506b4586f12Sdrh 
507b4586f12Sdrh /*
508633e6d57Sdrh ** Free memory that might be associated with a particular database
509633e6d57Sdrh ** connection.
510633e6d57Sdrh */
511633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
5127047e25cSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
5139ccd8659Sdrh   if( p==0 ) return;
514174b9a16Sdrh   if( db ){
515174b9a16Sdrh     if( db->pnBytesFreed ){
516b4586f12Sdrh       measureAllocationSize(db, p);
517174b9a16Sdrh       return;
518d46def77Sdan     }
519633e6d57Sdrh     if( isLookaside(db, p) ){
520633e6d57Sdrh       LookasideSlot *pBuf = (LookasideSlot*)p;
5213608f177Sdrh #if SQLITE_DEBUG
5223608f177Sdrh       /* Trash all content in the buffer being freed */
5233608f177Sdrh       memset(p, 0xaa, db->lookaside.sz);
5243608f177Sdrh #endif
525633e6d57Sdrh       pBuf->pNext = db->lookaside.pFree;
526633e6d57Sdrh       db->lookaside.pFree = pBuf;
527633e6d57Sdrh       db->lookaside.nOut--;
528174b9a16Sdrh       return;
529174b9a16Sdrh     }
530174b9a16Sdrh   }
531d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
532d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
533174b9a16Sdrh   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
534107b56e8Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
535633e6d57Sdrh   sqlite3_free(p);
536633e6d57Sdrh }
537633e6d57Sdrh 
538633e6d57Sdrh /*
539fec00eabSdrh ** Change the size of an existing memory allocation
540fec00eabSdrh */
541da4ca9d1Sdrh void *sqlite3Realloc(void *pOld, u64 nBytes){
542ca591febSshaneh   int nOld, nNew, nDiff;
543fec00eabSdrh   void *pNew;
544d231aa3aSdrh   assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
545d425864dSmistachkin   assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
546fec00eabSdrh   if( pOld==0 ){
5478da47419Sdrh     return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
548fec00eabSdrh   }
549da4ca9d1Sdrh   if( nBytes==0 ){
5508da47419Sdrh     sqlite3_free(pOld); /* IMP: R-26507-47431 */
551fec00eabSdrh     return 0;
552fec00eabSdrh   }
553b6063cf8Sdrh   if( nBytes>=0x7fffff00 ){
554b6063cf8Sdrh     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
555b6063cf8Sdrh     return 0;
556b6063cf8Sdrh   }
557fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
5589f129f46Sdrh   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
5599f129f46Sdrh   ** argument to xRealloc is always a value returned by a prior call to
5609f129f46Sdrh   ** xRoundup. */
561da4ca9d1Sdrh   nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
562fec00eabSdrh   if( nOld==nNew ){
563fec00eabSdrh     pNew = pOld;
5647c6791c8Sdrh   }else if( sqlite3GlobalConfig.bMemstat ){
5657c6791c8Sdrh     sqlite3_mutex_enter(mem0.mutex);
5663329a63aSdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
5678e1bb041Sdrh     nDiff = nNew - nOld;
5688e1bb041Sdrh     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
5698e1bb041Sdrh           mem0.alarmThreshold-nDiff ){
5702e5a422eSdrh       sqlite3MallocAlarm(nDiff);
571fec00eabSdrh     }
572075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
573d09414cdSdanielk1977     if( pNew==0 && mem0.alarmCallback ){
5743329a63aSdrh       sqlite3MallocAlarm((int)nBytes);
575075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
576fec00eabSdrh     }
577fec00eabSdrh     if( pNew ){
578c702c7ccSdrh       nNew = sqlite3MallocSize(pNew);
579af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
580fec00eabSdrh     }
581fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
582fec00eabSdrh   }else{
5837c6791c8Sdrh     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
584fec00eabSdrh   }
5858da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
586fec00eabSdrh   return pNew;
587fec00eabSdrh }
588fec00eabSdrh 
589fec00eabSdrh /*
590fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
591fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
592fec00eabSdrh */
593fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
594fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
595fec00eabSdrh   if( sqlite3_initialize() ) return 0;
596fec00eabSdrh #endif
5978da47419Sdrh   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
598da4ca9d1Sdrh   return sqlite3Realloc(pOld, n);
599da4ca9d1Sdrh }
600da4ca9d1Sdrh void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
601da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
602da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
603da4ca9d1Sdrh #endif
604fec00eabSdrh   return sqlite3Realloc(pOld, n);
605fec00eabSdrh }
606fec00eabSdrh 
607a3152895Sdrh 
608a3152895Sdrh /*
60917435752Sdrh ** Allocate and zero memory.
610a3152895Sdrh */
611da4ca9d1Sdrh void *sqlite3MallocZero(u64 n){
612fec00eabSdrh   void *p = sqlite3Malloc(n);
613a3152895Sdrh   if( p ){
61420f3df04Sdrh     memset(p, 0, (size_t)n);
615a3152895Sdrh   }
616a3152895Sdrh   return p;
617a3152895Sdrh }
61817435752Sdrh 
61917435752Sdrh /*
62017435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
62117435752Sdrh ** the mallocFailed flag in the connection pointer.
62217435752Sdrh */
623da4ca9d1Sdrh void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
624a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
62517435752Sdrh   if( p ){
62620f3df04Sdrh     memset(p, 0, (size_t)n);
62717435752Sdrh   }
62817435752Sdrh   return p;
62917435752Sdrh }
63017435752Sdrh 
63117435752Sdrh /*
63217435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
63317435752Sdrh ** the mallocFailed flag in the connection pointer.
634ddecae79Sdrh **
635ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
636ddecae79Sdrh ** failure on the same database connection) then always return 0.
637ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
638ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
639ddecae79Sdrh ** This is an important assumption.  There are many places in the
640ddecae79Sdrh ** code that do things like this:
641ddecae79Sdrh **
642ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
643ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
644ddecae79Sdrh **         if( b ) a[10] = 9;
645ddecae79Sdrh **
646ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
647ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
64817435752Sdrh */
649da4ca9d1Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
650633e6d57Sdrh   void *p;
651d9da78a2Sdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
652ccd4ad3eSdan   assert( db==0 || db->pnBytesFreed==0 );
6534150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
654633e6d57Sdrh   if( db ){
655633e6d57Sdrh     LookasideSlot *pBuf;
656633e6d57Sdrh     if( db->mallocFailed ){
657633e6d57Sdrh       return 0;
658633e6d57Sdrh     }
6590b12e7f8Sdrh     if( db->lookaside.bEnabled ){
6600b12e7f8Sdrh       if( n>db->lookaside.sz ){
6610b12e7f8Sdrh         db->lookaside.anStat[1]++;
6620b12e7f8Sdrh       }else if( (pBuf = db->lookaside.pFree)==0 ){
6630b12e7f8Sdrh         db->lookaside.anStat[2]++;
6640b12e7f8Sdrh       }else{
665633e6d57Sdrh         db->lookaside.pFree = pBuf->pNext;
666633e6d57Sdrh         db->lookaside.nOut++;
6670b12e7f8Sdrh         db->lookaside.anStat[0]++;
668633e6d57Sdrh         if( db->lookaside.nOut>db->lookaside.mxOut ){
669633e6d57Sdrh           db->lookaside.mxOut = db->lookaside.nOut;
670633e6d57Sdrh         }
671633e6d57Sdrh         return (void*)pBuf;
672633e6d57Sdrh       }
673633e6d57Sdrh     }
6740b12e7f8Sdrh   }
675ddecae79Sdrh #else
676ddecae79Sdrh   if( db && db->mallocFailed ){
677ddecae79Sdrh     return 0;
678ddecae79Sdrh   }
6794150ebf8Sdrh #endif
680fec00eabSdrh   p = sqlite3Malloc(n);
681f3a65f7eSdrh   if( !p && db ){
68217435752Sdrh     db->mallocFailed = 1;
68317435752Sdrh   }
684d231aa3aSdrh   sqlite3MemdebugSetType(p,
685d231aa3aSdrh          (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
68617435752Sdrh   return p;
68717435752Sdrh }
68817435752Sdrh 
68926783a58Sdanielk1977 /*
69026783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
69126783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
69226783a58Sdanielk1977 */
693da4ca9d1Sdrh void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
694a1644fd8Sdanielk1977   void *pNew = 0;
695d9da78a2Sdrh   assert( db!=0 );
6967047e25cSdrh   assert( sqlite3_mutex_held(db->mutex) );
697a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
698633e6d57Sdrh     if( p==0 ){
699633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
700633e6d57Sdrh     }
701633e6d57Sdrh     if( isLookaside(db, p) ){
702633e6d57Sdrh       if( n<=db->lookaside.sz ){
703633e6d57Sdrh         return p;
704633e6d57Sdrh       }
705633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
706633e6d57Sdrh       if( pNew ){
707633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
708633e6d57Sdrh         sqlite3DbFree(db, p);
709633e6d57Sdrh       }
710633e6d57Sdrh     }else{
711d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
712d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
713107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
7143329a63aSdrh       pNew = sqlite3_realloc64(p, n);
715a1644fd8Sdanielk1977       if( !pNew ){
716a1644fd8Sdanielk1977         db->mallocFailed = 1;
717a1644fd8Sdanielk1977       }
718d231aa3aSdrh       sqlite3MemdebugSetType(pNew,
719174b9a16Sdrh             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
720a1644fd8Sdanielk1977     }
721633e6d57Sdrh   }
722a1644fd8Sdanielk1977   return pNew;
723a1644fd8Sdanielk1977 }
724a1644fd8Sdanielk1977 
72517435752Sdrh /*
72617435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
72717435752Sdrh ** and set the mallocFailed flag in the database connection.
72817435752Sdrh */
729da4ca9d1Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
730a3152895Sdrh   void *pNew;
731a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
732a3152895Sdrh   if( !pNew ){
733633e6d57Sdrh     sqlite3DbFree(db, p);
734a3152895Sdrh   }
735a3152895Sdrh   return pNew;
736a3152895Sdrh }
737a3152895Sdrh 
738a3152895Sdrh /*
739a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
740a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
741a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
742a3152895Sdrh ** called via macros that record the current file and line number in the
743a3152895Sdrh ** ThreadData structure.
744a3152895Sdrh */
745633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
746a3152895Sdrh   char *zNew;
747633e6d57Sdrh   size_t n;
748633e6d57Sdrh   if( z==0 ){
749633e6d57Sdrh     return 0;
750a3152895Sdrh   }
751dee0e404Sdrh   n = sqlite3Strlen30(z) + 1;
752633e6d57Sdrh   assert( (n&0x7fffffff)==n );
753633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
754a3152895Sdrh   if( zNew ){
755a3152895Sdrh     memcpy(zNew, z, n);
7561e536953Sdanielk1977   }
7571e536953Sdanielk1977   return zNew;
7581e536953Sdanielk1977 }
759da4ca9d1Sdrh char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
760633e6d57Sdrh   char *zNew;
761633e6d57Sdrh   if( z==0 ){
762633e6d57Sdrh     return 0;
763633e6d57Sdrh   }
764633e6d57Sdrh   assert( (n&0x7fffffff)==n );
765633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
766633e6d57Sdrh   if( zNew ){
76720f3df04Sdrh     memcpy(zNew, z, (size_t)n);
768633e6d57Sdrh     zNew[n] = 0;
7691e536953Sdanielk1977   }
7701e536953Sdanielk1977   return zNew;
7711e536953Sdanielk1977 }
7721e536953Sdanielk1977 
773a3152895Sdrh /*
77422c17b8bSdrh ** Free any prior content in *pz and replace it with a copy of zNew.
775a3152895Sdrh */
77622c17b8bSdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
777633e6d57Sdrh   sqlite3DbFree(db, *pz);
77822c17b8bSdrh   *pz = sqlite3DbStrDup(db, zNew);
779a3152895Sdrh }
780a3152895Sdrh 
781b50c65d5Sdrh /*
782b50c65d5Sdrh ** Take actions at the end of an API call to indicate an OOM error
783b50c65d5Sdrh */
784b50c65d5Sdrh static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
785b50c65d5Sdrh   db->mallocFailed = 0;
786b50c65d5Sdrh   sqlite3Error(db, SQLITE_NOMEM);
787b50c65d5Sdrh   return SQLITE_NOMEM;
788b50c65d5Sdrh }
789a3152895Sdrh 
790a3152895Sdrh /*
791a3152895Sdrh ** This function must be called before exiting any API function (i.e.
79217435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
79317435752Sdrh ** sqlite3_realloc.
794a3152895Sdrh **
795a3152895Sdrh ** The returned value is normally a copy of the second argument to this
796be217793Sshane ** function. However, if a malloc() failure has occurred since the previous
797a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
798a3152895Sdrh **
799597d2b64Sdrh ** If an OOM as occurred, then the connection error-code (the value
800597d2b64Sdrh ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
801a3152895Sdrh */
802a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
803597d2b64Sdrh   /* If the db handle must hold the connection handle mutex here.
804597d2b64Sdrh   ** Otherwise the read (and possible write) of db->mallocFailed
805a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
806a1644fd8Sdanielk1977   */
807597d2b64Sdrh   assert( db!=0 );
808597d2b64Sdrh   assert( sqlite3_mutex_held(db->mutex) );
809b50c65d5Sdrh   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
810b50c65d5Sdrh     return apiOomError(db);
811a3152895Sdrh   }
812b50c65d5Sdrh   return rc & db->errMask;
813a3152895Sdrh }
814