blob: a4520933e37ef436a0d4dadf3d87a7c3857d3ce5 [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**
danielk197762c14b32008-11-19 09:05:26 +000015** $Id: malloc.c,v 1.48 2008/11/19 09:05:27 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,
danielk197762c14b32008-11-19 09:05:26 +000028 sqlite3_int64 NotUsed2,
drh153c62c2007-08-24 03:51:33 +000029 int allocSize
drhb21c8cd2007-08-21 19:33:56 +000030){
danielk197762c14b32008-11-19 09:05:26 +000031 UNUSED_PARAMETER2(NotUsed, NotUsed2);
drhb21c8cd2007-08-21 19:33:56 +000032 sqlite3_release_memory(allocSize);
33}
34
35/*
danielk197784680242008-06-23 11:11:35 +000036** Set the soft heap-size limit for the library. Passing a zero or
37** negative value indicates no limit.
drha3152892007-05-05 11:48:52 +000038*/
39void sqlite3_soft_heap_limit(int n){
drhb21c8cd2007-08-21 19:33:56 +000040 sqlite3_uint64 iLimit;
41 int overage;
42 if( n<0 ){
43 iLimit = 0;
44 }else{
45 iLimit = n;
drha3152892007-05-05 11:48:52 +000046 }
drh9ac3fe92008-06-18 18:12:04 +000047 sqlite3_initialize();
drhb21c8cd2007-08-21 19:33:56 +000048 if( iLimit>0 ){
shane4a27a282008-09-04 04:32:49 +000049 sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
drhb21c8cd2007-08-21 19:33:56 +000050 }else{
shane4a27a282008-09-04 04:32:49 +000051 sqlite3MemoryAlarm(0, 0, 0);
drhb21c8cd2007-08-21 19:33:56 +000052 }
53 overage = sqlite3_memory_used() - n;
54 if( overage>0 ){
55 sqlite3_release_memory(overage);
56 }
drha3152892007-05-05 11:48:52 +000057}
58
59/*
danielk197784680242008-06-23 11:11:35 +000060** Attempt to release up to n bytes of non-essential memory currently
61** held by SQLite. An example of non-essential memory is memory used to
62** cache database pages that are not currently in use.
drha3152892007-05-05 11:48:52 +000063*/
64int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +000065#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197767e3da72008-08-21 12:19:44 +000066 int nRet = 0;
67#if 0
68 nRet += sqlite3VdbeReleaseMemory(n);
69#endif
70 nRet += sqlite3PcacheReleaseMemory(n-nRet);
danielk1977dfb316d2008-03-26 18:34:43 +000071 return nRet;
danielk19771e536952007-08-16 10:09:01 +000072#else
danielk197762c14b32008-11-19 09:05:26 +000073 UNUSED_PARAMETER(n);
danielk19771e536952007-08-16 10:09:01 +000074 return SQLITE_OK;
75#endif
drha3152892007-05-05 11:48:52 +000076}
drha3152892007-05-05 11:48:52 +000077
drhfec00ea2008-06-14 16:56:21 +000078/*
79** State information local to the memory allocation subsystem.
80*/
danielk19775c8f8582008-09-02 10:22:00 +000081static SQLITE_WSD struct Mem0Global {
danielk197723bf0f42008-09-02 17:52:51 +000082 /* Number of free pages for scratch and page-cache memory */
83 u32 nScratchFree;
84 u32 nPageFree;
85
drhfec00ea2008-06-14 16:56:21 +000086 sqlite3_mutex *mutex; /* Mutex to serialize access */
87
88 /*
89 ** The alarm callback and its arguments. The mem0.mutex lock will
90 ** be held while the callback is running. Recursive calls into
91 ** the memory subsystem are allowed, but no new callbacks will be
92 ** issued. The alarmBusy variable is set to prevent recursive
93 ** callbacks.
94 */
95 sqlite3_int64 alarmThreshold;
96 void (*alarmCallback)(void*, sqlite3_int64,int);
97 void *alarmArg;
98 int alarmBusy;
99
100 /*
danielk1977075c23a2008-09-01 18:34:20 +0000101 ** Pointers to the end of sqlite3GlobalConfig.pScratch and
102 ** sqlite3GlobalConfig.pPage to a block of memory that records
drh9ac3fe92008-06-18 18:12:04 +0000103 ** which pages are available.
104 */
105 u32 *aScratchFree;
106 u32 *aPageFree;
danielk1977cdcfe952008-11-18 07:27:24 +0000107} mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +0000108
109#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +0000110
111/*
112** Initialize the memory allocation subsystem.
113*/
114int sqlite3MallocInit(void){
danielk1977075c23a2008-09-01 18:34:20 +0000115 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000116 sqlite3MemSetDefault();
117 }
118 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000119 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000120 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000121 }
danielk1977075c23a2008-09-01 18:34:20 +0000122 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
123 && sqlite3GlobalConfig.nScratch>=0 ){
drh9ac3fe92008-06-18 18:12:04 +0000124 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000125 sqlite3GlobalConfig.szScratch -= 4;
126 mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
127 [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
128 for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
129 mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
drh9ac3fe92008-06-18 18:12:04 +0000130 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000131 sqlite3GlobalConfig.pScratch = 0;
132 sqlite3GlobalConfig.szScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000133 }
danielk1977075c23a2008-09-01 18:34:20 +0000134 if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
135 && sqlite3GlobalConfig.nPage>=1 ){
drh9ac3fe92008-06-18 18:12:04 +0000136 int i;
drh0a60a382008-07-31 17:16:05 +0000137 int overhead;
danielk1977075c23a2008-09-01 18:34:20 +0000138 int sz = sqlite3GlobalConfig.szPage;
139 int n = sqlite3GlobalConfig.nPage;
drh0a60a382008-07-31 17:16:05 +0000140 overhead = (4*n + sz - 1)/sz;
danielk1977075c23a2008-09-01 18:34:20 +0000141 sqlite3GlobalConfig.nPage -= overhead;
142 mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
143 [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
144 for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
145 mem0.nPageFree = sqlite3GlobalConfig.nPage;
drh9ac3fe92008-06-18 18:12:04 +0000146 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000147 sqlite3GlobalConfig.pPage = 0;
148 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000149 }
danielk1977075c23a2008-09-01 18:34:20 +0000150 return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
drhfec00ea2008-06-14 16:56:21 +0000151}
152
153/*
154** Deinitialize the memory allocation subsystem.
155*/
156void sqlite3MallocEnd(void){
danielk1977075c23a2008-09-01 18:34:20 +0000157 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
drh9ac3fe92008-06-18 18:12:04 +0000158 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000159}
160
161/*
162** Return the amount of memory currently checked out.
163*/
164sqlite3_int64 sqlite3_memory_used(void){
drhf7141992008-06-19 00:16:08 +0000165 int n, mx;
drhc376a192008-07-14 12:30:54 +0000166 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000167 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000168 res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
169 return res;
drhfec00ea2008-06-14 16:56:21 +0000170}
171
172/*
173** Return the maximum amount of memory that has ever been
174** checked out since either the beginning of this process
175** or since the most recent reset.
176*/
177sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhf7141992008-06-19 00:16:08 +0000178 int n, mx;
drhc376a192008-07-14 12:30:54 +0000179 sqlite3_int64 res;
drhf7141992008-06-19 00:16:08 +0000180 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
drh7986a712008-07-14 12:38:20 +0000181 res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
drhc376a192008-07-14 12:30:54 +0000182 return res;
drhfec00ea2008-06-14 16:56:21 +0000183}
184
185/*
186** Change the alarm callback
187*/
shane4a27a282008-09-04 04:32:49 +0000188int sqlite3MemoryAlarm(
drhfec00ea2008-06-14 16:56:21 +0000189 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
190 void *pArg,
191 sqlite3_int64 iThreshold
192){
193 sqlite3_mutex_enter(mem0.mutex);
194 mem0.alarmCallback = xCallback;
195 mem0.alarmArg = pArg;
196 mem0.alarmThreshold = iThreshold;
197 sqlite3_mutex_leave(mem0.mutex);
198 return SQLITE_OK;
199}
200
shaneeec556d2008-10-12 00:27:53 +0000201#ifndef SQLITE_OMIT_DEPRECATED
drhfec00ea2008-06-14 16:56:21 +0000202/*
shane4a27a282008-09-04 04:32:49 +0000203** Deprecated external interface. Internal/core SQLite code
204** should call sqlite3MemoryAlarm.
205*/
206int sqlite3_memory_alarm(
207 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
208 void *pArg,
209 sqlite3_int64 iThreshold
210){
211 return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
212}
shaneeec556d2008-10-12 00:27:53 +0000213#endif
shane4a27a282008-09-04 04:32:49 +0000214
215/*
drhfec00ea2008-06-14 16:56:21 +0000216** Trigger the alarm
217*/
218static void sqlite3MallocAlarm(int nByte){
219 void (*xCallback)(void*,sqlite3_int64,int);
220 sqlite3_int64 nowUsed;
221 void *pArg;
222 if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
223 mem0.alarmBusy = 1;
224 xCallback = mem0.alarmCallback;
drhf7141992008-06-19 00:16:08 +0000225 nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhfec00ea2008-06-14 16:56:21 +0000226 pArg = mem0.alarmArg;
227 sqlite3_mutex_leave(mem0.mutex);
228 xCallback(pArg, nowUsed, nByte);
229 sqlite3_mutex_enter(mem0.mutex);
230 mem0.alarmBusy = 0;
231}
232
drhf7141992008-06-19 00:16:08 +0000233/*
234** Do a memory allocation with statistics and alarms. Assume the
235** lock is already held.
236*/
237static int mallocWithAlarm(int n, void **pp){
238 int nFull;
239 void *p;
240 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000241 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000242 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
243 if( mem0.alarmCallback!=0 ){
244 int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
245 if( nUsed+nFull >= mem0.alarmThreshold ){
246 sqlite3MallocAlarm(nFull);
247 }
248 }
danielk1977075c23a2008-09-01 18:34:20 +0000249 p = sqlite3GlobalConfig.m.xMalloc(nFull);
danielk1977d09414c2008-06-19 18:17:49 +0000250 if( p==0 && mem0.alarmCallback ){
251 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000252 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000253 }
drhc702c7c2008-07-18 18:56:16 +0000254 if( p ){
255 nFull = sqlite3MallocSize(p);
256 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
257 }
drhf7141992008-06-19 00:16:08 +0000258 *pp = p;
259 return nFull;
260}
drhfec00ea2008-06-14 16:56:21 +0000261
262/*
263** Allocate memory. This routine is like sqlite3_malloc() except that it
264** assumes the memory subsystem has already been initialized.
265*/
266void *sqlite3Malloc(int n){
267 void *p;
drhfec00ea2008-06-14 16:56:21 +0000268 if( n<=0 ){
drhf7141992008-06-19 00:16:08 +0000269 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000270 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000271 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000272 mallocWithAlarm(n, &p);
drhfec00ea2008-06-14 16:56:21 +0000273 sqlite3_mutex_leave(mem0.mutex);
274 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000275 p = sqlite3GlobalConfig.m.xMalloc(n);
drhfec00ea2008-06-14 16:56:21 +0000276 }
277 return p;
278}
279
280/*
281** This version of the memory allocation is for use by the application.
282** First make sure the memory subsystem is initialized, then do the
283** allocation.
284*/
285void *sqlite3_malloc(int n){
286#ifndef SQLITE_OMIT_AUTOINIT
287 if( sqlite3_initialize() ) return 0;
288#endif
289 return sqlite3Malloc(n);
290}
291
292/*
drhe5ae5732008-06-15 02:51:47 +0000293** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000294** xScratchMalloc(). We verify this constraint in the single-threaded
295** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000296** is outstanding clearing it when the allocation is freed.
297*/
298#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000299static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000300#endif
301
302
303/*
304** Allocate memory that is to be used and released right away.
305** This routine is similar to alloca() in that it is not intended
306** for situations where the memory might be held long-term. This
307** routine is intended to get memory to old large transient data
308** structures that would not normally fit on the stack of an
309** embedded processor.
310*/
drhfacf0302008-06-17 15:12:00 +0000311void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000312 void *p;
313 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000314
drhe5ae5732008-06-15 02:51:47 +0000315#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000316 /* Verify that no more than one scratch allocation per thread
317 ** is outstanding at one time. (This is only checked in the
318 ** single-threaded case since checking in the multi-threaded case
319 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000320 assert( scratchAllocOut==0 );
drhe5ae5732008-06-15 02:51:47 +0000321#endif
drh9ac3fe92008-06-18 18:12:04 +0000322
danielk1977075c23a2008-09-01 18:34:20 +0000323 if( sqlite3GlobalConfig.szScratch<n ){
drhf7141992008-06-19 00:16:08 +0000324 goto scratch_overflow;
325 }else{
326 sqlite3_mutex_enter(mem0.mutex);
327 if( mem0.nScratchFree==0 ){
328 sqlite3_mutex_leave(mem0.mutex);
329 goto scratch_overflow;
330 }else{
331 int i;
332 i = mem0.aScratchFree[--mem0.nScratchFree];
danielk1977075c23a2008-09-01 18:34:20 +0000333 i *= sqlite3GlobalConfig.szScratch;
drhf7141992008-06-19 00:16:08 +0000334 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
drhe50135e2008-08-05 17:53:22 +0000335 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
danielk19778183e332008-08-29 17:56:12 +0000336 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000337 p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
drhf7141992008-06-19 00:16:08 +0000338 }
drhe5ae5732008-06-15 02:51:47 +0000339 }
drhf7141992008-06-19 00:16:08 +0000340#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
341 scratchAllocOut = p!=0;
342#endif
343
drhe5ae5732008-06-15 02:51:47 +0000344 return p;
drhf7141992008-06-19 00:16:08 +0000345
346scratch_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000347 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000348 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000349 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000350 n = mallocWithAlarm(n, &p);
351 if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
352 sqlite3_mutex_leave(mem0.mutex);
353 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000354 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000355 }
356#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
357 scratchAllocOut = p!=0;
358#endif
359 return p;
drhe5ae5732008-06-15 02:51:47 +0000360}
drhfacf0302008-06-17 15:12:00 +0000361void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000362 if( p ){
drh9ac3fe92008-06-18 18:12:04 +0000363
drhe5ae5732008-06-15 02:51:47 +0000364#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drh9ac3fe92008-06-18 18:12:04 +0000365 /* Verify that no more than one scratch allocation per thread
366 ** is outstanding at one time. (This is only checked in the
367 ** single-threaded case since checking in the multi-threaded case
368 ** would be much more complicated.) */
drhfacf0302008-06-17 15:12:00 +0000369 assert( scratchAllocOut==1 );
370 scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000371#endif
drh9ac3fe92008-06-18 18:12:04 +0000372
danielk1977075c23a2008-09-01 18:34:20 +0000373 if( sqlite3GlobalConfig.pScratch==0
374 || p<sqlite3GlobalConfig.pScratch
drhf7141992008-06-19 00:16:08 +0000375 || p>=(void*)mem0.aScratchFree ){
danielk1977075c23a2008-09-01 18:34:20 +0000376 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000377 int iSize = sqlite3MallocSize(p);
378 sqlite3_mutex_enter(mem0.mutex);
379 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
380 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000381 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000382 sqlite3_mutex_leave(mem0.mutex);
383 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000384 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000385 }
drh9ac3fe92008-06-18 18:12:04 +0000386 }else{
387 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000388 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
389 i /= sqlite3GlobalConfig.szScratch;
390 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
drhf7141992008-06-19 00:16:08 +0000391 sqlite3_mutex_enter(mem0.mutex);
danielk197700e13612008-11-17 19:18:54 +0000392 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
drh9ac3fe92008-06-18 18:12:04 +0000393 mem0.aScratchFree[mem0.nScratchFree++] = i;
drhf7141992008-06-19 00:16:08 +0000394 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
drh9ac3fe92008-06-18 18:12:04 +0000395 sqlite3_mutex_leave(mem0.mutex);
396 }
drhe5ae5732008-06-15 02:51:47 +0000397 }
398}
399
400/*
drhf7141992008-06-19 00:16:08 +0000401** Allocate memory to be used by the page cache. Make use of the
402** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
403** and that memory is of the right size and is not completely
404** consumed. Otherwise, failover to sqlite3Malloc().
drhfacf0302008-06-17 15:12:00 +0000405*/
danielk19778c0a7912008-08-20 14:49:23 +0000406#if 0
drhf7141992008-06-19 00:16:08 +0000407void *sqlite3PageMalloc(int n){
408 void *p;
409 assert( n>0 );
410 assert( (n & (n-1))==0 );
411 assert( n>=512 && n<=32768 );
drhf7141992008-06-19 00:16:08 +0000412
danielk1977075c23a2008-09-01 18:34:20 +0000413 if( sqlite3GlobalConfig.szPage<n ){
drhf7141992008-06-19 00:16:08 +0000414 goto page_overflow;
415 }else{
416 sqlite3_mutex_enter(mem0.mutex);
417 if( mem0.nPageFree==0 ){
418 sqlite3_mutex_leave(mem0.mutex);
419 goto page_overflow;
420 }else{
421 int i;
422 i = mem0.aPageFree[--mem0.nPageFree];
423 sqlite3_mutex_leave(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000424 i *= sqlite3GlobalConfig.szPage;
drhe50135e2008-08-05 17:53:22 +0000425 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000426 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000427 p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
drhf7141992008-06-19 00:16:08 +0000428 }
429 }
430 return p;
431
432page_overflow:
danielk1977075c23a2008-09-01 18:34:20 +0000433 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000434 sqlite3_mutex_enter(mem0.mutex);
drhe50135e2008-08-05 17:53:22 +0000435 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
drhf7141992008-06-19 00:16:08 +0000436 n = mallocWithAlarm(n, &p);
437 if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
438 sqlite3_mutex_leave(mem0.mutex);
439 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000440 p = sqlite3GlobalConfig.m.xMalloc(n);
drhf7141992008-06-19 00:16:08 +0000441 }
442 return p;
drhfacf0302008-06-17 15:12:00 +0000443}
drhf7141992008-06-19 00:16:08 +0000444void sqlite3PageFree(void *p){
445 if( p ){
danielk1977075c23a2008-09-01 18:34:20 +0000446 if( sqlite3GlobalConfig.pPage==0
447 || p<sqlite3GlobalConfig.pPage
drhf7141992008-06-19 00:16:08 +0000448 || p>=(void*)mem0.aPageFree ){
danielk19774b9507a2008-06-21 08:12:15 +0000449 /* In this case, the page allocation was obtained from a regular
450 ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
451 ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
452 */
danielk1977075c23a2008-09-01 18:34:20 +0000453 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000454 int iSize = sqlite3MallocSize(p);
455 sqlite3_mutex_enter(mem0.mutex);
456 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
457 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
danielk1977075c23a2008-09-01 18:34:20 +0000458 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000459 sqlite3_mutex_leave(mem0.mutex);
460 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000461 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000462 }
463 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000464 /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
danielk19774b9507a2008-06-21 08:12:15 +0000465 ** buffer. In this case all that is add the index of the page in
danielk1977075c23a2008-09-01 18:34:20 +0000466 ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
danielk19774b9507a2008-06-21 08:12:15 +0000467 ** in the mem0.aPageFree[] array.
468 */
drhf7141992008-06-19 00:16:08 +0000469 int i;
danielk1977075c23a2008-09-01 18:34:20 +0000470 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
471 i /= sqlite3GlobalConfig.szPage;
472 assert( i>=0 && i<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000473 sqlite3_mutex_enter(mem0.mutex);
danielk1977075c23a2008-09-01 18:34:20 +0000474 assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
drhf7141992008-06-19 00:16:08 +0000475 mem0.aPageFree[mem0.nPageFree++] = i;
476 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
477 sqlite3_mutex_leave(mem0.mutex);
drh5f4bcf12008-07-29 14:29:06 +0000478#if !defined(NDEBUG) && 0
danielk19774b9507a2008-06-21 08:12:15 +0000479 /* Assert that a duplicate was not just inserted into aPageFree[]. */
480 for(i=0; i<mem0.nPageFree-1; i++){
481 assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
482 }
483#endif
drhf7141992008-06-19 00:16:08 +0000484 }
485 }
drhfacf0302008-06-17 15:12:00 +0000486}
danielk19778c0a7912008-08-20 14:49:23 +0000487#endif
drhfacf0302008-06-17 15:12:00 +0000488
489/*
drh633e6d52008-07-28 19:34:53 +0000490** TRUE if p is a lookaside memory allocation from db
491*/
drh4150ebf2008-10-11 15:38:29 +0000492#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000493static int isLookaside(sqlite3 *db, void *p){
494 return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
495}
drh4150ebf2008-10-11 15:38:29 +0000496#else
497#define isLookaside(A,B) 0
498#endif
drh633e6d52008-07-28 19:34:53 +0000499
500/*
drhfec00ea2008-06-14 16:56:21 +0000501** Return the size of a memory allocation previously obtained from
502** sqlite3Malloc() or sqlite3_malloc().
503*/
504int sqlite3MallocSize(void *p){
danielk1977075c23a2008-09-01 18:34:20 +0000505 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000506}
drh633e6d52008-07-28 19:34:53 +0000507int sqlite3DbMallocSize(sqlite3 *db, void *p){
508 if( isLookaside(db, p) ){
509 return db->lookaside.sz;
510 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000511 return sqlite3GlobalConfig.m.xSize(p);
drh633e6d52008-07-28 19:34:53 +0000512 }
513}
drhfec00ea2008-06-14 16:56:21 +0000514
515/*
516** Free memory previously obtained from sqlite3Malloc().
517*/
518void sqlite3_free(void *p){
519 if( p==0 ) return;
danielk1977075c23a2008-09-01 18:34:20 +0000520 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000521 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000522 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
danielk1977075c23a2008-09-01 18:34:20 +0000523 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000524 sqlite3_mutex_leave(mem0.mutex);
525 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000526 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000527 }
528}
529
530/*
drh633e6d52008-07-28 19:34:53 +0000531** Free memory that might be associated with a particular database
532** connection.
533*/
534void sqlite3DbFree(sqlite3 *db, void *p){
535 if( isLookaside(db, p) ){
536 LookasideSlot *pBuf = (LookasideSlot*)p;
537 pBuf->pNext = db->lookaside.pFree;
538 db->lookaside.pFree = pBuf;
539 db->lookaside.nOut--;
540 }else{
541 sqlite3_free(p);
542 }
543}
544
545/*
drhfec00ea2008-06-14 16:56:21 +0000546** Change the size of an existing memory allocation
547*/
548void *sqlite3Realloc(void *pOld, int nBytes){
549 int nOld, nNew;
550 void *pNew;
551 if( pOld==0 ){
552 return sqlite3Malloc(nBytes);
553 }
554 if( nBytes<=0 ){
555 sqlite3_free(pOld);
556 return 0;
557 }
558 nOld = sqlite3MallocSize(pOld);
danielk1977075c23a2008-09-01 18:34:20 +0000559 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000560 sqlite3_mutex_enter(mem0.mutex);
drhf7141992008-06-19 00:16:08 +0000561 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000562 nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
drhfec00ea2008-06-14 16:56:21 +0000563 if( nOld==nNew ){
564 pNew = pOld;
565 }else{
drhf7141992008-06-19 00:16:08 +0000566 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
567 mem0.alarmThreshold ){
drhfec00ea2008-06-14 16:56:21 +0000568 sqlite3MallocAlarm(nNew-nOld);
569 }
danielk1977075c23a2008-09-01 18:34:20 +0000570 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
danielk1977d09414c2008-06-19 18:17:49 +0000571 if( pNew==0 && mem0.alarmCallback ){
572 sqlite3MallocAlarm(nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000573 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000574 }
575 if( pNew ){
drhc702c7c2008-07-18 18:56:16 +0000576 nNew = sqlite3MallocSize(pNew);
drhf7141992008-06-19 00:16:08 +0000577 sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000578 }
579 }
580 sqlite3_mutex_leave(mem0.mutex);
581 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000582 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
drhfec00ea2008-06-14 16:56:21 +0000583 }
584 return pNew;
585}
586
587/*
588** The public interface to sqlite3Realloc. Make sure that the memory
589** subsystem is initialized prior to invoking sqliteRealloc.
590*/
591void *sqlite3_realloc(void *pOld, int n){
592#ifndef SQLITE_OMIT_AUTOINIT
593 if( sqlite3_initialize() ) return 0;
594#endif
595 return sqlite3Realloc(pOld, n);
596}
597
drha3152892007-05-05 11:48:52 +0000598
599/*
drh17435752007-08-16 04:30:38 +0000600** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000601*/
drhfec00ea2008-06-14 16:56:21 +0000602void *sqlite3MallocZero(int n){
603 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000604 if( p ){
605 memset(p, 0, n);
606 }
607 return p;
608}
drh17435752007-08-16 04:30:38 +0000609
610/*
611** Allocate and zero memory. If the allocation fails, make
612** the mallocFailed flag in the connection pointer.
613*/
drhfec00ea2008-06-14 16:56:21 +0000614void *sqlite3DbMallocZero(sqlite3 *db, int n){
danielk1977a1644fd2007-08-29 12:31:25 +0000615 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000616 if( p ){
617 memset(p, 0, n);
drh17435752007-08-16 04:30:38 +0000618 }
619 return p;
620}
621
622/*
623** Allocate and zero memory. If the allocation fails, make
624** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000625**
626** If db!=0 and db->mallocFailed is true (indicating a prior malloc
627** failure on the same database connection) then always return 0.
628** Hence for a particular database connection, once malloc starts
629** failing, it fails consistently until mallocFailed is reset.
630** This is an important assumption. There are many places in the
631** code that do things like this:
632**
633** int *a = (int*)sqlite3DbMallocRaw(db, 100);
634** int *b = (int*)sqlite3DbMallocRaw(db, 200);
635** if( b ) a[10] = 9;
636**
637** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
638** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000639*/
drhfec00ea2008-06-14 16:56:21 +0000640void *sqlite3DbMallocRaw(sqlite3 *db, int n){
drh633e6d52008-07-28 19:34:53 +0000641 void *p;
drh4150ebf2008-10-11 15:38:29 +0000642#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000643 if( db ){
644 LookasideSlot *pBuf;
645 if( db->mallocFailed ){
646 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000647 }
drh633e6d52008-07-28 19:34:53 +0000648 if( db->lookaside.bEnabled && n<=db->lookaside.sz
649 && (pBuf = db->lookaside.pFree)!=0 ){
650 db->lookaside.pFree = pBuf->pNext;
651 db->lookaside.nOut++;
652 if( db->lookaside.nOut>db->lookaside.mxOut ){
653 db->lookaside.mxOut = db->lookaside.nOut;
654 }
655 return (void*)pBuf;
656 }
657 }
drhddecae72008-10-11 17:35:16 +0000658#else
659 if( db && db->mallocFailed ){
660 return 0;
661 }
drh4150ebf2008-10-11 15:38:29 +0000662#endif
drh633e6d52008-07-28 19:34:53 +0000663 p = sqlite3Malloc(n);
664 if( !p && db ){
665 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000666 }
667 return p;
668}
669
danielk197726783a52007-08-29 14:06:22 +0000670/*
671** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000672** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000673*/
danielk1977a1644fd2007-08-29 12:31:25 +0000674void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
675 void *pNew = 0;
676 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000677 if( p==0 ){
678 return sqlite3DbMallocRaw(db, n);
679 }
680 if( isLookaside(db, p) ){
681 if( n<=db->lookaside.sz ){
682 return p;
683 }
684 pNew = sqlite3DbMallocRaw(db, n);
685 if( pNew ){
686 memcpy(pNew, p, db->lookaside.sz);
687 sqlite3DbFree(db, p);
688 }
689 }else{
690 pNew = sqlite3_realloc(p, n);
691 if( !pNew ){
692 db->mallocFailed = 1;
693 }
danielk1977a1644fd2007-08-29 12:31:25 +0000694 }
695 }
696 return pNew;
697}
698
drh17435752007-08-16 04:30:38 +0000699/*
700** Attempt to reallocate p. If the reallocation fails, then free p
701** and set the mallocFailed flag in the database connection.
702*/
703void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
drha3152892007-05-05 11:48:52 +0000704 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000705 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000706 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000707 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000708 }
709 return pNew;
710}
711
drha3152892007-05-05 11:48:52 +0000712/*
713** Make a copy of a string in memory obtained from sqliteMalloc(). These
714** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
715** is because when memory debugging is turned on, these two functions are
716** called via macros that record the current file and line number in the
717** ThreadData structure.
718*/
drh633e6d52008-07-28 19:34:53 +0000719char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000720 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000721 size_t n;
722 if( z==0 ){
723 return 0;
724 }
drha3152892007-05-05 11:48:52 +0000725 n = strlen(z)+1;
drh633e6d52008-07-28 19:34:53 +0000726 assert( (n&0x7fffffff)==n );
727 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000728 if( zNew ){
729 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000730 }
731 return zNew;
732}
733char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
drh633e6d52008-07-28 19:34:53 +0000734 char *zNew;
735 if( z==0 ){
736 return 0;
737 }
738 assert( (n&0x7fffffff)==n );
739 zNew = sqlite3DbMallocRaw(db, n+1);
740 if( zNew ){
741 memcpy(zNew, z, n);
742 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000743 }
744 return zNew;
745}
746
drha3152892007-05-05 11:48:52 +0000747/*
drhf089aa42008-07-08 19:34:06 +0000748** Create a string from the zFromat argument and the va_list that follows.
749** Store the string in memory obtained from sqliteMalloc() and make *pz
750** point to that string.
drha3152892007-05-05 11:48:52 +0000751*/
drhf089aa42008-07-08 19:34:06 +0000752void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
drha3152892007-05-05 11:48:52 +0000753 va_list ap;
drhf089aa42008-07-08 19:34:06 +0000754 char *z;
drha3152892007-05-05 11:48:52 +0000755
drhf089aa42008-07-08 19:34:06 +0000756 va_start(ap, zFormat);
757 z = sqlite3VMPrintf(db, zFormat, ap);
drha3152892007-05-05 11:48:52 +0000758 va_end(ap);
drh633e6d52008-07-28 19:34:53 +0000759 sqlite3DbFree(db, *pz);
drhf089aa42008-07-08 19:34:06 +0000760 *pz = z;
drha3152892007-05-05 11:48:52 +0000761}
762
763
764/*
765** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000766** returning control to the user) that has called sqlite3_malloc or
767** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000768**
769** The returned value is normally a copy of the second argument to this
770** function. However, if a malloc() failure has occured since the previous
771** invocation SQLITE_NOMEM is returned instead.
772**
773** If the first argument, db, is not NULL and a malloc() error has occured,
774** then the connection error-code (the value returned by sqlite3_errcode())
775** is set to SQLITE_NOMEM.
776*/
drha3152892007-05-05 11:48:52 +0000777int sqlite3ApiExit(sqlite3* db, int rc){
danielk1977a1644fd2007-08-29 12:31:25 +0000778 /* If the db handle is not NULL, then we must hold the connection handle
779 ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
780 ** is unsafe, as is the call to sqlite3Error().
781 */
782 assert( !db || sqlite3_mutex_held(db->mutex) );
danielk197798c21902008-09-23 16:41:29 +0000783 if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
drha3152892007-05-05 11:48:52 +0000784 sqlite3Error(db, SQLITE_NOMEM, 0);
drh17435752007-08-16 04:30:38 +0000785 db->mallocFailed = 0;
drha3152892007-05-05 11:48:52 +0000786 rc = SQLITE_NOMEM;
787 }
788 return rc & (db ? db->errMask : 0xff);
789}