blob: d7f9df5efc83b4e64f2045b7225c57a53029f519 [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/*
36** State information local to the memory allocation subsystem.
37*/
danielk19775c8f8582008-09-02 10:22:00 +000038static SQLITE_WSD struct Mem0Global {
drhfec00ea2008-06-14 16:56:21 +000039 sqlite3_mutex *mutex; /* Mutex to serialize access */
drh5fb72e52015-09-10 01:22:09 +000040 sqlite3_int64 alarmThreshold; /* The soft heap limit */
drhfec00ea2008-06-14 16:56:21 +000041
42 /*
drh50d1b5f2010-08-27 12:21:06 +000043 ** True if heap is nearly "full" where "full" is defined by the
44 ** sqlite3_soft_heap_limit() setting.
45 */
46 int nearlyFull;
drhb2a0f752017-08-28 15:51:35 +000047} mem0 = { 0, 0, 0 };
danielk19775c8f8582008-09-02 10:22:00 +000048
49#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +000050
51/*
drhaf89fe62015-03-23 17:25:18 +000052** Return the memory allocator mutex. sqlite3_status() needs it.
53*/
54sqlite3_mutex *sqlite3MallocMutex(void){
55 return mem0.mutex;
56}
57
drhf82ccf62010-09-15 17:54:31 +000058#ifndef SQLITE_OMIT_DEPRECATED
59/*
drh5fb72e52015-09-10 01:22:09 +000060** Deprecated external interface. It used to set an alarm callback
61** that was invoked when memory usage grew too large. Now it is a
62** no-op.
drhf82ccf62010-09-15 17:54:31 +000063*/
64int sqlite3_memory_alarm(
65 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
66 void *pArg,
67 sqlite3_int64 iThreshold
68){
drh5fb72e52015-09-10 01:22:09 +000069 (void)xCallback;
70 (void)pArg;
71 (void)iThreshold;
drh4ef299a2015-09-02 14:56:56 +000072 return SQLITE_OK;
drhf82ccf62010-09-15 17:54:31 +000073}
74#endif
75
76/*
77** Set the soft heap-size limit for the library. Passing a zero or
78** negative value indicates no limit.
79*/
80sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
81 sqlite3_int64 priorLimit;
drh5fb72e52015-09-10 01:22:09 +000082 sqlite3_int64 excess;
83 sqlite3_int64 nUsed;
drhf82ccf62010-09-15 17:54:31 +000084#ifndef SQLITE_OMIT_AUTOINIT
drhde0f1812011-12-22 17:10:35 +000085 int rc = sqlite3_initialize();
86 if( rc ) return -1;
drhf82ccf62010-09-15 17:54:31 +000087#endif
88 sqlite3_mutex_enter(mem0.mutex);
89 priorLimit = mem0.alarmThreshold;
drh5fb72e52015-09-10 01:22:09 +000090 if( n<0 ){
91 sqlite3_mutex_leave(mem0.mutex);
92 return priorLimit;
drhf82ccf62010-09-15 17:54:31 +000093 }
drh5fb72e52015-09-10 01:22:09 +000094 mem0.alarmThreshold = n;
95 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
96 mem0.nearlyFull = (n>0 && n<=nUsed);
drh4ef299a2015-09-02 14:56:56 +000097 sqlite3_mutex_leave(mem0.mutex);
drh5fb72e52015-09-10 01:22:09 +000098 excess = sqlite3_memory_used() - n;
99 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
drhf82ccf62010-09-15 17:54:31 +0000100 return priorLimit;
101}
102void sqlite3_soft_heap_limit(int n){
103 if( n<0 ) n = 0;
104 sqlite3_soft_heap_limit64(n);
105}
106
107/*
drhfec00ea2008-06-14 16:56:21 +0000108** Initialize the memory allocation subsystem.
109*/
110int sqlite3MallocInit(void){
drh592f0cb2015-03-26 17:04:23 +0000111 int rc;
danielk1977075c23a2008-09-01 18:34:20 +0000112 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000113 sqlite3MemSetDefault();
114 }
115 memset(&mem0, 0, sizeof(mem0));
drh59a05232015-10-15 17:21:35 +0000116 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drh50d1b5f2010-08-27 12:21:06 +0000117 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000118 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000119 sqlite3GlobalConfig.pPage = 0;
120 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000121 }
drh592f0cb2015-03-26 17:04:23 +0000122 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
123 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
124 return rc;
drhfec00ea2008-06-14 16:56:21 +0000125}
126
127/*
drh50d1b5f2010-08-27 12:21:06 +0000128** Return true if the heap is currently under memory pressure - in other
129** words if the amount of heap used is close to the limit set by
130** sqlite3_soft_heap_limit().
131*/
132int sqlite3HeapNearlyFull(void){
133 return mem0.nearlyFull;
134}
135
136/*
drhfec00ea2008-06-14 16:56:21 +0000137** Deinitialize the memory allocation subsystem.
138*/
139void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000140 if( sqlite3GlobalConfig.m.xShutdown ){
141 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
142 }
drh9ac3fe92008-06-18 18:12:04 +0000143 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000144}
145
146/*
147** Return the amount of memory currently checked out.
148*/
149sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000150 sqlite3_int64 res, mx;
151 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000152 return res;
drhfec00ea2008-06-14 16:56:21 +0000153}
154
155/*
156** Return the maximum amount of memory that has ever been
157** checked out since either the beginning of this process
158** or since the most recent reset.
159*/
160sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000161 sqlite3_int64 res, mx;
162 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
163 return mx;
drhfec00ea2008-06-14 16:56:21 +0000164}
165
166/*
drh5fb72e52015-09-10 01:22:09 +0000167** Trigger the alarm
168*/
169static void sqlite3MallocAlarm(int nByte){
170 if( mem0.alarmThreshold<=0 ) return;
171 sqlite3_mutex_leave(mem0.mutex);
172 sqlite3_release_memory(nByte);
173 sqlite3_mutex_enter(mem0.mutex);
174}
175
176/*
drhf7141992008-06-19 00:16:08 +0000177** Do a memory allocation with statistics and alarms. Assume the
178** lock is already held.
179*/
drh1d21bac2017-01-10 16:09:46 +0000180static void mallocWithAlarm(int n, void **pp){
drhf7141992008-06-19 00:16:08 +0000181 void *p;
drh087a29c2017-02-08 16:01:57 +0000182 int nFull;
drhf7141992008-06-19 00:16:08 +0000183 assert( sqlite3_mutex_held(mem0.mutex) );
drh087a29c2017-02-08 16:01:57 +0000184 assert( n>0 );
185
mistachkin40b84362017-02-08 18:13:46 +0000186 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
drh087a29c2017-02-08 16:01:57 +0000187 ** implementation of malloc_good_size(), which must be called in debug
188 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
mistachkin40b84362017-02-08 18:13:46 +0000189 ** or else a crash results. Hence, do not attempt to optimize out the
190 ** following xRoundup() call. */
drh087a29c2017-02-08 16:01:57 +0000191 nFull = sqlite3GlobalConfig.m.xRoundup(n);
192
drhe43d6ae2017-03-10 15:55:54 +0000193#ifdef SQLITE_MAX_MEMORY
194 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
drha6bf20b2017-03-10 17:03:11 +0000195 *pp = 0;
196 return;
drhe43d6ae2017-03-10 15:55:54 +0000197 }
198#endif
199
drhb02392e2015-10-15 15:28:56 +0000200 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
drh5fb72e52015-09-10 01:22:09 +0000201 if( mem0.alarmThreshold>0 ){
202 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
203 if( nUsed >= mem0.alarmThreshold - nFull ){
204 mem0.nearlyFull = 1;
205 sqlite3MallocAlarm(nFull);
206 }else{
207 mem0.nearlyFull = 0;
208 }
209 }
drh087a29c2017-02-08 16:01:57 +0000210 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000211#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000212 if( p==0 && mem0.alarmThreshold>0 ){
213 sqlite3MallocAlarm(nFull);
drh087a29c2017-02-08 16:01:57 +0000214 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000215 }
drh50d1b5f2010-08-27 12:21:06 +0000216#endif
drhc702c7c2008-07-18 18:56:16 +0000217 if( p ){
drhbe7a0ce2017-01-13 12:53:35 +0000218 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000219 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
220 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000221 }
drhf7141992008-06-19 00:16:08 +0000222 *pp = p;
drhf7141992008-06-19 00:16:08 +0000223}
drhfec00ea2008-06-14 16:56:21 +0000224
225/*
226** Allocate memory. This routine is like sqlite3_malloc() except that it
227** assumes the memory subsystem has already been initialized.
228*/
drhda4ca9d2014-09-09 17:27:35 +0000229void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000230 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000231 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000232 /* A memory allocation of a number of bytes which is near the maximum
233 ** signed integer value might cause an integer overflow inside of the
234 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
235 ** 255 bytes of overhead. SQLite itself will never use anything near
236 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000237 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000238 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000239 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000240 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000241 sqlite3_mutex_leave(mem0.mutex);
242 }else{
drhda4ca9d2014-09-09 17:27:35 +0000243 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000244 }
drh8da47412014-10-03 14:54:47 +0000245 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000246 return p;
247}
248
249/*
250** This version of the memory allocation is for use by the application.
251** First make sure the memory subsystem is initialized, then do the
252** allocation.
253*/
254void *sqlite3_malloc(int n){
255#ifndef SQLITE_OMIT_AUTOINIT
256 if( sqlite3_initialize() ) return 0;
257#endif
drhda4ca9d2014-09-09 17:27:35 +0000258 return n<=0 ? 0 : sqlite3Malloc(n);
259}
260void *sqlite3_malloc64(sqlite3_uint64 n){
261#ifndef SQLITE_OMIT_AUTOINIT
262 if( sqlite3_initialize() ) return 0;
263#endif
drhfec00ea2008-06-14 16:56:21 +0000264 return sqlite3Malloc(n);
265}
266
267/*
drh633e6d52008-07-28 19:34:53 +0000268** TRUE if p is a lookaside memory allocation from db
269*/
drh4150ebf2008-10-11 15:38:29 +0000270#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000271static int isLookaside(sqlite3 *db, void *p){
drhac536e62015-12-10 15:09:17 +0000272 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
drh633e6d52008-07-28 19:34:53 +0000273}
drh4150ebf2008-10-11 15:38:29 +0000274#else
275#define isLookaside(A,B) 0
276#endif
drh633e6d52008-07-28 19:34:53 +0000277
278/*
drhfec00ea2008-06-14 16:56:21 +0000279** Return the size of a memory allocation previously obtained from
280** sqlite3Malloc() or sqlite3_malloc().
281*/
282int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000283 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000284 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000285}
drh633e6d52008-07-28 19:34:53 +0000286int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh039ca6a2015-10-15 16:20:57 +0000287 assert( p!=0 );
drh054bbab2015-09-01 20:09:33 +0000288 if( db==0 || !isLookaside(db,p) ){
drhd879e3e2017-02-13 13:35:55 +0000289#ifdef SQLITE_DEBUG
drh054bbab2015-09-01 20:09:33 +0000290 if( db==0 ){
291 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
292 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000293 }else{
drhd231aa32014-10-07 15:46:54 +0000294 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000295 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000296 }
drh054bbab2015-09-01 20:09:33 +0000297#endif
298 return sqlite3GlobalConfig.m.xSize(p);
299 }else{
300 assert( sqlite3_mutex_held(db->mutex) );
301 return db->lookaside.sz;
drh633e6d52008-07-28 19:34:53 +0000302 }
303}
drhda4ca9d2014-09-09 17:27:35 +0000304sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000305 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000306 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh039ca6a2015-10-15 16:20:57 +0000307 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
drhda4ca9d2014-09-09 17:27:35 +0000308}
drhfec00ea2008-06-14 16:56:21 +0000309
310/*
311** Free memory previously obtained from sqlite3Malloc().
312*/
313void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000314 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000315 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000316 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000317 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000318 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000319 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
320 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000321 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000322 sqlite3_mutex_leave(mem0.mutex);
323 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000324 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000325 }
326}
327
328/*
drhb4586f12014-08-23 19:42:06 +0000329** Add the size of memory allocation "p" to the count in
330** *db->pnBytesFreed.
331*/
332static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
drh56d90be2015-10-26 12:55:56 +0000333 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
drhb4586f12014-08-23 19:42:06 +0000334}
335
336/*
drh633e6d52008-07-28 19:34:53 +0000337** Free memory that might be associated with a particular database
drhdbd6a7d2017-04-05 12:39:49 +0000338** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
339** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
drh633e6d52008-07-28 19:34:53 +0000340*/
drhdbd6a7d2017-04-05 12:39:49 +0000341void sqlite3DbFreeNN(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000342 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drhdbd6a7d2017-04-05 12:39:49 +0000343 assert( p!=0 );
drh174b9a12010-07-26 11:07:20 +0000344 if( db ){
345 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000346 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000347 return;
dand46def72010-07-24 11:28:28 +0000348 }
drh174b9a12010-07-26 11:07:20 +0000349 if( isLookaside(db, p) ){
350 LookasideSlot *pBuf = (LookasideSlot*)p;
drhd879e3e2017-02-13 13:35:55 +0000351#ifdef SQLITE_DEBUG
drh3608f172012-05-21 16:59:16 +0000352 /* Trash all content in the buffer being freed */
353 memset(p, 0xaa, db->lookaside.sz);
354#endif
drh174b9a12010-07-26 11:07:20 +0000355 pBuf->pNext = db->lookaside.pFree;
356 db->lookaside.pFree = pBuf;
drh174b9a12010-07-26 11:07:20 +0000357 return;
358 }
drh633e6d52008-07-28 19:34:53 +0000359 }
drhd231aa32014-10-07 15:46:54 +0000360 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000361 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000362 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
363 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
364 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000365}
drhdbd6a7d2017-04-05 12:39:49 +0000366void sqlite3DbFree(sqlite3 *db, void *p){
367 assert( db==0 || sqlite3_mutex_held(db->mutex) );
368 if( p ) sqlite3DbFreeNN(db, p);
369}
drh633e6d52008-07-28 19:34:53 +0000370
371/*
drhfec00ea2008-06-14 16:56:21 +0000372** Change the size of an existing memory allocation
373*/
drhda4ca9d2014-09-09 17:27:35 +0000374void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000375 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000376 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000377 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000378 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000379 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000380 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000381 }
drhda4ca9d2014-09-09 17:27:35 +0000382 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000383 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000384 return 0;
385 }
drhb6063cf2009-06-27 00:48:33 +0000386 if( nBytes>=0x7fffff00 ){
387 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
388 return 0;
389 }
drhfec00ea2008-06-14 16:56:21 +0000390 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000391 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
392 ** argument to xRealloc is always a value returned by a prior call to
393 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000394 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000395 if( nOld==nNew ){
396 pNew = pOld;
397 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000398 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000399 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000400 nDiff = nNew - nOld;
drh1aa34692016-12-27 12:08:36 +0000401 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
drh5fb72e52015-09-10 01:22:09 +0000402 mem0.alarmThreshold-nDiff ){
403 sqlite3MallocAlarm(nDiff);
404 }
drh7c6791c2009-08-18 14:48:53 +0000405 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh5fb72e52015-09-10 01:22:09 +0000406 if( pNew==0 && mem0.alarmThreshold>0 ){
407 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000408 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000409 }
410 if( pNew ){
411 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000412 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000413 }
414 sqlite3_mutex_leave(mem0.mutex);
415 }else{
drh7c6791c2009-08-18 14:48:53 +0000416 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000417 }
drh8da47412014-10-03 14:54:47 +0000418 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000419 return pNew;
420}
421
422/*
423** The public interface to sqlite3Realloc. Make sure that the memory
424** subsystem is initialized prior to invoking sqliteRealloc.
425*/
426void *sqlite3_realloc(void *pOld, int n){
427#ifndef SQLITE_OMIT_AUTOINIT
428 if( sqlite3_initialize() ) return 0;
429#endif
drh8da47412014-10-03 14:54:47 +0000430 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000431 return sqlite3Realloc(pOld, n);
432}
433void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
434#ifndef SQLITE_OMIT_AUTOINIT
435 if( sqlite3_initialize() ) return 0;
436#endif
drhfec00ea2008-06-14 16:56:21 +0000437 return sqlite3Realloc(pOld, n);
438}
439
drha3152892007-05-05 11:48:52 +0000440
441/*
drh17435752007-08-16 04:30:38 +0000442** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000443*/
drhda4ca9d2014-09-09 17:27:35 +0000444void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000445 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000446 if( p ){
drh20f3df02014-09-18 02:20:54 +0000447 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000448 }
449 return p;
450}
drh17435752007-08-16 04:30:38 +0000451
452/*
453** Allocate and zero memory. If the allocation fails, make
454** the mallocFailed flag in the connection pointer.
455*/
drhda4ca9d2014-09-09 17:27:35 +0000456void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000457 void *p;
458 testcase( db==0 );
459 p = sqlite3DbMallocRaw(db, n);
460 if( p ) memset(p, 0, (size_t)n);
461 return p;
462}
463
464
465/* Finish the work of sqlite3DbMallocRawNN for the unusual and
466** slower case when the allocation cannot be fulfilled using lookaside.
467*/
468static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
469 void *p;
470 assert( db!=0 );
471 p = sqlite3Malloc(n);
472 if( !p ) sqlite3OomFault(db);
473 sqlite3MemdebugSetType(p,
474 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000475 return p;
476}
477
478/*
drh1da26a42016-01-20 03:36:32 +0000479** Allocate memory, either lookaside (if possible) or heap.
480** If the allocation fails, set the mallocFailed flag in
481** the connection pointer.
drhddecae72008-10-11 17:35:16 +0000482**
483** If db!=0 and db->mallocFailed is true (indicating a prior malloc
484** failure on the same database connection) then always return 0.
485** Hence for a particular database connection, once malloc starts
486** failing, it fails consistently until mallocFailed is reset.
487** This is an important assumption. There are many places in the
488** code that do things like this:
489**
490** int *a = (int*)sqlite3DbMallocRaw(db, 100);
491** int *b = (int*)sqlite3DbMallocRaw(db, 200);
492** if( b ) a[10] = 9;
493**
494** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
495** that all prior mallocs (ex: "a") worked too.
drh575fad62016-02-05 13:38:36 +0000496**
497** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
498** not a NULL pointer.
drh17435752007-08-16 04:30:38 +0000499*/
drhda4ca9d2014-09-09 17:27:35 +0000500void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000501 void *p;
502 if( db ) return sqlite3DbMallocRawNN(db, n);
503 p = sqlite3Malloc(n);
504 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
505 return p;
506}
507void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
drhf5818aa2016-02-06 19:48:50 +0000508#ifndef SQLITE_OMIT_LOOKASIDE
509 LookasideSlot *pBuf;
drh575fad62016-02-05 13:38:36 +0000510 assert( db!=0 );
511 assert( sqlite3_mutex_held(db->mutex) );
512 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000513 if( db->lookaside.bDisable==0 ){
514 assert( db->mallocFailed==0 );
515 if( n>db->lookaside.sz ){
516 db->lookaside.anStat[1]++;
drh52fb8e12017-08-29 20:21:12 +0000517 }else if( (pBuf = db->lookaside.pFree)!=0 ){
drh575fad62016-02-05 13:38:36 +0000518 db->lookaside.pFree = pBuf->pNext;
drh575fad62016-02-05 13:38:36 +0000519 db->lookaside.anStat[0]++;
drh575fad62016-02-05 13:38:36 +0000520 return (void*)pBuf;
drh52fb8e12017-08-29 20:21:12 +0000521 }else if( (pBuf = db->lookaside.pInit)!=0 ){
522 db->lookaside.pInit = pBuf->pNext;
523 db->lookaside.anStat[0]++;
524 return (void*)pBuf;
525 }else{
526 db->lookaside.anStat[2]++;
drh633e6d52008-07-28 19:34:53 +0000527 }
drh575fad62016-02-05 13:38:36 +0000528 }else if( db->mallocFailed ){
529 return 0;
drh633e6d52008-07-28 19:34:53 +0000530 }
drhddecae72008-10-11 17:35:16 +0000531#else
drhf5818aa2016-02-06 19:48:50 +0000532 assert( db!=0 );
533 assert( sqlite3_mutex_held(db->mutex) );
534 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000535 if( db->mallocFailed ){
drhddecae72008-10-11 17:35:16 +0000536 return 0;
537 }
drh4150ebf2008-10-11 15:38:29 +0000538#endif
drh1da26a42016-01-20 03:36:32 +0000539 return dbMallocRawFinish(db, n);
540}
drh17435752007-08-16 04:30:38 +0000541
drhb84e5742016-02-05 02:42:54 +0000542/* Forward declaration */
543static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
544
danielk197726783a52007-08-29 14:06:22 +0000545/*
546** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000547** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000548*/
drhda4ca9d2014-09-09 17:27:35 +0000549void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
drhb84e5742016-02-05 02:42:54 +0000550 assert( db!=0 );
drh575fad62016-02-05 13:38:36 +0000551 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
drhb84e5742016-02-05 02:42:54 +0000552 assert( sqlite3_mutex_held(db->mutex) );
553 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
554 return dbReallocFinish(db, p, n);
555}
556static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000557 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000558 assert( db!=0 );
drhb84e5742016-02-05 02:42:54 +0000559 assert( p!=0 );
danielk1977a1644fd2007-08-29 12:31:25 +0000560 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000561 if( isLookaside(db, p) ){
drh575fad62016-02-05 13:38:36 +0000562 pNew = sqlite3DbMallocRawNN(db, n);
drh633e6d52008-07-28 19:34:53 +0000563 if( pNew ){
564 memcpy(pNew, p, db->lookaside.sz);
565 sqlite3DbFree(db, p);
566 }
567 }else{
drhd231aa32014-10-07 15:46:54 +0000568 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000569 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000570 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000571 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000572 if( !pNew ){
drh4a642b62016-02-05 01:55:27 +0000573 sqlite3OomFault(db);
drh633e6d52008-07-28 19:34:53 +0000574 }
drhd231aa32014-10-07 15:46:54 +0000575 sqlite3MemdebugSetType(pNew,
drh4a642b62016-02-05 01:55:27 +0000576 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000577 }
578 }
579 return pNew;
580}
581
drh17435752007-08-16 04:30:38 +0000582/*
583** Attempt to reallocate p. If the reallocation fails, then free p
584** and set the mallocFailed flag in the database connection.
585*/
drhda4ca9d2014-09-09 17:27:35 +0000586void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000587 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000588 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000589 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000590 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000591 }
592 return pNew;
593}
594
drha3152892007-05-05 11:48:52 +0000595/*
596** Make a copy of a string in memory obtained from sqliteMalloc(). These
597** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
598** is because when memory debugging is turned on, these two functions are
599** called via macros that record the current file and line number in the
600** ThreadData structure.
601*/
drh633e6d52008-07-28 19:34:53 +0000602char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000603 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000604 size_t n;
605 if( z==0 ){
606 return 0;
607 }
drhcee11ad2016-10-17 00:48:06 +0000608 n = strlen(z) + 1;
609 zNew = sqlite3DbMallocRaw(db, n);
drha3152892007-05-05 11:48:52 +0000610 if( zNew ){
611 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000612 }
613 return zNew;
614}
drhda4ca9d2014-09-09 17:27:35 +0000615char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000616 char *zNew;
drh575fad62016-02-05 13:38:36 +0000617 assert( db!=0 );
drh633e6d52008-07-28 19:34:53 +0000618 if( z==0 ){
619 return 0;
620 }
621 assert( (n&0x7fffffff)==n );
drh575fad62016-02-05 13:38:36 +0000622 zNew = sqlite3DbMallocRawNN(db, n+1);
drh633e6d52008-07-28 19:34:53 +0000623 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000624 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000625 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000626 }
627 return zNew;
628}
629
drha3152892007-05-05 11:48:52 +0000630/*
drh9b2e0432017-12-27 19:43:22 +0000631** The text between zStart and zEnd represents a phrase within a larger
632** SQL statement. Make a copy of this phrase in space obtained form
633** sqlite3DbMalloc(). Omit leading and trailing whitespace.
634*/
635char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
636 int n;
637 while( sqlite3Isspace(zStart[0]) ) zStart++;
638 n = (int)(zEnd - zStart);
drhe75d1f52018-01-10 13:58:23 +0000639 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
drh9b2e0432017-12-27 19:43:22 +0000640 return sqlite3DbStrNDup(db, zStart, n);
641}
642
643/*
drh22c17b82015-05-15 04:13:15 +0000644** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000645*/
drh22c17b82015-05-15 04:13:15 +0000646void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000647 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000648 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000649}
650
drhb50c65d2014-08-23 20:25:53 +0000651/*
drh4a642b62016-02-05 01:55:27 +0000652** Call this routine to record the fact that an OOM (out-of-memory) error
653** has happened. This routine will set db->mallocFailed, and also
654** temporarily disable the lookaside memory allocator and interrupt
655** any running VDBEs.
656*/
657void sqlite3OomFault(sqlite3 *db){
658 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
659 db->mallocFailed = 1;
660 if( db->nVdbeExec>0 ){
661 db->u1.isInterrupted = 1;
662 }
663 db->lookaside.bDisable++;
664 }
665}
666
667/*
668** This routine reactivates the memory allocator and clears the
669** db->mallocFailed flag as necessary.
670**
671** The memory allocator is not restarted if there are running
672** VDBEs.
673*/
674void sqlite3OomClear(sqlite3 *db){
675 if( db->mallocFailed && db->nVdbeExec==0 ){
676 db->mallocFailed = 0;
677 db->u1.isInterrupted = 0;
678 assert( db->lookaside.bDisable>0 );
679 db->lookaside.bDisable--;
680 }
681}
682
683/*
drhb50c65d2014-08-23 20:25:53 +0000684** Take actions at the end of an API call to indicate an OOM error
685*/
686static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
drh4a642b62016-02-05 01:55:27 +0000687 sqlite3OomClear(db);
drhb50c65d2014-08-23 20:25:53 +0000688 sqlite3Error(db, SQLITE_NOMEM);
mistachkinfad30392016-02-13 23:43:46 +0000689 return SQLITE_NOMEM_BKPT;
drhb50c65d2014-08-23 20:25:53 +0000690}
drha3152892007-05-05 11:48:52 +0000691
692/*
693** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000694** returning control to the user) that has called sqlite3_malloc or
695** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000696**
697** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000698** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000699** invocation SQLITE_NOMEM is returned instead.
700**
drh597d2b62015-06-30 03:13:47 +0000701** If an OOM as occurred, then the connection error-code (the value
702** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000703*/
drha3152892007-05-05 11:48:52 +0000704int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000705 /* If the db handle must hold the connection handle mutex here.
706 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000707 ** is unsafe, as is the call to sqlite3Error().
708 */
drh597d2b62015-06-30 03:13:47 +0000709 assert( db!=0 );
710 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000711 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
712 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000713 }
drhb50c65d2014-08-23 20:25:53 +0000714 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000715}