Compare commits

...

4 Commits

Author SHA1 Message Date
dan 97bc0873cc Further tweaks to malloc paths.
FossilOrigin-Name: 0a33444105e29d79475e267021d33c1a006b85a3
2012-07-19 20:27:14 +00:00
dan 32e28c8ab6 Remove another branch from the DbMalloc() paths.
FossilOrigin-Name: defd828e90a5494e58cc2cd14c8f4cce7fd2bb2b
2012-07-18 16:07:24 +00:00
dan a785a59fb8 Reorganize code to remove a few of the branches introduced into the critical sqlite3DbMallocXXX() path by the previous commit.
FossilOrigin-Name: 75c095ee463c0c76e9637edccd33e807f837eb4e
2012-07-18 11:28:51 +00:00
dan cb2b9c8e19 Enhance the custom memory allocation interface to allow the user to specify a calloc() function.
FossilOrigin-Name: 8752237d123240b330baa461f855dbc01ffcab5b
2012-07-17 19:32:32 +00:00
13 changed files with 235 additions and 144 deletions
+16 -16
View File
@@ -1,5 +1,5 @@
C Ensure\sthat\sthere\sis\salways\sat\sleast\sone\saReadMark\sslot\susable\sby\san\sunprivileged\sreader\swhile\sa\scheckpoint\sis\srunning.\sAlso,\sif\sone\sor\smore\stransactions\sare\srecovered\sfrom\sa\slog\sfile,\sinitialize\sone\sof\sthe\saReadMark\sslots\sto\scontain\smxFrame\sas\spart\sof\sthe\srecovery\sprocess.
D 2012-07-17T14:37:12.494
C Further\stweaks\sto\smalloc\spaths.
D 2012-07-19T20:27:14.139
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 8f6d858bf3df9978ba43df19985146a1173025e4
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -117,7 +117,7 @@ F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc
F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad
F src/alter.c 149cc80d9257971b0bff34e58fb2263e01998289
F src/analyze.c 70c46504c0d2543ea5cdca01140b2cd3e1d886e7
F src/analyze.c 1f05d25f1fb11ba60f95b793e0922757f35355fd
F src/attach.c 577bf5675b0c50495fc28549f2fcbdb1bac71143
F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34
F src/backup.c 5524df36810668b3b5a6de2d7e3910b98e721e33
@@ -126,7 +126,7 @@ F src/btmutex.c 976f45a12e37293e32cae0281b15a21d48a8aaa7
F src/btree.c f0b71054103cb77eb5e782088c16998ec4f06624
F src/btree.h 48a013f8964f12d944d90e4700df47b72dd6d923
F src/btreeInt.h 38a639c0542c29fe8331a221c4aed0cb8686249e
F src/build.c 47c4506afe4bcb4ed1f4b5357582d1cb3402f8ad
F src/build.c 4357149e38f95d8034f58ee2c7013423321fa877
F src/callback.c 0cb4228cdcd827dcc5def98fb099edcc9142dbcd
F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac
F src/ctime.c 500d019da966631ad957c37705642be87524463b
@@ -134,7 +134,7 @@ F src/date.c 067a81c9942c497aafd2c260e13add8a7d0c7dd4
F src/delete.c 4c20ea4f6213b3bc1c6a510586864b679946e05e
F src/expr.c 06a7733d19dc725dc46ba51afd9feadb4b85d991
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
F src/fkey.c 657212460bf5cfd3ae607d12ea62092844c227b5
F src/fkey.c 7d1ec22d7278486c7fe42da43cabfc2a5ed33647
F src/func.c 18dfedfb857e100b05755a1b12e88b389f957879
F src/global.c 4cfdca5cb0edd33c4d021baec4ede958cb2c793b
F src/hash.c 458488dcc159c301b8e7686280ab209f1fb915af
@@ -145,10 +145,10 @@ F src/journal.c 552839e54d1bf76fb8f7abe51868b66acacf6a0e
F src/legacy.c a199d7683d60cef73089e892409113e69c23a99f
F src/lempar.c 0ee69fca0be54cd93939df98d2aca4ca46f44416
F src/loadext.c f20382fbaeec832438a1ba7797bee3d3c8a6d51d
F src/main.c 02255cf1da50956c5427c469abddb15bccc4ba09
F src/malloc.c fe085aa851b666b7c375c1ff957643dc20a04bf6
F src/main.c 6241183d894ff51a2049bd6bf246aea877d648b5
F src/malloc.c 57121e3ef46431b95a73651f9b5653f96867e358
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c b3677415e69603d6a0e7c5410a1b3731d55beda1
F src/mem1.c 7ed7d5cd9a6ec97cc881ea80d996f6a16e8b7426
F src/mem2.c e307323e86b5da1853d7111b68fd6b84ad6f09cf
F src/mem3.c 61c9d47b792908c532ca3a62b999cf21795c6534
F src/mem5.c c2c63b7067570b00bf33d751c39af24182316f7f
@@ -166,7 +166,7 @@ F src/os_unix.c d7c96b5d140f550f07345870112fae5d7ef99757
F src/os_win.c e3d3d3e26b65a35d4293d753137a58510bd3299b
F src/pager.c e381c118b77dc22021a1a59d3fec24815e91df78
F src/pager.h 8b8c9bc065a3c66769df8724dfdf492ee1aab3c5
F src/parse.y f29df90bd3adc64b33114ab1de9fb7768fcf2099
F src/parse.y 6548920e5a3ee92c6efc7007e30b03105850ca4c
F src/pcache.c f8043b433a57aba85384a531e3937a804432a346
F src/pcache.h 1b5dcc3dc8103d03e625b177023ee67764fa6b7c
F src/pcache1.c 2234d84f9c003d800a57f00f8535c91667fa4f6c
@@ -176,11 +176,11 @@ F src/printf.c 4a9f882f1c1787a8b494a2987765acf9d97ac21f
F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
F src/resolve.c b3c70ab28cac60de33684c9aa9e5138dcf71d6dd
F src/rowset.c f6a49f3e9579428024662f6e2931832511f831a1
F src/select.c f6c4833c4d8e94714761d99013d74f381e084f1d
F src/select.c 5c9f09de4ca3e12da17d1c5215b851ce3356891c
F src/shell.c 076e1c90d594644f36027c8ecff9a392cf2d3a06
F src/sqlite.h.in 310ae7e538883fa1619ab0638c775ce11ad43015
F src/sqlite.h.in c660e2c8c5b5412d1ce51d58807353fa1dfdbee0
F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
F src/sqliteInt.h 2bc2ebc2ff1a2b530ee5ed9ffd46c6fce93b244c
F src/sqliteInt.h 42082771dfad5ccc5b46ad97bddfe2829a61eaef
F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
F src/status.c 35939e7e03abf1b7577ce311f48f682c40de3208
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -231,7 +231,7 @@ F src/test_vfs.c da6d0d982b11756c94c1760196355d33d03ff745
F src/test_vfstrace.c 6b28adb2a0e8ecd0f2e3581482e1f658b11b4067
F src/test_wholenumber.c 3d2b9ed1505c40ad5c5ca2ad16ae7a289d6cc251
F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9
F src/tokenize.c 1e86210d3976717a19238ea7b047fac481fe8c12
F src/tokenize.c a054f58de63c2d7f5ba69439ef7fc75e7f22c158
F src/trigger.c ee7e178fb9188f44b532cebd449a7c1df90fb684
F src/update.c d3076782c887c10e882996550345da9c4c9f9dea
F src/utf.c 890c67dcfcc7a74623c95baac7535aadfe265e84
@@ -1005,7 +1005,7 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
P 8c9ee1d78f99394eef73a177141ca9e1c67e4e07
R 21a0c6942de3e9593e3e93f462c443c7
P defd828e90a5494e58cc2cd14c8f4cce7fd2bb2b
R 62a7b8739dbc0504b7043c6629065afd
U dan
Z a98d8c358c03bd601d60af308961371e
Z f8c7a516451a2fad10c61fa887e0e873
+1 -1
View File
@@ -1 +1 @@
e4163596339c2166f9c4356ab824fff8bda8d0b0
0a33444105e29d79475e267021d33c1a006b85a3
+4 -4
View File
@@ -932,7 +932,7 @@ static int loadStat3(sqlite3 *db, const char *zDb){
int eType; /* Datatype of a sample */
IndexSample *pSample; /* A slot in pIdx->aSample[] */
assert( db->lookaside.bEnabled==0 );
assert( db->lookaside.sz==0 );
if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
return SQLITE_OK;
}
@@ -1108,10 +1108,10 @@ int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
/* Load the statistics from the sqlite_stat3 table. */
#ifdef SQLITE_ENABLE_STAT3
if( rc==SQLITE_OK ){
int lookasideEnabled = db->lookaside.bEnabled;
db->lookaside.bEnabled = 0;
int lookasideSz = db->lookaside.sz;
db->lookaside.sz = 0;
rc = loadStat3(db, sInfo.zDatabase);
db->lookaside.bEnabled = lookasideEnabled;
db->lookaside.sz = lookasideSz;
}
#endif
+3 -3
View File
@@ -1816,11 +1816,11 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
assert( pTable->pSelect );
pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
if( pSel ){
u8 enableLookaside = db->lookaside.bEnabled;
u16 lookasideSz = db->lookaside.sz;
n = pParse->nTab;
sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
pTable->nCol = -1;
db->lookaside.bEnabled = 0;
db->lookaside.sz = 0;
#ifndef SQLITE_OMIT_AUTHORIZATION
xAuth = db->xAuth;
db->xAuth = 0;
@@ -1829,7 +1829,7 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
#else
pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
#endif
db->lookaside.bEnabled = enableLookaside;
db->lookaside.sz = lookasideSz;
pParse->nTab = n;
if( pSelTab ){
assert( pTable->aCol==0 );
+4 -4
View File
@@ -981,7 +981,7 @@ static Trigger *fkActionTrigger(
pTrigger = pFKey->apTrigger[iAction];
if( action!=OE_None && !pTrigger ){
u8 enableLookaside; /* Copy of db->lookaside.bEnabled */
u16 lookasideSz; /* Copy of db->lookaside.sz */
char const *zFrom; /* Name of child table */
int nFrom; /* Length in bytes of zFrom */
Index *pIdx = 0; /* Parent key index for this FK */
@@ -1090,8 +1090,8 @@ static Trigger *fkActionTrigger(
}
/* Disable lookaside memory allocation */
enableLookaside = db->lookaside.bEnabled;
db->lookaside.bEnabled = 0;
lookasideSz = db->lookaside.sz;
db->lookaside.sz = 0;
pTrigger = (Trigger *)sqlite3DbMallocZero(db,
sizeof(Trigger) + /* struct Trigger */
@@ -1114,7 +1114,7 @@ static Trigger *fkActionTrigger(
}
/* Re-enable the lookaside buffer, if it was disabled earlier. */
db->lookaside.bEnabled = enableLookaside;
db->lookaside.sz = lookasideSz;
sqlite3ExprDelete(db, pWhere);
sqlite3ExprDelete(db, pWhen);
+31 -5
View File
@@ -302,6 +302,17 @@ int sqlite3_shutdown(void){
return SQLITE_OK;
}
/*
** If a custom memory allocator is configured using the legacy
** SQLITE_CONFIG_MALLOC interface, this function is used as the xCalloc()
** method.
*/
static void *syntheticCalloc(int nByte){
void *pRet = sqlite3GlobalConfig.m.xMalloc(nByte);
if( pRet ) memset(pRet, 0, nByte);
return pRet;
}
/*
** This API allows applications to modify the global configuration of
** the SQLite library at run-time.
@@ -357,18 +368,33 @@ int sqlite3_config(int op, ...){
}
#endif
case SQLITE_CONFIG_MALLOC: {
/* Specify an alternative malloc implementation */
int nCopy = offsetof(sqlite3_mem_methods, xCalloc);
memcpy(&sqlite3GlobalConfig.m, va_arg(ap, sqlite3_mem_methods*), nCopy);
sqlite3GlobalConfig.m.xCalloc = syntheticCalloc;
break;
}
case SQLITE_CONFIG_GETMALLOC: {
/* Retrieve the current malloc() implementation */
int nCopy = offsetof(sqlite3_mem_methods, xCalloc);
if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
memcpy(va_arg(ap, sqlite3_mem_methods*), &sqlite3GlobalConfig.m, nCopy);
break;
}
case SQLITE_CONFIG_MALLOC2: {
/* Specify an alternative malloc implementation */
sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
break;
}
case SQLITE_CONFIG_GETMALLOC: {
case SQLITE_CONFIG_GETMALLOC2: {
/* Retrieve the current malloc() implementation */
if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
*va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
break;
}
case SQLITE_CONFIG_MEMSTATUS: {
/* Enable or disable the malloc status collection */
sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
@@ -526,7 +552,7 @@ static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
}
db->lookaside.pStart = pStart;
db->lookaside.pFree = 0;
db->lookaside.sz = (u16)sz;
db->lookaside.szEnabled = db->lookaside.sz = (u16)sz;
if( pStart ){
int i;
LookasideSlot *p;
@@ -538,12 +564,12 @@ static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
p = (LookasideSlot*)&((u8*)p)[sz];
}
db->lookaside.pEnd = p;
db->lookaside.bEnabled = 1;
db->lookaside.bMalloced = pBuf==0 ?1:0;
assert( db->lookaside.sz>0 );
}else{
db->lookaside.pEnd = 0;
db->lookaside.bEnabled = 0;
db->lookaside.bMalloced = 0;
db->lookaside.sz = 0;
}
return SQLITE_OK;
}
+127 -78
View File
@@ -259,14 +259,27 @@ static void sqlite3MallocAlarm(int nByte){
** 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;
static void *mallocWithAlarm(
void *(*xAlloc)(int), /* Memory allocation function */
int n /* Bytes of memory to allocate */
){
void *p; /* Pointer to allocated memory */
int nFull; /* Value returned by xRoundup (if required) */
assert( sqlite3_mutex_held(mem0.mutex) );
nFull = sqlite3GlobalConfig.m.xRoundup(n);
assert( xAlloc==sqlite3GlobalConfig.m.xMalloc
|| xAlloc==sqlite3GlobalConfig.m.xCalloc
);
/* Note: At one point this function would call xRoundup() and use the
** resulting value as the argument to xAlloc(). However, this is not
** required. And it is a (small) drag on performance. */
/* nFull = sqlite3GlobalConfig.m.xRoundup(n); */
sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
if( mem0.alarmCallback!=0 ){
if( mem0.alarmCallback ){
int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
nFull = sqlite3GlobalConfig.m.xRoundup(n);
if( nUsed >= mem0.alarmThreshold - nFull ){
mem0.nearlyFull = 1;
sqlite3MallocAlarm(nFull);
@@ -274,27 +287,33 @@ static int mallocWithAlarm(int n, void **pp){
mem0.nearlyFull = 0;
}
}
p = sqlite3GlobalConfig.m.xMalloc(nFull);
p = xAlloc(n);
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
if( p==0 && mem0.alarmCallback ){
sqlite3MallocAlarm(nFull);
p = sqlite3GlobalConfig.m.xMalloc(nFull);
p = xAlloc(nFull);
}
#endif
if( p ){
nFull = sqlite3MallocSize(p);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
}else{
testcase( sqlite3GlobalConfig.xLog!=0 );
sqlite3_log(SQLITE_NOMEM, "failed to allocate %d bytes of memory", n);
}
*pp = p;
return nFull;
return p;
}
/*
** Allocate memory. This routine is like sqlite3_malloc() except that it
** assumes the memory subsystem has already been initialized.
** Use allocator function xAlloc to allocate n bytes of memory.
*/
void *sqlite3Malloc(int n){
static void *memAllocate(
sqlite3 *db,
void *(*xAlloc)(int), /* Memory allocation function */
int n /* Bytes of space to allocate */
){
void *p;
if( n<=0 /* IMP: R-65312-04917 */
|| n>=0x7fffff00
@@ -305,17 +324,42 @@ void *sqlite3Malloc(int n){
** 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( sqlite3GlobalConfig.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
mallocWithAlarm(n, &p);
sqlite3_mutex_leave(mem0.mutex);
}else{
p = sqlite3GlobalConfig.m.xMalloc(n);
if( sqlite3GlobalConfig.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
p = mallocWithAlarm(xAlloc, n);
sqlite3_mutex_leave(mem0.mutex);
}else{
p = xAlloc(n);
}
if( !p && db ) db->mallocFailed = 1;
}
assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-04675-44850 */
if( db ){
sqlite3MemdebugSetType(p, MEMTYPE_DB |
((db && db->lookaside.sz) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)
);
}
return p;
}
/*
** Allocate memory. This routine is like sqlite3_malloc() except that it
** assumes the memory subsystem has already been initialized.
*/
void *sqlite3Malloc(int n){
return memAllocate(0, sqlite3GlobalConfig.m.xMalloc, n);
}
/*
** Allocate and zero memory.
*/
void *sqlite3MallocZero(int n){
return memAllocate(0, sqlite3GlobalConfig.m.xCalloc, n);
}
/*
** This version of the memory allocation is for use by the application.
** First make sure the memory subsystem is initialized, then do the
@@ -362,8 +406,11 @@ void *sqlite3ScratchMalloc(int n){
}else{
if( sqlite3GlobalConfig.bMemstat ){
sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
n = mallocWithAlarm(n, &p);
if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
p = mallocWithAlarm(sqlite3GlobalConfig.m.xMalloc, n);
if( p ){
n = sqlite3MallocSize(p);
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
}
sqlite3_mutex_leave(mem0.mutex);
}else{
sqlite3_mutex_leave(mem0.mutex);
@@ -570,33 +617,42 @@ void *sqlite3_realloc(void *pOld, int n){
return sqlite3Realloc(pOld, n);
}
/*
** Allocate and zero memory.
*/
void *sqlite3MallocZero(int n){
void *p = sqlite3Malloc(n);
if( p ){
memset(p, 0, n);
}
return p;
}
/*
** Allocate and zero memory. If the allocation fails, make
** the mallocFailed flag in the connection pointer.
** Attempt to allocate an n byte block from the lookaside buffer of
** connection db. If successful, return a pointer to the new allocation.
** Otherwise, return a NULL pointer.
*/
void *sqlite3DbMallocZero(sqlite3 *db, int n){
void *p = sqlite3DbMallocRaw(db, n);
if( p ){
memset(p, 0, n);
#ifndef SQLITE_OMIT_LOOKASIDE
static void *lookasideAlloc(sqlite3 *db, int n){
assert( db->lookaside.sz==0 || db->lookaside.sz==db->lookaside.szEnabled );
if( n>db->lookaside.sz ){
/* If db->lookaside.sz is 0, then the lookaside buffer is currently
** disabled. In this case do not increment the "size misses" stat. */
if( db->lookaside.sz ) db->lookaside.anStat[1]++;
}else{
LookasideSlot *pBuf;
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;
}
}
sqlite3MemdebugSetType((void *)pBuf, MEMTYPE_DB | MEMTYPE_LOOKASIDE);
return (void*)pBuf;
}
return p;
return 0;
}
#else
# define lookasideAlloc(x,y) 0
#endif
/*
** Allocate and zero memory. If the allocation fails, make
** the mallocFailed flag in the connection pointer.
** Allocate and zero memory. If the allocation fails, set 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.
@@ -612,44 +668,37 @@ void *sqlite3DbMallocZero(sqlite3 *db, int n){
** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
** that all prior mallocs (ex: "a") worked too.
*/
void *sqlite3DbMallocZero(sqlite3 *db, int n){
void *p;
if( db ){
if( db->mallocFailed ) return 0;
if( (p = lookasideAlloc(db, n)) ){
memset(p, 0, n);
return p;
}
}
return memAllocate(db, sqlite3GlobalConfig.m.xCalloc, n);
}
/*
** Allocate 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.
** See also comments above sqlite3DbMallocZero() for details.
*/
void *sqlite3DbMallocRaw(sqlite3 *db, int n){
void *p;
assert( db==0 || sqlite3_mutex_held(db->mutex) );
assert( db==0 || db->pnBytesFreed==0 );
#ifndef SQLITE_OMIT_LOOKASIDE
if( db ){
LookasideSlot *pBuf;
if( db->mallocFailed ){
return 0;
}
if( db->lookaside.bEnabled ){
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;
}
return (void*)pBuf;
}
if( db->mallocFailed ) return 0;
if( (p = lookasideAlloc(db, n)) ){
return p;
}
}
#else
if( db && db->mallocFailed ){
return 0;
}
#endif
p = sqlite3Malloc(n);
if( !p && db ){
db->mallocFailed = 1;
}
sqlite3MemdebugSetType(p, MEMTYPE_DB |
((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
return p;
return memAllocate(db, sqlite3GlobalConfig.m.xMalloc, n);
}
/*
@@ -665,12 +714,12 @@ void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
return sqlite3DbMallocRaw(db, n);
}
if( isLookaside(db, p) ){
if( n<=db->lookaside.sz ){
if( n<=db->lookaside.szEnabled ){
return p;
}
pNew = sqlite3DbMallocRaw(db, n);
if( pNew ){
memcpy(pNew, p, db->lookaside.sz);
memcpy(pNew, p, db->lookaside.szEnabled);
sqlite3DbFree(db, p);
}
}else{
@@ -683,7 +732,7 @@ void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
db->mallocFailed = 1;
}
sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
(db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
(db->lookaside.sz ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
}
}
return pNew;
+28 -19
View File
@@ -70,6 +70,7 @@
#include <libkern/OSAtomic.h>
static malloc_zone_t* _sqliteZone_;
#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
#define SQLITE_CALLOC(x) malloc_zone_calloc(_sqliteZone_, (x), 1)
#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
#define SQLITE_MALLOCSIZE(x) \
@@ -82,6 +83,7 @@ static malloc_zone_t* _sqliteZone_;
** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
*/
#define SQLITE_MALLOC(x) malloc(x)
#define SQLITE_CALLOC(x) calloc((x), 1)
#define SQLITE_FREE(x) free(x)
#define SQLITE_REALLOC(x,y) realloc((x),(y))
@@ -100,8 +102,7 @@ static malloc_zone_t* _sqliteZone_;
#endif /* __APPLE__ or not __APPLE__ */
/*
** Like malloc(), but remember the size of the allocation
** so that we can find it later using sqlite3MemSize().
** Allocate nByte bytes of memory.
**
** For this low-level routine, we are guaranteed that nByte>0 because
** cases of nByte<=0 will be intercepted and dealt with by higher level
@@ -109,24 +110,31 @@ static malloc_zone_t* _sqliteZone_;
*/
static void *sqlite3MemMalloc(int nByte){
#ifdef SQLITE_MALLOCSIZE
void *p = SQLITE_MALLOC( nByte );
if( p==0 ){
testcase( sqlite3GlobalConfig.xLog!=0 );
sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
}
return p;
return SQLITE_MALLOC( ROUND8(nByte) );
#else
sqlite3_int64 *p;
assert( nByte>0 );
i64 *p;
nByte = ROUND8(nByte);
p = SQLITE_MALLOC( nByte+8 );
if( p ){
p[0] = nByte;
p++;
}else{
testcase( sqlite3GlobalConfig.xLog!=0 );
sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
}
p = SQLITE_MALLOC(nByte+8);
if( p ) *(p++) = (i64)nByte;
return (void *)p;
#endif
}
/*
** Allocate and zero nByte bytes of memory.
**
** For this low-level routine, we are guaranteed that nByte>0 because
** cases of nByte<=0 will be intercepted and dealt with by higher level
** routines.
*/
static void *sqlite3MemCalloc(int nByte){
#ifdef SQLITE_MALLOCSIZE
return SQLITE_CALLOC( ROUND8(nByte) );
#else
i64 *p;
nByte = ROUND8(nByte);
p = SQLITE_CALLOC(nByte+8);
if( p ) *(p++) = (i64)nByte;
return (void *)p;
#endif
}
@@ -271,7 +279,8 @@ void sqlite3MemSetDefault(void){
sqlite3MemRoundup,
sqlite3MemInit,
sqlite3MemShutdown,
0
0,
sqlite3MemCalloc
};
sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
}
+2 -2
View File
@@ -152,7 +152,7 @@ create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
}
createkw(A) ::= CREATE(X). {
pParse->db->lookaside.bEnabled = 0;
pParse->db->lookaside.sz = 0;
A = X;
}
%type ifnotexists {int}
@@ -1378,7 +1378,7 @@ cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
sqlite3AlterFinishAddColumn(pParse, &Y);
}
add_column_fullname ::= fullname(X). {
pParse->db->lookaside.bEnabled = 0;
pParse->db->lookaside.sz = 0;
sqlite3AlterBeginAddColumn(pParse, X);
}
kwcolumn_opt ::= .
+3 -3
View File
@@ -1398,9 +1398,9 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
if( pTab==0 ){
return 0;
}
/* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
** is disabled */
assert( db->lookaside.bEnabled==0 );
/* The sqlite3ResultSetOfSelect() should only used in contexts where the
** lookaside buffer is disabled. */
assert( db->lookaside.sz==0 );
pTab->nRef = 1;
pTab->zName = 0;
pTab->nRowEst = 1000000;
+5 -2
View File
@@ -1341,6 +1341,7 @@ struct sqlite3_mem_methods {
int (*xInit)(void*); /* Initialize the memory allocator */
void (*xShutdown)(void*); /* Deinitialize the memory allocator */
void *pAppData; /* Argument to xInit() and xShutdown() */
void *(*xCalloc)(int); /* Zeroed memory allocation function */
};
/*
@@ -1577,8 +1578,8 @@ struct sqlite3_mem_methods {
#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* no xCalloc() */
#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* no xCalloc() */
/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
#define SQLITE_CONFIG_PCACHE 14 /* no-op */
@@ -1587,6 +1588,8 @@ struct sqlite3_mem_methods {
#define SQLITE_CONFIG_URI 17 /* int */
#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
#define SQLITE_CONFIG_MALLOC2 20 /* sqlite3_mem_methods* incl xCalloc() */
#define SQLITE_CONFIG_GETMALLOC2 21 /* sqlite3_mem_methods* incl xCalloc() */
/*
** CAPI3REF: Database Connection Configuration Options
+5 -3
View File
@@ -783,12 +783,14 @@ struct Schema {
** with a particular database connection. Hence, schema information cannot
** be stored in lookaside because in shared cache mode the schema information
** is shared by multiple database connections. Therefore, while parsing
** schema information, the Lookaside.bEnabled flag is cleared so that
** lookaside allocations are not used to construct the schema objects.
** schema information, the Lookaside.sz variable is temporarily set to
** zero so that lookaside allocations are not used to construct the schema
** objects. Lookaside.szEnabled always contains the allocation size that
** Lookaside.sz is set to when the buffer is enabled.
*/
struct Lookaside {
u16 sz; /* Size of each buffer in bytes */
u8 bEnabled; /* False to disable new lookaside allocations */
u16 szEnabled; /* Value of 'sz' when buffer is enabled. */
u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
int nOut; /* Number of buffers currently checked out */
int mxOut; /* Highwater mark for nOut */
+6 -4
View File
@@ -390,7 +390,7 @@ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
void *pEngine; /* The LEMON-generated LALR(1) parser */
int tokenType; /* type of the next token */
int lastTokenParsed = -1; /* type of the previous token */
u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
u16 lookasideSz; /* Saved value of db->lookaside.sz */
sqlite3 *db = pParse->db; /* The database connection */
int mxSqlLen; /* Max length of an SQL string */
@@ -413,8 +413,10 @@ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
assert( pParse->nVar==0 );
assert( pParse->nzVar==0 );
assert( pParse->azVar==0 );
enableLookaside = db->lookaside.bEnabled;
if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
lookasideSz = db->lookaside.sz;
if( db->lookaside.pStart ){
db->lookaside.sz = db->lookaside.szEnabled;
}
while( !db->mallocFailed && zSql[i]!=0 ){
assert( i>=0 );
pParse->sLastToken.z = &zSql[i];
@@ -468,7 +470,7 @@ abort_parse:
);
#endif /* YYDEBUG */
sqlite3ParserFree(pEngine, sqlite3_free);
db->lookaside.bEnabled = enableLookaside;
db->lookaside.sz = lookasideSz;
if( db->mallocFailed ){
pParse->rc = SQLITE_NOMEM;
}