From: danielk1977 Date: Wed, 18 Jan 2006 15:25:17 +0000 (+0000) Subject: Change sqlite3MallocClearFailed() calls to sqlite3ApiExit(), a better API. (CVS 2970) X-Git-Tag: version-3.6.10~3197 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54f0198e47ef06ee7a4326ccc51d6a21ce8914d2;p=thirdparty%2Fsqlite.git Change sqlite3MallocClearFailed() calls to sqlite3ApiExit(), a better API. (CVS 2970) FossilOrigin-Name: e0b022e5b2bfd272b4e25cd7a7b472206a118bbe --- diff --git a/manifest b/manifest index d7edeadf21..fbab084397 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Recursive\smutexes\sin\sos_win.c.\s(CVS\s2969) -D 2006-01-18T14:20:17 +C Change\ssqlite3MallocClearFailed()\scalls\sto\ssqlite3ApiExit(),\sa\sbetter\sAPI.\s(CVS\s2970) +D 2006-01-18T15:25:17 F Makefile.in ab3ffd8d469cef4477257169b82810030a6bb967 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -34,11 +34,11 @@ F src/alter.c f1b66e9a6d606450d6e8fc23e2a96cd84666fdd3 F src/analyze.c 7d2b7ab9a9c2fd6e55700f69064dfdd3e36d7a8a F src/attach.c a447b4672a5125abc4ee27df10621abd84d18301 F src/auth.c 9ae84d2d94eb96195e04515715e08e85963e96c2 -F src/btree.c fcf6aec3b6a6df8130213a45f7b543e0a025be9d +F src/btree.c cff3ab167ad25b394d8c8dcac783bf3db5ae7e31 F src/btree.h 5663c4f43e8521546ccebc8fc95acb013b8f3184 F src/build.c 539b5c6e21aa633e480ff97706f331baef1a859a F src/callback.c ba3e6cc7a6beb562e7a66f92e26fabcb21aab1e2 -F src/complete.c df1681cef40dec33a286006981845f87b194e7a4 +F src/complete.c 7d1a44be8f37de125fcafd3d3a018690b3799675 F src/date.c 2f148728b53675266cd9e1a873fd702929d3f4ee F src/delete.c c7bd5708a629585e073ce34cf3b1fcb52c2fef38 F src/experimental.c 50c1e3b34f752f4ac10c36f287db095c2b61766d @@ -47,30 +47,30 @@ F src/func.c 96b26601c092b7b43a13e440e3f988b32a385f6a F src/hash.c 8747cf51d12de46512880dfcf1b68b4e24072863 F src/hash.h 1b0c445e1c89ff2aaad9b4605ba61375af001e84 F src/insert.c a5595cf8d1d8ba087b676a63f1f7277ea44b5ac1 -F src/legacy.c 9bf7ee1b63c99aac6669533986a5240b16101458 -F src/main.c fd9cb79c9fb6c61a67ad8be19a782372b0c4f48d +F src/legacy.c 0fd27db5e07a46853b9876baaa370b40e75a9b40 +F src/main.c bf810309d9760654f58c77688524d2a556b2827c F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217 F src/os.c 1d1a61cdf150e9f9520a3bc787c8465148ea2e78 F src/os.h 9debc3d3ca4cdafde222a0ea74a4c8415aef4f22 F src/os_common.h 95b29ca6f3e6636cb33c9219b3f91a96fa7224b1 F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3 -F src/os_unix.c 7c085f807922309006f00b5fd8963865b92ff939 +F src/os_unix.c fb7ae89d8672102b60e855dae5834f9d1ef7c99c F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e F src/os_win.c e1c0600129f2468c8a852ae61f7b7bacf72aa7dc F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b -F src/pager.c e84713f7196a81103cc8e2b55cebbaa1723c4926 +F src/pager.c 62c0f6b4c4856a760d4a7430c88492a7b626ac18 F src/pager.h e0acb095b3ad0bca48f2ab00c87346665643f64f F src/parse.y 83df51fea35f68f7e07384d75dce83d1ed30434c F src/pragma.c 4496cc77dc35824e1c978c3d1413b8a5a4c777d3 -F src/prepare.c 84c3cbca337a49133554108d748864c5e13aef34 +F src/prepare.c 1294009215173899f3d64dc16f1dc50edaedbb6a F src/printf.c f47a2f4b5387cd2ebb12e9117a1a5d6bd9a2b812 F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261 F src/select.c 28d449c7762dd580aaba49a9c6c16e93ca951e49 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/shell.c 66b073375efbdee19045e7e0cd38b85f9aff71da F src/sqlite.h.in 492580f7e3ff71eb43193eb7bb98e2d549889ce3 -F src/sqliteInt.h d7b20e0a9453db123809d0bcc46d6ae0e4cf6bca +F src/sqliteInt.h b23caafa3ce3d71f4a0f0e3aa3fb6779eda3a1b7 F src/table.c 486dcfce532685b53b5a2b5da8bba0ded6fb2316 F src/tclsqlite.c d650bea0248fc0a310ddc2cb94273a3a5021fddf F src/test1.c b076d9c42b547ec0891b67722f00681b21756d91 @@ -85,12 +85,12 @@ F src/tokenize.c 8b694d42b48f22c2037da7e1e716201aaaaa0f3a F src/trigger.c 694b247476d2fc0dce003af564f79e8752fc1158 F src/update.c 261d75c702c2852d1a64274d7c414485e6f2d177 F src/utf.c 5ab8ca05d4e9ec81174b010f01ab12a232f0087d -F src/util.c 25e867296bce168095c87460ab5eec150015cb7d +F src/util.c 28f399a6e3b4b55caf635f1941ee2391072d1a78 F src/vacuum.c 21a3c7f6f7be86bb1182fbc3df416ad702435b9e F src/vdbe.c 3db9bfc86e71dc0758524fb82cfcfd4c30e92716 F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13 F src/vdbeInt.h 5451cf71f229e366ac543607c0a17f36e5737ea9 -F src/vdbeapi.c 2c3068bc5d17f7bd179b346a4a920199ea451694 +F src/vdbeapi.c cbc552ea2d1acca790cfee88bb7e670cf34a054b F src/vdbeaux.c 723e0ec773aeec53e9448b2ac5b136677e64ece0 F src/vdbefifo.c 9efb94c8c3f4c979ebd0028219483f88e57584f5 F src/vdbemem.c dd08a0eea4868ac4a2d91fdec32424308b1db772 @@ -341,7 +341,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 -P 8830bbbac8e0c9243956aac42dc9f86a0bd1fa07 -R 7ad1313dca6d71746c68d32b98aa5742 -U drh -Z 25414a8af7b032353ad4c225fe562fb4 +P dd3e07cae4d0cbd4f8977e1dd11e0103e0e45b75 +R baef608325fb18b2b8e10ab8f84cb339 +U danielk1977 +Z 2a85eb76c461ae700bdd874fef6ec61c diff --git a/manifest.uuid b/manifest.uuid index 2776453eb9..66b878cc24 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -dd3e07cae4d0cbd4f8977e1dd11e0103e0e45b75 \ No newline at end of file +e0b022e5b2bfd272b4e25cd7a7b472206a118bbe \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 8674e9c6ed..7a46d26ec3 100644 --- a/src/btree.c +++ b/src/btree.c @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.299 2006/01/16 15:14:27 danielk1977 Exp $ +** $Id: btree.c,v 1.300 2006/01/18 15:25:17 danielk1977 Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to @@ -1662,7 +1662,11 @@ int sqlite3BtreeOpen( sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize); #ifndef SQLITE_OMIT_SHARED_CACHE - /* Add the new btree to the linked list starting at ThreadData.pBtree */ + /* Add the new btree to the linked list starting at ThreadData.pBtree. + ** There is no chance that a malloc() may fail inside of the + ** sqlite3ThreadData() call, as the ThreadData structure must have already + ** been allocated for pTsdro->useSharedData to be non-zero. + */ if( pTsdro->useSharedData && zFilename && !isMemdb ){ pBt->pNext = pTsdro->pBtree; sqlite3ThreadData()->pBtree = pBt; @@ -1712,14 +1716,19 @@ int sqlite3BtreeClose(Btree *p){ return SQLITE_OK; } - /* Remove the shared-btree from the thread wide list */ - pTsd = sqlite3ThreadData(); + /* Remove the shared-btree from the thread wide list. Call + ** ThreadDataReadOnly() and then cast away the const property of the + ** pointer to avoid allocating thread data if it is not really required. + */ + pTsd = (ThreadData *)sqlite3ThreadDataReadOnly(); if( pTsd->pBtree==pBt ){ + assert( pTsd==sqlite3ThreadData() ); pTsd->pBtree = pBt->pNext; }else{ BtShared *pPrev; for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext); if( pPrev ){ + assert( pTsd==sqlite3ThreadData() ); pPrev->pNext = pBt->pNext; } } diff --git a/src/complete.c b/src/complete.c index 61a87e57c3..536ffadbc2 100644 --- a/src/complete.c +++ b/src/complete.c @@ -16,7 +16,7 @@ ** separating it out, the code will be automatically omitted from ** static links that do not use it. ** -** $Id: complete.c,v 1.2 2005/12/12 06:53:04 danielk1977 Exp $ +** $Id: complete.c,v 1.3 2006/01/18 15:25:17 danielk1977 Exp $ */ #include "sqliteInt.h" #ifndef SQLITE_OMIT_COMPLETE @@ -255,12 +255,9 @@ int sqlite3_complete16(const void *zSql){ zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); if( zSql8 ){ rc = sqlite3_complete(zSql8); - }else if( zSql ){ - rc = SQLITE_NOMEM; - sqlite3MallocClearFailed(); } sqlite3ValueFree(pVal); - return rc; + return sqlite3ApiExit(0, rc); } #endif /* SQLITE_OMIT_UTF16 */ #endif /* SQLITE_OMIT_COMPLETE */ diff --git a/src/legacy.c b/src/legacy.c index b149f5b29f..98f37a922a 100644 --- a/src/legacy.c +++ b/src/legacy.c @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: legacy.c,v 1.11 2006/01/11 21:41:22 drh Exp $ +** $Id: legacy.c,v 1.12 2006/01/18 15:25:17 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -121,11 +121,7 @@ exec_out: if( pStmt ) sqlite3_finalize(pStmt); if( azCols ) sqliteFree(azCols); - if( sqlite3ThreadDataReadOnly()->mallocFailed ){ - rc = SQLITE_NOMEM; - sqlite3MallocClearFailed(); - } - + rc = sqlite3ApiExit(0, rc); if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){ *pzErrMsg = malloc(1+strlen(sqlite3_errmsg(db))); if( *pzErrMsg ){ diff --git a/src/main.c b/src/main.c index 77fe0710a2..c2822a7e53 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.326 2006/01/18 05:51:58 danielk1977 Exp $ +** $Id: main.c,v 1.327 2006/01/18 15:25:17 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -485,12 +485,7 @@ int sqlite3_create_function( assert( !sqlite3ThreadDataReadOnly()->mallocFailed ); rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); - if( sqlite3ThreadDataReadOnly()->mallocFailed ){ - sqlite3MallocClearFailed(); - rc = SQLITE_NOMEM; - sqlite3Error(db, SQLITE_NOMEM, 0); - } - return rc; + return sqlite3ApiExit(db, rc); } #ifndef SQLITE_OMIT_UTF16 @@ -512,12 +507,7 @@ int sqlite3_create_function16( rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); sqliteFree(zFunc8); - if( sqlite3ThreadDataReadOnly()->mallocFailed ){ - sqlite3MallocClearFailed(); - rc = SQLITE_NOMEM; - sqlite3Error(db, SQLITE_NOMEM, 0); - } - return rc; + return sqlite3ApiExit(db, rc); } #endif @@ -727,7 +717,7 @@ const void *sqlite3_errmsg16(sqlite3 *db){ SQLITE_UTF8, SQLITE_STATIC); z = sqlite3_value_text16(db->pErr); } - sqlite3MallocClearFailed(); + sqlite3ApiExit(0, 0); return z; } #endif /* SQLITE_OMIT_UTF16 */ @@ -835,10 +825,7 @@ static int openDatabase( createCollation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) || (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 ){ - /* sqlite3_create_collation() is an external API. So the mallocFailed flag - ** will have been cleared before returning. So set it explicitly here. - */ - sqlite3ThreadData()->mallocFailed = 1; + assert( sqlite3ThreadDataReadOnly()->mallocFailed ); db->magic = SQLITE_MAGIC_CLOSED; goto opendb_out; } @@ -895,8 +882,7 @@ opendb_out: db = 0; } *ppDb = db; - sqlite3MallocClearFailed(); - return rc; + return sqlite3ApiExit(0, rc); } /* @@ -918,7 +904,7 @@ int sqlite3_open16( sqlite3 **ppDb ){ char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ - int rc = SQLITE_NOMEM; + int rc = SQLITE_OK; sqlite3_value *pVal; assert( zFilename ); @@ -936,13 +922,10 @@ int sqlite3_open16( *ppDb = 0; } } - }else{ - assert( sqlite3ThreadDataReadOnly()->mallocFailed ); - sqlite3MallocClearFailed(); } sqlite3ValueFree(pVal); - return rc; + return sqlite3ApiExit(0, rc); } #endif /* SQLITE_OMIT_UTF16 */ @@ -997,12 +980,7 @@ int sqlite3_create_collation( int rc; assert( !sqlite3ThreadDataReadOnly()->mallocFailed ); rc = createCollation(db, zName, enc, pCtx, xCompare); - if( sqlite3ThreadDataReadOnly()->mallocFailed ){ - sqlite3MallocClearFailed(); - rc = SQLITE_NOMEM; - sqlite3Error(db, rc, 0); - } - return rc; + return sqlite3ApiExit(db, rc); } #ifndef SQLITE_OMIT_UTF16 @@ -1024,12 +1002,7 @@ int sqlite3_create_collation16( rc = createCollation(db, zName8, enc, pCtx, xCompare); sqliteFree(zName8); } - if( sqlite3ThreadDataReadOnly()->mallocFailed ){ - sqlite3MallocClearFailed(); - rc = SQLITE_NOMEM; - sqlite3Error(db, rc, 0); - } - return rc; + return sqlite3ApiExit(db, rc); } #endif /* SQLITE_OMIT_UTF16 */ @@ -1115,6 +1088,9 @@ int sqlite3Corrupt(void){ */ int sqlite3_enable_shared_cache(int enable){ ThreadData *pTd = sqlite3ThreadData(); + if( !pTd ){ + return SQLITE_NOMEM; + } /* It is only legal to call sqlite3_enable_shared_cache() when there ** are no currently open b-trees that were opened by the calling thread. @@ -1138,6 +1114,8 @@ int sqlite3_enable_shared_cache(int enable){ */ void sqlite3_thread_cleanup(void){ ThreadData *pTd = sqlite3ThreadData(); - memset(pTd, 0, sizeof(*pTd)); + if( pTd ){ + memset(pTd, 0, sizeof(*pTd)); + } sqlite3ReleaseThreadData(); } diff --git a/src/os_unix.c b/src/os_unix.c index 35d29ad3cf..5257bfd5e3 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -1739,6 +1739,19 @@ int sqlite3_tsd_count = 0; # define TSD_COUNTER(N) /* no-op */ #endif +#if 0 && defined(SQLITE_MEMDEBUG) +static void *mallocThreadData(size_t nBytes){ + if( sqlite3_iMallocFail>=0 ){ + sqlite3_iMallocFail--; + if( sqlite3_iMallocFail==0 ){ + return 0; + } + } + return sqlite3OsMalloc(nBytes); +} +#else + #define mallocThreadData(x) sqlite3OsMalloc(x) +#endif /* ** If called with allocateFlag>0, then return a pointer to thread @@ -1778,7 +1791,7 @@ ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){ pTsd = pthread_getspecific(key); if( allocateFlag>0 ){ if( pTsd==0 ){ - pTsd = sqlite3OsMalloc(sizeof(zeroData)); + pTsd = mallocThreadData(sizeof(zeroData)); if( pTsd ){ *pTsd = zeroData; pthread_setspecific(key, pTsd); @@ -1797,7 +1810,7 @@ ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){ static ThreadData *pTsd = 0; if( allocateFlag>0 ){ if( pTsd==0 ){ - pTsd = sqlite3OsMalloc( sizeof(zeroData) ); + pTsd = mallocThreadData( sizeof(zeroData) ); if( pTsd ){ *pTsd = zeroData; TSD_COUNTER(+1); diff --git a/src/pager.c b/src/pager.c index 597e6e0048..0425a4419e 100644 --- a/src/pager.c +++ b/src/pager.c @@ -18,7 +18,7 @@ ** file simultaneously, or one process from reading the database while ** another is writing. ** -** @(#) $Id: pager.c,v 1.241 2006/01/16 15:32:23 danielk1977 Exp $ +** @(#) $Id: pager.c,v 1.242 2006/01/18 15:25:17 danielk1977 Exp $ */ #ifndef SQLITE_OMIT_DISKIO #include "sqliteInt.h" @@ -1577,7 +1577,15 @@ int sqlite3pager_open( int noReadlock = (flags & PAGER_NO_READLOCK)!=0; char zTemp[SQLITE_TEMPNAME_SIZE]; #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to + ** malloc() must have already been made by this thread before it gets + ** to this point. This means the ThreadData must have been allocated already + ** so that ThreadData.nAlloc can be set. It would be nice to assert + ** that ThreadData.nAlloc is non-zero, but alas this breaks test cases + ** written to invoke the pager directly. + */ ThreadData *pTsd = sqlite3ThreadData(); + assert( pTsd ); #endif /* If malloc() has already failed return SQLITE_NOMEM. Before even @@ -1985,7 +1993,13 @@ int sqlite3pager_truncate(Pager *pPager, Pgno nPage){ int sqlite3pager_close(Pager *pPager){ PgHdr *pPg, *pNext; #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to + ** malloc() must have already been made by this thread before it gets + ** to this point. This means the ThreadData must have been allocated already + ** so that ThreadData.nAlloc can be set. + */ ThreadData *pTsd = sqlite3ThreadData(); + assert( pTsd && pTsd->nAlloc ); #endif switch( pPager->state ){ diff --git a/src/prepare.c b/src/prepare.c index 26dc155b11..cd82f1a319 100644 --- a/src/prepare.c +++ b/src/prepare.c @@ -13,7 +13,7 @@ ** interface, and routines that contribute to loading the database schema ** from disk. ** -** $Id: prepare.c,v 1.24 2006/01/17 13:21:40 danielk1977 Exp $ +** $Id: prepare.c,v 1.25 2006/01/18 15:25:18 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -570,16 +570,8 @@ int sqlite3_prepare( sqlite3Error(db, rc, 0); } - /* We must check for malloc failure last of all, in case malloc() failed - ** inside of the sqlite3Error() call above or something. - */ - if( sParse.pTsd->mallocFailed ){ - rc = SQLITE_NOMEM; - sqlite3Error(db, rc, 0); - } - sParse.pTsd->nRef--; - sqlite3MallocClearFailed(); + rc = sqlite3ApiExit(db, rc); sqlite3ReleaseThreadData(); return rc; } @@ -599,20 +591,17 @@ int sqlite3_prepare16( ** encoded string to UTF-8, then invoking sqlite3_prepare(). The ** tricky bit is figuring out the pointer to return in *pzTail. */ - char *zSql8 = 0; + char *zSql8; const char *zTail8 = 0; - int rc; + int rc = SQLITE_OK; if( sqlite3SafetyCheck(db) ){ return SQLITE_MISUSE; } zSql8 = sqlite3utf16to8(zSql, nBytes); - if( !zSql8 ){ - sqlite3MallocClearFailed(); - sqlite3Error(db, SQLITE_NOMEM, 0); - return SQLITE_NOMEM; + if( zSql8 ){ + rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8); } - rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8); if( zTail8 && pzTail ){ /* If sqlite3_prepare returns a tail pointer, we calculate the @@ -624,6 +613,6 @@ int sqlite3_prepare16( *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed); } sqliteFree(zSql8); - return rc; + return sqlite3ApiExit(db, rc); } #endif /* SQLITE_OMIT_UTF16 */ diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 2c8ea08441..4bb2c14e5e 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.472 2006/01/17 16:10:14 danielk1977 Exp $ +** @(#) $Id: sqliteInt.h,v 1.473 2006/01/18 15:25:18 danielk1977 Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -1765,6 +1765,7 @@ KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *); int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*)); +int sqlite3ApiExit(sqlite3 *db, int); #ifndef SQLITE_OMIT_SHARED_CACHE void sqlite3TableLock(Parse *, int, int, u8, const char *); @@ -1772,7 +1773,6 @@ int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, #define sqlite3TableLock(v,w,x,y,z) #endif -void sqlite3MallocClearFailed(); #ifdef SQLITE_MEMDEBUG void sqlite3MallocDisallow(); void sqlite3MallocAllow(); diff --git a/src/util.c b/src/util.c index ab7e1420c9..36cf5dc8da 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.173 2006/01/17 15:36:32 danielk1977 Exp $ +** $Id: util.c,v 1.174 2006/01/18 15:25:18 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -1336,15 +1336,28 @@ void sqlite3ReleaseThreadData(){ } /* -** Clear the "mallocFailed" flag. This should be invoked before exiting any -** entry points that may have called sqliteMalloc(). +** This function must be called before exiting any API function (i.e. +** returning control to the user) that has called sqlite3Malloc or +** sqlite3Realloc. +** +** 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. */ -void sqlite3MallocClearFailed(){ +int sqlite3ApiExit(sqlite3* db, int rc){ ThreadData *pTd = sqlite3OsThreadSpecificData(0); if( pTd && pTd->mallocFailed ){ pTd->mallocFailed = 0; - sqlite3OsThreadSpecificData(0); + if( db ){ + sqlite3Error(db, SQLITE_NOMEM, 0); + } + return SQLITE_NOMEM; } + return rc; } #ifdef SQLITE_MEMDEBUG diff --git a/src/vdbeapi.c b/src/vdbeapi.c index 5d5ec27f2e..c1ae4806ac 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -242,8 +242,7 @@ int sqlite3_step(sqlite3_stmt *pStmt){ #endif sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); - sqlite3MallocClearFailed(); - return rc; + return sqlite3ApiExit(p->db, rc); } /* @@ -405,10 +404,8 @@ static void columnMallocFailure(sqlite3_stmt *pStmt) ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR ** and _finalize() will return NOMEM. */ - if( sqlite3ThreadDataReadOnly()->mallocFailed ){ - ((Vdbe *)pStmt)->rc = SQLITE_NOMEM; - sqlite3MallocClearFailed(); - } + Vdbe *p = (Vdbe *)pStmt; + p->rc = sqlite3ApiExit(0, p->rc); } /**************************** sqlite3_column_ ******************************* @@ -503,7 +500,7 @@ static const void *columnName( /* A malloc may have failed inside of the xFunc() call. If this is the case, ** clear the mallocFailed flag and return NULL. */ - sqlite3MallocClearFailed(); + sqlite3ApiExit(0, 0); return ret; } @@ -634,9 +631,9 @@ static int bindText( if( rc==SQLITE_OK && encoding!=0 ){ rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); } - sqlite3MallocClearFailed(); + sqlite3Error(((Vdbe *)pStmt)->db, rc, 0); - return rc; + return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc); }