extensions/sqlite/sqlite-source/main.c
changeset 1645 27307df4d711
parent 1203 633c25ce6405
     1.1 --- a/extensions/sqlite/sqlite-source/main.c	Sun Oct 28 05:06:12 2007 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/main.c	Sun Oct 28 05:48:06 2007 +0000
     1.3 @@ -17,7 +17,6 @@
     1.4  ** $Id$
     1.5  */
     1.6  #include "sqliteInt.h"
     1.7 -#include "os.h"
     1.8  #include <ctype.h>
     1.9  
    1.10  /*
    1.11 @@ -26,6 +25,7 @@
    1.12  const char sqlite3_version[] = SQLITE_VERSION;
    1.13  const char *sqlite3_libversion(void){ return sqlite3_version; }
    1.14  int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
    1.15 +int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
    1.16  
    1.17  /*
    1.18  ** If the following function pointer is not NULL and if
    1.19 @@ -119,6 +119,7 @@
    1.20    if( sqlite3SafetyCheck(db) ){
    1.21      return SQLITE_MISUSE;
    1.22    }
    1.23 +  sqlite3_mutex_enter(db->mutex);
    1.24  
    1.25  #ifdef SQLITE_SSE
    1.26    {
    1.27 @@ -142,6 +143,7 @@
    1.28    if( db->pVdbe ){
    1.29      sqlite3Error(db, SQLITE_BUSY, 
    1.30          "Unable to close due to unfinalised statements");
    1.31 +    sqlite3_mutex_leave(db->mutex);
    1.32      return SQLITE_BUSY;
    1.33    }
    1.34    assert( !sqlite3SafetyCheck(db) );
    1.35 @@ -156,6 +158,7 @@
    1.36    */
    1.37    if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
    1.38      /* printf("DID NOT CLOSE\n"); fflush(stdout); */
    1.39 +    sqlite3_mutex_leave(db->mutex);
    1.40      return SQLITE_ERROR;
    1.41    }
    1.42  
    1.43 @@ -176,7 +179,7 @@
    1.44      FuncDef *pFunc, *pNext;
    1.45      for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
    1.46        pNext = pFunc->pNext;
    1.47 -      sqliteFree(pFunc);
    1.48 +      sqlite3_free(pFunc);
    1.49      }
    1.50    }
    1.51  
    1.52 @@ -188,7 +191,7 @@
    1.53          pColl[j].xDel(pColl[j].pUser);
    1.54        }
    1.55      }
    1.56 -    sqliteFree(pColl);
    1.57 +    sqlite3_free(pColl);
    1.58    }
    1.59    sqlite3HashClear(&db->aCollSeq);
    1.60  #ifndef SQLITE_OMIT_VIRTUALTABLE
    1.61 @@ -197,7 +200,7 @@
    1.62      if( pMod->xDestroy ){
    1.63        pMod->xDestroy(pMod->pAux);
    1.64      }
    1.65 -    sqliteFree(pMod);
    1.66 +    sqlite3_free(pMod);
    1.67    }
    1.68    sqlite3HashClear(&db->aModule);
    1.69  #endif
    1.70 @@ -217,9 +220,10 @@
    1.71    ** the same sqliteMalloc() as the one that allocates the database 
    1.72    ** structure?
    1.73    */
    1.74 -  sqliteFree(db->aDb[1].pSchema);
    1.75 -  sqliteFree(db);
    1.76 -  sqlite3ReleaseThreadData();
    1.77 +  sqlite3_free(db->aDb[1].pSchema);
    1.78 +  sqlite3_mutex_leave(db->mutex);
    1.79 +  sqlite3_mutex_free(db->mutex);
    1.80 +  sqlite3_free(db);
    1.81    return SQLITE_OK;
    1.82  }
    1.83  
    1.84 @@ -229,6 +233,8 @@
    1.85  void sqlite3RollbackAll(sqlite3 *db){
    1.86    int i;
    1.87    int inTrans = 0;
    1.88 +  assert( sqlite3_mutex_held(db->mutex) );
    1.89 +  sqlite3MallocEnterBenignBlock(1);                 /* Enter benign region */
    1.90    for(i=0; i<db->nDb; i++){
    1.91      if( db->aDb[i].pBt ){
    1.92        if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
    1.93 @@ -239,7 +245,10 @@
    1.94      }
    1.95    }
    1.96    sqlite3VtabRollback(db);
    1.97 +  sqlite3MallocLeaveBenignBlock();                 /* Leave benign region */
    1.98 +
    1.99    if( db->flags&SQLITE_InternChanges ){
   1.100 +    sqlite3ExpirePreparedStatements(db);
   1.101      sqlite3ResetInternalSchema(db, 0);
   1.102    }
   1.103  
   1.104 @@ -303,7 +312,8 @@
   1.105    static const u8 totals[] =
   1.106       { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
   1.107  # define NDELAY (sizeof(delays)/sizeof(delays[0]))
   1.108 -  int timeout = ((sqlite3 *)ptr)->busyTimeout;
   1.109 +  sqlite3 *db = (sqlite3 *)ptr;
   1.110 +  int timeout = db->busyTimeout;
   1.111    int delay, prior;
   1.112  
   1.113    assert( count>=0 );
   1.114 @@ -318,14 +328,15 @@
   1.115      delay = timeout - prior;
   1.116      if( delay<=0 ) return 0;
   1.117    }
   1.118 -  sqlite3OsSleep(delay);
   1.119 +  sqlite3OsSleep(db->pVfs, delay*1000);
   1.120    return 1;
   1.121  #else
   1.122 +  sqlite3 *db = (sqlite3 *)ptr;
   1.123    int timeout = ((sqlite3 *)ptr)->busyTimeout;
   1.124    if( (count+1)*1000 > timeout ){
   1.125      return 0;
   1.126    }
   1.127 -  sqlite3OsSleep(1000);
   1.128 +  sqlite3OsSleep(db->pVfs, 1000000);
   1.129    return 1;
   1.130  #endif
   1.131  }
   1.132 @@ -361,9 +372,11 @@
   1.133    if( sqlite3SafetyCheck(db) ){
   1.134      return SQLITE_MISUSE;
   1.135    }
   1.136 +  sqlite3_mutex_enter(db->mutex);
   1.137    db->busyHandler.xFunc = xBusy;
   1.138    db->busyHandler.pArg = pArg;
   1.139    db->busyHandler.nBusy = 0;
   1.140 +  sqlite3_mutex_leave(db->mutex);
   1.141    return SQLITE_OK;
   1.142  }
   1.143  
   1.144 @@ -380,6 +393,7 @@
   1.145    void *pArg
   1.146  ){
   1.147    if( !sqlite3SafetyCheck(db) ){
   1.148 +    sqlite3_mutex_enter(db->mutex);
   1.149      if( nOps>0 ){
   1.150        db->xProgress = xProgress;
   1.151        db->nProgressOps = nOps;
   1.152 @@ -389,6 +403,7 @@
   1.153        db->nProgressOps = 0;
   1.154        db->pProgressArg = 0;
   1.155      }
   1.156 +    sqlite3_mutex_leave(db->mutex);
   1.157    }
   1.158  }
   1.159  #endif
   1.160 @@ -420,30 +435,6 @@
   1.161    }
   1.162  }
   1.163  
   1.164 -/*
   1.165 -** Memory allocation routines that use SQLites internal memory
   1.166 -** memory allocator.  Depending on how SQLite is compiled, the
   1.167 -** internal memory allocator might be just an alias for the
   1.168 -** system default malloc/realloc/free.  Or the built-in allocator
   1.169 -** might do extra stuff like put sentinals around buffers to 
   1.170 -** check for overruns or look for memory leaks.
   1.171 -**
   1.172 -** Use sqlite3_free() to free memory returned by sqlite3_mprintf().
   1.173 -*/
   1.174 -void sqlite3_free(void *p){ if( p ) sqlite3OsFree(p); }
   1.175 -void *sqlite3_malloc(int nByte){ return nByte>0 ? sqlite3OsMalloc(nByte) : 0; }
   1.176 -void *sqlite3_realloc(void *pOld, int nByte){ 
   1.177 -  if( pOld ){
   1.178 -    if( nByte>0 ){
   1.179 -      return sqlite3OsRealloc(pOld, nByte);
   1.180 -    }else{
   1.181 -      sqlite3OsFree(pOld);
   1.182 -      return 0;
   1.183 -    }
   1.184 -  }else{
   1.185 -    return sqlite3_malloc(nByte);
   1.186 -  }
   1.187 -}
   1.188  
   1.189  /*
   1.190  ** This function is exactly the same as sqlite3_create_function(), except
   1.191 @@ -464,6 +455,7 @@
   1.192    FuncDef *p;
   1.193    int nName;
   1.194  
   1.195 +  assert( sqlite3_mutex_held(db->mutex) );
   1.196    if( sqlite3SafetyCheck(db) ){
   1.197      return SQLITE_MISUSE;
   1.198    }
   1.199 @@ -491,10 +483,13 @@
   1.200      int rc;
   1.201      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
   1.202           pUserData, xFunc, xStep, xFinal);
   1.203 -    if( rc!=SQLITE_OK ) return rc;
   1.204 -    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
   1.205 -        pUserData, xFunc, xStep, xFinal);
   1.206 -    if( rc!=SQLITE_OK ) return rc;
   1.207 +    if( rc==SQLITE_OK ){
   1.208 +      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
   1.209 +          pUserData, xFunc, xStep, xFinal);
   1.210 +    }
   1.211 +    if( rc!=SQLITE_OK ){
   1.212 +      return rc;
   1.213 +    }
   1.214      enc = SQLITE_UTF16BE;
   1.215    }
   1.216  #else
   1.217 @@ -511,7 +506,7 @@
   1.218      if( db->activeVdbeCnt ){
   1.219        sqlite3Error(db, SQLITE_BUSY, 
   1.220          "Unable to delete/modify user-function due to active statements");
   1.221 -      assert( !sqlite3MallocFailed() );
   1.222 +      assert( !db->mallocFailed );
   1.223        return SQLITE_BUSY;
   1.224      }else{
   1.225        sqlite3ExpirePreparedStatements(db);
   1.226 @@ -519,14 +514,16 @@
   1.227    }
   1.228  
   1.229    p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
   1.230 -  if( p ){
   1.231 -    p->flags = 0;
   1.232 -    p->xFunc = xFunc;
   1.233 -    p->xStep = xStep;
   1.234 -    p->xFinalize = xFinal;
   1.235 -    p->pUserData = pUserData;
   1.236 -    p->nArg = nArg;
   1.237 +  assert(p || db->mallocFailed);
   1.238 +  if( !p ){
   1.239 +    return SQLITE_NOMEM;
   1.240    }
   1.241 +  p->flags = 0;
   1.242 +  p->xFunc = xFunc;
   1.243 +  p->xStep = xStep;
   1.244 +  p->xFinalize = xFinal;
   1.245 +  p->pUserData = pUserData;
   1.246 +  p->nArg = nArg;
   1.247    return SQLITE_OK;
   1.248  }
   1.249  
   1.250 @@ -544,10 +541,12 @@
   1.251    void (*xFinal)(sqlite3_context*)
   1.252  ){
   1.253    int rc;
   1.254 -  assert( !sqlite3MallocFailed() );
   1.255 +  sqlite3_mutex_enter(db->mutex);
   1.256 +  assert( !db->mallocFailed );
   1.257    rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
   1.258 -
   1.259 -  return sqlite3ApiExit(db, rc);
   1.260 +  rc = sqlite3ApiExit(db, rc);
   1.261 +  sqlite3_mutex_leave(db->mutex);
   1.262 +  return rc;
   1.263  }
   1.264  
   1.265  #ifndef SQLITE_OMIT_UTF16
   1.266 @@ -563,13 +562,14 @@
   1.267  ){
   1.268    int rc;
   1.269    char *zFunc8;
   1.270 -  assert( !sqlite3MallocFailed() );
   1.271 -
   1.272 -  zFunc8 = sqlite3Utf16to8(zFunctionName, -1);
   1.273 +  sqlite3_mutex_enter(db->mutex);
   1.274 +  assert( !db->mallocFailed );
   1.275 +  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
   1.276    rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
   1.277 -  sqliteFree(zFunc8);
   1.278 -
   1.279 -  return sqlite3ApiExit(db, rc);
   1.280 +  sqlite3_free(zFunc8);
   1.281 +  rc = sqlite3ApiExit(db, rc);
   1.282 +  sqlite3_mutex_leave(db->mutex);
   1.283 +  return rc;
   1.284  }
   1.285  #endif
   1.286  
   1.287 @@ -592,11 +592,15 @@
   1.288    int nArg
   1.289  ){
   1.290    int nName = strlen(zName);
   1.291 +  int rc;
   1.292 +  sqlite3_mutex_enter(db->mutex);
   1.293    if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
   1.294      sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
   1.295                        0, sqlite3InvalidFunction, 0, 0);
   1.296    }
   1.297 -  return sqlite3ApiExit(db, SQLITE_OK);
   1.298 +  rc = sqlite3ApiExit(db, SQLITE_OK);
   1.299 +  sqlite3_mutex_leave(db->mutex);
   1.300 +  return rc;
   1.301  }
   1.302  
   1.303  #ifndef SQLITE_OMIT_TRACE
   1.304 @@ -609,9 +613,12 @@
   1.305  ** SQL statement.
   1.306  */
   1.307  void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
   1.308 -  void *pOld = db->pTraceArg;
   1.309 +  void *pOld;
   1.310 +  sqlite3_mutex_enter(db->mutex);
   1.311 +  pOld = db->pTraceArg;
   1.312    db->xTrace = xTrace;
   1.313    db->pTraceArg = pArg;
   1.314 +  sqlite3_mutex_leave(db->mutex);
   1.315    return pOld;
   1.316  }
   1.317  /*
   1.318 @@ -627,9 +634,12 @@
   1.319    void (*xProfile)(void*,const char*,sqlite_uint64),
   1.320    void *pArg
   1.321  ){
   1.322 -  void *pOld = db->pProfileArg;
   1.323 +  void *pOld;
   1.324 +  sqlite3_mutex_enter(db->mutex);
   1.325 +  pOld = db->pProfileArg;
   1.326    db->xProfile = xProfile;
   1.327    db->pProfileArg = pArg;
   1.328 +  sqlite3_mutex_leave(db->mutex);
   1.329    return pOld;
   1.330  }
   1.331  #endif /* SQLITE_OMIT_TRACE */
   1.332 @@ -645,9 +655,12 @@
   1.333    int (*xCallback)(void*),  /* Function to invoke on each commit */
   1.334    void *pArg                /* Argument to the function */
   1.335  ){
   1.336 -  void *pOld = db->pCommitArg;
   1.337 +  void *pOld;
   1.338 +  sqlite3_mutex_enter(db->mutex);
   1.339 +  pOld = db->pCommitArg;
   1.340    db->xCommitCallback = xCallback;
   1.341    db->pCommitArg = pArg;
   1.342 +  sqlite3_mutex_leave(db->mutex);
   1.343    return pOld;
   1.344  }
   1.345  
   1.346 @@ -660,9 +673,12 @@
   1.347    void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
   1.348    void *pArg                /* Argument to the function */
   1.349  ){
   1.350 -  void *pRet = db->pUpdateArg;
   1.351 +  void *pRet;
   1.352 +  sqlite3_mutex_enter(db->mutex);
   1.353 +  pRet = db->pUpdateArg;
   1.354    db->xUpdateCallback = xCallback;
   1.355    db->pUpdateArg = pArg;
   1.356 +  sqlite3_mutex_leave(db->mutex);
   1.357    return pRet;
   1.358  }
   1.359  
   1.360 @@ -675,9 +691,12 @@
   1.361    void (*xCallback)(void*), /* Callback function */
   1.362    void *pArg                /* Argument to the function */
   1.363  ){
   1.364 -  void *pRet = db->pRollbackArg;
   1.365 +  void *pRet;
   1.366 +  sqlite3_mutex_enter(db->mutex);
   1.367 +  pRet = db->pRollbackArg;
   1.368    db->xRollbackCallback = xCallback;
   1.369    db->pRollbackArg = pArg;
   1.370 +  sqlite3_mutex_leave(db->mutex);
   1.371    return pRet;
   1.372  }
   1.373  
   1.374 @@ -711,17 +730,19 @@
   1.375    const char *zFilename,    /* Name of the file containing the BTree database */
   1.376    int omitJournal,          /* if TRUE then do not journal this file */
   1.377    int nCache,               /* How many pages in the page cache */
   1.378 +  int vfsFlags,             /* Flags passed through to vfsOpen */
   1.379    Btree **ppBtree           /* Pointer to new Btree object written here */
   1.380  ){
   1.381 -  int btree_flags = 0;
   1.382 +  int btFlags = 0;
   1.383    int rc;
   1.384    
   1.385 +  assert( sqlite3_mutex_held(db->mutex) );
   1.386    assert( ppBtree != 0);
   1.387    if( omitJournal ){
   1.388 -    btree_flags |= BTREE_OMIT_JOURNAL;
   1.389 +    btFlags |= BTREE_OMIT_JOURNAL;
   1.390    }
   1.391    if( db->flags & SQLITE_NoReadlock ){
   1.392 -    btree_flags |= BTREE_NO_READLOCK;
   1.393 +    btFlags |= BTREE_NO_READLOCK;
   1.394    }
   1.395    if( zFilename==0 ){
   1.396  #if TEMP_STORE==0
   1.397 @@ -740,7 +761,10 @@
   1.398  #endif /* SQLITE_OMIT_MEMORYDB */
   1.399    }
   1.400  
   1.401 -  rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btree_flags);
   1.402 +  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
   1.403 +    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
   1.404 +  }
   1.405 +  rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
   1.406    if( rc==SQLITE_OK ){
   1.407      sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
   1.408      sqlite3BtreeSetCacheSize(*ppBtree, nCache);
   1.409 @@ -754,17 +778,19 @@
   1.410  */
   1.411  const char *sqlite3_errmsg(sqlite3 *db){
   1.412    const char *z;
   1.413 -  assert( !sqlite3MallocFailed() );
   1.414    if( !db ){
   1.415      return sqlite3ErrStr(SQLITE_NOMEM);
   1.416    }
   1.417    if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   1.418      return sqlite3ErrStr(SQLITE_MISUSE);
   1.419    }
   1.420 +  sqlite3_mutex_enter(db->mutex);
   1.421 +  assert( !db->mallocFailed );
   1.422    z = (char*)sqlite3_value_text(db->pErr);
   1.423    if( z==0 ){
   1.424      z = sqlite3ErrStr(db->errCode);
   1.425    }
   1.426 +  sqlite3_mutex_leave(db->mutex);
   1.427    return z;
   1.428  }
   1.429  
   1.430 @@ -794,13 +820,14 @@
   1.431    };
   1.432  
   1.433    const void *z;
   1.434 -  assert( !sqlite3MallocFailed() );
   1.435    if( !db ){
   1.436      return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   1.437    }
   1.438    if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   1.439      return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   1.440    }
   1.441 +  sqlite3_mutex_enter(db->mutex);
   1.442 +  assert( !db->mallocFailed );
   1.443    z = sqlite3_value_text16(db->pErr);
   1.444    if( z==0 ){
   1.445      sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
   1.446 @@ -808,6 +835,7 @@
   1.447      z = sqlite3_value_text16(db->pErr);
   1.448    }
   1.449    sqlite3ApiExit(0, 0);
   1.450 +  sqlite3_mutex_leave(db->mutex);
   1.451    return z;
   1.452  }
   1.453  #endif /* SQLITE_OMIT_UTF16 */
   1.454 @@ -817,7 +845,7 @@
   1.455  ** passed to this function, we assume a malloc() failed during sqlite3_open().
   1.456  */
   1.457  int sqlite3_errcode(sqlite3 *db){
   1.458 -  if( !db || sqlite3MallocFailed() ){
   1.459 +  if( !db || db->mallocFailed ){
   1.460      return SQLITE_NOMEM;
   1.461    }
   1.462    if( sqlite3SafetyCheck(db) ){
   1.463 @@ -844,6 +872,7 @@
   1.464    if( sqlite3SafetyCheck(db) ){
   1.465      return SQLITE_MISUSE;
   1.466    }
   1.467 +  assert( sqlite3_mutex_held(db->mutex) );
   1.468  
   1.469    /* If SQLITE_UTF16 is specified as the encoding type, transform this
   1.470    ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   1.471 @@ -912,21 +941,28 @@
   1.472  */
   1.473  static int openDatabase(
   1.474    const char *zFilename, /* Database filename UTF-8 encoded */
   1.475 -  sqlite3 **ppDb         /* OUT: Returned database handle */
   1.476 +  sqlite3 **ppDb,        /* OUT: Returned database handle */
   1.477 +  unsigned flags,        /* Operational flags */
   1.478 +  const char *zVfs       /* Name of the VFS to use */
   1.479  ){
   1.480    sqlite3 *db;
   1.481    int rc;
   1.482    CollSeq *pColl;
   1.483  
   1.484 -  assert( !sqlite3MallocFailed() );
   1.485 -
   1.486    /* Allocate the sqlite data structure */
   1.487 -  db = sqliteMalloc( sizeof(sqlite3) );
   1.488 +  db = sqlite3MallocZero( sizeof(sqlite3) );
   1.489    if( db==0 ) goto opendb_out;
   1.490 +  db->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
   1.491 +  if( db->mutex==0 ){
   1.492 +    sqlite3_free(db);
   1.493 +    db = 0;
   1.494 +    goto opendb_out;
   1.495 +  }
   1.496 +  sqlite3_mutex_enter(db->mutex);
   1.497    db->errMask = 0xff;
   1.498    db->priorNewRowid = 0;
   1.499 +  db->nDb = 2;
   1.500    db->magic = SQLITE_MAGIC_BUSY;
   1.501 -  db->nDb = 2;
   1.502    db->aDb = db->aDbStatic;
   1.503    db->autoCommit = 1;
   1.504    db->flags |= SQLITE_ShortColNames
   1.505 @@ -943,6 +979,14 @@
   1.506    sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0);
   1.507  #endif
   1.508  
   1.509 +  db->pVfs = sqlite3_vfs_find(zVfs);
   1.510 +  if( !db->pVfs ){
   1.511 +    rc = SQLITE_ERROR;
   1.512 +    db->magic = SQLITE_MAGIC_CLOSED;
   1.513 +    sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)"));
   1.514 +    goto opendb_out;
   1.515 +  }
   1.516 +
   1.517    /* Add the default collation sequence BINARY. BINARY works for both UTF-8
   1.518    ** and UTF-16, so add a version for each to avoid any unnecessary
   1.519    ** conversions. The only error that can occur here is a malloc() failure.
   1.520 @@ -952,7 +996,7 @@
   1.521        createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) ||
   1.522        (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 
   1.523    ){
   1.524 -    assert( sqlite3MallocFailed() );
   1.525 +    assert( db->mallocFailed );
   1.526      db->magic = SQLITE_MAGIC_CLOSED;
   1.527      goto opendb_out;
   1.528    }
   1.529 @@ -968,15 +1012,17 @@
   1.530    }
   1.531  
   1.532    /* Open the backend database driver */
   1.533 -  rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE,
   1.534 +  db->openFlags = flags;
   1.535 +  rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, 
   1.536 +                           flags | SQLITE_OPEN_MAIN_DB,
   1.537                             &db->aDb[0].pBt);
   1.538    if( rc!=SQLITE_OK ){
   1.539      sqlite3Error(db, rc, 0);
   1.540      db->magic = SQLITE_MAGIC_CLOSED;
   1.541      goto opendb_out;
   1.542    }
   1.543 -  db->aDb[0].pSchema = sqlite3SchemaGet(db->aDb[0].pBt);
   1.544 -  db->aDb[1].pSchema = sqlite3SchemaGet(0);
   1.545 +  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
   1.546 +  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
   1.547  
   1.548  
   1.549    /* The default safety_level for the main database is 'full'; for the temp
   1.550 @@ -990,7 +1036,7 @@
   1.551  #endif
   1.552  
   1.553    db->magic = SQLITE_MAGIC_OPEN;
   1.554 -  if( sqlite3MallocFailed() ){
   1.555 +  if( db->mallocFailed ){
   1.556      goto opendb_out;
   1.557    }
   1.558  
   1.559 @@ -1010,21 +1056,28 @@
   1.560    }
   1.561  
   1.562  #ifdef SQLITE_ENABLE_FTS1
   1.563 -  if( !sqlite3MallocFailed() ){
   1.564 +  if( !db->mallocFailed ){
   1.565      extern int sqlite3Fts1Init(sqlite3*);
   1.566      rc = sqlite3Fts1Init(db);
   1.567    }
   1.568  #endif
   1.569  
   1.570  #ifdef SQLITE_ENABLE_FTS2
   1.571 -  if( !sqlite3MallocFailed() && rc==SQLITE_OK ){
   1.572 +  if( !db->mallocFailed && rc==SQLITE_OK ){
   1.573      extern int sqlite3Fts2Init(sqlite3*);
   1.574      rc = sqlite3Fts2Init(db);
   1.575    }
   1.576  #endif
   1.577  
   1.578 +#ifdef SQLITE_ENABLE_FTS3
   1.579 +  if( !db->mallocFailed && rc==SQLITE_OK ){
   1.580 +    extern int sqlite3Fts3Init(sqlite3*);
   1.581 +    rc = sqlite3Fts3Init(db);
   1.582 +  }
   1.583 +#endif
   1.584 +
   1.585  #ifdef SQLITE_ENABLE_ICU
   1.586 -  if( !sqlite3MallocFailed() && rc==SQLITE_OK ){
   1.587 +  if( !db->mallocFailed && rc==SQLITE_OK ){
   1.588      extern int sqlite3IcuInit(sqlite3*);
   1.589      rc = sqlite3IcuInit(db);
   1.590    }
   1.591 @@ -1042,6 +1095,9 @@
   1.592  #endif
   1.593  
   1.594  opendb_out:
   1.595 +  if( db && db->mutex ){
   1.596 +    sqlite3_mutex_leave(db->mutex);
   1.597 +  }
   1.598    if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
   1.599      sqlite3_close(db);
   1.600      db = 0;
   1.601 @@ -1057,7 +1113,16 @@
   1.602    const char *zFilename, 
   1.603    sqlite3 **ppDb 
   1.604  ){
   1.605 -  return openDatabase(zFilename, ppDb);
   1.606 +  return openDatabase(zFilename, ppDb,
   1.607 +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
   1.608 +}
   1.609 +int sqlite3_open_v2(
   1.610 +  const char *filename,   /* Database filename (UTF-8) */
   1.611 +  sqlite3 **ppDb,         /* OUT: SQLite db handle */
   1.612 +  int flags,              /* Flags */
   1.613 +  const char *zVfs        /* Name of VFS module to use */
   1.614 +){
   1.615 +  return openDatabase(filename, ppDb, flags, zVfs);
   1.616  }
   1.617  
   1.618  #ifndef SQLITE_OMIT_UTF16
   1.619 @@ -1069,17 +1134,18 @@
   1.620    sqlite3 **ppDb
   1.621  ){
   1.622    char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
   1.623 -  int rc = SQLITE_OK;
   1.624    sqlite3_value *pVal;
   1.625 +  int rc = SQLITE_NOMEM;
   1.626  
   1.627    assert( zFilename );
   1.628    assert( ppDb );
   1.629    *ppDb = 0;
   1.630 -  pVal = sqlite3ValueNew();
   1.631 +  pVal = sqlite3ValueNew(0);
   1.632    sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   1.633    zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
   1.634    if( zFilename8 ){
   1.635 -    rc = openDatabase(zFilename8, ppDb);
   1.636 +    rc = openDatabase(zFilename8, ppDb,
   1.637 +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
   1.638      if( rc==SQLITE_OK && *ppDb ){
   1.639        rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
   1.640        if( rc!=SQLITE_OK ){
   1.641 @@ -1095,45 +1161,6 @@
   1.642  #endif /* SQLITE_OMIT_UTF16 */
   1.643  
   1.644  /*
   1.645 -** The following routine destroys a virtual machine that is created by
   1.646 -** the sqlite3_compile() routine. The integer returned is an SQLITE_
   1.647 -** success/failure code that describes the result of executing the virtual
   1.648 -** machine.
   1.649 -**
   1.650 -** This routine sets the error code and string returned by
   1.651 -** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
   1.652 -*/
   1.653 -int sqlite3_finalize(sqlite3_stmt *pStmt){
   1.654 -  int rc;
   1.655 -  if( pStmt==0 ){
   1.656 -    rc = SQLITE_OK;
   1.657 -  }else{
   1.658 -    rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
   1.659 -  }
   1.660 -  return rc;
   1.661 -}
   1.662 -
   1.663 -/*
   1.664 -** Terminate the current execution of an SQL statement and reset it
   1.665 -** back to its starting state so that it can be reused. A success code from
   1.666 -** the prior execution is returned.
   1.667 -**
   1.668 -** This routine sets the error code and string returned by
   1.669 -** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
   1.670 -*/
   1.671 -int sqlite3_reset(sqlite3_stmt *pStmt){
   1.672 -  int rc;
   1.673 -  if( pStmt==0 ){
   1.674 -    rc = SQLITE_OK;
   1.675 -  }else{
   1.676 -    rc = sqlite3VdbeReset((Vdbe*)pStmt);
   1.677 -    sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0);
   1.678 -    assert( (rc & (sqlite3_db_handle(pStmt)->errMask))==rc );
   1.679 -  }
   1.680 -  return rc;
   1.681 -}
   1.682 -
   1.683 -/*
   1.684  ** Register a new collation sequence with the database handle db.
   1.685  */
   1.686  int sqlite3_create_collation(
   1.687 @@ -1144,9 +1171,12 @@
   1.688    int(*xCompare)(void*,int,const void*,int,const void*)
   1.689  ){
   1.690    int rc;
   1.691 -  assert( !sqlite3MallocFailed() );
   1.692 +  sqlite3_mutex_enter(db->mutex);
   1.693 +  assert( !db->mallocFailed );
   1.694    rc = createCollation(db, zName, enc, pCtx, xCompare, 0);
   1.695 -  return sqlite3ApiExit(db, rc);
   1.696 +  rc = sqlite3ApiExit(db, rc);
   1.697 +  sqlite3_mutex_leave(db->mutex);
   1.698 +  return rc;
   1.699  }
   1.700  
   1.701  /*
   1.702 @@ -1161,9 +1191,12 @@
   1.703    void(*xDel)(void*)
   1.704  ){
   1.705    int rc;
   1.706 -  assert( !sqlite3MallocFailed() );
   1.707 +  sqlite3_mutex_enter(db->mutex);
   1.708 +  assert( !db->mallocFailed );
   1.709    rc = createCollation(db, zName, enc, pCtx, xCompare, xDel);
   1.710 -  return sqlite3ApiExit(db, rc);
   1.711 +  rc = sqlite3ApiExit(db, rc);
   1.712 +  sqlite3_mutex_leave(db->mutex);
   1.713 +  return rc;
   1.714  }
   1.715  
   1.716  #ifndef SQLITE_OMIT_UTF16
   1.717 @@ -1179,13 +1212,16 @@
   1.718  ){
   1.719    int rc = SQLITE_OK;
   1.720    char *zName8; 
   1.721 -  assert( !sqlite3MallocFailed() );
   1.722 -  zName8 = sqlite3Utf16to8(zName, -1);
   1.723 +  sqlite3_mutex_enter(db->mutex);
   1.724 +  assert( !db->mallocFailed );
   1.725 +  zName8 = sqlite3Utf16to8(db, zName, -1);
   1.726    if( zName8 ){
   1.727      rc = createCollation(db, zName8, enc, pCtx, xCompare, 0);
   1.728 -    sqliteFree(zName8);
   1.729 +    sqlite3_free(zName8);
   1.730    }
   1.731 -  return sqlite3ApiExit(db, rc);
   1.732 +  rc = sqlite3ApiExit(db, rc);
   1.733 +  sqlite3_mutex_leave(db->mutex);
   1.734 +  return rc;
   1.735  }
   1.736  #endif /* SQLITE_OMIT_UTF16 */
   1.737  
   1.738 @@ -1201,9 +1237,11 @@
   1.739    if( sqlite3SafetyCheck(db) ){
   1.740      return SQLITE_MISUSE;
   1.741    }
   1.742 +  sqlite3_mutex_enter(db->mutex);
   1.743    db->xCollNeeded = xCollNeeded;
   1.744    db->xCollNeeded16 = 0;
   1.745    db->pCollNeededArg = pCollNeededArg;
   1.746 +  sqlite3_mutex_leave(db->mutex);
   1.747    return SQLITE_OK;
   1.748  }
   1.749  
   1.750 @@ -1220,9 +1258,11 @@
   1.751    if( sqlite3SafetyCheck(db) ){
   1.752      return SQLITE_MISUSE;
   1.753    }
   1.754 +  sqlite3_mutex_enter(db->mutex);
   1.755    db->xCollNeeded = 0;
   1.756    db->xCollNeeded16 = xCollNeeded16;
   1.757    db->pCollNeededArg = pCollNeededArg;
   1.758 +  sqlite3_mutex_leave(db->mutex);
   1.759    return SQLITE_OK;
   1.760  }
   1.761  #endif /* SQLITE_OMIT_UTF16 */
   1.762 @@ -1260,45 +1300,14 @@
   1.763  }
   1.764  #endif
   1.765  
   1.766 -
   1.767 -#ifndef SQLITE_OMIT_SHARED_CACHE
   1.768 -/*
   1.769 -** Enable or disable the shared pager and schema features for the
   1.770 -** current thread.
   1.771 -**
   1.772 -** This routine should only be called when there are no open
   1.773 -** database connections.
   1.774 -*/
   1.775 -int sqlite3_enable_shared_cache(int enable){
   1.776 -  ThreadData *pTd = sqlite3ThreadData();
   1.777 -  if( pTd ){
   1.778 -    /* It is only legal to call sqlite3_enable_shared_cache() when there
   1.779 -    ** are no currently open b-trees that were opened by the calling thread.
   1.780 -    ** This condition is only easy to detect if the shared-cache were 
   1.781 -    ** previously enabled (and is being disabled). 
   1.782 -    */
   1.783 -    if( pTd->pBtree && !enable ){
   1.784 -      assert( pTd->useSharedData );
   1.785 -      return SQLITE_MISUSE;
   1.786 -    }
   1.787 -
   1.788 -    pTd->useSharedData = enable;
   1.789 -    sqlite3ReleaseThreadData();
   1.790 -  }
   1.791 -  return sqlite3ApiExit(0, SQLITE_OK);
   1.792 -}
   1.793 -#endif
   1.794 -
   1.795  /*
   1.796  ** This is a convenience routine that makes sure that all thread-specific
   1.797  ** data for this thread has been deallocated.
   1.798 +**
   1.799 +** SQLite no longer uses thread-specific data so this routine is now a
   1.800 +** no-op.  It is retained for historical compatibility.
   1.801  */
   1.802  void sqlite3_thread_cleanup(void){
   1.803 -  ThreadData *pTd = sqlite3OsThreadSpecificData(0);
   1.804 -  if( pTd ){
   1.805 -    memset(pTd, 0, sizeof(*pTd));
   1.806 -    sqlite3OsThreadSpecificData(-1);
   1.807 -  }
   1.808  }
   1.809  
   1.810  /*
   1.811 @@ -1333,6 +1342,7 @@
   1.812    if( sqlite3SafetyOn(db) ){
   1.813      return SQLITE_MISUSE;
   1.814    }
   1.815 +  sqlite3_mutex_enter(db->mutex);
   1.816    rc = sqlite3Init(db, &zErrMsg);
   1.817    if( SQLITE_OK!=rc ){
   1.818      goto error_out;
   1.819 @@ -1409,34 +1419,67 @@
   1.820      rc = SQLITE_ERROR;
   1.821    }
   1.822    sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
   1.823 -  sqliteFree(zErrMsg);
   1.824 -  return sqlite3ApiExit(db, rc);
   1.825 +  sqlite3_free(zErrMsg);
   1.826 +  rc = sqlite3ApiExit(db, rc);
   1.827 +  sqlite3_mutex_leave(db->mutex);
   1.828 +  return rc;
   1.829  }
   1.830  #endif
   1.831  
   1.832  /*
   1.833 -** Set all the parameters in the compiled SQL statement to NULL.
   1.834 -*/
   1.835 -int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
   1.836 -  int i;
   1.837 -  int rc = SQLITE_OK;
   1.838 -  for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
   1.839 -    rc = sqlite3_bind_null(pStmt, i);
   1.840 -  }
   1.841 -  return rc;
   1.842 -}
   1.843 -
   1.844 -/*
   1.845  ** Sleep for a little while.  Return the amount of time slept.
   1.846  */
   1.847  int sqlite3_sleep(int ms){
   1.848 -  return sqlite3OsSleep(ms);
   1.849 +  sqlite3_vfs *pVfs;
   1.850 +  int rc;
   1.851 +  pVfs = sqlite3_vfs_find(0);
   1.852 +
   1.853 +  /* This function works in milliseconds, but the underlying OsSleep() 
   1.854 +  ** API uses microseconds. Hence the 1000's.
   1.855 +  */
   1.856 +  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
   1.857 +  return rc;
   1.858  }
   1.859  
   1.860  /*
   1.861  ** Enable or disable the extended result codes.
   1.862  */
   1.863  int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
   1.864 +  sqlite3_mutex_enter(db->mutex);
   1.865    db->errMask = onoff ? 0xffffffff : 0xff;
   1.866 +  sqlite3_mutex_leave(db->mutex);
   1.867    return SQLITE_OK;
   1.868  }
   1.869 +
   1.870 +/*
   1.871 +** Invoke the xFileControl method on a particular database.
   1.872 +*/
   1.873 +int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
   1.874 +  int rc = SQLITE_ERROR;
   1.875 +  int iDb;
   1.876 +  sqlite3_mutex_enter(db->mutex);
   1.877 +  if( zDbName==0 ){
   1.878 +    iDb = 0;
   1.879 +  }else{
   1.880 +    for(iDb=0; iDb<db->nDb; iDb++){
   1.881 +      if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
   1.882 +    }
   1.883 +  }
   1.884 +  if( iDb<db->nDb ){
   1.885 +    Btree *pBtree = db->aDb[iDb].pBt;
   1.886 +    if( pBtree ){
   1.887 +      Pager *pPager;
   1.888 +      sqlite3BtreeEnter(pBtree);
   1.889 +      pPager = sqlite3BtreePager(pBtree);
   1.890 +      if( pPager ){
   1.891 +        sqlite3_file *fd = sqlite3PagerFile(pPager);
   1.892 +        if( fd ){
   1.893 +          rc = sqlite3OsFileControl(fd, op, pArg);
   1.894 +        }
   1.895 +      }
   1.896 +      sqlite3BtreeLeave(pBtree);
   1.897 +    }
   1.898 +  }
   1.899 +  sqlite3_mutex_leave(db->mutex);
   1.900 +  return rc;   
   1.901 +}