diff options
author | drh <drh@noemail.net> | 2007-08-16 04:30:38 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2007-08-16 04:30:38 +0000 |
commit | 174357527a47e97f3d782c1327be6dfd48f7082f (patch) | |
tree | 846ccc36dd9fae23e6c33b52686b4f742ca7640d /src | |
parent | 0e6f1546b05fd04fb3b3e963df948259683deee4 (diff) | |
download | sqlite-174357527a47e97f3d782c1327be6dfd48f7082f.tar.gz sqlite-174357527a47e97f3d782c1327be6dfd48f7082f.zip |
Half-way through a major refactoring of the memory allocation.
I have not even attempted to compile so I am certain there are
countless errors. (CVS 4231)
FossilOrigin-Name: deb7ecd65f7b83eaf0ba610eeef3b0ede61db1c3
Diffstat (limited to 'src')
-rw-r--r-- | src/alter.c | 44 | ||||
-rw-r--r-- | src/analyze.c | 12 | ||||
-rw-r--r-- | src/attach.c | 31 | ||||
-rw-r--r-- | src/btree.c | 86 | ||||
-rw-r--r-- | src/build.c | 205 | ||||
-rw-r--r-- | src/callback.c | 22 | ||||
-rw-r--r-- | src/date.c | 6 | ||||
-rw-r--r-- | src/delete.c | 8 | ||||
-rw-r--r-- | src/expr.c | 217 | ||||
-rw-r--r-- | src/func.c | 20 | ||||
-rw-r--r-- | src/hash.c | 30 | ||||
-rw-r--r-- | src/hash.h | 6 | ||||
-rw-r--r-- | src/insert.c | 14 | ||||
-rw-r--r-- | src/legacy.c | 8 | ||||
-rw-r--r-- | src/loadext.c | 10 | ||||
-rw-r--r-- | src/main.c | 54 | ||||
-rw-r--r-- | src/malloc.c | 699 | ||||
-rw-r--r-- | src/os_unix.c | 50 | ||||
-rw-r--r-- | src/pager.c | 91 | ||||
-rw-r--r-- | src/parse.y | 198 | ||||
-rw-r--r-- | src/pragma.c | 14 | ||||
-rw-r--r-- | src/prepare.c | 28 | ||||
-rw-r--r-- | src/printf.c | 58 | ||||
-rw-r--r-- | src/select.c | 192 | ||||
-rw-r--r-- | src/sqliteInt.h | 90 | ||||
-rw-r--r-- | src/test1.c | 150 | ||||
-rw-r--r-- | src/test3.c | 14 | ||||
-rw-r--r-- | src/test4.c | 6 | ||||
-rw-r--r-- | src/test5.c | 6 | ||||
-rw-r--r-- | src/test6.c | 14 | ||||
-rw-r--r-- | src/test7.c | 6 | ||||
-rw-r--r-- | src/test8.c | 46 | ||||
-rw-r--r-- | src/test_async.c | 18 | ||||
-rw-r--r-- | src/test_hexio.c | 14 | ||||
-rw-r--r-- | src/test_schema.c | 14 | ||||
-rw-r--r-- | src/test_tclvar.c | 10 | ||||
-rw-r--r-- | src/tokenize.c | 20 | ||||
-rw-r--r-- | src/trigger.c | 98 | ||||
-rw-r--r-- | src/update.c | 31 | ||||
-rw-r--r-- | src/utf.c | 26 | ||||
-rw-r--r-- | src/util.c | 37 | ||||
-rw-r--r-- | src/vacuum.c | 4 | ||||
-rw-r--r-- | src/vdbe.c | 78 | ||||
-rw-r--r-- | src/vdbeInt.h | 2 | ||||
-rw-r--r-- | src/vdbeapi.c | 13 | ||||
-rw-r--r-- | src/vdbeaux.c | 94 | ||||
-rw-r--r-- | src/vdbeblob.c | 18 | ||||
-rw-r--r-- | src/vdbefifo.c | 6 | ||||
-rw-r--r-- | src/vdbemem.c | 101 | ||||
-rw-r--r-- | src/vtab.c | 64 | ||||
-rw-r--r-- | src/where.c | 64 |
51 files changed, 1248 insertions, 1899 deletions
diff --git a/src/alter.c b/src/alter.c index b8c767a63..88fe11bc6 100644 --- a/src/alter.c +++ b/src/alter.c @@ -12,7 +12,7 @@ ** This file contains C code routines that used to generate VDBE code ** that implements the ALTER TABLE command. ** -** $Id: alter.c,v 1.27 2007/06/27 17:09:24 danielk1977 Exp $ +** $Id: alter.c,v 1.28 2007/08/16 04:30:39 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -203,7 +203,7 @@ static char *whereTempTriggers(Parse *pParse, Table *pTab){ }else{ tmp = zWhere; zWhere = sqlite3MPrintf("%s OR name=%Q", zWhere, pTrig->name); - sqliteFree(tmp); + sqlite3_free(tmp); } } } @@ -281,7 +281,7 @@ void sqlite3AlterRenameTable( #endif int isVirtualRename = 0; /* True if this is a v-table with an xRename() */ - if( sqlite3MallocFailed() ) goto exit_rename_table; + if( db->mallocFailed ) goto exit_rename_table; assert( pSrc->nSrc==1 ); pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase); @@ -290,7 +290,7 @@ void sqlite3AlterRenameTable( zDb = db->aDb[iDb].zName; /* Get a NULL terminated version of the new table name. */ - zName = sqlite3NameFromToken(pName); + zName = sqlite3NameFromToken(db, pName); if( !zName ) goto exit_rename_table; /* Check that a table or index named 'zName' does not already exist @@ -404,7 +404,7 @@ void sqlite3AlterRenameTable( "sql = sqlite_rename_trigger(sql, %Q), " "tbl_name = %Q " "WHERE %s;", zName, zName, zWhere); - sqliteFree(zWhere); + sqlite3_free(zWhere); } #endif @@ -413,7 +413,7 @@ void sqlite3AlterRenameTable( exit_rename_table: sqlite3SrcListDelete(pSrc); - sqliteFree(zName); + sqlite3_free(zName); } @@ -434,17 +434,19 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ char *zCol; /* Null-terminated column definition */ Column *pCol; /* The new column */ Expr *pDflt; /* Default value for the new column */ + sqlite3 *db; /* The database connection; */ if( pParse->nErr ) return; pNew = pParse->pNewTable; assert( pNew ); - iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema); - zDb = pParse->db->aDb[iDb].zName; + db = pParse->db; + iDb = sqlite3SchemaToIndex(db, pNew->pSchema); + zDb = db->aDb[iDb].zName; zTab = pNew->zName; pCol = &pNew->aCol[pNew->nCol-1]; pDflt = pCol->pDflt; - pTab = sqlite3FindTable(pParse->db, zTab, zDb); + pTab = sqlite3FindTable(db, zTab, zDb); assert( pTab ); #ifndef SQLITE_OMIT_AUTHORIZATION @@ -486,7 +488,7 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ if( pDflt ){ sqlite3_value *pVal; if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){ - /* malloc() has failed */ + db->mallocFailed = 1; return; } if( !pVal ){ @@ -497,7 +499,7 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ } /* Modify the CREATE TABLE statement. */ - zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n); + zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); if( zCol ){ char *zEnd = &zCol[pColDef->n-1]; while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){ @@ -510,7 +512,7 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, zTab ); - sqliteFree(zCol); + sqlite3_free(zCol); } /* If the default value of the new column is NULL, then set the file @@ -545,10 +547,11 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ int iDb; int i; int nAlloc; + sqlite3 *db = pParse->db; /* Look up the table being altered. */ assert( pParse->pNewTable==0 ); - if( sqlite3MallocFailed() ) goto exit_begin_add_column; + if( db->mallocFailed ) goto exit_begin_add_column; pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase); if( !pTab ) goto exit_begin_add_column; @@ -566,12 +569,12 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ } assert( pTab->addColOffset>0 ); - iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); /* Put a copy of the Table struct in Parse.pNewTable for the ** sqlite3AddColumn() function and friends to modify. */ - pNew = (Table *)sqliteMalloc(sizeof(Table)); + pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); if( !pNew ) goto exit_begin_add_column; pParse->pNewTable = pNew; pNew->nRef = 1; @@ -579,20 +582,21 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ assert( pNew->nCol>0 ); nAlloc = (((pNew->nCol-1)/8)*8)+8; assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); - pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc); - pNew->zName = sqliteStrDup(pTab->zName); + pNew->aCol = (Column*)sqlite3_malloc(sizeof(Column)*nAlloc); + pNew->zName = sqlite3DbStrDup(db, pTab->zName); if( !pNew->aCol || !pNew->zName ){ + db->mallocFailed = 1; goto exit_begin_add_column; } memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); for(i=0; i<pNew->nCol; i++){ Column *pCol = &pNew->aCol[i]; - pCol->zName = sqliteStrDup(pCol->zName); + pCol->zName = sqlite3DbStrDup(db, pCol->zName); pCol->zColl = 0; pCol->zType = 0; pCol->pDflt = 0; } - pNew->pSchema = pParse->db->aDb[iDb].pSchema; + pNew->pSchema = db->aDb[iDb].pSchema; pNew->addColOffset = pTab->addColOffset; pNew->nRef = 1; @@ -600,7 +604,7 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ sqlite3BeginWriteOperation(pParse, 0, iDb); v = sqlite3GetVdbe(pParse); if( !v ) goto exit_begin_add_column; - sqlite3ChangeCookie(pParse->db, v, iDb); + sqlite3ChangeCookie(db, v, iDb); exit_begin_add_column: sqlite3SrcListDelete(pSrc); diff --git a/src/analyze.c b/src/analyze.c index b835aebd9..7ce218d66 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code associated with the ANALYZE command. ** -** @(#) $Id: analyze.c,v 1.19 2007/06/20 13:37:31 drh Exp $ +** @(#) $Id: analyze.c,v 1.20 2007/08/16 04:30:39 drh Exp $ */ #ifndef SQLITE_OMIT_ANALYZE #include "sqliteInt.h" @@ -304,9 +304,9 @@ void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ if( iDb>=0 ){ analyzeDatabase(pParse, iDb); }else{ - z = sqlite3NameFromToken(pName1); + z = sqlite3NameFromToken(db, pName1); pTab = sqlite3LocateTable(pParse, z, 0); - sqliteFree(z); + sqlite3_free(z); if( pTab ){ analyzeTable(pParse, pTab); } @@ -316,10 +316,10 @@ void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); if( iDb>=0 ){ zDb = db->aDb[iDb].zName; - z = sqlite3NameFromToken(pTableName); + z = sqlite3NameFromToken(db, pTableName); if( z ){ pTab = sqlite3LocateTable(pParse, z, zDb); - sqliteFree(z); + sqlite3_free(z); if( pTab ){ analyzeTable(pParse, pTab); } @@ -402,7 +402,7 @@ int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ sqlite3SafetyOff(db); rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); sqlite3SafetyOn(db); - sqliteFree(zSql); + sqlite3_free(zSql); return rc; } diff --git a/src/attach.c b/src/attach.c index e3645d939..82377fbb3 100644 --- a/src/attach.c +++ b/src/attach.c @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code used to implement the ATTACH and DETACH commands. ** -** $Id: attach.c,v 1.60 2007/05/09 20:31:30 drh Exp $ +** $Id: attach.c,v 1.61 2007/08/16 04:30:39 drh Exp $ */ #include "sqliteInt.h" @@ -102,7 +102,8 @@ static void attachFunc( for(i=0; i<db->nDb; i++){ char *z = db->aDb[i].zName; if( z && zName && sqlite3StrICmp(z, zName)==0 ){ - sqlite3_snprintf(sizeof(zErr), zErr, "database %s is already in use", zName); + sqlite3_snprintf(sizeof(zErr), zErr, + "database %s is already in use", zName); goto attach_error; } } @@ -111,14 +112,16 @@ static void attachFunc( ** hash tables. */ if( db->aDb==db->aDbStatic ){ - aNew = sqliteMalloc( sizeof(db->aDb[0])*3 ); + aNew = sqlite3_malloc( sizeof(db->aDb[0])*3 ); if( aNew==0 ){ + db->mallocFailed = 1; return; } memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); }else{ - aNew = sqliteRealloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); + aNew = sqlite3_realloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); if( aNew==0 ){ + db->mallocFailed = 1; return; } } @@ -132,7 +135,7 @@ static void attachFunc( */ rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE, &aNew->pBt); if( rc==SQLITE_OK ){ - aNew->pSchema = sqlite3SchemaGet(aNew->pBt); + aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt); if( !aNew->pSchema ){ rc = SQLITE_NOMEM; }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ @@ -142,7 +145,7 @@ static void attachFunc( } sqlite3PagerLockingMode(sqlite3BtreePager(aNew->pBt), db->dfltLockMode); } - aNew->zName = sqliteStrDup(zName); + aNew->zName = sqlite3DbStrDup(db, zName); aNew->safety_level = 3; #if SQLITE_HAS_CODEC @@ -155,7 +158,7 @@ static void attachFunc( switch( t ){ case SQLITE_INTEGER: case SQLITE_FLOAT: - zErrDyn = sqliteStrDup("Invalid key value"); + zErrDyn = sqlite3DbStrDup(db, "Invalid key value"); rc = SQLITE_ERROR; break; @@ -196,7 +199,7 @@ static void attachFunc( sqlite3ResetInternalSchema(db, 0); db->nDb = iDb; if( rc==SQLITE_NOMEM ){ - sqlite3FailedMalloc(); + db->mallocFailed = 1; sqlite3_snprintf(sizeof(zErr),zErr, "out of memory"); }else{ sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile); @@ -210,7 +213,7 @@ attach_error: /* Return an error if we get here */ if( zErrDyn ){ sqlite3_result_error(context, zErrDyn, -1); - sqliteFree(zErrDyn); + sqlite3_free(zErrDyn); }else{ zErr[sizeof(zErr)-1] = 0; sqlite3_result_error(context, zErr, -1); @@ -292,14 +295,14 @@ static void codeAttach( sqlite3* db = pParse->db; #ifndef SQLITE_OMIT_AUTHORIZATION - assert( sqlite3MallocFailed() || pAuthArg ); + assert( db->mallocFailed || pAuthArg ); if( pAuthArg ){ - char *zAuthArg = sqlite3NameFromToken(&pAuthArg->span); + char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span); if( !zAuthArg ){ goto attach_end; } rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); - sqliteFree(zAuthArg); + sqlite3_free(zAuthArg); if(rc!=SQLITE_OK ){ goto attach_end; } @@ -323,7 +326,7 @@ static void codeAttach( sqlite3ExprCode(pParse, pDbname); sqlite3ExprCode(pParse, pKey); - assert( v || sqlite3MallocFailed() ); + assert( v || db->mallocFailed ); if( v ){ sqlite3VdbeAddOp(v, OP_Function, 0, nFunc); pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0); @@ -424,7 +427,7 @@ int sqlite3FixSrcList( zDb = pFix->zDb; for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ if( pItem->zDatabase==0 ){ - pItem->zDatabase = sqliteStrDup(zDb); + pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb); }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){ sqlite3ErrorMsg(pFix->pParse, "%s %T cannot reference objects in database %s", diff --git a/src/btree.c b/src/btree.c index 1e3a43e47..31c64311e 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.396 2007/08/13 14:56:44 drh Exp $ +** $Id: btree.c,v 1.397 2007/08/16 04:30:39 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. @@ -143,7 +143,7 @@ static int lockTable(Btree *p, Pgno iTable, u8 eLock){ ** with table iTable, allocate one and link it into the list. */ if( !pLock ){ - pLock = (BtLock *)sqliteMalloc(sizeof(BtLock)); + pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); if( !pLock ){ return SQLITE_NOMEM; } @@ -182,7 +182,7 @@ static void unlockAllTables(Btree *p){ BtLock *pLock = *ppIter; if( pLock->pBtree==p ){ *ppIter = pLock->pNext; - sqliteFree(pLock); + sqlite3_free(pLock); }else{ ppIter = &pLock->pNext; } @@ -197,7 +197,7 @@ static void releasePage(MemPage *pPage); /* Forward reference */ ** Invalidate the overflow page-list cache for cursor pCur, if any. */ static void invalidateOverflowCache(BtCursor *pCur){ - sqliteFree(pCur->aOverflow); + sqlite3_free(pCur->aOverflow); pCur->aOverflow = 0; } @@ -235,13 +235,13 @@ static int saveCursorPosition(BtCursor *pCur){ ** data. */ if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ - void *pKey = sqliteMalloc(pCur->nKey); + void *pKey = sqlite3_malloc(pCur->nKey); if( pKey ){ rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); if( rc==SQLITE_OK ){ pCur->pKey = pKey; }else{ - sqliteFree(pKey); + sqlite3_free(pKey); } }else{ rc = SQLITE_NOMEM; @@ -282,7 +282,7 @@ static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ ** Clear the current cursor position. */ static void clearCursorPosition(BtCursor *pCur){ - sqliteFree(pCur->pKey); + sqlite3_free(pCur->pKey); pCur->pKey = 0; pCur->eState = CURSOR_INVALID; } @@ -309,7 +309,7 @@ int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){ pCur->eState = CURSOR_INVALID; rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); if( rc==SQLITE_OK ){ - sqliteFree(pCur->pKey); + sqlite3_free(pCur->pKey); pCur->pKey = 0; assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); } @@ -608,7 +608,7 @@ static int defragmentPage(MemPage *pPage){ assert( pPage->pBt!=0 ); assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); assert( pPage->nOverflow==0 ); - temp = sqliteMalloc( pPage->pBt->pageSize ); + temp = sqlite3_malloc( pPage->pBt->pageSize ); if( temp==0 ) return SQLITE_NOMEM; data = pPage->aData; hdr = pPage->hdrOffset; @@ -636,7 +636,7 @@ static int defragmentPage(MemPage *pPage){ data[hdr+7] = 0; addr = cellOffset+2*nCell; memset(&data[addr], 0, brk-addr); - sqliteFree(temp); + sqlite3_free(temp); return SQLITE_OK; } @@ -1052,7 +1052,7 @@ int sqlite3BtreeOpen( #endif #endif - p = sqliteMalloc(sizeof(Btree)); + p = sqlite3MallocZero(sizeof(Btree)); if( !p ){ return SQLITE_NOMEM; } @@ -1065,7 +1065,7 @@ int sqlite3BtreeOpen( if( pTsdro->useSharedData && zFilename && !isMemdb ){ char *zFullPathname = sqlite3OsFullPathname(zFilename); if( !zFullPathname ){ - sqliteFree(p); + sqlite3_free(p); return SQLITE_NOMEM; } for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){ @@ -1074,11 +1074,11 @@ int sqlite3BtreeOpen( p->pBt = pBt; *ppBtree = p; pBt->nRef++; - sqliteFree(zFullPathname); + sqlite3_free(zFullPathname); return SQLITE_OK; } } - sqliteFree(zFullPathname); + sqlite3_free(zFullPathname); } #endif @@ -1093,7 +1093,7 @@ int sqlite3BtreeOpen( assert( sizeof(u16)==2 ); assert( sizeof(Pgno)==4 ); - pBt = sqliteMalloc( sizeof(*pBt) ); + pBt = sqlite3MallocZero( sizeof(*pBt) ); if( pBt==0 ){ rc = SQLITE_NOMEM; goto btree_open_out; @@ -1166,8 +1166,8 @@ btree_open_out: if( pBt && pBt->pPager ){ sqlite3PagerClose(pBt->pPager); } - sqliteFree(pBt); - sqliteFree(p); + sqlite3_free(pBt); + sqlite3_free(p); *ppBtree = 0; } return rc; @@ -1199,7 +1199,7 @@ int sqlite3BtreeClose(Btree *p){ ** this handle. */ sqlite3BtreeRollback(p); - sqliteFree(p); + sqlite3_free(p); #ifndef SQLITE_OMIT_SHARED_CACHE /* If there are still other outstanding references to the shared-btree @@ -1236,8 +1236,8 @@ int sqlite3BtreeClose(Btree *p){ if( pBt->xFreeSchema && pBt->pSchema ){ pBt->xFreeSchema(pBt->pSchema); } - sqliteFree(pBt->pSchema); - sqliteFree(pBt); + sqlite3_free(pBt->pSchema); + sqlite3_free(pBt); return SQLITE_OK; } @@ -2381,7 +2381,7 @@ int sqlite3BtreeCursor( return SQLITE_READONLY; } } - pCur = sqliteMalloc( sizeof(*pCur) ); + pCur = sqlite3MallocZero( sizeof(*pCur) ); if( pCur==0 ){ rc = SQLITE_NOMEM; goto create_cursor_exception; @@ -2416,7 +2416,7 @@ int sqlite3BtreeCursor( create_cursor_exception: if( pCur ){ releasePage(pCur->pPage); - sqliteFree(pCur); + sqlite3_free(pCur); } unlockBtreeIfUnused(pBt); return rc; @@ -2440,7 +2440,7 @@ int sqlite3BtreeCloseCursor(BtCursor *pCur){ releasePage(pCur->pPage); unlockBtreeIfUnused(pBt); invalidateOverflowCache(pCur); - sqliteFree(pCur); + sqlite3_free(pCur); return SQLITE_OK; } @@ -2763,7 +2763,7 @@ static int accessPayload( */ if( pCur->isIncrblobHandle && !pCur->aOverflow ){ int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; - pCur->aOverflow = (Pgno *)sqliteMalloc(sizeof(Pgno)*nOvfl); + pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); if( nOvfl && !pCur->aOverflow ){ rc = SQLITE_NOMEM; } @@ -3238,11 +3238,11 @@ int sqlite3BtreeMoveto( if( available>=nCellKey ){ c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); }else{ - pCellKey = sqliteMallocRaw( nCellKey ); + pCellKey = sqlite3_malloc( nCellKey ); if( pCellKey==0 ) return SQLITE_NOMEM; rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); - sqliteFree(pCellKey); + sqlite3_free(pCellKey); if( rc ) return rc; } } @@ -4452,7 +4452,7 @@ static int balance_nonroot(MemPage *pPage){ /* ** Allocate space for memory structures */ - apCell = sqliteMallocRaw( + apCell = sqlite3_malloc( nMaxCells*sizeof(u8*) /* apCell */ + nMaxCells*sizeof(int) /* szCell */ + ROUND8(sizeof(MemPage))*NB /* aCopy */ @@ -4863,7 +4863,7 @@ static int balance_nonroot(MemPage *pPage){ ** Cleanup before returning. */ balance_cleanup: - sqliteFree(apCell); + sqlite3_free(apCell); for(i=0; i<nOld; i++){ releasePage(apOld[i]); } @@ -4894,7 +4894,7 @@ static int balance_shallower(MemPage *pPage){ assert( pPage->nCell==0 ); pBt = pPage->pBt; mxCellPerPage = MX_CELL(pBt); - apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); + apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); if( apCell==0 ) return SQLITE_NOMEM; szCell = (int*)&apCell[mxCellPerPage]; if( pPage->leaf ){ @@ -4968,7 +4968,7 @@ static int balance_shallower(MemPage *pPage){ releasePage(pChild); } end_shallow_balance: - sqliteFree(apCell); + sqlite3_free(apCell); return rc; } @@ -5151,7 +5151,7 @@ int sqlite3BtreeInsert( assert( pPage->isInit ); rc = sqlite3PagerWrite(pPage->pDbPage); if( rc ) return rc; - newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); + newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); if( newCell==0 ) return SQLITE_NOMEM; rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); if( rc ) goto end_insert; @@ -5184,7 +5184,7 @@ int sqlite3BtreeInsert( moveToRoot(pCur); } end_insert: - sqliteFree(newCell); + sqlite3_free(newCell); return rc; } @@ -5267,7 +5267,7 @@ int sqlite3BtreeDelete(BtCursor *pCur){ pNext = findCell(leafCur.pPage, leafCur.idx); szNext = cellSizePtr(leafCur.pPage, pNext); assert( MX_CELL_SIZE(pBt)>=szNext+4 ); - tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); + tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); if( tempCell==0 ){ rc = SQLITE_NOMEM; } @@ -5283,7 +5283,7 @@ int sqlite3BtreeDelete(BtCursor *pCur){ dropCell(leafCur.pPage, leafCur.idx, szNext); rc = balance(leafCur.pPage, 0); } - sqliteFree(tempCell); + sqlite3_free(tempCell); sqlite3BtreeReleaseTempCursor(&leafCur); }else{ TRACE(("DELETE: table=%d delete from leaf %d\n", @@ -5748,11 +5748,11 @@ static void checkAppendMsg( char *zOld = pCheck->zErrMsg; pCheck->zErrMsg = 0; sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); - sqliteFree(zOld); + sqlite3_free(zOld); }else{ sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); } - sqliteFree(zMsg2); + sqlite3_free(zMsg2); } #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ @@ -5995,7 +5995,7 @@ static int checkTreePage( */ data = pPage->aData; hdr = pPage->hdrOffset; - hit = sqliteMalloc( usableSize ); + hit = sqlite3MallocZero( usableSize ); if( hit ){ memset(hit, 1, get2byte(&data[hdr+5])); nCell = get2byte(&data[hdr+3]); @@ -6038,7 +6038,7 @@ static int checkTreePage( cnt, data[hdr+7], iPage); } } - sqliteFree(hit); + sqlite3_free(hit); releasePage(pPage); return depth+1; @@ -6070,7 +6070,7 @@ char *sqlite3BtreeIntegrityCheck( nRef = sqlite3PagerRefcount(pBt->pPager); if( lockBtreeWithRetry(p)!=SQLITE_OK ){ - return sqliteStrDup("Unable to acquire a read lock on the database"); + return sqlite3StrDup("Unable to acquire a read lock on the database"); } sCheck.pBt = pBt; sCheck.pPager = pBt->pPager; @@ -6087,7 +6087,7 @@ char *sqlite3BtreeIntegrityCheck( unlockBtreeIfUnused(pBt); return 0; } - sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); + sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); if( !sCheck.anRef ){ unlockBtreeIfUnused(pBt); *pnErr = 1; @@ -6152,7 +6152,7 @@ char *sqlite3BtreeIntegrityCheck( /* Clean up and report errors. */ - sqliteFree(sCheck.anRef); + sqlite3_free(sCheck.anRef); *pnErr = sCheck.nErr; return sCheck.zErrMsg; } @@ -6280,13 +6280,13 @@ int sqlite3BtreeIsInReadTrans(Btree *p){ ** ** Just before the shared-btree is closed, the function passed as the ** xFree argument when the memory allocation was made is invoked on the -** blob of allocated memory. This function should not call sqliteFree() +** blob of allocated memory. This function should not call sqlite3_free() ** on the memory, the btree layer does that. */ void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ BtShared *pBt = p->pBt; if( !pBt->pSchema ){ - pBt->pSchema = sqliteMalloc(nBytes); + pBt->pSchema = sqlite3MallocZero(nBytes); pBt->xFreeSchema = xFree; } return pBt->pSchema; diff --git a/src/build.c b/src/build.c index 0fd41807e..09ab2e817 100644 --- a/src/build.c +++ b/src/build.c @@ -22,7 +22,7 @@ ** COMMIT ** ROLLBACK ** -** $Id: build.c,v 1.433 2007/07/02 19:31:27 drh Exp $ +** $Id: build.c,v 1.434 2007/08/16 04:30:39 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -82,7 +82,8 @@ void sqlite3TableLock( } nBytes = sizeof(TableLock) * (pParse->nTableLock+1); - pParse->aTableLock = sqliteReallocOrFree(pParse->aTableLock, nBytes); + pParse->aTableLock = + sqlite3DbReallocOrFree(pParse->db, pParse->aTableLock, nBytes); if( pParse->aTableLock ){ p = &pParse->aTableLock[pParse->nTableLock++]; p->iDb = iDb; @@ -132,7 +133,8 @@ void sqlite3FinishCoding(Parse *pParse){ sqlite3 *db; Vdbe *v; - if( sqlite3MallocFailed() ) return; + db = pParse->db; + if( db->mallocFailed ) return; if( pParse->nested ) return; if( !pParse->pVdbe ){ if( pParse->rc==SQLITE_OK && pParse->nErr ){ @@ -144,7 +146,6 @@ void sqlite3FinishCoding(Parse *pParse){ /* Begin by generating some termination code at the end of the ** vdbe program */ - db = pParse->db; v = sqlite3GetVdbe(pParse); if( v ){ sqlite3VdbeAddOp(v, OP_Halt, 0, 0); @@ -193,7 +194,7 @@ void sqlite3FinishCoding(Parse *pParse){ /* Get the VDBE program ready for execution */ - if( v && pParse->nErr==0 && !sqlite3MallocFailed() ){ + if( v && pParse->nErr==0 && !db->mallocFailed ){ #ifdef SQLITE_DEBUG FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; sqlite3VdbeTrace(v, trace); @@ -237,13 +238,14 @@ void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ zSql = sqlite3VMPrintf(zFormat, ap); va_end(ap); if( zSql==0 ){ + pParse->db->mallocFailed = 1; return; /* A malloc must have failed */ } pParse->nested++; memcpy(saveBuf, &pParse->nVar, SAVE_SZ); memset(&pParse->nVar, 0, SAVE_SZ); sqlite3RunParser(pParse, zSql, 0); - sqliteFree(zSql); + sqlite3_free(zSql); memcpy(&pParse->nVar, saveBuf, SAVE_SZ); pParse->nested--; } @@ -336,8 +338,8 @@ Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ ** Reclaim the memory used by an index */ static void freeIndex(Index *p){ - sqliteFree(p->zColAff); - sqliteFree(p); + sqlite3_free(p->zColAff); + sqlite3_free(p); } /* @@ -426,7 +428,7 @@ void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){ for(i=j=2; i<db->nDb; i++){ struct Db *pDb = &db->aDb[i]; if( pDb->pBt==0 ){ - sqliteFree(pDb->zName); + sqlite3_free(pDb->zName); pDb->zName = 0; continue; } @@ -439,7 +441,7 @@ void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){ db->nDb = j; if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); - sqliteFree(db->aDb); + sqlite3_free(db->aDb); db->aDb = db->aDbStatic; } } @@ -460,12 +462,12 @@ static void sqliteResetColumnNames(Table *pTable){ assert( pTable!=0 ); if( (pCol = pTable->aCol)!=0 ){ for(i=0; i<pTable->nCol; i++, pCol++){ - sqliteFree(pCol->zName); + sqlite3_free(pCol->zName); sqlite3ExprDelete(pCol->pDflt); - sqliteFree(pCol->zType); - sqliteFree(pCol->zColl); + sqlite3_free(pCol->zType); + sqlite3_free(pCol->zColl); } - sqliteFree(pTable->aCol); + sqlite3_free(pTable->aCol); } pTable->aCol = 0; pTable->nCol = 0; @@ -510,21 +512,21 @@ void sqlite3DeleteTable(Table *pTable){ pNextFKey = pFKey->pNextFrom; assert( sqlite3HashFind(&pTable->pSchema->aFKey, pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey ); - sqliteFree(pFKey); + sqlite3_free(pFKey); } #endif /* Delete the Table structure itself. */ sqliteResetColumnNames(pTable); - sqliteFree(pTable->zName); - sqliteFree(pTable->zColAff); + sqlite3_free(pTable->zName); + sqlite3_free(pTable->zColAff); sqlite3SelectDelete(pTable->pSelect); #ifndef SQLITE_OMIT_CHECK sqlite3ExprDelete(pTable->pCheck); #endif sqlite3VtabClear(pTable); - sqliteFree(pTable); + sqlite3_free(pTable); } /* @@ -571,10 +573,10 @@ void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ ** are not \000 terminated and are not persistent. The returned string ** is \000 terminated and is persistent. */ -char *sqlite3NameFromToken(Token *pName){ +char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ char *zName; if( pName ){ - zName = sqliteStrNDup((char*)pName->z, pName->n); + zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); sqlite3Dequote(zName); }else{ zName = 0; @@ -606,7 +608,7 @@ int sqlite3FindDb(sqlite3 *db, Token *pName){ Db *pDb; /* A database whose name space is being searched */ char *zName; /* Name we are searching for */ - zName = sqlite3NameFromToken(pName); + zName = sqlite3NameFromToken(db, pName); if( zName ){ n = strlen(zName); for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ @@ -615,7 +617,7 @@ int sqlite3FindDb(sqlite3 *db, Token *pName){ break; } } - sqliteFree(zName); + sqlite3_free(zName); } return i; } @@ -738,7 +740,7 @@ void sqlite3StartTable( if( !OMIT_TEMPDB && isTemp ) iDb = 1; pParse->sNameToken = *pName; - zName = sqlite3NameFromToken(pName); + zName = sqlite3NameFromToken(db, pName); if( zName==0 ) return; if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto begin_table_error; @@ -795,8 +797,9 @@ void sqlite3StartTable( } } - pTable = sqliteMalloc( sizeof(Table) ); + pTable = sqlite3MallocZero( sizeof(Table) ); if( pTable==0 ){ + db->mallocFailed = 1; pParse->rc = SQLITE_NOMEM; pParse->nErr++; goto begin_table_error; @@ -881,7 +884,7 @@ void sqlite3StartTable( /* If an error occurs, we jump here */ begin_table_error: - sqliteFree(zName); + sqlite3_free(zName); return; } @@ -916,20 +919,21 @@ void sqlite3AddColumn(Parse *pParse, Token *pName){ sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); return; } - z = sqlite3NameFromToken(pName); + z = sqlite3NameFromToken(pParse->db, pName); if( z==0 ) return; for(i=0; i<p->nCol; i++){ if( STRICMP(z, p->aCol[i].zName) ){ sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); - sqliteFree(z); + sqlite3_free(z); return; } } if( (p->nCol & 0x7)==0 ){ Column *aNew; - aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); + aNew = sqlite3_realloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); if( aNew==0 ){ - sqliteFree(z); + pParse->db->mallocFailed = 1; + sqlite3_free(z); return; } p->aCol = aNew; @@ -1041,8 +1045,8 @@ void sqlite3AddColumnType(Parse *pParse, Token *pType){ i = p->nCol-1; if( i<0 ) return; pCol = &p->aCol[i]; - sqliteFree(pCol->zType); - pCol->zType = sqlite3NameFromToken(pType); + sqlite3_free(pCol->zType); + pCol->zType = sqlite3NameFromToken(pParse->db, pType); pCol->affinity = sqlite3AffinityType(pType); } @@ -1066,10 +1070,11 @@ void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){ pCol->zName); }else{ Expr *pCopy; + sqlite3 *db = pParse->db; sqlite3ExprDelete(pCol->pDflt); - pCol->pDflt = pCopy = sqlite3ExprDup(pExpr); + pCol->pDflt = pCopy = sqlite3ExprDup(db, pExpr); if( pCopy ){ - sqlite3TokenCopy(&pCopy->span, &pExpr->span); + sqlite3TokenCopy(db, &pCopy->span, &pExpr->span); } } } @@ -1159,11 +1164,13 @@ void sqlite3AddCheckConstraint( ){ #ifndef SQLITE_OMIT_CHECK Table *pTab = pParse->pNewTable; + sqlite3 *db = pParse->db; if( pTab && !IN_DECLARE_VTAB ){ /* The CHECK expression must be duplicated so that tokens refer ** to malloced space and not the (ephemeral) text of the CREATE TABLE ** statement */ - pTab->pCheck = sqlite3ExprAnd(pTab->pCheck, sqlite3ExprDup(pCheckExpr)); + pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, + sqlite3ExprDup(db, pCheckExpr)); } #endif sqlite3ExprDelete(pCheckExpr); @@ -1182,7 +1189,7 @@ void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){ if( sqlite3LocateCollSeq(pParse, zType, nType) ){ Index *pIdx; - p->aCol[i].zColl = sqliteStrNDup(zType, nType); + p->aCol[i].zColl = sqlite3DbStrNDup(pParse->db, zType, nType); /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", ** then an index may have been created on this column before the @@ -1326,7 +1333,7 @@ static char *createTableStmt(Table *p, int isTemp){ zEnd = "\n)"; } n += 35 + 6*p->nCol; - zStmt = sqliteMallocRaw( n ); + zStmt = sqlite3_malloc( n ); if( zStmt==0 ) return 0; sqlite3_snprintf(n, zStmt, !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE "); @@ -1379,7 +1386,7 @@ void sqlite3EndTable( sqlite3 *db = pParse->db; int iDb; - if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3MallocFailed() ) { + if( (pEnd==0 && pSelect==0) || pParse->nErr || db->mallocFailed ) { return; } p = pParse->pNewTable; @@ -1513,7 +1520,7 @@ void sqlite3EndTable( p->zName, zStmt ); - sqliteFree(zStmt); + sqlite3_free(zStmt); sqlite3ChangeCookie(db, v, iDb); #ifndef SQLITE_OMIT_AUTOINCREMENT @@ -1546,6 +1553,7 @@ void sqlite3EndTable( pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, strlen(p->zName)+1,p); if( pOld ){ assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ + db->mallocFailed = 1; return; } #ifndef SQLITE_OMIT_FOREIGN_KEY @@ -1594,6 +1602,7 @@ void sqlite3CreateView( DbFixer sFix; Token *pName; int iDb; + sqlite3 *db = pParse->db; if( pParse->nVar>0 ){ sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); @@ -1607,7 +1616,7 @@ void sqlite3CreateView( return; } sqlite3TwoPartName(pParse, pName1, pName2, &pName); - iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); + iDb = sqlite3SchemaToIndex(db, p->pSchema); if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) && sqlite3FixSelect(&sFix, pSelect) ){ @@ -1620,12 +1629,12 @@ void sqlite3CreateView( ** allocated rather than point to the input string - which means that ** they will persist after the current sqlite3_exec() call returns. */ - p->pSelect = sqlite3SelectDup(pSelect); + p->pSelect = sqlite3SelectDup(db, pSelect); sqlite3SelectDelete(pSelect); - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ return; } - if( !pParse->db->init.busy ){ + if( !db->init.busy ){ sqlite3ViewGetColumnNames(pParse, p); } @@ -1660,6 +1669,7 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ Select *pSel; /* Copy of the SELECT that implements the view */ int nErr = 0; /* Number of errors encountered */ int n; /* Temporarily holds the number of cursors assigned */ + sqlite3 *db = pParse->db; /* Database connection for malloc errors */ assert( pTable ); @@ -1700,7 +1710,7 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ ** statement that defines the view. */ assert( pTable->pSelect ); - pSel = sqlite3SelectDup(pTable->pSelect); + pSel = sqlite3SelectDup(db, pTable->pSelect); if( pSel ){ n = pParse->nTab; sqlite3SrcListAssignCursors(pParse, pSel->pSrc); @@ -1879,7 +1889,7 @@ void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ sqlite3 *db = pParse->db; int iDb; - if( pParse->nErr || sqlite3MallocFailed() ){ + if( pParse->nErr || db->mallocFailed ){ goto exit_drop_table; } assert( pName->nSrc==1 ); @@ -2082,8 +2092,10 @@ void sqlite3CreateForeignKey( nByte += strlen(pToCol->a[i].zName) + 1; } } - pFKey = sqliteMalloc( nByte ); - if( pFKey==0 ) goto fk_end; + pFKey = sqlite3DbMallocZero(pParse->db, nByte ); + if( pFKey==0 ){ + goto fk_end; + } pFKey->pFrom = p; pFKey->pNextFrom = p->pFKey; z = (char*)&pFKey[1]; @@ -2134,7 +2146,7 @@ void sqlite3CreateForeignKey( pFKey = 0; fk_end: - sqliteFree(pFKey); + sqlite3_free(pFKey); #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ sqlite3ExprListDelete(pFromCol); sqlite3ExprListDelete(pToCol); @@ -2175,11 +2187,12 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ int tnum; /* Root page of index */ Vdbe *v; /* Generate code into this virtual machine */ KeyInfo *pKey; /* KeyInfo for index */ - int iDb = sqlite3SchemaToIndex(pParse->db, pIndex->pSchema); + sqlite3 *db = pParse->db; /* The database connection */ + int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); #ifndef SQLITE_OMIT_AUTHORIZATION if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, - pParse->db->aDb[iDb].zName ) ){ + db->aDb[iDb].zName ) ){ return; } #endif @@ -2211,7 +2224,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ sqlite3VdbeAddOp(v, OP_IsUnique, iIdx, addr2); sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort, "indexed columns are not unique", P3_STATIC); - assert( sqlite3MallocFailed() || addr2==sqlite3VdbeCurrentAddr(v) ); + assert( db->mallocFailed || addr2==sqlite3VdbeCurrentAddr(v) ); } sqlite3VdbeAddOp(v, OP_IdxInsert, iIdx, 0); sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1); @@ -2261,7 +2274,7 @@ void sqlite3CreateIndex( int nExtra = 0; char *zExtra; - if( pParse->nErr || sqlite3MallocFailed() || IN_DECLARE_VTAB ){ + if( pParse->nErr || db->mallocFailed || IN_DECLARE_VTAB ){ goto exit_create_index; } @@ -2339,7 +2352,7 @@ void sqlite3CreateIndex( ** own name. */ if( pName ){ - zName = sqlite3NameFromToken(pName); + zName = sqlite3NameFromToken(db, pName); if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index; if( zName==0 ) goto exit_create_index; if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ @@ -2412,7 +2425,7 @@ void sqlite3CreateIndex( */ nName = strlen(zName); nCol = pList->nExpr; - pIndex = sqliteMalloc( + pIndex = sqlite3DbMallocZero(db, sizeof(Index) + /* Index structure */ sizeof(int)*nCol + /* Index.aiColumn */ sizeof(int)*(nCol+1) + /* Index.aiRowEst */ @@ -2421,7 +2434,9 @@ void sqlite3CreateIndex( nName + 1 + /* Index.zName */ nExtra /* Collation sequence names */ ); - if( sqlite3MallocFailed() ) goto exit_create_index; + if( db->mallocFailed ){ + goto exit_create_index; + } pIndex->azColl = (char**)(&pIndex[1]); pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]); pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]); @@ -2548,6 +2563,7 @@ void sqlite3CreateIndex( pIndex->zName, strlen(pIndex->zName)+1, pIndex); if( p ){ assert( p==pIndex ); /* Malloc must have failed */ + db->mallocFailed = 1; goto exit_create_index; } db->flags |= SQLITE_InternChanges; @@ -2611,7 +2627,7 @@ void sqlite3CreateIndex( zStmt ); sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqliteFree(zStmt); + sqlite3_free(zStmt); /* Fill the index with data and reparse the schema. Code an OP_Expire ** to invalidate all pre-compiled statements. @@ -2653,7 +2669,7 @@ exit_create_index: } sqlite3ExprListDelete(pList); sqlite3SrcListDelete(pTblName); - sqliteFree(zName); + sqlite3_free(zName); return; } @@ -2718,7 +2734,7 @@ void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ sqlite3 *db = pParse->db; int iDb; - if( pParse->nErr || sqlite3MallocFailed() ){ + if( pParse->nErr || db->mallocFailed ){ goto exit_drop_index; } assert( pName->nSrc==1 ); @@ -2788,6 +2804,7 @@ exit_drop_index: ** pointer if the array was resized. */ void *sqlite3ArrayAllocate( + sqlite3 *db, /* Connection to notify of malloc failures */ void *pArray, /* Array of objects. Might be reallocated */ int szEntry, /* Size of each object in the array */ int initSize, /* Suggested initial allocation, in elements */ @@ -2800,8 +2817,9 @@ void *sqlite3ArrayAllocate( void *pNew; int newSize; newSize = (*pnAlloc)*2 + initSize; - pNew = sqliteRealloc(pArray, newSize*szEntry); + pNew = sqlite3_realloc(pArray, newSize*szEntry); if( pNew==0 ){ + db->mallocFailed = 1; *pIdx = -1; return pArray; } @@ -2821,14 +2839,15 @@ void *sqlite3ArrayAllocate( ** ** A new IdList is returned, or NULL if malloc() fails. */ -IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){ +IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){ int i; if( pList==0 ){ - pList = sqliteMalloc( sizeof(IdList) ); + pList = sqlite3DbMallocZero(db, sizeof(IdList) ); if( pList==0 ) return 0; pList->nAlloc = 0; } pList->a = sqlite3ArrayAllocate( + db, pList->a, sizeof(pList->a[0]), 5, @@ -2840,7 +2859,7 @@ IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){ sqlite3IdListDelete(pList); return 0; } - pList->a[i].zName = sqlite3NameFromToken(pToken); + pList->a[i].zName = sqlite3NameFromToken(db, pToken); return pList; } @@ -2851,10 +2870,10 @@ void sqlite3IdListDelete(IdList *pList){ int i; if( pList==0 ) return; for(i=0; i<pList->nId; i++){ - sqliteFree(pList->a[i].zName); + sqlite3_free(pList->a[i].zName); } - sqliteFree(pList->a); - sqliteFree(pList); + sqlite3_free(pList->a); + sqlite3_free(pList); } /* @@ -2886,28 +2905,34 @@ int sqlite3IdListIndex(IdList *pList, const char *zName){ ** ** In other words, if call like this: ** -** sqlite3SrcListAppend(A,B,0); +** sqlite3SrcListAppend(D,A,B,0); ** ** Then B is a table name and the database name is unspecified. If called ** like this: ** -** sqlite3SrcListAppend(A,B,C); +** sqlite3SrcListAppend(D,A,B,C); ** ** Then C is the table name and B is the database name. */ -SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ +SrcList *sqlite3SrcListAppend( + sqlite3 *db, /* Connection to notify of malloc failures */ + SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ + Token *pTable, /* Table to append */ + Token *pDatabase /* Database of the table */ +){ struct SrcList_item *pItem; if( pList==0 ){ - pList = sqliteMalloc( sizeof(SrcList) ); + pList = sqlite3DbMallocZero(db, sizeof(SrcList) ); if( pList==0 ) return 0; pList->nAlloc = 1; } if( pList->nSrc>=pList->nAlloc ){ SrcList *pNew; pList->nAlloc *= 2; - pNew = sqliteRealloc(pList, + pNew = sqlite3_realloc(pList, sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) ); if( pNew==0 ){ + db->mallocFailed = 1; sqlite3SrcListDelete(pList); return 0; } @@ -2923,8 +2948,8 @@ SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ pDatabase = pTable; pTable = pTemp; } - pItem->zName = sqlite3NameFromToken(pTable); - pItem->zDatabase = sqlite3NameFromToken(pDatabase); + pItem->zName = sqlite3NameFromToken(db, pTable); + pItem->zDatabase = sqlite3NameFromToken(db, pDatabase); pItem->iCursor = -1; pItem->isPopulated = 0; pList->nSrc++; @@ -2937,7 +2962,7 @@ SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ int i; struct SrcList_item *pItem; - assert(pList || sqlite3MallocFailed() ); + assert(pList || pParse->db->mallocFailed ); if( pList ){ for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ if( pItem->iCursor>=0 ) break; @@ -2957,15 +2982,15 @@ void sqlite3SrcListDelete(SrcList *pList){ struct SrcList_item *pItem; if( pList==0 ) return; for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ - sqliteFree(pItem->zDatabase); - sqliteFree(pItem->zName); - sqliteFree(pItem->zAlias); + sqlite3_free(pItem->zDatabase); + sqlite3_free(pItem->zName); + sqlite3_free(pItem->zAlias); sqlite3DeleteTable(pItem->pTab); sqlite3SelectDelete(pItem->pSelect); sqlite3ExprDelete(pItem->pOn); sqlite3IdListDelete(pItem->pUsing); } - sqliteFree(pList); + sqlite3_free(pList); } /* @@ -2985,6 +3010,7 @@ void sqlite3SrcListDelete(SrcList *pList){ ** term added. */ SrcList *sqlite3SrcListAppendFromTerm( + Parse *pParse, /* Parsing context */ SrcList *p, /* The left part of the FROM clause already seen */ Token *pTable, /* Name of the table to add to the FROM clause */ Token *pDatabase, /* Name of the database containing pTable */ @@ -2994,7 +3020,8 @@ SrcList *sqlite3SrcListAppendFromTerm( IdList *pUsing /* The USING clause of a join */ ){ struct SrcList_item *pItem; - p = sqlite3SrcListAppend(p, pTable, pDatabase); + sqlite3 *db = pParse->db; + p = sqlite3SrcListAppend(db, p, pTable, pDatabase); if( p==0 || p->nSrc==0 ){ sqlite3ExprDelete(pOn); sqlite3IdListDelete(pUsing); @@ -3003,7 +3030,7 @@ SrcList *sqlite3SrcListAppendFromTerm( } pItem = &p->a[p->nSrc-1]; if( pAlias && pAlias->n ){ - pItem->zAlias = sqlite3NameFromToken(pAlias); + pItem->zAlias = sqlite3NameFromToken(db, pAlias); } pItem->pSelect = pSubquery; pItem->pOn = pOn; @@ -3045,7 +3072,7 @@ void sqlite3BeginTransaction(Parse *pParse, int type){ int i; if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; - if( pParse->nErr || sqlite3MallocFailed() ) return; + if( pParse->nErr || db->mallocFailed ) return; if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return; v = sqlite3GetVdbe(pParse); @@ -3066,7 +3093,7 @@ void sqlite3CommitTransaction(Parse *pParse){ Vdbe *v; if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; - if( pParse->nErr || sqlite3MallocFailed() ) return; + if( pParse->nErr || db->mallocFailed ) return; if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return; v = sqlite3GetVdbe(pParse); @@ -3083,7 +3110,7 @@ void sqlite3RollbackTransaction(Parse *pParse){ Vdbe *v; if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; - if( pParse->nErr || sqlite3MallocFailed() ) return; + if( pParse->nErr || db->mallocFailed ) return; if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return; v = sqlite3GetVdbe(pParse); @@ -3295,27 +3322,27 @@ void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ assert( pName1->z ); pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0); if( pColl ){ - char *zColl = sqliteStrNDup((const char *)pName1->z, pName1->n); + char *zColl = sqlite3DbStrNDup(db, (const char *)pName1->z, pName1->n); if( zColl ){ reindexDatabases(pParse, zColl); - sqliteFree(zColl); + sqlite3_free(zColl); } return; } } iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); if( iDb<0 ) return; - z = sqlite3NameFromToken(pObjName); + z = sqlite3NameFromToken(db, pObjName); if( z==0 ) return; zDb = db->aDb[iDb].zName; pTab = sqlite3FindTable(db, z, zDb); if( pTab ){ reindexTable(pParse, pTab, 0); - sqliteFree(z); + sqlite3_free(z); return; } pIndex = sqlite3FindIndex(db, z, zDb); - sqliteFree(z); + sqlite3_free(z); if( pIndex ){ sqlite3BeginWriteOperation(pParse, 0, iDb); sqlite3RefillIndex(pParse, pIndex, -1); @@ -3330,7 +3357,7 @@ void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ ** with OP_OpenRead or OP_OpenWrite to access database index pIdx. ** ** If successful, a pointer to the new structure is returned. In this case -** the caller is responsible for calling sqliteFree() on the returned +** the caller is responsible for calling sqlite3_free() on the returned ** pointer. If an error occurs (out of memory or missing collation ** sequence), NULL is returned and the state of pParse updated to reflect ** the error. @@ -3339,7 +3366,7 @@ KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){ int i; int nCol = pIdx->nColumn; int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol; - KeyInfo *pKey = (KeyInfo *)sqliteMalloc(nBytes); + KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZer(pParse->db, nBytes); if( pKey ){ pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]); @@ -3354,7 +3381,7 @@ KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){ } if( pParse->nErr ){ - sqliteFree(pKey); + sqlite3_free(pKey); pKey = 0; } return pKey; diff --git a/src/callback.c b/src/callback.c index f3d418808..1b2fff130 100644 --- a/src/callback.c +++ b/src/callback.c @@ -13,7 +13,7 @@ ** This file contains functions used to access the internal hash tables ** of user defined functions and collation sequences. ** -** $Id: callback.c,v 1.18 2007/05/07 09:32:45 danielk1977 Exp $ +** $Id: callback.c,v 1.19 2007/08/16 04:30:39 drh Exp $ */ #include "sqliteInt.h" @@ -27,10 +27,10 @@ static void callCollNeeded(sqlite3 *db, const char *zName, int nName){ assert( !db->xCollNeeded || !db->xCollNeeded16 ); if( nName<0 ) nName = strlen(zName); if( db->xCollNeeded ){ - char *zExternal = sqliteStrNDup(zName, nName); + char *zExternal = sqlite3DbStrNDup(db, zName, nName); if( !zExternal ) return; db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal); - sqliteFree(zExternal); + sqlite3_free(zExternal); } #ifndef SQLITE_OMIT_UTF16 if( db->xCollNeeded16 ){ @@ -162,7 +162,7 @@ static CollSeq *findCollSeqEntry( pColl = sqlite3HashFind(&db->aCollSeq, zName, nName); if( 0==pColl && create ){ - pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 ); + pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 ); if( pColl ){ CollSeq *pDel = 0; pColl[0].zName = (char*)&pColl[3]; @@ -179,9 +179,9 @@ static CollSeq *findCollSeqEntry( ** return the pColl pointer to be deleted (because it wasn't added ** to the hash table). */ - assert( !pDel || (sqlite3MallocFailed() && pDel==pColl) ); + assert( !pDel || (db->mallocFailed && pDel==pColl) ); if( pDel ){ - sqliteFree(pDel); + sqlite3_free(pDel); pColl = 0; } } @@ -303,14 +303,14 @@ FuncDef *sqlite3FindFunction( ** new entry to the hash table and return it. */ if( createFlag && bestmatch<6 && - (pBest = sqliteMalloc(sizeof(*pBest)+nName))!=0 ){ + (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName))!=0 ){ pBest->nArg = nArg; pBest->pNext = pFirst; pBest->iPrefEnc = enc; memcpy(pBest->zName, zName, nName); pBest->zName[nName] = 0; if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){ - sqliteFree(pBest); + sqlite3_free(pBest); return 0; } } @@ -323,7 +323,7 @@ FuncDef *sqlite3FindFunction( /* ** Free all resources held by the schema structure. The void* argument points -** at a Schema struct. This function does not call sqliteFree() on the +** at a Schema struct. This function does not call sqlite3_free() on the ** pointer itself, it just cleans up subsiduary resources (i.e. the contents ** of the schema hash tables). */ @@ -356,12 +356,12 @@ void sqlite3SchemaFree(void *p){ ** Find and return the schema associated with a BTree. Create ** a new one if necessary. */ -Schema *sqlite3SchemaGet(Btree *pBt){ +Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ Schema * p; if( pBt ){ p = (Schema *)sqlite3BtreeSchema(pBt,sizeof(Schema),sqlite3SchemaFree); }else{ - p = (Schema *)sqliteMalloc(sizeof(Schema)); + p = (Schema *)sqlite3DbMallocZero(db,sizeof(Schema)); } if( p && 0==p->file_format ){ sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0); diff --git a/src/date.c b/src/date.c index bf4bc5631..d1b80038e 100644 --- a/src/date.c +++ b/src/date.c @@ -16,7 +16,7 @@ ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: date.c,v 1.66 2007/05/08 21:56:00 drh Exp $ +** $Id: date.c,v 1.67 2007/08/16 04:30:40 drh Exp $ ** ** SQLite processes all times and dates as Julian Day numbers. The ** dates and times are stored as the number of days since noon @@ -817,7 +817,7 @@ static void strftimeFunc( sqlite3_result_error_toobig(context); return; }else{ - z = sqliteMalloc( n ); + z = sqlite3_malloc( n ); if( z==0 ) return; } computeJD(&x); @@ -880,7 +880,7 @@ static void strftimeFunc( z[j] = 0; sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); if( z!=zBuf ){ - sqliteFree(z); + sqlite3_free(z); } } diff --git a/src/delete.c b/src/delete.c index b30452624..12e61cd96 100644 --- a/src/delete.c +++ b/src/delete.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** in order to generate code for DELETE FROM statements. ** -** $Id: delete.c,v 1.129 2007/04/16 15:06:25 danielk1977 Exp $ +** $Id: delete.c,v 1.130 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" @@ -115,10 +115,10 @@ void sqlite3DeleteFrom( #endif sContext.pParse = 0; - if( pParse->nErr || sqlite3MallocFailed() ){ + db = pParse->db; + if( pParse->nErr || db->mallocFailed ){ goto delete_from_cleanup; } - db = pParse->db; assert( pTabList->nSrc==1 ); /* Locate the table which we want to delete. This table has to be @@ -196,7 +196,7 @@ void sqlite3DeleteFrom( ** a ephemeral table. */ if( isView ){ - Select *pView = sqlite3SelectDup(pTab->pSelect); + Select *pView = sqlite3SelectDup(db, pTab->pSelect); sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0); sqlite3SelectDelete(pView); } diff --git a/src/expr.c b/src/expr.c index b640bec09..a85c5ef37 100644 --- a/src/expr.c +++ b/src/expr.c @@ -12,7 +12,7 @@ ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** -** $Id: expr.c,v 1.303 2007/08/07 17:13:04 drh Exp $ +** $Id: expr.c,v 1.304 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -217,12 +217,17 @@ static int codeCompare( /* ** Construct a new expression node and return a pointer to it. Memory -** for this node is obtained from sqliteMalloc(). The calling function +** for this node is obtained from sqlite3_malloc(). The calling function ** is responsible for making sure the node eventually gets freed. */ -Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ +Expr *sqlite3Expr( + int op, /* Expression opcode */ + Expr *pLeft, /* Left operand */ + Expr *pRight, /* Right operand */ + const Token *pToken /* Argument token */ +){ Expr *pNew; - pNew = sqliteMalloc( sizeof(Expr) ); + pNew = sqlite3MallocZero( sizeof(Expr) ); if( pNew==0 ){ /* When malloc fails, delete pLeft and pRight. Expressions passed to ** this function must always be allocated with sqlite3Expr() for this @@ -258,14 +263,21 @@ Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ } /* -** Works like sqlite3Expr() but frees its pLeft and pRight arguments -** if it fails due to a malloc problem. +** Works like sqlite3Expr() except that it takes an extra Parse* +** argument and notifies the associated connection object if malloc fails. */ -Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ +Expr *sqlite3PExpr( + Parse *pParse, /* Parsing context */ + int op, /* Expression opcode */ + Expr *pLeft, /* Left operand */ + Expr *pRight, /* Right operand */ + const Token *pToken /* Argument token */ +){ Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken); if( pNew==0 ){ sqlite3ExprDelete(pLeft); sqlite3ExprDelete(pRight); + pParse->db->mallocFailed = 1; } return pNew; } @@ -292,6 +304,7 @@ Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ if( v==0 ) return 0; p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); if( p==0 ){ + pParse->db->mallocFailed = 1; return 0; /* Malloc failed */ } depth = atoi((char*)&pToken->z[1]); @@ -305,13 +318,16 @@ Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ ** Join two expressions using an AND operator. If either expression is ** NULL, then just return the other expression. */ -Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ +Expr *sqlite3ExprAnd(sqlite *db, Expr *pLeft, Expr *pRight){ if( pLeft==0 ){ return pRight; }else if( pRight==0 ){ return pLeft; }else{ - return sqlite3Expr(TK_AND, pLeft, pRight, 0); + Expr *p = sqlite3Expr(TK_AND, pLeft, pRight, 0); + if( p==0 ){ + db->mallocFailed = 1; + } } } @@ -322,7 +338,7 @@ Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ assert( pRight!=0 ); assert( pLeft!=0 ); - if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){ + if( pRight->z && pLeft->z ){ assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); if( pLeft->dyn==0 && pRight->dyn==0 ){ pExpr->span.z = pLeft->z; @@ -337,10 +353,10 @@ void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ ** Construct a new expression node for a function with multiple ** arguments. */ -Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ +Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ Expr *pNew; assert( pToken ); - pNew = sqliteMalloc( sizeof(Expr) ); + pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) ); if( pNew==0 ){ sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ return 0; @@ -373,6 +389,8 @@ Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ */ void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ Token *pToken; + sqlite3 *db = pParse->db; + if( pExpr==0 ) return; pToken = &pExpr->token; assert( pToken->n>=1 ); @@ -413,10 +431,14 @@ void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ pExpr->iTable = ++pParse->nVar; if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; - pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr, - pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); + pParse->apVarExpr = + sqlite3DbReallocOrFree( + db, + pParse->apVarExpr, + pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) + ); } - if( !sqlite3MallocFailed() ){ + if( !db->mallocFailed ){ assert( pParse->apVarExpr!=0 ); pParse->apVarExpr[pParse->nVarExpr++] = pExpr; } @@ -432,26 +454,26 @@ void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ */ void sqlite3ExprDelete(Expr *p){ if( p==0 ) return; - if( p->span.dyn ) sqliteFree((char*)p->span.z); - if( p->token.dyn ) sqliteFree((char*)p->token.z); + if( p->span.dyn ) sqlite3_free((char*)p->span.z); + if( p->token.dyn ) sqlite3_free((char*)p->token.z); sqlite3ExprDelete(p->pLeft); sqlite3ExprDelete(p->pRight); sqlite3ExprListDelete(p->pList); sqlite3SelectDelete(p->pSelect); - sqliteFree(p); + sqlite3_free(p); } /* ** The Expr.token field might be a string literal that is quoted. ** If so, remove the quotation marks. */ -void sqlite3DequoteExpr(Expr *p){ +void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ if( ExprHasAnyProperty(p, EP_Dequoted) ){ return; } ExprSetProperty(p, EP_Dequoted); if( p->token.dyn==0 ){ - sqlite3TokenCopy(&p->token, &p->token); + sqlite3TokenCopy(db, &p->token, &p->token); } sqlite3Dequote((char*)p->token.z); } @@ -469,62 +491,62 @@ void sqlite3DequoteExpr(Expr *p){ ** ** Any tables that the SrcList might point to are not duplicated. */ -Expr *sqlite3ExprDup(Expr *p){ +Expr *sqlite3ExprDup(sqlite *db, Expr *p){ Expr *pNew; if( p==0 ) return 0; - pNew = sqliteMallocRaw( sizeof(*p) ); + pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); if( pNew==0 ) return 0; memcpy(pNew, p, sizeof(*pNew)); if( p->token.z!=0 ){ - pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n); + pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); pNew->token.dyn = 1; }else{ assert( pNew->token.z==0 ); } pNew->span.z = 0; - pNew->pLeft = sqlite3ExprDup(p->pLeft); - pNew->pRight = sqlite3ExprDup(p->pRight); - pNew->pList = sqlite3ExprListDup(p->pList); - pNew->pSelect = sqlite3SelectDup(p->pSelect); + pNew->pLeft = sqlite3ExprDup(db, p->pLeft); + pNew->pRight = sqlite3ExprDup(db, p->pRight); + pNew->pList = sqlite3ExprListDup(db, p->pList); + pNew->pSelect = sqlite3SelectDup(db, p->pSelect); return pNew; } -void sqlite3TokenCopy(Token *pTo, Token *pFrom){ - if( pTo->dyn ) sqliteFree((char*)pTo->z); +void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ + if( pTo->dyn ) sqlite3_free((char*)pTo->z); if( pFrom->z ){ pTo->n = pFrom->n; - pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n); + pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); pTo->dyn = 1; }else{ pTo->z = 0; } } -ExprList *sqlite3ExprListDup(ExprList *p){ +ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ ExprList *pNew; struct ExprList_item *pItem, *pOldItem; int i; if( p==0 ) return 0; - pNew = sqliteMalloc( sizeof(*pNew) ); + pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); if( pNew==0 ) return 0; pNew->nExpr = pNew->nAlloc = p->nExpr; - pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); + pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); if( pItem==0 ){ - sqliteFree(pNew); + sqlite3_free(pNew); return 0; } pOldItem = p->a; for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ Expr *pNewExpr, *pOldExpr; - pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); + pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); if( pOldExpr->span.z!=0 && pNewExpr ){ /* Always make a copy of the span for top-level expressions in the ** expression list. The logic in SELECT processing that determines ** the names of columns in the result set needs this information */ - sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); + sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); } assert( pNewExpr==0 || pNewExpr->span.z!=0 || pOldExpr->span.z==0 - || sqlite3MallocFailed() ); - pItem->zName = sqliteStrDup(pOldItem->zName); + || db->mallocFailed ); + pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); pItem->sortOrder = pOldItem->sortOrder; pItem->isAgg = pOldItem->isAgg; pItem->done = 0; @@ -540,22 +562,22 @@ ExprList *sqlite3ExprListDup(ExprList *p){ */ #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ || !defined(SQLITE_OMIT_SUBQUERY) -SrcList *sqlite3SrcListDup(SrcList *p){ +SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ SrcList *pNew; int i; int nByte; if( p==0 ) return 0; nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); - pNew = sqliteMallocRaw( nByte ); + pNew = sqlite3DbMallocRaw(db, nByte ); if( pNew==0 ) return 0; pNew->nSrc = pNew->nAlloc = p->nSrc; for(i=0; i<p->nSrc; i++){ struct SrcList_item *pNewItem = &pNew->a[i]; struct SrcList_item *pOldItem = &p->a[i]; Table *pTab; - pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); - pNewItem->zName = sqliteStrDup(pOldItem->zName); - pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); + pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); + pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); + pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); pNewItem->jointype = pOldItem->jointype; pNewItem->iCursor = pOldItem->iCursor; pNewItem->isPopulated = pOldItem->isPopulated; @@ -563,49 +585,49 @@ SrcList *sqlite3SrcListDup(SrcList *p){ if( pTab ){ pTab->nRef++; } - pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); - pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); - pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); + pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); + pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); + pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); pNewItem->colUsed = pOldItem->colUsed; } return pNew; } -IdList *sqlite3IdListDup(IdList *p){ +IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ IdList *pNew; int i; if( p==0 ) return 0; - pNew = sqliteMallocRaw( sizeof(*pNew) ); + pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); if( pNew==0 ) return 0; pNew->nId = pNew->nAlloc = p->nId; - pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); + pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); if( pNew->a==0 ){ - sqliteFree(pNew); + sqlite3_free(pNew); return 0; } for(i=0; i<p->nId; i++){ struct IdList_item *pNewItem = &pNew->a[i]; struct IdList_item *pOldItem = &p->a[i]; - pNewItem->zName = sqliteStrDup(pOldItem->zName); + pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); pNewItem->idx = pOldItem->idx; } return pNew; } -Select *sqlite3SelectDup(Select *p){ +Select *sqlite3SelectDup(sqlite3 *db, Select *p){ Select *pNew; if( p==0 ) return 0; - pNew = sqliteMallocRaw( sizeof(*p) ); + pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); if( pNew==0 ) return 0; pNew->isDistinct = p->isDistinct; - pNew->pEList = sqlite3ExprListDup(p->pEList); - pNew->pSrc = sqlite3SrcListDup(p->pSrc); - pNew->pWhere = sqlite3ExprDup(p->pWhere); - pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); - pNew->pHaving = sqlite3ExprDup(p->pHaving); - pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); + pNew->pEList = sqlite3ExprListDup(db, p->pEList); + pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); + pNew->pWhere = sqlite3ExprDup(db, p->pWhere); + pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); + pNew->pHaving = sqlite3ExprDup(db, p->pHaving); + pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); pNew->op = p->op; - pNew->pPrior = sqlite3SelectDup(p->pPrior); - pNew->pLimit = sqlite3ExprDup(p->pLimit); - pNew->pOffset = sqlite3ExprDup(p->pOffset); + pNew->pPrior = sqlite3SelectDup(db, p->pPrior); + pNew->pLimit = sqlite3ExprDup(db, p->pLimit); + pNew->pOffset = sqlite3ExprDup(db, p->pOffset); pNew->iLimit = -1; pNew->iOffset = -1; pNew->isResolved = p->isResolved; @@ -619,7 +641,7 @@ Select *sqlite3SelectDup(Select *p){ return pNew; } #else -Select *sqlite3SelectDup(Select *p){ +Select *sqlite3SelectDup(sqlite3 *db, Select *p){ assert( p==0 ); return 0; } @@ -630,9 +652,15 @@ Select *sqlite3SelectDup(Select *p){ ** Add a new element to the end of an expression list. If pList is ** initially NULL, then create a new expression list. */ -ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ +ExprList *sqlite3ExprListAppend( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List to which to append. Might be NULL */ + Expr *pExpr, /* Expression to be appended */ + Token *pName /* AS keyword for the expression */ +){ + sqlite3 *db = pParse->db; if( pList==0 ){ - pList = sqliteMalloc( sizeof(ExprList) ); + pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); if( pList==0 ){ goto no_mem; } @@ -641,7 +669,7 @@ ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ if( pList->nAlloc<=pList->nExpr ){ struct ExprList_item *a; int n = pList->nAlloc*2 + 4; - a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); + a = sqlite3_realloc(pList->a, n*sizeof(pList->a[0])); if( a==0 ){ goto no_mem; } @@ -652,13 +680,14 @@ ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ if( pExpr || pName ){ struct ExprList_item *pItem = &pList->a[pList->nExpr++]; memset(pItem, 0, sizeof(*pItem)); - pItem->zName = sqlite3NameFromToken(pName); + pItem->zName = sqlite3NameFromToken(db, pName); pItem->pExpr = pExpr; } return pList; no_mem: /* Avoid leaking memory if malloc has failed. */ + db->mallocFailed = 1; sqlite3ExprDelete(pExpr); sqlite3ExprListDelete(pList); return 0; @@ -756,10 +785,10 @@ void sqlite3ExprListDelete(ExprList *pList){ assert( pList->nExpr<=pList->nAlloc ); for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ sqlite3ExprDelete(pItem->pExpr); - sqliteFree(pItem->zName); + sqlite3_free(pItem->zName); } - sqliteFree(pList->a); - sqliteFree(pList); + sqlite3_free(pList->a); + sqlite3_free(pList); } /* @@ -996,10 +1025,10 @@ static int lookupName( NameContext *pTopNC = pNC; /* First namecontext in the list */ assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ - zDb = sqlite3NameFromToken(pDbToken); - zTab = sqlite3NameFromToken(pTableToken); - zCol = sqlite3NameFromToken(pColumnToken); - if( sqlite3MallocFailed() ){ + zDb = sqlite3NameFromToken(db, pDbToken); + zTab = sqlite3NameFromToken(db, pTableToken); + zCol = sqlite3NameFromToken(db, pColumnToken); + if( db->mallocFailed ){ goto lookupname_end; } @@ -1147,7 +1176,7 @@ static int lookupName( pOrig = pEList->a[j].pExpr; if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); - sqliteFree(zCol); + sqlite3_free(zCol); return 2; } pDup = sqlite3ExprDup(pOrig); @@ -1155,10 +1184,10 @@ static int lookupName( pDup->pColl = pExpr->pColl; pDup->flags |= EP_ExpCollate; } - if( pExpr->span.dyn ) sqliteFree((char*)pExpr->span.z); - if( pExpr->token.dyn ) sqliteFree((char*)pExpr->token.z); + if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z); + if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z); memcpy(pExpr, pDup, sizeof(*pExpr)); - sqliteFree(pDup); + sqlite3_free(pDup); cnt = 1; pMatch = 0; assert( zTab==0 && zDb==0 ); @@ -1186,7 +1215,7 @@ static int lookupName( ** fields are not changed in any context. */ if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ - sqliteFree(zCol); + sqlite3_free(zCol); return 0; } @@ -1203,10 +1232,10 @@ static int lookupName( }else if( zTab ){ sqlite3SetString(&z, zTab, ".", zCol, (char*)0); }else{ - z = sqliteStrDup(zCol); + z = sqlite3StrDup(zCol); } sqlite3ErrorMsg(pParse, zErr, z); - sqliteFree(z); + sqlite3_free(z); pTopNC->nErr++; } @@ -1228,15 +1257,15 @@ static int lookupName( lookupname_end: /* Clean up and return */ - sqliteFree(zDb); - sqliteFree(zTab); + sqlite3_free(zDb); + sqlite3_free(zTab); sqlite3ExprDelete(pExpr->pLeft); pExpr->pLeft = 0; sqlite3ExprDelete(pExpr->pRight); pExpr->pRight = 0; pExpr->op = TK_COLUMN; lookupname_end_2: - sqliteFree(zCol); + sqlite3_free(zCol); if( cnt==1 ){ assert( pNC!=0 ); sqlite3AuthRead(pParse, pExpr, pNC->pSrcList); @@ -1527,7 +1556,7 @@ void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ int mem = pParse->nMem++; sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0); - assert( testAddr>0 || sqlite3MallocFailed() ); + assert( testAddr>0 || pParse->db->mallocFailed ); sqlite3VdbeAddOp(v, OP_MemInt, 1, mem); } @@ -1660,7 +1689,7 @@ void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ ** text z[0..n-1] on the stack. */ static void codeInteger(Vdbe *v, const char *z, int n){ - assert( z || sqlite3MallocFailed() ); + assert( z || v==0 || sqlite3DbOfVdbe(v)->mallocFailed ); if( z ){ int i; if( sqlite3GetInt32(z, &i) ){ @@ -1860,7 +1889,7 @@ void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ }else{ codeInteger(v, z, p->n+1); } - sqliteFree(z); + sqlite3_free(z); break; } /* Fall through into TK_NOT */ @@ -1906,8 +1935,10 @@ void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ const char *zId; int constMask = 0; int i; - u8 enc = ENC(pParse->db); + sqlite3 *db = pParse->db; + u8 enc = ENC(db); CollSeq *pColl = 0; + zId = (char*)pExpr->token.z; nId = pExpr->token.n; pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); @@ -1927,9 +1958,9 @@ void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ ** for function overloading. But we use the B term in "glob(B,A)". */ if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ - pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr); + pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); }else if( nExpr>0 ){ - pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr); + pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); } #endif for(i=0; i<nExpr && i<32; i++){ @@ -2389,9 +2420,10 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB){ ** Add a new element to the pAggInfo->aCol[] array. Return the index of ** the new element. Return a negative number if malloc fails. */ -static int addAggInfoColumn(AggInfo *pInfo){ +static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ int i; pInfo->aCol = sqlite3ArrayAllocate( + db, pInfo->aCol, sizeof(pInfo->aCol[0]), 3, @@ -2406,9 +2438,10 @@ static int addAggInfoColumn(AggInfo *pInfo){ ** Add a new element to the pAggInfo->aFunc[] array. Return the index of ** the new element. Return a negative number if malloc fails. */ -static int addAggInfoFunc(AggInfo *pInfo){ +static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ int i; pInfo->aFunc = sqlite3ArrayAllocate( + db, pInfo->aFunc, sizeof(pInfo->aFunc[0]), 3, diff --git a/src/func.c b/src/func.c index 2a7019d79..18cd74a1f 100644 --- a/src/func.c +++ b/src/func.c @@ -16,7 +16,7 @@ ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: func.c,v 1.163 2007/07/26 06:50:06 danielk1977 Exp $ +** $Id: func.c,v 1.164 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -334,10 +334,10 @@ static void randomBlob( sqlite3_result_error_toobig(context); return; } - p = sqliteMalloc(n); + p = sqlite3_malloc(n); if( p ){ sqlite3Randomness(n, p); - sqlite3_result_blob(context, (char*)p, n, sqlite3FreeX); + sqlite3_result_blob(context, (char*)p, n, sqlite3_free); } } @@ -667,7 +667,7 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ sqlite3_result_error_toobig(context); return; } - zText = (char *)sqliteMalloc((2*nBlob)+4); + zText = (char *)sqlite3_malloc((2*nBlob)+4); if( !zText ){ sqlite3_result_error(context, "out of memory", -1); }else{ @@ -681,7 +681,7 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ zText[0] = 'X'; zText[1] = '\''; sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); - sqliteFree(zText); + sqlite3_free(zText); } break; } @@ -697,7 +697,7 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ sqlite3_result_error_toobig(context); return; } - z = sqliteMalloc( i+n+3 ); + z = sqlite3_malloc( i+n+3 ); if( z==0 ) return; z[0] = '\''; for(i=0, j=1; zArg[i]; i++){ @@ -709,7 +709,7 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ z[j++] = '\''; z[j] = 0; sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); - sqliteFree(z); + sqlite3_free(z); } } } @@ -1049,7 +1049,7 @@ static void destructor(void *p){ char *zVal = (char *)p; assert(zVal); zVal--; - sqliteFree(zVal); + sqlite3_free(zVal); test_destructor_count_var--; } static void test_destructor( @@ -1102,7 +1102,7 @@ static void test_destructor_count( ** registration, the result for that argument is 1. The overall result ** is the individual argument results separated by spaces. */ -static void free_test_auxdata(void *p) {sqliteFree(p);} +static void free_test_auxdata(void *p) {sqlite3_free(p);} static void test_auxdata( sqlite3_context *pCtx, int nArg, @@ -1124,7 +1124,7 @@ static void test_auxdata( } }else{ zRet[i*2] = '0'; - zAux = sqliteStrDup(z); + zAux = sqlite3StrDup(z); sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); } zRet[i*2+1] = ' '; diff --git a/src/hash.c b/src/hash.c index 86d999502..5154752a1 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.19 2007/03/31 03:59:24 drh Exp $ +** $Id: hash.c,v 1.20 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include <assert.h> @@ -41,8 +41,6 @@ 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. @@ -55,15 +53,15 @@ void sqlite3HashClear(Hash *pH){ assert( pH!=0 ); elem = pH->first; pH->first = 0; - if( pH->ht ) pH->xFree(pH->ht); + if( pH->ht ) sqlite3_free(pH->ht); pH->ht = 0; pH->htsize = 0; while( elem ){ HashElem *next_elem = elem->next; if( pH->copyKey && elem->pKey ){ - pH->xFree(elem->pKey); + sqlite3_free(elem->pKey); } - pH->xFree(elem); + sqlite3_free(elem); elem = next_elem; } pH->count = 0; @@ -216,7 +214,7 @@ static void insertElement( /* Resize the hash table so that it cantains "new_size" buckets. ** "new_size" must be a power of 2. The hash table might fail -** to resize if sqliteMalloc() fails. +** to resize if sqlite3_malloc() fails. */ static void rehash(Hash *pH, int new_size){ struct _ht *new_ht; /* The new hash table */ @@ -224,9 +222,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 *)pH->xMalloc( new_size*sizeof(struct _ht) ); + new_ht = (struct _ht *)sqlite3_malloc( new_size*sizeof(struct _ht) ); if( new_ht==0 ) return; - if( pH->ht ) pH->xFree(pH->ht); + if( pH->ht ) sqlite3_free(pH->ht); pH->ht = new_ht; pH->htsize = new_size; xHash = hashFunction(pH->keyClass); @@ -292,9 +290,9 @@ static void removeElementGivenHash( pEntry->chain = 0; } if( pH->copyKey ){ - pH->xFree(elem->pKey); + sqlite3_free(elem->pKey); } - pH->xFree( elem ); + sqlite3_free( elem ); pH->count--; if( pH->count<=0 ){ assert( pH->first==0 ); @@ -360,12 +358,12 @@ void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ return old_data; } if( data==0 ) return 0; - new_elem = (HashElem*)pH->xMalloc( sizeof(HashElem) ); + new_elem = (HashElem*)sqlite3_malloc( sizeof(HashElem) ); if( new_elem==0 ) return data; if( pH->copyKey && pKey!=0 ){ - new_elem->pKey = pH->xMalloc( nKey ); + new_elem->pKey = sqlite3_malloc( nKey ); if( new_elem->pKey==0 ){ - pH->xFree(new_elem); + sqlite3_free(new_elem); return data; } memcpy((void*)new_elem->pKey, pKey, nKey); @@ -379,9 +377,9 @@ void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, void *data){ if( pH->htsize==0 ){ pH->count = 0; if( pH->copyKey ){ - pH->xFree(new_elem->pKey); + sqlite3_free(new_elem->pKey); } - pH->xFree(new_elem); + sqlite3_free(new_elem); return data; } } diff --git a/src/hash.h b/src/hash.h index 78f60b4b9..275ac7545 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.9 2006/02/14 10:48:39 danielk1977 Exp $ +** $Id: hash.h,v 1.10 2007/08/16 04:30:40 drh Exp $ */ #ifndef _SQLITE_HASH_H_ #define _SQLITE_HASH_H_ @@ -33,10 +33,8 @@ struct Hash { char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */ 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 */ + HashElem *first; /* The first element of the array */ struct _ht { /* the hash table */ int count; /* Number of entries with this hash */ HashElem *chain; /* Pointer to first entry with this hash */ diff --git a/src/insert.c b/src/insert.c index ba9426d61..542cfb761 100644 --- a/src/insert.c +++ b/src/insert.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. ** -** $Id: insert.c,v 1.188 2007/07/23 19:39:47 drh Exp $ +** $Id: insert.c,v 1.189 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" @@ -41,7 +41,8 @@ void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ */ int n; Table *pTab = pIdx->pTable; - pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1); + sqlite3 *db = sqlite3DbOfVdbe(v); + pIdx->zColAff = (char *)sqlite3DbMallocZero(db, pIdx->nColumn+1); if( !pIdx->zColAff ){ return; } @@ -79,8 +80,9 @@ void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ if( !pTab->zColAff ){ char *zColAff; int i; + sqlite3 *db = sqlite3DbOfVdbe(v); - zColAff = (char *)sqliteMalloc(pTab->nCol+1); + zColAff = (char *)sqlite3DbMallocZero(db, pTab->nCol+1); if( !zColAff ){ return; } @@ -356,10 +358,10 @@ void sqlite3Insert( int triggers_exist = 0; /* True if there are FOR EACH ROW triggers */ #endif - if( pParse->nErr || sqlite3MallocFailed() ){ + db = pParse->db; + if( pParse->nErr || db->mallocFailed ){ goto insert_cleanup; } - db = pParse->db; /* Locate the table into which we will be inserting new information. */ @@ -462,7 +464,7 @@ void sqlite3Insert( /* Resolve the expressions in the SELECT statement and execute it. */ rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0); - if( rc || pParse->nErr || sqlite3MallocFailed() ){ + if( rc || pParse->nErr || db->mallocFailed ){ goto insert_cleanup; } diff --git a/src/legacy.c b/src/legacy.c index 8d96b1e58..5dff806d1 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.18 2007/05/04 13:15:56 drh Exp $ +** $Id: legacy.c,v 1.19 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" @@ -66,7 +66,7 @@ int sqlite3_exec( nCallback = 0; nCol = sqlite3_column_count(pStmt); - azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1); + azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char *) + 1); if( azCols==0 ){ goto exec_out; } @@ -108,13 +108,13 @@ int sqlite3_exec( } } - sqliteFree(azCols); + sqlite3_free(azCols); azCols = 0; } exec_out: if( pStmt ) sqlite3_finalize(pStmt); - if( azCols ) sqliteFree(azCols); + if( azCols ) sqlite3_free(azCols); rc = sqlite3ApiExit(0, rc); if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){ diff --git a/src/loadext.c b/src/loadext.c index b2f4726cd..de085c7f1 100644 --- a/src/loadext.c +++ b/src/loadext.c @@ -316,14 +316,14 @@ int sqlite3_load_extension( /* Append the new shared library handle to the db->aExtension array. */ db->nExtension++; - aHandle = sqliteMalloc(sizeof(handle)*db->nExtension); + aHandle = sqlite3DbMallocZero(db, sizeof(handle)*db->nExtension); if( aHandle==0 ){ return SQLITE_NOMEM; } if( db->nExtension>0 ){ memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1)); } - sqliteFree(db->aExtension); + sqlite3_free(db->aExtension); db->aExtension = aHandle; db->aExtension[db->nExtension-1] = handle; @@ -339,7 +339,7 @@ void sqlite3CloseExtensions(sqlite3 *db){ for(i=0; i<db->nExtension; i++){ sqlite3OsDlclose(db->aExtension[i]); } - sqliteFree(db->aExtension); + sqlite3_free(db->aExtension); } /* @@ -378,7 +378,7 @@ int sqlite3_auto_extension(void *xInit){ } if( i==nAutoExtension ){ nAutoExtension++; - aAutoExtension = sqlite3Realloc( aAutoExtension, + aAutoExtension = sqlite3_realloc( aAutoExtension, nAutoExtension*sizeof(aAutoExtension[0]) ); if( aAutoExtension==0 ){ nAutoExtension = 0; @@ -397,7 +397,7 @@ int sqlite3_auto_extension(void *xInit){ */ void sqlite3_reset_auto_extension(void){ sqlite3OsEnterMutex(); - sqliteFree(aAutoExtension); + sqlite3_free(aAutoExtension); aAutoExtension = 0; nAutoExtension = 0; sqlite3OsLeaveMutex(); diff --git a/src/main.c b/src/main.c index b797f774f..eebe1692d 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.379 2007/08/15 13:04:54 drh Exp $ +** $Id: main.c,v 1.380 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -176,7 +176,7 @@ int sqlite3_close(sqlite3 *db){ FuncDef *pFunc, *pNext; for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ pNext = pFunc->pNext; - sqliteFree(pFunc); + sqlite3_free(pFunc); } } @@ -188,7 +188,7 @@ int sqlite3_close(sqlite3 *db){ pColl[j].xDel(pColl[j].pUser); } } - sqliteFree(pColl); + sqlite3_free(pColl); } sqlite3HashClear(&db->aCollSeq); #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -197,7 +197,7 @@ int sqlite3_close(sqlite3 *db){ if( pMod->xDestroy ){ pMod->xDestroy(pMod->pAux); } - sqliteFree(pMod); + sqlite3_free(pMod); } sqlite3HashClear(&db->aModule); #endif @@ -217,8 +217,8 @@ int sqlite3_close(sqlite3 *db){ ** the same sqliteMalloc() as the one that allocates the database ** structure? */ - sqliteFree(db->aDb[1].pSchema); - sqliteFree(db); + sqlite3_free(db->aDb[1].pSchema); + sqlite3_free(db); sqlite3ReleaseThreadData(); return SQLITE_OK; } @@ -488,7 +488,7 @@ int sqlite3CreateFunc( if( db->activeVdbeCnt ){ sqlite3Error(db, SQLITE_BUSY, "Unable to delete/modify user-function due to active statements"); - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); return SQLITE_BUSY; }else{ sqlite3ExpirePreparedStatements(db); @@ -521,7 +521,7 @@ int sqlite3_create_function( void (*xFinal)(sqlite3_context*) ){ int rc; - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); return sqlite3ApiExit(db, rc); @@ -540,11 +540,11 @@ int sqlite3_create_function16( ){ int rc; char *zFunc8; - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); zFunc8 = sqlite3Utf16to8(zFunctionName, -1); rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); - sqliteFree(zFunc8); + sqlite3_free(zFunc8); return sqlite3ApiExit(db, rc); } @@ -731,10 +731,10 @@ int sqlite3BtreeFactory( */ const char *sqlite3_errmsg(sqlite3 *db){ const char *z; - assert( !sqlite3MallocFailed() ); if( !db ){ return sqlite3ErrStr(SQLITE_NOMEM); } + assert( !db->mallocFailed ); if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ return sqlite3ErrStr(SQLITE_MISUSE); } @@ -771,7 +771,7 @@ const void *sqlite3_errmsg16(sqlite3 *db){ }; const void *z; - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); if( !db ){ return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); } @@ -794,7 +794,7 @@ const void *sqlite3_errmsg16(sqlite3 *db){ ** passed to this function, we assume a malloc() failed during sqlite3_open(). */ int sqlite3_errcode(sqlite3 *db){ - if( !db || sqlite3MallocFailed() ){ + if( !db || db->mallocFailed ){ return SQLITE_NOMEM; } if( sqlite3SafetyCheck(db) ){ @@ -895,10 +895,8 @@ static int openDatabase( int rc; CollSeq *pColl; - assert( !sqlite3MallocFailed() ); - /* Allocate the sqlite data structure */ - db = sqliteMalloc( sizeof(sqlite3) ); + db = sqlite3MallocZero( sizeof(sqlite3) ); if( db==0 ) goto opendb_out; db->errMask = 0xff; db->priorNewRowid = 0; @@ -929,7 +927,7 @@ static int openDatabase( createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) || (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 ){ - assert( sqlite3MallocFailed() ); + assert( db->mallocFailed ); db->magic = SQLITE_MAGIC_CLOSED; goto opendb_out; } @@ -952,8 +950,8 @@ static int openDatabase( db->magic = SQLITE_MAGIC_CLOSED; goto opendb_out; } - db->aDb[0].pSchema = sqlite3SchemaGet(db->aDb[0].pBt); - db->aDb[1].pSchema = sqlite3SchemaGet(0); + db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); + db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); /* The default safety_level for the main database is 'full'; for the temp @@ -967,7 +965,7 @@ static int openDatabase( #endif db->magic = SQLITE_MAGIC_OPEN; - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ goto opendb_out; } @@ -987,21 +985,21 @@ static int openDatabase( } #ifdef SQLITE_ENABLE_FTS1 - if( !sqlite3MallocFailed() ){ + if( !db->mallocFailed ){ extern int sqlite3Fts1Init(sqlite3*); rc = sqlite3Fts1Init(db); } #endif #ifdef SQLITE_ENABLE_FTS2 - if( !sqlite3MallocFailed() && rc==SQLITE_OK ){ + if( !db->mallocFailed && rc==SQLITE_OK ){ extern int sqlite3Fts2Init(sqlite3*); rc = sqlite3Fts2Init(db); } #endif #ifdef SQLITE_ENABLE_ICU - if( !sqlite3MallocFailed() && rc==SQLITE_OK ){ + if( !db->mallocFailed && rc==SQLITE_OK ){ extern int sqlite3IcuInit(sqlite3*); rc = sqlite3IcuInit(db); } @@ -1121,7 +1119,7 @@ int sqlite3_create_collation( int(*xCompare)(void*,int,const void*,int,const void*) ){ int rc; - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); rc = createCollation(db, zName, enc, pCtx, xCompare, 0); return sqlite3ApiExit(db, rc); } @@ -1138,7 +1136,7 @@ int sqlite3_create_collation_v2( void(*xDel)(void*) ){ int rc; - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); rc = createCollation(db, zName, enc, pCtx, xCompare, xDel); return sqlite3ApiExit(db, rc); } @@ -1156,11 +1154,11 @@ int sqlite3_create_collation16( ){ int rc = SQLITE_OK; char *zName8; - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); zName8 = sqlite3Utf16to8(zName, -1); if( zName8 ){ rc = createCollation(db, zName8, enc, pCtx, xCompare, 0); - sqliteFree(zName8); + sqlite3_free(zName8); } return sqlite3ApiExit(db, rc); } @@ -1386,7 +1384,7 @@ error_out: rc = SQLITE_ERROR; } sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); - sqliteFree(zErrMsg); + sqlite3_free(zErrMsg); return sqlite3ApiExit(db, rc); } #endif diff --git a/src/malloc.c b/src/malloc.c index 8a993326b..e0a678eae 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -12,7 +12,7 @@ ** Memory allocation functions used throughout sqlite. ** ** -** $Id: malloc.c,v 1.4 2007/08/08 01:04:52 drh Exp $ +** $Id: malloc.c,v 1.5 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -20,52 +20,6 @@ #include <ctype.h> /* -** MALLOC WRAPPER ARCHITECTURE -** -** The sqlite code accesses dynamic memory allocation/deallocation by invoking -** the following six APIs (which may be implemented as macros). -** -** sqlite3Malloc() -** sqlite3MallocRaw() -** sqlite3Realloc() -** sqlite3ReallocOrFree() -** sqlite3Free() -** sqlite3AllocSize() -** -** The function sqlite3FreeX performs the same task as sqlite3Free and is -** guaranteed to be a real function. The same holds for sqlite3MallocX -** -** The above APIs are implemented in terms of the functions provided in the -** operating-system interface. The OS interface is never accessed directly -** by code outside of this file. -** -** sqlite3OsMalloc() -** sqlite3OsRealloc() -** sqlite3OsFree() -** sqlite3OsAllocationSize() -** -** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke -** sqlite3_release_memory() if a call to sqlite3OsMalloc() or -** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is -** exceeded). Function sqlite3Malloc() usually invokes -** sqlite3MallocRaw(). -** -** MALLOC TEST WRAPPER ARCHITECTURE -** -** The test wrapper provides extra test facilities to ensure the library -** does not leak memory and handles the failure of the underlying OS level -** allocation system correctly. It is only present if the library is -** compiled with the SQLITE_MEMDEBUG macro set. -** -** * Guardposts to detect overwrites. -** * Ability to cause a specific Malloc() or Realloc() to fail. -** * Audit outstanding memory allocations (i.e check for leaks). -*/ - -#define MAX(x,y) ((x)>(y)?(x):(y)) - -#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) -/* ** Set the soft heap-size limit for the current thread. Passing a negative ** value indicates no limit. */ @@ -83,637 +37,59 @@ void sqlite3_soft_heap_limit(int n){ int sqlite3_release_memory(int n){ return sqlite3PagerReleaseMemory(n); } -#else -/* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version -** of sqlite3_release_memory() to be used by other code in this file. -** This is done for no better reason than to reduce the number of -** pre-processor #ifndef statements. -*/ -#define sqlite3_release_memory(x) 0 /* 0 == no memory freed */ -#endif - -#ifdef SQLITE_MEMDEBUG -/*-------------------------------------------------------------------------- -** Begin code for memory allocation system test layer. -** -** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro. -** -** SQLITE_MEMDEBUG==1 -> Fence-posting only (thread safe) -** SQLITE_MEMDEBUG==2 -> Fence-posting + linked list of allocations (not ts) -** SQLITE_MEMDEBUG==3 -> Above + backtraces (not thread safe, req. glibc) -*/ -/* Figure out whether or not to store backtrace() information for each malloc. -** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or -** greater and glibc is in use. If we don't want to use backtrace(), then just -** define it as an empty macro and set the amount of space reserved to 0. -*/ -#if defined(__GLIBC__) && SQLITE_MEMDEBUG>2 - extern int backtrace(void **, int); - #define TESTALLOC_STACKSIZE 128 - #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*)) -#else - #define backtrace(x, y) - #define TESTALLOC_STACKSIZE 0 - #define TESTALLOC_STACKFRAMES 0 -#endif /* -** Number of 32-bit guard words. This should probably be a multiple of -** 2 since on 64-bit machines we want the value returned by sqliteMalloc() -** to be 8-byte aligned. -*/ -#ifndef TESTALLOC_NGUARD -# define TESTALLOC_NGUARD 2 -#endif - -/* -** Size reserved for storing file-name along with each malloc()ed blob. -*/ -#define TESTALLOC_FILESIZE 64 - -/* -** Size reserved for storing the user string. Each time a Malloc() or Realloc() -** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by -** sqlite3_malloc_id are stored along with the other test system metadata. -*/ -#define TESTALLOC_USERSIZE 64 -const char *sqlite3_malloc_id = 0; - -/* -** Blocks used by the test layer have the following format: -** -** <sizeof(void *) pNext pointer> -** <sizeof(void *) pPrev pointer> -** <TESTALLOC_NGUARD 32-bit guard words> -** <The application level allocation> -** <TESTALLOC_NGUARD 32-bit guard words> -** <32-bit line number> -** <TESTALLOC_FILESIZE bytes containing null-terminated file name> -** <TESTALLOC_STACKSIZE bytes of backtrace() output> +** Allocate and zero memory. */ - -#define TESTALLOC_OFFSET_GUARD1(p) (sizeof(void *) * 2) -#define TESTALLOC_OFFSET_DATA(p) ( \ - TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \ -) -#define TESTALLOC_OFFSET_GUARD2(p) ( \ - TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \ -) -#define TESTALLOC_OFFSET_LINENUMBER(p) ( \ - TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \ -) -#define TESTALLOC_OFFSET_FILENAME(p) ( \ - TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \ -) -#define TESTALLOC_OFFSET_USER(p) ( \ - TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \ -) -#define TESTALLOC_OFFSET_STACK(p) ( \ - TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \ - (TESTALLOC_OFFSET_USER(p) % 8) \ -) - -#define TESTALLOC_OVERHEAD ( \ - sizeof(void *)*2 + /* pPrev and pNext pointers */ \ - TESTALLOC_NGUARD*sizeof(u32)*2 + /* Guard words */ \ - sizeof(u32) + TESTALLOC_FILESIZE + /* File and line number */ \ - TESTALLOC_USERSIZE + /* User string */ \ - TESTALLOC_STACKSIZE /* backtrace() stack */ \ -) - - -/* -** For keeping track of the number of mallocs and frees. This -** is used to check for memory leaks. The iMallocFail and iMallocReset -** values are used to simulate malloc() failures during testing in -** order to verify that the library correctly handles an out-of-memory -** condition. -*/ -int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ -int sqlite3_nFree; /* Number of sqliteFree() calls */ -int sqlite3_memUsed; /* TODO Total memory obtained from malloc */ -int sqlite3_memMax; /* TODO Mem usage high-water mark */ -int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ -int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */ - -void *sqlite3_pFirst = 0; /* Pointer to linked list of allocations */ -int sqlite3_nMaxAlloc = 0; /* High water mark of ThreadData.nAlloc */ -int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */ -int sqlite3_isFail = 0; /* True if all malloc calls should fail */ -const char *sqlite3_zFile = 0; /* Filename to associate debug info with */ -int sqlite3_iLine = 0; /* Line number for debug info */ -int sqlite3_mallocfail_trace = 0; /* Print a msg on malloc fail if true */ - -/* -** Check for a simulated memory allocation failure. Return true if -** the failure should be simulated. Return false to proceed as normal. -*/ -int sqlite3TestMallocFail(){ - if( sqlite3_isFail ){ - return 1; - } - if( sqlite3_iMallocFail>=0 ){ - sqlite3_iMallocFail--; - if( sqlite3_iMallocFail==0 ){ - sqlite3_iMallocFail = sqlite3_iMallocReset; - sqlite3_isFail = 1; - if( sqlite3_mallocfail_trace ){ - sqlite3DebugPrintf("###_malloc_fails_###\n"); - } - return 1; - } - } - return 0; -} - -/* -** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc(). -** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the -** values set by the applyGuards() function. -*/ -static void checkGuards(u32 *p) -{ - int i; - char *zAlloc = (char *)p; - char *z; - - /* First set of guard words */ - z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)]; - for(i=0; i<TESTALLOC_NGUARD; i++){ - assert(((u32 *)z)[i]==0xdead1122); - } - - /* Second set of guard words */ - z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)]; - for(i=0; i<TESTALLOC_NGUARD; i++){ - u32 guard = 0; - memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32)); - assert(guard==0xdead3344); - } -} - -/* -** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The -** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as -** guard-posts. -*/ -static void applyGuards(u32 *p) -{ - int i; - char *z; - char *zAlloc = (char *)p; - - /* First set of guard words */ - z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)]; - for(i=0; i<TESTALLOC_NGUARD; i++){ - ((u32 *)z)[i] = 0xdead1122; - } - - /* Second set of guard words */ - z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)]; - for(i=0; i<TESTALLOC_NGUARD; i++){ - static const int guard = 0xdead3344; - memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32)); - } - - /* Line number */ - z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)]; /* Guard words */ - z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)]; - memcpy(z, &sqlite3_iLine, sizeof(u32)); - - /* File name */ - z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)]; - strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE); - z[TESTALLOC_FILESIZE - 1] = '\0'; - - /* User string */ - z = &zAlloc[TESTALLOC_OFFSET_USER(p)]; - z[0] = 0; - if( sqlite3_malloc_id ){ - strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE); - z[TESTALLOC_USERSIZE-1] = 0; - } - - /* backtrace() stack */ - z = &zAlloc[TESTALLOC_OFFSET_STACK(p)]; - backtrace((void **)z, TESTALLOC_STACKFRAMES); - - /* Sanity check to make sure checkGuards() is working */ - checkGuards(p); -} - -/* -** The argument is a malloc()ed pointer as returned by the test-wrapper. -** Return a pointer to the Os level allocation. -*/ -static void *getOsPointer(void *p) -{ - char *z = (char *)p; - return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]); -} - - -#if SQLITE_MEMDEBUG>1 -/* -** The argument points to an Os level allocation. Link it into the threads list -** of allocations. -*/ -static void linkAlloc(void *p){ - void **pp = (void **)p; - pp[0] = 0; - pp[1] = sqlite3_pFirst; - if( sqlite3_pFirst ){ - ((void **)sqlite3_pFirst)[0] = p; - } - sqlite3_pFirst = p; -} - -/* -** The argument points to an Os level allocation. Unlinke it from the threads -** list of allocations. -*/ -static void unlinkAlloc(void *p) -{ - void **pp = (void **)p; - if( p==sqlite3_pFirst ){ - assert(!pp[0]); - assert(!pp[1] || ((void **)(pp[1]))[0]==p); - sqlite3_pFirst = pp[1]; - if( sqlite3_pFirst ){ - ((void **)sqlite3_pFirst)[0] = 0; - } - }else{ - void **pprev = pp[0]; - void **pnext = pp[1]; - assert(pprev); - assert(pprev[1]==p); - pprev[1] = (void *)pnext; - if( pnext ){ - assert(pnext[0]==p); - pnext[0] = (void *)pprev; - } - } -} - -/* -** Pointer p is a pointer to an OS level allocation that has just been -** realloc()ed. Set the list pointers that point to this entry to it's new -** location. -*/ -static void relinkAlloc(void *p) -{ - void **pp = (void **)p; - if( pp[0] ){ - ((void **)(pp[0]))[1] = p; - }else{ - sqlite3_pFirst = p; - } - if( pp[1] ){ - ((void **)(pp[1]))[0] = p; - } -} -#else -#define linkAlloc(x) -#define relinkAlloc(x) -#define unlinkAlloc(x) -#endif - -/* -** This function sets the result of the Tcl interpreter passed as an argument -** to a list containing an entry for each currently outstanding call made to -** sqliteMalloc and friends by the current thread. Each list entry is itself a -** list, consisting of the following (in order): -** -** * The number of bytes allocated -** * The __FILE__ macro at the time of the sqliteMalloc() call. -** * The __LINE__ macro ... -** * The value of the sqlite3_malloc_id variable ... -** * The output of backtrace() (if available) ... -** -** Todo: We could have a version of this function that outputs to stdout, -** to debug memory leaks when Tcl is not available. -*/ -#if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1 -#include <tcl.h> -int sqlite3OutstandingMallocs(Tcl_Interp *interp){ - void *p; - Tcl_Obj *pRes = Tcl_NewObj(); - Tcl_IncrRefCount(pRes); - - - for(p=sqlite3_pFirst; p; p=((void **)p)[1]){ - Tcl_Obj *pEntry = Tcl_NewObj(); - Tcl_Obj *pStack = Tcl_NewObj(); - char *z; - u32 iLine; - int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD; - char *zAlloc = (char *)p; - int i; - - Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes)); - - z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)]; - Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1)); - - z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)]; - memcpy(&iLine, z, sizeof(u32)); - Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine)); - - z = &zAlloc[TESTALLOC_OFFSET_USER(p)]; - Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1)); - - z = &zAlloc[TESTALLOC_OFFSET_STACK(p)]; - for(i=0; i<TESTALLOC_STACKFRAMES; i++){ - char zHex[128]; - sqlite3_snprintf(sizeof(zHex), zHex, "%p", ((void **)z)[i]); - Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1)); - } - - Tcl_ListObjAppendElement(0, pEntry, pStack); - Tcl_ListObjAppendElement(0, pRes, pEntry); - } - - Tcl_ResetResult(interp); - Tcl_SetObjResult(interp, pRes); - Tcl_DecrRefCount(pRes); - return TCL_OK; -} -#endif - -/* -** 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); -#endif - assert( !sqlite3_mallocDisallowed ); - if( !sqlite3TestMallocFail() ){ - u32 *p; - p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD); - assert(p); - sqlite3_nMalloc++; - applyGuards(p); - linkAlloc(p); - sqlite3OsLeaveMutex(); - return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); - } - sqlite3OsLeaveMutex(); - return 0; -} - -static int OSSIZEOF(void *p){ +void *sqlite3MallocZero(unsigned n){ + void *p = sqlite3_malloc(n); if( p ){ - u32 *pOs = (u32 *)getOsPointer(p); - return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD; - } - return 0; -} - -/* -** This is the test layer's wrapper around sqlite3OsFree(). The argument is a -** pointer to the space allocated for the application to use. -*/ -static void OSFREE(void *pFree){ - u32 *p; /* Pointer to the OS-layer allocation */ - sqlite3OsEnterMutex(); - p = (u32 *)getOsPointer(pFree); - checkGuards(p); - unlinkAlloc(p); - memset(pFree, 0x55, OSSIZEOF(pFree)); - sqlite3OsFree(p); - sqlite3_nFree++; - sqlite3OsLeaveMutex(); -} - -/* -** This is the test layer's wrapper around sqlite3OsRealloc(). -*/ -static void * OSREALLOC(void *pRealloc, int n){ -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - sqlite3_nMaxAlloc = - MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc); -#endif - assert( !sqlite3_mallocDisallowed ); - if( !sqlite3TestMallocFail() ){ - u32 *p = (u32 *)getOsPointer(pRealloc); - checkGuards(p); - p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD); - applyGuards(p); - relinkAlloc(p); - return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); - } - return 0; -} - -static void OSMALLOC_FAILED(){ - sqlite3_isFail = 0; -} - -#else -/* Define macros to call the sqlite3OsXXX interface directly if -** the SQLITE_MEMDEBUG macro is not defined. -*/ -#define OSMALLOC(x) sqlite3OsMalloc(x) -#define OSREALLOC(x,y) sqlite3OsRealloc(x,y) -#define OSFREE(x) sqlite3OsFree(x) -#define OSSIZEOF(x) sqlite3OsAllocationSize(x) -#define OSMALLOC_FAILED() - -#endif /* SQLITE_MEMDEBUG */ -/* -** End code for memory allocation system test layer. -**--------------------------------------------------------------------------*/ - -/* -** This routine is called when we are about to allocate n additional bytes -** of memory. If the new allocation will put is over the soft allocation -** limit, then invoke sqlite3_release_memory() to try to release some -** memory before continuing with the allocation. -** -** This routine also makes sure that the thread-specific-data (TSD) has -** be allocated. If it has not and can not be allocated, then return -** false. The updateMemoryUsedCount() routine below will deallocate -** the TSD if it ought to be. -** -** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is -** a no-op -*/ -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT -static int enforceSoftLimit(int n){ - ThreadData *pTsd = sqlite3ThreadData(); - if( pTsd==0 ){ - return 0; - } - assert( pTsd->nAlloc>=0 ); - if( n>0 && pTsd->nSoftHeapLimit>0 ){ - while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){} - } - return 1; -} -#else -# define enforceSoftLimit(X) 1 -#endif - -/* -** Update the count of total outstanding memory that is held in -** thread-specific-data (TSD). If after this update the TSD is -** no longer being used, then deallocate it. -** -** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is -** a no-op -*/ -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT -static void updateMemoryUsedCount(int n){ - ThreadData *pTsd = sqlite3ThreadData(); - if( pTsd ){ - pTsd->nAlloc += n; - assert( pTsd->nAlloc>=0 ); - if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){ - sqlite3ReleaseThreadData(); - } - } -} -#else -#define updateMemoryUsedCount(x) /* no-op */ -#endif - -/* -** Allocate and return N bytes of uninitialised memory by calling -** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory -** by calling sqlite3_release_memory(). -*/ -void *sqlite3MallocRaw(int n, int doMemManage){ - void *p = 0; - if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){ - while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){} - if( !p ){ - sqlite3FailedMalloc(); - OSMALLOC_FAILED(); - }else if( doMemManage ){ - updateMemoryUsedCount(OSSIZEOF(p)); - } + memset(p, 0, n); } return p; } /* -** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The -** pointer to the new allocation is returned. If the Realloc() call fails, -** attempt to free memory by calling sqlite3_release_memory(). +** Allocate and zero memory. If the allocation fails, make +** the mallocFailed flag in the connection pointer. */ -void *sqlite3Realloc(void *p, int n){ - if( sqlite3MallocFailed() ){ - return 0; - } - - if( !p ){ - return sqlite3Malloc(n, 1); +void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){ + void *p = sqlite3_malloc(n); + if( p ){ + memset(p, 0, n); }else{ - void *np = 0; -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - int origSize = OSSIZEOF(p); -#endif - if( enforceSoftLimit(n - origSize) ){ - while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){} - if( !np ){ - sqlite3FailedMalloc(); - OSMALLOC_FAILED(); - }else{ - updateMemoryUsedCount(OSSIZEOF(np) - origSize); - } - } - return np; + db->mallocFailed = 1; } + return p; } /* -** Free the memory pointed to by p. p must be either a NULL pointer or a -** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc(). +** Allocate and zero memory. If the allocation fails, make +** the mallocFailed flag in the connection pointer. */ -void sqlite3FreeX(void *p){ - if( p ){ - updateMemoryUsedCount(0 - OSSIZEOF(p)); - OSFREE(p); +void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){ + void *p = sqlite3_malloc(n); + if( !p ){ + db->mallocFailed = 1; } + return p; } /* -** A version of sqliteMalloc() that is always a function, not a macro. -** Currently, this is used only to alloc to allocate the parser engine. +** Attempt to reallocate p. If the reallocation fails, then free p +** and set the mallocFailed flag in the database connection. */ -void *sqlite3MallocX(int n){ - return sqliteMalloc(n); -} - -/* -** sqlite3Malloc -** sqlite3ReallocOrFree -** -** These two are implemented as wrappers around sqlite3MallocRaw(), -** sqlite3Realloc() and sqlite3Free(). -*/ -void *sqlite3Malloc(int n, int doMemManage){ - void *p = sqlite3MallocRaw(n, doMemManage); - if( p ){ - memset(p, 0, n); - } - return p; -} -void *sqlite3ReallocOrFree(void *p, int n){ +void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ void *pNew; - pNew = sqlite3Realloc(p, n); + pNew = sqlite3_realloc(p, n); if( !pNew ){ sqlite3FreeX(p); + db->mallocFailed = 1; } return pNew; } -/* -** 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){ - (void)ENTER_MALLOC; - return sqlite3Malloc(n, 0); -} -void sqlite3ThreadSafeFree(void *p){ - (void)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 -** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree(). -** -** The number of bytes allocated does not include any overhead inserted by -** any malloc() wrapper functions that may be called. So the value returned -** is the number of bytes that were available to SQLite using pointer p, -** regardless of how much memory was actually allocated. -*/ -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT -int sqlite3AllocSize(void *p){ - return OSSIZEOF(p); -} -#endif /* ** Make a copy of a string in memory obtained from sqliteMalloc(). These @@ -781,8 +157,8 @@ void sqlite3SetString(char **pz, ...){ /* ** This function must be called before exiting any API function (i.e. -** returning control to the user) that has called sqlite3Malloc or -** sqlite3Realloc. +** returning control to the user) that has called sqlite3_malloc or +** 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 @@ -792,36 +168,25 @@ void sqlite3SetString(char **pz, ...){ ** then the connection error-code (the value returned by sqlite3_errcode()) ** is set to SQLITE_NOMEM. */ -int sqlite3MallocHasFailed = 0; int sqlite3ApiExit(sqlite3* db, int rc){ - if( sqlite3MallocFailed() ){ - sqlite3MallocHasFailed = 0; - sqlite3OsLeaveMutex(); + if( db->mallocFailed ){ sqlite3Error(db, SQLITE_NOMEM, 0); + db->mallocFailed = 0; rc = SQLITE_NOMEM; } return rc & (db ? db->errMask : 0xff); } -/* -** Set the "malloc has failed" condition to true for this thread. -*/ -void sqlite3FailedMalloc(){ - if( !sqlite3MallocFailed() ){ - sqlite3OsEnterMutex(); - assert( sqlite3MallocHasFailed==0 ); - sqlite3MallocHasFailed = 1; - } -} - #ifdef SQLITE_MEMDEBUG /* ** This function sets a flag in the thread-specific-data structure that will ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called. */ void sqlite3MallocDisallow(){ +#if 0 assert( sqlite3_mallocDisallowed>=0 ); sqlite3_mallocDisallowed++; +#endif } /* @@ -829,7 +194,9 @@ void sqlite3MallocDisallow(){ ** by sqlite3MallocDisallow(). */ void sqlite3MallocAllow(){ +#if 0 assert( sqlite3_mallocDisallowed>0 ); sqlite3_mallocDisallowed--; +#endif } #endif diff --git a/src/os_unix.c b/src/os_unix.c index dc223d929..ff842b896 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -357,10 +357,8 @@ 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, - sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0}; -static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, - sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0}; +static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0}; +static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0}; #ifdef SQLITE_ENABLE_LOCKING_STYLE /* @@ -537,7 +535,7 @@ static void releaseLockInfo(struct lockInfo *pLock){ pLock->nRef--; if( pLock->nRef==0 ){ sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0); - sqlite3ThreadSafeFree(pLock); + sqlite3_free(pLock); } } @@ -552,7 +550,7 @@ static void releaseOpenCnt(struct openCnt *pOpen){ if( pOpen->nRef==0 ){ sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0); free(pOpen->aPending); - sqlite3ThreadSafeFree(pOpen); + sqlite3_free(pOpen); } } @@ -664,7 +662,7 @@ static int findLockInfo( pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1)); if( pLock==0 ){ struct lockInfo *pOld; - pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) ); + pLock = sqlite3_malloc( sizeof(*pLock) ); if( pLock==0 ){ rc = 1; goto exit_findlockinfo; @@ -676,7 +674,7 @@ static int findLockInfo( pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock); if( pOld!=0 ){ assert( pOld==pLock ); - sqlite3ThreadSafeFree(pLock); + sqlite3_free(pLock); rc = 1; goto exit_findlockinfo; } @@ -688,7 +686,7 @@ static int findLockInfo( pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2)); if( pOpen==0 ){ struct openCnt *pOld; - pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) ); + pOpen = sqlite3_malloc( sizeof(*pOpen) ); if( pOpen==0 ){ releaseLockInfo(pLock); rc = 1; @@ -702,7 +700,7 @@ static int findLockInfo( pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen); if( pOld!=0 ){ assert( pOld==pOpen ); - sqlite3ThreadSafeFree(pOpen); + sqlite3_free(pOpen); releaseLockInfo(pLock); rc = 1; goto exit_findlockinfo; @@ -1701,7 +1699,7 @@ static int unixClose(sqlite3_file *id){ pFile->isOpen = 0; OSTRACE2("CLOSE %-3d\n", pFile->h); OpenCounter(-1); - sqlite3ThreadSafeFree(id); + sqlite3_free(id); return SQLITE_OK; } @@ -2006,8 +2004,8 @@ static int afpUnixClose(OsFile **pId) { /* free the AFP locking structure */ if (id->lockingContext != NULL) { if (((afpLockingContext *)id->lockingContext)->filePath != NULL) - sqlite3ThreadSafeFree(((afpLockingContext*)id->lockingContext)->filePath); - sqlite3ThreadSafeFree(id->lockingContext); + sqlite3_free(((afpLockingContext*)id->lockingContext)->filePath); + sqlite3_free(id->lockingContext); } if( id->dirfd>=0 ) close(id->dirfd); @@ -2016,7 +2014,7 @@ static int afpUnixClose(OsFile **pId) { id->isOpen = 0; OSTRACE2("CLOSE %-3d\n", id->h); OpenCounter(-1); - sqlite3ThreadSafeFree(id); + sqlite3_free(id); *pId = 0; return SQLITE_OK; } @@ -2112,7 +2110,7 @@ static int flockUnixClose(OsFile **pId) { id->isOpen = 0; OSTRACE2("CLOSE %-3d\n", id->h); OpenCounter(-1); - sqlite3ThreadSafeFree(id); + sqlite3_free(id); *pId = 0; return SQLITE_OK; } @@ -2216,9 +2214,9 @@ static int dotlockUnixClose(OsFile **pId) { /* free the dotlock locking structure */ if (id->lockingContext != NULL) { if (((dotlockLockingContext *)id->lockingContext)->lockPath != NULL) - sqlite3ThreadSafeFree( ( (dotlockLockingContext *) + sqlite3_free( ( (dotlockLockingContext *) id->lockingContext)->lockPath); - sqlite3ThreadSafeFree(id->lockingContext); + sqlite3_free(id->lockingContext); } if( id->dirfd>=0 ) close(id->dirfd); @@ -2231,7 +2229,7 @@ static int dotlockUnixClose(OsFile **pId) { id->isOpen = 0; OSTRACE2("CLOSE %-3d\n", id->h); OpenCounter(-1); - sqlite3ThreadSafeFree(id); + sqlite3_free(id); *pId = 0; return SQLITE_OK; } @@ -2273,7 +2271,7 @@ static int nolockUnixClose(OsFile **pId) { id->isOpen = 0; OSTRACE2("CLOSE %-3d\n", id->h); OpenCounter(-1); - sqlite3ThreadSafeFree(id); + sqlite3_free(id); *pId = 0; return SQLITE_OK; } @@ -2291,7 +2289,7 @@ char *sqlite3UnixFullPathname(const char *zRelative){ if( zRelative[0]=='/' ){ sqlite3SetString(&zFull, zRelative, (char*)0); }else{ - char *zBuf = sqliteMalloc(5000); + char *zBuf = sqlite3_malloc(5000); if( zBuf==0 ){ return 0; } @@ -2531,7 +2529,7 @@ static int allocateUnixFile( f.dirfd = -1; f.h = h; SET_THREADID(&f); - pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) ); + pNew = sqlite3_malloc( sizeof(unixFile) ); if( pNew==0 ){ close(h); sqlite3OsEnterMutex(); @@ -2549,10 +2547,10 @@ static int allocateUnixFile( int nFilename; pNew->pMethod = &sqlite3AFPLockingUnixIoMethod; pNew->lockingContext = - sqlite3ThreadSafeMalloc(sizeof(afpLockingContext)); + sqlite3_malloc(sizeof(afpLockingContext)); nFilename = strlen(zFilename)+1; ((afpLockingContext *)pNew->lockingContext)->filePath = - sqlite3ThreadSafeMalloc(nFilename); + sqlite3_malloc(nFilename); memcpy(((afpLockingContext *)pNew->lockingContext)->filePath, zFilename, nFilename); srandomdev(); @@ -2567,11 +2565,11 @@ static int allocateUnixFile( ** the dotlockLockingContext */ int nFilename; pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod; - pNew->lockingContext = sqlite3ThreadSafeMalloc( + pNew->lockingContext = sqlite3_malloc( sizeof(dotlockLockingContext)); nFilename = strlen(zFilename) + 6; ((dotlockLockingContext *)pNew->lockingContext)->lockPath = - sqlite3ThreadSafeMalloc( nFilename ); + sqlite3_malloc( nFilename ); sqlite3_snprintf(nFilename, ((dotlockLockingContext *)pNew->lockingContext)->lockPath, "%s.lock", zFilename); @@ -2620,7 +2618,7 @@ static int allocateUnixFile( f.dirfd = -1; f.h = h; SET_THREADID(&f); - pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) ); + pNew = sqlite3_malloc( sizeof(unixFile) ); if( pNew==0 ){ close(h); sqlite3OsEnterMutex(); diff --git a/src/pager.c b/src/pager.c index 35a2667da..ca11cf580 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.356 2007/08/15 17:08:46 danielk1977 Exp $ +** @(#) $Id: pager.c,v 1.357 2007/08/16 04:30:40 drh Exp $ */ #ifndef SQLITE_OMIT_DISKIO #include "sqliteInt.h" @@ -495,12 +495,12 @@ static int pageInStatement(PgHdr *pPg){ static void pager_resize_hash_table(Pager *pPager, int N){ PgHdr **aHash, *pPg; assert( N>0 && (N&(N-1))==0 ); - aHash = sqliteMalloc( sizeof(aHash[0])*N ); + aHash = sqlite3MallocZero( sizeof(aHash[0])*N ); if( aHash==0 ){ /* Failure to rehash is not an error. It is only a performance hit. */ return; } - sqliteFree(pPager->aHash); + sqlite3_free(pPager->aHash); pPager->nHash = N; pPager->aHash = aHash; for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ @@ -620,9 +620,9 @@ static void checkPage(PgHdr *pPg){ /* ** When this is called the journal file for pager pPager must be open. ** The master journal file name is read from the end of the file and -** written into memory obtained from sqliteMalloc(). *pzMaster is +** written into memory obtained from sqlite3_malloc(). *pzMaster is ** set to point at the memory and SQLITE_OK returned. The caller must -** sqliteFree() *pzMaster. +** sqlite3_free() *pzMaster. ** ** If no master journal file name is present *pzMaster is set to 0 and ** SQLITE_OK returned. @@ -649,13 +649,13 @@ static int readMasterJournal(sqlite3_file *pJrnl, char **pzMaster){ rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8); if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc; - *pzMaster = (char *)sqliteMalloc(len+1); + *pzMaster = (char *)sqlite3MallocZero(len+1); if( !*pzMaster ){ return SQLITE_NOMEM; } rc = sqlite3OsRead(pJrnl, *pzMaster, len, szJ-16-len); if( rc!=SQLITE_OK ){ - sqliteFree(*pzMaster); + sqlite3_free(*pzMaster); *pzMaster = 0; return rc; } @@ -670,7 +670,7 @@ static int readMasterJournal(sqlite3_file *pJrnl, char **pzMaster){ ** definitely roll back, so just return SQLITE_OK and report a (nul) ** master-journal filename. */ - sqliteFree(*pzMaster); + sqlite3_free(*pzMaster); *pzMaster = 0; }else{ (*pzMaster)[len] = '\0'; @@ -970,7 +970,7 @@ static void pager_reset(Pager *pPager){ IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno)); PAGER_INCR(sqlite3_pager_pgfree_count); pNext = pPg->pNextAll; - sqliteFree(pPg); + sqlite3_free(pPg); } pPager->pStmt = 0; pPager->pFirst = 0; @@ -978,7 +978,7 @@ static void pager_reset(Pager *pPager){ pPager->pLast = 0; pPager->pAll = 0; pPager->nHash = 0; - sqliteFree(pPager->aHash); + sqlite3_free(pPager->aHash); pPager->nPage = 0; pPager->aHash = 0; pPager->nRef = 0; @@ -1026,7 +1026,7 @@ static int pager_end_transaction(Pager *pPager){ rc = sqlite3OsDelete(pPager->zJournal); } } - sqliteFree( pPager->aInJournal ); + sqlite3_free( pPager->aInJournal ); pPager->aInJournal = 0; for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ pPg->inJournal = 0; @@ -1247,9 +1247,9 @@ static int pager_delmaster(const char *zMaster){ char *zMasterPtr = 0; /* Load the entire master journal file into space obtained from - ** sqliteMalloc() and pointed to by zMasterJournal. + ** sqlite3_malloc() and pointed to by zMasterJournal. */ - zMasterJournal = (char *)sqliteMalloc(nMasterJournal); + zMasterJournal = (char *)sqlite3_malloc(nMasterJournal); if( !zMasterJournal ){ rc = SQLITE_NOMEM; goto delmaster_out; @@ -1280,7 +1280,7 @@ static int pager_delmaster(const char *zMaster){ } c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0; - sqliteFree(zMasterPtr); + sqlite3_free(zMasterPtr); if( c ){ /* We have a match. Do not delete the master journal file. */ goto delmaster_out; @@ -1294,7 +1294,7 @@ static int pager_delmaster(const char *zMaster){ delmaster_out: if( zMasterJournal ){ - sqliteFree(zMasterJournal); + sqlite3_free(zMasterJournal); } if( master_open ){ sqlite3OsClose(&master); @@ -1412,7 +1412,7 @@ static int pager_playback(Pager *pPager, int isHot){ rc = readMasterJournal(pPager->jfd, &zMaster); assert( rc!=SQLITE_DONE ); if( rc!=SQLITE_OK || (zMaster && !sqlite3OsFileExists(zMaster)) ){ - sqliteFree(zMaster); + sqlite3_free(zMaster); zMaster = 0; if( rc==SQLITE_DONE ) rc = SQLITE_OK; goto end_playback; @@ -1498,7 +1498,7 @@ end_playback: if( rc==SQLITE_OK ){ rc = pager_delmaster(zMaster); } - sqliteFree(zMaster); + sqlite3_free(zMaster); } /* The Pager.sectorSize variable may have been updated while rolling @@ -1745,7 +1745,7 @@ int sqlite3PagerOpen( /* We used to test if malloc() had already failed before proceeding. ** But the way this function is used in SQLite means that can never ** happen. Furthermore, if the malloc-failed flag is already set, - ** either the call to sqliteStrDup() or sqliteMalloc() below will + ** either the call to sqlite3StrDup() or sqlite3_malloc() below will ** fail shortly and SQLITE_NOMEM returned anyway. */ *ppPager = 0; @@ -1757,7 +1757,7 @@ int sqlite3PagerOpen( #ifndef SQLITE_OMIT_MEMORYDB if( strcmp(zFilename,":memory:")==0 ){ memDb = 1; - zFullPathname = sqliteStrDup(""); + zFullPathname = sqlite3StrDup(""); }else #endif { @@ -1783,9 +1783,9 @@ int sqlite3PagerOpen( */ if( zFullPathname ){ nameLen = strlen(zFullPathname); - pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 ); + pPager = sqlite3MallocZero( sizeof(*pPager) + nameLen*3 + 30 ); if( pPager && rc==SQLITE_OK ){ - pPager->pTmpSpace = (char *)sqliteMallocRaw(SQLITE_DEFAULT_PAGE_SIZE); + pPager->pTmpSpace = (char *)sqlite3_malloc(SQLITE_DEFAULT_PAGE_SIZE); } } @@ -1797,8 +1797,8 @@ int sqlite3PagerOpen( */ if( !pPager || !zFullPathname || !pPager->pTmpSpace || rc!=SQLITE_OK ){ sqlite3OsClose(&fd); - sqliteFree(zFullPathname); - sqliteFree(pPager); + sqlite3_free(zFullPathname); + sqlite3_free(pPager); return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc); } @@ -1813,7 +1813,7 @@ int sqlite3PagerOpen( for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){} if( i>0 ) pPager->zDirectory[i-1] = 0; memcpy(pPager->zJournal, zFullPathname,nameLen); - sqliteFree(zFullPathname); + sqlite3_free(zFullPathname); memcpy(&pPager->zJournal[nameLen], "-journal",sizeof("-journal")); pPager->fd = fd; /* pPager->journalOpen = 0; */ @@ -1900,7 +1900,8 @@ int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){ if( !pPager->memDb && pPager->nRef==0 ){ pager_reset(pPager); pPager->pageSize = pageSize; - pPager->pTmpSpace = sqlite3ReallocOrFree(pPager->pTmpSpace, pageSize); + sqlite3_free(pPager->pTmpSpace); + pPager->pTmpSpace = sqlite3_malloc(pageSize); } return pPager->pageSize; } @@ -2014,8 +2015,8 @@ int sqlite3PagerPagecount(Pager *pPager){ ** Clear a PgHistory block */ static void clearHistory(PgHistory *pHist){ - sqliteFree(pHist->pOrig); - sqliteFree(pHist->pStmt); + sqlite3_free(pHist->pOrig); + sqlite3_free(pHist->pStmt); pHist->pOrig = 0; pHist->pStmt = 0; } @@ -2119,7 +2120,7 @@ static void pager_truncate_cache(Pager *pPager){ PAGER_INCR(sqlite3_pager_pgfree_count); unlinkPage(pPg); makeClean(pPg); - sqliteFree(pPg); + sqlite3_free(pPg); pPager->nPage--; } } @@ -2230,7 +2231,7 @@ int sqlite3PagerClose(Pager *pPager){ if( pPager->journalOpen ){ sqlite3OsClose(&pPager->jfd); } - sqliteFree(pPager->aInJournal); + sqlite3_free(pPager->aInJournal); if( pPager->stmtOpen ){ sqlite3OsClose(&pPager->stfd); } @@ -2253,9 +2254,9 @@ int sqlite3PagerClose(Pager *pPager){ pTmp->pNext = pPager->pNext; } #endif - sqliteFree(pPager->aHash); - sqliteFree(pPager->pTmpSpace); - sqliteFree(pPager); + sqlite3_free(pPager->aHash); + sqlite3_free(pPager->pTmpSpace); + sqlite3_free(pPager); return SQLITE_OK; } @@ -2683,7 +2684,7 @@ static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){ /* ** This function is called to free superfluous dynamically allocated memory ** held by the pager system. Memory in use by any SQLite pager allocated -** by the current thread may be sqliteFree()ed. +** by the current thread may be sqlite3_free()ed. ** ** nReq is the number of bytes of memory required. Once this much has ** been released, the function returns. A negative value for nReq means @@ -2747,7 +2748,7 @@ int sqlite3PagerReleaseMemory(int nReq){ nReleased += sqliteAllocSize(pPg); IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno)); PAGER_INCR(sqlite3_pager_pgfree_count); - sqliteFree(pPg); + sqlite3_free(pPg); } if( rc!=SQLITE_OK ){ @@ -2986,7 +2987,7 @@ static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){ goto pager_allocate_out; } } - pPg = sqliteMallocRaw( sizeof(*pPg) + pPager->pageSize + pPg = sqlite3_malloc( sizeof(*pPg) + pPager->pageSize + sizeof(u32) + pPager->nExtra + MEMDB*sizeof(PgHistory) ); if( pPg==0 ){ @@ -3290,7 +3291,7 @@ static int pager_open_journal(Pager *pPager){ assert( pPager->useJournal ); assert( pPager->aInJournal==0 ); sqlite3PagerPagecount(pPager); - pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 ); + pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); if( pPager->aInJournal==0 ){ rc = SQLITE_NOMEM; goto failed_to_open_journal; @@ -3337,7 +3338,7 @@ static int pager_open_journal(Pager *pPager){ return rc; failed_to_open_journal: - sqliteFree(pPager->aInJournal); + sqlite3_free(pPager->aInJournal); pPager->aInJournal = 0; return rc; } @@ -3406,7 +3407,7 @@ int sqlite3PagerBegin(DbPage *pPg, int exFlag){ assert( pPager->origDbSize==0 ); assert( pPager->aInJournal==0 ); sqlite3PagerPagecount(pPager); - pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 ); + pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); if( !pPager->aInJournal ){ rc = SQLITE_NOMEM; }else{ @@ -3541,7 +3542,7 @@ static int pager_write(PgHdr *pPg){ PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); assert( pHist->pOrig==0 ); - pHist->pOrig = sqliteMallocRaw( pPager->pageSize ); + pHist->pOrig = sqlite3_malloc( pPager->pageSize ); if( pHist->pOrig ){ memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize); } @@ -3608,7 +3609,7 @@ static int pager_write(PgHdr *pPg){ if( MEMDB ){ PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); assert( pHist->pStmt==0 ); - pHist->pStmt = sqliteMallocRaw( pPager->pageSize ); + pHist->pStmt = sqlite3_malloc( pPager->pageSize ); if( pHist->pStmt ){ memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize); } @@ -4154,7 +4155,7 @@ int sqlite3PagerStmtBegin(Pager *pPager){ return SQLITE_OK; } assert( pPager->journalOpen ); - pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 ); + pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 ); if( pPager->aInStmt==0 ){ /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */ return SQLITE_NOMEM; @@ -4179,7 +4180,7 @@ int sqlite3PagerStmtBegin(Pager *pPager){ stmt_begin_failed: if( pPager->aInStmt ){ - sqliteFree(pPager->aInStmt); + sqlite3_free(pPager->aInStmt); pPager->aInStmt = 0; } return rc; @@ -4194,7 +4195,7 @@ int sqlite3PagerStmtCommit(Pager *pPager){ PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager)); if( !MEMDB ){ /* sqlite3OsTruncate(pPager->stfd, 0); */ - sqliteFree( pPager->aInStmt ); + sqlite3_free( pPager->aInStmt ); pPager->aInStmt = 0; }else{ for(pPg=pPager->pStmt; pPg; pPg=pNext){ @@ -4203,7 +4204,7 @@ int sqlite3PagerStmtCommit(Pager *pPager){ assert( pHist->inStmt ); pHist->inStmt = 0; pHist->pPrevStmt = pHist->pNextStmt = 0; - sqliteFree(pHist->pStmt); + sqlite3_free(pHist->pStmt); pHist->pStmt = 0; } } @@ -4229,7 +4230,7 @@ int sqlite3PagerStmtRollback(Pager *pPager){ pHist = PGHDR_TO_HIST(pPg, pPager); if( pHist->pStmt ){ memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize); - sqliteFree(pHist->pStmt); + sqlite3_free(pHist->pStmt); pHist->pStmt = 0; } } diff --git a/src/parse.y b/src/parse.y index 1de400f72..c4d7238dc 100644 --- a/src/parse.y +++ b/src/parse.y @@ -14,7 +14,7 @@ ** the parser. Lemon will also generate a header file containing ** numeric codes for all of the tokens. ** -** @(#) $Id: parse.y,v 1.231 2007/06/20 12:18:31 drh Exp $ +** @(#) $Id: parse.y,v 1.232 2007/08/16 04:30:40 drh Exp $ */ // All token codes are small integers with #defines that begin with "TK_" @@ -253,11 +253,11 @@ ccons ::= DEFAULT term(X). {sqlite3AddDefaultValue(pParse,X);} ccons ::= DEFAULT LP expr(X) RP. {sqlite3AddDefaultValue(pParse,X);} ccons ::= DEFAULT PLUS term(X). {sqlite3AddDefaultValue(pParse,X);} ccons ::= DEFAULT MINUS term(X). { - Expr *p = sqlite3Expr(TK_UMINUS, X, 0, 0); + Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0, 0); sqlite3AddDefaultValue(pParse,p); } ccons ::= DEFAULT id(X). { - Expr *p = sqlite3Expr(TK_STRING, 0, 0, &X); + Expr *p = sqlite3PExpr(pParse, TK_STRING, 0, 0, &X); sqlite3AddDefaultValue(pParse,p); } @@ -393,7 +393,7 @@ multiselect_op(A) ::= EXCEPT|INTERSECT(OP). {A = @OP;} %endif SQLITE_OMIT_COMPOUND_SELECT oneselect(A) ::= SELECT distinct(D) selcollist(W) from(X) where_opt(Y) groupby_opt(P) having_opt(Q) orderby_opt(Z) limit_opt(L). { - A = sqlite3SelectNew(W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset); + A = sqlite3SelectNew(pParse,W,X,Y,P,Q,Z,D,L.pLimit,L.pOffset); } // The "distinct" nonterminal is true (1) if the DISTINCT keyword is @@ -416,15 +416,17 @@ distinct(A) ::= . {A = 0;} sclp(A) ::= selcollist(X) COMMA. {A = X;} sclp(A) ::= . {A = 0;} selcollist(A) ::= sclp(P) expr(X) as(Y). { - A = sqlite3ExprListAppend(P,X,Y.n?&Y:0); + A = sqlite3ExprListAppend(pParse,P,X,Y.n?&Y:0); } selcollist(A) ::= sclp(P) STAR. { - A = sqlite3ExprListAppend(P, sqlite3Expr(TK_ALL, 0, 0, 0), 0); + Expr *p = sqlite3Expr(pParse, TK_ALL, 0, 0, 0); + A = sqlite3ExprListAppend(pParse, P, p, 0); } selcollist(A) ::= sclp(P) nm(X) DOT STAR. { - Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0); - Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &X); - A = sqlite3ExprListAppend(P, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0); + Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0); + Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); + Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); + A = sqlite3ExprListAppend(pParse,P, pDot, 0); } // An option "AS <id>" phrase that can follow one of the expressions that @@ -445,8 +447,8 @@ as(X) ::= . {X.n = 0;} // A complete FROM clause. // -from(A) ::= . {A = sqliteMalloc(sizeof(*A));} -from(A) ::= FROM seltablist(X). { +from(A) ::= . {A = sqlite3DbMallocZero(pParse->db, sizeof(*A));} +from(A) ::= FROM seltablist(X). { A = X; sqlite3SrcListShiftJoinType(A); } @@ -460,12 +462,12 @@ stl_prefix(A) ::= seltablist(X) joinop(Y). { } stl_prefix(A) ::= . {A = 0;} seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). { - A = sqlite3SrcListAppendFromTerm(X,&Y,&D,&Z,0,N,U); + A = sqlite3SrcListAppendFromTerm(pParse,X,&Y,&D,&Z,0,N,U); } %ifndef SQLITE_OMIT_SUBQUERY seltablist(A) ::= stl_prefix(X) LP seltablist_paren(S) RP as(Z) on_opt(N) using_opt(U). { - A = sqlite3SrcListAppendFromTerm(X,0,0,&Z,S,N,U); + A = sqlite3SrcListAppendFromTerm(pParse,X,0,0,&Z,S,N,U); } // A seltablist_paren nonterminal represents anything in a FROM that @@ -477,7 +479,7 @@ seltablist(A) ::= stl_prefix(X) nm(Y) dbnm(D) as(Z) on_opt(N) using_opt(U). { seltablist_paren(A) ::= select(S). {A = S;} seltablist_paren(A) ::= seltablist(F). { sqlite3SrcListShiftJoinType(F); - A = sqlite3SelectNew(0,F,0,0,0,0,0,0,0); + A = sqlite3SelectNew(pParse,0,F,0,0,0,0,0,0,0); } %endif SQLITE_OMIT_SUBQUERY @@ -487,7 +489,7 @@ dbnm(A) ::= DOT nm(X). {A = X;} %type fullname {SrcList*} %destructor fullname {sqlite3SrcListDelete($$);} -fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(0,&X,&Y);} +fullname(A) ::= nm(X) dbnm(Y). {A = sqlite3SrcListAppend(pParse->db,0,&X,&Y);} %type joinop {int} %type joinop2 {int} @@ -518,11 +520,11 @@ using_opt(U) ::= . {U = 0;} orderby_opt(A) ::= . {A = 0;} orderby_opt(A) ::= ORDER BY sortlist(X). {A = X;} sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). { - A = sqlite3ExprListAppend(X,Y,0); + A = sqlite3ExprListAppend(pParse,X,Y,0); if( A ) A->a[A->nExpr-1].sortOrder = Z; } sortlist(A) ::= sortitem(Y) sortorder(Z). { - A = sqlite3ExprListAppend(0,Y,0); + A = sqlite3ExprListAppend(pParse,0,Y,0); if( A && A->a ) A->a[0].sortOrder = Z; } sortitem(A) ::= expr(X). {A = X;} @@ -584,8 +586,9 @@ cmd ::= UPDATE orconf(R) fullname(X) SET setlist(Y) where_opt(Z). { %destructor setlist {sqlite3ExprListDelete($$);} setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). - {A = sqlite3ExprListAppend(Z,Y,&X);} -setlist(A) ::= nm(X) EQ expr(Y). {A = sqlite3ExprListAppend(0,Y,&X);} + {A = sqlite3ExprListAppend(pParse,Z,Y,&X);} +setlist(A) ::= nm(X) EQ expr(Y). + {A = sqlite3ExprListAppend(pParse,0,Y,&X);} ////////////////////////// The INSERT command ///////////////////////////////// // @@ -605,8 +608,10 @@ insert_cmd(A) ::= REPLACE. {A = OE_Replace;} %type itemlist {ExprList*} %destructor itemlist {sqlite3ExprListDelete($$);} -itemlist(A) ::= itemlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);} -itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);} +itemlist(A) ::= itemlist(X) COMMA expr(Y). + {A = sqlite3ExprListAppend(pParse,X,Y,0);} +itemlist(A) ::= expr(X). + {A = sqlite3ExprListAppend(pParse,0,X,0);} %type inscollist_opt {IdList*} %destructor inscollist_opt {sqlite3IdListDelete($$);} @@ -615,8 +620,10 @@ itemlist(A) ::= expr(X). {A = sqlite3ExprListAppend(0,X,0);} inscollist_opt(A) ::= . {A = 0;} inscollist_opt(A) ::= LP inscollist(X) RP. {A = X;} -inscollist(A) ::= inscollist(X) COMMA nm(Y). {A = sqlite3IdListAppend(X,&Y);} -inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);} +inscollist(A) ::= inscollist(X) COMMA nm(Y). + {A = sqlite3IdListAppend(pParse->db,X,&Y);} +inscollist(A) ::= nm(Y). + {A = sqlite3IdListAppend(pParse->db,0,&Y);} /////////////////////////// Expression Processing ///////////////////////////// // @@ -628,27 +635,27 @@ inscollist(A) ::= nm(Y). {A = sqlite3IdListAppend(0,&Y);} expr(A) ::= term(X). {A = X;} expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); } -term(A) ::= NULL(X). {A = sqlite3Expr(@X, 0, 0, &X);} -expr(A) ::= ID(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} -expr(A) ::= JOIN_KW(X). {A = sqlite3Expr(TK_ID, 0, 0, &X);} +term(A) ::= NULL(X). {A = sqlite3PExpr(pParse, @X, 0, 0, &X);} +expr(A) ::= ID(X). {A = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);} +expr(A) ::= JOIN_KW(X). {A = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);} expr(A) ::= nm(X) DOT nm(Y). { - Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); - Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); - A = sqlite3Expr(TK_DOT, temp1, temp2, 0); + Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); + Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y); + A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0); } expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). { - Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X); - Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y); - Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z); - Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0); - A = sqlite3Expr(TK_DOT, temp1, temp4, 0); + Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X); + Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y); + Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z); + Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0); + A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0); } -term(A) ::= INTEGER|FLOAT|BLOB(X). {A = sqlite3Expr(@X, 0, 0, &X);} -term(A) ::= STRING(X). {A = sqlite3Expr(@X, 0, 0, &X);} +term(A) ::= INTEGER|FLOAT|BLOB(X). {A = sqlite3PExpr(pParse, @X, 0, 0, &X);} +term(A) ::= STRING(X). {A = sqlite3PExpr(pParse, @X, 0, 0, &X);} expr(A) ::= REGISTER(X). {A = sqlite3RegisterExpr(pParse, &X);} expr(A) ::= VARIABLE(X). { Token *pToken = &X; - Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken); + Expr *pExpr = A = sqlite3PExpr(pParse, TK_VARIABLE, 0, 0, pToken); sqlite3ExprAssignVarNumber(pParse, pExpr); } expr(A) ::= expr(E) COLLATE id(C). { @@ -656,7 +663,7 @@ expr(A) ::= expr(E) COLLATE id(C). { } %ifndef SQLITE_OMIT_CAST expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { - A = sqlite3Expr(TK_CAST, E, 0, &T); + A = sqlite3PExpr(pParse, TK_CAST, E, 0, &T); sqlite3ExprSpan(A,&X,&Y); } %endif SQLITE_OMIT_CAST @@ -664,34 +671,36 @@ expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). { if( Y && Y->nExpr>SQLITE_MAX_FUNCTION_ARG ){ sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X); } - A = sqlite3ExprFunction(Y, &X); + A = sqlite3ExprFunction(pParse, Y, &X); sqlite3ExprSpan(A,&X,&E); if( D && A ){ A->flags |= EP_Distinct; } } expr(A) ::= ID(X) LP STAR RP(E). { - A = sqlite3ExprFunction(0, &X); + A = sqlite3ExprFunction(pParse, 0, &X); sqlite3ExprSpan(A,&X,&E); } term(A) ::= CTIME_KW(OP). { /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are ** treated as functions that return constants */ - A = sqlite3ExprFunction(0,&OP); + A = sqlite3ExprFunction(pParse, 0,&OP); if( A ){ A->op = TK_CONST_FUNC; A->span = OP; } } -expr(A) ::= expr(X) AND(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} -expr(A) ::= expr(X) OR(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} -expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} -expr(A) ::= expr(X) EQ|NE(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} +expr(A) ::= expr(X) AND(OP) expr(Y). {A = sqlite3PExpr(pParse,@OP,X,Y,0);} +expr(A) ::= expr(X) OR(OP) expr(Y). {A = sqlite3PExpr(pParse,@OP,X,Y,0);} +expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y). + {A = sqlite3PExpr(pParse,@OP,X,Y,0);} +expr(A) ::= expr(X) EQ|NE(OP) expr(Y). {A = sqlite3PExpr(pParse,@OP,X,Y,0);} expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y). - {A = sqlite3Expr(@OP, X, Y, 0);} -expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} -expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} -expr(A) ::= expr(X) CONCAT(OP) expr(Y). {A = sqlite3Expr(@OP, X, Y, 0);} + {A = sqlite3PExpr(pParse,@OP,X,Y,0);} +expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).{A = sqlite3PExpr(pParse,@OP,X,Y,0);} +expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y). + {A = sqlite3PExpr(pParse,@OP,X,Y,0);} +expr(A) ::= expr(X) CONCAT(OP) expr(Y). {A = sqlite3PExpr(pParse,@OP,X,Y,0);} %type likeop {struct LikeOp} likeop(A) ::= LIKE_KW(X). {A.eOperator = X; A.not = 0;} likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;} @@ -703,58 +712,58 @@ escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;} escape(X) ::= . [ESCAPE] {X = 0;} expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E). [LIKE_KW] { ExprList *pList; - pList = sqlite3ExprListAppend(0, Y, 0); - pList = sqlite3ExprListAppend(pList, X, 0); + pList = sqlite3ExprListAppend(pParse,0, Y, 0); + pList = sqlite3ExprListAppend(pParse,pList, X, 0); if( E ){ - pList = sqlite3ExprListAppend(pList, E, 0); + pList = sqlite3ExprListAppend(pParse,pList, E, 0); } - A = sqlite3ExprFunction(pList, &OP.eOperator); - if( OP.not ) A = sqlite3Expr(TK_NOT, A, 0, 0); + A = sqlite3ExprFunction(pParse, pList, &OP.eOperator); + if( OP.not ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0); sqlite3ExprSpan(A, &X->span, &Y->span); if( A ) A->flags |= EP_InfixFunc; } expr(A) ::= expr(X) ISNULL|NOTNULL(E). { - A = sqlite3Expr(@E, X, 0, 0); + A = sqlite3PExpr(pParse, @E, X, 0, 0); sqlite3ExprSpan(A,&X->span,&E); } expr(A) ::= expr(X) IS NULL(E). { - A = sqlite3Expr(TK_ISNULL, X, 0, 0); + A = sqlite3PExpr(pParse, TK_ISNULL, X, 0, 0); sqlite3ExprSpan(A,&X->span,&E); } expr(A) ::= expr(X) NOT NULL(E). { - A = sqlite3Expr(TK_NOTNULL, X, 0, 0); + A = sqlite3PExpr(pParse, TK_NOTNULL, X, 0, 0); sqlite3ExprSpan(A,&X->span,&E); } expr(A) ::= expr(X) IS NOT NULL(E). { - A = sqlite3Expr(TK_NOTNULL, X, 0, 0); + A = sqlite3PExpr(pParse, TK_NOTNULL, X, 0, 0); sqlite3ExprSpan(A,&X->span,&E); } expr(A) ::= NOT|BITNOT(B) expr(X). { - A = sqlite3Expr(@B, X, 0, 0); + A = sqlite3PExpr(pParse, @B, X, 0, 0); sqlite3ExprSpan(A,&B,&X->span); } expr(A) ::= MINUS(B) expr(X). [UMINUS] { - A = sqlite3Expr(TK_UMINUS, X, 0, 0); + A = sqlite3PExpr(pParse, TK_UMINUS, X, 0, 0); sqlite3ExprSpan(A,&B,&X->span); } expr(A) ::= PLUS(B) expr(X). [UPLUS] { - A = sqlite3Expr(TK_UPLUS, X, 0, 0); + A = sqlite3PExpr(pParse, TK_UPLUS, X, 0, 0); sqlite3ExprSpan(A,&B,&X->span); } %type between_op {int} between_op(A) ::= BETWEEN. {A = 0;} between_op(A) ::= NOT BETWEEN. {A = 1;} expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] { - ExprList *pList = sqlite3ExprListAppend(0, X, 0); - pList = sqlite3ExprListAppend(pList, Y, 0); - A = sqlite3Expr(TK_BETWEEN, W, 0, 0); + ExprList *pList = sqlite3ExprListAppend(pParse,0, X, 0); + pList = sqlite3ExprListAppend(pParse,pList, Y, 0); + A = sqlite3PExpr(pParse, TK_BETWEEN, W, 0, 0); if( A ){ A->pList = pList; }else{ sqlite3ExprListDelete(pList); } - if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0); sqlite3ExprSpan(A,&W->span,&Y->span); } %ifndef SQLITE_OMIT_SUBQUERY @@ -762,18 +771,18 @@ expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] { in_op(A) ::= IN. {A = 0;} in_op(A) ::= NOT IN. {A = 1;} expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] { - A = sqlite3Expr(TK_IN, X, 0, 0); + A = sqlite3PExpr(pParse, TK_IN, X, 0, 0); if( A ){ A->pList = Y; sqlite3ExprSetHeight(A); }else{ sqlite3ExprListDelete(Y); } - if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0); sqlite3ExprSpan(A,&X->span,&E); } expr(A) ::= LP(B) select(X) RP(E). { - A = sqlite3Expr(TK_SELECT, 0, 0, 0); + A = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0); if( A ){ A->pSelect = X; sqlite3ExprSetHeight(A); @@ -783,30 +792,30 @@ expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] { sqlite3ExprSpan(A,&B,&E); } expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E). [IN] { - A = sqlite3Expr(TK_IN, X, 0, 0); + A = sqlite3PExpr(pParse, TK_IN, X, 0, 0); if( A ){ A->pSelect = Y; sqlite3ExprSetHeight(A); }else{ sqlite3SelectDelete(Y); } - if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0); sqlite3ExprSpan(A,&X->span,&E); } expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] { - SrcList *pSrc = sqlite3SrcListAppend(0,&Y,&Z); - A = sqlite3Expr(TK_IN, X, 0, 0); + SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z); + A = sqlite3PExpr(pParse, TK_IN, X, 0, 0); if( A ){ A->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0); sqlite3ExprSetHeight(A); }else{ sqlite3SrcListDelete(pSrc); } - if( N ) A = sqlite3Expr(TK_NOT, A, 0, 0); + if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0); sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y); } expr(A) ::= EXISTS(B) LP select(Y) RP(E). { - Expr *p = A = sqlite3Expr(TK_EXISTS, 0, 0, 0); + Expr *p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0); if( p ){ p->pSelect = Y; sqlite3ExprSpan(p,&B,&E); @@ -819,7 +828,7 @@ expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] { /* CASE expressions */ expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { - A = sqlite3Expr(TK_CASE, X, Z, 0); + A = sqlite3PExpr(pParse, TK_CASE, X, Z, 0); if( A ){ A->pList = Y; sqlite3ExprSetHeight(A); @@ -831,12 +840,12 @@ expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). { %type case_exprlist {ExprList*} %destructor case_exprlist {sqlite3ExprListDelete($$);} case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). { - A = sqlite3ExprListAppend(X, Y, 0); - A = sqlite3ExprListAppend(A, Z, 0); + A = sqlite3ExprListAppend(pParse,X, Y, 0); + A = sqlite3ExprListAppend(pParse,A, Z, 0); } case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). { - A = sqlite3ExprListAppend(0, Y, 0); - A = sqlite3ExprListAppend(A, Z, 0); + A = sqlite3ExprListAppend(pParse,0, Y, 0); + A = sqlite3ExprListAppend(pParse,A, Z, 0); } %type case_else {Expr*} %destructor case_else {sqlite3ExprDelete($$);} @@ -854,15 +863,18 @@ case_operand(A) ::= . {A = 0;} exprlist(A) ::= nexprlist(X). {A = X;} exprlist(A) ::= . {A = 0;} -nexprlist(A) ::= nexprlist(X) COMMA expr(Y). {A = sqlite3ExprListAppend(X,Y,0);} -nexprlist(A) ::= expr(Y). {A = sqlite3ExprListAppend(0,Y,0);} +nexprlist(A) ::= nexprlist(X) COMMA expr(Y). + {A = sqlite3ExprListAppend(pParse,X,Y,0);} +nexprlist(A) ::= expr(Y). + {A = sqlite3ExprListAppend(pParse,0,Y,0);} ///////////////////////////// The CREATE INDEX command /////////////////////// // cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D) ON nm(Y) LP idxlist(Z) RP(E). { - sqlite3CreateIndex(pParse, &X, &D, sqlite3SrcListAppend(0,&Y,0), Z, U, + sqlite3CreateIndex(pParse, &X, &D, + sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U, &S, &E, SQLITE_SO_ASC, NE); } @@ -881,20 +893,20 @@ idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;} idxlist(A) ::= idxlist(X) COMMA idxitem(Y) collate(C) sortorder(Z). { Expr *p = 0; if( C.n>0 ){ - p = sqlite3Expr(TK_COLUMN, 0, 0, 0); + p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0); if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n); } - A = sqlite3ExprListAppend(X, p, &Y); + A = sqlite3ExprListAppend(pParse,X, p, &Y); sqlite3ExprListCheckLength(pParse, A, SQLITE_MAX_COLUMN, "index"); if( A ) A->a[A->nExpr-1].sortOrder = Z; } idxlist(A) ::= idxitem(Y) collate(C) sortorder(Z). { Expr *p = 0; if( C.n>0 ){ - p = sqlite3Expr(TK_COLUMN, 0, 0, 0); + p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0); if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)C.z, C.n); } - A = sqlite3ExprListAppend(0, p, &Y); + A = sqlite3ExprListAppend(pParse,0, p, &Y); sqlite3ExprListCheckLength(pParse, A, SQLITE_MAX_COLUMN, "index"); if( A ) A->a[A->nExpr-1].sortOrder = Z; } @@ -992,33 +1004,33 @@ trigger_cmd_list(A) ::= . { A = 0; } %destructor trigger_cmd {sqlite3DeleteTriggerStep($$);} // UPDATE trigger_cmd(A) ::= UPDATE orconf(R) nm(X) SET setlist(Y) where_opt(Z). - { A = sqlite3TriggerUpdateStep(&X, Y, Z, R); } + { A = sqlite3TriggerUpdateStep(pParse->db, &X, Y, Z, R); } // INSERT trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) VALUES LP itemlist(Y) RP. - {A = sqlite3TriggerInsertStep(&X, F, Y, 0, R);} + {A = sqlite3TriggerInsertStep(pParse->db, &X, F, Y, 0, R);} trigger_cmd(A) ::= insert_cmd(R) INTO nm(X) inscollist_opt(F) select(S). - {A = sqlite3TriggerInsertStep(&X, F, 0, S, R);} + {A = sqlite3TriggerInsertStep(pParse->db, &X, F, 0, S, R);} // DELETE trigger_cmd(A) ::= DELETE FROM nm(X) where_opt(Y). - {A = sqlite3TriggerDeleteStep(&X, Y);} + {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);} // SELECT -trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(X); } +trigger_cmd(A) ::= select(X). {A = sqlite3TriggerSelectStep(pParse->db, X); } // The special RAISE expression that may occur in trigger programs expr(A) ::= RAISE(X) LP IGNORE RP(Y). { - A = sqlite3Expr(TK_RAISE, 0, 0, 0); + A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); if( A ){ A->iColumn = OE_Ignore; sqlite3ExprSpan(A, &X, &Y); } } expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y). { - A = sqlite3Expr(TK_RAISE, 0, 0, &Z); + A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z); if( A ) { A->iColumn = T; sqlite3ExprSpan(A, &X, &Y); diff --git a/src/pragma.c b/src/pragma.c index 9a9dd3051..ec047eb3b 100644 --- a/src/pragma.c +++ b/src/pragma.c @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code used to implement the PRAGMA command. ** -** $Id: pragma.c,v 1.142 2007/06/26 10:38:55 danielk1977 Exp $ +** $Id: pragma.c,v 1.143 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -264,12 +264,12 @@ void sqlite3Pragma( return; } - zLeft = sqlite3NameFromToken(pId); + zLeft = sqlite3NameFromToken(db, pId); if( !zLeft ) return; if( minusFlag ){ - zRight = sqlite3MPrintf("-%T", pValue); + zRight = sqlite3MPrintf(db, "-%T", pValue); }else{ - zRight = sqlite3NameFromToken(pValue); + zRight = sqlite3NameFromToken(db, pValue); } zDb = ((iDb>0)?pDb->zName:0); @@ -567,7 +567,7 @@ void sqlite3Pragma( ){ invalidateTempStorage(pParse); } - sqliteFree(sqlite3_temp_directory); + sqlite3_free(sqlite3_temp_directory); if( zRight[0] ){ sqlite3_temp_directory = zRight; zRight = 0; @@ -1173,8 +1173,8 @@ void sqlite3Pragma( #endif } pragma_out: - sqliteFree(zLeft); - sqliteFree(zRight); + sqlite3_free(zLeft); + sqlite3_free(zRight); } #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */ diff --git a/src/prepare.c b/src/prepare.c index 4a040eb49..b173566a5 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.52 2007/08/13 14:41:19 danielk1977 Exp $ +** $Id: prepare.c,v 1.53 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -24,7 +24,7 @@ ** that the database is corrupt. */ static void corruptSchema(InitData *pData, const char *zExtra){ - if( !sqlite3MallocFailed() ){ + if( !pData->db->mallocFailed ){ sqlite3SetString(pData->pzErrMsg, "malformed database schema", zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); } @@ -50,7 +50,7 @@ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ pData->rc = SQLITE_OK; DbClearProperty(db, iDb, DB_Empty); - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ corruptSchema(pData, 0); return SQLITE_NOMEM; } @@ -79,7 +79,7 @@ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ if( SQLITE_OK!=rc ){ pData->rc = rc; if( rc==SQLITE_NOMEM ){ - sqlite3FailedMalloc(); + db->mallocFailed = 1; }else if( rc!=SQLITE_INTERRUPT ){ corruptSchema(pData, zErr); } @@ -297,7 +297,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); if( rc==SQLITE_ABORT ) rc = initData.rc; sqlite3SafetyOn(db); - sqliteFree(zSql); + sqlite3_free(zSql); #ifndef SQLITE_OMIT_ANALYZE if( rc==SQLITE_OK ){ sqlite3AnalysisLoad(db, iDb); @@ -305,7 +305,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ #endif sqlite3BtreeCloseCursor(curMain); } - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */ rc = SQLITE_NOMEM; sqlite3ResetInternalSchema(db, 0); @@ -464,7 +464,7 @@ int sqlite3Prepare( int i; /* Assert that malloc() has not failed */ - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); assert( ppStmt ); *ppStmt = 0; @@ -492,17 +492,17 @@ int sqlite3Prepare( if( nBytes>SQLITE_MAX_SQL_LENGTH ){ return SQLITE_TOOBIG; } - zSqlCopy = sqlite3StrNDup(zSql, nBytes); + zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); if( zSqlCopy ){ sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); - sqliteFree(zSqlCopy); + sqlite3_free(zSqlCopy); } sParse.zTail = &zSql[nBytes]; }else{ sqlite3RunParser(&sParse, zSql, &zErrMsg); } - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ sParse.rc = SQLITE_NOMEM; } if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; @@ -512,7 +512,7 @@ int sqlite3Prepare( if( sParse.rc==SQLITE_SCHEMA ){ sqlite3ResetInternalSchema(db, 0); } - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ sParse.rc = SQLITE_NOMEM; } if( pzTail ){ @@ -545,7 +545,7 @@ int sqlite3Prepare( if( saveSqlFlag ){ sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql); } - if( rc!=SQLITE_OK || sqlite3MallocFailed() ){ + if( rc!=SQLITE_OK || db->mallocFailed ){ sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); assert(!(*ppStmt)); }else{ @@ -554,7 +554,7 @@ int sqlite3Prepare( if( zErrMsg ){ sqlite3Error(db, rc, "%s", zErrMsg); - sqliteFree(zErrMsg); + sqlite3_free(zErrMsg); }else{ sqlite3Error(db, rc, 0); } @@ -661,7 +661,7 @@ static int sqlite3Prepare16( int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8); *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); } - sqliteFree(zSql8); + sqlite3_free(zSql8); return sqlite3ApiExit(db, rc); } diff --git a/src/printf.c b/src/printf.c index 01ed7a1ee..fb7a78655 100644 --- a/src/printf.c +++ b/src/printf.c @@ -627,7 +627,7 @@ static int vxprintf( needQuote = !isnull && xtype==etSQLESCAPE2; n += i + 1 + needQuote*2; if( n>etBUFSIZE ){ - bufpt = zExtra = sqliteMalloc( n ); + bufpt = zExtra = sqlite3_malloc( n ); if( bufpt==0 ) return -1; }else{ bufpt = buf; @@ -729,29 +729,33 @@ struct sgMprintf { static void mout(void *arg, const char *zNewText, int nNewChar){ struct sgMprintf *pM = (struct sgMprintf*)arg; pM->nTotal += nNewChar; - if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ - if( pM->xRealloc==0 ){ - nNewChar = pM->nAlloc - pM->nChar - 1; - }else{ - int nAlloc = pM->nChar + nNewChar*2 + 1; - if( pM->zText==pM->zBase ){ - pM->zText = pM->xRealloc(0, nAlloc); - if( pM->zText && pM->nChar ){ - memcpy(pM->zText, pM->zBase, pM->nChar); - } + if( pM->zText ){ + if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ + if( pM->xRealloc==0 ){ + nNewChar = pM->nAlloc - pM->nChar - 1; }else{ - char *zNew; - zNew = pM->xRealloc(pM->zText, nAlloc); - if( zNew ){ - pM->zText = zNew; + int nAlloc = pM->nChar + nNewChar*2 + 1; + if( pM->zText==pM->zBase ){ + pM->zText = pM->xRealloc(0, nAlloc); + if( pM->zText==0 ){ + return; + }else if( pM->nChar ){ + memcpy(pM->zText, pM->zBase, pM->nChar); + } }else{ - return; + char *zNew; + zNew = pM->xRealloc(pM->zText, nAlloc); + if( zNew ){ + pM->zText = zNew; + }else{ + pM->xRealloc(pM->zText, 0); + pM->zText = 0; + return; + } } + pM->nAlloc = nAlloc; } - pM->nAlloc = nAlloc; } - } - if( pM->zText ){ if( nNewChar>0 ){ memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); pM->nChar += nNewChar; @@ -798,29 +802,37 @@ static char *base_vprintf( ** Realloc that is a real function, not a macro. */ static void *printf_realloc(void *old, int size){ - return sqliteRealloc(old,size); + return sqlite3_realloc(old,size); } /* ** Print into memory obtained from sqliteMalloc(). Use the internal ** %-conversion extensions. */ -char *sqlite3VMPrintf(const char *zFormat, va_list ap){ +char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ + char *z; char zBase[SQLITE_PRINT_BUF_SIZE]; - return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); + z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); + if( z==0 && db!=0 ){ + db->mallocFailed = 1; + } + return z; } /* ** Print into memory obtained from sqliteMalloc(). Use the internal ** %-conversion extensions. */ -char *sqlite3MPrintf(const char *zFormat, ...){ +char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ va_list ap; char *z; char zBase[SQLITE_PRINT_BUF_SIZE]; va_start(ap, zFormat); z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); va_end(ap); + if( z==0 && db!=0 ){ + db->mallocFailed = 1; + } return z; } diff --git a/src/select.c b/src/select.c index 8c29b57db..e9b1958bd 100644 --- a/src/select.c +++ b/src/select.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** -** $Id: select.c,v 1.354 2007/07/18 18:17:12 drh Exp $ +** $Id: select.c,v 1.355 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" @@ -39,6 +39,7 @@ static void clearSelect(Select *p){ ** structure. */ Select *sqlite3SelectNew( + Parse *pParse, /* Parsing context */ ExprList *pEList, /* which columns to include in the result */ SrcList *pSrc, /* the FROM clause -- which tables to scan */ Expr *pWhere, /* the WHERE clause */ @@ -51,14 +52,15 @@ Select *sqlite3SelectNew( ){ Select *pNew; Select standin; - pNew = sqliteMalloc( sizeof(*pNew) ); + sqlite3 *db = pParse->db; + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */ if( pNew==0 ){ pNew = &standin; memset(pNew, 0, sizeof(*pNew)); } if( pEList==0 ){ - pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0); + pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(TK_ALL,0,0,0), 0); } pNew->pEList = pEList; pNew->pSrc = pSrc; @@ -89,7 +91,7 @@ Select *sqlite3SelectNew( void sqlite3SelectDelete(Select *p){ if( p ){ clearSelect(p); - sqliteFree(p); + sqlite3_free(p); } } @@ -202,10 +204,10 @@ static void setQuotedToken(Token *p, const char *z){ /* ** Create an expression node for an identifier with the name of zName */ -Expr *sqlite3CreateIdExpr(const char *zName){ +Expr *sqlite3CreateIdExpr(Parse *pParse, const char *zName){ Token dummy; setToken(&dummy, zName); - return sqlite3Expr(TK_ID, 0, 0, &dummy); + return sqlite3PExpr(pParse, TK_ID, 0, 0, &dummy); } @@ -214,6 +216,7 @@ Expr *sqlite3CreateIdExpr(const char *zName){ ** zCol column to be equal in the two tables pTab1 and pTab2. */ static void addWhereTerm( + Parse *pParse, /* Parsing context */ const char *zCol, /* Name of the column */ const Table *pTab1, /* First table */ const char *zAlias1, /* Alias for first table. May be NULL */ @@ -226,24 +229,24 @@ static void addWhereTerm( Expr *pE2a, *pE2b, *pE2c; Expr *pE; - pE1a = sqlite3CreateIdExpr(zCol); - pE2a = sqlite3CreateIdExpr(zCol); + pE1a = sqlite3CreateIdExpr(pParse, zCol); + pE2a = sqlite3CreateIdExpr(pParse, zCol); if( zAlias1==0 ){ zAlias1 = pTab1->zName; } - pE1b = sqlite3CreateIdExpr(zAlias1); + pE1b = sqlite3CreateIdExpr(pParse, zAlias1); if( zAlias2==0 ){ zAlias2 = pTab2->zName; } - pE2b = sqlite3CreateIdExpr(zAlias2); - pE1c = sqlite3ExprOrFree(TK_DOT, pE1b, pE1a, 0); - pE2c = sqlite3ExprOrFree(TK_DOT, pE2b, pE2a, 0); - pE = sqlite3ExprOrFree(TK_EQ, pE1c, pE2c, 0); + pE2b = sqlite3CreateIdExpr(pParse, zAlias2); + pE1c = sqlite3PExpr(pParse, TK_DOT, pE1b, pE1a, 0); + pE2c = sqlite3PExpr(pParse, TK_DOT, pE2b, pE2a, 0); + pE = sqlite3DbExpr(pParse, TK_EQ, pE1c, pE2c, 0); if( pE ){ ExprSetProperty(pE, EP_FromJoin); pE->iRightJoinTable = iRightJoinTable; } - pE = sqlite3ExprAnd(*ppExpr, pE); + pE = sqlite3ExprAnd(pParse->db,*ppExpr, pE); if( pE ){ *ppExpr = pE; } @@ -346,7 +349,7 @@ static int sqliteProcessJoin(Parse *pParse, Select *p){ */ if( pRight->pOn ){ setJoinExpr(pRight->pOn, pRight->iCursor); - p->pWhere = sqlite3ExprAnd(p->pWhere, pRight->pOn); + p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn); pRight->pOn = 0; } @@ -689,7 +692,7 @@ static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){ int i; nExpr = pList->nExpr; - pInfo = sqliteMalloc( sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) ); + pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) ); if( pInfo ){ pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr]; pInfo->nField = nExpr; @@ -992,7 +995,7 @@ static void generateColumnNames( #endif assert( v!=0 ); - if( pParse->colNamesSet || v==0 || sqlite3MallocFailed() ) return; + if( pParse->colNamesSet || v==0 || db->mallocFailed ) return; pParse->colNamesSet = 1; fullNames = (db->flags & SQLITE_FullColNames)!=0; shortNames = (db->flags & SQLITE_ShortColNames)!=0; @@ -1076,6 +1079,7 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ int i, j; ExprList *pEList; Column *aCol, *pCol; + sqlite3 *db = pParse->db; while( pSelect->pPrior ) pSelect = pSelect->pPrior; if( prepSelectStmt(pParse, pSelect) ){ @@ -1084,16 +1088,16 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ if( sqlite3SelectResolve(pParse, pSelect, 0) ){ return 0; } - pTab = sqliteMalloc( sizeof(Table) ); + pTab = sqlite3DbMallocZero(db, sizeof(Table) ); if( pTab==0 ){ return 0; } pTab->nRef = 1; - pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; + pTab->zName = zTabName ? sqlite3DbStrDup(db, zTabName) : 0; pEList = pSelect->pEList; pTab->nCol = pEList->nExpr; assert( pTab->nCol>0 ); - pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); + pTab->aCol = aCol = sqlite3DbMallocZero(db, sizeof(pTab->aCol[0])*pTab->nCol); for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){ Expr *p, *pR; char *zType; @@ -1109,21 +1113,21 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 ); if( (zName = pEList->a[i].zName)!=0 ){ /* If the column contains an "AS <name>" phrase, use <name> as the name */ - zName = sqliteStrDup(zName); + zName = sqlite3DbStrDup(db, zName); }else if( p->op==TK_DOT && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ /* For columns of the from A.B use B as the name */ - zName = sqlite3MPrintf("%T", &pR->token); + zName = sqlite3MPrintf(db, "%T", &pR->token); }else if( p->span.z && p->span.z[0] ){ /* Use the original text of the column expression as its name */ - zName = sqlite3MPrintf("%T", &p->span); + zName = sqlite3MPrintf(db, "%T", &p->span); }else{ /* If all else fails, make up a name */ - zName = sqlite3MPrintf("column%d", i+1); + zName = sqlite3MPrintf(db, "column%d", i+1); } sqlite3Dequote(zName); - if( sqlite3MallocFailed() ){ - sqliteFree(zName); + if( db->mallocFailed ){ + sqlite3_free(zName); sqlite3DeleteTable(pTab); return 0; } @@ -1147,12 +1151,12 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ */ memset(&sNC, 0, sizeof(sNC)); sNC.pSrcList = pSelect->pSrc; - zType = sqliteStrDup(columnType(&sNC, p, 0, 0, 0)); + zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0)); pCol->zType = zType; pCol->affinity = sqlite3ExprAffinity(p); pColl = sqlite3ExprCollSeq(pParse, p); if( pColl ){ - pCol->zColl = sqliteStrDup(pColl->zName); + pCol->zColl = sqlite3DbStrDup(db, pColl->zName); } } pTab->iPKey = -1; @@ -1190,8 +1194,9 @@ static int prepSelectStmt(Parse *pParse, Select *p){ SrcList *pTabList; ExprList *pEList; struct SrcList_item *pFrom; + sqlite3 *db = pParse->db; - if( p==0 || p->pSrc==0 || sqlite3MallocFailed() ){ + if( p==0 || p->pSrc==0 || db->mallocFailed ){ return 1; } pTabList = p->pSrc; @@ -1255,7 +1260,7 @@ static int prepSelectStmt(Parse *pParse, Select *p){ ** in the inner view. */ if( pFrom->pSelect==0 ){ - pFrom->pSelect = sqlite3SelectDup(pTab->pSelect); + pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect); } } #endif @@ -1301,7 +1306,7 @@ static int prepSelectStmt(Parse *pParse, Select *p){ (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ /* This particular expression does not need to be expanded. */ - pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); + pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0); if( pNew ){ pNew->a[pNew->nExpr-1].zName = a[k].zName; }else{ @@ -1315,7 +1320,7 @@ static int prepSelectStmt(Parse *pParse, Select *p){ int tableSeen = 0; /* Set to 1 when TABLE matches */ char *zTName; /* text of name of TABLE */ if( pE->op==TK_DOT && pE->pLeft ){ - zTName = sqlite3NameFromToken(&pE->pLeft->token); + zTName = sqlite3NameFromToken(db, &pE->pLeft->token); }else{ zTName = 0; } @@ -1376,9 +1381,9 @@ static int prepSelectStmt(Parse *pParse, Select *p){ pExpr->span.dyn = 0; } if( longNames ){ - pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span); + pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pExpr->span); }else{ - pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); + pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pRight->token); } } } @@ -1390,7 +1395,7 @@ static int prepSelectStmt(Parse *pParse, Select *p){ } rc = 1; } - sqliteFree(zTName); + sqlite3_free(zTName); } } sqlite3ExprListDelete(pEList); @@ -1400,7 +1405,7 @@ static int prepSelectStmt(Parse *pParse, Select *p){ sqlite3ErrorMsg(pParse, "too many columns in result set"); rc = SQLITE_ERROR; } - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ rc = SQLITE_NOMEM; } return rc; @@ -1430,6 +1435,7 @@ static int matchOrderbyToColumn( int nErr = 0; int i, j; ExprList *pEList; + sqlite3 *db = pParse->db; if( pSelect==0 || pOrderBy==0 ) return 1; if( mustComplete ){ @@ -1462,23 +1468,23 @@ static int matchOrderbyToColumn( if( !mustComplete ) continue; iCol--; } - if( iCol<0 && (zLabel = sqlite3NameFromToken(&pE->token))!=0 ){ + if( iCol<0 && (zLabel = sqlite3NameFromToken(db, &pE->token))!=0 ){ for(j=0, pItem=pEList->a; j<pEList->nExpr; j++, pItem++){ char *zName; int isMatch; if( pItem->zName ){ - zName = sqlite3StrDup(pItem->zName); + zName = sqlite3DbStrDup(db, pItem->zName); }else{ - zName = sqlite3NameFromToken(&pItem->pExpr->token); + zName = sqlite3NameFromToken(db, &pItem->pExpr->token); } isMatch = zName && sqlite3StrICmp(zName, zLabel)==0; - sqliteFree(zName); + sqlite3_free(zName); if( isMatch ){ iCol = j; break; } } - sqliteFree(zLabel); + sqlite3_free(zLabel); } if( iCol>=0 ){ pE->op = TK_COLUMN; @@ -1962,7 +1968,8 @@ static int multiSelect( assert( p->pRightmost==p ); nKeyCol = nCol + (pOrderBy ? pOrderBy->nExpr : 0); - pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1)); + pKeyInfo = sqlite3DbMallocZero(pParse->db, + sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1)); if( !pKeyInfo ){ rc = SQLITE_NOMEM; goto multi_select_end; @@ -2037,7 +2044,7 @@ static int multiSelect( generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); } - sqliteFree(pKeyInfo); + sqlite3_free(pKeyInfo); } multi_select_end: @@ -2046,6 +2053,10 @@ multi_select_end: #endif /* SQLITE_OMIT_COMPOUND_SELECT */ #ifndef SQLITE_OMIT_VIEW +/* Forward Declarations */ +static void substExprList(sqlite3*, ExprList*, int, ExprList*); +static void substSelect(sqlite3*, Select *, int, ExprList *); + /* ** Scan through the expression pExpr. Replace every reference to ** a column in table number iTable with a copy of the iColumn-th @@ -2059,9 +2070,12 @@ multi_select_end: ** changes to pExpr so that it refers directly to the source table ** of the subquery rather the result set of the subquery. */ -static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ -static void substSelect(Select *, int, ExprList *); /* Forward Decl */ -static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ +static void substExpr( + sqlite3 *db, /* Report malloc errors to this connection */ + Expr *pExpr, /* Expr in which substitution occurs */ + int iTable, /* Table to be substituted */ + ExprList *pEList /* Substitute expressions */ +){ if( pExpr==0 ) return; if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ if( pExpr->iColumn<0 ){ @@ -2074,42 +2088,52 @@ static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ assert( pNew!=0 ); pExpr->op = pNew->op; assert( pExpr->pLeft==0 ); - pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); + pExpr->pLeft = sqlite3ExprDup(db, pNew->pLeft); assert( pExpr->pRight==0 ); - pExpr->pRight = sqlite3ExprDup(pNew->pRight); + pExpr->pRight = sqlite3ExprDup(db, pNew->pRight); assert( pExpr->pList==0 ); - pExpr->pList = sqlite3ExprListDup(pNew->pList); + pExpr->pList = sqlite3ExprListDup(db, pNew->pList); pExpr->iTable = pNew->iTable; pExpr->pTab = pNew->pTab; pExpr->iColumn = pNew->iColumn; pExpr->iAgg = pNew->iAgg; - sqlite3TokenCopy(&pExpr->token, &pNew->token); - sqlite3TokenCopy(&pExpr->span, &pNew->span); - pExpr->pSelect = sqlite3SelectDup(pNew->pSelect); + sqlite3TokenCopy(db, &pExpr->token, &pNew->token); + sqlite3TokenCopy(db, &pExpr->span, &pNew->span); + pExpr->pSelect = sqlite3SelectDup(db, pNew->pSelect); pExpr->flags = pNew->flags; } }else{ - substExpr(pExpr->pLeft, iTable, pEList); - substExpr(pExpr->pRight, iTable, pEList); - substSelect(pExpr->pSelect, iTable, pEList); - substExprList(pExpr->pList, iTable, pEList); + substExpr(db, pExpr->pLeft, iTable, pEList); + substExpr(db, pExpr->pRight, iTable, pEList); + substSelect(db, pExpr->pSelect, iTable, pEList); + substExprList(db, pExpr->pList, iTable, pEList); } } -static void substExprList(ExprList *pList, int iTable, ExprList *pEList){ +static void substExprList( + sqlite3 *db, /* Report malloc errors here */ + ExprList *pList, /* List to scan and in which to make substitutes */ + int iTable, /* Table to be substituted */ + ExprList *pEList /* Substitute values */ +){ int i; if( pList==0 ) return; for(i=0; i<pList->nExpr; i++){ - substExpr(pList->a[i].pExpr, iTable, pEList); + substExpr(db, pList->a[i].pExpr, iTable, pEList); } } -static void substSelect(Select *p, int iTable, ExprList *pEList){ +static void substSelect( + sqlite3 *db, /* Report malloc errors here */ + Select *p, /* SELECT statement in which to make substitutions */ + int iTable, /* Table to be replaced */ + ExprList *pEList /* Substitute values */ +){ if( !p ) return; - substExprList(p->pEList, iTable, pEList); - substExprList(p->pGroupBy, iTable, pEList); - substExprList(p->pOrderBy, iTable, pEList); - substExpr(p->pHaving, iTable, pEList); - substExpr(p->pWhere, iTable, pEList); - substSelect(p->pPrior, iTable, pEList); + substExprList(db, p->pEList, iTable, pEList); + substExprList(db, p->pGroupBy, iTable, pEList); + substExprList(db, p->pOrderBy, iTable, pEList); + substExpr(db, p->pHaving, iTable, pEList); + substExpr(db, p->pWhere, iTable, pEList); + substSelect(db, p->pPrior, iTable, pEList); } #endif /* !defined(SQLITE_OMIT_VIEW) */ @@ -2192,6 +2216,7 @@ static void substSelect(Select *p, int iTable, ExprList *pEList){ ** the subquery before this routine runs. */ static int flattenSubquery( + sqlite3 *db, /* Database connection */ Select *p, /* The parent or outer SELECT statement */ int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ int isAgg, /* True if outer SELECT uses aggregate functions */ @@ -2289,13 +2314,13 @@ static int flattenSubquery( int jointype = pSubitem->jointype; sqlite3DeleteTable(pSubitem->pTab); - sqliteFree(pSubitem->zDatabase); - sqliteFree(pSubitem->zName); - sqliteFree(pSubitem->zAlias); + sqlite3_free(pSubitem->zDatabase); + sqlite3_free(pSubitem->zName); + sqlite3_free(pSubitem->zAlias); if( nSubSrc>1 ){ int extra = nSubSrc - 1; for(i=1; i<nSubSrc; i++){ - pSrc = sqlite3SrcListAppend(pSrc, 0, 0); + pSrc = sqlite3SrcListAppend(db, pSrc, 0, 0); } p->pSrc = pSrc; for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ @@ -2325,7 +2350,8 @@ static int flattenSubquery( for(i=0; i<pList->nExpr; i++){ Expr *pExpr; if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ - pList->a[i].zName = sqliteStrNDup((char*)pExpr->span.z, pExpr->span.n); + pList->a[i].zName = + sqlite3DbStrNDup(db, (char*)pExpr->span.z, pExpr->span.n); } } substExprList(p->pEList, iParent, pSub->pEList); @@ -2341,7 +2367,7 @@ static int flattenSubquery( substExprList(p->pOrderBy, iParent, pSub->pEList); } if( pSub->pWhere ){ - pWhere = sqlite3ExprDup(pSub->pWhere); + pWhere = sqlite3ExprDup(db, pSub->pWhere); }else{ pWhere = 0; } @@ -2350,12 +2376,13 @@ static int flattenSubquery( p->pHaving = p->pWhere; p->pWhere = pWhere; substExpr(p->pHaving, iParent, pSub->pEList); - p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving)); + p->pHaving = sqlite3ExprAnd(db, p->pHaving, + sqlite3ExprDup(db, pSub->pHaving)); assert( p->pGroupBy==0 ); - p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); + p->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy); }else{ substExpr(p->pWhere, iParent, pSub->pEList); - p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere); + p->pWhere = sqlite3ExprAnd(db, p->pWhere, pWhere); } /* The flattened query is distinct if either the inner or the @@ -2571,7 +2598,8 @@ static int processOrderGroupBy( CollSeq *pColl = pE->pColl; int flags = pE->flags & EP_ExpCollate; sqlite3ExprDelete(pE); - pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); + pE = sqlite3ExprDup(pParse->db, pEList->a[iCol-1].pExpr); + pOrderBy->a[i].pExpr = pE; if( pColl && flags ){ pE->pColl = pColl; pE->flags |= flags; @@ -2690,7 +2718,7 @@ int sqlite3SelectResolve( } } - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ return SQLITE_NOMEM; } @@ -2897,8 +2925,10 @@ int sqlite3Select( int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */ AggInfo sAggInfo; /* Information used by aggregate queries */ int iEnd; /* Address of the end of the query */ + sqlite3 *db; /* The database connection */ - if( p==0 || sqlite3MallocFailed() || pParse->nErr ){ + db = pParse->db; + if( p==0 || db->mallocFailed || pParse->nErr ){ return 1; } if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; @@ -3027,7 +3057,7 @@ int sqlite3Select( */ #ifndef SQLITE_OMIT_VIEW if( pParent && pParentAgg && - flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){ + flattenSubquery(db, pParent, parentTab, *pParentAgg, isAgg) ){ if( isAgg ) *pParentAgg = 1; goto select_end; } @@ -3154,7 +3184,7 @@ int sqlite3Select( goto select_end; } } - if( sqlite3MallocFailed() ) goto select_end; + if( db->mallocFailed ) goto select_end; /* Processing for aggregates with GROUP BY is very different and ** much more complex tha aggregates without a GROUP BY. @@ -3403,8 +3433,8 @@ select_end: generateColumnNames(pParse, pTabList, pEList); } - sqliteFree(sAggInfo.aCol); - sqliteFree(sAggInfo.aFunc); + sqlite3_free(sAggInfo.aCol); + sqlite3_free(sAggInfo.aFunc); return rc; } diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 019b0ef04..1adba20d4 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.586 2007/08/15 13:04:54 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.587 2007/08/16 04:30:40 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -449,6 +449,7 @@ struct sqlite3 { int errMask; /* & result codes with this before returning */ u8 autoCommit; /* The auto-commit flag. */ u8 temp_store; /* 1: file 2: memory 0: default */ + u8 mallocFailed; /* True if we have seen a malloc failure */ int nTable; /* Number of tables in the database */ CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ i64 lastRowid; /* ROWID of most recent insert (see above) */ @@ -1579,21 +1580,17 @@ int sqlite3StrICmp(const char *, const char *); int sqlite3StrNICmp(const char *, const char *, int); int sqlite3IsNumber(const char*, int*, u8); -void *sqlite3Malloc(int,int); -void *sqlite3MallocRaw(int,int); -void *sqlite3Realloc(void*,int); +void *sqlite3MallocZero(unsigned); +void *sqlite3DbMallocZero(sqlite3*, unsigned); +void *sqlite3DbMallocRaw(sqlite3*, unsigned); +void *sqlite3ReallocOrFree(sqlite3*,void*,int); char *sqlite3StrDup(const char*); char *sqlite3StrNDup(const char*, int); -# define sqlite3CheckMemory(a,b) -void *sqlite3ReallocOrFree(void*,int); -void sqlite3FreeX(void*); -void *sqlite3MallocX(int); -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - int sqlite3AllocSize(void *); -#endif +char *sqlite3DbStrDup(sqlite3*,const char*); +char *sqlite3DbStrNDup(sqlite3*,const char*, int); -char *sqlite3MPrintf(const char*, ...); -char *sqlite3VMPrintf(const char*, va_list); +char *sqlite3MPrintf(sqlite3*,const char*, ...); +char *sqlite3VMPrintf(sqlite3*,const char*, va_list); #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) void sqlite3DebugPrintf(const char*, ...); void *sqlite3TextToPtr(const char*); @@ -1602,19 +1599,19 @@ void sqlite3SetString(char **, ...); void sqlite3ErrorMsg(Parse*, const char*, ...); void sqlite3ErrorClear(Parse*); void sqlite3Dequote(char*); -void sqlite3DequoteExpr(Expr*); +void sqlite3DequoteExpr(sqlite3*, Expr*); int sqlite3KeywordCode(const unsigned char*, int); int sqlite3RunParser(Parse*, const char*, char **); void sqlite3FinishCoding(Parse*); Expr *sqlite3Expr(int, Expr*, Expr*, const Token*); -Expr *sqlite3ExprOrFree(int, Expr*, Expr*, const Token*); +Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*); Expr *sqlite3RegisterExpr(Parse*,Token*); -Expr *sqlite3ExprAnd(Expr*, Expr*); +Expr *sqlite3ExprAnd(sqlite*,Expr*, Expr*); void sqlite3ExprSpan(Expr*,Token*,Token*); -Expr *sqlite3ExprFunction(ExprList*, Token*); +Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*); void sqlite3ExprAssignVarNumber(Parse*, Expr*); void sqlite3ExprDelete(Expr*); -ExprList *sqlite3ExprListAppend(ExprList*,Expr*,Token*); +ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*,Token*); void sqlite3ExprListDelete(ExprList*); int sqlite3Init(sqlite3*, char**); int sqlite3InitCallback(void*, int, char**, char**); @@ -1645,11 +1642,11 @@ void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int); void sqlite3DropTable(Parse*, SrcList*, int, int); void sqlite3DeleteTable(Table*); void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int); -void *sqlite3ArrayAllocate(void*,int,int,int*,int*,int*); -IdList *sqlite3IdListAppend(IdList*, Token*); +void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*); +IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*); int sqlite3IdListIndex(IdList*,const char*); -SrcList *sqlite3SrcListAppend(SrcList*, Token*, Token*); -SrcList *sqlite3SrcListAppendFromTerm(SrcList*, Token*, Token*, Token*, +SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*); +SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*, Token*, Select*, Expr*, IdList*); void sqlite3SrcListShiftJoinType(SrcList*); void sqlite3SrcListAssignCursors(Parse*, SrcList*); @@ -1659,8 +1656,8 @@ void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, Token*, int, int); void sqlite3DropIndex(Parse*, SrcList*, int); int sqlite3Select(Parse*, Select*, int, int, Select*, int, int*, char *aff); -Select *sqlite3SelectNew(ExprList*,SrcList*,Expr*,ExprList*,Expr*,ExprList*, - int,Expr*,Expr*); +Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, + Expr*,ExprList*,int,Expr*,Expr*); void sqlite3SelectDelete(Select*); Table *sqlite3SrcListLookup(Parse*, SrcList*); int sqlite3IsReadOnly(Parse*, Table*, int); @@ -1682,9 +1679,8 @@ void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); void sqlite3Vacuum(Parse*); int sqlite3RunVacuum(char**, sqlite3*); -char *sqlite3NameFromToken(Token*); +char *sqlite3NameFromToken(sqlite3*, Token*); int sqlite3ExprCompare(Expr*, Expr*); -int sqliteFuncId(Token*); int sqlite3ExprResolveNames(NameContext *, Expr *); int sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); int sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); @@ -1708,12 +1704,12 @@ void sqlite3GenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int); void sqlite3CompleteInsertion(Parse*, Table*, int, char*, int, int, int, int); void sqlite3OpenTableAndIndices(Parse*, Table*, int, int); void sqlite3BeginWriteOperation(Parse*, int, int); -Expr *sqlite3ExprDup(Expr*); -void sqlite3TokenCopy(Token*, Token*); -ExprList *sqlite3ExprListDup(ExprList*); -SrcList *sqlite3SrcListDup(SrcList*); -IdList *sqlite3IdListDup(IdList*); -Select *sqlite3SelectDup(Select*); +Expr *sqlite3ExprDup(sqlite3*,Expr*); +void sqlite3TokenCopy(sqlite3*,Token*, Token*); +ExprList *sqlite3ExprListDup(sqlite3*,ExprList*); +SrcList *sqlite3SrcListDup(sqlite3*,SrcList*); +IdList *sqlite3IdListDup(sqlite3*,IdList*); +Select *sqlite3SelectDup(sqlite3*,Select*); FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int); void sqlite3RegisterBuiltinFunctions(sqlite3*); void sqlite3RegisterDateTimeFunctions(sqlite3*); @@ -1733,10 +1729,11 @@ void sqlite3ChangeCookie(sqlite3*, Vdbe*, int); int, int); void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*); void sqlite3DeleteTriggerStep(TriggerStep*); - TriggerStep *sqlite3TriggerSelectStep(Select*); - TriggerStep *sqlite3TriggerInsertStep(Token*, IdList*, ExprList*,Select*,int); - TriggerStep *sqlite3TriggerUpdateStep(Token*, ExprList*, Expr*, int); - TriggerStep *sqlite3TriggerDeleteStep(Token*, Expr*); + TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*); + TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*, + ExprList*,Select*,int); + TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, int); + TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*); void sqlite3DeleteTrigger(Trigger*); void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*); #else @@ -1873,23 +1870,12 @@ void sqlite3Parser(void*, int, Token, Parse*); int sqlite3Utf8To8(unsigned char*); #endif -#ifdef SQLITE_MEMDEBUG - void sqlite3MallocDisallow(void); - void sqlite3MallocAllow(void); - int sqlite3TestMallocFail(void); -#else - #define sqlite3TestMallocFail() 0 - #define sqlite3MallocDisallow() - #define sqlite3MallocAllow() -#endif +/* +** FIX ME: create these routines +*/ +#define sqlite3MallocDisallow() +#define sqlite3MallocAllow() -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - void *sqlite3ThreadSafeMalloc(int); - void sqlite3ThreadSafeFree(void *); -#else - #define sqlite3ThreadSafeMalloc sqlite3MallocX - #define sqlite3ThreadSafeFree sqlite3FreeX -#endif #ifdef SQLITE_OMIT_VIRTUALTABLE # define sqlite3VtabClear(X) diff --git a/src/test1.c b/src/test1.c index b86f4c8f6..f95674290 100644 --- a/src/test1.c +++ b/src/test1.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test1.c,v 1.259 2007/08/08 12:11:21 drh Exp $ +** $Id: test1.c,v 1.260 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -415,7 +415,7 @@ static int test_mprintf_z( zResult = sqlite3MPrintf("%z%s%s", zResult, argv[1], argv[i]); } Tcl_AppendResult(interp, zResult, 0); - sqliteFree(zResult); + sqlite3_free(zResult); return TCL_OK; } @@ -434,7 +434,7 @@ static int test_mprintf_n( char *zStr; int n = 0; zStr = sqlite3MPrintf("%s%n", argv[1], &n); - sqliteFree(zStr); + sqlite3_free(zStr); Tcl_SetObjResult(interp, Tcl_NewIntObj(n)); return TCL_OK; } @@ -689,9 +689,9 @@ static void dstrAppend(struct dstr *p, const char *z, int divider){ if( p->nUsed + n + 2 > p->nAlloc ){ char *zNew; p->nAlloc = p->nAlloc*2 + n + 200; - zNew = sqliteRealloc(p->z, p->nAlloc); + zNew = sqlite3_realloc(p->z, p->nAlloc); if( zNew==0 ){ - sqliteFree(p->z); + sqlite3_free(p->z); memset(p, 0, sizeof(*p)); return; } @@ -742,7 +742,7 @@ static void sqlite3ExecFunc( (char*)sqlite3_value_text(argv[0]), execFuncCallback, &x, 0); sqlite3_result_text(context, x.z, x.nUsed, SQLITE_TRANSIENT); - sqliteFree(x.z); + sqlite3_free(x.z); } /* @@ -917,7 +917,7 @@ static int test_create_function( ** because it is not tested anywhere else. */ if( rc==SQLITE_OK ){ sqlite3_value *pVal; -#ifdef SQLITE_MEMDEBUG +#ifdef 0 if( sqlite3_iMallocFail>0 ){ sqlite3_iMallocFail++; } @@ -1299,125 +1299,6 @@ static int sqlite3_mprintf_hexdouble( } /* -** Usage: sqlite_malloc_fail N ?REPEAT-INTERVAL? -** -** Rig sqliteMalloc() to fail on the N-th call and every REPEAT-INTERVAL call -** after that. If REPEAT-INTERVAL is 0 or is omitted, then only a single -** malloc will fail. If REPEAT-INTERVAL is 1 then all mallocs after the -** first failure will continue to fail on every call. If REPEAT-INTERVAL is -** 2 then every other malloc will fail. And so forth. -** -** Turn off this mechanism and reset the sqlite3ThreadData()->mallocFailed -** variable if N==0. -*/ -#ifdef SQLITE_MEMDEBUG -static int sqlite_malloc_fail( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - char **argv /* Text of each argument */ -){ - int n; - int rep; - if( argc!=2 && argc!=3 ){ - Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " N\"", 0); - return TCL_ERROR; - } - if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; - if( argc==3 ){ - if( Tcl_GetInt(interp, argv[2], &rep) ) return TCL_ERROR; - }else{ - rep = 0; - } - sqlite3_iMallocFail = n; - sqlite3_iMallocReset = rep; - return TCL_OK; -} -#endif - -/* -** Usage: sqlite_malloc_stat -** -** Return the number of prior calls to sqliteMalloc() and sqliteFree(). -*/ -#ifdef SQLITE_MEMDEBUG -static int sqlite_malloc_stat( - void *NotUsed, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int argc, /* Number of arguments */ - char **argv /* Text of each argument */ -){ - char zBuf[200]; - sprintf(zBuf, "%d %d %d", sqlite3_nMalloc,sqlite3_nFree,sqlite3_iMallocFail); - Tcl_AppendResult(interp, zBuf, 0); - return TCL_OK; -} - -/* -** This function implements a Tcl command that may be invoked using any of -** the four forms enumerated below. -** -** sqlite_malloc_outstanding -** Return a summary of all unfreed blocks of memory allocated by the -** current thread. See comments above function sqlite3OutstandingMallocs() -** in util.c for a description of the returned value. -** -** sqlite_malloc_outstanding -bytes -** Return the total amount of unfreed memory (in bytes) allocated by -** this thread. -** -** sqlite_malloc_outstanding -maxbytes -** Return the maximum amount of dynamic memory in use at one time -** by this thread. -** -** sqlite_malloc_outstanding -clearmaxbytes -** Set the value returned by [sqlite_malloc_outstanding -maxbytes] -** to the current value of [sqlite_malloc_outstanding -bytes]. -*/ -static int sqlite_malloc_outstanding( - ClientData clientData, - Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ - int objc, /* Number of arguments */ - Tcl_Obj *CONST objv[] /* Command arguments */ -){ - extern int sqlite3OutstandingMallocs(Tcl_Interp *interp); - -#if defined(SQLITE_DEBUG) && defined(SQLITE_MEMDEBUG) && SQLITE_MEMDEBUG>1 - if( objc==2 ){ - const char *zArg = Tcl_GetString(objv[1]); -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT - ThreadData const *pTd = sqlite3ThreadDataReadOnly(); - if( 0==strcmp(zArg, "-bytes") ){ - Tcl_SetObjResult(interp, Tcl_NewIntObj(pTd->nAlloc)); - }else if( 0==strcmp(zArg, "-clearmaxbytes") ){ - sqlite3_nMaxAlloc = pTd->nAlloc; - }else -#endif - if( 0==strcmp(zArg, "-maxbytes") ){ - Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sqlite3_nMaxAlloc)); - }else{ - Tcl_AppendResult(interp, "bad option \"", zArg, - "\": must be -bytes, -maxbytes or -clearmaxbytes", 0 - ); - return TCL_ERROR; - } - - return TCL_OK; - } - - if( objc!=1 ){ - Tcl_WrongNumArgs(interp, 1, objv, "?-bytes?"); - return TCL_ERROR; - } - - return sqlite3OutstandingMallocs(interp); -#else - return TCL_OK; -#endif -} -#endif - -/* ** Usage: sqlite3_enable_shared_cache BOOLEAN ** */ @@ -4304,10 +4185,6 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "sqlite3_create_aggregate", (Tcl_CmdProc*)test_create_aggregate }, { "sqlite_register_test_function", (Tcl_CmdProc*)test_register_func }, { "sqlite_abort", (Tcl_CmdProc*)sqlite_abort }, -#ifdef SQLITE_MEMDEBUG - { "sqlite_malloc_fail", (Tcl_CmdProc*)sqlite_malloc_fail }, - { "sqlite_malloc_stat", (Tcl_CmdProc*)sqlite_malloc_stat }, -#endif { "sqlite_bind", (Tcl_CmdProc*)test_bind }, { "breakpoint", (Tcl_CmdProc*)test_breakpoint }, { "sqlite3_key", (Tcl_CmdProc*)test_key }, @@ -4420,9 +4297,6 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ { "add_test_collate_needed", test_collate_needed, 0 }, { "add_test_function", test_function, 0 }, #endif -#ifdef SQLITE_MEMDEBUG - { "sqlite_malloc_outstanding", sqlite_malloc_outstanding, 0}, -#endif { "sqlite3_test_errstr", test_errstr, 0 }, { "tcl_variable_type", tcl_variable_type, 0 }, #ifndef SQLITE_OMIT_SHARED_CACHE @@ -4512,16 +4386,6 @@ int Sqlitetest1_Init(Tcl_Interp *interp){ Tcl_LinkVar(interp, "sqlite_last_needed_collation", (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY); #endif -#ifdef SQLITE_MEMDEBUG - { - extern char *sqlite3_malloc_id; - extern int sqlite3_mallocfail_trace; - Tcl_LinkVar(interp, "sqlite_malloc_id", - (char*)&sqlite3_malloc_id, TCL_LINK_STRING); - Tcl_LinkVar(interp, "sqlite3_mallocfail_trace", - (char*)&sqlite3_mallocfail_trace, TCL_LINK_INT); - } -#endif #if OS_WIN Tcl_LinkVar(interp, "sqlite_os_type", (char*)&sqlite3_os_type, TCL_LINK_INT); diff --git a/src/test3.c b/src/test3.c index 720cad935..e5b2cc185 100644 --- a/src/test3.c +++ b/src/test3.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test3.c,v 1.75 2007/05/17 14:45:13 danielk1977 Exp $ +** $Id: test3.c,v 1.76 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "pager.h" @@ -577,7 +577,7 @@ static int btree_integrity_check( } pBt = sqlite3TextToPtr(argv[1]); nRoot = argc-2; - aRoot = (int*)malloc( sizeof(int)*(argc-2) ); + aRoot = (int*)sqlite3_malloc( sizeof(int)*(argc-2) ); for(i=0; i<argc-2; i++){ if( Tcl_GetInt(interp, argv[i+2], &aRoot[i]) ) return TCL_ERROR; } @@ -586,7 +586,7 @@ static int btree_integrity_check( #else zResult = 0; #endif - free((void*)aRoot); + sqlite3_free((void*)aRoot); if( zResult ){ Tcl_AppendResult(interp, zResult, 0); sqliteFree(zResult); @@ -1014,7 +1014,7 @@ static int btree_key( sqlite3_snprintf(sizeof(zBuf2),zBuf2, "%llu", n); Tcl_AppendResult(interp, zBuf2, 0); }else{ - zBuf = malloc( n+1 ); + zBuf = sqlite3_malloc( n+1 ); rc = sqlite3BtreeKey(pCur, 0, n, zBuf); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); @@ -1022,7 +1022,7 @@ static int btree_key( } zBuf[n] = 0; Tcl_AppendResult(interp, zBuf, 0); - free(zBuf); + sqlite3_free(zBuf); } return SQLITE_OK; } @@ -1054,7 +1054,7 @@ static int btree_data( }else{ n = atoi(argv[2]); } - zBuf = malloc( n+1 ); + zBuf = sqlite3_malloc( n+1 ); rc = sqlite3BtreeData(pCur, 0, n, zBuf); if( rc ){ Tcl_AppendResult(interp, errorName(rc), 0); @@ -1063,7 +1063,7 @@ static int btree_data( } zBuf[n] = 0; Tcl_AppendResult(interp, zBuf, 0); - free(zBuf); + sqlite3_free(zBuf); return SQLITE_OK; } diff --git a/src/test4.c b/src/test4.c index 4336e3ff5..520ad2a68 100644 --- a/src/test4.c +++ b/src/test4.c @@ -11,7 +11,7 @@ ************************************************************************* ** Code for testing the the SQLite library in a multithreaded environment. ** -** $Id: test4.c,v 1.17 2006/02/23 21:43:56 drh Exp $ +** $Id: test4.c,v 1.18 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -143,7 +143,7 @@ static int tcl_thread_create( } threadset[i].busy = 1; sqliteFree(threadset[i].zFilename); - threadset[i].zFilename = sqliteStrDup(argv[2]); + threadset[i].zFilename = sqlite3StrDup(argv[2]); threadset[i].opnum = 1; threadset[i].completed = 0; rc = pthread_create(&x, 0, thread_main, &threadset[i]); @@ -477,7 +477,7 @@ static int tcl_thread_compile( thread_wait(&threadset[i]); threadset[i].xOp = do_compile; sqliteFree(threadset[i].zArg); - threadset[i].zArg = sqliteStrDup(argv[2]); + threadset[i].zArg = sqlite3StrDup(argv[2]); threadset[i].opnum++; return TCL_OK; } diff --git a/src/test5.c b/src/test5.c index 79f9bb419..c9b86c2bc 100644 --- a/src/test5.c +++ b/src/test5.c @@ -15,7 +15,7 @@ ** is used for testing the SQLite routines for converting between ** the various supported unicode encodings. ** -** $Id: test5.c,v 1.16 2007/05/08 20:37:40 drh Exp $ +** $Id: test5.c,v 1.17 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "vdbeInt.h" @@ -155,14 +155,14 @@ static int test_translate( if( enc_from==SQLITE_UTF8 ){ z = Tcl_GetString(objv[1]); if( objc==5 ){ - z = sqliteStrDup(z); + z = sqlite3StrDup(z); } sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); }else{ z = (char*)Tcl_GetByteArrayFromObj(objv[1], &len); if( objc==5 ){ char *zTmp = z; - z = sqliteMalloc(len); + z = sqlite3_malloc(len); memcpy(z, zTmp, len); } sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); diff --git a/src/test6.c b/src/test6.c index 534a09f1b..dc8d0913d 100644 --- a/src/test6.c +++ b/src/test6.c @@ -206,7 +206,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ rc = sqlite3OsTruncate(pFile->pRealFile, pWrite->iOffset); } *ppPtr = pWrite->pNext; - sqliteFree(pWrite); + sqlite3_free(pWrite); break; } case 2: { /* Do nothing */ @@ -218,7 +218,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ sqlite3_int64 iFirst = (pWrite->iOffset%g.iSectorSize); sqlite3_int64 iLast = (pWrite->iOffset+pWrite->nBuf-1)%g.iSectorSize; - zGarbage = sqliteMalloc(g.iSectorSize); + zGarbage = sqlite3_malloc(g.iSectorSize); if( zGarbage ){ sqlite3_int64 i; for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){ @@ -227,7 +227,7 @@ static int writeListSync(CrashFile *pFile, int isCrash){ pFile->pRealFile, i*g.iSectorSize, zGarbage, g.iSectorSize ); } - sqliteFree(zGarbage); + sqlite3_free(zGarbage); }else{ rc = SQLITE_NOMEM; } @@ -263,7 +263,7 @@ static int writeListAppend( assert((zBuf && nBuf) || (!nBuf && !zBuf)); - pNew = (WriteBuffer *)sqliteMalloc(sizeof(WriteBuffer) + nBuf); + pNew = (WriteBuffer *)sqlite3_malloc(sizeof(WriteBuffer) + nBuf); pNew->iOffset = iOffset; pNew->nBuf = nBuf; pNew->pFile = (CrashFile *)pFile; @@ -471,7 +471,7 @@ int sqlite3CrashFileOpen( CrashFile *pWrapper = (CrashFile *)pFile; int rc = SQLITE_NOMEM; sqlite3_file *pReal; - pReal = (sqlite3_file *)sqliteMalloc(pVfs->szOsFile); + pReal = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile); if( pReal ){ pWrapper->pMethod = &CrashFileVtab; pWrapper->zName = zName; @@ -479,7 +479,7 @@ int sqlite3CrashFileOpen( if( rc==SQLITE_OK ){ pWrapper->pRealFile = pFile; }else{ - sqliteFree(pReal); + sqlite3_free(pReal); } } return rc; @@ -491,7 +491,7 @@ int sqlite3CrashFileWrap( sqlite3_file **ppWrapper ){ CrashFile *pWrapper; - pWrapper = (CrashFile *)sqliteMalloc(sizeof(CrashFile)+strlen(zName)+1); + pWrapper = (CrashFile *)sqlite3_malloc(sizeof(CrashFile)+strlen(zName)+1); if( !pWrapper ){ return SQLITE_NOMEM; } diff --git a/src/test7.c b/src/test7.c index 268229848..17c2a4984 100644 --- a/src/test7.c +++ b/src/test7.c @@ -12,7 +12,7 @@ ** Code for testing the client/server version of the SQLite library. ** Derived from test4.c. ** -** $Id: test7.c,v 1.4 2006/03/22 22:10:08 drh Exp $ +** $Id: test7.c,v 1.5 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -165,7 +165,7 @@ static int tcl_client_create( } threadset[i].busy = 1; sqliteFree(threadset[i].zFilename); - threadset[i].zFilename = sqliteStrDup(argv[2]); + threadset[i].zFilename = sqlite3StrDup(argv[2]); threadset[i].opnum = 1; threadset[i].completed = 0; rc = pthread_create(&x, 0, client_main, &threadset[i]); @@ -508,7 +508,7 @@ static int tcl_client_compile( client_wait(&threadset[i]); threadset[i].xOp = do_compile; sqliteFree(threadset[i].zArg); - threadset[i].zArg = sqliteStrDup(argv[2]); + threadset[i].zArg = sqlite3StrDup(argv[2]); threadset[i].opnum++; return TCL_OK; } diff --git a/src/test8.c b/src/test8.c index 06cf55863..3b6631bb8 100644 --- a/src/test8.c +++ b/src/test8.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test8.c,v 1.48 2007/07/20 00:35:59 drh Exp $ +** $Id: test8.c,v 1.49 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -122,8 +122,8 @@ static void dequoteString(char *z){ ** code otherwise. ** ** If successful, the number of columns is written to *pnCol. *paCol is -** set to point at sqliteMalloc()'d space containing the array of -** nCol column names. The caller is responsible for calling sqliteFree +** set to point at sqlite3_malloc()'d space containing the array of +** nCol column names. The caller is responsible for calling sqlite3_free ** on *paCol. */ static int getColumnNames( @@ -142,13 +142,13 @@ static int getColumnNames( ** of the result set of the compiled SELECT will be the same as ** the column names of table <tbl>. */ - zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab); + zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab); if( !zSql ){ rc = SQLITE_NOMEM; goto out; } rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); - sqliteFree(zSql); + sqlite3_free(zSql); if( rc==SQLITE_OK ){ int ii; @@ -163,7 +163,7 @@ static int getColumnNames( for(ii=0; ii<nCol; ii++){ nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1); } - aCol = (char **)sqliteMalloc(nBytes); + aCol = (char **)sqlite3_malloc(nBytes); if( !aCol ){ rc = SQLITE_NOMEM; goto out; @@ -213,7 +213,7 @@ static int getIndexArray( char *zSql; /* Allocate space for the index array */ - aIndex = (int *)sqliteMalloc(sizeof(int) * nCol); + aIndex = (int *)sqlite3_malloc(sizeof(int) * nCol); if( !aIndex ){ rc = SQLITE_NOMEM; goto get_index_array_out; @@ -226,7 +226,7 @@ static int getIndexArray( goto get_index_array_out; } rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); - sqliteFree(zSql); + sqlite3_free(zSql); /* For each index, figure out the left-most column and set the ** corresponding entry in aIndex[] to 1. @@ -240,7 +240,7 @@ static int getIndexArray( goto get_index_array_out; } rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0); - sqliteFree(zSql); + sqlite3_free(zSql); if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){ int cid = sqlite3_column_int(pStmt2, 1); assert( cid>=0 && cid<nCol ); @@ -263,7 +263,7 @@ get_index_array_out: } } if( rc!=SQLITE_OK ){ - sqliteFree(aIndex); + sqlite3_free(aIndex); aIndex = 0; } *paIndex = aIndex; @@ -339,12 +339,12 @@ static int echoDeclareVtab( */ static int echoDestructor(sqlite3_vtab *pVtab){ echo_vtab *p = (echo_vtab*)pVtab; - sqliteFree(p->aIndex); - sqliteFree(p->aCol); - sqliteFree(p->zThis); - sqliteFree(p->zTableName); - sqliteFree(p->zLogName); - sqliteFree(p); + sqlite3_free(p->aIndex); + sqlite3_free(p->aCol); + sqlite3_free(p->zThis); + sqlite3_free(p->zTableName); + sqlite3_free(p->zLogName); + sqlite3_free(p); return 0; } @@ -364,7 +364,7 @@ static int echoConstructor( echo_vtab *pVtab; /* Allocate the sqlite3_vtab/echo_vtab structure itself */ - pVtab = sqliteMalloc( sizeof(*pVtab) ); + pVtab = sqlite3_malloc( sizeof(*pVtab) ); if( !pVtab ){ return SQLITE_NOMEM; } @@ -384,7 +384,7 @@ static int echoConstructor( dequoteString(pVtab->zTableName); if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){ char *z = sqlite3MPrintf("%s%s", argv[2], &(pVtab->zTableName[1])); - sqliteFree(pVtab->zTableName); + sqlite3_free(pVtab->zTableName); pVtab->zTableName = z; pVtab->isPattern = 1; } @@ -443,7 +443,7 @@ static int echoCreate( pVtab->zLogName = sqlite3MPrintf("%s", argv[4]); zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName); rc = sqlite3_exec(db, zSql, 0, 0, 0); - sqliteFree(zSql); + sqlite3_free(zSql); } return rc; @@ -484,7 +484,7 @@ static int echoDestroy(sqlite3_vtab *pVtab){ char *zSql; zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName); rc = sqlite3_exec(p->db, zSql, 0, 0, 0); - sqliteFree(zSql); + sqlite3_free(zSql); } if( rc==SQLITE_OK ){ @@ -498,7 +498,7 @@ static int echoDestroy(sqlite3_vtab *pVtab){ */ static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ echo_cursor *pCur; - pCur = sqliteMalloc(sizeof(echo_cursor)); + pCur = sqlite3_malloc(sizeof(echo_cursor)); *ppCursor = (sqlite3_vtab_cursor *)pCur; return (pCur ? SQLITE_OK : SQLITE_NOMEM); } @@ -511,7 +511,7 @@ static int echoClose(sqlite3_vtab_cursor *cur){ echo_cursor *pCur = (echo_cursor *)cur; sqlite3_stmt *pStmt = pCur->pStmt; pCur->pStmt = 0; - sqliteFree(pCur); + sqlite3_free(pCur); rc = sqlite3_finalize(pStmt); return rc; } @@ -1041,7 +1041,7 @@ static int echoRename(sqlite3_vtab *vtab, const char *zNewName){ p->zTableName, zNewName, &p->zTableName[nThis] ); rc = sqlite3_exec(p->db, zSql, 0, 0, 0); - sqliteFree(zSql); + sqlite3_free(zSql); } return rc; diff --git a/src/test_async.c b/src/test_async.c index af9a476cb..9f38f2c7c 100644 --- a/src/test_async.c +++ b/src/test_async.c @@ -316,8 +316,8 @@ static const char *azOpcodeName[] = { ** ** ** For an ASYNC_WRITE operation, zBuf points to the data to write to the file. -** This space is sqliteMalloc()d along with the AsyncWrite structure in a -** single blob, so is deleted when sqliteFree() is called on the parent +** This space is sqlite3_malloc()d along with the AsyncWrite structure in a +** single blob, so is deleted when sqlite3_free() is called on the parent ** structure. */ struct AsyncWrite { @@ -343,8 +343,8 @@ struct AsyncFile { /* ** Add an entry to the end of the global write-op list. pWrite should point -** to an AsyncWrite structure allocated using sqlite3OsMalloc(). The writer -** thread will call sqlite3OsFree() to free the structure after the specified +** to an AsyncWrite structure allocated using sqlite3_malloc(). The writer +** thread will call sqlite3_free() to free the structure after the specified ** operation has been completed. ** ** Once an AsyncWrite structure has been added to the list, it becomes the @@ -410,7 +410,7 @@ static int addNewAsyncWrite( if( op!=ASYNC_CLOSE && async.ioError ){ return async.ioError; } - p = sqlite3OsMalloc(sizeof(AsyncWrite) + (zByte?nByte:0)); + p = sqlite3_malloc(sizeof(AsyncWrite) + (zByte?nByte:0)); if( !p ){ return SQLITE_NOMEM; } @@ -718,7 +718,7 @@ static int asyncOpenFile( n = strlen(zName); for(i=n-1; i>=0 && zName[i]!='/'; i--){} - p = (AsyncFile *)sqlite3OsMalloc(sizeof(AsyncFile) + n - i); + p = (AsyncFile *)sqlite3_malloc(sizeof(AsyncFile) + n - i); if( !p ){ rc = SQLITE_NOMEM; goto error_out; @@ -755,7 +755,7 @@ static int asyncOpenExclusive(const char *z, OsFile **ppFile, int delFlag){ i64 i = (i64)(delFlag); rc = addNewAsyncWrite(pFile, ASYNC_OPENEXCLUSIVE, i, nByte, z); if( rc!=SQLITE_OK ){ - sqlite3OsFree(pFile); + sqlite3_free(pFile); *ppFile = 0; } } @@ -991,7 +991,7 @@ static void *asyncWriterThread(void *NotUsed){ ASYNC_TRACE(("CLOSE %s\n", p->pFile->zName)); sqlite3OsClose(&p->pFile->pBaseWrite); sqlite3OsClose(&p->pFile->pBaseRead); - sqlite3OsFree(p->pFile); + sqlite3_free(p->pFile); break; case ASYNC_OPENDIRECTORY: @@ -1048,7 +1048,7 @@ static void *asyncWriterThread(void *NotUsed){ async.pQueueLast = 0; } async.pQueueFirst = p->pNext; - sqlite3OsFree(p); + sqlite3_free(p); assert( holdingMutex ); /* An IO error has occured. We cannot report the error back to the diff --git a/src/test_hexio.c b/src/test_hexio.c index d6931cc27..15d9274d4 100644 --- a/src/test_hexio.c +++ b/src/test_hexio.c @@ -17,7 +17,7 @@ ** with historical versions of the "binary" command. So it seems ** easier and safer to build our own mechanism. ** -** $Id: test_hexio.c,v 1.3 2007/05/10 17:23:12 drh Exp $ +** $Id: test_hexio.c,v 1.4 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -115,7 +115,7 @@ static int hexio_read( if( Tcl_GetIntFromObj(interp, objv[2], &offset) ) return TCL_ERROR; if( Tcl_GetIntFromObj(interp, objv[3], &amt) ) return TCL_ERROR; zFile = Tcl_GetString(objv[1]); - zBuf = malloc( amt*2+1 ); + zBuf = sqlite3_malloc( amt*2+1 ); if( zBuf==0 ){ return TCL_ERROR; } @@ -132,7 +132,7 @@ static int hexio_read( } binToHex(zBuf, got); Tcl_AppendResult(interp, zBuf, 0); - free(zBuf); + sqlite3_free(zBuf); return TCL_OK; } @@ -163,7 +163,7 @@ static int hexio_write( if( Tcl_GetIntFromObj(interp, objv[2], &offset) ) return TCL_ERROR; zFile = Tcl_GetString(objv[1]); zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[3], &nIn); - aOut = malloc( nIn/2 ); + aOut = sqlite3_malloc( nIn/2 ); if( aOut==0 ){ return TCL_ERROR; } @@ -175,7 +175,7 @@ static int hexio_write( } fseek(out, offset, SEEK_SET); written = fwrite(aOut, 1, nOut, out); - free(aOut); + sqlite3_free(aOut); fclose(out); Tcl_SetObjResult(interp, Tcl_NewIntObj(written)); return TCL_OK; @@ -205,7 +205,7 @@ static int hexio_get_int( return TCL_ERROR; } zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[1], &nIn); - aOut = malloc( nIn/2 ); + aOut = sqlite3_malloc( nIn/2 ); if( aOut==0 ){ return TCL_ERROR; } @@ -216,7 +216,7 @@ static int hexio_get_int( memset(aNum, 0, sizeof(aNum)); memcpy(&aNum[4-nOut], aOut, nOut); } - free(aOut); + sqlite3_free(aOut); val = (aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; Tcl_SetObjResult(interp, Tcl_NewIntObj(val)); return TCL_OK; diff --git a/src/test_schema.c b/src/test_schema.c index a5aaf5b3b..880052015 100644 --- a/src/test_schema.c +++ b/src/test_schema.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test_schema.c,v 1.12 2007/06/27 16:26:07 danielk1977 Exp $ +** $Id: test_schema.c,v 1.13 2007/08/16 04:30:40 drh Exp $ */ /* The code in this file defines a sqlite3 virtual-table module that @@ -39,13 +39,9 @@ #ifdef SQLITE_TEST #include "sqliteInt.h" #include "tcl.h" - #define MALLOC(x) sqliteMallocRaw(x) - #define FREE(x) sqliteFree(x) #else #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 - #define MALLOC(x) malloc(x) - #define FREE(x) free(x) #endif #include <stdlib.h> @@ -74,7 +70,7 @@ struct schema_cursor { ** Table destructor for the schema module. */ static int schemaDestroy(sqlite3_vtab *pVtab){ - FREE(pVtab); + sqlite3_free(pVtab); return 0; } @@ -89,7 +85,7 @@ static int schemaCreate( char **pzErr ){ int rc = SQLITE_NOMEM; - schema_vtab *pVtab = MALLOC(sizeof(schema_vtab)); + schema_vtab *pVtab = sqlite3_malloc(sizeof(schema_vtab)); if( pVtab ){ memset(pVtab, 0, sizeof(schema_vtab)); pVtab->db = db; @@ -107,7 +103,7 @@ static int schemaCreate( static int schemaOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ int rc = SQLITE_NOMEM; schema_cursor *pCur; - pCur = MALLOC(sizeof(schema_cursor)); + pCur = sqlite3_malloc(sizeof(schema_cursor)); if( pCur ){ memset(pCur, 0, sizeof(schema_cursor)); *ppCursor = (sqlite3_vtab_cursor *)pCur; @@ -124,7 +120,7 @@ static int schemaClose(sqlite3_vtab_cursor *cur){ sqlite3_finalize(pCur->pDbList); sqlite3_finalize(pCur->pTableList); sqlite3_finalize(pCur->pColumnList); - FREE(pCur); + sqlite3_free(pCur); return SQLITE_OK; } diff --git a/src/test_tclvar.c b/src/test_tclvar.c index 5563260a8..86e762219 100644 --- a/src/test_tclvar.c +++ b/src/test_tclvar.c @@ -16,7 +16,7 @@ ** The emphasis of this file is a virtual table that provides ** access to TCL variables. ** -** $Id: test_tclvar.c,v 1.11 2007/06/27 16:26:07 danielk1977 Exp $ +** $Id: test_tclvar.c,v 1.12 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -58,7 +58,7 @@ static int tclvarConnect( tclvar_vtab *pVtab; static const char zSchema[] = "CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)"; - pVtab = sqliteMalloc( sizeof(*pVtab) ); + pVtab = sqlite3_malloc( sizeof(*pVtab) ); if( pVtab==0 ) return SQLITE_NOMEM; *ppVtab = &pVtab->base; pVtab->interp = (Tcl_Interp *)pAux; @@ -69,7 +69,7 @@ static int tclvarConnect( ** methods are identical. */ static int tclvarDisconnect(sqlite3_vtab *pVtab){ - sqliteFree(pVtab); + sqlite3_free(pVtab); return SQLITE_OK; } /* The xDisconnect and xDestroy methods are also the same */ @@ -79,7 +79,7 @@ static int tclvarDisconnect(sqlite3_vtab *pVtab){ */ static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ tclvar_cursor *pCur; - pCur = sqliteMalloc(sizeof(tclvar_cursor)); + pCur = sqlite3_malloc(sizeof(tclvar_cursor)); *ppCursor = &pCur->base; return SQLITE_OK; } @@ -95,7 +95,7 @@ static int tclvarClose(sqlite3_vtab_cursor *cur){ if( pCur->pList2 ){ Tcl_DecrRefCount(pCur->pList2); } - sqliteFree(pCur); + sqlite3_free(pCur); return SQLITE_OK; } diff --git a/src/tokenize.c b/src/tokenize.c index 7a933ffd3..72f8c075b 100644 --- a/src/tokenize.c +++ b/src/tokenize.c @@ -15,7 +15,7 @@ ** individual tokens and sends those tokens one-by-one over to the ** parser for analysis. ** -** $Id: tokenize.c,v 1.131 2007/07/23 19:31:17 drh Exp $ +** $Id: tokenize.c,v 1.132 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -384,7 +384,7 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){ ** Run the parser on the given SQL string. The parser structure is ** passed in. An SQLITE_ status code is returned. If an error occurs ** and pzErrMsg!=NULL then an error message might be written into -** memory obtained from malloc() and *pzErrMsg made to point to that +** memory obtained from sqlite3_malloc() and *pzErrMsg made to point to that ** error message. Or maybe not. */ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ @@ -400,7 +400,7 @@ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ } pParse->rc = SQLITE_OK; i = 0; - pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3MallocX); + pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3_malloc); if( pEngine==0 ){ return SQLITE_NOMEM; } @@ -412,7 +412,7 @@ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ assert( pParse->nVarExprAlloc==0 ); assert( pParse->apVarExpr==0 ); pParse->zTail = pParse->zSql = zSql; - while( !sqlite3MallocFailed() && zSql[i]!=0 ){ + while( !db->mallocFailed && zSql[i]!=0 ){ assert( i>=0 ); pParse->sLastToken.z = (u8*)&zSql[i]; assert( pParse->sLastToken.dyn==0 ); @@ -434,7 +434,7 @@ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ } case TK_ILLEGAL: { if( pzErrMsg ){ - sqliteFree(*pzErrMsg); + sqlite3_free(*pzErrMsg); *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"", &pParse->sLastToken); } @@ -463,8 +463,8 @@ abort_parse: } sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); } - sqlite3ParserFree(pEngine, sqlite3FreeX); - if( sqlite3MallocFailed() ){ + sqlite3ParserFree(pEngine, sqlite3_free); + if( db->mallocFailed ){ pParse->rc = SQLITE_NOMEM; } if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ @@ -474,7 +474,7 @@ abort_parse: if( pzErrMsg && *pzErrMsg==0 ){ *pzErrMsg = pParse->zErrMsg; }else{ - sqliteFree(pParse->zErrMsg); + sqlite3_free(pParse->zErrMsg); } pParse->zErrMsg = 0; if( !nErr ) nErr++; @@ -485,7 +485,7 @@ abort_parse: } #ifndef SQLITE_OMIT_SHARED_CACHE if( pParse->nested==0 ){ - sqliteFree(pParse->aTableLock); + sqlite3_free(pParse->aTableLock); pParse->aTableLock = 0; pParse->nTableLock = 0; } @@ -500,7 +500,7 @@ abort_parse: } sqlite3DeleteTrigger(pParse->pNewTrigger); - sqliteFree(pParse->apVarExpr); + sqlite3_free(pParse->apVarExpr); if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ pParse->rc = SQLITE_ERROR; } diff --git a/src/trigger.c b/src/trigger.c index 5d5ec8da6..6a581b2d8 100644 --- a/src/trigger.c +++ b/src/trigger.c @@ -21,13 +21,13 @@ void sqlite3DeleteTriggerStep(TriggerStep *pTriggerStep){ TriggerStep * pTmp = pTriggerStep; pTriggerStep = pTriggerStep->pNext; - if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z); + if( pTmp->target.dyn ) sqlite3_free((char*)pTmp->target.z); sqlite3ExprDelete(pTmp->pWhere); sqlite3ExprListDelete(pTmp->pExprList); sqlite3SelectDelete(pTmp->pSelect); sqlite3IdListDelete(pTmp->pIdList); - sqliteFree(pTmp); + sqlite3_free(pTmp); } } @@ -83,7 +83,7 @@ void sqlite3BeginTrigger( ** If sqlite3SrcListLookup() returns 0, indicating the table does not ** exist, the error is caught by the block below. */ - if( !pTableName || sqlite3MallocFailed() ){ + if( !pTableName || db->mallocFailed ){ goto trigger_cleanup; } pTab = sqlite3SrcListLookup(pParse, pTableName); @@ -92,7 +92,7 @@ void sqlite3BeginTrigger( } /* Ensure the table name matches database name and that the table exists */ - if( sqlite3MallocFailed() ) goto trigger_cleanup; + if( db->mallocFailed ) goto trigger_cleanup; assert( pTableName->nSrc==1 ); if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && sqlite3FixSrcList(&sFix, pTableName) ){ @@ -110,7 +110,7 @@ void sqlite3BeginTrigger( /* Check that the trigger name is not reserved and that no trigger of the ** specified name exists */ - zName = sqlite3NameFromToken(pName); + zName = sqlite3NameFromToken(db, pName); if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto trigger_cleanup; } @@ -168,23 +168,23 @@ void sqlite3BeginTrigger( } /* Build the Trigger object */ - pTrigger = (Trigger*)sqliteMalloc(sizeof(Trigger)); + pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger)); if( pTrigger==0 ) goto trigger_cleanup; pTrigger->name = zName; zName = 0; - pTrigger->table = sqliteStrDup(pTableName->a[0].zName); + pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName); pTrigger->pSchema = db->aDb[iDb].pSchema; pTrigger->pTabSchema = pTab->pSchema; pTrigger->op = op; pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER; - pTrigger->pWhen = sqlite3ExprDup(pWhen); - pTrigger->pColumns = sqlite3IdListDup(pColumns); - sqlite3TokenCopy(&pTrigger->nameToken,pName); + pTrigger->pWhen = sqlite3ExprDup(db, pWhen); + pTrigger->pColumns = sqlite3IdListDup(db, pColumns); + sqlite3TokenCopy(db, &pTrigger->nameToken,pName); assert( pParse->pNewTrigger==0 ); pParse->pNewTrigger = pTrigger; trigger_cleanup: - sqliteFree(zName); + sqlite3_free(zName); sqlite3SrcListDelete(pTableName); sqlite3IdListDelete(pColumns); sqlite3ExprDelete(pWhen); @@ -264,7 +264,7 @@ void sqlite3FinishTrigger( pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash, pTrig->name, strlen(pTrig->name), pTrig); if( pDel ){ - assert( sqlite3MallocFailed() && pDel==pTrig ); + assert( db->mallocFailed && pDel==pTrig ); goto triggerfinish_cleanup; } n = strlen(pTrig->table) + 1; @@ -284,35 +284,35 @@ triggerfinish_cleanup: /* ** Make a copy of all components of the given trigger step. This has ** the effect of copying all Expr.token.z values into memory obtained -** from sqliteMalloc(). As initially created, the Expr.token.z values +** from sqlite3_malloc(). As initially created, the Expr.token.z values ** all point to the input string that was fed to the parser. But that ** string is ephemeral - it will go away as soon as the sqlite3_exec() ** call that started the parser exits. This routine makes a persistent ** copy of all the Expr.token.z strings so that the TriggerStep structure ** will be valid even after the sqlite3_exec() call returns. */ -static void sqlitePersistTriggerStep(TriggerStep *p){ +static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){ if( p->target.z ){ - p->target.z = (u8*)sqliteStrNDup((char*)p->target.z, p->target.n); + p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n); p->target.dyn = 1; } if( p->pSelect ){ - Select *pNew = sqlite3SelectDup(p->pSelect); + Select *pNew = sqlite3SelectDup(db, p->pSelect); sqlite3SelectDelete(p->pSelect); p->pSelect = pNew; } if( p->pWhere ){ - Expr *pNew = sqlite3ExprDup(p->pWhere); + Expr *pNew = sqlite3ExprDup(db, p->pWhere); sqlite3ExprDelete(p->pWhere); p->pWhere = pNew; } if( p->pExprList ){ - ExprList *pNew = sqlite3ExprListDup(p->pExprList); + ExprList *pNew = sqlite3ExprListDup(db, p->pExprList); sqlite3ExprListDelete(p->pExprList); p->pExprList = pNew; } if( p->pIdList ){ - IdList *pNew = sqlite3IdListDup(p->pIdList); + IdList *pNew = sqlite3IdListDup(db, p->pIdList); sqlite3IdListDelete(p->pIdList); p->pIdList = pNew; } @@ -325,8 +325,8 @@ static void sqlitePersistTriggerStep(TriggerStep *p){ ** The parser calls this routine when it finds a SELECT statement in ** body of a TRIGGER. */ -TriggerStep *sqlite3TriggerSelectStep(Select *pSelect){ - TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); +TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){ + TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); if( pTriggerStep==0 ) { sqlite3SelectDelete(pSelect); return 0; @@ -335,7 +335,7 @@ TriggerStep *sqlite3TriggerSelectStep(Select *pSelect){ pTriggerStep->op = TK_SELECT; pTriggerStep->pSelect = pSelect; pTriggerStep->orconf = OE_Default; - sqlitePersistTriggerStep(pTriggerStep); + sqlitePersistTriggerStep(db, pTriggerStep); return pTriggerStep; } @@ -348,13 +348,14 @@ TriggerStep *sqlite3TriggerSelectStep(Select *pSelect){ ** body of a trigger. */ TriggerStep *sqlite3TriggerInsertStep( + sqlite3 *db, /* The database connection */ Token *pTableName, /* Name of the table into which we insert */ IdList *pColumn, /* List of columns in pTableName to insert into */ ExprList *pEList, /* The VALUE clause: a list of values to be inserted */ Select *pSelect, /* A SELECT statement that supplies values */ int orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ ){ - TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); + TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); assert(pEList == 0 || pSelect == 0); assert(pEList != 0 || pSelect != 0); @@ -366,11 +367,11 @@ TriggerStep *sqlite3TriggerInsertStep( pTriggerStep->pIdList = pColumn; pTriggerStep->pExprList = pEList; pTriggerStep->orconf = orconf; - sqlitePersistTriggerStep(pTriggerStep); + sqlitePersistTriggerStep(db, pTriggerStep); }else{ sqlite3IdListDelete(pColumn); sqlite3ExprListDelete(pEList); - sqlite3SelectDup(pSelect); + sqlite3SelectDelete(pSelect); } return pTriggerStep; @@ -382,12 +383,13 @@ TriggerStep *sqlite3TriggerInsertStep( ** sees an UPDATE statement inside the body of a CREATE TRIGGER. */ TriggerStep *sqlite3TriggerUpdateStep( + sqlite3 *db, /* The database connection */ Token *pTableName, /* Name of the table to be updated */ ExprList *pEList, /* The SET clause: list of column and new values */ Expr *pWhere, /* The WHERE clause */ int orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ ){ - TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); + TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); if( pTriggerStep==0 ){ sqlite3ExprListDelete(pEList); sqlite3ExprDelete(pWhere); @@ -399,7 +401,7 @@ TriggerStep *sqlite3TriggerUpdateStep( pTriggerStep->pExprList = pEList; pTriggerStep->pWhere = pWhere; pTriggerStep->orconf = orconf; - sqlitePersistTriggerStep(pTriggerStep); + sqlitePersistTriggerStep(db, pTriggerStep); return pTriggerStep; } @@ -409,8 +411,12 @@ TriggerStep *sqlite3TriggerUpdateStep( ** a pointer to that trigger step. The parser calls this routine when it ** sees a DELETE statement inside the body of a CREATE TRIGGER. */ -TriggerStep *sqlite3TriggerDeleteStep(Token *pTableName, Expr *pWhere){ - TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep)); +TriggerStep *sqlite3TriggerDeleteStep( + sqlite3 *db, /* Database connection */ + Token *pTableName, /* The table from which rows are deleted */ + Expr *pWhere /* The WHERE clause */ +){ + TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); if( pTriggerStep==0 ){ sqlite3ExprDelete(pWhere); return 0; @@ -420,7 +426,7 @@ TriggerStep *sqlite3TriggerDeleteStep(Token *pTableName, Expr *pWhere){ pTriggerStep->target = *pTableName; pTriggerStep->pWhere = pWhere; pTriggerStep->orconf = OE_Default; - sqlitePersistTriggerStep(pTriggerStep); + sqlitePersistTriggerStep(db, pTriggerStep); return pTriggerStep; } @@ -431,12 +437,12 @@ TriggerStep *sqlite3TriggerDeleteStep(Token *pTableName, Expr *pWhere){ void sqlite3DeleteTrigger(Trigger *pTrigger){ if( pTrigger==0 ) return; sqlite3DeleteTriggerStep(pTrigger->step_list); - sqliteFree(pTrigger->name); - sqliteFree(pTrigger->table); + sqlite3_free(pTrigger->name); + sqlite3_free(pTrigger->table); sqlite3ExprDelete(pTrigger->pWhen); sqlite3IdListDelete(pTrigger->pColumns); - if( pTrigger->nameToken.dyn ) sqliteFree((char*)pTrigger->nameToken.z); - sqliteFree(pTrigger); + if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z); + sqlite3_free(pTrigger); } /* @@ -455,7 +461,7 @@ void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ int nName; sqlite3 *db = pParse->db; - if( sqlite3MallocFailed() ) goto drop_trigger_cleanup; + if( db->mallocFailed ) goto drop_trigger_cleanup; if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ goto drop_trigger_cleanup; } @@ -644,9 +650,9 @@ static SrcList *targetSrcList( assert( iDb<pParse->db->nDb ); sDb.z = (u8*)pParse->db->aDb[iDb].zName; sDb.n = strlen((char*)sDb.z); - pSrc = sqlite3SrcListAppend(0, &sDb, &pStep->target); + pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target); } else { - pSrc = sqlite3SrcListAppend(0, &pStep->target, 0); + pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0); } return pSrc; } @@ -663,6 +669,7 @@ static int codeTriggerProgram( TriggerStep * pTriggerStep = pStepList; int orconf; Vdbe *v = pParse->pVdbe; + sqlite3 *db = pParse->db; assert( pTriggerStep!=0 ); assert( v!=0 ); @@ -673,7 +680,7 @@ static int codeTriggerProgram( pParse->trigStack->orconf = orconf; switch( pTriggerStep->op ){ case TK_SELECT: { - Select *ss = sqlite3SelectDup(pTriggerStep->pSelect); + Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect); if( ss ){ sqlite3SelectResolve(pParse, ss, 0); sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0); @@ -686,8 +693,8 @@ static int codeTriggerProgram( pSrc = targetSrcList(pParse, pTriggerStep); sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); sqlite3Update(pParse, pSrc, - sqlite3ExprListDup(pTriggerStep->pExprList), - sqlite3ExprDup(pTriggerStep->pWhere), orconf); + sqlite3ExprListDup(db, pTriggerStep->pExprList), + sqlite3ExprDup(db, pTriggerStep->pWhere), orconf); sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); break; } @@ -696,9 +703,9 @@ static int codeTriggerProgram( pSrc = targetSrcList(pParse, pTriggerStep); sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); sqlite3Insert(pParse, pSrc, - sqlite3ExprListDup(pTriggerStep->pExprList), - sqlite3SelectDup(pTriggerStep->pSelect), - sqlite3IdListDup(pTriggerStep->pIdList), orconf); + sqlite3ExprListDup(db, pTriggerStep->pExprList), + sqlite3SelectDup(db, pTriggerStep->pSelect), + sqlite3IdListDup(db, pTriggerStep->pIdList), orconf); sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); break; } @@ -706,7 +713,8 @@ static int codeTriggerProgram( SrcList *pSrc; sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); pSrc = targetSrcList(pParse, pTriggerStep); - sqlite3DeleteFrom(pParse, pSrc, sqlite3ExprDup(pTriggerStep->pWhere)); + sqlite3DeleteFrom(pParse, pSrc, + sqlite3ExprDup(db, pTriggerStep->pWhere)); sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); break; } @@ -805,7 +813,7 @@ int sqlite3CodeRowTrigger( /* code the WHEN clause */ endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe); - whenExpr = sqlite3ExprDup(p->pWhen); + whenExpr = sqlite3ExprDup(pParse->db, p->pWhen); if( sqlite3ExprResolveNames(&sNC, whenExpr) ){ pParse->trigStack = trigStackEntry.pNext; sqlite3ExprDelete(whenExpr); diff --git a/src/update.c b/src/update.c index 0a4eddc9f..3348470fe 100644 --- a/src/update.c +++ b/src/update.c @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle UPDATE statements. ** -** $Id: update.c,v 1.138 2007/06/25 16:29:34 danielk1977 Exp $ +** $Id: update.c,v 1.139 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" @@ -115,10 +115,10 @@ void sqlite3Update( int oldIdx = -1; /* index of trigger "old" temp table */ sContext.pParse = 0; - if( pParse->nErr || sqlite3MallocFailed() ){ + db = pParse->db; + if( pParse->nErr || db->mallocFailed ){ goto update_cleanup; } - db = pParse->db; assert( pTabList->nSrc==1 ); /* Locate the table which we want to update. @@ -148,7 +148,7 @@ void sqlite3Update( if( sqlite3ViewGetColumnNames(pParse, pTab) ){ goto update_cleanup; } - aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol ); + aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); if( aXRef==0 ) goto update_cleanup; for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; @@ -235,7 +235,7 @@ void sqlite3Update( if( i<pIdx->nColumn ) nIdx++; } if( nIdxTotal>0 ){ - apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal ); + apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal ); if( apIdx==0 ) goto update_cleanup; aIdxUsed = (char*)&apIdx[nIdx]; } @@ -291,7 +291,7 @@ void sqlite3Update( */ if( isView ){ Select *pView; - pView = sqlite3SelectDup(pTab->pSelect); + pView = sqlite3SelectDup(db, pTab->pSelect); sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0); sqlite3SelectDelete(pView); } @@ -525,8 +525,8 @@ void sqlite3Update( update_cleanup: sqlite3AuthContextPop(&sContext); - sqliteFree(apIdx); - sqliteFree(aXRef); + sqlite3_free(apIdx); + sqlite3_free(aXRef); sqlite3SrcListDelete(pTabList); sqlite3ExprListDelete(pChanges); sqlite3ExprDelete(pWhere); @@ -569,24 +569,27 @@ static void updateVirtualTable( int ephemTab; /* Table holding the result of the SELECT */ int i; /* Loop counter */ int addr; /* Address of top of loop */ + sqlite3 *db = pParse->db; /* Database connection */ /* Construct the SELECT statement that will find the new values for ** all updated rows. */ - pEList = sqlite3ExprListAppend(0, sqlite3CreateIdExpr("_rowid_"), 0); + pEList = sqlite3ExprListAppend(pParse, 0, + sqlite3CreateIdExpr(pParse, "_rowid_"), 0); if( pRowid ){ - pEList = sqlite3ExprListAppend(pEList, sqlite3ExprDup(pRowid), 0); + pEList = sqlite3ExprListAppend(pParse, pEList, + sqlite3ExprDup(db, pRowid), 0); } assert( pTab->iPKey<0 ); for(i=0; i<pTab->nCol; i++){ if( aXRef[i]>=0 ){ - pExpr = sqlite3ExprDup(pChanges->a[aXRef[i]].pExpr); + pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr); }else{ - pExpr = sqlite3CreateIdExpr(pTab->aCol[i].zName); + pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName); } - pEList = sqlite3ExprListAppend(pEList, pExpr, 0); + pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0); } - pSelect = sqlite3SelectNew(pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0); + pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0); /* Create the ephemeral table into which the update results will ** be stored. @@ -12,7 +12,7 @@ ** This file contains routines used to translate between UTF-8, ** UTF-16, UTF-16BE, and UTF-16LE. ** -** $Id: utf.c,v 1.53 2007/08/07 17:04:59 drh Exp $ +** $Id: utf.c,v 1.54 2007/08/16 04:30:40 drh Exp $ ** ** Notes on UTF-8: ** @@ -187,7 +187,7 @@ int sqlite3Utf8Read( ** desiredEnc. It is an error if the string is already of the desired ** encoding, or if *pMem does not contain a string value. */ -int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ +int sqlite3VdbeMemTranslate(sqlite3 *db, Mem *pMem, u8 desiredEnc){ unsigned char zShort[NBFS]; /* Temporary short output buffer */ int len; /* Maximum length of output string in bytes */ unsigned char *zOut; /* Output buffer */ @@ -254,13 +254,13 @@ int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ ** byte past the end. ** ** Variable zOut is set to point at the output buffer. This may be space - ** obtained from malloc(), or Mem.zShort, if it large enough and not in - ** use, or the zShort array on the stack (see above). + ** obtained from sqlite3_malloc(), or Mem.zShort, if it large enough and + ** not in use, or the zShort array on the stack (see above). */ zIn = (u8*)pMem->z; zTerm = &zIn[pMem->n]; if( len>NBFS ){ - zOut = sqliteMallocRaw(len); + zOut = sqlite3DbMallocRaw(db, len); if( !zOut ) return SQLITE_NOMEM; }else{ zOut = zShort; @@ -402,19 +402,19 @@ int sqlite3Utf8CharLen(const char *zIn, int nByte){ #ifndef SQLITE_OMIT_UTF16 /* ** Convert a UTF-16 string in the native encoding into a UTF-8 string. -** Memory to hold the UTF-8 string is obtained from malloc and must be -** freed by the calling function. +** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must +** be freed by the calling function. ** ** NULL is returned if there is an allocation error. */ -char *sqlite3Utf16to8(const void *z, int nByte){ +char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){ Mem m; memset(&m, 0, sizeof(m)); - sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC); - sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8); - assert( (m.flags & MEM_Term)!=0 || sqlite3MallocFailed() ); - assert( (m.flags & MEM_Str)!=0 || sqlite3MallocFailed() ); - return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z); + sqlite3VdbeMemSetStr(db, &m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC); + sqlite3VdbeChangeEncoding(db, &m, SQLITE_UTF8); + assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); + assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); + return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z); } /* diff --git a/src/util.c b/src/util.c index cf222b5d9..fef1c00c8 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.207 2007/06/26 00:37:28 drh Exp $ +** $Id: util.c,v 1.208 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -631,13 +631,13 @@ static int hexToInt(int h){ ** binary value has been obtained from malloc and must be freed by ** the calling routine. */ -void *sqlite3HexToBlob(const char *z){ +void *sqlite3HexToBlob(sqlite3 *db, const char *z){ char *zBlob; int i; int n = strlen(z); if( n%2 ) return 0; - zBlob = (char *)sqliteMalloc(n/2); + zBlob = (char *)sqlite3DbMallocRaw(db, n/2); if( zBlob ){ for(i=0; i<n; i+=2){ zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]); @@ -699,34 +699,3 @@ int sqlite3SafetyOff(sqlite3 *db){ return 1; } } - -/* -** Return a pointer to the ThreadData associated with the calling thread. -*/ -ThreadData *sqlite3ThreadData(){ - ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1); - if( !p ){ - sqlite3FailedMalloc(); - } - return p; -} - -/* -** Return a pointer to the ThreadData associated with the calling thread. -** If no ThreadData has been allocated to this thread yet, return a pointer -** to a substitute ThreadData structure that is all zeros. -*/ -const ThreadData *sqlite3ThreadDataReadOnly(){ - static const ThreadData zeroData = {0}; /* Initializer to silence warnings - ** from broken compilers */ - const ThreadData *pTd = sqlite3OsThreadSpecificData(0); - return pTd ? pTd : &zeroData; -} - -/* -** Check to see if the ThreadData for this thread is all zero. If it -** is, then deallocate it. -*/ -void sqlite3ReleaseThreadData(){ - sqlite3OsThreadSpecificData(-1); -} diff --git a/src/vacuum.c b/src/vacuum.c index e34a4e973..ee0567f17 100644 --- a/src/vacuum.c +++ b/src/vacuum.c @@ -14,7 +14,7 @@ ** Most of the code in this file may be omitted by defining the ** SQLITE_OMIT_VACUUM macro. ** -** $Id: vacuum.c,v 1.69 2007/03/27 16:19:52 danielk1977 Exp $ +** $Id: vacuum.c,v 1.70 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "vdbeInt.h" @@ -112,7 +112,7 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ pTemp = db->aDb[db->nDb-1].pBt; sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), sqlite3BtreeGetReserve(pMain)); - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ rc = SQLITE_NOMEM; goto end_of_vacuum; } diff --git a/src/vdbe.c b/src/vdbe.c index d6925119d..00abd1630 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -43,7 +43,7 @@ ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.639 2007/07/26 06:50:06 danielk1977 Exp $ +** $Id: vdbe.c,v 1.640 2007/08/16 04:30:40 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -111,15 +111,6 @@ int sqlite3_max_blobsize = 0; { goto no_mem; } /* -** Convert the given stack entity into a string that has been obtained -** from sqliteMalloc(). This is different from Stringify() above in that -** Stringify() will use the NBFS bytes of static string space if the string -** will fit but this routine always mallocs for space. -** Return non-zero if we run out of memory. -*/ -#define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P) - -/* ** The header of a record consists of a sequence variable-length integers. ** These integers are almost always small and are encoded as a single byte. ** The following macro takes advantage this fact to provide a fast decode @@ -206,7 +197,7 @@ static Cursor *allocateCursor(Vdbe *p, int iCur, int iDb){ if( p->apCsr[iCur] ){ sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); } - p->apCsr[iCur] = pCx = sqliteMalloc( sizeof(Cursor) ); + p->apCsr[iCur] = pCx = sqlite3MallocZero( sizeof(Cursor) ); if( pCx ){ pCx->iDb = iDb; } @@ -219,14 +210,14 @@ static Cursor *allocateCursor(Vdbe *p, int iCur, int iDb){ ** looks like a number, convert it into a number. If it does not ** look like a number, leave it alone. */ -static void applyNumericAffinity(Mem *pRec){ +static void applyNumericAffinity(sqlite3 *db, Mem *pRec){ if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){ int realnum; - sqlite3VdbeMemNulTerminate(pRec); + sqlite3VdbeMemNulTerminate(db, pRec); if( (pRec->flags&MEM_Str) && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){ i64 value; - sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8); + sqlite3VdbeChangeEncoding(db, pRec, SQLITE_UTF8); if( !realnum && sqlite3Atoi64(pRec->z, &value) ){ sqlite3VdbeMemRelease(pRec); pRec->u.i = value; @@ -256,20 +247,25 @@ static void applyNumericAffinity(Mem *pRec){ ** SQLITE_AFF_NONE: ** No-op. pRec is unchanged. */ -static void applyAffinity(Mem *pRec, char affinity, u8 enc){ +static void applyAffinity( + sqlite3 *db, /* Report malloc() errors to this db connection */ + Mem *pRec, /* The value to apply affinity to */ + char affinity, /* The affinity to be applied */ + u8 enc /* Use this text encoding */ +){ if( affinity==SQLITE_AFF_TEXT ){ /* Only attempt the conversion to TEXT if there is an integer or real ** representation (blob and NULL do not get converted) but no string ** representation. */ if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ - sqlite3VdbeMemStringify(pRec, enc); + sqlite3VdbeMemStringify(db, pRec, enc); } pRec->flags &= ~(MEM_Real|MEM_Int); }else if( affinity!=SQLITE_AFF_NONE ){ assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL || affinity==SQLITE_AFF_NUMERIC ); - applyNumericAffinity(pRec); + applyNumericAffinity(db, pRec); if( pRec->flags & MEM_Real ){ sqlite3VdbeIntegerAffinity(pRec); } @@ -429,7 +425,7 @@ __inline__ unsigned long long int hwtime(void){ ** return SQLITE_BUSY. ** ** If an error occurs, an error message is written to memory obtained -** from sqliteMalloc() and p->zErrMsg is made to point to that memory. +** from sqlite3_malloc() and p->zErrMsg is made to point to that memory. ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. ** ** If the callback ever returns non-zero, then the program exits @@ -501,7 +497,7 @@ int sqlite3VdbeExec( for(pc=p->pc; rc==SQLITE_OK; pc++){ assert( pc>=0 && pc<p->nOp ); assert( pTos<=&p->aStack[pc] ); - if( sqlite3MallocFailed() ) goto no_mem; + if( db->mallocFailed ) goto no_mem; #ifdef VDBE_PROFILE origPc = pc; start = hwtime(); @@ -753,11 +749,11 @@ case OP_String8: { /* same as TK_STRING */ pTos++; sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC); if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem; - if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem; + if( SQLITE_OK!=sqlite3VdbeMemDynamicify(db, pTos) ) goto no_mem; pTos->flags &= ~(MEM_Dyn); pTos->flags |= MEM_Static; if( pOp->p3type==P3_DYNAMIC ){ - sqliteFree(pOp->p3); + sqlite3_free(pOp->p3); } pOp->p3type = P3_DYNAMIC; pOp->p3 = pTos->z; @@ -814,13 +810,13 @@ case OP_HexBlob: { /* same as TK_BLOB */ char *zBlob = sqlite3HexToBlob(pOp->p3); if( !zBlob ) goto no_mem; if( pOp->p3type==P3_DYNAMIC ){ - sqliteFree(pOp->p3); + sqlite3_free(pOp->p3); } pOp->p3 = zBlob; pOp->p3type = P3_DYNAMIC; }else{ if( pOp->p3type==P3_DYNAMIC ){ - sqliteFree(pOp->p3); + sqlite3_free(pOp->p3); } pOp->p3type = P3_STATIC; pOp->p3 = ""; @@ -1015,7 +1011,7 @@ case OP_Callback: { /* no-push */ ** any element of the stack is NULL, then the result is NULL. ** ** When P1==1, this routine makes a copy of the top stack element -** into memory obtained from sqliteMalloc(). +** into memory obtained from sqlite3_malloc(). */ case OP_Concat: { /* same as TK_CONCAT */ char *zNew; @@ -1056,7 +1052,7 @@ case OP_Concat: { /* same as TK_CONCAT */ if( nByte+2>SQLITE_MAX_LENGTH ){ goto too_big; } - zNew = sqliteMallocRaw( nByte+2 ); + zNew = sqlite3DbMallocRaw(db, nByte+2 ); if( zNew==0 ) goto no_mem; j = 0; pTerm = &pTos[1-nField]; @@ -1289,7 +1285,7 @@ case OP_Function: { if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; (*ctx.pFunc->xFunc)(&ctx, n, apVal); if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ /* Even though a malloc() has failed, the implementation of the ** user function may have called an sqlite3_result_XXX() function ** to return a value. The following call releases any resources @@ -2075,7 +2071,7 @@ case OP_Column: { aType = pC->aType; if( aType==0 ){ - pC->aType = aType = sqliteMallocRaw( 2*nField*sizeof(aType) ); + pC->aType = aType = sqlite3DbMallocRaw(db, 2*nField*sizeof(aType) ); } if( aType==0 ){ goto no_mem; @@ -2346,7 +2342,7 @@ case OP_MakeRecord: { /* Allocate space for the new record. */ if( nByte>sizeof(zTemp) ){ - zNewRecord = sqliteMallocRaw(nByte); + zNewRecord = sqlite3DbMallocRaw(db, nByte); if( !zNewRecord ){ goto no_mem; } @@ -3468,14 +3464,14 @@ case OP_Insert: { /* no-push */ assert( pTos->flags & (MEM_Blob|MEM_Str) ); } if( pC->pseudoTable ){ - sqliteFree(pC->pData); + sqlite3_free(pC->pData); pC->iKey = iKey; pC->nData = pTos->n; if( pTos->flags & MEM_Dyn ){ pC->pData = pTos->z; pTos->flags = MEM_Null; }else{ - pC->pData = sqliteMallocRaw( pC->nData+2 ); + pC->pData = sqlite3_malloc( pC->nData+2 ); if( !pC->pData ) goto no_mem; memcpy(pC->pData, pTos->z, pC->nData); pC->pData[pC->nData] = 0; @@ -3644,7 +3640,7 @@ case OP_RowData: { pTos->flags = MEM_Blob | MEM_Short; pTos->z = pTos->zShort; }else{ - char *z = sqliteMallocRaw( n ); + char *z = sqlite3_malloc( n ); if( z==0 ) goto no_mem; pTos->flags = MEM_Blob | MEM_Dyn; pTos->xDel = 0; @@ -4213,14 +4209,13 @@ case OP_ParseSchema: { /* no-push */ sqlite3SafetyOff(db); assert( db->init.busy==0 ); db->init.busy = 1; - assert( !sqlite3MallocFailed() ); + assert( !db->mallocFailed ); rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); if( rc==SQLITE_ABORT ) rc = initData.rc; - sqliteFree(zSql); + sqlite3_free(zSql); db->init.busy = 0; sqlite3SafetyOn(db); if( rc==SQLITE_NOMEM ){ - sqlite3FailedMalloc(); goto no_mem; } break; @@ -4311,7 +4306,7 @@ case OP_IntegrityCk: { if( (pTos[-nRoot].flags & MEM_Int)==0 ) break; } assert( nRoot>0 ); - aRoot = sqliteMallocRaw( sizeof(int)*(nRoot+1) ); + aRoot = sqlite3_malloc( sizeof(int)*(nRoot+1) ); if( aRoot==0 ) goto no_mem; j = pOp->p1; assert( j>=0 && j<p->nMem ); @@ -4337,7 +4332,7 @@ case OP_IntegrityCk: { } pTos->enc = SQLITE_UTF8; sqlite3VdbeChangeEncoding(pTos, encoding); - sqliteFree(aRoot); + sqlite3_free(aRoot); break; } #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ @@ -4350,7 +4345,9 @@ case OP_IntegrityCk: { case OP_FifoWrite: { /* no-push */ assert( pTos>=p->aStack ); sqlite3VdbeMemIntegerify(pTos); - sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i); + if( sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i)==SQLITE_NOMEM ){ + goto nomem; + } assert( (pTos->flags & MEM_Dyn)==0 ); pTos--; break; @@ -4390,7 +4387,7 @@ case OP_ContextPush: { /* no-push */ /* FIX ME: This should be allocated as part of the vdbe at compile-time */ if( i>=p->contextStackDepth ){ p->contextStackDepth = i+1; - p->contextStack = sqliteReallocOrFree(p->contextStack, + p->contextStack = sqlite3ReallocOrFree(db, p->contextStack, sizeof(Context)*(i+1)); if( p->contextStack==0 ) goto no_mem; } @@ -4624,6 +4621,7 @@ case OP_AggStep: { /* no-push */ ctx.s.xDel = 0; ctx.isError = 0; ctx.pColl = 0; + ctx.db = db; if( ctx.pFunc->needCollSeq ){ assert( pOp>p->aOp ); assert( pOp[-1].p3type==P3_COLLSEQ ); @@ -4820,6 +4818,7 @@ case OP_VOpen: { /* no-push */ pCur->pVtabCursor = pVtabCursor; pCur->pModule = pVtabCursor->pVtab->pModule; }else{ + db->mallocFailed = 1; pModule->xClose(pVtabCursor); } } @@ -5201,6 +5200,7 @@ too_big: /* Jump to here if a malloc() fails. */ no_mem: + db->mallocFailed = 1; sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0); rc = SQLITE_NOMEM; goto vdbe_halt; @@ -5216,7 +5216,7 @@ abort_due_to_misuse: */ abort_due_to_error: if( p->zErrMsg==0 ){ - if( sqlite3MallocFailed() ) rc = SQLITE_NOMEM; + if( db->mallocFailed ) rc = SQLITE_NOMEM; sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0); } goto vdbe_halt; diff --git a/src/vdbeInt.h b/src/vdbeInt.h index 78550c434..9ed2e4fad 100644 --- a/src/vdbeInt.h +++ b/src/vdbeInt.h @@ -219,6 +219,7 @@ struct sqlite3_context { Mem *pMem; /* Memory cell used to store aggregate context */ u8 isError; /* Set to true for an error */ CollSeq *pColl; /* Collating sequence */ + sqlite3 *db; /* Database connection */ }; /* @@ -361,6 +362,7 @@ struct Vdbe { /* ** Function prototypes */ +sqlite3 *sqlite3DbOfVdbe(Vdbe*); void sqlite3VdbeFreeCursor(Vdbe *, Cursor*); void sqliteVdbePopStack(Vdbe*,int); int sqlite3VdbeCursorMoveto(Cursor*); diff --git a/src/vdbeapi.c b/src/vdbeapi.c index 2ef0d3c11..808d496c3 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -175,7 +175,8 @@ static int sqlite3Step(Vdbe *p){ int rc; /* Assert that malloc() has not failed */ - assert( !sqlite3MallocFailed() ); + db = p->db; + assert( !db->mallocFailed ); if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ return SQLITE_MISUSE; @@ -190,7 +191,6 @@ static int sqlite3Step(Vdbe *p){ rc = SQLITE_ERROR; goto end_of_step; } - db = p->db; if( sqlite3SafetyOn(db) ){ p->rc = SQLITE_MISUSE; return SQLITE_MISUSE; @@ -358,7 +358,7 @@ void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ pMem->z = pMem->zShort; memset(pMem->z, 0, nByte); }else{ - pMem->z = sqliteMalloc( nByte ); + pMem->z = sqlite3DbMallocZero(p->db, nByte); } } } @@ -395,8 +395,11 @@ void sqlite3_set_auxdata( pVdbeFunc = pCtx->pVdbeFunc; if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; - pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); - if( !pVdbeFunc ) goto failed; + pVdbeFunc = sqlite3_realloc(pVdbeFunc, nMalloc); + if( !pVdbeFunc ){ + pCtx->db->mallocFailed = 1; + goto failed; + } pCtx->pVdbeFunc = pVdbeFunc; memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); diff --git a/src/vdbeaux.c b/src/vdbeaux.c index f7d3066be..f5ccec0b6 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -29,13 +29,20 @@ int sqlite3_vdbe_addop_trace = 0; #endif +/* +** Return the database connection associated with a VDBE +*/ +sqlite3 *sqlite3DbOfVdbe(Vdbe *p){ + return p->db; +} + /* ** Create a new virtual database engine. */ Vdbe *sqlite3VdbeCreate(sqlite3 *db){ Vdbe *p; - p = sqliteMalloc( sizeof(Vdbe) ); + p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); if( p==0 ) return 0; p->db = db; if( db->pVdbe ){ @@ -54,7 +61,7 @@ Vdbe *sqlite3VdbeCreate(sqlite3 *db){ void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){ if( p==0 ) return; assert( p->zSql==0 ); - p->zSql = sqlite3StrNDup(z, n); + p->zSql = sqlite3DbStrNDup(p->db, z, n); } /* @@ -115,13 +122,15 @@ static void resizeOpArray(Vdbe *p, int N){ VdbeOp *pNew; int nNew = N + 100*(!runMode); int oldSize = p->nOpAlloc; - pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op)); + pNew = sqlite_realloc(p->aOp, nNew*sizeof(Op)); if( pNew ){ p->nOpAlloc = nNew; p->aOp = pNew; if( nNew>oldSize ){ memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op)); } + }else{ + p->db->mallocFailed = 1; } } } @@ -150,7 +159,7 @@ int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){ assert( p->magic==VDBE_MAGIC_INIT ); if( p->nOpAlloc<=i ){ resizeOpArray(p, i+1); - if( sqlite3MallocFailed() ){ + if( p->db->mallocFailed ){ return 0; } } @@ -197,7 +206,7 @@ int sqlite3VdbeMakeLabel(Vdbe *p){ assert( p->magic==VDBE_MAGIC_INIT ); if( i>=p->nLabelAlloc ){ p->nLabelAlloc = p->nLabelAlloc*2 + 10; - p->aLabel = sqliteReallocOrFree(p->aLabel, + p->aLabel = sqlite3ReallocOrFree(p->db, p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0])); } if( p->aLabel ){ @@ -325,7 +334,7 @@ static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){ assert( -1-pOp->p2<p->nLabel ); pOp->p2 = aLabel[-1-pOp->p2]; } - sqliteFree(p->aLabel); + sqlite3_free(p->aLabel); p->aLabel = 0; *pMaxFuncArgs = nMaxArgs; @@ -361,7 +370,7 @@ int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ int addr; assert( p->magic==VDBE_MAGIC_INIT ); resizeOpArray(p, p->nOp + nOp); - if( sqlite3MallocFailed() ){ + if( p->db->mallocFailed ){ return 0; } addr = p->nOp; @@ -427,7 +436,7 @@ void sqlite3VdbeJumpHere(Vdbe *p, int addr){ */ static void freeEphemeralFunction(FuncDef *pDef){ if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ - sqliteFree(pDef); + sqlite3_free(pDef); } } @@ -440,7 +449,7 @@ static void freeP3(int p3type, void *p3){ case P3_DYNAMIC: case P3_KEYINFO: case P3_KEYINFO_HANDOFF: { - sqliteFree(p3); + sqlite3_free(p3); break; } case P3_MPRINTF: { @@ -451,7 +460,7 @@ static void freeP3(int p3type, void *p3){ VdbeFunc *pVdbeFunc = (VdbeFunc *)p3; freeEphemeralFunction(pVdbeFunc->pFunc); sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); - sqliteFree(pVdbeFunc); + sqlite3_free(pVdbeFunc); break; } case P3_FUNCDEF: { @@ -489,15 +498,15 @@ void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ ** few minor changes to the program. ** ** If n>=0 then the P3 operand is dynamic, meaning that a copy of -** the string is made into memory obtained from sqliteMalloc(). +** the string is made into memory obtained from sqlite3_malloc(). ** A value of n==0 means copy bytes of zP3 up to and including the ** first null byte. If n>0 then copy n+1 bytes of zP3. ** ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure. ** A copy is made of the KeyInfo structure into memory obtained from -** sqliteMalloc, to be freed when the Vdbe is finalized. +** sqlite3_malloc, to be freed when the Vdbe is finalized. ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure -** stored in memory that the caller has obtained from sqliteMalloc. The +** stored in memory that the caller has obtained from sqlite3_malloc. The ** caller should not free the allocation, it will be freed when the Vdbe is ** finalized. ** @@ -510,7 +519,7 @@ void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ Op *pOp; assert( p==0 || p->magic==VDBE_MAGIC_INIT ); - if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){ + if( p==0 || p->aOp==0 || p->db->mallocFailed ){ if (n != P3_KEYINFO) { freeP3(n, (void*)*(char**)&zP3); } @@ -532,7 +541,7 @@ void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ nField = ((KeyInfo*)zP3)->nField; nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; - pKeyInfo = sqliteMallocRaw( nByte ); + pKeyInfo = sqlite3_malloc( nByte ); pOp->p3 = (char*)pKeyInfo; if( pKeyInfo ){ unsigned char *aSortOrder; @@ -544,6 +553,7 @@ void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ } pOp->p3type = P3_KEYINFO; }else{ + p->db->mallocFailed = 1; pOp->p3type = P3_NOTUSED; } }else if( n==P3_KEYINFO_HANDOFF ){ @@ -554,7 +564,7 @@ void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ pOp->p3type = n; }else{ if( n==0 ) n = strlen(zP3); - pOp->p3 = sqliteStrNDup(zP3, n); + pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n); pOp->p3type = P3_DYNAMIC; } } @@ -567,7 +577,7 @@ void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ va_list ap; assert( p->nOp>0 || p->aOp==0 ); - assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || sqlite3MallocFailed() ); + assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed ); va_start(ap, zFormat); sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC); va_end(ap); @@ -579,7 +589,7 @@ void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ */ VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ assert( p->magic==VDBE_MAGIC_INIT ); - assert( (addr>=0 && addr<p->nOp) || sqlite3MallocFailed() ); + assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0); } @@ -864,7 +874,7 @@ void sqlite3VdbeMakeReady( if( isExplain ){ nStack = 10; } - p->aStack = sqliteMalloc( + p->aStack = sqlite3DbMallocZer(db, nStack*sizeof(p->aStack[0]) /* aStack */ + nArg*sizeof(Mem*) /* apArg */ + nVar*sizeof(Mem) /* aVar */ @@ -872,7 +882,7 @@ void sqlite3VdbeMakeReady( + nMem*sizeof(Mem) /* aMem */ + nCursor*sizeof(Cursor*) /* apCsr */ ); - if( !sqlite3MallocFailed() ){ + if( !db->mallocFailed ){ p->aMem = &p->aStack[nStack]; p->nMem = nMem; p->aVar = &p->aMem[nMem]; @@ -940,9 +950,9 @@ void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){ p->inVtabMethod = 0; } #endif - sqliteFree(pCx->pData); - sqliteFree(pCx->aType); - sqliteFree(pCx); + sqlite3_free(pCx->pData); + sqlite3_free(pCx->aType); + sqlite3_free(pCx); } /* @@ -979,12 +989,12 @@ static void Cleanup(Vdbe *p){ for(i=0; i<p->contextStackTop; i++){ sqlite3VdbeFifoClear(&p->contextStack[i].sFifo); } - sqliteFree(p->contextStack); + sqlite3_free(p->contextStack); } p->contextStack = 0; p->contextStackDepth = 0; p->contextStackTop = 0; - sqliteFree(p->zErrMsg); + sqlite3_free(p->zErrMsg); p->zErrMsg = 0; p->resOnStack = 0; } @@ -999,10 +1009,10 @@ void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ Mem *pColName; int n; releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); - sqliteFree(p->aColName); + sqlite3_free(p->aColName); n = nResColumn*COLNAME_N; p->nResColumn = nResColumn; - p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n ); + p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); if( p->aColName==0 ) return; while( n-- > 0 ){ (pColName++)->flags = MEM_Null; @@ -1017,7 +1027,7 @@ void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ ** ** If N==P3_STATIC it means that zName is a pointer to a constant static ** string and we can just copy the pointer. If it is P3_DYNAMIC, then -** the string is freed using sqliteFree() when the vdbe is finished with +** the string is freed using sqlite3_free() when the vdbe is finished with ** it. Otherwise, N bytes of zName are copied. */ int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){ @@ -1025,7 +1035,7 @@ int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){ Mem *pColName; assert( idx<p->nResColumn ); assert( var<COLNAME_N ); - if( sqlite3MallocFailed() ) return SQLITE_NOMEM; + if( p->db->mallocFailed ) return SQLITE_NOMEM; assert( p->aColName!=0 ); pColName = &(p->aColName[idx+var*p->nResColumn]); if( N==P3_DYNAMIC || N==P3_STATIC ){ @@ -1135,7 +1145,7 @@ static int vdbeCommit(sqlite3 *db){ /* Select a master journal file name */ do { u32 random; - sqliteFree(zMaster); + sqlite3_free(zMaster); sqlite3Randomness(sizeof(random), &random); zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff); if( !zMaster ){ @@ -1146,7 +1156,7 @@ static int vdbeCommit(sqlite3 *db){ /* Open the master journal. */ rc = sqlite3OsOpenExclusive(zMaster, &master, 0); if( rc!=SQLITE_OK ){ - sqliteFree(zMaster); + sqlite3_free(zMaster); return rc; } @@ -1170,7 +1180,7 @@ static int vdbeCommit(sqlite3 *db){ if( rc!=SQLITE_OK ){ sqlite3OsClose(&master); sqlite3OsDelete(zMaster); - sqliteFree(zMaster); + sqlite3_free(zMaster); return rc; } } @@ -1187,7 +1197,7 @@ static int vdbeCommit(sqlite3 *db){ (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){ sqlite3OsClose(&master); sqlite3OsDelete(zMaster); - sqliteFree(zMaster); + sqlite3_free(zMaster); return rc; } #endif @@ -1210,7 +1220,7 @@ static int vdbeCommit(sqlite3 *db){ } sqlite3OsClose(&master); if( rc!=SQLITE_OK ){ - sqliteFree(zMaster); + sqlite3_free(zMaster); return rc; } @@ -1219,7 +1229,7 @@ static int vdbeCommit(sqlite3 *db){ ** transaction files are deleted. */ rc = sqlite3OsDelete(zMaster); - sqliteFree(zMaster); + sqlite3_free(zMaster); zMaster = 0; if( rc ){ return rc; @@ -1350,7 +1360,7 @@ int sqlite3VdbeHalt(Vdbe *p){ ** */ - if( sqlite3MallocFailed() ){ + if( p->db->mallocFailed ){ p->rc = SQLITE_NOMEM; } if( p->magic!=VDBE_MAGIC_RUN ){ @@ -1663,16 +1673,16 @@ void sqlite3VdbeDelete(Vdbe *p){ Op *pOp = &p->aOp[i]; freeP3(pOp->p3type, pOp->p3); } - sqliteFree(p->aOp); + sqlite3_free(p->aOp); } releaseMemArray(p->aVar, p->nVar); - sqliteFree(p->aLabel); - sqliteFree(p->aStack); + sqlite3_free(p->aLabel); + sqlite3_free(p->aStack); releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); - sqliteFree(p->aColName); - sqliteFree(p->zSql); + sqlite3_free(p->aColName); + sqlite3_free(p->zSql); p->magic = VDBE_MAGIC_DEAD; - sqliteFree(p); + sqlite3_free(p); } /* diff --git a/src/vdbeblob.c b/src/vdbeblob.c index 8ec836f35..8fdf06380 100644 --- a/src/vdbeblob.c +++ b/src/vdbeblob.c @@ -12,7 +12,7 @@ ** ** This file contains code used to implement incremental BLOB I/O. ** -** $Id: vdbeblob.c,v 1.11 2007/06/27 00:36:14 drh Exp $ +** $Id: vdbeblob.c,v 1.12 2007/08/16 04:30:41 drh Exp $ */ #include "sqliteInt.h" @@ -104,7 +104,7 @@ int sqlite3_blob_open( if( sParse.zErrMsg ){ sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg); } - sqliteFree(sParse.zErrMsg); + sqlite3_free(sParse.zErrMsg); rc = SQLITE_ERROR; sqlite3SafetyOff(db); goto blob_open_out; @@ -173,13 +173,13 @@ int sqlite3_blob_open( ** and offset cache without causing any IO. */ sqlite3VdbeChangeP2(v, 5, pTab->nCol+1); - if( !sqlite3MallocFailed() ){ + if( !db->mallocFailed ){ sqlite3VdbeMakeReady(v, 1, 0, 1, 0); } } rc = sqlite3SafetyOff(db); - if( rc!=SQLITE_OK || sqlite3MallocFailed() ){ + if( rc!=SQLITE_OK || db->mallocFailed ){ goto blob_open_out; } @@ -208,9 +208,9 @@ int sqlite3_blob_open( rc = SQLITE_ERROR; goto blob_open_out; } - pBlob = (Incrblob *)sqliteMalloc(sizeof(Incrblob)); - if( sqlite3MallocFailed() ){ - sqliteFree(pBlob); + pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob)); + if( db->mallocFailed ){ + sqlite3_free(pBlob); goto blob_open_out; } pBlob->flags = flags; @@ -228,7 +228,7 @@ int sqlite3_blob_open( blob_open_out: zErr[sizeof(zErr)-1] = '\0'; - if( rc!=SQLITE_OK || sqlite3MallocFailed() ){ + if( rc!=SQLITE_OK || db->mallocFailed ){ sqlite3_finalize((sqlite3_stmt *)v); } sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr)); @@ -242,7 +242,7 @@ blob_open_out: int sqlite3_blob_close(sqlite3_blob *pBlob){ Incrblob *p = (Incrblob *)pBlob; sqlite3_stmt *pStmt = p->pStmt; - sqliteFree(p); + sqlite3_free(p); return sqlite3_finalize(pStmt); } diff --git a/src/vdbefifo.c b/src/vdbefifo.c index a7c419ae5..f03f5c6e9 100644 --- a/src/vdbefifo.c +++ b/src/vdbefifo.c @@ -24,7 +24,7 @@ static FifoPage *allocateFifoPage(int nEntry){ if( nEntry>32767 ){ nEntry = 32767; } - pPage = sqliteMallocRaw( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) ); + pPage = sqlite3_malloc( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) ); if( pPage ){ pPage->nSlot = nEntry; pPage->iWrite = 0; @@ -87,7 +87,7 @@ int sqlite3VdbeFifoPop(Fifo *pFifo, i64 *pVal){ pFifo->nEntry--; if( pPage->iRead>=pPage->iWrite ){ pFifo->pFirst = pPage->pNext; - sqliteFree(pPage); + sqlite3_free(pPage); if( pFifo->nEntry==0 ){ assert( pFifo->pLast==pPage ); pFifo->pLast = 0; @@ -108,7 +108,7 @@ void sqlite3VdbeFifoClear(Fifo *pFifo){ FifoPage *pPage, *pNextPage; for(pPage=pFifo->pFirst; pPage; pPage=pNextPage){ pNextPage = pPage->pNext; - sqliteFree(pPage); + sqlite3_free(pPage); } sqlite3VdbeFifoInit(pFifo); } diff --git a/src/vdbemem.c b/src/vdbemem.c index f05e05cce..b0f26995d 100644 --- a/src/vdbemem.c +++ b/src/vdbemem.c @@ -25,7 +25,7 @@ ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) ** P if required. */ -#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) +#define expandBlob(D,P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(D,P):0) /* ** If pMem is an object with a valid string representation, this routine @@ -66,17 +66,17 @@ int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ ** ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. */ -int sqlite3VdbeMemDynamicify(Mem *pMem){ +int sqlite3VdbeMemDynamicify(sqlite3 *db, Mem *pMem){ int n; u8 *z; - expandBlob(pMem); + expandBlob(db, pMem); if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){ return SQLITE_OK; } assert( (pMem->flags & MEM_Dyn)==0 ); n = pMem->n; assert( pMem->flags & (MEM_Str|MEM_Blob) ); - z = sqliteMallocRaw( n+2 ); + z = sqlite3_malloc( n+2 ); if( z==0 ){ return SQLITE_NOMEM; } @@ -95,15 +95,16 @@ int sqlite3VdbeMemDynamicify(Mem *pMem){ ** blob stored in dynamically allocated space. */ #ifndef SQLITE_OMIT_INCRBLOB -int sqlite3VdbeMemExpandBlob(Mem *pMem){ +int sqlite3VdbeMemExpandBlob(sqlite3 *db, Mem *pMem){ if( pMem->flags & MEM_Zero ){ char *pNew; int nByte; assert( (pMem->flags & MEM_Blob)!=0 ); nByte = pMem->n + pMem->u.i; if( nByte<=0 ) nByte = 1; - pNew = sqliteMalloc(nByte); - if( pNew==0 ){ + pNew = sqlite3_malloc(nByte); + if( pNew==0 ){ + if( db ) db->mallocFailed = 1; return SQLITE_NOMEM; } memcpy(pNew, pMem->z, pMem->n); @@ -126,10 +127,10 @@ int sqlite3VdbeMemExpandBlob(Mem *pMem){ ** ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. */ -int sqlite3VdbeMemMakeWriteable(Mem *pMem){ +int sqlite3VdbeMemMakeWriteable(sqlite3 *db, Mem *pMem){ int n; u8 *z; - expandBlob(pMem); + expandBlob(db, pMem); if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){ return SQLITE_OK; } @@ -139,8 +140,9 @@ int sqlite3VdbeMemMakeWriteable(Mem *pMem){ z = (u8*)pMem->zShort; pMem->flags |= MEM_Short|MEM_Term; }else{ - z = sqliteMallocRaw( n+2 ); + z = sqlite3_malloc( n+2 ); if( z==0 ){ + db->mallocFailed = 1; return SQLITE_NOMEM; } pMem->flags |= MEM_Dyn|MEM_Term; @@ -158,7 +160,7 @@ int sqlite3VdbeMemMakeWriteable(Mem *pMem){ /* ** Make sure the given Mem is \u0000 terminated. */ -int sqlite3VdbeMemNulTerminate(Mem *pMem){ +int sqlite3VdbeMemNulTerminate(sqlite3 *db, Mem *pMem){ if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){ return SQLITE_OK; /* Nothing to do */ } @@ -166,17 +168,19 @@ int sqlite3VdbeMemNulTerminate(Mem *pMem){ return sqlite3VdbeMemMakeWriteable(pMem); }else{ char *z; - sqlite3VdbeMemExpandBlob(pMem); - z = sqliteMalloc(pMem->n+2); - - if( !z ) return SQLITE_NOMEM; + sqlite3VdbeMemExpandBlob(db, pMem); + z = sqlite3_malloc(pMem->n+2); + if( !z ){ + db->mallocFailed = 1; + return SQLITE_NOMEM; + } memcpy(z, pMem->z, pMem->n); z[pMem->n] = 0; z[pMem->n+1] = 0; if( pMem->xDel ){ pMem->xDel(pMem->z); }else{ - sqliteFree(pMem->z); + sqlite3_free(pMem->z); } pMem->xDel = 0; pMem->z = z; @@ -247,7 +251,7 @@ int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ ctx.isError = 0; pFunc->xFinalize(&ctx); if( pMem->z && pMem->z!=pMem->zShort ){ - sqliteFree( pMem->z ); + sqlite3_free( pMem->z ); } *pMem = ctx.s; if( pMem->flags & MEM_Short ){ @@ -276,7 +280,7 @@ void sqlite3VdbeMemRelease(Mem *p){ p->xDel((void *)p->z); } }else{ - sqliteFree(p->z); + sqlite3_free(p->z); } p->z = 0; p->xDel = 0; @@ -723,6 +727,7 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ ** to read from the disk) then the pMem is left in an inconsistent state. */ int sqlite3VdbeMemFromBtree( + sqlite3 *db, /* Database connect to report malloc errors to */ BtCursor *pCur, /* Cursor pointing at record to retrieve. */ int offset, /* Offset from the start of data to return bytes from. */ int amt, /* Number of bytes to return. */ @@ -746,8 +751,9 @@ int sqlite3VdbeMemFromBtree( }else{ int rc; if( amt>NBFS-2 ){ - zData = (char *)sqliteMallocRaw(amt+2); + zData = (char *)sqlite3_malloc(amt+2); if( !zData ){ + db->mallocFailed = 1; return SQLITE_NOMEM; } pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; @@ -771,7 +777,7 @@ int sqlite3VdbeMemFromBtree( if( amt>NBFS-2 ){ assert( zData!=pMem->zShort ); assert( pMem->flags & MEM_Dyn ); - sqliteFree(zData); + sqlite3_free(zData); } else { assert( zData==pMem->zShort ); assert( pMem->flags & MEM_Short ); @@ -842,7 +848,7 @@ void sqlite3VdbeMemSanity(Mem *pMem){ ** If that is the case, then the result must be aligned on an even byte ** boundary. */ -const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ +const void *sqlite3ValueText(sqlite3 *db, sqlite3_value* pVal, u8 enc){ if( !pVal ) return 0; assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); @@ -851,22 +857,22 @@ const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ } assert( (MEM_Blob>>3) == MEM_Str ); pVal->flags |= (pVal->flags & MEM_Blob)>>3; - expandBlob(pVal); + expandBlob(db, pVal); if( pVal->flags&MEM_Str ){ sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){ assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); - if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ + if( sqlite3VdbeMemMakeWriteable(db, pVal)!=SQLITE_OK ){ return 0; } } - sqlite3VdbeMemNulTerminate(pVal); + sqlite3VdbeMemNulTerminate(db, pVal); }else{ assert( (pVal->flags&MEM_Blob)==0 ); - sqlite3VdbeMemStringify(pVal, enc); + sqlite3VdbeMemStringify(db, pVal, enc); assert( 0==(1&(int)pVal->z) ); } - assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || sqlite3MallocFailed() ); + assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || db->mallocFailed ); if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ return pVal->z; }else{ @@ -877,11 +883,13 @@ const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ /* ** Create a new sqlite3_value object. */ -sqlite3_value *sqlite3ValueNew(void){ - Mem *p = sqliteMalloc(sizeof(*p)); +sqlite3_value *sqlite3ValueNew(sqlite3 *db){ + Mem *p = sqlite3_malloc(sizeof(*p)); if( p ){ p->flags = MEM_Null; p->type = SQLITE_NULL; + }else{ + db->mallocFailed = 1; } return p; } @@ -897,10 +905,11 @@ sqlite3_value *sqlite3ValueNew(void){ ** cannot be converted to a value, then *ppVal is set to NULL. */ int sqlite3ValueFromExpr( - Expr *pExpr, - u8 enc, - u8 affinity, - sqlite3_value **ppVal + sqlite3 *db, /* Report malloc() errors here */ + Expr *pExpr, /* The expression to evaluate */ + u8 enc, /* Encoding to use */ + u8 affinity, /* Affinity to use */ + sqlite3_value **ppVal /* Write the new value here */ ){ int op; char *zVal = 0; @@ -913,11 +922,11 @@ int sqlite3ValueFromExpr( op = pExpr->op; if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ - zVal = sqliteStrNDup((char*)pExpr->token.z, pExpr->token.n); - pVal = sqlite3ValueNew(); + zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n); + pVal = sqlite3ValueNew(db); if( !zVal || !pVal ) goto no_mem; sqlite3Dequote(zVal); - sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX); + sqlite3ValueSetStr(db, pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX); if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc); }else{ @@ -933,12 +942,12 @@ int sqlite3ValueFromExpr( else if( op==TK_BLOB ){ int nVal; pVal = sqlite3ValueNew(); - zVal = sqliteStrNDup((char*)pExpr->token.z+1, pExpr->token.n-1); + zVal = sqlite3StrNDup((char*)pExpr->token.z+1, pExpr->token.n-1); if( !zVal || !pVal ) goto no_mem; sqlite3Dequote(zVal); nVal = strlen(zVal)/2; sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(zVal), nVal, 0, sqlite3FreeX); - sqliteFree(zVal); + sqlite3_free(zVal); } #endif @@ -946,7 +955,8 @@ int sqlite3ValueFromExpr( return SQLITE_OK; no_mem: - sqliteFree(zVal); + db->mallocFailed = 1; + sqlite3_free(zVal); sqlite3ValueFree(pVal); *ppVal = 0; return SQLITE_NOMEM; @@ -956,13 +966,14 @@ no_mem: ** Change the string value of an sqlite3_value object */ void sqlite3ValueSetStr( - sqlite3_value *v, - int n, - const void *z, - u8 enc, - void (*xDel)(void*) + sqlite3 *db, /* Report malloc errors here */ + sqlite3_value *v, /* Value to be set */ + int n, /* Length of string z */ + const void *z, /* Text of the new string */ + u8 enc, /* Encoding to use */ + void (*xDel)(void*) /* Destructor for the string */ ){ - if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); + if( v ) sqlite3VdbeMemSetStr(db, (Mem *)v, z, n, enc, xDel); } /* @@ -971,7 +982,7 @@ void sqlite3ValueSetStr( void sqlite3ValueFree(sqlite3_value *v){ if( !v ) return; sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC); - sqliteFree(v); + sqlite3_free(v); } /* diff --git a/src/vtab.c b/src/vtab.c index 611725de9..ebc1216f4 100644 --- a/src/vtab.c +++ b/src/vtab.c @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code used to help implement virtual tables. ** -** $Id: vtab.c,v 1.48 2007/06/26 10:38:55 danielk1977 Exp $ +** $Id: vtab.c,v 1.49 2007/08/16 04:30:41 drh Exp $ */ #ifndef SQLITE_OMIT_VIRTUALTABLE #include "sqliteInt.h" @@ -24,7 +24,7 @@ static int createModule( void (*xDestroy)(void *) /* Module destructor function */ ) { int nName = strlen(zName); - Module *pMod = (Module *)sqliteMallocRaw(sizeof(Module) + nName + 1); + Module *pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); if( pMod ){ char *zCopy = (char *)(&pMod[1]); memcpy(zCopy, zName, nName+1); @@ -36,7 +36,7 @@ static int createModule( if( pMod && pMod->xDestroy ){ pMod->xDestroy(pMod->pAux); } - sqliteFree(pMod); + sqlite3_free(pMod); sqlite3ResetInternalSchema(db, 0); } return sqlite3ApiExit(db, SQLITE_OK); @@ -114,9 +114,9 @@ void sqlite3VtabClear(Table *p){ if( p->azModuleArg ){ int i; for(i=0; i<p->nModuleArg; i++){ - sqliteFree(p->azModuleArg[i]); + sqlite3_free(p->azModuleArg[i]); } - sqliteFree(p->azModuleArg); + sqlite3_free(p->azModuleArg); } } @@ -126,19 +126,20 @@ void sqlite3VtabClear(Table *p){ ** string will be freed automatically when the table is ** deleted. */ -static void addModuleArgument(Table *pTable, char *zArg){ +static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ int i = pTable->nModuleArg++; int nBytes = sizeof(char *)*(1+pTable->nModuleArg); char **azModuleArg; - azModuleArg = sqliteRealloc(pTable->azModuleArg, nBytes); + azModuleArg = sqlite3_realloc(pTable->azModuleArg, nBytes); if( azModuleArg==0 ){ int j; for(j=0; j<i; j++){ - sqliteFree(pTable->azModuleArg[j]); + sqlite3_free(pTable->azModuleArg[j]); } - sqliteFree(zArg); - sqliteFree(pTable->azModuleArg); + sqlite3_free(zArg); + sqlite3_free(pTable->azModuleArg); pTable->nModuleArg = 0; + db->mallocFailed = 1; }else{ azModuleArg[i] = zArg; azModuleArg[i+1] = 0; @@ -159,6 +160,7 @@ void sqlite3VtabBeginParse( ){ int iDb; /* The database the table is being created in */ Table *pTable; /* The new virtual table */ + sqlite3 *db; /* Database connection */ #ifndef SQLITE_OMIT_SHARED_CACHE if( sqlite3ThreadDataReadOnly()->useSharedData ){ @@ -172,14 +174,15 @@ void sqlite3VtabBeginParse( if( pTable==0 || pParse->nErr ) return; assert( 0==pTable->pIndex ); - iDb = sqlite3SchemaToIndex(pParse->db, pTable->pSchema); + db = pParse->db; + iDb = sqlite3SchemaToIndex(db, pTable->pSchema); assert( iDb>=0 ); pTable->isVirtual = 1; pTable->nModuleArg = 0; - addModuleArgument(pTable, sqlite3NameFromToken(pModuleName)); - addModuleArgument(pTable, sqlite3StrDup(pParse->db->aDb[iDb].zName)); - addModuleArgument(pTable, sqlite3StrDup(pTable->zName)); + addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); + addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName)); + addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z; #ifndef SQLITE_OMIT_AUTHORIZATION @@ -204,7 +207,8 @@ static void addArgumentToVtab(Parse *pParse){ if( pParse->sArg.z && pParse->pNewTable ){ const char *z = (const char*)pParse->sArg.z; int n = pParse->sArg.n; - addModuleArgument(pParse->pNewTable, sqliteStrNDup(z, n)); + sqlite3 *db = pParse->db; + addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); } } @@ -267,7 +271,7 @@ void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ pTab->zName, zStmt ); - sqliteFree(zStmt); + sqlite3_free(zStmt); v = sqlite3GetVdbe(pParse); sqlite3ChangeCookie(db, v, iDb); @@ -375,7 +379,7 @@ static int vtabCallConstructor( rc = rc2; } db->pVTab = 0; - sqliteFree(zModuleName); + sqlite3_free(zModuleName); /* If everything went according to plan, loop through the columns ** of the table to see if any of them contain the token "hidden". @@ -444,7 +448,7 @@ int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ if( rc!=SQLITE_OK ){ sqlite3ErrorMsg(pParse, "%s", zErr); } - sqliteFree(zErr); + sqlite3_free(zErr); } return rc; @@ -460,8 +464,9 @@ static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){ if( (db->nVTrans%ARRAY_INCR)==0 ){ sqlite3_vtab **aVTrans; int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); - aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes); + aVTrans = sqlite3_realloc((void *)db->aVTrans, nBytes); if( !aVTrans ){ + db->mallocFailed = 1; return SQLITE_NOMEM; } memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); @@ -480,7 +485,7 @@ static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){ ** ** If an error occurs, *pzErr is set to point an an English language ** description of the error and an SQLITE_XXX error code is returned. -** In this case the caller must call sqliteFree() on *pzErr. +** In this case the caller must call sqlite3_free() on *pzErr. */ int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ int rc = SQLITE_OK; @@ -546,7 +551,7 @@ int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ db->pVTab = 0; } else { sqlite3Error(db, SQLITE_ERROR, zErr); - sqliteFree(zErr); + sqlite3_free(zErr); rc = SQLITE_ERROR; } sParse.declareVtab = 0; @@ -607,7 +612,7 @@ static void callFinaliser(sqlite3 *db, int offset){ if( x ) x(pVtab); sqlite3VtabUnlock(db, pVtab); } - sqliteFree(db->aVTrans); + sqlite3_free(db->aVTrans); db->nVTrans = 0; db->aVTrans = 0; } @@ -724,6 +729,7 @@ int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){ ** SQLITE_FUNC_EPHEM flag. */ FuncDef *sqlite3VtabOverloadFunction( + sqlite3 *db, /* Database connection for reporting malloc problems */ FuncDef *pDef, /* Function to possibly overload */ int nArg, /* Number of arguments to the function */ Expr *pExpr /* First argument to the function */ @@ -754,19 +760,21 @@ FuncDef *sqlite3VtabOverloadFunction( /* Call the xFuncFunction method on the virtual table implementation ** to see if the implementation wants to overload this function */ - zLowerName = sqlite3StrDup(pDef->zName); - for(z=(unsigned char*)zLowerName; *z; z++){ - *z = sqlite3UpperToLower[*z]; + zLowerName = sqlite3DbStrDup(db, pDef->zName); + if( zLowerName ){ + for(z=(unsigned char*)zLowerName; *z; z++){ + *z = sqlite3UpperToLower[*z]; + } + rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg); + sqlite3_free(zLowerName); } - rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg); - sqliteFree(zLowerName); if( rc==0 ){ return pDef; } /* Create a new ephemeral function definition for the overloaded ** function */ - pNew = sqliteMalloc( sizeof(*pNew) + strlen(pDef->zName) ); + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) ); if( pNew==0 ){ return pDef; } diff --git a/src/where.c b/src/where.c index bf255d177..f55d3b89d 100644 --- a/src/where.c +++ b/src/where.c @@ -16,7 +16,7 @@ ** so is applicable. Because this module is responsible for selecting ** indices, you might also think of this module as the "query optimizer". ** -** $Id: where.c,v 1.254 2007/07/30 14:40:48 danielk1977 Exp $ +** $Id: where.c,v 1.255 2007/08/16 04:30:41 drh Exp $ */ #include "sqliteInt.h" @@ -207,7 +207,7 @@ static void whereClauseClear(WhereClause *pWC){ } } if( pWC->a!=pWC->aStatic ){ - sqliteFree(pWC->a); + sqlite3_free(pWC->a); } } @@ -228,8 +228,9 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, int flags){ int idx; if( pWC->nTerm>=pWC->nSlot ){ WhereTerm *pOld = pWC->a; - pWC->a = sqliteMalloc( sizeof(pWC->a[0])*pWC->nSlot*2 ); + pWC->a = sqlite3_malloc( sizeof(pWC->a[0])*pWC->nSlot*2 ); if( pWC->a==0 ){ + pWC->pParse->db->mallocFailed = 1; if( flags & TERM_DYNAMIC ){ sqlite3ExprDelete(p); } @@ -237,7 +238,7 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, int flags){ } memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); if( pOld!=pWC->aStatic ){ - sqliteFree(pOld); + sqlite3_free(pOld); } pWC->nSlot *= 2; } @@ -550,7 +551,7 @@ static int isLikeOrGlob( (pColl->type!=SQLITE_COLL_NOCASE || !noCase) ){ return 0; } - sqlite3DequoteExpr(pRight); + sqlite3DequoteExpr(db, pRight); z = (char *)pRight->token.z; for(cnt=0; (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2]; cnt++){} if( cnt==0 || 255==(u8)z[cnt] ){ @@ -715,8 +716,9 @@ static void exprAnalyze( int nPattern; int isComplete; int op; + sqlite3 *db = pWC->pParse->db; - if( sqlite3MallocFailed() ) return; + if( db->mallocFailed ) return; prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); op = pExpr->op; if( op==TK_IN ){ @@ -749,8 +751,8 @@ static void exprAnalyze( Expr *pDup; if( pTerm->leftCursor>=0 ){ int idxNew; - pDup = sqlite3ExprDup(pExpr); - if( sqlite3MallocFailed() ){ + pDup = sqlite3ExprDup(db, pExpr); + if( db->mallocFailed ){ sqlite3ExprDelete(pDup); return; } @@ -788,8 +790,8 @@ static void exprAnalyze( for(i=0; i<2; i++){ Expr *pNewExpr; int idxNew; - pNewExpr = sqlite3Expr(ops[i], sqlite3ExprDup(pExpr->pLeft), - sqlite3ExprDup(pList->a[i].pExpr), 0); + pNewExpr = sqlite3Expr(ops[i], sqlite3ExprDup(db, pExpr->pLeft), + sqlite3ExprDup(db, pList->a[i].pExpr), 0); idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); exprAnalyze(pSrc, pWC, idxNew); pTerm = &pWC->a[idxTerm]; @@ -849,12 +851,12 @@ static void exprAnalyze( Expr *pLeft = 0; for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){ if( (pOrTerm->flags & TERM_OR_OK)==0 ) continue; - pDup = sqlite3ExprDup(pOrTerm->pExpr->pRight); - pList = sqlite3ExprListAppend(pList, pDup, 0); + pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight); + pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup, 0); pLeft = pOrTerm->pExpr->pLeft; } assert( pLeft!=0 ); - pDup = sqlite3ExprDup(pLeft); + pDup = sqlite3ExprDup(db, pLeft); pNew = sqlite3Expr(TK_IN, pDup, 0, 0); if( pNew ){ int idxNew; @@ -878,7 +880,7 @@ or_not_possible: /* Add constraints to reduce the search space on a LIKE or GLOB ** operator. */ - if( isLikeOrGlob(pWC->pParse->db, pExpr, &nPattern, &isComplete) ){ + if( isLikeOrGlob(db, pExpr, &nPattern, &isComplete) ){ Expr *pLeft, *pRight; Expr *pStr1, *pStr2; Expr *pNewExpr1, *pNewExpr2; @@ -886,21 +888,21 @@ or_not_possible: pLeft = pExpr->pList->a[1].pExpr; pRight = pExpr->pList->a[0].pExpr; - pStr1 = sqlite3Expr(TK_STRING, 0, 0, 0); + pStr1 = sqlite3PExpr(pParse, TK_STRING, 0, 0, 0); if( pStr1 ){ - sqlite3TokenCopy(&pStr1->token, &pRight->token); + sqlite3TokenCopy(db, &pStr1->token, &pRight->token); pStr1->token.n = nPattern; pStr1->flags = EP_Dequoted; } - pStr2 = sqlite3ExprDup(pStr1); + pStr2 = sqlite3ExprDup(db, pStr1); if( pStr2 ){ assert( pStr2->token.dyn ); ++*(u8*)&pStr2->token.z[nPattern-1]; } - pNewExpr1 = sqlite3Expr(TK_GE, sqlite3ExprDup(pLeft), pStr1, 0); + pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft), pStr1, 0); idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); exprAnalyze(pSrc, pWC, idxNew1); - pNewExpr2 = sqlite3Expr(TK_LT, sqlite3ExprDup(pLeft), pStr2, 0); + pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft), pStr2, 0); idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); exprAnalyze(pSrc, pWC, idxNew2); pTerm = &pWC->a[idxTerm]; @@ -931,7 +933,7 @@ or_not_possible: prereqColumn = exprTableUsage(pMaskSet, pLeft); if( (prereqExpr & prereqColumn)==0 ){ Expr *pNewExpr; - pNewExpr = sqlite3Expr(TK_MATCH, 0, sqlite3ExprDup(pRight), 0); + pNewExpr = sqlite3Expr(TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0); idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); pNewTerm = &pWC->a[idxNew]; pNewTerm->prereqRight = prereqExpr; @@ -1257,10 +1259,11 @@ static double bestVirtualIndex( /* Allocate the sqlite3_index_info structure */ - pIdxInfo = sqliteMalloc( sizeof(*pIdxInfo) + pIdxInfo = sqlite3_malloc( sizeof(*pIdxInfo) + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm + sizeof(*pIdxOrderBy)*nOrderBy ); if( pIdxInfo==0 ){ + pParse->db->mallocFailed = 1; sqlite3ErrorMsg(pParse, "out of memory"); return 0.0; } @@ -1375,7 +1378,7 @@ static double bestVirtualIndex( TRACE_IDX_OUTPUTS(pIdxInfo); if( rc!=SQLITE_OK ){ if( rc==SQLITE_NOMEM ){ - sqlite3FailedMalloc(); + pParse->db->mallocFailed = 1; }else { sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); } @@ -1736,7 +1739,7 @@ static void codeEqualityTerm( pLevel->nxt = sqlite3VdbeMakeLabel(v); } pLevel->nIn++; - pLevel->aInLoop = sqliteReallocOrFree(pLevel->aInLoop, + pLevel->aInLoop = sqlite3DbReallocOrFree(pParse->db, pLevel->aInLoop, sizeof(pLevel->aInLoop[0])*pLevel->nIn); pIn = pLevel->aInLoop; if( pIn ){ @@ -1855,10 +1858,10 @@ static void whereInfoFree(WhereInfo *pWInfo){ */ sqlite3_free(pInfo->idxStr); } - sqliteFree(pInfo); + sqlite3_free(pInfo); } } - sqliteFree(pWInfo); + sqlite3_free(pWInfo); } } @@ -1969,6 +1972,7 @@ WhereInfo *sqlite3WhereBegin( WhereLevel *pLevel; /* A single level in the pWInfo list */ int iFrom; /* First unused FROM clause element */ int andFlags; /* AND-ed combination of all wc.a[].flags */ + sqlite3 *db; /* Database connection */ /* The number of tables in the FROM clause is limited by the number of ** bits in a Bitmask @@ -1988,8 +1992,10 @@ WhereInfo *sqlite3WhereBegin( /* Allocate and initialize the WhereInfo structure that will become the ** return value. */ - pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel)); - if( sqlite3MallocFailed() ){ + db = pParse->db; + pWInfo = sqlite3DbMallocZero(db, + sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel)); + if( db->mallocFailed ){ goto whereBeginNoMem; } pWInfo->nLevel = pTabList->nSrc; @@ -2014,7 +2020,7 @@ WhereInfo *sqlite3WhereBegin( createMask(&maskSet, pTabList->a[i].iCursor); } exprAnalyzeAll(pTabList, &wc); - if( sqlite3MallocFailed() ){ + if( db->mallocFailed ){ goto whereBeginNoMem; } @@ -2691,7 +2697,7 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){ sqlite3VdbeAddOp(v, OP_Next, pIn->iCur, pIn->topAddr); sqlite3VdbeJumpHere(v, pIn->topAddr-1); } - sqliteFree(pLevel->aInLoop); + sqlite3_free(pLevel->aInLoop); } sqlite3VdbeResolveLabel(v, pLevel->brk); if( pLevel->iLeftJoin ){ |