blob: f20eb6e79659c06aecebb739f4cba7cfd90c8a78 [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 */
drh5fb72e52015-09-10 01:22:09 +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
drhf82ccf62010-09-15 17:54:31 +000076#ifndef SQLITE_OMIT_DEPRECATED
77/*
drh5fb72e52015-09-10 01:22:09 +000078** Deprecated external interface. It used to set an alarm callback
79** that was invoked when memory usage grew too large. Now it is a
80** no-op.
drhf82ccf62010-09-15 17:54:31 +000081*/
82int sqlite3_memory_alarm(
83 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
84 void *pArg,
85 sqlite3_int64 iThreshold
86){
drh5fb72e52015-09-10 01:22:09 +000087 (void)xCallback;
88 (void)pArg;
89 (void)iThreshold;
drh4ef299a2015-09-02 14:56:56 +000090 return SQLITE_OK;
drhf82ccf62010-09-15 17:54:31 +000091}
92#endif
93
94/*
95** Set the soft heap-size limit for the library. Passing a zero or
96** negative value indicates no limit.
97*/
98sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
99 sqlite3_int64 priorLimit;
drh5fb72e52015-09-10 01:22:09 +0000100 sqlite3_int64 excess;
101 sqlite3_int64 nUsed;
drhf82ccf62010-09-15 17:54:31 +0000102#ifndef SQLITE_OMIT_AUTOINIT
drhde0f1812011-12-22 17:10:35 +0000103 int rc = sqlite3_initialize();
104 if( rc ) return -1;
drhf82ccf62010-09-15 17:54:31 +0000105#endif
106 sqlite3_mutex_enter(mem0.mutex);
107 priorLimit = mem0.alarmThreshold;
drh5fb72e52015-09-10 01:22:09 +0000108 if( n<0 ){
109 sqlite3_mutex_leave(mem0.mutex);
110 return priorLimit;
drhf82ccf62010-09-15 17:54:31 +0000111 }
drh5fb72e52015-09-10 01:22:09 +0000112 mem0.alarmThreshold = n;
113 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
114 mem0.nearlyFull = (n>0 && n<=nUsed);
drh4ef299a2015-09-02 14:56:56 +0000115 sqlite3_mutex_leave(mem0.mutex);
drh5fb72e52015-09-10 01:22:09 +0000116 excess = sqlite3_memory_used() - n;
117 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
drhf82ccf62010-09-15 17:54:31 +0000118 return priorLimit;
119}
120void sqlite3_soft_heap_limit(int n){
121 if( n<0 ) n = 0;
122 sqlite3_soft_heap_limit64(n);
123}
124
125/*
drhfec00ea2008-06-14 16:56:21 +0000126** Initialize the memory allocation subsystem.
127*/
128int sqlite3MallocInit(void){
drh592f0cb2015-03-26 17:04:23 +0000129 int rc;
danielk1977075c23a2008-09-01 18:34:20 +0000130 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000131 sqlite3MemSetDefault();
132 }
133 memset(&mem0, 0, sizeof(mem0));
danielk1977075c23a2008-09-01 18:34:20 +0000134 if( sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +0000135 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drhfec00ea2008-06-14 16:56:21 +0000136 }
danielk1977075c23a2008-09-01 18:34:20 +0000137 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000138 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000139 int i, n, sz;
140 ScratchFreeslot *pSlot;
141 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
142 sqlite3GlobalConfig.szScratch = sz;
143 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
144 n = sqlite3GlobalConfig.nScratch;
145 mem0.pScratchFree = pSlot;
146 mem0.nScratchFree = n;
147 for(i=0; i<n-1; i++){
148 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
149 pSlot = pSlot->pNext;
150 }
151 pSlot->pNext = 0;
152 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000153 }else{
drhbadc9802010-08-27 17:16:44 +0000154 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000155 sqlite3GlobalConfig.pScratch = 0;
156 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000157 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000158 }
drh50d1b5f2010-08-27 12:21:06 +0000159 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000160 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000161 sqlite3GlobalConfig.pPage = 0;
162 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000163 }
drh592f0cb2015-03-26 17:04:23 +0000164 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
165 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
166 return rc;
drhfec00ea2008-06-14 16:56:21 +0000167}
168
169/*
drh50d1b5f2010-08-27 12:21:06 +0000170** Return true if the heap is currently under memory pressure - in other
171** words if the amount of heap used is close to the limit set by
172** sqlite3_soft_heap_limit().
173*/
174int sqlite3HeapNearlyFull(void){
175 return mem0.nearlyFull;
176}
177
178/*
drhfec00ea2008-06-14 16:56:21 +0000179** Deinitialize the memory allocation subsystem.
180*/
181void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000182 if( sqlite3GlobalConfig.m.xShutdown ){
183 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
184 }
drh9ac3fe92008-06-18 18:12:04 +0000185 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000186}
187
188/*
189** Return the amount of memory currently checked out.
190*/
191sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000192 sqlite3_int64 res, mx;
193 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000194 return res;
drhfec00ea2008-06-14 16:56:21 +0000195}
196
197/*
198** Return the maximum amount of memory that has ever been
199** checked out since either the beginning of this process
200** or since the most recent reset.
201*/
202sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000203 sqlite3_int64 res, mx;
204 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
205 return mx;
drhfec00ea2008-06-14 16:56:21 +0000206}
207
208/*
drh5fb72e52015-09-10 01:22:09 +0000209** Trigger the alarm
210*/
211static void sqlite3MallocAlarm(int nByte){
212 if( mem0.alarmThreshold<=0 ) return;
213 sqlite3_mutex_leave(mem0.mutex);
214 sqlite3_release_memory(nByte);
215 sqlite3_mutex_enter(mem0.mutex);
216}
217
218/*
drhf7141992008-06-19 00:16:08 +0000219** Do a memory allocation with statistics and alarms. Assume the
220** lock is already held.
221*/
222static int mallocWithAlarm(int n, void **pp){
223 int nFull;
224 void *p;
225 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000226 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhf7141992008-06-19 00:16:08 +0000227 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
drh5fb72e52015-09-10 01:22:09 +0000228 if( mem0.alarmThreshold>0 ){
229 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
230 if( nUsed >= mem0.alarmThreshold - nFull ){
231 mem0.nearlyFull = 1;
232 sqlite3MallocAlarm(nFull);
233 }else{
234 mem0.nearlyFull = 0;
235 }
236 }
danielk1977075c23a2008-09-01 18:34:20 +0000237 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000238#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000239 if( p==0 && mem0.alarmThreshold>0 ){
240 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000241 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000242 }
drh50d1b5f2010-08-27 12:21:06 +0000243#endif
drhc702c7c2008-07-18 18:56:16 +0000244 if( p ){
245 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000246 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
247 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000248 }
drhf7141992008-06-19 00:16:08 +0000249 *pp = p;
250 return nFull;
251}
drhfec00ea2008-06-14 16:56:21 +0000252
253/*
254** Allocate memory. This routine is like sqlite3_malloc() except that it
255** assumes the memory subsystem has already been initialized.
256*/
drhda4ca9d2014-09-09 17:27:35 +0000257void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000258 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000259 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000260 /* A memory allocation of a number of bytes which is near the maximum
261 ** signed integer value might cause an integer overflow inside of the
262 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
263 ** 255 bytes of overhead. SQLite itself will never use anything near
264 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000265 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000266 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000267 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000268 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000269 sqlite3_mutex_leave(mem0.mutex);
270 }else{
drhda4ca9d2014-09-09 17:27:35 +0000271 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000272 }
drh8da47412014-10-03 14:54:47 +0000273 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000274 return p;
275}
276
277/*
278** This version of the memory allocation is for use by the application.
279** First make sure the memory subsystem is initialized, then do the
280** allocation.
281*/
282void *sqlite3_malloc(int n){
283#ifndef SQLITE_OMIT_AUTOINIT
284 if( sqlite3_initialize() ) return 0;
285#endif
drhda4ca9d2014-09-09 17:27:35 +0000286 return n<=0 ? 0 : sqlite3Malloc(n);
287}
288void *sqlite3_malloc64(sqlite3_uint64 n){
289#ifndef SQLITE_OMIT_AUTOINIT
290 if( sqlite3_initialize() ) return 0;
291#endif
drhfec00ea2008-06-14 16:56:21 +0000292 return sqlite3Malloc(n);
293}
294
295/*
drhe5ae5732008-06-15 02:51:47 +0000296** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000297** xScratchMalloc(). We verify this constraint in the single-threaded
298** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000299** is outstanding clearing it when the allocation is freed.
300*/
301#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000302static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000303#endif
304
305
306/*
307** Allocate memory that is to be used and released right away.
308** This routine is similar to alloca() in that it is not intended
309** for situations where the memory might be held long-term. This
310** routine is intended to get memory to old large transient data
311** structures that would not normally fit on the stack of an
312** embedded processor.
313*/
drhfacf0302008-06-17 15:12:00 +0000314void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000315 void *p;
316 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000317
drhbadc9802010-08-27 17:16:44 +0000318 sqlite3_mutex_enter(mem0.mutex);
drh3ccd5bf2014-08-23 19:04:55 +0000319 sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000320 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
321 p = mem0.pScratchFree;
322 mem0.pScratchFree = mem0.pScratchFree->pNext;
323 mem0.nScratchFree--;
drhaf89fe62015-03-23 17:25:18 +0000324 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000325 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000326 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000327 sqlite3_mutex_leave(mem0.mutex);
328 p = sqlite3Malloc(n);
329 if( sqlite3GlobalConfig.bMemstat && p ){
330 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000331 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000332 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000333 }
334 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
335 }
drh1ff6e3a2010-09-02 17:15:19 +0000336 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000337
drhbadc9802010-08-27 17:16:44 +0000338
drhe5ae5732008-06-15 02:51:47 +0000339#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhcbd55b02014-11-04 14:22:27 +0000340 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
341 ** buffers per thread.
342 **
343 ** This can only be checked in single-threaded mode.
344 */
345 assert( scratchAllocOut==0 );
drhbadc9802010-08-27 17:16:44 +0000346 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000347#endif
348
drhe5ae5732008-06-15 02:51:47 +0000349 return p;
350}
drhfacf0302008-06-17 15:12:00 +0000351void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000352 if( p ){
drhbadc9802010-08-27 17:16:44 +0000353
354#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
355 /* Verify that no more than two scratch allocation per thread
356 ** is outstanding at one time. (This is only checked in the
357 ** single-threaded case since checking in the multi-threaded case
358 ** would be much more complicated.) */
359 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
360 scratchAllocOut--;
361#endif
362
363 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
364 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
365 ScratchFreeslot *pSlot;
366 pSlot = (ScratchFreeslot*)p;
367 sqlite3_mutex_enter(mem0.mutex);
368 pSlot->pNext = mem0.pScratchFree;
369 mem0.pScratchFree = pSlot;
370 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000371 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhaf89fe62015-03-23 17:25:18 +0000372 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
drhbadc9802010-08-27 17:16:44 +0000373 sqlite3_mutex_leave(mem0.mutex);
374 }else{
375 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000376 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
mistachkind4258642015-03-21 23:38:59 +0000377 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000378 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000379 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000380 int iSize = sqlite3MallocSize(p);
381 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000382 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
383 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
384 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000385 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000386 sqlite3_mutex_leave(mem0.mutex);
387 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000388 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000389 }
drh9ac3fe92008-06-18 18:12:04 +0000390 }
drhe5ae5732008-06-15 02:51:47 +0000391 }
392}
393
394/*
drh633e6d52008-07-28 19:34:53 +0000395** TRUE if p is a lookaside memory allocation from db
396*/
drh4150ebf2008-10-11 15:38:29 +0000397#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000398static int isLookaside(sqlite3 *db, void *p){
drhb0e77042013-12-10 19:49:00 +0000399 return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000400}
drh4150ebf2008-10-11 15:38:29 +0000401#else
402#define isLookaside(A,B) 0
403#endif
drh633e6d52008-07-28 19:34:53 +0000404
405/*
drhfec00ea2008-06-14 16:56:21 +0000406** Return the size of a memory allocation previously obtained from
407** sqlite3Malloc() or sqlite3_malloc().
408*/
409int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000410 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000411 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000412}
drh633e6d52008-07-28 19:34:53 +0000413int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh054bbab2015-09-01 20:09:33 +0000414 if( db==0 || !isLookaside(db,p) ){
415#if SQLITE_DEBUG
416 if( db==0 ){
417 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
418 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000419 }else{
drhd231aa32014-10-07 15:46:54 +0000420 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000421 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000422 }
drh054bbab2015-09-01 20:09:33 +0000423#endif
424 return sqlite3GlobalConfig.m.xSize(p);
425 }else{
426 assert( sqlite3_mutex_held(db->mutex) );
427 return db->lookaside.sz;
drh633e6d52008-07-28 19:34:53 +0000428 }
429}
drhda4ca9d2014-09-09 17:27:35 +0000430sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000431 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000432 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drhda4ca9d2014-09-09 17:27:35 +0000433 return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
434}
drhfec00ea2008-06-14 16:56:21 +0000435
436/*
437** Free memory previously obtained from sqlite3Malloc().
438*/
439void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000440 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000441 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000442 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000443 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000444 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000445 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
446 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000447 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000448 sqlite3_mutex_leave(mem0.mutex);
449 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000450 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000451 }
452}
453
454/*
drhb4586f12014-08-23 19:42:06 +0000455** Add the size of memory allocation "p" to the count in
456** *db->pnBytesFreed.
457*/
458static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
459 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
460}
461
462/*
drh633e6d52008-07-28 19:34:53 +0000463** Free memory that might be associated with a particular database
464** connection.
465*/
466void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000467 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000468 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000469 if( db ){
470 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000471 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000472 return;
dand46def72010-07-24 11:28:28 +0000473 }
drh174b9a12010-07-26 11:07:20 +0000474 if( isLookaside(db, p) ){
475 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000476#if SQLITE_DEBUG
477 /* Trash all content in the buffer being freed */
478 memset(p, 0xaa, db->lookaside.sz);
479#endif
drh174b9a12010-07-26 11:07:20 +0000480 pBuf->pNext = db->lookaside.pFree;
481 db->lookaside.pFree = pBuf;
482 db->lookaside.nOut--;
483 return;
484 }
drh633e6d52008-07-28 19:34:53 +0000485 }
drhd231aa32014-10-07 15:46:54 +0000486 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000487 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000488 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
489 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
490 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000491}
492
493/*
drhfec00ea2008-06-14 16:56:21 +0000494** Change the size of an existing memory allocation
495*/
drhda4ca9d2014-09-09 17:27:35 +0000496void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000497 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000498 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000499 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000500 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000501 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000502 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000503 }
drhda4ca9d2014-09-09 17:27:35 +0000504 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000505 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000506 return 0;
507 }
drhb6063cf2009-06-27 00:48:33 +0000508 if( nBytes>=0x7fffff00 ){
509 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
510 return 0;
511 }
drhfec00ea2008-06-14 16:56:21 +0000512 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000513 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
514 ** argument to xRealloc is always a value returned by a prior call to
515 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000516 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000517 if( nOld==nNew ){
518 pNew = pOld;
519 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000520 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000521 sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000522 nDiff = nNew - nOld;
drh5fb72e52015-09-10 01:22:09 +0000523 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
524 mem0.alarmThreshold-nDiff ){
525 sqlite3MallocAlarm(nDiff);
526 }
drh7c6791c2009-08-18 14:48:53 +0000527 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh5fb72e52015-09-10 01:22:09 +0000528 if( pNew==0 && mem0.alarmThreshold>0 ){
529 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000530 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000531 }
532 if( pNew ){
533 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000534 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000535 }
536 sqlite3_mutex_leave(mem0.mutex);
537 }else{
drh7c6791c2009-08-18 14:48:53 +0000538 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000539 }
drh8da47412014-10-03 14:54:47 +0000540 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000541 return pNew;
542}
543
544/*
545** The public interface to sqlite3Realloc. Make sure that the memory
546** subsystem is initialized prior to invoking sqliteRealloc.
547*/
548void *sqlite3_realloc(void *pOld, int n){
549#ifndef SQLITE_OMIT_AUTOINIT
550 if( sqlite3_initialize() ) return 0;
551#endif
drh8da47412014-10-03 14:54:47 +0000552 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000553 return sqlite3Realloc(pOld, n);
554}
555void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
556#ifndef SQLITE_OMIT_AUTOINIT
557 if( sqlite3_initialize() ) return 0;
558#endif
drhfec00ea2008-06-14 16:56:21 +0000559 return sqlite3Realloc(pOld, n);
560}
561
drha3152892007-05-05 11:48:52 +0000562
563/*
drh17435752007-08-16 04:30:38 +0000564** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000565*/
drhda4ca9d2014-09-09 17:27:35 +0000566void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000567 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000568 if( p ){
drh20f3df02014-09-18 02:20:54 +0000569 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000570 }
571 return p;
572}
drh17435752007-08-16 04:30:38 +0000573
574/*
575** Allocate and zero memory. If the allocation fails, make
576** the mallocFailed flag in the connection pointer.
577*/
drhda4ca9d2014-09-09 17:27:35 +0000578void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000579 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000580 if( p ){
drh20f3df02014-09-18 02:20:54 +0000581 memset(p, 0, (size_t)n);
drh17435752007-08-16 04:30:38 +0000582 }
583 return p;
584}
585
586/*
587** Allocate and zero memory. If the allocation fails, make
588** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000589**
590** If db!=0 and db->mallocFailed is true (indicating a prior malloc
591** failure on the same database connection) then always return 0.
592** Hence for a particular database connection, once malloc starts
593** failing, it fails consistently until mallocFailed is reset.
594** This is an important assumption. There are many places in the
595** code that do things like this:
596**
597** int *a = (int*)sqlite3DbMallocRaw(db, 100);
598** int *b = (int*)sqlite3DbMallocRaw(db, 200);
599** if( b ) a[10] = 9;
600**
601** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
602** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000603*/
drhda4ca9d2014-09-09 17:27:35 +0000604void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh633e6d52008-07-28 19:34:53 +0000605 void *p;
drhd9da78a2009-03-24 15:08:09 +0000606 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000607 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000608#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000609 if( db ){
610 LookasideSlot *pBuf;
611 if( db->mallocFailed ){
612 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000613 }
drh0b12e7f2010-12-20 15:51:58 +0000614 if( db->lookaside.bEnabled ){
615 if( n>db->lookaside.sz ){
616 db->lookaside.anStat[1]++;
617 }else if( (pBuf = db->lookaside.pFree)==0 ){
618 db->lookaside.anStat[2]++;
619 }else{
620 db->lookaside.pFree = pBuf->pNext;
621 db->lookaside.nOut++;
622 db->lookaside.anStat[0]++;
623 if( db->lookaside.nOut>db->lookaside.mxOut ){
624 db->lookaside.mxOut = db->lookaside.nOut;
625 }
626 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000627 }
drh633e6d52008-07-28 19:34:53 +0000628 }
629 }
drhddecae72008-10-11 17:35:16 +0000630#else
631 if( db && db->mallocFailed ){
632 return 0;
633 }
drh4150ebf2008-10-11 15:38:29 +0000634#endif
drh633e6d52008-07-28 19:34:53 +0000635 p = sqlite3Malloc(n);
636 if( !p && db ){
637 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000638 }
drhd231aa32014-10-07 15:46:54 +0000639 sqlite3MemdebugSetType(p,
640 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000641 return p;
642}
643
danielk197726783a52007-08-29 14:06:22 +0000644/*
645** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000646** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000647*/
drhda4ca9d2014-09-09 17:27:35 +0000648void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000649 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000650 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000651 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000652 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000653 if( p==0 ){
654 return sqlite3DbMallocRaw(db, n);
655 }
656 if( isLookaside(db, p) ){
657 if( n<=db->lookaside.sz ){
658 return p;
659 }
660 pNew = sqlite3DbMallocRaw(db, n);
661 if( pNew ){
662 memcpy(pNew, p, db->lookaside.sz);
663 sqlite3DbFree(db, p);
664 }
665 }else{
drhd231aa32014-10-07 15:46:54 +0000666 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000667 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000668 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000669 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000670 if( !pNew ){
671 db->mallocFailed = 1;
672 }
drhd231aa32014-10-07 15:46:54 +0000673 sqlite3MemdebugSetType(pNew,
drh174b9a12010-07-26 11:07:20 +0000674 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000675 }
676 }
677 return pNew;
678}
679
drh17435752007-08-16 04:30:38 +0000680/*
681** Attempt to reallocate p. If the reallocation fails, then free p
682** and set the mallocFailed flag in the database connection.
683*/
drhda4ca9d2014-09-09 17:27:35 +0000684void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000685 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000686 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000687 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000688 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000689 }
690 return pNew;
691}
692
drha3152892007-05-05 11:48:52 +0000693/*
694** Make a copy of a string in memory obtained from sqliteMalloc(). These
695** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
696** is because when memory debugging is turned on, these two functions are
697** called via macros that record the current file and line number in the
698** ThreadData structure.
699*/
drh633e6d52008-07-28 19:34:53 +0000700char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000701 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000702 size_t n;
703 if( z==0 ){
704 return 0;
705 }
drhdee0e402009-05-03 20:23:53 +0000706 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000707 assert( (n&0x7fffffff)==n );
708 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000709 if( zNew ){
710 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000711 }
712 return zNew;
713}
drhda4ca9d2014-09-09 17:27:35 +0000714char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000715 char *zNew;
716 if( z==0 ){
717 return 0;
718 }
719 assert( (n&0x7fffffff)==n );
720 zNew = sqlite3DbMallocRaw(db, n+1);
721 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000722 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000723 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000724 }
725 return zNew;
726}
727
drha3152892007-05-05 11:48:52 +0000728/*
drh22c17b82015-05-15 04:13:15 +0000729** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000730*/
drh22c17b82015-05-15 04:13:15 +0000731void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000732 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000733 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000734}
735
drhb50c65d2014-08-23 20:25:53 +0000736/*
737** Take actions at the end of an API call to indicate an OOM error
738*/
739static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
740 db->mallocFailed = 0;
741 sqlite3Error(db, SQLITE_NOMEM);
742 return SQLITE_NOMEM;
743}
drha3152892007-05-05 11:48:52 +0000744
745/*
746** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000747** returning control to the user) that has called sqlite3_malloc or
748** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000749**
750** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000751** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000752** invocation SQLITE_NOMEM is returned instead.
753**
drh597d2b62015-06-30 03:13:47 +0000754** If an OOM as occurred, then the connection error-code (the value
755** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000756*/
drha3152892007-05-05 11:48:52 +0000757int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000758 /* If the db handle must hold the connection handle mutex here.
759 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000760 ** is unsafe, as is the call to sqlite3Error().
761 */
drh597d2b62015-06-30 03:13:47 +0000762 assert( db!=0 );
763 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000764 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
765 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000766 }
drhb50c65d2014-08-23 20:25:53 +0000767 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000768}