blob: 8e9bbd38129d3b3deccfd61d4933287a399be5d6 [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**
danielk1977cdcfe952008-11-18 07:27:24 +000015** $Id: malloc.c,v 1.47 2008/11/18 07:27:24 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 ){
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;
danielk1977cdcfe952008-11-18 07:27:24 +0000105} mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 };
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
shaneeec556d2008-10-12 00:27:53 +0000199#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000200/*
shane4a27a282008-09-04 04:32:49 +0000201** Deprecated external interface. Internal/core SQLite code
202** should call sqlite3MemoryAlarm.
203*/
204int sqlite3_memory_alarm(
205 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
206 void *pArg,
207 sqlite3_int64 iThreshold
208){
209 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
210}
shaneeec556d2008-10-12 00:27:53 +0000211#endif
shane4a27a282008-09-04 04:32:49 +0000212
213/*
drhfec00ea2008-06-14 16:56:21 +0000214** Trigger the alarm
215*/
216static void sqlite3MallocAlarm(int nByte){
217 void (*xCallback)(void*,sqlite3_int64,int);
218 sqlite3_int64 nowUsed;
219 void *pArg;
220 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
221 mem0.alarmBusy = 1;
222 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000223 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000224 pArg = mem0.alarmArg;
225 sqlite3_mutex_leave(mem0.mutex);
226 xCallback(pArg, nowUsed, nByte);
227 sqlite3_mutex_enter(mem0.mutex);
228 mem0.alarmBusy = 0;
229}
230
drhf7141992008-06-19 00:16:08 +0000231/*
232** Do a memory allocation with statistics and alarms. Assume the
233** lock is already held.
234*/
235static int mallocWithAlarm(int n, void **pp){
236 int nFull;
237 void *p;
238 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000239 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000240 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
241 if( mem0.alarmCallback!=0 ){
242 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
243 if( nUsed+nFull >= mem0.alarmThreshold ){
244 sqlite3MallocAlarm(nFull);
245 }
246 }
danielk1977075c23a2008-09-01 18:34:20 +0000247 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000248 if( p==0 && mem0.alarmCallback ){
249 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000250 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000251 }
drhc702c7c2008-07-18 18:56:16 +0000252 if( p ){
253 nFull = sqlite3MallocSize(p);
254 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
255 }
drhf7141992008-06-19 00:16:08 +0000256 *pp = p;
257 return nFull;
258}
drhfec00ea2008-06-14 16:56:21 +0000259
260/*
261** Allocate memory. This routine is like sqlite3_malloc() except that it
262** assumes the memory subsystem has already been initialized.
263*/
264void *sqlite3Malloc(int n){
265 void *p;
drhfec00ea2008-06-14 16:56:21 +0000266 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000267 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000268 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000269 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000270 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000271 sqlite3_mutex_leave(mem0.mutex);
272 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000273 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000274 }
275 return p;
276}
277
278/*
279** This version of the memory allocation is for use by the application.
280** First make sure the memory subsystem is initialized, then do the
281** allocation.
282*/
283void *sqlite3_malloc(int n){
284#ifndef SQLITE_OMIT_AUTOINIT
285 if( sqlite3_initialize() ) return 0;
286#endif
287 return sqlite3Malloc(n);
288}
289
290/*
drhe5ae5732008-06-15 02:51:47 +0000291** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000292** xScratchMalloc(). We verify this constraint in the single-threaded
293** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000294** is outstanding clearing it when the allocation is freed.
295*/
296#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000297static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000298#endif
299
300
301/*
302** Allocate memory that is to be used and released right away.
303** This routine is similar to alloca() in that it is not intended
304** for situations where the memory might be held long-term. This
305** routine is intended to get memory to old large transient data
306** structures that would not normally fit on the stack of an
307** embedded processor.
308*/
drhfacf0302008-06-17 15:12:00 +0000309void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000310 void *p;
311 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000312
drhe5ae5732008-06-15 02:51:47 +0000313#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000314 /* Verify that no more than one scratch allocation per thread
315 ** is outstanding at one time. (This is only checked in the
316 ** single-threaded case since checking in the multi-threaded case
317 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000318 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000319#endif
drh9ac3fe92008-06-18 18:12:04 +0000320
danielk1977075c23a2008-09-01 18:34:20 +0000321 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000322 goto scratch_overflow;
323 }else{
324 sqlite3_mutex_enter(mem0.mutex);
325 if( mem0.nScratchFree==0 ){
326 sqlite3_mutex_leave(mem0.mutex);
327 goto scratch_overflow;
328 }else{
329 int i;
330 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000331 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000332 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000333 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000334 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000335 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
drhf7141992008-06-19 00:16:08 +0000336 }
drhe5ae5732008-06-15 02:51:47 +0000337 }
drhf7141992008-06-19 00:16:08 +0000338#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
339 scratchAllocOut = p!=0;
340#endif
341
drhe5ae5732008-06-15 02:51:47 +0000342 return p;
drhf7141992008-06-19 00:16:08 +0000343
344scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000345 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000346 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000347 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000348 n = mallocWithAlarm(n, &p);
349 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
350 sqlite3_mutex_leave(mem0.mutex);
351 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000352 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000353 }
354#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
355 scratchAllocOut = p!=0;
356#endif
357 return p;
drhe5ae5732008-06-15 02:51:47 +0000358}
drhfacf0302008-06-17 15:12:00 +0000359void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000360 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000361
drhe5ae5732008-06-15 02:51:47 +0000362#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000363 /* Verify that no more than one scratch allocation per thread
364 ** is outstanding at one time. (This is only checked in the
365 ** single-threaded case since checking in the multi-threaded case
366 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000367 assert( scratchAllocOut==1 );
368 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000369#endif
drh9ac3fe92008-06-18 18:12:04 +0000370
danielk1977075c23a2008-09-01 18:34:20 +0000371 if( sqlite3GlobalConfig.pScratch==0
372 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000373 || p>=(void*)mem0.aScratchFree ){
danielk1977075c23a2008-09-01 18:34:20 +0000374 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000375 int iSize = sqlite3MallocSize(p);
376 sqlite3_mutex_enter(mem0.mutex);
377 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
378 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000379 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000380 sqlite3_mutex_leave(mem0.mutex);
381 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000382 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000383 }
drh9ac3fe92008-06-18 18:12:04 +0000384 }else{
385 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000386 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
387 i /= sqlite3GlobalConfig.szScratch;
388 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000389 sqlite3_mutex_enter(mem0.mutex);
danielk197700e13612008-11-17 19:18:54 +0000390 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000391 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000392 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000393 sqlite3_mutex_leave(mem0.mutex);
394 }
drhe5ae5732008-06-15 02:51:47 +0000395 }
396}
397
398/*
drhf7141992008-06-19 00:16:08 +0000399** Allocate memory to be used by the page cache. Make use of the
400** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
401** and that memory is of the right size and is not completely
402** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000403*/
danielk19778c0a7912008-08-20 14:49:23 +0000404#if 0
drhf7141992008-06-19 00:16:08 +0000405void *sqlite3PageMalloc(int n){
406 void *p;
407 assert( n>0 );
408 assert( (n & (n-1))==0 );
409 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000410
danielk1977075c23a2008-09-01 18:34:20 +0000411 if( sqlite3GlobalConfig.szPage<n ){
drhf7141992008-06-19 00:16:08 +0000412 goto page_overflow;
413 }else{
414 sqlite3_mutex_enter(mem0.mutex);
415 if( mem0.nPageFree==0 ){
416 sqlite3_mutex_leave(mem0.mutex);
417 goto page_overflow;
418 }else{
419 int i;
420 i = mem0.aPageFree[--mem0.nPageFree];
421 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000422 i *= sqlite3GlobalConfig.szPage;
drhe50135e2008-08-05 17:53:22 +0000423 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000424 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000425 p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
drhf7141992008-06-19 00:16:08 +0000426 }
427 }
428 return p;
429
430page_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000431 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000432 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000433 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000434 n = mallocWithAlarm(n, &p);
435 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
436 sqlite3_mutex_leave(mem0.mutex);
437 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000438 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000439 }
440 return p;
drhfacf0302008-06-17 15:12:00 +0000441}
drhf7141992008-06-19 00:16:08 +0000442void sqlite3PageFree(void *p){
443 if( p ){
danielk1977075c23a2008-09-01 18:34:20 +0000444 if( sqlite3GlobalConfig.pPage==0
445 || p<sqlite3GlobalConfig.pPage
drhf7141992008-06-19 00:16:08 +0000446 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000447 /* In this case, the page allocation was obtained from a regular
448 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
449 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
450 */
danielk1977075c23a2008-09-01 18:34:20 +0000451 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000452 int iSize = sqlite3MallocSize(p);
453 sqlite3_mutex_enter(mem0.mutex);
454 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
455 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000456 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000457 sqlite3_mutex_leave(mem0.mutex);
458 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000459 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000460 }
461 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000462 /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
danielk19774b9507a2008-06-21 08:12:15 +0000463 ** buffer. In this case all that is add the index of the page in
danielk1977075c23a2008-09-01 18:34:20 +0000464 ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
danielk19774b9507a2008-06-21 08:12:15 +0000465 ** in the mem0.aPageFree[] array.
466 */
drhf7141992008-06-19 00:16:08 +0000467 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000468 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
469 i /= sqlite3GlobalConfig.szPage;
470 assert( i>=0 && i<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000471 sqlite3_mutex_enter(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000472 assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000473 mem0.aPageFree[mem0.nPageFree++] = i;
474 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
475 sqlite3_mutex_leave(mem0.mutex);
drh5f4bcf12008-07-29 14:29:06 +0000476#if !defined(NDEBUG) && 0
danielk19774b9507a2008-06-21 08:12:15 +0000477 /* Assert that a duplicate was not just inserted into aPageFree[]. */
478 for(i=0; i<mem0.nPageFree-1; i++){
479 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
480 }
481#endif
drhf7141992008-06-19 00:16:08 +0000482 }
483 }
drhfacf0302008-06-17 15:12:00 +0000484}
danielk19778c0a7912008-08-20 14:49:23 +0000485#endif
drhfacf0302008-06-17 15:12:00 +0000486
487/*
drh633e6d52008-07-28 19:34:53 +0000488** TRUE if p is a lookaside memory allocation from db
489*/
drh4150ebf2008-10-11 15:38:29 +0000490#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000491static int isLookaside(sqlite3 *db, void *p){
492 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
493}
drh4150ebf2008-10-11 15:38:29 +0000494#else
495#define isLookaside(A,B) 0
496#endif
drh633e6d52008-07-28 19:34:53 +0000497
498/*
drhfec00ea2008-06-14 16:56:21 +0000499** Return the size of a memory allocation previously obtained from
500** sqlite3Malloc() or sqlite3_malloc().
501*/
502int sqlite3MallocSize(void *p){
danielk1977075c23a2008-09-01 18:34:20 +0000503 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000504}
drh633e6d52008-07-28 19:34:53 +0000505int sqlite3DbMallocSize(sqlite3 *db, void *p){
506 if( isLookaside(db, p) ){
507 return db->lookaside.sz;
508 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000509 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000510 }
511}
drhfec00ea2008-06-14 16:56:21 +0000512
513/*
514** Free memory previously obtained from sqlite3Malloc().
515*/
516void sqlite3_free(void *p){
517 if( p==0 ) return;
danielk1977075c23a2008-09-01 18:34:20 +0000518 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000519 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000520 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000521 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000522 sqlite3_mutex_leave(mem0.mutex);
523 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000524 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000525 }
526}
527
528/*
drh633e6d52008-07-28 19:34:53 +0000529** Free memory that might be associated with a particular database
530** connection.
531*/
532void sqlite3DbFree(sqlite3 *db, void *p){
533 if( isLookaside(db, p) ){
534 LookasideSlot *pBuf = (LookasideSlot*)p;
535 pBuf->pNext = db->lookaside.pFree;
536 db->lookaside.pFree = pBuf;
537 db->lookaside.nOut--;
538 }else{
539 sqlite3_free(p);
540 }
541}
542
543/*
drhfec00ea2008-06-14 16:56:21 +0000544** Change the size of an existing memory allocation
545*/
546void *sqlite3Realloc(void *pOld, int nBytes){
547 int nOld, nNew;
548 void *pNew;
549 if( pOld==0 ){
550 return sqlite3Malloc(nBytes);
551 }
552 if( nBytes<=0 ){
553 sqlite3_free(pOld);
554 return 0;
555 }
556 nOld = sqlite3MallocSize(pOld);
danielk1977075c23a2008-09-01 18:34:20 +0000557 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000558 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000559 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000560 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000561 if( nOld==nNew ){
562 pNew = pOld;
563 }else{
drhf7141992008-06-19 00:16:08 +0000564 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
565 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000566 sqlite3MallocAlarm(nNew-nOld);
567 }
danielk1977075c23a2008-09-01 18:34:20 +0000568 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
danielk1977d09414c2008-06-19 18:17:49 +0000569 if( pNew==0 && mem0.alarmCallback ){
570 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000571 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000572 }
573 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000574 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000575 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000576 }
577 }
578 sqlite3_mutex_leave(mem0.mutex);
579 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000580 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000581 }
582 return pNew;
583}
584
585/*
586** The public interface to sqlite3Realloc. Make sure that the memory
587** subsystem is initialized prior to invoking sqliteRealloc.
588*/
589void *sqlite3_realloc(void *pOld, int n){
590#ifndef SQLITE_OMIT_AUTOINIT
591 if( sqlite3_initialize() ) return 0;
592#endif
593 return sqlite3Realloc(pOld, n);
594}
595
drha3152892007-05-05 11:48:52 +0000596
597/*
drh17435752007-08-16 04:30:38 +0000598** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000599*/
drhfec00ea2008-06-14 16:56:21 +0000600void *sqlite3MallocZero(int n){
601 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000602 if( p ){
603 memset(p, 0, n);
604 }
605 return p;
606}
drh17435752007-08-16 04:30:38 +0000607
608/*
609** Allocate and zero memory. If the allocation fails, make
610** the mallocFailed flag in the connection pointer.
611*/
drhfec00ea2008-06-14 16:56:21 +0000612void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000613 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000614 if( p ){
615 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000616 }
617 return p;
618}
619
620/*
621** Allocate and zero memory. If the allocation fails, make
622** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000623**
624** If db!=0 and db->mallocFailed is true (indicating a prior malloc
625** failure on the same database connection) then always return 0.
626** Hence for a particular database connection, once malloc starts
627** failing, it fails consistently until mallocFailed is reset.
628** This is an important assumption. There are many places in the
629** code that do things like this:
630**
631** int *a = (int*)sqlite3DbMallocRaw(db, 100);
632** int *b = (int*)sqlite3DbMallocRaw(db, 200);
633** if( b ) a[10] = 9;
634**
635** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
636** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000637*/
drhfec00ea2008-06-14 16:56:21 +0000638void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000639 void *p;
drh4150ebf2008-10-11 15:38:29 +0000640#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000641 if( db ){
642 LookasideSlot *pBuf;
643 if( db->mallocFailed ){
644 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000645 }
drh633e6d52008-07-28 19:34:53 +0000646 if( db->lookaside.bEnabled && n<=db->lookaside.sz
647 && (pBuf = db->lookaside.pFree)!=0 ){
648 db->lookaside.pFree = pBuf->pNext;
649 db->lookaside.nOut++;
650 if( db->lookaside.nOut>db->lookaside.mxOut ){
651 db->lookaside.mxOut = db->lookaside.nOut;
652 }
653 return (void*)pBuf;
654 }
655 }
drhddecae72008-10-11 17:35:16 +0000656#else
657 if( db && db->mallocFailed ){
658 return 0;
659 }
drh4150ebf2008-10-11 15:38:29 +0000660#endif
drh633e6d52008-07-28 19:34:53 +0000661 p = sqlite3Malloc(n);
662 if( !p && db ){
663 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000664 }
665 return p;
666}
667
danielk197726783a52007-08-29 14:06:22 +0000668/*
669** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000670** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000671*/
danielk1977a1644fd2007-08-29 12:31:25 +0000672void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
673 void *pNew = 0;
674 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000675 if( p==0 ){
676 return sqlite3DbMallocRaw(db, n);
677 }
678 if( isLookaside(db, p) ){
679 if( n<=db->lookaside.sz ){
680 return p;
681 }
682 pNew = sqlite3DbMallocRaw(db, n);
683 if( pNew ){
684 memcpy(pNew, p, db->lookaside.sz);
685 sqlite3DbFree(db, p);
686 }
687 }else{
688 pNew = sqlite3_realloc(p, n);
689 if( !pNew ){
690 db->mallocFailed = 1;
691 }
danielk1977a1644fd2007-08-29 12:31:25 +0000692 }
693 }
694 return pNew;
695}
696
drh17435752007-08-16 04:30:38 +0000697/*
698** Attempt to reallocate p. If the reallocation fails, then free p
699** and set the mallocFailed flag in the database connection.
700*/
701void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000702 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000703 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000704 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000705 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000706 }
707 return pNew;
708}
709
drha3152892007-05-05 11:48:52 +0000710/*
711** Make a copy of a string in memory obtained from sqliteMalloc(). These
712** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
713** is because when memory debugging is turned on, these two functions are
714** called via macros that record the current file and line number in the
715** ThreadData structure.
716*/
drh633e6d52008-07-28 19:34:53 +0000717char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000718 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000719 size_t n;
720 if( z==0 ){
721 return 0;
722 }
drha3152892007-05-05 11:48:52 +0000723 n = strlen(z)+1;
drh633e6d52008-07-28 19:34:53 +0000724 assert( (n&0x7fffffff)==n );
725 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000726 if( zNew ){
727 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000728 }
729 return zNew;
730}
731char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000732 char *zNew;
733 if( z==0 ){
734 return 0;
735 }
736 assert( (n&0x7fffffff)==n );
737 zNew = sqlite3DbMallocRaw(db, n+1);
738 if( zNew ){
739 memcpy(zNew, z, n);
740 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000741 }
742 return zNew;
743}
744
drha3152892007-05-05 11:48:52 +0000745/*
drhf089aa42008-07-08 19:34:06 +0000746** Create a string from the zFromat argument and the va_list that follows.
747** Store the string in memory obtained from sqliteMalloc() and make *pz
748** point to that string.
drha3152892007-05-05 11:48:52 +0000749*/
drhf089aa42008-07-08 19:34:06 +0000750void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000751 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000752 char *z;
drha3152892007-05-05 11:48:52 +0000753
drhf089aa42008-07-08 19:34:06 +0000754 va_start(ap, zFormat);
755 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000756 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000757 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000758 *pz = z;
drha3152892007-05-05 11:48:52 +0000759}
760
761
762/*
763** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000764** returning control to the user) that has called sqlite3_malloc or
765** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000766**
767** The returned value is normally a copy of the second argument to this
768** function. However, if a malloc() failure has occured since the previous
769** invocation SQLITE_NOMEM is returned instead.
770**
771** If the first argument, db, is not NULL and a malloc() error has occured,
772** then the connection error-code (the value returned by sqlite3_errcode())
773** is set to SQLITE_NOMEM.
774*/
drha3152892007-05-05 11:48:52 +0000775int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000776 /* If the db handle is not NULL, then we must hold the connection handle
777 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
778 ** is unsafe, as is the call to sqlite3Error().
779 */
780 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000781 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000782 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000783 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000784 rc = SQLITE_NOMEM;
785 }
786 return rc & (db ? db->errMask : 0xff);
787}