diff --git a/manifest b/manifest index f3dd4563d3..44bd32e914 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Disable\sthe\s/./\sand\s/../\scollapser\slogic\sin\ssqlite3OsFullPathname\sunder\sUnix.\s(CVS\s3092) -D 2006-02-13T18:42:21 +C Changes\sso\sthat\stest_async.c\sworks\swith\smemory\smanagement\sturned\son.\s(CVS\s3093) +D 2006-02-14T10:48:39 F Makefile.in 5d8dff443383918b700e495de42ec65bc1c8865b F Makefile.linux-gcc 74ba0eadf88748a9ce3fd03d2a3ede2e6715baec F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -44,8 +44,8 @@ F src/delete.c ca404d5fd5f678e32f2f46377ad802cd0219aa99 F src/experimental.c 1b2d1a6cd62ecc39610e97670332ca073c50792b F src/expr.c 9c957fabf95ef62288151eecd5c490a629470666 F src/func.c aa62ef0f5b7d2907ea7e34a33402409f1a8806eb -F src/hash.c 8747cf51d12de46512880dfcf1b68b4e24072863 -F src/hash.h 1b0c445e1c89ff2aaad9b4605ba61375af001e84 +F src/hash.c 449f3d6620193aa557f5d86cbc5cc6b87702b185 +F src/hash.h 1b3f7e2609141fd571f62199fc38687d262e9564 F src/insert.c 67b3dc11831c58d8703eb502355ad3704ee18f66 F src/legacy.c 86b669707b3cefd570e34154e2f6457547d1df4f F src/main.c 9a42464c44a6532003391486e802e65e88789cfc @@ -55,7 +55,7 @@ F src/os.h 93035a0e3b9dd05cdd0aaef32ea28ca28e02fe78 F src/os_common.h 108cd719c96a2b714b64e02aeabbd40684274e6a F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3 -F src/os_unix.c 365edd498b0fac097be5490b85fdb5ed7807691a +F src/os_unix.c 757a7b726764367f7b0595c4302969582c04413d F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e F src/os_win.c c67a2c46d929cf54c8f80ec5e6079cf684a141a9 F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b @@ -70,7 +70,7 @@ F src/select.c 7d069e875d0eec05129c7e8b9c99422d7c9c6321 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/shell.c 738f55ed75fb36731e764bfdb40756ac43b90b08 F src/sqlite.h.in bc78a247fd9f294b30a4c03894f93fcb1e166410 -F src/sqliteInt.h f61b60f243f1709c943c62cfb6d7a50209080e38 +F src/sqliteInt.h ba831d1f89d89892590f1cbe89058f5900b6d2f2 F src/table.c f64ec4fbfe333f8df925bc6ba494f55e05b0e75e F src/tclsqlite.c d9c26374b52cd47233ae0620d0a858a59b601f89 F src/test1.c ca8cb34747c53479e0748c11d1a10cc07d582bb8 @@ -80,14 +80,14 @@ F src/test4.c ff4e9406b3d2809966d8f0e82468ac5508be9f56 F src/test5.c 7162f8526affb771c4ed256826eee7bb9eca265f F src/test6.c 60a02961ceb7b3edc25f5dc5c1ac2556622a76de F src/test7.c d28d3e62f9594923648fc6a8fb030eba36564ba1 -F src/test_async.c 2e7632272cb1ec33982314e29f9952f5a72e7b38 +F src/test_async.c 02531d8b049ae0d8bf1a709ac691dac1d9b147bb F src/test_md5.c 6c42bc0a3c0b54be34623ff77a0eec32b2fa96e3 F src/test_server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/tokenize.c 382b3bb0ca26eb9153b5d20b246ef512a114a24f F src/trigger.c 4d3644cbd16959b568c95ae73493402be8021b08 F src/update.c 050a7e0ddaac03dec5271712eee62f1a9e699049 F src/utf.c 1199766bbb0157931a83aa6eede6b6381177be64 -F src/util.c 405f46fef062b476826d2c171ec21def29563b75 +F src/util.c 8aa4f443a2c4a790f4ab3b38d148e108e9c00f2e F src/vacuum.c 3865673cc66acd0717ecd517f6b8fdb2a5e7924b F src/vdbe.c 0a7fd81609429bae2b3c326687b02a60a9c01c49 F src/vdbe.h 80ba1c391ec28180dd07a630577f50b22c2062da @@ -105,7 +105,7 @@ F test/alter2.test cc0b8832e4e98605dbc26910efd4bb89abe59cb2 F test/alter3.test a6eec8f454be9b6ce73d8d7dc711453675a10ce7 F test/altermalloc.test 6e1f404ec021eb2ba6582e3c77b0a35cf206b7af F test/analyze.test 2f55535aa335785db1a2f97d3f3831c16c09f8b0 -F test/async.test eb14d928f45e078df9a8927ae9a0b4bcff981db1 +F test/async.test ae59f861f17f3e9076cd557cd93677b7c77e57b5 F test/attach.test 036315207c477211470168bf121b1c493f781515 F test/attach2.test 0e6a7c54343c85dd877a1e86073a05176043ed40 F test/attach3.test 63013383adc4380af69779f34f4af19bd49f7cbe @@ -214,7 +214,7 @@ F test/pagesize.test 05c74ea49f790734ec1e9ab765d9bf1cce79b8f2 F test/pragma.test 12e1b74e37964bb161f21fb8b1d3f31b2724b47e F test/printf.test 9e10c74e16bf889f8495ddb3d6f5f891e75ff1b7 F test/progress.test 16496001da445e6534afb94562c286708316d82f x -F test/quick.test 38d1feefe5bfd0a61aa535775448276630ecfb92 +F test/quick.test e220b3b6e62fe4fb4e2a703ab2ba730fedfe0424 F test/quote.test 5891f2338980916cf7415484b4ce785294044adb F test/reindex.test 38b138abe36bf9a08c791ed44d9f76cd6b97b78b F test/rollback.test 673cd8c44c685ad54987fe7f0eeba84efa09685d @@ -352,7 +352,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 -P 5b16c43542194d6c736bf7aadaaa7f31ff12af31 -R 831835406768778b86263965c0d8f6b6 -U drh -Z 22bc2ca1978bf264cba39ec7791e0786 +P 111a426b3e2fae77c9e6c3cd903fd80652b23720 +R 9e1ed54ea3d446f32aefb065fced0a43 +U danielk1977 +Z fdf8411d6a6b39c9d4eae52b0ecfc18b diff --git a/manifest.uuid b/manifest.uuid index d19b181f02..9ef0ef60fa 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -111a426b3e2fae77c9e6c3cd903fd80652b23720 \ No newline at end of file +f4150c29df2774b4422d4296d913cdbcee62c859 \ No newline at end of file diff --git a/src/hash.c b/src/hash.c index 2ef86a84f3..f15613e143 100644 --- a/src/hash.c +++ b/src/hash.c @@ -12,7 +12,7 @@ ** This is the implementation of generic hash-tables ** used in SQLite. ** -** $Id: hash.c,v 1.17 2005/10/03 15:11:09 drh Exp $ +** $Id: hash.c,v 1.18 2006/02/14 10:48:39 danielk1977 Exp $ */ #include "sqliteInt.h" #include @@ -41,6 +41,8 @@ void sqlite3HashInit(Hash *pNew, int keyClass, int copyKey){ pNew->count = 0; pNew->htsize = 0; pNew->ht = 0; + pNew->xMalloc = sqlite3MallocX; + pNew->xFree = sqlite3FreeX; } /* Remove all entries from a hash table. Reclaim all memory. @@ -53,15 +55,15 @@ void sqlite3HashClear(Hash *pH){ assert( pH!=0 ); elem = pH->first; pH->first = 0; - if( pH->ht ) sqliteFree(pH->ht); + if( pH->ht ) pH->xFree(pH->ht); pH->ht = 0; pH->htsize = 0; while( elem ){ HashElem *next_elem = elem->next; if( pH->copyKey && elem->pKey ){ - sqliteFree(elem->pKey); + pH->xFree(elem->pKey); } - sqliteFree(elem); + pH->xFree(elem); elem = next_elem; } pH->count = 0; @@ -222,9 +224,9 @@ static void rehash(Hash *pH, int new_size){ int (*xHash)(const void*,int); /* The hash function */ assert( (new_size & (new_size-1))==0 ); - new_ht = (struct _ht *)sqliteMalloc( new_size*sizeof(struct _ht) ); + new_ht = (struct _ht *)pH->xMalloc( new_size*sizeof(struct _ht) ); if( new_ht==0 ) return; - if( pH->ht ) sqliteFree(pH->ht); + if( pH->ht ) pH->xFree(pH->ht); pH->ht = new_ht; pH->htsize = new_size; xHash = hashFunction(pH->keyClass); @@ -290,9 +292,9 @@ static void removeElementGivenHash( pEntry->chain = 0; } if( pH->copyKey && elem->pKey ){ - sqliteFree(elem->pKey); + pH->xFree(elem->pKey); } - sqliteFree( elem ); + pH->xFree( elem ); pH->count--; if( pH->count<=0 ){ assert( pH->first==0 ); @@ -358,12 +360,12 @@ void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ return old_data; } if( data==0 ) return 0; - new_elem = (HashElem*)sqliteMalloc( sizeof(HashElem) ); + new_elem = (HashElem*)pH->xMalloc( sizeof(HashElem) ); if( new_elem==0 ) return data; if( pH->copyKey && pKey!=0 ){ - new_elem->pKey = sqliteMallocRaw( nKey ); + new_elem->pKey = pH->xMalloc( nKey ); if( new_elem->pKey==0 ){ - sqliteFree(new_elem); + pH->xFree(new_elem); return data; } memcpy((void*)new_elem->pKey, pKey, nKey); @@ -376,7 +378,7 @@ void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ rehash(pH,8); if( pH->htsize==0 ){ pH->count = 0; - sqliteFree(new_elem); + pH->xFree(new_elem); return data; } } diff --git a/src/hash.h b/src/hash.h index aa17828a6e..78f60b4b9a 100644 --- a/src/hash.h +++ b/src/hash.h @@ -12,7 +12,7 @@ ** This is the header file for the generic hash-table implemenation ** used in SQLite. ** -** $Id: hash.h,v 1.8 2004/08/20 14:08:51 drh Exp $ +** $Id: hash.h,v 1.9 2006/02/14 10:48:39 danielk1977 Exp $ */ #ifndef _SQLITE_HASH_H_ #define _SQLITE_HASH_H_ @@ -34,6 +34,8 @@ struct Hash { char copyKey; /* True if copy of key made on insert */ int count; /* Number of entries in this table */ HashElem *first; /* The first element of the array */ + void *(*xMalloc)(int); /* malloc() function to use */ + void (*xFree)(void *); /* free() function to use */ int htsize; /* Number of buckets in the hash table */ struct _ht { /* the hash table */ int count; /* Number of entries with this hash */ diff --git a/src/os_unix.c b/src/os_unix.c index ce3101ed10..c61efb81e6 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -340,9 +340,10 @@ struct openCnt { ** openKey structures) into lockInfo and openCnt structures. Access to ** these hash tables must be protected by a mutex. */ -static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 }; -static Hash openHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 }; - +static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, + sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0}; +static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, + sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0}; #ifdef SQLITE_UNIX_THREADS /* @@ -491,7 +492,7 @@ static void releaseLockInfo(struct lockInfo *pLock){ pLock->nRef--; if( pLock->nRef==0 ){ sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0); - sqliteFree(pLock); + sqlite3ThreadSafeFree(pLock); } } @@ -504,7 +505,7 @@ static void releaseOpenCnt(struct openCnt *pOpen){ if( pOpen->nRef==0 ){ sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0); free(pOpen->aPending); - sqliteFree(pOpen); + sqlite3ThreadSafeFree(pOpen); } } @@ -545,7 +546,7 @@ static int findLockInfo( pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1)); if( pLock==0 ){ struct lockInfo *pOld; - pLock = sqliteMallocRaw( sizeof(*pLock) ); + pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) ); if( pLock==0 ){ rc = 1; goto exit_findlockinfo; @@ -557,7 +558,7 @@ static int findLockInfo( pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock); if( pOld!=0 ){ assert( pOld==pLock ); - sqliteFree(pLock); + sqlite3ThreadSafeFree(pLock); rc = 1; goto exit_findlockinfo; } @@ -569,7 +570,7 @@ static int findLockInfo( pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2)); if( pOpen==0 ){ struct openCnt *pOld; - pOpen = sqliteMallocRaw( sizeof(*pOpen) ); + pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) ); if( pOpen==0 ){ releaseLockInfo(pLock); rc = 1; @@ -583,7 +584,7 @@ static int findLockInfo( pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen); if( pOld!=0 ){ assert( pOld==pOpen ); - sqliteFree(pOpen); + sqlite3ThreadSafeFree(pOpen); releaseLockInfo(pLock); rc = 1; goto exit_findlockinfo; @@ -1526,7 +1527,7 @@ static int unixClose(OsFile **pId){ id->isOpen = 0; TRACE2("CLOSE %-3d\n", id->h); OpenCounter(-1); - sqliteFree(id); + sqlite3ThreadSafeFree(id); *pId = 0; return SQLITE_OK; } @@ -1635,7 +1636,7 @@ static int allocateUnixFile(unixFile *pInit, OsFile **pId){ pInit->fullSync = 0; pInit->locktype = 0; SET_THREADID(pInit); - pNew = sqliteMalloc( sizeof(unixFile) ); + pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) ); if( pNew==0 ){ close(pInit->h); sqlite3OsEnterMutex(); diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 99619bba36..07ba7e9427 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.481 2006/02/11 01:25:51 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.482 2006/02/14 10:48:39 danielk1977 Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -267,7 +267,6 @@ extern int sqlite3_nFree; /* Number of sqliteFree() calls */ extern int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ extern int sqlite3_iMallocReset; /* Set iMallocFail to this when it reaches 0 */ - extern void *sqlite3_pFirst; /* Pointer to linked list of allocations */ extern int sqlite3_nMaxAlloc; /* High water mark of ThreadData.nAlloc */ extern int sqlite3_mallocDisallowed; /* assert() in sqlite3Malloc() if set */ @@ -276,8 +275,8 @@ extern const char *sqlite3_zFile; /* Filename to associate debug info with */ extern int sqlite3_iLine; /* Line number for debug info */ #define ENTER_MALLOC (sqlite3_zFile = __FILE__, sqlite3_iLine = __LINE__) -#define sqliteMalloc(x) (ENTER_MALLOC, sqlite3Malloc(x)) -#define sqliteMallocRaw(x) (ENTER_MALLOC, sqlite3MallocRaw(x)) +#define sqliteMalloc(x) (ENTER_MALLOC, sqlite3Malloc(x,1)) +#define sqliteMallocRaw(x) (ENTER_MALLOC, sqlite3MallocRaw(x,1)) #define sqliteRealloc(x,y) (ENTER_MALLOC, sqlite3Realloc(x,y)) #define sqliteStrDup(x) (ENTER_MALLOC, sqlite3StrDup(x)) #define sqliteStrNDup(x,y) (ENTER_MALLOC, sqlite3StrNDup(x,y)) @@ -1483,8 +1482,8 @@ int sqlite3Compare(const char *, const char *); int sqlite3SortCompare(const char *, const char *); void sqlite3RealToSortable(double r, char *); -void *sqlite3Malloc(int); -void *sqlite3MallocRaw(int); +void *sqlite3Malloc(int,int); +void *sqlite3MallocRaw(int,int); void sqlite3Free(void*); void *sqlite3Realloc(void*,int); char *sqlite3StrDup(const char*); @@ -1767,6 +1766,14 @@ void sqlite3AbortOtherActiveVdbes(sqlite3 *, Vdbe *); #define sqlite3MallocAllow() #endif +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + void *sqlite3ThreadSafeMalloc(int); + void sqlite3ThreadSafeFree(void *); +#else + #define sqlite3ThreadSafeMalloc sqlite3MallocX + #define sqlite3ThreadSafeFree sqlite3FreeX +#endif + #ifdef SQLITE_SSE #include "sseInt.h" #endif diff --git a/src/test_async.c b/src/test_async.c index a05ed2a0cf..3760583561 100644 --- a/src/test_async.c +++ b/src/test_async.c @@ -475,6 +475,11 @@ static int asyncRead(OsFile *id, void *obuf, int amt){ i64 filesize; int nRead; AsyncFile *pFile = (AsyncFile *)id; + OsFile *pBase = pFile->pBaseRead; + + if( !pBase ){ + pBase = pFile->pBaseWrite; + } /* If an I/O error has previously occurred on this file, then all ** subsequent operations fail. @@ -486,18 +491,18 @@ static int asyncRead(OsFile *id, void *obuf, int amt){ /* Grab the write queue mutex for the duration of the call */ pthread_mutex_lock(&async.queueMutex); - if( pFile->pBaseRead ){ - rc = sqlite3OsFileSize(pFile->pBaseRead, &filesize); + if( pBase ){ + rc = sqlite3OsFileSize(pBase, &filesize); if( rc!=SQLITE_OK ){ goto asyncread_out; } - rc = sqlite3OsSeek(pFile->pBaseRead, pFile->iOffset); + rc = sqlite3OsSeek(pBase, pFile->iOffset); if( rc!=SQLITE_OK ){ goto asyncread_out; } nRead = MIN(filesize - pFile->iOffset, amt); if( nRead>0 ){ - rc = sqlite3OsRead(pFile->pBaseRead, obuf, nRead); + rc = sqlite3OsRead(pBase, obuf, nRead); TRACE(("READ %s %d bytes at %d\n", pFile->zName, nRead, pFile->iOffset)); } } @@ -562,6 +567,9 @@ int asyncFileSize(OsFile *id, i64 *pSize){ ** file-system. */ pBase = ((AsyncFile *)id)->pBaseRead; + if( !pBase ){ + pBase = ((AsyncFile *)id)->pBaseWrite; + } if( pBase ){ rc = sqlite3OsFileSize(pBase, &s); } @@ -802,8 +810,8 @@ static int asyncFileExists(const char *z){ */ static void asyncEnable(int enable){ if( enable && xOrigOpenReadWrite==0 ){ + assert(sqlite3Os.xOpenReadWrite); sqlite3HashInit(&async.aLock, SQLITE_HASH_BINARY, 1); - xOrigOpenReadWrite = sqlite3Os.xOpenReadWrite; xOrigOpenReadOnly = sqlite3Os.xOpenReadOnly; xOrigOpenExclusive = sqlite3Os.xOpenExclusive; @@ -817,10 +825,11 @@ static void asyncEnable(int enable){ sqlite3Os.xDelete = asyncDelete; sqlite3Os.xFileExists = asyncFileExists; sqlite3Os.xSyncDirectory = asyncSyncDirectory; + assert(sqlite3Os.xOpenReadWrite); } if( !enable && xOrigOpenReadWrite!=0 ){ + assert(sqlite3Os.xOpenReadWrite); sqlite3HashClear(&async.aLock); - sqlite3Os.xOpenReadWrite = xOrigOpenReadWrite; sqlite3Os.xOpenReadOnly = xOrigOpenReadOnly; sqlite3Os.xOpenExclusive = xOrigOpenExclusive; @@ -834,6 +843,7 @@ static void asyncEnable(int enable){ xOrigDelete = 0; xOrigFileExists = 0; xOrigSyncDirectory = 0; + assert(sqlite3Os.xOpenReadWrite); } } @@ -947,8 +957,8 @@ static void *asyncWriterThread(void *NotUsed){ case ASYNC_CLOSE: TRACE(("CLOSE %s\n", p->pFile->zName)); - sqlite3OsClose(&p->pFile->pBaseRead); sqlite3OsClose(&p->pFile->pBaseWrite); + sqlite3OsClose(&p->pFile->pBaseRead); sqlite3OsFree(p->pFile); break; @@ -979,12 +989,13 @@ static void *asyncWriterThread(void *NotUsed){ int delFlag = ((p->iOffset)?1:0); OsFile *pBase = 0; TRACE(("OPEN %s delFlag=%d\n", p->zBuf, delFlag)); + assert(pFile->pBaseRead==0 && pFile->pBaseWrite==0); rc = xOrigOpenExclusive(p->zBuf, &pBase, delFlag); assert( holdingMutex==0 ); pthread_mutex_lock(&async.queueMutex); holdingMutex = 1; if( rc==SQLITE_OK ){ - pFile->pBaseRead = pBase; + pFile->pBaseWrite = pBase; } break; } @@ -1173,6 +1184,7 @@ static int testAsyncWait( Tcl_AppendResult(interp, "would block forever", (char*)0); return TCL_ERROR; } + while( cnt-- && !pthread_mutex_trylock(&async.writerMutex) ){ pthread_mutex_unlock(&async.writerMutex); sched_yield(); diff --git a/src/util.c b/src/util.c index 0f2bc94aa3..13a084e839 100644 --- a/src/util.c +++ b/src/util.c @@ -14,7 +14,7 @@ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.184 2006/02/06 21:22:31 drh Exp $ +** $Id: util.c,v 1.185 2006/02/14 10:48:39 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -443,6 +443,7 @@ int sqlite3OutstandingMallocs(Tcl_Interp *interp){ ** This is the test layer's wrapper around sqlite3OsMalloc(). */ static void * OSMALLOC(int n){ + sqlite3OsEnterMutex(); #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT sqlite3_nMaxAlloc = MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc); @@ -455,8 +456,10 @@ static void * OSMALLOC(int n){ sqlite3_nMalloc++; applyGuards(p); linkAlloc(p); + sqlite3OsLeaveMutex(); return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); } + sqlite3OsLeaveMutex(); return 0; } @@ -473,12 +476,14 @@ static int OSSIZEOF(void *p){ ** pointer to the space allocated for the application to use. */ static void OSFREE(void *pFree){ + sqlite3OsEnterMutex(); u32 *p = (u32 *)getOsPointer(pFree); /* p points to Os level allocation */ checkGuards(p); unlinkAlloc(p); memset(pFree, 0x55, OSSIZEOF(pFree)); sqlite3OsFree(p); sqlite3_nFree++; + sqlite3OsLeaveMutex(); } /* @@ -578,14 +583,14 @@ static void updateMemoryUsedCount(int n){ ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory ** by calling sqlite3_release_memory(). */ -void *sqlite3MallocRaw(int n){ +void *sqlite3MallocRaw(int n, int doMemManage){ void *p = 0; - if( n>0 && !sqlite3MallocFailed() && enforceSoftLimit(n) ){ + if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){ while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ); if( !p ){ sqlite3FailedMalloc(); OSMALLOC_FAILED(); - }else{ + }else if( doMemManage ){ updateMemoryUsedCount(OSSIZEOF(p)); } } @@ -603,7 +608,7 @@ void *sqlite3Realloc(void *p, int n){ } if( !p ){ - return sqlite3Malloc(n); + return sqlite3Malloc(n, 1); }else{ void *np = 0; #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT @@ -648,8 +653,8 @@ void *sqlite3MallocX(int n){ ** These two are implemented as wrappers around sqlite3MallocRaw(), ** sqlite3Realloc() and sqlite3Free(). */ -void *sqlite3Malloc(int n){ - void *p = sqlite3MallocRaw(n); +void *sqlite3Malloc(int n, int doMemManage){ + void *p = sqlite3MallocRaw(n, doMemManage); if( p ){ memset(p, 0, n); } @@ -663,6 +668,33 @@ void sqlite3ReallocOrFree(void **pp, int n){ *pp = p; } +/* +** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those +** rare scenarios where sqlite may allocate memory in one thread and free +** it in another. They are exactly the same as sqlite3Malloc() and +** sqlite3Free() except that: +** +** * The allocated memory is not included in any calculations with +** respect to the soft-heap-limit, and +** +** * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(), +** not sqlite3Free(). Calling sqlite3Free() on memory obtained from +** ThreadSafeMalloc() will cause an error somewhere down the line. +*/ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +void *sqlite3ThreadSafeMalloc(int n){ + ENTER_MALLOC; + return sqlite3Malloc(n, 0); +} +void sqlite3ThreadSafeFree(void *p){ + ENTER_MALLOC; + if( p ){ + OSFREE(p); + } +} +#endif + + /* ** Return the number of bytes allocated at location p. p must be either ** a NULL pointer (in which case 0 is returned) or a pointer returned by @@ -689,14 +721,14 @@ int sqlite3AllocSize(void *p){ char *sqlite3StrDup(const char *z){ char *zNew; if( z==0 ) return 0; - zNew = sqlite3MallocRaw(strlen(z)+1); + zNew = sqlite3MallocRaw(strlen(z)+1, 1); if( zNew ) strcpy(zNew, z); return zNew; } char *sqlite3StrNDup(const char *z, int n){ char *zNew; if( z==0 ) return 0; - zNew = sqlite3MallocRaw(n+1); + zNew = sqlite3MallocRaw(n+1, 1); if( zNew ){ memcpy(zNew, z, n); zNew[n] = 0; diff --git a/test/async.test b/test/async.test index d71878c2dd..f87d395e4e 100644 --- a/test/async.test +++ b/test/async.test @@ -6,7 +6,7 @@ #*********************************************************************** # This file runs all tests. # -# $Id: async.test,v 1.5 2006/02/13 18:42:21 drh Exp $ +# $Id: async.test,v 1.6 2006/02/14 10:48:40 danielk1977 Exp $ if {[catch {sqlite3async_enable}]} { @@ -31,7 +31,7 @@ set INCLUDE { insert3.test trans.test } -#set INCLUDE [lrange $INCLUDE 0 0] +# set INCLUDE {select4.test} # Enable asynchronous IO. sqlite3async_enable 1 @@ -51,11 +51,13 @@ foreach testfile [lsort -dictionary [glob $testdir/*.test]] { catch {db close} } - -set sqlite_open_file_count 0 -really_finish_test -sqlite3async_halt now +# Flush the write-queue and disable asynchronous IO. This should ensure +# all allocated memory is cleaned up. +sqlite3async_halt idle +sqlite3async_start sqlite3async_wait sqlite3async_enable 0 + +really_finish_test rename really_do_test do_test rename really_finish_test finish_test diff --git a/test/quick.test b/test/quick.test index b704aa9f68..5d03b6f157 100644 --- a/test/quick.test +++ b/test/quick.test @@ -6,7 +6,7 @@ #*********************************************************************** # This file runs all tests. # -# $Id: quick.test,v 1.42 2006/01/20 10:55:05 danielk1977 Exp $ +# $Id: quick.test,v 1.43 2006/02/14 10:48:40 danielk1977 Exp $ proc lshift {lvar} { upvar $lvar l @@ -35,6 +35,7 @@ set ISQUICK 1 set EXCLUDE { all.test async.test + async2.test btree2.test btree3.test btree4.test