xref: /sqlite-3.40.0/src/malloc.c (revision badc980a)
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 /*
19b21c8cd4Sdrh ** This routine runs when the memory allocator sees that the
20b21c8cd4Sdrh ** total memory allocation is about to exceed the soft heap
21b21c8cd4Sdrh ** limit.
22b21c8cd4Sdrh */
23b21c8cd4Sdrh static void softHeapLimitEnforcer(
24b21c8cd4Sdrh   void *NotUsed,
2562c14b34Sdanielk1977   sqlite3_int64 NotUsed2,
26153c62c4Sdrh   int allocSize
27b21c8cd4Sdrh ){
2862c14b34Sdanielk1977   UNUSED_PARAMETER2(NotUsed, NotUsed2);
29b21c8cd4Sdrh   sqlite3_release_memory(allocSize);
30b21c8cd4Sdrh }
31b21c8cd4Sdrh 
32b21c8cd4Sdrh /*
338468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or
348468024dSdanielk1977 ** negative value indicates no limit.
35a3152895Sdrh */
36a3152895Sdrh void sqlite3_soft_heap_limit(int n){
37b21c8cd4Sdrh   sqlite3_uint64 iLimit;
38b21c8cd4Sdrh   int overage;
39b21c8cd4Sdrh   if( n<0 ){
40b21c8cd4Sdrh     iLimit = 0;
41b21c8cd4Sdrh   }else{
42b21c8cd4Sdrh     iLimit = n;
43a3152895Sdrh   }
449ac06509Sdrh #ifndef SQLITE_OMIT_AUTOINIT
459ac3fe97Sdrh   sqlite3_initialize();
469ac06509Sdrh #endif
47b21c8cd4Sdrh   if( iLimit>0 ){
484a27a286Sshane     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
49b21c8cd4Sdrh   }else{
504a27a286Sshane     sqlite3MemoryAlarm(0, 0, 0);
51b21c8cd4Sdrh   }
521bd10f8aSdrh   overage = (int)(sqlite3_memory_used() - (i64)n);
53b21c8cd4Sdrh   if( overage>0 ){
54b21c8cd4Sdrh     sqlite3_release_memory(overage);
55b21c8cd4Sdrh   }
56a3152895Sdrh }
57a3152895Sdrh 
58a3152895Sdrh /*
598468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently
608468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to
618468024dSdanielk1977 ** cache database pages that are not currently in use.
62a3152895Sdrh */
63a3152895Sdrh int sqlite3_release_memory(int n){
6486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
6567e3da7aSdanielk1977   int nRet = 0;
6667e3da7aSdanielk1977   nRet += sqlite3PcacheReleaseMemory(n-nRet);
67dfb316d4Sdanielk1977   return nRet;
681e536953Sdanielk1977 #else
6962c14b34Sdanielk1977   UNUSED_PARAMETER(n);
701e536953Sdanielk1977   return SQLITE_OK;
711e536953Sdanielk1977 #endif
72a3152895Sdrh }
73a3152895Sdrh 
74fec00eabSdrh /*
75*badc980aSdrh ** An instance of the following object records the location of
76*badc980aSdrh ** each unused scratch buffer.
77*badc980aSdrh */
78*badc980aSdrh typedef struct ScratchFreeslot {
79*badc980aSdrh   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
80*badc980aSdrh } ScratchFreeslot;
81*badc980aSdrh 
82*badc980aSdrh /*
83fec00eabSdrh ** State information local to the memory allocation subsystem.
84fec00eabSdrh */
855c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
86fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
87fec00eabSdrh 
88fec00eabSdrh   /*
89fec00eabSdrh   ** The alarm callback and its arguments.  The mem0.mutex lock will
90fec00eabSdrh   ** be held while the callback is running.  Recursive calls into
91fec00eabSdrh   ** the memory subsystem are allowed, but no new callbacks will be
92e64ca7baSdrh   ** issued.
93fec00eabSdrh   */
94fec00eabSdrh   sqlite3_int64 alarmThreshold;
95fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
96fec00eabSdrh   void *alarmArg;
97fec00eabSdrh 
98fec00eabSdrh   /*
99*badc980aSdrh   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
100*badc980aSdrh   ** (so that a range test can be used to determine if an allocation
101*badc980aSdrh   ** being freed came from pScratch) and a pointer to the list of
102*badc980aSdrh   ** unused scratch allocations.
1039ac3fe97Sdrh   */
104*badc980aSdrh   void *pScratchEnd;
105*badc980aSdrh   ScratchFreeslot *pScratchFree;
106*badc980aSdrh   u32 nScratchFree;
10750d1b5f3Sdrh 
10850d1b5f3Sdrh   /*
10950d1b5f3Sdrh   ** True if heap is nearly "full" where "full" is defined by the
11050d1b5f3Sdrh   ** sqlite3_soft_heap_limit() setting.
11150d1b5f3Sdrh   */
11250d1b5f3Sdrh   int nearlyFull;
113*badc980aSdrh } mem0 = { 0, 0, 0, 0, 0, 0, 0 };
1145c8f8587Sdanielk1977 
1155c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
116fec00eabSdrh 
117fec00eabSdrh /*
118fec00eabSdrh ** Initialize the memory allocation subsystem.
119fec00eabSdrh */
120fec00eabSdrh int sqlite3MallocInit(void){
121075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
122fec00eabSdrh     sqlite3MemSetDefault();
123fec00eabSdrh   }
124fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
125075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
12659f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
127fec00eabSdrh   }
128075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
129075c23afSdanielk1977       && sqlite3GlobalConfig.nScratch>=0 ){
130*badc980aSdrh     int i, n, sz;
131*badc980aSdrh     ScratchFreeslot *pSlot;
132*badc980aSdrh     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
133*badc980aSdrh     sqlite3GlobalConfig.szScratch = sz;
134*badc980aSdrh     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
135*badc980aSdrh     n = sqlite3GlobalConfig.nScratch;
136*badc980aSdrh     mem0.pScratchFree = pSlot;
137*badc980aSdrh     mem0.nScratchFree = n;
138*badc980aSdrh     for(i=0; i<n-1; i++){
139*badc980aSdrh       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
140*badc980aSdrh       pSlot = pSlot->pNext;
141*badc980aSdrh     }
142*badc980aSdrh     pSlot->pNext = 0;
143*badc980aSdrh     mem0.pScratchEnd = (void*)&pSlot[1];
1449ac3fe97Sdrh   }else{
145*badc980aSdrh     mem0.pScratchEnd = 0;
146075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
147075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
148*badc980aSdrh     sqlite3GlobalConfig.nScratch = 0;
1499ac3fe97Sdrh   }
15050d1b5f3Sdrh   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
15150d1b5f3Sdrh       || sqlite3GlobalConfig.nPage<1 ){
152075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
153075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
15450d1b5f3Sdrh     sqlite3GlobalConfig.nPage = 0;
1559ac3fe97Sdrh   }
156075c23afSdanielk1977   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
157fec00eabSdrh }
158fec00eabSdrh 
159fec00eabSdrh /*
16050d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other
16150d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by
16250d1b5f3Sdrh ** sqlite3_soft_heap_limit().
16350d1b5f3Sdrh */
16450d1b5f3Sdrh int sqlite3HeapNearlyFull(void){
16550d1b5f3Sdrh   return mem0.nearlyFull;
16650d1b5f3Sdrh }
16750d1b5f3Sdrh 
16850d1b5f3Sdrh /*
169fec00eabSdrh ** Deinitialize the memory allocation subsystem.
170fec00eabSdrh */
171fec00eabSdrh void sqlite3MallocEnd(void){
1720a549071Sdanielk1977   if( sqlite3GlobalConfig.m.xShutdown ){
173075c23afSdanielk1977     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
1740a549071Sdanielk1977   }
1759ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
176fec00eabSdrh }
177fec00eabSdrh 
178fec00eabSdrh /*
179fec00eabSdrh ** Return the amount of memory currently checked out.
180fec00eabSdrh */
181fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
182f7141990Sdrh   int n, mx;
183c376a198Sdrh   sqlite3_int64 res;
184f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
185c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
186c376a198Sdrh   return res;
187fec00eabSdrh }
188fec00eabSdrh 
189fec00eabSdrh /*
190fec00eabSdrh ** Return the maximum amount of memory that has ever been
191fec00eabSdrh ** checked out since either the beginning of this process
192fec00eabSdrh ** or since the most recent reset.
193fec00eabSdrh */
194fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
195f7141990Sdrh   int n, mx;
196c376a198Sdrh   sqlite3_int64 res;
197f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
1987986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
199c376a198Sdrh   return res;
200fec00eabSdrh }
201fec00eabSdrh 
202fec00eabSdrh /*
203fec00eabSdrh ** Change the alarm callback
204fec00eabSdrh */
2054a27a286Sshane int sqlite3MemoryAlarm(
206fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
207fec00eabSdrh   void *pArg,
208fec00eabSdrh   sqlite3_int64 iThreshold
209fec00eabSdrh ){
21050d1b5f3Sdrh   int nUsed;
211fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
212fec00eabSdrh   mem0.alarmCallback = xCallback;
213fec00eabSdrh   mem0.alarmArg = pArg;
214fec00eabSdrh   mem0.alarmThreshold = iThreshold;
21550d1b5f3Sdrh   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
21650d1b5f3Sdrh   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
217fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
218fec00eabSdrh   return SQLITE_OK;
219fec00eabSdrh }
220fec00eabSdrh 
221eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
222fec00eabSdrh /*
2234a27a286Sshane ** Deprecated external interface.  Internal/core SQLite code
2244a27a286Sshane ** should call sqlite3MemoryAlarm.
2254a27a286Sshane */
2264a27a286Sshane int sqlite3_memory_alarm(
2274a27a286Sshane   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
2284a27a286Sshane   void *pArg,
2294a27a286Sshane   sqlite3_int64 iThreshold
2304a27a286Sshane ){
2314a27a286Sshane   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
2324a27a286Sshane }
233eec556d3Sshane #endif
2344a27a286Sshane 
2354a27a286Sshane /*
236fec00eabSdrh ** Trigger the alarm
237fec00eabSdrh */
238fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
239fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
240fec00eabSdrh   sqlite3_int64 nowUsed;
241fec00eabSdrh   void *pArg;
242e64ca7baSdrh   if( mem0.alarmCallback==0 ) return;
243fec00eabSdrh   xCallback = mem0.alarmCallback;
244f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
245fec00eabSdrh   pArg = mem0.alarmArg;
246e64ca7baSdrh   mem0.alarmCallback = 0;
247fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
248fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
249fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
250e64ca7baSdrh   mem0.alarmCallback = xCallback;
251e64ca7baSdrh   mem0.alarmArg = pArg;
252fec00eabSdrh }
253fec00eabSdrh 
254fec00eabSdrh /*
255f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
256f7141990Sdrh ** lock is already held.
257fec00eabSdrh */
258f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
259fec00eabSdrh   int nFull;
260f7141990Sdrh   void *p;
261f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
262075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
263f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
264f7141990Sdrh   if( mem0.alarmCallback!=0 ){
265f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
266f7141990Sdrh     if( nUsed+nFull >= mem0.alarmThreshold ){
26750d1b5f3Sdrh       mem0.nearlyFull = 1;
268fec00eabSdrh       sqlite3MallocAlarm(nFull);
26950d1b5f3Sdrh     }else{
27050d1b5f3Sdrh       mem0.nearlyFull = 0;
271fec00eabSdrh     }
272f7141990Sdrh   }
273075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
27450d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
275d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
276fec00eabSdrh     sqlite3MallocAlarm(nFull);
277075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
278fec00eabSdrh   }
27950d1b5f3Sdrh #endif
280c702c7ccSdrh   if( p ){
281c702c7ccSdrh     nFull = sqlite3MallocSize(p);
282c702c7ccSdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
283eafc43b1Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
284c702c7ccSdrh   }
285f7141990Sdrh   *pp = p;
286f7141990Sdrh   return nFull;
287fec00eabSdrh }
288f7141990Sdrh 
289f7141990Sdrh /*
290f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
291f7141990Sdrh ** assumes the memory subsystem has already been initialized.
292f7141990Sdrh */
293f7141990Sdrh void *sqlite3Malloc(int n){
294f7141990Sdrh   void *p;
295e08ed7e7Sdrh   if( n<=0 || n>=0x7fffff00 ){
296e08ed7e7Sdrh     /* A memory allocation of a number of bytes which is near the maximum
297e08ed7e7Sdrh     ** signed integer value might cause an integer overflow inside of the
298e08ed7e7Sdrh     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
299e08ed7e7Sdrh     ** 255 bytes of overhead.  SQLite itself will never use anything near
300e08ed7e7Sdrh     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
301f7141990Sdrh     p = 0;
302075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
303f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
304f7141990Sdrh     mallocWithAlarm(n, &p);
305fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
306fec00eabSdrh   }else{
307075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
308fec00eabSdrh   }
309fec00eabSdrh   return p;
310fec00eabSdrh }
311fec00eabSdrh 
312fec00eabSdrh /*
313fec00eabSdrh ** This version of the memory allocation is for use by the application.
314fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
315fec00eabSdrh ** allocation.
316fec00eabSdrh */
317fec00eabSdrh void *sqlite3_malloc(int n){
318fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
319fec00eabSdrh   if( sqlite3_initialize() ) return 0;
320fec00eabSdrh #endif
321fec00eabSdrh   return sqlite3Malloc(n);
322fec00eabSdrh }
323fec00eabSdrh 
324fec00eabSdrh /*
325e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
326facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
327facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
328e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
329e5ae5735Sdrh */
330e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
331facf0307Sdrh static int scratchAllocOut = 0;
332e5ae5735Sdrh #endif
333e5ae5735Sdrh 
334e5ae5735Sdrh 
335e5ae5735Sdrh /*
336e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
337e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
338e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
339e5ae5735Sdrh ** routine is intended to get memory to old large transient data
340e5ae5735Sdrh ** structures that would not normally fit on the stack of an
341e5ae5735Sdrh ** embedded processor.
342e5ae5735Sdrh */
343facf0307Sdrh void *sqlite3ScratchMalloc(int n){
344e5ae5735Sdrh   void *p;
345e5ae5735Sdrh   assert( n>0 );
3469ac3fe97Sdrh 
347*badc980aSdrh   sqlite3_mutex_enter(mem0.mutex);
348*badc980aSdrh   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
349*badc980aSdrh     p = mem0.pScratchFree;
350*badc980aSdrh     mem0.pScratchFree = mem0.pScratchFree->pNext;
351*badc980aSdrh     mem0.nScratchFree--;
352*badc980aSdrh     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
353*badc980aSdrh     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
354*badc980aSdrh   }else{
355*badc980aSdrh     if( sqlite3GlobalConfig.bMemstat ){
356*badc980aSdrh       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
357*badc980aSdrh       n = mallocWithAlarm(n, &p);
358*badc980aSdrh       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
359*badc980aSdrh     }else{
360*badc980aSdrh       p = sqlite3GlobalConfig.m.xMalloc(n);
361*badc980aSdrh     }
362*badc980aSdrh     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
363*badc980aSdrh   }
364*badc980aSdrh   sqlite3_mutex_leave(mem0.mutex);
365*badc980aSdrh 
366*badc980aSdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
367*badc980aSdrh   /* Verify that no more than two scratch allocations per thread
368*badc980aSdrh   ** are outstanding at one time.  (This is only checked in the
369*badc980aSdrh   ** single-threaded case since checking in the multi-threaded case
370*badc980aSdrh   ** would be much more complicated.) */
371*badc980aSdrh   assert( scratchAllocOut<=1 );
372*badc980aSdrh   if( p ) scratchAllocOut++;
373*badc980aSdrh #endif
374*badc980aSdrh 
375*badc980aSdrh   return p;
376*badc980aSdrh }
377*badc980aSdrh void sqlite3ScratchFree(void *p){
378*badc980aSdrh   if( p ){
379*badc980aSdrh 
380e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
38137f99187Sdrh     /* Verify that no more than two scratch allocation per thread
3829ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3839ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3849ac3fe97Sdrh     ** would be much more complicated.) */
385*badc980aSdrh     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
386*badc980aSdrh     scratchAllocOut--;
387e5ae5735Sdrh #endif
3889ac3fe97Sdrh 
389*badc980aSdrh     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
390*badc980aSdrh       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
391*badc980aSdrh       ScratchFreeslot *pSlot;
392*badc980aSdrh       pSlot = (ScratchFreeslot*)p;
393e5ae5735Sdrh       sqlite3_mutex_enter(mem0.mutex);
394*badc980aSdrh       pSlot->pNext = mem0.pScratchFree;
395*badc980aSdrh       mem0.pScratchFree = pSlot;
396*badc980aSdrh       mem0.nScratchFree++;
397*badc980aSdrh       assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch );
398*badc980aSdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
3999ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
400f7141990Sdrh     }else{
401*badc980aSdrh       /* Release memory back to the heap */
402107b56e8Sdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
403174b9a16Sdrh       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
404107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
405075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
406f7141990Sdrh         int iSize = sqlite3MallocSize(p);
407f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
408f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
409f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
41081ba7d16Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
411075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
412f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
413f7141990Sdrh       }else{
414075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
415f7141990Sdrh       }
4169ac3fe97Sdrh     }
417e5ae5735Sdrh   }
418e5ae5735Sdrh }
419e5ae5735Sdrh 
420e5ae5735Sdrh /*
421633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
422633e6d57Sdrh */
4234150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
424633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
425174b9a16Sdrh   return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
426633e6d57Sdrh }
4274150ebf8Sdrh #else
4284150ebf8Sdrh #define isLookaside(A,B) 0
4294150ebf8Sdrh #endif
430633e6d57Sdrh 
431633e6d57Sdrh /*
432fec00eabSdrh ** Return the size of a memory allocation previously obtained from
433fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
434fec00eabSdrh */
435fec00eabSdrh int sqlite3MallocSize(void *p){
436107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
437174b9a16Sdrh   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
438075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
439fec00eabSdrh }
440633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
4417047e25cSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
442174b9a16Sdrh   if( db && isLookaside(db, p) ){
443633e6d57Sdrh     return db->lookaside.sz;
444633e6d57Sdrh   }else{
445174b9a16Sdrh     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
446174b9a16Sdrh     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
447174b9a16Sdrh     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
448075c23afSdanielk1977     return sqlite3GlobalConfig.m.xSize(p);
449633e6d57Sdrh   }
450633e6d57Sdrh }
451fec00eabSdrh 
452fec00eabSdrh /*
453fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
454fec00eabSdrh */
455fec00eabSdrh void sqlite3_free(void *p){
456fec00eabSdrh   if( p==0 ) return;
457174b9a16Sdrh   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
458107b56e8Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
459075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
460fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
461f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
462eafc43b1Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
463075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
464fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
465fec00eabSdrh   }else{
466075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
467fec00eabSdrh   }
468fec00eabSdrh }
469fec00eabSdrh 
470fec00eabSdrh /*
471633e6d57Sdrh ** Free memory that might be associated with a particular database
472633e6d57Sdrh ** connection.
473633e6d57Sdrh */
474633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
4757047e25cSdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
476174b9a16Sdrh   if( db ){
477174b9a16Sdrh     if( db->pnBytesFreed ){
478174b9a16Sdrh       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
479174b9a16Sdrh       return;
480d46def77Sdan     }
481633e6d57Sdrh     if( isLookaside(db, p) ){
482633e6d57Sdrh       LookasideSlot *pBuf = (LookasideSlot*)p;
483633e6d57Sdrh       pBuf->pNext = db->lookaside.pFree;
484633e6d57Sdrh       db->lookaside.pFree = pBuf;
485633e6d57Sdrh       db->lookaside.nOut--;
486174b9a16Sdrh       return;
487174b9a16Sdrh     }
488174b9a16Sdrh   }
489174b9a16Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
490174b9a16Sdrh   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
491174b9a16Sdrh   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
492107b56e8Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
493633e6d57Sdrh   sqlite3_free(p);
494633e6d57Sdrh }
495633e6d57Sdrh 
496633e6d57Sdrh /*
497fec00eabSdrh ** Change the size of an existing memory allocation
498fec00eabSdrh */
499fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
500fec00eabSdrh   int nOld, nNew;
501fec00eabSdrh   void *pNew;
502fec00eabSdrh   if( pOld==0 ){
503fec00eabSdrh     return sqlite3Malloc(nBytes);
504fec00eabSdrh   }
505b6063cf8Sdrh   if( nBytes<=0 ){
506fec00eabSdrh     sqlite3_free(pOld);
507fec00eabSdrh     return 0;
508fec00eabSdrh   }
509b6063cf8Sdrh   if( nBytes>=0x7fffff00 ){
510b6063cf8Sdrh     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
511b6063cf8Sdrh     return 0;
512b6063cf8Sdrh   }
513fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
514075c23afSdanielk1977   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
515fec00eabSdrh   if( nOld==nNew ){
516fec00eabSdrh     pNew = pOld;
5177c6791c8Sdrh   }else if( sqlite3GlobalConfig.bMemstat ){
5187c6791c8Sdrh     sqlite3_mutex_enter(mem0.mutex);
5197c6791c8Sdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
520f7141990Sdrh     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
521f7141990Sdrh           mem0.alarmThreshold ){
522fec00eabSdrh       sqlite3MallocAlarm(nNew-nOld);
523fec00eabSdrh     }
524107b56e8Sdrh     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
525174b9a16Sdrh     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
526075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
527d09414cdSdanielk1977     if( pNew==0 && mem0.alarmCallback ){
528fec00eabSdrh       sqlite3MallocAlarm(nBytes);
529075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
530fec00eabSdrh     }
531fec00eabSdrh     if( pNew ){
532c702c7ccSdrh       nNew = sqlite3MallocSize(pNew);
533f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
534fec00eabSdrh     }
535fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
536fec00eabSdrh   }else{
5377c6791c8Sdrh     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
538fec00eabSdrh   }
539fec00eabSdrh   return pNew;
540fec00eabSdrh }
541fec00eabSdrh 
542fec00eabSdrh /*
543fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
544fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
545fec00eabSdrh */
546fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
547fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
548fec00eabSdrh   if( sqlite3_initialize() ) return 0;
549fec00eabSdrh #endif
550fec00eabSdrh   return sqlite3Realloc(pOld, n);
551fec00eabSdrh }
552fec00eabSdrh 
553a3152895Sdrh 
554a3152895Sdrh /*
55517435752Sdrh ** Allocate and zero memory.
556a3152895Sdrh */
557fec00eabSdrh void *sqlite3MallocZero(int n){
558fec00eabSdrh   void *p = sqlite3Malloc(n);
559a3152895Sdrh   if( p ){
560a3152895Sdrh     memset(p, 0, n);
561a3152895Sdrh   }
562a3152895Sdrh   return p;
563a3152895Sdrh }
56417435752Sdrh 
56517435752Sdrh /*
56617435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
56717435752Sdrh ** the mallocFailed flag in the connection pointer.
56817435752Sdrh */
569fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
570a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
57117435752Sdrh   if( p ){
57217435752Sdrh     memset(p, 0, n);
57317435752Sdrh   }
57417435752Sdrh   return p;
57517435752Sdrh }
57617435752Sdrh 
57717435752Sdrh /*
57817435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
57917435752Sdrh ** the mallocFailed flag in the connection pointer.
580ddecae79Sdrh **
581ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
582ddecae79Sdrh ** failure on the same database connection) then always return 0.
583ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
584ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
585ddecae79Sdrh ** This is an important assumption.  There are many places in the
586ddecae79Sdrh ** code that do things like this:
587ddecae79Sdrh **
588ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
589ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
590ddecae79Sdrh **         if( b ) a[10] = 9;
591ddecae79Sdrh **
592ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
593ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
59417435752Sdrh */
595fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
596633e6d57Sdrh   void *p;
597d9da78a2Sdrh   assert( db==0 || sqlite3_mutex_held(db->mutex) );
598ccd4ad3eSdan   assert( db==0 || db->pnBytesFreed==0 );
5994150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
600633e6d57Sdrh   if( db ){
601633e6d57Sdrh     LookasideSlot *pBuf;
602633e6d57Sdrh     if( db->mallocFailed ){
603633e6d57Sdrh       return 0;
604633e6d57Sdrh     }
605633e6d57Sdrh     if( db->lookaside.bEnabled && n<=db->lookaside.sz
606633e6d57Sdrh          && (pBuf = db->lookaside.pFree)!=0 ){
607633e6d57Sdrh       db->lookaside.pFree = pBuf->pNext;
608633e6d57Sdrh       db->lookaside.nOut++;
609633e6d57Sdrh       if( db->lookaside.nOut>db->lookaside.mxOut ){
610633e6d57Sdrh         db->lookaside.mxOut = db->lookaside.nOut;
611633e6d57Sdrh       }
612633e6d57Sdrh       return (void*)pBuf;
613633e6d57Sdrh     }
614633e6d57Sdrh   }
615ddecae79Sdrh #else
616ddecae79Sdrh   if( db && db->mallocFailed ){
617ddecae79Sdrh     return 0;
618ddecae79Sdrh   }
6194150ebf8Sdrh #endif
620fec00eabSdrh   p = sqlite3Malloc(n);
621f3a65f7eSdrh   if( !p && db ){
62217435752Sdrh     db->mallocFailed = 1;
62317435752Sdrh   }
624174b9a16Sdrh   sqlite3MemdebugSetType(p, MEMTYPE_DB |
625174b9a16Sdrh          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
62617435752Sdrh   return p;
62717435752Sdrh }
62817435752Sdrh 
62926783a58Sdanielk1977 /*
63026783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
63126783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
63226783a58Sdanielk1977 */
633a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
634a1644fd8Sdanielk1977   void *pNew = 0;
635d9da78a2Sdrh   assert( db!=0 );
6367047e25cSdrh   assert( sqlite3_mutex_held(db->mutex) );
637a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
638633e6d57Sdrh     if( p==0 ){
639633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
640633e6d57Sdrh     }
641633e6d57Sdrh     if( isLookaside(db, p) ){
642633e6d57Sdrh       if( n<=db->lookaside.sz ){
643633e6d57Sdrh         return p;
644633e6d57Sdrh       }
645633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
646633e6d57Sdrh       if( pNew ){
647633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
648633e6d57Sdrh         sqlite3DbFree(db, p);
649633e6d57Sdrh       }
650633e6d57Sdrh     }else{
651174b9a16Sdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
652174b9a16Sdrh       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
653107b56e8Sdrh       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
654a1644fd8Sdanielk1977       pNew = sqlite3_realloc(p, n);
655a1644fd8Sdanielk1977       if( !pNew ){
656174b9a16Sdrh         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
657a1644fd8Sdanielk1977         db->mallocFailed = 1;
658a1644fd8Sdanielk1977       }
659174b9a16Sdrh       sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
660174b9a16Sdrh             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
661a1644fd8Sdanielk1977     }
662633e6d57Sdrh   }
663a1644fd8Sdanielk1977   return pNew;
664a1644fd8Sdanielk1977 }
665a1644fd8Sdanielk1977 
66617435752Sdrh /*
66717435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
66817435752Sdrh ** and set the mallocFailed flag in the database connection.
66917435752Sdrh */
67017435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
671a3152895Sdrh   void *pNew;
672a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
673a3152895Sdrh   if( !pNew ){
674633e6d57Sdrh     sqlite3DbFree(db, p);
675a3152895Sdrh   }
676a3152895Sdrh   return pNew;
677a3152895Sdrh }
678a3152895Sdrh 
679a3152895Sdrh /*
680a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
681a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
682a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
683a3152895Sdrh ** called via macros that record the current file and line number in the
684a3152895Sdrh ** ThreadData structure.
685a3152895Sdrh */
686633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
687a3152895Sdrh   char *zNew;
688633e6d57Sdrh   size_t n;
689633e6d57Sdrh   if( z==0 ){
690633e6d57Sdrh     return 0;
691a3152895Sdrh   }
692dee0e404Sdrh   n = sqlite3Strlen30(z) + 1;
693633e6d57Sdrh   assert( (n&0x7fffffff)==n );
694633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
695a3152895Sdrh   if( zNew ){
696a3152895Sdrh     memcpy(zNew, z, n);
6971e536953Sdanielk1977   }
6981e536953Sdanielk1977   return zNew;
6991e536953Sdanielk1977 }
7001e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
701633e6d57Sdrh   char *zNew;
702633e6d57Sdrh   if( z==0 ){
703633e6d57Sdrh     return 0;
704633e6d57Sdrh   }
705633e6d57Sdrh   assert( (n&0x7fffffff)==n );
706633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
707633e6d57Sdrh   if( zNew ){
708633e6d57Sdrh     memcpy(zNew, z, n);
709633e6d57Sdrh     zNew[n] = 0;
7101e536953Sdanielk1977   }
7111e536953Sdanielk1977   return zNew;
7121e536953Sdanielk1977 }
7131e536953Sdanielk1977 
714a3152895Sdrh /*
715f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
716f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
717f089aa45Sdrh ** point to that string.
718a3152895Sdrh */
719f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
720a3152895Sdrh   va_list ap;
721f089aa45Sdrh   char *z;
722a3152895Sdrh 
723f089aa45Sdrh   va_start(ap, zFormat);
724f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
725a3152895Sdrh   va_end(ap);
726633e6d57Sdrh   sqlite3DbFree(db, *pz);
727f089aa45Sdrh   *pz = z;
728a3152895Sdrh }
729a3152895Sdrh 
730a3152895Sdrh 
731a3152895Sdrh /*
732a3152895Sdrh ** This function must be called before exiting any API function (i.e.
73317435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
73417435752Sdrh ** sqlite3_realloc.
735a3152895Sdrh **
736a3152895Sdrh ** The returned value is normally a copy of the second argument to this
737be217793Sshane ** function. However, if a malloc() failure has occurred since the previous
738a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
739a3152895Sdrh **
740be217793Sshane ** If the first argument, db, is not NULL and a malloc() error has occurred,
741a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
742a3152895Sdrh ** is set to SQLITE_NOMEM.
743a3152895Sdrh */
744a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
745a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
746a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
747a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
748a1644fd8Sdanielk1977   */
749a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
75098c21903Sdanielk1977   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
751a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
75217435752Sdrh     db->mallocFailed = 0;
753a3152895Sdrh     rc = SQLITE_NOMEM;
754a3152895Sdrh   }
755a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
756a3152895Sdrh }
757