xref: /sqlite-3.40.0/src/malloc.c (revision 1bd10f8a)
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 **
15*1bd10f8aSdrh ** $Id: malloc.c,v 1.51 2008/12/10 21:19:57 drh Exp $
16a3152895Sdrh */
17a3152895Sdrh #include "sqliteInt.h"
18a3152895Sdrh #include <stdarg.h>
19a3152895Sdrh #include <ctype.h>
20a3152895Sdrh 
21a3152895Sdrh /*
22b21c8cd4Sdrh ** This routine runs when the memory allocator sees that the
23b21c8cd4Sdrh ** total memory allocation is about to exceed the soft heap
24b21c8cd4Sdrh ** limit.
25b21c8cd4Sdrh */
26b21c8cd4Sdrh static void softHeapLimitEnforcer(
27b21c8cd4Sdrh   void *NotUsed,
2862c14b34Sdanielk1977   sqlite3_int64 NotUsed2,
29153c62c4Sdrh   int allocSize
30b21c8cd4Sdrh ){
3162c14b34Sdanielk1977   UNUSED_PARAMETER2(NotUsed, NotUsed2);
32b21c8cd4Sdrh   sqlite3_release_memory(allocSize);
33b21c8cd4Sdrh }
34b21c8cd4Sdrh 
35b21c8cd4Sdrh /*
368468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or
378468024dSdanielk1977 ** negative value indicates no limit.
38a3152895Sdrh */
39a3152895Sdrh void sqlite3_soft_heap_limit(int n){
40b21c8cd4Sdrh   sqlite3_uint64 iLimit;
41b21c8cd4Sdrh   int overage;
42b21c8cd4Sdrh   if( n<0 ){
43b21c8cd4Sdrh     iLimit = 0;
44b21c8cd4Sdrh   }else{
45b21c8cd4Sdrh     iLimit = n;
46a3152895Sdrh   }
479ac3fe97Sdrh   sqlite3_initialize();
48b21c8cd4Sdrh   if( iLimit>0 ){
494a27a286Sshane     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
50b21c8cd4Sdrh   }else{
514a27a286Sshane     sqlite3MemoryAlarm(0, 0, 0);
52b21c8cd4Sdrh   }
53*1bd10f8aSdrh   overage = (int)(sqlite3_memory_used() - (i64)n);
54b21c8cd4Sdrh   if( overage>0 ){
55b21c8cd4Sdrh     sqlite3_release_memory(overage);
56b21c8cd4Sdrh   }
57a3152895Sdrh }
58a3152895Sdrh 
59a3152895Sdrh /*
608468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently
618468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to
628468024dSdanielk1977 ** cache database pages that are not currently in use.
63a3152895Sdrh */
64a3152895Sdrh int sqlite3_release_memory(int n){
6586f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
6667e3da7aSdanielk1977   int nRet = 0;
6767e3da7aSdanielk1977 #if 0
6867e3da7aSdanielk1977   nRet += sqlite3VdbeReleaseMemory(n);
6967e3da7aSdanielk1977 #endif
7067e3da7aSdanielk1977   nRet += sqlite3PcacheReleaseMemory(n-nRet);
71dfb316d4Sdanielk1977   return nRet;
721e536953Sdanielk1977 #else
7362c14b34Sdanielk1977   UNUSED_PARAMETER(n);
741e536953Sdanielk1977   return SQLITE_OK;
751e536953Sdanielk1977 #endif
76a3152895Sdrh }
77a3152895Sdrh 
78fec00eabSdrh /*
79fec00eabSdrh ** State information local to the memory allocation subsystem.
80fec00eabSdrh */
815c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
8223bf0f41Sdanielk1977   /* Number of free pages for scratch and page-cache memory */
8323bf0f41Sdanielk1977   u32 nScratchFree;
8423bf0f41Sdanielk1977   u32 nPageFree;
8523bf0f41Sdanielk1977 
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
92fec00eabSdrh   ** issued.  The alarmBusy variable is set to prevent recursive
93fec00eabSdrh   ** callbacks.
94fec00eabSdrh   */
95fec00eabSdrh   sqlite3_int64 alarmThreshold;
96fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
97fec00eabSdrh   void *alarmArg;
98fec00eabSdrh   int alarmBusy;
99fec00eabSdrh 
100fec00eabSdrh   /*
101075c23afSdanielk1977   ** Pointers to the end of sqlite3GlobalConfig.pScratch and
102075c23afSdanielk1977   ** sqlite3GlobalConfig.pPage to a block of memory that records
1039ac3fe97Sdrh   ** which pages are available.
1049ac3fe97Sdrh   */
1059ac3fe97Sdrh   u32 *aScratchFree;
1069ac3fe97Sdrh   u32 *aPageFree;
107cdcfe95cSdanielk1977 } mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 };
1085c8f8587Sdanielk1977 
1095c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
110fec00eabSdrh 
111fec00eabSdrh /*
112fec00eabSdrh ** Initialize the memory allocation subsystem.
113fec00eabSdrh */
114fec00eabSdrh int sqlite3MallocInit(void){
115075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
116fec00eabSdrh     sqlite3MemSetDefault();
117fec00eabSdrh   }
118fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
119075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
12059f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
121fec00eabSdrh   }
122075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
123075c23afSdanielk1977       && sqlite3GlobalConfig.nScratch>=0 ){
1249ac3fe97Sdrh     int i;
125075c23afSdanielk1977     sqlite3GlobalConfig.szScratch -= 4;
126075c23afSdanielk1977     mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
127075c23afSdanielk1977                   [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
128075c23afSdanielk1977     for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
129075c23afSdanielk1977     mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
1309ac3fe97Sdrh   }else{
131075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
132075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
1339ac3fe97Sdrh   }
134075c23afSdanielk1977   if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
135075c23afSdanielk1977       && sqlite3GlobalConfig.nPage>=1 ){
1369ac3fe97Sdrh     int i;
1370a60a384Sdrh     int overhead;
138075c23afSdanielk1977     int sz = sqlite3GlobalConfig.szPage;
139075c23afSdanielk1977     int n = sqlite3GlobalConfig.nPage;
1400a60a384Sdrh     overhead = (4*n + sz - 1)/sz;
141075c23afSdanielk1977     sqlite3GlobalConfig.nPage -= overhead;
142075c23afSdanielk1977     mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
143075c23afSdanielk1977                   [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
144075c23afSdanielk1977     for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
145075c23afSdanielk1977     mem0.nPageFree = sqlite3GlobalConfig.nPage;
1469ac3fe97Sdrh   }else{
147075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
148075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
1499ac3fe97Sdrh   }
150075c23afSdanielk1977   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
151fec00eabSdrh }
152fec00eabSdrh 
153fec00eabSdrh /*
154fec00eabSdrh ** Deinitialize the memory allocation subsystem.
155fec00eabSdrh */
156fec00eabSdrh void sqlite3MallocEnd(void){
157075c23afSdanielk1977   sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
1589ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
159fec00eabSdrh }
160fec00eabSdrh 
161fec00eabSdrh /*
162fec00eabSdrh ** Return the amount of memory currently checked out.
163fec00eabSdrh */
164fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
165f7141990Sdrh   int n, mx;
166c376a198Sdrh   sqlite3_int64 res;
167f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
168c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
169c376a198Sdrh   return res;
170fec00eabSdrh }
171fec00eabSdrh 
172fec00eabSdrh /*
173fec00eabSdrh ** Return the maximum amount of memory that has ever been
174fec00eabSdrh ** checked out since either the beginning of this process
175fec00eabSdrh ** or since the most recent reset.
176fec00eabSdrh */
177fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
178f7141990Sdrh   int n, mx;
179c376a198Sdrh   sqlite3_int64 res;
180f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
1817986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
182c376a198Sdrh   return res;
183fec00eabSdrh }
184fec00eabSdrh 
185fec00eabSdrh /*
186fec00eabSdrh ** Change the alarm callback
187fec00eabSdrh */
1884a27a286Sshane int sqlite3MemoryAlarm(
189fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
190fec00eabSdrh   void *pArg,
191fec00eabSdrh   sqlite3_int64 iThreshold
192fec00eabSdrh ){
193fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
194fec00eabSdrh   mem0.alarmCallback = xCallback;
195fec00eabSdrh   mem0.alarmArg = pArg;
196fec00eabSdrh   mem0.alarmThreshold = iThreshold;
197fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
198fec00eabSdrh   return SQLITE_OK;
199fec00eabSdrh }
200fec00eabSdrh 
201eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED
202fec00eabSdrh /*
2034a27a286Sshane ** Deprecated external interface.  Internal/core SQLite code
2044a27a286Sshane ** should call sqlite3MemoryAlarm.
2054a27a286Sshane */
2064a27a286Sshane int sqlite3_memory_alarm(
2074a27a286Sshane   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
2084a27a286Sshane   void *pArg,
2094a27a286Sshane   sqlite3_int64 iThreshold
2104a27a286Sshane ){
2114a27a286Sshane   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
2124a27a286Sshane }
213eec556d3Sshane #endif
2144a27a286Sshane 
2154a27a286Sshane /*
216fec00eabSdrh ** Trigger the alarm
217fec00eabSdrh */
218fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
219fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
220fec00eabSdrh   sqlite3_int64 nowUsed;
221fec00eabSdrh   void *pArg;
222fec00eabSdrh   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
223fec00eabSdrh   mem0.alarmBusy = 1;
224fec00eabSdrh   xCallback = mem0.alarmCallback;
225f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
226fec00eabSdrh   pArg = mem0.alarmArg;
227fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
228fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
229fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
230fec00eabSdrh   mem0.alarmBusy = 0;
231fec00eabSdrh }
232fec00eabSdrh 
233fec00eabSdrh /*
234f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
235f7141990Sdrh ** lock is already held.
236fec00eabSdrh */
237f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
238fec00eabSdrh   int nFull;
239f7141990Sdrh   void *p;
240f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
241075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
242f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
243f7141990Sdrh   if( mem0.alarmCallback!=0 ){
244f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
245f7141990Sdrh     if( nUsed+nFull >= mem0.alarmThreshold ){
246fec00eabSdrh       sqlite3MallocAlarm(nFull);
247fec00eabSdrh     }
248f7141990Sdrh   }
249075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
250d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
251fec00eabSdrh     sqlite3MallocAlarm(nFull);
252075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
253fec00eabSdrh   }
254c702c7ccSdrh   if( p ){
255c702c7ccSdrh     nFull = sqlite3MallocSize(p);
256c702c7ccSdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
257c702c7ccSdrh   }
258f7141990Sdrh   *pp = p;
259f7141990Sdrh   return nFull;
260fec00eabSdrh }
261f7141990Sdrh 
262f7141990Sdrh /*
263f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
264f7141990Sdrh ** assumes the memory subsystem has already been initialized.
265f7141990Sdrh */
266f7141990Sdrh void *sqlite3Malloc(int n){
267f7141990Sdrh   void *p;
268f7141990Sdrh   if( n<=0 ){
269f7141990Sdrh     p = 0;
270075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
271f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
272f7141990Sdrh     mallocWithAlarm(n, &p);
273fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
274fec00eabSdrh   }else{
275075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
276fec00eabSdrh   }
277fec00eabSdrh   return p;
278fec00eabSdrh }
279fec00eabSdrh 
280fec00eabSdrh /*
281fec00eabSdrh ** This version of the memory allocation is for use by the application.
282fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
283fec00eabSdrh ** allocation.
284fec00eabSdrh */
285fec00eabSdrh void *sqlite3_malloc(int n){
286fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
287fec00eabSdrh   if( sqlite3_initialize() ) return 0;
288fec00eabSdrh #endif
289fec00eabSdrh   return sqlite3Malloc(n);
290fec00eabSdrh }
291fec00eabSdrh 
292fec00eabSdrh /*
293e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
294facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
295facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
296e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
297e5ae5735Sdrh */
298e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
299facf0307Sdrh static int scratchAllocOut = 0;
300e5ae5735Sdrh #endif
301e5ae5735Sdrh 
302e5ae5735Sdrh 
303e5ae5735Sdrh /*
304e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
305e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
306e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
307e5ae5735Sdrh ** routine is intended to get memory to old large transient data
308e5ae5735Sdrh ** structures that would not normally fit on the stack of an
309e5ae5735Sdrh ** embedded processor.
310e5ae5735Sdrh */
311facf0307Sdrh void *sqlite3ScratchMalloc(int n){
312e5ae5735Sdrh   void *p;
313e5ae5735Sdrh   assert( n>0 );
3149ac3fe97Sdrh 
315e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3169ac3fe97Sdrh   /* Verify that no more than one scratch allocation per thread
3179ac3fe97Sdrh   ** is outstanding at one time.  (This is only checked in the
3189ac3fe97Sdrh   ** single-threaded case since checking in the multi-threaded case
3199ac3fe97Sdrh   ** would be much more complicated.) */
320facf0307Sdrh   assert( scratchAllocOut==0 );
321e5ae5735Sdrh #endif
3229ac3fe97Sdrh 
323075c23afSdanielk1977   if( sqlite3GlobalConfig.szScratch<n ){
324f7141990Sdrh     goto scratch_overflow;
325f7141990Sdrh   }else{
326e5ae5735Sdrh     sqlite3_mutex_enter(mem0.mutex);
327f7141990Sdrh     if( mem0.nScratchFree==0 ){
328f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
329f7141990Sdrh       goto scratch_overflow;
330e5ae5735Sdrh     }else{
3319ac3fe97Sdrh       int i;
3329ac3fe97Sdrh       i = mem0.aScratchFree[--mem0.nScratchFree];
333075c23afSdanielk1977       i *= sqlite3GlobalConfig.szScratch;
334f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
335e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
3368183e339Sdanielk1977       sqlite3_mutex_leave(mem0.mutex);
337075c23afSdanielk1977       p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
338e5ae5735Sdrh     }
339f7141990Sdrh   }
340f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
341f7141990Sdrh   scratchAllocOut = p!=0;
342f7141990Sdrh #endif
343f7141990Sdrh 
344f7141990Sdrh   return p;
345f7141990Sdrh 
346f7141990Sdrh scratch_overflow:
347075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
348f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
349e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
350f7141990Sdrh     n = mallocWithAlarm(n, &p);
351f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
3529ac3fe97Sdrh     sqlite3_mutex_leave(mem0.mutex);
353f7141990Sdrh   }else{
354075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
355f7141990Sdrh   }
356f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
357f7141990Sdrh   scratchAllocOut = p!=0;
358f7141990Sdrh #endif
359e5ae5735Sdrh   return p;
360e5ae5735Sdrh }
361facf0307Sdrh void sqlite3ScratchFree(void *p){
362e5ae5735Sdrh   if( p ){
3639ac3fe97Sdrh 
364e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3659ac3fe97Sdrh     /* Verify that no more than one scratch allocation per thread
3669ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3679ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3689ac3fe97Sdrh     ** would be much more complicated.) */
369facf0307Sdrh     assert( scratchAllocOut==1 );
370facf0307Sdrh     scratchAllocOut = 0;
371e5ae5735Sdrh #endif
3729ac3fe97Sdrh 
373075c23afSdanielk1977     if( sqlite3GlobalConfig.pScratch==0
374075c23afSdanielk1977            || p<sqlite3GlobalConfig.pScratch
3759ac3fe97Sdrh            || p>=(void*)mem0.aScratchFree ){
376075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
377f7141990Sdrh         int iSize = sqlite3MallocSize(p);
378f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
379f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
380f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
381075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
382f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
383f7141990Sdrh       }else{
384075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
385f7141990Sdrh       }
3869ac3fe97Sdrh     }else{
3879ac3fe97Sdrh       int i;
388*1bd10f8aSdrh       i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
389075c23afSdanielk1977       i /= sqlite3GlobalConfig.szScratch;
390075c23afSdanielk1977       assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
391f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
39200e13613Sdanielk1977       assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
3939ac3fe97Sdrh       mem0.aScratchFree[mem0.nScratchFree++] = i;
394f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
3959ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
3969ac3fe97Sdrh     }
397e5ae5735Sdrh   }
398e5ae5735Sdrh }
399e5ae5735Sdrh 
400e5ae5735Sdrh /*
401f7141990Sdrh ** Allocate memory to be used by the page cache.  Make use of the
402f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
403f7141990Sdrh ** and that memory is of the right size and is not completely
404f7141990Sdrh ** consumed.  Otherwise, failover to sqlite3Malloc().
405facf0307Sdrh */
4068c0a791aSdanielk1977 #if 0
407f7141990Sdrh void *sqlite3PageMalloc(int n){
408f7141990Sdrh   void *p;
409f7141990Sdrh   assert( n>0 );
410f7141990Sdrh   assert( (n & (n-1))==0 );
411f7141990Sdrh   assert( n>=512 && n<=32768 );
412f7141990Sdrh 
413075c23afSdanielk1977   if( sqlite3GlobalConfig.szPage<n ){
414f7141990Sdrh     goto page_overflow;
415f7141990Sdrh   }else{
416f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
417f7141990Sdrh     if( mem0.nPageFree==0 ){
418f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
419f7141990Sdrh       goto page_overflow;
420f7141990Sdrh     }else{
421f7141990Sdrh       int i;
422f7141990Sdrh       i = mem0.aPageFree[--mem0.nPageFree];
423f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
424075c23afSdanielk1977       i *= sqlite3GlobalConfig.szPage;
425e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
426f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
427075c23afSdanielk1977       p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
428f7141990Sdrh     }
429f7141990Sdrh   }
430f7141990Sdrh   return p;
431f7141990Sdrh 
432f7141990Sdrh page_overflow:
433075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
434f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
435e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
436f7141990Sdrh     n = mallocWithAlarm(n, &p);
437f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
438f7141990Sdrh     sqlite3_mutex_leave(mem0.mutex);
439f7141990Sdrh   }else{
440075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
441f7141990Sdrh   }
442f7141990Sdrh   return p;
443f7141990Sdrh }
444f7141990Sdrh void sqlite3PageFree(void *p){
445f7141990Sdrh   if( p ){
446075c23afSdanielk1977     if( sqlite3GlobalConfig.pPage==0
447075c23afSdanielk1977            || p<sqlite3GlobalConfig.pPage
448f7141990Sdrh            || p>=(void*)mem0.aPageFree ){
4494b9507a0Sdanielk1977       /* In this case, the page allocation was obtained from a regular
4504b9507a0Sdanielk1977       ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
4514b9507a0Sdanielk1977       ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
4524b9507a0Sdanielk1977       */
453075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
454f7141990Sdrh         int iSize = sqlite3MallocSize(p);
455f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
456f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
457f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
458075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
459f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
460f7141990Sdrh       }else{
461075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
462f7141990Sdrh       }
463f7141990Sdrh     }else{
464075c23afSdanielk1977       /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
4654b9507a0Sdanielk1977       ** buffer. In this case all that is add the index of the page in
466075c23afSdanielk1977       ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
4674b9507a0Sdanielk1977       ** in the mem0.aPageFree[] array.
4684b9507a0Sdanielk1977       */
469f7141990Sdrh       int i;
470075c23afSdanielk1977       i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
471075c23afSdanielk1977       i /= sqlite3GlobalConfig.szPage;
472075c23afSdanielk1977       assert( i>=0 && i<sqlite3GlobalConfig.nPage );
473f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
474075c23afSdanielk1977       assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
475f7141990Sdrh       mem0.aPageFree[mem0.nPageFree++] = i;
476f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
477f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
4785f4bcf15Sdrh #if !defined(NDEBUG) && 0
4794b9507a0Sdanielk1977       /* Assert that a duplicate was not just inserted into aPageFree[]. */
4804b9507a0Sdanielk1977       for(i=0; i<mem0.nPageFree-1; i++){
4814b9507a0Sdanielk1977         assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
4824b9507a0Sdanielk1977       }
4834b9507a0Sdanielk1977 #endif
484f7141990Sdrh     }
485f7141990Sdrh   }
486facf0307Sdrh }
4878c0a791aSdanielk1977 #endif
488facf0307Sdrh 
489facf0307Sdrh /*
490633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
491633e6d57Sdrh */
4924150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
493633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
494633e6d57Sdrh   return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
495633e6d57Sdrh }
4964150ebf8Sdrh #else
4974150ebf8Sdrh #define isLookaside(A,B) 0
4984150ebf8Sdrh #endif
499633e6d57Sdrh 
500633e6d57Sdrh /*
501fec00eabSdrh ** Return the size of a memory allocation previously obtained from
502fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
503fec00eabSdrh */
504fec00eabSdrh int sqlite3MallocSize(void *p){
505075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
506fec00eabSdrh }
507633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
5086a1e071fSdrh   if( p==0 ){
5096a1e071fSdrh     return 0;
5106a1e071fSdrh   }else if( isLookaside(db, p) ){
511633e6d57Sdrh     return db->lookaside.sz;
512633e6d57Sdrh   }else{
513075c23afSdanielk1977     return sqlite3GlobalConfig.m.xSize(p);
514633e6d57Sdrh   }
515633e6d57Sdrh }
516fec00eabSdrh 
517fec00eabSdrh /*
518fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
519fec00eabSdrh */
520fec00eabSdrh void sqlite3_free(void *p){
521fec00eabSdrh   if( p==0 ) return;
522075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
523fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
524f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
525075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
526fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
527fec00eabSdrh   }else{
528075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
529fec00eabSdrh   }
530fec00eabSdrh }
531fec00eabSdrh 
532fec00eabSdrh /*
533633e6d57Sdrh ** Free memory that might be associated with a particular database
534633e6d57Sdrh ** connection.
535633e6d57Sdrh */
536633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
537633e6d57Sdrh   if( isLookaside(db, p) ){
538633e6d57Sdrh     LookasideSlot *pBuf = (LookasideSlot*)p;
539633e6d57Sdrh     pBuf->pNext = db->lookaside.pFree;
540633e6d57Sdrh     db->lookaside.pFree = pBuf;
541633e6d57Sdrh     db->lookaside.nOut--;
542633e6d57Sdrh   }else{
543633e6d57Sdrh     sqlite3_free(p);
544633e6d57Sdrh   }
545633e6d57Sdrh }
546633e6d57Sdrh 
547633e6d57Sdrh /*
548fec00eabSdrh ** Change the size of an existing memory allocation
549fec00eabSdrh */
550fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
551fec00eabSdrh   int nOld, nNew;
552fec00eabSdrh   void *pNew;
553fec00eabSdrh   if( pOld==0 ){
554fec00eabSdrh     return sqlite3Malloc(nBytes);
555fec00eabSdrh   }
556fec00eabSdrh   if( nBytes<=0 ){
557fec00eabSdrh     sqlite3_free(pOld);
558fec00eabSdrh     return 0;
559fec00eabSdrh   }
560fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
561075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
562fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
563f7141990Sdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
564075c23afSdanielk1977     nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
565fec00eabSdrh     if( nOld==nNew ){
566fec00eabSdrh       pNew = pOld;
567fec00eabSdrh     }else{
568f7141990Sdrh       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
569f7141990Sdrh             mem0.alarmThreshold ){
570fec00eabSdrh         sqlite3MallocAlarm(nNew-nOld);
571fec00eabSdrh       }
572075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
573d09414cdSdanielk1977       if( pNew==0 && mem0.alarmCallback ){
574fec00eabSdrh         sqlite3MallocAlarm(nBytes);
575075c23afSdanielk1977         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
576fec00eabSdrh       }
577fec00eabSdrh       if( pNew ){
578c702c7ccSdrh         nNew = sqlite3MallocSize(pNew);
579f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
580fec00eabSdrh       }
581fec00eabSdrh     }
582fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
583fec00eabSdrh   }else{
584075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
585fec00eabSdrh   }
586fec00eabSdrh   return pNew;
587fec00eabSdrh }
588fec00eabSdrh 
589fec00eabSdrh /*
590fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
591fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
592fec00eabSdrh */
593fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
594fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
595fec00eabSdrh   if( sqlite3_initialize() ) return 0;
596fec00eabSdrh #endif
597fec00eabSdrh   return sqlite3Realloc(pOld, n);
598fec00eabSdrh }
599fec00eabSdrh 
600a3152895Sdrh 
601a3152895Sdrh /*
60217435752Sdrh ** Allocate and zero memory.
603a3152895Sdrh */
604fec00eabSdrh void *sqlite3MallocZero(int n){
605fec00eabSdrh   void *p = sqlite3Malloc(n);
606a3152895Sdrh   if( p ){
607a3152895Sdrh     memset(p, 0, 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 */
616fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
617a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
61817435752Sdrh   if( p ){
61917435752Sdrh     memset(p, 0, 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 */
642fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
643633e6d57Sdrh   void *p;
6444150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
645633e6d57Sdrh   if( db ){
646633e6d57Sdrh     LookasideSlot *pBuf;
647633e6d57Sdrh     if( db->mallocFailed ){
648633e6d57Sdrh       return 0;
649633e6d57Sdrh     }
650633e6d57Sdrh     if( db->lookaside.bEnabled && n<=db->lookaside.sz
651633e6d57Sdrh          && (pBuf = db->lookaside.pFree)!=0 ){
652633e6d57Sdrh       db->lookaside.pFree = pBuf->pNext;
653633e6d57Sdrh       db->lookaside.nOut++;
654633e6d57Sdrh       if( db->lookaside.nOut>db->lookaside.mxOut ){
655633e6d57Sdrh         db->lookaside.mxOut = db->lookaside.nOut;
656633e6d57Sdrh       }
657633e6d57Sdrh       return (void*)pBuf;
658633e6d57Sdrh     }
659633e6d57Sdrh   }
660ddecae79Sdrh #else
661ddecae79Sdrh   if( db && db->mallocFailed ){
662ddecae79Sdrh     return 0;
663ddecae79Sdrh   }
6644150ebf8Sdrh #endif
665fec00eabSdrh   p = sqlite3Malloc(n);
666f3a65f7eSdrh   if( !p && db ){
66717435752Sdrh     db->mallocFailed = 1;
66817435752Sdrh   }
66917435752Sdrh   return p;
67017435752Sdrh }
67117435752Sdrh 
67226783a58Sdanielk1977 /*
67326783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
67426783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
67526783a58Sdanielk1977 */
676a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
677a1644fd8Sdanielk1977   void *pNew = 0;
678a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
679633e6d57Sdrh     if( p==0 ){
680633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
681633e6d57Sdrh     }
682633e6d57Sdrh     if( isLookaside(db, p) ){
683633e6d57Sdrh       if( n<=db->lookaside.sz ){
684633e6d57Sdrh         return p;
685633e6d57Sdrh       }
686633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
687633e6d57Sdrh       if( pNew ){
688633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
689633e6d57Sdrh         sqlite3DbFree(db, p);
690633e6d57Sdrh       }
691633e6d57Sdrh     }else{
692a1644fd8Sdanielk1977       pNew = sqlite3_realloc(p, n);
693a1644fd8Sdanielk1977       if( !pNew ){
694a1644fd8Sdanielk1977         db->mallocFailed = 1;
695a1644fd8Sdanielk1977       }
696a1644fd8Sdanielk1977     }
697633e6d57Sdrh   }
698a1644fd8Sdanielk1977   return pNew;
699a1644fd8Sdanielk1977 }
700a1644fd8Sdanielk1977 
70117435752Sdrh /*
70217435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
70317435752Sdrh ** and set the mallocFailed flag in the database connection.
70417435752Sdrh */
70517435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
706a3152895Sdrh   void *pNew;
707a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
708a3152895Sdrh   if( !pNew ){
709633e6d57Sdrh     sqlite3DbFree(db, p);
710a3152895Sdrh   }
711a3152895Sdrh   return pNew;
712a3152895Sdrh }
713a3152895Sdrh 
714a3152895Sdrh /*
715a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
716a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
717a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
718a3152895Sdrh ** called via macros that record the current file and line number in the
719a3152895Sdrh ** ThreadData structure.
720a3152895Sdrh */
721633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
722a3152895Sdrh   char *zNew;
723633e6d57Sdrh   size_t n;
724633e6d57Sdrh   if( z==0 ){
725633e6d57Sdrh     return 0;
726a3152895Sdrh   }
727ea678832Sdrh   n = (db ? sqlite3Strlen(db, z) : sqlite3Strlen30(z))+1;
728633e6d57Sdrh   assert( (n&0x7fffffff)==n );
729633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
730a3152895Sdrh   if( zNew ){
731a3152895Sdrh     memcpy(zNew, z, n);
7321e536953Sdanielk1977   }
7331e536953Sdanielk1977   return zNew;
7341e536953Sdanielk1977 }
7351e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
736633e6d57Sdrh   char *zNew;
737633e6d57Sdrh   if( z==0 ){
738633e6d57Sdrh     return 0;
739633e6d57Sdrh   }
740633e6d57Sdrh   assert( (n&0x7fffffff)==n );
741633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
742633e6d57Sdrh   if( zNew ){
743633e6d57Sdrh     memcpy(zNew, z, n);
744633e6d57Sdrh     zNew[n] = 0;
7451e536953Sdanielk1977   }
7461e536953Sdanielk1977   return zNew;
7471e536953Sdanielk1977 }
7481e536953Sdanielk1977 
749a3152895Sdrh /*
750f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
751f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
752f089aa45Sdrh ** point to that string.
753a3152895Sdrh */
754f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
755a3152895Sdrh   va_list ap;
756f089aa45Sdrh   char *z;
757a3152895Sdrh 
758f089aa45Sdrh   va_start(ap, zFormat);
759f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
760a3152895Sdrh   va_end(ap);
761633e6d57Sdrh   sqlite3DbFree(db, *pz);
762f089aa45Sdrh   *pz = z;
763a3152895Sdrh }
764a3152895Sdrh 
765a3152895Sdrh 
766a3152895Sdrh /*
767a3152895Sdrh ** This function must be called before exiting any API function (i.e.
76817435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
76917435752Sdrh ** sqlite3_realloc.
770a3152895Sdrh **
771a3152895Sdrh ** The returned value is normally a copy of the second argument to this
772a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
773a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
774a3152895Sdrh **
775a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
776a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
777a3152895Sdrh ** is set to SQLITE_NOMEM.
778a3152895Sdrh */
779a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
780a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
781a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
782a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
783a1644fd8Sdanielk1977   */
784a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
78598c21903Sdanielk1977   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
786a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
78717435752Sdrh     db->mallocFailed = 0;
788a3152895Sdrh     rc = SQLITE_NOMEM;
789a3152895Sdrh   }
790a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
791a3152895Sdrh }
792