xref: /sqlite-3.40.0/src/malloc.c (revision e50135e2)
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*e50135e2Sdrh ** $Id: malloc.c,v 1.34 2008/08/05 17:53:23 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 ){
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 /*
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
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   }
1156480aad4Sdrh   if( sqlite3Config.pScratch && sqlite3Config.szScratch>=100
1166480aad4Sdrh       && sqlite3Config.nScratch>=0 ){
1179ac3fe97Sdrh     int i;
1180a60a384Sdrh     sqlite3Config.szScratch -= 4;
1199ac3fe97Sdrh     mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
1209ac3fe97Sdrh                   [sqlite3Config.szScratch*sqlite3Config.nScratch];
1219ac3fe97Sdrh     for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
1229ac3fe97Sdrh     mem0.nScratchFree = sqlite3Config.nScratch;
1239ac3fe97Sdrh   }else{
1249ac3fe97Sdrh     sqlite3Config.pScratch = 0;
125f7141990Sdrh     sqlite3Config.szScratch = 0;
1269ac3fe97Sdrh   }
1279ac3fe97Sdrh   if( sqlite3Config.pPage && sqlite3Config.szPage>=512
1286480aad4Sdrh       && sqlite3Config.nPage>=1 ){
1299ac3fe97Sdrh     int i;
1300a60a384Sdrh     int overhead;
1310a60a384Sdrh     int sz = sqlite3Config.szPage;
1320a60a384Sdrh     int n = sqlite3Config.nPage;
1330a60a384Sdrh     overhead = (4*n + sz - 1)/sz;
1340a60a384Sdrh     sqlite3Config.nPage -= overhead;
1359ac3fe97Sdrh     mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
1369ac3fe97Sdrh                   [sqlite3Config.szPage*sqlite3Config.nPage];
1379ac3fe97Sdrh     for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
1389ac3fe97Sdrh     mem0.nPageFree = sqlite3Config.nPage;
1399ac3fe97Sdrh   }else{
1409ac3fe97Sdrh     sqlite3Config.pPage = 0;
141f7141990Sdrh     sqlite3Config.szPage = 0;
1429ac3fe97Sdrh   }
143fec00eabSdrh   return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
144fec00eabSdrh }
145fec00eabSdrh 
146fec00eabSdrh /*
147fec00eabSdrh ** Deinitialize the memory allocation subsystem.
148fec00eabSdrh */
149fec00eabSdrh void sqlite3MallocEnd(void){
150fec00eabSdrh   sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
1519ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
152fec00eabSdrh }
153fec00eabSdrh 
154fec00eabSdrh /*
155fec00eabSdrh ** Return the amount of memory currently checked out.
156fec00eabSdrh */
157fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
158f7141990Sdrh   int n, mx;
159c376a198Sdrh   sqlite3_int64 res;
160f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
161c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
162c376a198Sdrh   return res;
163fec00eabSdrh }
164fec00eabSdrh 
165fec00eabSdrh /*
166fec00eabSdrh ** Return the maximum amount of memory that has ever been
167fec00eabSdrh ** checked out since either the beginning of this process
168fec00eabSdrh ** or since the most recent reset.
169fec00eabSdrh */
170fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
171f7141990Sdrh   int n, mx;
172c376a198Sdrh   sqlite3_int64 res;
173f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
1747986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
175c376a198Sdrh   return res;
176fec00eabSdrh }
177fec00eabSdrh 
178fec00eabSdrh /*
179fec00eabSdrh ** Change the alarm callback
180fec00eabSdrh */
181fec00eabSdrh int sqlite3_memory_alarm(
182fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
183fec00eabSdrh   void *pArg,
184fec00eabSdrh   sqlite3_int64 iThreshold
185fec00eabSdrh ){
186fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
187fec00eabSdrh   mem0.alarmCallback = xCallback;
188fec00eabSdrh   mem0.alarmArg = pArg;
189fec00eabSdrh   mem0.alarmThreshold = iThreshold;
190fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
191fec00eabSdrh   return SQLITE_OK;
192fec00eabSdrh }
193fec00eabSdrh 
194fec00eabSdrh /*
195fec00eabSdrh ** Trigger the alarm
196fec00eabSdrh */
197fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
198fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
199fec00eabSdrh   sqlite3_int64 nowUsed;
200fec00eabSdrh   void *pArg;
201fec00eabSdrh   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
202fec00eabSdrh   mem0.alarmBusy = 1;
203fec00eabSdrh   xCallback = mem0.alarmCallback;
204f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
205fec00eabSdrh   pArg = mem0.alarmArg;
206fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
207fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
208fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
209fec00eabSdrh   mem0.alarmBusy = 0;
210fec00eabSdrh }
211fec00eabSdrh 
212fec00eabSdrh /*
213f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
214f7141990Sdrh ** lock is already held.
215fec00eabSdrh */
216f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
217fec00eabSdrh   int nFull;
218f7141990Sdrh   void *p;
219f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
220fec00eabSdrh   nFull = sqlite3Config.m.xRoundup(n);
221f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
222f7141990Sdrh   if( mem0.alarmCallback!=0 ){
223f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
224f7141990Sdrh     if( nUsed+nFull >= mem0.alarmThreshold ){
225fec00eabSdrh       sqlite3MallocAlarm(nFull);
226fec00eabSdrh     }
227f7141990Sdrh   }
228fec00eabSdrh   p = sqlite3Config.m.xMalloc(nFull);
229d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
230fec00eabSdrh     sqlite3MallocAlarm(nFull);
231d09414cdSdanielk1977     p = sqlite3Config.m.xMalloc(nFull);
232fec00eabSdrh   }
233c702c7ccSdrh   if( p ){
234c702c7ccSdrh     nFull = sqlite3MallocSize(p);
235c702c7ccSdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
236c702c7ccSdrh   }
237f7141990Sdrh   *pp = p;
238f7141990Sdrh   return nFull;
239fec00eabSdrh }
240f7141990Sdrh 
241f7141990Sdrh /*
242f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
243f7141990Sdrh ** assumes the memory subsystem has already been initialized.
244f7141990Sdrh */
245f7141990Sdrh void *sqlite3Malloc(int n){
246f7141990Sdrh   void *p;
247f7141990Sdrh   if( n<=0 ){
248f7141990Sdrh     p = 0;
249f7141990Sdrh   }else if( sqlite3Config.bMemstat ){
250f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
251f7141990Sdrh     mallocWithAlarm(n, &p);
252fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
253fec00eabSdrh   }else{
254fec00eabSdrh     p = sqlite3Config.m.xMalloc(n);
255fec00eabSdrh   }
256fec00eabSdrh   return p;
257fec00eabSdrh }
258fec00eabSdrh 
259fec00eabSdrh /*
260fec00eabSdrh ** This version of the memory allocation is for use by the application.
261fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
262fec00eabSdrh ** allocation.
263fec00eabSdrh */
264fec00eabSdrh void *sqlite3_malloc(int n){
265fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
266fec00eabSdrh   if( sqlite3_initialize() ) return 0;
267fec00eabSdrh #endif
268fec00eabSdrh   return sqlite3Malloc(n);
269fec00eabSdrh }
270fec00eabSdrh 
271fec00eabSdrh /*
272e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
273facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
274facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
275e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
276e5ae5735Sdrh */
277e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
278facf0307Sdrh static int scratchAllocOut = 0;
279e5ae5735Sdrh #endif
280e5ae5735Sdrh 
281e5ae5735Sdrh 
282e5ae5735Sdrh /*
283e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
284e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
285e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
286e5ae5735Sdrh ** routine is intended to get memory to old large transient data
287e5ae5735Sdrh ** structures that would not normally fit on the stack of an
288e5ae5735Sdrh ** embedded processor.
289e5ae5735Sdrh */
290facf0307Sdrh void *sqlite3ScratchMalloc(int n){
291e5ae5735Sdrh   void *p;
292e5ae5735Sdrh   assert( n>0 );
2939ac3fe97Sdrh 
294e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
2959ac3fe97Sdrh   /* Verify that no more than one scratch allocation per thread
2969ac3fe97Sdrh   ** is outstanding at one time.  (This is only checked in the
2979ac3fe97Sdrh   ** single-threaded case since checking in the multi-threaded case
2989ac3fe97Sdrh   ** would be much more complicated.) */
299facf0307Sdrh   assert( scratchAllocOut==0 );
300e5ae5735Sdrh #endif
3019ac3fe97Sdrh 
302f7141990Sdrh   if( sqlite3Config.szScratch<n ){
303f7141990Sdrh     goto scratch_overflow;
304f7141990Sdrh   }else{
305e5ae5735Sdrh     sqlite3_mutex_enter(mem0.mutex);
306f7141990Sdrh     if( mem0.nScratchFree==0 ){
307f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
308f7141990Sdrh       goto scratch_overflow;
309e5ae5735Sdrh     }else{
3109ac3fe97Sdrh       int i;
3119ac3fe97Sdrh       i = mem0.aScratchFree[--mem0.nScratchFree];
312f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
3139ac3fe97Sdrh       i *= sqlite3Config.szScratch;
314f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
315*e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
3169ac3fe97Sdrh       p = (void*)&((char*)sqlite3Config.pScratch)[i];
317e5ae5735Sdrh     }
318f7141990Sdrh   }
319f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
320f7141990Sdrh   scratchAllocOut = p!=0;
321f7141990Sdrh #endif
322f7141990Sdrh 
323f7141990Sdrh   return p;
324f7141990Sdrh 
325f7141990Sdrh scratch_overflow:
326f7141990Sdrh   if( sqlite3Config.bMemstat ){
327f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
328*e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
329f7141990Sdrh     n = mallocWithAlarm(n, &p);
330f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
3319ac3fe97Sdrh     sqlite3_mutex_leave(mem0.mutex);
332f7141990Sdrh   }else{
333f7141990Sdrh     p = sqlite3Config.m.xMalloc(n);
334f7141990Sdrh   }
335f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
336f7141990Sdrh   scratchAllocOut = p!=0;
337f7141990Sdrh #endif
338e5ae5735Sdrh   return p;
339e5ae5735Sdrh }
340facf0307Sdrh void sqlite3ScratchFree(void *p){
341e5ae5735Sdrh   if( p ){
3429ac3fe97Sdrh 
343e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3449ac3fe97Sdrh     /* Verify that no more than one scratch allocation per thread
3459ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3469ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3479ac3fe97Sdrh     ** would be much more complicated.) */
348facf0307Sdrh     assert( scratchAllocOut==1 );
349facf0307Sdrh     scratchAllocOut = 0;
350e5ae5735Sdrh #endif
3519ac3fe97Sdrh 
3529ac3fe97Sdrh     if( sqlite3Config.pScratch==0
3539ac3fe97Sdrh            || p<sqlite3Config.pScratch
3549ac3fe97Sdrh            || p>=(void*)mem0.aScratchFree ){
355f7141990Sdrh       if( sqlite3Config.bMemstat ){
356f7141990Sdrh         int iSize = sqlite3MallocSize(p);
357f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
358f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
359f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
360facf0307Sdrh         sqlite3Config.m.xFree(p);
361f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
362f7141990Sdrh       }else{
363f7141990Sdrh         sqlite3Config.m.xFree(p);
364f7141990Sdrh       }
3659ac3fe97Sdrh     }else{
3669ac3fe97Sdrh       int i;
367867d05a0Sdanielk1977       i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
3689ac3fe97Sdrh       i /= sqlite3Config.szScratch;
3699ac3fe97Sdrh       assert( i>=0 && i<sqlite3Config.nScratch );
370f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
371f7141990Sdrh       assert( mem0.nScratchFree<sqlite3Config.nScratch );
3729ac3fe97Sdrh       mem0.aScratchFree[mem0.nScratchFree++] = i;
373f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
3749ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
3759ac3fe97Sdrh     }
376e5ae5735Sdrh   }
377e5ae5735Sdrh }
378e5ae5735Sdrh 
379e5ae5735Sdrh /*
380f7141990Sdrh ** Allocate memory to be used by the page cache.  Make use of the
381f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
382f7141990Sdrh ** and that memory is of the right size and is not completely
383f7141990Sdrh ** consumed.  Otherwise, failover to sqlite3Malloc().
384facf0307Sdrh */
385f7141990Sdrh void *sqlite3PageMalloc(int n){
386f7141990Sdrh   void *p;
387f7141990Sdrh   assert( n>0 );
388f7141990Sdrh   assert( (n & (n-1))==0 );
389f7141990Sdrh   assert( n>=512 && n<=32768 );
390f7141990Sdrh 
391f7141990Sdrh   if( sqlite3Config.szPage<n ){
392f7141990Sdrh     goto page_overflow;
393f7141990Sdrh   }else{
394f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
395f7141990Sdrh     if( mem0.nPageFree==0 ){
396f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
397f7141990Sdrh       goto page_overflow;
398f7141990Sdrh     }else{
399f7141990Sdrh       int i;
400f7141990Sdrh       i = mem0.aPageFree[--mem0.nPageFree];
401f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
402f7141990Sdrh       i *= sqlite3Config.szPage;
403*e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
404f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
405f7141990Sdrh       p = (void*)&((char*)sqlite3Config.pPage)[i];
406f7141990Sdrh     }
407f7141990Sdrh   }
408f7141990Sdrh   return p;
409f7141990Sdrh 
410f7141990Sdrh page_overflow:
411f7141990Sdrh   if( sqlite3Config.bMemstat ){
412f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
413*e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
414f7141990Sdrh     n = mallocWithAlarm(n, &p);
415f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
416f7141990Sdrh     sqlite3_mutex_leave(mem0.mutex);
417f7141990Sdrh   }else{
418f7141990Sdrh     p = sqlite3Config.m.xMalloc(n);
419f7141990Sdrh   }
420f7141990Sdrh   return p;
421f7141990Sdrh }
422f7141990Sdrh void sqlite3PageFree(void *p){
423f7141990Sdrh   if( p ){
424f7141990Sdrh     if( sqlite3Config.pPage==0
425f7141990Sdrh            || p<sqlite3Config.pPage
426f7141990Sdrh            || p>=(void*)mem0.aPageFree ){
4274b9507a0Sdanielk1977       /* In this case, the page allocation was obtained from a regular
4284b9507a0Sdanielk1977       ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
4294b9507a0Sdanielk1977       ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
4304b9507a0Sdanielk1977       */
431f7141990Sdrh       if( sqlite3Config.bMemstat ){
432f7141990Sdrh         int iSize = sqlite3MallocSize(p);
433f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
434f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
435f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
436f7141990Sdrh         sqlite3Config.m.xFree(p);
437f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
438f7141990Sdrh       }else{
439f7141990Sdrh         sqlite3Config.m.xFree(p);
440f7141990Sdrh       }
441f7141990Sdrh     }else{
4424b9507a0Sdanielk1977       /* The page allocation was allocated from the sqlite3Config.pPage
4434b9507a0Sdanielk1977       ** buffer. In this case all that is add the index of the page in
4444b9507a0Sdanielk1977       ** the sqlite3Config.pPage array to the set of free indexes stored
4454b9507a0Sdanielk1977       ** in the mem0.aPageFree[] array.
4464b9507a0Sdanielk1977       */
447f7141990Sdrh       int i;
448867d05a0Sdanielk1977       i = (u8 *)p - (u8 *)sqlite3Config.pPage;
449f7141990Sdrh       i /= sqlite3Config.szPage;
450f7141990Sdrh       assert( i>=0 && i<sqlite3Config.nPage );
451f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
452f7141990Sdrh       assert( mem0.nPageFree<sqlite3Config.nPage );
453f7141990Sdrh       mem0.aPageFree[mem0.nPageFree++] = i;
454f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
455f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
4565f4bcf15Sdrh #if !defined(NDEBUG) && 0
4574b9507a0Sdanielk1977       /* Assert that a duplicate was not just inserted into aPageFree[]. */
4584b9507a0Sdanielk1977       for(i=0; i<mem0.nPageFree-1; i++){
4594b9507a0Sdanielk1977         assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
4604b9507a0Sdanielk1977       }
4614b9507a0Sdanielk1977 #endif
462f7141990Sdrh     }
463f7141990Sdrh   }
464facf0307Sdrh }
465facf0307Sdrh 
466facf0307Sdrh /*
467633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
468633e6d57Sdrh */
469633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
470633e6d57Sdrh   return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
471633e6d57Sdrh }
472633e6d57Sdrh 
473633e6d57Sdrh /*
474fec00eabSdrh ** Return the size of a memory allocation previously obtained from
475fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
476fec00eabSdrh */
477fec00eabSdrh int sqlite3MallocSize(void *p){
478fec00eabSdrh   return sqlite3Config.m.xSize(p);
479fec00eabSdrh }
480633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
481633e6d57Sdrh   if( isLookaside(db, p) ){
482633e6d57Sdrh     return db->lookaside.sz;
483633e6d57Sdrh   }else{
484633e6d57Sdrh     return sqlite3Config.m.xSize(p);
485633e6d57Sdrh   }
486633e6d57Sdrh }
487fec00eabSdrh 
488fec00eabSdrh /*
489fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
490fec00eabSdrh */
491fec00eabSdrh void sqlite3_free(void *p){
492fec00eabSdrh   if( p==0 ) return;
493fec00eabSdrh   if( sqlite3Config.bMemstat ){
494fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
495f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
496fec00eabSdrh     sqlite3Config.m.xFree(p);
497fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
498fec00eabSdrh   }else{
499fec00eabSdrh     sqlite3Config.m.xFree(p);
500fec00eabSdrh   }
501fec00eabSdrh }
502fec00eabSdrh 
503fec00eabSdrh /*
504633e6d57Sdrh ** Free memory that might be associated with a particular database
505633e6d57Sdrh ** connection.
506633e6d57Sdrh */
507633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
508633e6d57Sdrh   if( isLookaside(db, p) ){
509633e6d57Sdrh     LookasideSlot *pBuf = (LookasideSlot*)p;
510633e6d57Sdrh     pBuf->pNext = db->lookaside.pFree;
511633e6d57Sdrh     db->lookaside.pFree = pBuf;
512633e6d57Sdrh     db->lookaside.nOut--;
513633e6d57Sdrh   }else{
514633e6d57Sdrh     sqlite3_free(p);
515633e6d57Sdrh   }
516633e6d57Sdrh }
517633e6d57Sdrh 
518633e6d57Sdrh /*
519fec00eabSdrh ** Change the size of an existing memory allocation
520fec00eabSdrh */
521fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
522fec00eabSdrh   int nOld, nNew;
523fec00eabSdrh   void *pNew;
524fec00eabSdrh   if( pOld==0 ){
525fec00eabSdrh     return sqlite3Malloc(nBytes);
526fec00eabSdrh   }
527fec00eabSdrh   if( nBytes<=0 ){
528fec00eabSdrh     sqlite3_free(pOld);
529fec00eabSdrh     return 0;
530fec00eabSdrh   }
531fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
532fec00eabSdrh   if( sqlite3Config.bMemstat ){
533fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
534f7141990Sdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
535fec00eabSdrh     nNew = sqlite3Config.m.xRoundup(nBytes);
536fec00eabSdrh     if( nOld==nNew ){
537fec00eabSdrh       pNew = pOld;
538fec00eabSdrh     }else{
539f7141990Sdrh       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
540f7141990Sdrh             mem0.alarmThreshold ){
541fec00eabSdrh         sqlite3MallocAlarm(nNew-nOld);
542fec00eabSdrh       }
543fec00eabSdrh       pNew = sqlite3Config.m.xRealloc(pOld, nNew);
544d09414cdSdanielk1977       if( pNew==0 && mem0.alarmCallback ){
545fec00eabSdrh         sqlite3MallocAlarm(nBytes);
546fec00eabSdrh         pNew = sqlite3Config.m.xRealloc(pOld, nNew);
547fec00eabSdrh       }
548fec00eabSdrh       if( pNew ){
549c702c7ccSdrh         nNew = sqlite3MallocSize(pNew);
550f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
551fec00eabSdrh       }
552fec00eabSdrh     }
553fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
554fec00eabSdrh   }else{
555fec00eabSdrh     pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
556fec00eabSdrh   }
557fec00eabSdrh   return pNew;
558fec00eabSdrh }
559fec00eabSdrh 
560fec00eabSdrh /*
561fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
562fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
563fec00eabSdrh */
564fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
565fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
566fec00eabSdrh   if( sqlite3_initialize() ) return 0;
567fec00eabSdrh #endif
568fec00eabSdrh   return sqlite3Realloc(pOld, n);
569fec00eabSdrh }
570fec00eabSdrh 
571a3152895Sdrh 
572a3152895Sdrh /*
57317435752Sdrh ** Allocate and zero memory.
574a3152895Sdrh */
575fec00eabSdrh void *sqlite3MallocZero(int n){
576fec00eabSdrh   void *p = sqlite3Malloc(n);
577a3152895Sdrh   if( p ){
578a3152895Sdrh     memset(p, 0, n);
579a3152895Sdrh   }
580a3152895Sdrh   return p;
581a3152895Sdrh }
58217435752Sdrh 
58317435752Sdrh /*
58417435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
58517435752Sdrh ** the mallocFailed flag in the connection pointer.
58617435752Sdrh */
587fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
588a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
58917435752Sdrh   if( p ){
59017435752Sdrh     memset(p, 0, n);
59117435752Sdrh   }
59217435752Sdrh   return p;
59317435752Sdrh }
59417435752Sdrh 
59517435752Sdrh /*
59617435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
59717435752Sdrh ** the mallocFailed flag in the connection pointer.
59817435752Sdrh */
599fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
600633e6d57Sdrh   void *p;
601633e6d57Sdrh   if( db ){
602633e6d57Sdrh     LookasideSlot *pBuf;
603633e6d57Sdrh     if( db->mallocFailed ){
604633e6d57Sdrh       return 0;
605633e6d57Sdrh     }
606633e6d57Sdrh     if( db->lookaside.bEnabled && n<=db->lookaside.sz
607633e6d57Sdrh          && (pBuf = db->lookaside.pFree)!=0 ){
608633e6d57Sdrh       db->lookaside.pFree = pBuf->pNext;
609633e6d57Sdrh       db->lookaside.nOut++;
610633e6d57Sdrh       if( db->lookaside.nOut>db->lookaside.mxOut ){
611633e6d57Sdrh         db->lookaside.mxOut = db->lookaside.nOut;
612633e6d57Sdrh       }
613633e6d57Sdrh       return (void*)pBuf;
614633e6d57Sdrh     }
615633e6d57Sdrh   }
616fec00eabSdrh   p = sqlite3Malloc(n);
617f3a65f7eSdrh   if( !p && db ){
61817435752Sdrh     db->mallocFailed = 1;
61917435752Sdrh   }
62017435752Sdrh   return p;
62117435752Sdrh }
62217435752Sdrh 
62326783a58Sdanielk1977 /*
62426783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
62526783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
62626783a58Sdanielk1977 */
627a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
628a1644fd8Sdanielk1977   void *pNew = 0;
629a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
630633e6d57Sdrh     if( p==0 ){
631633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
632633e6d57Sdrh     }
633633e6d57Sdrh     if( isLookaside(db, p) ){
634633e6d57Sdrh       if( n<=db->lookaside.sz ){
635633e6d57Sdrh         return p;
636633e6d57Sdrh       }
637633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
638633e6d57Sdrh       if( pNew ){
639633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
640633e6d57Sdrh         sqlite3DbFree(db, p);
641633e6d57Sdrh       }
642633e6d57Sdrh     }else{
643a1644fd8Sdanielk1977       pNew = sqlite3_realloc(p, n);
644a1644fd8Sdanielk1977       if( !pNew ){
645a1644fd8Sdanielk1977         db->mallocFailed = 1;
646a1644fd8Sdanielk1977       }
647a1644fd8Sdanielk1977     }
648633e6d57Sdrh   }
649a1644fd8Sdanielk1977   return pNew;
650a1644fd8Sdanielk1977 }
651a1644fd8Sdanielk1977 
65217435752Sdrh /*
65317435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
65417435752Sdrh ** and set the mallocFailed flag in the database connection.
65517435752Sdrh */
65617435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
657a3152895Sdrh   void *pNew;
658a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
659a3152895Sdrh   if( !pNew ){
660633e6d57Sdrh     sqlite3DbFree(db, p);
661a3152895Sdrh   }
662a3152895Sdrh   return pNew;
663a3152895Sdrh }
664a3152895Sdrh 
665a3152895Sdrh /*
666a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
667a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
668a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
669a3152895Sdrh ** called via macros that record the current file and line number in the
670a3152895Sdrh ** ThreadData structure.
671a3152895Sdrh */
672633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
673a3152895Sdrh   char *zNew;
674633e6d57Sdrh   size_t n;
675633e6d57Sdrh   if( z==0 ){
676633e6d57Sdrh     return 0;
677a3152895Sdrh   }
678633e6d57Sdrh   n = strlen(z)+1;
679633e6d57Sdrh   assert( (n&0x7fffffff)==n );
680633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
681a3152895Sdrh   if( zNew ){
682a3152895Sdrh     memcpy(zNew, z, n);
6831e536953Sdanielk1977   }
6841e536953Sdanielk1977   return zNew;
6851e536953Sdanielk1977 }
6861e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
687633e6d57Sdrh   char *zNew;
688633e6d57Sdrh   if( z==0 ){
689633e6d57Sdrh     return 0;
690633e6d57Sdrh   }
691633e6d57Sdrh   assert( (n&0x7fffffff)==n );
692633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
693633e6d57Sdrh   if( zNew ){
694633e6d57Sdrh     memcpy(zNew, z, n);
695633e6d57Sdrh     zNew[n] = 0;
6961e536953Sdanielk1977   }
6971e536953Sdanielk1977   return zNew;
6981e536953Sdanielk1977 }
6991e536953Sdanielk1977 
700a3152895Sdrh /*
701f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
702f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
703f089aa45Sdrh ** point to that string.
704a3152895Sdrh */
705f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
706a3152895Sdrh   va_list ap;
707f089aa45Sdrh   char *z;
708a3152895Sdrh 
709f089aa45Sdrh   va_start(ap, zFormat);
710f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
711a3152895Sdrh   va_end(ap);
712633e6d57Sdrh   sqlite3DbFree(db, *pz);
713f089aa45Sdrh   *pz = z;
714a3152895Sdrh }
715a3152895Sdrh 
716a3152895Sdrh 
717a3152895Sdrh /*
718a3152895Sdrh ** This function must be called before exiting any API function (i.e.
71917435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
72017435752Sdrh ** sqlite3_realloc.
721a3152895Sdrh **
722a3152895Sdrh ** The returned value is normally a copy of the second argument to this
723a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
724a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
725a3152895Sdrh **
726a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
727a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
728a3152895Sdrh ** is set to SQLITE_NOMEM.
729a3152895Sdrh */
730a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
731a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
732a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
733a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
734a1644fd8Sdanielk1977   */
735a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
7361e536953Sdanielk1977   if( db && db->mallocFailed ){
737a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
73817435752Sdrh     db->mallocFailed = 0;
739a3152895Sdrh     rc = SQLITE_NOMEM;
740a3152895Sdrh   }
741a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
742a3152895Sdrh }
743