summaryrefslogtreecommitdiffstats
path: root/third_party/sqlite/src/malloc.c
diff options
context:
space:
mode:
authormdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-18 18:27:25 +0000
committermdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-18 18:27:25 +0000
commit997e22224e1062a4cd39373057a68879a1d7a3ac (patch)
treea90a9ce4272fc78f2459b1b2c78b52a3f6d4e5d3 /third_party/sqlite/src/malloc.c
parent0d683c611a18dc6ea0e99f38c73b4fb96611041f (diff)
downloadchromium_src-997e22224e1062a4cd39373057a68879a1d7a3ac.zip
chromium_src-997e22224e1062a4cd39373057a68879a1d7a3ac.tar.gz
chromium_src-997e22224e1062a4cd39373057a68879a1d7a3ac.tar.bz2
Update sqlite to version 3.6.18, porting our patches.
Hopefully this will help to address some valgrind issues. BUG=none TEST=none git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26596 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/sqlite/src/malloc.c')
-rw-r--r--third_party/sqlite/src/malloc.c337
1 files changed, 155 insertions, 182 deletions
diff --git a/third_party/sqlite/src/malloc.c b/third_party/sqlite/src/malloc.c
index fa9c88f..6678d56 100644
--- a/third_party/sqlite/src/malloc.c
+++ b/third_party/sqlite/src/malloc.c
@@ -12,11 +12,10 @@
**
** Memory allocation functions used throughout sqlite.
**
-** $Id: malloc.c,v 1.34 2008/08/05 17:53:23 drh Exp $
+** $Id: malloc.c,v 1.66 2009/07/17 11:44:07 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
-#include <ctype.h>
/*
** This routine runs when the memory allocator sees that the
@@ -25,9 +24,10 @@
*/
static void softHeapLimitEnforcer(
void *NotUsed,
- sqlite3_int64 inUse,
+ sqlite3_int64 NotUsed2,
int allocSize
){
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
sqlite3_release_memory(allocSize);
}
@@ -43,13 +43,15 @@ void sqlite3_soft_heap_limit(int n){
}else{
iLimit = n;
}
+#ifndef SQLITE_OMIT_AUTOINIT
sqlite3_initialize();
+#endif
if( iLimit>0 ){
- sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
+ sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, iLimit);
}else{
- sqlite3_memory_alarm(0, 0, 0);
+ sqlite3MemoryAlarm(0, 0, 0);
}
- overage = sqlite3_memory_used() - n;
+ overage = (int)(sqlite3_memory_used() - (i64)n);
if( overage>0 ){
sqlite3_release_memory(overage);
}
@@ -62,10 +64,14 @@ void sqlite3_soft_heap_limit(int n){
*/
int sqlite3_release_memory(int n){
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
- int nRet = sqlite3VdbeReleaseMemory(n);
- nRet += sqlite3PagerReleaseMemory(n-nRet);
+ int nRet = 0;
+#if 0
+ nRet += sqlite3VdbeReleaseMemory(n);
+#endif
+ nRet += sqlite3PcacheReleaseMemory(n-nRet);
return nRet;
#else
+ UNUSED_PARAMETER(n);
return SQLITE_OK;
#endif
}
@@ -73,81 +79,83 @@ int sqlite3_release_memory(int n){
/*
** State information local to the memory allocation subsystem.
*/
-static struct {
+static SQLITE_WSD struct Mem0Global {
+ /* Number of free pages for scratch and page-cache memory */
+ u32 nScratchFree;
+ u32 nPageFree;
+
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.
+ ** issued.
*/
sqlite3_int64 alarmThreshold;
void (*alarmCallback)(void*, sqlite3_int64,int);
void *alarmArg;
- int alarmBusy;
/*
- ** Pointers to the end of sqlite3Config.pScratch and
- ** sqlite3Config.pPage to a block of memory that records
+ ** Pointers to the end of sqlite3GlobalConfig.pScratch and
+ ** sqlite3GlobalConfig.pPage to a block of memory that records
** which pages are available.
*/
u32 *aScratchFree;
u32 *aPageFree;
+} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
- /* Number of free pages for scratch and page-cache memory */
- u32 nScratchFree;
- u32 nPageFree;
-} mem0;
+#define mem0 GLOBAL(struct Mem0Global, mem0)
/*
** Initialize the memory allocation subsystem.
*/
int sqlite3MallocInit(void){
- if( sqlite3Config.m.xMalloc==0 ){
+ if( sqlite3GlobalConfig.m.xMalloc==0 ){
sqlite3MemSetDefault();
}
memset(&mem0, 0, sizeof(mem0));
- if( sqlite3Config.bCoreMutex ){
+ if( sqlite3GlobalConfig.bCoreMutex ){
mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
}
- if( sqlite3Config.pScratch && sqlite3Config.szScratch>=100
- && sqlite3Config.nScratch>=0 ){
+ if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
+ && sqlite3GlobalConfig.nScratch>=0 ){
int i;
- sqlite3Config.szScratch -= 4;
- 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;
+ sqlite3GlobalConfig.szScratch = ROUNDDOWN8(sqlite3GlobalConfig.szScratch-4);
+ mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
+ [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
+ for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
+ mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
}else{
- sqlite3Config.pScratch = 0;
- sqlite3Config.szScratch = 0;
+ sqlite3GlobalConfig.pScratch = 0;
+ sqlite3GlobalConfig.szScratch = 0;
}
- if( sqlite3Config.pPage && sqlite3Config.szPage>=512
- && sqlite3Config.nPage>=1 ){
+ if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
+ && sqlite3GlobalConfig.nPage>=1 ){
int i;
int overhead;
- int sz = sqlite3Config.szPage;
- int n = sqlite3Config.nPage;
+ int sz = ROUNDDOWN8(sqlite3GlobalConfig.szPage);
+ int n = sqlite3GlobalConfig.nPage;
overhead = (4*n + sz - 1)/sz;
- sqlite3Config.nPage -= overhead;
- 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;
+ sqlite3GlobalConfig.nPage -= overhead;
+ mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
+ [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
+ for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
+ mem0.nPageFree = sqlite3GlobalConfig.nPage;
}else{
- sqlite3Config.pPage = 0;
- sqlite3Config.szPage = 0;
+ sqlite3GlobalConfig.pPage = 0;
+ sqlite3GlobalConfig.szPage = 0;
}
- return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
+ return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
}
/*
** Deinitialize the memory allocation subsystem.
*/
void sqlite3MallocEnd(void){
- sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
+ if( sqlite3GlobalConfig.m.xShutdown ){
+ sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
+ }
memset(&mem0, 0, sizeof(mem0));
}
@@ -178,7 +186,7 @@ sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
/*
** Change the alarm callback
*/
-int sqlite3_memory_alarm(
+int sqlite3MemoryAlarm(
void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
void *pArg,
sqlite3_int64 iThreshold
@@ -191,6 +199,20 @@ int sqlite3_memory_alarm(
return SQLITE_OK;
}
+#ifndef SQLITE_OMIT_DEPRECATED
+/*
+** Deprecated external interface. Internal/core SQLite code
+** should call sqlite3MemoryAlarm.
+*/
+int sqlite3_memory_alarm(
+ void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
+ void *pArg,
+ sqlite3_int64 iThreshold
+){
+ return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
+}
+#endif
+
/*
** Trigger the alarm
*/
@@ -198,15 +220,16 @@ 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;
+ if( mem0.alarmCallback==0 ) return;
xCallback = mem0.alarmCallback;
nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
pArg = mem0.alarmArg;
+ mem0.alarmCallback = 0;
sqlite3_mutex_leave(mem0.mutex);
xCallback(pArg, nowUsed, nByte);
sqlite3_mutex_enter(mem0.mutex);
- mem0.alarmBusy = 0;
+ mem0.alarmCallback = xCallback;
+ mem0.alarmArg = pArg;
}
/*
@@ -217,7 +240,7 @@ static int mallocWithAlarm(int n, void **pp){
int nFull;
void *p;
assert( sqlite3_mutex_held(mem0.mutex) );
- nFull = sqlite3Config.m.xRoundup(n);
+ nFull = sqlite3GlobalConfig.m.xRoundup(n);
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
if( mem0.alarmCallback!=0 ){
int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
@@ -225,10 +248,10 @@ static int mallocWithAlarm(int n, void **pp){
sqlite3MallocAlarm(nFull);
}
}
- p = sqlite3Config.m.xMalloc(nFull);
+ p = sqlite3GlobalConfig.m.xMalloc(nFull);
if( p==0 && mem0.alarmCallback ){
sqlite3MallocAlarm(nFull);
- p = sqlite3Config.m.xMalloc(nFull);
+ p = sqlite3GlobalConfig.m.xMalloc(nFull);
}
if( p ){
nFull = sqlite3MallocSize(p);
@@ -244,14 +267,19 @@ static int mallocWithAlarm(int n, void **pp){
*/
void *sqlite3Malloc(int n){
void *p;
- if( n<=0 ){
+ if( n<=0 || n>=0x7fffff00 ){
+ /* 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() */
p = 0;
- }else if( sqlite3Config.bMemstat ){
+ }else if( sqlite3GlobalConfig.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
mallocWithAlarm(n, &p);
sqlite3_mutex_leave(mem0.mutex);
}else{
- p = sqlite3Config.m.xMalloc(n);
+ p = sqlite3GlobalConfig.m.xMalloc(n);
}
return p;
}
@@ -299,7 +327,7 @@ void *sqlite3ScratchMalloc(int n){
assert( scratchAllocOut==0 );
#endif
- if( sqlite3Config.szScratch<n ){
+ if( sqlite3GlobalConfig.szScratch<n ){
goto scratch_overflow;
}else{
sqlite3_mutex_enter(mem0.mutex);
@@ -309,11 +337,12 @@ void *sqlite3ScratchMalloc(int n){
}else{
int i;
i = mem0.aScratchFree[--mem0.nScratchFree];
- sqlite3_mutex_leave(mem0.mutex);
- i *= sqlite3Config.szScratch;
+ i *= sqlite3GlobalConfig.szScratch;
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
- p = (void*)&((char*)sqlite3Config.pScratch)[i];
+ sqlite3_mutex_leave(mem0.mutex);
+ p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
+ assert( (((u8*)p - (u8*)0) & 7)==0 );
}
}
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
@@ -323,14 +352,14 @@ void *sqlite3ScratchMalloc(int n){
return p;
scratch_overflow:
- if( sqlite3Config.bMemstat ){
+ if( sqlite3GlobalConfig.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
n = mallocWithAlarm(n, &p);
if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
sqlite3_mutex_leave(mem0.mutex);
}else{
- p = sqlite3Config.m.xMalloc(n);
+ p = sqlite3GlobalConfig.m.xMalloc(n);
}
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
scratchAllocOut = p!=0;
@@ -349,26 +378,26 @@ void sqlite3ScratchFree(void *p){
scratchAllocOut = 0;
#endif
- if( sqlite3Config.pScratch==0
- || p<sqlite3Config.pScratch
+ if( sqlite3GlobalConfig.pScratch==0
+ || p<sqlite3GlobalConfig.pScratch
|| p>=(void*)mem0.aScratchFree ){
- if( sqlite3Config.bMemstat ){
+ if( sqlite3GlobalConfig.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);
+ sqlite3GlobalConfig.m.xFree(p);
sqlite3_mutex_leave(mem0.mutex);
}else{
- sqlite3Config.m.xFree(p);
+ sqlite3GlobalConfig.m.xFree(p);
}
}else{
int i;
- i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
- i /= sqlite3Config.szScratch;
- assert( i>=0 && i<sqlite3Config.nScratch );
+ i = (int)((u8*)p - (u8*)sqlite3GlobalConfig.pScratch);
+ i /= sqlite3GlobalConfig.szScratch;
+ assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
sqlite3_mutex_enter(mem0.mutex);
- assert( mem0.nScratchFree<sqlite3Config.nScratch );
+ assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
mem0.aScratchFree[mem0.nScratchFree++] = i;
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
sqlite3_mutex_leave(mem0.mutex);
@@ -377,111 +406,29 @@ void sqlite3ScratchFree(void *p){
}
/*
-** 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().
-*/
-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;
- sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
- p = (void*)&((char*)sqlite3Config.pPage)[i];
- }
- }
- return p;
-
-page_overflow:
- if( sqlite3Config.bMemstat ){
- sqlite3_mutex_enter(mem0.mutex);
- sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
- 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;
-}
-void sqlite3PageFree(void *p){
- if( p ){
- if( sqlite3Config.pPage==0
- || p<sqlite3Config.pPage
- || p>=(void*)mem0.aPageFree ){
- /* 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().
- */
- 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{
- /* 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.
- */
- int i;
- i = (u8 *)p - (u8 *)sqlite3Config.pPage;
- 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);
-#if !defined(NDEBUG) && 0
- /* 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
- }
- }
-}
-
-/*
** TRUE if p is a lookaside memory allocation from db
*/
+#ifndef SQLITE_OMIT_LOOKASIDE
static int isLookaside(sqlite3 *db, void *p){
return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
}
+#else
+#define isLookaside(A,B) 0
+#endif
/*
** Return the size of a memory allocation previously obtained from
** sqlite3Malloc() or sqlite3_malloc().
*/
int sqlite3MallocSize(void *p){
- return sqlite3Config.m.xSize(p);
+ return sqlite3GlobalConfig.m.xSize(p);
}
int sqlite3DbMallocSize(sqlite3 *db, void *p){
+ assert( db==0 || sqlite3_mutex_held(db->mutex) );
if( isLookaside(db, p) ){
return db->lookaside.sz;
}else{
- return sqlite3Config.m.xSize(p);
+ return sqlite3GlobalConfig.m.xSize(p);
}
}
@@ -490,13 +437,13 @@ int sqlite3DbMallocSize(sqlite3 *db, void *p){
*/
void sqlite3_free(void *p){
if( p==0 ) return;
- if( sqlite3Config.bMemstat ){
+ if( sqlite3GlobalConfig.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
- sqlite3Config.m.xFree(p);
+ sqlite3GlobalConfig.m.xFree(p);
sqlite3_mutex_leave(mem0.mutex);
}else{
- sqlite3Config.m.xFree(p);
+ sqlite3GlobalConfig.m.xFree(p);
}
}
@@ -505,6 +452,7 @@ void sqlite3_free(void *p){
** connection.
*/
void sqlite3DbFree(sqlite3 *db, void *p){
+ assert( db==0 || sqlite3_mutex_held(db->mutex) );
if( isLookaside(db, p) ){
LookasideSlot *pBuf = (LookasideSlot*)p;
pBuf->pNext = db->lookaside.pFree;
@@ -528,31 +476,33 @@ void *sqlite3Realloc(void *pOld, int nBytes){
sqlite3_free(pOld);
return 0;
}
+ if( nBytes>=0x7fffff00 ){
+ /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
+ return 0;
+ }
nOld = sqlite3MallocSize(pOld);
- if( sqlite3Config.bMemstat ){
+ nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
+ if( nOld==nNew ){
+ pNew = pOld;
+ }else if( sqlite3GlobalConfig.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
- nNew = sqlite3Config.m.xRoundup(nBytes);
- if( nOld==nNew ){
- pNew = pOld;
- }else{
- if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
- mem0.alarmThreshold ){
- sqlite3MallocAlarm(nNew-nOld);
- }
- pNew = sqlite3Config.m.xRealloc(pOld, nNew);
- if( pNew==0 && mem0.alarmCallback ){
- sqlite3MallocAlarm(nBytes);
- pNew = sqlite3Config.m.xRealloc(pOld, nNew);
- }
- if( pNew ){
- nNew = sqlite3MallocSize(pNew);
- sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
- }
+ if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >=
+ mem0.alarmThreshold ){
+ sqlite3MallocAlarm(nNew-nOld);
+ }
+ pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
+ if( pNew==0 && mem0.alarmCallback ){
+ sqlite3MallocAlarm(nBytes);
+ pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
+ }
+ if( pNew ){
+ nNew = sqlite3MallocSize(pNew);
+ sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
}
sqlite3_mutex_leave(mem0.mutex);
}else{
- pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
+ pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
}
return pNew;
}
@@ -595,9 +545,25 @@ void *sqlite3DbMallocZero(sqlite3 *db, int n){
/*
** Allocate and zero memory. If the allocation fails, make
** the mallocFailed flag in the connection pointer.
+**
+** 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.
*/
void *sqlite3DbMallocRaw(sqlite3 *db, int n){
void *p;
+ assert( db==0 || sqlite3_mutex_held(db->mutex) );
+#ifndef SQLITE_OMIT_LOOKASIDE
if( db ){
LookasideSlot *pBuf;
if( db->mallocFailed ){
@@ -613,6 +579,11 @@ void *sqlite3DbMallocRaw(sqlite3 *db, int n){
return (void*)pBuf;
}
}
+#else
+ if( db && db->mallocFailed ){
+ return 0;
+ }
+#endif
p = sqlite3Malloc(n);
if( !p && db ){
db->mallocFailed = 1;
@@ -626,6 +597,8 @@ void *sqlite3DbMallocRaw(sqlite3 *db, int n){
*/
void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
void *pNew = 0;
+ assert( db!=0 );
+ assert( sqlite3_mutex_held(db->mutex) );
if( db->mallocFailed==0 ){
if( p==0 ){
return sqlite3DbMallocRaw(db, n);
@@ -675,7 +648,7 @@ char *sqlite3DbStrDup(sqlite3 *db, const char *z){
if( z==0 ){
return 0;
}
- n = strlen(z)+1;
+ n = sqlite3Strlen30(z) + 1;
assert( (n&0x7fffffff)==n );
zNew = sqlite3DbMallocRaw(db, (int)n);
if( zNew ){
@@ -720,10 +693,10 @@ void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
** sqlite3_realloc.
**
** The returned value is normally a copy of the second argument to this
-** function. However, if a malloc() failure has occured since the previous
+** function. However, if a malloc() failure has occurred since the previous
** invocation SQLITE_NOMEM is returned instead.
**
-** If the first argument, db, is not NULL and a malloc() error has occured,
+** If the first argument, db, is not NULL and a malloc() error has occurred,
** then the connection error-code (the value returned by sqlite3_errcode())
** is set to SQLITE_NOMEM.
*/
@@ -733,7 +706,7 @@ int sqlite3ApiExit(sqlite3* db, int rc){
** is unsafe, as is the call to sqlite3Error().
*/
assert( !db || sqlite3_mutex_held(db->mutex) );
- if( db && db->mallocFailed ){
+ if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
sqlite3Error(db, SQLITE_NOMEM, 0);
db->mallocFailed = 0;
rc = SQLITE_NOMEM;