blob: 2d09af68ae2d82a9d46b8cd81f9e48d7e0244441 [file] [log] [blame]
drha3152892007-05-05 11:48:52 +00001/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
drhfec00ea2008-06-14 16:56:21 +000012**
drha3152892007-05-05 11:48:52 +000013** Memory allocation functions used throughout sqlite.
14**
drh4150ebf2008-10-11 15:38:29 +000015** $Id: malloc.c,v 1.43 2008/10/11 15:38:30 drh Exp $
drha3152892007-05-05 11:48:52 +000016*/
17#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000018#include <stdarg.h>
19#include <ctype.h>
20
21/*
drhb21c8cd2007-08-21 19:33:56 +000022** This routine runs when the memory allocator sees that the
23** total memory allocation is about to exceed the soft heap
24** limit.
25*/
26static void softHeapLimitEnforcer(
27 void *NotUsed,
drh153c62c2007-08-24 03:51:33 +000028 sqlite3_int64 inUse,
29 int allocSize
drhb21c8cd2007-08-21 19:33:56 +000030){
31 sqlite3_release_memory(allocSize);
32}
33
34/*
danielk197784680242008-06-23 11:11:35 +000035** Set the soft heap-size limit for the library. Passing a zero or
36** negative value indicates no limit.
drha3152892007-05-05 11:48:52 +000037*/
38void sqlite3_soft_heap_limit(int n){
drhb21c8cd2007-08-21 19:33:56 +000039 sqlite3_uint64 iLimit;
40 int overage;
41 if( n<0 ){
42 iLimit = 0;
43 }else{
44 iLimit = n;
drha3152892007-05-05 11:48:52 +000045 }
drh9ac3fe92008-06-18 18:12:04 +000046 sqlite3_initialize();
drhb21c8cd2007-08-21 19:33:56 +000047 if( iLimit>0 ){
shane4a27a282008-09-04 04:32:49 +000048 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
drhb21c8cd2007-08-21 19:33:56 +000049 }else{
shane4a27a282008-09-04 04:32:49 +000050 sqlite3MemoryAlarm(0, 0, 0);
drhb21c8cd2007-08-21 19:33:56 +000051 }
52 overage = sqlite3_memory_used() - n;
53 if( overage>0 ){
54 sqlite3_release_memory(overage);
55 }
drha3152892007-05-05 11:48:52 +000056}
57
58/*
danielk197784680242008-06-23 11:11:35 +000059** Attempt to release up to n bytes of non-essential memory currently
60** held by SQLite. An example of non-essential memory is memory used to
61** cache database pages that are not currently in use.
drha3152892007-05-05 11:48:52 +000062*/
63int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +000064#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197767e3da72008-08-21 12:19:44 +000065 int nRet = 0;
66#if 0
67 nRet += sqlite3VdbeReleaseMemory(n);
68#endif
69 nRet += sqlite3PcacheReleaseMemory(n-nRet);
danielk1977dfb316d2008-03-26 18:34:43 +000070 return nRet;
danielk19771e536952007-08-16 10:09:01 +000071#else
72 return SQLITE_OK;
73#endif
drha3152892007-05-05 11:48:52 +000074}
drha3152892007-05-05 11:48:52 +000075
drhfec00ea2008-06-14 16:56:21 +000076/*
77** State information local to the memory allocation subsystem.
78*/
danielk19775c8f8582008-09-02 10:22:00 +000079static SQLITE_WSD struct Mem0Global {
danielk197723bf0f42008-09-02 17:52:51 +000080 /* Number of free pages for scratch and page-cache memory */
81 u32 nScratchFree;
82 u32 nPageFree;
83
drhfec00ea2008-06-14 16:56:21 +000084 sqlite3_mutex *mutex; /* Mutex to serialize access */
85
86 /*
87 ** The alarm callback and its arguments. The mem0.mutex lock will
88 ** be held while the callback is running. Recursive calls into
89 ** the memory subsystem are allowed, but no new callbacks will be
90 ** issued. The alarmBusy variable is set to prevent recursive
91 ** callbacks.
92 */
93 sqlite3_int64 alarmThreshold;
94 void (*alarmCallback)(void*, sqlite3_int64,int);
95 void *alarmArg;
96 int alarmBusy;
97
98 /*
danielk1977075c23a2008-09-01 18:34:20 +000099 ** Pointers to the end of sqlite3GlobalConfig.pScratch and
100 ** sqlite3GlobalConfig.pPage to a block of memory that records
drh9ac3fe92008-06-18 18:12:04 +0000101 ** which pages are available.
102 */
103 u32 *aScratchFree;
104 u32 *aPageFree;
danielk197723bf0f42008-09-02 17:52:51 +0000105} mem0 = { 62560955 };
danielk19775c8f8582008-09-02 10:22:00 +0000106
107#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000108
109/*
110** Initialize the memory allocation subsystem.
111*/
112int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000113 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000114 sqlite3MemSetDefault();
115 }
116 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000117 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000118 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000119 }
danielk1977075c23a2008-09-01 18:34:20 +0000120 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
121 && sqlite3GlobalConfig.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000122 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000123 sqlite3GlobalConfig.szScratch -= 4;
124 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
125 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
126 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
127 mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
drh9ac3fe92008-06-18 18:12:04 +0000128 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000129 sqlite3GlobalConfig.pScratch = 0;
130 sqlite3GlobalConfig.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000131 }
danielk1977075c23a2008-09-01 18:34:20 +0000132 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
133 && sqlite3GlobalConfig.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000134 int i;
drh0a60a382008-07-31 17:16:05 +0000135 int overhead;
danielk1977075c23a2008-09-01 18:34:20 +0000136 int sz = sqlite3GlobalConfig.szPage;
137 int n = sqlite3GlobalConfig.nPage;
drh0a60a382008-07-31 17:16:05 +0000138 overhead = (4*n + sz - 1)/sz;
danielk1977075c23a2008-09-01 18:34:20 +0000139 sqlite3GlobalConfig.nPage -= overhead;
140 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
141 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
142 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
143 mem0.nPageFree = sqlite3GlobalConfig.nPage;
drh9ac3fe92008-06-18 18:12:04 +0000144 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000145 sqlite3GlobalConfig.pPage = 0;
146 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000147 }
danielk1977075c23a2008-09-01 18:34:20 +0000148 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000149}
150
151/*
152** Deinitialize the memory allocation subsystem.
153*/
154void sqlite3MallocEnd(void){
danielk1977075c23a2008-09-01 18:34:20 +0000155 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
drh9ac3fe92008-06-18 18:12:04 +0000156 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000157}
158
159/*
160** Return the amount of memory currently checked out.
161*/
162sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000163 int n, mx;
drhc376a192008-07-14 12:30:54 +0000164 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000165 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000166 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
167 return res;
drhfec00ea2008-06-14 16:56:21 +0000168}
169
170/*
171** Return the maximum amount of memory that has ever been
172** checked out since either the beginning of this process
173** or since the most recent reset.
174*/
175sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000176 int n, mx;
drhc376a192008-07-14 12:30:54 +0000177 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000178 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000179 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000180 return res;
drhfec00ea2008-06-14 16:56:21 +0000181}
182
183/*
184** Change the alarm callback
185*/
shane4a27a282008-09-04 04:32:49 +0000186int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000187 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
188 void *pArg,
189 sqlite3_int64 iThreshold
190){
191 sqlite3_mutex_enter(mem0.mutex);
192 mem0.alarmCallback = xCallback;
193 mem0.alarmArg = pArg;
194 mem0.alarmThreshold = iThreshold;
195 sqlite3_mutex_leave(mem0.mutex);
196 return SQLITE_OK;
197}
198
199/*
shane4a27a282008-09-04 04:32:49 +0000200** Deprecated external interface. Internal/core SQLite code
201** should call sqlite3MemoryAlarm.
202*/
203int sqlite3_memory_alarm(
204 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
205 void *pArg,
206 sqlite3_int64 iThreshold
207){
208 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
209}
210
211/*
drhfec00ea2008-06-14 16:56:21 +0000212** Trigger the alarm
213*/
214static void sqlite3MallocAlarm(int nByte){
215 void (*xCallback)(void*,sqlite3_int64,int);
216 sqlite3_int64 nowUsed;
217 void *pArg;
218 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
219 mem0.alarmBusy = 1;
220 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000221 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000222 pArg = mem0.alarmArg;
223 sqlite3_mutex_leave(mem0.mutex);
224 xCallback(pArg, nowUsed, nByte);
225 sqlite3_mutex_enter(mem0.mutex);
226 mem0.alarmBusy = 0;
227}
228
drhf7141992008-06-19 00:16:08 +0000229/*
230** Do a memory allocation with statistics and alarms. Assume the
231** lock is already held.
232*/
233static int mallocWithAlarm(int n, void **pp){
234 int nFull;
235 void *p;
236 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000237 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000238 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
239 if( mem0.alarmCallback!=0 ){
240 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
241 if( nUsed+nFull >= mem0.alarmThreshold ){
242 sqlite3MallocAlarm(nFull);
243 }
244 }
danielk1977075c23a2008-09-01 18:34:20 +0000245 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000246 if( p==0 && mem0.alarmCallback ){
247 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000248 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000249 }
drhc702c7c2008-07-18 18:56:16 +0000250 if( p ){
251 nFull = sqlite3MallocSize(p);
252 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
253 }
drhf7141992008-06-19 00:16:08 +0000254 *pp = p;
255 return nFull;
256}
drhfec00ea2008-06-14 16:56:21 +0000257
258/*
259** Allocate memory. This routine is like sqlite3_malloc() except that it
260** assumes the memory subsystem has already been initialized.
261*/
262void *sqlite3Malloc(int n){
263 void *p;
drhfec00ea2008-06-14 16:56:21 +0000264 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000265 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000266 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000267 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000268 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000269 sqlite3_mutex_leave(mem0.mutex);
270 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000271 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000272 }
273 return p;
274}
275
276/*
277** This version of the memory allocation is for use by the application.
278** First make sure the memory subsystem is initialized, then do the
279** allocation.
280*/
281void *sqlite3_malloc(int n){
282#ifndef SQLITE_OMIT_AUTOINIT
283 if( sqlite3_initialize() ) return 0;
284#endif
285 return sqlite3Malloc(n);
286}
287
288/*
drhe5ae5732008-06-15 02:51:47 +0000289** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000290** xScratchMalloc(). We verify this constraint in the single-threaded
291** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000292** is outstanding clearing it when the allocation is freed.
293*/
294#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000295static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000296#endif
297
298
299/*
300** Allocate memory that is to be used and released right away.
301** This routine is similar to alloca() in that it is not intended
302** for situations where the memory might be held long-term. This
303** routine is intended to get memory to old large transient data
304** structures that would not normally fit on the stack of an
305** embedded processor.
306*/
drhfacf0302008-06-17 15:12:00 +0000307void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000308 void *p;
309 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000310
drhe5ae5732008-06-15 02:51:47 +0000311#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000312 /* Verify that no more than one scratch allocation per thread
313 ** is outstanding at one time. (This is only checked in the
314 ** single-threaded case since checking in the multi-threaded case
315 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000316 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000317#endif
drh9ac3fe92008-06-18 18:12:04 +0000318
danielk1977075c23a2008-09-01 18:34:20 +0000319 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000320 goto scratch_overflow;
321 }else{
322 sqlite3_mutex_enter(mem0.mutex);
323 if( mem0.nScratchFree==0 ){
324 sqlite3_mutex_leave(mem0.mutex);
325 goto scratch_overflow;
326 }else{
327 int i;
328 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000329 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000330 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000331 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000332 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000333 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
drhf7141992008-06-19 00:16:08 +0000334 }
drhe5ae5732008-06-15 02:51:47 +0000335 }
drhf7141992008-06-19 00:16:08 +0000336#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
337 scratchAllocOut = p!=0;
338#endif
339
drhe5ae5732008-06-15 02:51:47 +0000340 return p;
drhf7141992008-06-19 00:16:08 +0000341
342scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000343 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000344 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000345 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000346 n = mallocWithAlarm(n, &p);
347 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
348 sqlite3_mutex_leave(mem0.mutex);
349 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000350 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000351 }
352#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
353 scratchAllocOut = p!=0;
354#endif
355 return p;
drhe5ae5732008-06-15 02:51:47 +0000356}
drhfacf0302008-06-17 15:12:00 +0000357void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000358 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000359
drhe5ae5732008-06-15 02:51:47 +0000360#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000361 /* Verify that no more than one scratch allocation per thread
362 ** is outstanding at one time. (This is only checked in the
363 ** single-threaded case since checking in the multi-threaded case
364 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000365 assert( scratchAllocOut==1 );
366 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000367#endif
drh9ac3fe92008-06-18 18:12:04 +0000368
danielk1977075c23a2008-09-01 18:34:20 +0000369 if( sqlite3GlobalConfig.pScratch==0
370 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000371 || p>=(void*)mem0.aScratchFree ){
danielk1977075c23a2008-09-01 18:34:20 +0000372 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000373 int iSize = sqlite3MallocSize(p);
374 sqlite3_mutex_enter(mem0.mutex);
375 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
376 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000377 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000378 sqlite3_mutex_leave(mem0.mutex);
379 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000380 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000381 }
drh9ac3fe92008-06-18 18:12:04 +0000382 }else{
383 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000384 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
385 i /= sqlite3GlobalConfig.szScratch;
386 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000387 sqlite3_mutex_enter(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000388 assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000389 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000390 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000391 sqlite3_mutex_leave(mem0.mutex);
392 }
drhe5ae5732008-06-15 02:51:47 +0000393 }
394}
395
396/*
drhf7141992008-06-19 00:16:08 +0000397** Allocate memory to be used by the page cache. Make use of the
398** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
399** and that memory is of the right size and is not completely
400** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000401*/
danielk19778c0a7912008-08-20 14:49:23 +0000402#if 0
drhf7141992008-06-19 00:16:08 +0000403void *sqlite3PageMalloc(int n){
404 void *p;
405 assert( n>0 );
406 assert( (n & (n-1))==0 );
407 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000408
danielk1977075c23a2008-09-01 18:34:20 +0000409 if( sqlite3GlobalConfig.szPage<n ){
drhf7141992008-06-19 00:16:08 +0000410 goto page_overflow;
411 }else{
412 sqlite3_mutex_enter(mem0.mutex);
413 if( mem0.nPageFree==0 ){
414 sqlite3_mutex_leave(mem0.mutex);
415 goto page_overflow;
416 }else{
417 int i;
418 i = mem0.aPageFree[--mem0.nPageFree];
419 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000420 i *= sqlite3GlobalConfig.szPage;
drhe50135e2008-08-05 17:53:22 +0000421 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000422 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000423 p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
drhf7141992008-06-19 00:16:08 +0000424 }
425 }
426 return p;
427
428page_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000429 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000430 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000431 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000432 n = mallocWithAlarm(n, &p);
433 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
434 sqlite3_mutex_leave(mem0.mutex);
435 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000436 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000437 }
438 return p;
drhfacf0302008-06-17 15:12:00 +0000439}
drhf7141992008-06-19 00:16:08 +0000440void sqlite3PageFree(void *p){
441 if( p ){
danielk1977075c23a2008-09-01 18:34:20 +0000442 if( sqlite3GlobalConfig.pPage==0
443 || p<sqlite3GlobalConfig.pPage
drhf7141992008-06-19 00:16:08 +0000444 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000445 /* In this case, the page allocation was obtained from a regular
446 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
447 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
448 */
danielk1977075c23a2008-09-01 18:34:20 +0000449 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000450 int iSize = sqlite3MallocSize(p);
451 sqlite3_mutex_enter(mem0.mutex);
452 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
453 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000454 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000455 sqlite3_mutex_leave(mem0.mutex);
456 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000457 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000458 }
459 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000460 /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
danielk19774b9507a2008-06-21 08:12:15 +0000461 ** buffer. In this case all that is add the index of the page in
danielk1977075c23a2008-09-01 18:34:20 +0000462 ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
danielk19774b9507a2008-06-21 08:12:15 +0000463 ** in the mem0.aPageFree[] array.
464 */
drhf7141992008-06-19 00:16:08 +0000465 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000466 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
467 i /= sqlite3GlobalConfig.szPage;
468 assert( i>=0 && i<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000469 sqlite3_mutex_enter(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000470 assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000471 mem0.aPageFree[mem0.nPageFree++] = i;
472 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
473 sqlite3_mutex_leave(mem0.mutex);
drh5f4bcf12008-07-29 14:29:06 +0000474#if !defined(NDEBUG) && 0
danielk19774b9507a2008-06-21 08:12:15 +0000475 /* Assert that a duplicate was not just inserted into aPageFree[]. */
476 for(i=0; i<mem0.nPageFree-1; i++){
477 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
478 }
479#endif
drhf7141992008-06-19 00:16:08 +0000480 }
481 }
drhfacf0302008-06-17 15:12:00 +0000482}
danielk19778c0a7912008-08-20 14:49:23 +0000483#endif
drhfacf0302008-06-17 15:12:00 +0000484
485/*
drh633e6d52008-07-28 19:34:53 +0000486** TRUE if p is a lookaside memory allocation from db
487*/
drh4150ebf2008-10-11 15:38:29 +0000488#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000489static int isLookaside(sqlite3 *db, void *p){
490 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
491}
drh4150ebf2008-10-11 15:38:29 +0000492#else
493#define isLookaside(A,B) 0
494#endif
drh633e6d52008-07-28 19:34:53 +0000495
496/*
drhfec00ea2008-06-14 16:56:21 +0000497** Return the size of a memory allocation previously obtained from
498** sqlite3Malloc() or sqlite3_malloc().
499*/
500int sqlite3MallocSize(void *p){
danielk1977075c23a2008-09-01 18:34:20 +0000501 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000502}
drh633e6d52008-07-28 19:34:53 +0000503int sqlite3DbMallocSize(sqlite3 *db, void *p){
504 if( isLookaside(db, p) ){
505 return db->lookaside.sz;
506 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000507 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000508 }
509}
drhfec00ea2008-06-14 16:56:21 +0000510
511/*
512** Free memory previously obtained from sqlite3Malloc().
513*/
514void sqlite3_free(void *p){
515 if( p==0 ) return;
danielk1977075c23a2008-09-01 18:34:20 +0000516 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000517 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000518 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000519 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000520 sqlite3_mutex_leave(mem0.mutex);
521 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000522 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000523 }
524}
525
526/*
drh633e6d52008-07-28 19:34:53 +0000527** Free memory that might be associated with a particular database
528** connection.
529*/
530void sqlite3DbFree(sqlite3 *db, void *p){
531 if( isLookaside(db, p) ){
532 LookasideSlot *pBuf = (LookasideSlot*)p;
533 pBuf->pNext = db->lookaside.pFree;
534 db->lookaside.pFree = pBuf;
535 db->lookaside.nOut--;
536 }else{
537 sqlite3_free(p);
538 }
539}
540
541/*
drhfec00ea2008-06-14 16:56:21 +0000542** Change the size of an existing memory allocation
543*/
544void *sqlite3Realloc(void *pOld, int nBytes){
545 int nOld, nNew;
546 void *pNew;
547 if( pOld==0 ){
548 return sqlite3Malloc(nBytes);
549 }
550 if( nBytes<=0 ){
551 sqlite3_free(pOld);
552 return 0;
553 }
554 nOld = sqlite3MallocSize(pOld);
danielk1977075c23a2008-09-01 18:34:20 +0000555 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000556 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000557 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000558 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000559 if( nOld==nNew ){
560 pNew = pOld;
561 }else{
drhf7141992008-06-19 00:16:08 +0000562 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
563 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000564 sqlite3MallocAlarm(nNew-nOld);
565 }
danielk1977075c23a2008-09-01 18:34:20 +0000566 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
danielk1977d09414c2008-06-19 18:17:49 +0000567 if( pNew==0 && mem0.alarmCallback ){
568 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000569 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000570 }
571 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000572 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000573 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000574 }
575 }
576 sqlite3_mutex_leave(mem0.mutex);
577 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000578 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000579 }
580 return pNew;
581}
582
583/*
584** The public interface to sqlite3Realloc. Make sure that the memory
585** subsystem is initialized prior to invoking sqliteRealloc.
586*/
587void *sqlite3_realloc(void *pOld, int n){
588#ifndef SQLITE_OMIT_AUTOINIT
589 if( sqlite3_initialize() ) return 0;
590#endif
591 return sqlite3Realloc(pOld, n);
592}
593
drha3152892007-05-05 11:48:52 +0000594
595/*
drh17435752007-08-16 04:30:38 +0000596** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000597*/
drhfec00ea2008-06-14 16:56:21 +0000598void *sqlite3MallocZero(int n){
599 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000600 if( p ){
601 memset(p, 0, n);
602 }
603 return p;
604}
drh17435752007-08-16 04:30:38 +0000605
606/*
607** Allocate and zero memory. If the allocation fails, make
608** the mallocFailed flag in the connection pointer.
609*/
drhfec00ea2008-06-14 16:56:21 +0000610void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000611 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000612 if( p ){
613 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000614 }
615 return p;
616}
617
618/*
619** Allocate and zero memory. If the allocation fails, make
620** the mallocFailed flag in the connection pointer.
621*/
drhfec00ea2008-06-14 16:56:21 +0000622void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000623 void *p;
drh4150ebf2008-10-11 15:38:29 +0000624#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000625 if( db ){
626 LookasideSlot *pBuf;
627 if( db->mallocFailed ){
628 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000629 }
drh633e6d52008-07-28 19:34:53 +0000630 if( db->lookaside.bEnabled && n<=db->lookaside.sz
631 && (pBuf = db->lookaside.pFree)!=0 ){
632 db->lookaside.pFree = pBuf->pNext;
633 db->lookaside.nOut++;
634 if( db->lookaside.nOut>db->lookaside.mxOut ){
635 db->lookaside.mxOut = db->lookaside.nOut;
636 }
637 return (void*)pBuf;
638 }
639 }
drh4150ebf2008-10-11 15:38:29 +0000640#endif
drh633e6d52008-07-28 19:34:53 +0000641 p = sqlite3Malloc(n);
642 if( !p && db ){
643 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000644 }
645 return p;
646}
647
danielk197726783a52007-08-29 14:06:22 +0000648/*
649** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000650** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000651*/
danielk1977a1644fd2007-08-29 12:31:25 +0000652void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
653 void *pNew = 0;
654 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000655 if( p==0 ){
656 return sqlite3DbMallocRaw(db, n);
657 }
658 if( isLookaside(db, p) ){
659 if( n<=db->lookaside.sz ){
660 return p;
661 }
662 pNew = sqlite3DbMallocRaw(db, n);
663 if( pNew ){
664 memcpy(pNew, p, db->lookaside.sz);
665 sqlite3DbFree(db, p);
666 }
667 }else{
668 pNew = sqlite3_realloc(p, n);
669 if( !pNew ){
670 db->mallocFailed = 1;
671 }
danielk1977a1644fd2007-08-29 12:31:25 +0000672 }
673 }
674 return pNew;
675}
676
drh17435752007-08-16 04:30:38 +0000677/*
678** Attempt to reallocate p. If the reallocation fails, then free p
679** and set the mallocFailed flag in the database connection.
680*/
681void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000682 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000683 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000684 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000685 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000686 }
687 return pNew;
688}
689
drha3152892007-05-05 11:48:52 +0000690/*
691** Make a copy of a string in memory obtained from sqliteMalloc(). These
692** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
693** is because when memory debugging is turned on, these two functions are
694** called via macros that record the current file and line number in the
695** ThreadData structure.
696*/
drh633e6d52008-07-28 19:34:53 +0000697char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000698 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000699 size_t n;
700 if( z==0 ){
701 return 0;
702 }
drha3152892007-05-05 11:48:52 +0000703 n = strlen(z)+1;
drh633e6d52008-07-28 19:34:53 +0000704 assert( (n&0x7fffffff)==n );
705 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000706 if( zNew ){
707 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000708 }
709 return zNew;
710}
711char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000712 char *zNew;
713 if( z==0 ){
714 return 0;
715 }
716 assert( (n&0x7fffffff)==n );
717 zNew = sqlite3DbMallocRaw(db, n+1);
718 if( zNew ){
719 memcpy(zNew, z, n);
720 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000721 }
722 return zNew;
723}
724
drha3152892007-05-05 11:48:52 +0000725/*
drhf089aa42008-07-08 19:34:06 +0000726** Create a string from the zFromat argument and the va_list that follows.
727** Store the string in memory obtained from sqliteMalloc() and make *pz
728** point to that string.
drha3152892007-05-05 11:48:52 +0000729*/
drhf089aa42008-07-08 19:34:06 +0000730void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000731 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000732 char *z;
drha3152892007-05-05 11:48:52 +0000733
drhf089aa42008-07-08 19:34:06 +0000734 va_start(ap, zFormat);
735 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000736 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000737 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000738 *pz = z;
drha3152892007-05-05 11:48:52 +0000739}
740
741
742/*
743** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000744** returning control to the user) that has called sqlite3_malloc or
745** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000746**
747** The returned value is normally a copy of the second argument to this
748** function. However, if a malloc() failure has occured since the previous
749** invocation SQLITE_NOMEM is returned instead.
750**
751** If the first argument, db, is not NULL and a malloc() error has occured,
752** then the connection error-code (the value returned by sqlite3_errcode())
753** is set to SQLITE_NOMEM.
754*/
drha3152892007-05-05 11:48:52 +0000755int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000756 /* If the db handle is not NULL, then we must hold the connection handle
757 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
758 ** is unsafe, as is the call to sqlite3Error().
759 */
760 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000761 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000762 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000763 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000764 rc = SQLITE_NOMEM;
765 }
766 return rc & (db ? db->errMask : 0xff);
767}