1a3152895Sdrh /*
2a3152895Sdrh ** 2001 September 15
3a3152895Sdrh **
4a3152895Sdrh ** The author disclaims copyright to this source code. In place of
5a3152895Sdrh ** a legal notice, here is a blessing:
6a3152895Sdrh **
7a3152895Sdrh ** May you do good and not evil.
8a3152895Sdrh ** May you find forgiveness for yourself and forgive others.
9a3152895Sdrh ** May you share freely, never taking more than you give.
10a3152895Sdrh **
11a3152895Sdrh *************************************************************************
12fec00eabSdrh **
13a3152895Sdrh ** Memory allocation functions used throughout sqlite.
14a3152895Sdrh */
15a3152895Sdrh #include "sqliteInt.h"
16a3152895Sdrh #include <stdarg.h>
17a3152895Sdrh
18a3152895Sdrh /*
198468024dSdanielk1977 ** Attempt to release up to n bytes of non-essential memory currently
208468024dSdanielk1977 ** held by SQLite. An example of non-essential memory is memory used to
218468024dSdanielk1977 ** cache database pages that are not currently in use.
22a3152895Sdrh */
sqlite3_release_memory(int n)23a3152895Sdrh int sqlite3_release_memory(int n){
2486f8c197Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
259f129f46Sdrh return sqlite3PcacheReleaseMemory(n);
261e536953Sdanielk1977 #else
279f129f46Sdrh /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
289f129f46Sdrh ** is a no-op returning zero if SQLite is not compiled with
299f129f46Sdrh ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
3062c14b34Sdanielk1977 UNUSED_PARAMETER(n);
319f129f46Sdrh return 0;
321e536953Sdanielk1977 #endif
33a3152895Sdrh }
34a3152895Sdrh
35fec00eabSdrh /*
3631999c5cSdrh ** Default value of the hard heap limit. 0 means "no limit".
3731999c5cSdrh */
3831999c5cSdrh #ifndef SQLITE_MAX_MEMORY
3931999c5cSdrh # define SQLITE_MAX_MEMORY 0
4031999c5cSdrh #endif
4131999c5cSdrh
4231999c5cSdrh /*
43fec00eabSdrh ** State information local to the memory allocation subsystem.
44fec00eabSdrh */
455c8f8587Sdanielk1977 static SQLITE_WSD struct Mem0Global {
46fec00eabSdrh sqlite3_mutex *mutex; /* Mutex to serialize access */
474ef299a3Sdrh sqlite3_int64 alarmThreshold; /* The soft heap limit */
4810c0e711Sdrh sqlite3_int64 hardLimit; /* The hard upper bound on memory */
49fec00eabSdrh
50fec00eabSdrh /*
5150d1b5f3Sdrh ** True if heap is nearly "full" where "full" is defined by the
5250d1b5f3Sdrh ** sqlite3_soft_heap_limit() setting.
5350d1b5f3Sdrh */
5450d1b5f3Sdrh int nearlyFull;
5531999c5cSdrh } mem0 = { 0, SQLITE_MAX_MEMORY, SQLITE_MAX_MEMORY, 0 };
565c8f8587Sdanielk1977
575c8f8587Sdanielk1977 #define mem0 GLOBAL(struct Mem0Global, mem0)
58fec00eabSdrh
59fec00eabSdrh /*
60af89fe66Sdrh ** Return the memory allocator mutex. sqlite3_status() needs it.
61af89fe66Sdrh */
sqlite3MallocMutex(void)62af89fe66Sdrh sqlite3_mutex *sqlite3MallocMutex(void){
63af89fe66Sdrh return mem0.mutex;
64af89fe66Sdrh }
65af89fe66Sdrh
66f82ccf64Sdrh #ifndef SQLITE_OMIT_DEPRECATED
67f82ccf64Sdrh /*
685fb72e5fSdrh ** Deprecated external interface. It used to set an alarm callback
695fb72e5fSdrh ** that was invoked when memory usage grew too large. Now it is a
705fb72e5fSdrh ** no-op.
71f82ccf64Sdrh */
sqlite3_memory_alarm(void (* xCallback)(void * pArg,sqlite3_int64 used,int N),void * pArg,sqlite3_int64 iThreshold)72f82ccf64Sdrh int sqlite3_memory_alarm(
73f82ccf64Sdrh void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
74f82ccf64Sdrh void *pArg,
75f82ccf64Sdrh sqlite3_int64 iThreshold
76f82ccf64Sdrh ){
775fb72e5fSdrh (void)xCallback;
785fb72e5fSdrh (void)pArg;
795fb72e5fSdrh (void)iThreshold;
804ef299a3Sdrh return SQLITE_OK;
81f82ccf64Sdrh }
82f82ccf64Sdrh #endif
83f82ccf64Sdrh
84f82ccf64Sdrh /*
8510c0e711Sdrh ** Set the soft heap-size limit for the library. An argument of
8610c0e711Sdrh ** zero disables the limit. A negative argument is a no-op used to
8710c0e711Sdrh ** obtain the return value.
8810c0e711Sdrh **
8910c0e711Sdrh ** The return value is the value of the heap limit just before this
9010c0e711Sdrh ** interface was called.
9110c0e711Sdrh **
9210c0e711Sdrh ** If the hard heap limit is enabled, then the soft heap limit cannot
9310c0e711Sdrh ** be disabled nor raised above the hard heap limit.
94f82ccf64Sdrh */
sqlite3_soft_heap_limit64(sqlite3_int64 n)95f82ccf64Sdrh sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
96f82ccf64Sdrh sqlite3_int64 priorLimit;
975fb72e5fSdrh sqlite3_int64 excess;
985fb72e5fSdrh sqlite3_int64 nUsed;
99f82ccf64Sdrh #ifndef SQLITE_OMIT_AUTOINIT
100de0f1815Sdrh int rc = sqlite3_initialize();
101de0f1815Sdrh if( rc ) return -1;
102f82ccf64Sdrh #endif
103f82ccf64Sdrh sqlite3_mutex_enter(mem0.mutex);
104f82ccf64Sdrh priorLimit = mem0.alarmThreshold;
1055fb72e5fSdrh if( n<0 ){
1064ef299a3Sdrh sqlite3_mutex_leave(mem0.mutex);
107f82ccf64Sdrh return priorLimit;
108f82ccf64Sdrh }
10910c0e711Sdrh if( mem0.hardLimit>0 && (n>mem0.hardLimit || n==0) ){
11010c0e711Sdrh n = mem0.hardLimit;
11110c0e711Sdrh }
1125fb72e5fSdrh mem0.alarmThreshold = n;
1135fb72e5fSdrh nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
11423bef340Sdrh AtomicStore(&mem0.nearlyFull, n>0 && n<=nUsed);
1155fb72e5fSdrh sqlite3_mutex_leave(mem0.mutex);
1165fb72e5fSdrh excess = sqlite3_memory_used() - n;
1175fb72e5fSdrh if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
1185fb72e5fSdrh return priorLimit;
1195fb72e5fSdrh }
sqlite3_soft_heap_limit(int n)120f82ccf64Sdrh void sqlite3_soft_heap_limit(int n){
121f82ccf64Sdrh if( n<0 ) n = 0;
122f82ccf64Sdrh sqlite3_soft_heap_limit64(n);
123f82ccf64Sdrh }
124f82ccf64Sdrh
125f82ccf64Sdrh /*
12610c0e711Sdrh ** Set the hard heap-size limit for the library. An argument of zero
12710c0e711Sdrh ** disables the hard heap limit. A negative argument is a no-op used
12810c0e711Sdrh ** to obtain the return value without affecting the hard heap limit.
12910c0e711Sdrh **
13010c0e711Sdrh ** The return value is the value of the hard heap limit just prior to
13110c0e711Sdrh ** calling this interface.
13210c0e711Sdrh **
13310c0e711Sdrh ** Setting the hard heap limit will also activate the soft heap limit
13410c0e711Sdrh ** and constrain the soft heap limit to be no more than the hard heap
13510c0e711Sdrh ** limit.
13610c0e711Sdrh */
sqlite3_hard_heap_limit64(sqlite3_int64 n)13710c0e711Sdrh sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 n){
13810c0e711Sdrh sqlite3_int64 priorLimit;
13910c0e711Sdrh #ifndef SQLITE_OMIT_AUTOINIT
14010c0e711Sdrh int rc = sqlite3_initialize();
14110c0e711Sdrh if( rc ) return -1;
14210c0e711Sdrh #endif
14310c0e711Sdrh sqlite3_mutex_enter(mem0.mutex);
14410c0e711Sdrh priorLimit = mem0.hardLimit;
14510c0e711Sdrh if( n>=0 ){
14610c0e711Sdrh mem0.hardLimit = n;
14710c0e711Sdrh if( n<mem0.alarmThreshold || mem0.alarmThreshold==0 ){
14810c0e711Sdrh mem0.alarmThreshold = n;
14910c0e711Sdrh }
15010c0e711Sdrh }
15110c0e711Sdrh sqlite3_mutex_leave(mem0.mutex);
15210c0e711Sdrh return priorLimit;
15310c0e711Sdrh }
15410c0e711Sdrh
15510c0e711Sdrh
15610c0e711Sdrh /*
157fec00eabSdrh ** Initialize the memory allocation subsystem.
158fec00eabSdrh */
sqlite3MallocInit(void)159fec00eabSdrh int sqlite3MallocInit(void){
160592f0cb1Sdrh int rc;
161075c23afSdanielk1977 if( sqlite3GlobalConfig.m.xMalloc==0 ){
162fec00eabSdrh sqlite3MemSetDefault();
163fec00eabSdrh }
16459f8c08eSdanielk1977 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
16550d1b5f3Sdrh if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
16601c5c00cSdrh || sqlite3GlobalConfig.nPage<=0 ){
167075c23afSdanielk1977 sqlite3GlobalConfig.pPage = 0;
168075c23afSdanielk1977 sqlite3GlobalConfig.szPage = 0;
1699ac3fe97Sdrh }
170592f0cb1Sdrh rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
171592f0cb1Sdrh if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
172592f0cb1Sdrh return rc;
173fec00eabSdrh }
174fec00eabSdrh
175fec00eabSdrh /*
17650d1b5f3Sdrh ** Return true if the heap is currently under memory pressure - in other
17750d1b5f3Sdrh ** words if the amount of heap used is close to the limit set by
17850d1b5f3Sdrh ** sqlite3_soft_heap_limit().
17950d1b5f3Sdrh */
sqlite3HeapNearlyFull(void)18050d1b5f3Sdrh int sqlite3HeapNearlyFull(void){
1819f603fceSdrh return AtomicLoad(&mem0.nearlyFull);
18250d1b5f3Sdrh }
18350d1b5f3Sdrh
18450d1b5f3Sdrh /*
185fec00eabSdrh ** Deinitialize the memory allocation subsystem.
186fec00eabSdrh */
sqlite3MallocEnd(void)187fec00eabSdrh void sqlite3MallocEnd(void){
1880a549071Sdanielk1977 if( sqlite3GlobalConfig.m.xShutdown ){
189075c23afSdanielk1977 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
1900a549071Sdanielk1977 }
1919ac3fe97Sdrh memset(&mem0, 0, sizeof(mem0));
192fec00eabSdrh }
193fec00eabSdrh
194fec00eabSdrh /*
195fec00eabSdrh ** Return the amount of memory currently checked out.
196fec00eabSdrh */
sqlite3_memory_used(void)197fec00eabSdrh sqlite3_int64 sqlite3_memory_used(void){
198df5e1a00Sdrh sqlite3_int64 res, mx;
199df5e1a00Sdrh sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
200c376a198Sdrh return res;
201fec00eabSdrh }
202fec00eabSdrh
203fec00eabSdrh /*
204fec00eabSdrh ** Return the maximum amount of memory that has ever been
205fec00eabSdrh ** checked out since either the beginning of this process
206fec00eabSdrh ** or since the most recent reset.
207fec00eabSdrh */
sqlite3_memory_highwater(int resetFlag)208fec00eabSdrh sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
209df5e1a00Sdrh sqlite3_int64 res, mx;
210df5e1a00Sdrh sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
211df5e1a00Sdrh return mx;
212fec00eabSdrh }
213fec00eabSdrh
214fec00eabSdrh /*
2155fb72e5fSdrh ** Trigger the alarm
2165fb72e5fSdrh */
sqlite3MallocAlarm(int nByte)2175fb72e5fSdrh static void sqlite3MallocAlarm(int nByte){
2185fb72e5fSdrh if( mem0.alarmThreshold<=0 ) return;
2195fb72e5fSdrh sqlite3_mutex_leave(mem0.mutex);
2205fb72e5fSdrh sqlite3_release_memory(nByte);
2215fb72e5fSdrh sqlite3_mutex_enter(mem0.mutex);
2225fb72e5fSdrh }
2235fb72e5fSdrh
2245fb72e5fSdrh /*
225f7141990Sdrh ** Do a memory allocation with statistics and alarms. Assume the
226f7141990Sdrh ** lock is already held.
227fec00eabSdrh */
mallocWithAlarm(int n,void ** pp)2281d21bac8Sdrh static void mallocWithAlarm(int n, void **pp){
229f7141990Sdrh void *p;
230087a29c7Sdrh int nFull;
231f7141990Sdrh assert( sqlite3_mutex_held(mem0.mutex) );
232087a29c7Sdrh assert( n>0 );
233087a29c7Sdrh
23440b84365Smistachkin /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
235087a29c7Sdrh ** implementation of malloc_good_size(), which must be called in debug
236087a29c7Sdrh ** mode and specifically when the DMD "Dark Matter Detector" is enabled
23740b84365Smistachkin ** or else a crash results. Hence, do not attempt to optimize out the
23840b84365Smistachkin ** following xRoundup() call. */
239087a29c7Sdrh nFull = sqlite3GlobalConfig.m.xRoundup(n);
240087a29c7Sdrh
241b02392e6Sdrh sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
2425fb72e5fSdrh if( mem0.alarmThreshold>0 ){
2435fb72e5fSdrh sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2445fb72e5fSdrh if( nUsed >= mem0.alarmThreshold - nFull ){
24523bef340Sdrh AtomicStore(&mem0.nearlyFull, 1);
2465fb72e5fSdrh sqlite3MallocAlarm(nFull);
24710c0e711Sdrh if( mem0.hardLimit ){
24810c0e711Sdrh nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
24910c0e711Sdrh if( nUsed >= mem0.hardLimit - nFull ){
25010c0e711Sdrh *pp = 0;
25110c0e711Sdrh return;
25210c0e711Sdrh }
25310c0e711Sdrh }
2545fb72e5fSdrh }else{
25523bef340Sdrh AtomicStore(&mem0.nearlyFull, 0);
2565fb72e5fSdrh }
2575fb72e5fSdrh }
258087a29c7Sdrh p = sqlite3GlobalConfig.m.xMalloc(nFull);
25950d1b5f3Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
2605fb72e5fSdrh if( p==0 && mem0.alarmThreshold>0 ){
2615fb72e5fSdrh sqlite3MallocAlarm(nFull);
262087a29c7Sdrh p = sqlite3GlobalConfig.m.xMalloc(nFull);
263fec00eabSdrh }
26450d1b5f3Sdrh #endif
265c702c7ccSdrh if( p ){
266be7a0ceeSdrh nFull = sqlite3MallocSize(p);
267af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
268af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
269c702c7ccSdrh }
270f7141990Sdrh *pp = p;
271fec00eabSdrh }
272f7141990Sdrh
273f7141990Sdrh /*
274*c9099d2dSstephan ** Maximum size of any single memory allocation.
275*c9099d2dSstephan **
276*c9099d2dSstephan ** This is not a limit on the total amount of memory used. This is
277*c9099d2dSstephan ** a limit on the size parameter to sqlite3_malloc() and sqlite3_realloc().
278*c9099d2dSstephan **
279*c9099d2dSstephan ** The upper bound is slightly less than 2GiB: 0x7ffffeff == 2,147,483,391
280*c9099d2dSstephan ** This provides a 256-byte safety margin for defense against 32-bit
281*c9099d2dSstephan ** signed integer overflow bugs when computing memory allocation sizes.
282*c9099d2dSstephan ** Parnoid applications might want to reduce the maximum allocation size
283*c9099d2dSstephan ** further for an even larger safety margin. 0x3fffffff or 0x0fffffff
284*c9099d2dSstephan ** or even smaller would be reasonable upper bounds on the size of a memory
285*c9099d2dSstephan ** allocations for most applications.
286*c9099d2dSstephan */
287*c9099d2dSstephan #ifndef SQLITE_MAX_ALLOCATION_SIZE
288*c9099d2dSstephan # define SQLITE_MAX_ALLOCATION_SIZE 2147483391
289*c9099d2dSstephan #endif
290*c9099d2dSstephan #if SQLITE_MAX_ALLOCATION_SIZE>2147483391
291*c9099d2dSstephan # error Maximum size for SQLITE_MAX_ALLOCATION_SIZE is 2147483391
292*c9099d2dSstephan #endif
293*c9099d2dSstephan
294*c9099d2dSstephan /*
295f7141990Sdrh ** Allocate memory. This routine is like sqlite3_malloc() except that it
296f7141990Sdrh ** assumes the memory subsystem has already been initialized.
297f7141990Sdrh */
sqlite3Malloc(u64 n)298da4ca9d1Sdrh void *sqlite3Malloc(u64 n){
299f7141990Sdrh void *p;
300*c9099d2dSstephan if( n==0 || n>SQLITE_MAX_ALLOCATION_SIZE ){
301f7141990Sdrh p = 0;
302075c23afSdanielk1977 }else if( sqlite3GlobalConfig.bMemstat ){
303f7141990Sdrh sqlite3_mutex_enter(mem0.mutex);
3043329a63aSdrh mallocWithAlarm((int)n, &p);
305fec00eabSdrh sqlite3_mutex_leave(mem0.mutex);
306fec00eabSdrh }else{
307da4ca9d1Sdrh p = sqlite3GlobalConfig.m.xMalloc((int)n);
308fec00eabSdrh }
3098da47419Sdrh assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
310fec00eabSdrh return p;
311fec00eabSdrh }
312fec00eabSdrh
313fec00eabSdrh /*
314fec00eabSdrh ** This version of the memory allocation is for use by the application.
315fec00eabSdrh ** First make sure the memory subsystem is initialized, then do the
316fec00eabSdrh ** allocation.
317fec00eabSdrh */
sqlite3_malloc(int n)318fec00eabSdrh void *sqlite3_malloc(int n){
319fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
320fec00eabSdrh if( sqlite3_initialize() ) return 0;
321fec00eabSdrh #endif
322da4ca9d1Sdrh return n<=0 ? 0 : sqlite3Malloc(n);
323da4ca9d1Sdrh }
sqlite3_malloc64(sqlite3_uint64 n)324da4ca9d1Sdrh void *sqlite3_malloc64(sqlite3_uint64 n){
325da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
326da4ca9d1Sdrh if( sqlite3_initialize() ) return 0;
327da4ca9d1Sdrh #endif
328fec00eabSdrh return sqlite3Malloc(n);
329fec00eabSdrh }
330fec00eabSdrh
331fec00eabSdrh /*
332633e6d57Sdrh ** TRUE if p is a lookaside memory allocation from db
333633e6d57Sdrh */
3344150ebf8Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
isLookaside(sqlite3 * db,const void * p)335b6dad520Sdrh static int isLookaside(sqlite3 *db, const void *p){
336376860baSdrh return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
337633e6d57Sdrh }
3384150ebf8Sdrh #else
3394150ebf8Sdrh #define isLookaside(A,B) 0
3404150ebf8Sdrh #endif
341633e6d57Sdrh
342633e6d57Sdrh /*
343fec00eabSdrh ** Return the size of a memory allocation previously obtained from
344fec00eabSdrh ** sqlite3Malloc() or sqlite3_malloc().
345fec00eabSdrh */
sqlite3MallocSize(const void * p)346b6dad520Sdrh int sqlite3MallocSize(const void *p){
347107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
348b6dad520Sdrh return sqlite3GlobalConfig.m.xSize((void*)p);
349fec00eabSdrh }
lookasideMallocSize(sqlite3 * db,const void * p)350b6dad520Sdrh static int lookasideMallocSize(sqlite3 *db, const void *p){
351cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
352cf014f6cSdrh return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
353115d663cSnumist #else
354115d663cSnumist return db->lookaside.szTrue;
355115d663cSnumist #endif
356115d663cSnumist }
sqlite3DbMallocSize(sqlite3 * db,const void * p)357b6dad520Sdrh int sqlite3DbMallocSize(sqlite3 *db, const void *p){
358039ca6abSdrh assert( p!=0 );
359d879e3ebSdrh #ifdef SQLITE_DEBUG
36017bcb102Sdrh if( db==0 ){
361d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
362d231aa3aSdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
363376860baSdrh }else if( !isLookaside(db,p) ){
364d231aa3aSdrh assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
365d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
366633e6d57Sdrh }
367e6068027Sdrh #endif
368e6068027Sdrh if( db ){
369376860baSdrh if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
370cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
371e6068027Sdrh if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
372e6068027Sdrh assert( sqlite3_mutex_held(db->mutex) );
373cf014f6cSdrh return LOOKASIDE_SMALL;
374e6068027Sdrh }
375e6068027Sdrh #endif
376e6068027Sdrh if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
377e6068027Sdrh assert( sqlite3_mutex_held(db->mutex) );
378e6068027Sdrh return db->lookaside.szTrue;
379e6068027Sdrh }
380e6068027Sdrh }
381e6068027Sdrh }
382b6dad520Sdrh return sqlite3GlobalConfig.m.xSize((void*)p);
38317bcb102Sdrh }
sqlite3_msize(void * p)384da4ca9d1Sdrh sqlite3_uint64 sqlite3_msize(void *p){
385d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
386d231aa3aSdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
387039ca6abSdrh return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
388da4ca9d1Sdrh }
389fec00eabSdrh
390fec00eabSdrh /*
391fec00eabSdrh ** Free memory previously obtained from sqlite3Malloc().
392fec00eabSdrh */
sqlite3_free(void * p)393fec00eabSdrh void sqlite3_free(void *p){
39471a1a0f4Sdrh if( p==0 ) return; /* IMP: R-49053-54554 */
395107b56e8Sdrh assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
396d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
397075c23afSdanielk1977 if( sqlite3GlobalConfig.bMemstat ){
398fec00eabSdrh sqlite3_mutex_enter(mem0.mutex);
399af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
400af89fe66Sdrh sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
401075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p);
402fec00eabSdrh sqlite3_mutex_leave(mem0.mutex);
403fec00eabSdrh }else{
404075c23afSdanielk1977 sqlite3GlobalConfig.m.xFree(p);
405fec00eabSdrh }
406fec00eabSdrh }
407fec00eabSdrh
408fec00eabSdrh /*
409b4586f12Sdrh ** Add the size of memory allocation "p" to the count in
410b4586f12Sdrh ** *db->pnBytesFreed.
411b4586f12Sdrh */
measureAllocationSize(sqlite3 * db,void * p)412b4586f12Sdrh static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
41356d90be1Sdrh *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
414b4586f12Sdrh }
415b4586f12Sdrh
416b4586f12Sdrh /*
417633e6d57Sdrh ** Free memory that might be associated with a particular database
418dbd6a7dcSdrh ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
419dbd6a7dcSdrh ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
420633e6d57Sdrh */
sqlite3DbFreeNN(sqlite3 * db,void * p)421dbd6a7dcSdrh void sqlite3DbFreeNN(sqlite3 *db, void *p){
4227047e25cSdrh assert( db==0 || sqlite3_mutex_held(db->mutex) );
423dbd6a7dcSdrh assert( p!=0 );
424174b9a16Sdrh if( db ){
425e6068027Sdrh if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
426cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
427e6068027Sdrh if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
428e6068027Sdrh LookasideSlot *pBuf = (LookasideSlot*)p;
429376860baSdrh assert( db->pnBytesFreed==0 );
430115d663cSnumist #ifdef SQLITE_DEBUG
431cf014f6cSdrh memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
432115d663cSnumist #endif
433cf014f6cSdrh pBuf->pNext = db->lookaside.pSmallFree;
434cf014f6cSdrh db->lookaside.pSmallFree = pBuf;
435115d663cSnumist return;
436115d663cSnumist }
437cf014f6cSdrh #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
438e6068027Sdrh if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
439e6068027Sdrh LookasideSlot *pBuf = (LookasideSlot*)p;
440376860baSdrh assert( db->pnBytesFreed==0 );
441d879e3ebSdrh #ifdef SQLITE_DEBUG
442e6068027Sdrh memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
4433608f177Sdrh #endif
444633e6d57Sdrh pBuf->pNext = db->lookaside.pFree;
445633e6d57Sdrh db->lookaside.pFree = pBuf;
446174b9a16Sdrh return;
447174b9a16Sdrh }
448174b9a16Sdrh }
449376860baSdrh if( db->pnBytesFreed ){
450376860baSdrh measureAllocationSize(db, p);
451376860baSdrh return;
452376860baSdrh }
453e6068027Sdrh }
454d231aa3aSdrh assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
455d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
456174b9a16Sdrh assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
457107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
458633e6d57Sdrh sqlite3_free(p);
459633e6d57Sdrh }
sqlite3DbNNFreeNN(sqlite3 * db,void * p)46041ce47c4Sdrh void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
46141ce47c4Sdrh assert( db!=0 );
46241ce47c4Sdrh assert( sqlite3_mutex_held(db->mutex) );
46341ce47c4Sdrh assert( p!=0 );
46441ce47c4Sdrh if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
46541ce47c4Sdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
46641ce47c4Sdrh if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
46741ce47c4Sdrh LookasideSlot *pBuf = (LookasideSlot*)p;
468376860baSdrh assert( db->pnBytesFreed==0 );
46941ce47c4Sdrh #ifdef SQLITE_DEBUG
47041ce47c4Sdrh memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
47141ce47c4Sdrh #endif
47241ce47c4Sdrh pBuf->pNext = db->lookaside.pSmallFree;
47341ce47c4Sdrh db->lookaside.pSmallFree = pBuf;
47441ce47c4Sdrh return;
47541ce47c4Sdrh }
47641ce47c4Sdrh #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
47741ce47c4Sdrh if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
47841ce47c4Sdrh LookasideSlot *pBuf = (LookasideSlot*)p;
479376860baSdrh assert( db->pnBytesFreed==0 );
48041ce47c4Sdrh #ifdef SQLITE_DEBUG
48141ce47c4Sdrh memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
48241ce47c4Sdrh #endif
48341ce47c4Sdrh pBuf->pNext = db->lookaside.pFree;
48441ce47c4Sdrh db->lookaside.pFree = pBuf;
48541ce47c4Sdrh return;
48641ce47c4Sdrh }
48741ce47c4Sdrh }
488376860baSdrh if( db->pnBytesFreed ){
489376860baSdrh measureAllocationSize(db, p);
490376860baSdrh return;
491376860baSdrh }
49241ce47c4Sdrh assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
49341ce47c4Sdrh assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
49441ce47c4Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
49541ce47c4Sdrh sqlite3_free(p);
49641ce47c4Sdrh }
sqlite3DbFree(sqlite3 * db,void * p)497dbd6a7dcSdrh void sqlite3DbFree(sqlite3 *db, void *p){
498dbd6a7dcSdrh assert( db==0 || sqlite3_mutex_held(db->mutex) );
499dbd6a7dcSdrh if( p ) sqlite3DbFreeNN(db, p);
500dbd6a7dcSdrh }
501633e6d57Sdrh
502633e6d57Sdrh /*
503fec00eabSdrh ** Change the size of an existing memory allocation
504fec00eabSdrh */
sqlite3Realloc(void * pOld,u64 nBytes)505da4ca9d1Sdrh void *sqlite3Realloc(void *pOld, u64 nBytes){
506ca591febSshaneh int nOld, nNew, nDiff;
507fec00eabSdrh void *pNew;
508d231aa3aSdrh assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
509d425864dSmistachkin assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
510fec00eabSdrh if( pOld==0 ){
5118da47419Sdrh return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
512fec00eabSdrh }
513da4ca9d1Sdrh if( nBytes==0 ){
5148da47419Sdrh sqlite3_free(pOld); /* IMP: R-26507-47431 */
515fec00eabSdrh return 0;
516fec00eabSdrh }
517b6063cf8Sdrh if( nBytes>=0x7fffff00 ){
518b6063cf8Sdrh /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
519b6063cf8Sdrh return 0;
520b6063cf8Sdrh }
521fec00eabSdrh nOld = sqlite3MallocSize(pOld);
5229f129f46Sdrh /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
5239f129f46Sdrh ** argument to xRealloc is always a value returned by a prior call to
5249f129f46Sdrh ** xRoundup. */
525da4ca9d1Sdrh nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
526fec00eabSdrh if( nOld==nNew ){
527fec00eabSdrh pNew = pOld;
5287c6791c8Sdrh }else if( sqlite3GlobalConfig.bMemstat ){
529672f07c6Sdrh sqlite3_int64 nUsed;
5307c6791c8Sdrh sqlite3_mutex_enter(mem0.mutex);
531b02392e6Sdrh sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
5328e1bb041Sdrh nDiff = nNew - nOld;
533672f07c6Sdrh if( nDiff>0 && (nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)) >=
5345fb72e5fSdrh mem0.alarmThreshold-nDiff ){
5355fb72e5fSdrh sqlite3MallocAlarm(nDiff);
536672f07c6Sdrh if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
537672f07c6Sdrh sqlite3_mutex_leave(mem0.mutex);
538672f07c6Sdrh return 0;
539672f07c6Sdrh }
5405fb72e5fSdrh }
541075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
542acc17521Sdrh #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
5435fb72e5fSdrh if( pNew==0 && mem0.alarmThreshold>0 ){
5445fb72e5fSdrh sqlite3MallocAlarm((int)nBytes);
545075c23afSdanielk1977 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
546fec00eabSdrh }
547acc17521Sdrh #endif
548fec00eabSdrh if( pNew ){
549c702c7ccSdrh nNew = sqlite3MallocSize(pNew);
550af89fe66Sdrh sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
551fec00eabSdrh }
552fec00eabSdrh sqlite3_mutex_leave(mem0.mutex);
553fec00eabSdrh }else{
5547c6791c8Sdrh pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
555fec00eabSdrh }
5568da47419Sdrh assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
557fec00eabSdrh return pNew;
558fec00eabSdrh }
559fec00eabSdrh
560fec00eabSdrh /*
561fec00eabSdrh ** The public interface to sqlite3Realloc. Make sure that the memory
562fec00eabSdrh ** subsystem is initialized prior to invoking sqliteRealloc.
563fec00eabSdrh */
sqlite3_realloc(void * pOld,int n)564fec00eabSdrh void *sqlite3_realloc(void *pOld, int n){
565fec00eabSdrh #ifndef SQLITE_OMIT_AUTOINIT
566fec00eabSdrh if( sqlite3_initialize() ) return 0;
567fec00eabSdrh #endif
5688da47419Sdrh if( n<0 ) n = 0; /* IMP: R-26507-47431 */
569da4ca9d1Sdrh return sqlite3Realloc(pOld, n);
570da4ca9d1Sdrh }
sqlite3_realloc64(void * pOld,sqlite3_uint64 n)571da4ca9d1Sdrh void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
572da4ca9d1Sdrh #ifndef SQLITE_OMIT_AUTOINIT
573da4ca9d1Sdrh if( sqlite3_initialize() ) return 0;
574da4ca9d1Sdrh #endif
575fec00eabSdrh return sqlite3Realloc(pOld, n);
576fec00eabSdrh }
577fec00eabSdrh
578a3152895Sdrh
579a3152895Sdrh /*
58017435752Sdrh ** Allocate and zero memory.
581a3152895Sdrh */
sqlite3MallocZero(u64 n)582da4ca9d1Sdrh void *sqlite3MallocZero(u64 n){
583fec00eabSdrh void *p = sqlite3Malloc(n);
584a3152895Sdrh if( p ){
58520f3df04Sdrh memset(p, 0, (size_t)n);
586a3152895Sdrh }
587a3152895Sdrh return p;
588a3152895Sdrh }
58917435752Sdrh
59017435752Sdrh /*
59117435752Sdrh ** Allocate and zero memory. If the allocation fails, make
59217435752Sdrh ** the mallocFailed flag in the connection pointer.
59317435752Sdrh */
sqlite3DbMallocZero(sqlite3 * db,u64 n)594da4ca9d1Sdrh void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
595575fad65Sdrh void *p;
596575fad65Sdrh testcase( db==0 );
597575fad65Sdrh p = sqlite3DbMallocRaw(db, n);
598575fad65Sdrh if( p ) memset(p, 0, (size_t)n);
599575fad65Sdrh return p;
60017435752Sdrh }
601575fad65Sdrh
602575fad65Sdrh
603575fad65Sdrh /* Finish the work of sqlite3DbMallocRawNN for the unusual and
604575fad65Sdrh ** slower case when the allocation cannot be fulfilled using lookaside.
605575fad65Sdrh */
dbMallocRawFinish(sqlite3 * db,u64 n)606575fad65Sdrh static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
607575fad65Sdrh void *p;
608575fad65Sdrh assert( db!=0 );
609575fad65Sdrh p = sqlite3Malloc(n);
610575fad65Sdrh if( !p ) sqlite3OomFault(db);
611575fad65Sdrh sqlite3MemdebugSetType(p,
612575fad65Sdrh (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
61317435752Sdrh return p;
61417435752Sdrh }
61517435752Sdrh
61617435752Sdrh /*
6171da26a48Sdrh ** Allocate memory, either lookaside (if possible) or heap.
6181da26a48Sdrh ** If the allocation fails, set the mallocFailed flag in
6191da26a48Sdrh ** the connection pointer.
620ddecae79Sdrh **
621ddecae79Sdrh ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
622ddecae79Sdrh ** failure on the same database connection) then always return 0.
623ddecae79Sdrh ** Hence for a particular database connection, once malloc starts
624ddecae79Sdrh ** failing, it fails consistently until mallocFailed is reset.
625ddecae79Sdrh ** This is an important assumption. There are many places in the
626ddecae79Sdrh ** code that do things like this:
627ddecae79Sdrh **
628ddecae79Sdrh ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
629ddecae79Sdrh ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
630ddecae79Sdrh ** if( b ) a[10] = 9;
631ddecae79Sdrh **
632ddecae79Sdrh ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
633ddecae79Sdrh ** that all prior mallocs (ex: "a") worked too.
634575fad65Sdrh **
635575fad65Sdrh ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
636575fad65Sdrh ** not a NULL pointer.
63717435752Sdrh */
sqlite3DbMallocRaw(sqlite3 * db,u64 n)638da4ca9d1Sdrh void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
639575fad65Sdrh void *p;
640575fad65Sdrh if( db ) return sqlite3DbMallocRawNN(db, n);
641575fad65Sdrh p = sqlite3Malloc(n);
642575fad65Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
643575fad65Sdrh return p;
644575fad65Sdrh }
sqlite3DbMallocRawNN(sqlite3 * db,u64 n)645575fad65Sdrh void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
646f5818aa5Sdrh #ifndef SQLITE_OMIT_LOOKASIDE
647f5818aa5Sdrh LookasideSlot *pBuf;
648575fad65Sdrh assert( db!=0 );
649575fad65Sdrh assert( sqlite3_mutex_held(db->mutex) );
650575fad65Sdrh assert( db->pnBytesFreed==0 );
6510b12e7f8Sdrh if( n>db->lookaside.sz ){
6521b47c9c9Snumist if( !db->lookaside.bDisable ){
6530b12e7f8Sdrh db->lookaside.anStat[1]++;
6541b47c9c9Snumist }else if( db->mallocFailed ){
6551b47c9c9Snumist return 0;
6561b47c9c9Snumist }
657c947d6a4Snumist return dbMallocRawFinish(db, n);
658c947d6a4Snumist }
659cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
660cf014f6cSdrh if( n<=LOOKASIDE_SMALL ){
661cf014f6cSdrh if( (pBuf = db->lookaside.pSmallFree)!=0 ){
662cf014f6cSdrh db->lookaside.pSmallFree = pBuf->pNext;
663115d663cSnumist db->lookaside.anStat[0]++;
664115d663cSnumist return (void*)pBuf;
665cf014f6cSdrh }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
666cf014f6cSdrh db->lookaside.pSmallInit = pBuf->pNext;
667115d663cSnumist db->lookaside.anStat[0]++;
668115d663cSnumist return (void*)pBuf;
669115d663cSnumist }
670c947d6a4Snumist }
671115d663cSnumist #endif
672c947d6a4Snumist if( (pBuf = db->lookaside.pFree)!=0 ){
673633e6d57Sdrh db->lookaside.pFree = pBuf->pNext;
6740b12e7f8Sdrh db->lookaside.anStat[0]++;
675633e6d57Sdrh return (void*)pBuf;
67652fb8e19Sdrh }else if( (pBuf = db->lookaside.pInit)!=0 ){
67752fb8e19Sdrh db->lookaside.pInit = pBuf->pNext;
67852fb8e19Sdrh db->lookaside.anStat[0]++;
67952fb8e19Sdrh return (void*)pBuf;
68052fb8e19Sdrh }else{
68152fb8e19Sdrh db->lookaside.anStat[2]++;
682633e6d57Sdrh }
683ddecae79Sdrh #else
684f5818aa5Sdrh assert( db!=0 );
685f5818aa5Sdrh assert( sqlite3_mutex_held(db->mutex) );
686f5818aa5Sdrh assert( db->pnBytesFreed==0 );
687575fad65Sdrh if( db->mallocFailed ){
688ddecae79Sdrh return 0;
689ddecae79Sdrh }
6904150ebf8Sdrh #endif
6911da26a48Sdrh return dbMallocRawFinish(db, n);
6921da26a48Sdrh }
69317435752Sdrh
694b84e574cSdrh /* Forward declaration */
695b84e574cSdrh static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
696b84e574cSdrh
69726783a58Sdanielk1977 /*
69826783a58Sdanielk1977 ** Resize the block of memory pointed to by p to n bytes. If the
69926783a58Sdanielk1977 ** resize fails, set the mallocFailed flag in the connection object.
70026783a58Sdanielk1977 */
sqlite3DbRealloc(sqlite3 * db,void * p,u64 n)701da4ca9d1Sdrh void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
702b84e574cSdrh assert( db!=0 );
703575fad65Sdrh if( p==0 ) return sqlite3DbMallocRawNN(db, n);
704b84e574cSdrh assert( sqlite3_mutex_held(db->mutex) );
705e6068027Sdrh if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
706cf014f6cSdrh #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
707e6068027Sdrh if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
708cf014f6cSdrh if( n<=LOOKASIDE_SMALL ) return p;
709e6068027Sdrh }else
710e6068027Sdrh #endif
711e6068027Sdrh if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
712e6068027Sdrh if( n<=db->lookaside.szTrue ) return p;
713e6068027Sdrh }
714e6068027Sdrh }
715b84e574cSdrh return dbReallocFinish(db, p, n);
716b84e574cSdrh }
dbReallocFinish(sqlite3 * db,void * p,u64 n)717b84e574cSdrh static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
718a1644fd8Sdanielk1977 void *pNew = 0;
719d9da78a2Sdrh assert( db!=0 );
720b84e574cSdrh assert( p!=0 );
721a1644fd8Sdanielk1977 if( db->mallocFailed==0 ){
722633e6d57Sdrh if( isLookaside(db, p) ){
723575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, n);
724633e6d57Sdrh if( pNew ){
725115d663cSnumist memcpy(pNew, p, lookasideMallocSize(db, p));
726633e6d57Sdrh sqlite3DbFree(db, p);
727633e6d57Sdrh }
728633e6d57Sdrh }else{
729d231aa3aSdrh assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
730d425864dSmistachkin assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
731107b56e8Sdrh sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
732d924e7bcSdrh pNew = sqlite3Realloc(p, n);
733a1644fd8Sdanielk1977 if( !pNew ){
7344a642b60Sdrh sqlite3OomFault(db);
735a1644fd8Sdanielk1977 }
736d231aa3aSdrh sqlite3MemdebugSetType(pNew,
7374a642b60Sdrh (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
738a1644fd8Sdanielk1977 }
739633e6d57Sdrh }
740a1644fd8Sdanielk1977 return pNew;
741a1644fd8Sdanielk1977 }
742a1644fd8Sdanielk1977
74317435752Sdrh /*
74417435752Sdrh ** Attempt to reallocate p. If the reallocation fails, then free p
74517435752Sdrh ** and set the mallocFailed flag in the database connection.
74617435752Sdrh */
sqlite3DbReallocOrFree(sqlite3 * db,void * p,u64 n)747da4ca9d1Sdrh void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
748a3152895Sdrh void *pNew;
749a1644fd8Sdanielk1977 pNew = sqlite3DbRealloc(db, p, n);
750a3152895Sdrh if( !pNew ){
751633e6d57Sdrh sqlite3DbFree(db, p);
752a3152895Sdrh }
753a3152895Sdrh return pNew;
754a3152895Sdrh }
755a3152895Sdrh
756a3152895Sdrh /*
757a3152895Sdrh ** Make a copy of a string in memory obtained from sqliteMalloc(). These
758a3152895Sdrh ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
759a3152895Sdrh ** is because when memory debugging is turned on, these two functions are
760a3152895Sdrh ** called via macros that record the current file and line number in the
761a3152895Sdrh ** ThreadData structure.
762a3152895Sdrh */
sqlite3DbStrDup(sqlite3 * db,const char * z)763633e6d57Sdrh char *sqlite3DbStrDup(sqlite3 *db, const char *z){
764a3152895Sdrh char *zNew;
765633e6d57Sdrh size_t n;
766633e6d57Sdrh if( z==0 ){
767633e6d57Sdrh return 0;
768a3152895Sdrh }
769cee11adaSdrh n = strlen(z) + 1;
770cee11adaSdrh zNew = sqlite3DbMallocRaw(db, n);
771a3152895Sdrh if( zNew ){
772a3152895Sdrh memcpy(zNew, z, n);
7731e536953Sdanielk1977 }
7741e536953Sdanielk1977 return zNew;
7751e536953Sdanielk1977 }
sqlite3DbStrNDup(sqlite3 * db,const char * z,u64 n)776da4ca9d1Sdrh char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
777633e6d57Sdrh char *zNew;
778575fad65Sdrh assert( db!=0 );
779369e758fSdrh assert( z!=0 || n==0 );
780633e6d57Sdrh assert( (n&0x7fffffff)==n );
781369e758fSdrh zNew = z ? sqlite3DbMallocRawNN(db, n+1) : 0;
782633e6d57Sdrh if( zNew ){
78320f3df04Sdrh memcpy(zNew, z, (size_t)n);
784633e6d57Sdrh zNew[n] = 0;
7851e536953Sdanielk1977 }
7861e536953Sdanielk1977 return zNew;
7871e536953Sdanielk1977 }
7881e536953Sdanielk1977
789a3152895Sdrh /*
7909b2e0435Sdrh ** The text between zStart and zEnd represents a phrase within a larger
7919b2e0435Sdrh ** SQL statement. Make a copy of this phrase in space obtained form
7929b2e0435Sdrh ** sqlite3DbMalloc(). Omit leading and trailing whitespace.
7939b2e0435Sdrh */
sqlite3DbSpanDup(sqlite3 * db,const char * zStart,const char * zEnd)7949b2e0435Sdrh char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
7959b2e0435Sdrh int n;
7969b2e0435Sdrh while( sqlite3Isspace(zStart[0]) ) zStart++;
7979b2e0435Sdrh n = (int)(zEnd - zStart);
798e75d1f52Sdrh while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
7999b2e0435Sdrh return sqlite3DbStrNDup(db, zStart, n);
8009b2e0435Sdrh }
8019b2e0435Sdrh
8029b2e0435Sdrh /*
80322c17b8bSdrh ** Free any prior content in *pz and replace it with a copy of zNew.
804a3152895Sdrh */
sqlite3SetString(char ** pz,sqlite3 * db,const char * zNew)80522c17b8bSdrh void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
806f7413d9aSdan char *z = sqlite3DbStrDup(db, zNew);
807633e6d57Sdrh sqlite3DbFree(db, *pz);
808f7413d9aSdan *pz = z;
809a3152895Sdrh }
810a3152895Sdrh
811b50c65d5Sdrh /*
8124a642b60Sdrh ** Call this routine to record the fact that an OOM (out-of-memory) error
8134a642b60Sdrh ** has happened. This routine will set db->mallocFailed, and also
8144a642b60Sdrh ** temporarily disable the lookaside memory allocator and interrupt
8154a642b60Sdrh ** any running VDBEs.
8163cdb1394Sdrh **
8173cdb1394Sdrh ** Always return a NULL pointer so that this routine can be invoked using
8183cdb1394Sdrh **
8193cdb1394Sdrh ** return sqlite3OomFault(db);
8203cdb1394Sdrh **
8213cdb1394Sdrh ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
8223cdb1394Sdrh ** common case where no OOM occurs.
8234a642b60Sdrh */
sqlite3OomFault(sqlite3 * db)8243cdb1394Sdrh void *sqlite3OomFault(sqlite3 *db){
8254a642b60Sdrh if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
8264a642b60Sdrh db->mallocFailed = 1;
8274a642b60Sdrh if( db->nVdbeExec>0 ){
828892edb69Sdan AtomicStore(&db->u1.isInterrupted, 1);
8294a642b60Sdrh }
83031f69626Sdrh DisableLookaside;
8311cf19758Sdrh if( db->pParse ){
83225bb72a7Sdrh Parse *pParse;
8333cdb1394Sdrh sqlite3ErrorMsg(db->pParse, "out of memory");
8341cf19758Sdrh db->pParse->rc = SQLITE_NOMEM_BKPT;
83525bb72a7Sdrh for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
83625bb72a7Sdrh pParse->nErr++;
83725bb72a7Sdrh pParse->rc = SQLITE_NOMEM;
83825bb72a7Sdrh }
8391cf19758Sdrh }
8404a642b60Sdrh }
8413cdb1394Sdrh return 0;
8424a642b60Sdrh }
8434a642b60Sdrh
8444a642b60Sdrh /*
8454a642b60Sdrh ** This routine reactivates the memory allocator and clears the
8464a642b60Sdrh ** db->mallocFailed flag as necessary.
8474a642b60Sdrh **
8484a642b60Sdrh ** The memory allocator is not restarted if there are running
8494a642b60Sdrh ** VDBEs.
8504a642b60Sdrh */
sqlite3OomClear(sqlite3 * db)8514a642b60Sdrh void sqlite3OomClear(sqlite3 *db){
8524a642b60Sdrh if( db->mallocFailed && db->nVdbeExec==0 ){
8534a642b60Sdrh db->mallocFailed = 0;
854892edb69Sdan AtomicStore(&db->u1.isInterrupted, 0);
8554a642b60Sdrh assert( db->lookaside.bDisable>0 );
85631f69626Sdrh EnableLookaside;
8574a642b60Sdrh }
8584a642b60Sdrh }
8594a642b60Sdrh
8604a642b60Sdrh /*
8615a07d10fSdrh ** Take actions at the end of an API call to deal with error codes.
862b50c65d5Sdrh */
apiHandleError(sqlite3 * db,int rc)8635a07d10fSdrh static SQLITE_NOINLINE int apiHandleError(sqlite3 *db, int rc){
8645a07d10fSdrh if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
8654a642b60Sdrh sqlite3OomClear(db);
866b50c65d5Sdrh sqlite3Error(db, SQLITE_NOMEM);
867fad3039cSmistachkin return SQLITE_NOMEM_BKPT;
868b50c65d5Sdrh }
8695a07d10fSdrh return rc & db->errMask;
8705a07d10fSdrh }
871a3152895Sdrh
872a3152895Sdrh /*
873a3152895Sdrh ** This function must be called before exiting any API function (i.e.
87417435752Sdrh ** returning control to the user) that has called sqlite3_malloc or
87517435752Sdrh ** sqlite3_realloc.
876a3152895Sdrh **
877a3152895Sdrh ** The returned value is normally a copy of the second argument to this
878be217793Sshane ** function. However, if a malloc() failure has occurred since the previous
879a3152895Sdrh ** invocation SQLITE_NOMEM is returned instead.
880a3152895Sdrh **
881597d2b64Sdrh ** If an OOM as occurred, then the connection error-code (the value
882597d2b64Sdrh ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
883a3152895Sdrh */
sqlite3ApiExit(sqlite3 * db,int rc)884a3152895Sdrh int sqlite3ApiExit(sqlite3* db, int rc){
885597d2b64Sdrh /* If the db handle must hold the connection handle mutex here.
886597d2b64Sdrh ** Otherwise the read (and possible write) of db->mallocFailed
887a1644fd8Sdanielk1977 ** is unsafe, as is the call to sqlite3Error().
888a1644fd8Sdanielk1977 */
889597d2b64Sdrh assert( db!=0 );
890597d2b64Sdrh assert( sqlite3_mutex_held(db->mutex) );
8915a07d10fSdrh if( db->mallocFailed || rc ){
8925a07d10fSdrh return apiHandleError(db, rc);
893a3152895Sdrh }
894b50c65d5Sdrh return rc & db->errMask;
895a3152895Sdrh }
896