blob: 8f77c964380054dd6533d7df52019afeadc74a31 [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);
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/*
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 }
164 memset(&mem0, 0, sizeof(mem0));
drh59a05232015-10-15 17:21:35 +0000165 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
drh50d1b5f2010-08-27 12:21:06 +0000166 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
drh01c5c002015-07-04 18:15:04 +0000167 || sqlite3GlobalConfig.nPage<=0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000168 sqlite3GlobalConfig.pPage = 0;
169 sqlite3GlobalConfig.szPage = 0;
drh9ac3fe92008-06-18 18:12:04 +0000170 }
drh592f0cb2015-03-26 17:04:23 +0000171 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
172 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
173 return rc;
drhfec00ea2008-06-14 16:56:21 +0000174}
175
176/*
drh50d1b5f2010-08-27 12:21:06 +0000177** Return true if the heap is currently under memory pressure - in other
178** words if the amount of heap used is close to the limit set by
179** sqlite3_soft_heap_limit().
180*/
181int sqlite3HeapNearlyFull(void){
182 return mem0.nearlyFull;
183}
184
185/*
drhfec00ea2008-06-14 16:56:21 +0000186** Deinitialize the memory allocation subsystem.
187*/
188void sqlite3MallocEnd(void){
danielk19770a549072009-02-17 16:29:10 +0000189 if( sqlite3GlobalConfig.m.xShutdown ){
190 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
191 }
drh9ac3fe92008-06-18 18:12:04 +0000192 memset(&mem0, 0, sizeof(mem0));
drhfec00ea2008-06-14 16:56:21 +0000193}
194
195/*
196** Return the amount of memory currently checked out.
197*/
198sqlite3_int64 sqlite3_memory_used(void){
drhdf5e1a02015-05-10 02:01:08 +0000199 sqlite3_int64 res, mx;
200 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
drhc376a192008-07-14 12:30:54 +0000201 return res;
drhfec00ea2008-06-14 16:56:21 +0000202}
203
204/*
205** Return the maximum amount of memory that has ever been
206** checked out since either the beginning of this process
207** or since the most recent reset.
208*/
209sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
drhdf5e1a02015-05-10 02:01:08 +0000210 sqlite3_int64 res, mx;
211 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
212 return mx;
drhfec00ea2008-06-14 16:56:21 +0000213}
214
215/*
drh5fb72e52015-09-10 01:22:09 +0000216** Trigger the alarm
217*/
218static void sqlite3MallocAlarm(int nByte){
219 if( mem0.alarmThreshold<=0 ) return;
220 sqlite3_mutex_leave(mem0.mutex);
221 sqlite3_release_memory(nByte);
222 sqlite3_mutex_enter(mem0.mutex);
223}
224
225/*
drhf7141992008-06-19 00:16:08 +0000226** Do a memory allocation with statistics and alarms. Assume the
227** lock is already held.
228*/
drh1d21bac2017-01-10 16:09:46 +0000229static void mallocWithAlarm(int n, void **pp){
drhf7141992008-06-19 00:16:08 +0000230 void *p;
drh087a29c2017-02-08 16:01:57 +0000231 int nFull;
drhf7141992008-06-19 00:16:08 +0000232 assert( sqlite3_mutex_held(mem0.mutex) );
drh087a29c2017-02-08 16:01:57 +0000233 assert( n>0 );
234
mistachkin40b84362017-02-08 18:13:46 +0000235 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
drh087a29c2017-02-08 16:01:57 +0000236 ** implementation of malloc_good_size(), which must be called in debug
237 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
mistachkin40b84362017-02-08 18:13:46 +0000238 ** or else a crash results. Hence, do not attempt to optimize out the
239 ** following xRoundup() call. */
drh087a29c2017-02-08 16:01:57 +0000240 nFull = sqlite3GlobalConfig.m.xRoundup(n);
241
drhb02392e2015-10-15 15:28:56 +0000242 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
drh5fb72e52015-09-10 01:22:09 +0000243 if( mem0.alarmThreshold>0 ){
244 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
245 if( nUsed >= mem0.alarmThreshold - nFull ){
246 mem0.nearlyFull = 1;
247 sqlite3MallocAlarm(nFull);
drh10c0e712019-04-25 18:15:38 +0000248 if( mem0.hardLimit ){
249 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
250 if( nUsed >= mem0.hardLimit - nFull ){
251 *pp = 0;
252 return;
253 }
254 }
drh5fb72e52015-09-10 01:22:09 +0000255 }else{
256 mem0.nearlyFull = 0;
257 }
258 }
drh087a29c2017-02-08 16:01:57 +0000259 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drh50d1b5f2010-08-27 12:21:06 +0000260#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh5fb72e52015-09-10 01:22:09 +0000261 if( p==0 && mem0.alarmThreshold>0 ){
262 sqlite3MallocAlarm(nFull);
drh087a29c2017-02-08 16:01:57 +0000263 p = sqlite3GlobalConfig.m.xMalloc(nFull);
drhf7141992008-06-19 00:16:08 +0000264 }
drh50d1b5f2010-08-27 12:21:06 +0000265#endif
drhc702c7c2008-07-18 18:56:16 +0000266 if( p ){
drhbe7a0ce2017-01-13 12:53:35 +0000267 nFull = sqlite3MallocSize(p);
drhaf89fe62015-03-23 17:25:18 +0000268 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
269 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
drhc702c7c2008-07-18 18:56:16 +0000270 }
drhf7141992008-06-19 00:16:08 +0000271 *pp = p;
drhf7141992008-06-19 00:16:08 +0000272}
drhfec00ea2008-06-14 16:56:21 +0000273
274/*
275** Allocate memory. This routine is like sqlite3_malloc() except that it
276** assumes the memory subsystem has already been initialized.
277*/
drhda4ca9d2014-09-09 17:27:35 +0000278void *sqlite3Malloc(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000279 void *p;
drhda4ca9d2014-09-09 17:27:35 +0000280 if( n==0 || n>=0x7fffff00 ){
drhe08ed7e2009-06-26 18:35:16 +0000281 /* A memory allocation of a number of bytes which is near the maximum
282 ** signed integer value might cause an integer overflow inside of the
283 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
284 ** 255 bytes of overhead. SQLite itself will never use anything near
285 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
drhf7141992008-06-19 00:16:08 +0000286 p = 0;
danielk1977075c23a2008-09-01 18:34:20 +0000287 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000288 sqlite3_mutex_enter(mem0.mutex);
drh3329a632014-09-18 01:21:43 +0000289 mallocWithAlarm((int)n, &p);
drhfec00ea2008-06-14 16:56:21 +0000290 sqlite3_mutex_leave(mem0.mutex);
291 }else{
drhda4ca9d2014-09-09 17:27:35 +0000292 p = sqlite3GlobalConfig.m.xMalloc((int)n);
drhfec00ea2008-06-14 16:56:21 +0000293 }
drh8da47412014-10-03 14:54:47 +0000294 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000295 return p;
296}
297
298/*
299** This version of the memory allocation is for use by the application.
300** First make sure the memory subsystem is initialized, then do the
301** allocation.
302*/
303void *sqlite3_malloc(int n){
304#ifndef SQLITE_OMIT_AUTOINIT
305 if( sqlite3_initialize() ) return 0;
306#endif
drhda4ca9d2014-09-09 17:27:35 +0000307 return n<=0 ? 0 : sqlite3Malloc(n);
308}
309void *sqlite3_malloc64(sqlite3_uint64 n){
310#ifndef SQLITE_OMIT_AUTOINIT
311 if( sqlite3_initialize() ) return 0;
312#endif
drhfec00ea2008-06-14 16:56:21 +0000313 return sqlite3Malloc(n);
314}
315
316/*
drh633e6d52008-07-28 19:34:53 +0000317** TRUE if p is a lookaside memory allocation from db
318*/
drh4150ebf2008-10-11 15:38:29 +0000319#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000320static int isLookaside(sqlite3 *db, void *p){
drhac536e62015-12-10 15:09:17 +0000321 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
drh633e6d52008-07-28 19:34:53 +0000322}
drh4150ebf2008-10-11 15:38:29 +0000323#else
324#define isLookaside(A,B) 0
325#endif
drh633e6d52008-07-28 19:34:53 +0000326
327/*
drhfec00ea2008-06-14 16:56:21 +0000328** Return the size of a memory allocation previously obtained from
329** sqlite3Malloc() or sqlite3_malloc().
330*/
331int sqlite3MallocSize(void *p){
drh107b56e2010-03-12 16:32:53 +0000332 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000333 return sqlite3GlobalConfig.m.xSize(p);
drhfec00ea2008-06-14 16:56:21 +0000334}
numist115d6632019-12-12 02:50:07 +0000335static int lookasideMallocSize(sqlite3 *db, void *p){
336#ifndef SQLITE_OMIT_MINI_LOOKASIDE
drh0225d812019-12-12 17:17:24 +0000337 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : MINI_SZ;
numist115d6632019-12-12 02:50:07 +0000338#else
339 return db->lookaside.szTrue;
340#endif
341}
drh633e6d52008-07-28 19:34:53 +0000342int sqlite3DbMallocSize(sqlite3 *db, void *p){
drh039ca6a2015-10-15 16:20:57 +0000343 assert( p!=0 );
drh054bbab2015-09-01 20:09:33 +0000344 if( db==0 || !isLookaside(db,p) ){
drhd879e3e2017-02-13 13:35:55 +0000345#ifdef SQLITE_DEBUG
drh054bbab2015-09-01 20:09:33 +0000346 if( db==0 ){
347 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
348 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh17bcb102014-09-18 21:25:33 +0000349 }else{
drhd231aa32014-10-07 15:46:54 +0000350 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000351 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh17bcb102014-09-18 21:25:33 +0000352 }
drh054bbab2015-09-01 20:09:33 +0000353#endif
354 return sqlite3GlobalConfig.m.xSize(p);
355 }else{
356 assert( sqlite3_mutex_held(db->mutex) );
numist115d6632019-12-12 02:50:07 +0000357 return lookasideMallocSize(db, p);
drh633e6d52008-07-28 19:34:53 +0000358 }
359}
drhda4ca9d2014-09-09 17:27:35 +0000360sqlite3_uint64 sqlite3_msize(void *p){
mistachkind4258642015-03-21 23:38:59 +0000361 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
drhd231aa32014-10-07 15:46:54 +0000362 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
drh039ca6a2015-10-15 16:20:57 +0000363 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
drhda4ca9d2014-09-09 17:27:35 +0000364}
drhfec00ea2008-06-14 16:56:21 +0000365
366/*
367** Free memory previously obtained from sqlite3Malloc().
368*/
369void sqlite3_free(void *p){
drh71a1a0f2010-09-11 16:15:55 +0000370 if( p==0 ) return; /* IMP: R-49053-54554 */
drh107b56e2010-03-12 16:32:53 +0000371 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000372 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
danielk1977075c23a2008-09-01 18:34:20 +0000373 if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000374 sqlite3_mutex_enter(mem0.mutex);
drhaf89fe62015-03-23 17:25:18 +0000375 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
376 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
danielk1977075c23a2008-09-01 18:34:20 +0000377 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000378 sqlite3_mutex_leave(mem0.mutex);
379 }else{
danielk1977075c23a2008-09-01 18:34:20 +0000380 sqlite3GlobalConfig.m.xFree(p);
drhfec00ea2008-06-14 16:56:21 +0000381 }
382}
383
384/*
drhb4586f12014-08-23 19:42:06 +0000385** Add the size of memory allocation "p" to the count in
386** *db->pnBytesFreed.
387*/
388static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
drh56d90be2015-10-26 12:55:56 +0000389 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
drhb4586f12014-08-23 19:42:06 +0000390}
391
392/*
drh633e6d52008-07-28 19:34:53 +0000393** Free memory that might be associated with a particular database
drhdbd6a7d2017-04-05 12:39:49 +0000394** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
395** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
drh633e6d52008-07-28 19:34:53 +0000396*/
drhdbd6a7d2017-04-05 12:39:49 +0000397void sqlite3DbFreeNN(sqlite3 *db, void *p){
drh7047e252009-03-23 17:49:14 +0000398 assert( db==0 || sqlite3_mutex_held(db->mutex) );
drhdbd6a7d2017-04-05 12:39:49 +0000399 assert( p!=0 );
drh174b9a12010-07-26 11:07:20 +0000400 if( db ){
401 if( db->pnBytesFreed ){
drhb4586f12014-08-23 19:42:06 +0000402 measureAllocationSize(db, p);
drh174b9a12010-07-26 11:07:20 +0000403 return;
dand46def72010-07-24 11:28:28 +0000404 }
drh174b9a12010-07-26 11:07:20 +0000405 if( isLookaside(db, p) ){
406 LookasideSlot *pBuf = (LookasideSlot*)p;
numist115d6632019-12-12 02:50:07 +0000407#ifndef SQLITE_OMIT_MINI_LOOKASIDE
408 if( p>=db->lookaside.pMiddle ){
409# ifdef SQLITE_DEBUG
410 /* Trash all content in the buffer being freed */
drh0225d812019-12-12 17:17:24 +0000411 memset(p, 0xaa, MINI_SZ);
numist115d6632019-12-12 02:50:07 +0000412# endif
413 pBuf->pNext = db->lookaside.pMiniFree;
414 db->lookaside.pMiniFree = pBuf;
415 return;
416 }
417#endif
drhd879e3e2017-02-13 13:35:55 +0000418#ifdef SQLITE_DEBUG
drh3608f172012-05-21 16:59:16 +0000419 /* Trash all content in the buffer being freed */
drh31f69622019-10-05 14:39:36 +0000420 memset(p, 0xaa, db->lookaside.szTrue);
drh3608f172012-05-21 16:59:16 +0000421#endif
drh174b9a12010-07-26 11:07:20 +0000422 pBuf->pNext = db->lookaside.pFree;
423 db->lookaside.pFree = pBuf;
drh174b9a12010-07-26 11:07:20 +0000424 return;
425 }
drh633e6d52008-07-28 19:34:53 +0000426 }
drhd231aa32014-10-07 15:46:54 +0000427 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000428 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh174b9a12010-07-26 11:07:20 +0000429 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
430 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
431 sqlite3_free(p);
drh633e6d52008-07-28 19:34:53 +0000432}
drhdbd6a7d2017-04-05 12:39:49 +0000433void sqlite3DbFree(sqlite3 *db, void *p){
434 assert( db==0 || sqlite3_mutex_held(db->mutex) );
435 if( p ) sqlite3DbFreeNN(db, p);
436}
drh633e6d52008-07-28 19:34:53 +0000437
438/*
drhfec00ea2008-06-14 16:56:21 +0000439** Change the size of an existing memory allocation
440*/
drhda4ca9d2014-09-09 17:27:35 +0000441void *sqlite3Realloc(void *pOld, u64 nBytes){
shanehca591fe2011-04-15 19:30:42 +0000442 int nOld, nNew, nDiff;
drhfec00ea2008-06-14 16:56:21 +0000443 void *pNew;
drhd231aa32014-10-07 15:46:54 +0000444 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
mistachkind4258642015-03-21 23:38:59 +0000445 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
drhfec00ea2008-06-14 16:56:21 +0000446 if( pOld==0 ){
drh8da47412014-10-03 14:54:47 +0000447 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
drhfec00ea2008-06-14 16:56:21 +0000448 }
drhda4ca9d2014-09-09 17:27:35 +0000449 if( nBytes==0 ){
drh8da47412014-10-03 14:54:47 +0000450 sqlite3_free(pOld); /* IMP: R-26507-47431 */
drhfec00ea2008-06-14 16:56:21 +0000451 return 0;
452 }
drhb6063cf2009-06-27 00:48:33 +0000453 if( nBytes>=0x7fffff00 ){
454 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
455 return 0;
456 }
drhfec00ea2008-06-14 16:56:21 +0000457 nOld = sqlite3MallocSize(pOld);
drh9f129f42010-08-31 15:27:32 +0000458 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
459 ** argument to xRealloc is always a value returned by a prior call to
460 ** xRoundup. */
drhda4ca9d2014-09-09 17:27:35 +0000461 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
drh7c6791c2009-08-18 14:48:53 +0000462 if( nOld==nNew ){
463 pNew = pOld;
464 }else if( sqlite3GlobalConfig.bMemstat ){
drhfec00ea2008-06-14 16:56:21 +0000465 sqlite3_mutex_enter(mem0.mutex);
drhb02392e2015-10-15 15:28:56 +0000466 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
drh8e1bb042011-04-15 16:39:52 +0000467 nDiff = nNew - nOld;
drh1aa34692016-12-27 12:08:36 +0000468 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
drh5fb72e52015-09-10 01:22:09 +0000469 mem0.alarmThreshold-nDiff ){
470 sqlite3MallocAlarm(nDiff);
471 }
drh7c6791c2009-08-18 14:48:53 +0000472 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh5fb72e52015-09-10 01:22:09 +0000473 if( pNew==0 && mem0.alarmThreshold>0 ){
474 sqlite3MallocAlarm((int)nBytes);
danielk1977075c23a2008-09-01 18:34:20 +0000475 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drh7c6791c2009-08-18 14:48:53 +0000476 }
477 if( pNew ){
478 nNew = sqlite3MallocSize(pNew);
drhaf89fe62015-03-23 17:25:18 +0000479 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
drhfec00ea2008-06-14 16:56:21 +0000480 }
481 sqlite3_mutex_leave(mem0.mutex);
482 }else{
drh7c6791c2009-08-18 14:48:53 +0000483 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
drhfec00ea2008-06-14 16:56:21 +0000484 }
drh8da47412014-10-03 14:54:47 +0000485 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
drhfec00ea2008-06-14 16:56:21 +0000486 return pNew;
487}
488
489/*
490** The public interface to sqlite3Realloc. Make sure that the memory
491** subsystem is initialized prior to invoking sqliteRealloc.
492*/
493void *sqlite3_realloc(void *pOld, int n){
494#ifndef SQLITE_OMIT_AUTOINIT
495 if( sqlite3_initialize() ) return 0;
496#endif
drh8da47412014-10-03 14:54:47 +0000497 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
drhda4ca9d2014-09-09 17:27:35 +0000498 return sqlite3Realloc(pOld, n);
499}
500void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
501#ifndef SQLITE_OMIT_AUTOINIT
502 if( sqlite3_initialize() ) return 0;
503#endif
drhfec00ea2008-06-14 16:56:21 +0000504 return sqlite3Realloc(pOld, n);
505}
506
drha3152892007-05-05 11:48:52 +0000507
508/*
drh17435752007-08-16 04:30:38 +0000509** Allocate and zero memory.
drha3152892007-05-05 11:48:52 +0000510*/
drhda4ca9d2014-09-09 17:27:35 +0000511void *sqlite3MallocZero(u64 n){
drhfec00ea2008-06-14 16:56:21 +0000512 void *p = sqlite3Malloc(n);
drha3152892007-05-05 11:48:52 +0000513 if( p ){
drh20f3df02014-09-18 02:20:54 +0000514 memset(p, 0, (size_t)n);
drha3152892007-05-05 11:48:52 +0000515 }
516 return p;
517}
drh17435752007-08-16 04:30:38 +0000518
519/*
520** Allocate and zero memory. If the allocation fails, make
521** the mallocFailed flag in the connection pointer.
522*/
drhda4ca9d2014-09-09 17:27:35 +0000523void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000524 void *p;
525 testcase( db==0 );
526 p = sqlite3DbMallocRaw(db, n);
527 if( p ) memset(p, 0, (size_t)n);
528 return p;
529}
530
531
532/* Finish the work of sqlite3DbMallocRawNN for the unusual and
533** slower case when the allocation cannot be fulfilled using lookaside.
534*/
535static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
536 void *p;
537 assert( db!=0 );
538 p = sqlite3Malloc(n);
539 if( !p ) sqlite3OomFault(db);
540 sqlite3MemdebugSetType(p,
541 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
drh17435752007-08-16 04:30:38 +0000542 return p;
543}
544
545/*
drh1da26a42016-01-20 03:36:32 +0000546** Allocate memory, either lookaside (if possible) or heap.
547** If the allocation fails, set the mallocFailed flag in
548** the connection pointer.
drhddecae72008-10-11 17:35:16 +0000549**
550** If db!=0 and db->mallocFailed is true (indicating a prior malloc
551** failure on the same database connection) then always return 0.
552** Hence for a particular database connection, once malloc starts
553** failing, it fails consistently until mallocFailed is reset.
554** This is an important assumption. There are many places in the
555** code that do things like this:
556**
557** int *a = (int*)sqlite3DbMallocRaw(db, 100);
558** int *b = (int*)sqlite3DbMallocRaw(db, 200);
559** if( b ) a[10] = 9;
560**
561** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
562** that all prior mallocs (ex: "a") worked too.
drh575fad62016-02-05 13:38:36 +0000563**
564** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
565** not a NULL pointer.
drh17435752007-08-16 04:30:38 +0000566*/
drhda4ca9d2014-09-09 17:27:35 +0000567void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
drh575fad62016-02-05 13:38:36 +0000568 void *p;
569 if( db ) return sqlite3DbMallocRawNN(db, n);
570 p = sqlite3Malloc(n);
571 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
572 return p;
573}
574void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
drhf5818aa2016-02-06 19:48:50 +0000575#ifndef SQLITE_OMIT_LOOKASIDE
576 LookasideSlot *pBuf;
drh575fad62016-02-05 13:38:36 +0000577 assert( db!=0 );
578 assert( sqlite3_mutex_held(db->mutex) );
579 assert( db->pnBytesFreed==0 );
drh31f69622019-10-05 14:39:36 +0000580 if( n>db->lookaside.sz ){
581 if( db->lookaside.bDisable ){
582 return db->mallocFailed ? 0 : dbMallocRawFinish(db, n);
drh633e6d52008-07-28 19:34:53 +0000583 }
drh31f69622019-10-05 14:39:36 +0000584 db->lookaside.anStat[1]++;
numist115d6632019-12-12 02:50:07 +0000585# ifndef SQLITE_OMIT_MINI_LOOKASIDE
drh0225d812019-12-12 17:17:24 +0000586 }else if( n<=MINI_SZ ){
numist115d6632019-12-12 02:50:07 +0000587 if( (pBuf = db->lookaside.pMiniFree)!=0 ){
588 db->lookaside.pMiniFree = pBuf->pNext;
589 db->lookaside.anStat[0]++;
590 return (void*)pBuf;
591 }else if( (pBuf = db->lookaside.pMiniInit)!=0 ){
592 db->lookaside.pMiniInit = pBuf->pNext;
593 db->lookaside.anStat[0]++;
594 return (void*)pBuf;
595 }
596# endif
drh31f69622019-10-05 14:39:36 +0000597 }else if( (pBuf = db->lookaside.pFree)!=0 ){
598 db->lookaside.pFree = pBuf->pNext;
599 db->lookaside.anStat[0]++;
600 return (void*)pBuf;
601 }else if( (pBuf = db->lookaside.pInit)!=0 ){
602 db->lookaside.pInit = pBuf->pNext;
603 db->lookaside.anStat[0]++;
604 return (void*)pBuf;
605 }else{
606 db->lookaside.anStat[2]++;
drh633e6d52008-07-28 19:34:53 +0000607 }
drhddecae72008-10-11 17:35:16 +0000608#else
drhf5818aa2016-02-06 19:48:50 +0000609 assert( db!=0 );
610 assert( sqlite3_mutex_held(db->mutex) );
611 assert( db->pnBytesFreed==0 );
drh575fad62016-02-05 13:38:36 +0000612 if( db->mallocFailed ){
drhddecae72008-10-11 17:35:16 +0000613 return 0;
614 }
drh4150ebf2008-10-11 15:38:29 +0000615#endif
drh1da26a42016-01-20 03:36:32 +0000616 return dbMallocRawFinish(db, n);
617}
drh17435752007-08-16 04:30:38 +0000618
drhb84e5742016-02-05 02:42:54 +0000619/* Forward declaration */
620static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
621
danielk197726783a52007-08-29 14:06:22 +0000622/*
623** Resize the block of memory pointed to by p to n bytes. If the
drh633e6d52008-07-28 19:34:53 +0000624** resize fails, set the mallocFailed flag in the connection object.
danielk197726783a52007-08-29 14:06:22 +0000625*/
drhda4ca9d2014-09-09 17:27:35 +0000626void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
drhb84e5742016-02-05 02:42:54 +0000627 assert( db!=0 );
drh575fad62016-02-05 13:38:36 +0000628 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
drhb84e5742016-02-05 02:42:54 +0000629 assert( sqlite3_mutex_held(db->mutex) );
numist115d6632019-12-12 02:50:07 +0000630 if( isLookaside(db,p) && n<lookasideMallocSize(db, p) ) return p;
drhb84e5742016-02-05 02:42:54 +0000631 return dbReallocFinish(db, p, n);
632}
633static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
danielk1977a1644fd2007-08-29 12:31:25 +0000634 void *pNew = 0;
drhd9da78a2009-03-24 15:08:09 +0000635 assert( db!=0 );
drhb84e5742016-02-05 02:42:54 +0000636 assert( p!=0 );
danielk1977a1644fd2007-08-29 12:31:25 +0000637 if( db->mallocFailed==0 ){
drh633e6d52008-07-28 19:34:53 +0000638 if( isLookaside(db, p) ){
drh575fad62016-02-05 13:38:36 +0000639 pNew = sqlite3DbMallocRawNN(db, n);
drh633e6d52008-07-28 19:34:53 +0000640 if( pNew ){
numist115d6632019-12-12 02:50:07 +0000641 memcpy(pNew, p, lookasideMallocSize(db, p));
drh633e6d52008-07-28 19:34:53 +0000642 sqlite3DbFree(db, p);
643 }
644 }else{
drhd231aa32014-10-07 15:46:54 +0000645 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
mistachkind4258642015-03-21 23:38:59 +0000646 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
drh107b56e2010-03-12 16:32:53 +0000647 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
drh3329a632014-09-18 01:21:43 +0000648 pNew = sqlite3_realloc64(p, n);
drh633e6d52008-07-28 19:34:53 +0000649 if( !pNew ){
drh4a642b62016-02-05 01:55:27 +0000650 sqlite3OomFault(db);
drh633e6d52008-07-28 19:34:53 +0000651 }
drhd231aa32014-10-07 15:46:54 +0000652 sqlite3MemdebugSetType(pNew,
drh4a642b62016-02-05 01:55:27 +0000653 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
danielk1977a1644fd2007-08-29 12:31:25 +0000654 }
655 }
656 return pNew;
657}
658
drh17435752007-08-16 04:30:38 +0000659/*
660** Attempt to reallocate p. If the reallocation fails, then free p
661** and set the mallocFailed flag in the database connection.
662*/
drhda4ca9d2014-09-09 17:27:35 +0000663void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
drha3152892007-05-05 11:48:52 +0000664 void *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000665 pNew = sqlite3DbRealloc(db, p, n);
drha3152892007-05-05 11:48:52 +0000666 if( !pNew ){
drh633e6d52008-07-28 19:34:53 +0000667 sqlite3DbFree(db, p);
drha3152892007-05-05 11:48:52 +0000668 }
669 return pNew;
670}
671
drha3152892007-05-05 11:48:52 +0000672/*
673** Make a copy of a string in memory obtained from sqliteMalloc(). These
674** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
675** is because when memory debugging is turned on, these two functions are
676** called via macros that record the current file and line number in the
677** ThreadData structure.
678*/
drh633e6d52008-07-28 19:34:53 +0000679char *sqlite3DbStrDup(sqlite3 *db, const char *z){
drha3152892007-05-05 11:48:52 +0000680 char *zNew;
drh633e6d52008-07-28 19:34:53 +0000681 size_t n;
682 if( z==0 ){
683 return 0;
684 }
drhcee11ad2016-10-17 00:48:06 +0000685 n = strlen(z) + 1;
686 zNew = sqlite3DbMallocRaw(db, n);
drha3152892007-05-05 11:48:52 +0000687 if( zNew ){
688 memcpy(zNew, z, n);
danielk19771e536952007-08-16 10:09:01 +0000689 }
690 return zNew;
691}
drhda4ca9d2014-09-09 17:27:35 +0000692char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
drh633e6d52008-07-28 19:34:53 +0000693 char *zNew;
drh575fad62016-02-05 13:38:36 +0000694 assert( db!=0 );
drh633e6d52008-07-28 19:34:53 +0000695 if( z==0 ){
696 return 0;
697 }
698 assert( (n&0x7fffffff)==n );
drh575fad62016-02-05 13:38:36 +0000699 zNew = sqlite3DbMallocRawNN(db, n+1);
drh633e6d52008-07-28 19:34:53 +0000700 if( zNew ){
drh20f3df02014-09-18 02:20:54 +0000701 memcpy(zNew, z, (size_t)n);
drh633e6d52008-07-28 19:34:53 +0000702 zNew[n] = 0;
danielk19771e536952007-08-16 10:09:01 +0000703 }
704 return zNew;
705}
706
drha3152892007-05-05 11:48:52 +0000707/*
drh9b2e0432017-12-27 19:43:22 +0000708** The text between zStart and zEnd represents a phrase within a larger
709** SQL statement. Make a copy of this phrase in space obtained form
710** sqlite3DbMalloc(). Omit leading and trailing whitespace.
711*/
712char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
713 int n;
714 while( sqlite3Isspace(zStart[0]) ) zStart++;
715 n = (int)(zEnd - zStart);
drhe75d1f52018-01-10 13:58:23 +0000716 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
drh9b2e0432017-12-27 19:43:22 +0000717 return sqlite3DbStrNDup(db, zStart, n);
718}
719
720/*
drh22c17b82015-05-15 04:13:15 +0000721** Free any prior content in *pz and replace it with a copy of zNew.
drha3152892007-05-05 11:48:52 +0000722*/
drh22c17b82015-05-15 04:13:15 +0000723void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
drh633e6d52008-07-28 19:34:53 +0000724 sqlite3DbFree(db, *pz);
drh22c17b82015-05-15 04:13:15 +0000725 *pz = sqlite3DbStrDup(db, zNew);
drha3152892007-05-05 11:48:52 +0000726}
727
drhb50c65d2014-08-23 20:25:53 +0000728/*
drh4a642b62016-02-05 01:55:27 +0000729** Call this routine to record the fact that an OOM (out-of-memory) error
730** has happened. This routine will set db->mallocFailed, and also
731** temporarily disable the lookaside memory allocator and interrupt
732** any running VDBEs.
733*/
734void sqlite3OomFault(sqlite3 *db){
735 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
736 db->mallocFailed = 1;
737 if( db->nVdbeExec>0 ){
738 db->u1.isInterrupted = 1;
739 }
drh31f69622019-10-05 14:39:36 +0000740 DisableLookaside;
drh1cf19752019-02-08 14:55:30 +0000741 if( db->pParse ){
742 db->pParse->rc = SQLITE_NOMEM_BKPT;
743 }
drh4a642b62016-02-05 01:55:27 +0000744 }
745}
746
747/*
748** This routine reactivates the memory allocator and clears the
749** db->mallocFailed flag as necessary.
750**
751** The memory allocator is not restarted if there are running
752** VDBEs.
753*/
754void sqlite3OomClear(sqlite3 *db){
755 if( db->mallocFailed && db->nVdbeExec==0 ){
756 db->mallocFailed = 0;
757 db->u1.isInterrupted = 0;
758 assert( db->lookaside.bDisable>0 );
drh31f69622019-10-05 14:39:36 +0000759 EnableLookaside;
drh4a642b62016-02-05 01:55:27 +0000760 }
761}
762
763/*
drhb50c65d2014-08-23 20:25:53 +0000764** Take actions at the end of an API call to indicate an OOM error
765*/
766static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
drh4a642b62016-02-05 01:55:27 +0000767 sqlite3OomClear(db);
drhb50c65d2014-08-23 20:25:53 +0000768 sqlite3Error(db, SQLITE_NOMEM);
mistachkinfad30392016-02-13 23:43:46 +0000769 return SQLITE_NOMEM_BKPT;
drhb50c65d2014-08-23 20:25:53 +0000770}
drha3152892007-05-05 11:48:52 +0000771
772/*
773** This function must be called before exiting any API function (i.e.
drh17435752007-08-16 04:30:38 +0000774** returning control to the user) that has called sqlite3_malloc or
775** sqlite3_realloc.
drha3152892007-05-05 11:48:52 +0000776**
777** The returned value is normally a copy of the second argument to this
shanebe217792009-03-05 04:20:31 +0000778** function. However, if a malloc() failure has occurred since the previous
drha3152892007-05-05 11:48:52 +0000779** invocation SQLITE_NOMEM is returned instead.
780**
drh597d2b62015-06-30 03:13:47 +0000781** If an OOM as occurred, then the connection error-code (the value
782** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
drha3152892007-05-05 11:48:52 +0000783*/
drha3152892007-05-05 11:48:52 +0000784int sqlite3ApiExit(sqlite3* db, int rc){
drh597d2b62015-06-30 03:13:47 +0000785 /* If the db handle must hold the connection handle mutex here.
786 ** Otherwise the read (and possible write) of db->mallocFailed
danielk1977a1644fd2007-08-29 12:31:25 +0000787 ** is unsafe, as is the call to sqlite3Error().
788 */
drh597d2b62015-06-30 03:13:47 +0000789 assert( db!=0 );
790 assert( sqlite3_mutex_held(db->mutex) );
drhb50c65d2014-08-23 20:25:53 +0000791 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
792 return apiOomError(db);
drha3152892007-05-05 11:48:52 +0000793 }
drhb50c65d2014-08-23 20:25:53 +0000794 return rc & db->errMask;
drha3152892007-05-05 11:48:52 +0000795}