blob: 23d7598ae3257c12ed7a5fbd4546df64e96052b5 [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.
drha3152892007-05-05 11:48:52 +000014*/
15#include "sqliteInt.h"
drha3152892007-05-05 11:48:52 +000016#include <stdarg.h>
drha3152892007-05-05 11:48:52 +000017
18/*
danielk197784680242008-06-23 11:11:35 +000019** Attempt to release up to n bytes of non-essential memory currently
20** held by SQLite. An example of non-essential memory is memory used to
21** cache database pages that are not currently in use.
drha3152892007-05-05 11:48:52 +000022*/
23int sqlite3_release_memory(int n){
drh86f8c192007-08-22 00:39:19 +000024#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh9f129f42010-08-31 15:27:32 +000025 return sqlite3PcacheReleaseMemory(n);
danielk19771e536952007-08-16 10:09:01 +000026#else
drh9f129f42010-08-31 15:27:32 +000027 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
28 ** is a no-op returning zero if SQLite is not compiled with
29 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
danielk197762c14b32008-11-19 09:05:26 +000030 UNUSED_PARAMETER(n);
drh9f129f42010-08-31 15:27:32 +000031 return 0;
danielk19771e536952007-08-16 10:09:01 +000032#endif
drha3152892007-05-05 11:48:52 +000033}
drha3152892007-05-05 11:48:52 +000034
drhfec00ea2008-06-14 16:56:21 +000035/*
drhbadc9802010-08-27 17:16:44 +000036** An instance of the following object records the location of
37** each unused scratch buffer.
38*/
39typedef struct ScratchFreeslot {
40 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
41} ScratchFreeslot;
42
43/*
drhfec00ea2008-06-14 16:56:21 +000044** State information local to the memory allocation subsystem.
45*/
danielk19775c8f8582008-09-02 10:22:00 +000046static SQLITE_WSD struct Mem0Global {
drhfec00ea2008-06-14 16:56:21 +000047 sqlite3_mutex *mutex; /* Mutex to serialize access */
drh4ef299a2015-09-02 14:56:56 +000048 sqlite3_int64 alarmThreshold; /* The soft heap limit */
drhfec00ea2008-06-14 16:56:21 +000049
50 /*
drhbadc9802010-08-27 17:16:44 +000051 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
52 ** (so that a range test can be used to determine if an allocation
53 ** being freed came from pScratch) and a pointer to the list of
54 ** unused scratch allocations.
drh9ac3fe92008-06-18 18:12:04 +000055 */
drhbadc9802010-08-27 17:16:44 +000056 void *pScratchEnd;
57 ScratchFreeslot *pScratchFree;
58 u32 nScratchFree;
drh50d1b5f2010-08-27 12:21:06 +000059
60 /*
61 ** True if heap is nearly "full" where "full" is defined by the
62 ** sqlite3_soft_heap_limit() setting.
63 */
64 int nearlyFull;
drh4ef299a2015-09-02 14:56:56 +000065} mem0 = { 0, 0, 0, 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +000066
67#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +000068
69/*
drhaf89fe62015-03-23 17:25:18 +000070** Return the memory allocator mutex. sqlite3_status() needs it.
71*/
72sqlite3_mutex *sqlite3MallocMutex(void){
73 return mem0.mutex;
74}
75
76/*
drh4ef299a2015-09-02 14:56:56 +000077** Return the amount of memory currently in use.
drhf82ccf62010-09-15 17:54:31 +000078*/
drh4ef299a2015-09-02 14:56:56 +000079static sqlite3_int64 memInUse(void){
80 assert( sqlite3_mutex_held(mem0.mutex) );
81 return sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drhf82ccf62010-09-15 17:54:31 +000082}
83
84/*
drh4ef299a2015-09-02 14:56:56 +000085** Called when the soft heap limit is exceeded for an allocation
86** of nBytes.
drhf82ccf62010-09-15 17:54:31 +000087*/
drh4ef299a2015-09-02 14:56:56 +000088#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
89static void sqlite3HeapLimitExceeded(int nByte){
90 sqlite3_int64 excess = memInUse() + nByte - mem0.alarmThreshold;
drhf82ccf62010-09-15 17:54:31 +000091 sqlite3_mutex_leave(mem0.mutex);
drh4ef299a2015-09-02 14:56:56 +000092 sqlite3_release_memory((int)(excess & 0x7fffffff));
93 sqlite3_mutex_enter(mem0.mutex);
94}
95#else
96# define sqlite3HeapLimitExceeded(X) /* no-op */
97#endif
98
99/*
100** Check to see if increasing the total memory usage by nNew bytes
101** will exceed the soft heap limit.
102**
103** If the soft heap limit is exceeded, set the mem0.nearlyFull flag
104** and invoke sqlite3HeapLimitExceeded() to try to free up some
105** memory.
106*/
107static void sqlite3CheckSoftHeapLimit(int nNew){
108 assert( sqlite3_mutex_held(mem0.mutex) );
109 if( mem0.alarmThreshold>0 ){
110 if( mem0.alarmThreshold-nNew >= memInUse() ){
111 mem0.nearlyFull = 1;
112 sqlite3HeapLimitExceeded(nNew);
113 }else{
114 mem0.nearlyFull = 0;
115 }
116 }
drhf82ccf62010-09-15 17:54:31 +0000117}
118
119#ifndef SQLITE_OMIT_DEPRECATED
120/*
drh4ef299a2015-09-02 14:56:56 +0000121** Deprecated external interface. First deprecated 2007-11-05. Changed
122** into a no-op on 2015-09-02.
drhf82ccf62010-09-15 17:54:31 +0000123*/
124int sqlite3_memory_alarm(
125 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
126 void *pArg,
127 sqlite3_int64 iThreshold
128){
drh4ef299a2015-09-02 14:56:56 +0000129 return SQLITE_OK;
drhf82ccf62010-09-15 17:54:31 +0000130}
131#endif
132
133/*
134** Set the soft heap-size limit for the library. Passing a zero or
135** negative value indicates no limit.
136*/
137sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
138 sqlite3_int64 priorLimit;
drhf82ccf62010-09-15 17:54:31 +0000139#ifndef SQLITE_OMIT_AUTOINIT
drhde0f1812011-12-22 17:10:35 +0000140 int rc = sqlite3_initialize();
141 if( rc ) return -1;
drhf82ccf62010-09-15 17:54:31 +0000142#endif
143 sqlite3_mutex_enter(mem0.mutex);
144 priorLimit = mem0.alarmThreshold;
drhf82ccf62010-09-15 17:54:31 +0000145 if( n>0 ){
drh4ef299a2015-09-02 14:56:56 +0000146 mem0.alarmThreshold = n;
147 sqlite3CheckSoftHeapLimit(0);
148 }else if( n==0 ){
149 mem0.alarmThreshold = 0;
150 mem0.nearlyFull = 0;
drhf82ccf62010-09-15 17:54:31 +0000151 }
drh4ef299a2015-09-02 14:56:56 +0000152 sqlite3_mutex_leave(mem0.mutex);
drhf82ccf62010-09-15 17:54:31 +0000153 return priorLimit;
154}
155void sqlite3_soft_heap_limit(int n){
156 if( n<0 ) n = 0;
157 sqlite3_soft_heap_limit64(n);
158}
159
160/*
drhfec00ea2008-06-14 16:56:21 +0000161** Initialize the memory allocation subsystem.
162*/
163int sqlite3MallocInit(void){
drh592f0cb2015-03-26 17:04:23 +0000164 int rc;
danielk1977075c23a2008-09-01 18:34:20 +0000165 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000166 sqlite3MemSetDefault();
167 }
168 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000169 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000170 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000171 }
danielk1977075c23a2008-09-01 18:34:20 +0000172 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000173 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000174 int i, n, sz;
175 ScratchFreeslot *pSlot;
176 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
177 sqlite3GlobalConfig.szScratch = sz;
178 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
179 n = sqlite3GlobalConfig.nScratch;
180 mem0.pScratchFree = pSlot;
181 mem0.nScratchFree = n;
182 for(i=0; i<n-1; i++){
183 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
184 pSlot = pSlot->pNext;
185 }
186 pSlot->pNext = 0;
187 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000188 }else{
drhbadc9802010-08-27 17:16:44 +0000189 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000190 sqlite3GlobalConfig.pScratch = 0;
191 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000192 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000193 }
drh50d1b5f2010-08-27 12:21:06 +0000194 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000195 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000196 sqlite3GlobalConfig.pPage = 0;
197 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000198 }
drh592f0cb2015-03-26 17:04:23 +0000199 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
200 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
201 return rc;
drhfec00ea2008-06-14 16:56:21 +0000202}
203
204/*
drh50d1b5f2010-08-27 12:21:06 +0000205** Return true if the heap is currently under memory pressure - in other
206** words if the amount of heap used is close to the limit set by
207** sqlite3_soft_heap_limit().
208*/
209int sqlite3HeapNearlyFull(void){
210 return mem0.nearlyFull;
211}
212
213/*
drhfec00ea2008-06-14 16:56:21 +0000214** Deinitialize the memory allocation subsystem.
215*/
216void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000217 if( sqlite3GlobalConfig.m.xShutdown ){
218 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
219 }
drh9ac3fe92008-06-18 18:12:04 +0000220 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000221}
222
223/*
224** Return the amount of memory currently checked out.
225*/
226sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000227 sqlite3_int64 res, mx;
228 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000229 return res;
drhfec00ea2008-06-14 16:56:21 +0000230}
231
232/*
233** Return the maximum amount of memory that has ever been
234** checked out since either the beginning of this process
235** or since the most recent reset.
236*/
237sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000238 sqlite3_int64 res, mx;
239 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
240 return mx;
drhfec00ea2008-06-14 16:56:21 +0000241}
242
243/*
drhf7141992008-06-19 00:16:08 +0000244** Do a memory allocation with statistics and alarms. Assume the
245** lock is already held.
246*/
247static int mallocWithAlarm(int n, void **pp){
248 int nFull;
249 void *p;
250 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000251 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000252 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
drh4ef299a2015-09-02 14:56:56 +0000253 sqlite3CheckSoftHeapLimit(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000254 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000255#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh4ef299a2015-09-02 14:56:56 +0000256 if( p==0 && mem0.alarmThreshold ){
257 sqlite3HeapLimitExceeded(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000258 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000259 }
drh50d1b5f2010-08-27 12:21:06 +0000260#endif
drhc702c7c2008-07-18 18:56:16 +0000261 if( p ){
262 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000263 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
264 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000265 }
drhf7141992008-06-19 00:16:08 +0000266 *pp = p;
267 return nFull;
268}
drhfec00ea2008-06-14 16:56:21 +0000269
270/*
271** Allocate memory. This routine is like sqlite3_malloc() except that it
272** assumes the memory subsystem has already been initialized.
273*/
drhda4ca9d2014-09-09 17:27:35 +0000274void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000275 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000276 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000277 /* A memory allocation of a number of bytes which is near the maximum
278 ** signed integer value might cause an integer overflow inside of the
279 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
280 ** 255 bytes of overhead. SQLite itself will never use anything near
281 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000282 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000283 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000284 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000285 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000286 sqlite3_mutex_leave(mem0.mutex);
287 }else{
drhda4ca9d2014-09-09 17:27:35 +0000288 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000289 }
drh8da47412014-10-03 14:54:47 +0000290 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000291 return p;
292}
293
294/*
295** This version of the memory allocation is for use by the application.
296** First make sure the memory subsystem is initialized, then do the
297** allocation.
298*/
299void *sqlite3_malloc(int n){
300#ifndef SQLITE_OMIT_AUTOINIT
301 if( sqlite3_initialize() ) return 0;
302#endif
drhda4ca9d2014-09-09 17:27:35 +0000303 return n<=0 ? 0 : sqlite3Malloc(n);
304}
305void *sqlite3_malloc64(sqlite3_uint64 n){
306#ifndef SQLITE_OMIT_AUTOINIT
307 if( sqlite3_initialize() ) return 0;
308#endif
drhfec00ea2008-06-14 16:56:21 +0000309 return sqlite3Malloc(n);
310}
311
312/*
drhe5ae5732008-06-15 02:51:47 +0000313** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000314** xScratchMalloc(). We verify this constraint in the single-threaded
315** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000316** is outstanding clearing it when the allocation is freed.
317*/
318#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000319static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000320#endif
321
322
323/*
324** Allocate memory that is to be used and released right away.
325** This routine is similar to alloca() in that it is not intended
326** for situations where the memory might be held long-term. This
327** routine is intended to get memory to old large transient data
328** structures that would not normally fit on the stack of an
329** embedded processor.
330*/
drhfacf0302008-06-17 15:12:00 +0000331void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000332 void *p;
333 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000334
drhbadc9802010-08-27 17:16:44 +0000335 sqlite3_mutex_enter(mem0.mutex);
drh3ccd5bf2014-08-23 19:04:55 +0000336 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000337 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
338 p = mem0.pScratchFree;
339 mem0.pScratchFree = mem0.pScratchFree->pNext;
340 mem0.nScratchFree--;
drhaf89fe62015-03-23 17:25:18 +0000341 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000342 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000343 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000344 sqlite3_mutex_leave(mem0.mutex);
345 p = sqlite3Malloc(n);
346 if( sqlite3GlobalConfig.bMemstat && p ){
347 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000348 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000349 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000350 }
351 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
352 }
drh1ff6e3a2010-09-02 17:15:19 +0000353 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000354
drhbadc9802010-08-27 17:16:44 +0000355
drhe5ae5732008-06-15 02:51:47 +0000356#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhcbd55b02014-11-04 14:22:27 +0000357 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
358 ** buffers per thread.
359 **
360 ** This can only be checked in single-threaded mode.
361 */
362 assert( scratchAllocOut==0 );
drhbadc9802010-08-27 17:16:44 +0000363 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000364#endif
365
drhe5ae5732008-06-15 02:51:47 +0000366 return p;
367}
drhfacf0302008-06-17 15:12:00 +0000368void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000369 if( p ){
drhbadc9802010-08-27 17:16:44 +0000370
371#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
372 /* Verify that no more than two scratch allocation per thread
373 ** is outstanding at one time. (This is only checked in the
374 ** single-threaded case since checking in the multi-threaded case
375 ** would be much more complicated.) */
376 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
377 scratchAllocOut--;
378#endif
379
380 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
381 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
382 ScratchFreeslot *pSlot;
383 pSlot = (ScratchFreeslot*)p;
384 sqlite3_mutex_enter(mem0.mutex);
385 pSlot->pNext = mem0.pScratchFree;
386 mem0.pScratchFree = pSlot;
387 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000388 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhaf89fe62015-03-23 17:25:18 +0000389 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
drhbadc9802010-08-27 17:16:44 +0000390 sqlite3_mutex_leave(mem0.mutex);
391 }else{
392 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000393 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
mistachkind4258642015-03-21 23:38:59 +0000394 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000395 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000396 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000397 int iSize = sqlite3MallocSize(p);
398 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000399 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
400 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
401 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000402 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000403 sqlite3_mutex_leave(mem0.mutex);
404 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000405 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000406 }
drh9ac3fe92008-06-18 18:12:04 +0000407 }
drhe5ae5732008-06-15 02:51:47 +0000408 }
409}
410
411/*
drh633e6d52008-07-28 19:34:53 +0000412** TRUE if p is a lookaside memory allocation from db
413*/
drh4150ebf2008-10-11 15:38:29 +0000414#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000415static int isLookaside(sqlite3 *db, void *p){
drhb0e77042013-12-10 19:49:00 +0000416 return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000417}
drh4150ebf2008-10-11 15:38:29 +0000418#else
419#define isLookaside(A,B) 0
420#endif
drh633e6d52008-07-28 19:34:53 +0000421
422/*
drhfec00ea2008-06-14 16:56:21 +0000423** Return the size of a memory allocation previously obtained from
424** sqlite3Malloc() or sqlite3_malloc().
425*/
426int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000427 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000428 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000429}
drh633e6d52008-07-28 19:34:53 +0000430int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh054bbab2015-09-01 20:09:33 +0000431 if( db==0 || !isLookaside(db,p) ){
432#if SQLITE_DEBUG
433 if( db==0 ){
434 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
435 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000436 }else{
drhd231aa32014-10-07 15:46:54 +0000437 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000438 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000439 }
drh054bbab2015-09-01 20:09:33 +0000440#endif
441 return sqlite3GlobalConfig.m.xSize(p);
442 }else{
443 assert( sqlite3_mutex_held(db->mutex) );
444 return db->lookaside.sz;
drh633e6d52008-07-28 19:34:53 +0000445 }
446}
drhda4ca9d2014-09-09 17:27:35 +0000447sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000448 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000449 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drhda4ca9d2014-09-09 17:27:35 +0000450 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
451}
drhfec00ea2008-06-14 16:56:21 +0000452
453/*
454** Free memory previously obtained from sqlite3Malloc().
455*/
456void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000457 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000458 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000459 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000460 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000461 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000462 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
463 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000464 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000465 sqlite3_mutex_leave(mem0.mutex);
466 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000467 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000468 }
469}
470
471/*
drhb4586f12014-08-23 19:42:06 +0000472** Add the size of memory allocation "p" to the count in
473** *db->pnBytesFreed.
474*/
475static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
476 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
477}
478
479/*
drh633e6d52008-07-28 19:34:53 +0000480** Free memory that might be associated with a particular database
481** connection.
482*/
483void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000484 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000485 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000486 if( db ){
487 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000488 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000489 return;
dand46def72010-07-24 11:28:28 +0000490 }
drh174b9a12010-07-26 11:07:20 +0000491 if( isLookaside(db, p) ){
492 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000493#if SQLITE_DEBUG
494 /* Trash all content in the buffer being freed */
495 memset(p, 0xaa, db->lookaside.sz);
496#endif
drh174b9a12010-07-26 11:07:20 +0000497 pBuf->pNext = db->lookaside.pFree;
498 db->lookaside.pFree = pBuf;
499 db->lookaside.nOut--;
500 return;
501 }
drh633e6d52008-07-28 19:34:53 +0000502 }
drhd231aa32014-10-07 15:46:54 +0000503 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000504 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000505 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
506 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
507 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000508}
509
510/*
drhfec00ea2008-06-14 16:56:21 +0000511** Change the size of an existing memory allocation
512*/
drhda4ca9d2014-09-09 17:27:35 +0000513void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000514 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000515 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000516 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000517 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000518 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000519 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000520 }
drhda4ca9d2014-09-09 17:27:35 +0000521 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000522 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000523 return 0;
524 }
drhb6063cf2009-06-27 00:48:33 +0000525 if( nBytes>=0x7fffff00 ){
526 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
527 return 0;
528 }
drhfec00ea2008-06-14 16:56:21 +0000529 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000530 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
531 ** argument to xRealloc is always a value returned by a prior call to
532 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000533 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000534 if( nOld==nNew ){
535 pNew = pOld;
536 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000537 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000538 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000539 nDiff = nNew - nOld;
drh4ef299a2015-09-02 14:56:56 +0000540 sqlite3CheckSoftHeapLimit(nDiff);
drh7c6791c2009-08-18 14:48:53 +0000541 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh4ef299a2015-09-02 14:56:56 +0000542#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
543 if( pNew==0 && mem0.alarmThreshold ){
544 sqlite3HeapLimitExceeded((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000545 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000546 }
drh4ef299a2015-09-02 14:56:56 +0000547#endif
drh7c6791c2009-08-18 14:48:53 +0000548 if( pNew ){
549 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000550 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000551 }
552 sqlite3_mutex_leave(mem0.mutex);
553 }else{
drh7c6791c2009-08-18 14:48:53 +0000554 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000555 }
drh8da47412014-10-03 14:54:47 +0000556 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000557 return pNew;
558}
559
560/*
561** The public interface to sqlite3Realloc. Make sure that the memory
562** subsystem is initialized prior to invoking sqliteRealloc.
563*/
564void *sqlite3_realloc(void *pOld, int n){
565#ifndef SQLITE_OMIT_AUTOINIT
566 if( sqlite3_initialize() ) return 0;
567#endif
drh8da47412014-10-03 14:54:47 +0000568 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000569 return sqlite3Realloc(pOld, n);
570}
571void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
572#ifndef SQLITE_OMIT_AUTOINIT
573 if( sqlite3_initialize() ) return 0;
574#endif
drhfec00ea2008-06-14 16:56:21 +0000575 return sqlite3Realloc(pOld, n);
576}
577
drha3152892007-05-05 11:48:52 +0000578
579/*
drh17435752007-08-16 04:30:38 +0000580** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000581*/
drhda4ca9d2014-09-09 17:27:35 +0000582void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000583 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000584 if( p ){
drh20f3df02014-09-18 02:20:54 +0000585 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000586 }
587 return p;
588}
drh17435752007-08-16 04:30:38 +0000589
590/*
591** Allocate and zero memory. If the allocation fails, make
592** the mallocFailed flag in the connection pointer.
593*/
drhda4ca9d2014-09-09 17:27:35 +0000594void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000595 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000596 if( p ){
drh20f3df02014-09-18 02:20:54 +0000597 memset(p, 0, (size_t)n);
drh17435752007-08-16 04:30:38 +0000598 }
599 return p;
600}
601
602/*
603** Allocate and zero memory. If the allocation fails, make
604** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000605**
606** If db!=0 and db->mallocFailed is true (indicating a prior malloc
607** failure on the same database connection) then always return 0.
608** Hence for a particular database connection, once malloc starts
609** failing, it fails consistently until mallocFailed is reset.
610** This is an important assumption. There are many places in the
611** code that do things like this:
612**
613** int *a = (int*)sqlite3DbMallocRaw(db, 100);
614** int *b = (int*)sqlite3DbMallocRaw(db, 200);
615** if( b ) a[10] = 9;
616**
617** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
618** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000619*/
drhda4ca9d2014-09-09 17:27:35 +0000620void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh633e6d52008-07-28 19:34:53 +0000621 void *p;
drhd9da78a2009-03-24 15:08:09 +0000622 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000623 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000624#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000625 if( db ){
626 LookasideSlot *pBuf;
627 if( db->mallocFailed ){
628 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000629 }
drh0b12e7f2010-12-20 15:51:58 +0000630 if( db->lookaside.bEnabled ){
631 if( n>db->lookaside.sz ){
632 db->lookaside.anStat[1]++;
633 }else if( (pBuf = db->lookaside.pFree)==0 ){
634 db->lookaside.anStat[2]++;
635 }else{
636 db->lookaside.pFree = pBuf->pNext;
637 db->lookaside.nOut++;
638 db->lookaside.anStat[0]++;
639 if( db->lookaside.nOut>db->lookaside.mxOut ){
640 db->lookaside.mxOut = db->lookaside.nOut;
641 }
642 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000643 }
drh633e6d52008-07-28 19:34:53 +0000644 }
645 }
drhddecae72008-10-11 17:35:16 +0000646#else
647 if( db && db->mallocFailed ){
648 return 0;
649 }
drh4150ebf2008-10-11 15:38:29 +0000650#endif
drh633e6d52008-07-28 19:34:53 +0000651 p = sqlite3Malloc(n);
652 if( !p && db ){
653 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000654 }
drhd231aa32014-10-07 15:46:54 +0000655 sqlite3MemdebugSetType(p,
656 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000657 return p;
658}
659
danielk197726783a52007-08-29 14:06:22 +0000660/*
661** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000662** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000663*/
drhda4ca9d2014-09-09 17:27:35 +0000664void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000665 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000666 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000667 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000668 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000669 if( p==0 ){
670 return sqlite3DbMallocRaw(db, n);
671 }
672 if( isLookaside(db, p) ){
673 if( n<=db->lookaside.sz ){
674 return p;
675 }
676 pNew = sqlite3DbMallocRaw(db, n);
677 if( pNew ){
678 memcpy(pNew, p, db->lookaside.sz);
679 sqlite3DbFree(db, p);
680 }
681 }else{
drhd231aa32014-10-07 15:46:54 +0000682 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000683 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000684 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000685 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000686 if( !pNew ){
687 db->mallocFailed = 1;
688 }
drhd231aa32014-10-07 15:46:54 +0000689 sqlite3MemdebugSetType(pNew,
drh174b9a12010-07-26 11:07:20 +0000690 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000691 }
692 }
693 return pNew;
694}
695
drh17435752007-08-16 04:30:38 +0000696/*
697** Attempt to reallocate p. If the reallocation fails, then free p
698** and set the mallocFailed flag in the database connection.
699*/
drhda4ca9d2014-09-09 17:27:35 +0000700void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000701 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000702 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000703 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000704 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000705 }
706 return pNew;
707}
708
drha3152892007-05-05 11:48:52 +0000709/*
710** Make a copy of a string in memory obtained from sqliteMalloc(). These
711** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
712** is because when memory debugging is turned on, these two functions are
713** called via macros that record the current file and line number in the
714** ThreadData structure.
715*/
drh633e6d52008-07-28 19:34:53 +0000716char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000717 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000718 size_t n;
719 if( z==0 ){
720 return 0;
721 }
drhdee0e402009-05-03 20:23:53 +0000722 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000723 assert( (n&0x7fffffff)==n );
724 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000725 if( zNew ){
726 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000727 }
728 return zNew;
729}
drhda4ca9d2014-09-09 17:27:35 +0000730char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000731 char *zNew;
732 if( z==0 ){
733 return 0;
734 }
735 assert( (n&0x7fffffff)==n );
736 zNew = sqlite3DbMallocRaw(db, n+1);
737 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000738 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000739 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000740 }
741 return zNew;
742}
743
drha3152892007-05-05 11:48:52 +0000744/*
drh22c17b82015-05-15 04:13:15 +0000745** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000746*/
drh22c17b82015-05-15 04:13:15 +0000747void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000748 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000749 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000750}
751
drhb50c65d2014-08-23 20:25:53 +0000752/*
753** Take actions at the end of an API call to indicate an OOM error
754*/
755static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
756 db->mallocFailed = 0;
757 sqlite3Error(db, SQLITE_NOMEM);
758 return SQLITE_NOMEM;
759}
drha3152892007-05-05 11:48:52 +0000760
761/*
762** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000763** returning control to the user) that has called sqlite3_malloc or
764** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000765**
766** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000767** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000768** invocation SQLITE_NOMEM is returned instead.
769**
drh597d2b62015-06-30 03:13:47 +0000770** If an OOM as occurred, then the connection error-code (the value
771** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000772*/
drha3152892007-05-05 11:48:52 +0000773int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000774 /* If the db handle must hold the connection handle mutex here.
775 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000776 ** is unsafe, as is the call to sqlite3Error().
777 */
drh597d2b62015-06-30 03:13:47 +0000778 assert( db!=0 );
779 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000780 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
781 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000782 }
drhb50c65d2014-08-23 20:25:53 +0000783 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000784}