blob: 32047a2a5fd0e9bca68f54f48fae765cbbb2cd36 [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));
drh59a05232015-10-15 17:21:35 +0000134 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
danielk1977075c23a2008-09-01 18:34:20 +0000135 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
drh7ff27192010-09-02 18:13:00 +0000136 && sqlite3GlobalConfig.nScratch>0 ){
drhbadc9802010-08-27 17:16:44 +0000137 int i, n, sz;
138 ScratchFreeslot *pSlot;
139 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
140 sqlite3GlobalConfig.szScratch = sz;
141 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
142 n = sqlite3GlobalConfig.nScratch;
143 mem0.pScratchFree = pSlot;
144 mem0.nScratchFree = n;
145 for(i=0; i<n-1; i++){
146 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
147 pSlot = pSlot->pNext;
148 }
149 pSlot->pNext = 0;
150 mem0.pScratchEnd = (void*)&pSlot[1];
drh9ac3fe92008-06-18 18:12:04 +0000151 }else{
drhbadc9802010-08-27 17:16:44 +0000152 mem0.pScratchEnd = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000153 sqlite3GlobalConfig.pScratch = 0;
154 sqlite3GlobalConfig.szScratch = 0;
drhbadc9802010-08-27 17:16:44 +0000155 sqlite3GlobalConfig.nScratch = 0;
drh9ac3fe92008-06-18 18:12:04 +0000156 }
drh50d1b5f2010-08-27 12:21:06 +0000157 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000158 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000159 sqlite3GlobalConfig.pPage = 0;
160 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000161 }
drh592f0cb2015-03-26 17:04:23 +0000162 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
163 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
164 return rc;
drhfec00ea2008-06-14 16:56:21 +0000165}
166
167/*
drh50d1b5f2010-08-27 12:21:06 +0000168** Return true if the heap is currently under memory pressure - in other
169** words if the amount of heap used is close to the limit set by
170** sqlite3_soft_heap_limit().
171*/
172int sqlite3HeapNearlyFull(void){
173 return mem0.nearlyFull;
174}
175
176/*
drhfec00ea2008-06-14 16:56:21 +0000177** Deinitialize the memory allocation subsystem.
178*/
179void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000180 if( sqlite3GlobalConfig.m.xShutdown ){
181 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
182 }
drh9ac3fe92008-06-18 18:12:04 +0000183 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000184}
185
186/*
187** Return the amount of memory currently checked out.
188*/
189sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000190 sqlite3_int64 res, mx;
191 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000192 return res;
drhfec00ea2008-06-14 16:56:21 +0000193}
194
195/*
196** Return the maximum amount of memory that has ever been
197** checked out since either the beginning of this process
198** or since the most recent reset.
199*/
200sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000201 sqlite3_int64 res, mx;
202 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
203 return mx;
drhfec00ea2008-06-14 16:56:21 +0000204}
205
206/*
drh5fb72e52015-09-10 01:22:09 +0000207** Trigger the alarm
208*/
209static void sqlite3MallocAlarm(int nByte){
210 if( mem0.alarmThreshold<=0 ) return;
211 sqlite3_mutex_leave(mem0.mutex);
212 sqlite3_release_memory(nByte);
213 sqlite3_mutex_enter(mem0.mutex);
214}
215
216/*
drhf7141992008-06-19 00:16:08 +0000217** Do a memory allocation with statistics and alarms. Assume the
218** lock is already held.
219*/
220static int mallocWithAlarm(int n, void **pp){
221 int nFull;
222 void *p;
223 assert( sqlite3_mutex_held(mem0.mutex) );
danielk1977075c23a2008-09-01 18:34:20 +0000224 nFull = sqlite3GlobalConfig.m.xRoundup(n);
drhb02392e2015-10-15 15:28:56 +0000225 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
drh5fb72e52015-09-10 01:22:09 +0000226 if( mem0.alarmThreshold>0 ){
227 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
228 if( nUsed >= mem0.alarmThreshold - nFull ){
229 mem0.nearlyFull = 1;
230 sqlite3MallocAlarm(nFull);
231 }else{
232 mem0.nearlyFull = 0;
233 }
234 }
danielk1977075c23a2008-09-01 18:34:20 +0000235 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000236#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000237 if( p==0 && mem0.alarmThreshold>0 ){
238 sqlite3MallocAlarm(nFull);
danielk1977075c23a2008-09-01 18:34:20 +0000239 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000240 }
drh50d1b5f2010-08-27 12:21:06 +0000241#endif
drhc702c7c2008-07-18 18:56:16 +0000242 if( p ){
243 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000244 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
245 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000246 }
drhf7141992008-06-19 00:16:08 +0000247 *pp = p;
248 return nFull;
249}
drhfec00ea2008-06-14 16:56:21 +0000250
251/*
252** Allocate memory. This routine is like sqlite3_malloc() except that it
253** assumes the memory subsystem has already been initialized.
254*/
drhda4ca9d2014-09-09 17:27:35 +0000255void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000256 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000257 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000258 /* A memory allocation of a number of bytes which is near the maximum
259 ** signed integer value might cause an integer overflow inside of the
260 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
261 ** 255 bytes of overhead. SQLite itself will never use anything near
262 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000263 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000264 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000265 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000266 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000267 sqlite3_mutex_leave(mem0.mutex);
268 }else{
drhda4ca9d2014-09-09 17:27:35 +0000269 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000270 }
drh8da47412014-10-03 14:54:47 +0000271 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000272 return p;
273}
274
275/*
276** This version of the memory allocation is for use by the application.
277** First make sure the memory subsystem is initialized, then do the
278** allocation.
279*/
280void *sqlite3_malloc(int n){
281#ifndef SQLITE_OMIT_AUTOINIT
282 if( sqlite3_initialize() ) return 0;
283#endif
drhda4ca9d2014-09-09 17:27:35 +0000284 return n<=0 ? 0 : sqlite3Malloc(n);
285}
286void *sqlite3_malloc64(sqlite3_uint64 n){
287#ifndef SQLITE_OMIT_AUTOINIT
288 if( sqlite3_initialize() ) return 0;
289#endif
drhfec00ea2008-06-14 16:56:21 +0000290 return sqlite3Malloc(n);
291}
292
293/*
drhe5ae5732008-06-15 02:51:47 +0000294** Each thread may only have a single outstanding allocation from
drhfacf0302008-06-17 15:12:00 +0000295** xScratchMalloc(). We verify this constraint in the single-threaded
296** case by setting scratchAllocOut to 1 when an allocation
drhe5ae5732008-06-15 02:51:47 +0000297** is outstanding clearing it when the allocation is freed.
298*/
299#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhfacf0302008-06-17 15:12:00 +0000300static int scratchAllocOut = 0;
drhe5ae5732008-06-15 02:51:47 +0000301#endif
302
303
304/*
305** Allocate memory that is to be used and released right away.
306** This routine is similar to alloca() in that it is not intended
307** for situations where the memory might be held long-term. This
308** routine is intended to get memory to old large transient data
309** structures that would not normally fit on the stack of an
310** embedded processor.
311*/
drhfacf0302008-06-17 15:12:00 +0000312void *sqlite3ScratchMalloc(int n){
drhe5ae5732008-06-15 02:51:47 +0000313 void *p;
314 assert( n>0 );
drh9ac3fe92008-06-18 18:12:04 +0000315
drhbadc9802010-08-27 17:16:44 +0000316 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000317 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
drhbadc9802010-08-27 17:16:44 +0000318 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
319 p = mem0.pScratchFree;
320 mem0.pScratchFree = mem0.pScratchFree->pNext;
321 mem0.nScratchFree--;
drhaf89fe62015-03-23 17:25:18 +0000322 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
danb0c6a882010-09-02 10:08:41 +0000323 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000324 }else{
drh3ccd5bf2014-08-23 19:04:55 +0000325 sqlite3_mutex_leave(mem0.mutex);
326 p = sqlite3Malloc(n);
327 if( sqlite3GlobalConfig.bMemstat && p ){
328 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000329 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
danb0c6a882010-09-02 10:08:41 +0000330 sqlite3_mutex_leave(mem0.mutex);
drhbadc9802010-08-27 17:16:44 +0000331 }
332 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
333 }
drh1ff6e3a2010-09-02 17:15:19 +0000334 assert( sqlite3_mutex_notheld(mem0.mutex) );
danb0c6a882010-09-02 10:08:41 +0000335
drhbadc9802010-08-27 17:16:44 +0000336
drhe5ae5732008-06-15 02:51:47 +0000337#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
drhcbd55b02014-11-04 14:22:27 +0000338 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
339 ** buffers per thread.
340 **
341 ** This can only be checked in single-threaded mode.
342 */
343 assert( scratchAllocOut==0 );
drhbadc9802010-08-27 17:16:44 +0000344 if( p ) scratchAllocOut++;
drhf7141992008-06-19 00:16:08 +0000345#endif
346
drhe5ae5732008-06-15 02:51:47 +0000347 return p;
348}
drhfacf0302008-06-17 15:12:00 +0000349void sqlite3ScratchFree(void *p){
drhe5ae5732008-06-15 02:51:47 +0000350 if( p ){
drhbadc9802010-08-27 17:16:44 +0000351
352#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
353 /* Verify that no more than two scratch allocation per thread
354 ** is outstanding at one time. (This is only checked in the
355 ** single-threaded case since checking in the multi-threaded case
356 ** would be much more complicated.) */
357 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
358 scratchAllocOut--;
359#endif
360
361 if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
362 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
363 ScratchFreeslot *pSlot;
364 pSlot = (ScratchFreeslot*)p;
365 sqlite3_mutex_enter(mem0.mutex);
366 pSlot->pNext = mem0.pScratchFree;
367 mem0.pScratchFree = pSlot;
368 mem0.nScratchFree++;
drhfcd71b62011-04-05 22:08:24 +0000369 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
drhaf89fe62015-03-23 17:25:18 +0000370 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
drhbadc9802010-08-27 17:16:44 +0000371 sqlite3_mutex_leave(mem0.mutex);
372 }else{
373 /* Release memory back to the heap */
drh107b56e2010-03-12 16:32:53 +0000374 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
mistachkind4258642015-03-21 23:38:59 +0000375 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
drh107b56e2010-03-12 16:32:53 +0000376 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
danielk1977075c23a2008-09-01 18:34:20 +0000377 if( sqlite3GlobalConfig.bMemstat ){
drhf7141992008-06-19 00:16:08 +0000378 int iSize = sqlite3MallocSize(p);
379 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000380 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
381 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
382 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000383 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000384 sqlite3_mutex_leave(mem0.mutex);
385 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000386 sqlite3GlobalConfig.m.xFree(p);
drhf7141992008-06-19 00:16:08 +0000387 }
drh9ac3fe92008-06-18 18:12:04 +0000388 }
drhe5ae5732008-06-15 02:51:47 +0000389 }
390}
391
392/*
drh633e6d52008-07-28 19:34:53 +0000393** TRUE if p is a lookaside memory allocation from db
394*/
drh4150ebf2008-10-11 15:38:29 +0000395#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000396static int isLookaside(sqlite3 *db, void *p){
drhb0e77042013-12-10 19:49:00 +0000397 return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
drh633e6d52008-07-28 19:34:53 +0000398}
drh4150ebf2008-10-11 15:38:29 +0000399#else
400#define isLookaside(A,B) 0
401#endif
drh633e6d52008-07-28 19:34:53 +0000402
403/*
drhfec00ea2008-06-14 16:56:21 +0000404** Return the size of a memory allocation previously obtained from
405** sqlite3Malloc() or sqlite3_malloc().
406*/
407int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000408 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000409 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000410}
drh633e6d52008-07-28 19:34:53 +0000411int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh039ca6a2015-10-15 16:20:57 +0000412 assert( p!=0 );
drh054bbab2015-09-01 20:09:33 +0000413 if( db==0 || !isLookaside(db,p) ){
414#if SQLITE_DEBUG
415 if( db==0 ){
416 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
417 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000418 }else{
drhd231aa32014-10-07 15:46:54 +0000419 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000420 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000421 }
drh054bbab2015-09-01 20:09:33 +0000422#endif
423 return sqlite3GlobalConfig.m.xSize(p);
424 }else{
425 assert( sqlite3_mutex_held(db->mutex) );
426 return db->lookaside.sz;
drh633e6d52008-07-28 19:34:53 +0000427 }
428}
drhda4ca9d2014-09-09 17:27:35 +0000429sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000430 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000431 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh039ca6a2015-10-15 16:20:57 +0000432 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
drhda4ca9d2014-09-09 17:27:35 +0000433}
drhfec00ea2008-06-14 16:56:21 +0000434
435/*
436** Free memory previously obtained from sqlite3Malloc().
437*/
438void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000439 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000440 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000441 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000442 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000443 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000444 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
445 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000446 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000447 sqlite3_mutex_leave(mem0.mutex);
448 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000449 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000450 }
451}
452
453/*
drhb4586f12014-08-23 19:42:06 +0000454** Add the size of memory allocation "p" to the count in
455** *db->pnBytesFreed.
456*/
457static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
drh039ca6a2015-10-15 16:20:57 +0000458 if( p ) *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
drhb4586f12014-08-23 19:42:06 +0000459}
460
461/*
drh633e6d52008-07-28 19:34:53 +0000462** Free memory that might be associated with a particular database
463** connection.
464*/
465void sqlite3DbFree(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000466 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drh9ccd8652013-09-13 16:36:46 +0000467 if( p==0 ) return;
drh174b9a12010-07-26 11:07:20 +0000468 if( db ){
469 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000470 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000471 return;
dand46def72010-07-24 11:28:28 +0000472 }
drh174b9a12010-07-26 11:07:20 +0000473 if( isLookaside(db, p) ){
474 LookasideSlot *pBuf = (LookasideSlot*)p;
drh3608f172012-05-21 16:59:16 +0000475#if SQLITE_DEBUG
476 /* Trash all content in the buffer being freed */
477 memset(p, 0xaa, db->lookaside.sz);
478#endif
drh174b9a12010-07-26 11:07:20 +0000479 pBuf->pNext = db->lookaside.pFree;
480 db->lookaside.pFree = pBuf;
481 db->lookaside.nOut--;
482 return;
483 }
drh633e6d52008-07-28 19:34:53 +0000484 }
drhd231aa32014-10-07 15:46:54 +0000485 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000486 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000487 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
488 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
489 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000490}
491
492/*
drhfec00ea2008-06-14 16:56:21 +0000493** Change the size of an existing memory allocation
494*/
drhda4ca9d2014-09-09 17:27:35 +0000495void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000496 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000497 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000498 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000499 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000500 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000501 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000502 }
drhda4ca9d2014-09-09 17:27:35 +0000503 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000504 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000505 return 0;
506 }
drhb6063cf2009-06-27 00:48:33 +0000507 if( nBytes>=0x7fffff00 ){
508 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
509 return 0;
510 }
drhfec00ea2008-06-14 16:56:21 +0000511 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000512 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
513 ** argument to xRealloc is always a value returned by a prior call to
514 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000515 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000516 if( nOld==nNew ){
517 pNew = pOld;
518 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000519 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000520 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000521 nDiff = nNew - nOld;
drh5fb72e52015-09-10 01:22:09 +0000522 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
523 mem0.alarmThreshold-nDiff ){
524 sqlite3MallocAlarm(nDiff);
525 }
drh7c6791c2009-08-18 14:48:53 +0000526 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh5fb72e52015-09-10 01:22:09 +0000527 if( pNew==0 && mem0.alarmThreshold>0 ){
528 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000529 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000530 }
531 if( pNew ){
532 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000533 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000534 }
535 sqlite3_mutex_leave(mem0.mutex);
536 }else{
drh7c6791c2009-08-18 14:48:53 +0000537 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000538 }
drh8da47412014-10-03 14:54:47 +0000539 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000540 return pNew;
541}
542
543/*
544** The public interface to sqlite3Realloc. Make sure that the memory
545** subsystem is initialized prior to invoking sqliteRealloc.
546*/
547void *sqlite3_realloc(void *pOld, int n){
548#ifndef SQLITE_OMIT_AUTOINIT
549 if( sqlite3_initialize() ) return 0;
550#endif
drh8da47412014-10-03 14:54:47 +0000551 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000552 return sqlite3Realloc(pOld, n);
553}
554void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
555#ifndef SQLITE_OMIT_AUTOINIT
556 if( sqlite3_initialize() ) return 0;
557#endif
drhfec00ea2008-06-14 16:56:21 +0000558 return sqlite3Realloc(pOld, n);
559}
560
drha3152892007-05-05 11:48:52 +0000561
562/*
drh17435752007-08-16 04:30:38 +0000563** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000564*/
drhda4ca9d2014-09-09 17:27:35 +0000565void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000566 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000567 if( p ){
drh20f3df02014-09-18 02:20:54 +0000568 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000569 }
570 return p;
571}
drh17435752007-08-16 04:30:38 +0000572
573/*
574** Allocate and zero memory. If the allocation fails, make
575** the mallocFailed flag in the connection pointer.
576*/
drhda4ca9d2014-09-09 17:27:35 +0000577void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000578 void *p = sqlite3DbMallocRaw(db, n);
drh17435752007-08-16 04:30:38 +0000579 if( p ){
drh20f3df02014-09-18 02:20:54 +0000580 memset(p, 0, (size_t)n);
drh17435752007-08-16 04:30:38 +0000581 }
582 return p;
583}
584
585/*
586** Allocate and zero memory. If the allocation fails, make
587** the mallocFailed flag in the connection pointer.
drhddecae72008-10-11 17:35:16 +0000588**
589** If db!=0 and db->mallocFailed is true (indicating a prior malloc
590** failure on the same database connection) then always return 0.
591** Hence for a particular database connection, once malloc starts
592** failing, it fails consistently until mallocFailed is reset.
593** This is an important assumption. There are many places in the
594** code that do things like this:
595**
596** int *a = (int*)sqlite3DbMallocRaw(db, 100);
597** int *b = (int*)sqlite3DbMallocRaw(db, 200);
598** if( b ) a[10] = 9;
599**
600** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
601** that all prior mallocs (ex: "a") worked too.
drh17435752007-08-16 04:30:38 +0000602*/
drhda4ca9d2014-09-09 17:27:35 +0000603void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh633e6d52008-07-28 19:34:53 +0000604 void *p;
drhd9da78a2009-03-24 15:08:09 +0000605 assert( db==0 || sqlite3_mutex_held(db->mutex) );
danccd4ad32010-07-26 14:47:14 +0000606 assert( db==0 || db->pnBytesFreed==0 );
drh4150ebf2008-10-11 15:38:29 +0000607#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000608 if( db ){
609 LookasideSlot *pBuf;
610 if( db->mallocFailed ){
611 return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000612 }
drh0b12e7f2010-12-20 15:51:58 +0000613 if( db->lookaside.bEnabled ){
614 if( n>db->lookaside.sz ){
615 db->lookaside.anStat[1]++;
616 }else if( (pBuf = db->lookaside.pFree)==0 ){
617 db->lookaside.anStat[2]++;
618 }else{
619 db->lookaside.pFree = pBuf->pNext;
620 db->lookaside.nOut++;
621 db->lookaside.anStat[0]++;
622 if( db->lookaside.nOut>db->lookaside.mxOut ){
623 db->lookaside.mxOut = db->lookaside.nOut;
624 }
625 return (void*)pBuf;
drh633e6d52008-07-28 19:34:53 +0000626 }
drh633e6d52008-07-28 19:34:53 +0000627 }
628 }
drhddecae72008-10-11 17:35:16 +0000629#else
630 if( db && db->mallocFailed ){
631 return 0;
632 }
drh4150ebf2008-10-11 15:38:29 +0000633#endif
drh633e6d52008-07-28 19:34:53 +0000634 p = sqlite3Malloc(n);
635 if( !p && db ){
636 db->mallocFailed = 1;
drh17435752007-08-16 04:30:38 +0000637 }
drhd231aa32014-10-07 15:46:54 +0000638 sqlite3MemdebugSetType(p,
639 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000640 return p;
641}
642
danielk197726783a52007-08-29 14:06:22 +0000643/*
644** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000645** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000646*/
drhda4ca9d2014-09-09 17:27:35 +0000647void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000648 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000649 assert( db!=0 );
drh7047e252009-03-23 17:49:14 +0000650 assert( sqlite3_mutex_held(db->mutex) );
danielk1977a1644fd2007-08-29 12:31:25 +0000651 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000652 if( p==0 ){
653 return sqlite3DbMallocRaw(db, n);
654 }
655 if( isLookaside(db, p) ){
656 if( n<=db->lookaside.sz ){
657 return p;
658 }
659 pNew = sqlite3DbMallocRaw(db, n);
660 if( pNew ){
661 memcpy(pNew, p, db->lookaside.sz);
662 sqlite3DbFree(db, p);
663 }
664 }else{
drhd231aa32014-10-07 15:46:54 +0000665 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000666 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000667 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000668 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000669 if( !pNew ){
670 db->mallocFailed = 1;
671 }
drhd231aa32014-10-07 15:46:54 +0000672 sqlite3MemdebugSetType(pNew,
drh174b9a12010-07-26 11:07:20 +0000673 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000674 }
675 }
676 return pNew;
677}
678
drh17435752007-08-16 04:30:38 +0000679/*
680** Attempt to reallocate p. If the reallocation fails, then free p
681** and set the mallocFailed flag in the database connection.
682*/
drhda4ca9d2014-09-09 17:27:35 +0000683void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000684 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000685 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000686 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000687 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000688 }
689 return pNew;
690}
691
drha3152892007-05-05 11:48:52 +0000692/*
693** Make a copy of a string in memory obtained from sqliteMalloc(). These
694** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
695** is because when memory debugging is turned on, these two functions are
696** called via macros that record the current file and line number in the
697** ThreadData structure.
698*/
drh633e6d52008-07-28 19:34:53 +0000699char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000700 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000701 size_t n;
702 if( z==0 ){
703 return 0;
704 }
drhdee0e402009-05-03 20:23:53 +0000705 n = sqlite3Strlen30(z) + 1;
drh633e6d52008-07-28 19:34:53 +0000706 assert( (n&0x7fffffff)==n );
707 zNew = sqlite3DbMallocRaw(db, (int)n);
drha3152892007-05-05 11:48:52 +0000708 if( zNew ){
709 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000710 }
711 return zNew;
712}
drhda4ca9d2014-09-09 17:27:35 +0000713char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000714 char *zNew;
715 if( z==0 ){
716 return 0;
717 }
718 assert( (n&0x7fffffff)==n );
719 zNew = sqlite3DbMallocRaw(db, n+1);
720 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000721 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000722 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000723 }
724 return zNew;
725}
726
drha3152892007-05-05 11:48:52 +0000727/*
drh22c17b82015-05-15 04:13:15 +0000728** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000729*/
drh22c17b82015-05-15 04:13:15 +0000730void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000731 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000732 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000733}
734
drhb50c65d2014-08-23 20:25:53 +0000735/*
736** Take actions at the end of an API call to indicate an OOM error
737*/
738static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
739 db->mallocFailed = 0;
740 sqlite3Error(db, SQLITE_NOMEM);
741 return SQLITE_NOMEM;
742}
drha3152892007-05-05 11:48:52 +0000743
744/*
745** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000746** returning control to the user) that has called sqlite3_malloc or
747** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000748**
749** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000750** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000751** invocation SQLITE_NOMEM is returned instead.
752**
drh597d2b62015-06-30 03:13:47 +0000753** If an OOM as occurred, then the connection error-code (the value
754** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000755*/
drha3152892007-05-05 11:48:52 +0000756int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000757 /* If the db handle must hold the connection handle mutex here.
758 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000759 ** is unsafe, as is the call to sqlite3Error().
760 */
drh597d2b62015-06-30 03:13:47 +0000761 assert( db!=0 );
762 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000763 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
764 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000765 }
drhb50c65d2014-08-23 20:25:53 +0000766 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000767}