xref: /sqlite-3.40.0/src/malloc.c (revision ddecae79)
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*ddecae79Sdrh ** $Id: malloc.c,v 1.44 2008/10/11 17:35:16 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,
28153c62c4Sdrh   sqlite3_int64 inUse,
29153c62c4Sdrh   int allocSize
30b21c8cd4Sdrh ){
31b21c8cd4Sdrh   sqlite3_release_memory(allocSize);
32b21c8cd4Sdrh }
33b21c8cd4Sdrh 
34b21c8cd4Sdrh /*
358468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or
368468024dSdanielk1977 ** negative value indicates no limit.
37a3152895Sdrh */
38a3152895Sdrh void sqlite3_soft_heap_limit(int n){
39b21c8cd4Sdrh   sqlite3_uint64 iLimit;
40b21c8cd4Sdrh   int overage;
41b21c8cd4Sdrh   if( n<0 ){
42b21c8cd4Sdrh     iLimit = 0;
43b21c8cd4Sdrh   }else{
44b21c8cd4Sdrh     iLimit = n;
45a3152895Sdrh   }
469ac3fe97Sdrh   sqlite3_initialize();
47b21c8cd4Sdrh   if( iLimit>0 ){
484a27a286Sshane     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
49b21c8cd4Sdrh   }else{
504a27a286Sshane     sqlite3MemoryAlarm(0, 0, 0);
51b21c8cd4Sdrh   }
52b21c8cd4Sdrh   overage = sqlite3_memory_used() - 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 #if 0
6767e3da7aSdanielk1977   nRet += sqlite3VdbeReleaseMemory(n);
6867e3da7aSdanielk1977 #endif
6967e3da7aSdanielk1977   nRet += sqlite3PcacheReleaseMemory(n-nRet);
70dfb316d4Sdanielk1977   return nRet;
711e536953Sdanielk1977 #else
721e536953Sdanielk1977   return SQLITE_OK;
731e536953Sdanielk1977 #endif
74a3152895Sdrh }
75a3152895Sdrh 
76fec00eabSdrh /*
77fec00eabSdrh ** State information local to the memory allocation subsystem.
78fec00eabSdrh */
795c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
8023bf0f41Sdanielk1977   /* Number of free pages for scratch and page-cache memory */
8123bf0f41Sdanielk1977   u32 nScratchFree;
8223bf0f41Sdanielk1977   u32 nPageFree;
8323bf0f41Sdanielk1977 
84fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
85fec00eabSdrh 
86fec00eabSdrh   /*
87fec00eabSdrh   ** The alarm callback and its arguments.  The mem0.mutex lock will
88fec00eabSdrh   ** be held while the callback is running.  Recursive calls into
89fec00eabSdrh   ** the memory subsystem are allowed, but no new callbacks will be
90fec00eabSdrh   ** issued.  The alarmBusy variable is set to prevent recursive
91fec00eabSdrh   ** callbacks.
92fec00eabSdrh   */
93fec00eabSdrh   sqlite3_int64 alarmThreshold;
94fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
95fec00eabSdrh   void *alarmArg;
96fec00eabSdrh   int alarmBusy;
97fec00eabSdrh 
98fec00eabSdrh   /*
99075c23afSdanielk1977   ** Pointers to the end of sqlite3GlobalConfig.pScratch and
100075c23afSdanielk1977   ** sqlite3GlobalConfig.pPage to a block of memory that records
1019ac3fe97Sdrh   ** which pages are available.
1029ac3fe97Sdrh   */
1039ac3fe97Sdrh   u32 *aScratchFree;
1049ac3fe97Sdrh   u32 *aPageFree;
10523bf0f41Sdanielk1977 } mem0 = { 62560955 };
1065c8f8587Sdanielk1977 
1075c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
108fec00eabSdrh 
109fec00eabSdrh /*
110fec00eabSdrh ** Initialize the memory allocation subsystem.
111fec00eabSdrh */
112fec00eabSdrh int sqlite3MallocInit(void){
113075c23afSdanielk1977   if( sqlite3GlobalConfig.m.xMalloc==0 ){
114fec00eabSdrh     sqlite3MemSetDefault();
115fec00eabSdrh   }
116fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
117075c23afSdanielk1977   if( sqlite3GlobalConfig.bCoreMutex ){
11859f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
119fec00eabSdrh   }
120075c23afSdanielk1977   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
121075c23afSdanielk1977       && sqlite3GlobalConfig.nScratch>=0 ){
1229ac3fe97Sdrh     int i;
123075c23afSdanielk1977     sqlite3GlobalConfig.szScratch -= 4;
124075c23afSdanielk1977     mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
125075c23afSdanielk1977                   [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
126075c23afSdanielk1977     for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
127075c23afSdanielk1977     mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
1289ac3fe97Sdrh   }else{
129075c23afSdanielk1977     sqlite3GlobalConfig.pScratch = 0;
130075c23afSdanielk1977     sqlite3GlobalConfig.szScratch = 0;
1319ac3fe97Sdrh   }
132075c23afSdanielk1977   if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
133075c23afSdanielk1977       && sqlite3GlobalConfig.nPage>=1 ){
1349ac3fe97Sdrh     int i;
1350a60a384Sdrh     int overhead;
136075c23afSdanielk1977     int sz = sqlite3GlobalConfig.szPage;
137075c23afSdanielk1977     int n = sqlite3GlobalConfig.nPage;
1380a60a384Sdrh     overhead = (4*n + sz - 1)/sz;
139075c23afSdanielk1977     sqlite3GlobalConfig.nPage -= overhead;
140075c23afSdanielk1977     mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
141075c23afSdanielk1977                   [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
142075c23afSdanielk1977     for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
143075c23afSdanielk1977     mem0.nPageFree = sqlite3GlobalConfig.nPage;
1449ac3fe97Sdrh   }else{
145075c23afSdanielk1977     sqlite3GlobalConfig.pPage = 0;
146075c23afSdanielk1977     sqlite3GlobalConfig.szPage = 0;
1479ac3fe97Sdrh   }
148075c23afSdanielk1977   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
149fec00eabSdrh }
150fec00eabSdrh 
151fec00eabSdrh /*
152fec00eabSdrh ** Deinitialize the memory allocation subsystem.
153fec00eabSdrh */
154fec00eabSdrh void sqlite3MallocEnd(void){
155075c23afSdanielk1977   sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
1569ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
157fec00eabSdrh }
158fec00eabSdrh 
159fec00eabSdrh /*
160fec00eabSdrh ** Return the amount of memory currently checked out.
161fec00eabSdrh */
162fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
163f7141990Sdrh   int n, mx;
164c376a198Sdrh   sqlite3_int64 res;
165f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
166c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
167c376a198Sdrh   return res;
168fec00eabSdrh }
169fec00eabSdrh 
170fec00eabSdrh /*
171fec00eabSdrh ** Return the maximum amount of memory that has ever been
172fec00eabSdrh ** checked out since either the beginning of this process
173fec00eabSdrh ** or since the most recent reset.
174fec00eabSdrh */
175fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
176f7141990Sdrh   int n, mx;
177c376a198Sdrh   sqlite3_int64 res;
178f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
1797986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
180c376a198Sdrh   return res;
181fec00eabSdrh }
182fec00eabSdrh 
183fec00eabSdrh /*
184fec00eabSdrh ** Change the alarm callback
185fec00eabSdrh */
1864a27a286Sshane int sqlite3MemoryAlarm(
187fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
188fec00eabSdrh   void *pArg,
189fec00eabSdrh   sqlite3_int64 iThreshold
190fec00eabSdrh ){
191fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
192fec00eabSdrh   mem0.alarmCallback = xCallback;
193fec00eabSdrh   mem0.alarmArg = pArg;
194fec00eabSdrh   mem0.alarmThreshold = iThreshold;
195fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
196fec00eabSdrh   return SQLITE_OK;
197fec00eabSdrh }
198fec00eabSdrh 
199fec00eabSdrh /*
2004a27a286Sshane ** Deprecated external interface.  Internal/core SQLite code
2014a27a286Sshane ** should call sqlite3MemoryAlarm.
2024a27a286Sshane */
2034a27a286Sshane int sqlite3_memory_alarm(
2044a27a286Sshane   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
2054a27a286Sshane   void *pArg,
2064a27a286Sshane   sqlite3_int64 iThreshold
2074a27a286Sshane ){
2084a27a286Sshane   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
2094a27a286Sshane }
2104a27a286Sshane 
2114a27a286Sshane /*
212fec00eabSdrh ** Trigger the alarm
213fec00eabSdrh */
214fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
215fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
216fec00eabSdrh   sqlite3_int64 nowUsed;
217fec00eabSdrh   void *pArg;
218fec00eabSdrh   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
219fec00eabSdrh   mem0.alarmBusy = 1;
220fec00eabSdrh   xCallback = mem0.alarmCallback;
221f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
222fec00eabSdrh   pArg = mem0.alarmArg;
223fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
224fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
225fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
226fec00eabSdrh   mem0.alarmBusy = 0;
227fec00eabSdrh }
228fec00eabSdrh 
229fec00eabSdrh /*
230f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
231f7141990Sdrh ** lock is already held.
232fec00eabSdrh */
233f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
234fec00eabSdrh   int nFull;
235f7141990Sdrh   void *p;
236f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
237075c23afSdanielk1977   nFull = sqlite3GlobalConfig.m.xRoundup(n);
238f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
239f7141990Sdrh   if( mem0.alarmCallback!=0 ){
240f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
241f7141990Sdrh     if( nUsed+nFull >= mem0.alarmThreshold ){
242fec00eabSdrh       sqlite3MallocAlarm(nFull);
243fec00eabSdrh     }
244f7141990Sdrh   }
245075c23afSdanielk1977   p = sqlite3GlobalConfig.m.xMalloc(nFull);
246d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
247fec00eabSdrh     sqlite3MallocAlarm(nFull);
248075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(nFull);
249fec00eabSdrh   }
250c702c7ccSdrh   if( p ){
251c702c7ccSdrh     nFull = sqlite3MallocSize(p);
252c702c7ccSdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
253c702c7ccSdrh   }
254f7141990Sdrh   *pp = p;
255f7141990Sdrh   return nFull;
256fec00eabSdrh }
257f7141990Sdrh 
258f7141990Sdrh /*
259f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
260f7141990Sdrh ** assumes the memory subsystem has already been initialized.
261f7141990Sdrh */
262f7141990Sdrh void *sqlite3Malloc(int n){
263f7141990Sdrh   void *p;
264f7141990Sdrh   if( n<=0 ){
265f7141990Sdrh     p = 0;
266075c23afSdanielk1977   }else if( sqlite3GlobalConfig.bMemstat ){
267f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
268f7141990Sdrh     mallocWithAlarm(n, &p);
269fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
270fec00eabSdrh   }else{
271075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
272fec00eabSdrh   }
273fec00eabSdrh   return p;
274fec00eabSdrh }
275fec00eabSdrh 
276fec00eabSdrh /*
277fec00eabSdrh ** This version of the memory allocation is for use by the application.
278fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
279fec00eabSdrh ** allocation.
280fec00eabSdrh */
281fec00eabSdrh void *sqlite3_malloc(int n){
282fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
283fec00eabSdrh   if( sqlite3_initialize() ) return 0;
284fec00eabSdrh #endif
285fec00eabSdrh   return sqlite3Malloc(n);
286fec00eabSdrh }
287fec00eabSdrh 
288fec00eabSdrh /*
289e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
290facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
291facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
292e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
293e5ae5735Sdrh */
294e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
295facf0307Sdrh static int scratchAllocOut = 0;
296e5ae5735Sdrh #endif
297e5ae5735Sdrh 
298e5ae5735Sdrh 
299e5ae5735Sdrh /*
300e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
301e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
302e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
303e5ae5735Sdrh ** routine is intended to get memory to old large transient data
304e5ae5735Sdrh ** structures that would not normally fit on the stack of an
305e5ae5735Sdrh ** embedded processor.
306e5ae5735Sdrh */
307facf0307Sdrh void *sqlite3ScratchMalloc(int n){
308e5ae5735Sdrh   void *p;
309e5ae5735Sdrh   assert( n>0 );
3109ac3fe97Sdrh 
311e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3129ac3fe97Sdrh   /* Verify that no more than one scratch allocation per thread
3139ac3fe97Sdrh   ** is outstanding at one time.  (This is only checked in the
3149ac3fe97Sdrh   ** single-threaded case since checking in the multi-threaded case
3159ac3fe97Sdrh   ** would be much more complicated.) */
316facf0307Sdrh   assert( scratchAllocOut==0 );
317e5ae5735Sdrh #endif
3189ac3fe97Sdrh 
319075c23afSdanielk1977   if( sqlite3GlobalConfig.szScratch<n ){
320f7141990Sdrh     goto scratch_overflow;
321f7141990Sdrh   }else{
322e5ae5735Sdrh     sqlite3_mutex_enter(mem0.mutex);
323f7141990Sdrh     if( mem0.nScratchFree==0 ){
324f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
325f7141990Sdrh       goto scratch_overflow;
326e5ae5735Sdrh     }else{
3279ac3fe97Sdrh       int i;
3289ac3fe97Sdrh       i = mem0.aScratchFree[--mem0.nScratchFree];
329075c23afSdanielk1977       i *= sqlite3GlobalConfig.szScratch;
330f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
331e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
3328183e339Sdanielk1977       sqlite3_mutex_leave(mem0.mutex);
333075c23afSdanielk1977       p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
334e5ae5735Sdrh     }
335f7141990Sdrh   }
336f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
337f7141990Sdrh   scratchAllocOut = p!=0;
338f7141990Sdrh #endif
339f7141990Sdrh 
340f7141990Sdrh   return p;
341f7141990Sdrh 
342f7141990Sdrh scratch_overflow:
343075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
344f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
345e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
346f7141990Sdrh     n = mallocWithAlarm(n, &p);
347f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
3489ac3fe97Sdrh     sqlite3_mutex_leave(mem0.mutex);
349f7141990Sdrh   }else{
350075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
351f7141990Sdrh   }
352f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
353f7141990Sdrh   scratchAllocOut = p!=0;
354f7141990Sdrh #endif
355e5ae5735Sdrh   return p;
356e5ae5735Sdrh }
357facf0307Sdrh void sqlite3ScratchFree(void *p){
358e5ae5735Sdrh   if( p ){
3599ac3fe97Sdrh 
360e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3619ac3fe97Sdrh     /* Verify that no more than one scratch allocation per thread
3629ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3639ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3649ac3fe97Sdrh     ** would be much more complicated.) */
365facf0307Sdrh     assert( scratchAllocOut==1 );
366facf0307Sdrh     scratchAllocOut = 0;
367e5ae5735Sdrh #endif
3689ac3fe97Sdrh 
369075c23afSdanielk1977     if( sqlite3GlobalConfig.pScratch==0
370075c23afSdanielk1977            || p<sqlite3GlobalConfig.pScratch
3719ac3fe97Sdrh            || p>=(void*)mem0.aScratchFree ){
372075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
373f7141990Sdrh         int iSize = sqlite3MallocSize(p);
374f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
375f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
376f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
377075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
378f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
379f7141990Sdrh       }else{
380075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
381f7141990Sdrh       }
3829ac3fe97Sdrh     }else{
3839ac3fe97Sdrh       int i;
384075c23afSdanielk1977       i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
385075c23afSdanielk1977       i /= sqlite3GlobalConfig.szScratch;
386075c23afSdanielk1977       assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
387f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
388075c23afSdanielk1977       assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch );
3899ac3fe97Sdrh       mem0.aScratchFree[mem0.nScratchFree++] = i;
390f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
3919ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
3929ac3fe97Sdrh     }
393e5ae5735Sdrh   }
394e5ae5735Sdrh }
395e5ae5735Sdrh 
396e5ae5735Sdrh /*
397f7141990Sdrh ** Allocate memory to be used by the page cache.  Make use of the
398f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
399f7141990Sdrh ** and that memory is of the right size and is not completely
400f7141990Sdrh ** consumed.  Otherwise, failover to sqlite3Malloc().
401facf0307Sdrh */
4028c0a791aSdanielk1977 #if 0
403f7141990Sdrh void *sqlite3PageMalloc(int n){
404f7141990Sdrh   void *p;
405f7141990Sdrh   assert( n>0 );
406f7141990Sdrh   assert( (n & (n-1))==0 );
407f7141990Sdrh   assert( n>=512 && n<=32768 );
408f7141990Sdrh 
409075c23afSdanielk1977   if( sqlite3GlobalConfig.szPage<n ){
410f7141990Sdrh     goto page_overflow;
411f7141990Sdrh   }else{
412f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
413f7141990Sdrh     if( mem0.nPageFree==0 ){
414f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
415f7141990Sdrh       goto page_overflow;
416f7141990Sdrh     }else{
417f7141990Sdrh       int i;
418f7141990Sdrh       i = mem0.aPageFree[--mem0.nPageFree];
419f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
420075c23afSdanielk1977       i *= sqlite3GlobalConfig.szPage;
421e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
422f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
423075c23afSdanielk1977       p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
424f7141990Sdrh     }
425f7141990Sdrh   }
426f7141990Sdrh   return p;
427f7141990Sdrh 
428f7141990Sdrh page_overflow:
429075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
430f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
431e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
432f7141990Sdrh     n = mallocWithAlarm(n, &p);
433f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
434f7141990Sdrh     sqlite3_mutex_leave(mem0.mutex);
435f7141990Sdrh   }else{
436075c23afSdanielk1977     p = sqlite3GlobalConfig.m.xMalloc(n);
437f7141990Sdrh   }
438f7141990Sdrh   return p;
439f7141990Sdrh }
440f7141990Sdrh void sqlite3PageFree(void *p){
441f7141990Sdrh   if( p ){
442075c23afSdanielk1977     if( sqlite3GlobalConfig.pPage==0
443075c23afSdanielk1977            || p<sqlite3GlobalConfig.pPage
444f7141990Sdrh            || p>=(void*)mem0.aPageFree ){
4454b9507a0Sdanielk1977       /* In this case, the page allocation was obtained from a regular
4464b9507a0Sdanielk1977       ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
4474b9507a0Sdanielk1977       ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
4484b9507a0Sdanielk1977       */
449075c23afSdanielk1977       if( sqlite3GlobalConfig.bMemstat ){
450f7141990Sdrh         int iSize = sqlite3MallocSize(p);
451f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
452f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
453f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
454075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
455f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
456f7141990Sdrh       }else{
457075c23afSdanielk1977         sqlite3GlobalConfig.m.xFree(p);
458f7141990Sdrh       }
459f7141990Sdrh     }else{
460075c23afSdanielk1977       /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
4614b9507a0Sdanielk1977       ** buffer. In this case all that is add the index of the page in
462075c23afSdanielk1977       ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
4634b9507a0Sdanielk1977       ** in the mem0.aPageFree[] array.
4644b9507a0Sdanielk1977       */
465f7141990Sdrh       int i;
466075c23afSdanielk1977       i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
467075c23afSdanielk1977       i /= sqlite3GlobalConfig.szPage;
468075c23afSdanielk1977       assert( i>=0 && i<sqlite3GlobalConfig.nPage );
469f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
470075c23afSdanielk1977       assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
471f7141990Sdrh       mem0.aPageFree[mem0.nPageFree++] = i;
472f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
473f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
4745f4bcf15Sdrh #if !defined(NDEBUG) && 0
4754b9507a0Sdanielk1977       /* Assert that a duplicate was not just inserted into aPageFree[]. */
4764b9507a0Sdanielk1977       for(i=0; i<mem0.nPageFree-1; i++){
4774b9507a0Sdanielk1977         assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
4784b9507a0Sdanielk1977       }
4794b9507a0Sdanielk1977 #endif
480f7141990Sdrh     }
481f7141990Sdrh   }
482facf0307Sdrh }
4838c0a791aSdanielk1977 #endif
484facf0307Sdrh 
485facf0307Sdrh /*
486633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
487633e6d57Sdrh */
4884150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
489633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
490633e6d57Sdrh   return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
491633e6d57Sdrh }
4924150ebf8Sdrh #else
4934150ebf8Sdrh #define isLookaside(A,B) 0
4944150ebf8Sdrh #endif
495633e6d57Sdrh 
496633e6d57Sdrh /*
497fec00eabSdrh ** Return the size of a memory allocation previously obtained from
498fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
499fec00eabSdrh */
500fec00eabSdrh int sqlite3MallocSize(void *p){
501075c23afSdanielk1977   return sqlite3GlobalConfig.m.xSize(p);
502fec00eabSdrh }
503633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
504633e6d57Sdrh   if( isLookaside(db, p) ){
505633e6d57Sdrh     return db->lookaside.sz;
506633e6d57Sdrh   }else{
507075c23afSdanielk1977     return sqlite3GlobalConfig.m.xSize(p);
508633e6d57Sdrh   }
509633e6d57Sdrh }
510fec00eabSdrh 
511fec00eabSdrh /*
512fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
513fec00eabSdrh */
514fec00eabSdrh void sqlite3_free(void *p){
515fec00eabSdrh   if( p==0 ) return;
516075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
517fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
518f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
519075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
520fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
521fec00eabSdrh   }else{
522075c23afSdanielk1977     sqlite3GlobalConfig.m.xFree(p);
523fec00eabSdrh   }
524fec00eabSdrh }
525fec00eabSdrh 
526fec00eabSdrh /*
527633e6d57Sdrh ** Free memory that might be associated with a particular database
528633e6d57Sdrh ** connection.
529633e6d57Sdrh */
530633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
531633e6d57Sdrh   if( isLookaside(db, p) ){
532633e6d57Sdrh     LookasideSlot *pBuf = (LookasideSlot*)p;
533633e6d57Sdrh     pBuf->pNext = db->lookaside.pFree;
534633e6d57Sdrh     db->lookaside.pFree = pBuf;
535633e6d57Sdrh     db->lookaside.nOut--;
536633e6d57Sdrh   }else{
537633e6d57Sdrh     sqlite3_free(p);
538633e6d57Sdrh   }
539633e6d57Sdrh }
540633e6d57Sdrh 
541633e6d57Sdrh /*
542fec00eabSdrh ** Change the size of an existing memory allocation
543fec00eabSdrh */
544fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
545fec00eabSdrh   int nOld, nNew;
546fec00eabSdrh   void *pNew;
547fec00eabSdrh   if( pOld==0 ){
548fec00eabSdrh     return sqlite3Malloc(nBytes);
549fec00eabSdrh   }
550fec00eabSdrh   if( nBytes<=0 ){
551fec00eabSdrh     sqlite3_free(pOld);
552fec00eabSdrh     return 0;
553fec00eabSdrh   }
554fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
555075c23afSdanielk1977   if( sqlite3GlobalConfig.bMemstat ){
556fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
557f7141990Sdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
558075c23afSdanielk1977     nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
559fec00eabSdrh     if( nOld==nNew ){
560fec00eabSdrh       pNew = pOld;
561fec00eabSdrh     }else{
562f7141990Sdrh       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
563f7141990Sdrh             mem0.alarmThreshold ){
564fec00eabSdrh         sqlite3MallocAlarm(nNew-nOld);
565fec00eabSdrh       }
566075c23afSdanielk1977       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
567d09414cdSdanielk1977       if( pNew==0 && mem0.alarmCallback ){
568fec00eabSdrh         sqlite3MallocAlarm(nBytes);
569075c23afSdanielk1977         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
570fec00eabSdrh       }
571fec00eabSdrh       if( pNew ){
572c702c7ccSdrh         nNew = sqlite3MallocSize(pNew);
573f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
574fec00eabSdrh       }
575fec00eabSdrh     }
576fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
577fec00eabSdrh   }else{
578075c23afSdanielk1977     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
579fec00eabSdrh   }
580fec00eabSdrh   return pNew;
581fec00eabSdrh }
582fec00eabSdrh 
583fec00eabSdrh /*
584fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
585fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
586fec00eabSdrh */
587fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
588fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
589fec00eabSdrh   if( sqlite3_initialize() ) return 0;
590fec00eabSdrh #endif
591fec00eabSdrh   return sqlite3Realloc(pOld, n);
592fec00eabSdrh }
593fec00eabSdrh 
594a3152895Sdrh 
595a3152895Sdrh /*
59617435752Sdrh ** Allocate and zero memory.
597a3152895Sdrh */
598fec00eabSdrh void *sqlite3MallocZero(int n){
599fec00eabSdrh   void *p = sqlite3Malloc(n);
600a3152895Sdrh   if( p ){
601a3152895Sdrh     memset(p, 0, n);
602a3152895Sdrh   }
603a3152895Sdrh   return p;
604a3152895Sdrh }
60517435752Sdrh 
60617435752Sdrh /*
60717435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
60817435752Sdrh ** the mallocFailed flag in the connection pointer.
60917435752Sdrh */
610fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
611a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
61217435752Sdrh   if( p ){
61317435752Sdrh     memset(p, 0, n);
61417435752Sdrh   }
61517435752Sdrh   return p;
61617435752Sdrh }
61717435752Sdrh 
61817435752Sdrh /*
61917435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
62017435752Sdrh ** the mallocFailed flag in the connection pointer.
621*ddecae79Sdrh **
622*ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
623*ddecae79Sdrh ** failure on the same database connection) then always return 0.
624*ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
625*ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
626*ddecae79Sdrh ** This is an important assumption.  There are many places in the
627*ddecae79Sdrh ** code that do things like this:
628*ddecae79Sdrh **
629*ddecae79Sdrh **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
630*ddecae79Sdrh **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
631*ddecae79Sdrh **         if( b ) a[10] = 9;
632*ddecae79Sdrh **
633*ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
634*ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
63517435752Sdrh */
636fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
637633e6d57Sdrh   void *p;
6384150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
639633e6d57Sdrh   if( db ){
640633e6d57Sdrh     LookasideSlot *pBuf;
641633e6d57Sdrh     if( db->mallocFailed ){
642633e6d57Sdrh       return 0;
643633e6d57Sdrh     }
644633e6d57Sdrh     if( db->lookaside.bEnabled && n<=db->lookaside.sz
645633e6d57Sdrh          && (pBuf = db->lookaside.pFree)!=0 ){
646633e6d57Sdrh       db->lookaside.pFree = pBuf->pNext;
647633e6d57Sdrh       db->lookaside.nOut++;
648633e6d57Sdrh       if( db->lookaside.nOut>db->lookaside.mxOut ){
649633e6d57Sdrh         db->lookaside.mxOut = db->lookaside.nOut;
650633e6d57Sdrh       }
651633e6d57Sdrh       return (void*)pBuf;
652633e6d57Sdrh     }
653633e6d57Sdrh   }
654*ddecae79Sdrh #else
655*ddecae79Sdrh   if( db && db->mallocFailed ){
656*ddecae79Sdrh     return 0;
657*ddecae79Sdrh   }
6584150ebf8Sdrh #endif
659fec00eabSdrh   p = sqlite3Malloc(n);
660f3a65f7eSdrh   if( !p && db ){
66117435752Sdrh     db->mallocFailed = 1;
66217435752Sdrh   }
66317435752Sdrh   return p;
66417435752Sdrh }
66517435752Sdrh 
66626783a58Sdanielk1977 /*
66726783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
66826783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
66926783a58Sdanielk1977 */
670a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
671a1644fd8Sdanielk1977   void *pNew = 0;
672a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
673633e6d57Sdrh     if( p==0 ){
674633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
675633e6d57Sdrh     }
676633e6d57Sdrh     if( isLookaside(db, p) ){
677633e6d57Sdrh       if( n<=db->lookaside.sz ){
678633e6d57Sdrh         return p;
679633e6d57Sdrh       }
680633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
681633e6d57Sdrh       if( pNew ){
682633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
683633e6d57Sdrh         sqlite3DbFree(db, p);
684633e6d57Sdrh       }
685633e6d57Sdrh     }else{
686a1644fd8Sdanielk1977       pNew = sqlite3_realloc(p, n);
687a1644fd8Sdanielk1977       if( !pNew ){
688a1644fd8Sdanielk1977         db->mallocFailed = 1;
689a1644fd8Sdanielk1977       }
690a1644fd8Sdanielk1977     }
691633e6d57Sdrh   }
692a1644fd8Sdanielk1977   return pNew;
693a1644fd8Sdanielk1977 }
694a1644fd8Sdanielk1977 
69517435752Sdrh /*
69617435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
69717435752Sdrh ** and set the mallocFailed flag in the database connection.
69817435752Sdrh */
69917435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
700a3152895Sdrh   void *pNew;
701a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
702a3152895Sdrh   if( !pNew ){
703633e6d57Sdrh     sqlite3DbFree(db, p);
704a3152895Sdrh   }
705a3152895Sdrh   return pNew;
706a3152895Sdrh }
707a3152895Sdrh 
708a3152895Sdrh /*
709a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
710a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
711a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
712a3152895Sdrh ** called via macros that record the current file and line number in the
713a3152895Sdrh ** ThreadData structure.
714a3152895Sdrh */
715633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
716a3152895Sdrh   char *zNew;
717633e6d57Sdrh   size_t n;
718633e6d57Sdrh   if( z==0 ){
719633e6d57Sdrh     return 0;
720a3152895Sdrh   }
721633e6d57Sdrh   n = strlen(z)+1;
722633e6d57Sdrh   assert( (n&0x7fffffff)==n );
723633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
724a3152895Sdrh   if( zNew ){
725a3152895Sdrh     memcpy(zNew, z, n);
7261e536953Sdanielk1977   }
7271e536953Sdanielk1977   return zNew;
7281e536953Sdanielk1977 }
7291e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
730633e6d57Sdrh   char *zNew;
731633e6d57Sdrh   if( z==0 ){
732633e6d57Sdrh     return 0;
733633e6d57Sdrh   }
734633e6d57Sdrh   assert( (n&0x7fffffff)==n );
735633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
736633e6d57Sdrh   if( zNew ){
737633e6d57Sdrh     memcpy(zNew, z, n);
738633e6d57Sdrh     zNew[n] = 0;
7391e536953Sdanielk1977   }
7401e536953Sdanielk1977   return zNew;
7411e536953Sdanielk1977 }
7421e536953Sdanielk1977 
743a3152895Sdrh /*
744f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
745f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
746f089aa45Sdrh ** point to that string.
747a3152895Sdrh */
748f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
749a3152895Sdrh   va_list ap;
750f089aa45Sdrh   char *z;
751a3152895Sdrh 
752f089aa45Sdrh   va_start(ap, zFormat);
753f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
754a3152895Sdrh   va_end(ap);
755633e6d57Sdrh   sqlite3DbFree(db, *pz);
756f089aa45Sdrh   *pz = z;
757a3152895Sdrh }
758a3152895Sdrh 
759a3152895Sdrh 
760a3152895Sdrh /*
761a3152895Sdrh ** This function must be called before exiting any API function (i.e.
76217435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
76317435752Sdrh ** sqlite3_realloc.
764a3152895Sdrh **
765a3152895Sdrh ** The returned value is normally a copy of the second argument to this
766a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
767a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
768a3152895Sdrh **
769a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
770a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
771a3152895Sdrh ** is set to SQLITE_NOMEM.
772a3152895Sdrh */
773a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
774a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
775a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
776a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
777a1644fd8Sdanielk1977   */
778a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
77998c21903Sdanielk1977   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
780a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
78117435752Sdrh     db->mallocFailed = 0;
782a3152895Sdrh     rc = SQLITE_NOMEM;
783a3152895Sdrh   }
784a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
785a3152895Sdrh }
786