xref: /sqlite-3.40.0/src/malloc.c (revision d231aa3a)
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 /*
79f82ccf64Sdrh ** This routine runs when the memory allocator sees that the
80f82ccf64Sdrh ** total memory allocation is about to exceed the soft heap
81f82ccf64Sdrh ** limit.
82f82ccf64Sdrh */
83f82ccf64Sdrh static void softHeapLimitEnforcer(
84f82ccf64Sdrh   void *NotUsed,
85f82ccf64Sdrh   sqlite3_int64 NotUsed2,
86f82ccf64Sdrh   int allocSize
87f82ccf64Sdrh ){
88f82ccf64Sdrh   UNUSED_PARAMETER2(NotUsed, NotUsed2);
89f82ccf64Sdrh   sqlite3_release_memory(allocSize);
90f82ccf64Sdrh }
91f82ccf64Sdrh 
92f82ccf64Sdrh /*
93f82ccf64Sdrh ** Change the alarm callback
94f82ccf64Sdrh */
95f82ccf64Sdrh static int sqlite3MemoryAlarm(
96f82ccf64Sdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
97f82ccf64Sdrh   void *pArg,
98f82ccf64Sdrh   sqlite3_int64 iThreshold
99f82ccf64Sdrh ){
100f82ccf64Sdrh   int nUsed;
101f82ccf64Sdrh   sqlite3_mutex_enter(mem0.mutex);
102f82ccf64Sdrh   mem0.alarmCallback = xCallback;
103f82ccf64Sdrh   mem0.alarmArg = pArg;
104f82ccf64Sdrh   mem0.alarmThreshold = iThreshold;
105f82ccf64Sdrh   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
106f82ccf64Sdrh   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
107f82ccf64Sdrh   sqlite3_mutex_leave(mem0.mutex);
108f82ccf64Sdrh   return SQLITE_OK;
109f82ccf64Sdrh }
110f82ccf64Sdrh 
111f82ccf64Sdrh #ifndef SQLITE_OMIT_DEPRECATED
112f82ccf64Sdrh /*
113f82ccf64Sdrh ** Deprecated external interface.  Internal/core SQLite code
114f82ccf64Sdrh ** should call sqlite3MemoryAlarm.
115f82ccf64Sdrh */
116f82ccf64Sdrh int sqlite3_memory_alarm(
117f82ccf64Sdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
118f82ccf64Sdrh   void *pArg,
119f82ccf64Sdrh   sqlite3_int64 iThreshold
120f82ccf64Sdrh ){
121f82ccf64Sdrh   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
122f82ccf64Sdrh }
123f82ccf64Sdrh #endif
124f82ccf64Sdrh 
125f82ccf64Sdrh /*
126f82ccf64Sdrh ** Set the soft heap-size limit for the library. Passing a zero or
127f82ccf64Sdrh ** negative value indicates no limit.
128f82ccf64Sdrh */
129f82ccf64Sdrh sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
130f82ccf64Sdrh   sqlite3_int64 priorLimit;
131f82ccf64Sdrh   sqlite3_int64 excess;
132f82ccf64Sdrh #ifndef SQLITE_OMIT_AUTOINIT
133de0f1815Sdrh   int rc = sqlite3_initialize();
134de0f1815Sdrh   if( rc ) return -1;
135f82ccf64Sdrh #endif
136f82ccf64Sdrh   sqlite3_mutex_enter(mem0.mutex);
137f82ccf64Sdrh   priorLimit = mem0.alarmThreshold;
138f82ccf64Sdrh   sqlite3_mutex_leave(mem0.mutex);
139f82ccf64Sdrh   if( n<0 ) return priorLimit;
140f82ccf64Sdrh   if( n>0 ){
141f82ccf64Sdrh     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
142f82ccf64Sdrh   }else{
143f82ccf64Sdrh     sqlite3MemoryAlarm(0, 0, 0);
144f82ccf64Sdrh   }
145f82ccf64Sdrh   excess = sqlite3_memory_used() - n;
1464b03f21eSshaneh   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
147f82ccf64Sdrh   return priorLimit;
148f82ccf64Sdrh }
149f82ccf64Sdrh void sqlite3_soft_heap_limit(int n){
150f82ccf64Sdrh   if( n<0 ) n = 0;
151f82ccf64Sdrh   sqlite3_soft_heap_limit64(n);
152f82ccf64Sdrh }
153f82ccf64Sdrh 
154f82ccf64Sdrh /*
155fec00eabSdrh ** Initialize the memory allocation subsystem.
156fec00eabSdrh */
157fec00eabSdrh int sqlite3MallocInit(void){
158075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
159fec00eabSdrh     sqlite3MemSetDefault();
160fec00eabSdrh   }
161fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
162075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
16359f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
164fec00eabSdrh   }
165075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
1667ff2719eSdrh       && sqlite3GlobalConfig.nScratch>0 ){
167badc980aSdrh     int i, n, sz;
168badc980aSdrh     ScratchFreeslot *pSlot;
169badc980aSdrh     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
170badc980aSdrh     sqlite3GlobalConfig.szScratch = sz;
171badc980aSdrh     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
172badc980aSdrh     n = sqlite3GlobalConfig.nScratch;
173badc980aSdrh     mem0.pScratchFree = pSlot;
174badc980aSdrh     mem0.nScratchFree = n;
175badc980aSdrh     for(i=0; i<n-1; i++){
176badc980aSdrh       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
177badc980aSdrh       pSlot = pSlot->pNext;
178badc980aSdrh     }
179badc980aSdrh     pSlot->pNext = 0;
180badc980aSdrh     mem0.pScratchEnd = (void*)&pSlot[1];
1819ac3fe97Sdrh   }else{
182badc980aSdrh     mem0.pScratchEnd = 0;
183075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
184075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
185badc980aSdrh     sqlite3GlobalConfig.nScratch = 0;
1869ac3fe97Sdrh   }
18750d1b5f3Sdrh   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
18850d1b5f3Sdrh       || sqlite3GlobalConfig.nPage<1 ){
189075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
190075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
19150d1b5f3Sdrh     sqlite3GlobalConfig.nPage = 0;
1929ac3fe97Sdrh   }
193075c23afSdanielk1977   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
194fec00eabSdrh }
195fec00eabSdrh 
196fec00eabSdrh /*
19750d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other
19850d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by
19950d1b5f3Sdrh ** sqlite3_soft_heap_limit().
20050d1b5f3Sdrh */
20150d1b5f3Sdrh int sqlite3HeapNearlyFull(void){
20250d1b5f3Sdrh   return mem0.nearlyFull;
20350d1b5f3Sdrh }
20450d1b5f3Sdrh 
20550d1b5f3Sdrh /*
206fec00eabSdrh ** Deinitialize the memory allocation subsystem.
207fec00eabSdrh */
208fec00eabSdrh void sqlite3MallocEnd(void){
2090a549071Sdanielk1977   if( sqlite3GlobalConfig.m.xShutdown ){
210075c23afSdanielk1977     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
2110a549071Sdanielk1977   }
2129ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
213fec00eabSdrh }
214fec00eabSdrh 
215fec00eabSdrh /*
216fec00eabSdrh ** Return the amount of memory currently checked out.
217fec00eabSdrh */
218fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
219f7141990Sdrh   int n, mx;
220c376a198Sdrh   sqlite3_int64 res;
221f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
222c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
223c376a198Sdrh   return res;
224fec00eabSdrh }
225fec00eabSdrh 
226fec00eabSdrh /*
227fec00eabSdrh ** Return the maximum amount of memory that has ever been
228fec00eabSdrh ** checked out since either the beginning of this process
229fec00eabSdrh ** or since the most recent reset.
230fec00eabSdrh */
231fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
232f7141990Sdrh   int n, mx;
233c376a198Sdrh   sqlite3_int64 res;
234f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
2357986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
236c376a198Sdrh   return res;
237fec00eabSdrh }
238fec00eabSdrh 
239fec00eabSdrh /*
240fec00eabSdrh ** Trigger the alarm
241fec00eabSdrh */
242fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
243fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
244fec00eabSdrh   sqlite3_int64 nowUsed;
245fec00eabSdrh   void *pArg;
246e64ca7baSdrh   if( mem0.alarmCallback==0 ) return;
247fec00eabSdrh   xCallback = mem0.alarmCallback;
248f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
249fec00eabSdrh   pArg = mem0.alarmArg;
250e64ca7baSdrh   mem0.alarmCallback = 0;
251fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
252fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
253fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
254e64ca7baSdrh   mem0.alarmCallback = xCallback;
255e64ca7baSdrh   mem0.alarmArg = pArg;
256fec00eabSdrh }
257fec00eabSdrh 
258fec00eabSdrh /*
259f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
260f7141990Sdrh ** lock is already held.
261fec00eabSdrh */
262f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
263fec00eabSdrh   int nFull;
264f7141990Sdrh   void *p;
265f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
266075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
267f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
268f7141990Sdrh   if( mem0.alarmCallback!=0 ){
269f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2708e1bb041Sdrh     if( nUsed >= mem0.alarmThreshold - nFull ){
27150d1b5f3Sdrh       mem0.nearlyFull = 1;
272fec00eabSdrh       sqlite3MallocAlarm(nFull);
27350d1b5f3Sdrh     }else{
27450d1b5f3Sdrh       mem0.nearlyFull = 0;
275fec00eabSdrh     }
276f7141990Sdrh   }
277075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
27850d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
279d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
280fec00eabSdrh     sqlite3MallocAlarm(nFull);
281075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
282fec00eabSdrh   }
28350d1b5f3Sdrh #endif
284c702c7ccSdrh   if( p ){
285c702c7ccSdrh     nFull = sqlite3MallocSize(p);
286c702c7ccSdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
287eafc43b1Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
288c702c7ccSdrh   }
289f7141990Sdrh   *pp = p;
290f7141990Sdrh   return nFull;
291fec00eabSdrh }
292f7141990Sdrh 
293f7141990Sdrh /*
294f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
295f7141990Sdrh ** assumes the memory subsystem has already been initialized.
296f7141990Sdrh */
297da4ca9d1Sdrh void *sqlite3Malloc(u64 n){
298f7141990Sdrh   void *p;
299da4ca9d1Sdrh   if( n==0 || n>=0x7fffff00 ){
300e08ed7e7Sdrh     /* A memory allocation of a number of bytes which is near the maximum
301e08ed7e7Sdrh     ** signed integer value might cause an integer overflow inside of the
302e08ed7e7Sdrh     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
303e08ed7e7Sdrh     ** 255 bytes of overhead.  SQLite itself will never use anything near
304e08ed7e7Sdrh     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
305f7141990Sdrh     p = 0;
306075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
307f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
3083329a63aSdrh     mallocWithAlarm((int)n, &p);
309fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
310fec00eabSdrh   }else{
311da4ca9d1Sdrh     p = sqlite3GlobalConfig.m.xMalloc((int)n);
312fec00eabSdrh   }
3138da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
314fec00eabSdrh   return p;
315fec00eabSdrh }
316fec00eabSdrh 
317fec00eabSdrh /*
318fec00eabSdrh ** This version of the memory allocation is for use by the application.
319fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
320fec00eabSdrh ** allocation.
321fec00eabSdrh */
322fec00eabSdrh void *sqlite3_malloc(int n){
323fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
324fec00eabSdrh   if( sqlite3_initialize() ) return 0;
325fec00eabSdrh #endif
326da4ca9d1Sdrh   return n<=0 ? 0 : sqlite3Malloc(n);
327da4ca9d1Sdrh }
328da4ca9d1Sdrh void *sqlite3_malloc64(sqlite3_uint64 n){
329da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
330da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
331da4ca9d1Sdrh #endif
332fec00eabSdrh   return sqlite3Malloc(n);
333fec00eabSdrh }
334fec00eabSdrh 
335fec00eabSdrh /*
336e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
337facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
338facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
339e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
340e5ae5735Sdrh */
341e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
342facf0307Sdrh static int scratchAllocOut = 0;
343e5ae5735Sdrh #endif
344e5ae5735Sdrh 
345e5ae5735Sdrh 
346e5ae5735Sdrh /*
347e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
348e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
349e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
350e5ae5735Sdrh ** routine is intended to get memory to old large transient data
351e5ae5735Sdrh ** structures that would not normally fit on the stack of an
352e5ae5735Sdrh ** embedded processor.
353e5ae5735Sdrh */
354facf0307Sdrh void *sqlite3ScratchMalloc(int n){
355e5ae5735Sdrh   void *p;
356e5ae5735Sdrh   assert( n>0 );
3579ac3fe97Sdrh 
358badc980aSdrh   sqlite3_mutex_enter(mem0.mutex);
3593ccd5bf8Sdrh   sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
360badc980aSdrh   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
361badc980aSdrh     p = mem0.pScratchFree;
362badc980aSdrh     mem0.pScratchFree = mem0.pScratchFree->pNext;
363badc980aSdrh     mem0.nScratchFree--;
364badc980aSdrh     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
365b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
366badc980aSdrh   }else{
367b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
3683ccd5bf8Sdrh     p = sqlite3Malloc(n);
3693ccd5bf8Sdrh     if( sqlite3GlobalConfig.bMemstat && p ){
3703ccd5bf8Sdrh       sqlite3_mutex_enter(mem0.mutex);
3713ccd5bf8Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
3723ccd5bf8Sdrh       sqlite3_mutex_leave(mem0.mutex);
373badc980aSdrh     }
374badc980aSdrh     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
375badc980aSdrh   }
3761ff6e3abSdrh   assert( sqlite3_mutex_notheld(mem0.mutex) );
377b0c6a888Sdan 
378badc980aSdrh 
379badc980aSdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
380badc980aSdrh   /* Verify that no more than two scratch allocations per thread
381badc980aSdrh   ** are outstanding at one time.  (This is only checked in the
382badc980aSdrh   ** single-threaded case since checking in the multi-threaded case
383badc980aSdrh   ** would be much more complicated.) */
384badc980aSdrh   assert( scratchAllocOut<=1 );
385badc980aSdrh   if( p ) scratchAllocOut++;
386badc980aSdrh #endif
387badc980aSdrh 
388badc980aSdrh   return p;
389badc980aSdrh }
390badc980aSdrh void sqlite3ScratchFree(void *p){
391badc980aSdrh   if( p ){
392badc980aSdrh 
393e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
39437f99187Sdrh     /* Verify that no more than two scratch allocation per thread
3959ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3969ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3979ac3fe97Sdrh     ** would be much more complicated.) */
398badc980aSdrh     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
399badc980aSdrh     scratchAllocOut--;
400e5ae5735Sdrh #endif
4019ac3fe97Sdrh 
402badc980aSdrh     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
403badc980aSdrh       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
404badc980aSdrh       ScratchFreeslot *pSlot;
405badc980aSdrh       pSlot = (ScratchFreeslot*)p;
406e5ae5735Sdrh       sqlite3_mutex_enter(mem0.mutex);
407badc980aSdrh       pSlot->pNext = mem0.pScratchFree;
408badc980aSdrh       mem0.pScratchFree = pSlot;
409badc980aSdrh       mem0.nScratchFree++;
410fcd71b60Sdrh       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
411badc980aSdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
4129ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
413f7141990Sdrh     }else{
414badc980aSdrh       /* Release memory back to the heap */
415107b56e8Sdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
416174b9a16Sdrh       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
417107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
418075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
419f7141990Sdrh         int iSize = sqlite3MallocSize(p);
420f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
421f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
422f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
42381ba7d16Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
424075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
425f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
426f7141990Sdrh       }else{
427075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
428f7141990Sdrh       }
4299ac3fe97Sdrh     }
430e5ae5735Sdrh   }
431e5ae5735Sdrh }
432e5ae5735Sdrh 
433e5ae5735Sdrh /*
434633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
435633e6d57Sdrh */
4364150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
437633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
438b0e7704eSdrh   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
439633e6d57Sdrh }
4404150ebf8Sdrh #else
4414150ebf8Sdrh #define isLookaside(A,B) 0
4424150ebf8Sdrh #endif
443633e6d57Sdrh 
444633e6d57Sdrh /*
445fec00eabSdrh ** Return the size of a memory allocation previously obtained from
446fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
447fec00eabSdrh */
448fec00eabSdrh int sqlite3MallocSize(void *p){
449107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
450075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
451fec00eabSdrh }
452633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
45317bcb102Sdrh   if( db==0 ){
454*d231aa3aSdrh     assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
455*d231aa3aSdrh     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
45617bcb102Sdrh     return sqlite3MallocSize(p);
45717bcb102Sdrh   }else{
458b0e7704eSdrh     assert( sqlite3_mutex_held(db->mutex) );
459b0e7704eSdrh     if( isLookaside(db, p) ){
460633e6d57Sdrh       return db->lookaside.sz;
461633e6d57Sdrh     }else{
462*d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
463*d231aa3aSdrh       assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
464075c23afSdanielk1977       return sqlite3GlobalConfig.m.xSize(p);
465633e6d57Sdrh     }
466633e6d57Sdrh   }
46717bcb102Sdrh }
468da4ca9d1Sdrh sqlite3_uint64 sqlite3_msize(void *p){
469*d231aa3aSdrh   assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
470*d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
471da4ca9d1Sdrh   return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
472da4ca9d1Sdrh }
473fec00eabSdrh 
474fec00eabSdrh /*
475fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
476fec00eabSdrh */
477fec00eabSdrh void sqlite3_free(void *p){
47871a1a0f4Sdrh   if( p==0 ) return;  /* IMP: R-49053-54554 */
479107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
480*d231aa3aSdrh   assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
481075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
482fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
483f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
484eafc43b1Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
485075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
486fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
487fec00eabSdrh   }else{
488075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
489fec00eabSdrh   }
490fec00eabSdrh }
491fec00eabSdrh 
492fec00eabSdrh /*
493b4586f12Sdrh ** Add the size of memory allocation "p" to the count in
494b4586f12Sdrh ** *db->pnBytesFreed.
495b4586f12Sdrh */
496b4586f12Sdrh static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
497b4586f12Sdrh   *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
498b4586f12Sdrh }
499b4586f12Sdrh 
500b4586f12Sdrh /*
501633e6d57Sdrh ** Free memory that might be associated with a particular database
502633e6d57Sdrh ** connection.
503633e6d57Sdrh */
504633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
5057047e25cSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
5069ccd8659Sdrh   if( p==0 ) return;
507174b9a16Sdrh   if( db ){
508174b9a16Sdrh     if( db->pnBytesFreed ){
509b4586f12Sdrh       measureAllocationSize(db, p);
510174b9a16Sdrh       return;
511d46def77Sdan     }
512633e6d57Sdrh     if( isLookaside(db, p) ){
513633e6d57Sdrh       LookasideSlot *pBuf = (LookasideSlot*)p;
5143608f177Sdrh #if SQLITE_DEBUG
5153608f177Sdrh       /* Trash all content in the buffer being freed */
5163608f177Sdrh       memset(p, 0xaa, db->lookaside.sz);
5173608f177Sdrh #endif
518633e6d57Sdrh       pBuf->pNext = db->lookaside.pFree;
519633e6d57Sdrh       db->lookaside.pFree = pBuf;
520633e6d57Sdrh       db->lookaside.nOut--;
521174b9a16Sdrh       return;
522174b9a16Sdrh     }
523174b9a16Sdrh   }
524*d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
525*d231aa3aSdrh   assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
526174b9a16Sdrh   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
527107b56e8Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
528633e6d57Sdrh   sqlite3_free(p);
529633e6d57Sdrh }
530633e6d57Sdrh 
531633e6d57Sdrh /*
532fec00eabSdrh ** Change the size of an existing memory allocation
533fec00eabSdrh */
534da4ca9d1Sdrh void *sqlite3Realloc(void *pOld, u64 nBytes){
535ca591febSshaneh   int nOld, nNew, nDiff;
536fec00eabSdrh   void *pNew;
537*d231aa3aSdrh   assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
538*d231aa3aSdrh   assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
539fec00eabSdrh   if( pOld==0 ){
5408da47419Sdrh     return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
541fec00eabSdrh   }
542da4ca9d1Sdrh   if( nBytes==0 ){
5438da47419Sdrh     sqlite3_free(pOld); /* IMP: R-26507-47431 */
544fec00eabSdrh     return 0;
545fec00eabSdrh   }
546b6063cf8Sdrh   if( nBytes>=0x7fffff00 ){
547b6063cf8Sdrh     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
548b6063cf8Sdrh     return 0;
549b6063cf8Sdrh   }
550fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
5519f129f46Sdrh   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
5529f129f46Sdrh   ** argument to xRealloc is always a value returned by a prior call to
5539f129f46Sdrh   ** xRoundup. */
554da4ca9d1Sdrh   nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
555fec00eabSdrh   if( nOld==nNew ){
556fec00eabSdrh     pNew = pOld;
5577c6791c8Sdrh   }else if( sqlite3GlobalConfig.bMemstat ){
5587c6791c8Sdrh     sqlite3_mutex_enter(mem0.mutex);
5593329a63aSdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
5608e1bb041Sdrh     nDiff = nNew - nOld;
5618e1bb041Sdrh     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
5628e1bb041Sdrh           mem0.alarmThreshold-nDiff ){
5632e5a422eSdrh       sqlite3MallocAlarm(nDiff);
564fec00eabSdrh     }
565075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
566d09414cdSdanielk1977     if( pNew==0 && mem0.alarmCallback ){
5673329a63aSdrh       sqlite3MallocAlarm((int)nBytes);
568075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
569fec00eabSdrh     }
570fec00eabSdrh     if( pNew ){
571c702c7ccSdrh       nNew = sqlite3MallocSize(pNew);
5722e5a422eSdrh       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
573fec00eabSdrh     }
574fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
575fec00eabSdrh   }else{
5767c6791c8Sdrh     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
577fec00eabSdrh   }
5788da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
579fec00eabSdrh   return pNew;
580fec00eabSdrh }
581fec00eabSdrh 
582fec00eabSdrh /*
583fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
584fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
585fec00eabSdrh */
586fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
587fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
588fec00eabSdrh   if( sqlite3_initialize() ) return 0;
589fec00eabSdrh #endif
5908da47419Sdrh   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
591da4ca9d1Sdrh   return sqlite3Realloc(pOld, n);
592da4ca9d1Sdrh }
593da4ca9d1Sdrh void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
594da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
595da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
596da4ca9d1Sdrh #endif
597fec00eabSdrh   return sqlite3Realloc(pOld, n);
598fec00eabSdrh }
599fec00eabSdrh 
600a3152895Sdrh 
601a3152895Sdrh /*
60217435752Sdrh ** Allocate and zero memory.
603a3152895Sdrh */
604da4ca9d1Sdrh void *sqlite3MallocZero(u64 n){
605fec00eabSdrh   void *p = sqlite3Malloc(n);
606a3152895Sdrh   if( p ){
60720f3df04Sdrh     memset(p, 0, (size_t)n);
608a3152895Sdrh   }
609a3152895Sdrh   return p;
610a3152895Sdrh }
61117435752Sdrh 
61217435752Sdrh /*
61317435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
61417435752Sdrh ** the mallocFailed flag in the connection pointer.
61517435752Sdrh */
616da4ca9d1Sdrh void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
617a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
61817435752Sdrh   if( p ){
61920f3df04Sdrh     memset(p, 0, (size_t)n);
62017435752Sdrh   }
62117435752Sdrh   return p;
62217435752Sdrh }
62317435752Sdrh 
62417435752Sdrh /*
62517435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
62617435752Sdrh ** the mallocFailed flag in the connection pointer.
627ddecae79Sdrh **
628ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
629ddecae79Sdrh ** failure on the same database connection) then always return 0.
630ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
631ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
632ddecae79Sdrh ** This is an important assumption.  There are many places in the
633ddecae79Sdrh ** code that do things like this:
634ddecae79Sdrh **
635ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
636ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
637ddecae79Sdrh **         if( b ) a[10] = 9;
638ddecae79Sdrh **
639ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
640ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
64117435752Sdrh */
642da4ca9d1Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
643633e6d57Sdrh   void *p;
644d9da78a2Sdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
645ccd4ad3eSdan   assert( db==0 || db->pnBytesFreed==0 );
6464150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
647633e6d57Sdrh   if( db ){
648633e6d57Sdrh     LookasideSlot *pBuf;
649633e6d57Sdrh     if( db->mallocFailed ){
650633e6d57Sdrh       return 0;
651633e6d57Sdrh     }
6520b12e7f8Sdrh     if( db->lookaside.bEnabled ){
6530b12e7f8Sdrh       if( n>db->lookaside.sz ){
6540b12e7f8Sdrh         db->lookaside.anStat[1]++;
6550b12e7f8Sdrh       }else if( (pBuf = db->lookaside.pFree)==0 ){
6560b12e7f8Sdrh         db->lookaside.anStat[2]++;
6570b12e7f8Sdrh       }else{
658633e6d57Sdrh         db->lookaside.pFree = pBuf->pNext;
659633e6d57Sdrh         db->lookaside.nOut++;
6600b12e7f8Sdrh         db->lookaside.anStat[0]++;
661633e6d57Sdrh         if( db->lookaside.nOut>db->lookaside.mxOut ){
662633e6d57Sdrh           db->lookaside.mxOut = db->lookaside.nOut;
663633e6d57Sdrh         }
664633e6d57Sdrh         return (void*)pBuf;
665633e6d57Sdrh       }
666633e6d57Sdrh     }
6670b12e7f8Sdrh   }
668ddecae79Sdrh #else
669ddecae79Sdrh   if( db && db->mallocFailed ){
670ddecae79Sdrh     return 0;
671ddecae79Sdrh   }
6724150ebf8Sdrh #endif
673fec00eabSdrh   p = sqlite3Malloc(n);
674f3a65f7eSdrh   if( !p && db ){
67517435752Sdrh     db->mallocFailed = 1;
67617435752Sdrh   }
677*d231aa3aSdrh   sqlite3MemdebugSetType(p,
678*d231aa3aSdrh          (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
67917435752Sdrh   return p;
68017435752Sdrh }
68117435752Sdrh 
68226783a58Sdanielk1977 /*
68326783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
68426783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
68526783a58Sdanielk1977 */
686da4ca9d1Sdrh void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
687a1644fd8Sdanielk1977   void *pNew = 0;
688d9da78a2Sdrh   assert( db!=0 );
6897047e25cSdrh   assert( sqlite3_mutex_held(db->mutex) );
690a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
691633e6d57Sdrh     if( p==0 ){
692633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
693633e6d57Sdrh     }
694633e6d57Sdrh     if( isLookaside(db, p) ){
695633e6d57Sdrh       if( n<=db->lookaside.sz ){
696633e6d57Sdrh         return p;
697633e6d57Sdrh       }
698633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
699633e6d57Sdrh       if( pNew ){
700633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
701633e6d57Sdrh         sqlite3DbFree(db, p);
702633e6d57Sdrh       }
703633e6d57Sdrh     }else{
704*d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
705*d231aa3aSdrh       assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
706107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
7073329a63aSdrh       pNew = sqlite3_realloc64(p, n);
708a1644fd8Sdanielk1977       if( !pNew ){
709a1644fd8Sdanielk1977         db->mallocFailed = 1;
710a1644fd8Sdanielk1977       }
711*d231aa3aSdrh       sqlite3MemdebugSetType(pNew,
712174b9a16Sdrh             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
713a1644fd8Sdanielk1977     }
714633e6d57Sdrh   }
715a1644fd8Sdanielk1977   return pNew;
716a1644fd8Sdanielk1977 }
717a1644fd8Sdanielk1977 
71817435752Sdrh /*
71917435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
72017435752Sdrh ** and set the mallocFailed flag in the database connection.
72117435752Sdrh */
722da4ca9d1Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
723a3152895Sdrh   void *pNew;
724a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
725a3152895Sdrh   if( !pNew ){
726633e6d57Sdrh     sqlite3DbFree(db, p);
727a3152895Sdrh   }
728a3152895Sdrh   return pNew;
729a3152895Sdrh }
730a3152895Sdrh 
731a3152895Sdrh /*
732a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
733a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
734a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
735a3152895Sdrh ** called via macros that record the current file and line number in the
736a3152895Sdrh ** ThreadData structure.
737a3152895Sdrh */
738633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
739a3152895Sdrh   char *zNew;
740633e6d57Sdrh   size_t n;
741633e6d57Sdrh   if( z==0 ){
742633e6d57Sdrh     return 0;
743a3152895Sdrh   }
744dee0e404Sdrh   n = sqlite3Strlen30(z) + 1;
745633e6d57Sdrh   assert( (n&0x7fffffff)==n );
746633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
747a3152895Sdrh   if( zNew ){
748a3152895Sdrh     memcpy(zNew, z, n);
7491e536953Sdanielk1977   }
7501e536953Sdanielk1977   return zNew;
7511e536953Sdanielk1977 }
752da4ca9d1Sdrh char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
753633e6d57Sdrh   char *zNew;
754633e6d57Sdrh   if( z==0 ){
755633e6d57Sdrh     return 0;
756633e6d57Sdrh   }
757633e6d57Sdrh   assert( (n&0x7fffffff)==n );
758633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
759633e6d57Sdrh   if( zNew ){
76020f3df04Sdrh     memcpy(zNew, z, (size_t)n);
761633e6d57Sdrh     zNew[n] = 0;
7621e536953Sdanielk1977   }
7631e536953Sdanielk1977   return zNew;
7641e536953Sdanielk1977 }
7651e536953Sdanielk1977 
766a3152895Sdrh /*
767f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
768f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
769f089aa45Sdrh ** point to that string.
770a3152895Sdrh */
771f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
772a3152895Sdrh   va_list ap;
773f089aa45Sdrh   char *z;
774a3152895Sdrh 
775f089aa45Sdrh   va_start(ap, zFormat);
776f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
777a3152895Sdrh   va_end(ap);
778633e6d57Sdrh   sqlite3DbFree(db, *pz);
779f089aa45Sdrh   *pz = z;
780a3152895Sdrh }
781a3152895Sdrh 
782b50c65d5Sdrh /*
783b50c65d5Sdrh ** Take actions at the end of an API call to indicate an OOM error
784b50c65d5Sdrh */
785b50c65d5Sdrh static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
786b50c65d5Sdrh   db->mallocFailed = 0;
787b50c65d5Sdrh   sqlite3Error(db, SQLITE_NOMEM);
788b50c65d5Sdrh   return SQLITE_NOMEM;
789b50c65d5Sdrh }
790a3152895Sdrh 
791a3152895Sdrh /*
792a3152895Sdrh ** This function must be called before exiting any API function (i.e.
79317435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
79417435752Sdrh ** sqlite3_realloc.
795a3152895Sdrh **
796a3152895Sdrh ** The returned value is normally a copy of the second argument to this
797be217793Sshane ** function. However, if a malloc() failure has occurred since the previous
798a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
799a3152895Sdrh **
800be217793Sshane ** If the first argument, db, is not NULL and a malloc() error has occurred,
801a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
802a3152895Sdrh ** is set to SQLITE_NOMEM.
803a3152895Sdrh */
804a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
805a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
806a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
807a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
808a1644fd8Sdanielk1977   */
809a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
810b50c65d5Sdrh   if( db==0 ) return rc & 0xff;
811b50c65d5Sdrh   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
812b50c65d5Sdrh     return apiOomError(db);
813a3152895Sdrh   }
814b50c65d5Sdrh   return rc & db->errMask;
815a3152895Sdrh }
816