mirror of
https://github.com/sqlite/sqlite.git
synced 2025-08-07 02:42:48 +03:00
Fix minor malloc() related problems and add sqlite3_soft_heap_limit() stubs. (CVS 2814)
FossilOrigin-Name: 1637f3796015d1582ed8c6bc8bdf8c067b4bade9
This commit is contained in:
80
src/main.c
80
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.307 2005/12/09 20:02:05 drh Exp $
|
||||
** $Id: main.c,v 1.308 2005/12/12 06:53:04 danielk1977 Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
@@ -153,6 +153,9 @@ int sqlite3_close(sqlite3 *db){
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
/* sqlite3_close() may not invoke sqliteMalloc(). */
|
||||
sqlite3MallocDisallow();
|
||||
|
||||
for(j=0; j<db->nDb; j++){
|
||||
struct Db *pDb = &db->aDb[j];
|
||||
if( pDb->pBt ){
|
||||
@@ -186,6 +189,7 @@ int sqlite3_close(sqlite3 *db){
|
||||
sqlite3ValueFree(db->pErr);
|
||||
}
|
||||
|
||||
#if 0
|
||||
#ifndef SQLITE_OMIT_GLOBALRECOVER
|
||||
{
|
||||
sqlite3 *pPrev;
|
||||
@@ -202,10 +206,12 @@ int sqlite3_close(sqlite3 *db){
|
||||
}
|
||||
sqlite3Os.xLeaveMutex();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
db->magic = SQLITE_MAGIC_ERROR;
|
||||
sqliteFree(db);
|
||||
sqlite3MallocAllow();
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
@@ -691,10 +697,11 @@ const void *sqlite3_errmsg16(sqlite3 *db){
|
||||
#endif /* SQLITE_OMIT_UTF16 */
|
||||
|
||||
/*
|
||||
** Return the most recent error code generated by an SQLite routine.
|
||||
** Return the most recent error code generated by an SQLite routine. If NULL is
|
||||
** passed to this function, we assume a malloc() failed during sqlite3_open().
|
||||
*/
|
||||
int sqlite3_errcode(sqlite3 *db){
|
||||
if( sqlite3Tsd()->mallocFailed ){
|
||||
if( !db || sqlite3Tsd()->mallocFailed ){
|
||||
return SQLITE_NOMEM;
|
||||
}
|
||||
if( sqlite3SafetyCheck(db) ){
|
||||
@@ -716,6 +723,8 @@ static int openDatabase(
|
||||
int rc, i;
|
||||
CollSeq *pColl;
|
||||
|
||||
assert( !sqlite3Tsd()->mallocFailed );
|
||||
|
||||
/* Allocate the sqlite data structure */
|
||||
db = sqliteMalloc( sizeof(sqlite3) );
|
||||
if( db==0 ) goto opendb_out;
|
||||
@@ -741,8 +750,12 @@ static int openDatabase(
|
||||
*/
|
||||
if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||
|
||||
sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||
|
||||
(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0))==0 ){
|
||||
assert(rc!=SQLITE_OK || sqlite3Tsd()->mallocFailed);
|
||||
(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0))==0
|
||||
){
|
||||
/* sqlite3_create_collation() is an external API. So the mallocFailed flag
|
||||
** will have been cleared before returning. So set it explicitly here.
|
||||
*/
|
||||
sqlite3Tsd()->mallocFailed = 1;
|
||||
db->magic = SQLITE_MAGIC_CLOSED;
|
||||
goto opendb_out;
|
||||
}
|
||||
@@ -785,19 +798,13 @@ static int openDatabase(
|
||||
db->magic = SQLITE_MAGIC_OPEN;
|
||||
|
||||
opendb_out:
|
||||
if( sqlite3_errcode(db)==SQLITE_OK && sqlite3Tsd()->mallocFailed ){
|
||||
sqlite3Error(db, SQLITE_NOMEM, 0);
|
||||
if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
|
||||
sqlite3_close(db);
|
||||
db = 0;
|
||||
}
|
||||
*ppDb = db;
|
||||
#ifndef SQLITE_OMIT_GLOBALRECOVER
|
||||
if( db ){
|
||||
sqlite3Os.xEnterMutex();
|
||||
db->pNext = pDbList;
|
||||
pDbList = db;
|
||||
sqlite3Os.xLeaveMutex();
|
||||
}
|
||||
#endif
|
||||
return sqlite3_errcode(db);
|
||||
sqlite3MallocClearFailed();
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -822,6 +829,7 @@ int sqlite3_open16(
|
||||
int rc = SQLITE_NOMEM;
|
||||
sqlite3_value *pVal;
|
||||
|
||||
assert( zFilename );
|
||||
assert( ppDb );
|
||||
*ppDb = 0;
|
||||
pVal = sqlite3ValueNew();
|
||||
@@ -832,10 +840,11 @@ int sqlite3_open16(
|
||||
if( rc==SQLITE_OK && *ppDb ){
|
||||
rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
|
||||
}
|
||||
}else{
|
||||
assert( sqlite3Tsd()->mallocFailed );
|
||||
sqlite3MallocClearFailed();
|
||||
}
|
||||
if( pVal ){
|
||||
sqlite3ValueFree(pVal);
|
||||
}
|
||||
sqlite3ValueFree(pVal);
|
||||
|
||||
return rc;
|
||||
}
|
||||
@@ -1001,39 +1010,10 @@ int sqlite3_collation_needed16(
|
||||
|
||||
#ifndef SQLITE_OMIT_GLOBALRECOVER
|
||||
/*
|
||||
** This function is called to recover from a malloc failure that occured
|
||||
** within SQLite.
|
||||
**
|
||||
** This function is *not* threadsafe. Calling this from within a threaded
|
||||
** application when threads other than the caller have used SQLite is
|
||||
** dangerous and will almost certainly result in malfunctions.
|
||||
** This function is now an anachronism. It used to be used to recover from a
|
||||
** malloc() failure, but SQLite now does this automatically.
|
||||
*/
|
||||
int sqlite3_global_recover(){
|
||||
#if 0
|
||||
int rc = SQLITE_OK;
|
||||
|
||||
if( sqlite3Tsd()->mallocFailed ){
|
||||
sqlite3 *db;
|
||||
int i;
|
||||
sqlite3Tsd()->mallocFailed = 0;
|
||||
for(db=pDbList; db; db=db->pNext ){
|
||||
sqlite3ExpirePreparedStatements(db);
|
||||
for(i=0; i<db->nDb; i++){
|
||||
Btree *pBt = db->aDb[i].pBt;
|
||||
if( pBt && (rc=sqlite3BtreeReset(pBt))!=0 ){
|
||||
goto recover_out;
|
||||
}
|
||||
}
|
||||
db->autoCommit = 1;
|
||||
}
|
||||
}
|
||||
|
||||
recover_out:
|
||||
if( rc!=SQLITE_OK ){
|
||||
sqlite3Tsd()->mallocFailed = 1;
|
||||
}
|
||||
return rc;
|
||||
#endif
|
||||
return SQLITE_OK;
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user