blob: d9c036da6626524fb38dfa70832f929250c7f334 [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**
danielk19770a549072009-02-17 16:29:10 +000015** $Id: malloc.c,v 1.55 2009/02/17 16:29:11 danielk1977 Exp $
drha3152892007-05-05 11:48:52 +000016*/
17#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000018#include <stdarg.h>
drha3152892007-05-05 11:48:52 +000019
20/*
drhb21c8cd2007-08-21 19:33:56 +000021** This routine runs when the memory allocator sees that the
22** total memory allocation is about to exceed the soft heap
23** limit.
24*/
25static void softHeapLimitEnforcer(
26 void *NotUsed,
danielk197762c14b32008-11-19 09:05:26 +000027 sqlite3_int64 NotUsed2,
drh153c62c2007-08-24 03:51:33 +000028 int allocSize
drhb21c8cd2007-08-21 19:33:56 +000029){
danielk197762c14b32008-11-19 09:05:26 +000030 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhb21c8cd2007-08-21 19:33:56 +000031 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 }
drh1bd10f82008-12-10 21:19:56 +000052 overage = (int)(sqlite3_memory_used() - (i64)n);
drhb21c8cd2007-08-21 19:33:56 +000053 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
danielk197762c14b32008-11-19 09:05:26 +000072 UNUSED_PARAMETER(n);
danielk19771e536952007-08-16 10:09:01 +000073 return SQLITE_OK;
74#endif
drha3152892007-05-05 11:48:52 +000075}
drha3152892007-05-05 11:48:52 +000076
drhfec00ea2008-06-14 16:56:21 +000077/*
78** State information local to the memory allocation subsystem.
79*/
danielk19775c8f8582008-09-02 10:22:00 +000080static SQLITE_WSD struct Mem0Global {
danielk197723bf0f42008-09-02 17:52:51 +000081 /* Number of free pages for scratch and page-cache memory */
82 u32 nScratchFree;
83 u32 nPageFree;
84
drhfec00ea2008-06-14 16:56:21 +000085 sqlite3_mutex *mutex; /* Mutex to serialize access */
86
87 /*
88 ** The alarm callback and its arguments. The mem0.mutex lock will
89 ** be held while the callback is running. Recursive calls into
90 ** the memory subsystem are allowed, but no new callbacks will be
91 ** issued. The alarmBusy variable is set to prevent recursive
92 ** callbacks.
93 */
94 sqlite3_int64 alarmThreshold;
95 void (*alarmCallback)(void*, sqlite3_int64,int);
96 void *alarmArg;
97 int alarmBusy;
98
99 /*
danielk1977075c23a2008-09-01 18:34:20 +0000100 ** Pointers to the end of sqlite3GlobalConfig.pScratch and
101 ** sqlite3GlobalConfig.pPage to a block of memory that records
drh9ac3fe92008-06-18 18:12:04 +0000102 ** which pages are available.
103 */
104 u32 *aScratchFree;
105 u32 *aPageFree;
danielk1977cdcfe952008-11-18 07:27:24 +0000106} mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000107
108#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000109
110/*
111** Initialize the memory allocation subsystem.
112*/
113int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000114 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000115 sqlite3MemSetDefault();
116 }
117 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000118 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000119 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000120 }
danielk1977075c23a2008-09-01 18:34:20 +0000121 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
122 && sqlite3GlobalConfig.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000123 int i;
drh66e80082008-12-16 13:46:29 +0000124 sqlite3GlobalConfig.szScratch = (sqlite3GlobalConfig.szScratch - 4) & ~7;
danielk1977075c23a2008-09-01 18:34:20 +0000125 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
126 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
127 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
128 mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
drh9ac3fe92008-06-18 18:12:04 +0000129 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000130 sqlite3GlobalConfig.pScratch = 0;
131 sqlite3GlobalConfig.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000132 }
danielk1977075c23a2008-09-01 18:34:20 +0000133 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
134 && sqlite3GlobalConfig.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000135 int i;
drh0a60a382008-07-31 17:16:05 +0000136 int overhead;
drh66e80082008-12-16 13:46:29 +0000137 int sz = sqlite3GlobalConfig.szPage & ~7;
danielk1977075c23a2008-09-01 18:34:20 +0000138 int n = sqlite3GlobalConfig.nPage;
drh0a60a382008-07-31 17:16:05 +0000139 overhead = (4*n + sz - 1)/sz;
danielk1977075c23a2008-09-01 18:34:20 +0000140 sqlite3GlobalConfig.nPage -= overhead;
141 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
142 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
143 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
144 mem0.nPageFree = sqlite3GlobalConfig.nPage;
drh9ac3fe92008-06-18 18:12:04 +0000145 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000146 sqlite3GlobalConfig.pPage = 0;
147 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000148 }
danielk1977075c23a2008-09-01 18:34:20 +0000149 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000150}
151
152/*
153** Deinitialize the memory allocation subsystem.
154*/
155void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000156 if( sqlite3GlobalConfig.m.xShutdown ){
157 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
158 }
drh9ac3fe92008-06-18 18:12:04 +0000159 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000160}
161
162/*
163** Return the amount of memory currently checked out.
164*/
165sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000166 int n, mx;
drhc376a192008-07-14 12:30:54 +0000167 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000168 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000169 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
170 return res;
drhfec00ea2008-06-14 16:56:21 +0000171}
172
173/*
174** Return the maximum amount of memory that has ever been
175** checked out since either the beginning of this process
176** or since the most recent reset.
177*/
178sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000179 int n, mx;
drhc376a192008-07-14 12:30:54 +0000180 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000181 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000182 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000183 return res;
drhfec00ea2008-06-14 16:56:21 +0000184}
185
186/*
187** Change the alarm callback
188*/
shane4a27a282008-09-04 04:32:49 +0000189int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000190 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
191 void *pArg,
192 sqlite3_int64 iThreshold
193){
194 sqlite3_mutex_enter(mem0.mutex);
195 mem0.alarmCallback = xCallback;
196 mem0.alarmArg = pArg;
197 mem0.alarmThreshold = iThreshold;
198 sqlite3_mutex_leave(mem0.mutex);
199 return SQLITE_OK;
200}
201
shaneeec556d2008-10-12 00:27:53 +0000202#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000203/*
shane4a27a282008-09-04 04:32:49 +0000204** Deprecated external interface. Internal/core SQLite code
205** should call sqlite3MemoryAlarm.
206*/
207int sqlite3_memory_alarm(
208 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
209 void *pArg,
210 sqlite3_int64 iThreshold
211){
212 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
213}
shaneeec556d2008-10-12 00:27:53 +0000214#endif
shane4a27a282008-09-04 04:32:49 +0000215
216/*
drhfec00ea2008-06-14 16:56:21 +0000217** Trigger the alarm
218*/
219static void sqlite3MallocAlarm(int nByte){
220 void (*xCallback)(void*,sqlite3_int64,int);
221 sqlite3_int64 nowUsed;
222 void *pArg;
223 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
224 mem0.alarmBusy = 1;
225 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000226 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000227 pArg = mem0.alarmArg;
228 sqlite3_mutex_leave(mem0.mutex);
229 xCallback(pArg, nowUsed, nByte);
230 sqlite3_mutex_enter(mem0.mutex);
231 mem0.alarmBusy = 0;
232}
233
drhf7141992008-06-19 00:16:08 +0000234/*
235** Do a memory allocation with statistics and alarms. Assume the
236** lock is already held.
237*/
238static int mallocWithAlarm(int n, void **pp){
239 int nFull;
240 void *p;
241 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000242 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000243 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
244 if( mem0.alarmCallback!=0 ){
245 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
246 if( nUsed+nFull >= mem0.alarmThreshold ){
247 sqlite3MallocAlarm(nFull);
248 }
249 }
danielk1977075c23a2008-09-01 18:34:20 +0000250 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000251 if( p==0 && mem0.alarmCallback ){
252 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000253 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000254 }
drhc702c7c2008-07-18 18:56:16 +0000255 if( p ){
256 nFull = sqlite3MallocSize(p);
257 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
258 }
drhf7141992008-06-19 00:16:08 +0000259 *pp = p;
260 return nFull;
261}
drhfec00ea2008-06-14 16:56:21 +0000262
263/*
264** Allocate memory. This routine is like sqlite3_malloc() except that it
265** assumes the memory subsystem has already been initialized.
266*/
267void *sqlite3Malloc(int n){
268 void *p;
drhfec00ea2008-06-14 16:56:21 +0000269 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000270 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000271 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000272 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000273 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000274 sqlite3_mutex_leave(mem0.mutex);
275 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000276 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000277 }
278 return p;
279}
280
281/*
282** This version of the memory allocation is for use by the application.
283** First make sure the memory subsystem is initialized, then do the
284** allocation.
285*/
286void *sqlite3_malloc(int n){
287#ifndef SQLITE_OMIT_AUTOINIT
288 if( sqlite3_initialize() ) return 0;
289#endif
290 return sqlite3Malloc(n);
291}
292
293/*
drhe5ae5732008-06-15 02:51:47 +0000294** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000295** xScratchMalloc(). We verify this constraint in the single-threaded
296** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000297** is outstanding clearing it when the allocation is freed.
298*/
299#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000300static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000301#endif
302
303
304/*
305** Allocate memory that is to be used and released right away.
306** This routine is similar to alloca() in that it is not intended
307** for situations where the memory might be held long-term. This
308** routine is intended to get memory to old large transient data
309** structures that would not normally fit on the stack of an
310** embedded processor.
311*/
drhfacf0302008-06-17 15:12:00 +0000312void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000313 void *p;
314 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000315
drhe5ae5732008-06-15 02:51:47 +0000316#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000317 /* Verify that no more than one scratch allocation per thread
318 ** is outstanding at one time. (This is only checked in the
319 ** single-threaded case since checking in the multi-threaded case
320 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000321 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000322#endif
drh9ac3fe92008-06-18 18:12:04 +0000323
danielk1977075c23a2008-09-01 18:34:20 +0000324 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000325 goto scratch_overflow;
326 }else{
327 sqlite3_mutex_enter(mem0.mutex);
328 if( mem0.nScratchFree==0 ){
329 sqlite3_mutex_leave(mem0.mutex);
330 goto scratch_overflow;
331 }else{
332 int i;
333 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000334 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000335 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000336 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000337 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000338 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
shane15301592008-12-16 17:20:38 +0000339 assert( (((u8*)p - (u8*)0) & 7)==0 );
drhf7141992008-06-19 00:16:08 +0000340 }
drhe5ae5732008-06-15 02:51:47 +0000341 }
drhf7141992008-06-19 00:16:08 +0000342#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
343 scratchAllocOut = p!=0;
344#endif
345
drhe5ae5732008-06-15 02:51:47 +0000346 return p;
drhf7141992008-06-19 00:16:08 +0000347
348scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000349 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000350 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000351 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000352 n = mallocWithAlarm(n, &p);
353 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
354 sqlite3_mutex_leave(mem0.mutex);
355 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000356 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000357 }
358#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
359 scratchAllocOut = p!=0;
360#endif
361 return p;
drhe5ae5732008-06-15 02:51:47 +0000362}
drhfacf0302008-06-17 15:12:00 +0000363void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000364 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000365
drhe5ae5732008-06-15 02:51:47 +0000366#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000367 /* Verify that no more than one scratch allocation per thread
368 ** is outstanding at one time. (This is only checked in the
369 ** single-threaded case since checking in the multi-threaded case
370 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000371 assert( scratchAllocOut==1 );
372 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000373#endif
drh9ac3fe92008-06-18 18:12:04 +0000374
danielk1977075c23a2008-09-01 18:34:20 +0000375 if( sqlite3GlobalConfig.pScratch==0
376 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000377 || p>=(void*)mem0.aScratchFree ){
danielk1977075c23a2008-09-01 18:34:20 +0000378 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000379 int iSize = sqlite3MallocSize(p);
380 sqlite3_mutex_enter(mem0.mutex);
381 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
382 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000383 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000384 sqlite3_mutex_leave(mem0.mutex);
385 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000386 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000387 }
drh9ac3fe92008-06-18 18:12:04 +0000388 }else{
389 int i;
drh1bd10f82008-12-10 21:19:56 +0000390 i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
danielk1977075c23a2008-09-01 18:34:20 +0000391 i /= sqlite3GlobalConfig.szScratch;
392 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000393 sqlite3_mutex_enter(mem0.mutex);
danielk197700e13612008-11-17 19:18:54 +0000394 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000395 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000396 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000397 sqlite3_mutex_leave(mem0.mutex);
398 }
drhe5ae5732008-06-15 02:51:47 +0000399 }
400}
401
402/*
drhf7141992008-06-19 00:16:08 +0000403** Allocate memory to be used by the page cache. Make use of the
404** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
405** and that memory is of the right size and is not completely
406** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000407*/
danielk19778c0a7912008-08-20 14:49:23 +0000408#if 0
drhf7141992008-06-19 00:16:08 +0000409void *sqlite3PageMalloc(int n){
410 void *p;
411 assert( n>0 );
412 assert( (n & (n-1))==0 );
413 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000414
danielk1977075c23a2008-09-01 18:34:20 +0000415 if( sqlite3GlobalConfig.szPage<n ){
drhf7141992008-06-19 00:16:08 +0000416 goto page_overflow;
417 }else{
418 sqlite3_mutex_enter(mem0.mutex);
419 if( mem0.nPageFree==0 ){
420 sqlite3_mutex_leave(mem0.mutex);
421 goto page_overflow;
422 }else{
423 int i;
424 i = mem0.aPageFree[--mem0.nPageFree];
425 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000426 i *= sqlite3GlobalConfig.szPage;
drhe50135e2008-08-05 17:53:22 +0000427 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000428 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000429 p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
drhf7141992008-06-19 00:16:08 +0000430 }
431 }
432 return p;
433
434page_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000435 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000436 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000437 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000438 n = mallocWithAlarm(n, &p);
439 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
440 sqlite3_mutex_leave(mem0.mutex);
441 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000442 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000443 }
444 return p;
drhfacf0302008-06-17 15:12:00 +0000445}
drhf7141992008-06-19 00:16:08 +0000446void sqlite3PageFree(void *p){
447 if( p ){
danielk1977075c23a2008-09-01 18:34:20 +0000448 if( sqlite3GlobalConfig.pPage==0
449 || p<sqlite3GlobalConfig.pPage
drhf7141992008-06-19 00:16:08 +0000450 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000451 /* In this case, the page allocation was obtained from a regular
452 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
453 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
454 */
danielk1977075c23a2008-09-01 18:34:20 +0000455 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000456 int iSize = sqlite3MallocSize(p);
457 sqlite3_mutex_enter(mem0.mutex);
458 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
459 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000460 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000461 sqlite3_mutex_leave(mem0.mutex);
462 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000463 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000464 }
465 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000466 /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
danielk19774b9507a2008-06-21 08:12:15 +0000467 ** buffer. In this case all that is add the index of the page in
danielk1977075c23a2008-09-01 18:34:20 +0000468 ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
danielk19774b9507a2008-06-21 08:12:15 +0000469 ** in the mem0.aPageFree[] array.
470 */
drhf7141992008-06-19 00:16:08 +0000471 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000472 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
473 i /= sqlite3GlobalConfig.szPage;
474 assert( i>=0 && i<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000475 sqlite3_mutex_enter(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000476 assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000477 mem0.aPageFree[mem0.nPageFree++] = i;
478 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
479 sqlite3_mutex_leave(mem0.mutex);
drh5f4bcf12008-07-29 14:29:06 +0000480#if !defined(NDEBUG) && 0
danielk19774b9507a2008-06-21 08:12:15 +0000481 /* Assert that a duplicate was not just inserted into aPageFree[]. */
482 for(i=0; i<mem0.nPageFree-1; i++){
483 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
484 }
485#endif
drhf7141992008-06-19 00:16:08 +0000486 }
487 }
drhfacf0302008-06-17 15:12:00 +0000488}
danielk19778c0a7912008-08-20 14:49:23 +0000489#endif
drhfacf0302008-06-17 15:12:00 +0000490
491/*
drh633e6d52008-07-28 19:34:53 +0000492** TRUE if p is a lookaside memory allocation from db
493*/
drh4150ebf2008-10-11 15:38:29 +0000494#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000495static int isLookaside(sqlite3 *db, void *p){
496 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
497}
drh4150ebf2008-10-11 15:38:29 +0000498#else
499#define isLookaside(A,B) 0
500#endif
drh633e6d52008-07-28 19:34:53 +0000501
502/*
drhfec00ea2008-06-14 16:56:21 +0000503** Return the size of a memory allocation previously obtained from
504** sqlite3Malloc() or sqlite3_malloc().
505*/
506int sqlite3MallocSize(void *p){
danielk1977075c23a2008-09-01 18:34:20 +0000507 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000508}
drh633e6d52008-07-28 19:34:53 +0000509int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh6a1e0712008-12-05 15:24:15 +0000510 if( p==0 ){
511 return 0;
512 }else if( isLookaside(db, p) ){
drh633e6d52008-07-28 19:34:53 +0000513 return db->lookaside.sz;
514 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000515 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000516 }
517}
drhfec00ea2008-06-14 16:56:21 +0000518
519/*
520** Free memory previously obtained from sqlite3Malloc().
521*/
522void sqlite3_free(void *p){
523 if( p==0 ) return;
danielk1977075c23a2008-09-01 18:34:20 +0000524 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000525 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000526 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000527 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000528 sqlite3_mutex_leave(mem0.mutex);
529 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000530 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000531 }
532}
533
534/*
drh633e6d52008-07-28 19:34:53 +0000535** Free memory that might be associated with a particular database
536** connection.
537*/
538void sqlite3DbFree(sqlite3 *db, void *p){
539 if( isLookaside(db, p) ){
540 LookasideSlot *pBuf = (LookasideSlot*)p;
541 pBuf->pNext = db->lookaside.pFree;
542 db->lookaside.pFree = pBuf;
543 db->lookaside.nOut--;
544 }else{
545 sqlite3_free(p);
546 }
547}
548
549/*
drhfec00ea2008-06-14 16:56:21 +0000550** Change the size of an existing memory allocation
551*/
552void *sqlite3Realloc(void *pOld, int nBytes){
553 int nOld, nNew;
554 void *pNew;
555 if( pOld==0 ){
556 return sqlite3Malloc(nBytes);
557 }
558 if( nBytes<=0 ){
559 sqlite3_free(pOld);
560 return 0;
561 }
562 nOld = sqlite3MallocSize(pOld);
danielk1977075c23a2008-09-01 18:34:20 +0000563 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000564 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000565 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000566 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000567 if( nOld==nNew ){
568 pNew = pOld;
569 }else{
drhf7141992008-06-19 00:16:08 +0000570 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
571 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000572 sqlite3MallocAlarm(nNew-nOld);
573 }
danielk1977075c23a2008-09-01 18:34:20 +0000574 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
danielk1977d09414c2008-06-19 18:17:49 +0000575 if( pNew==0 && mem0.alarmCallback ){
576 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000577 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000578 }
579 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000580 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000581 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000582 }
583 }
584 sqlite3_mutex_leave(mem0.mutex);
585 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000586 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000587 }
588 return pNew;
589}
590
591/*
592** The public interface to sqlite3Realloc. Make sure that the memory
593** subsystem is initialized prior to invoking sqliteRealloc.
594*/
595void *sqlite3_realloc(void *pOld, int n){
596#ifndef SQLITE_OMIT_AUTOINIT
597 if( sqlite3_initialize() ) return 0;
598#endif
599 return sqlite3Realloc(pOld, n);
600}
601
drha3152892007-05-05 11:48:52 +0000602
603/*
drh17435752007-08-16 04:30:38 +0000604** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000605*/
drhfec00ea2008-06-14 16:56:21 +0000606void *sqlite3MallocZero(int n){
607 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000608 if( p ){
609 memset(p, 0, n);
610 }
611 return p;
612}
drh17435752007-08-16 04:30:38 +0000613
614/*
615** Allocate and zero memory. If the allocation fails, make
616** the mallocFailed flag in the connection pointer.
617*/
drhfec00ea2008-06-14 16:56:21 +0000618void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000619 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000620 if( p ){
621 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000622 }
623 return p;
624}
625
626/*
627** Allocate and zero memory. If the allocation fails, make
628** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000629**
630** If db!=0 and db->mallocFailed is true (indicating a prior malloc
631** failure on the same database connection) then always return 0.
632** Hence for a particular database connection, once malloc starts
633** failing, it fails consistently until mallocFailed is reset.
634** This is an important assumption. There are many places in the
635** code that do things like this:
636**
637** int *a = (int*)sqlite3DbMallocRaw(db, 100);
638** int *b = (int*)sqlite3DbMallocRaw(db, 200);
639** if( b ) a[10] = 9;
640**
641** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
642** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000643*/
drhfec00ea2008-06-14 16:56:21 +0000644void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000645 void *p;
drh4150ebf2008-10-11 15:38:29 +0000646#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000647 if( db ){
648 LookasideSlot *pBuf;
649 if( db->mallocFailed ){
650 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000651 }
drh633e6d52008-07-28 19:34:53 +0000652 if( db->lookaside.bEnabled && n<=db->lookaside.sz
653 && (pBuf = db->lookaside.pFree)!=0 ){
654 db->lookaside.pFree = pBuf->pNext;
655 db->lookaside.nOut++;
656 if( db->lookaside.nOut>db->lookaside.mxOut ){
657 db->lookaside.mxOut = db->lookaside.nOut;
658 }
659 return (void*)pBuf;
660 }
661 }
drhddecae72008-10-11 17:35:16 +0000662#else
663 if( db && db->mallocFailed ){
664 return 0;
665 }
drh4150ebf2008-10-11 15:38:29 +0000666#endif
drh633e6d52008-07-28 19:34:53 +0000667 p = sqlite3Malloc(n);
668 if( !p && db ){
669 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000670 }
671 return p;
672}
673
danielk197726783a52007-08-29 14:06:22 +0000674/*
675** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000676** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000677*/
danielk1977a1644fd2007-08-29 12:31:25 +0000678void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
679 void *pNew = 0;
680 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000681 if( p==0 ){
682 return sqlite3DbMallocRaw(db, n);
683 }
684 if( isLookaside(db, p) ){
685 if( n<=db->lookaside.sz ){
686 return p;
687 }
688 pNew = sqlite3DbMallocRaw(db, n);
689 if( pNew ){
690 memcpy(pNew, p, db->lookaside.sz);
691 sqlite3DbFree(db, p);
692 }
693 }else{
694 pNew = sqlite3_realloc(p, n);
695 if( !pNew ){
696 db->mallocFailed = 1;
697 }
danielk1977a1644fd2007-08-29 12:31:25 +0000698 }
699 }
700 return pNew;
701}
702
drh17435752007-08-16 04:30:38 +0000703/*
704** Attempt to reallocate p. If the reallocation fails, then free p
705** and set the mallocFailed flag in the database connection.
706*/
707void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000708 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000709 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000710 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000711 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000712 }
713 return pNew;
714}
715
drha3152892007-05-05 11:48:52 +0000716/*
717** Make a copy of a string in memory obtained from sqliteMalloc(). These
718** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
719** is because when memory debugging is turned on, these two functions are
720** called via macros that record the current file and line number in the
721** ThreadData structure.
722*/
drh633e6d52008-07-28 19:34:53 +0000723char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000724 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000725 size_t n;
726 if( z==0 ){
727 return 0;
728 }
drhea678832008-12-10 19:26:22 +0000729 n = (db ? sqlite3Strlen(db, z) : sqlite3Strlen30(z))+1;
drh633e6d52008-07-28 19:34:53 +0000730 assert( (n&0x7fffffff)==n );
731 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000732 if( zNew ){
733 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000734 }
735 return zNew;
736}
737char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000738 char *zNew;
739 if( z==0 ){
740 return 0;
741 }
742 assert( (n&0x7fffffff)==n );
743 zNew = sqlite3DbMallocRaw(db, n+1);
744 if( zNew ){
745 memcpy(zNew, z, n);
746 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000747 }
748 return zNew;
749}
750
drha3152892007-05-05 11:48:52 +0000751/*
drhf089aa42008-07-08 19:34:06 +0000752** Create a string from the zFromat argument and the va_list that follows.
753** Store the string in memory obtained from sqliteMalloc() and make *pz
754** point to that string.
drha3152892007-05-05 11:48:52 +0000755*/
drhf089aa42008-07-08 19:34:06 +0000756void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000757 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000758 char *z;
drha3152892007-05-05 11:48:52 +0000759
drhf089aa42008-07-08 19:34:06 +0000760 va_start(ap, zFormat);
761 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000762 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000763 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000764 *pz = z;
drha3152892007-05-05 11:48:52 +0000765}
766
767
768/*
769** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000770** returning control to the user) that has called sqlite3_malloc or
771** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000772**
773** The returned value is normally a copy of the second argument to this
774** function. However, if a malloc() failure has occured since the previous
775** invocation SQLITE_NOMEM is returned instead.
776**
777** If the first argument, db, is not NULL and a malloc() error has occured,
778** then the connection error-code (the value returned by sqlite3_errcode())
779** is set to SQLITE_NOMEM.
780*/
drha3152892007-05-05 11:48:52 +0000781int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000782 /* If the db handle is not NULL, then we must hold the connection handle
783 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
784 ** is unsafe, as is the call to sqlite3Error().
785 */
786 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000787 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000788 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000789 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000790 rc = SQLITE_NOMEM;
791 }
792 return rc & (db ? db->errMask : 0xff);
793}