blob: af83743fc00cec7fdb33142d65c1e0165cace867 [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/*
drh31999c52019-11-14 17:46:32 +000036** Default value of the hard heap limit. 0 means "no limit".
37*/
38#ifndef SQLITE_MAX_MEMORY
39# define SQLITE_MAX_MEMORY 0
40#endif
41
42/*
drhfec00ea2008-06-14 16:56:21 +000043** State information local to the memory allocation subsystem.
44*/
danielk19775c8f8582008-09-02 10:22:00 +000045static SQLITE_WSD struct Mem0Global {
drhfec00ea2008-06-14 16:56:21 +000046 sqlite3_mutex *mutex; /* Mutex to serialize access */
drh5fb72e52015-09-10 01:22:09 +000047 sqlite3_int64 alarmThreshold; /* The soft heap limit */
drh10c0e712019-04-25 18:15:38 +000048 sqlite3_int64 hardLimit; /* The hard upper bound on memory */
drhfec00ea2008-06-14 16:56:21 +000049
50 /*
drh50d1b5f2010-08-27 12:21:06 +000051 ** True if heap is nearly "full" where "full" is defined by the
52 ** sqlite3_soft_heap_limit() setting.
53 */
54 int nearlyFull;
drh31999c52019-11-14 17:46:32 +000055} mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
danielk19775c8f8582008-09-02 10:22:00 +000056
57#define mem0 GLOBAL(struct Mem0Global, mem0)
drhfec00ea2008-06-14 16:56:21 +000058
59/*
drhaf89fe62015-03-23 17:25:18 +000060** Return the memory allocator mutex. sqlite3_status() needs it.
61*/
62sqlite3_mutex *sqlite3MallocMutex(void){
63 return mem0.mutex;
64}
65
drhf82ccf62010-09-15 17:54:31 +000066#ifndef SQLITE_OMIT_DEPRECATED
67/*
drh5fb72e52015-09-10 01:22:09 +000068** Deprecated external interface. It used to set an alarm callback
69** that was invoked when memory usage grew too large. Now it is a
70** no-op.
drhf82ccf62010-09-15 17:54:31 +000071*/
72int sqlite3_memory_alarm(
73 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
74 void *pArg,
75 sqlite3_int64 iThreshold
76){
drh5fb72e52015-09-10 01:22:09 +000077 (void)xCallback;
78 (void)pArg;
79 (void)iThreshold;
drh4ef299a2015-09-02 14:56:56 +000080 return SQLITE_OK;
drhf82ccf62010-09-15 17:54:31 +000081}
82#endif
83
84/*
drh10c0e712019-04-25 18:15:38 +000085** Set the soft heap-size limit for the library. An argument of
86** zero disables the limit. A negative argument is a no-op used to
87** obtain the return value.
88**
89** The return value is the value of the heap limit just before this
90** interface was called.
91**
92** If the hard heap limit is enabled, then the soft heap limit cannot
93** be disabled nor raised above the hard heap limit.
drhf82ccf62010-09-15 17:54:31 +000094*/
95sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
96 sqlite3_int64 priorLimit;
drh5fb72e52015-09-10 01:22:09 +000097 sqlite3_int64 excess;
98 sqlite3_int64 nUsed;
drhf82ccf62010-09-15 17:54:31 +000099#ifndef SQLITE_OMIT_AUTOINIT
drhde0f1812011-12-22 17:10:35 +0000100 int rc = sqlite3_initialize();
101 if( rc ) return -1;
drhf82ccf62010-09-15 17:54:31 +0000102#endif
103 sqlite3_mutex_enter(mem0.mutex);
104 priorLimit = mem0.alarmThreshold;
drh5fb72e52015-09-10 01:22:09 +0000105 if( n<0 ){
106 sqlite3_mutex_leave(mem0.mutex);
107 return priorLimit;
drhf82ccf62010-09-15 17:54:31 +0000108 }
drh10c0e712019-04-25 18:15:38 +0000109 if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
110 n = mem0.hardLimit;
111 }
drh5fb72e52015-09-10 01:22:09 +0000112 mem0.alarmThreshold = n;
113 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
drh23bef342020-04-28 14:01:31 +0000114 AtomicStore(&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/*
drh10c0e712019-04-25 18:15:38 +0000126** Set the hard heap-size limit for the library. An argument of zero
127** disables the hard heap limit. A negative argument is a no-op used
128** to obtain the return value without affecting the hard heap limit.
129**
130** The return value is the value of the hard heap limit just prior to
131** calling this interface.
132**
133** Setting the hard heap limit will also activate the soft heap limit
134** and constrain the soft heap limit to be no more than the hard heap
135** limit.
136*/
137sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
138 sqlite3_int64 priorLimit;
139#ifndef SQLITE_OMIT_AUTOINIT
140 int rc = sqlite3_initialize();
141 if( rc ) return -1;
142#endif
143 sqlite3_mutex_enter(mem0.mutex);
144 priorLimit = mem0.hardLimit;
145 if( n>=0 ){
146 mem0.hardLimit = n;
147 if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
148 mem0.alarmThreshold = n;
149 }
150 }
151 sqlite3_mutex_leave(mem0.mutex);
152 return priorLimit;
153}
154
155
156/*
drhfec00ea2008-06-14 16:56:21 +0000157** Initialize the memory allocation subsystem.
158*/
159int sqlite3MallocInit(void){
drh592f0cb2015-03-26 17:04:23 +0000160 int rc;
danielk1977075c23a2008-09-01 18:34:20 +0000161 if( sqlite3GlobalConfig.m.xMalloc==0 ){
drhfec00ea2008-06-14 16:56:21 +0000162 sqlite3MemSetDefault();
163 }
drh59a05232015-10-15 17:21:35 +0000164 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drh50d1b5f2010-08-27 12:21:06 +0000165 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000166 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000167 sqlite3GlobalConfig.pPage = 0;
168 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000169 }
drh592f0cb2015-03-26 17:04:23 +0000170 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
171 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
172 return rc;
drhfec00ea2008-06-14 16:56:21 +0000173}
174
175/*
drh50d1b5f2010-08-27 12:21:06 +0000176** Return true if the heap is currently under memory pressure - in other
177** words if the amount of heap used is close to the limit set by
178** sqlite3_soft_heap_limit().
179*/
180int sqlite3HeapNearlyFull(void){
drh9f603fc2020-04-28 11:45:41 +0000181 return AtomicLoad(&mem0.nearlyFull);
drh50d1b5f2010-08-27 12:21:06 +0000182}
183
184/*
drhfec00ea2008-06-14 16:56:21 +0000185** Deinitialize the memory allocation subsystem.
186*/
187void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000188 if( sqlite3GlobalConfig.m.xShutdown ){
189 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
190 }
drh9ac3fe92008-06-18 18:12:04 +0000191 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000192}
193
194/*
195** Return the amount of memory currently checked out.
196*/
197sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000198 sqlite3_int64 res, mx;
199 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000200 return res;
drhfec00ea2008-06-14 16:56:21 +0000201}
202
203/*
204** Return the maximum amount of memory that has ever been
205** checked out since either the beginning of this process
206** or since the most recent reset.
207*/
208sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000209 sqlite3_int64 res, mx;
210 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
211 return mx;
drhfec00ea2008-06-14 16:56:21 +0000212}
213
214/*
drh5fb72e52015-09-10 01:22:09 +0000215** Trigger the alarm
216*/
217static void sqlite3MallocAlarm(int nByte){
218 if( mem0.alarmThreshold<=0 ) return;
219 sqlite3_mutex_leave(mem0.mutex);
220 sqlite3_release_memory(nByte);
221 sqlite3_mutex_enter(mem0.mutex);
222}
223
224/*
drhf7141992008-06-19 00:16:08 +0000225** Do a memory allocation with statistics and alarms. Assume the
226** lock is already held.
227*/
drh1d21bac2017-01-10 16:09:46 +0000228static void mallocWithAlarm(int n, void **pp){
drhf7141992008-06-19 00:16:08 +0000229 void *p;
drh087a29c2017-02-08 16:01:57 +0000230 int nFull;
drhf7141992008-06-19 00:16:08 +0000231 assert( sqlite3_mutex_held(mem0.mutex) );
drh087a29c2017-02-08 16:01:57 +0000232 assert( n>0 );
233
mistachkin40b84362017-02-08 18:13:46 +0000234 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
drh087a29c2017-02-08 16:01:57 +0000235 ** implementation of malloc_good_size(), which must be called in debug
236 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
mistachkin40b84362017-02-08 18:13:46 +0000237 ** or else a crash results. Hence, do not attempt to optimize out the
238 ** following xRoundup() call. */
drh087a29c2017-02-08 16:01:57 +0000239 nFull = sqlite3GlobalConfig.m.xRoundup(n);
240
drhb02392e2015-10-15 15:28:56 +0000241 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
drh5fb72e52015-09-10 01:22:09 +0000242 if( mem0.alarmThreshold>0 ){
243 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
244 if( nUsed >= mem0.alarmThreshold - nFull ){
drh23bef342020-04-28 14:01:31 +0000245 AtomicStore(&mem0.nearlyFull, 1);
drh5fb72e52015-09-10 01:22:09 +0000246 sqlite3MallocAlarm(nFull);
drh10c0e712019-04-25 18:15:38 +0000247 if( mem0.hardLimit ){
248 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
249 if( nUsed >= mem0.hardLimit - nFull ){
250 *pp = 0;
251 return;
252 }
253 }
drh5fb72e52015-09-10 01:22:09 +0000254 }else{
drh23bef342020-04-28 14:01:31 +0000255 AtomicStore(&mem0.nearlyFull, 0);
drh5fb72e52015-09-10 01:22:09 +0000256 }
257 }
drh087a29c2017-02-08 16:01:57 +0000258 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000259#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000260 if( p==0 && mem0.alarmThreshold>0 ){
261 sqlite3MallocAlarm(nFull);
drh087a29c2017-02-08 16:01:57 +0000262 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000263 }
drh50d1b5f2010-08-27 12:21:06 +0000264#endif
drhc702c7c2008-07-18 18:56:16 +0000265 if( p ){
drhbe7a0ce2017-01-13 12:53:35 +0000266 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000267 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
268 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000269 }
drhf7141992008-06-19 00:16:08 +0000270 *pp = p;
drhf7141992008-06-19 00:16:08 +0000271}
drhfec00ea2008-06-14 16:56:21 +0000272
273/*
drh5d48e0c2022-09-27 16:35:06 +0000274** Maximum size of any single memory allocation.
275**
276** This is not a limit on the total amount of memory used. This is
277** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
278**
279** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
280** This provides a 256-byte safety margin for defense against 32-bit
281** signed integer overflow bugs when computing memory allocation sizes.
282** Parnoid applications might want to reduce the maximum allocation size
283** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
284** or even smaller would be reasonable upper bounds on the size of a memory
285** allocations for most applications.
286*/
287#ifndef SQLITE_MAX_ALLOCATION_SIZE
288# define SQLITE_MAX_ALLOCATION_SIZE 2147483391
289#endif
290#if SQLITE_MAX_ALLOCATION_SIZE>2147483391
291# error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
292#endif
293
294/*
drhfec00ea2008-06-14 16:56:21 +0000295** Allocate memory. This routine is like sqlite3_malloc() except that it
296** assumes the memory subsystem has already been initialized.
297*/
drhda4ca9d2014-09-09 17:27:35 +0000298void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000299 void *p;
drh5d48e0c2022-09-27 16:35:06 +0000300 if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){
drhf7141992008-06-19 00:16:08 +0000301 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000302 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000303 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000304 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000305 sqlite3_mutex_leave(mem0.mutex);
306 }else{
drhda4ca9d2014-09-09 17:27:35 +0000307 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000308 }
drh8da47412014-10-03 14:54:47 +0000309 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000310 return p;
311}
312
313/*
314** This version of the memory allocation is for use by the application.
315** First make sure the memory subsystem is initialized, then do the
316** allocation.
317*/
318void *sqlite3_malloc(int n){
319#ifndef SQLITE_OMIT_AUTOINIT
320 if( sqlite3_initialize() ) return 0;
321#endif
drhda4ca9d2014-09-09 17:27:35 +0000322 return n<=0 ? 0 : sqlite3Malloc(n);
323}
324void *sqlite3_malloc64(sqlite3_uint64 n){
325#ifndef SQLITE_OMIT_AUTOINIT
326 if( sqlite3_initialize() ) return 0;
327#endif
drhfec00ea2008-06-14 16:56:21 +0000328 return sqlite3Malloc(n);
329}
330
331/*
drh633e6d52008-07-28 19:34:53 +0000332** TRUE if p is a lookaside memory allocation from db
333*/
drh4150ebf2008-10-11 15:38:29 +0000334#ifndef SQLITE_OMIT_LOOKASIDE
drhb6dad522021-09-24 16:14:47 +0000335static int isLookaside(sqlite3 *db, const void *p){
drh376860b2022-08-22 15:18:37 +0000336 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
drh633e6d52008-07-28 19:34:53 +0000337}
drh4150ebf2008-10-11 15:38:29 +0000338#else
339#define isLookaside(A,B) 0
340#endif
drh633e6d52008-07-28 19:34:53 +0000341
342/*
drhfec00ea2008-06-14 16:56:21 +0000343** Return the size of a memory allocation previously obtained from
344** sqlite3Malloc() or sqlite3_malloc().
345*/
drhb6dad522021-09-24 16:14:47 +0000346int sqlite3MallocSize(const void *p){
drh107b56e2010-03-12 16:32:53 +0000347 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drhb6dad522021-09-24 16:14:47 +0000348 return sqlite3GlobalConfig.m.xSize((void*)p);
drhfec00ea2008-06-14 16:56:21 +0000349}
drhb6dad522021-09-24 16:14:47 +0000350static int lookasideMallocSize(sqlite3 *db, const void *p){
drhcf014f62019-12-31 15:12:34 +0000351#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
352 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
numist115d6632019-12-12 02:50:07 +0000353#else
354 return db->lookaside.szTrue;
355#endif
356}
drhb6dad522021-09-24 16:14:47 +0000357int sqlite3DbMallocSize(sqlite3 *db, const void *p){
drh039ca6a2015-10-15 16:20:57 +0000358 assert( p!=0 );
drhd879e3e2017-02-13 13:35:55 +0000359#ifdef SQLITE_DEBUG
drh376860b2022-08-22 15:18:37 +0000360 if( db==0 ){
361 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
362 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
363 }else if( !isLookaside(db,p) ){
364 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
365 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh633e6d52008-07-28 19:34:53 +0000366 }
drhe6068022019-12-13 15:48:21 +0000367#endif
368 if( db ){
drh376860b2022-08-22 15:18:37 +0000369 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
drhcf014f62019-12-31 15:12:34 +0000370#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
drhe6068022019-12-13 15:48:21 +0000371 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
372 assert( sqlite3_mutex_held(db->mutex) );
drhcf014f62019-12-31 15:12:34 +0000373 return LOOKASIDE_SMALL;
drhe6068022019-12-13 15:48:21 +0000374 }
375#endif
376 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
377 assert( sqlite3_mutex_held(db->mutex) );
378 return db->lookaside.szTrue;
379 }
380 }
381 }
drhb6dad522021-09-24 16:14:47 +0000382 return sqlite3GlobalConfig.m.xSize((void*)p);
drh633e6d52008-07-28 19:34:53 +0000383}
drhda4ca9d2014-09-09 17:27:35 +0000384sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000385 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000386 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh039ca6a2015-10-15 16:20:57 +0000387 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
drhda4ca9d2014-09-09 17:27:35 +0000388}
drhfec00ea2008-06-14 16:56:21 +0000389
390/*
391** Free memory previously obtained from sqlite3Malloc().
392*/
393void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000394 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000395 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000396 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000397 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000398 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000399 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
400 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000401 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000402 sqlite3_mutex_leave(mem0.mutex);
403 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000404 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000405 }
406}
407
408/*
drhb4586f12014-08-23 19:42:06 +0000409** Add the size of memory allocation "p" to the count in
410** *db->pnBytesFreed.
411*/
412static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
drh56d90be2015-10-26 12:55:56 +0000413 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
drhb4586f12014-08-23 19:42:06 +0000414}
415
416/*
drh633e6d52008-07-28 19:34:53 +0000417** Free memory that might be associated with a particular database
drhdbd6a7d2017-04-05 12:39:49 +0000418** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
419** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
drh633e6d52008-07-28 19:34:53 +0000420*/
drhdbd6a7d2017-04-05 12:39:49 +0000421void sqlite3DbFreeNN(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000422 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drhdbd6a7d2017-04-05 12:39:49 +0000423 assert( p!=0 );
drh174b9a12010-07-26 11:07:20 +0000424 if( db ){
drhe6068022019-12-13 15:48:21 +0000425 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
drhcf014f62019-12-31 15:12:34 +0000426#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
drhe6068022019-12-13 15:48:21 +0000427 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
428 LookasideSlot *pBuf = (LookasideSlot*)p;
drh376860b2022-08-22 15:18:37 +0000429 assert( db->pnBytesFreed==0 );
drhe6068022019-12-13 15:48:21 +0000430#ifdef SQLITE_DEBUG
drhcf014f62019-12-31 15:12:34 +0000431 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
drhe6068022019-12-13 15:48:21 +0000432#endif
drhcf014f62019-12-31 15:12:34 +0000433 pBuf->pNext = db->lookaside.pSmallFree;
434 db->lookaside.pSmallFree = pBuf;
numist115d6632019-12-12 02:50:07 +0000435 return;
436 }
drhcf014f62019-12-31 15:12:34 +0000437#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
drhe6068022019-12-13 15:48:21 +0000438 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
439 LookasideSlot *pBuf = (LookasideSlot*)p;
drh376860b2022-08-22 15:18:37 +0000440 assert( db->pnBytesFreed==0 );
drhd879e3e2017-02-13 13:35:55 +0000441#ifdef SQLITE_DEBUG
drhe6068022019-12-13 15:48:21 +0000442 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
drh3608f172012-05-21 16:59:16 +0000443#endif
drhe6068022019-12-13 15:48:21 +0000444 pBuf->pNext = db->lookaside.pFree;
445 db->lookaside.pFree = pBuf;
446 return;
447 }
drh174b9a12010-07-26 11:07:20 +0000448 }
drh376860b2022-08-22 15:18:37 +0000449 if( db->pnBytesFreed ){
450 measureAllocationSize(db, p);
451 return;
452 }
drh633e6d52008-07-28 19:34:53 +0000453 }
drhd231aa32014-10-07 15:46:54 +0000454 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000455 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000456 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
457 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
458 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000459}
drh41ce47c2022-08-22 02:00:26 +0000460void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
461 assert( db!=0 );
462 assert( sqlite3_mutex_held(db->mutex) );
463 assert( p!=0 );
drh41ce47c2022-08-22 02:00:26 +0000464 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
465#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
466 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
467 LookasideSlot *pBuf = (LookasideSlot*)p;
drh376860b2022-08-22 15:18:37 +0000468 assert( db->pnBytesFreed==0 );
drh41ce47c2022-08-22 02:00:26 +0000469#ifdef SQLITE_DEBUG
470 memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
471#endif
472 pBuf->pNext = db->lookaside.pSmallFree;
473 db->lookaside.pSmallFree = pBuf;
474 return;
475 }
476#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
477 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
478 LookasideSlot *pBuf = (LookasideSlot*)p;
drh376860b2022-08-22 15:18:37 +0000479 assert( db->pnBytesFreed==0 );
drh41ce47c2022-08-22 02:00:26 +0000480#ifdef SQLITE_DEBUG
481 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
482#endif
483 pBuf->pNext = db->lookaside.pFree;
484 db->lookaside.pFree = pBuf;
485 return;
486 }
487 }
drh376860b2022-08-22 15:18:37 +0000488 if( db->pnBytesFreed ){
489 measureAllocationSize(db, p);
490 return;
491 }
drh41ce47c2022-08-22 02:00:26 +0000492 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
493 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
494 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
495 sqlite3_free(p);
496}
drhdbd6a7d2017-04-05 12:39:49 +0000497void sqlite3DbFree(sqlite3 *db, void *p){
498 assert( db==0 || sqlite3_mutex_held(db->mutex) );
499 if( p ) sqlite3DbFreeNN(db, p);
500}
drh633e6d52008-07-28 19:34:53 +0000501
502/*
drhfec00ea2008-06-14 16:56:21 +0000503** Change the size of an existing memory allocation
504*/
drhda4ca9d2014-09-09 17:27:35 +0000505void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000506 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000507 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000508 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000509 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000510 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000511 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000512 }
drhda4ca9d2014-09-09 17:27:35 +0000513 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000514 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000515 return 0;
516 }
drhb6063cf2009-06-27 00:48:33 +0000517 if( nBytes>=0x7fffff00 ){
518 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
519 return 0;
520 }
drhfec00ea2008-06-14 16:56:21 +0000521 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000522 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
523 ** argument to xRealloc is always a value returned by a prior call to
524 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000525 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000526 if( nOld==nNew ){
527 pNew = pOld;
528 }else if( sqlite3GlobalConfig.bMemstat ){
drh672f07c2020-10-20 14:40:53 +0000529 sqlite3_int64 nUsed;
drhfec00ea2008-06-14 16:56:21 +0000530 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000531 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000532 nDiff = nNew - nOld;
drh672f07c2020-10-20 14:40:53 +0000533 if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
drh5fb72e52015-09-10 01:22:09 +0000534 mem0.alarmThreshold-nDiff ){
535 sqlite3MallocAlarm(nDiff);
drh672f07c2020-10-20 14:40:53 +0000536 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
537 sqlite3_mutex_leave(mem0.mutex);
538 return 0;
539 }
drh5fb72e52015-09-10 01:22:09 +0000540 }
drh7c6791c2009-08-18 14:48:53 +0000541 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhacc17522020-05-08 10:44:23 +0000542#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000543 if( pNew==0 && mem0.alarmThreshold>0 ){
544 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000545 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000546 }
drhacc17522020-05-08 10:44:23 +0000547#endif
drh7c6791c2009-08-18 14:48:53 +0000548 if( pNew ){
549 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000550 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000551 }
552 sqlite3_mutex_leave(mem0.mutex);
553 }else{
drh7c6791c2009-08-18 14:48:53 +0000554 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000555 }
drh8da47412014-10-03 14:54:47 +0000556 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000557 return pNew;
558}
559
560/*
561** The public interface to sqlite3Realloc. Make sure that the memory
562** subsystem is initialized prior to invoking sqliteRealloc.
563*/
564void *sqlite3_realloc(void *pOld, int n){
565#ifndef SQLITE_OMIT_AUTOINIT
566 if( sqlite3_initialize() ) return 0;
567#endif
drh8da47412014-10-03 14:54:47 +0000568 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000569 return sqlite3Realloc(pOld, n);
570}
571void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
572#ifndef SQLITE_OMIT_AUTOINIT
573 if( sqlite3_initialize() ) return 0;
574#endif
drhfec00ea2008-06-14 16:56:21 +0000575 return sqlite3Realloc(pOld, n);
576}
577
drha3152892007-05-05 11:48:52 +0000578
579/*
drh17435752007-08-16 04:30:38 +0000580** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000581*/
drhda4ca9d2014-09-09 17:27:35 +0000582void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000583 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000584 if( p ){
drh20f3df02014-09-18 02:20:54 +0000585 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000586 }
587 return p;
588}
drh17435752007-08-16 04:30:38 +0000589
590/*
591** Allocate and zero memory. If the allocation fails, make
592** the mallocFailed flag in the connection pointer.
593*/
drhda4ca9d2014-09-09 17:27:35 +0000594void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000595 void *p;
596 testcase( db==0 );
597 p = sqlite3DbMallocRaw(db, n);
598 if( p ) memset(p, 0, (size_t)n);
599 return p;
600}
601
602
603/* Finish the work of sqlite3DbMallocRawNN for the unusual and
604** slower case when the allocation cannot be fulfilled using lookaside.
605*/
606static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
607 void *p;
608 assert( db!=0 );
609 p = sqlite3Malloc(n);
610 if( !p ) sqlite3OomFault(db);
611 sqlite3MemdebugSetType(p,
612 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000613 return p;
614}
615
616/*
drh1da26a42016-01-20 03:36:32 +0000617** Allocate memory, either lookaside (if possible) or heap.
618** If the allocation fails, set the mallocFailed flag in
619** the connection pointer.
drhddecae72008-10-11 17:35:16 +0000620**
621** If db!=0 and db->mallocFailed is true (indicating a prior malloc
622** failure on the same database connection) then always return 0.
623** Hence for a particular database connection, once malloc starts
624** failing, it fails consistently until mallocFailed is reset.
625** This is an important assumption. There are many places in the
626** code that do things like this:
627**
628** int *a = (int*)sqlite3DbMallocRaw(db, 100);
629** int *b = (int*)sqlite3DbMallocRaw(db, 200);
630** if( b ) a[10] = 9;
631**
632** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
633** that all prior mallocs (ex: "a") worked too.
drh575fad62016-02-05 13:38:36 +0000634**
635** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
636** not a NULL pointer.
drh17435752007-08-16 04:30:38 +0000637*/
drhda4ca9d2014-09-09 17:27:35 +0000638void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000639 void *p;
640 if( db ) return sqlite3DbMallocRawNN(db, n);
641 p = sqlite3Malloc(n);
642 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
643 return p;
644}
645void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
drhf5818aa2016-02-06 19:48:50 +0000646#ifndef SQLITE_OMIT_LOOKASIDE
647 LookasideSlot *pBuf;
drh575fad62016-02-05 13:38:36 +0000648 assert( db!=0 );
649 assert( sqlite3_mutex_held(db->mutex) );
650 assert( db->pnBytesFreed==0 );
drh31f69622019-10-05 14:39:36 +0000651 if( n>db->lookaside.sz ){
numist1b47c9c2019-12-12 20:58:32 +0000652 if( !db->lookaside.bDisable ){
653 db->lookaside.anStat[1]++;
654 }else if( db->mallocFailed ){
655 return 0;
drh633e6d52008-07-28 19:34:53 +0000656 }
numistc947d6a2019-12-12 20:39:47 +0000657 return dbMallocRawFinish(db, n);
658 }
drhcf014f62019-12-31 15:12:34 +0000659#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
660 if( n<=LOOKASIDE_SMALL ){
661 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
662 db->lookaside.pSmallFree = pBuf->pNext;
numist115d6632019-12-12 02:50:07 +0000663 db->lookaside.anStat[0]++;
664 return (void*)pBuf;
drhcf014f62019-12-31 15:12:34 +0000665 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
666 db->lookaside.pSmallInit = pBuf->pNext;
numist115d6632019-12-12 02:50:07 +0000667 db->lookaside.anStat[0]++;
668 return (void*)pBuf;
669 }
numistc947d6a2019-12-12 20:39:47 +0000670 }
drhe6068022019-12-13 15:48:21 +0000671#endif
numistc947d6a2019-12-12 20:39:47 +0000672 if( (pBuf = db->lookaside.pFree)!=0 ){
drh31f69622019-10-05 14:39:36 +0000673 db->lookaside.pFree = pBuf->pNext;
674 db->lookaside.anStat[0]++;
675 return (void*)pBuf;
676 }else if( (pBuf = db->lookaside.pInit)!=0 ){
677 db->lookaside.pInit = pBuf->pNext;
678 db->lookaside.anStat[0]++;
679 return (void*)pBuf;
680 }else{
681 db->lookaside.anStat[2]++;
drh633e6d52008-07-28 19:34:53 +0000682 }
drhddecae72008-10-11 17:35:16 +0000683#else
drhf5818aa2016-02-06 19:48:50 +0000684 assert( db!=0 );
685 assert( sqlite3_mutex_held(db->mutex) );
686 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000687 if( db->mallocFailed ){
drhddecae72008-10-11 17:35:16 +0000688 return 0;
689 }
drh4150ebf2008-10-11 15:38:29 +0000690#endif
drh1da26a42016-01-20 03:36:32 +0000691 return dbMallocRawFinish(db, n);
692}
drh17435752007-08-16 04:30:38 +0000693
drhb84e5742016-02-05 02:42:54 +0000694/* Forward declaration */
695static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
696
danielk197726783a52007-08-29 14:06:22 +0000697/*
698** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000699** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000700*/
drhda4ca9d2014-09-09 17:27:35 +0000701void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
drhb84e5742016-02-05 02:42:54 +0000702 assert( db!=0 );
drh575fad62016-02-05 13:38:36 +0000703 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
drhb84e5742016-02-05 02:42:54 +0000704 assert( sqlite3_mutex_held(db->mutex) );
drhe6068022019-12-13 15:48:21 +0000705 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
drhcf014f62019-12-31 15:12:34 +0000706#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
drhe6068022019-12-13 15:48:21 +0000707 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
drhcf014f62019-12-31 15:12:34 +0000708 if( n<=LOOKASIDE_SMALL ) return p;
drhe6068022019-12-13 15:48:21 +0000709 }else
710#endif
711 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
712 if( n<=db->lookaside.szTrue ) return p;
713 }
714 }
drhb84e5742016-02-05 02:42:54 +0000715 return dbReallocFinish(db, p, n);
716}
717static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000718 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000719 assert( db!=0 );
drhb84e5742016-02-05 02:42:54 +0000720 assert( p!=0 );
danielk1977a1644fd2007-08-29 12:31:25 +0000721 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000722 if( isLookaside(db, p) ){
drh575fad62016-02-05 13:38:36 +0000723 pNew = sqlite3DbMallocRawNN(db, n);
drh633e6d52008-07-28 19:34:53 +0000724 if( pNew ){
numist115d6632019-12-12 02:50:07 +0000725 memcpy(pNew, p, lookasideMallocSize(db, p));
drh633e6d52008-07-28 19:34:53 +0000726 sqlite3DbFree(db, p);
727 }
728 }else{
drhd231aa32014-10-07 15:46:54 +0000729 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000730 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000731 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drhd924e7b2020-05-17 00:26:44 +0000732 pNew = sqlite3Realloc(p, n);
drh633e6d52008-07-28 19:34:53 +0000733 if( !pNew ){
drh4a642b62016-02-05 01:55:27 +0000734 sqlite3OomFault(db);
drh633e6d52008-07-28 19:34:53 +0000735 }
drhd231aa32014-10-07 15:46:54 +0000736 sqlite3MemdebugSetType(pNew,
drh4a642b62016-02-05 01:55:27 +0000737 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000738 }
739 }
740 return pNew;
741}
742
drh17435752007-08-16 04:30:38 +0000743/*
744** Attempt to reallocate p. If the reallocation fails, then free p
745** and set the mallocFailed flag in the database connection.
746*/
drhda4ca9d2014-09-09 17:27:35 +0000747void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000748 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000749 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000750 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000751 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000752 }
753 return pNew;
754}
755
drha3152892007-05-05 11:48:52 +0000756/*
757** Make a copy of a string in memory obtained from sqliteMalloc(). These
758** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
759** is because when memory debugging is turned on, these two functions are
760** called via macros that record the current file and line number in the
761** ThreadData structure.
762*/
drh633e6d52008-07-28 19:34:53 +0000763char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000764 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000765 size_t n;
766 if( z==0 ){
767 return 0;
768 }
drhcee11ad2016-10-17 00:48:06 +0000769 n = strlen(z) + 1;
770 zNew = sqlite3DbMallocRaw(db, n);
drha3152892007-05-05 11:48:52 +0000771 if( zNew ){
772 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000773 }
774 return zNew;
775}
drhda4ca9d2014-09-09 17:27:35 +0000776char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000777 char *zNew;
drh575fad62016-02-05 13:38:36 +0000778 assert( db!=0 );
drh369e7582020-07-16 14:19:57 +0000779 assert( z!=0 || n==0 );
drh633e6d52008-07-28 19:34:53 +0000780 assert( (n&0x7fffffff)==n );
drh369e7582020-07-16 14:19:57 +0000781 zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
drh633e6d52008-07-28 19:34:53 +0000782 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000783 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000784 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000785 }
786 return zNew;
787}
788
drha3152892007-05-05 11:48:52 +0000789/*
drh9b2e0432017-12-27 19:43:22 +0000790** The text between zStart and zEnd represents a phrase within a larger
791** SQL statement. Make a copy of this phrase in space obtained form
792** sqlite3DbMalloc(). Omit leading and trailing whitespace.
793*/
794char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
795 int n;
796 while( sqlite3Isspace(zStart[0]) ) zStart++;
797 n = (int)(zEnd - zStart);
drhe75d1f52018-01-10 13:58:23 +0000798 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
drh9b2e0432017-12-27 19:43:22 +0000799 return sqlite3DbStrNDup(db, zStart, n);
800}
801
802/*
drh22c17b82015-05-15 04:13:15 +0000803** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000804*/
drh22c17b82015-05-15 04:13:15 +0000805void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
danf7413d92022-04-12 11:02:06 +0000806 char *z = sqlite3DbStrDup(db, zNew);
drh633e6d52008-07-28 19:34:53 +0000807 sqlite3DbFree(db, *pz);
danf7413d92022-04-12 11:02:06 +0000808 *pz = z;
drha3152892007-05-05 11:48:52 +0000809}
810
drhb50c65d2014-08-23 20:25:53 +0000811/*
drh4a642b62016-02-05 01:55:27 +0000812** Call this routine to record the fact that an OOM (out-of-memory) error
813** has happened. This routine will set db->mallocFailed, and also
814** temporarily disable the lookaside memory allocator and interrupt
815** any running VDBEs.
drh3cdb1392022-01-24 12:48:54 +0000816**
817** Always return a NULL pointer so that this routine can be invoked using
818**
819** return sqlite3OomFault(db);
820**
821** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
822** common case where no OOM occurs.
drh4a642b62016-02-05 01:55:27 +0000823*/
drh3cdb1392022-01-24 12:48:54 +0000824void *sqlite3OomFault(sqlite3 *db){
drh4a642b62016-02-05 01:55:27 +0000825 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
826 db->mallocFailed = 1;
827 if( db->nVdbeExec>0 ){
dan892edb62020-03-30 13:35:05 +0000828 AtomicStore(&db->u1.isInterrupted, 1);
drh4a642b62016-02-05 01:55:27 +0000829 }
drh31f69622019-10-05 14:39:36 +0000830 DisableLookaside;
drh1cf19752019-02-08 14:55:30 +0000831 if( db->pParse ){
drh25bb72a2022-07-10 21:12:54 +0000832 Parse *pParse;
drh3cdb1392022-01-24 12:48:54 +0000833 sqlite3ErrorMsg(db->pParse, "out of memory");
drh1cf19752019-02-08 14:55:30 +0000834 db->pParse->rc = SQLITE_NOMEM_BKPT;
drh25bb72a2022-07-10 21:12:54 +0000835 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
836 pParse->nErr++;
837 pParse->rc = SQLITE_NOMEM;
838 }
drh1cf19752019-02-08 14:55:30 +0000839 }
drh4a642b62016-02-05 01:55:27 +0000840 }
drh3cdb1392022-01-24 12:48:54 +0000841 return 0;
drh4a642b62016-02-05 01:55:27 +0000842}
843
844/*
845** This routine reactivates the memory allocator and clears the
846** db->mallocFailed flag as necessary.
847**
848** The memory allocator is not restarted if there are running
849** VDBEs.
850*/
851void sqlite3OomClear(sqlite3 *db){
852 if( db->mallocFailed && db->nVdbeExec==0 ){
853 db->mallocFailed = 0;
dan892edb62020-03-30 13:35:05 +0000854 AtomicStore(&db->u1.isInterrupted, 0);
drh4a642b62016-02-05 01:55:27 +0000855 assert( db->lookaside.bDisable>0 );
drh31f69622019-10-05 14:39:36 +0000856 EnableLookaside;
drh4a642b62016-02-05 01:55:27 +0000857 }
858}
859
860/*
drh5a07d102020-11-18 12:48:48 +0000861** Take actions at the end of an API call to deal with error codes.
drhb50c65d2014-08-23 20:25:53 +0000862*/
drh5a07d102020-11-18 12:48:48 +0000863static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
864 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
865 sqlite3OomClear(db);
866 sqlite3Error(db, SQLITE_NOMEM);
867 return SQLITE_NOMEM_BKPT;
868 }
drh5a07d102020-11-18 12:48:48 +0000869 return rc & db->errMask;
drhb50c65d2014-08-23 20:25:53 +0000870}
drha3152892007-05-05 11:48:52 +0000871
872/*
873** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000874** returning control to the user) that has called sqlite3_malloc or
875** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000876**
877** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000878** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000879** invocation SQLITE_NOMEM is returned instead.
880**
drh597d2b62015-06-30 03:13:47 +0000881** If an OOM as occurred, then the connection error-code (the value
882** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000883*/
drha3152892007-05-05 11:48:52 +0000884int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000885 /* If the db handle must hold the connection handle mutex here.
886 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000887 ** is unsafe, as is the call to sqlite3Error().
888 */
drh597d2b62015-06-30 03:13:47 +0000889 assert( db!=0 );
890 assert( sqlite3_mutex_held(db->mutex) );
drh5a07d102020-11-18 12:48:48 +0000891 if( db->mallocFailed || rc ){
892 return apiHandleError(db, rc);
drha3152892007-05-05 11:48:52 +0000893 }
drhb50c65d2014-08-23 20:25:53 +0000894 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000895}