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
|
|
|
**
|
2008-07-29 18:29:06 +04:00
|
|
|
** $Id: malloc.c,v 1.31 2008/07/29 14:29:07 drh Exp $
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
/*
|
2007-08-21 23:33:56 +04:00
|
|
|
** This routine runs when the memory allocator sees that the
|
|
|
|
** total memory allocation is about to exceed the soft heap
|
|
|
|
** limit.
|
|
|
|
*/
|
|
|
|
static void softHeapLimitEnforcer(
|
|
|
|
void *NotUsed,
|
2007-08-24 07:51:33 +04:00
|
|
|
sqlite3_int64 inUse,
|
|
|
|
int allocSize
|
2007-08-21 23:33:56 +04:00
|
|
|
){
|
|
|
|
sqlite3_release_memory(allocSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-06-23 15:11:35 +04:00
|
|
|
** Set the soft heap-size limit for the library. Passing a zero or
|
|
|
|
** negative value indicates no limit.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
|
|
|
void sqlite3_soft_heap_limit(int n){
|
2007-08-21 23:33:56 +04:00
|
|
|
sqlite3_uint64 iLimit;
|
|
|
|
int overage;
|
|
|
|
if( n<0 ){
|
|
|
|
iLimit = 0;
|
|
|
|
}else{
|
|
|
|
iLimit = n;
|
|
|
|
}
|
2008-06-18 22:12:04 +04:00
|
|
|
sqlite3_initialize();
|
2007-08-21 23:33:56 +04:00
|
|
|
if( iLimit>0 ){
|
|
|
|
sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
|
|
|
|
}else{
|
|
|
|
sqlite3_memory_alarm(0, 0, 0);
|
|
|
|
}
|
|
|
|
overage = sqlite3_memory_used() - n;
|
|
|
|
if( overage>0 ){
|
|
|
|
sqlite3_release_memory(overage);
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
2008-03-26 21:34:43 +03:00
|
|
|
int nRet = sqlite3VdbeReleaseMemory(n);
|
|
|
|
nRet += sqlite3PagerReleaseMemory(n-nRet);
|
|
|
|
return nRet;
|
2007-08-16 14:09:01 +04:00
|
|
|
#else
|
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
|
2008-06-14 20:56:21 +04:00
|
|
|
/*
|
|
|
|
** State information local to the memory allocation subsystem.
|
|
|
|
*/
|
|
|
|
static struct {
|
|
|
|
sqlite3_mutex *mutex; /* Mutex to serialize access */
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The alarm callback and its arguments. The mem0.mutex lock will
|
|
|
|
** be held while the callback is running. Recursive calls into
|
|
|
|
** the memory subsystem are allowed, but no new callbacks will be
|
|
|
|
** issued. The alarmBusy variable is set to prevent recursive
|
|
|
|
** callbacks.
|
|
|
|
*/
|
|
|
|
sqlite3_int64 alarmThreshold;
|
|
|
|
void (*alarmCallback)(void*, sqlite3_int64,int);
|
|
|
|
void *alarmArg;
|
|
|
|
int alarmBusy;
|
|
|
|
|
2008-06-18 22:12:04 +04:00
|
|
|
/*
|
|
|
|
** Pointers to the end of sqlite3Config.pScratch and
|
|
|
|
** sqlite3Config.pPage to a block of memory that records
|
|
|
|
** which pages are available.
|
|
|
|
*/
|
|
|
|
u32 *aScratchFree;
|
|
|
|
u32 *aPageFree;
|
|
|
|
|
|
|
|
/* Number of free pages for scratch and page-cache memory */
|
|
|
|
u32 nScratchFree;
|
|
|
|
u32 nPageFree;
|
2008-06-14 20:56:21 +04:00
|
|
|
} mem0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Initialize the memory allocation subsystem.
|
|
|
|
*/
|
|
|
|
int sqlite3MallocInit(void){
|
|
|
|
if( sqlite3Config.m.xMalloc==0 ){
|
|
|
|
sqlite3MemSetDefault();
|
|
|
|
}
|
|
|
|
memset(&mem0, 0, sizeof(mem0));
|
2008-06-18 22:12:04 +04:00
|
|
|
if( sqlite3Config.bCoreMutex ){
|
2008-06-18 21:09:10 +04:00
|
|
|
mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
2008-06-18 22:12:04 +04:00
|
|
|
if( sqlite3Config.pScratch && sqlite3Config.szScratch>=3000
|
|
|
|
&& sqlite3Config.nScratch>0 ){
|
|
|
|
int i;
|
|
|
|
mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
|
|
|
|
[sqlite3Config.szScratch*sqlite3Config.nScratch];
|
|
|
|
for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
|
|
|
|
mem0.nScratchFree = sqlite3Config.nScratch;
|
|
|
|
}else{
|
|
|
|
sqlite3Config.pScratch = 0;
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3Config.szScratch = 0;
|
2008-06-18 22:12:04 +04:00
|
|
|
}
|
|
|
|
if( sqlite3Config.pPage && sqlite3Config.szPage>=512
|
|
|
|
&& sqlite3Config.nPage>0 ){
|
|
|
|
int i;
|
|
|
|
mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
|
|
|
|
[sqlite3Config.szPage*sqlite3Config.nPage];
|
|
|
|
for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
|
|
|
|
mem0.nPageFree = sqlite3Config.nPage;
|
|
|
|
}else{
|
|
|
|
sqlite3Config.pPage = 0;
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3Config.szPage = 0;
|
2008-06-18 22:12:04 +04:00
|
|
|
}
|
2008-06-14 20:56:21 +04:00
|
|
|
return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Deinitialize the memory allocation subsystem.
|
|
|
|
*/
|
|
|
|
void sqlite3MallocEnd(void){
|
2008-06-18 22:12:04 +04:00
|
|
|
sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
|
|
|
|
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){
|
2008-06-19 04:16:08 +04:00
|
|
|
int n, mx;
|
2008-07-14 16:30:54 +04:00
|
|
|
sqlite3_int64 res;
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
|
2008-07-14 16:30:54 +04:00
|
|
|
res = (sqlite3_int64)n; /* Work around bug in Borland C. Ticket #3216 */
|
|
|
|
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){
|
2008-06-19 04:16:08 +04:00
|
|
|
int n, mx;
|
2008-07-14 16:30:54 +04:00
|
|
|
sqlite3_int64 res;
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
|
2008-07-14 16:38:20 +04:00
|
|
|
res = (sqlite3_int64)mx; /* Work around bug in Borland C. Ticket #3216 */
|
2008-07-14 16:30:54 +04:00
|
|
|
return res;
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Change the alarm callback
|
|
|
|
*/
|
|
|
|
int sqlite3_memory_alarm(
|
|
|
|
void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
|
|
|
|
void *pArg,
|
|
|
|
sqlite3_int64 iThreshold
|
|
|
|
){
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
mem0.alarmCallback = xCallback;
|
|
|
|
mem0.alarmArg = pArg;
|
|
|
|
mem0.alarmThreshold = iThreshold;
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Trigger the alarm
|
|
|
|
*/
|
|
|
|
static void sqlite3MallocAlarm(int nByte){
|
|
|
|
void (*xCallback)(void*,sqlite3_int64,int);
|
|
|
|
sqlite3_int64 nowUsed;
|
|
|
|
void *pArg;
|
|
|
|
if( mem0.alarmCallback==0 || mem0.alarmBusy ) return;
|
|
|
|
mem0.alarmBusy = 1;
|
|
|
|
xCallback = mem0.alarmCallback;
|
2008-06-19 04:16:08 +04:00
|
|
|
nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
|
2008-06-14 20:56:21 +04:00
|
|
|
pArg = mem0.alarmArg;
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
xCallback(pArg, nowUsed, nByte);
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
mem0.alarmBusy = 0;
|
|
|
|
}
|
|
|
|
|
2008-06-19 04:16:08 +04:00
|
|
|
/*
|
|
|
|
** Do a memory allocation with statistics and alarms. Assume the
|
|
|
|
** lock is already held.
|
|
|
|
*/
|
|
|
|
static int mallocWithAlarm(int n, void **pp){
|
|
|
|
int nFull;
|
|
|
|
void *p;
|
|
|
|
assert( sqlite3_mutex_held(mem0.mutex) );
|
|
|
|
nFull = sqlite3Config.m.xRoundup(n);
|
|
|
|
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
|
|
|
|
if( mem0.alarmCallback!=0 ){
|
|
|
|
int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
|
|
|
|
if( nUsed+nFull >= mem0.alarmThreshold ){
|
|
|
|
sqlite3MallocAlarm(nFull);
|
|
|
|
}
|
|
|
|
}
|
2008-06-19 22:17:49 +04:00
|
|
|
p = sqlite3Config.m.xMalloc(nFull);
|
|
|
|
if( p==0 && mem0.alarmCallback ){
|
|
|
|
sqlite3MallocAlarm(nFull);
|
2008-06-19 04:16:08 +04:00
|
|
|
p = sqlite3Config.m.xMalloc(nFull);
|
|
|
|
}
|
2008-07-18 22:56:16 +04:00
|
|
|
if( p ){
|
|
|
|
nFull = sqlite3MallocSize(p);
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
|
|
|
|
}
|
2008-06-19 04:16:08 +04:00
|
|
|
*pp = p;
|
|
|
|
return nFull;
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
void *sqlite3Malloc(int n){
|
|
|
|
void *p;
|
|
|
|
if( n<=0 ){
|
2008-06-19 04:16:08 +04:00
|
|
|
p = 0;
|
2008-06-14 20:56:21 +04:00
|
|
|
}else if( sqlite3Config.bMemstat ){
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2008-06-19 04:16:08 +04:00
|
|
|
mallocWithAlarm(n, &p);
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
p = sqlite3Config.m.xMalloc(n);
|
|
|
|
}
|
|
|
|
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){
|
|
|
|
#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
|
|
|
|
2008-06-15 06:51:47 +04:00
|
|
|
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
|
2008-06-18 22:12:04 +04:00
|
|
|
/* Verify that no more than one 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.) */
|
2008-06-17 19:12:00 +04:00
|
|
|
assert( scratchAllocOut==0 );
|
2008-06-15 06:51:47 +04:00
|
|
|
#endif
|
2008-06-18 22:12:04 +04:00
|
|
|
|
2008-06-19 04:16:08 +04:00
|
|
|
if( sqlite3Config.szScratch<n ){
|
|
|
|
goto scratch_overflow;
|
|
|
|
}else{
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
if( mem0.nScratchFree==0 ){
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
goto scratch_overflow;
|
|
|
|
}else{
|
|
|
|
int i;
|
|
|
|
i = mem0.aScratchFree[--mem0.nScratchFree];
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
i *= sqlite3Config.szScratch;
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
|
|
|
|
p = (void*)&((char*)sqlite3Config.pScratch)[i];
|
|
|
|
}
|
2008-06-15 06:51:47 +04:00
|
|
|
}
|
2008-06-19 04:16:08 +04:00
|
|
|
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
|
|
|
|
scratchAllocOut = p!=0;
|
|
|
|
#endif
|
|
|
|
|
2008-06-15 06:51:47 +04:00
|
|
|
return p;
|
2008-06-19 04:16:08 +04:00
|
|
|
|
|
|
|
scratch_overflow:
|
|
|
|
if( sqlite3Config.bMemstat ){
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
n = mallocWithAlarm(n, &p);
|
|
|
|
if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
p = sqlite3Config.m.xMalloc(n);
|
|
|
|
}
|
|
|
|
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
|
|
|
|
scratchAllocOut = p!=0;
|
|
|
|
#endif
|
|
|
|
return p;
|
2008-06-15 06:51:47 +04:00
|
|
|
}
|
2008-06-17 19:12:00 +04:00
|
|
|
void sqlite3ScratchFree(void *p){
|
2008-06-15 06:51:47 +04:00
|
|
|
if( p ){
|
2008-06-18 22:12:04 +04:00
|
|
|
|
2008-06-15 06:51:47 +04:00
|
|
|
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
|
2008-06-18 22:12:04 +04:00
|
|
|
/* Verify that no more than one 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.) */
|
2008-06-17 19:12:00 +04:00
|
|
|
assert( scratchAllocOut==1 );
|
|
|
|
scratchAllocOut = 0;
|
2008-06-15 06:51:47 +04:00
|
|
|
#endif
|
2008-06-18 22:12:04 +04:00
|
|
|
|
|
|
|
if( sqlite3Config.pScratch==0
|
2008-06-19 04:16:08 +04:00
|
|
|
|| p<sqlite3Config.pScratch
|
|
|
|
|| p>=(void*)mem0.aScratchFree ){
|
|
|
|
if( sqlite3Config.bMemstat ){
|
|
|
|
int iSize = sqlite3MallocSize(p);
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
|
|
|
|
sqlite3Config.m.xFree(p);
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
sqlite3Config.m.xFree(p);
|
|
|
|
}
|
2008-06-18 22:12:04 +04:00
|
|
|
}else{
|
|
|
|
int i;
|
2008-06-23 18:03:45 +04:00
|
|
|
i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
|
2008-06-18 22:12:04 +04:00
|
|
|
i /= sqlite3Config.szScratch;
|
|
|
|
assert( i>=0 && i<sqlite3Config.nScratch );
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
assert( mem0.nScratchFree<sqlite3Config.nScratch );
|
2008-06-18 22:12:04 +04:00
|
|
|
mem0.aScratchFree[mem0.nScratchFree++] = i;
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
|
2008-06-18 22:12:04 +04:00
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}
|
2008-06-15 06:51:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-17 19:12:00 +04:00
|
|
|
/*
|
2008-06-19 04:16:08 +04:00
|
|
|
** Allocate memory to be used by the page cache. Make use of the
|
|
|
|
** memory buffer provided by SQLITE_CONFIG_PAGECACHE if there is one
|
|
|
|
** and that memory is of the right size and is not completely
|
|
|
|
** consumed. Otherwise, failover to sqlite3Malloc().
|
2008-06-17 19:12:00 +04:00
|
|
|
*/
|
2008-06-19 04:16:08 +04:00
|
|
|
void *sqlite3PageMalloc(int n){
|
|
|
|
void *p;
|
|
|
|
assert( n>0 );
|
|
|
|
assert( (n & (n-1))==0 );
|
|
|
|
assert( n>=512 && n<=32768 );
|
|
|
|
|
|
|
|
if( sqlite3Config.szPage<n ){
|
|
|
|
goto page_overflow;
|
|
|
|
}else{
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
if( mem0.nPageFree==0 ){
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
goto page_overflow;
|
|
|
|
}else{
|
|
|
|
int i;
|
|
|
|
i = mem0.aPageFree[--mem0.nPageFree];
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
i *= sqlite3Config.szPage;
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
|
|
|
|
p = (void*)&((char*)sqlite3Config.pPage)[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
|
|
|
|
page_overflow:
|
|
|
|
if( sqlite3Config.bMemstat ){
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
n = mallocWithAlarm(n, &p);
|
|
|
|
if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
p = sqlite3Config.m.xMalloc(n);
|
|
|
|
}
|
|
|
|
return p;
|
2008-06-17 19:12:00 +04:00
|
|
|
}
|
2008-06-19 04:16:08 +04:00
|
|
|
void sqlite3PageFree(void *p){
|
|
|
|
if( p ){
|
|
|
|
if( sqlite3Config.pPage==0
|
|
|
|
|| p<sqlite3Config.pPage
|
|
|
|
|| p>=(void*)mem0.aPageFree ){
|
2008-06-21 12:12:15 +04:00
|
|
|
/* In this case, the page allocation was obtained from a regular
|
|
|
|
** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory
|
|
|
|
** "overflow"). Free the block with sqlite3_mem_methods.xFree().
|
|
|
|
*/
|
2008-06-19 04:16:08 +04:00
|
|
|
if( sqlite3Config.bMemstat ){
|
|
|
|
int iSize = sqlite3MallocSize(p);
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
|
|
|
|
sqlite3Config.m.xFree(p);
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
sqlite3Config.m.xFree(p);
|
|
|
|
}
|
|
|
|
}else{
|
2008-06-21 12:12:15 +04:00
|
|
|
/* The page allocation was allocated from the sqlite3Config.pPage
|
|
|
|
** buffer. In this case all that is add the index of the page in
|
|
|
|
** the sqlite3Config.pPage array to the set of free indexes stored
|
|
|
|
** in the mem0.aPageFree[] array.
|
|
|
|
*/
|
2008-06-19 04:16:08 +04:00
|
|
|
int i;
|
2008-06-23 18:03:45 +04:00
|
|
|
i = (u8 *)p - (u8 *)sqlite3Config.pPage;
|
2008-06-19 04:16:08 +04:00
|
|
|
i /= sqlite3Config.szPage;
|
|
|
|
assert( i>=0 && i<sqlite3Config.nPage );
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
|
|
|
assert( mem0.nPageFree<sqlite3Config.nPage );
|
|
|
|
mem0.aPageFree[mem0.nPageFree++] = i;
|
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
2008-07-29 18:29:06 +04:00
|
|
|
#if !defined(NDEBUG) && 0
|
2008-06-21 12:12:15 +04:00
|
|
|
/* Assert that a duplicate was not just inserted into aPageFree[]. */
|
|
|
|
for(i=0; i<mem0.nPageFree-1; i++){
|
|
|
|
assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
|
|
|
|
}
|
|
|
|
#endif
|
2008-06-19 04:16:08 +04:00
|
|
|
}
|
|
|
|
}
|
2008-06-17 19:12:00 +04:00
|
|
|
}
|
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
/*
|
|
|
|
** TRUE if p is a lookaside memory allocation from db
|
|
|
|
*/
|
|
|
|
static int isLookaside(sqlite3 *db, void *p){
|
|
|
|
return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
|
|
|
|
}
|
|
|
|
|
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){
|
|
|
|
return sqlite3Config.m.xSize(p);
|
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
int sqlite3DbMallocSize(sqlite3 *db, void *p){
|
|
|
|
if( isLookaside(db, p) ){
|
|
|
|
return db->lookaside.sz;
|
|
|
|
}else{
|
|
|
|
return sqlite3Config.m.xSize(p);
|
|
|
|
}
|
|
|
|
}
|
2008-06-14 20:56:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Free memory previously obtained from sqlite3Malloc().
|
|
|
|
*/
|
|
|
|
void sqlite3_free(void *p){
|
|
|
|
if( p==0 ) return;
|
|
|
|
if( sqlite3Config.bMemstat ){
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3Config.m.xFree(p);
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
sqlite3Config.m.xFree(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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){
|
|
|
|
if( isLookaside(db, p) ){
|
|
|
|
LookasideSlot *pBuf = (LookasideSlot*)p;
|
|
|
|
pBuf->pNext = db->lookaside.pFree;
|
|
|
|
db->lookaside.pFree = pBuf;
|
|
|
|
db->lookaside.nOut--;
|
|
|
|
}else{
|
|
|
|
sqlite3_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-14 20:56:21 +04:00
|
|
|
/*
|
|
|
|
** Change the size of an existing memory allocation
|
|
|
|
*/
|
|
|
|
void *sqlite3Realloc(void *pOld, int nBytes){
|
|
|
|
int nOld, nNew;
|
|
|
|
void *pNew;
|
|
|
|
if( pOld==0 ){
|
|
|
|
return sqlite3Malloc(nBytes);
|
|
|
|
}
|
|
|
|
if( nBytes<=0 ){
|
|
|
|
sqlite3_free(pOld);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nOld = sqlite3MallocSize(pOld);
|
|
|
|
if( sqlite3Config.bMemstat ){
|
|
|
|
sqlite3_mutex_enter(mem0.mutex);
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
|
2008-06-14 20:56:21 +04:00
|
|
|
nNew = sqlite3Config.m.xRoundup(nBytes);
|
|
|
|
if( nOld==nNew ){
|
|
|
|
pNew = pOld;
|
|
|
|
}else{
|
2008-06-19 04:16:08 +04:00
|
|
|
if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
|
|
|
|
mem0.alarmThreshold ){
|
2008-06-14 20:56:21 +04:00
|
|
|
sqlite3MallocAlarm(nNew-nOld);
|
|
|
|
}
|
2008-06-19 22:17:49 +04:00
|
|
|
pNew = sqlite3Config.m.xRealloc(pOld, nNew);
|
|
|
|
if( pNew==0 && mem0.alarmCallback ){
|
|
|
|
sqlite3MallocAlarm(nBytes);
|
2008-06-14 20:56:21 +04:00
|
|
|
pNew = sqlite3Config.m.xRealloc(pOld, nNew);
|
|
|
|
}
|
|
|
|
if( pNew ){
|
2008-07-18 22:56:16 +04:00
|
|
|
nNew = sqlite3MallocSize(pNew);
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
|
2008-06-14 20:56:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_mutex_leave(mem0.mutex);
|
|
|
|
}else{
|
|
|
|
pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
|
|
|
|
}
|
|
|
|
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){
|
|
|
|
#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
|
|
|
*/
|
2008-06-14 20:56:21 +04:00
|
|
|
void *sqlite3MallocZero(int n){
|
|
|
|
void *p = sqlite3Malloc(n);
|
2007-05-05 15:48:52 +04:00
|
|
|
if( p ){
|
2007-08-16 08:30:38 +04:00
|
|
|
memset(p, 0, 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
|
|
|
*/
|
2008-06-14 20:56:21 +04:00
|
|
|
void *sqlite3DbMallocZero(sqlite3 *db, int n){
|
2007-08-29 16:31:25 +04:00
|
|
|
void *p = sqlite3DbMallocRaw(db, n);
|
2007-08-16 08:30:38 +04:00
|
|
|
if( p ){
|
|
|
|
memset(p, 0, n);
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
return p;
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
2008-06-14 20:56:21 +04:00
|
|
|
void *sqlite3DbMallocRaw(sqlite3 *db, int n){
|
2008-07-28 23:34:53 +04:00
|
|
|
void *p;
|
|
|
|
if( db ){
|
|
|
|
LookasideSlot *pBuf;
|
|
|
|
if( db->mallocFailed ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if( db->lookaside.bEnabled && n<=db->lookaside.sz
|
|
|
|
&& (pBuf = db->lookaside.pFree)!=0 ){
|
|
|
|
db->lookaside.pFree = pBuf->pNext;
|
|
|
|
db->lookaside.nOut++;
|
|
|
|
if( db->lookaside.nOut>db->lookaside.mxOut ){
|
|
|
|
db->lookaside.mxOut = db->lookaside.nOut;
|
|
|
|
}
|
|
|
|
return (void*)pBuf;
|
2007-08-29 16:31:25 +04:00
|
|
|
}
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
p = sqlite3Malloc(n);
|
|
|
|
if( !p && db ){
|
|
|
|
db->mallocFailed = 1;
|
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
return p;
|
2007-05-05 15:48:52 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2007-08-29 16:31:25 +04:00
|
|
|
void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
|
|
|
|
void *pNew = 0;
|
|
|
|
if( db->mallocFailed==0 ){
|
2008-07-28 23:34:53 +04:00
|
|
|
if( p==0 ){
|
|
|
|
return sqlite3DbMallocRaw(db, n);
|
|
|
|
}
|
|
|
|
if( isLookaside(db, p) ){
|
|
|
|
if( n<=db->lookaside.sz ){
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
pNew = sqlite3DbMallocRaw(db, n);
|
|
|
|
if( pNew ){
|
|
|
|
memcpy(pNew, p, db->lookaside.sz);
|
|
|
|
sqlite3DbFree(db, p);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
pNew = sqlite3_realloc(p, n);
|
|
|
|
if( !pNew ){
|
|
|
|
db->mallocFailed = 1;
|
|
|
|
}
|
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
|
|
|
*/
|
2007-08-16 08:30:38 +04:00
|
|
|
void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int 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;
|
|
|
|
}
|
2007-05-05 15:48:52 +04:00
|
|
|
n = strlen(z)+1;
|
2008-07-28 23:34:53 +04:00
|
|
|
assert( (n&0x7fffffff)==n );
|
|
|
|
zNew = sqlite3DbMallocRaw(db, (int)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;
|
|
|
|
}
|
|
|
|
char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
|
2008-07-28 23:34:53 +04:00
|
|
|
char *zNew;
|
|
|
|
if( z==0 ){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert( (n&0x7fffffff)==n );
|
|
|
|
zNew = sqlite3DbMallocRaw(db, n+1);
|
|
|
|
if( zNew ){
|
|
|
|
memcpy(zNew, z, n);
|
|
|
|
zNew[n] = 0;
|
2007-08-16 14:09:01 +04:00
|
|
|
}
|
|
|
|
return zNew;
|
|
|
|
}
|
|
|
|
|
2007-05-05 15:48:52 +04:00
|
|
|
/*
|
2008-07-08 23:34:06 +04:00
|
|
|
** Create a string from the zFromat argument and the va_list that follows.
|
|
|
|
** Store the string in memory obtained from sqliteMalloc() and make *pz
|
|
|
|
** point to that string.
|
2007-05-05 15:48:52 +04:00
|
|
|
*/
|
2008-07-08 23:34:06 +04:00
|
|
|
void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
|
2007-05-05 15:48:52 +04:00
|
|
|
va_list ap;
|
2008-07-08 23:34:06 +04:00
|
|
|
char *z;
|
|
|
|
|
|
|
|
va_start(ap, zFormat);
|
|
|
|
z = sqlite3VMPrintf(db, zFormat, ap);
|
2007-05-05 15:48:52 +04:00
|
|
|
va_end(ap);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, *pz);
|
2008-07-08 23:34:06 +04:00
|
|
|
*pz = z;
|
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
|
|
|
|
** function. However, if a malloc() failure has occured since the previous
|
|
|
|
** invocation SQLITE_NOMEM is returned instead.
|
|
|
|
**
|
|
|
|
** If the first argument, db, is not NULL and a malloc() error has occured,
|
|
|
|
** then the connection error-code (the value returned by sqlite3_errcode())
|
|
|
|
** is set to SQLITE_NOMEM.
|
|
|
|
*/
|
|
|
|
int sqlite3ApiExit(sqlite3* db, int rc){
|
2007-08-29 16:31:25 +04:00
|
|
|
/* If the db handle is not NULL, then we must hold the connection handle
|
|
|
|
** mutex here. Otherwise the read (and possible write) of db->mallocFailed
|
|
|
|
** is unsafe, as is the call to sqlite3Error().
|
|
|
|
*/
|
|
|
|
assert( !db || sqlite3_mutex_held(db->mutex) );
|
2007-08-16 14:09:01 +04:00
|
|
|
if( db && db->mallocFailed ){
|
2007-05-05 15:48:52 +04:00
|
|
|
sqlite3Error(db, SQLITE_NOMEM, 0);
|
2007-08-16 08:30:38 +04:00
|
|
|
db->mallocFailed = 0;
|
2007-05-05 15:48:52 +04:00
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
return rc & (db ? db->errMask : 0xff);
|
|
|
|
}
|