2007-05-05 15:48:52 +04:00
|
|
|
/*
|
|
|
|
** 2001 September 15
|
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
**
|
2008-06-14 20:56:21 +04:00
|
|
|
** Memory allocation functions used throughout sqlite.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
/*
|
2008-06-23 15:11:35 +04:00
|
|
|
** Attempt to release up to n bytes of non-essential memory currently
|
|
|
|
** held by SQLite. An example of non-essential memory is memory used to
|
|
|
|
** cache database pages that are not currently in use.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
|
|
|
int sqlite3_release_memory(int n){
|
2007-08-22 04:39:19 +04:00
|
|
|
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
|
2010-08-31 19:27:32 +04:00
|
|
|
return sqlite3PcacheReleaseMemory(n);
|
2007-08-16 14:09:01 +04:00
|
|
|
#else
|
2010-08-31 19:27:32 +04:00
|
|
|
/* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
|
|
|
|
** is a no-op returning zero if SQLite is not compiled with
|
|
|
|
** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
|
2008-11-19 12:05:26 +03:00
|
|
|
UNUSED_PARAMETER(n);
|
2010-08-31 19:27:32 +04:00
|
|
|
return 0;
|
2007-08-16 14:09:01 +04:00
|
|
|
#endif
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
|
2010-08-27 21:16:44 +04:00
|
|
|
/*
|
|
|
|
** An instance of the following object records the location of
|
|
|
|
** each unused scratch buffer.
|
|
|
|
*/
|
|
|
|
typedef struct ScratchFreeslot {
|
|
|
|
struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
|
|
|
|
} ScratchFreeslot;
|
|
|
|
|
2008-06-14 20:56:21 +04:00
|
|
|
/*
|
|
|
|
** State information local to the memory allocation subsystem.
|
|
|
|
*/
|
2008-09-02 14:22:00 +04:00
|
|
|
static SQLITE_WSD struct Mem0Global {
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3_mutex *mutex; /* Mutex to serialize access */
|
2015-09-10 04:22:09 +03:00
|
|
|
sqlite3_int64 alarmThreshold; /* The soft heap limit */
|
2008-06-14 20:56:21 +04:00
|
|
|
|
2008-06-18 22:12:04 +04:00
|
|
|
/*
|
2010-08-27 21:16:44 +04:00
|
|
|
** Pointers to the end of sqlite3GlobalConfig.pScratch memory
|
|
|
|
** (so that a range test can be used to determine if an allocation
|
|
|
|
** being freed came from pScratch) and a pointer to the list of
|
|
|
|
** unused scratch allocations.
|
2008-06-18 22:12:04 +04:00
|
|
|
*/
|
2010-08-27 21:16:44 +04:00
|
|
|
void *pScratchEnd;
|
|
|
|
ScratchFreeslot *pScratchFree;
|
|
|
|
u32 nScratchFree;
|
2010-08-27 16:21:06 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** True if heap is nearly "full" where "full" is defined by the
|
|
|
|
** sqlite3_soft_heap_limit() setting.
|
|
|
|
*/
|
|
|
|
int nearlyFull;
|
2015-09-02 17:56:56 +03:00
|
|
|
} mem0 = { 0, 0, 0, 0, 0, 0 };
|
2008-09-02 14:22:00 +04:00
|
|
|
|
|
|
|
#define mem0 GLOBAL(struct Mem0Global, mem0)
|
2008-06-14 20:56:21 +04:00
|
|
|
|
2015-03-23 20:25:18 +03:00
|
|
|
/*
|
|
|
|
** Return the memory allocator mutex. sqlite3_status() needs it.
|
|
|
|
*/
|
|
|
|
sqlite3_mutex *sqlite3MallocMutex(void){
|
|
|
|
return mem0.mutex;
|
|
|
|
}
|
|
|
|
|
2010-09-15 21:54:31 +04:00
|
|
|
#ifndef SQLITE_OMIT_DEPRECATED
|
|
|
|
/*
|
2015-09-10 04:22:09 +03:00
|
|
|
** Deprecated external interface. It used to set an alarm callback
|
|
|
|
** that was invoked when memory usage grew too large. Now it is a
|
|
|
|
** no-op.
|
2010-09-15 21:54:31 +04:00
|
|
|
*/
|
|
|
|
int sqlite3_memory_alarm(
|
|
|
|
void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
|
|
|
|
void *pArg,
|
|
|
|
sqlite3_int64 iThreshold
|
|
|
|
){
|
2015-09-10 04:22:09 +03:00
|
|
|
(void)xCallback;
|
|
|
|
(void)pArg;
|
|
|
|
(void)iThreshold;
|
2015-09-02 17:56:56 +03:00
|
|
|
return SQLITE_OK;
|
2010-09-15 21:54:31 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Set the soft heap-size limit for the library. Passing a zero or
|
|
|
|
** negative value indicates no limit.
|
|
|
|
*/
|
|
|
|
sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
|
|
|
|
sqlite3_int64 priorLimit;
|
2015-09-10 04:22:09 +03:00
|
|
|
sqlite3_int64 excess;
|
|
|
|
sqlite3_int64 nUsed;
|
2010-09-15 21:54:31 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
2011-12-22 21:10:35 +04:00
|
|
|
int rc = sqlite3_initialize();
|
|
|
|
if( rc ) return -1;
|
2010-09-15 21:54:31 +04:00
|
|
|
#endif
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
priorLimit = mem0.alarmThreshold;
|
2015-09-10 04:22:09 +03:00
|
|
|
if( n<0 ){
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
return priorLimit;
|
2010-09-15 21:54:31 +04:00
|
|
|
}
|
2015-09-10 04:22:09 +03:00
|
|
|
mem0.alarmThreshold = n;
|
|
|
|
nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
|
|
|
|
mem0.nearlyFull = (n>0 && n<=nUsed);
|
2015-09-02 17:56:56 +03:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
2015-09-10 04:22:09 +03:00
|
|
|
excess = sqlite3_memory_used() - n;
|
|
|
|
if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
|
2010-09-15 21:54:31 +04:00
|
|
|
return priorLimit;
|
|
|
|
}
|
|
|
|
void sqlite3_soft_heap_limit(int n){
|
|
|
|
if( n<0 ) n = 0;
|
|
|
|
sqlite3_soft_heap_limit64(n);
|
|
|
|
}
|
|
|
|
|
2008-06-14 20:56:21 +04:00
|
|
|
/*
|
|
|
|
** Initialize the memory allocation subsystem.
|
|
|
|
*/
|
|
|
|
int sqlite3MallocInit(void){
|
2015-03-26 20:04:23 +03:00
|
|
|
int rc;
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.m.xMalloc==0 ){
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3MemSetDefault();
|
|
|
|
}
|
|
|
|
memset(&mem0, 0, sizeof(mem0));
|
2015-10-15 20:21:35 +03:00
|
|
|
mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
|
2010-09-02 22:13:00 +04:00
|
|
|
&& sqlite3GlobalConfig.nScratch>0 ){
|
2010-08-27 21:16:44 +04:00
|
|
|
int i, n, sz;
|
|
|
|
ScratchFreeslot *pSlot;
|
|
|
|
sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
|
|
|
|
sqlite3GlobalConfig.szScratch = sz;
|
|
|
|
pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
|
|
|
|
n = sqlite3GlobalConfig.nScratch;
|
|
|
|
mem0.pScratchFree = pSlot;
|
|
|
|
mem0.nScratchFree = n;
|
|
|
|
for(i=0; i<n-1; i++){
|
|
|
|
pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
|
|
|
|
pSlot = pSlot->pNext;
|
|
|
|
}
|
|
|
|
pSlot->pNext = 0;
|
|
|
|
mem0.pScratchEnd = (void*)&pSlot[1];
|
2008-06-18 22:12:04 +04:00
|
|
|
}else{
|
2010-08-27 21:16:44 +04:00
|
|
|
mem0.pScratchEnd = 0;
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.pScratch = 0;
|
|
|
|
sqlite3GlobalConfig.szScratch = 0;
|
2010-08-27 21:16:44 +04:00
|
|
|
sqlite3GlobalConfig.nScratch = 0;
|
2008-06-18 22:12:04 +04:00
|
|
|
}
|
2010-08-27 16:21:06 +04:00
|
|
|
if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
|
2015-07-04 21:15:04 +03:00
|
|
|
|| sqlite3GlobalConfig.nPage<=0 ){
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.pPage = 0;
|
|
|
|
sqlite3GlobalConfig.szPage = 0;
|
2008-06-18 22:12:04 +04:00
|
|
|
}
|
2015-03-26 20:04:23 +03:00
|
|
|
rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
|
|
|
|
if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
|
|
|
|
return rc;
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
|
2010-08-27 16:21:06 +04:00
|
|
|
/*
|
|
|
|
** Return true if the heap is currently under memory pressure - in other
|
|
|
|
** words if the amount of heap used is close to the limit set by
|
|
|
|
** sqlite3_soft_heap_limit().
|
|
|
|
*/
|
|
|
|
int sqlite3HeapNearlyFull(void){
|
|
|
|
return mem0.nearlyFull;
|
|
|
|
}
|
|
|
|
|
2008-06-14 20:56:21 +04:00
|
|
|
/*
|
|
|
|
** Deinitialize the memory allocation subsystem.
|
|
|
|
*/
|
|
|
|
void sqlite3MallocEnd(void){
|
2009-02-17 19:29:10 +03:00
|
|
|
if( sqlite3GlobalConfig.m.xShutdown ){
|
|
|
|
sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
|
|
|
|
}
|
2008-06-18 22:12:04 +04:00
|
|
|
memset(&mem0, 0, sizeof(mem0));
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the amount of memory currently checked out.
|
|
|
|
*/
|
|
|
|
sqlite3_int64 sqlite3_memory_used(void){
|
2015-05-10 05:01:08 +03:00
|
|
|
sqlite3_int64 res, mx;
|
|
|
|
sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
|
2008-07-14 16:30:54 +04:00
|
|
|
return res;
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the maximum amount of memory that has ever been
|
|
|
|
** checked out since either the beginning of this process
|
|
|
|
** or since the most recent reset.
|
|
|
|
*/
|
|
|
|
sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
|
2015-05-10 05:01:08 +03:00
|
|
|
sqlite3_int64 res, mx;
|
|
|
|
sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
|
|
|
|
return mx;
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
|
2015-09-10 04:22:09 +03:00
|
|
|
/*
|
|
|
|
** Trigger the alarm
|
|
|
|
*/
|
|
|
|
static void sqlite3MallocAlarm(int nByte){
|
|
|
|
if( mem0.alarmThreshold<=0 ) return;
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
sqlite3_release_memory(nByte);
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
}
|
|
|
|
|
2008-06-19 04:16:08 +04:00
|
|
|
/*
|
|
|
|
** Do a memory allocation with statistics and alarms. Assume the
|
|
|
|
** lock is already held.
|
|
|
|
*/
|
2017-01-10 19:09:46 +03:00
|
|
|
static void mallocWithAlarm(int n, void **pp){
|
2008-06-19 04:16:08 +04:00
|
|
|
void *p;
|
2017-02-08 19:01:57 +03:00
|
|
|
int nFull;
|
2008-06-19 04:16:08 +04:00
|
|
|
assert( sqlite3_mutex_held(mem0.mutex) );
|
2017-02-08 19:01:57 +03:00
|
|
|
assert( n>0 );
|
|
|
|
|
|
|
|
/* In Firefox (circa 2017-02-08), xRoundup is remapped to an internal
|
|
|
|
** implementation of malloc_good_size(), which must be called in debug
|
|
|
|
** mode and specifically when the DMD "Dark Matter Detector" is enabled
|
|
|
|
** or else a crash results. Hence, do not attempt to optimization out
|
|
|
|
** the following xRoundup() call. */
|
|
|
|
nFull = sqlite3GlobalConfig.m.xRoundup(n);
|
|
|
|
|
2015-10-15 18:28:56 +03:00
|
|
|
sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
|
2015-09-10 04:22:09 +03:00
|
|
|
if( mem0.alarmThreshold>0 ){
|
|
|
|
sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
|
|
|
|
if( nUsed >= mem0.alarmThreshold - nFull ){
|
|
|
|
mem0.nearlyFull = 1;
|
|
|
|
sqlite3MallocAlarm(nFull);
|
|
|
|
}else{
|
|
|
|
mem0.nearlyFull = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 19:01:57 +03:00
|
|
|
p = sqlite3GlobalConfig.m.xMalloc(nFull);
|
2010-08-27 16:21:06 +04:00
|
|
|
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
|
2015-09-10 04:22:09 +03:00
|
|
|
if( p==0 && mem0.alarmThreshold>0 ){
|
|
|
|
sqlite3MallocAlarm(nFull);
|
2017-02-08 19:01:57 +03:00
|
|
|
p = sqlite3GlobalConfig.m.xMalloc(nFull);
|
2008-06-19 04:16:08 +04:00
|
|
|
}
|
2010-08-27 16:21:06 +04:00
|
|
|
#endif
|
2008-07-18 22:56:16 +04:00
|
|
|
if( p ){
|
2017-01-13 15:53:35 +03:00
|
|
|
nFull = sqlite3MallocSize(p);
|
2015-03-23 20:25:18 +03:00
|
|
|
sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
|
|
|
|
sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
|
2008-07-18 22:56:16 +04:00
|
|
|
}
|
2008-06-19 04:16:08 +04:00
|
|
|
*pp = p;
|
|
|
|
}
|
2008-06-14 20:56:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Allocate memory. This routine is like sqlite3_malloc() except that it
|
|
|
|
** assumes the memory subsystem has already been initialized.
|
|
|
|
*/
|
2014-09-09 21:27:35 +04:00
|
|
|
void *sqlite3Malloc(u64 n){
|
2008-06-14 20:56:21 +04:00
|
|
|
void *p;
|
2014-09-09 21:27:35 +04:00
|
|
|
if( n==0 || n>=0x7fffff00 ){
|
2009-06-26 22:35:16 +04:00
|
|
|
/* A memory allocation of a number of bytes which is near the maximum
|
|
|
|
** signed integer value might cause an integer overflow inside of the
|
|
|
|
** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
|
|
|
|
** 255 bytes of overhead. SQLite itself will never use anything near
|
|
|
|
** this amount. The only way to reach the limit is with sqlite3_malloc() */
|
2008-06-19 04:16:08 +04:00
|
|
|
p = 0;
|
2008-09-01 22:34:20 +04:00
|
|
|
}else if( sqlite3GlobalConfig.bMemstat ){
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2014-09-18 05:21:43 +04:00
|
|
|
mallocWithAlarm((int)n, &p);
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
2014-09-09 21:27:35 +04:00
|
|
|
p = sqlite3GlobalConfig.m.xMalloc((int)n);
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
2014-10-03 18:54:47 +04:00
|
|
|
assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
|
2008-06-14 20:56:21 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This version of the memory allocation is for use by the application.
|
|
|
|
** First make sure the memory subsystem is initialized, then do the
|
|
|
|
** allocation.
|
|
|
|
*/
|
|
|
|
void *sqlite3_malloc(int n){
|
2014-09-09 21:27:35 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
if( sqlite3_initialize() ) return 0;
|
|
|
|
#endif
|
|
|
|
return n<=0 ? 0 : sqlite3Malloc(n);
|
|
|
|
}
|
|
|
|
void *sqlite3_malloc64(sqlite3_uint64 n){
|
2008-06-14 20:56:21 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
if( sqlite3_initialize() ) return 0;
|
|
|
|
#endif
|
|
|
|
return sqlite3Malloc(n);
|
|
|
|
}
|
|
|
|
|
2008-06-15 06:51:47 +04:00
|
|
|
/*
|
|
|
|
** Each thread may only have a single outstanding allocation from
|
2008-06-17 19:12:00 +04:00
|
|
|
** xScratchMalloc(). We verify this constraint in the single-threaded
|
|
|
|
** case by setting scratchAllocOut to 1 when an allocation
|
2008-06-15 06:51:47 +04:00
|
|
|
** is outstanding clearing it when the allocation is freed.
|
|
|
|
*/
|
|
|
|
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
|
2008-06-17 19:12:00 +04:00
|
|
|
static int scratchAllocOut = 0;
|
2008-06-15 06:51:47 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Allocate memory that is to be used and released right away.
|
|
|
|
** This routine is similar to alloca() in that it is not intended
|
|
|
|
** for situations where the memory might be held long-term. This
|
|
|
|
** routine is intended to get memory to old large transient data
|
|
|
|
** structures that would not normally fit on the stack of an
|
|
|
|
** embedded processor.
|
|
|
|
*/
|
2008-06-17 19:12:00 +04:00
|
|
|
void *sqlite3ScratchMalloc(int n){
|
2008-06-15 06:51:47 +04:00
|
|
|
void *p;
|
|
|
|
assert( n>0 );
|
2008-06-18 22:12:04 +04:00
|
|
|
|
2010-08-27 21:16:44 +04:00
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2015-10-15 18:28:56 +03:00
|
|
|
sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
|
2010-08-27 21:16:44 +04:00
|
|
|
if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
|
|
|
|
p = mem0.pScratchFree;
|
|
|
|
mem0.pScratchFree = mem0.pScratchFree->pNext;
|
|
|
|
mem0.nScratchFree--;
|
2015-03-23 20:25:18 +03:00
|
|
|
sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
|
2010-09-02 14:08:41 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
2010-08-27 21:16:44 +04:00
|
|
|
}else{
|
2014-08-23 23:04:55 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
p = sqlite3Malloc(n);
|
|
|
|
if( sqlite3GlobalConfig.bMemstat && p ){
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2015-03-23 20:25:18 +03:00
|
|
|
sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
|
2010-09-02 14:08:41 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
2008-06-19 04:16:08 +04:00
|
|
|
}
|
2010-08-27 21:16:44 +04:00
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
|
2008-06-15 06:51:47 +04:00
|
|
|
}
|
2010-09-02 21:15:19 +04:00
|
|
|
assert( sqlite3_mutex_notheld(mem0.mutex) );
|
2010-09-02 14:08:41 +04:00
|
|
|
|
2010-08-27 21:16:44 +04:00
|
|
|
|
2008-06-19 04:16:08 +04:00
|
|
|
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
|
2014-11-04 17:22:27 +03:00
|
|
|
/* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
|
|
|
|
** buffers per thread.
|
|
|
|
**
|
|
|
|
** This can only be checked in single-threaded mode.
|
|
|
|
*/
|
|
|
|
assert( scratchAllocOut==0 );
|
2010-08-27 21:16:44 +04:00
|
|
|
if( p ) scratchAllocOut++;
|
2008-06-19 04:16:08 +04:00
|
|
|
#endif
|
|
|
|
|
2008-06-15 06:51:47 +04:00
|
|
|
return p;
|
|
|
|
}
|
2008-06-17 19:12:00 +04:00
|
|
|
void sqlite3ScratchFree(void *p){
|
2008-06-15 06:51:47 +04:00
|
|
|
if( p ){
|
2010-08-27 21:16:44 +04:00
|
|
|
|
|
|
|
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
|
|
|
|
/* Verify that no more than two scratch allocation per thread
|
|
|
|
** is outstanding at one time. (This is only checked in the
|
|
|
|
** single-threaded case since checking in the multi-threaded case
|
|
|
|
** would be much more complicated.) */
|
|
|
|
assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
|
|
|
|
scratchAllocOut--;
|
|
|
|
#endif
|
|
|
|
|
2015-12-10 18:09:17 +03:00
|
|
|
if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
|
2010-08-27 21:16:44 +04:00
|
|
|
/* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
|
|
|
|
ScratchFreeslot *pSlot;
|
|
|
|
pSlot = (ScratchFreeslot*)p;
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
pSlot->pNext = mem0.pScratchFree;
|
|
|
|
mem0.pScratchFree = pSlot;
|
|
|
|
mem0.nScratchFree++;
|
2011-04-06 02:08:24 +04:00
|
|
|
assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
|
2015-03-23 20:25:18 +03:00
|
|
|
sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
|
2010-08-27 21:16:44 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
/* Release memory back to the heap */
|
2010-03-12 19:32:53 +03:00
|
|
|
assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
|
2015-03-22 02:38:59 +03:00
|
|
|
assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
|
2010-03-12 19:32:53 +03:00
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.bMemstat ){
|
2008-06-19 04:16:08 +04:00
|
|
|
int iSize = sqlite3MallocSize(p);
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2015-03-23 20:25:18 +03:00
|
|
|
sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
|
|
|
|
sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
|
|
|
|
sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.m.xFree(p);
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.m.xFree(p);
|
2008-06-19 04:16:08 +04:00
|
|
|
}
|
2008-06-18 22:12:04 +04:00
|
|
|
}
|
2008-06-15 06:51:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
/*
|
|
|
|
** TRUE if p is a lookaside memory allocation from db
|
|
|
|
*/
|
2008-10-11 19:38:29 +04:00
|
|
|
#ifndef SQLITE_OMIT_LOOKASIDE
|
2008-07-28 23:34:53 +04:00
|
|
|
static int isLookaside(sqlite3 *db, void *p){
|
2015-12-10 18:09:17 +03:00
|
|
|
return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
2008-10-11 19:38:29 +04:00
|
|
|
#else
|
|
|
|
#define isLookaside(A,B) 0
|
|
|
|
#endif
|
2008-07-28 23:34:53 +04:00
|
|
|
|
2008-06-14 20:56:21 +04:00
|
|
|
/*
|
|
|
|
** Return the size of a memory allocation previously obtained from
|
|
|
|
** sqlite3Malloc() or sqlite3_malloc().
|
|
|
|
*/
|
|
|
|
int sqlite3MallocSize(void *p){
|
2010-03-12 19:32:53 +03:00
|
|
|
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
2008-09-01 22:34:20 +04:00
|
|
|
return sqlite3GlobalConfig.m.xSize(p);
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
int sqlite3DbMallocSize(sqlite3 *db, void *p){
|
2015-10-15 19:20:57 +03:00
|
|
|
assert( p!=0 );
|
2015-09-01 23:09:33 +03:00
|
|
|
if( db==0 || !isLookaside(db,p) ){
|
|
|
|
#if SQLITE_DEBUG
|
|
|
|
if( db==0 ){
|
|
|
|
assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
|
|
|
|
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
2014-09-19 01:25:33 +04:00
|
|
|
}else{
|
2014-10-07 19:46:54 +04:00
|
|
|
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
2015-03-22 02:38:59 +03:00
|
|
|
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
2014-09-19 01:25:33 +04:00
|
|
|
}
|
2015-09-01 23:09:33 +03:00
|
|
|
#endif
|
|
|
|
return sqlite3GlobalConfig.m.xSize(p);
|
|
|
|
}else{
|
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
|
|
|
return db->lookaside.sz;
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-09 21:27:35 +04:00
|
|
|
sqlite3_uint64 sqlite3_msize(void *p){
|
2015-03-22 02:38:59 +03:00
|
|
|
assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
|
2014-10-07 19:46:54 +04:00
|
|
|
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
2015-10-15 19:20:57 +03:00
|
|
|
return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
|
2014-09-09 21:27:35 +04:00
|
|
|
}
|
2008-06-14 20:56:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Free memory previously obtained from sqlite3Malloc().
|
|
|
|
*/
|
|
|
|
void sqlite3_free(void *p){
|
2010-09-11 20:15:55 +04:00
|
|
|
if( p==0 ) return; /* IMP: R-49053-54554 */
|
2010-03-12 19:32:53 +03:00
|
|
|
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
|
2015-03-22 02:38:59 +03:00
|
|
|
assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.bMemstat ){
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2015-03-23 20:25:18 +03:00
|
|
|
sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
|
|
|
|
sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.m.xFree(p);
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.m.xFree(p);
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 23:42:06 +04:00
|
|
|
/*
|
|
|
|
** Add the size of memory allocation "p" to the count in
|
|
|
|
** *db->pnBytesFreed.
|
|
|
|
*/
|
|
|
|
static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
|
2015-10-26 15:55:56 +03:00
|
|
|
*db->pnBytesFreed += sqlite3DbMallocSize(db,p);
|
2014-08-23 23:42:06 +04:00
|
|
|
}
|
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
/*
|
|
|
|
** Free memory that might be associated with a particular database
|
|
|
|
** connection.
|
|
|
|
*/
|
|
|
|
void sqlite3DbFree(sqlite3 *db, void *p){
|
2009-03-23 20:49:14 +03:00
|
|
|
assert( db==0 || sqlite3_mutex_held(db->mutex) );
|
2013-09-13 20:36:46 +04:00
|
|
|
if( p==0 ) return;
|
2010-07-26 15:07:20 +04:00
|
|
|
if( db ){
|
|
|
|
if( db->pnBytesFreed ){
|
2014-08-23 23:42:06 +04:00
|
|
|
measureAllocationSize(db, p);
|
2010-07-26 15:07:20 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-07-24 15:28:28 +04:00
|
|
|
if( isLookaside(db, p) ){
|
2010-07-26 15:07:20 +04:00
|
|
|
LookasideSlot *pBuf = (LookasideSlot*)p;
|
2012-05-21 20:59:16 +04:00
|
|
|
#if SQLITE_DEBUG
|
|
|
|
/* Trash all content in the buffer being freed */
|
|
|
|
memset(p, 0xaa, db->lookaside.sz);
|
|
|
|
#endif
|
2010-07-26 15:07:20 +04:00
|
|
|
pBuf->pNext = db->lookaside.pFree;
|
|
|
|
db->lookaside.pFree = pBuf;
|
|
|
|
db->lookaside.nOut--;
|
|
|
|
return;
|
2010-07-24 15:28:28 +04:00
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
2014-10-07 19:46:54 +04:00
|
|
|
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
2015-03-22 02:38:59 +03:00
|
|
|
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
2010-07-26 15:07:20 +04:00
|
|
|
assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
|
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
|
|
|
sqlite3_free(p);
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
|
|
|
|
2008-06-14 20:56:21 +04:00
|
|
|
/*
|
|
|
|
** Change the size of an existing memory allocation
|
|
|
|
*/
|
2014-09-09 21:27:35 +04:00
|
|
|
void *sqlite3Realloc(void *pOld, u64 nBytes){
|
2011-04-15 23:30:42 +04:00
|
|
|
int nOld, nNew, nDiff;
|
2008-06-14 20:56:21 +04:00
|
|
|
void *pNew;
|
2014-10-07 19:46:54 +04:00
|
|
|
assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
|
2015-03-22 02:38:59 +03:00
|
|
|
assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
|
2008-06-14 20:56:21 +04:00
|
|
|
if( pOld==0 ){
|
2014-10-03 18:54:47 +04:00
|
|
|
return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
2014-09-09 21:27:35 +04:00
|
|
|
if( nBytes==0 ){
|
2014-10-03 18:54:47 +04:00
|
|
|
sqlite3_free(pOld); /* IMP: R-26507-47431 */
|
2008-06-14 20:56:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2009-06-27 04:48:33 +04:00
|
|
|
if( nBytes>=0x7fffff00 ){
|
|
|
|
/* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-14 20:56:21 +04:00
|
|
|
nOld = sqlite3MallocSize(pOld);
|
2010-08-31 19:27:32 +04:00
|
|
|
/* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
|
|
|
|
** argument to xRealloc is always a value returned by a prior call to
|
|
|
|
** xRoundup. */
|
2014-09-09 21:27:35 +04:00
|
|
|
nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
|
2009-08-18 18:48:53 +04:00
|
|
|
if( nOld==nNew ){
|
|
|
|
pNew = pOld;
|
|
|
|
}else if( sqlite3GlobalConfig.bMemstat ){
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2015-10-15 18:28:56 +03:00
|
|
|
sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
|
2011-04-15 20:39:52 +04:00
|
|
|
nDiff = nNew - nOld;
|
2016-12-27 15:08:36 +03:00
|
|
|
if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
|
2015-09-10 04:22:09 +03:00
|
|
|
mem0.alarmThreshold-nDiff ){
|
|
|
|
sqlite3MallocAlarm(nDiff);
|
|
|
|
}
|
2009-08-18 18:48:53 +04:00
|
|
|
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
|
2015-09-10 04:22:09 +03:00
|
|
|
if( pNew==0 && mem0.alarmThreshold>0 ){
|
|
|
|
sqlite3MallocAlarm((int)nBytes);
|
2008-09-01 22:34:20 +04:00
|
|
|
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
|
2009-08-18 18:48:53 +04:00
|
|
|
}
|
|
|
|
if( pNew ){
|
|
|
|
nNew = sqlite3MallocSize(pNew);
|
2015-03-23 20:25:18 +03:00
|
|
|
sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
2009-08-18 18:48:53 +04:00
|
|
|
pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
2014-10-03 18:54:47 +04:00
|
|
|
assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
|
2008-06-14 20:56:21 +04:00
|
|
|
return pNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The public interface to sqlite3Realloc. Make sure that the memory
|
|
|
|
** subsystem is initialized prior to invoking sqliteRealloc.
|
|
|
|
*/
|
|
|
|
void *sqlite3_realloc(void *pOld, int n){
|
2014-09-09 21:27:35 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
if( sqlite3_initialize() ) return 0;
|
|
|
|
#endif
|
2014-10-03 18:54:47 +04:00
|
|
|
if( n<0 ) n = 0; /* IMP: R-26507-47431 */
|
2014-09-09 21:27:35 +04:00
|
|
|
return sqlite3Realloc(pOld, n);
|
|
|
|
}
|
|
|
|
void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
|
2008-06-14 20:56:21 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
if( sqlite3_initialize() ) return 0;
|
|
|
|
#endif
|
|
|
|
return sqlite3Realloc(pOld, n);
|
|
|
|
}
|
|
|
|
|
2007-05-05 15:48:52 +04:00
|
|
|
|
|
|
|
/*
|
2007-08-16 08:30:38 +04:00
|
|
|
** Allocate and zero memory.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
2014-09-09 21:27:35 +04:00
|
|
|
void *sqlite3MallocZero(u64 n){
|
2008-06-14 20:56:21 +04:00
|
|
|
void *p = sqlite3Malloc(n);
|
2007-05-05 15:48:52 +04:00
|
|
|
if( p ){
|
2014-09-18 06:20:54 +04:00
|
|
|
memset(p, 0, (size_t)n);
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-08-16 08:30:38 +04:00
|
|
|
** Allocate and zero memory. If the allocation fails, make
|
|
|
|
** the mallocFailed flag in the connection pointer.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
2014-09-09 21:27:35 +04:00
|
|
|
void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
|
2016-02-05 16:38:36 +03:00
|
|
|
void *p;
|
|
|
|
testcase( db==0 );
|
|
|
|
p = sqlite3DbMallocRaw(db, n);
|
|
|
|
if( p ) memset(p, 0, (size_t)n);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Finish the work of sqlite3DbMallocRawNN for the unusual and
|
|
|
|
** slower case when the allocation cannot be fulfilled using lookaside.
|
|
|
|
*/
|
|
|
|
static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
|
|
|
|
void *p;
|
|
|
|
assert( db!=0 );
|
|
|
|
p = sqlite3Malloc(n);
|
|
|
|
if( !p ) sqlite3OomFault(db);
|
|
|
|
sqlite3MemdebugSetType(p,
|
|
|
|
(db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
|
2007-08-16 08:30:38 +04:00
|
|
|
return p;
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-01-20 06:36:32 +03:00
|
|
|
** Allocate memory, either lookaside (if possible) or heap.
|
|
|
|
** If the allocation fails, set the mallocFailed flag in
|
|
|
|
** the connection pointer.
|
2008-10-11 21:35:16 +04:00
|
|
|
**
|
|
|
|
** If db!=0 and db->mallocFailed is true (indicating a prior malloc
|
|
|
|
** failure on the same database connection) then always return 0.
|
|
|
|
** Hence for a particular database connection, once malloc starts
|
|
|
|
** failing, it fails consistently until mallocFailed is reset.
|
|
|
|
** This is an important assumption. There are many places in the
|
|
|
|
** code that do things like this:
|
|
|
|
**
|
|
|
|
** int *a = (int*)sqlite3DbMallocRaw(db, 100);
|
|
|
|
** int *b = (int*)sqlite3DbMallocRaw(db, 200);
|
|
|
|
** if( b ) a[10] = 9;
|
|
|
|
**
|
|
|
|
** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
|
|
|
|
** that all prior mallocs (ex: "a") worked too.
|
2016-02-05 16:38:36 +03:00
|
|
|
**
|
|
|
|
** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
|
|
|
|
** not a NULL pointer.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
2014-09-09 21:27:35 +04:00
|
|
|
void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
|
2016-02-05 16:38:36 +03:00
|
|
|
void *p;
|
|
|
|
if( db ) return sqlite3DbMallocRawNN(db, n);
|
|
|
|
p = sqlite3Malloc(n);
|
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
|
2016-02-06 22:48:50 +03:00
|
|
|
#ifndef SQLITE_OMIT_LOOKASIDE
|
|
|
|
LookasideSlot *pBuf;
|
2016-02-05 16:38:36 +03:00
|
|
|
assert( db!=0 );
|
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
|
|
|
assert( db->pnBytesFreed==0 );
|
|
|
|
if( db->lookaside.bDisable==0 ){
|
|
|
|
assert( db->mallocFailed==0 );
|
|
|
|
if( n>db->lookaside.sz ){
|
|
|
|
db->lookaside.anStat[1]++;
|
|
|
|
}else if( (pBuf = db->lookaside.pFree)==0 ){
|
|
|
|
db->lookaside.anStat[2]++;
|
|
|
|
}else{
|
|
|
|
db->lookaside.pFree = pBuf->pNext;
|
|
|
|
db->lookaside.nOut++;
|
|
|
|
db->lookaside.anStat[0]++;
|
|
|
|
if( db->lookaside.nOut>db->lookaside.mxOut ){
|
|
|
|
db->lookaside.mxOut = db->lookaside.nOut;
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
2016-02-05 16:38:36 +03:00
|
|
|
return (void*)pBuf;
|
2007-08-29 16:31:25 +04:00
|
|
|
}
|
2016-02-05 16:38:36 +03:00
|
|
|
}else if( db->mallocFailed ){
|
|
|
|
return 0;
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
2008-10-11 21:35:16 +04:00
|
|
|
#else
|
2016-02-06 22:48:50 +03:00
|
|
|
assert( db!=0 );
|
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
|
|
|
assert( db->pnBytesFreed==0 );
|
2016-02-05 16:38:36 +03:00
|
|
|
if( db->mallocFailed ){
|
2008-10-11 21:35:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-11 19:38:29 +04:00
|
|
|
#endif
|
2016-01-20 06:36:32 +03:00
|
|
|
return dbMallocRawFinish(db, n);
|
|
|
|
}
|
2007-05-05 15:48:52 +04:00
|
|
|
|
2016-02-05 05:42:54 +03:00
|
|
|
/* Forward declaration */
|
|
|
|
static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
|
|
|
|
|
2007-08-29 18:06:22 +04:00
|
|
|
/*
|
|
|
|
** Resize the block of memory pointed to by p to n bytes. If the
|
2008-07-28 23:34:53 +04:00
|
|
|
** resize fails, set the mallocFailed flag in the connection object.
|
2007-08-29 18:06:22 +04:00
|
|
|
*/
|
2014-09-09 21:27:35 +04:00
|
|
|
void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
|
2009-03-24 18:08:09 +03:00
|
|
|
assert( db!=0 );
|
2016-02-05 16:38:36 +03:00
|
|
|
if( p==0 ) return sqlite3DbMallocRawNN(db, n);
|
2009-03-23 20:49:14 +03:00
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
2016-02-05 05:42:54 +03:00
|
|
|
if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
|
|
|
|
return dbReallocFinish(db, p, n);
|
|
|
|
}
|
|
|
|
static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
|
|
|
|
void *pNew = 0;
|
|
|
|
assert( db!=0 );
|
|
|
|
assert( p!=0 );
|
2007-08-29 16:31:25 +04:00
|
|
|
if( db->mallocFailed==0 ){
|
2008-07-28 23:34:53 +04:00
|
|
|
if( isLookaside(db, p) ){
|
2016-02-05 16:38:36 +03:00
|
|
|
pNew = sqlite3DbMallocRawNN(db, n);
|
2008-07-28 23:34:53 +04:00
|
|
|
if( pNew ){
|
|
|
|
memcpy(pNew, p, db->lookaside.sz);
|
|
|
|
sqlite3DbFree(db, p);
|
|
|
|
}
|
|
|
|
}else{
|
2014-10-07 19:46:54 +04:00
|
|
|
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
2015-03-22 02:38:59 +03:00
|
|
|
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
|
2010-03-12 19:32:53 +03:00
|
|
|
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
|
2014-09-18 05:21:43 +04:00
|
|
|
pNew = sqlite3_realloc64(p, n);
|
2008-07-28 23:34:53 +04:00
|
|
|
if( !pNew ){
|
2016-02-05 04:55:27 +03:00
|
|
|
sqlite3OomFault(db);
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
2014-10-07 19:46:54 +04:00
|
|
|
sqlite3MemdebugSetType(pNew,
|
2016-02-05 04:55:27 +03:00
|
|
|
(db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
|
2007-08-29 16:31:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pNew;
|
|
|
|
}
|
|
|
|
|
2007-05-05 15:48:52 +04:00
|
|
|
/*
|
2007-08-16 08:30:38 +04:00
|
|
|
** Attempt to reallocate p. If the reallocation fails, then free p
|
|
|
|
** and set the mallocFailed flag in the database connection.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
2014-09-09 21:27:35 +04:00
|
|
|
void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
|
2007-05-05 15:48:52 +04:00
|
|
|
void *pNew;
|
2007-08-29 16:31:25 +04:00
|
|
|
pNew = sqlite3DbRealloc(db, p, n);
|
2007-05-05 15:48:52 +04:00
|
|
|
if( !pNew ){
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, p);
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
return pNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Make a copy of a string in memory obtained from sqliteMalloc(). These
|
|
|
|
** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
|
|
|
|
** is because when memory debugging is turned on, these two functions are
|
|
|
|
** called via macros that record the current file and line number in the
|
|
|
|
** ThreadData structure.
|
|
|
|
*/
|
2008-07-28 23:34:53 +04:00
|
|
|
char *sqlite3DbStrDup(sqlite3 *db, const char *z){
|
2007-05-05 15:48:52 +04:00
|
|
|
char *zNew;
|
2008-07-28 23:34:53 +04:00
|
|
|
size_t n;
|
|
|
|
if( z==0 ){
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-17 03:48:06 +03:00
|
|
|
n = strlen(z) + 1;
|
|
|
|
zNew = sqlite3DbMallocRaw(db, n);
|
2007-05-05 15:48:52 +04:00
|
|
|
if( zNew ){
|
|
|
|
memcpy(zNew, z, n);
|
2007-08-16 14:09:01 +04:00
|
|
|
}
|
|
|
|
return zNew;
|
|
|
|
}
|
2014-09-09 21:27:35 +04:00
|
|
|
char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
|
2008-07-28 23:34:53 +04:00
|
|
|
char *zNew;
|
2016-02-05 16:38:36 +03:00
|
|
|
assert( db!=0 );
|
2008-07-28 23:34:53 +04:00
|
|
|
if( z==0 ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert( (n&0x7fffffff)==n );
|
2016-02-05 16:38:36 +03:00
|
|
|
zNew = sqlite3DbMallocRawNN(db, n+1);
|
2008-07-28 23:34:53 +04:00
|
|
|
if( zNew ){
|
2014-09-18 06:20:54 +04:00
|
|
|
memcpy(zNew, z, (size_t)n);
|
2008-07-28 23:34:53 +04:00
|
|
|
zNew[n] = 0;
|
2007-08-16 14:09:01 +04:00
|
|
|
}
|
|
|
|
return zNew;
|
|
|
|
}
|
|
|
|
|
2007-05-05 15:48:52 +04:00
|
|
|
/*
|
2015-05-15 07:13:15 +03:00
|
|
|
** Free any prior content in *pz and replace it with a copy of zNew.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
2015-05-15 07:13:15 +03:00
|
|
|
void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, *pz);
|
2015-05-15 07:13:15 +03:00
|
|
|
*pz = sqlite3DbStrDup(db, zNew);
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
|
2016-02-05 04:55:27 +03:00
|
|
|
/*
|
|
|
|
** Call this routine to record the fact that an OOM (out-of-memory) error
|
|
|
|
** has happened. This routine will set db->mallocFailed, and also
|
|
|
|
** temporarily disable the lookaside memory allocator and interrupt
|
|
|
|
** any running VDBEs.
|
|
|
|
*/
|
|
|
|
void sqlite3OomFault(sqlite3 *db){
|
|
|
|
if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
|
|
|
|
db->mallocFailed = 1;
|
|
|
|
if( db->nVdbeExec>0 ){
|
|
|
|
db->u1.isInterrupted = 1;
|
|
|
|
}
|
|
|
|
db->lookaside.bDisable++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine reactivates the memory allocator and clears the
|
|
|
|
** db->mallocFailed flag as necessary.
|
|
|
|
**
|
|
|
|
** The memory allocator is not restarted if there are running
|
|
|
|
** VDBEs.
|
|
|
|
*/
|
|
|
|
void sqlite3OomClear(sqlite3 *db){
|
|
|
|
if( db->mallocFailed && db->nVdbeExec==0 ){
|
|
|
|
db->mallocFailed = 0;
|
|
|
|
db->u1.isInterrupted = 0;
|
|
|
|
assert( db->lookaside.bDisable>0 );
|
|
|
|
db->lookaside.bDisable--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-24 00:25:53 +04:00
|
|
|
/*
|
|
|
|
** Take actions at the end of an API call to indicate an OOM error
|
|
|
|
*/
|
|
|
|
static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
|
2016-02-05 04:55:27 +03:00
|
|
|
sqlite3OomClear(db);
|
2014-08-24 00:25:53 +04:00
|
|
|
sqlite3Error(db, SQLITE_NOMEM);
|
2016-02-14 02:43:46 +03:00
|
|
|
return SQLITE_NOMEM_BKPT;
|
2014-08-24 00:25:53 +04:00
|
|
|
}
|
2007-05-05 15:48:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This function must be called before exiting any API function (i.e.
|
2007-08-16 08:30:38 +04:00
|
|
|
** returning control to the user) that has called sqlite3_malloc or
|
|
|
|
** sqlite3_realloc.
|
2007-05-05 15:48:52 +04:00
|
|
|
**
|
|
|
|
** The returned value is normally a copy of the second argument to this
|
2009-03-05 07:20:31 +03:00
|
|
|
** function. However, if a malloc() failure has occurred since the previous
|
2007-05-05 15:48:52 +04:00
|
|
|
** invocation SQLITE_NOMEM is returned instead.
|
|
|
|
**
|
2015-06-30 06:13:47 +03:00
|
|
|
** If an OOM as occurred, then the connection error-code (the value
|
|
|
|
** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
|
|
|
int sqlite3ApiExit(sqlite3* db, int rc){
|
2015-06-30 06:13:47 +03:00
|
|
|
/* If the db handle must hold the connection handle mutex here.
|
|
|
|
** Otherwise the read (and possible write) of db->mallocFailed
|
2007-08-29 16:31:25 +04:00
|
|
|
** is unsafe, as is the call to sqlite3Error().
|
|
|
|
*/
|
2015-06-30 06:13:47 +03:00
|
|
|
assert( db!=0 );
|
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
2014-08-24 00:25:53 +04:00
|
|
|
if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
|
|
|
|
return apiOomError(db);
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
2014-08-24 00:25:53 +04:00
|
|
|
return rc & db->errMask;
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|