xref: /sqlite-3.40.0/src/malloc.c (revision 8468024d)
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*8468024dSdanielk1977 ** $Id: malloc.c,v 1.24 2008/06/23 11:11:36 danielk1977 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 /*
35*8468024dSdanielk1977 ** Set the soft heap-size limit for the library. Passing a zero or
36*8468024dSdanielk1977 ** 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 ){
48b21c8cd4Sdrh     sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
49b21c8cd4Sdrh   }else{
50b21c8cd4Sdrh     sqlite3_memory_alarm(0, 0, 0);
51b21c8cd4Sdrh   }
52b21c8cd4Sdrh   overage = sqlite3_memory_used() - n;
53b21c8cd4Sdrh   if( overage>0 ){
54b21c8cd4Sdrh     sqlite3_release_memory(overage);
55b21c8cd4Sdrh   }
56a3152895Sdrh }
57a3152895Sdrh 
58a3152895Sdrh /*
59*8468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently
60*8468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to
61*8468024dSdanielk1977 ** cache database pages that are not currently in use.
62a3152895Sdrh */
63a3152895Sdrh int sqlite3_release_memory(int n){
6486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
65dfb316d4Sdanielk1977   int nRet = sqlite3VdbeReleaseMemory(n);
66dfb316d4Sdanielk1977   nRet += sqlite3PagerReleaseMemory(n-nRet);
67dfb316d4Sdanielk1977   return nRet;
681e536953Sdanielk1977 #else
691e536953Sdanielk1977   return SQLITE_OK;
701e536953Sdanielk1977 #endif
71a3152895Sdrh }
72a3152895Sdrh 
73fec00eabSdrh /*
74fec00eabSdrh ** State information local to the memory allocation subsystem.
75fec00eabSdrh */
76fec00eabSdrh static struct {
77fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
78fec00eabSdrh 
79fec00eabSdrh   /*
80fec00eabSdrh   ** The alarm callback and its arguments.  The mem0.mutex lock will
81fec00eabSdrh   ** be held while the callback is running.  Recursive calls into
82fec00eabSdrh   ** the memory subsystem are allowed, but no new callbacks will be
83fec00eabSdrh   ** issued.  The alarmBusy variable is set to prevent recursive
84fec00eabSdrh   ** callbacks.
85fec00eabSdrh   */
86fec00eabSdrh   sqlite3_int64 alarmThreshold;
87fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
88fec00eabSdrh   void *alarmArg;
89fec00eabSdrh   int alarmBusy;
90fec00eabSdrh 
91fec00eabSdrh   /*
929ac3fe97Sdrh   ** Pointers to the end of sqlite3Config.pScratch and
939ac3fe97Sdrh   ** sqlite3Config.pPage to a block of memory that records
949ac3fe97Sdrh   ** which pages are available.
959ac3fe97Sdrh   */
969ac3fe97Sdrh   u32 *aScratchFree;
979ac3fe97Sdrh   u32 *aPageFree;
989ac3fe97Sdrh 
999ac3fe97Sdrh   /* Number of free pages for scratch and page-cache memory */
1009ac3fe97Sdrh   u32 nScratchFree;
1019ac3fe97Sdrh   u32 nPageFree;
102fec00eabSdrh } mem0;
103fec00eabSdrh 
104fec00eabSdrh /*
105fec00eabSdrh ** Initialize the memory allocation subsystem.
106fec00eabSdrh */
107fec00eabSdrh int sqlite3MallocInit(void){
108fec00eabSdrh   if( sqlite3Config.m.xMalloc==0 ){
109fec00eabSdrh     sqlite3MemSetDefault();
110fec00eabSdrh   }
111fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
1129ac3fe97Sdrh   if( sqlite3Config.bCoreMutex ){
11359f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
114fec00eabSdrh   }
1159ac3fe97Sdrh   if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000
1169ac3fe97Sdrh       && sqlite3Config.nScratch>0 ){
1179ac3fe97Sdrh     int i;
1189ac3fe97Sdrh     mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
1199ac3fe97Sdrh                   [sqlite3Config.szScratch*sqlite3Config.nScratch];
1209ac3fe97Sdrh     for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
1219ac3fe97Sdrh     mem0.nScratchFree = sqlite3Config.nScratch;
1229ac3fe97Sdrh   }else{
1239ac3fe97Sdrh     sqlite3Config.pScratch = 0;
124f7141990Sdrh     sqlite3Config.szScratch = 0;
1259ac3fe97Sdrh   }
1269ac3fe97Sdrh   if( sqlite3Config.pPage && sqlite3Config.szPage>=512
1279ac3fe97Sdrh       && sqlite3Config.nPage>0 ){
1289ac3fe97Sdrh     int i;
1299ac3fe97Sdrh     mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
1309ac3fe97Sdrh                   [sqlite3Config.szPage*sqlite3Config.nPage];
1319ac3fe97Sdrh     for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
1329ac3fe97Sdrh     mem0.nPageFree = sqlite3Config.nPage;
1339ac3fe97Sdrh   }else{
1349ac3fe97Sdrh     sqlite3Config.pPage = 0;
135f7141990Sdrh     sqlite3Config.szPage = 0;
1369ac3fe97Sdrh   }
137fec00eabSdrh   return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
138fec00eabSdrh }
139fec00eabSdrh 
140fec00eabSdrh /*
141fec00eabSdrh ** Deinitialize the memory allocation subsystem.
142fec00eabSdrh */
143fec00eabSdrh void sqlite3MallocEnd(void){
144fec00eabSdrh   sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
1459ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
146fec00eabSdrh }
147fec00eabSdrh 
148fec00eabSdrh /*
149fec00eabSdrh ** Return the amount of memory currently checked out.
150fec00eabSdrh */
151fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
152f7141990Sdrh   int n, mx;
153f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
154f7141990Sdrh   return (sqlite3_int64)n;
155fec00eabSdrh }
156fec00eabSdrh 
157fec00eabSdrh /*
158fec00eabSdrh ** Return the maximum amount of memory that has ever been
159fec00eabSdrh ** checked out since either the beginning of this process
160fec00eabSdrh ** or since the most recent reset.
161fec00eabSdrh */
162fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
163f7141990Sdrh   int n, mx;
164f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
165f7141990Sdrh   return (sqlite3_int64)mx;
166fec00eabSdrh }
167fec00eabSdrh 
168fec00eabSdrh /*
169fec00eabSdrh ** Change the alarm callback
170fec00eabSdrh */
171fec00eabSdrh int sqlite3_memory_alarm(
172fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
173fec00eabSdrh   void *pArg,
174fec00eabSdrh   sqlite3_int64 iThreshold
175fec00eabSdrh ){
176fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
177fec00eabSdrh   mem0.alarmCallback = xCallback;
178fec00eabSdrh   mem0.alarmArg = pArg;
179fec00eabSdrh   mem0.alarmThreshold = iThreshold;
180fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
181fec00eabSdrh   return SQLITE_OK;
182fec00eabSdrh }
183fec00eabSdrh 
184fec00eabSdrh /*
185fec00eabSdrh ** Trigger the alarm
186fec00eabSdrh */
187fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
188fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
189fec00eabSdrh   sqlite3_int64 nowUsed;
190fec00eabSdrh   void *pArg;
191fec00eabSdrh   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
192fec00eabSdrh   mem0.alarmBusy = 1;
193fec00eabSdrh   xCallback = mem0.alarmCallback;
194f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
195fec00eabSdrh   pArg = mem0.alarmArg;
196fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
197fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
198fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
199fec00eabSdrh   mem0.alarmBusy = 0;
200fec00eabSdrh }
201fec00eabSdrh 
202fec00eabSdrh /*
203f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
204f7141990Sdrh ** lock is already held.
205fec00eabSdrh */
206f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
207fec00eabSdrh   int nFull;
208f7141990Sdrh   void *p;
209f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
210fec00eabSdrh   nFull = sqlite3Config.m.xRoundup(n);
211f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
212f7141990Sdrh   if( mem0.alarmCallback!=0 ){
213f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
214f7141990Sdrh     if( nUsed+nFull >= mem0.alarmThreshold ){
215fec00eabSdrh       sqlite3MallocAlarm(nFull);
216fec00eabSdrh     }
217f7141990Sdrh   }
218fec00eabSdrh   p = sqlite3Config.m.xMalloc(nFull);
219d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
220fec00eabSdrh     sqlite3MallocAlarm(nFull);
221d09414cdSdanielk1977     p = sqlite3Config.m.xMalloc(nFull);
222fec00eabSdrh   }
223f7141990Sdrh   if( p ) sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
224f7141990Sdrh   *pp = p;
225f7141990Sdrh   return nFull;
226fec00eabSdrh }
227f7141990Sdrh 
228f7141990Sdrh /*
229f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
230f7141990Sdrh ** assumes the memory subsystem has already been initialized.
231f7141990Sdrh */
232f7141990Sdrh void *sqlite3Malloc(int n){
233f7141990Sdrh   void *p;
234f7141990Sdrh   if( n<=0 ){
235f7141990Sdrh     p = 0;
236f7141990Sdrh   }else if( sqlite3Config.bMemstat ){
237f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
238f7141990Sdrh     mallocWithAlarm(n, &p);
239fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
240fec00eabSdrh   }else{
241fec00eabSdrh     p = sqlite3Config.m.xMalloc(n);
242fec00eabSdrh   }
243fec00eabSdrh   return p;
244fec00eabSdrh }
245fec00eabSdrh 
246fec00eabSdrh /*
247fec00eabSdrh ** This version of the memory allocation is for use by the application.
248fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
249fec00eabSdrh ** allocation.
250fec00eabSdrh */
251fec00eabSdrh void *sqlite3_malloc(int n){
252fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
253fec00eabSdrh   if( sqlite3_initialize() ) return 0;
254fec00eabSdrh #endif
255fec00eabSdrh   return sqlite3Malloc(n);
256fec00eabSdrh }
257fec00eabSdrh 
258fec00eabSdrh /*
259e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
260facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
261facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
262e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
263e5ae5735Sdrh */
264e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
265facf0307Sdrh static int scratchAllocOut = 0;
266e5ae5735Sdrh #endif
267e5ae5735Sdrh 
268e5ae5735Sdrh 
269e5ae5735Sdrh /*
270e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
271e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
272e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
273e5ae5735Sdrh ** routine is intended to get memory to old large transient data
274e5ae5735Sdrh ** structures that would not normally fit on the stack of an
275e5ae5735Sdrh ** embedded processor.
276e5ae5735Sdrh */
277facf0307Sdrh void *sqlite3ScratchMalloc(int n){
278e5ae5735Sdrh   void *p;
279e5ae5735Sdrh   assert( n>0 );
2809ac3fe97Sdrh 
281e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
2829ac3fe97Sdrh   /* Verify that no more than one scratch allocation per thread
2839ac3fe97Sdrh   ** is outstanding at one time.  (This is only checked in the
2849ac3fe97Sdrh   ** single-threaded case since checking in the multi-threaded case
2859ac3fe97Sdrh   ** would be much more complicated.) */
286facf0307Sdrh   assert( scratchAllocOut==0 );
287e5ae5735Sdrh #endif
2889ac3fe97Sdrh 
289f7141990Sdrh   if( sqlite3Config.szScratch<n ){
290f7141990Sdrh     goto scratch_overflow;
291f7141990Sdrh   }else{
292e5ae5735Sdrh     sqlite3_mutex_enter(mem0.mutex);
293f7141990Sdrh     if( mem0.nScratchFree==0 ){
294f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
295f7141990Sdrh       goto scratch_overflow;
296e5ae5735Sdrh     }else{
2979ac3fe97Sdrh       int i;
2989ac3fe97Sdrh       i = mem0.aScratchFree[--mem0.nScratchFree];
299f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
3009ac3fe97Sdrh       i *= sqlite3Config.szScratch;
301f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
3029ac3fe97Sdrh       p = (void*)&((char*)sqlite3Config.pScratch)[i];
303e5ae5735Sdrh     }
304f7141990Sdrh   }
305f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
306f7141990Sdrh   scratchAllocOut = p!=0;
307f7141990Sdrh #endif
308f7141990Sdrh 
309f7141990Sdrh   return p;
310f7141990Sdrh 
311f7141990Sdrh scratch_overflow:
312f7141990Sdrh   if( sqlite3Config.bMemstat ){
313f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
314f7141990Sdrh     n = mallocWithAlarm(n, &p);
315f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
3169ac3fe97Sdrh     sqlite3_mutex_leave(mem0.mutex);
317f7141990Sdrh   }else{
318f7141990Sdrh     p = sqlite3Config.m.xMalloc(n);
319f7141990Sdrh   }
320f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
321f7141990Sdrh   scratchAllocOut = p!=0;
322f7141990Sdrh #endif
323e5ae5735Sdrh   return p;
324e5ae5735Sdrh }
325facf0307Sdrh void sqlite3ScratchFree(void *p){
326e5ae5735Sdrh   if( p ){
3279ac3fe97Sdrh 
328e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3299ac3fe97Sdrh     /* Verify that no more than one scratch allocation per thread
3309ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3319ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3329ac3fe97Sdrh     ** would be much more complicated.) */
333facf0307Sdrh     assert( scratchAllocOut==1 );
334facf0307Sdrh     scratchAllocOut = 0;
335e5ae5735Sdrh #endif
3369ac3fe97Sdrh 
3379ac3fe97Sdrh     if( sqlite3Config.pScratch==0
3389ac3fe97Sdrh            || p<sqlite3Config.pScratch
3399ac3fe97Sdrh            || p>=(void*)mem0.aScratchFree ){
340f7141990Sdrh       if( sqlite3Config.bMemstat ){
341f7141990Sdrh         int iSize = sqlite3MallocSize(p);
342f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
343f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
344f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
345facf0307Sdrh         sqlite3Config.m.xFree(p);
346f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
347f7141990Sdrh       }else{
348f7141990Sdrh         sqlite3Config.m.xFree(p);
349f7141990Sdrh       }
3509ac3fe97Sdrh     }else{
3519ac3fe97Sdrh       int i;
3529ac3fe97Sdrh       i = p - sqlite3Config.pScratch;
3539ac3fe97Sdrh       i /= sqlite3Config.szScratch;
3549ac3fe97Sdrh       assert( i>=0 && i<sqlite3Config.nScratch );
355f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
356f7141990Sdrh       assert( mem0.nScratchFree<sqlite3Config.nScratch );
3579ac3fe97Sdrh       mem0.aScratchFree[mem0.nScratchFree++] = i;
358f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
3599ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
3609ac3fe97Sdrh     }
361e5ae5735Sdrh   }
362e5ae5735Sdrh }
363e5ae5735Sdrh 
364e5ae5735Sdrh /*
365f7141990Sdrh ** Allocate memory to be used by the page cache.  Make use of the
366f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
367f7141990Sdrh ** and that memory is of the right size and is not completely
368f7141990Sdrh ** consumed.  Otherwise, failover to sqlite3Malloc().
369facf0307Sdrh */
370f7141990Sdrh void *sqlite3PageMalloc(int n){
371f7141990Sdrh   void *p;
372f7141990Sdrh   assert( n>0 );
373f7141990Sdrh   assert( (n & (n-1))==0 );
374f7141990Sdrh   assert( n>=512 && n<=32768 );
375f7141990Sdrh 
376f7141990Sdrh   if( sqlite3Config.szPage<n ){
377f7141990Sdrh     goto page_overflow;
378f7141990Sdrh   }else{
379f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
380f7141990Sdrh     if( mem0.nPageFree==0 ){
381f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
382f7141990Sdrh       goto page_overflow;
383f7141990Sdrh     }else{
384f7141990Sdrh       int i;
385f7141990Sdrh       i = mem0.aPageFree[--mem0.nPageFree];
386f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
387f7141990Sdrh       i *= sqlite3Config.szPage;
388f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
389f7141990Sdrh       p = (void*)&((char*)sqlite3Config.pPage)[i];
390f7141990Sdrh     }
391f7141990Sdrh   }
392f7141990Sdrh   return p;
393f7141990Sdrh 
394f7141990Sdrh page_overflow:
395f7141990Sdrh   if( sqlite3Config.bMemstat ){
396f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
397f7141990Sdrh     n = mallocWithAlarm(n, &p);
398f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
399f7141990Sdrh     sqlite3_mutex_leave(mem0.mutex);
400f7141990Sdrh   }else{
401f7141990Sdrh     p = sqlite3Config.m.xMalloc(n);
402f7141990Sdrh   }
403f7141990Sdrh   return p;
404f7141990Sdrh }
405f7141990Sdrh void sqlite3PageFree(void *p){
406f7141990Sdrh   if( p ){
407f7141990Sdrh     if( sqlite3Config.pPage==0
408f7141990Sdrh            || p<sqlite3Config.pPage
409f7141990Sdrh            || p>=(void*)mem0.aPageFree ){
4104b9507a0Sdanielk1977       /* In this case, the page allocation was obtained from a regular
4114b9507a0Sdanielk1977       ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
4124b9507a0Sdanielk1977       ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
4134b9507a0Sdanielk1977       */
414f7141990Sdrh       if( sqlite3Config.bMemstat ){
415f7141990Sdrh         int iSize = sqlite3MallocSize(p);
416f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
417f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
418f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
419f7141990Sdrh         sqlite3Config.m.xFree(p);
420f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
421f7141990Sdrh       }else{
422f7141990Sdrh         sqlite3Config.m.xFree(p);
423f7141990Sdrh       }
424f7141990Sdrh     }else{
4254b9507a0Sdanielk1977       /* The page allocation was allocated from the sqlite3Config.pPage
4264b9507a0Sdanielk1977       ** buffer. In this case all that is add the index of the page in
4274b9507a0Sdanielk1977       ** the sqlite3Config.pPage array to the set of free indexes stored
4284b9507a0Sdanielk1977       ** in the mem0.aPageFree[] array.
4294b9507a0Sdanielk1977       */
430f7141990Sdrh       int i;
431f7141990Sdrh       i = p - sqlite3Config.pPage;
432f7141990Sdrh       i /= sqlite3Config.szPage;
433f7141990Sdrh       assert( i>=0 && i<sqlite3Config.nPage );
434f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
435f7141990Sdrh       assert( mem0.nPageFree<sqlite3Config.nPage );
436f7141990Sdrh       mem0.aPageFree[mem0.nPageFree++] = i;
437f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
438f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
4394b9507a0Sdanielk1977 #ifndef NDEBUG
4404b9507a0Sdanielk1977       /* Assert that a duplicate was not just inserted into aPageFree[]. */
4414b9507a0Sdanielk1977       for(i=0; i<mem0.nPageFree-1; i++){
4424b9507a0Sdanielk1977         assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
4434b9507a0Sdanielk1977       }
4444b9507a0Sdanielk1977 #endif
445f7141990Sdrh     }
446f7141990Sdrh   }
447facf0307Sdrh }
448facf0307Sdrh 
449facf0307Sdrh /*
450fec00eabSdrh ** Return the size of a memory allocation previously obtained from
451fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
452fec00eabSdrh */
453fec00eabSdrh int sqlite3MallocSize(void *p){
454fec00eabSdrh   return sqlite3Config.m.xSize(p);
455fec00eabSdrh }
456fec00eabSdrh 
457fec00eabSdrh /*
458fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
459fec00eabSdrh */
460fec00eabSdrh void sqlite3_free(void *p){
461fec00eabSdrh   if( p==0 ) return;
462fec00eabSdrh   if( sqlite3Config.bMemstat ){
463fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
464f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
465fec00eabSdrh     sqlite3Config.m.xFree(p);
466fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
467fec00eabSdrh   }else{
468fec00eabSdrh     sqlite3Config.m.xFree(p);
469fec00eabSdrh   }
470fec00eabSdrh }
471fec00eabSdrh 
472fec00eabSdrh /*
473fec00eabSdrh ** Change the size of an existing memory allocation
474fec00eabSdrh */
475fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
476fec00eabSdrh   int nOld, nNew;
477fec00eabSdrh   void *pNew;
478fec00eabSdrh   if( pOld==0 ){
479fec00eabSdrh     return sqlite3Malloc(nBytes);
480fec00eabSdrh   }
481fec00eabSdrh   if( nBytes<=0 ){
482fec00eabSdrh     sqlite3_free(pOld);
483fec00eabSdrh     return 0;
484fec00eabSdrh   }
485fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
486fec00eabSdrh   if( sqlite3Config.bMemstat ){
487fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
488f7141990Sdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
489fec00eabSdrh     nNew = sqlite3Config.m.xRoundup(nBytes);
490fec00eabSdrh     if( nOld==nNew ){
491fec00eabSdrh       pNew = pOld;
492fec00eabSdrh     }else{
493f7141990Sdrh       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
494f7141990Sdrh             mem0.alarmThreshold ){
495fec00eabSdrh         sqlite3MallocAlarm(nNew-nOld);
496fec00eabSdrh       }
497fec00eabSdrh       pNew = sqlite3Config.m.xRealloc(pOld, nNew);
498d09414cdSdanielk1977       if( pNew==0 && mem0.alarmCallback ){
499fec00eabSdrh         sqlite3MallocAlarm(nBytes);
500fec00eabSdrh         pNew = sqlite3Config.m.xRealloc(pOld, nNew);
501fec00eabSdrh       }
502fec00eabSdrh       if( pNew ){
503f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
504fec00eabSdrh       }
505fec00eabSdrh     }
506fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
507fec00eabSdrh   }else{
508fec00eabSdrh     pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
509fec00eabSdrh   }
510fec00eabSdrh   return pNew;
511fec00eabSdrh }
512fec00eabSdrh 
513fec00eabSdrh /*
514fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
515fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
516fec00eabSdrh */
517fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
518fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
519fec00eabSdrh   if( sqlite3_initialize() ) return 0;
520fec00eabSdrh #endif
521fec00eabSdrh   return sqlite3Realloc(pOld, n);
522fec00eabSdrh }
523fec00eabSdrh 
524a3152895Sdrh 
525a3152895Sdrh /*
52617435752Sdrh ** Allocate and zero memory.
527a3152895Sdrh */
528fec00eabSdrh void *sqlite3MallocZero(int n){
529fec00eabSdrh   void *p = sqlite3Malloc(n);
530a3152895Sdrh   if( p ){
531a3152895Sdrh     memset(p, 0, n);
532a3152895Sdrh   }
533a3152895Sdrh   return p;
534a3152895Sdrh }
53517435752Sdrh 
53617435752Sdrh /*
53717435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
53817435752Sdrh ** the mallocFailed flag in the connection pointer.
53917435752Sdrh */
540fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
541a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
54217435752Sdrh   if( p ){
54317435752Sdrh     memset(p, 0, n);
54417435752Sdrh   }
54517435752Sdrh   return p;
54617435752Sdrh }
54717435752Sdrh 
54817435752Sdrh /*
54917435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
55017435752Sdrh ** the mallocFailed flag in the connection pointer.
55117435752Sdrh */
552fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
553a1644fd8Sdanielk1977   void *p = 0;
554a1644fd8Sdanielk1977   if( !db || db->mallocFailed==0 ){
555fec00eabSdrh     p = sqlite3Malloc(n);
556f3a65f7eSdrh     if( !p && db ){
55717435752Sdrh       db->mallocFailed = 1;
55817435752Sdrh     }
559a1644fd8Sdanielk1977   }
56017435752Sdrh   return p;
56117435752Sdrh }
56217435752Sdrh 
56326783a58Sdanielk1977 /*
56426783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
56526783a58Sdanielk1977 ** resize fails, set the mallocFailed flag inthe connection object.
56626783a58Sdanielk1977 */
567a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
568a1644fd8Sdanielk1977   void *pNew = 0;
569a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
570a1644fd8Sdanielk1977     pNew = sqlite3_realloc(p, n);
571a1644fd8Sdanielk1977     if( !pNew ){
572a1644fd8Sdanielk1977       db->mallocFailed = 1;
573a1644fd8Sdanielk1977     }
574a1644fd8Sdanielk1977   }
575a1644fd8Sdanielk1977   return pNew;
576a1644fd8Sdanielk1977 }
577a1644fd8Sdanielk1977 
57817435752Sdrh /*
57917435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
58017435752Sdrh ** and set the mallocFailed flag in the database connection.
58117435752Sdrh */
58217435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
583a3152895Sdrh   void *pNew;
584a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
585a3152895Sdrh   if( !pNew ){
5861e536953Sdanielk1977     sqlite3_free(p);
587a3152895Sdrh   }
588a3152895Sdrh   return pNew;
589a3152895Sdrh }
590a3152895Sdrh 
591a3152895Sdrh /*
592a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
593a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
594a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
595a3152895Sdrh ** called via macros that record the current file and line number in the
596a3152895Sdrh ** ThreadData structure.
597a3152895Sdrh */
598a3152895Sdrh char *sqlite3StrDup(const char *z){
599a3152895Sdrh   char *zNew;
600a3152895Sdrh   int n;
601a3152895Sdrh   if( z==0 ) return 0;
602a3152895Sdrh   n = strlen(z)+1;
603e5ae5735Sdrh   zNew = sqlite3Malloc(n);
604a3152895Sdrh   if( zNew ) memcpy(zNew, z, n);
605a3152895Sdrh   return zNew;
606a3152895Sdrh }
607a3152895Sdrh char *sqlite3StrNDup(const char *z, int n){
608a3152895Sdrh   char *zNew;
609a3152895Sdrh   if( z==0 ) return 0;
610e5ae5735Sdrh   zNew = sqlite3Malloc(n+1);
611a3152895Sdrh   if( zNew ){
612a3152895Sdrh     memcpy(zNew, z, n);
613a3152895Sdrh     zNew[n] = 0;
614a3152895Sdrh   }
615a3152895Sdrh   return zNew;
616a3152895Sdrh }
617a3152895Sdrh 
6181e536953Sdanielk1977 char *sqlite3DbStrDup(sqlite3 *db, const char *z){
6191e536953Sdanielk1977   char *zNew = sqlite3StrDup(z);
6201e536953Sdanielk1977   if( z && !zNew ){
6211e536953Sdanielk1977     db->mallocFailed = 1;
6221e536953Sdanielk1977   }
6231e536953Sdanielk1977   return zNew;
6241e536953Sdanielk1977 }
6251e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
6261e536953Sdanielk1977   char *zNew = sqlite3StrNDup(z, n);
6271e536953Sdanielk1977   if( z && !zNew ){
6281e536953Sdanielk1977     db->mallocFailed = 1;
6291e536953Sdanielk1977   }
6301e536953Sdanielk1977   return zNew;
6311e536953Sdanielk1977 }
6321e536953Sdanielk1977 
633a3152895Sdrh /*
634a3152895Sdrh ** Create a string from the 2nd and subsequent arguments (up to the
635a3152895Sdrh ** first NULL argument), store the string in memory obtained from
636a3152895Sdrh ** sqliteMalloc() and make the pointer indicated by the 1st argument
637a3152895Sdrh ** point to that string.  The 1st argument must either be NULL or
638a3152895Sdrh ** point to memory obtained from sqliteMalloc().
639a3152895Sdrh */
640a3152895Sdrh void sqlite3SetString(char **pz, ...){
641a3152895Sdrh   va_list ap;
642a3152895Sdrh   int nByte;
643a3152895Sdrh   const char *z;
644a3152895Sdrh   char *zResult;
645a3152895Sdrh 
646a3152895Sdrh   assert( pz!=0 );
647a3152895Sdrh   nByte = 1;
648a3152895Sdrh   va_start(ap, pz);
649a3152895Sdrh   while( (z = va_arg(ap, const char*))!=0 ){
650a3152895Sdrh     nByte += strlen(z);
651a3152895Sdrh   }
652a3152895Sdrh   va_end(ap);
6531e536953Sdanielk1977   sqlite3_free(*pz);
654e5ae5735Sdrh   *pz = zResult = sqlite3Malloc(nByte);
655a3152895Sdrh   if( zResult==0 ){
656a3152895Sdrh     return;
657a3152895Sdrh   }
658a3152895Sdrh   *zResult = 0;
659a3152895Sdrh   va_start(ap, pz);
660a3152895Sdrh   while( (z = va_arg(ap, const char*))!=0 ){
661a3152895Sdrh     int n = strlen(z);
662a3152895Sdrh     memcpy(zResult, z, n);
663a3152895Sdrh     zResult += n;
664a3152895Sdrh   }
665a3152895Sdrh   zResult[0] = 0;
666a3152895Sdrh   va_end(ap);
667a3152895Sdrh }
668a3152895Sdrh 
669a3152895Sdrh 
670a3152895Sdrh /*
671a3152895Sdrh ** This function must be called before exiting any API function (i.e.
67217435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
67317435752Sdrh ** sqlite3_realloc.
674a3152895Sdrh **
675a3152895Sdrh ** The returned value is normally a copy of the second argument to this
676a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
677a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
678a3152895Sdrh **
679a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
680a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
681a3152895Sdrh ** is set to SQLITE_NOMEM.
682a3152895Sdrh */
683a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
684a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
685a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
686a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
687a1644fd8Sdanielk1977   */
688a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
6891e536953Sdanielk1977   if( db && db->mallocFailed ){
690a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
69117435752Sdrh     db->mallocFailed = 0;
692a3152895Sdrh     rc = SQLITE_NOMEM;
693a3152895Sdrh   }
694a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
695a3152895Sdrh }
696