xref: /sqlite-3.40.0/src/malloc.c (revision 67e3da7a)
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*67e3da7aSdanielk1977 ** $Id: malloc.c,v 1.36 2008/08/21 12:19:44 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 /*
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
65*67e3da7aSdanielk1977   int nRet = 0;
66*67e3da7aSdanielk1977 #if 0
67*67e3da7aSdanielk1977   nRet += sqlite3VdbeReleaseMemory(n);
68*67e3da7aSdanielk1977 #endif
69*67e3da7aSdanielk1977   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 */
79fec00eabSdrh static struct {
80fec00eabSdrh   sqlite3_mutex *mutex;         /* Mutex to serialize access */
81fec00eabSdrh 
82fec00eabSdrh   /*
83fec00eabSdrh   ** The alarm callback and its arguments.  The mem0.mutex lock will
84fec00eabSdrh   ** be held while the callback is running.  Recursive calls into
85fec00eabSdrh   ** the memory subsystem are allowed, but no new callbacks will be
86fec00eabSdrh   ** issued.  The alarmBusy variable is set to prevent recursive
87fec00eabSdrh   ** callbacks.
88fec00eabSdrh   */
89fec00eabSdrh   sqlite3_int64 alarmThreshold;
90fec00eabSdrh   void (*alarmCallback)(void*, sqlite3_int64,int);
91fec00eabSdrh   void *alarmArg;
92fec00eabSdrh   int alarmBusy;
93fec00eabSdrh 
94fec00eabSdrh   /*
959ac3fe97Sdrh   ** Pointers to the end of sqlite3Config.pScratch and
969ac3fe97Sdrh   ** sqlite3Config.pPage to a block of memory that records
979ac3fe97Sdrh   ** which pages are available.
989ac3fe97Sdrh   */
999ac3fe97Sdrh   u32 *aScratchFree;
1009ac3fe97Sdrh   u32 *aPageFree;
1019ac3fe97Sdrh 
1029ac3fe97Sdrh   /* Number of free pages for scratch and page-cache memory */
1039ac3fe97Sdrh   u32 nScratchFree;
1049ac3fe97Sdrh   u32 nPageFree;
105fec00eabSdrh } mem0;
106fec00eabSdrh 
107fec00eabSdrh /*
108fec00eabSdrh ** Initialize the memory allocation subsystem.
109fec00eabSdrh */
110fec00eabSdrh int sqlite3MallocInit(void){
111fec00eabSdrh   if( sqlite3Config.m.xMalloc==0 ){
112fec00eabSdrh     sqlite3MemSetDefault();
113fec00eabSdrh   }
114fec00eabSdrh   memset(&mem0, 0, sizeof(mem0));
1159ac3fe97Sdrh   if( sqlite3Config.bCoreMutex ){
11659f8c08eSdanielk1977     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
117fec00eabSdrh   }
1186480aad4Sdrh   if( sqlite3Config.pScratch && sqlite3Config.szScratch>=100
1196480aad4Sdrh       && sqlite3Config.nScratch>=0 ){
1209ac3fe97Sdrh     int i;
1210a60a384Sdrh     sqlite3Config.szScratch -= 4;
1229ac3fe97Sdrh     mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
1239ac3fe97Sdrh                   [sqlite3Config.szScratch*sqlite3Config.nScratch];
1249ac3fe97Sdrh     for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
1259ac3fe97Sdrh     mem0.nScratchFree = sqlite3Config.nScratch;
1269ac3fe97Sdrh   }else{
1279ac3fe97Sdrh     sqlite3Config.pScratch = 0;
128f7141990Sdrh     sqlite3Config.szScratch = 0;
1299ac3fe97Sdrh   }
1309ac3fe97Sdrh   if( sqlite3Config.pPage && sqlite3Config.szPage>=512
1316480aad4Sdrh       && sqlite3Config.nPage>=1 ){
1329ac3fe97Sdrh     int i;
1330a60a384Sdrh     int overhead;
1340a60a384Sdrh     int sz = sqlite3Config.szPage;
1350a60a384Sdrh     int n = sqlite3Config.nPage;
1360a60a384Sdrh     overhead = (4*n + sz - 1)/sz;
1370a60a384Sdrh     sqlite3Config.nPage -= overhead;
1389ac3fe97Sdrh     mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
1399ac3fe97Sdrh                   [sqlite3Config.szPage*sqlite3Config.nPage];
1409ac3fe97Sdrh     for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
1419ac3fe97Sdrh     mem0.nPageFree = sqlite3Config.nPage;
1429ac3fe97Sdrh   }else{
1439ac3fe97Sdrh     sqlite3Config.pPage = 0;
144f7141990Sdrh     sqlite3Config.szPage = 0;
1459ac3fe97Sdrh   }
146fec00eabSdrh   return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
147fec00eabSdrh }
148fec00eabSdrh 
149fec00eabSdrh /*
150fec00eabSdrh ** Deinitialize the memory allocation subsystem.
151fec00eabSdrh */
152fec00eabSdrh void sqlite3MallocEnd(void){
153fec00eabSdrh   sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
1549ac3fe97Sdrh   memset(&mem0, 0, sizeof(mem0));
155fec00eabSdrh }
156fec00eabSdrh 
157fec00eabSdrh /*
158fec00eabSdrh ** Return the amount of memory currently checked out.
159fec00eabSdrh */
160fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
161f7141990Sdrh   int n, mx;
162c376a198Sdrh   sqlite3_int64 res;
163f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
164c376a198Sdrh   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
165c376a198Sdrh   return res;
166fec00eabSdrh }
167fec00eabSdrh 
168fec00eabSdrh /*
169fec00eabSdrh ** Return the maximum amount of memory that has ever been
170fec00eabSdrh ** checked out since either the beginning of this process
171fec00eabSdrh ** or since the most recent reset.
172fec00eabSdrh */
173fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
174f7141990Sdrh   int n, mx;
175c376a198Sdrh   sqlite3_int64 res;
176f7141990Sdrh   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
1777986a71aSdrh   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
178c376a198Sdrh   return res;
179fec00eabSdrh }
180fec00eabSdrh 
181fec00eabSdrh /*
182fec00eabSdrh ** Change the alarm callback
183fec00eabSdrh */
184fec00eabSdrh int sqlite3_memory_alarm(
185fec00eabSdrh   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
186fec00eabSdrh   void *pArg,
187fec00eabSdrh   sqlite3_int64 iThreshold
188fec00eabSdrh ){
189fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
190fec00eabSdrh   mem0.alarmCallback = xCallback;
191fec00eabSdrh   mem0.alarmArg = pArg;
192fec00eabSdrh   mem0.alarmThreshold = iThreshold;
193fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
194fec00eabSdrh   return SQLITE_OK;
195fec00eabSdrh }
196fec00eabSdrh 
197fec00eabSdrh /*
198fec00eabSdrh ** Trigger the alarm
199fec00eabSdrh */
200fec00eabSdrh static void sqlite3MallocAlarm(int nByte){
201fec00eabSdrh   void (*xCallback)(void*,sqlite3_int64,int);
202fec00eabSdrh   sqlite3_int64 nowUsed;
203fec00eabSdrh   void *pArg;
204fec00eabSdrh   if( mem0.alarmCallback==0 || mem0.alarmBusy  ) return;
205fec00eabSdrh   mem0.alarmBusy = 1;
206fec00eabSdrh   xCallback = mem0.alarmCallback;
207f7141990Sdrh   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
208fec00eabSdrh   pArg = mem0.alarmArg;
209fec00eabSdrh   sqlite3_mutex_leave(mem0.mutex);
210fec00eabSdrh   xCallback(pArg, nowUsed, nByte);
211fec00eabSdrh   sqlite3_mutex_enter(mem0.mutex);
212fec00eabSdrh   mem0.alarmBusy = 0;
213fec00eabSdrh }
214fec00eabSdrh 
215fec00eabSdrh /*
216f7141990Sdrh ** Do a memory allocation with statistics and alarms.  Assume the
217f7141990Sdrh ** lock is already held.
218fec00eabSdrh */
219f7141990Sdrh static int mallocWithAlarm(int n, void **pp){
220fec00eabSdrh   int nFull;
221f7141990Sdrh   void *p;
222f7141990Sdrh   assert( sqlite3_mutex_held(mem0.mutex) );
223fec00eabSdrh   nFull = sqlite3Config.m.xRoundup(n);
224f7141990Sdrh   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
225f7141990Sdrh   if( mem0.alarmCallback!=0 ){
226f7141990Sdrh     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
227f7141990Sdrh     if( nUsed+nFull >= mem0.alarmThreshold ){
228fec00eabSdrh       sqlite3MallocAlarm(nFull);
229fec00eabSdrh     }
230f7141990Sdrh   }
231fec00eabSdrh   p = sqlite3Config.m.xMalloc(nFull);
232d09414cdSdanielk1977   if( p==0 && mem0.alarmCallback ){
233fec00eabSdrh     sqlite3MallocAlarm(nFull);
234d09414cdSdanielk1977     p = sqlite3Config.m.xMalloc(nFull);
235fec00eabSdrh   }
236c702c7ccSdrh   if( p ){
237c702c7ccSdrh     nFull = sqlite3MallocSize(p);
238c702c7ccSdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
239c702c7ccSdrh   }
240f7141990Sdrh   *pp = p;
241f7141990Sdrh   return nFull;
242fec00eabSdrh }
243f7141990Sdrh 
244f7141990Sdrh /*
245f7141990Sdrh ** Allocate memory.  This routine is like sqlite3_malloc() except that it
246f7141990Sdrh ** assumes the memory subsystem has already been initialized.
247f7141990Sdrh */
248f7141990Sdrh void *sqlite3Malloc(int n){
249f7141990Sdrh   void *p;
250f7141990Sdrh   if( n<=0 ){
251f7141990Sdrh     p = 0;
252f7141990Sdrh   }else if( sqlite3Config.bMemstat ){
253f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
254f7141990Sdrh     mallocWithAlarm(n, &p);
255fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
256fec00eabSdrh   }else{
257fec00eabSdrh     p = sqlite3Config.m.xMalloc(n);
258fec00eabSdrh   }
259fec00eabSdrh   return p;
260fec00eabSdrh }
261fec00eabSdrh 
262fec00eabSdrh /*
263fec00eabSdrh ** This version of the memory allocation is for use by the application.
264fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
265fec00eabSdrh ** allocation.
266fec00eabSdrh */
267fec00eabSdrh void *sqlite3_malloc(int n){
268fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
269fec00eabSdrh   if( sqlite3_initialize() ) return 0;
270fec00eabSdrh #endif
271fec00eabSdrh   return sqlite3Malloc(n);
272fec00eabSdrh }
273fec00eabSdrh 
274fec00eabSdrh /*
275e5ae5735Sdrh ** Each thread may only have a single outstanding allocation from
276facf0307Sdrh ** xScratchMalloc().  We verify this constraint in the single-threaded
277facf0307Sdrh ** case by setting scratchAllocOut to 1 when an allocation
278e5ae5735Sdrh ** is outstanding clearing it when the allocation is freed.
279e5ae5735Sdrh */
280e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
281facf0307Sdrh static int scratchAllocOut = 0;
282e5ae5735Sdrh #endif
283e5ae5735Sdrh 
284e5ae5735Sdrh 
285e5ae5735Sdrh /*
286e5ae5735Sdrh ** Allocate memory that is to be used and released right away.
287e5ae5735Sdrh ** This routine is similar to alloca() in that it is not intended
288e5ae5735Sdrh ** for situations where the memory might be held long-term.  This
289e5ae5735Sdrh ** routine is intended to get memory to old large transient data
290e5ae5735Sdrh ** structures that would not normally fit on the stack of an
291e5ae5735Sdrh ** embedded processor.
292e5ae5735Sdrh */
293facf0307Sdrh void *sqlite3ScratchMalloc(int n){
294e5ae5735Sdrh   void *p;
295e5ae5735Sdrh   assert( n>0 );
2969ac3fe97Sdrh 
297e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
2989ac3fe97Sdrh   /* Verify that no more than one scratch allocation per thread
2999ac3fe97Sdrh   ** is outstanding at one time.  (This is only checked in the
3009ac3fe97Sdrh   ** single-threaded case since checking in the multi-threaded case
3019ac3fe97Sdrh   ** would be much more complicated.) */
302facf0307Sdrh   assert( scratchAllocOut==0 );
303e5ae5735Sdrh #endif
3049ac3fe97Sdrh 
305f7141990Sdrh   if( sqlite3Config.szScratch<n ){
306f7141990Sdrh     goto scratch_overflow;
307f7141990Sdrh   }else{
308e5ae5735Sdrh     sqlite3_mutex_enter(mem0.mutex);
309f7141990Sdrh     if( mem0.nScratchFree==0 ){
310f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
311f7141990Sdrh       goto scratch_overflow;
312e5ae5735Sdrh     }else{
3139ac3fe97Sdrh       int i;
3149ac3fe97Sdrh       i = mem0.aScratchFree[--mem0.nScratchFree];
315f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
3169ac3fe97Sdrh       i *= sqlite3Config.szScratch;
317f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
318e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
3199ac3fe97Sdrh       p = (void*)&((char*)sqlite3Config.pScratch)[i];
320e5ae5735Sdrh     }
321f7141990Sdrh   }
322f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
323f7141990Sdrh   scratchAllocOut = p!=0;
324f7141990Sdrh #endif
325f7141990Sdrh 
326f7141990Sdrh   return p;
327f7141990Sdrh 
328f7141990Sdrh scratch_overflow:
329f7141990Sdrh   if( sqlite3Config.bMemstat ){
330f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
331e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
332f7141990Sdrh     n = mallocWithAlarm(n, &p);
333f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
3349ac3fe97Sdrh     sqlite3_mutex_leave(mem0.mutex);
335f7141990Sdrh   }else{
336f7141990Sdrh     p = sqlite3Config.m.xMalloc(n);
337f7141990Sdrh   }
338f7141990Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
339f7141990Sdrh   scratchAllocOut = p!=0;
340f7141990Sdrh #endif
341e5ae5735Sdrh   return p;
342e5ae5735Sdrh }
343facf0307Sdrh void sqlite3ScratchFree(void *p){
344e5ae5735Sdrh   if( p ){
3459ac3fe97Sdrh 
346e5ae5735Sdrh #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
3479ac3fe97Sdrh     /* Verify that no more than one scratch allocation per thread
3489ac3fe97Sdrh     ** is outstanding at one time.  (This is only checked in the
3499ac3fe97Sdrh     ** single-threaded case since checking in the multi-threaded case
3509ac3fe97Sdrh     ** would be much more complicated.) */
351facf0307Sdrh     assert( scratchAllocOut==1 );
352facf0307Sdrh     scratchAllocOut = 0;
353e5ae5735Sdrh #endif
3549ac3fe97Sdrh 
3559ac3fe97Sdrh     if( sqlite3Config.pScratch==0
3569ac3fe97Sdrh            || p<sqlite3Config.pScratch
3579ac3fe97Sdrh            || p>=(void*)mem0.aScratchFree ){
358f7141990Sdrh       if( sqlite3Config.bMemstat ){
359f7141990Sdrh         int iSize = sqlite3MallocSize(p);
360f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
361f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
362f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
363facf0307Sdrh         sqlite3Config.m.xFree(p);
364f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
365f7141990Sdrh       }else{
366f7141990Sdrh         sqlite3Config.m.xFree(p);
367f7141990Sdrh       }
3689ac3fe97Sdrh     }else{
3699ac3fe97Sdrh       int i;
370867d05a0Sdanielk1977       i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
3719ac3fe97Sdrh       i /= sqlite3Config.szScratch;
3729ac3fe97Sdrh       assert( i>=0 && i<sqlite3Config.nScratch );
373f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
374f7141990Sdrh       assert( mem0.nScratchFree<sqlite3Config.nScratch );
3759ac3fe97Sdrh       mem0.aScratchFree[mem0.nScratchFree++] = i;
376f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
3779ac3fe97Sdrh       sqlite3_mutex_leave(mem0.mutex);
3789ac3fe97Sdrh     }
379e5ae5735Sdrh   }
380e5ae5735Sdrh }
381e5ae5735Sdrh 
382e5ae5735Sdrh /*
383f7141990Sdrh ** Allocate memory to be used by the page cache.  Make use of the
384f7141990Sdrh ** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
385f7141990Sdrh ** and that memory is of the right size and is not completely
386f7141990Sdrh ** consumed.  Otherwise, failover to sqlite3Malloc().
387facf0307Sdrh */
3888c0a791aSdanielk1977 #if 0
389f7141990Sdrh void *sqlite3PageMalloc(int n){
390f7141990Sdrh   void *p;
391f7141990Sdrh   assert( n>0 );
392f7141990Sdrh   assert( (n & (n-1))==0 );
393f7141990Sdrh   assert( n>=512 && n<=32768 );
394f7141990Sdrh 
395f7141990Sdrh   if( sqlite3Config.szPage<n ){
396f7141990Sdrh     goto page_overflow;
397f7141990Sdrh   }else{
398f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
399f7141990Sdrh     if( mem0.nPageFree==0 ){
400f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
401f7141990Sdrh       goto page_overflow;
402f7141990Sdrh     }else{
403f7141990Sdrh       int i;
404f7141990Sdrh       i = mem0.aPageFree[--mem0.nPageFree];
405f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
406f7141990Sdrh       i *= sqlite3Config.szPage;
407e50135e2Sdrh       sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
408f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
409f7141990Sdrh       p = (void*)&((char*)sqlite3Config.pPage)[i];
410f7141990Sdrh     }
411f7141990Sdrh   }
412f7141990Sdrh   return p;
413f7141990Sdrh 
414f7141990Sdrh page_overflow:
415f7141990Sdrh   if( sqlite3Config.bMemstat ){
416f7141990Sdrh     sqlite3_mutex_enter(mem0.mutex);
417e50135e2Sdrh     sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
418f7141990Sdrh     n = mallocWithAlarm(n, &p);
419f7141990Sdrh     if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
420f7141990Sdrh     sqlite3_mutex_leave(mem0.mutex);
421f7141990Sdrh   }else{
422f7141990Sdrh     p = sqlite3Config.m.xMalloc(n);
423f7141990Sdrh   }
424f7141990Sdrh   return p;
425f7141990Sdrh }
426f7141990Sdrh void sqlite3PageFree(void *p){
427f7141990Sdrh   if( p ){
428f7141990Sdrh     if( sqlite3Config.pPage==0
429f7141990Sdrh            || p<sqlite3Config.pPage
430f7141990Sdrh            || p>=(void*)mem0.aPageFree ){
4314b9507a0Sdanielk1977       /* In this case, the page allocation was obtained from a regular
4324b9507a0Sdanielk1977       ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
4334b9507a0Sdanielk1977       ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
4344b9507a0Sdanielk1977       */
435f7141990Sdrh       if( sqlite3Config.bMemstat ){
436f7141990Sdrh         int iSize = sqlite3MallocSize(p);
437f7141990Sdrh         sqlite3_mutex_enter(mem0.mutex);
438f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
439f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
440f7141990Sdrh         sqlite3Config.m.xFree(p);
441f7141990Sdrh         sqlite3_mutex_leave(mem0.mutex);
442f7141990Sdrh       }else{
443f7141990Sdrh         sqlite3Config.m.xFree(p);
444f7141990Sdrh       }
445f7141990Sdrh     }else{
4464b9507a0Sdanielk1977       /* The page allocation was allocated from the sqlite3Config.pPage
4474b9507a0Sdanielk1977       ** buffer. In this case all that is add the index of the page in
4484b9507a0Sdanielk1977       ** the sqlite3Config.pPage array to the set of free indexes stored
4494b9507a0Sdanielk1977       ** in the mem0.aPageFree[] array.
4504b9507a0Sdanielk1977       */
451f7141990Sdrh       int i;
452867d05a0Sdanielk1977       i = (u8 *)p - (u8 *)sqlite3Config.pPage;
453f7141990Sdrh       i /= sqlite3Config.szPage;
454f7141990Sdrh       assert( i>=0 && i<sqlite3Config.nPage );
455f7141990Sdrh       sqlite3_mutex_enter(mem0.mutex);
456f7141990Sdrh       assert( mem0.nPageFree<sqlite3Config.nPage );
457f7141990Sdrh       mem0.aPageFree[mem0.nPageFree++] = i;
458f7141990Sdrh       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
459f7141990Sdrh       sqlite3_mutex_leave(mem0.mutex);
4605f4bcf15Sdrh #if !defined(NDEBUG) && 0
4614b9507a0Sdanielk1977       /* Assert that a duplicate was not just inserted into aPageFree[]. */
4624b9507a0Sdanielk1977       for(i=0; i<mem0.nPageFree-1; i++){
4634b9507a0Sdanielk1977         assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
4644b9507a0Sdanielk1977       }
4654b9507a0Sdanielk1977 #endif
466f7141990Sdrh     }
467f7141990Sdrh   }
468facf0307Sdrh }
4698c0a791aSdanielk1977 #endif
470facf0307Sdrh 
471facf0307Sdrh /*
472633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
473633e6d57Sdrh */
474633e6d57Sdrh static int isLookaside(sqlite3 *db, void *p){
475633e6d57Sdrh   return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
476633e6d57Sdrh }
477633e6d57Sdrh 
478633e6d57Sdrh /*
479fec00eabSdrh ** Return the size of a memory allocation previously obtained from
480fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
481fec00eabSdrh */
482fec00eabSdrh int sqlite3MallocSize(void *p){
483fec00eabSdrh   return sqlite3Config.m.xSize(p);
484fec00eabSdrh }
485633e6d57Sdrh int sqlite3DbMallocSize(sqlite3 *db, void *p){
486633e6d57Sdrh   if( isLookaside(db, p) ){
487633e6d57Sdrh     return db->lookaside.sz;
488633e6d57Sdrh   }else{
489633e6d57Sdrh     return sqlite3Config.m.xSize(p);
490633e6d57Sdrh   }
491633e6d57Sdrh }
492fec00eabSdrh 
493fec00eabSdrh /*
494fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
495fec00eabSdrh */
496fec00eabSdrh void sqlite3_free(void *p){
497fec00eabSdrh   if( p==0 ) return;
498fec00eabSdrh   if( sqlite3Config.bMemstat ){
499fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
500f7141990Sdrh     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
501fec00eabSdrh     sqlite3Config.m.xFree(p);
502fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
503fec00eabSdrh   }else{
504fec00eabSdrh     sqlite3Config.m.xFree(p);
505fec00eabSdrh   }
506fec00eabSdrh }
507fec00eabSdrh 
508fec00eabSdrh /*
509633e6d57Sdrh ** Free memory that might be associated with a particular database
510633e6d57Sdrh ** connection.
511633e6d57Sdrh */
512633e6d57Sdrh void sqlite3DbFree(sqlite3 *db, void *p){
513633e6d57Sdrh   if( isLookaside(db, p) ){
514633e6d57Sdrh     LookasideSlot *pBuf = (LookasideSlot*)p;
515633e6d57Sdrh     pBuf->pNext = db->lookaside.pFree;
516633e6d57Sdrh     db->lookaside.pFree = pBuf;
517633e6d57Sdrh     db->lookaside.nOut--;
518633e6d57Sdrh   }else{
519633e6d57Sdrh     sqlite3_free(p);
520633e6d57Sdrh   }
521633e6d57Sdrh }
522633e6d57Sdrh 
523633e6d57Sdrh /*
524fec00eabSdrh ** Change the size of an existing memory allocation
525fec00eabSdrh */
526fec00eabSdrh void *sqlite3Realloc(void *pOld, int nBytes){
527fec00eabSdrh   int nOld, nNew;
528fec00eabSdrh   void *pNew;
529fec00eabSdrh   if( pOld==0 ){
530fec00eabSdrh     return sqlite3Malloc(nBytes);
531fec00eabSdrh   }
532fec00eabSdrh   if( nBytes<=0 ){
533fec00eabSdrh     sqlite3_free(pOld);
534fec00eabSdrh     return 0;
535fec00eabSdrh   }
536fec00eabSdrh   nOld = sqlite3MallocSize(pOld);
537fec00eabSdrh   if( sqlite3Config.bMemstat ){
538fec00eabSdrh     sqlite3_mutex_enter(mem0.mutex);
539f7141990Sdrh     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
540fec00eabSdrh     nNew = sqlite3Config.m.xRoundup(nBytes);
541fec00eabSdrh     if( nOld==nNew ){
542fec00eabSdrh       pNew = pOld;
543fec00eabSdrh     }else{
544f7141990Sdrh       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
545f7141990Sdrh             mem0.alarmThreshold ){
546fec00eabSdrh         sqlite3MallocAlarm(nNew-nOld);
547fec00eabSdrh       }
548fec00eabSdrh       pNew = sqlite3Config.m.xRealloc(pOld, nNew);
549d09414cdSdanielk1977       if( pNew==0 && mem0.alarmCallback ){
550fec00eabSdrh         sqlite3MallocAlarm(nBytes);
551fec00eabSdrh         pNew = sqlite3Config.m.xRealloc(pOld, nNew);
552fec00eabSdrh       }
553fec00eabSdrh       if( pNew ){
554c702c7ccSdrh         nNew = sqlite3MallocSize(pNew);
555f7141990Sdrh         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
556fec00eabSdrh       }
557fec00eabSdrh     }
558fec00eabSdrh     sqlite3_mutex_leave(mem0.mutex);
559fec00eabSdrh   }else{
560fec00eabSdrh     pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
561fec00eabSdrh   }
562fec00eabSdrh   return pNew;
563fec00eabSdrh }
564fec00eabSdrh 
565fec00eabSdrh /*
566fec00eabSdrh ** The public interface to sqlite3Realloc.  Make sure that the memory
567fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
568fec00eabSdrh */
569fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
570fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
571fec00eabSdrh   if( sqlite3_initialize() ) return 0;
572fec00eabSdrh #endif
573fec00eabSdrh   return sqlite3Realloc(pOld, n);
574fec00eabSdrh }
575fec00eabSdrh 
576a3152895Sdrh 
577a3152895Sdrh /*
57817435752Sdrh ** Allocate and zero memory.
579a3152895Sdrh */
580fec00eabSdrh void *sqlite3MallocZero(int n){
581fec00eabSdrh   void *p = sqlite3Malloc(n);
582a3152895Sdrh   if( p ){
583a3152895Sdrh     memset(p, 0, n);
584a3152895Sdrh   }
585a3152895Sdrh   return p;
586a3152895Sdrh }
58717435752Sdrh 
58817435752Sdrh /*
58917435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
59017435752Sdrh ** the mallocFailed flag in the connection pointer.
59117435752Sdrh */
592fec00eabSdrh void *sqlite3DbMallocZero(sqlite3 *db, int n){
593a1644fd8Sdanielk1977   void *p = sqlite3DbMallocRaw(db, n);
59417435752Sdrh   if( p ){
59517435752Sdrh     memset(p, 0, n);
59617435752Sdrh   }
59717435752Sdrh   return p;
59817435752Sdrh }
59917435752Sdrh 
60017435752Sdrh /*
60117435752Sdrh ** Allocate and zero memory.  If the allocation fails, make
60217435752Sdrh ** the mallocFailed flag in the connection pointer.
60317435752Sdrh */
604fec00eabSdrh void *sqlite3DbMallocRaw(sqlite3 *db, int n){
605633e6d57Sdrh   void *p;
606633e6d57Sdrh   if( db ){
607633e6d57Sdrh     LookasideSlot *pBuf;
608633e6d57Sdrh     if( db->mallocFailed ){
609633e6d57Sdrh       return 0;
610633e6d57Sdrh     }
611633e6d57Sdrh     if( db->lookaside.bEnabled && n<=db->lookaside.sz
612633e6d57Sdrh          && (pBuf = db->lookaside.pFree)!=0 ){
613633e6d57Sdrh       db->lookaside.pFree = pBuf->pNext;
614633e6d57Sdrh       db->lookaside.nOut++;
615633e6d57Sdrh       if( db->lookaside.nOut>db->lookaside.mxOut ){
616633e6d57Sdrh         db->lookaside.mxOut = db->lookaside.nOut;
617633e6d57Sdrh       }
618633e6d57Sdrh       return (void*)pBuf;
619633e6d57Sdrh     }
620633e6d57Sdrh   }
621fec00eabSdrh   p = sqlite3Malloc(n);
622f3a65f7eSdrh   if( !p && db ){
62317435752Sdrh     db->mallocFailed = 1;
62417435752Sdrh   }
62517435752Sdrh   return p;
62617435752Sdrh }
62717435752Sdrh 
62826783a58Sdanielk1977 /*
62926783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
63026783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
63126783a58Sdanielk1977 */
632a1644fd8Sdanielk1977 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
633a1644fd8Sdanielk1977   void *pNew = 0;
634a1644fd8Sdanielk1977   if( db->mallocFailed==0 ){
635633e6d57Sdrh     if( p==0 ){
636633e6d57Sdrh       return sqlite3DbMallocRaw(db, n);
637633e6d57Sdrh     }
638633e6d57Sdrh     if( isLookaside(db, p) ){
639633e6d57Sdrh       if( n<=db->lookaside.sz ){
640633e6d57Sdrh         return p;
641633e6d57Sdrh       }
642633e6d57Sdrh       pNew = sqlite3DbMallocRaw(db, n);
643633e6d57Sdrh       if( pNew ){
644633e6d57Sdrh         memcpy(pNew, p, db->lookaside.sz);
645633e6d57Sdrh         sqlite3DbFree(db, p);
646633e6d57Sdrh       }
647633e6d57Sdrh     }else{
648a1644fd8Sdanielk1977       pNew = sqlite3_realloc(p, n);
649a1644fd8Sdanielk1977       if( !pNew ){
650a1644fd8Sdanielk1977         db->mallocFailed = 1;
651a1644fd8Sdanielk1977       }
652a1644fd8Sdanielk1977     }
653633e6d57Sdrh   }
654a1644fd8Sdanielk1977   return pNew;
655a1644fd8Sdanielk1977 }
656a1644fd8Sdanielk1977 
65717435752Sdrh /*
65817435752Sdrh ** Attempt to reallocate p.  If the reallocation fails, then free p
65917435752Sdrh ** and set the mallocFailed flag in the database connection.
66017435752Sdrh */
66117435752Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
662a3152895Sdrh   void *pNew;
663a1644fd8Sdanielk1977   pNew = sqlite3DbRealloc(db, p, n);
664a3152895Sdrh   if( !pNew ){
665633e6d57Sdrh     sqlite3DbFree(db, p);
666a3152895Sdrh   }
667a3152895Sdrh   return pNew;
668a3152895Sdrh }
669a3152895Sdrh 
670a3152895Sdrh /*
671a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
672a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
673a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
674a3152895Sdrh ** called via macros that record the current file and line number in the
675a3152895Sdrh ** ThreadData structure.
676a3152895Sdrh */
677633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
678a3152895Sdrh   char *zNew;
679633e6d57Sdrh   size_t n;
680633e6d57Sdrh   if( z==0 ){
681633e6d57Sdrh     return 0;
682a3152895Sdrh   }
683633e6d57Sdrh   n = strlen(z)+1;
684633e6d57Sdrh   assert( (n&0x7fffffff)==n );
685633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, (int)n);
686a3152895Sdrh   if( zNew ){
687a3152895Sdrh     memcpy(zNew, z, n);
6881e536953Sdanielk1977   }
6891e536953Sdanielk1977   return zNew;
6901e536953Sdanielk1977 }
6911e536953Sdanielk1977 char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
692633e6d57Sdrh   char *zNew;
693633e6d57Sdrh   if( z==0 ){
694633e6d57Sdrh     return 0;
695633e6d57Sdrh   }
696633e6d57Sdrh   assert( (n&0x7fffffff)==n );
697633e6d57Sdrh   zNew = sqlite3DbMallocRaw(db, n+1);
698633e6d57Sdrh   if( zNew ){
699633e6d57Sdrh     memcpy(zNew, z, n);
700633e6d57Sdrh     zNew[n] = 0;
7011e536953Sdanielk1977   }
7021e536953Sdanielk1977   return zNew;
7031e536953Sdanielk1977 }
7041e536953Sdanielk1977 
705a3152895Sdrh /*
706f089aa45Sdrh ** Create a string from the zFromat argument and the va_list that follows.
707f089aa45Sdrh ** Store the string in memory obtained from sqliteMalloc() and make *pz
708f089aa45Sdrh ** point to that string.
709a3152895Sdrh */
710f089aa45Sdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
711a3152895Sdrh   va_list ap;
712f089aa45Sdrh   char *z;
713a3152895Sdrh 
714f089aa45Sdrh   va_start(ap, zFormat);
715f089aa45Sdrh   z = sqlite3VMPrintf(db, zFormat, ap);
716a3152895Sdrh   va_end(ap);
717633e6d57Sdrh   sqlite3DbFree(db, *pz);
718f089aa45Sdrh   *pz = z;
719a3152895Sdrh }
720a3152895Sdrh 
721a3152895Sdrh 
722a3152895Sdrh /*
723a3152895Sdrh ** This function must be called before exiting any API function (i.e.
72417435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
72517435752Sdrh ** sqlite3_realloc.
726a3152895Sdrh **
727a3152895Sdrh ** The returned value is normally a copy of the second argument to this
728a3152895Sdrh ** function. However, if a malloc() failure has occured since the previous
729a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
730a3152895Sdrh **
731a3152895Sdrh ** If the first argument, db, is not NULL and a malloc() error has occured,
732a3152895Sdrh ** then the connection error-code (the value returned by sqlite3_errcode())
733a3152895Sdrh ** is set to SQLITE_NOMEM.
734a3152895Sdrh */
735a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
736a1644fd8Sdanielk1977   /* If the db handle is not NULL, then we must hold the connection handle
737a1644fd8Sdanielk1977   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
738a1644fd8Sdanielk1977   ** is unsafe, as is the call to sqlite3Error().
739a1644fd8Sdanielk1977   */
740a1644fd8Sdanielk1977   assert( !db || sqlite3_mutex_held(db->mutex) );
7411e536953Sdanielk1977   if( db && db->mallocFailed ){
742a3152895Sdrh     sqlite3Error(db, SQLITE_NOMEM, 0);
74317435752Sdrh     db->mallocFailed = 0;
744a3152895Sdrh     rc = SQLITE_NOMEM;
745a3152895Sdrh   }
746a3152895Sdrh   return rc & (db ? db->errMask : 0xff);
747a3152895Sdrh }
748