blob: 61e1c1dbff58b3d54e52a19c59a5430d82eba884 [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**
danielk197723bf0f42008-09-02 17:52:51 +000015** $Id: malloc.c,v 1.40 2008/09/02 17:52:52 danielk1977 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 ){
48 sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
49 }else{
50 sqlite3_memory_alarm(0, 0, 0);
51 }
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*/
186int sqlite3_memory_alarm(
187 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/*
200** Trigger the alarm
201*/
202static void sqlite3MallocAlarm(int nByte){
203 void (*xCallback)(void*,sqlite3_int64,int);
204 sqlite3_int64 nowUsed;
205 void *pArg;
206 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
207 mem0.alarmBusy = 1;
208 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000209 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000210 pArg = mem0.alarmArg;
211 sqlite3_mutex_leave(mem0.mutex);
212 xCallback(pArg, nowUsed, nByte);
213 sqlite3_mutex_enter(mem0.mutex);
214 mem0.alarmBusy = 0;
215}
216
drhf7141992008-06-19 00:16:08 +0000217/*
218** Do a memory allocation with statistics and alarms. Assume the
219** lock is already held.
220*/
221static int mallocWithAlarm(int n, void **pp){
222 int nFull;
223 void *p;
224 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000225 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000226 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
227 if( mem0.alarmCallback!=0 ){
228 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
229 if( nUsed+nFull >= mem0.alarmThreshold ){
230 sqlite3MallocAlarm(nFull);
231 }
232 }
danielk1977075c23a2008-09-01 18:34:20 +0000233 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000234 if( p==0 && mem0.alarmCallback ){
235 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000236 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000237 }
drhc702c7c2008-07-18 18:56:16 +0000238 if( p ){
239 nFull = sqlite3MallocSize(p);
240 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
241 }
drhf7141992008-06-19 00:16:08 +0000242 *pp = p;
243 return nFull;
244}
drhfec00ea2008-06-14 16:56:21 +0000245
246/*
247** Allocate memory. This routine is like sqlite3_malloc() except that it
248** assumes the memory subsystem has already been initialized.
249*/
250void *sqlite3Malloc(int n){
251 void *p;
drhfec00ea2008-06-14 16:56:21 +0000252 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000253 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000254 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000255 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000256 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000257 sqlite3_mutex_leave(mem0.mutex);
258 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000259 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000260 }
261 return p;
262}
263
264/*
265** This version of the memory allocation is for use by the application.
266** First make sure the memory subsystem is initialized, then do the
267** allocation.
268*/
269void *sqlite3_malloc(int n){
270#ifndef SQLITE_OMIT_AUTOINIT
271 if( sqlite3_initialize() ) return 0;
272#endif
273 return sqlite3Malloc(n);
274}
275
276/*
drhe5ae5732008-06-15 02:51:47 +0000277** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000278** xScratchMalloc(). We verify this constraint in the single-threaded
279** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000280** is outstanding clearing it when the allocation is freed.
281*/
282#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000283static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000284#endif
285
286
287/*
288** Allocate memory that is to be used and released right away.
289** This routine is similar to alloca() in that it is not intended
290** for situations where the memory might be held long-term. This
291** routine is intended to get memory to old large transient data
292** structures that would not normally fit on the stack of an
293** embedded processor.
294*/
drhfacf0302008-06-17 15:12:00 +0000295void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000296 void *p;
297 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000298
drhe5ae5732008-06-15 02:51:47 +0000299#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000300 /* Verify that no more than one scratch allocation per thread
301 ** is outstanding at one time. (This is only checked in the
302 ** single-threaded case since checking in the multi-threaded case
303 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000304 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000305#endif
drh9ac3fe92008-06-18 18:12:04 +0000306
danielk1977075c23a2008-09-01 18:34:20 +0000307 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000308 goto scratch_overflow;
309 }else{
310 sqlite3_mutex_enter(mem0.mutex);
311 if( mem0.nScratchFree==0 ){
312 sqlite3_mutex_leave(mem0.mutex);
313 goto scratch_overflow;
314 }else{
315 int i;
316 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000317 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000318 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000319 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000320 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000321 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
drhf7141992008-06-19 00:16:08 +0000322 }
drhe5ae5732008-06-15 02:51:47 +0000323 }
drhf7141992008-06-19 00:16:08 +0000324#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
325 scratchAllocOut = p!=0;
326#endif
327
drhe5ae5732008-06-15 02:51:47 +0000328 return p;
drhf7141992008-06-19 00:16:08 +0000329
330scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000331 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000332 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000333 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000334 n = mallocWithAlarm(n, &p);
335 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
336 sqlite3_mutex_leave(mem0.mutex);
337 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000338 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000339 }
340#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
341 scratchAllocOut = p!=0;
342#endif
343 return p;
drhe5ae5732008-06-15 02:51:47 +0000344}
drhfacf0302008-06-17 15:12:00 +0000345void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000346 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000347
drhe5ae5732008-06-15 02:51:47 +0000348#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000349 /* Verify that no more than one scratch allocation per thread
350 ** is outstanding at one time. (This is only checked in the
351 ** single-threaded case since checking in the multi-threaded case
352 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000353 assert( scratchAllocOut==1 );
354 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000355#endif
drh9ac3fe92008-06-18 18:12:04 +0000356
danielk1977075c23a2008-09-01 18:34:20 +0000357 if( sqlite3GlobalConfig.pScratch==0
358 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000359 || p>=(void*)mem0.aScratchFree ){
danielk1977075c23a2008-09-01 18:34:20 +0000360 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000361 int iSize = sqlite3MallocSize(p);
362 sqlite3_mutex_enter(mem0.mutex);
363 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
364 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000365 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000366 sqlite3_mutex_leave(mem0.mutex);
367 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000368 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000369 }
drh9ac3fe92008-06-18 18:12:04 +0000370 }else{
371 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000372 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
373 i /= sqlite3GlobalConfig.szScratch;
374 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000375 sqlite3_mutex_enter(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000376 assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000377 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000378 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000379 sqlite3_mutex_leave(mem0.mutex);
380 }
drhe5ae5732008-06-15 02:51:47 +0000381 }
382}
383
384/*
drhf7141992008-06-19 00:16:08 +0000385** Allocate memory to be used by the page cache. Make use of the
386** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
387** and that memory is of the right size and is not completely
388** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000389*/
danielk19778c0a7912008-08-20 14:49:23 +0000390#if 0
drhf7141992008-06-19 00:16:08 +0000391void *sqlite3PageMalloc(int n){
392 void *p;
393 assert( n>0 );
394 assert( (n & (n-1))==0 );
395 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000396
danielk1977075c23a2008-09-01 18:34:20 +0000397 if( sqlite3GlobalConfig.szPage<n ){
drhf7141992008-06-19 00:16:08 +0000398 goto page_overflow;
399 }else{
400 sqlite3_mutex_enter(mem0.mutex);
401 if( mem0.nPageFree==0 ){
402 sqlite3_mutex_leave(mem0.mutex);
403 goto page_overflow;
404 }else{
405 int i;
406 i = mem0.aPageFree[--mem0.nPageFree];
407 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000408 i *= sqlite3GlobalConfig.szPage;
drhe50135e2008-08-05 17:53:22 +0000409 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000410 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000411 p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
drhf7141992008-06-19 00:16:08 +0000412 }
413 }
414 return p;
415
416page_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000417 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000418 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000419 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000420 n = mallocWithAlarm(n, &p);
421 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
422 sqlite3_mutex_leave(mem0.mutex);
423 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000424 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000425 }
426 return p;
drhfacf0302008-06-17 15:12:00 +0000427}
drhf7141992008-06-19 00:16:08 +0000428void sqlite3PageFree(void *p){
429 if( p ){
danielk1977075c23a2008-09-01 18:34:20 +0000430 if( sqlite3GlobalConfig.pPage==0
431 || p<sqlite3GlobalConfig.pPage
drhf7141992008-06-19 00:16:08 +0000432 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000433 /* In this case, the page allocation was obtained from a regular
434 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
435 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
436 */
danielk1977075c23a2008-09-01 18:34:20 +0000437 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000438 int iSize = sqlite3MallocSize(p);
439 sqlite3_mutex_enter(mem0.mutex);
440 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
441 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000442 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000443 sqlite3_mutex_leave(mem0.mutex);
444 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000445 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000446 }
447 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000448 /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
danielk19774b9507a2008-06-21 08:12:15 +0000449 ** buffer. In this case all that is add the index of the page in
danielk1977075c23a2008-09-01 18:34:20 +0000450 ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
danielk19774b9507a2008-06-21 08:12:15 +0000451 ** in the mem0.aPageFree[] array.
452 */
drhf7141992008-06-19 00:16:08 +0000453 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000454 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
455 i /= sqlite3GlobalConfig.szPage;
456 assert( i>=0 && i<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000457 sqlite3_mutex_enter(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000458 assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000459 mem0.aPageFree[mem0.nPageFree++] = i;
460 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
461 sqlite3_mutex_leave(mem0.mutex);
drh5f4bcf12008-07-29 14:29:06 +0000462#if !defined(NDEBUG) && 0
danielk19774b9507a2008-06-21 08:12:15 +0000463 /* Assert that a duplicate was not just inserted into aPageFree[]. */
464 for(i=0; i<mem0.nPageFree-1; i++){
465 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
466 }
467#endif
drhf7141992008-06-19 00:16:08 +0000468 }
469 }
drhfacf0302008-06-17 15:12:00 +0000470}
danielk19778c0a7912008-08-20 14:49:23 +0000471#endif
drhfacf0302008-06-17 15:12:00 +0000472
473/*
drh633e6d52008-07-28 19:34:53 +0000474** TRUE if p is a lookaside memory allocation from db
475*/
476static int isLookaside(sqlite3 *db, void *p){
477 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
478}
479
480/*
drhfec00ea2008-06-14 16:56:21 +0000481** Return the size of a memory allocation previously obtained from
482** sqlite3Malloc() or sqlite3_malloc().
483*/
484int sqlite3MallocSize(void *p){
danielk1977075c23a2008-09-01 18:34:20 +0000485 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000486}
drh633e6d52008-07-28 19:34:53 +0000487int sqlite3DbMallocSize(sqlite3 *db, void *p){
488 if( isLookaside(db, p) ){
489 return db->lookaside.sz;
490 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000491 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000492 }
493}
drhfec00ea2008-06-14 16:56:21 +0000494
495/*
496** Free memory previously obtained from sqlite3Malloc().
497*/
498void sqlite3_free(void *p){
499 if( p==0 ) return;
danielk1977075c23a2008-09-01 18:34:20 +0000500 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000501 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000502 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000503 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000504 sqlite3_mutex_leave(mem0.mutex);
505 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000506 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000507 }
508}
509
510/*
drh633e6d52008-07-28 19:34:53 +0000511** Free memory that might be associated with a particular database
512** connection.
513*/
514void sqlite3DbFree(sqlite3 *db, void *p){
515 if( isLookaside(db, p) ){
516 LookasideSlot *pBuf = (LookasideSlot*)p;
517 pBuf->pNext = db->lookaside.pFree;
518 db->lookaside.pFree = pBuf;
519 db->lookaside.nOut--;
520 }else{
521 sqlite3_free(p);
522 }
523}
524
525/*
drhfec00ea2008-06-14 16:56:21 +0000526** Change the size of an existing memory allocation
527*/
528void *sqlite3Realloc(void *pOld, int nBytes){
529 int nOld, nNew;
530 void *pNew;
531 if( pOld==0 ){
532 return sqlite3Malloc(nBytes);
533 }
534 if( nBytes<=0 ){
535 sqlite3_free(pOld);
536 return 0;
537 }
538 nOld = sqlite3MallocSize(pOld);
danielk1977075c23a2008-09-01 18:34:20 +0000539 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000540 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000541 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000542 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000543 if( nOld==nNew ){
544 pNew = pOld;
545 }else{
drhf7141992008-06-19 00:16:08 +0000546 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
547 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000548 sqlite3MallocAlarm(nNew-nOld);
549 }
danielk1977075c23a2008-09-01 18:34:20 +0000550 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
danielk1977d09414c2008-06-19 18:17:49 +0000551 if( pNew==0 && mem0.alarmCallback ){
552 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000553 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000554 }
555 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000556 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000557 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000558 }
559 }
560 sqlite3_mutex_leave(mem0.mutex);
561 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000562 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000563 }
564 return pNew;
565}
566
567/*
568** The public interface to sqlite3Realloc. Make sure that the memory
569** subsystem is initialized prior to invoking sqliteRealloc.
570*/
571void *sqlite3_realloc(void *pOld, int n){
572#ifndef SQLITE_OMIT_AUTOINIT
573 if( sqlite3_initialize() ) return 0;
574#endif
575 return sqlite3Realloc(pOld, n);
576}
577
drha3152892007-05-05 11:48:52 +0000578
579/*
drh17435752007-08-16 04:30:38 +0000580** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000581*/
drhfec00ea2008-06-14 16:56:21 +0000582void *sqlite3MallocZero(int n){
583 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000584 if( p ){
585 memset(p, 0, n);
586 }
587 return p;
588}
drh17435752007-08-16 04:30:38 +0000589
590/*
591** Allocate and zero memory. If the allocation fails, make
592** the mallocFailed flag in the connection pointer.
593*/
drhfec00ea2008-06-14 16:56:21 +0000594void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000595 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000596 if( p ){
597 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000598 }
599 return p;
600}
601
602/*
603** Allocate and zero memory. If the allocation fails, make
604** the mallocFailed flag in the connection pointer.
605*/
drhfec00ea2008-06-14 16:56:21 +0000606void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000607 void *p;
608 if( db ){
609 LookasideSlot *pBuf;
610 if( db->mallocFailed ){
611 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000612 }
drh633e6d52008-07-28 19:34:53 +0000613 if( db->lookaside.bEnabled && n<=db->lookaside.sz
614 && (pBuf = db->lookaside.pFree)!=0 ){
615 db->lookaside.pFree = pBuf->pNext;
616 db->lookaside.nOut++;
617 if( db->lookaside.nOut>db->lookaside.mxOut ){
618 db->lookaside.mxOut = db->lookaside.nOut;
619 }
620 return (void*)pBuf;
621 }
622 }
623 p = sqlite3Malloc(n);
624 if( !p && db ){
625 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000626 }
627 return p;
628}
629
danielk197726783a52007-08-29 14:06:22 +0000630/*
631** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000632** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000633*/
danielk1977a1644fd2007-08-29 12:31:25 +0000634void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
635 void *pNew = 0;
636 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000637 if( p==0 ){
638 return sqlite3DbMallocRaw(db, n);
639 }
640 if( isLookaside(db, p) ){
641 if( n<=db->lookaside.sz ){
642 return p;
643 }
644 pNew = sqlite3DbMallocRaw(db, n);
645 if( pNew ){
646 memcpy(pNew, p, db->lookaside.sz);
647 sqlite3DbFree(db, p);
648 }
649 }else{
650 pNew = sqlite3_realloc(p, n);
651 if( !pNew ){
652 db->mallocFailed = 1;
653 }
danielk1977a1644fd2007-08-29 12:31:25 +0000654 }
655 }
656 return pNew;
657}
658
drh17435752007-08-16 04:30:38 +0000659/*
660** Attempt to reallocate p. If the reallocation fails, then free p
661** and set the mallocFailed flag in the database connection.
662*/
663void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000664 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000665 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000666 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000667 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000668 }
669 return pNew;
670}
671
drha3152892007-05-05 11:48:52 +0000672/*
673** Make a copy of a string in memory obtained from sqliteMalloc(). These
674** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
675** is because when memory debugging is turned on, these two functions are
676** called via macros that record the current file and line number in the
677** ThreadData structure.
678*/
drh633e6d52008-07-28 19:34:53 +0000679char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000680 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000681 size_t n;
682 if( z==0 ){
683 return 0;
684 }
drha3152892007-05-05 11:48:52 +0000685 n = strlen(z)+1;
drh633e6d52008-07-28 19:34:53 +0000686 assert( (n&0x7fffffff)==n );
687 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000688 if( zNew ){
689 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000690 }
691 return zNew;
692}
693char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000694 char *zNew;
695 if( z==0 ){
696 return 0;
697 }
698 assert( (n&0x7fffffff)==n );
699 zNew = sqlite3DbMallocRaw(db, n+1);
700 if( zNew ){
701 memcpy(zNew, z, n);
702 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000703 }
704 return zNew;
705}
706
drha3152892007-05-05 11:48:52 +0000707/*
drhf089aa42008-07-08 19:34:06 +0000708** Create a string from the zFromat argument and the va_list that follows.
709** Store the string in memory obtained from sqliteMalloc() and make *pz
710** point to that string.
drha3152892007-05-05 11:48:52 +0000711*/
drhf089aa42008-07-08 19:34:06 +0000712void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000713 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000714 char *z;
drha3152892007-05-05 11:48:52 +0000715
drhf089aa42008-07-08 19:34:06 +0000716 va_start(ap, zFormat);
717 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000718 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000719 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000720 *pz = z;
drha3152892007-05-05 11:48:52 +0000721}
722
723
724/*
725** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000726** returning control to the user) that has called sqlite3_malloc or
727** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000728**
729** The returned value is normally a copy of the second argument to this
730** function. However, if a malloc() failure has occured since the previous
731** invocation SQLITE_NOMEM is returned instead.
732**
733** If the first argument, db, is not NULL and a malloc() error has occured,
734** then the connection error-code (the value returned by sqlite3_errcode())
735** is set to SQLITE_NOMEM.
736*/
drha3152892007-05-05 11:48:52 +0000737int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000738 /* If the db handle is not NULL, then we must hold the connection handle
739 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
740 ** is unsafe, as is the call to sqlite3Error().
741 */
742 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk19771e536952007-08-16 10:09:01 +0000743 if( db && db->mallocFailed ){
drha3152892007-05-05 11:48:52 +0000744 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000745 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000746 rc = SQLITE_NOMEM;
747 }
748 return rc & (db ? db->errMask : 0xff);
749}