xref: /sqlite-3.40.0/src/malloc.c (revision af89fe66)
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 /*
79*af89fe66Sdrh ** Return the memory allocator mutex. sqlite3_status() needs it.
80*af89fe66Sdrh */
81*af89fe66Sdrh sqlite3_mutex *sqlite3MallocMutex(void){
82*af89fe66Sdrh   return mem0.mutex;
83*af89fe66Sdrh }
84*af89fe66Sdrh 
85*af89fe66Sdrh /*
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 ){
107*af89fe66Sdrh   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){
165075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
166fec00eabSdrh     sqlite3MemSetDefault();
167fec00eabSdrh   }
168fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
169075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
17059f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
171fec00eabSdrh   }
172075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
1737ff2719eSdrh       && sqlite3GlobalConfig.nScratch>0 ){
174badc980aSdrh     int i, n, sz;
175badc980aSdrh     ScratchFreeslot *pSlot;
176badc980aSdrh     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
177badc980aSdrh     sqlite3GlobalConfig.szScratch = sz;
178badc980aSdrh     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
179badc980aSdrh     n = sqlite3GlobalConfig.nScratch;
180badc980aSdrh     mem0.pScratchFree = pSlot;
181badc980aSdrh     mem0.nScratchFree = n;
182badc980aSdrh     for(i=0; i<n-1; i++){
183badc980aSdrh       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
184badc980aSdrh       pSlot = pSlot->pNext;
185badc980aSdrh     }
186badc980aSdrh     pSlot->pNext = 0;
187badc980aSdrh     mem0.pScratchEnd = (void*)&pSlot[1];
1889ac3fe97Sdrh   }else{
189badc980aSdrh     mem0.pScratchEnd = 0;
190075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
191075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
192badc980aSdrh     sqlite3GlobalConfig.nScratch = 0;
1939ac3fe97Sdrh   }
19450d1b5f3Sdrh   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
19550d1b5f3Sdrh       || sqlite3GlobalConfig.nPage<1 ){
196075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
197075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
19850d1b5f3Sdrh     sqlite3GlobalConfig.nPage = 0;
1999ac3fe97Sdrh   }
200075c23afSdanielk1977   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
201fec00eabSdrh }
202fec00eabSdrh 
203fec00eabSdrh /*
20450d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other
20550d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by
20650d1b5f3Sdrh ** sqlite3_soft_heap_limit().
20750d1b5f3Sdrh */
20850d1b5f3Sdrh int sqlite3HeapNearlyFull(void){
20950d1b5f3Sdrh   return mem0.nearlyFull;
21050d1b5f3Sdrh }
21150d1b5f3Sdrh 
21250d1b5f3Sdrh /*
213fec00eabSdrh ** Deinitialize the memory allocation subsystem.
214fec00eabSdrh */
215fec00eabSdrh void sqlite3MallocEnd(void){
2160a549071Sdanielk1977   if( sqlite3GlobalConfig.m.xShutdown ){
217075c23afSdanielk1977     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
2180a549071Sdanielk1977   }
2199ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
220fec00eabSdrh }
221fec00eabSdrh 
222fec00eabSdrh /*
223fec00eabSdrh ** Return the amount of memory currently checked out.
224fec00eabSdrh */
225fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
226f7141990Sdrh   int n, mx;
227c376a198Sdrh   sqlite3_int64 res;
228f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
229c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
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){
239f7141990Sdrh   int n, mx;
240c376a198Sdrh   sqlite3_int64 res;
241f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
2427986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
243c376a198Sdrh   return res;
244fec00eabSdrh }
245fec00eabSdrh 
246fec00eabSdrh /*
247fec00eabSdrh ** Trigger the alarm
248fec00eabSdrh */
249fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
250fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
251fec00eabSdrh   sqlite3_int64 nowUsed;
252fec00eabSdrh   void *pArg;
253e64ca7baSdrh   if( mem0.alarmCallback==0 ) return;
254fec00eabSdrh   xCallback = mem0.alarmCallback;
255f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
256fec00eabSdrh   pArg = mem0.alarmArg;
257e64ca7baSdrh   mem0.alarmCallback = 0;
258fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
259fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
260fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
261e64ca7baSdrh   mem0.alarmCallback = xCallback;
262e64ca7baSdrh   mem0.alarmArg = pArg;
263fec00eabSdrh }
264fec00eabSdrh 
265fec00eabSdrh /*
266f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
267f7141990Sdrh ** lock is already held.
268fec00eabSdrh */
269f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
270fec00eabSdrh   int nFull;
271f7141990Sdrh   void *p;
272f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
273075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
274f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
275f7141990Sdrh   if( mem0.alarmCallback!=0 ){
276*af89fe66Sdrh     sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2778e1bb041Sdrh     if( nUsed >= mem0.alarmThreshold - nFull ){
27850d1b5f3Sdrh       mem0.nearlyFull = 1;
279fec00eabSdrh       sqlite3MallocAlarm(nFull);
28050d1b5f3Sdrh     }else{
28150d1b5f3Sdrh       mem0.nearlyFull = 0;
282fec00eabSdrh     }
283f7141990Sdrh   }
284075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
28550d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
286d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
287fec00eabSdrh     sqlite3MallocAlarm(nFull);
288075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
289fec00eabSdrh   }
29050d1b5f3Sdrh #endif
291c702c7ccSdrh   if( p ){
292c702c7ccSdrh     nFull = sqlite3MallocSize(p);
293*af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
294*af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
295c702c7ccSdrh   }
296f7141990Sdrh   *pp = p;
297f7141990Sdrh   return nFull;
298fec00eabSdrh }
299f7141990Sdrh 
300f7141990Sdrh /*
301f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
302f7141990Sdrh ** assumes the memory subsystem has already been initialized.
303f7141990Sdrh */
304da4ca9d1Sdrh void *sqlite3Malloc(u64 n){
305f7141990Sdrh   void *p;
306da4ca9d1Sdrh   if( n==0 || n>=0x7fffff00 ){
307e08ed7e7Sdrh     /* A memory allocation of a number of bytes which is near the maximum
308e08ed7e7Sdrh     ** signed integer value might cause an integer overflow inside of the
309e08ed7e7Sdrh     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
310e08ed7e7Sdrh     ** 255 bytes of overhead.  SQLite itself will never use anything near
311e08ed7e7Sdrh     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
312f7141990Sdrh     p = 0;
313075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
314f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
3153329a63aSdrh     mallocWithAlarm((int)n, &p);
316fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
317fec00eabSdrh   }else{
318da4ca9d1Sdrh     p = sqlite3GlobalConfig.m.xMalloc((int)n);
319fec00eabSdrh   }
3208da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
321fec00eabSdrh   return p;
322fec00eabSdrh }
323fec00eabSdrh 
324fec00eabSdrh /*
325fec00eabSdrh ** This version of the memory allocation is for use by the application.
326fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
327fec00eabSdrh ** allocation.
328fec00eabSdrh */
329fec00eabSdrh void *sqlite3_malloc(int n){
330fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
331fec00eabSdrh   if( sqlite3_initialize() ) return 0;
332fec00eabSdrh #endif
333da4ca9d1Sdrh   return n<=0 ? 0 : sqlite3Malloc(n);
334da4ca9d1Sdrh }
335da4ca9d1Sdrh void *sqlite3_malloc64(sqlite3_uint64 n){
336da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
337da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
338da4ca9d1Sdrh #endif
339fec00eabSdrh   return sqlite3Malloc(n);
340fec00eabSdrh }
341fec00eabSdrh 
342fec00eabSdrh /*
343e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
344facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
345facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
346e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
347e5ae5735Sdrh */
348e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
349facf0307Sdrh static int scratchAllocOut = 0;
350e5ae5735Sdrh #endif
351e5ae5735Sdrh 
352e5ae5735Sdrh 
353e5ae5735Sdrh /*
354e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
355e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
356e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
357e5ae5735Sdrh ** routine is intended to get memory to old large transient data
358e5ae5735Sdrh ** structures that would not normally fit on the stack of an
359e5ae5735Sdrh ** embedded processor.
360e5ae5735Sdrh */
361facf0307Sdrh void *sqlite3ScratchMalloc(int n){
362e5ae5735Sdrh   void *p;
363e5ae5735Sdrh   assert( n>0 );
3649ac3fe97Sdrh 
365badc980aSdrh   sqlite3_mutex_enter(mem0.mutex);
3663ccd5bf8Sdrh   sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
367badc980aSdrh   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
368badc980aSdrh     p = mem0.pScratchFree;
369badc980aSdrh     mem0.pScratchFree = mem0.pScratchFree->pNext;
370badc980aSdrh     mem0.nScratchFree--;
371*af89fe66Sdrh     sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
372b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
373badc980aSdrh   }else{
374b0c6a888Sdan     sqlite3_mutex_leave(mem0.mutex);
3753ccd5bf8Sdrh     p = sqlite3Malloc(n);
3763ccd5bf8Sdrh     if( sqlite3GlobalConfig.bMemstat && p ){
3773ccd5bf8Sdrh       sqlite3_mutex_enter(mem0.mutex);
378*af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
3793ccd5bf8Sdrh       sqlite3_mutex_leave(mem0.mutex);
380badc980aSdrh     }
381badc980aSdrh     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
382badc980aSdrh   }
3831ff6e3abSdrh   assert( sqlite3_mutex_notheld(mem0.mutex) );
384b0c6a888Sdan 
385badc980aSdrh 
386badc980aSdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
387cbd55b03Sdrh   /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
388cbd55b03Sdrh   ** buffers per thread.
389cbd55b03Sdrh   **
390cbd55b03Sdrh   ** This can only be checked in single-threaded mode.
391cbd55b03Sdrh   */
392cbd55b03Sdrh   assert( scratchAllocOut==0 );
393badc980aSdrh   if( p ) scratchAllocOut++;
394badc980aSdrh #endif
395badc980aSdrh 
396badc980aSdrh   return p;
397badc980aSdrh }
398badc980aSdrh void sqlite3ScratchFree(void *p){
399badc980aSdrh   if( p ){
400badc980aSdrh 
401e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
40237f99187Sdrh     /* Verify that no more than two scratch allocation per thread
4039ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
4049ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
4059ac3fe97Sdrh     ** would be much more complicated.) */
406badc980aSdrh     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
407badc980aSdrh     scratchAllocOut--;
408e5ae5735Sdrh #endif
4099ac3fe97Sdrh 
410badc980aSdrh     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
411badc980aSdrh       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
412badc980aSdrh       ScratchFreeslot *pSlot;
413badc980aSdrh       pSlot = (ScratchFreeslot*)p;
414e5ae5735Sdrh       sqlite3_mutex_enter(mem0.mutex);
415badc980aSdrh       pSlot->pNext = mem0.pScratchFree;
416badc980aSdrh       mem0.pScratchFree = pSlot;
417badc980aSdrh       mem0.nScratchFree++;
418fcd71b60Sdrh       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
419*af89fe66Sdrh       sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
4209ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
421f7141990Sdrh     }else{
422badc980aSdrh       /* Release memory back to the heap */
423107b56e8Sdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
424d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
425107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
426075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
427f7141990Sdrh         int iSize = sqlite3MallocSize(p);
428f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
429*af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
430*af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
431*af89fe66Sdrh         sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
432075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
433f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
434f7141990Sdrh       }else{
435075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
436f7141990Sdrh       }
4379ac3fe97Sdrh     }
438e5ae5735Sdrh   }
439e5ae5735Sdrh }
440e5ae5735Sdrh 
441e5ae5735Sdrh /*
442633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
443633e6d57Sdrh */
4444150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
445633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
446b0e7704eSdrh   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
447633e6d57Sdrh }
4484150ebf8Sdrh #else
4494150ebf8Sdrh #define isLookaside(A,B) 0
4504150ebf8Sdrh #endif
451633e6d57Sdrh 
452633e6d57Sdrh /*
453fec00eabSdrh ** Return the size of a memory allocation previously obtained from
454fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
455fec00eabSdrh */
456fec00eabSdrh int sqlite3MallocSize(void *p){
457107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
458075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
459fec00eabSdrh }
460633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
46117bcb102Sdrh   if( db==0 ){
462d425864dSmistachkin     assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
463d231aa3aSdrh     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
46417bcb102Sdrh     return sqlite3MallocSize(p);
46517bcb102Sdrh   }else{
466b0e7704eSdrh     assert( sqlite3_mutex_held(db->mutex) );
467b0e7704eSdrh     if( isLookaside(db, p) ){
468633e6d57Sdrh       return db->lookaside.sz;
469633e6d57Sdrh     }else{
470d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
471d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
472075c23afSdanielk1977       return sqlite3GlobalConfig.m.xSize(p);
473633e6d57Sdrh     }
474633e6d57Sdrh   }
47517bcb102Sdrh }
476da4ca9d1Sdrh sqlite3_uint64 sqlite3_msize(void *p){
477d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
478d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
479da4ca9d1Sdrh   return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
480da4ca9d1Sdrh }
481fec00eabSdrh 
482fec00eabSdrh /*
483fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
484fec00eabSdrh */
485fec00eabSdrh void sqlite3_free(void *p){
48671a1a0f4Sdrh   if( p==0 ) return;  /* IMP: R-49053-54554 */
487107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
488d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
489075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
490fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
491*af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
492*af89fe66Sdrh     sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
493075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
494fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
495fec00eabSdrh   }else{
496075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
497fec00eabSdrh   }
498fec00eabSdrh }
499fec00eabSdrh 
500fec00eabSdrh /*
501b4586f12Sdrh ** Add the size of memory allocation "p" to the count in
502b4586f12Sdrh ** *db->pnBytesFreed.
503b4586f12Sdrh */
504b4586f12Sdrh static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
505b4586f12Sdrh   *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
506b4586f12Sdrh }
507b4586f12Sdrh 
508b4586f12Sdrh /*
509633e6d57Sdrh ** Free memory that might be associated with a particular database
510633e6d57Sdrh ** connection.
511633e6d57Sdrh */
512633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
5137047e25cSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
5149ccd8659Sdrh   if( p==0 ) return;
515174b9a16Sdrh   if( db ){
516174b9a16Sdrh     if( db->pnBytesFreed ){
517b4586f12Sdrh       measureAllocationSize(db, p);
518174b9a16Sdrh       return;
519d46def77Sdan     }
520633e6d57Sdrh     if( isLookaside(db, p) ){
521633e6d57Sdrh       LookasideSlot *pBuf = (LookasideSlot*)p;
5223608f177Sdrh #if SQLITE_DEBUG
5233608f177Sdrh       /* Trash all content in the buffer being freed */
5243608f177Sdrh       memset(p, 0xaa, db->lookaside.sz);
5253608f177Sdrh #endif
526633e6d57Sdrh       pBuf->pNext = db->lookaside.pFree;
527633e6d57Sdrh       db->lookaside.pFree = pBuf;
528633e6d57Sdrh       db->lookaside.nOut--;
529174b9a16Sdrh       return;
530174b9a16Sdrh     }
531174b9a16Sdrh   }
532d231aa3aSdrh   assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
533d425864dSmistachkin   assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
534174b9a16Sdrh   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
535107b56e8Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
536633e6d57Sdrh   sqlite3_free(p);
537633e6d57Sdrh }
538633e6d57Sdrh 
539633e6d57Sdrh /*
540fec00eabSdrh ** Change the size of an existing memory allocation
541fec00eabSdrh */
542da4ca9d1Sdrh void *sqlite3Realloc(void *pOld, u64 nBytes){
543ca591febSshaneh   int nOld, nNew, nDiff;
544fec00eabSdrh   void *pNew;
545d231aa3aSdrh   assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
546d425864dSmistachkin   assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
547fec00eabSdrh   if( pOld==0 ){
5488da47419Sdrh     return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
549fec00eabSdrh   }
550da4ca9d1Sdrh   if( nBytes==0 ){
5518da47419Sdrh     sqlite3_free(pOld); /* IMP: R-26507-47431 */
552fec00eabSdrh     return 0;
553fec00eabSdrh   }
554b6063cf8Sdrh   if( nBytes>=0x7fffff00 ){
555b6063cf8Sdrh     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
556b6063cf8Sdrh     return 0;
557b6063cf8Sdrh   }
558fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
5599f129f46Sdrh   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
5609f129f46Sdrh   ** argument to xRealloc is always a value returned by a prior call to
5619f129f46Sdrh   ** xRoundup. */
562da4ca9d1Sdrh   nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
563fec00eabSdrh   if( nOld==nNew ){
564fec00eabSdrh     pNew = pOld;
5657c6791c8Sdrh   }else if( sqlite3GlobalConfig.bMemstat ){
5667c6791c8Sdrh     sqlite3_mutex_enter(mem0.mutex);
5673329a63aSdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
5688e1bb041Sdrh     nDiff = nNew - nOld;
5698e1bb041Sdrh     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
5708e1bb041Sdrh           mem0.alarmThreshold-nDiff ){
5712e5a422eSdrh       sqlite3MallocAlarm(nDiff);
572fec00eabSdrh     }
573075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
574d09414cdSdanielk1977     if( pNew==0 && mem0.alarmCallback ){
5753329a63aSdrh       sqlite3MallocAlarm((int)nBytes);
576075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
577fec00eabSdrh     }
578fec00eabSdrh     if( pNew ){
579c702c7ccSdrh       nNew = sqlite3MallocSize(pNew);
580*af89fe66Sdrh       sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
581fec00eabSdrh     }
582fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
583fec00eabSdrh   }else{
5847c6791c8Sdrh     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
585fec00eabSdrh   }
5868da47419Sdrh   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
587fec00eabSdrh   return pNew;
588fec00eabSdrh }
589fec00eabSdrh 
590fec00eabSdrh /*
591fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
592fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
593fec00eabSdrh */
594fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
595fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
596fec00eabSdrh   if( sqlite3_initialize() ) return 0;
597fec00eabSdrh #endif
5988da47419Sdrh   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
599da4ca9d1Sdrh   return sqlite3Realloc(pOld, n);
600da4ca9d1Sdrh }
601da4ca9d1Sdrh void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
602da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
603da4ca9d1Sdrh   if( sqlite3_initialize() ) return 0;
604da4ca9d1Sdrh #endif
605fec00eabSdrh   return sqlite3Realloc(pOld, n);
606fec00eabSdrh }
607fec00eabSdrh 
608a3152895Sdrh 
609a3152895Sdrh /*
61017435752Sdrh ** Allocate and zero memory.
611a3152895Sdrh */
612da4ca9d1Sdrh void *sqlite3MallocZero(u64 n){
613fec00eabSdrh   void *p = sqlite3Malloc(n);
614a3152895Sdrh   if( p ){
61520f3df04Sdrh     memset(p, 0, (size_t)n);
616a3152895Sdrh   }
617a3152895Sdrh   return p;
618a3152895Sdrh }
61917435752Sdrh 
62017435752Sdrh /*
62117435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
62217435752Sdrh ** the mallocFailed flag in the connection pointer.
62317435752Sdrh */
624da4ca9d1Sdrh void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
625a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
62617435752Sdrh   if( p ){
62720f3df04Sdrh     memset(p, 0, (size_t)n);
62817435752Sdrh   }
62917435752Sdrh   return p;
63017435752Sdrh }
63117435752Sdrh 
63217435752Sdrh /*
63317435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
63417435752Sdrh ** the mallocFailed flag in the connection pointer.
635ddecae79Sdrh **
636ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
637ddecae79Sdrh ** failure on the same database connection) then always return 0.
638ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
639ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
640ddecae79Sdrh ** This is an important assumption.  There are many places in the
641ddecae79Sdrh ** code that do things like this:
642ddecae79Sdrh **
643ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
644ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
645ddecae79Sdrh **         if( b ) a[10] = 9;
646ddecae79Sdrh **
647ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
648ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
64917435752Sdrh */
650da4ca9d1Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
651633e6d57Sdrh   void *p;
652d9da78a2Sdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
653ccd4ad3eSdan   assert( db==0 || db->pnBytesFreed==0 );
6544150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
655633e6d57Sdrh   if( db ){
656633e6d57Sdrh     LookasideSlot *pBuf;
657633e6d57Sdrh     if( db->mallocFailed ){
658633e6d57Sdrh       return 0;
659633e6d57Sdrh     }
6600b12e7f8Sdrh     if( db->lookaside.bEnabled ){
6610b12e7f8Sdrh       if( n>db->lookaside.sz ){
6620b12e7f8Sdrh         db->lookaside.anStat[1]++;
6630b12e7f8Sdrh       }else if( (pBuf = db->lookaside.pFree)==0 ){
6640b12e7f8Sdrh         db->lookaside.anStat[2]++;
6650b12e7f8Sdrh       }else{
666633e6d57Sdrh         db->lookaside.pFree = pBuf->pNext;
667633e6d57Sdrh         db->lookaside.nOut++;
6680b12e7f8Sdrh         db->lookaside.anStat[0]++;
669633e6d57Sdrh         if( db->lookaside.nOut>db->lookaside.mxOut ){
670633e6d57Sdrh           db->lookaside.mxOut = db->lookaside.nOut;
671633e6d57Sdrh         }
672633e6d57Sdrh         return (void*)pBuf;
673633e6d57Sdrh       }
674633e6d57Sdrh     }
6750b12e7f8Sdrh   }
676ddecae79Sdrh #else
677ddecae79Sdrh   if( db && db->mallocFailed ){
678ddecae79Sdrh     return 0;
679ddecae79Sdrh   }
6804150ebf8Sdrh #endif
681fec00eabSdrh   p = sqlite3Malloc(n);
682f3a65f7eSdrh   if( !p && db ){
68317435752Sdrh     db->mallocFailed = 1;
68417435752Sdrh   }
685d231aa3aSdrh   sqlite3MemdebugSetType(p,
686d231aa3aSdrh          (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
68717435752Sdrh   return p;
68817435752Sdrh }
68917435752Sdrh 
69026783a58Sdanielk1977 /*
69126783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
69226783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
69326783a58Sdanielk1977 */
694da4ca9d1Sdrh void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
695a1644fd8Sdanielk1977   void *pNew = 0;
696d9da78a2Sdrh   assert( db!=0 );
6977047e25cSdrh   assert( sqlite3_mutex_held(db->mutex) );
698a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
699633e6d57Sdrh     if( p==0 ){
700633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
701633e6d57Sdrh     }
702633e6d57Sdrh     if( isLookaside(db, p) ){
703633e6d57Sdrh       if( n<=db->lookaside.sz ){
704633e6d57Sdrh         return p;
705633e6d57Sdrh       }
706633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
707633e6d57Sdrh       if( pNew ){
708633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
709633e6d57Sdrh         sqlite3DbFree(db, p);
710633e6d57Sdrh       }
711633e6d57Sdrh     }else{
712d231aa3aSdrh       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
713d425864dSmistachkin       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
714107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
7153329a63aSdrh       pNew = sqlite3_realloc64(p, n);
716a1644fd8Sdanielk1977       if( !pNew ){
717a1644fd8Sdanielk1977         db->mallocFailed = 1;
718a1644fd8Sdanielk1977       }
719d231aa3aSdrh       sqlite3MemdebugSetType(pNew,
720174b9a16Sdrh             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
721a1644fd8Sdanielk1977     }
722633e6d57Sdrh   }
723a1644fd8Sdanielk1977   return pNew;
724a1644fd8Sdanielk1977 }
725a1644fd8Sdanielk1977 
72617435752Sdrh /*
72717435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
72817435752Sdrh ** and set the mallocFailed flag in the database connection.
72917435752Sdrh */
730da4ca9d1Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
731a3152895Sdrh   void *pNew;
732a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
733a3152895Sdrh   if( !pNew ){
734633e6d57Sdrh     sqlite3DbFree(db, p);
735a3152895Sdrh   }
736a3152895Sdrh   return pNew;
737a3152895Sdrh }
738a3152895Sdrh 
739a3152895Sdrh /*
740a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
741a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
742a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
743a3152895Sdrh ** called via macros that record the current file and line number in the
744a3152895Sdrh ** ThreadData structure.
745a3152895Sdrh */
746633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
747a3152895Sdrh   char *zNew;
748633e6d57Sdrh   size_t n;
749633e6d57Sdrh   if( z==0 ){
750633e6d57Sdrh     return 0;
751a3152895Sdrh   }
752dee0e404Sdrh   n = sqlite3Strlen30(z) + 1;
753633e6d57Sdrh   assert( (n&0x7fffffff)==n );
754633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
755a3152895Sdrh   if( zNew ){
756a3152895Sdrh     memcpy(zNew, z, n);
7571e536953Sdanielk1977   }
7581e536953Sdanielk1977   return zNew;
7591e536953Sdanielk1977 }
760da4ca9d1Sdrh char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
761633e6d57Sdrh   char *zNew;
762633e6d57Sdrh   if( z==0 ){
763633e6d57Sdrh     return 0;
764633e6d57Sdrh   }
765633e6d57Sdrh   assert( (n&0x7fffffff)==n );
766633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
767633e6d57Sdrh   if( zNew ){
76820f3df04Sdrh     memcpy(zNew, z, (size_t)n);
769633e6d57Sdrh     zNew[n] = 0;
7701e536953Sdanielk1977   }
7711e536953Sdanielk1977   return zNew;
7721e536953Sdanielk1977 }
7731e536953Sdanielk1977 
774a3152895Sdrh /*
775f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
776f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
777f089aa45Sdrh ** point to that string.
778a3152895Sdrh */
779f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
780a3152895Sdrh   va_list ap;
781f089aa45Sdrh   char *z;
782a3152895Sdrh 
783f089aa45Sdrh   va_start(ap, zFormat);
784f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
785a3152895Sdrh   va_end(ap);
786633e6d57Sdrh   sqlite3DbFree(db, *pz);
787f089aa45Sdrh   *pz = z;
788a3152895Sdrh }
789a3152895Sdrh 
790b50c65d5Sdrh /*
791b50c65d5Sdrh ** Take actions at the end of an API call to indicate an OOM error
792b50c65d5Sdrh */
793b50c65d5Sdrh static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
794b50c65d5Sdrh   db->mallocFailed = 0;
795b50c65d5Sdrh   sqlite3Error(db, SQLITE_NOMEM);
796b50c65d5Sdrh   return SQLITE_NOMEM;
797b50c65d5Sdrh }
798a3152895Sdrh 
799a3152895Sdrh /*
800a3152895Sdrh ** This function must be called before exiting any API function (i.e.
80117435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
80217435752Sdrh ** sqlite3_realloc.
803a3152895Sdrh **
804a3152895Sdrh ** The returned value is normally a copy of the second argument to this
805be217793Sshane ** function. However, if a malloc() failure has occurred since the previous
806a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
807a3152895Sdrh **
808be217793Sshane ** If the first argument, db, is not NULL and a malloc() error has occurred,
809a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
810a3152895Sdrh ** is set to SQLITE_NOMEM.
811a3152895Sdrh */
812a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
813a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
814a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
815a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
816a1644fd8Sdanielk1977   */
817a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
818b50c65d5Sdrh   if( db==0 ) return rc & 0xff;
819b50c65d5Sdrh   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
820b50c65d5Sdrh     return apiOomError(db);
821a3152895Sdrh   }
822b50c65d5Sdrh   return rc & db->errMask;
823a3152895Sdrh }
824