System.Data.SQLite
Check-in [a7f98646af]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Latest Jan 12 CVS HEAD
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sourceforge
Files: files | file ages | folders
SHA1: a7f98646af388f7701567f0a7e9c8c143dd1c024
User & Date: rmsimpson 2006-01-12 20:54:07
Context
2006-01-12
20:55
Optimize and fix the DataAdapter tests, shorten the locktest duration check-in: bb2c450c63 user: rmsimpson tags: sourceforge
20:54
Latest Jan 12 CVS HEAD check-in: a7f98646af user: rmsimpson tags: sourceforge
20:53
Remove unnecessary assignment check-in: 3407377976 user: rmsimpson tags: sourceforge
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/src/alter.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that used to generate VDBE code
    13     13   ** that implements the ALTER TABLE command.
    14     14   **
    15         -** $Id: alter.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           15  +** $Id: alter.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** The code in this file only exists if we are not omitting the
    22     22   ** ALTER TABLE logic from the build.
................................................................................
   263    263     char *zName = 0;          /* NULL-terminated version of pName */ 
   264    264     sqlite3 *db = pParse->db; /* Database connection */
   265    265     Vdbe *v;
   266    266   #ifndef SQLITE_OMIT_TRIGGER
   267    267     char *zWhere = 0;         /* Where clause to locate temp triggers */
   268    268   #endif
   269    269     
   270         -  if( sqlite3ThreadData()->mallocFailed ) goto exit_rename_table;
          270  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto exit_rename_table;
   271    271     assert( pSrc->nSrc==1 );
   272    272   
   273    273     pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   274    274     if( !pTab ) goto exit_rename_table;
   275    275     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   276    276     zDb = db->aDb[iDb].zName;
   277    277   
................................................................................
   497    497     Vdbe *v;
   498    498     int iDb;
   499    499     int i;
   500    500     int nAlloc;
   501    501   
   502    502     /* Look up the table being altered. */
   503    503     assert( pParse->pNewTable==0 );
   504         -  if( sqlite3ThreadData()->mallocFailed ) goto exit_begin_add_column;
          504  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto exit_begin_add_column;
   505    505     pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   506    506     if( !pTab ) goto exit_begin_add_column;
   507    507   
   508    508     /* Make sure this is not an attempt to ALTER a view. */
   509    509     if( pTab->pSelect ){
   510    510       sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
   511    511       goto exit_begin_add_column;

Changes to SQLite.Interop/src/analyze.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code associated with the ANALYZE command.
    13     13   **
    14         -** @(#) $Id: analyze.c,v 1.7 2006/01/11 03:22:29 rmsimpson Exp $
           14  +** @(#) $Id: analyze.c,v 1.8 2006/01/12 20:54:07 rmsimpson Exp $
    15     15   */
    16     16   #ifndef SQLITE_OMIT_ANALYZE
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** This routine generates code that opens the sqlite_stat1 table on cursor
    21     21   ** iStatCur.

Changes to SQLite.Interop/src/attach.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the ATTACH and DETACH commands.
    13     13   **
    14         -** $Id: attach.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           14  +** $Id: attach.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   /*
    19     19   ** Resolve an expression that was part of an ATTACH or DETACH statement. This
    20     20   ** is slightly different from resolving a normal SQL expression, because simple
    21     21   ** identifiers are treated as strings, not possible column names or aliases.
................................................................................
   167    167   
   168    168     /* If the file was opened successfully, read the schema for the new database.
   169    169     ** If this fails, or if opening the file failed, then close the file and 
   170    170     ** remove the entry from the db->aDb[] array. i.e. put everything back the way
   171    171     ** we found it.
   172    172     */
   173    173     if( rc==SQLITE_OK ){
   174         -    db->flags &= ~SQLITE_Initialized;
   175    174       sqlite3SafetyOn(db);
   176    175       rc = sqlite3Init(db, &zErrDyn);
   177    176       sqlite3SafetyOff(db);
   178    177     }
   179    178     if( rc ){
   180    179       int i = db->nDb - 1;
   181    180       assert( i>=2 );
................................................................................
   269    268     int rc;
   270    269     NameContext sName;
   271    270     Vdbe *v;
   272    271     FuncDef *pFunc;
   273    272     sqlite3* db = pParse->db;
   274    273   
   275    274   #ifndef SQLITE_OMIT_AUTHORIZATION
   276         -  assert( sqlite3ThreadData()->mallocFailed || pAuthArg );
          275  +  assert( sqlite3ThreadDataReadOnly()->mallocFailed || pAuthArg );
   277    276     if( pAuthArg ){
   278    277       char *zAuthArg = sqlite3NameFromToken(&pAuthArg->span);
   279    278       if( !zAuthArg ){
   280    279         goto attach_end;
   281    280       }
   282    281       rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
   283    282       sqliteFree(zAuthArg);
................................................................................
   300    299     }
   301    300   
   302    301     v = sqlite3GetVdbe(pParse);
   303    302     sqlite3ExprCode(pParse, pFilename);
   304    303     sqlite3ExprCode(pParse, pDbname);
   305    304     sqlite3ExprCode(pParse, pKey);
   306    305   
   307         -  assert(v || sqlite3ThreadData()->mallocFailed);
          306  +  assert(v || sqlite3ThreadDataReadOnly()->mallocFailed);
   308    307     if( v ){
   309    308       sqlite3VdbeAddOp(v, OP_Function, 0, nFunc);
   310    309       pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
   311    310       sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF);
   312    311   
   313    312       /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
   314    313       ** statement only). For DETACH, set it to false (expire all existing

Changes to SQLite.Interop/src/auth.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the sqlite3_set_authorizer()
    13     13   ** API.  This facility is an optional feature of the library.  Embedded
    14     14   ** systems that do not need this facility may omit it by recompiling
    15     15   ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
    16     16   **
    17         -** $Id: auth.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           17  +** $Id: auth.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** All of the code in this file may be omitted by defining a single
    23     23   ** macro.
    24     24   */

Changes to SQLite.Interop/src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.15 2006/01/11 03:22:29 rmsimpson Exp $
           12  +** $Id: btree.c,v 1.16 2006/01/12 20:54:07 rmsimpson Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** For a detailed discussion of BTrees, refer to
    16     16   **
    17     17   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    18     18   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    19     19   **     Publishing Company, Reading, Massachusetts.
................................................................................
   553    553   /*
   554    554   ** Save the positions of all cursors except pExcept open on the table 
   555    555   ** with root-page iRoot. Usually, this is called just before cursor
   556    556   ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
   557    557   */
   558    558   static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
   559    559     BtCursor *p;
   560         -  if( sqlite3ThreadData()->useSharedData ){
          560  +  if( sqlite3ThreadDataReadOnly()->useSharedData ){
   561    561       for(p=pBt->pCursor; p; p=p->pNext){
   562    562         if( p!=pExcept && p->pgnoRoot==iRoot && p->eState==CURSOR_VALID ){
   563    563           int rc = saveCursorPosition(p);
   564    564           if( SQLITE_OK!=rc ){
   565    565             return rc;
   566    566           }
   567    567         }
................................................................................
   580    580   ** If the second argument argument - doSeek - is false, then instead of 
   581    581   ** returning the cursor to it's saved position, any saved position is deleted
   582    582   ** and the cursor state set to CURSOR_INVALID.
   583    583   */
   584    584   static int restoreCursorPosition(BtCursor *pCur, int doSeek){
   585    585     int rc = SQLITE_OK;
   586    586     if( pCur->eState==CURSOR_REQUIRESEEK ){
   587         -    assert( sqlite3ThreadData()->useSharedData );
          587  +    assert( sqlite3ThreadDataReadOnly()->useSharedData );
   588    588       if( doSeek ){
   589    589         rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip);
   590    590       }else{
   591    591         pCur->eState = CURSOR_INVALID;
   592    592       }
   593    593       if( rc==SQLITE_OK ){
   594    594         sqliteFree(pCur->pKey);
................................................................................
   606    606   ** SQLITE_LOCKED if not.
   607    607   */
   608    608   static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
   609    609     BtShared *pBt = p->pBt;
   610    610     BtLock *pIter;
   611    611   
   612    612     /* This is a no-op if the shared-cache is not enabled */
   613         -  if( 0==sqlite3ThreadData()->useSharedData ){
          613  +  if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
   614    614       return SQLITE_OK;
   615    615     }
   616    616   
   617    617     /* This (along with lockTable()) is where the ReadUncommitted flag is
   618    618     ** dealt with. If the caller is querying for a read-lock and the flag is
   619    619     ** set, it is unconditionally granted - even if there are write-locks
   620    620     ** on the table. If a write-lock is requested, the ReadUncommitted flag
................................................................................
   654    654   */
   655    655   static int lockTable(Btree *p, Pgno iTable, u8 eLock){
   656    656     BtShared *pBt = p->pBt;
   657    657     BtLock *pLock = 0;
   658    658     BtLock *pIter;
   659    659   
   660    660     /* This is a no-op if the shared-cache is not enabled */
   661         -  if( 0==sqlite3ThreadData()->useSharedData ){
          661  +  if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
   662    662       return SQLITE_OK;
   663    663     }
   664    664   
   665    665     assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
   666    666   
   667    667     /* If the read-uncommitted flag is set and a read-lock is requested,
   668    668     ** return early without adding an entry to the BtShared.pLock list. See
................................................................................
   719    719   static void unlockAllTables(Btree *p){
   720    720     BtLock **ppIter = &p->pBt->pLock;
   721    721   
   722    722     /* If the shared-cache extension is not enabled, there should be no
   723    723     ** locks in the BtShared.pLock list, making this procedure a no-op. Assert
   724    724     ** that this is the case.
   725    725     */
   726         -  assert( sqlite3ThreadData()->useSharedData || 0==*ppIter );
          726  +  assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter );
   727    727   
   728    728     while( *ppIter ){
   729    729       BtLock *pLock = *ppIter;
   730    730       if( pLock->pBtree==p ){
   731    731         *ppIter = pLock->pNext;
   732    732         sqliteFree(pLock);
   733    733       }else{
................................................................................
  1542   1542     int flags               /* Options */
  1543   1543   ){
  1544   1544     BtShared *pBt;          /* Shared part of btree structure */
  1545   1545     Btree *p;               /* Handle to return */
  1546   1546     int rc;
  1547   1547     int nReserve;
  1548   1548     unsigned char zDbHeader[100];
  1549         -#ifndef SQLITE_OMIT_SHARED_CACHE
  1550         -  ThreadData *pTsd = sqlite3ThreadData();
         1549  +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
         1550  +  const ThreadData *pTsdro;
  1551   1551   #endif
  1552   1552   
  1553   1553     /* Set the variable isMemdb to true for an in-memory database, or 
  1554   1554     ** false for a file-based database. This symbol is only required if
  1555   1555     ** either of the shared-data or autovacuum features are compiled 
  1556   1556     ** into the library.
  1557   1557     */
................................................................................
  1568   1568       return SQLITE_NOMEM;
  1569   1569     }
  1570   1570     p->inTrans = TRANS_NONE;
  1571   1571     p->pSqlite = pSqlite;
  1572   1572   
  1573   1573     /* Try to find an existing Btree structure opened on zFilename. */
  1574   1574   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  1575         -  if( pTsd->useSharedData && zFilename && !isMemdb ){
         1575  +  pTsdro = sqlite3ThreadDataReadOnly();
         1576  +  if( pTsdro->useSharedData && zFilename && !isMemdb ){
  1576   1577       char *zFullPathname = sqlite3OsFullPathname(zFilename);
  1577   1578       if( !zFullPathname ){
  1578   1579         sqliteFree(p);
  1579   1580         return SQLITE_NOMEM;
  1580   1581       }
  1581         -    for(pBt=pTsd->pBtree; pBt; pBt=pBt->pNext){
         1582  +    for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){
         1583  +      assert( pBt->nRef>0 );
  1582   1584         if( 0==strcmp(zFullPathname, sqlite3pager_filename(pBt->pPager)) ){
  1583   1585           p->pBt = pBt;
  1584   1586           *ppBtree = p;
  1585   1587           pBt->nRef++;
  1586   1588           sqliteFree(zFullPathname);
  1587   1589           return SQLITE_OK;
  1588   1590         }
................................................................................
  1655   1657     }
  1656   1658     pBt->usableSize = pBt->pageSize - nReserve;
  1657   1659     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
  1658   1660     sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize);
  1659   1661   
  1660   1662   #ifndef SQLITE_OMIT_SHARED_CACHE
  1661   1663     /* Add the new btree to the linked list starting at ThreadData.pBtree */
  1662         -  if( pTsd->useSharedData && zFilename && !isMemdb ){
  1663         -    pBt->pNext = pTsd->pBtree;
  1664         -    pTsd->pBtree = pBt;
         1664  +  if( pTsdro->useSharedData && zFilename && !isMemdb ){
         1665  +    pBt->pNext = pTsdro->pBtree;
         1666  +    sqlite3ThreadData()->pBtree = pBt;
  1665   1667     }
  1666   1668   #endif
  1667   1669     pBt->nRef = 1;
  1668   1670     *ppBtree = p;
  1669   1671     return SQLITE_OK;
  1670   1672   }
  1671   1673   
................................................................................
  1673   1675   ** Close an open database and invalidate all cursors.
  1674   1676   */
  1675   1677   int sqlite3BtreeClose(Btree *p){
  1676   1678     BtShared *pBt = p->pBt;
  1677   1679     BtCursor *pCur;
  1678   1680   
  1679   1681   #ifndef SQLITE_OMIT_SHARED_CACHE
  1680         -  ThreadData *pTsd = sqlite3ThreadData();
         1682  +  ThreadData *pTsd;
  1681   1683   #endif
  1682   1684   
  1683   1685     /* Drop any table-locks */
  1684   1686     unlockAllTables(p);
  1685   1687   
  1686   1688     /* Close all cursors opened via this handle.  */
  1687   1689     pCur = pBt->pCursor;
................................................................................
  1703   1705     assert( pBt->nRef>0 );
  1704   1706     pBt->nRef--;
  1705   1707     if( pBt->nRef ){
  1706   1708       return SQLITE_OK;
  1707   1709     }
  1708   1710   
  1709   1711     /* Remove the shared-btree from the thread wide list */
         1712  +  pTsd = sqlite3ThreadData();
  1710   1713     if( pTsd->pBtree==pBt ){
  1711   1714       pTsd->pBtree = pBt->pNext;
  1712   1715     }else{
  1713   1716       BtShared *pPrev;
  1714   1717       for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext);
  1715   1718       if( pPrev ){
  1716   1719         pPrev->pNext = pBt->pNext;
................................................................................
  3319   3322         return SQLITE_CORRUPT_BKPT;
  3320   3323       }
  3321   3324       pageIntegrity(pPage);
  3322   3325       while( lwr<=upr ){
  3323   3326         void *pCellKey;
  3324   3327         i64 nCellKey;
  3325   3328         pCur->idx = (lwr+upr)/2;
  3326         -      pCur->info.nSize = 0;
  3327         -      sqlite3BtreeKeySize(pCur, &nCellKey);
  3328   3329         if( pPage->intKey ){
         3330  +        u8 *pCell = findCell(pPage, pCur->idx);
         3331  +        pCell += pPage->childPtrSize;
         3332  +        if( pPage->hasData ){
         3333  +          int dummy;
         3334  +          pCell += getVarint32(pCell, &dummy);
         3335  +        }
         3336  +        getVarint(pCell, &nCellKey);
         3337  +        pCur->info.nSize = 0;
  3329   3338           if( nCellKey<nKey ){
  3330   3339             c = -1;
  3331   3340           }else if( nCellKey>nKey ){
  3332   3341             c = +1;
  3333   3342           }else{
  3334   3343             c = 0;
  3335   3344           }
  3336   3345         }else{
  3337   3346           int available;
         3347  +        parseCell(pPage, pCur->idx, &pCur->info);
         3348  +        nCellKey = pCur->info.nKey;
  3338   3349           pCellKey = (void *)fetchPayload(pCur, &available, 0);
  3339   3350           if( available>=nCellKey ){
  3340   3351             c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
  3341   3352           }else{
  3342   3353             pCellKey = sqliteMallocRaw( nCellKey );
  3343   3354             if( pCellKey==0 ) return SQLITE_NOMEM;
  3344   3355             rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
................................................................................
  6494   6505     rc = queryTableLock(p, iTab, lockType);
  6495   6506     if( rc==SQLITE_OK ){
  6496   6507       rc = lockTable(p, iTab, lockType);
  6497   6508     }
  6498   6509   #endif
  6499   6510     return rc;
  6500   6511   }
         6512  +
         6513  +/*
         6514  +** The following debugging interface has to be in this file (rather
         6515  +** than in, for example, test1.c) so that it can get access to
         6516  +** the definition of BtShared.
         6517  +*/
         6518  +#if defined(SQLITE_TEST) && defined(TCLSH)
         6519  +#include <tcl.h>
         6520  +int sqlite3_shared_cache_report(
         6521  +  void * clientData,
         6522  +  Tcl_Interp *interp,
         6523  +  int objc,
         6524  +  Tcl_Obj *CONST objv[]
         6525  +){
         6526  +  const ThreadData *pTd = sqlite3ThreadDataReadOnly();
         6527  +  if( pTd->useSharedData ){
         6528  +    BtShared *pBt;
         6529  +    Tcl_Obj *pRet = Tcl_NewObj();
         6530  +    for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){
         6531  +      const char *zFile = sqlite3pager_filename(pBt->pPager);
         6532  +      Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
         6533  +      Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
         6534  +    }
         6535  +    Tcl_SetObjResult(interp, pRet);
         6536  +  }
         6537  +  return TCL_OK;
         6538  +}
         6539  +#endif

Changes to SQLite.Interop/src/btree.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite B-Tree file
    13     13   ** subsystem.  See comments in the source code for a detailed description
    14     14   ** of what each interface routine does.
    15     15   **
    16         -** @(#) $Id: btree.h,v 1.14 2006/01/11 03:22:29 rmsimpson Exp $
           16  +** @(#) $Id: btree.h,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
    17     17   */
    18     18   #ifndef _BTREE_H_
    19     19   #define _BTREE_H_
    20     20   
    21     21   /* TODO: This definition is just included so other modules compile. It
    22     22   ** needs to be revisited.
    23     23   */

Changes to SQLite.Interop/src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           25  +** $Id: build.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   #include <ctype.h>
    29     29   
    30     30   /*
    31     31   ** This routine is called when a new SQL statement is beginning to
    32     32   ** be parsed.  Initialize the pParse structure as needed.
................................................................................
    62     62     int iTab, 
    63     63     u8 isWriteLock,  
    64     64     const char *zName
    65     65   ){
    66     66     int i;
    67     67     int nBytes;
    68     68     TableLock *p;
    69         -  ThreadData *pTsd = sqlite3ThreadData();
    70     69   
    71         -  if( 0==pTsd->useSharedData || iDb<0 ){
           70  +  if( 0==sqlite3ThreadDataReadOnly()->useSharedData || iDb<0 ){
    72     71       return;
    73     72     }
    74     73   
    75     74     for(i=0; i<pParse->nTableLock; i++){
    76     75       p = &pParse->aTableLock[i];
    77     76       if( p->iDb==iDb && p->iTab==iTab ){
    78     77         p->isWriteLock = (p->isWriteLock || isWriteLock);
................................................................................
    94     93   /*
    95     94   ** Code an OP_TableLock instruction for each table locked by the
    96     95   ** statement (configured by calls to sqlite3TableLock()).
    97     96   */
    98     97   static void codeTableLocks(Parse *pParse){
    99     98     int i;
   100     99     Vdbe *pVdbe; 
   101         -  assert( sqlite3ThreadData()->useSharedData || pParse->nTableLock==0 );
          100  +  assert( sqlite3ThreadDataReadOnly()->useSharedData || pParse->nTableLock==0 );
   102    101   
   103    102     if( 0==(pVdbe = sqlite3GetVdbe(pParse)) ){
   104    103       return;
   105    104     }
   106    105   
   107    106     for(i=0; i<pParse->nTableLock; i++){
   108    107       TableLock *p = &pParse->aTableLock[i];
................................................................................
   127    126   ** Note that if an error occurred, it might be the case that
   128    127   ** no VDBE code was generated.
   129    128   */
   130    129   void sqlite3FinishCoding(Parse *pParse){
   131    130     sqlite3 *db;
   132    131     Vdbe *v;
   133    132   
   134         -  if( sqlite3ThreadData()->mallocFailed ) return;
          133  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) return;
   135    134     if( pParse->nested ) return;
   136    135     if( !pParse->pVdbe ){
   137    136       if( pParse->rc==SQLITE_OK && pParse->nErr ){
   138    137         pParse->rc = SQLITE_ERROR;
   139    138       }
   140    139       return;
   141    140     }
................................................................................
   251    250   **
   252    251   ** See also sqlite3LocateTable().
   253    252   */
   254    253   Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
   255    254     Table *p = 0;
   256    255     int i;
   257    256     assert( zName!=0 );
   258         -  assert( (db->flags & SQLITE_Initialized) || db->init.busy );
   259    257     for(i=OMIT_TEMPDB; i<db->nDb; i++){
   260    258       int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
   261    259       if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
   262    260       p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, strlen(zName)+1);
   263    261       if( p ) break;
   264    262     }
   265    263     return p;
................................................................................
   307    305   ** for duplicate index names is done.)  The search order is
   308    306   ** TEMP first, then MAIN, then any auxiliary databases added
   309    307   ** using the ATTACH command.
   310    308   */
   311    309   Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
   312    310     Index *p = 0;
   313    311     int i;
   314         -  assert( (db->flags & SQLITE_Initialized) || db->init.busy );
   315    312     for(i=OMIT_TEMPDB; i<db->nDb; i++){
   316    313       int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
   317    314       Schema *pSchema = db->aDb[j].pSchema;
   318    315       if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
   319    316       assert( pSchema || (j==1 && !db->aDb[1].pBt) );
   320    317       if( pSchema ){
   321    318         p = sqlite3HashFind(&pSchema->idxHash, zName, strlen(zName)+1);
................................................................................
   389    386   ** files.  If iDb>=2 then reset the internal schema for only the
   390    387   ** single file indicated.
   391    388   */
   392    389   void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
   393    390     int i, j;
   394    391   
   395    392     assert( iDb>=0 && iDb<db->nDb );
   396         -  db->flags &= ~SQLITE_Initialized;
   397    393     for(i=iDb; i<db->nDb; i++){
   398    394       Db *pDb = &db->aDb[i];
   399    395       if( pDb->pSchema ){
   400    396         sqlite3SchemaFree(pDb->pSchema);
   401    397       }
   402    398       if( iDb>0 ) return;
   403    399     }
................................................................................
   833    829     ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
   834    830     ** indices to be created and the table record must come before the 
   835    831     ** indices.  Hence, the record number for the table must be allocated
   836    832     ** now.
   837    833     */
   838    834     if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
   839    835       int lbl;
          836  +    int fileFormat;
   840    837       sqlite3BeginWriteOperation(pParse, 0, iDb);
   841    838   
   842    839       /* If the file format and encoding in the database have not been set, 
   843    840       ** set them now.
   844    841       */
   845    842       sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1);   /* file_format */
   846    843       lbl = sqlite3VdbeMakeLabel(v);
   847    844       sqlite3VdbeAddOp(v, OP_If, 0, lbl);
   848         -    sqlite3VdbeAddOp(v, OP_Integer, SQLITE_DEFAULT_FILE_FORMAT, 0);
          845  +    fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
          846  +                  1 : SQLITE_DEFAULT_FILE_FORMAT;
          847  +    sqlite3VdbeAddOp(v, OP_Integer, fileFormat, 0);
   849    848       sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
   850    849       sqlite3VdbeAddOp(v, OP_Integer, ENC(db), 0);
   851    850       sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
   852    851       sqlite3VdbeResolveLabel(v, lbl);
   853    852   
   854    853       /* This just creates a place-holder record in the sqlite_master table.
   855    854       ** The record created does not contain anything yet.  It will be replaced
................................................................................
  1362   1361     Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
  1363   1362   ){
  1364   1363     Table *p;
  1365   1364     sqlite3 *db = pParse->db;
  1366   1365     int iDb;
  1367   1366   
  1368   1367     if( (pEnd==0 && pSelect==0) || 
  1369         -      pParse->nErr || sqlite3ThreadData()->mallocFailed ) {
         1368  +      pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) {
  1370   1369       return;
  1371   1370     }
  1372   1371     p = pParse->pNewTable;
  1373   1372     if( p==0 ) return;
  1374   1373   
  1375   1374     assert( !db->init.busy || !pSelect );
  1376   1375   
................................................................................
  1599   1598     /* Make a copy of the entire SELECT statement that defines the view.
  1600   1599     ** This will force all the Expr.token.z values to be dynamically
  1601   1600     ** allocated rather than point to the input string - which means that
  1602   1601     ** they will persist after the current sqlite3_exec() call returns.
  1603   1602     */
  1604   1603     p->pSelect = sqlite3SelectDup(pSelect);
  1605   1604     sqlite3SelectDelete(pSelect);
  1606         -  if( sqlite3ThreadData()->mallocFailed ){
         1605  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
  1607   1606       return;
  1608   1607     }
  1609   1608     if( !pParse->db->init.busy ){
  1610   1609       sqlite3ViewGetColumnNames(pParse, p);
  1611   1610     }
  1612   1611   
  1613   1612     /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
................................................................................
  1841   1840   */
  1842   1841   void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
  1843   1842     Table *pTab;
  1844   1843     Vdbe *v;
  1845   1844     sqlite3 *db = pParse->db;
  1846   1845     int iDb;
  1847   1846   
  1848         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ) goto exit_drop_table;
         1847  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
         1848  +    goto exit_drop_table;
         1849  +  }
  1849   1850     assert( pName->nSrc==1 );
  1850   1851     pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
  1851   1852   
  1852   1853     if( pTab==0 ){
  1853   1854       if( noErr ){
  1854   1855         sqlite3ErrorClear(pParse);
  1855   1856       }
................................................................................
  2200   2201     int iDb;             /* Index of the database that is being written */
  2201   2202     Token *pName = 0;    /* Unqualified name of the index to create */
  2202   2203     struct ExprList_item *pListItem; /* For looping over pList */
  2203   2204     int nCol;
  2204   2205     int nExtra = 0;
  2205   2206     char *zExtra;
  2206   2207   
  2207         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ){
         2208  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
  2208   2209       goto exit_create_index;
  2209   2210     }
  2210   2211   
  2211   2212     /*
  2212   2213     ** Find the table that is to be indexed.  Return early if not found.
  2213   2214     */
  2214   2215     if( pTblName!=0 ){
................................................................................
  2354   2355         sizeof(int)*nCol +           /* Index.aiColumn   */
  2355   2356         sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
  2356   2357         sizeof(char *)*nCol +        /* Index.azColl     */
  2357   2358         sizeof(u8)*nCol +            /* Index.aSortOrder */
  2358   2359         nName + 1 +                  /* Index.zName      */
  2359   2360         nExtra                       /* Collation sequence names */
  2360   2361     );
  2361         -  if( sqlite3ThreadData()->mallocFailed ) goto exit_create_index;
         2362  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto exit_create_index;
  2362   2363     pIndex->aiColumn = (int *)(&pIndex[1]);
  2363   2364     pIndex->aiRowEst = (int *)(&pIndex->aiColumn[nCol]);
  2364   2365     pIndex->azColl = (char **)(&pIndex->aiRowEst[nCol+1]);
  2365   2366     pIndex->aSortOrder = (u8 *)(&pIndex->azColl[nCol]);
  2366   2367     pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
  2367   2368     zExtra = (char *)(&pIndex->zName[nName+1]);
  2368   2369     strcpy(pIndex->zName, zName);
................................................................................
  2641   2642   */
  2642   2643   void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
  2643   2644     Index *pIndex;
  2644   2645     Vdbe *v;
  2645   2646     sqlite3 *db = pParse->db;
  2646   2647     int iDb;
  2647   2648   
  2648         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ){
         2649  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
  2649   2650       goto exit_drop_index;
  2650   2651     }
  2651   2652     assert( pName->nSrc==1 );
  2652   2653     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  2653   2654       goto exit_drop_index;
  2654   2655     }
  2655   2656     pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
................................................................................
  2850   2851   
  2851   2852   /*
  2852   2853   ** Assign cursors to all tables in a SrcList
  2853   2854   */
  2854   2855   void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
  2855   2856     int i;
  2856   2857     struct SrcList_item *pItem;
  2857         -  assert(pList || sqlite3ThreadData()->mallocFailed);
         2858  +  assert(pList || sqlite3ThreadDataReadOnly()->mallocFailed);
  2858   2859     if( pList ){
  2859   2860       for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
  2860   2861         if( pItem->iCursor>=0 ) break;
  2861   2862         pItem->iCursor = pParse->nTab++;
  2862   2863         if( pItem->pSelect ){
  2863   2864           sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
  2864   2865         }
................................................................................
  2899   2900   */
  2900   2901   void sqlite3BeginTransaction(Parse *pParse, int type){
  2901   2902     sqlite3 *db;
  2902   2903     Vdbe *v;
  2903   2904     int i;
  2904   2905   
  2905   2906     if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
  2906         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ) return;
         2907  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
  2907   2908     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
  2908   2909   
  2909   2910     v = sqlite3GetVdbe(pParse);
  2910   2911     if( !v ) return;
  2911   2912     if( type!=TK_DEFERRED ){
  2912   2913       for(i=0; i<db->nDb; i++){
  2913   2914         sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
................................................................................
  2920   2921   ** Commit a transaction
  2921   2922   */
  2922   2923   void sqlite3CommitTransaction(Parse *pParse){
  2923   2924     sqlite3 *db;
  2924   2925     Vdbe *v;
  2925   2926   
  2926   2927     if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
  2927         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ) return;
         2928  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
  2928   2929     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
  2929   2930   
  2930   2931     v = sqlite3GetVdbe(pParse);
  2931   2932     if( v ){
  2932   2933       sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
  2933   2934     }
  2934   2935   }
................................................................................
  2937   2938   ** Rollback a transaction
  2938   2939   */
  2939   2940   void sqlite3RollbackTransaction(Parse *pParse){
  2940   2941     sqlite3 *db;
  2941   2942     Vdbe *v;
  2942   2943   
  2943   2944     if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
  2944         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ) return;
         2945  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
  2945   2946     if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
  2946   2947   
  2947   2948     v = sqlite3GetVdbe(pParse);
  2948   2949     if( v ){
  2949   2950       sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
  2950   2951     }
  2951   2952   }
................................................................................
  3211   3212   
  3212   3213     if( pParse->nErr ){
  3213   3214       sqliteFree(pKey);
  3214   3215       pKey = 0;
  3215   3216     }
  3216   3217     return pKey;
  3217   3218   }
  3218         -

Changes to SQLite.Interop/src/callback.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains functions used to access the internal hash tables
    14     14   ** of user defined functions and collation sequences.
    15     15   **
    16         -** $Id: callback.c,v 1.9 2006/01/11 03:22:29 rmsimpson Exp $
           16  +** $Id: callback.c,v 1.10 2006/01/12 20:54:07 rmsimpson Exp $
    17     17   */
    18     18   
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** Invoke the 'collation needed' callback to request a collation sequence
    23     23   ** in the database text encoding of name zName, length nName.
................................................................................
   174    174         pColl[0].zName[nName] = 0;
   175    175         pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
   176    176   
   177    177         /* If a malloc() failure occured in sqlite3HashInsert(), it will 
   178    178         ** return the pColl pointer to be deleted (because it wasn't added
   179    179         ** to the hash table).
   180    180         */
   181         -      assert( !pDel || (sqlite3ThreadData()->mallocFailed && pDel==pColl) );
          181  +      assert( !pDel ||
          182  +              (sqlite3ThreadDataReadOnly()->mallocFailed && pDel==pColl) );
   182    183         sqliteFree(pDel);
   183    184       }
   184    185     }
   185    186     return pColl;
   186    187   }
   187    188   
   188    189   /*

Changes to SQLite.Interop/src/complete.c.

    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that implements the sqlite3_complete() API.
    15     15   ** This code used to be part of the tokenizer.c source file.  But by
    16     16   ** separating it out, the code will be automatically omitted from
    17     17   ** static links that do not use it.
    18     18   **
    19         -** $Id: complete.c,v 1.7 2006/01/11 03:22:29 rmsimpson Exp $
           19  +** $Id: complete.c,v 1.8 2006/01/12 20:54:07 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #ifndef SQLITE_OMIT_COMPLETE
    23     23   
    24     24   /*
    25     25   ** This is defined in tokenize.c.  We just have to import the definition.
    26     26   */

Changes to SQLite.Interop/src/date.c.

    12     12   ** This file contains the C functions that implement date and time
    13     13   ** functions for SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: date.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           19  +** $Id: date.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    20     20   **
    21     21   ** NOTES:
    22     22   **
    23     23   ** SQLite processes all times and dates as Julian Day numbers.  The
    24     24   ** dates and times are stored as the number of days since noon
    25     25   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    26     26   ** calendar system.

Changes to SQLite.Interop/src/delete.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** in order to generate code for DELETE FROM statements.
    14     14   **
    15         -** $Id: delete.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           15  +** $Id: delete.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Look up every table that is named in pSrc.  If any table is not found,
    21     21   ** add an error message to pParse->zErrMsg and return NULL.  If all tables
    22     22   ** are found, return a pointer to the last table.
................................................................................
   104    104   
   105    105   #ifndef SQLITE_OMIT_TRIGGER
   106    106     int isView;                  /* True if attempting to delete from a view */
   107    107     int triggers_exist = 0;      /* True if any triggers exist */
   108    108   #endif
   109    109   
   110    110     sContext.pParse = 0;
   111         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ){
          111  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
   112    112       goto delete_from_cleanup;
   113    113     }
   114    114     db = pParse->db;
   115    115     assert( pTabList->nSrc==1 );
   116    116   
   117    117     /* Locate the table which we want to delete.  This table has to be
   118    118     ** put in an SrcList structure because some of the subroutines we

Changes to SQLite.Interop/src/experimental.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are not a part of the official
    13     13   ** SQLite API.  These routines are unsupported.
    14     14   **
    15         -** $Id: experimental.c,v 1.3 2006/01/11 03:22:30 rmsimpson Exp $
           15  +** $Id: experimental.c,v 1.4 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set all the parameters in the compiled SQL statement to NULL.
    21     21   */
    22     22   int sqlite3_clear_bindings(sqlite3_stmt *pStmt){

Changes to SQLite.Interop/src/expr.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.20 2006/01/11 03:22:29 rmsimpson Exp $
           15  +** $Id: expr.c,v 1.21 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** Return the 'affinity' of the expression pExpr if any.
    22     22   **
................................................................................
   259    259   /*
   260    260   ** Set the Expr.span field of the given expression to span all
   261    261   ** text between the two given tokens.
   262    262   */
   263    263   void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
   264    264     assert( pRight!=0 );
   265    265     assert( pLeft!=0 );
   266         -  if( !sqlite3ThreadData()->mallocFailed && pRight->z && pLeft->z ){
          266  +  if( !sqlite3ThreadDataReadOnly()->mallocFailed && pRight->z && pLeft->z ){
   267    267       assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
   268    268       if( pLeft->dyn==0 && pRight->dyn==0 ){
   269    269         pExpr->span.z = pLeft->z;
   270    270         pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
   271    271       }else{
   272    272         pExpr->span.z = 0;
   273    273       }
................................................................................
   354    354       if( i>=pParse->nVarExpr ){
   355    355         pExpr->iTable = ++pParse->nVar;
   356    356         if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
   357    357           pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
   358    358           sqlite3ReallocOrFree((void**)&pParse->apVarExpr,
   359    359                          pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
   360    360         }
   361         -      if( !sqlite3ThreadData()->mallocFailed ){
          361  +      if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
   362    362           assert( pParse->apVarExpr!=0 );
   363    363           pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
   364    364         }
   365    365       }
   366    366     } 
   367    367   }
   368    368   
................................................................................
   458    458       if( pOldExpr->span.z!=0 && pNewExpr ){
   459    459         /* Always make a copy of the span for top-level expressions in the
   460    460         ** expression list.  The logic in SELECT processing that determines
   461    461         ** the names of columns in the result set needs this information */
   462    462         sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
   463    463       }
   464    464       assert( pNewExpr==0 || pNewExpr->span.z!=0 
   465         -            || pOldExpr->span.z==0 || sqlite3ThreadData()->mallocFailed );
          465  +            || pOldExpr->span.z==0
          466  +            || sqlite3ThreadDataReadOnly()->mallocFailed );
   466    467       pItem->zName = sqliteStrDup(pOldItem->zName);
   467    468       pItem->sortOrder = pOldItem->sortOrder;
   468    469       pItem->isAgg = pOldItem->isAgg;
   469    470       pItem->done = 0;
   470    471     }
   471    472     return pNew;
   472    473   }
................................................................................
   827    828     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   828    829     NameContext *pTopNC = pNC;        /* First namecontext in the list */
   829    830   
   830    831     assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
   831    832     zDb = sqlite3NameFromToken(pDbToken);
   832    833     zTab = sqlite3NameFromToken(pTableToken);
   833    834     zCol = sqlite3NameFromToken(pColumnToken);
   834         -  if( sqlite3ThreadData()->mallocFailed ){
          835  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   835    836       goto lookupname_end;
   836    837     }
   837    838   
   838    839     pExpr->iTable = -1;
   839    840     while( pNC && cnt==0 ){
   840    841       ExprList *pEList;
   841    842       SrcList *pSrcList = pNC->pSrcList;
................................................................................
  1304   1305     ** If all of the above are false, then we can run this code just once
  1305   1306     ** save the results, and reuse the same result on subsequent invocations.
  1306   1307     */
  1307   1308     if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
  1308   1309       int mem = pParse->nMem++;
  1309   1310       sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
  1310   1311       testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
  1311         -    assert( testAddr>0 || sqlite3ThreadData()->mallocFailed );
         1312  +    assert( testAddr>0 || sqlite3ThreadDataReadOnly()->mallocFailed );
  1312   1313       sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
  1313   1314     }
  1314   1315   
  1315   1316     switch( pExpr->op ){
  1316   1317       case TK_IN: {
  1317   1318         char affinity;
  1318   1319         KeyInfo keyInfo;

Changes to SQLite.Interop/src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.14 2006/01/11 03:22:29 rmsimpson Exp $
           19  +** $Id: func.c,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <ctype.h>
    23     23   /* #include <math.h> */
    24     24   #include <stdlib.h>
    25     25   #include <assert.h>
    26     26   #include "vdbeInt.h"

Changes to SQLite.Interop/src/hash.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This is the implementation of generic hash-tables
    13     13   ** used in SQLite.
    14     14   **
    15         -** $Id: hash.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           15  +** $Id: hash.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <assert.h>
    19     19   
    20     20   /* Turn bulk memory into a hash table object by initializing the
    21     21   ** fields of the Hash structure.
    22     22   **

Changes to SQLite.Interop/src/hash.h.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This is the header file for the generic hash-table implemenation
    13     13   ** used in SQLite.
    14     14   **
    15         -** $Id: hash.h,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           15  +** $Id: hash.h,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #ifndef _SQLITE_HASH_H_
    18     18   #define _SQLITE_HASH_H_
    19     19   
    20     20   /* Forward declarations of structures. */
    21     21   typedef struct Hash Hash;
    22     22   typedef struct HashElem HashElem;

Changes to SQLite.Interop/src/insert.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle INSERT statements in SQLite.
    14     14   **
    15         -** $Id: insert.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           15  +** $Id: insert.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Set P3 of the most recently inserted opcode to a column affinity
    21     21   ** string for index pIdx. A column affinity string has one character
    22     22   ** for each column in the table, according to the affinity of the column:
................................................................................
   221    221     int triggers_exist = 0;     /* True if there are FOR EACH ROW triggers */
   222    222   #endif
   223    223   
   224    224   #ifndef SQLITE_OMIT_AUTOINCREMENT
   225    225     int counterRowid;     /* Memory cell holding rowid of autoinc counter */
   226    226   #endif
   227    227   
   228         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ) goto insert_cleanup;
          228  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
          229  +    goto insert_cleanup;
          230  +  }
   229    231     db = pParse->db;
   230    232   
   231    233     /* Locate the table into which we will be inserting new information.
   232    234     */
   233    235     assert( pTabList->nSrc==1 );
   234    236     zTab = pTabList->a[0].zName;
   235    237     if( zTab==0 ) goto insert_cleanup;
................................................................................
   327    329       int rc, iInitCode;
   328    330       iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
   329    331       iSelectLoop = sqlite3VdbeCurrentAddr(v);
   330    332       iInsertBlock = sqlite3VdbeMakeLabel(v);
   331    333   
   332    334       /* Resolve the expressions in the SELECT statement and execute it. */
   333    335       rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
   334         -    if( rc || pParse->nErr || sqlite3ThreadData()->mallocFailed ) goto insert_cleanup;
          336  +    if( rc || pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
          337  +      goto insert_cleanup;
          338  +    }
   335    339   
   336    340       iCleanup = sqlite3VdbeMakeLabel(v);
   337    341       sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
   338    342       assert( pSelect->pEList );
   339    343       nColumn = pSelect->pEList->nExpr;
   340    344   
   341    345       /* Set useTempTable to TRUE if the result of the SELECT statement

Changes to SQLite.Interop/src/keywordhash.h.

     8      8       "CASECASTCOLLATECOLUMNCOMMITCONFLICTCONSTRAINTERSECTCREATECROSS"
     9      9       "CURRENT_DATECURRENT_TIMESTAMPLANDESCDETACHDISTINCTDROPRAGMATCH"
    10     10       "FAILIMITFROMFULLGROUPDATEIFIMMEDIATEINSERTINSTEADINTOFFSETISNULL"
    11     11       "JOINORDEREPLACEOUTERESTRICTPRIMARYQUERYRIGHTROLLBACKROWHENUNION"
    12     12       "UNIQUEUSINGVACUUMVALUESVIEWHERE";
    13     13     static const unsigned char aHash[127] = {
    14     14         92,  80, 107,  91,   0,   4,   0,   0, 114,   0,  83,   0,   0,
    15         -      95,  44,  76,  93,   0, 106, 109,  97,  90,   0,  10,   0,   0,
           15  +      96,  44,  76,  93,   0, 106, 109,  97,  90,   0,  10,   0,   0,
    16     16        113,   0, 110, 103,   0,  28,  48,   0,  41,   0,   0,  65,  71,
    17         -       0,  63,  19,   0, 105,  36, 104,   0, 108,  74,   0,   0,  33,
           17  +       0,  63,  19,   0, 105,  36, 104,   0, 108,  75,   0,   0,  33,
    18     18          0,  61,  37,   0,   8,   0, 115,  38,  12,   0,  77,  40,  25,
    19     19         66,   0,   0,  31,  81,  53,  30,  50,  20,  88,   0,  34,   0,
    20         -      75,  26,   0,  72,   0,   0,   0,  64,  47,  67,  22,  87,  29,
           20  +      74,  26,   0,  72,   0,   0,   0,  64,  47,  67,  22,  87,  29,
    21     21         69,  86,   0,   1,   0,   9, 101,  58,  18,   0, 112,  82,  99,
    22         -      54,   6,  85,   0,   0,  49,  94,   0, 102,   0,  70,   0,   0,
    23         -      15,   0, 116,  51,  56,   0,   2,  55,   0, 111,
           22  +      55,   6,  85,   0,   0,  49,  94,   0, 102,   0,  70,   0,   0,
           23  +      15,   0, 116,  51,  56,   0,   2,  54,   0, 111,
    24     24     };
    25     25     static const unsigned char aNext[116] = {
    26     26          0,   0,   0,   0,   0,   3,   0,   0,   0,   0,   0,   0,   0,
    27     27          0,   0,   0,   0,   0,   0,   0,   0,  17,   0,   0,   0,   0,
    28     28          0,  11,   0,   0,   0,   0,   5,  13,   0,   7,   0,   0,   0,
    29         -       0,   0,   0,   0,   0,   0,   0,  43,   0,   0,   0,   0,   0,
    30         -       0,   0,  16,   0,  23,  52,   0,   0,   0,   0,  45,   0,  59,
    31         -       0,   0,   0,   0,   0,   0,   0,   0,  73,  42,   0,  24,  60,
           29  +       0,   0,   0,   0,   0,   0,   0,  42,   0,   0,   0,   0,   0,
           30  +       0,  16,   0,   0,  23,  52,   0,   0,   0,   0,  45,   0,  59,
           31  +       0,   0,   0,   0,   0,   0,   0,   0,  43,  73,   0,  24,  60,
    32     32         21,   0,  79,   0,   0,  68,   0,   0,  84,  46,   0,   0,   0,
    33         -       0,   0,   0,   0,   0,  39,  96,  98,   0,   0, 100,   0,  32,
           33  +       0,   0,   0,   0,   0,  39,  95,  98,   0,   0, 100,   0,  32,
    34     34          0,  14,  27,  78,   0,  57,  89,   0,  35,   0,  62,   0,
    35     35     };
    36     36     static const unsigned char aLen[116] = {
    37     37          5,   5,   4,   4,   9,   2,   3,   8,   2,   6,   4,   3,   7,
    38     38         11,   2,   7,   5,   5,   4,   5,   3,   5,  10,   6,   4,   6,
    39     39          7,   6,   7,   9,   3,   7,   9,   6,   9,   3,  10,   6,   6,
    40         -       4,   6,   3,   7,   6,   7,   5,  13,   2,   2,   5,   5,   6,
    41         -       7,   3,   7,   4,   4,   2,   7,   3,   8,   6,   4,   4,   7,
    42         -       6,   6,   8,  10,   9,   6,   5,  12,  12,  17,   4,   4,   6,
           40  +       4,   6,   7,   3,   6,   7,   5,  13,   2,   2,   5,   5,   6,
           41  +       7,   7,   3,   4,   4,   2,   7,   3,   8,   6,   4,   4,   7,
           42  +       6,   6,   8,  10,   9,   6,   5,  12,  17,  12,   4,   4,   6,
    43     43          8,   2,   4,   6,   5,   4,   5,   4,   4,   5,   6,   2,   9,
    44         -       6,   7,   4,   2,   6,   3,   6,   4,   5,   7,   5,   8,   7,
           44  +       6,   7,   4,   6,   2,   3,   6,   4,   5,   7,   5,   8,   7,
    45     45          5,   5,   8,   3,   4,   5,   6,   5,   6,   6,   4,   5,
    46     46     };
    47     47     static const unsigned short int aOffset[116] = {
    48     48          0,   4,   7,  10,  10,  14,  19,  21,  26,  27,  32,  34,  36,
    49     49         42,  51,  52,  57,  61,  65,  67,  71,  74,  78,  86,  91,  94,
    50     50         99, 105, 108, 113, 118, 122, 128, 136, 141, 150, 152, 162, 167,
    51     51        172, 175, 177, 177, 181, 185, 187, 192, 194, 196, 205, 208, 212,
................................................................................
    60     60       TK_OR,         TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     
    61     61       TK_THEN,       TK_END,        TK_DEFAULT,    TK_TRANSACTION,TK_ON,         
    62     62       TK_JOIN_KW,    TK_ALTER,      TK_RAISE,      TK_EACH,       TK_CHECK,      
    63     63       TK_KEY,        TK_AFTER,      TK_REFERENCES, TK_ESCAPE,     TK_ELSE,       
    64     64       TK_EXCEPT,     TK_TRIGGER,    TK_LIKE_KW,    TK_EXPLAIN,    TK_INITIALLY,  
    65     65       TK_ALL,        TK_ANALYZE,    TK_EXCLUSIVE,  TK_EXISTS,     TK_STATEMENT,  
    66     66       TK_AND,        TK_DEFERRABLE, TK_ATTACH,     TK_HAVING,     TK_LIKE_KW,    
    67         -    TK_BEFORE,     TK_FOR,        TK_FOREIGN,    TK_IGNORE,     TK_REINDEX,    
           67  +    TK_BEFORE,     TK_FOREIGN,    TK_FOR,        TK_IGNORE,     TK_REINDEX,    
    68     68       TK_INDEX,      TK_AUTOINCR,   TK_TO,         TK_IN,         TK_BEGIN,      
    69         -    TK_JOIN_KW,    TK_RENAME,     TK_BETWEEN,    TK_NOT,        TK_NOTNULL,    
           69  +    TK_JOIN_KW,    TK_RENAME,     TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        
    70     70       TK_NULL,       TK_LIKE_KW,    TK_BY,         TK_CASCADE,    TK_ASC,        
    71     71       TK_DEFERRED,   TK_DELETE,     TK_CASE,       TK_CAST,       TK_COLLATE,    
    72     72       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_CONSTRAINT, TK_INTERSECT,  
    73     73       TK_CREATE,     TK_JOIN_KW,    TK_CTIME_KW,   TK_CTIME_KW,   TK_CTIME_KW,   
    74     74       TK_PLAN,       TK_DESC,       TK_DETACH,     TK_DISTINCT,   TK_IS,         
    75     75       TK_DROP,       TK_PRAGMA,     TK_MATCH,      TK_FAIL,       TK_LIMIT,      
    76     76       TK_FROM,       TK_JOIN_KW,    TK_GROUP,      TK_UPDATE,     TK_IF,         
    77         -    TK_IMMEDIATE,  TK_INSERT,     TK_INSTEAD,    TK_INTO,       TK_OF,         
    78         -    TK_OFFSET,     TK_SET,        TK_ISNULL,     TK_JOIN,       TK_ORDER,      
           77  +    TK_IMMEDIATE,  TK_INSERT,     TK_INSTEAD,    TK_INTO,       TK_OFFSET,     
           78  +    TK_OF,         TK_SET,        TK_ISNULL,     TK_JOIN,       TK_ORDER,      
    79     79       TK_REPLACE,    TK_JOIN_KW,    TK_RESTRICT,   TK_PRIMARY,    TK_QUERY,      
    80     80       TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        TK_WHEN,       TK_UNION,      
    81     81       TK_UNIQUE,     TK_USING,      TK_VACUUM,     TK_VALUES,     TK_VIEW,       
    82     82       TK_WHERE,      
    83     83     };
    84     84     int h, i;
    85     85     if( n<2 ) return TK_ID;

Changes to SQLite.Interop/src/legacy.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: legacy.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           17  +** $Id: legacy.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    18     18   */
    19     19   
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   #include <ctype.h>
    23     23   
    24     24   /*
................................................................................
   117    117       azCols = 0;
   118    118     }
   119    119   
   120    120   exec_out:
   121    121     if( pStmt ) sqlite3_finalize(pStmt);
   122    122     if( azCols ) sqliteFree(azCols);
   123    123   
   124         -  if( sqlite3ThreadData()->mallocFailed ){
          124  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   125    125       rc = SQLITE_NOMEM;
   126    126       sqlite3MallocClearFailed();
   127    127     }
   128    128   
   129    129     if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
   130    130       *pzErrMsg = malloc(1+strlen(sqlite3_errmsg(db)));
   131    131       if( *pzErrMsg ){

Changes to SQLite.Interop/src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           17  +** $Id: main.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** The following constant value is used by the SQLITE_BIGENDIAN and
................................................................................
   172    172     /* The temp-database schema is allocated differently from the other schema
   173    173     ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
   174    174     ** So it needs to be freed here. Todo: Why not roll the temp schema into
   175    175     ** the same sqliteMalloc() as the one that allocates the database 
   176    176     ** structure?
   177    177     */
   178    178     sqliteFree(db->aDb[1].pSchema);
   179         -
   180    179     sqliteFree(db);
   181    180     sqlite3MallocAllow();
          181  +  sqlite3ReleaseThreadData();
   182    182     return SQLITE_OK;
   183    183   }
   184    184   
   185    185   /*
   186    186   ** Rollback all database files.
   187    187   */
   188    188   void sqlite3RollbackAll(sqlite3 *db){
................................................................................
   641    641   
   642    642   /*
   643    643   ** Return UTF-8 encoded English language explanation of the most recent
   644    644   ** error.
   645    645   */
   646    646   const char *sqlite3_errmsg(sqlite3 *db){
   647    647     const char *z;
   648         -  if( sqlite3ThreadData()->mallocFailed ){
          648  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   649    649       return sqlite3ErrStr(SQLITE_NOMEM);
   650    650     }
   651    651     if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   652    652       return sqlite3ErrStr(SQLITE_MISUSE);
   653    653     }
   654    654     z = (char*)sqlite3_value_text(db->pErr);
   655    655     if( z==0 ){
................................................................................
   680    680       0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', 
   681    681       0, 'o', 0, 'u', 0, 't', 0, ' ', 
   682    682       0, 'o', 0, 'f', 0, ' ', 
   683    683       0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
   684    684     };
   685    685   
   686    686     const void *z;
   687         -  if( sqlite3ThreadData()->mallocFailed ){
          687  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   688    688       return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   689    689     }
   690    690     if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   691    691       return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   692    692     }
   693    693     z = sqlite3_value_text16(db->pErr);
   694    694     if( z==0 ){
................................................................................
   701    701   #endif /* SQLITE_OMIT_UTF16 */
   702    702   
   703    703   /*
   704    704   ** Return the most recent error code generated by an SQLite routine. If NULL is
   705    705   ** passed to this function, we assume a malloc() failed during sqlite3_open().
   706    706   */
   707    707   int sqlite3_errcode(sqlite3 *db){
   708         -  if( !db || sqlite3ThreadData()->mallocFailed ){
          708  +  if( !db || sqlite3ThreadDataReadOnly()->mallocFailed ){
   709    709       return SQLITE_NOMEM;
   710    710     }
   711    711     if( sqlite3SafetyCheck(db) ){
   712    712       return SQLITE_MISUSE;
   713    713     }
   714    714     return db->errCode;
   715    715   }
................................................................................
   723    723     const char *zFilename, /* Database filename UTF-8 encoded */
   724    724     sqlite3 **ppDb         /* OUT: Returned database handle */
   725    725   ){
   726    726     sqlite3 *db;
   727    727     int rc;
   728    728     CollSeq *pColl;
   729    729   
   730         -  assert( !sqlite3ThreadData()->mallocFailed );
          730  +  assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
   731    731   
   732    732     /* Allocate the sqlite data structure */
   733    733     db = sqliteMalloc( sizeof(sqlite3) );
   734    734     if( db==0 ) goto opendb_out;
   735    735     db->priorNewRowid = 0;
   736    736     db->magic = SQLITE_MAGIC_BUSY;
   737    737     db->nDb = 2;
................................................................................
   850    850     zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
   851    851     if( zFilename8 ){
   852    852       rc = openDatabase(zFilename8, ppDb);
   853    853       if( rc==SQLITE_OK && *ppDb ){
   854    854         rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
   855    855       }
   856    856     }else{
   857         -    assert( sqlite3ThreadData()->mallocFailed );
          857  +    assert( sqlite3ThreadDataReadOnly()->mallocFailed );
   858    858       sqlite3MallocClearFailed();
   859    859     }
   860    860     sqlite3ValueFree(pVal);
   861    861   
   862    862     return rc;
   863    863   }
   864    864   #endif /* SQLITE_OMIT_UTF16 */
................................................................................
  1073   1073     */
  1074   1074     if( pTd->pBtree && !enable ){
  1075   1075       assert( pTd->useSharedData );
  1076   1076       return SQLITE_MISUSE;
  1077   1077     }
  1078   1078   
  1079   1079     pTd->useSharedData = enable;
         1080  +  sqlite3ReleaseThreadData();
  1080   1081     return SQLITE_OK;
  1081   1082   }
  1082   1083   #endif
         1084  +
         1085  +/*
         1086  +** This is a convenience routine that makes sure that all thread-specific
         1087  +** data for this thread has been deallocated.
         1088  +*/
         1089  +void sqlite3_thread_cleanup(void){
         1090  +  ThreadData *pTd = sqlite3ThreadData();
         1091  +  memset(pTd, 0, sizeof(*pTd));
         1092  +  sqlite3ReleaseThreadData();
         1093  +}

Changes to SQLite.Interop/src/opcodes.c.

     1      1   /* Automatically generated.  Do not edit */
     2      2   /* See the mkopcodec.awk script for details. */
     3      3   #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
     4      4   const char *const sqlite3OpcodeNames[] = { "?",
     5      5    /*   1 */ "MemLoad",
            6  + /* 125 */ "HexBlob",
     6      7    /*   2 */ "Column",
     7      8    /*   3 */ "SetCookie",
     8      9    /*   4 */ "IfMemPos",
           10  + /* 124 */ "Real",
     9     11    /*   5 */ "Sequence",
    10     12    /*   6 */ "MoveGt",
           13  + /*  71 */ "Ge",
    11     14    /*   7 */ "RowKey",
           15  + /*  67 */ "Eq",
    12     16    /*   8 */ "OpenWrite",
           17  + /*  65 */ "NotNull",
    13     18    /*   9 */ "If",
           19  + /* 140 */ "ToInt",
           20  + /*  86 */ "String8",
    14     21    /*  10 */ "Pop",
    15     22    /*  11 */ "CollSeq",
    16     23    /*  12 */ "OpenRead",
    17     24    /*  13 */ "Expire",
    18     25    /*  14 */ "AutoCommit",
           26  + /*  68 */ "Gt",
    19     27    /*  15 */ "IntegrityCk",
    20         - /*  16 */ "Not",
    21     28    /*  17 */ "Sort",
    22     29    /*  18 */ "Function",
           30  + /*  60 */ "And",
           31  + /*  78 */ "Subtract",
    23     32    /*  19 */ "Noop",
    24     33    /*  20 */ "Return",
           34  + /*  81 */ "Remainder",
    25     35    /*  21 */ "NewRowid",
           36  + /*  79 */ "Multiply",
    26     37    /*  22 */ "IfMemNeg",
    27     38    /*  23 */ "Variable",
    28     39    /*  24 */ "String",
    29     40    /*  25 */ "RealAffinity",
    30     41    /*  26 */ "ParseSchema",
    31     42    /*  27 */ "Close",
    32     43    /*  28 */ "CreateIndex",
................................................................................
    39     50    /*  35 */ "Rowid",
    40     51    /*  36 */ "IdxLT",
    41     52    /*  37 */ "AddImm",
    42     53    /*  38 */ "Statement",
    43     54    /*  39 */ "RowData",
    44     55    /*  40 */ "MemMax",
    45     56    /*  41 */ "Push",
           57  + /*  59 */ "Or",
    46     58    /*  42 */ "NotExists",
    47     59    /*  43 */ "MemIncr",
    48     60    /*  44 */ "Gosub",
           61  + /*  80 */ "Divide",
    49     62    /*  45 */ "Integer",
           63  + /* 139 */ "ToNumeric",
    50     64    /*  46 */ "MemInt",
    51     65    /*  47 */ "Prev",
           66  + /*  82 */ "Concat",
           67  + /*  73 */ "BitAnd",
    52     68    /*  48 */ "CreateTable",
    53     69    /*  49 */ "Last",
           70  + /*  64 */ "IsNull",
    54     71    /*  50 */ "IdxRowid",
    55     72    /*  51 */ "MakeIdxRec",
           73  + /*  76 */ "ShiftRight",
    56     74    /*  52 */ "ResetCount",
    57     75    /*  53 */ "FifoWrite",
    58     76    /*  54 */ "Callback",
    59     77    /*  55 */ "ContextPush",
    60     78    /*  56 */ "DropTrigger",
    61     79    /*  57 */ "DropIndex",
    62     80    /*  58 */ "IdxGE",
    63         - /*  59 */ "Or",
    64         - /*  60 */ "And",
    65     81    /*  61 */ "IdxDelete",
    66     82    /*  62 */ "Vacuum",
    67     83    /*  63 */ "MoveLe",
    68         - /*  64 */ "IsNull",
    69         - /*  65 */ "NotNull",
    70         - /*  66 */ "Ne",
    71         - /*  67 */ "Eq",
    72         - /*  68 */ "Gt",
    73         - /*  69 */ "Le",
    74         - /*  70 */ "Lt",
    75         - /*  71 */ "Ge",
    76     84    /*  72 */ "IfNot",
    77         - /*  73 */ "BitAnd",
    78         - /*  74 */ "BitOr",
    79         - /*  75 */ "ShiftLeft",
    80         - /*  76 */ "ShiftRight",
    81         - /*  77 */ "Add",
    82         - /*  78 */ "Subtract",
    83         - /*  79 */ "Multiply",
    84         - /*  80 */ "Divide",
    85         - /*  81 */ "Remainder",
    86         - /*  82 */ "Concat",
    87         - /*  83 */ "Negative",
    88     85    /*  84 */ "DropTable",
    89         - /*  85 */ "BitNot",
    90         - /*  86 */ "String8",
    91     86    /*  87 */ "MakeRecord",
           87  + /* 138 */ "ToBlob",
    92     88    /*  88 */ "Delete",
    93     89    /*  89 */ "AggFinal",
           90  + /*  75 */ "ShiftLeft",
    94     91    /*  90 */ "Dup",
    95     92    /*  91 */ "Goto",
    96     93    /*  92 */ "TableLock",
    97     94    /*  93 */ "FifoRead",
    98     95    /*  94 */ "Clear",
    99     96    /*  95 */ "IdxGT",
   100     97    /*  96 */ "MoveLt",
           98  + /*  69 */ "Le",
   101     99    /*  97 */ "VerifyCookie",
   102    100    /*  98 */ "AggStep",
   103    101    /*  99 */ "Pull",
          102  + /* 137 */ "ToText",
          103  + /*  16 */ "Not",
          104  + /* 141 */ "ToReal",
   104    105    /* 100 */ "SetNumColumns",
   105    106    /* 101 */ "AbsValue",
   106    107    /* 102 */ "Transaction",
          108  + /*  83 */ "Negative",
          109  + /*  66 */ "Ne",
   107    110    /* 103 */ "ContextPop",
          111  + /*  74 */ "BitOr",
   108    112    /* 104 */ "Next",
   109    113    /* 105 */ "IdxInsert",
   110    114    /* 106 */ "Distinct",
          115  + /*  70 */ "Lt",
   111    116    /* 107 */ "Insert",
   112    117    /* 108 */ "Destroy",
   113    118    /* 109 */ "ReadCookie",
   114    119    /* 110 */ "ForceInt",
   115    120    /* 111 */ "LoadAnalysis",
   116    121    /* 112 */ "OpenVirtual",
   117    122    /* 113 */ "Explain",
   118    123    /* 114 */ "IfMemZero",
   119    124    /* 115 */ "OpenPseudo",
   120    125    /* 116 */ "Null",
   121    126    /* 117 */ "Blob",
          127  + /*  77 */ "Add",
   122    128    /* 118 */ "MemStore",
   123    129    /* 119 */ "Rewind",
   124    130    /* 120 */ "MoveGe",
          131  + /*  85 */ "BitNot",
   125    132    /* 121 */ "MemMove",
   126    133    /* 122 */ "MemNull",
   127    134    /* 123 */ "Found",
   128         - /* 124 */ "Real",
   129         - /* 125 */ "HexBlob",
   130    135    /* 126 */ "NullRow",
   131    136    /* 127 */ "NotUsed_127",
   132    137    /* 128 */ "NotUsed_128",
   133    138    /* 129 */ "NotUsed_129",
   134    139    /* 130 */ "NotUsed_130",
   135    140    /* 131 */ "NotUsed_131",
   136    141    /* 132 */ "NotUsed_132",
   137    142    /* 133 */ "NotUsed_133",
   138    143    /* 134 */ "NotUsed_134",
   139    144    /* 135 */ "NotUsed_135",
   140    145    /* 136 */ "NotUsed_136",
   141         - /* 137 */ "ToText",
   142         - /* 138 */ "ToBlob",
   143         - /* 139 */ "ToNumeric",
   144         - /* 140 */ "ToInt",
   145         - /* 141 */ "ToReal",
   146    146   };
   147    147   #endif

Changes to SQLite.Interop/src/os.h.

   288    288   int sqlite3OsTempFileName(char*);
   289    289   int sqlite3OsRandomSeed(char*);
   290    290   int sqlite3OsSleep(int ms);
   291    291   int sqlite3OsCurrentTime(double*);
   292    292   void sqlite3OsEnterMutex(void);
   293    293   void sqlite3OsLeaveMutex(void);
   294    294   int sqlite3OsInMutex(void);
   295         -void *sqlite3OsThreadSpecificData(int);
          295  +ThreadData *sqlite3OsThreadSpecificData(int);
   296    296   void *sqlite3OsMalloc(int);
   297    297   void *sqlite3OsRealloc(void *, int);
   298    298   void sqlite3OsFree(void *);
   299    299   int sqlite3OsAllocationSize(void *);
   300    300   
   301    301   /*
   302    302   ** If the SQLITE_ENABLE_REDEF_IO macro is defined, then the OS-layer
................................................................................
   331    331     int (*xRandomSeed)(char*);
   332    332     int (*xSleep)(int ms);
   333    333     int (*xCurrentTime)(double*);
   334    334   
   335    335     void (*xEnterMutex)(void);
   336    336     void (*xLeaveMutex)(void);
   337    337     int (*xInMutex)(void);
   338         -  void *(*xThreadSpecificData)(int);
          338  +  ThreadData *(*xThreadSpecificData)(int);
   339    339   
   340    340     void *(*xMalloc)(int);
   341    341     void *(*xRealloc)(void *, int);
   342    342     void (*xFree)(void *);
   343    343     int (*xAllocationSize)(void *);
   344    344   };
   345    345   

Changes to SQLite.Interop/src/os_unix.c.

  1623   1623   ** Return TRUE if we are currently within the mutex and FALSE if not.
  1624   1624   */
  1625   1625   int sqlite3UnixInMutex(){
  1626   1626     return inMutex;
  1627   1627   }
  1628   1628   
  1629   1629   /*
  1630         -** This function is called automatically when a thread exists to delete
  1631         -** the threads ThreadData structure. 
  1632         -**
  1633         -** Because the ThreadData structure is required by higher level routines
  1634         -** such as sqliteMalloc() we use OsFree() and OsMalloc() directly to
  1635         -** allocate the thread specific data.
         1630  +** Remember the number of thread-specific-data blocks allocated.
         1631  +** Use this to verify that we are not leaking thread-specific-data.
         1632  +** Ticket #1601
  1636   1633   */
  1637         -#ifdef SQLITE_UNIX_THREADS
  1638         -static void deleteTsd(void *pTsd){
  1639         -  sqlite3OsFree(pTsd);
  1640         -}
         1634  +#ifdef SQLITE_TEST
         1635  +int sqlite3_tsd_count = 0;
         1636  +# ifdef SQLITE_UNIX_THREADS
         1637  +    static pthread_mutex_t tsd_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
         1638  +#   define TSD_COUNTER(N) \
         1639  +             pthread_mutex_lock(&tsd_counter_mutex); \
         1640  +             sqlite3_tsd_count += N; \
         1641  +             pthread_mutex_unlock(&tsd_counter_mutex);
         1642  +# else
         1643  +#   define TSD_COUNTER(N)  sqlite3_tsd_count += N
         1644  +# endif
         1645  +#else
         1646  +# define TSD_COUNTER(N)  /* no-op */
  1641   1647   #endif
  1642   1648   
  1643         -/* 
  1644         -** The first time this function is called from a specific thread, nByte 
  1645         -** bytes of data area are allocated and zeroed. A pointer to the new 
  1646         -** allocation is returned to the caller. 
         1649  +
         1650  +/*
         1651  +** If called with allocateFlag>1, then return a pointer to thread
         1652  +** specific data for the current thread.  Allocate and zero the
         1653  +** thread-specific data if it does not already exist necessary.
         1654  +**
         1655  +** If called with allocateFlag==0, then check the current thread
         1656  +** specific data.  Return it if it exists.  If it does not exist,
         1657  +** then return NULL.
  1647   1658   **
  1648         -** Each subsequent call to this function from the thread returns the same
  1649         -** pointer. The argument is ignored in this case.
         1659  +** If called with allocateFlag<0, check to see if the thread specific
         1660  +** data is allocated and is all zero.  If it is then deallocate it.
         1661  +** Return a pointer to the thread specific data or NULL if it is
         1662  +** unallocated or gets deallocated.
  1650   1663   */
  1651         -void *sqlite3UnixThreadSpecificData(int nByte){
         1664  +ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){
         1665  +  static const ThreadData zeroData;
  1652   1666   #ifdef SQLITE_UNIX_THREADS
  1653   1667     static pthread_key_t key;
  1654   1668     static int keyInit = 0;
  1655         -  void *pTsd;
         1669  +  ThreadData *pTsd;
  1656   1670   
  1657   1671     if( !keyInit ){
  1658   1672       sqlite3OsEnterMutex();
  1659   1673       if( !keyInit ){
  1660   1674         int rc;
  1661         -      rc = pthread_key_create(&key, deleteTsd);
         1675  +      rc = pthread_key_create(&key, 0);
  1662   1676         if( rc ){
  1663   1677           sqlite3OsLeaveMutex();
  1664   1678           return 0;
  1665   1679         }
  1666   1680         keyInit = 1;
  1667   1681       }
  1668   1682       sqlite3OsLeaveMutex();
  1669   1683     }
  1670   1684   
  1671   1685     pTsd = pthread_getspecific(key);
  1672         -  if( !pTsd ){
  1673         -    pTsd = sqlite3OsMalloc(nByte);
  1674         -    if( pTsd ){
  1675         -      memset(pTsd, 0, nByte);
  1676         -      pthread_setspecific(key, pTsd);
         1686  +  if( allocateFlag>0 ){
         1687  +    if( pTsd==0 ){
         1688  +      pTsd = sqlite3OsMalloc(sizeof(zeroData));
         1689  +      if( pTsd ){
         1690  +        *pTsd = zeroData;
         1691  +        pthread_setspecific(key, pTsd);
         1692  +        TSD_COUNTER(+1);
         1693  +      }
  1677   1694       }
         1695  +  }else if( pTsd!=0 && allocateFlag<0 
         1696  +            && memcmp(pTsd, &zeroData, sizeof(zeroData))==0 ){
         1697  +    sqlite3OsFree(pTsd);
         1698  +    pthread_setspecific(key, 0);
         1699  +    TSD_COUNTER(-1);
         1700  +    pTsd = 0;
  1678   1701     }
  1679   1702     return pTsd;
  1680   1703   #else
  1681         -  static void *pTsd = 0;
  1682         -  if( !pTsd ){
  1683         -    pTsd = sqlite3OsMalloc(nByte);
  1684         -    if( pTsd ){
  1685         -      memset(pTsd, 0, nByte);
         1704  +  static ThreadData *pTsd = 0;
         1705  +  if( allocateFlag>0 ){
         1706  +    if( pTsd==0 ){
         1707  +      pTsd = sqlite3OsMalloc( sizeof(zeroData) );
         1708  +      if( pTsd ){
         1709  +        *pTsd = zeroData;
         1710  +        TSD_COUNTER(+1);
         1711  +      }
  1686   1712       }
         1713  +  }else if( pTsd!=0 && allocateFlag<0
         1714  +            && memcmp(pTsd, &zeroData, sizeof(zeroData))==0 ){
         1715  +    sqlite3OsFree(pTsd);
         1716  +    TSD_COUNTER(-1);
         1717  +    pTsd = 0;
  1687   1718     }
  1688   1719     return pTsd;
  1689   1720   #endif
  1690   1721   }
  1691   1722   
  1692   1723   /*
  1693   1724   ** The following variable, if set to a non-zero value, becomes the result

Changes to SQLite.Interop/src/os_win.c.

    40     40   */
    41     41   #if defined(_WIN32_WCE)
    42     42   # define OS_WINCE 1
    43     43   #else
    44     44   # define OS_WINCE 0
    45     45   #endif
    46     46   
           47  +/*
           48  +** WinCE lacks native support for file locking so we have to fake it
           49  +** with some code of our own.
           50  +*/
    47     51   #if OS_WINCE
    48         -typedef struct _LOCKDATA
    49         -{
    50         -  long nReaders;
    51         -  long nPending;
    52         -  long nReserved;
    53         -  long nExclusive;
    54         -} LOCKDATA;
           52  +typedef struct winceLock {
           53  +  int nReaders;       /* Number of reader locks obtained */
           54  +  BOOL bPending;      /* Indicates a pending lock has been obtained */
           55  +  BOOL bReserved;     /* Indicates a reserved lock has been obtained */
           56  +  BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
           57  +} winceLock;
    55     58   #endif
    56     59   
    57     60   /*
    58     61   ** The winFile structure is a subclass of OsFile specific to the win32
    59     62   ** portability layer.
    60     63   */
    61     64   typedef struct winFile winFile;
    62     65   struct winFile {
    63     66     IoMethod const *pMethod;/* Must be first */
    64     67     HANDLE h;               /* Handle for accessing the file */
    65     68     unsigned char locktype; /* Type of lock currently held on this file */
    66     69     short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
    67     70   #if OS_WINCE
    68         -  WCHAR *zDeleteOnClose;   /* Name of file to delete when closing */
    69         -
    70         -#ifndef SQLITE_OMIT_WIN_LOCKS
    71         -  HANDLE hMutex;
    72         -  HANDLE hShared;
    73         -  LOCKDATA local;
    74         -  LOCKDATA *shared;
    75         -#endif 
    76         -
           71  +  WCHAR *zDeleteOnClose;  /* Name of file to delete when closing */
           72  +  HANDLE hMutex;          /* Mutex used to control access to shared lock */  
           73  +  HANDLE hShared;         /* Shared memory segment used for locking */
           74  +  winceLock local;        /* Locks obtained by this instance of winFile */
           75  +  winceLock *shared;      /* Global shared lock memory for the file  */
    77     76   #endif
    78     77   };
    79     78   
    80     79   
    81     80   /*
    82     81   ** Do not include any of the File I/O interface procedures if the
    83     82   ** SQLITE_OMIT_DISKIO macro is defined (indicating that there database
................................................................................
   120    119         GetVersionEx(&sInfo);
   121    120         sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
   122    121       }
   123    122       return sqlite3_os_type==2;
   124    123     }
   125    124   #endif /* OS_WINCE */
   126    125   
   127         -#if OS_WINCE
   128         -/*
   129         -** WindowsCE does not have a localtime() function.  So create a
   130         -** substitute.
   131         -*/
   132         -#include <time.h>
   133         -struct tm *__cdecl localtime(const time_t *t)
   134         -{
   135         -  static struct tm y;
   136         -  FILETIME uTm, lTm;
   137         -  SYSTEMTIME pTm;
   138         -  i64 t64;
   139         -  t64 = *t;
   140         -  t64 = (t64 + 11644473600)*10000000;
   141         -  uTm.dwLowDateTime = t64 & 0xFFFFFFFF;
   142         -  uTm.dwHighDateTime= t64 >> 32;
   143         -  FileTimeToLocalFileTime(&uTm,&lTm);
   144         -  FileTimeToSystemTime(&lTm,&pTm);
   145         -  y.tm_year = pTm.wYear - 1900;
   146         -  y.tm_mon = pTm.wMonth - 1;
   147         -  y.tm_wday = pTm.wDayOfWeek;
   148         -  y.tm_mday = pTm.wDay;
   149         -  y.tm_hour = pTm.wHour;
   150         -  y.tm_min = pTm.wMinute;
   151         -  y.tm_sec = pTm.wSecond;
   152         -  return &y;
   153         -}
   154         -
   155         -/* This will never be called, but defined to make the code compile */
   156         -#define GetTempPathA(a,b)
   157         -
   158         -#endif
   159         -
   160         -/*
   161         -** Compile with -DSQLITE_OMIT_WIN_LOCKS to disable file locking on
   162         -** windows.  If you do this and two or more connections attempt to
   163         -** write the database at the same time, the database file will be
   164         -** corrupted.  But some versions of WindowsCE do not support locking,
   165         -** in which case compiling with this option is required just to get
   166         -** it to work at all.
   167         -*/
   168         -#ifdef SQLITE_OMIT_WIN_LOCKS
   169         -# define LockFile(a,b,c,d,e) (1)
   170         -# define LockFileEx(a,b,c,d,e,f) (1)
   171         -# define UnlockFile(a,b,c,d,e) (1)
   172         -# define UnlockFileEx(a,b,c,d,e) (1)
   173         -#endif
   174         -
   175    126   /*
   176    127   ** Convert a UTF-8 string to UTF-32.  Space to hold the returned string
   177    128   ** is obtained from sqliteMalloc.
   178    129   */
   179    130   static WCHAR *utf8ToUnicode(const char *zFilename){
   180    131     int nByte;
   181    132     WCHAR *zWideFilename;
................................................................................
   214    165     if( nByte == 0 ){
   215    166       sqliteFree(zFilename);
   216    167       zFilename = 0;
   217    168     }
   218    169     return zFilename;
   219    170   }
   220    171   
   221         -#if OS_WINCE && !SQLITE_OMIT_WIN_LOCKS
   222         -#define LockFile(a, b, c, d, e) pseudoLockFile(&a, b, c, d, e)
   223         -#define UnlockFile(a, b, c, d, e) pseudoUnlockFile(&a, b, c, d, e)
   224         -#define LockFileEx(a, b, c, d, e, f) pseudoLockFileEx(&a, b, c, d, e, f)
   225         -
   226         -#define LOCKSTRUCT winFile
   227         -
   228         -#ifndef PtrToInt
   229         -#define PtrToInt( p )  ((INT)(INT_PTR) (p) )
   230         -#endif
   231         -
   232         -#define HANDLE_TO_LOCKSTRUCT(a) (LOCKSTRUCT *)&((LPBYTE)a)[-PtrToInt((&((LOCKSTRUCT *)0)->h))]
   233         -
   234         -#define MUTEX_ACQUIRE(h) { DWORD dwErr; do { dwErr = WaitForSingleObject(h, INFINITE); } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED); }
   235         -#define MUTEX_RELEASE(h) ReleaseMutex(h)
   236         -
   237         -static BOOL CreateLockStruct(const char *pszFilename, LOCKSTRUCT *pLocks)
          172  +#if OS_WINCE
          173  +/*************************************************************************
          174  +** This section contains code for WinCE only.
          175  +*/
          176  +/*
          177  +** WindowsCE does not have a localtime() function.  So create a
          178  +** substitute.
          179  +*/
          180  +#include <time.h>
          181  +struct tm *__cdecl localtime(const time_t *t)
   238    182   {
   239         -  WCHAR *pszTok;
   240         -  WCHAR *pszName = utf8ToUnicode(pszFilename);
          183  +  static struct tm y;
          184  +  FILETIME uTm, lTm;
          185  +  SYSTEMTIME pTm;
          186  +  i64 t64;
          187  +  t64 = *t;
          188  +  t64 = (t64 + 11644473600)*10000000;
          189  +  uTm.dwLowDateTime = t64 & 0xFFFFFFFF;
          190  +  uTm.dwHighDateTime= t64 >> 32;
          191  +  FileTimeToLocalFileTime(&uTm,&lTm);
          192  +  FileTimeToSystemTime(&lTm,&pTm);
          193  +  y.tm_year = pTm.wYear - 1900;
          194  +  y.tm_mon = pTm.wMonth - 1;
          195  +  y.tm_wday = pTm.wDayOfWeek;
          196  +  y.tm_mday = pTm.wDay;
          197  +  y.tm_hour = pTm.wHour;
          198  +  y.tm_min = pTm.wMinute;
          199  +  y.tm_sec = pTm.wSecond;
          200  +  return &y;
          201  +}
          202  +
          203  +/* This will never be called, but defined to make the code compile */
          204  +#define GetTempPathA(a,b)
          205  +
          206  +#define LockFile(a,b,c,d,e)       winceLockFile(&a, b, c, d, e)
          207  +#define UnlockFile(a,b,c,d,e)     winceUnlockFile(&a, b, c, d, e)
          208  +#define LockFileEx(a,b,c,d,e,f)   winceLockFileEx(&a, b, c, d, e, f)
          209  +
          210  +#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-offsetof(winFile,h)]
          211  +
          212  +/*
          213  +** Acquire a lock on the handle h
          214  +*/
          215  +static void winceMutexAcquire(HANDLE h){
          216  +   DWORD dwErr;
          217  +   do {
          218  +     dwErr = WaitForSingleObject(h, INFINITE);
          219  +   } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
          220  +}
          221  +/*
          222  +** Release a lock acquired by winceMutexAcquire()
          223  +*/
          224  +#define winceMutexRelease(h) ReleaseMutex(h)
          225  +
          226  +/*
          227  +** Create the mutex and shared memory used for locking in the file
          228  +** descriptor pFile
          229  +*/
          230  +static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
          231  +  WCHAR *zTok;
          232  +  WCHAR *zName = utf8ToUnicode(zFilename);
   241    233     BOOL bInit = TRUE;
   242    234   
   243    235     /* Initialize the local lockdata */
   244         -  ZeroMemory(&pLocks->local, sizeof(LOCKDATA));
          236  +  ZeroMemory(&pFile->local, sizeof(pFile->local));
   245    237   
   246         -  /* Create a unique global name for the mutex and subsequently the shared memory */
   247         -  CharLowerW(pszName);
   248         -  while (pszTok = wcschr(pszName, '\\'))
   249         -  {
   250         -    *pszTok = '_';
          238  +  /* Replace the backslashes from the filename and lowercase it
          239  +  ** to derive a mutex name. */
          240  +  zTok = CharLowerW(zName);
          241  +  for (;*zTok;zTok++){
          242  +    if (*zTok == '\\') *zTok = '_';
   251    243     }
   252    244   
   253    245     /* Create/open the named mutex */
   254         -  pLocks->hMutex = CreateMutexW(NULL, FALSE, pszName);
   255         -  if (!pLocks->hMutex)
   256         -  {
   257         -    sqliteFree(pszName);
          246  +  pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
          247  +  if (!pFile->hMutex){
          248  +    sqliteFree(zName);
   258    249       return FALSE;
   259    250     }
   260    251   
   261    252     /* Acquire the mutex before continuing */
   262         -  MUTEX_ACQUIRE(pLocks->hMutex);
          253  +  winceMutexAcquire(pFile->hMutex);
   263    254     
   264         -  /* Create/open the shared memory */
   265         -  CharUpperW(pszName);
   266         -  pLocks->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(LOCKDATA), pszName);  
          255  +  /* Since the names of named mutexes, semaphores, file mappings etc are 
          256  +  ** case-sensitive, take advantage of that by uppercasing the mutex name
          257  +  ** and using that as the shared filemapping name.
          258  +  */
          259  +  CharUpperW(zName);
          260  +  pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
          261  +                                       PAGE_READWRITE, 0, sizeof(winceLock),
          262  +                                       zName);  
   267    263   
   268         -  /* Set a flag that indicates we're the first to create the memory so it must be zero-initialized */
   269         -  if (GetLastError() == ERROR_ALREADY_EXISTS)
   270         -  {
          264  +  /* Set a flag that indicates we're the first to create the memory so it 
          265  +  ** must be zero-initialized */
          266  +  if (GetLastError() == ERROR_ALREADY_EXISTS){
   271    267       bInit = FALSE;
   272    268     }
   273    269   
   274         -  sqliteFree(pszName);
          270  +  sqliteFree(zName);
   275    271   
   276    272     /* If we succeeded in making the shared memory handle, map it. */
   277         -  if (pLocks->hShared)
   278         -  {
   279         -    pLocks->shared = (LOCKDATA *)MapViewOfFile(pLocks->hShared, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(LOCKDATA));
          273  +  if (pFile->hShared){
          274  +    pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared, 
          275  +             FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
   280    276       /* If mapping failed, close the shared memory handle and erase it */
   281         -    if (!pLocks->shared)
   282         -    {
   283         -      CloseHandle(pLocks->hShared);
   284         -      pLocks->hShared = NULL;
          277  +    if (!pFile->shared){
          278  +      CloseHandle(pFile->hShared);
          279  +      pFile->hShared = NULL;
   285    280       }
   286    281     }
   287    282   
   288    283     /* If shared memory could not be created, then close the mutex and fail */
   289         -  if (pLocks->hShared == NULL)
   290         -  {
   291         -    MUTEX_RELEASE(pLocks->hMutex);
   292         -    CloseHandle(pLocks->hMutex);
   293         -    pLocks->hMutex = NULL;
          284  +  if (pFile->hShared == NULL){
          285  +    winceMutexRelease(pFile->hMutex);
          286  +    CloseHandle(pFile->hMutex);
          287  +    pFile->hMutex = NULL;
   294    288       return FALSE;
   295    289     }
   296    290     
   297    291     /* Initialize the shared memory if we're supposed to */
   298         -  if (bInit)
   299         -  {
   300         -    ZeroMemory(pLocks->shared, sizeof(LOCKDATA));
          292  +  if (bInit) {
          293  +    ZeroMemory(pFile->shared, sizeof(winceLock));
   301    294     }
   302    295   
   303         -  MUTEX_RELEASE(pLocks->hMutex);
   304         -
          296  +  winceMutexRelease(pFile->hMutex);
   305    297     return TRUE;
   306    298   }
   307    299   
   308         -static void DestroyLockStruct(LOCKSTRUCT *pLocks)
   309         -{
   310         -  if (pLocks->hMutex)
   311         -  {
          300  +/*
          301  +** Destroy the part of winFile that deals with wince locks
          302  +*/
          303  +static void winceDestroyLock(winFile *pFile){
          304  +  if (pFile->hMutex){
   312    305       /* Acquire the mutex */
   313         -    MUTEX_ACQUIRE(pLocks->hMutex);
          306  +    winceMutexAcquire(pFile->hMutex);
   314    307   
   315    308       /* The following blocks should probably assert in debug mode, but they
   316    309          are to cleanup in case any locks remained open */
   317         -    if (pLocks->local.nReaders)
   318         -    {
   319         -      pLocks->shared->nReaders --;
          310  +    if (pFile->local.nReaders){
          311  +      pFile->shared->nReaders --;
   320    312       }
   321         -
   322         -    if (pLocks->local.nReserved)
   323         -    {
   324         -      pLocks->shared->nReserved = 0;
          313  +    if (pFile->local.bReserved){
          314  +      pFile->shared->bReserved = FALSE;
   325    315       }
   326         -
   327         -    if (pLocks->local.nPending)
   328         -    {
   329         -      pLocks->shared->nPending = 0;
          316  +    if (pFile->local.bPending){
          317  +      pFile->shared->bPending = FALSE;
   330    318       }
   331         -
   332         -    if (pLocks->local.nExclusive)
   333         -    {
   334         -      pLocks->shared->nExclusive = 0;
          319  +    if (pFile->local.bExclusive){
          320  +      pFile->shared->bExclusive = FALSE;
   335    321       }
   336    322   
   337    323       /* De-reference and close our copy of the shared memory handle */
   338         -    UnmapViewOfFile(pLocks->shared);
   339         -    CloseHandle(pLocks->hShared);
          324  +    UnmapViewOfFile(pFile->shared);
          325  +    CloseHandle(pFile->hShared);
   340    326   
   341    327       /* Done with the mutex */
   342         -    MUTEX_RELEASE(pLocks->hMutex);    
   343         -    CloseHandle(pLocks->hMutex);
   344         -    pLocks->hMutex = NULL;
          328  +    winceMutexRelease(pFile->hMutex);    
          329  +    CloseHandle(pFile->hMutex);
          330  +    pFile->hMutex = NULL;
   345    331     }
   346    332   }
   347    333   
   348         -/* Custom pseudo file locking support specifically for SQLite */
   349         -BOOL pseudoLockFile(HANDLE *phFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh, DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
   350         -{
   351         -  LOCKSTRUCT *pls = HANDLE_TO_LOCKSTRUCT(phFile);
          334  +/* 
          335  +** An implementation of the LockFile() API of windows for wince
          336  +*/
          337  +static BOOL winceLockFile(
          338  +  HANDLE *phFile,
          339  +  DWORD dwFileOffsetLow,
          340  +  DWORD dwFileOffsetHigh,
          341  +  DWORD nNumberOfBytesToLockLow,
          342  +  DWORD nNumberOfBytesToLockHigh
          343  +){
          344  +  winFile *pFile = HANDLE_TO_WINFILE(phFile);
   352    345     BOOL bReturn = FALSE;
   353    346   
   354         -  if (!pls->hMutex) return TRUE;
   355         -
   356         -  MUTEX_ACQUIRE(pls->hMutex);
          347  +  if (!pFile->hMutex) return TRUE;
          348  +  winceMutexAcquire(pFile->hMutex);
   357    349   
   358    350     /* Wanting an exclusive lock? */
   359         -  if (dwFileOffsetLow == SHARED_FIRST && nNumberOfBytesToLockLow == SHARED_SIZE)
   360         -  {
   361         -    if (pls->shared->nReaders == 0 && pls->shared->nExclusive == 0)
   362         -    {
   363         -        pls->shared->nExclusive = 1;
   364         -        pls->local.nExclusive = 1;
   365         -        bReturn = TRUE;
          351  +  if (dwFileOffsetLow == SHARED_FIRST
          352  +       && nNumberOfBytesToLockLow == SHARED_SIZE){
          353  +    if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
          354  +       pFile->shared->bExclusive = TRUE;
          355  +       pFile->local.bExclusive = TRUE;
          356  +       bReturn = TRUE;
   366    357       }
   367    358     }
          359  +
   368    360     /* Want a read-only lock? */
   369         -  else if ((dwFileOffsetLow >= SHARED_FIRST && dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE) && nNumberOfBytesToLockLow == 1)
   370         -  {
   371         -    if (pls->shared->nExclusive == 0)
   372         -    {
   373         -      pls->local.nReaders ++;
   374         -      if (pls->local.nReaders == 1)
   375         -      {
   376         -        pls->shared->nReaders ++;
          361  +  else if ((dwFileOffsetLow >= SHARED_FIRST &&
          362  +            dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE) &&
          363  +            nNumberOfBytesToLockLow == 1){
          364  +    if (pFile->shared->bExclusive == 0){
          365  +      pFile->local.nReaders ++;
          366  +      if (pFile->local.nReaders == 1){
          367  +        pFile->shared->nReaders ++;
   377    368         }
   378    369         bReturn = TRUE;
   379    370       }
   380    371     }
          372  +
   381    373     /* Want a pending lock? */
   382         -  else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToLockLow == 1)
   383         -  {
          374  +  else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToLockLow == 1){
   384    375       /* If no pending lock has been acquired, then acquire it */
   385         -    if (pls->shared->nPending == 0)
   386         -    {
   387         -      pls->shared->nPending = 1;
   388         -      pls->local.nPending = 1;
          376  +    if (pFile->shared->bPending == 0) {
          377  +      pFile->shared->bPending = TRUE;
          378  +      pFile->local.bPending = TRUE;
   389    379         bReturn = TRUE;
   390    380       }
   391    381     }
   392    382     /* Want a reserved lock? */
   393         -  else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToLockLow == 1)
   394         -  {
   395         -    if (pls->shared->nReserved == 0)
   396         -    {
   397         -      pls->shared->nReserved = 1;
   398         -      pls->local.nReserved = 1;
          383  +  else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
          384  +    if (pFile->shared->bReserved == 0) {
          385  +      pFile->shared->bReserved = TRUE;
          386  +      pFile->local.bReserved = TRUE;
   399    387         bReturn = TRUE;
   400    388       }
   401    389     }
   402    390   
   403         -  MUTEX_RELEASE(pls->hMutex);
   404         -
          391  +  winceMutexRelease(pFile->hMutex);
   405    392     return bReturn;
   406    393   }
   407    394   
   408         -BOOL pseudoUnlockFile(HANDLE *phFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh, DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
   409         -{
   410         -  LOCKSTRUCT *pls = HANDLE_TO_LOCKSTRUCT(phFile);
          395  +/*
          396  +** An implementation of the UnlockFile API of windows for wince
          397  +*/
          398  +static BOOL winceUnlockFile(
          399  +  HANDLE *phFile,
          400  +  DWORD dwFileOffsetLow,
          401  +  DWORD dwFileOffsetHigh,
          402  +  DWORD nNumberOfBytesToUnlockLow,
          403  +  DWORD nNumberOfBytesToUnlockHigh
          404  +){
          405  +  winFile *pFile = HANDLE_TO_WINFILE(phFile);
   411    406     BOOL bReturn = FALSE;
   412    407   
   413         -  if (!pls->hMutex) return TRUE;
   414         -
   415         -  MUTEX_ACQUIRE(pls->hMutex);
          408  +  if (!pFile->hMutex) return TRUE;
          409  +  winceMutexAcquire(pFile->hMutex);
   416    410   
   417    411     /* Releasing a reader lock or an exclusive lock */
   418         -  if (dwFileOffsetLow >= SHARED_FIRST && dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE)
   419         -  {
          412  +  if (dwFileOffsetLow >= SHARED_FIRST &&
          413  +       dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE){
   420    414       /* Did we have an exclusive lock? */
   421         -    if (pls->local.nExclusive)
   422         -    {
   423         -      pls->local.nExclusive = 0;
   424         -      pls->shared->nExclusive = 0;
          415  +    if (pFile->local.bExclusive){
          416  +      pFile->local.bExclusive = FALSE;
          417  +      pFile->shared->bExclusive = FALSE;
   425    418         bReturn = TRUE;
   426    419       }
          420  +
   427    421       /* Did we just have a reader lock? */
   428         -    else if (pls->local.nReaders)
   429         -    {
   430         -      pls->local.nReaders --;
   431         -      if (pls->local.nReaders == 0)
          422  +    else if (pFile->local.nReaders){
          423  +      pFile->local.nReaders --;
          424  +      if (pFile->local.nReaders == 0)
   432    425         {
   433         -        pls->shared->nReaders --;
          426  +        pFile->shared->nReaders --;
   434    427         }
   435    428         bReturn = TRUE;
   436    429       }
   437    430     }
          431  +
   438    432     /* Releasing a pending lock */
   439         -  else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToUnlockLow == 1)
   440         -  {
   441         -    if (pls->local.nPending)
   442         -    {
   443         -      pls->local.nPending = 0;
   444         -      pls->shared->nPending = 0;
          433  +  else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
          434  +    if (pFile->local.bPending){
          435  +      pFile->local.bPending = FALSE;
          436  +      pFile->shared->bPending = FALSE;
   445    437         bReturn = TRUE;
   446    438       }
   447    439     }
   448    440     /* Releasing a reserved lock */
   449         -  else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1)
   450         -  {
   451         -    if (pls->local.nReserved)
   452         -    {
   453         -      pls->local.nReserved = 0;
   454         -      pls->shared->nReserved = 0;
          441  +  else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
          442  +    if (pFile->local.bReserved) {
          443  +      pFile->local.bReserved = FALSE;
          444  +      pFile->shared->bReserved = FALSE;
   455    445         bReturn = TRUE;
   456    446       }
   457    447     }
   458    448   
   459         -  MUTEX_RELEASE(pls->hMutex);
   460         -
          449  +  winceMutexRelease(pFile->hMutex);
   461    450     return bReturn;
   462    451   }
   463    452   
   464         -BOOL pseudoLockFileEx(HANDLE *phFile, DWORD dwFlags, DWORD dwReserved, DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh, LPOVERLAPPED lpOverlapped)
   465         -{
   466         -  /* If the caller wants a shared read lock, forward this call to pseudoLockFile */
   467         -  if (lpOverlapped->Offset == SHARED_FIRST && dwFlags == 1 && nNumberOfBytesToLockLow == SHARED_SIZE)
   468         -    return pseudoLockFile(phFile, SHARED_FIRST, 0, 1, 0);
   469         -
          453  +/*
          454  +** An implementation of the LockFileEx() API of windows for wince
          455  +*/
          456  +static BOOL winceLockFileEx(
          457  +  HANDLE *phFile,
          458  +  DWORD dwFlags,
          459  +  DWORD dwReserved,
          460  +  DWORD nNumberOfBytesToLockLow,
          461  +  DWORD nNumberOfBytesToLockHigh,
          462  +  LPOVERLAPPED lpOverlapped
          463  +){
          464  +  /* If the caller wants a shared read lock, forward this call
          465  +  ** to winceLockFile */
          466  +  if (lpOverlapped->Offset == SHARED_FIRST &&
          467  +      dwFlags == 1 &&
          468  +      nNumberOfBytesToLockLow == SHARED_SIZE){
          469  +    return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
          470  +  }
   470    471     return FALSE;
   471    472   }
   472         -
   473         -#endif /* OS_WINCE && !SQLITE_OMIT_WIN_LOCKS */
          473  +/*
          474  +** End of the special code for wince
          475  +*****************************************************************************/
          476  +#endif /* OS_WINCE */
   474    477   
   475    478   /*
   476    479   ** Delete the named file
   477    480   */
   478    481   int sqlite3WinDelete(const char *zFilename){
   479    482     WCHAR *zWide = utf8ToUnicode(zFilename);
   480    483     if( zWide ){
................................................................................
   557    560           sqliteFree(zWide);
   558    561           return SQLITE_CANTOPEN;
   559    562         }
   560    563         *pReadonly = 1;
   561    564       }else{
   562    565         *pReadonly = 0;
   563    566       }
   564         -#if OS_WINCE && !SQLITE_OMIT_WIN_LOCKS
   565         -    if (!CreateLockStruct(zFilename, &f)){
          567  +#if OS_WINCE
          568  +    if (!winceCreateLock(zFilename, &f)){
   566    569         CloseHandle(h);
   567    570         sqliteFree(zWide);
   568    571         return SQLITE_CANTOPEN;
   569    572       }
   570    573   #endif
   571    574       sqliteFree(zWide);
   572    575     }else{
................................................................................
   664    667       return SQLITE_CANTOPEN;
   665    668     }
   666    669     f.h = h;
   667    670     f.locktype = NO_LOCK;
   668    671     f.sharedLockByte = 0;
   669    672   #if OS_WINCE
   670    673     f.zDeleteOnClose = delFlag ? utf8ToUnicode(zFilename) : 0;
   671         -#ifndef SQLITE_OMIT_WIN_LOCKS
   672    674     f.hMutex = NULL;
   673         -#endif
   674    675   #endif
   675    676     TRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
   676    677     return allocateWinFile(&f, pId);
   677    678   }
   678    679   
   679    680   /*
   680    681   ** Attempt to open a new file for read-only access.
................................................................................
   716    717       return SQLITE_CANTOPEN;
   717    718     }
   718    719     f.h = h;
   719    720     f.locktype = NO_LOCK;
   720    721     f.sharedLockByte = 0;
   721    722   #if OS_WINCE
   722    723     f.zDeleteOnClose = 0;
   723         -#ifndef SQLITE_OMIT_WIN_LOCKS
   724    724     f.hMutex = NULL;
   725         -#endif
   726    725   #endif
   727    726     TRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
   728    727     return allocateWinFile(&f, pId);
   729    728   }
   730    729   
   731    730   /*
   732    731   ** Attempt to open a file descriptor for the directory that contains a
................................................................................
   806    805   */
   807    806   static int winClose(OsFile **pId){
   808    807     winFile *pFile;
   809    808     if( pId && (pFile = (winFile*)*pId)!=0 ){
   810    809       TRACE2("CLOSE %d\n", pFile->h);
   811    810       CloseHandle(pFile->h);
   812    811   #if OS_WINCE
   813         -#ifndef SQLITE_OMIT_WIN_LOCKS
   814         -    DestroyLockStruct(pFile);
   815         -#endif
          812  +    winceDestroyLock(pFile);
   816    813       if( pFile->zDeleteOnClose ){
   817    814         DeleteFileW(pFile->zDeleteOnClose);
   818    815         sqliteFree(pFile->zDeleteOnClose);
   819    816       }
   820    817   #endif
   821    818       OpenCounter(-1);
   822    819       sqliteFree(pFile);
................................................................................
  1435   1432     if( sqlite3_current_time ){
  1436   1433       *prNow = sqlite3_current_time/86400.0 + 2440587.5;
  1437   1434     }
  1438   1435   #endif
  1439   1436     return 0;
  1440   1437   }
  1441   1438   
  1442         -/* 
  1443         -** The first time this function is called from a specific thread, nByte 
  1444         -** bytes of data area are allocated and zeroed. A pointer to the new 
  1445         -** allocation is returned to the caller. 
         1439  +/*
         1440  +** Remember the number of thread-specific-data blocks allocated.
         1441  +** Use this to verify that we are not leaking thread-specific-data.
         1442  +** Ticket #1601
         1443  +*/
         1444  +#ifdef SQLITE_TEST
         1445  +int sqlite3_tsd_count = 0;
         1446  +# define TSD_COUNTER_INCR InterlockedIncrement(&sqlite3_tsd_count)
         1447  +# define TSD_COUNTER_DECR InterlockedDecrement(&sqlite3_tsd_count)
         1448  +#else
         1449  +# define TSD_COUNTER_INCR  /* no-op */
         1450  +# define TSD_COUNTER_DECR  /* no-op */
         1451  +#endif
         1452  +
         1453  +
         1454  +
         1455  +/*
         1456  +** If called with allocateFlag>1, then return a pointer to thread
         1457  +** specific data for the current thread.  Allocate and zero the
         1458  +** thread-specific data if it does not already exist necessary.
         1459  +**
         1460  +** If called with allocateFlag==0, then check the current thread
         1461  +** specific data.  Return it if it exists.  If it does not exist,
         1462  +** then return NULL.
  1446   1463   **
  1447         -** Each subsequent call to this function from the thread returns the same
  1448         -** pointer. The argument is ignored in this case.
         1464  +** If called with allocateFlag<0, check to see if the thread specific
         1465  +** data is allocated and is all zero.  If it is then deallocate it.
         1466  +** Return a pointer to the thread specific data or NULL if it is
         1467  +** unallocated or gets deallocated.
  1449   1468   */
  1450         -void *sqlite3WinThreadSpecificData(int nByte){
  1451         -  static void *pTsd = 0;
         1469  +ThreadData *sqlite3WinThreadSpecificData(int allocateFlag){
  1452   1470     static int key;
  1453   1471     static int keyInit = 0;
         1472  +  static const ThreadData zeroData;
         1473  +  ThreadData *pTsd;
  1454   1474   
  1455   1475     if( !keyInit ){
  1456   1476       sqlite3OsEnterMutex();
  1457   1477       if( !keyInit ){
  1458   1478         key = TlsAlloc();
  1459   1479         if( key==0xffffffff ){
  1460   1480           sqlite3OsLeaveMutex();
................................................................................
  1461   1481           return 0;
  1462   1482         }
  1463   1483         keyInit = 1;
  1464   1484       }
  1465   1485       sqlite3OsLeaveMutex();
  1466   1486     }
  1467   1487     pTsd = TlsGetValue(key);
  1468         -  if( !pTsd ){
  1469         -    pTsd = sqlite3OsMalloc(nByte);
  1470         -    if( pTsd ){
  1471         -      memset(pTsd, 0, nByte);
  1472         -      TlsSetValue(key, pTsd);
         1488  +  if( allocateFlag>0 ){
         1489  +    if( !pTsd ){
         1490  +      pTsd = sqlite3OsMalloc( sizeof(zeroData) );
         1491  +      if( pTsd ){
         1492  +        *pTsd = zeroData;
         1493  +        TlsSetValue(key, pTsd);
         1494  +        TSD_COUNTER_INCR;
         1495  +      }
  1473   1496       }
         1497  +  }else if( pTsd!=0 && allocateFlag<0 
         1498  +              && memcmp(pTsd, &zeroData, sizeof(zeroData))==0 ){
         1499  +    sqlite3OsFree(pTsd);
         1500  +    TlsSetValue(key, 0);
         1501  +    TSD_COUNTER_DECR;
         1502  +    pTsd = 0;
  1474   1503     }
  1475   1504     return pTsd;
  1476   1505   }
  1477   1506   #endif /* OS_WIN */

Changes to SQLite.Interop/src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.14 2006/01/11 03:22:29 rmsimpson Exp $
           21  +** @(#) $Id: pager.c,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   #include "os.h"
    26     26   #include "pager.h"
    27     27   #include <assert.h>
    28     28   #include <string.h>
................................................................................
   280    280     int nRead,nWrite;           /* Database pages read/written */
   281    281   #endif
   282    282     void (*xDestructor)(void*,int); /* Call this routine when freeing pages */
   283    283     void (*xReiniter)(void*,int);   /* Call this routine when reloading pages */
   284    284     void (*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
   285    285     void *pCodecArg;            /* First argument to xCodec() */
   286    286     PgHdr *aHash[N_PG_HASH];    /* Hash table to map page number to PgHdr */
   287         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
          287  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   288    288     Pager *pNext;               /* Linked list of pagers in this thread */
   289    289   #endif
   290    290   };
   291    291   
   292    292   /*
   293    293   ** If SQLITE_TEST is defined then increment the variable given in
   294    294   ** the argument
................................................................................
  1614   1614     int i;
  1615   1615     int tempFile = 0;
  1616   1616     int memDb = 0;
  1617   1617     int readOnly = 0;
  1618   1618     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
  1619   1619     int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
  1620   1620     char zTemp[SQLITE_TEMPNAME_SIZE];
  1621         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  1622         -  ThreadData *pTsd = sqlite3ThreadData();
         1621  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
         1622  +  const ThreadData *pTsdro = sqlite3ThreadDataReadOnly();
  1623   1623   #endif
  1624   1624   
  1625   1625     /* If malloc() has already failed return SQLITE_NOMEM. Before even
  1626   1626     ** testing for this, set *ppPager to NULL so the caller knows the pager
  1627   1627     ** structure was never allocated. 
  1628   1628     */
  1629   1629     *ppPager = 0;
  1630         -  if( sqlite3ThreadData()->mallocFailed ){
         1630  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
  1631   1631       return SQLITE_NOMEM;
  1632   1632     }
  1633   1633     memset(&fd, 0, sizeof(fd));
  1634   1634   
  1635   1635     /* Open the pager file and set zFullPathname to point at malloc()ed 
  1636   1636     ** memory containing the complete filename (i.e. including the directory).
  1637   1637     */
................................................................................
  1716   1716     pPager->pFirstSynced = 0;
  1717   1717     pPager->pLast = 0;
  1718   1718     pPager->nExtra = FORCE_ALIGNMENT(nExtra);
  1719   1719     pPager->sectorSize = PAGER_SECTOR_SIZE;
  1720   1720     pPager->pBusyHandler = 0;
  1721   1721     memset(pPager->aHash, 0, sizeof(pPager->aHash));
  1722   1722     *ppPager = pPager;
  1723         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  1724         -  if( pTsd->useMemoryManagement ){
         1723  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
         1724  +  if( pTsdro->useMemoryManagement ){
         1725  +    ThreadData *pTsd = sqlite3ThreadData();
  1725   1726       pPager->pNext = pTsd->pPager;
  1726   1727       pTsd->pPager = pPager;
  1727   1728     }
  1728   1729   #endif
  1729   1730     return SQLITE_OK;
  1730   1731   }
  1731   1732   
................................................................................
  2023   2024   ** This function always succeeds. If a transaction is active an attempt
  2024   2025   ** is made to roll it back. If an error occurs during the rollback 
  2025   2026   ** a hot journal may be left in the filesystem but no error is returned
  2026   2027   ** to the caller.
  2027   2028   */
  2028   2029   int sqlite3pager_close(Pager *pPager){
  2029   2030     PgHdr *pPg, *pNext;
  2030         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  2031         -  ThreadData *pTsd = sqlite3ThreadData();
         2031  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
         2032  +  const ThreadData *pTsd = sqlite3ThreadDataReadOnly();
  2032   2033   #endif
  2033   2034   
  2034   2035     switch( pPager->state ){
  2035   2036       case PAGER_RESERVED:
  2036   2037       case PAGER_SYNCED: 
  2037   2038       case PAGER_EXCLUSIVE: {
  2038   2039         /* We ignore any IO errors that occur during the rollback
................................................................................
  2083   2084     sqlite3OsClose(&pPager->fd);
  2084   2085     /* Temp files are automatically deleted by the OS
  2085   2086     ** if( pPager->tempFile ){
  2086   2087     **   sqlite3OsDelete(pPager->zFilename);
  2087   2088     ** }
  2088   2089     */
  2089   2090   
  2090         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
         2091  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  2091   2092     /* Remove the pager from the linked list of pagers starting at 
  2092   2093     ** ThreadData.pPager if memory-management is enabled.
  2093   2094     */
  2094   2095     if( pTsd->useMemoryManagement ){
  2095   2096       if( pPager==pTsd->pPager ){
  2096   2097         pTsd->pPager = pPager->pNext;
  2097   2098       }else{
................................................................................
  2098   2099         Pager *pTmp;
  2099   2100         for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext);
  2100   2101         pTmp->pNext = pPager->pNext;
  2101   2102       }
  2102   2103     }
  2103   2104   #endif
  2104   2105   
         2106  +#ifdef SQLITE_HAS_CODEC
         2107  +  sqlite3pager_free_codecarg(pPager->pCodecArg);
         2108  +#endif
  2105   2109     sqliteFree(pPager);
  2106   2110     return SQLITE_OK;
  2107   2111   }
  2108   2112   
  2109   2113   /*
  2110   2114   ** Return the page number for the given page data.
  2111   2115   */
................................................................................
  2452   2456   ** by the current thread may be sqliteFree()ed.
  2453   2457   **
  2454   2458   ** nReq is the number of bytes of memory required. Once this much has
  2455   2459   ** been released, the function returns. A negative value for nReq means
  2456   2460   ** free as much memory as possible. The return value is the total number 
  2457   2461   ** of bytes of memory released.
  2458   2462   */
  2459         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
         2463  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  2460   2464   int sqlite3pager_release_memory(int nReq){
  2461         -  ThreadData *pTsd = sqlite3ThreadData();
         2465  +  const ThreadData *pTsdro = sqlite3ThreadDataReadOnly();
  2462   2466     Pager *p;
  2463   2467     int nReleased = 0;
  2464   2468     int i;
  2465   2469   
  2466   2470     /* If the the global mutex is held, this subroutine becomes a
  2467   2471     ** o-op; zero bytes of memory are freed.  This is because
  2468   2472     ** some of the code invoked by this function may also
................................................................................
  2477   2481     ** iteration (which only runs if the first failed to free nReq bytes of
  2478   2482     ** memory) is permitted to call fsync(). This is of course much more 
  2479   2483     ** expensive.
  2480   2484     */
  2481   2485     for(i=0; i<=1; i++){
  2482   2486   
  2483   2487       /* Loop through all the SQLite pagers opened by the current thread. */
  2484         -    for(p=pTsd->pPager; p && (nReq<0 || nReleased<nReq); p=p->pNext){
         2488  +    for(p=pTsdro->pPager; p && (nReq<0 || nReleased<nReq); p=p->pNext){
  2485   2489         PgHdr *pPg;
  2486   2490         int rc;
  2487   2491   
  2488   2492         /* For each pager, try to free as many pages as possible (without 
  2489   2493         ** calling fsync() if this is the first iteration of the outermost 
  2490   2494         ** loop).
  2491   2495         */
................................................................................
  2523   2527           pager_error(p, rc);
  2524   2528         }
  2525   2529       }
  2526   2530     }
  2527   2531   
  2528   2532     return nReleased;
  2529   2533   }
  2530         -#endif /* SQLITE_OMIT_MEMORY_MANAGEMENT */
         2534  +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
  2531   2535   
  2532   2536   /*
  2533   2537   ** Acquire a page.
  2534   2538   **
  2535   2539   ** A read lock on the disk file is obtained when the first page is acquired. 
  2536   2540   ** This read lock is dropped when the last page is released.
  2537   2541   **

Changes to SQLite.Interop/src/pager.h.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the sqlite page cache
    13     13   ** subsystem.  The page cache subsystem reads and writes a file a page
    14     14   ** at a time and provides a journal for rollback.
    15     15   **
    16         -** @(#) $Id: pager.h,v 1.14 2006/01/11 03:22:29 rmsimpson Exp $
           16  +** @(#) $Id: pager.h,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
    17     17   */
    18     18   
    19     19   #ifndef _PAGER_H_
    20     20   #define _PAGER_H_
    21     21   
    22     22   /*
    23     23   ** The default size of a database page.

Changes to SQLite.Interop/src/parse.c.

   171    171   **  yy_shift_ofst[]    For each state, the offset into yy_action for
   172    172   **                     shifting terminals.
   173    173   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
   174    174   **                     shifting non-terminals after a reduce.
   175    175   **  yy_default[]       Default action for each state.
   176    176   */
   177    177   static const YYACTIONTYPE yy_action[] = {
   178         - /*     0 */   280,   68,  284,   70,  148,  166,  547,  420,   62,   62,
   179         - /*    10 */    62,   62,  202,   64,   64,   64,   64,   65,   65,   66,
   180         - /*    20 */    66,   66,   67,   67,  549,  550,  433,   69,   64,   64,
   181         - /*    30 */    64,   64,   65,   65,   66,   66,   66,   67,   68,  455,
   182         - /*    40 */    70,  148,  500,   61,   59,  288,  441,  442,  438,  438,
   183         - /*    50 */    63,   63,   62,   62,   62,   62,  502,   64,   64,   64,
   184         - /*    60 */    64,   65,   65,   66,   66,   66,   67,  280,  372,  284,
   185         - /*    70 */   420,    2,  378,   80,  158,  115,  220,  305,  225,  306,
   186         - /*    80 */   170,  245,  857,  119,  560,  505,  204,    2,  246,  390,
   187         - /*    90 */   497,  219,   22,  433,  515,   21,  420,   58,  494,  171,
   188         - /*   100 */    64,   64,   64,   64,   65,   65,   66,   66,   66,   67,
   189         - /*   110 */    61,   59,  288,  441,  442,  438,  438,   63,   63,   62,
   190         - /*   120 */    62,   62,   62,  513,   64,   64,   64,   64,   65,   65,
   191         - /*   130 */    66,   66,   66,   67,  280,  379,  380,  175,  202,  378,
   192         - /*   140 */   331,  334,  335,  220,  305,  225,  306,  170,  245,  203,
   193         - /*   150 */   146,  358,  336,  282,  378,  246,   55,  302,  374,  420,
   194         - /*   160 */   433,  506,   92,  200,  531,   66,   66,   66,   67,  526,
   195         - /*   170 */   192,   65,   65,   66,   66,   66,   67,   61,   59,  288,
   196         - /*   180 */   441,  442,  438,  438,   63,   63,   62,   62,   62,   62,
   197         - /*   190 */   434,   64,   64,   64,   64,   65,   65,   66,   66,   66,
   198         - /*   200 */    67,  280,  379,  380,  412,  432,  110,  226,  428,  205,
   199         - /*   210 */   436,  437,  309,  359,  262,  261,  175,  379,  380,  331,
   200         - /*   220 */   334,  335,  373,  370,  202,  512,  481,  433,  548,  363,
   201         - /*   230 */   467,  336,  511,  501,  411,   41,  277,  415,  435,  430,
   202         - /*   240 */   504,  162,  233,  528,   61,   59,  288,  441,  442,  438,
   203         - /*   250 */   438,   63,   63,   62,   62,   62,   62,  320,   64,   64,
   204         - /*   260 */    64,   64,   65,   65,   66,   66,   66,   67,  280,  473,
   205         - /*   270 */   417,  417,  417,  309,  323,  236,  309,   68,  309,   70,
   206         - /*   280 */   148,    1,  309,  794,  309,  378,   68,  153,   70,  148,
   207         - /*   290 */   149,  378,  326,  283,  433,  411,   35,  552,  411,   35,
   208         - /*   300 */   411,   36,  428,  205,  411,   35,  411,   35,  287,  423,
   209         - /*   310 */   424,   61,   59,  288,  441,  442,  438,  438,   63,   63,
   210         - /*   320 */    62,   62,   62,   62,  412,   64,   64,   64,   64,   65,
   211         - /*   330 */    65,   66,   66,   66,   67,  309,  505,  467,  291,  255,
   212         - /*   340 */   280,  325,  486,  147,  237,  389,   21,  289,  379,  380,
   213         - /*   350 */   452,  420,  232,  452,  379,  380,  309,  411,   28,  452,
   214         - /*   360 */   175,  451,  487,  331,  334,  335,  433,  215,  348,  145,
   215         - /*   370 */   514,  204,  351,  186,  168,  336,  238,  412,  411,   41,
   216         - /*   380 */   256,  463,   76,   61,   59,  288,  441,  442,  438,  438,
   217         - /*   390 */    63,   63,   62,   62,   62,   62,  310,   64,   64,   64,
   218         - /*   400 */    64,   65,   65,   66,   66,   66,   67,  412,  412,  186,
   219         - /*   410 */   397,  309,  280,  292,  420,  339,  477,  309,  391,  234,
   220         - /*   420 */   169,  154,  398,  476,  397,  328,  494,  312,  423,  424,
   221         - /*   430 */   445,  378,  357,  411,   49,  399,  398,  395,  433,  411,
   222         - /*   440 */    49,  503,  171,  412,  430,  313,  162,  396,  352,  399,
   223         - /*   450 */   498,  319,  471,  353,   79,   61,   59,  288,  441,  442,
   224         - /*   460 */   438,  438,   63,   63,   62,   62,   62,   62,  357,   64,
   225         - /*   470 */    64,   64,   64,   65,   65,   66,   66,   66,   67,  280,
   226         - /*   480 */   299,  446,  377,  480,  533,  406,  300,   11,  505,  353,
   227         - /*   490 */   204,  378,  407,  378,  379,  380,  282,  557,   21,  492,
   228         - /*   500 */   492,  246,  561,  373,  370,  433,  393,  394,  315,  123,
   229         - /*   510 */   444,  444,  166,  290,  420,  315,  116,  444,  444,  251,
   230         - /*   520 */   265,  464,   61,   59,  288,  441,  442,  438,  438,   63,
   231         - /*   530 */    63,   62,   62,   62,   62,  293,   64,   64,   64,   64,
   232         - /*   540 */    65,   65,   66,   66,   66,   67,  280,  460,  329,  475,
   233         - /*   550 */   499,  309,  202,  309,  379,  380,  379,  380,  181,  131,
   234         - /*   560 */   179,  266,  309,    5,  309,  364,  315,  356,  444,  444,
   235         - /*   570 */   411,    3,  433,  411,   29,  411,   24,  420,  243,  244,
   236         - /*   580 */   381,  382,  383,  405,  411,   33,  411,   54,  467,   61,
   237         - /*   590 */    59,  288,  441,  442,  438,  438,   63,   63,   62,   62,
   238         - /*   600 */    62,   62,  309,   64,   64,   64,   64,   65,   65,   66,
   239         - /*   610 */    66,   66,   67,  280,  522,  345,  522,  249,  309,  492,
   240         - /*   620 */   309,  471,  309,  471,  411,   25,  309,  240,  309,  315,
   241         - /*   630 */   309,  444,  444,  213,  172,  173,  174,  142,  267,  433,
   242         - /*   640 */   411,   52,  411,   97,  411,   94,  529,  394,  411,   99,
   243         - /*   650 */   411,  100,  411,  111,  212,  255,   61,   59,  288,  441,
   244         - /*   660 */   442,  438,  438,   63,   63,   62,   62,   62,   62,  309,
   245         - /*   670 */    64,   64,   64,   64,   65,   65,   66,   66,   66,   67,
   246         - /*   680 */   280,  309,  346,  188,  298,   91,  309,  492,  309,  416,
   247         - /*   690 */   309,  411,  112,  309,  429,  309,  538,  309,  244,  165,
   248         - /*   700 */   154,  410,  356,  411,   18,  409,  433,  321,  411,   98,
   249         - /*   710 */   411,   34,  411,   95,  314,  411,   53,  411,  113,  411,
   250         - /*   720 */   114,  255,  294,   61,   59,  288,  441,  442,  438,  438,
   251         - /*   730 */    63,   63,   62,   62,   62,   62,  309,   64,   64,   64,
   252         - /*   740 */    64,   65,   65,   66,   66,   66,   67,  280,  309,  492,
   253         - /*   750 */   492,  524,  309,  453,  309,  523,  309,  462,  411,   26,
   254         - /*   760 */   309,   75,  540,   77,  309,  461,  244,  347,  214,  466,
   255         - /*   770 */   411,   37,  470,  433,  411,   38,  411,   27,  411,   39,
   256         - /*   780 */   242,   82,  411,   40,  295,  297,  411,   42,  439,  330,
   257         - /*   790 */    61,   59,  288,  441,  442,  438,  438,   63,   63,   62,
   258         - /*   800 */    62,   62,   62,  309,   64,   64,   64,   64,   65,   65,
   259         - /*   810 */    66,   66,   66,   67,  280,  309,  410,  190,  221,  309,
   260         - /*   820 */   409,  309,  152,  309,  159,  411,   43,  309,  244,  244,
   261         - /*   830 */   222,   20,  309,  139,  426,  426,  482,  411,   44,  483,
   262         - /*   840 */   433,  411,   30,  411,   31,  411,   45,  488,  462,  411,
   263         - /*   850 */    46,  412,  507,  255,  411,   47,  489,   61,   71,  288,
   264         - /*   860 */   441,  442,  438,  438,   63,   63,   62,   62,   62,   62,
   265         - /*   870 */   309,   64,   64,   64,   64,   65,   65,   66,   66,   66,
   266         - /*   880 */    67,  280,  309,  402,  403,  250,  309,  193,  309,  421,
   267         - /*   890 */   309,   23,  411,   48,  541,  450,  255,   14,  469,  478,
   268         - /*   900 */   167,   14,  485,  484,  411,   32,  252,  433,  411,   12,
   269         - /*   910 */   411,   50,  411,   51,  255,  255,  595,  255,  255,  150,
   270         - /*   920 */   490,  412,  123,  253,  280,   59,  288,  441,  442,  438,
   271         - /*   930 */   438,   63,   63,   62,   62,   62,   62,  542,   64,   64,
   272         - /*   940 */    64,   64,   65,   65,   66,   66,   66,   67,  254,  248,
   273         - /*   950 */   433,  123,  338,  412,  123,  268,  270,  196,  362,  367,
   274         - /*   960 */   183,  177,  180,  520,  521,  260,  535,  123,  167,  288,
   275         - /*   970 */   441,  442,  438,  438,   63,   63,   62,   62,   62,   62,
   276         - /*   980 */   343,   64,   64,   64,   64,   65,   65,   66,   66,   66,
   277         - /*   990 */    67,   72,  316,  259,    4,  412,  412,  536,  286,   89,
   278         - /*  1000 */   545,  350,   89,  354,  355,   19,  311,   72,  316,  369,
   279         - /*  1010 */     4,  387,  263,  264,  286,  223,  546,  271,  365,  274,
   280         - /*  1020 */   275,  141,  311,  318,  227,  317,  556,  425,  427,  481,
   281         - /*  1030 */   456,  459,  491,  432,  333,  493,  534,  157,  544,  318,
   282         - /*  1040 */   376,  384,  385,  386,    8,  303,  304,  392,  285,  432,
   283         - /*  1050 */   405,  400,   74,   73,  224,  404,  408,   82,  324,  322,
   284         - /*  1060 */    72,  307,  308,  401,  231,  415,   81,  206,   74,   73,
   285         - /*  1070 */   474,   57,   78,  164,  454,  413,   72,  307,  308,   72,
   286         - /*  1080 */   316,  415,    4,  228,  202,  229,  286,  235,  230,  457,
   287         - /*  1090 */   458,  414,  207,  120,  311,   83,  327,  102,  417,  417,
   288         - /*  1100 */   417,  418,  419,   13,  239,  496,  468,  241,  278,  208,
   289         - /*  1110 */   472,  318,  495,  210,  417,  417,  417,  418,  419,   13,
   290         - /*  1120 */   211,  432,  156,  279,  340,  508,  509,  216,  217,  218,
   291         - /*  1130 */   106,  510,  516,  178,  344,   84,  342,  182,  518,  457,
   292         - /*  1140 */    74,   73,   86,  198,  519,  272,  257,  184,   72,  307,
   293         - /*  1150 */   308,  349,  273,  415,  527,  530,  118,  187,  127,  537,
   294         - /*  1160 */   360,  136,  128,  543,  195,  129,  531,  301,  553,   90,
   295         - /*  1170 */   554,  194,  137,  197,  432,  130,  133,  555,  558,   96,
   296         - /*  1180 */   209,  101,  375,  388,  117,  201,  417,  417,  417,  418,
   297         - /*  1190 */   419,   13,   93,  143,  144,   56,  596,  109,  597,  160,
   298         - /*  1200 */   161,   60,  501,  422,  431,  440,  415,  443,  138,  447,
   299         - /*  1210 */   151,    6,  448,  449,    7,  361,  269,  261,  163,   15,
   300         - /*  1220 */   465,  281,   14,  121,  155,  122,  202,  479,  103,  332,
   301         - /*  1230 */   247,  104,   85,  105,  337,  222,  176,  341,  140,  417,
   302         - /*  1240 */   417,  417,  517,  124,  296,  167,  125,  525,  185,  107,
   303         - /*  1250 */   366,  258,    9,  532,   10,  126,   16,  189,  539,  191,
   304         - /*  1260 */   132,   87,   88,  134,  135,   17,  108,  276,  551,  371,
   305         - /*  1270 */   536,  199,  368,  559,
          178  + /*     0 */   281,   67,  344,   80,  150,  157,  225,  517,   92,   92,
          179  + /*    10 */    92,   92,  288,   59,   59,   59,   59,   58,   58,   57,
          180  + /*    20 */    57,   57,   65,  346,  478,   48,  544,   86,   59,   59,
          181  + /*    30 */    59,   59,   58,   58,   57,   57,   57,   65,   67,  481,
          182  + /*    40 */    80,  150,  231,   64,   88,  295,  525,  518,  530,  530,
          183  + /*    50 */    72,   72,   92,   92,   92,   92,  219,   59,   59,   59,
          184  + /*    60 */    59,   58,   58,   57,   57,   57,   65,  281,  252,  189,
          185  + /*    70 */   517,  296,  344,   81,   59,   59,   59,   59,   58,   58,
          186  + /*    80 */    57,   57,   57,   65,  166,  115,  246,  303,  264,  323,
          187  + /*    90 */   196,  236,  158,  544,  478,   49,  387,   69,  228,  857,
          188  + /*   100 */   128,  560,  247,   56,    2,  381,   22,  476,  456,  363,
          189  + /*   110 */    64,   88,  295,  525,  518,  530,  530,   72,   72,   92,
          190  + /*   120 */    92,   92,   92,  283,   59,   59,   59,   59,   58,   58,
          191  + /*   130 */    57,   57,   57,   65,  281,  344,  279,  386,  395,  553,
          192  + /*   140 */   388,  165,  386,  276,  361,  288,  223,  439,  520,  451,
          193  + /*   150 */   344,   58,   58,   57,   57,   57,   65,  478,   38,  365,
          194  + /*   160 */   544,  145,  143,  282,   67,  393,   80,  150,  451,  500,
          195  + /*   170 */   393,  189,  478,   36,  500,  424,  425,   64,   88,  295,
          196  + /*   180 */   525,  518,  530,  530,   72,   72,   92,   92,   92,   92,
          197  + /*   190 */    71,   59,   59,   59,   59,   58,   58,   57,   57,   57,
          198  + /*   200 */    65,  281,  508,  508,  508,  429,  146,  508,  508,  508,
          199  + /*   210 */   294,  512,  507,  198,  344,  520,  350,  357,  362,  246,
          200  + /*   220 */   303,  264,  323,  196,  236,  283,  221,  544,  367,  310,
          201  + /*   230 */   228,  228,   57,   57,   57,   65,  478,   48,  308,  299,
          202  + /*   240 */   534,  537,  306,  417,   64,   88,  295,  525,  518,  530,
          203  + /*   250 */   530,   72,   72,   92,   92,   92,   92,  551,   59,   59,
          204  + /*   260 */    59,   59,   58,   58,   57,   57,   57,   65,  281,  550,
          205  + /*   270 */   202,  416,  198,  375,  520,  350,  357,  362,  512,  507,
          206  + /*   280 */   413,  414,  344,  293,  344,  372,   21,  367,  198,  522,
          207  + /*   290 */   517,  350,  357,  362,  544,  359,  539,  371,  374,  126,
          208  + /*   300 */   403,  209,  200,  367,  478,   18,  478,   43,  194,  169,
          209  + /*   310 */   384,   64,   88,  295,  525,  518,  530,  530,   72,   72,
          210  + /*   320 */    92,   92,   92,   92,  232,   59,   59,   59,   59,   58,
          211  + /*   330 */    58,   57,   57,   57,   65,  511,  344,  512,  507,  427,
          212  + /*   340 */   281,  420,  479,  479,  148,  419,  331,  233,  344,   67,
          213  + /*   350 */   344,   80,  150,  517,  344,  176,  155,  309,  478,   32,
          214  + /*   360 */   315,  436,  149,  184,  464,  195,  544,  348,   78,  533,
          215  + /*   370 */   478,   51,  478,   51,  378,  428,  478,   51,  479,  443,
          216  + /*   380 */   158,  377,   85,   64,   88,  295,  525,  518,  530,  530,
          217  + /*   390 */    72,   72,   92,   92,   92,   92,  479,   59,   59,   59,
          218  + /*   400 */    59,   58,   58,   57,   57,   57,   65,  281,  387,  467,
          219  + /*   410 */   504,  162,   77,  324,  344,  290,  521,  457,   22,  300,
          220  + /*   420 */   353,  520,  513,  513,  432,  487,   14,  487,  329,  271,
          221  + /*   430 */   257,  487,  426,  544,  523,  524,  478,   51,  217,  208,
          222  + /*   440 */   206,  144,  380,  355,  534,  537,  353,   55,  513,  513,
          223  + /*   450 */    64,   88,  295,  525,  518,  530,  530,   72,   72,   92,
          224  + /*   460 */    92,   92,   92,  375,   59,   59,   59,   59,   58,   58,
          225  + /*   470 */    57,   57,   57,   65,  281,  372,   11,  127,   71,  218,
          226  + /*   480 */   520,  241,  182,  126,  512,  507,  164,  155,  374,  520,
          227  + /*   490 */   338,  488,  452,  398,  543,  353,  200,  513,  513,    1,
          228  + /*   500 */   544,  401,  353,  520,  513,  513,  200,  553,  366,  165,
          229  + /*   510 */   157,  463,  517,  358,  540,  538,  479,   64,   88,  295,
          230  + /*   520 */   525,  518,  530,  530,   72,   72,   92,   92,   92,   92,
          231  + /*   530 */   468,   59,   59,   59,   59,   58,   58,   57,   57,   57,
          232  + /*   540 */    65,  370,  541,  512,  507,  437,  281,  344,  172,  506,
          233  + /*   550 */   239,  344,  512,  507,  344,  387,  224,  448,   93,  344,
          234  + /*   560 */    89,  344,  313,  344,  555,   22,  512,  507,  182,  478,
          235  + /*   570 */    27,  520,  544,  478,   43,  517,  478,   50,  561,  547,
          236  + /*   580 */   369,  478,   47,  478,  114,  478,   95,  528,   91,   64,
          237  + /*   590 */    88,  295,  525,  518,  530,  530,   72,   72,   92,   92,
          238  + /*   600 */    92,   92,  479,   59,   59,   59,   59,   58,   58,   57,
          239  + /*   610 */    57,   57,   65,  281,  226,  344,  251,  174,  110,  344,
          240  + /*   620 */   141,  147,  344,  465,  344,  449,  325,  370,  270,  344,
          241  + /*   630 */   421,  344,  450,  554,  512,  507,    2,  478,   52,  544,
          242  + /*   640 */   595,  478,   44,  311,  478,   30,  478,   45,  313,  173,
          243  + /*   650 */   418,  478,   53,  478,   25,  479,   64,   88,  295,  525,
          244  + /*   660 */   518,  530,  530,   72,   72,   92,   92,   92,   92,  344,
          245  + /*   670 */    59,   59,   59,   59,   58,   58,   57,   57,   57,   65,
          246  + /*   680 */   281,  344,  404,  479,  433,  344,  470,  344,  152,  344,
          247  + /*   690 */   469,  478,  112,  344,  415,  314,  415,  344,  411,  344,
          248  + /*   700 */   126,  287,  161,  478,   94,  440,  544,  478,  113,  478,
          249  + /*   710 */    12,  478,   99,  401,  465,  478,   41,  292,  456,  478,
          250  + /*   720 */   100,  478,   46,   64,   88,  295,  525,  518,  530,  530,
          251  + /*   730 */    72,   72,   92,   92,   92,   92,  344,   59,   59,   59,
          252  + /*   740 */    59,   58,   58,   57,   57,   57,   65,  281,  344,  485,
          253  + /*   750 */    19,  404,  344,  514,  344,   79,  307,  260,  478,  111,
          254  + /*   760 */   344,  242,  344,  548,  548,  344,  503,  501,  497,  466,
          255  + /*   770 */   478,   42,  404,  544,  478,   26,  478,   39,  478,    3,
          256  + /*   780 */   304,  423,  478,   31,  478,   40,  291,  478,   37,  305,
          257  + /*   790 */    64,   88,  295,  525,  518,  530,  530,   72,   72,   92,
          258  + /*   800 */    92,   92,   92,  344,   59,   59,   59,   59,   58,   58,
          259  + /*   810 */    57,   57,   57,   65,  281,  344,  470,  404,  126,  344,
          260  + /*   820 */   469,  344,   20,  344,  139,  478,   97,  344,  320,    7,
          261  + /*   830 */   242,  344,  190,  181,  180,  208,  451,  478,   28,  258,
          262  + /*   840 */   544,  478,   54,  478,   35,  478,   33,  222,  327,  478,
          263  + /*   850 */    34,  262,  204,  478,   29,  435,  191,   64,   76,  295,
          264  + /*   860 */   525,  518,  530,  530,   72,   72,   92,   92,   92,   92,
          265  + /*   870 */   404,   59,   59,   59,   59,   58,   58,   57,   57,   57,
          266  + /*   880 */    65,  281,  404,  454,  177,  162,  344,  208,  344,  175,
          267  + /*   890 */   479,  320,  447,  235,  211,  794,  242,  286,  456,  516,
          268  + /*   900 */   352,  441,  409,  410,  409,  298,  385,  544,  478,   24,
          269  + /*   910 */   478,   98,  252,  252,  252,  252,  275,  284,  479,  252,
          270  + /*   920 */   334,  252,  252,  479,  281,   88,  295,  525,  518,  530,
          271  + /*   930 */   530,   72,   72,   92,   92,   92,   92,  274,   59,   59,
          272  + /*   940 */    59,   59,   58,   58,   57,   57,   57,   65,  517,  242,
          273  + /*   950 */   544,  244,  252,  237,  340,  215,  494,  214,  390,  546,
          274  + /*   960 */   492,  242,  256,  489,  475,  406,   79,  397,  273,  295,
          275  + /*   970 */   525,  518,  530,  530,   72,   72,   92,   92,   92,   92,
          276  + /*   980 */   116,   59,   59,   59,   59,   58,   58,   57,   57,   57,
          277  + /*   990 */    65,   62,  345,  484,    4,  407,  412,  269,  289,  126,
          278  + /*  1000 */   519,  259,   23,  550,  202,  552,  349,   62,  345,  549,
          279  + /*  1010 */     4,  517,  354,  493,  289,   14,  547,  369,  402,  316,
          280  + /*  1020 */   240,  453,  349,  339,  472,  356,  142,  266,  471,  477,
          281  + /*  1030 */   249,  319,  505,  386,  459,  343,  529,  428,  255,  339,
          282  + /*  1040 */    71,  458,  499,  118,  333,  130,  121,  192,  389,  386,
          283  + /*  1050 */   123,  156,   60,   61,  483,  103,   87,  125,  212,  480,
          284  + /*  1060 */    62,  328,  330,  178,  277,  500,  229,  210,   60,   61,
          285  + /*  1070 */   438,  297,  399,  491,  476,  473,   62,  328,  330,   62,
          286  + /*  1080 */   345,  500,    4,  474,  208,  302,  289,  342,  207,  186,
          287  + /*  1090 */   498,   68,  278,  120,  349,  136,  400,  556,  508,  508,
          288  + /*  1100 */   508,  509,  510,   17,  312,  106,  243,  326,  205,  245,
          289  + /*  1110 */   373,  339,  434,  285,  508,  508,  508,  509,  510,   17,
          290  + /*  1120 */    74,  386,  160,  431,  248,    8,  321,  227,  220,  230,
          291  + /*  1130 */   102,  332,  137,  382,  383,  536,  405,  234,   75,  183,
          292  + /*  1140 */    60,   61,  317,  170,  265,  254,  135,  336,   62,  328,
          293  + /*  1150 */   330,  442,  267,  500,  263,   66,  318,  261,  201,  455,
          294  + /*  1160 */   447,   73,  461,  408,  168,  531,  443,   83,  482,  446,
          295  + /*  1170 */   376,  171,  396,  167,  444,  542,  545,  208,  238,  272,
          296  + /*  1180 */   213,  163,  188,  101,  364,   96,  508,  508,  508,  509,
          297  + /*  1190 */   510,   17,  490,   63,  270,  322,  153,  105,  335,  535,
          298  + /*  1200 */   526,  108,  558,  394,  527,  532,  250,  515,  379,  391,
          299  + /*  1210 */    13,  368,  557,  107,  351,  337,  216,  257,   82,  132,
          300  + /*  1220 */   559,  280,  109,  179,  347,  140,  208,  159,   65,  185,
          301  + /*  1230 */   502,  341,  268,  193,  392,  131,  129,  203,  496,  151,
          302  + /*  1240 */    10,  104,  154,  430,  486,  138,  253,  199,  495,  422,
          303  + /*  1250 */   360,  162,  445,    5,   15,  597,    9,  187,  117,  122,
          304  + /*  1260 */   596,  119,  133,   16,    6,  124,  301,  134,   14,   90,
          305  + /*  1270 */    70,  462,   84,  460,  197,
   306    306   };
   307    307   static const YYCODETYPE yy_lookahead[] = {
   308         - /*     0 */    16,  216,   16,  218,  219,   21,  146,   23,   68,   69,
   309         - /*    10 */    70,   71,  109,   73,   74,   75,   76,   77,   78,   79,
   310         - /*    20 */    80,   81,   82,   82,  164,  165,   42,   72,   73,   74,
          308  + /*     0 */    16,  216,  146,  218,  219,   21,  146,   23,   68,   69,
          309  + /*    10 */    70,   71,   16,   73,   74,   75,   76,   77,   78,   79,
          310  + /*    20 */    80,   81,   82,  146,  168,  169,   42,   72,   73,   74,
   311    311    /*    30 */    75,   76,   77,   78,   79,   80,   81,   82,  216,  217,
   312         - /*    40 */   218,  219,  168,   59,   60,   61,   62,   63,   64,   65,
   313         - /*    50 */    66,   67,   68,   69,   70,   71,  168,   73,   74,   75,
   314         - /*    60 */    76,   77,   78,   79,   80,   81,   82,   16,  140,   16,
   315         - /*    70 */    86,  143,   23,   22,   88,   89,   90,   91,   92,   93,
   316         - /*    80 */    94,   95,  138,  139,  140,  146,  226,  143,  102,  166,
   317         - /*    90 */   167,  152,   19,   42,  155,  156,   23,   46,  175,   43,
   318         - /*   100 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
          312  + /*    40 */   218,  219,  146,   59,   60,   61,   62,   63,   64,   65,
          313  + /*    50 */    66,   67,   68,   69,   70,   71,  146,   73,   74,   75,
          314  + /*    60 */    76,   77,   78,   79,   80,   81,   82,   16,  146,   43,
          315  + /*    70 */    86,  215,  146,   22,   73,   74,   75,   76,   77,   78,
          316  + /*    80 */    79,   80,   81,   82,   88,   89,   90,   91,   92,   93,
          317  + /*    90 */    94,   95,   22,   42,  168,  169,  146,   46,  102,  138,
          318  + /*   100 */   139,  140,  152,   19,  143,  155,  156,   23,  160,  187,
   319    319    /*   110 */    59,   60,   61,   62,   63,   64,   65,   66,   67,   68,
   320         - /*   120 */    69,   70,   71,  180,   73,   74,   75,   76,   77,   78,
   321         - /*   130 */    79,   80,   81,   82,   16,   86,   87,   88,  109,   23,
   322         - /*   140 */    91,   92,   93,   90,   91,   92,   93,   94,   95,  191,
   323         - /*   150 */    22,  122,  103,   97,   23,  102,  198,  141,  142,   86,
   324         - /*   160 */    42,  180,   44,  147,   49,   79,   80,   81,   82,   18,
   325         - /*   170 */   154,   77,   78,   79,   80,   81,   82,   59,   60,   61,
          320  + /*   120 */    69,   70,   71,   97,   73,   74,   75,   76,   77,   78,
          321  + /*   130 */    79,   80,   81,   82,   16,  146,  157,   58,   20,  160,
          322  + /*   140 */   161,  162,   58,  189,   16,   16,  220,  199,   23,  146,
          323  + /*   150 */   146,   77,   78,   79,   80,   81,   82,  168,  169,  237,
          324  + /*   160 */    42,   77,   78,  149,  216,   86,  218,  219,  146,   90,
          325  + /*   170 */    86,   43,  168,  169,   90,   89,   90,   59,   60,   61,
   326    326    /*   180 */    62,   63,   64,   65,   66,   67,   68,   69,   70,   71,
   327         - /*   190 */    42,   73,   74,   75,   76,   77,   78,   79,   80,   81,
   328         - /*   200 */    82,   16,   86,   87,  188,   58,   21,  189,   77,   78,
   329         - /*   210 */    62,   63,  146,   98,   99,  100,   88,   86,   87,   91,
   330         - /*   220 */    92,   93,    1,    2,  109,  175,  176,   42,   97,  213,
   331         - /*   230 */   160,  103,  182,   86,  168,  169,  157,   90,   90,  160,
   332         - /*   240 */   161,  162,  146,   92,   59,   60,   61,   62,   63,   64,
   333         - /*   250 */    65,   66,   67,   68,   69,   70,   71,  185,   73,   74,
   334         - /*   260 */    75,   76,   77,   78,   79,   80,   81,   82,   16,  199,
   335         - /*   270 */   123,  124,  125,  146,  208,  209,  146,  216,  146,  218,
   336         - /*   280 */   219,   19,  146,  132,  146,   23,  216,  146,  218,  219,
   337         - /*   290 */   154,   23,  146,  149,   42,  168,  169,  236,  168,  169,
   338         - /*   300 */   168,  169,   77,   78,  168,  169,  168,  169,  163,  164,
   339         - /*   310 */   165,   59,   60,   61,   62,   63,   64,   65,   66,   67,
   340         - /*   320 */    68,   69,   70,   71,  188,   73,   74,   75,   76,   77,
   341         - /*   330 */    78,   79,   80,   81,   82,  146,  146,  160,  211,  146,
   342         - /*   340 */    16,  211,   30,  154,  146,  155,  156,  211,   86,   87,
   343         - /*   350 */   223,   23,  220,  223,   86,   87,  146,  168,  169,  223,
   344         - /*   360 */    88,  223,   50,   91,   92,   93,   42,  144,  224,  179,
   345         - /*   370 */   180,  226,  228,  154,  154,  103,  199,  188,  168,  169,
   346         - /*   380 */   187,  113,  130,   59,   60,   61,   62,   63,   64,   65,
   347         - /*   390 */    66,   67,   68,   69,   70,   71,  146,   73,   74,   75,
   348         - /*   400 */    76,   77,   78,   79,   80,   81,   82,  188,  188,  154,
   349         - /*   410 */    12,  146,   16,  101,   86,   16,   20,  146,  167,  209,
   350         - /*   420 */   200,  201,   24,   20,   12,  205,  175,  163,  164,  165,
   351         - /*   430 */    20,   23,  213,  168,  169,   37,   24,   39,   42,  168,
   352         - /*   440 */   169,  159,   43,  188,  160,  161,  162,   49,  229,   37,
   353         - /*   450 */   168,   39,  146,  234,  130,   59,   60,   61,   62,   63,
   354         - /*   460 */    64,   65,   66,   67,   68,   69,   70,   71,  213,   73,
   355         - /*   470 */    74,   75,   76,   77,   78,   79,   80,   81,   82,   16,
   356         - /*   480 */   215,   20,  146,   20,  229,   27,  215,   19,  146,  234,
   357         - /*   490 */   226,   23,   34,   23,   86,   87,   97,  155,  156,  146,
   358         - /*   500 */   146,  102,    0,    1,    2,   42,  184,  185,  105,   22,
   359         - /*   510 */   107,  108,   21,  207,   23,  105,  146,  107,  108,   14,
   360         - /*   520 */    14,  113,   59,   60,   61,   62,   63,   64,   65,   66,
   361         - /*   530 */    67,   68,   69,   70,   71,  181,   73,   74,   75,   76,
   362         - /*   540 */    77,   78,   79,   80,   81,   82,   16,   22,  146,   79,
   363         - /*   550 */    20,  146,  109,  146,   86,   87,   86,   87,   53,   53,
   364         - /*   560 */    55,   55,  146,  190,  146,  122,  105,  146,  107,  108,
   365         - /*   570 */   168,  169,   42,  168,  169,  168,  169,   86,  225,  225,
   366         - /*   580 */     7,    8,    9,   96,  168,  169,  168,  169,  160,   59,
          327  + /*   190 */   120,   73,   74,   75,   76,   77,   78,   79,   80,   81,
          328  + /*   200 */    82,   16,  123,  124,  125,   20,   22,  123,  124,  125,
          329  + /*   210 */   207,   86,   87,   88,  146,   23,   91,   92,   93,   90,
          330  + /*   220 */    91,   92,   93,   94,   95,   97,  146,   42,  103,  207,
          331  + /*   230 */   102,  102,   79,   80,   81,   82,  168,  169,  224,  163,
          332  + /*   240 */   164,  165,  228,  177,   59,   60,   61,   62,   63,   64,
          333  + /*   250 */    65,   66,   67,   68,   69,   70,   71,  146,   73,   74,
          334  + /*   260 */    75,   76,   77,   78,   79,   80,   81,   82,   16,   77,
          335  + /*   270 */    78,  177,   88,   12,   23,   91,   92,   93,   86,   87,
          336  + /*   280 */    51,   52,  146,  215,  146,   24,   19,  103,   88,   97,
          337  + /*   290 */    23,   91,   92,   93,   42,  141,  142,   20,   37,   22,
          338  + /*   300 */    39,  147,  226,  103,  168,  169,  168,  169,  154,  154,
          339  + /*   310 */    49,   59,   60,   61,   62,   63,   64,   65,   66,   67,
          340  + /*   320 */    68,   69,   70,   71,   14,   73,   74,   75,   76,   77,
          341  + /*   330 */    78,   79,   80,   81,   82,   20,  146,   86,   87,  146,
          342  + /*   340 */    16,   25,  188,  188,  154,   29,  208,  209,  146,  216,
          343  + /*   350 */   146,  218,  219,   86,  146,  200,  201,   41,  168,  169,
          344  + /*   360 */   205,   20,  154,   53,  113,   55,   42,  213,   44,  236,
          345  + /*   370 */   168,  169,  168,  169,  175,  176,  168,  169,  188,   49,
          346  + /*   380 */    22,  182,  130,   59,   60,   61,   62,   63,   64,   65,
          347  + /*   390 */    66,   67,   68,   69,   70,   71,  188,   73,   74,   75,
          348  + /*   400 */    76,   77,   78,   79,   80,   81,   82,   16,  146,   20,
          349  + /*   410 */    20,   22,   21,  211,  146,  211,  146,  155,  156,  211,
          350  + /*   420 */   105,   23,  107,  108,   20,  223,   22,  223,   98,   99,
          351  + /*   430 */   100,  223,  177,   42,  164,  165,  168,  169,   14,  109,
          352  + /*   440 */   191,  179,  180,  163,  164,  165,  105,  198,  107,  108,
          353  + /*   450 */    59,   60,   61,   62,   63,   64,   65,   66,   67,   68,
          354  + /*   460 */    69,   70,   71,   12,   73,   74,   75,   76,   77,   78,
          355  + /*   470 */    79,   80,   81,   82,   16,   24,   19,   53,  120,   55,
          356  + /*   480 */    23,   20,  154,   22,   86,   87,  200,  201,   37,   23,
          357  + /*   490 */    39,  223,  166,  167,   42,  105,  226,  107,  108,   19,
          358  + /*   500 */    42,  175,  105,   23,  107,  108,  226,  160,  161,  162,
          359  + /*   510 */    21,  113,   23,   16,   62,   63,  188,   59,   60,   61,
          360  + /*   520 */    62,   63,   64,   65,   66,   67,   68,   69,   70,   71,
          361  + /*   530 */    22,   73,   74,   75,   76,   77,   78,   79,   80,   81,
          362  + /*   540 */    82,  213,   90,   86,   87,   79,   16,  146,   19,  146,
          363  + /*   550 */    20,  146,   86,   87,  146,  146,  132,  229,  129,  146,
          364  + /*   560 */   131,  146,  234,  146,  155,  156,   86,   87,  154,  168,
          365  + /*   570 */   169,   23,   42,  168,  169,   86,  168,  169,    0,    1,
          366  + /*   580 */     2,  168,  169,  168,  169,  168,  169,   90,  130,   59,
   367    367    /*   590 */    60,   61,   62,   63,   64,   65,   66,   67,   68,   69,
   368         - /*   600 */    70,   71,  146,   73,   74,   75,   76,   77,   78,   79,
   369         - /*   610 */    80,   81,   82,   16,   98,   99,  100,   20,  146,  146,
   370         - /*   620 */   146,  146,  146,  146,  168,  169,  146,  199,  146,  105,
   371         - /*   630 */   146,  107,  108,  212,   98,   99,  100,  112,  132,   42,
   372         - /*   640 */   168,  169,  168,  169,  168,  169,  184,  185,  168,  169,
   373         - /*   650 */   168,  169,  168,  169,  181,  146,   59,   60,   61,   62,
          368  + /*   600 */    70,   71,  188,   73,   74,   75,   76,   77,   78,   79,
          369  + /*   610 */    80,   81,   82,   16,  209,  146,   90,  230,   21,  146,
          370  + /*   620 */   112,  154,  146,   22,  146,   27,  231,  213,  102,  146,
          371  + /*   630 */    30,  146,   34,  140,   86,   87,  143,  168,  169,   42,
          372  + /*   640 */   111,  168,  169,  229,  168,  169,  168,  169,  234,  154,
          373  + /*   650 */    50,  168,  169,  168,  169,  188,   59,   60,   61,   62,
   374    374    /*   660 */    63,   64,   65,   66,   67,   68,   69,   70,   71,  146,
   375    375    /*   670 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
   376         - /*   680 */    16,  146,  207,   22,  207,   21,  146,  146,  146,  146,
   377         - /*   690 */   146,  168,  169,  146,  160,  146,  187,  146,  225,  200,
   378         - /*   700 */   201,  106,  146,  168,  169,  110,   42,  146,  168,  169,
   379         - /*   710 */   168,  169,  168,  169,   16,  168,  169,  168,  169,  168,
   380         - /*   720 */   169,  146,  181,   59,   60,   61,   62,   63,   64,   65,
          376  + /*   680 */    16,  146,  146,  188,   20,  146,  106,  146,   87,  146,
          377  + /*   690 */   110,  168,  169,  146,   98,   99,  100,  146,   20,  146,
          378  + /*   700 */    22,  101,   19,  168,  169,  167,   42,  168,  169,  168,
          379  + /*   710 */   169,  168,  169,  175,  113,  168,  169,  181,  160,  168,
          380  + /*   720 */   169,  168,  169,   59,   60,   61,   62,   63,   64,   65,
   381    381    /*   730 */    66,   67,   68,   69,   70,   71,  146,   73,   74,   75,
   382    382    /*   740 */    76,   77,   78,   79,   80,   81,   82,   16,  146,  146,
   383         - /*   750 */   146,   25,  146,  146,  146,   29,  146,   22,  168,  169,
   384         - /*   760 */   146,  129,  187,  131,  146,  202,  225,   41,  212,  146,
          383  + /*   750 */    67,  146,  146,   20,  146,   22,  146,  199,  168,  169,
          384  + /*   760 */   146,  225,  146,  123,  124,  146,    7,    8,    9,  202,
   385    385    /*   770 */   168,  169,  146,   42,  168,  169,  168,  169,  168,  169,
   386         - /*   780 */   146,  120,  168,  169,  181,  181,  168,  169,   90,   79,
          386  + /*   780 */   146,   18,  168,  169,  168,  169,  181,  168,  169,   79,
   387    387    /*   790 */    59,   60,   61,   62,   63,   64,   65,   66,   67,   68,
   388    388    /*   800 */    69,   70,   71,  146,   73,   74,   75,   76,   77,   78,
   389         - /*   810 */    79,   80,   81,   82,   16,  146,  106,  154,   90,  146,
   390         - /*   820 */   110,  146,   87,  146,   19,  168,  169,  146,  225,  225,
   391         - /*   830 */   102,   19,  146,   21,  123,  124,  146,  168,  169,  177,
   392         - /*   840 */    42,  168,  169,  168,  169,  168,  169,  177,  113,  168,
   393         - /*   850 */   169,  188,  146,  146,  168,  169,  177,   59,   60,   61,
          389  + /*   810 */    79,   80,   81,   82,   16,  146,  106,  146,   22,  146,
          390  + /*   820 */   110,  146,   19,  146,   21,  168,  169,  146,  146,  190,
          391  + /*   830 */   225,  146,   98,   99,  100,  109,  146,  168,  169,  146,
          392  + /*   840 */    42,  168,  169,  168,  169,  168,  169,  146,  122,  168,
          393  + /*   850 */   169,  225,  181,  168,  169,   92,  154,   59,   60,   61,
   394    394    /*   860 */    62,   63,   64,   65,   66,   67,   68,   69,   70,   71,
   395    395    /*   870 */   146,   73,   74,   75,   76,   77,   78,   79,   80,   81,
   396         - /*   880 */    82,   16,  146,    7,    8,  146,  146,  154,  146,   20,
   397         - /*   890 */   146,   22,  168,  169,  187,   20,  146,   22,   20,   20,
   398         - /*   900 */    22,   22,   89,   90,  168,  169,  146,   42,  168,  169,
   399         - /*   910 */   168,  169,  168,  169,  146,  146,  111,  146,  146,  154,
   400         - /*   920 */    20,  188,   22,  146,   16,   60,   61,   62,   63,   64,
   401         - /*   930 */    65,   66,   67,   68,   69,   70,   71,  187,   73,   74,
   402         - /*   940 */    75,   76,   77,   78,   79,   80,   81,   82,  146,   20,
   403         - /*   950 */    42,   22,   20,  188,   22,  187,  187,   19,  187,  187,
   404         - /*   960 */   230,  154,  154,   51,   52,   20,   20,   22,   22,   61,
          396  + /*   880 */    82,   16,  146,   20,  154,   22,  146,  109,  146,  154,
          397  + /*   890 */   188,  146,   96,  146,  212,  132,  225,  207,  160,  146,
          398  + /*   900 */   122,  184,  185,  184,  185,  181,  146,   42,  168,  169,
          399  + /*   910 */   168,  169,  146,  146,  146,  146,  146,  181,  188,  146,
          400  + /*   920 */   146,  146,  146,  188,   16,   60,   61,   62,   63,   64,
          401  + /*   930 */    65,   66,   67,   68,   69,   70,   71,  199,   73,   74,
          402  + /*   940 */    75,   76,   77,   78,   79,   80,   81,   82,   23,  225,
          403  + /*   950 */    42,  146,  146,  187,  187,  187,  187,  212,  159,  227,
          404  + /*   960 */   187,  225,  187,  187,   20,  171,   22,  168,  146,   61,
   405    405    /*   970 */    62,   63,   64,   65,   66,   67,   68,   69,   70,   71,
   406         - /*   980 */   231,   73,   74,   75,   76,   77,   78,   79,   80,   81,
   407         - /*   990 */    82,   16,   17,  146,   19,  188,  188,   20,   23,   22,
   408         - /*  1000 */    20,  146,   22,  146,  146,   67,   31,   16,   17,  237,
   409         - /*  1010 */    19,  148,  146,  146,   23,  171,  146,  146,  146,  146,
   410         - /*  1020 */   146,  190,   31,   48,  192,  222,  146,  227,  227,  176,
   411         - /*  1030 */   171,  171,  171,   58,  172,  171,  193,    6,  193,   48,
   412         - /*  1040 */   145,  145,  145,  145,   22,  153,   97,  170,   40,   58,
   413         - /*  1050 */    96,  170,   77,   78,  170,  172,  170,  120,  117,  115,
   414         - /*  1060 */    85,   86,   87,  178,  196,   90,  118,  221,   77,   78,
   415         - /*  1070 */    79,  119,  129,  111,  151,  188,   85,   86,   87,   16,
   416         - /*  1080 */    17,   90,   19,  193,  109,  194,   23,   95,  195,   23,
   417         - /*  1090 */   159,  197,  210,  151,   31,   97,  114,   19,  123,  124,
   418         - /*  1100 */   125,  126,  127,  128,  203,  178,  204,  203,  173,  210,
   419         - /*  1110 */   204,   48,  159,  210,  123,  124,  125,  126,  127,  128,
   420         - /*  1120 */   210,   58,    5,  173,   15,  170,  170,   10,   11,   12,
   421         - /*  1130 */    13,  170,  151,  150,   38,   19,  151,  151,  151,   23,
   422         - /*  1140 */    77,   78,  129,   26,  233,   28,  232,  150,   85,   86,
   423         - /*  1150 */    87,  151,   35,   90,  170,  183,   59,  183,   19,  193,
   424         - /*  1160 */    15,  214,  186,  193,   47,  186,   49,  151,   33,  235,
   425         - /*  1170 */   151,   54,  214,   56,   58,  186,  183,  151,  136,  158,
   426         - /*  1180 */   174,  174,    1,   20,   32,   44,  123,  124,  125,  126,
   427         - /*  1190 */   127,  128,  235,   77,   78,   19,  111,  238,  111,  111,
   428         - /*  1200 */   111,   19,   86,   20,   20,   90,   90,  106,   19,   11,
   429         - /*  1210 */    19,  116,   20,   20,  116,   98,   99,  100,   22,   22,
   430         - /*  1220 */   113,  104,   22,   19,  111,   20,  109,   20,   19,   44,
   431         - /*  1230 */    20,   19,   19,   19,   44,  102,   94,   16,   21,  123,
   432         - /*  1240 */   124,  125,   17,   97,   36,   22,   45,   45,   97,   19,
   433         - /*  1250 */   133,  132,    5,   11,    1,  101,   19,  121,   17,  112,
   434         - /*  1260 */   112,   67,   67,  101,  121,   19,   14,  135,   20,    3,
   435         - /*  1270 */   239,  134,   57,    4,
          406  + /*   980 */   146,   73,   74,   75,   76,   77,   78,   79,   80,   81,
          407  + /*   990 */    82,   16,   17,  187,   19,    7,    8,   20,   23,   22,
          408  + /*  1000 */    20,  171,   22,   77,   78,  160,   31,   16,   17,  227,
          409  + /*  1010 */    19,   86,  172,   20,   23,   22,    1,    2,  171,  146,
          410  + /*  1020 */   192,  146,   31,   48,  171,  146,  190,  144,  148,  171,
          411  + /*  1030 */   146,  146,  193,   58,  146,  222,  146,  176,  146,   48,
          412  + /*  1040 */   120,  193,  193,   59,   15,  183,  186,  111,  151,   58,
          413  + /*  1050 */   186,    6,   77,   78,  145,   19,  129,  186,  221,  193,
          414  + /*  1060 */    85,   86,   87,  151,  173,   90,   95,  210,   77,   78,
          415  + /*  1070 */    79,   40,  178,  145,   23,  159,   85,   86,   87,   16,
          416  + /*  1080 */    17,   90,   19,  145,  109,  151,   23,   15,  210,  150,
          417  + /*  1090 */   197,  119,  173,   19,   31,  214,  159,  136,  123,  124,
          418  + /*  1100 */   125,  126,  127,  128,   97,  238,  193,  117,  210,  194,
          419  + /*  1110 */   170,   48,  170,  151,  123,  124,  125,  126,  127,  128,
          420  + /*  1120 */   118,   58,    5,  170,  195,   22,  153,   10,   11,   12,
          421  + /*  1130 */    13,  115,  214,  170,  170,   33,  151,  196,  235,  150,
          422  + /*  1140 */    77,   78,   38,   26,  170,   28,  151,  151,   85,   86,
          423  + /*  1150 */    87,  183,   35,   90,  232,   97,  114,  203,  210,  204,
          424  + /*  1160 */    96,  129,  170,  233,   47,  145,   49,  235,  151,  172,
          425  + /*  1170 */   170,   54,  178,   56,  204,  151,  151,  109,  135,  203,
          426  + /*  1180 */   174,  183,  134,  174,   57,  158,  123,  124,  125,  126,
          427  + /*  1190 */   127,  128,  188,   19,  102,  146,  146,   14,  185,    1,
          428  + /*  1200 */    90,   19,  180,  168,   20,   20,   20,  106,  180,  168,
          429  + /*  1210 */    19,   44,   20,   19,   44,   98,   99,  100,   19,  121,
          430  + /*  1220 */     4,  104,   19,  111,    3,   19,  109,  121,   82,  111,
          431  + /*  1230 */    11,   16,  146,   97,   17,  101,  112,   44,   20,   19,
          432  + /*  1240 */     5,   19,  111,   20,   17,   21,  132,   22,   20,   45,
          433  + /*  1250 */   133,   22,   11,  116,   22,  111,    1,  112,   32,   45,
          434  + /*  1260 */   111,  101,   20,   19,  116,   97,   36,   19,   22,   67,
          435  + /*  1270 */    19,  113,   67,   20,   94,
   436    436   };
   437         -#define YY_SHIFT_USE_DFLT (-98)
          437  +#define YY_SHIFT_USE_DFLT (-61)
   438    438   #define YY_SHIFT_MAX 371
   439    439   static const short yy_shift_ofst[] = {
   440         - /*     0 */   221,  975, 1117,  -16,  975, 1063, 1063, 1063,   49,  115,
   441         - /*    10 */   115,  -97,  118, 1063, 1063, 1063, 1063, 1063,  -45,  131,
   442         - /*    20 */   116,  328,  225,  225,   51,  185,  252,  324,  396,  463,
   443         - /*    30 */   530,  597,  664,  731,  798,  731,  731,  731,  731,  731,
   444         - /*    40 */   731,  731,  731,  731,  731,  731,  731,  731,  731,  731,
          440  + /*     0 */  1015,  975, 1117,  -16,  975, 1063, 1063, 1063,  125,  330,
          441  + /*    10 */   330, 1068,  324, 1063, 1063, 1063, 1063, 1063,  -45,  192,
          442  + /*    20 */   548,  926,  925,  926,   51,  391,  118,  185,  252,  458,
          443  + /*    30 */   530,  597,  664,  731,  731,  731,  731,  731,  731,  731,
          444  + /*    40 */   731,  731,  798,  731,  731,  731,  731,  731,  731,  731,
   445    445    /*    50 */   731,  731,  865,  908,  908,  991, 1063, 1063, 1063, 1063,
   446    446    /*    60 */  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
   447    447    /*    70 */  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
   448    448    /*    80 */  1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063, 1063,
   449         - /*    90 */  1063, 1063, 1063, 1063,  -60,  -60,  -14,   27,   27,   94,
   450         - /*   100 */    86,  399,  116,  116,  116,  116,  151,  116,  116,  116,
   451         - /*   110 */   328,  -59,  -98,  -98,  -98, 1116,   53,  398,  398,  502,
   452         - /*   120 */   491,  116,  491,  116,  116,  116,  116,  116,  116,  116,
   453         - /*   130 */   116,  116,  116,  116,  116,  116,   29,  443,  -97,  -97,
   454         - /*   140 */   -97,  -98,  -98,  147,  147,  128,  272,  403,  262,  410,
   455         - /*   150 */   461,  412,  268,  408,  468,  470,  573,  116,  116,  710,
   456         - /*   160 */   116,  116,   73,  116,  116,  735,  116,  116,  524,  735,
   457         - /*   170 */   116,  116,  312,  312,  312,  116,  116,  524,  116,  116,
   458         - /*   180 */   524,  116,  726,  516,  116,  116,  524,  116,  116,  116,
   459         - /*   190 */   524,  116,  524,  524,  116,  116,  116,  116,  116,  116,
   460         - /*   200 */   812,  458,  595,  525,  711,  711,  632,  458,  458,   56,
   461         - /*   210 */   458,  458,  487,  661,  661, 1031, 1031, 1031, 1031, 1022,
   462         - /*   220 */   949,  949, 1008,  949,  954,  949,  -97,  937,  941,  948,
   463         - /*   230 */   944,  952,  943,  962,  992, 1066,  992,  962,  998,  982,
   464         - /*   240 */   998,  982, 1078,  992,  992, 1066, 1008,  949,  949,  949,
   465         - /*   250 */  1078, 1109,  962,  962,  962,  962, 1096, 1013, 1109,  962,
   466         - /*   260 */   949, 1097, 1097, 1139,  937, 1145, 1145, 1145,  937, 1097,
   467         - /*   270 */  1139,  962, 1135, 1135,  962,  962, 1042,  -98,  -98,  -98,
   468         - /*   280 */   148,  506,  536,  505,  728,  876,  805,  869,  698,  875,
   469         - /*   290 */   878,  879,  813,  900,  929,  932,  912,  945,  946,  977,
   470         - /*   300 */   980,  938, 1181, 1163, 1152, 1141, 1176, 1085, 1087, 1088,
   471         - /*   310 */  1089, 1182, 1183, 1184, 1115, 1101, 1189, 1198, 1191, 1192,
   472         - /*   320 */  1196, 1193, 1095, 1197, 1098, 1200, 1107, 1204, 1205, 1113,
   473         - /*   330 */  1207, 1185, 1209, 1210, 1212, 1213, 1190, 1214, 1142, 1133,
   474         - /*   340 */  1221, 1225, 1217, 1146, 1208, 1201, 1223, 1202, 1119, 1151,
   475         - /*   350 */  1230, 1247, 1242, 1253, 1154, 1194, 1195, 1136, 1237, 1147,
   476         - /*   360 */  1241, 1148, 1162, 1143, 1246, 1248, 1252, 1215, 1137, 1132,
   477         - /*   370 */  1266, 1269,
          449  + /*    90 */  1063, 1063, 1063, 1063,  -60,  -60,   -4,    1,    1,   74,
          450  + /*   100 */   153,  128,  763,  548,  548,  548,  548,  548,  548,  548,
          451  + /*   110 */   925, 1146,  -61,  -61,  -61,   84,  129,  261,  261,  548,
          452  + /*   120 */   548,  548,  548,  548,  548,  548,  548,  548,  578,  548,
          453  + /*   130 */   548,  548,  548,  489,  548,  489,  778,  726, 1068, 1068,
          454  + /*   140 */  1068,  -61,  -61,   79,  184,   79,  200,  390,  341,  315,
          455  + /*   150 */   480,  451,  251,  398,  466,  457,  548,  548,  548,  548,
          456  + /*   160 */   759,  548,  548,  548,  601,  267,  548,  548,  548,  397,
          457  + /*   170 */   548,  548,  710,  397,  596,  397,  601,  397,  316,  548,
          458  + /*   180 */   600,  600,  397,  548,  548,  548,  548,  548,  548,  548,
          459  + /*   190 */   600,  397,  548,  548,  397,  548,  548,  548,  548,  548,
          460  + /*   200 */   640,  598,  640,  598,  796,  598,  508,  598,  580,  803,
          461  + /*   210 */   598,   70,  429,   26,  358,  920,  984, 1029, 1029,  936,
          462  + /*   220 */  1045,  920, 1036,  927, 1029,  936,  971, 1045, 1031, 1051,
          463  + /*   230 */  1045,  936, 1072,  971,  972, 1036, 1051, 1074,  961, 1007,
          464  + /*   240 */   920, 1007,  971,  990,  936, 1002, 1007, 1103, 1016, 1074,
          465  + /*   250 */  1007, 1007,  936, 1072, 1102,  936, 1104,  984,  936, 1007,
          466  + /*   260 */  1058, 1042,  971, 1032, 1007, 1064, 1045, 1102,  936, 1007,
          467  + /*   270 */  1031,  984, 1042,  936, 1058,  936, 1068,  -61,  -61,  -61,
          468  + /*   280 */   424,  452,  310,  734,  277,  683,  389,   86,  526,  529,
          469  + /*   290 */   404,  461,  678,  733,  863,  497,  944,  988,  977,  980,
          470  + /*   300 */   993,  229, 1136, 1193, 1222, 1223, 1235, 1131, 1114, 1204,
          471  + /*   310 */  1229, 1241, 1226, 1255, 1214, 1242, 1160, 1230, 1248, 1202,
          472  + /*   320 */  1205, 1253, 1158, 1251, 1246, 1168, 1148, 1244, 1149, 1145,
          473  + /*   330 */  1144, 1232, 1137, 1227, 1228, 1225, 1224, 1124, 1218, 1220,
          474  + /*   340 */  1134, 1217, 1215, 1219, 1118, 1206, 1112, 1216, 1098, 1199,
          475  + /*   350 */  1170, 1194, 1191, 1101, 1186, 1185, 1184, 1182, 1110, 1198,
          476  + /*   360 */  1183, 1092, 1174, 1127, 1048, 1043, 1192, 1167, 1203, 1221,
          477  + /*   370 */  1106, 1180,
   478    478   };
   479    479   #define YY_REDUCE_USE_DFLT (-216)
   480    480   #define YY_REDUCE_MAX 279
   481    481   static const short yy_reduce_ofst[] = {
   482         - /*     0 */   -56,  136,   16,   70,  189,  127,   66,  130,  190,  219,
   483         - /*    10 */   255,  220,   61,  132,  138,  210,  265,  271, -178, -140,
   484         - /*    20 */   -61,   79,  145,  264, -215, -215, -215, -215, -215, -215,
          482  + /*     0 */   -39,  208,  154,  -52,  190,  138,  202,  204,  262,  328,
          483  + /*    10 */   414,  155,  133,   68,  268,  405, -144,  -74, -178,  270,
          484  + /*    20 */   -50,   76,  -21,  280, -215, -215, -215, -215, -215, -215,
   485    485    /*    30 */  -215, -215, -215, -215, -215, -215, -215, -215, -215, -215,
   486    486    /*    40 */  -215, -215, -215, -215, -215, -215, -215, -215, -215, -215,
   487         - /*    50 */  -215, -215, -215, -215, -215,  402,  405,  407,  416,  418,
   488         - /*    60 */   456,  472,  474,  476,  480,  482,  484,  523,  535,  540,
   489         - /*    70 */   542,  544,  547,  549,  551,  590,  602,  606,  608,  610,
   490         - /*    80 */   614,  618,  657,  669,  673,  675,  677,  681,  686,  724,
   491         - /*    90 */   736,  740,  742,  744, -215, -215,  -77, -215, -215, -215,
   492         - /*   100 */  -215,   50,  354,  473,  541,  603,  144,  604,  772,  342,
   493         - /*   110 */   284, -215, -215, -215, -215,  282,  251,  322,  462,  -72,
   494         - /*   120 */   177,  306,  428,  353,  193,  475,  421,  477,  509,  575,
   495         - /*   130 */   707,  750,  768,  771,  556,  769,  663,  733,  765,  807,
   496         - /*   140 */   808,  -42,  499, -126, -112,  -57,  -19,   18,   96,   18,
   497         - /*   150 */    18,   72,  141,  146,  198,  250,  223,  336,  370,  373,
   498         - /*   160 */   250,  543,  534,  561,  607,  563,  623,  626,   18,  563,
   499         - /*   170 */   634,  690,  662,  670,  679,  706,  739,   18,  760,  777,
   500         - /*   180 */    18,  802,  730,  749,  847,  855,   18,  857,  858,  866,
   501         - /*   190 */    18,  867,   18,   18,  870,  871,  872,  873,  874,  880,
   502         - /*   200 */   863,  844,  831,  832,  800,  801,  803,  859,  860,  853,
   503         - /*   210 */   861,  864,  862,  843,  845,  895,  896,  897,  898,  892,
   504         - /*   220 */   877,  881,  885,  884,  883,  886,  887,  890,  891,  893,
   505         - /*   230 */   868,  894,  846,  923,  882,  931,  899,  942,  901,  902,
   506         - /*   240 */   904,  906,  935,  903,  910,  953,  927,  955,  956,  961,
   507         - /*   250 */   950,  983,  981,  985,  986,  987,  914,  911,  997, 1000,
   508         - /*   260 */   984,  972,  974,  947,  966,  976,  979,  989,  970,  993,
   509         - /*   270 */   958, 1016,  934,  957, 1019, 1026,  959, 1021, 1006, 1007,
          487  + /*    50 */  -215, -215, -215, -215, -215,  610,  606,  590,  551,  543,
          488  + /*    60 */   539,  523,  483,  476,  469,  415,  408,  136,  740,  -11,
          489  + /*    70 */   401,  413,  417,  473,  478,  485,  535,  541,  547,  553,
          490  + /*    80 */   602,  608,  614,    4,  616,  619,  657,  669,  673,  675,
          491  + /*    90 */   677,  681,  742,  685, -215, -215,  326, -215, -215, -215,
          492  + /*   100 */  -215,  199,   14,  536,  605,  -78,  409,  671,  724,  736,
          493  + /*   110 */   347, -215, -215, -215, -215,  799,  538,  719,  717,  745,
          494  + /*   120 */   690,  806,   22,  776,  775,  773,  626,  769,  493,  768,
          495  + /*   130 */   767,  682,  766,  738,    3,  558,  735,  730,  702,  495,
          496  + /*   140 */   467,  286,  249, 1041, 1028, 1035, 1022,  -46,  -46,  -46,
          497  + /*   150 */  1086, 1013, 1050, 1049, -123,  892,  890,  888,  885,  884,
          498  + /*   160 */   883,  879,  875,  873,  567,  845,  834,  822,  805,  -46,
          499  + /*   170 */   770,  753,  639,  -46,  395,  -46,  567,  -46,  387,  403,
          500  + /*   180 */    94,   66,  -46, -104, -140, -123,  -90,   80,  111,  193,
          501  + /*   190 */   255,  -46,  603,  634,  -46,  693,  701,  747,  760,  774,
          502  + /*   200 */   732,  794,  782,  830,  840,  847,  828,  853,  836,  880,
          503  + /*   210 */   858,  839,  813,  861,  848,  849,  862,  860,  864,  897,
          504  + /*   220 */   909,  866,  891,  837,  871,  912,  857,  928,  894,  916,
          505  + /*   230 */   938,  934,  939,  878,  893,  919,  937,  881,  867,  940,
          506  + /*   240 */   913,  942,  898,  915,  962,  929,  953,  973,  941,  918,
          507  + /*   250 */   963,  964,  985,  989,  903,  995,  922,  968,  996,  974,
          508  + /*   260 */   954,  955,  948,  930,  992,  997, 1020,  932, 1017, 1000,
          509  + /*   270 */   994,  998,  970, 1024,  976, 1025, 1004, 1006, 1009, 1027,
   510    510   };
   511    511   static const YYACTIONTYPE yy_default[] = {
   512         - /*     0 */   567,  791,  856,  682,  856,  791,  856,  791,  856,  829,
   513         - /*    10 */   829,  686,  842,  787,  791,  856,  856,  856,  762,  813,
   514         - /*    20 */   856,  598,  813,  813,  717,  856,  856,  856,  856,  856,
   515         - /*    30 */   856,  856,  856,  718,  856,  790,  786,  782,  784,  783,
   516         - /*    40 */   719,  706,  715,  722,  698,  827,  724,  725,  730,  731,
   517         - /*    50 */   843,  846,  752,  768,  751,  856,  856,  856,  856,  856,
          512  + /*     0 */   567,  791,  856,  682,  856,  856,  791,  791,  856,  829,
          513  + /*    10 */   829,  686,  842,  856,  791,  856,  856,  787,  762,  813,
          514  + /*    20 */   856,  813,  598,  813,  717,  856,  856,  856,  856,  856,
          515  + /*    30 */   856,  856,  856,  724,  782,  784,  846,  783,  718,  719,
          516  + /*    40 */   725,  843,  856,  706,  827,  715,  730,  722,  731,  786,
          517  + /*    50 */   698,  790,  752,  768,  751,  856,  856,  856,  856,  856,
   518    518    /*    60 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
   519    519    /*    70 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
   520    520    /*    80 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
   521         - /*    90 */   856,  856,  856,  856,  754,  773,  591,  753,  761,  755,
   522         - /*   100 */   756,  651,  856,  856,  856,  856,  586,  856,  856,  856,
   523         - /*   110 */   856,  757,  758,  769,  770,  856,  856,  856,  856,  567,
   524         - /*   120 */   682,  856,  682,  856,  856,  856,  856,  856,  856,  856,
   525         - /*   130 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
   526         - /*   140 */   856,  676,  686,  856,  856,  642,  856,  856,  856,  856,
   527         - /*   150 */   856,  856,  856,  856,  856,  856,  574,  572,  856,  674,
   528         - /*   160 */   856,  856,  600,  856,  856,  684,  856,  856,  689,  690,
   529         - /*   170 */   856,  856,  856,  856,  856,  856,  856,  588,  856,  856,
   530         - /*   180 */   663,  856,  819,  856,  856,  856,  834,  856,  856,  856,
   531         - /*   190 */   832,  856,  665,  727,  801,  856,  856,  847,  849,  856,
   532         - /*   200 */   856,  709,  674,  683,  856,  856,  785,  709,  709,  621,
   533         - /*   210 */   709,  709,  624,  721,  721,  571,  571,  571,  571,  641,
   534         - /*   220 */   653,  653,  638,  653,  624,  653,  856,  721,  712,  714,
   535         - /*   230 */   702,  716,  856,  691,  710,  856,  710,  691,  699,  701,
   536         - /*   240 */   699,  701,  795,  710,  710,  856,  638,  653,  653,  653,
   537         - /*   250 */   795,  583,  691,  691,  691,  691,  823,  826,  583,  691,
   538         - /*   260 */   653,  655,  655,  732,  721,  662,  662,  662,  721,  655,
   539         - /*   270 */   732,  691,  845,  845,  691,  691,  854,  608,  626,  626,
   540         - /*   280 */   856,  856,  856,  856,  856,  856,  739,  856,  856,  856,
          521  + /*    90 */   856,  856,  856,  856,  773,  754,  591,  761,  753,  755,
          522  + /*   100 */   756,  651,  586,  856,  856,  856,  856,  856,  856,  856,
          523  + /*   110 */   856,  757,  769,  770,  758,  856,  856,  856,  856,  856,
          524  + /*   120 */   856,  856,  856,  856,  856,  856,  856,  856,  567,  856,
          525  + /*   130 */   856,  856,  856,  682,  856,  682,  856,  856,  856,  856,
          526  + /*   140 */   856,  686,  676,  856,  642,  856,  856,  856,  856,  856,
          527  + /*   150 */   856,  856,  856,  856,  856,  856,  572,  856,  856,  856,
          528  + /*   160 */   574,  856,  856,  856,  684,  600,  856,  847,  856,  689,
          529  + /*   170 */   849,  801,  674,  588,  856,  727,  690,  832,  819,  856,
          530  + /*   180 */   856,  856,  834,  856,  856,  856,  856,  856,  856,  856,
          531  + /*   190 */   856,  663,  856,  856,  665,  856,  856,  856,  856,  856,
          532  + /*   200 */   856,  709,  856,  709,  624,  709,  683,  709,  674,  856,
          533  + /*   210 */   709,  721,  785,  621,  721,  721,  655,  662,  662,  691,
          534  + /*   220 */   571,  721,  795,  856,  662,  691,  710,  571,  638,  856,
          535  + /*   230 */   571,  691,  583,  710,  716,  795,  856,  732,  854,  653,
          536  + /*   240 */   721,  653,  710,  712,  691,  714,  653,  641,  702,  732,
          537  + /*   250 */   653,  653,  691,  583,  845,  691,  823,  655,  691,  653,
          538  + /*   260 */   699,  701,  710,  826,  653,  624,  571,  845,  691,  653,
          539  + /*   270 */   638,  655,  701,  691,  699,  691,  856,  626,  626,  608,
          540  + /*   280 */   856,  856,  856,  856,  856,  808,  856,  856,  856,  739,
   541    541    /*   290 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
   542         - /*   300 */   856,  808,  856,  856,  856,  856,  856,  744,  740,  856,
   543         - /*   310 */   741,  856,  856,  856,  856,  668,  856,  856,  856,  856,
   544         - /*   320 */   856,  856,  856,  703,  856,  713,  856,  856,  856,  856,
   545         - /*   330 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
   546         - /*   340 */   856,  856,  856,  856,  856,  821,  822,  856,  856,  856,
   547         - /*   350 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
   548         - /*   360 */   856,  856,  856,  856,  856,  856,  856,  853,  856,  856,
   549         - /*   370 */   568,  856,  562,  565,  564,  566,  570,  573,  595,  596,
   550         - /*   380 */   597,  575,  576,  577,  578,  579,  580,  581,  587,  589,
   551         - /*   390 */   607,  609,  616,  654,  657,  658,  659,  837,  838,  839,
   552         - /*   400 */   617,  636,  639,  640,  618,  625,  707,  708,  619,  672,
   553         - /*   410 */   673,  736,  666,  667,  671,  738,  742,  743,  745,  746,
   554         - /*   420 */   594,  601,  602,  605,  606,  809,  811,  810,  812,  604,
   555         - /*   430 */   603,  747,  750,  759,  760,  766,  772,  775,  764,  765,
   556         - /*   440 */   767,  771,  774,  669,  670,  778,  780,  781,  835,  836,
   557         - /*   450 */   776,  788,  789,  692,  779,  763,  704,  593,  711,  705,
   558         - /*   460 */   675,  685,  694,  695,  696,  697,  680,  681,  687,  700,
   559         - /*   470 */   734,  735,  688,  677,  678,  679,  777,  737,  748,  749,
   560         - /*   480 */   620,  627,  628,  629,  632,  633,  634,  635,  630,  631,
   561         - /*   490 */   796,  797,  799,  798,  622,  623,  637,  610,  611,  612,
   562         - /*   500 */   613,  744,  614,  615,  599,  592,  643,  646,  647,  648,
   563         - /*   510 */   649,  650,  652,  644,  645,  590,  582,  584,  693,  815,
   564         - /*   520 */   824,  825,  820,  816,  817,  818,  585,  792,  793,  656,
   565         - /*   530 */   728,  729,  814,  828,  830,  733,  831,  833,  660,  661,
   566         - /*   540 */   664,  800,  840,  720,  723,  726,  802,  803,  804,  805,
   567         - /*   550 */   806,  807,  841,  844,  848,  850,  851,  852,  855,  569,
          542  + /*   300 */   856,  856,  856,  856,  856,  856,  856,  856,  856,  856,
          543  + /*   310 */   822,  856,  856,  856,  821,  856,  856,  856,  856,  856,
          544  + /*   320 */   856,  856,  856,  856,  713,  856,  856,  856,  744,  856,
          545  + /*   330 */   740,  703,  856,  856,  856,  856,  856,  856,  856,  856,
          546  + /*   340 */   856,  856,  856,  856,  856,  856,  741,  856,  856,  856,
          547  + /*   350 */   856,  856,  856,  668,  856,  856,  856,  856,  856,  856,
          548  + /*   360 */   856,  856,  856,  853,  856,  856,  856,  856,  856,  568,
          549  + /*   370 */   856,  856,  838,  649,  839,  837,  648,  650,  652,  644,
          550  + /*   380 */   645,  590,  647,  617,  659,  646,  750,  592,  599,  582,
          551  + /*   390 */   615,  614,  584,  744,  613,  612,  636,  611,  610,  637,
          552  + /*   400 */   623,  622,  798,  658,  799,  693,  797,  639,  815,  657,
          553  + /*   410 */   654,  796,  640,  824,  825,  820,  631,  630,  635,  816,
          554  + /*   420 */   817,  634,  818,  585,  633,  632,  629,  628,  627,  620,
          555  + /*   430 */   749,  616,  748,  737,  792,  793,  777,  679,  678,  677,
          556  + /*   440 */   609,  656,  728,  729,  688,  814,  618,  625,  828,  707,
          557  + /*   450 */   708,  735,  607,  734,  700,  687,  681,  589,  830,  680,
          558  + /*   460 */   587,  619,  697,  696,  695,  694,  685,  733,  675,  672,
          559  + /*   470 */   673,  581,  705,  711,  580,  831,  593,  704,  736,  666,
          560  + /*   480 */   833,  763,  779,  579,  660,  692,  661,  789,  788,  664,
          561  + /*   490 */   667,  578,  800,  776,  840,  836,  835,  577,  671,  720,
          562  + /*   500 */   738,  576,  781,  575,  780,  723,  742,  597,  743,  745,
          563  + /*   510 */   746,  778,  596,  670,  726,  669,  802,  594,  774,  601,
          564  + /*   520 */   595,  803,  804,  805,  806,  771,  767,  807,  765,  573,
          565  + /*   530 */   764,  570,  602,  841,  605,  566,  844,  606,  775,  564,
          566  + /*   540 */   772,  766,  848,  760,  759,  850,  809,  565,  811,  810,
          567  + /*   550 */   812,  851,  604,  603,  562,  852,  855,  747,  643,  569,
   568    568    /*   560 */   563,
   569    569   };
   570    570   #define YY_SZ_ACTTAB (sizeof(yy_action)/sizeof(yy_action[0]))
   571    571   
   572    572   /* The next table maps tokens into fallback tokens.  If a construct
   573    573   ** like the following:
   574    574   ** 

Changes to SQLite.Interop/src/pragma.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the PRAGMA command.
    13     13   **
    14         -** $Id: pragma.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           14  +** $Id: pragma.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   #include "os.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /* Ignore this whole file if pragmas are disabled
    21     21   */
................................................................................
   147    147       { "vdbe_trace",               SQLITE_VdbeTrace     },
   148    148       { "sql_trace",                SQLITE_SqlTrace      },
   149    149       { "vdbe_listing",             SQLITE_VdbeListing   },
   150    150       { "full_column_names",        SQLITE_FullColNames  },
   151    151       { "short_column_names",       SQLITE_ShortColNames },
   152    152       { "count_changes",            SQLITE_CountRows     },
   153    153       { "empty_result_callbacks",   SQLITE_NullCallback  },
          154  +    { "legacy_file_format",       SQLITE_LegacyFileFmt },
   154    155   #ifndef SQLITE_OMIT_CHECK
   155    156       { "ignore_check_constraints", SQLITE_IgnoreChecks  },
   156    157   #endif
   157    158       /* The following is VERY experimental */
   158    159       { "writable_schema",          SQLITE_WriteSchema   },
   159    160       { "omit_readlock",            SQLITE_NoReadlock    },
   160    161   
................................................................................
   804    805         sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
   805    806       }else{                        /* "PRAGMA encoding = XXX" */
   806    807         /* Only change the value of sqlite.enc if the database handle is not
   807    808         ** initialized. If the main database exists, the new sqlite.enc value
   808    809         ** will be overwritten when the schema is next loaded. If it does not
   809    810         ** already exists, it will be created to use the new encoding value.
   810    811         */
   811         -      if( !(pParse->db->flags&SQLITE_Initialized) ){
          812  +      if( 
          813  +        !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
          814  +        DbHasProperty(db, 0, DB_Empty) 
          815  +      ){
   812    816           for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
   813    817             if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
   814    818               ENC(pParse->db) = pEnc->enc;
   815    819               break;
   816    820             }
   817    821           }
   818    822           if( !pEnc->zName ){

Changes to SQLite.Interop/src/prepare.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains the implementation of the sqlite3_prepare()
    13     13   ** interface, and routines that contribute to loading the database schema
    14     14   ** from disk.
    15     15   **
    16         -** $Id: prepare.c,v 1.9 2006/01/11 03:22:29 rmsimpson Exp $
           16  +** $Id: prepare.c,v 1.10 2006/01/12 20:54:07 rmsimpson Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "os.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   /*
    23     23   ** Fill the InitData structure with an error message that indicates
    24     24   ** that the database is corrupt.
    25     25   */
    26     26   static void corruptSchema(InitData *pData, const char *zExtra){
    27         -  if( !sqlite3ThreadData()->mallocFailed ){
           27  +  if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
    28     28       sqlite3SetString(pData->pzErrMsg, "malformed database schema",
    29     29          zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
    30     30     }
    31     31   }
    32     32   
    33     33   /*
    34     34   ** This is the callback routine for the code that initializes the
................................................................................
    45     45   **
    46     46   */
    47     47   int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
    48     48     InitData *pData = (InitData*)pInit;
    49     49     sqlite3 *db = pData->db;
    50     50     int iDb;
    51     51   
    52         -  if( sqlite3ThreadData()->mallocFailed ){
           52  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
    53     53       return SQLITE_NOMEM;
    54     54     }
    55     55   
    56     56     assert( argc==4 );
    57     57     if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
    58     58     if( argv[1]==0 || argv[3]==0 ){
    59     59       corruptSchema(pData, 0);
................................................................................
   150    150        ")"
   151    151     ;
   152    152   #else
   153    153     #define temp_master_schema 0
   154    154   #endif
   155    155   
   156    156     assert( iDb>=0 && iDb<db->nDb );
   157         -
   158    157     assert( db->aDb[iDb].pSchema );
   159         -#if 0
   160         -  if( 0==db->aDb[iDb].pSchema ){
   161         -    Schema *pS = sqlite3SchemaGet(db->aDb[iDb].pBt);
   162         -    db->aDb[iDb].pSchema = pS;
   163         -    if( !pS ){
   164         -      return SQLITE_NOMEM;
   165         -    }else if( pS->file_format!=0 ){
   166         -      /* This means that the shared-schema associated with the the btree
   167         -      ** is already open and populated.
   168         -      */
   169         -      if( pS->enc!=ENC(db) ){
   170         -        sqlite3SetString(pzErrMsg, "attached databases must use the same"
   171         -            " text encoding as main database", (char*)0);
   172         -        return SQLITE_ERROR;
   173         -      }
   174         -      return SQLITE_OK;
   175         -    }
   176         -  }
   177         -#endif
   178    158   
   179    159     /* zMasterSchema and zInitScript are set to point at the master schema
   180    160     ** and initialisation script appropriate for the database being
   181    161     ** initialised. zMasterName is the name of the master table.
   182    162     */
   183    163     if( !OMIT_TEMPDB && iDb==1 ){
   184    164       zMasterSchema = temp_master_schema;
................................................................................
   208    188     }
   209    189     sqlite3SafetyOn(db);
   210    190   
   211    191     /* Create a cursor to hold the database open
   212    192     */
   213    193     pDb = &db->aDb[iDb];
   214    194     if( pDb->pBt==0 ){
   215         -    if( !OMIT_TEMPDB && iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);
          195  +    if( !OMIT_TEMPDB && iDb==1 ){
          196  +      DbSetProperty(db, 1, DB_SchemaLoaded);
          197  +    }
   216    198       return SQLITE_OK;
   217    199     }
   218    200     rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
   219    201     if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
   220    202       sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
   221    203       return rc;
   222    204     }
................................................................................
   268    250         if( meta[4]!=ENC(db) ){
   269    251           sqlite3BtreeCloseCursor(curMain);
   270    252           sqlite3SetString(pzErrMsg, "attached databases must use the same"
   271    253               " text encoding as main database", (char*)0);
   272    254           return SQLITE_ERROR;
   273    255         }
   274    256       }
          257  +  }else{
          258  +    DbSetProperty(db, iDb, DB_Empty);
   275    259     }
   276    260     pDb->pSchema->enc = ENC(db);
   277    261   
   278    262     size = meta[2];
   279    263     if( size==0 ){ size = MAX_PAGES; }
   280    264     pDb->pSchema->cache_size = size;
   281    265     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
................................................................................
   315    299   #ifndef SQLITE_OMIT_ANALYZE
   316    300       if( rc==SQLITE_OK ){
   317    301         sqlite3AnalysisLoad(db, iDb);
   318    302       }
   319    303   #endif
   320    304       sqlite3BtreeCloseCursor(curMain);
   321    305     }
   322         -  if( sqlite3ThreadData()->mallocFailed ){
          306  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   323    307       sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
   324    308       rc = SQLITE_NOMEM;
   325    309       sqlite3ResetInternalSchema(db, 0);
   326    310     }
   327    311     if( rc==SQLITE_OK ){
   328    312       DbSetProperty(db, iDb, DB_SchemaLoaded);
   329    313     }else{
................................................................................
   339    323   ** error occurs, write an error message into *pzErrMsg.
   340    324   **
   341    325   ** After the database is initialized, the SQLITE_Initialized
   342    326   ** bit is set in the flags field of the sqlite structure. 
   343    327   */
   344    328   int sqlite3Init(sqlite3 *db, char **pzErrMsg){
   345    329     int i, rc;
          330  +  int called_initone = 0;
   346    331     
   347    332     if( db->init.busy ) return SQLITE_OK;
   348         -  assert( (db->flags & SQLITE_Initialized)==0 );
   349    333     rc = SQLITE_OK;
   350    334     db->init.busy = 1;
   351    335     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
   352    336       if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
   353    337       rc = sqlite3InitOne(db, i, pzErrMsg);
   354    338       if( rc ){
   355    339         sqlite3ResetInternalSchema(db, i);
   356    340       }
          341  +    called_initone = 1;
   357    342     }
   358    343   
   359    344     /* Once all the other databases have been initialised, load the schema
   360    345     ** for the TEMP database. This is loaded last, as the TEMP database
   361    346     ** schema may contain references to objects in other databases.
   362    347     */
   363    348   #ifndef SQLITE_OMIT_TEMPDB
   364    349     if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
   365    350       rc = sqlite3InitOne(db, 1, pzErrMsg);
   366    351       if( rc ){
   367    352         sqlite3ResetInternalSchema(db, 1);
   368    353       }
          354  +    called_initone = 1;
   369    355     }
   370    356   #endif
   371    357   
   372    358     db->init.busy = 0;
   373         -  if( rc==SQLITE_OK ){
   374         -    db->flags |= SQLITE_Initialized;
          359  +  if( rc==SQLITE_OK && called_initone ){
   375    360       sqlite3CommitInternalChanges(db);
   376    361     }
   377    362   
   378         -  if( rc!=SQLITE_OK ){
   379         -    db->flags &= ~SQLITE_Initialized;
   380         -  }
   381         -  return rc;
          363  +  return rc; 
   382    364   }
   383    365   
   384    366   /*
   385    367   ** This routine is a no-op if the database schema is already initialised.
   386    368   ** Otherwise, the schema is loaded. An error code is returned.
   387    369   */
   388    370   int sqlite3ReadSchema(Parse *pParse){
   389    371     int rc = SQLITE_OK;
   390    372     sqlite3 *db = pParse->db;
   391    373     if( !db->init.busy ){
   392         -    if( (db->flags & SQLITE_Initialized)==0 ){
   393         -      rc = sqlite3Init(db, &pParse->zErrMsg);
   394         -    }
          374  +    rc = sqlite3Init(db, &pParse->zErrMsg);
   395    375     }
   396         -  assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized) || db->init.busy );
   397    376     if( rc!=SQLITE_OK ){
   398    377       pParse->rc = rc;
   399    378       pParse->nErr++;
   400    379     }
   401    380     return rc;
   402    381   }
   403    382   
................................................................................
   511    490     const char** pzTail       /* OUT: End of parsed string */
   512    491   ){
   513    492     Parse sParse;
   514    493     char *zErrMsg = 0;
   515    494     int rc = SQLITE_OK;
   516    495     int i;
   517    496   
   518         -  assert( !sqlite3ThreadData()->mallocFailed );
          497  +  assert( !sqlite3ThreadDataReadOnly()->mallocFailed );
   519    498   
   520    499     assert( ppStmt );
   521    500     *ppStmt = 0;
   522    501     if( sqlite3SafetyOn(db) ){
   523    502       return SQLITE_MISUSE;
   524    503     }
   525    504   
................................................................................
   534    513         sqlite3SafetyOff(db);
   535    514         return SQLITE_LOCKED;
   536    515       }
   537    516     }
   538    517     
   539    518     memset(&sParse, 0, sizeof(sParse));
   540    519     sParse.db = db;
          520  +  sParse.pTsd = sqlite3ThreadData();
          521  +  sParse.pTsd->nRef++;
   541    522     sqlite3RunParser(&sParse, zSql, &zErrMsg);
   542    523   
   543         -  if( sqlite3ThreadData()->mallocFailed ){
          524  +  if( sParse.pTsd->mallocFailed ){
   544    525       sParse.rc = SQLITE_NOMEM;
   545    526     }
   546    527     if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
   547    528     if( sParse.checkSchema && !schemaIsValid(db) ){
   548    529       sParse.rc = SQLITE_SCHEMA;
   549    530     }
   550    531     if( sParse.rc==SQLITE_SCHEMA ){
................................................................................
   586    567     }else{
   587    568       sqlite3Error(db, rc, 0);
   588    569     }
   589    570   
   590    571     /* We must check for malloc failure last of all, in case malloc() failed
   591    572     ** inside of the sqlite3Error() call above or something.
   592    573     */
   593         -  if( sqlite3ThreadData()->mallocFailed ){
          574  +  if( sParse.pTsd->mallocFailed ){
   594    575       rc = SQLITE_NOMEM;
   595    576       sqlite3Error(db, rc, 0);
   596    577     }
   597    578   
          579  +  sParse.pTsd->nRef--;
   598    580     sqlite3MallocClearFailed();
          581  +  sqlite3ReleaseThreadData();
   599    582     return rc;
   600    583   }
   601    584   
   602    585   #ifndef SQLITE_OMIT_UTF16
   603    586   /*
   604    587   ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
   605    588   */

Changes to SQLite.Interop/src/random.c.

    11     11   *************************************************************************
    12     12   ** This file contains code to implement a pseudo-random number
    13     13   ** generator (PRNG) for SQLite.
    14     14   **
    15     15   ** Random numbers are used by some of the database backends in order
    16     16   ** to generate random integer keys for tables or random filenames.
    17     17   **
    18         -** $Id: random.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           18  +** $Id: random.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   
    23     23   
    24     24   /*
    25     25   ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex

Changes to SQLite.Interop/src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           15  +** $Id: select.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Allocate a new Select structure and return a pointer to that
    22     22   ** structure.
................................................................................
   868    868     /* If this is an EXPLAIN, skip this step */
   869    869     if( pParse->explain ){
   870    870       return;
   871    871     }
   872    872   #endif
   873    873   
   874    874     assert( v!=0 );
   875         -  if( pParse->colNamesSet || v==0 || sqlite3ThreadData()->mallocFailed ) return;
          875  +  if( pParse->colNamesSet || v==0
          876  +     || sqlite3ThreadDataReadOnly()->mallocFailed ) return;
   876    877     pParse->colNamesSet = 1;
   877    878     fullNames = (db->flags & SQLITE_FullColNames)!=0;
   878    879     shortNames = (db->flags & SQLITE_ShortColNames)!=0;
   879    880     sqlite3VdbeSetNumCols(v, pEList->nExpr);
   880    881     for(i=0; i<pEList->nExpr; i++){
   881    882       Expr *p;
   882    883       p = pEList->a[i].pExpr;
................................................................................
   997    998         /* Use the original text of the column expression as its name */
   998    999         zName = sqlite3MPrintf("%T", &p->span);
   999   1000       }else{
  1000   1001         /* If all else fails, make up a name */
  1001   1002         zName = sqlite3MPrintf("column%d", i+1);
  1002   1003       }
  1003   1004       sqlite3Dequote(zName);
  1004         -    if( sqlite3ThreadData()->mallocFailed ){
         1005  +    if( sqlite3ThreadDataReadOnly()->mallocFailed ){
  1005   1006         sqliteFree(zName);
  1006   1007         sqlite3DeleteTable(0, pTab);
  1007   1008         return 0;
  1008   1009       }
  1009   1010   
  1010   1011       /* Make sure the column name is unique.  If the name is not unique,
  1011   1012       ** append a integer to the name so that it becomes unique.
................................................................................
  1069   1070   static int prepSelectStmt(Parse *pParse, Select *p){
  1070   1071     int i, j, k, rc;
  1071   1072     SrcList *pTabList;
  1072   1073     ExprList *pEList;
  1073   1074     Table *pTab;
  1074   1075     struct SrcList_item *pFrom;
  1075   1076   
  1076         -  if( p==0 || p->pSrc==0 || sqlite3ThreadData()->mallocFailed ) return 1;
         1077  +  if( p==0 || p->pSrc==0 || sqlite3ThreadDataReadOnly()->mallocFailed ){
         1078  +    return 1;
         1079  +  }
  1077   1080     pTabList = p->pSrc;
  1078   1081     pEList = p->pEList;
  1079   1082   
  1080   1083     /* Make sure cursor numbers have been assigned to all entries in
  1081   1084     ** the FROM clause of the SELECT statement.
  1082   1085     */
  1083   1086     sqlite3SrcListAssignCursors(pParse, p->pSrc);
................................................................................
  2686   2689     int isDistinct;        /* True if the DISTINCT keyword is present */
  2687   2690     int distinct;          /* Table to use for the distinct set */
  2688   2691     int rc = 1;            /* Value to return from this function */
  2689   2692     int addrSortIndex;     /* Address of an OP_OpenVirtual instruction */
  2690   2693     AggInfo sAggInfo;      /* Information used by aggregate queries */
  2691   2694     int iEnd;              /* Address of the end of the query */
  2692   2695   
  2693         -  if( sqlite3ThreadData()->mallocFailed || pParse->nErr || p==0 ) return 1;
         2696  +  if( p==0 || sqlite3ThreadDataReadOnly()->mallocFailed || pParse->nErr ){
         2697  +    return 1;
         2698  +  }
  2694   2699     if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
  2695   2700     memset(&sAggInfo, 0, sizeof(sAggInfo));
  2696   2701   
  2697   2702   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  2698   2703     /* If there is are a sequence of queries, do the earlier ones first.
  2699   2704     */
  2700   2705     if( p->pPrior ){
................................................................................
  2940   2945       }
  2941   2946       sAggInfo.nAccumulator = sAggInfo.nColumn;
  2942   2947       for(i=0; i<sAggInfo.nFunc; i++){
  2943   2948         if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){
  2944   2949           goto select_end;
  2945   2950         }
  2946   2951       }
  2947         -    if( sqlite3ThreadData()->mallocFailed ) goto select_end;
         2952  +    if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto select_end;
  2948   2953   
  2949   2954       /* Processing for aggregates with GROUP BY is very different and
  2950   2955       ** much more complex tha aggregates without a GROUP BY.
  2951   2956       */
  2952   2957       if( pGroupBy ){
  2953   2958         KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
  2954   2959   

Changes to SQLite.Interop/src/server.c.

    58     58   ** to SQLite version 3.3.0 it probably was not worth the trouble.  But
    59     59   ** with SQLite version 3.3.0 and beyond you can get significant performance
    60     60   ** and concurrency improvements and memory usage reductions by going
    61     61   ** client/server.
    62     62   **
    63     63   ** Note:  The extra features of version 3.3.0 described by points (2)
    64     64   ** through (4) above are only available if you compile without the
    65         -** option -DSQLITE_OMIT_SHARED_CACHE.  For reasons of backwards
    66         -** compatibility, SQLite is compile with this option by default.
           65  +** option -DSQLITE_OMIT_SHARED_CACHE. 
    67     66   **
    68     67   ** Here is how the client/server approach works:  The database server
    69     68   ** thread is started on this procedure:
    70     69   **
    71     70   **       void *sqlite3_server(void *NotUsed);
    72     71   **
    73     72   ** The sqlite_server procedure runs as long as the g.serverHalt variable
................................................................................
   252    251   **        sqlite3_prepare
   253    252   **        sqlite3_step
   254    253   **        sqlite3_reset
   255    254   **        sqlite3_finalize
   256    255   **        sqlite3_close
   257    256   **
   258    257   ** Clients should use the following client-side routines instead of 
   259         -** the core routines.
          258  +** the core routines above.
   260    259   **
   261    260   **        sqlite3_client_open
   262    261   **        sqlite3_client_prepare
   263    262   **        sqlite3_client_step
   264    263   **        sqlite3_client_reset
   265    264   **        sqlite3_client_finalize
   266    265   **        sqlite3_client_close
................................................................................
   327    326   ** This routine implements the server.  To start the server, first
   328    327   ** make sure g.serverHalt is false, then create a new detached thread
   329    328   ** on this procedure.  See the sqlite3_server_start() routine below
   330    329   ** for an example.  This procedure loops until g.serverHalt becomes
   331    330   ** true.
   332    331   */
   333    332   void *sqlite3_server(void *NotUsed){
          333  +  sqlite3_enable_shared_cache(1);
   334    334     if( pthread_mutex_trylock(&g.serverMutex) ){
          335  +    sqlite3_enable_shared_cache(0);
   335    336       return 0;  /* Another server is already running */
   336    337     }
   337    338     while( !g.serverHalt ){
   338    339       SqlMessage *pMsg;
   339    340   
   340    341       /* Remove the last message from the message queue.
   341    342       */
................................................................................
   389    390       /* Signal the client that the message has been processed.
   390    391       */
   391    392       pMsg->op = MSG_Done;
   392    393       pthread_mutex_unlock(&pMsg->clientMutex);
   393    394       pthread_cond_signal(&pMsg->clientWakeup);
   394    395     }
   395    396     pthread_mutex_unlock(&g.serverMutex);
          397  +  sqlite3_thread_cleanup();
   396    398     return 0;
   397    399   }
   398    400   
   399    401   /*
   400    402   ** Start a server thread if one is not already running.  If there
   401    403   ** is aleady a server thread running, the new thread will quickly
   402    404   ** die and this routine is effectively a no-op.

Changes to SQLite.Interop/src/shell.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code to implement the "sqlite" command line
    13     13   ** utility for accessing SQLite databases.
    14     14   **
    15         -** $Id: shell.c,v 1.13 2006/01/11 03:22:29 rmsimpson Exp $
           15  +** $Id: shell.c,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #include <stdlib.h>
    18     18   #include <string.h>
    19     19   #include <stdio.h>
    20     20   #include <assert.h>
    21     21   #include "sqlite3.h"
    22     22   #include <ctype.h>

Changes to SQLite.Interop/src/sqlite3.def.

    36     36   sqlite3_complete16
    37     37   sqlite3_create_collation
    38     38   sqlite3_create_collation16
    39     39   sqlite3_create_function
    40     40   sqlite3_create_function16
    41     41   sqlite3_data_count
    42     42   sqlite3_db_handle
    43         -sqlite3_enable_memory_management
    44     43   sqlite3_enable_shared_cache
    45     44   sqlite3_errcode
    46     45   sqlite3_errmsg
    47     46   sqlite3_errmsg16
    48     47   sqlite3_exec
    49     48   sqlite3_expired
    50     49   sqlite3_finalize
................................................................................
    60     59   sqlite3_libversion_number
    61     60   sqlite3_mprintf
    62     61   sqlite3_open
    63     62   sqlite3_open16
    64     63   sqlite3_prepare
    65     64   sqlite3_prepare16
    66     65   sqlite3_progress_handler
    67         -sqlite3_release_memory
    68     66   sqlite3_reset
    69     67   sqlite3_result_blob
    70     68   sqlite3_result_double
    71     69   sqlite3_result_error
    72     70   sqlite3_result_error16
    73     71   sqlite3_result_int
    74     72   sqlite3_result_int64
................................................................................
    78     76   sqlite3_result_text16be
    79     77   sqlite3_result_text16le
    80     78   sqlite3_result_value
    81     79   sqlite3_rollback_hook
    82     80   sqlite3_set_authorizer
    83     81   sqlite3_set_auxdata
    84     82   sqlite3_snprintf
    85         -sqlite3_soft_heap_limit
    86     83   sqlite3_step
           84  +sqlite3_thread_cleanup
    87     85   sqlite3_total_changes
    88     86   sqlite3_trace
    89     87   sqlite3_transfer_bindings
    90     88   sqlite3_update_hook
    91     89   sqlite3_user_data
    92     90   sqlite3_value_blob
    93     91   sqlite3_value_bytes

Changes to SQLite.Interop/src/sqlite3.h.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This header file defines the interface that the SQLite library
    13     13   ** presents to client programs.
    14     14   **
    15         -** @(#) $Id: sqlite3.h,v 1.14 2006/01/11 03:22:30 rmsimpson Exp $
           15  +** @(#) $Id: sqlite3.h,v 1.15 2006/01/12 20:54:07 rmsimpson Exp $
    16     16   */
    17     17   #ifndef _SQLITE3_H_
    18     18   #define _SQLITE3_H_
    19     19   #include <stdarg.h>     /* Needed for the definition of va_list */
    20     20   
    21     21   /*
    22     22   ** Make sure we can call this stuff from C++.
................................................................................
   188    188   #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
   189    189   #define SQLITE_AUTH        23   /* Authorization denied */
   190    190   #define SQLITE_FORMAT      24   /* Auxiliary database format error */
   191    191   #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
   192    192   #define SQLITE_NOTADB      26   /* File opened that is not a database file */
   193    193   #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
   194    194   #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
          195  +/* end-of-return-codes */
   195    196   
   196    197   /*
   197    198   ** Each entry in an SQLite table has a unique integer key.  (The key is
   198    199   ** the value of the INTEGER PRIMARY KEY column if there is such a column,
   199    200   ** otherwise the key is generated at random.  The unique key is always
   200    201   ** available as the ROWID, OID, or _ROWID_ column.)  The following routine
   201    202   ** returns the integer key of the most recent insert in the database.
................................................................................
  1325   1326   ** This function is only available if the library is compiled without
  1326   1327   ** the SQLITE_OMIT_SHARED_CACHE macro defined. It is used to enable or
  1327   1328   ** disable (if the argument is true or false, respectively) the 
  1328   1329   ** "shared pager" feature.
  1329   1330   */
  1330   1331   int sqlite3_enable_shared_cache(int);
  1331   1332   
  1332         -/*
  1333         -** This function is only available if the library is compiled without
  1334         -** the SQLITE_OMIT_MEMORY_MANAGEMENT macro defined. It is used to enable or
  1335         -** disable (if the argument is true or false, respectively) the 
  1336         -** "memory management" features (accessed via the sqlite3_soft_heap_limit()
  1337         -** and sqlite3_release_memory() APIs).
  1338         -*/
  1339         -int sqlite3_enable_memory_management(int);
  1340         -
  1341   1333   /*
  1342   1334   ** Attempt to free N bytes of heap memory by deallocating non-essential
  1343   1335   ** memory allocations held by the database library (example: memory 
  1344   1336   ** used to cache database pages to improve performance).
  1345   1337   **
  1346         -** This function is a no-op unless memory-management has been enabled.
         1338  +** This function is not a part of standard builds.  It is only created
         1339  +** if SQLite is compiled with the SQLITE_ENABLE_MEMORY_MANAGEMENT macro.
  1347   1340   */
  1348   1341   int sqlite3_release_memory(int);
  1349   1342   
  1350   1343   /*
  1351   1344   ** Place a "soft" limit on the amount of heap memory that may be allocated by
  1352   1345   ** SQLite within the current thread. If an internal allocation is requested 
  1353   1346   ** that would exceed the specified limit, sqlite3_release_memory() is invoked
  1354   1347   ** one or more times to free up some space before the allocation is made.
  1355   1348   **
  1356   1349   ** The limit is called "soft", because if sqlite3_release_memory() cannot free
  1357   1350   ** sufficient memory to prevent the limit from being exceeded, the memory is
  1358   1351   ** allocated anyway and the current operation proceeds.
  1359   1352   **
  1360         -** This function is only available if the library was compiled without the 
  1361         -** SQLITE_OMIT_MEMORY_MANAGEMENT option set. It is a no-op unless 
         1353  +** This function is only available if the library was compiled with the 
         1354  +** SQLITE_ENABLE_MEMORY_MANAGEMENT option set.
  1362   1355   ** memory-management has been enabled.
  1363   1356   */
  1364   1357   void sqlite3_soft_heap_limit(int);
  1365   1358   
         1359  +/*
         1360  +** This routine makes sure that all thread-local storage has been
         1361  +** deallocated for the current thread.
         1362  +**
         1363  +** This routine is not technically necessary.  All thread-local storage
         1364  +** will be automatically deallocated once memory-management and
         1365  +** shared-cache are disabled and the soft heap limit has been set
         1366  +** to zero.  This routine is provided as a convenience for users who
         1367  +** want to make absolutely sure they have not forgotten something
         1368  +** prior to killing off a thread.
         1369  +*/
         1370  +void sqlite3_thread_cleanup(void);
         1371  +
  1366   1372   /*
  1367   1373   ** Undo the hack that converts floating point types to integer for
  1368   1374   ** builds on processors without floating point support.
  1369   1375   */
  1370   1376   #ifdef SQLITE_OMIT_FLOATING_POINT
  1371   1377   # undef double
  1372   1378   #endif
  1373   1379   
  1374   1380   #ifdef __cplusplus
  1375   1381   }  /* End of the 'extern "C"' block */
  1376   1382   #endif
  1377   1383   #endif

Changes to SQLite.Interop/src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.14 2006/01/12 20:54:07 rmsimpson Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Extra interface definitions for those who need them
    21     21   */
................................................................................
   282    282   
   283    283   #endif
   284    284   
   285    285   #define sqliteFree(x)          sqlite3FreeX(x)
   286    286   #define sqliteAllocSize(x)     sqlite3AllocSize(x)
   287    287   
   288    288   /*
   289         -** An instance of this structure is allocated for each thread that uses SQLite.
          289  +** An instance of this structure might be allocated to store information
          290  +** specific to a single thread.
          291  +**
          292  +** To avoid a memory leak on windows, the content of this structure is
          293  +** checked at the conclusion of each API call.  If it is all zero, it
          294  +** is deallocated.
   290    295   */
   291    296   struct ThreadData {
   292         -  u8 isInit;               /* True if structure has been initialised */
   293         -  u8 mallocFailed;         /* True after a malloc() has failed */
          297  +  int mallocFailed;        /* True after a malloc() has failed */
          298  +  int nRef;                /* Number of users */
   294    299   
   295         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
   296         -  u8 useMemoryManagement;  /* True if memory-management is enabled */
          300  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   297    301     int nSoftHeapLimit;      /* Suggested max mem allocation.  No limit if <0 */
   298    302     int nAlloc;              /* Number of bytes currently allocated */
   299    303     Pager *pPager;           /* Linked list of all pagers in this thread */
   300    304   #endif
   301    305   
   302    306   #ifndef SQLITE_OMIT_SHARED_CACHE
   303    307     u8 useSharedData;        /* True if shared pagers and schemas are enabled */
   304    308     BtShared *pBtree;        /* Linked list of all currently open BTrees */
   305    309   #endif
   306    310   
   307    311   #ifdef SQLITE_MEMDEBUG
   308    312     int nMaxAlloc;           /* High water mark of ThreadData.nAlloc */
   309         -  int mallocAllowed;       /* assert() in sqlite3Malloc() if not set */
          313  +  int mallocDisallowed;    /* assert() in sqlite3Malloc() if set */
   310    314     int isFail;              /* True if all malloc() calls should fail */
   311    315     const char *zFile;       /* Filename to associate debugging info with */
   312    316     int iLine;               /* Line number to associate debugging info with */
   313    317     void *pFirst;            /* Pointer to linked list of allocations */
   314    318   #endif
   315    319   };
   316    320   
................................................................................
   393    397     int schema_cookie;   /* Database schema version number for this file */
   394    398     Hash tblHash;        /* All tables indexed by name */
   395    399     Hash idxHash;        /* All (named) indices indexed by name */
   396    400     Hash trigHash;       /* All triggers indexed by name */
   397    401     Hash aFKey;          /* Foreign keys indexed by to-table */
   398    402     Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
   399    403     u8 file_format;      /* Schema format version for this file */
          404  +  u8 enc;              /* Text encoding used by this database */
   400    405     u16 flags;           /* Flags associated with this schema */
   401    406     int cache_size;      /* Number of pages to use in the cache */
   402         -  u8 enc;              /* Text encoding used by this database */
   403    407   };
   404    408   
   405    409   /*
   406    410   ** These macros can be used to test, set, or clear bits in the 
   407    411   ** Db.flags field.
   408    412   */
   409    413   #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
................................................................................
   419    423   **
   420    424   ** DB_UnresetViews means that one or more views have column names that
   421    425   ** have been filled out.  If the schema changes, these column names might
   422    426   ** changes and so the view will need to be reset.
   423    427   */
   424    428   #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
   425    429   #define DB_UnresetViews    0x0002  /* Some views have defined column names */
          430  +#define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
   426    431   
   427    432   #define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
   428    433   
   429    434   /*
   430    435   ** Each database is an instance of the following structure.
   431    436   **
   432    437   ** The sqlite.lastRowid records the last insert rowid generated by an
................................................................................
   518    523   ** Possible values for the sqlite.flags and or Db.flags fields.
   519    524   **
   520    525   ** On sqlite.flags, the SQLITE_InTrans value means that we have
   521    526   ** executed a BEGIN.  On Db.flags, SQLITE_InTrans means a statement
   522    527   ** transaction is active on that particular database file.
   523    528   */
   524    529   #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
   525         -#define SQLITE_Initialized    0x00000002  /* True after initialization */
   526    530   #define SQLITE_Interrupt      0x00000004  /* Cancel current operation */
   527    531   #define SQLITE_InTrans        0x00000008  /* True if in a transaction */
   528    532   #define SQLITE_InternChanges  0x00000010  /* Uncommitted Hash table changes */
   529    533   #define SQLITE_FullColNames   0x00000020  /* Show full column names on SELECT */
   530    534   #define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
   531    535   #define SQLITE_CountRows      0x00000080  /* Count rows changed by INSERT, */
   532    536                                             /*   DELETE, or UPDATE and return */
................................................................................
   536    540   #define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
   537    541   #define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
   538    542   #define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
   539    543   #define SQLITE_NoReadlock     0x00001000  /* Readlocks are omitted when 
   540    544                                             ** accessing read-only databases */
   541    545   #define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
   542    546   #define SQLITE_ReadUncommitted 0x00004000  /* For shared-cache mode */
          547  +#define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
   543    548   
   544    549   /*
   545    550   ** Possible values for the sqlite.magic field.
   546    551   ** The numbers are obtained at random and have no special meaning, other
   547    552   ** than being distinct from one another.
   548    553   */
   549    554   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
................................................................................
  1248   1253     int nErr;            /* Number of errors seen */
  1249   1254     int nTab;            /* Number of previously allocated VDBE cursors */
  1250   1255     int nMem;            /* Number of memory cells used so far */
  1251   1256     int nSet;            /* Number of sets used so far */
  1252   1257     int ckOffset;        /* Stack offset to data used by CHECK constraints */
  1253   1258     u32 writeMask;       /* Start a write transaction on these databases */
  1254   1259     u32 cookieMask;      /* Bitmask of schema verified databases */
         1260  +  ThreadData *pTsd;    /* Thread specific data for this thread */
  1255   1261     int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  1256   1262     int cookieValue[MAX_ATTACHED+2];  /* Values of cookies to verify */
  1257   1263   #ifndef SQLITE_OMIT_SHARED_CACHE
  1258   1264     int nTableLock;        /* Number of locks in aTableLock */
  1259   1265     TableLock *aTableLock; /* Required table locks for shared-cache mode */
  1260   1266   #endif
  1261   1267   
................................................................................
  1726   1732   void sqlite3Analyze(Parse*, Token*, Token*);
  1727   1733   int sqlite3InvokeBusyHandler(BusyHandler*);
  1728   1734   int sqlite3FindDb(sqlite3*, Token*);
  1729   1735   void sqlite3AnalysisLoad(sqlite3*,int iDB);
  1730   1736   void sqlite3DefaultRowEst(Index*);
  1731   1737   void sqlite3RegisterLikeFunctions(sqlite3*, int);
  1732   1738   int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
  1733         -ThreadData *sqlite3ThreadData();
         1739  +ThreadData *sqlite3ThreadData(void);
         1740  +const ThreadData *sqlite3ThreadDataReadOnly(void);
         1741  +void sqlite3ReleaseThreadData(void);
  1734   1742   void sqlite3AttachFunctions(sqlite3 *);
  1735   1743   void sqlite3MinimumFileFormat(Parse*, int, int);
  1736   1744   void sqlite3SchemaFree(void *);
  1737   1745   Schema *sqlite3SchemaGet(Btree *);
  1738   1746   int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
  1739   1747   KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
  1740   1748   

Changes to SQLite.Interop/src/tclsqlite.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** A TCL Interface to SQLite
    13     13   **
    14         -** $Id: tclsqlite.c,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           14  +** $Id: tclsqlite.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
    15     15   */
    16     16   #ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "hash.h"
    20     20   #include "tcl.h"
    21     21   #include <stdlib.h>

Changes to SQLite.Interop/src/tokenize.c.

    11     11   *************************************************************************
    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that splits an SQL input string up into
    15     15   ** individual tokens and sends those tokens one-by-one over to the
    16     16   ** parser for analysis.
    17     17   **
    18         -** $Id: tokenize.c,v 1.16 2006/01/11 03:22:30 rmsimpson Exp $
           18  +** $Id: tokenize.c,v 1.17 2006/01/12 20:54:08 rmsimpson Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   #include <ctype.h>
    23     23   #include <stdlib.h>
    24     24   
    25     25   /*
................................................................................
   257    257         }
   258    258         /* Fall through into the next case if the '#' is not followed by
   259    259         ** a digit. Try to match #AAAA where AAAA is a parameter name. */
   260    260       }
   261    261   #ifndef SQLITE_OMIT_TCL_VARIABLE
   262    262       case '$':
   263    263   #endif
   264         -    case '@': case ':': {
          264  +    case '@':
          265  +    case ':': {
   265    266         int n = 0;
   266    267         *tokenType = TK_VARIABLE;
   267    268         for(i=1; (c=z[i])!=0; i++){
   268    269           if( IdChar(c) ){
   269    270             n++;
   270    271   #ifndef SQLITE_OMIT_TCL_VARIABLE
   271    272           }else if( c=='(' && n>0 ){
................................................................................
   335    336   int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
   336    337     int nErr = 0;
   337    338     int i;
   338    339     void *pEngine;
   339    340     int tokenType;
   340    341     int lastTokenParsed = -1;
   341    342     sqlite3 *db = pParse->db;
          343  +  ThreadData *pTsd = pParse->pTsd;
   342    344     extern void *sqlite3ParserAlloc(void*(*)(int));
   343    345     extern void sqlite3ParserFree(void*, void(*)(void*));
   344    346     extern int sqlite3Parser(void*, int, Token, Parse*);
   345    347   
   346    348     db->flags &= ~SQLITE_Interrupt;
   347    349     pParse->rc = SQLITE_OK;
   348    350     i = 0;
................................................................................
   354    356     assert( pParse->pNewTable==0 );
   355    357     assert( pParse->pNewTrigger==0 );
   356    358     assert( pParse->nVar==0 );
   357    359     assert( pParse->nVarExpr==0 );
   358    360     assert( pParse->nVarExprAlloc==0 );
   359    361     assert( pParse->apVarExpr==0 );
   360    362     pParse->zTail = pParse->zSql = zSql;
   361         -  while( sqlite3ThreadData()->mallocFailed==0 && zSql[i]!=0 ){
          363  +  while( pTsd->mallocFailed==0 && zSql[i]!=0 ){
   362    364       assert( i>=0 );
   363    365       pParse->sLastToken.z = (u8*)&zSql[i];
   364    366       assert( pParse->sLastToken.dyn==0 );
   365    367       pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
   366    368       i += pParse->sLastToken.n;
   367    369       switch( tokenType ){
   368    370         case TK_SPACE:
................................................................................
   402    404       if( lastTokenParsed!=TK_SEMI ){
   403    405         sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
   404    406         pParse->zTail = &zSql[i];
   405    407       }
   406    408       sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
   407    409     }
   408    410     sqlite3ParserFree(pEngine, sqlite3FreeX);
   409         -  if( sqlite3ThreadData()->mallocFailed ){
          411  +  if( pTsd->mallocFailed ){
   410    412       pParse->rc = SQLITE_NOMEM;
   411    413     }
   412    414     if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
   413    415       sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
   414    416     }
   415    417     if( pParse->zErrMsg ){
   416    418       if( pzErrMsg && *pzErrMsg==0 ){

Changes to SQLite.Interop/src/trigger.c.

    77     77     }
    78     78   
    79     79     /* If the trigger name was unqualified, and the table is a temp table,
    80     80     ** then set iDb to 1 to create the trigger in the temporary database.
    81     81     ** If sqlite3SrcListLookup() returns 0, indicating the table does not
    82     82     ** exist, the error is caught by the block below.
    83     83     */
    84         -  if( !pTableName || sqlite3ThreadData()->mallocFailed ) goto trigger_cleanup;
           84  +  if( !pTableName || sqlite3ThreadDataReadOnly()->mallocFailed ){
           85  +    goto trigger_cleanup;
           86  +  }
    85     87     pTab = sqlite3SrcListLookup(pParse, pTableName);
    86     88     if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
    87     89       iDb = 1;
    88     90     }
    89     91   
    90     92     /* Ensure the table name matches database name and that the table exists */
    91         -  if( sqlite3ThreadData()->mallocFailed ) goto trigger_cleanup;
           93  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto trigger_cleanup;
    92     94     assert( pTableName->nSrc==1 );
    93     95     if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
    94     96         sqlite3FixSrcList(&sFix, pTableName) ){
    95     97       goto trigger_cleanup;
    96     98     }
    97     99     pTab = sqlite3SrcListLookup(pParse, pTableName);
    98    100     if( !pTab ){
................................................................................
   251    253     if( db->init.busy ){
   252    254       int n;
   253    255       Table *pTab;
   254    256       Trigger *pDel;
   255    257       pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash, 
   256    258                        pTrig->name, strlen(pTrig->name)+1, pTrig);
   257    259       if( pDel ){
   258         -      assert( sqlite3ThreadData()->mallocFailed && pDel==pTrig );
          260  +      assert( sqlite3ThreadDataReadOnly()->mallocFailed && pDel==pTrig );
   259    261         goto triggerfinish_cleanup;
   260    262       }
   261    263       n = strlen(pTrig->table) + 1;
   262    264       pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
   263    265       assert( pTab!=0 );
   264    266       pTrig->pNext = pTab->pTrigger;
   265    267       pTab->pTrigger = pTrig;
................................................................................
   435    437     Trigger *pTrigger = 0;
   436    438     int i;
   437    439     const char *zDb;
   438    440     const char *zName;
   439    441     int nName;
   440    442     sqlite3 *db = pParse->db;
   441    443   
   442         -  if( sqlite3ThreadData()->mallocFailed ) goto drop_trigger_cleanup;
          444  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) goto drop_trigger_cleanup;
   443    445     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   444    446       goto drop_trigger_cleanup;
   445    447     }
   446    448   
   447    449     assert( pName->nSrc==1 );
   448    450     zDb = pName->a[0].zDatabase;
   449    451     zName = pName->a[0].zName;

Changes to SQLite.Interop/src/update.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle UPDATE statements.
    14     14   **
    15         -** $Id: update.c,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           15  +** $Id: update.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** The most recently coded instruction was an OP_Column to retrieve the
    21     21   ** i-th column of table pTab. This routine sets the P3 parameter of the 
    22     22   ** OP_Column to the default value, if any.
................................................................................
    96     96     int triggers_exist = 0;      /* True if any row triggers exist */
    97     97   #endif
    98     98   
    99     99     int newIdx      = -1;  /* index of trigger "new" temp table       */
   100    100     int oldIdx      = -1;  /* index of trigger "old" temp table       */
   101    101   
   102    102     sContext.pParse = 0;
   103         -  if( pParse->nErr || sqlite3ThreadData()->mallocFailed ) goto update_cleanup;
          103  +  if( pParse->nErr || sqlite3ThreadDataReadOnly()->mallocFailed ){
          104  +    goto update_cleanup;
          105  +  }
   104    106     db = pParse->db;
   105    107     assert( pTabList->nSrc==1 );
   106    108   
   107    109     /* Locate the table which we want to update. 
   108    110     */
   109    111     pTab = sqlite3SrcListLookup(pParse, pTabList);
   110    112     if( pTab==0 ) goto update_cleanup;

Changes to SQLite.Interop/src/utf.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used to translate between UTF-8, 
    13     13   ** UTF-16, UTF-16BE, and UTF-16LE.
    14     14   **
    15         -** $Id: utf.c,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           15  +** $Id: utf.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
    16     16   **
    17     17   ** Notes on UTF-8:
    18     18   **
    19     19   **   Byte-0    Byte-1    Byte-2    Byte-3    Value
    20     20   **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
    21     21   **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
    22     22   **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx

Changes to SQLite.Interop/src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.14 2006/01/11 03:22:30 rmsimpson Exp $
           17  +** $Id: util.c,v 1.15 2006/01/12 20:54:08 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <stdarg.h>
    22     22   #include <ctype.h>
    23     23   
    24     24   /*
................................................................................
    62     62   **     * Guardposts to detect overwrites.
    63     63   **     * Ability to cause a specific Malloc() or Realloc() to fail.
    64     64   **     * Audit outstanding memory allocations (i.e check for leaks).
    65     65   */
    66     66   
    67     67   #define MAX(x,y) ((x)>(y)?(x):(y))
    68     68   
    69         -#if !defined(SQLITE_OMIT_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
           69  +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
    70     70   /*
    71     71   ** Set the soft heap-size limit for the current thread. Passing a negative
    72     72   ** value indicates no limit.
    73     73   */
    74     74   void sqlite3_soft_heap_limit(int n){
    75     75     sqlite3ThreadData()->nSoftHeapLimit = n;
           76  +  sqlite3ReleaseThreadData();
    76     77   }
    77     78   
    78     79   /*
    79     80   ** Release memory held by SQLite instances created by the current thread.
    80     81   */
    81     82   int sqlite3_release_memory(int n){
    82     83     return sqlite3pager_release_memory(n);
    83     84   }
    84     85   #else
    85         -/* If SQLITE_OMIT_MEMORY_MANAGEMENT is defined, then define a version
           86  +/* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version
    86     87   ** of sqlite3_release_memory() to be used by other code in this file.
    87     88   ** This is done for no better reason than to reduce the number of 
    88     89   ** pre-processor #ifndef statements.
    89     90   */
    90     91   #define sqlite3_release_memory(x) 0    /* 0 == no memory freed */
    91     92   #endif
    92     93   
................................................................................
   117    118   #endif
   118    119   
   119    120   /*
   120    121   ** Number of 32-bit guard words.  This should probably be a multiple of
   121    122   ** 2 since on 64-bit machines we want the value returned by sqliteMalloc()
   122    123   ** to be 8-byte aligned.
   123    124   */
   124         -#define TESTALLOC_NGUARD 2
          125  +#ifndef TESTALLOC_NGUARD
          126  +# define TESTALLOC_NGUARD 2
          127  +#endif
   125    128   
   126    129   /*
   127    130   ** Size reserved for storing file-name along with each malloc()ed blob.
   128    131   */
   129    132   #define TESTALLOC_FILESIZE 64
   130    133   
   131    134   /*
................................................................................
   430    433   }
   431    434   #endif
   432    435   
   433    436   /*
   434    437   ** This is the test layer's wrapper around sqlite3OsMalloc().
   435    438   */
   436    439   static void * OSMALLOC(int n){
   437         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
          440  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   438    441     ThreadData *pTsd = sqlite3ThreadData();
   439    442     pTsd->nMaxAlloc = MAX(pTsd->nMaxAlloc, pTsd->nAlloc);
   440    443   #endif
          444  +  assert( !sqlite3ThreadData()->mallocDisallowed );
   441    445     if( !failMalloc() ){
   442    446       u32 *p;
   443    447       p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
   444    448       assert(p);
   445    449       sqlite3_nMalloc++;
   446    450       applyGuards(p);
   447    451       linkAlloc(p);
................................................................................
   471    475     sqlite3_nFree++;
   472    476   }
   473    477   
   474    478   /*
   475    479   ** This is the test layer's wrapper around sqlite3OsRealloc().
   476    480   */
   477    481   static void * OSREALLOC(void *pRealloc, int n){
   478         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
          482  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   479    483     ThreadData *pTsd = sqlite3ThreadData();
   480    484     pTsd->nMaxAlloc = MAX(pTsd->nMaxAlloc, pTsd->nAlloc);
   481    485   #endif
          486  +  assert( !sqlite3ThreadData()->mallocDisallowed );
   482    487     if( !failMalloc() ){
   483    488       u32 *p = (u32 *)getOsPointer(pRealloc);
   484    489       checkGuards(p);
   485    490       p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
   486    491       applyGuards(p);
   487    492       relinkAlloc(p);
   488    493       return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
................................................................................
   500    505   */
   501    506   #define OSMALLOC(x)        sqlite3OsMalloc(x)
   502    507   #define OSREALLOC(x,y)     sqlite3OsRealloc(x,y)
   503    508   #define OSFREE(x)          sqlite3OsFree(x)
   504    509   #define OSSIZEOF(x)        sqlite3OsAllocationSize(x)
   505    510   #define OSMALLOC_FAILED()
   506    511   
   507         -#endif
          512  +#endif  /* SQLITE_MEMDEBUG */
   508    513   /*
   509    514   ** End code for memory allocation system test layer.
   510    515   **--------------------------------------------------------------------------*/
   511    516   
   512    517   /*
   513    518   ** The handleSoftLimit() function is called before each call to 
   514    519   ** sqlite3OsMalloc() or xRealloc(). The parameter 'n' is the number of
................................................................................
   515    520   ** extra bytes about to be allocated (for Realloc() this means the size of the
   516    521   ** new allocation less the size of the old allocation). If the extra allocation
   517    522   ** means that the total memory allocated to SQLite in this thread would exceed
   518    523   ** the limit set by sqlite3_soft_heap_limit(), then sqlite3_release_memory() is
   519    524   ** called to try to avoid this. No indication of whether or not this is
   520    525   ** successful is returned to the caller.
   521    526   **
   522         -** If SQLITE_OMIT_MEMORY_MANAGEMENT is defined, this function is a no-op.
          527  +** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
          528  +** a no-op
   523    529   */
   524         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
          530  +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   525    531   static void handleSoftLimit(int n){
   526    532     ThreadData *pTsd = sqlite3ThreadData();
   527    533     pTsd->nAlloc += n;
          534  +  assert( pTsd->nAlloc>=0 );
   528    535     if( n>0 && pTsd->nSoftHeapLimit>0 ){
   529    536       while( pTsd->nAlloc>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) );
          537  +  }else if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
          538  +    sqlite3ReleaseThreadData();
   530    539     }
   531    540   }
   532    541   #else
   533    542   #define handleSoftLimit(x)
   534    543   #endif
   535    544   
   536    545   /*
   537    546   ** Allocate and return N bytes of uninitialised memory by calling
   538    547   ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory 
   539    548   ** by calling sqlite3_release_memory().
   540    549   */
   541    550   void *sqlite3MallocRaw(int n){
   542         -  ThreadData *pTsd = sqlite3ThreadData();
   543    551     void *p = 0;
   544         -  if( n>0 && !pTsd->mallocFailed ){
          552  +  if( n>0 && !sqlite3ThreadDataReadOnly()->mallocFailed ){
   545    553       handleSoftLimit(n);
   546    554       while( !(p = OSMALLOC(n)) && sqlite3_release_memory(n) );
   547    555       if( !p ){
   548    556         /* If the allocation failed, call handleSoftLimit() again, this time
   549    557         ** with the additive inverse of the argument passed to 
   550    558         ** handleSoftLimit() above. This is so the ThreadData.nAlloc variable is
   551    559         ** still correct after a malloc() failure. 
................................................................................
   560    568   
   561    569   /*
   562    570   ** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The
   563    571   ** pointer to the new allocation is returned.  If the Realloc() call fails,
   564    572   ** attempt to free memory by calling sqlite3_release_memory().
   565    573   */
   566    574   void *sqlite3Realloc(void *p, int n){
   567         -  ThreadData *pTsd = sqlite3ThreadData();
   568         -  if( pTsd->mallocFailed ){
          575  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   569    576       return 0;
   570    577     }
   571    578   
   572    579     if( !p ){
   573    580       return sqlite3Malloc(n);
   574    581     }else{
   575    582       void *np = 0;
................................................................................
   578    585       if( !np ){
   579    586         /* If the allocation failed, call handleSoftLimit() again, this time
   580    587         ** with the additive inverse of the argument passed to 
   581    588         ** handleSoftLimit() above. This is so the ThreadData.nAlloc variable is
   582    589         ** still correct after a malloc() failure. 
   583    590         */
   584    591         handleSoftLimit(OSSIZEOF(p) - n);
   585         -      pTsd->mallocFailed = 1;
          592  +      sqlite3ThreadData()->mallocFailed = 1;
   586    593         OSMALLOC_FAILED();
   587    594       }
   588    595       return np;
   589    596     }
   590    597   }
   591    598   
   592    599   /*
................................................................................
  1302   1309   }
  1303   1310   #endif
  1304   1311   
  1305   1312   /*
  1306   1313   ** Return a pointer to the ThreadData associated with the calling thread.
  1307   1314   */
  1308   1315   ThreadData *sqlite3ThreadData(){
  1309         -  ThreadData *pTsd = sqlite3OsThreadSpecificData(sizeof(ThreadData));
  1310         -  if( pTsd && !pTsd->isInit ){
  1311         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  1312         -    pTsd->nSoftHeapLimit = -1;
  1313         -#endif
  1314         -#ifndef NDEBUG
  1315         -    pTsd->mallocAllowed = 1;
  1316         -#endif
  1317         -    pTsd->isInit = 1;
  1318         -  }
  1319         -  return pTsd;
         1316  +  return (ThreadData*)sqlite3OsThreadSpecificData(1);
         1317  +}
         1318  +
         1319  +/*
         1320  +** Return a pointer to the ThreadData associated with the calling thread.
         1321  +** If no ThreadData has been allocated to this thread yet, return a pointer
         1322  +** to a substitute ThreadData structure that is all zeros. 
         1323  +*/
         1324  +const ThreadData *sqlite3ThreadDataReadOnly(){
         1325  +  static const ThreadData zeroData;
         1326  +  const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
         1327  +  return pTd ? pTd : &zeroData;
         1328  +}
         1329  +
         1330  +/*
         1331  +** Check to see if the ThreadData for this thread is all zero.  If it
         1332  +** is, then deallocate it. 
         1333  +*/
         1334  +void sqlite3ReleaseThreadData(){
         1335  +  sqlite3OsThreadSpecificData(-1);
  1320   1336   }
  1321   1337   
  1322   1338   /*
  1323   1339   ** Clear the "mallocFailed" flag. This should be invoked before exiting any
  1324   1340   ** entry points that may have called sqliteMalloc().
  1325   1341   */
  1326   1342   void sqlite3MallocClearFailed(){
  1327         -  sqlite3ThreadData()->mallocFailed = 0;
  1328         -}
  1329         -
  1330         -#ifndef SQLITE_OMIT_MEMORY_MANAGEMENT
  1331         -/*
  1332         -** Enable the shared pager and schema features.
  1333         -*/
  1334         -int sqlite3_enable_memory_management(int enable){
  1335         -  ThreadData *pTsd = sqlite3ThreadData();
  1336         -
  1337         -  /* It is only legal to call sqlite3_enable_memory_management() when there
  1338         -  ** are no currently open connections that were opened by the calling 
  1339         -  ** thread. This condition is only easy to detect if the feature were
  1340         -  ** previously enabled (and is being disabled). 
  1341         -  */
  1342         -  if( pTsd->pPager && !enable ){
  1343         -    return SQLITE_MISUSE;
         1343  +  ThreadData *pTd = sqlite3OsThreadSpecificData(0);
         1344  +  if( pTd && pTd->mallocFailed ){
         1345  +    pTd->mallocFailed = 0;
         1346  +    sqlite3OsThreadSpecificData(0);
  1344   1347     }
  1345         -  pTsd->useMemoryManagement = enable;
  1346         -  return SQLITE_OK;
  1347   1348   }
  1348         -#endif
  1349   1349   
  1350   1350   #ifndef NDEBUG
  1351   1351   /*
  1352   1352   ** This function sets a flag in the thread-specific-data structure that will
  1353   1353   ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
  1354   1354   */
  1355   1355   void sqlite3MallocDisallow(){
  1356         -  assert(sqlite3ThreadData()->mallocAllowed);
  1357         -  sqlite3ThreadData()->mallocAllowed = 0;
         1356  +  assert(!sqlite3ThreadData()->mallocDisallowed);
         1357  +  sqlite3ThreadData()->mallocDisallowed = 1;
  1358   1358   }
  1359   1359   
  1360   1360   /*
  1361   1361   ** This function clears the flag set in the thread-specific-data structure set
  1362   1362   ** by sqlite3MallocDisallow().
  1363   1363   */
  1364   1364   void sqlite3MallocAllow(){
  1365         -  assert(!sqlite3ThreadData()->mallocAllowed);
  1366         -  sqlite3ThreadData()->mallocAllowed = 1;
         1365  +  assert(sqlite3ThreadData()->mallocDisallowed);
         1366  +  sqlite3ThreadData()->mallocDisallowed = 0;
  1367   1367   }
  1368   1368   #endif

Changes to SQLite.Interop/src/vacuum.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the VACUUM command.
    13     13   **
    14     14   ** Most of the code in this file may be omitted by defining the
    15     15   ** SQLITE_OMIT_VACUUM macro.
    16     16   **
    17         -** $Id: vacuum.c,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           17  +** $Id: vacuum.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "vdbeInt.h"
    21     21   #include "os.h"
    22     22   
    23     23   #ifndef SQLITE_OMIT_VACUUM
    24     24   /*
................................................................................
    99     99     int nFilename;          /* number of characters  in zFilename[] */
   100    100     char *zTemp = 0;        /* a temporary file in same directory as zFilename */
   101    101     Btree *pMain;           /* The database being vacuumed */
   102    102     Btree *pTemp;
   103    103     char *zSql = 0;
   104    104     int rc2;  
   105    105     int saved_flags;       /* Saved value of the db->flags */
   106         -  sqlite3_stmt *pDetach = 0;
          106  +  Db *pDb = 0;           /* Database to detach at end of vacuum */
   107    107   
   108    108     /* Save the current value of the write-schema flag before setting it. */
   109    109     saved_flags = db->flags;
   110    110     db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
   111    111   
   112    112     if( !db->autoCommit ){
   113    113       sqlite3SetString(pzErrMsg, "cannot VACUUM from within a transaction", 
................................................................................
   146    146     ** that we don't even bother to set a maximum loop count.
   147    147     */
   148    148     do {
   149    149       zTemp[nFilename] = '-';
   150    150       randomName((unsigned char*)&zTemp[nFilename+1]);
   151    151     } while( sqlite3OsFileExists(zTemp) );
   152    152   
   153         -  /* Before we even attach it, compile a DETACH statement for vacuum_db. This
   154         -  ** way, if malloc() fails we can detach the database without needing to
   155         -  ** dynamically allocate memory.
   156         -  */ 
   157         -  rc = sqlite3_prepare(db, "DETACH vacuum_db", -1, &pDetach, 0);
   158         -  if( rc!=SQLITE_OK ){
   159         -    goto end_of_vacuum;
   160         -  }
   161         -
   162    153     /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
   163    154     ** can be set to 'off' for this file, as it is not recovered if a crash
   164    155     ** occurs anyway. The integrity of the database is maintained by a
   165    156     ** (possibly synchronous) transaction opened on the main database before
   166    157     ** sqlite3BtreeCopyFile() is called.
   167    158     **
   168    159     ** An optimisation would be to use a non-journaled pager.
................................................................................
   172    163       rc = SQLITE_NOMEM;
   173    164       goto end_of_vacuum;
   174    165     }
   175    166     rc = execSql(db, zSql);
   176    167     sqliteFree(zSql);
   177    168     zSql = 0;
   178    169     if( rc!=SQLITE_OK ) goto end_of_vacuum;
          170  +  pDb = &db->aDb[db->nDb-1];
   179    171     assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
   180    172     pTemp = db->aDb[db->nDb-1].pBt;
   181    173     sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain),
   182    174        sqlite3BtreeGetReserve(pMain));
   183    175     assert( sqlite3BtreeGetPageSize(pTemp)==sqlite3BtreeGetPageSize(pMain) );
   184    176     rc = execSql(db, "PRAGMA vacuum_db.synchronous=OFF");
   185    177     if( rc!=SQLITE_OK ){
................................................................................
   306    298     ** was committed at the btree level). So it safe to end the transaction
   307    299     ** by manually setting the autoCommit flag to true and detaching the
   308    300     ** vacuum database. The vacuum_db journal file is deleted when the pager
   309    301     ** is closed by the DETACH.
   310    302     */
   311    303     db->autoCommit = 1;
   312    304   
   313         -  if( pDetach ){
   314         -    int mf = sqlite3ThreadData()->mallocFailed;
   315         -    sqlite3ThreadData()->mallocFailed = 0;
          305  +  if( pDb ){
   316    306       sqlite3MallocDisallow();
   317         -    ((Vdbe *)pDetach)->expired = 0;
   318         -    sqlite3_step(pDetach);
   319         -    rc2 = sqlite3_finalize(pDetach);
   320         -    if( rc==SQLITE_OK ){
   321         -      rc = rc2;
   322         -    }
          307  +    sqlite3BtreeClose(pDb->pBt);
   323    308       sqlite3MallocAllow();
   324         -    sqlite3ThreadData()->mallocFailed = mf;
          309  +    pDb->pBt = 0;
          310  +    pDb->pSchema = 0;
   325    311     }
   326    312   
   327    313     /* If one of the execSql() calls above returned SQLITE_NOMEM, then the
   328    314     ** mallocFailed flag will be clear (because execSql() calls sqlite3_exec()).
   329    315     ** Fix this so the flag and return code match.
   330    316     */
   331    317     if( rc==SQLITE_NOMEM ){

Changes to SQLite.Interop/src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           46  +** $Id: vdbe.c,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
   375    375   int sqlite3VdbeExec(
   376    376     Vdbe *p                    /* The VDBE */
   377    377   ){
   378    378     int pc;                    /* The program counter */
   379    379     Op *pOp;                   /* Current operation */
   380    380     int rc = SQLITE_OK;        /* Value to return */
   381    381     sqlite3 *db = p->db;       /* The database */
          382  +  u8 encoding = ENC(db);     /* The database encoding */
   382    383     Mem *pTos;                 /* Top entry in the operand stack */
   383    384   #ifdef VDBE_PROFILE
   384    385     unsigned long long start;  /* CPU clock count at start of opcode */
   385    386     int origPc;                /* Program counter at start of opcode */
   386    387   #endif
   387    388   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   388    389     int nProgressOps = 0;      /* Opcodes executed since progress callback. */
   389    390   #endif
   390    391   #ifndef NDEBUG
   391    392     Mem *pStackLimit;
   392    393   #endif
          394  +  ThreadData *pTsd = sqlite3ThreadData();
   393    395   
   394    396     if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
          397  +  pTsd->nRef++;
   395    398     assert( db->magic==SQLITE_MAGIC_BUSY );
   396    399     pTos = p->pTos;
   397    400     if( p->rc==SQLITE_NOMEM ){
   398    401       /* This happens if a malloc() inside a call to sqlite3_column_text() or
   399    402       ** sqlite3_column_text16() failed.  */
   400    403       goto no_mem;
   401    404     }
................................................................................
   408    411     }
   409    412     p->resOnStack = 0;
   410    413     db->busyHandler.nBusy = 0;
   411    414     CHECK_FOR_INTERRUPT;
   412    415     for(pc=p->pc; rc==SQLITE_OK; pc++){
   413    416       assert( pc>=0 && pc<p->nOp );
   414    417       assert( pTos<=&p->aStack[pc] );
   415         -    if( sqlite3ThreadData()->mallocFailed ) goto no_mem;
          418  +    if( pTsd->mallocFailed ) goto no_mem;
   416    419   #ifdef VDBE_PROFILE
   417    420       origPc = pc;
   418    421       start = hwtime();
   419    422   #endif
   420    423       pOp = &p->aOp[pc];
   421    424   
   422    425       /* Only allow tracing if SQLITE_DEBUG is defined.
................................................................................
   588    591     p->pc = pc;
   589    592     p->errorAction = pOp->p2;
   590    593     if( pOp->p3 ){
   591    594       sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
   592    595     }
   593    596     rc = sqlite3VdbeHalt(p);
   594    597     assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
          598  +  pTsd->nRef--;
   595    599     if( rc==SQLITE_BUSY ){
   596    600       p->rc = SQLITE_BUSY;
   597    601       return SQLITE_BUSY;
   598    602     }
   599    603     return p->rc ? SQLITE_ERROR : SQLITE_DONE;
   600    604   }
   601    605   
................................................................................
   635    639     pTos++;
   636    640     pTos->flags = MEM_Str|MEM_Static|MEM_Term;
   637    641     pTos->z = pOp->p3;
   638    642     pTos->n = strlen(pTos->z);
   639    643     pTos->enc = SQLITE_UTF8;
   640    644     pTos->r = sqlite3VdbeRealValue(pTos);
   641    645     pTos->flags |= MEM_Real;
   642         -  sqlite3VdbeChangeEncoding(pTos, ENC(db));
          646  +  sqlite3VdbeChangeEncoding(pTos, encoding);
   643    647     break;
   644    648   }
   645    649   
   646    650   /* Opcode: String8 * * P3
   647    651   **
   648    652   ** P3 points to a nul terminated UTF-8 string that is P1 character long
   649    653   ** (not counting the nul terminator). This opcode is transformed
................................................................................
   651    655   */
   652    656   case OP_String8: {         /* same as TK_STRING */
   653    657     assert( pOp->p3!=0 );
   654    658     pOp->opcode = OP_String;
   655    659     pOp->p1 = strlen(pOp->p3);
   656    660   
   657    661   #ifndef SQLITE_OMIT_UTF16
   658         -  if( ENC(db)!=SQLITE_UTF8 ){
          662  +  if( encoding!=SQLITE_UTF8 ){
   659    663       pTos++;
   660    664       sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC);
   661         -    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, ENC(db)) ) goto no_mem;
          665  +    if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem;
   662    666       if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem;
   663    667       pTos->flags &= ~(MEM_Dyn);
   664    668       pTos->flags |= MEM_Static;
   665    669       if( pOp->p3type==P3_DYNAMIC ){
   666    670         sqliteFree(pOp->p3);
   667    671       }
   668    672       pOp->p3type = P3_DYNAMIC;
................................................................................
   680    684   */
   681    685   case OP_String: {
   682    686     pTos++;
   683    687     assert( pOp->p3!=0 );
   684    688     pTos->flags = MEM_Str|MEM_Static|MEM_Term;
   685    689     pTos->z = pOp->p3;
   686    690     pTos->n = pOp->p1;
   687         -  pTos->enc = ENC(db);
          691  +  pTos->enc = encoding;
   688    692     break;
   689    693   }
   690    694   
   691    695   /* Opcode: Null * * *
   692    696   **
   693    697   ** Push a NULL onto the stack.
   694    698   */
................................................................................
   884    888   
   885    889     /* Make sure the results of the current row are \000 terminated
   886    890     ** and have an assigned type.  The results are deephemeralized as
   887    891     ** as side effect.
   888    892     */
   889    893     for(; pMem<=pTos; pMem++ ){
   890    894       sqlite3VdbeMemNulTerminate(pMem);
   891         -    storeTypeInfo(pMem, ENC(db));
          895  +    storeTypeInfo(pMem, encoding);
   892    896     }
   893    897   
   894    898     /* Set up the statement structure so that it will pop the current
   895    899     ** results from the stack when the statement returns.
   896    900     */
   897    901     p->resOnStack = 1;
   898    902     p->nCallback++;
   899    903     p->popStack = pOp->p1;
   900    904     p->pc = pc + 1;
   901    905     p->pTos = pTos;
          906  +  pTsd->nRef--;
   902    907     return SQLITE_ROW;
   903    908   }
   904    909   
   905    910   /* Opcode: Concat P1 P2 *
   906    911   **
   907    912   ** Look at the first P1+2 elements of the stack.  Append them all 
   908    913   ** together with the lowest element first.  The original P1+2 elements
................................................................................
   925    930     nByte = 0;
   926    931     for(i=0; i<nField; i++, pTerm++){
   927    932       assert( pOp->p2==0 || (pTerm->flags&MEM_Str) );
   928    933       if( pTerm->flags&MEM_Null ){
   929    934         nByte = -1;
   930    935         break;
   931    936       }
   932         -    Stringify(pTerm, ENC(db));
          937  +    Stringify(pTerm, encoding);
   933    938       nByte += pTerm->n;
   934    939     }
   935    940   
   936    941     if( nByte<0 ){
   937    942       /* If nByte is less than zero, then there is a NULL value on the stack.
   938    943       ** In this case just pop the values off the stack (if required) and
   939    944       ** push on a NULL.
................................................................................
   964    969       if( pOp->p2==0 ){
   965    970         popStack(&pTos, nField);
   966    971       }
   967    972       pTos++;
   968    973       pTos->n = j;
   969    974       pTos->flags = MEM_Str|MEM_Dyn|MEM_Term;
   970    975       pTos->xDel = 0;
   971         -    pTos->enc = ENC(db);
          976  +    pTos->enc = encoding;
   972    977       pTos->z = zNew;
   973    978     }
   974    979     break;
   975    980   }
   976    981   
   977    982   /* Opcode: Add * * *
   978    983   **
................................................................................
  1138   1143   
  1139   1144     apVal = p->apArg;
  1140   1145     assert( apVal || n==0 );
  1141   1146   
  1142   1147     pArg = &pTos[1-n];
  1143   1148     for(i=0; i<n; i++, pArg++){
  1144   1149       apVal[i] = pArg;
  1145         -    storeTypeInfo(pArg, ENC(db));
         1150  +    storeTypeInfo(pArg, encoding);
  1146   1151     }
  1147   1152   
  1148   1153     assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC );
  1149   1154     if( pOp->p3type==P3_FUNCDEF ){
  1150   1155       ctx.pFunc = (FuncDef*)pOp->p3;
  1151   1156       ctx.pVdbeFunc = 0;
  1152   1157     }else{
................................................................................
  1163   1168       assert( pOp[-1].p3type==P3_COLLSEQ );
  1164   1169       assert( pOp[-1].opcode==OP_CollSeq );
  1165   1170       ctx.pColl = (CollSeq *)pOp[-1].p3;
  1166   1171     }
  1167   1172     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  1168   1173     (*ctx.pFunc->xFunc)(&ctx, n, apVal);
  1169   1174     if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  1170         -  if( sqlite3ThreadData()->mallocFailed ) goto no_mem;
         1175  +  if( pTsd->mallocFailed ) goto no_mem;
  1171   1176     popStack(&pTos, n);
  1172   1177   
  1173   1178     /* If any auxilary data functions have been called by this user function,
  1174   1179     ** immediately call the destructor for any non-static values.
  1175   1180     */
  1176   1181     if( ctx.pVdbeFunc ){
  1177   1182       sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
  1178   1183       pOp->p3 = (char *)ctx.pVdbeFunc;
  1179   1184       pOp->p3type = P3_VDBEFUNC;
  1180   1185     }
  1181   1186   
  1182   1187     /* Copy the result of the function to the top of the stack */
  1183         -  sqlite3VdbeChangeEncoding(&ctx.s, ENC(db));
         1188  +  sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  1184   1189     pTos++;
  1185   1190     pTos->flags = 0;
  1186   1191     sqlite3VdbeMemMove(pTos, &ctx.s);
  1187   1192   
  1188   1193     /* If the function returned an error, throw an exception */
  1189   1194     if( ctx.isError ){
  1190   1195       if( !(pTos->flags&MEM_Str) ){
  1191   1196         sqlite3SetString(&p->zErrMsg, "user function error", (char*)0);
  1192   1197       }else{
  1193   1198         sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pTos), (char*)0);
  1194         -      sqlite3VdbeChangeEncoding(pTos, ENC(db));
         1199  +      sqlite3VdbeChangeEncoding(pTos, encoding);
  1195   1200       }
  1196   1201       rc = SQLITE_ERROR;
  1197   1202     }
  1198   1203     break;
  1199   1204   }
  1200   1205   
  1201   1206   /* Opcode: BitAnd * * *
................................................................................
  1280   1285   ** convert it into the least integer that is greater than or equal to its
  1281   1286   ** current value if P1==0, or to the least integer that is strictly
  1282   1287   ** greater than its current value if P1==1.
  1283   1288   */
  1284   1289   case OP_ForceInt: {            /* no-push */
  1285   1290     i64 v;
  1286   1291     assert( pTos>=p->aStack );
  1287         -  applyAffinity(pTos, SQLITE_AFF_NUMERIC, ENC(db));
         1292  +  applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
  1288   1293     if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){
  1289   1294       Release(pTos);
  1290   1295       pTos--;
  1291   1296       pc = pOp->p2 - 1;
  1292   1297       break;
  1293   1298     }
  1294   1299     if( pTos->flags & MEM_Int ){
................................................................................
  1315   1320   **
  1316   1321   ** If the top of the stack is not an integer and P2 is not zero and
  1317   1322   ** P1 is 1, then the stack is popped.  In all other cases, the depth
  1318   1323   ** of the stack is unchanged.
  1319   1324   */
  1320   1325   case OP_MustBeInt: {            /* no-push */
  1321   1326     assert( pTos>=p->aStack );
  1322         -  applyAffinity(pTos, SQLITE_AFF_NUMERIC, ENC(db));
         1327  +  applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
  1323   1328     if( (pTos->flags & MEM_Int)==0 ){
  1324   1329       if( pOp->p2==0 ){
  1325   1330         rc = SQLITE_MISMATCH;
  1326   1331         goto abort_due_to_error;
  1327   1332       }else{
  1328   1333         if( pOp->p1 ) popStack(&pTos, 1);
  1329   1334         pc = pOp->p2 - 1;
................................................................................
  1363   1368   ** A NULL value is not changed by this routine.  It remains NULL.
  1364   1369   */
  1365   1370   case OP_ToText: {                  /* same as TK_TO_TEXT, no-push */
  1366   1371     assert( pTos>=p->aStack );
  1367   1372     if( pTos->flags & MEM_Null ) break;
  1368   1373     assert( MEM_Str==(MEM_Blob>>3) );
  1369   1374     pTos->flags |= (pTos->flags&MEM_Blob)>>3;
  1370         -  applyAffinity(pTos, SQLITE_AFF_TEXT, ENC(db));
         1375  +  applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
  1371   1376     assert( pTos->flags & MEM_Str );
  1372   1377     pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
  1373   1378     break;
  1374   1379   }
  1375   1380   
  1376   1381   /* Opcode: ToBlob * * *
  1377   1382   **
................................................................................
  1382   1387   **
  1383   1388   ** A NULL value is not changed by this routine.  It remains NULL.
  1384   1389   */
  1385   1390   case OP_ToBlob: {                  /* same as TK_TO_BLOB, no-push */
  1386   1391     assert( pTos>=p->aStack );
  1387   1392     if( pTos->flags & MEM_Null ) break;
  1388   1393     if( (pTos->flags & MEM_Blob)==0 ){
  1389         -    applyAffinity(pTos, SQLITE_AFF_TEXT, ENC(db));
         1394  +    applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
  1390   1395       assert( pTos->flags & MEM_Str );
  1391   1396       pTos->flags |= MEM_Blob;
  1392   1397     }
  1393   1398     pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str);
  1394   1399     break;
  1395   1400   }
  1396   1401   
................................................................................
  1563   1568         }
  1564   1569         break;
  1565   1570       }
  1566   1571     }
  1567   1572   
  1568   1573     affinity = pOp->p1 & 0xFF;
  1569   1574     if( affinity ){
  1570         -    applyAffinity(pNos, affinity, ENC(db));
  1571         -    applyAffinity(pTos, affinity, ENC(db));
         1575  +    applyAffinity(pNos, affinity, encoding);
         1576  +    applyAffinity(pTos, affinity, encoding);
  1572   1577     }
  1573   1578   
  1574   1579     assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 );
  1575   1580     res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3);
  1576   1581     switch( pOp->opcode ){
  1577   1582       case OP_Eq:    res = res==0;     break;
  1578   1583       case OP_Ne:    res = res!=0;     break;
................................................................................
  2064   2069         rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,&sMem);
  2065   2070         if( rc!=SQLITE_OK ){
  2066   2071           goto op_column_out;
  2067   2072         }
  2068   2073         zData = sMem.z;
  2069   2074       }
  2070   2075       sqlite3VdbeSerialGet((u8*)zData, aType[p2], pTos);
  2071         -    pTos->enc = ENC(db);
         2076  +    pTos->enc = encoding;
  2072   2077     }else{
  2073   2078       if( pOp->p3type==P3_MEM ){
  2074   2079         sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static);
  2075   2080       }else{
  2076   2081         pTos->flags = MEM_Null;
  2077   2082       }
  2078   2083     }
................................................................................
  2188   2193     file_format = p->minWriteFileFormat;
  2189   2194   
  2190   2195     /* Loop through the elements that will make up the record to figure
  2191   2196     ** out how much space is required for the new record.
  2192   2197     */
  2193   2198     for(pRec=pData0; pRec<=pTos; pRec++){
  2194   2199       if( zAffinity ){
  2195         -      applyAffinity(pRec, zAffinity[pRec-pData0], ENC(db));
         2200  +      applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
  2196   2201       }
  2197   2202       if( pRec->flags&MEM_Null ){
  2198   2203         containsNull = 1;
  2199   2204       }
  2200   2205       serial_type = sqlite3VdbeSerialType(pRec, file_format);
  2201   2206       nData += sqlite3VdbeSerialTypeLen(serial_type);
  2202   2207       nHdr += sqlite3VarintLen(serial_type);
................................................................................
  2322   2327       ** still running, and a transaction is active, return an error indicating
  2323   2328       ** that the other VMs must complete first. 
  2324   2329       */
  2325   2330       sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit", 
  2326   2331           " transaction - SQL statements in progress", (char*)0);
  2327   2332       rc = SQLITE_ERROR;
  2328   2333     }else if( i!=db->autoCommit ){
         2334  +    pTsd->nRef--;
  2329   2335       if( pOp->p2 ){
  2330   2336         assert( i==1 );
  2331   2337         sqlite3RollbackAll(db);
  2332   2338         db->autoCommit = 1;
  2333   2339       }else{
  2334   2340         db->autoCommit = i;
  2335   2341         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
................................................................................
  2381   2387   
  2382   2388     if( pBt ){
  2383   2389       rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
  2384   2390       if( rc==SQLITE_BUSY ){
  2385   2391         p->pc = pc;
  2386   2392         p->rc = SQLITE_BUSY;
  2387   2393         p->pTos = pTos;
         2394  +      pTsd->nRef--;
  2388   2395         return SQLITE_BUSY;
  2389   2396       }
  2390   2397       if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
  2391   2398         goto abort_due_to_error;
  2392   2399       }
  2393   2400     }
  2394   2401     break;
................................................................................
  2588   2595       pCur->pIncrKey = &pCur->bogusIncrKey;
  2589   2596     }
  2590   2597     switch( rc ){
  2591   2598       case SQLITE_BUSY: {
  2592   2599         p->pc = pc;
  2593   2600         p->rc = SQLITE_BUSY;
  2594   2601         p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
         2602  +      pTsd->nRef--;
  2595   2603         return SQLITE_BUSY;
  2596   2604       }
  2597   2605       case SQLITE_OK: {
  2598   2606         int flags = sqlite3BtreeFlags(pCur->pCursor);
  2599   2607         /* Sanity checking.  Only the lower four bits of the flags byte should
  2600   2608         ** be used.  Bit 3 (mask 0x08) is unpreditable.  The lower 3 bits
  2601   2609         ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
................................................................................
  2796   2804         if( rc!=SQLITE_OK ){
  2797   2805           goto abort_due_to_error;
  2798   2806         }
  2799   2807         pC->lastRowid = pTos->i;
  2800   2808         pC->rowidIsValid = res==0;
  2801   2809       }else{
  2802   2810         assert( pTos->flags & MEM_Blob );
  2803         -      /* Stringify(pTos, ENC(db)); */
         2811  +      /* Stringify(pTos, encoding); */
  2804   2812         rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
  2805   2813         if( rc!=SQLITE_OK ){
  2806   2814           goto abort_due_to_error;
  2807   2815         }
  2808   2816         pC->rowidIsValid = 0;
  2809   2817       }
  2810   2818       pC->deferredMoveto = 0;
................................................................................
  2901   2909     Cursor *pC;
  2902   2910     assert( pTos>=p->aStack );
  2903   2911     assert( i>=0 && i<p->nCursor );
  2904   2912     assert( p->apCsr[i]!=0 );
  2905   2913     if( (pC = p->apCsr[i])->pCursor!=0 ){
  2906   2914       int res, rx;
  2907   2915       assert( pC->isTable==0 );
  2908         -    Stringify(pTos, ENC(db));
         2916  +    Stringify(pTos, encoding);
  2909   2917       rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
  2910   2918       alreadyExists = rx==SQLITE_OK && res==0;
  2911   2919       pC->deferredMoveto = 0;
  2912   2920       pC->cacheStatus = CACHE_STALE;
  2913   2921     }
  2914   2922     if( pOp->opcode==OP_Found ){
  2915   2923       if( alreadyExists ) pc = pOp->p2 - 1;
................................................................................
  2968   2976       char *zKey;    /* The value of K */
  2969   2977       int nKey;      /* Number of bytes in K */
  2970   2978       int len;       /* Number of bytes in K without the rowid at the end */
  2971   2979       int szRowid;   /* Size of the rowid column at the end of zKey */
  2972   2980   
  2973   2981       /* Make sure K is a string and make zKey point to K
  2974   2982       */
  2975         -    Stringify(pNos, ENC(db));
         2983  +    Stringify(pNos, encoding);
  2976   2984       zKey = pNos->z;
  2977   2985       nKey = pNos->n;
  2978   2986   
  2979   2987       szRowid = sqlite3VdbeIdxRowidLen(nKey, (u8*)zKey);
  2980   2988       len = nKey-szRowid;
  2981   2989   
  2982   2990       /* Search for an entry in P1 where all but the last four bytes match K.
................................................................................
  3799   3807     assert( i>=0 && i<p->nCursor );
  3800   3808     assert( p->apCsr[i]!=0 );
  3801   3809     assert( pTos>=p->aStack );
  3802   3810     if( (pC = p->apCsr[i])->pCursor!=0 ){
  3803   3811       int res, rc;
  3804   3812    
  3805   3813       assert( pTos->flags & MEM_Blob );  /* Created using OP_Make*Key */
  3806         -    Stringify(pTos, ENC(db));
         3814  +    Stringify(pTos, encoding);
  3807   3815       assert( pC->deferredMoveto==0 );
  3808   3816       *pC->pIncrKey = pOp->p3!=0;
  3809   3817       assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT );
  3810   3818       rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, (u8*)pTos->z, &res);
  3811   3819       *pC->pIncrKey = 0;
  3812   3820       if( rc!=SQLITE_OK ){
  3813   3821         break;
................................................................................
  4016   4024     zSql = sqlite3MPrintf(
  4017   4025        "SELECT name, rootpage, sql, %d FROM '%q'.%s WHERE %s",
  4018   4026        pOp->p1, db->aDb[iDb].zName, zMaster, pOp->p3);
  4019   4027     if( zSql==0 ) goto no_mem;
  4020   4028     sqlite3SafetyOff(db);
  4021   4029     assert( db->init.busy==0 );
  4022   4030     db->init.busy = 1;
  4023         -  assert(0==sqlite3ThreadData()->mallocFailed);
         4031  +  assert(0==pTsd->mallocFailed);
  4024   4032     rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
  4025   4033     sqliteFree(zSql);
  4026   4034     db->init.busy = 0;
  4027   4035     sqlite3SafetyOn(db);
  4028   4036     if( rc==SQLITE_NOMEM ){
  4029         -    sqlite3ThreadData()->mallocFailed = 1;
         4037  +    pTsd->mallocFailed = 1;
  4030   4038       goto no_mem;
  4031   4039     }
  4032   4040     break;  
  4033   4041   }
  4034   4042   
  4035   4043   #ifndef SQLITE_OMIT_ANALYZE
  4036   4044   /* Opcode: LoadAnalysis P1 * *
................................................................................
  4128   4136     }else{
  4129   4137       pTos->z = z;
  4130   4138       pTos->n = strlen(z);
  4131   4139       pTos->flags = MEM_Str | MEM_Dyn | MEM_Term;
  4132   4140       pTos->xDel = 0;
  4133   4141     }
  4134   4142     pTos->enc = SQLITE_UTF8;
  4135         -  sqlite3VdbeChangeEncoding(pTos, ENC(db));
         4143  +  sqlite3VdbeChangeEncoding(pTos, encoding);
  4136   4144     sqliteFree(aRoot);
  4137   4145     break;
  4138   4146   }
  4139   4147   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  4140   4148   
  4141   4149   /* Opcode: FifoWrite * * *
  4142   4150   **
................................................................................
  4404   4412     assert( n>=0 );
  4405   4413     pRec = &pTos[1-n];
  4406   4414     assert( pRec>=p->aStack );
  4407   4415     apVal = p->apArg;
  4408   4416     assert( apVal || n==0 );
  4409   4417     for(i=0; i<n; i++, pRec++){
  4410   4418       apVal[i] = pRec;
  4411         -    storeTypeInfo(pRec, ENC(db));
         4419  +    storeTypeInfo(pRec, encoding);
  4412   4420     }
  4413   4421     ctx.pFunc = (FuncDef*)pOp->p3;
  4414   4422     assert( pOp->p1>=0 && pOp->p1<p->nMem );
  4415   4423     ctx.pMem = pMem = &p->aMem[pOp->p1];
  4416   4424     pMem->n++;
  4417   4425     ctx.isError = 0;
  4418   4426     ctx.pColl = 0;
................................................................................
  4549   4557       ** of the program.  It is only here for testing and debugging.
  4550   4558       ** On the other hand, it does burn CPU cycles every time through
  4551   4559       ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
  4552   4560       */
  4553   4561   #ifndef NDEBUG
  4554   4562       /* Sanity checking on the top element of the stack */
  4555   4563       if( pTos>=p->aStack ){
  4556         -      sqlite3VdbeMemSanity(pTos, ENC(db));
         4564  +      sqlite3VdbeMemSanity(pTos, encoding);
  4557   4565       }
  4558   4566       assert( pc>=-1 && pc<p->nOp );
  4559   4567   #ifdef SQLITE_DEBUG
  4560   4568       /* Code for tracing the vdbe stack. */
  4561   4569       if( p->trace && pTos>=p->aStack ){
  4562   4570         int i;
  4563   4571         fprintf(p->trace, "Stack:");
................................................................................
  4591   4599       p->rc = rc;
  4592   4600       rc = SQLITE_ERROR;
  4593   4601     }else{
  4594   4602       rc = SQLITE_DONE;
  4595   4603     }
  4596   4604     sqlite3VdbeHalt(p);
  4597   4605     p->pTos = pTos;
         4606  +  pTsd->nRef--;
  4598   4607     return rc;
  4599   4608   
  4600   4609     /* Jump to here if a malloc() fails.  It's hard to get a malloc()
  4601   4610     ** to fail on a modern VM computer, so this code is untested.
  4602   4611     */
  4603   4612   no_mem:
  4604   4613     sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
................................................................................
  4612   4621     /* Fall thru into abort_due_to_error */
  4613   4622   
  4614   4623     /* Jump to here for any other kind of fatal error.  The "rc" variable
  4615   4624     ** should hold the error number.
  4616   4625     */
  4617   4626   abort_due_to_error:
  4618   4627     if( p->zErrMsg==0 ){
  4619         -    if( sqlite3ThreadData()->mallocFailed ) rc = SQLITE_NOMEM;
         4628  +    if( pTsd->mallocFailed ) rc = SQLITE_NOMEM;
  4620   4629       sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
  4621   4630     }
  4622   4631     goto vdbe_halt;
  4623   4632   
  4624   4633     /* Jump to here if the sqlite3_interrupt() API sets the interrupt
  4625   4634     ** flag.
  4626   4635     */

Changes to SQLite.Interop/src/vdbe.h.

    11     11   *************************************************************************
    12     12   ** Header file for the Virtual DataBase Engine (VDBE)
    13     13   **
    14     14   ** This header defines the interface to the virtual database engine
    15     15   ** or VDBE.  The VDBE implements an abstract machine that runs a
    16     16   ** simple program to access and modify the underlying database.
    17     17   **
    18         -** $Id: vdbe.h,v 1.13 2006/01/11 03:22:30 rmsimpson Exp $
           18  +** $Id: vdbe.h,v 1.14 2006/01/12 20:54:08 rmsimpson Exp $
    19     19   */
    20     20   #ifndef _SQLITE_VDBE_H_
    21     21   #define _SQLITE_VDBE_H_
    22     22   #include <stdio.h>
    23     23   
    24     24   /*
    25     25   ** A single VDBE is an opaque structure named "Vdbe".  Only routines

Changes to SQLite.Interop/src/vdbeapi.c.

   152    152   ** statement is completely executed or an error occurs.
   153    153   */
   154    154   int sqlite3_step(sqlite3_stmt *pStmt){
   155    155     Vdbe *p = (Vdbe*)pStmt;
   156    156     sqlite3 *db;
   157    157     int rc;
   158    158   
   159         -  assert(!sqlite3ThreadData()->mallocFailed);
          159  +  assert(!sqlite3ThreadDataReadOnly()->mallocFailed);
   160    160   
   161    161     if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
   162    162       return SQLITE_MISUSE;
   163    163     }
   164    164     if( p->aborted ){
   165    165       return SQLITE_ABORT;
   166    166     }
................................................................................
   400    400   static void columnMallocFailure(sqlite3_stmt *pStmt)
   401    401   {
   402    402     /* If malloc() failed during an encoding conversion within an
   403    403     ** sqlite3_column_XXX API, then set the return code of the statement to
   404    404     ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
   405    405     ** and _finalize() will return NOMEM.
   406    406     */
   407         -  if( sqlite3ThreadData()->mallocFailed ){
          407  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   408    408       ((Vdbe *)pStmt)->rc = SQLITE_NOMEM;
   409    409       sqlite3MallocClearFailed();
   410    410     }
   411    411   }
   412    412   
   413    413   /**************************** sqlite3_column_  *******************************
   414    414   ** The following routines are used to access elements of the current row

Changes to SQLite.Interop/src/vdbeaux.c.

    98     98     int i;
    99     99     VdbeOp *pOp;
   100    100   
   101    101     i = p->nOp;
   102    102     p->nOp++;
   103    103     assert( p->magic==VDBE_MAGIC_INIT );
   104    104     resizeOpArray(p, i+1);
   105         -  if( sqlite3ThreadData()->mallocFailed ){
          105  +  if( p->aOp==0 || p->nOp<=i ){
   106    106       return 0;
   107    107     }
   108    108     pOp = &p->aOp[i];
   109    109     pOp->opcode = op;
   110    110     pOp->p1 = p1;
   111    111     pOp->p2 = p2;
   112    112     pOp->p3 = 0;
................................................................................
   297    297   ** Add a whole list of operations to the operation stack.  Return the
   298    298   ** address of the first operation added.
   299    299   */
   300    300   int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
   301    301     int addr;
   302    302     assert( p->magic==VDBE_MAGIC_INIT );
   303    303     resizeOpArray(p, p->nOp + nOp);
   304         -  if( sqlite3ThreadData()->mallocFailed ){
          304  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
   305    305       return 0;
   306    306     }
   307    307     addr = p->nOp;
   308    308     if( nOp>0 ){
   309    309       int i;
   310    310       VdbeOpList const *pIn = aOp;
   311    311       for(i=0; i<nOp; i++, pIn++){
................................................................................
   411    411   ** the Vdbe. In these cases we can just copy the pointer.
   412    412   **
   413    413   ** If addr<0 then change P3 on the most recently inserted instruction.
   414    414   */
   415    415   void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
   416    416     Op *pOp;
   417    417     assert( p->magic==VDBE_MAGIC_INIT );
   418         -  if( p==0 || p->aOp==0 || sqlite3ThreadData()->mallocFailed ){
          418  +  if( p==0 || p->aOp==0 || sqlite3ThreadDataReadOnly()->mallocFailed ){
   419    419       if (n != P3_KEYINFO) {
   420    420         freeP3(n, (void*)*(char**)&zP3);
   421    421       }
   422    422       return;
   423    423     }
   424    424     if( addr<0 || addr>=p->nOp ){
   425    425       addr = p->nOp - 1;
................................................................................
   468    468   /*
   469    469   ** Replace the P3 field of the most recently coded instruction with
   470    470   ** comment text.
   471    471   */
   472    472   void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
   473    473     va_list ap;
   474    474     assert( p->nOp>0 );
   475         -  assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || sqlite3ThreadData()->mallocFailed );
          475  +  assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 
          476  +          || sqlite3ThreadDataReadOnly()->mallocFailed );
   476    477     va_start(ap, zFormat);
   477    478     sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
   478    479     va_end(ap);
   479    480   }
   480    481   #endif
   481    482   
   482    483   /*
................................................................................
   734    735           nStack*sizeof(p->aStack[0])    /* aStack */
   735    736         + nArg*sizeof(Mem*)              /* apArg */
   736    737         + nVar*sizeof(Mem)               /* aVar */
   737    738         + nVar*sizeof(char*)             /* azVar */
   738    739         + nMem*sizeof(Mem)               /* aMem */
   739    740         + nCursor*sizeof(Cursor*)        /* apCsr */
   740    741       );
   741         -    if( !sqlite3ThreadData()->mallocFailed ){
          742  +    if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
   742    743         p->aMem = &p->aStack[nStack];
   743    744         p->nMem = nMem;
   744    745         p->aVar = &p->aMem[nMem];
   745    746         p->nVar = nVar;
   746    747         p->okVar = 0;
   747    748         p->apArg = (Mem**)&p->aVar[nVar];
   748    749         p->azVar = (char**)&p->apArg[nArg];
................................................................................
   886    887   ** the string is freed using sqliteFree() when the vdbe is finished with
   887    888   ** it. Otherwise, N bytes of zName are copied.
   888    889   */
   889    890   int sqlite3VdbeSetColName(Vdbe *p, int idx, const char *zName, int N){
   890    891     int rc;
   891    892     Mem *pColName;
   892    893     assert( idx<(2*p->nResColumn) );
   893         -  if( sqlite3ThreadData()->mallocFailed ) return SQLITE_NOMEM;
          894  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) return SQLITE_NOMEM;
   894    895     assert( p->aColName!=0 );
   895    896     pColName = &(p->aColName[idx]);
   896    897     if( N==P3_DYNAMIC || N==P3_STATIC ){
   897    898       rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
   898    899     }else{
   899    900       rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
   900    901     }
................................................................................
  1149   1150   ** means the close did not happen and needs to be repeated.
  1150   1151   */
  1151   1152   int sqlite3VdbeHalt(Vdbe *p){
  1152   1153     sqlite3 *db = p->db;
  1153   1154     int i;
  1154   1155     int (*xFunc)(Btree *pBt) = 0;  /* Function to call on each btree backend */
  1155   1156   
  1156         -  if( sqlite3ThreadData()->mallocFailed ){
         1157  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
  1157   1158       p->rc = SQLITE_NOMEM;
  1158   1159     }
  1159   1160   
  1160   1161     if( p->magic!=VDBE_MAGIC_RUN ){
  1161   1162       /* Already halted.  Nothing to do. */
  1162   1163       assert( p->magic==VDBE_MAGIC_HALT );
  1163   1164       return SQLITE_OK;
................................................................................
  1675   1676         }
  1676   1677         return len;
  1677   1678       }
  1678   1679     }
  1679   1680     return 0;
  1680   1681   }
  1681   1682   
         1683  +/*
         1684  +** The header of a record consists of a sequence variable-length integers.
         1685  +** These integers are almost always small and are encoded as a single byte.
         1686  +** The following macro takes advantage this fact to provide a fast decode
         1687  +** of the integers in a record header.  It is faster for the common case
         1688  +** where the integer is a single byte.  It is a little slower when the
         1689  +** integer is two or more bytes.  But overall it is faster.
         1690  +**
         1691  +** The following expressions are equivalent:
         1692  +**
         1693  +**     x = sqlite3GetVarint32( A, &B );
         1694  +**
         1695  +**     x = GetVarint( A, B );
         1696  +**
         1697  +*/
         1698  +#define GetVarint(A,B)  ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
         1699  +
  1682   1700   /*
  1683   1701   ** This function compares the two table rows or index records specified by 
  1684   1702   ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
  1685   1703   ** or positive integer if {nKey1, pKey1} is less than, equal to or 
  1686   1704   ** greater than {nKey2, pKey2}.  Both Key1 and Key2 must be byte strings
  1687   1705   ** composed by the OP_MakeRecord opcode of the VDBE.
  1688   1706   */
................................................................................
  1702   1720     const unsigned char *aKey2 = (const unsigned char *)pKey2;
  1703   1721   
  1704   1722     Mem mem1;
  1705   1723     Mem mem2;
  1706   1724     mem1.enc = pKeyInfo->enc;
  1707   1725     mem2.enc = pKeyInfo->enc;
  1708   1726     
  1709         -  idx1 = sqlite3GetVarint32(pKey1, &szHdr1);
         1727  +  idx1 = GetVarint(aKey1, szHdr1);
  1710   1728     d1 = szHdr1;
  1711         -  idx2 = sqlite3GetVarint32(pKey2, &szHdr2);
         1729  +  idx2 = GetVarint(aKey2, szHdr2);
  1712   1730     d2 = szHdr2;
  1713   1731     nField = pKeyInfo->nField;
  1714   1732     while( idx1<szHdr1 && idx2<szHdr2 ){
  1715   1733       u32 serial_type1;
  1716   1734       u32 serial_type2;
  1717   1735   
  1718   1736       /* Read the serial types for the next element in each key. */
  1719         -    idx1 += sqlite3GetVarint32(&aKey1[idx1], &serial_type1);
         1737  +    idx1 += GetVarint( aKey1+idx1, serial_type1 );
  1720   1738       if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
  1721         -    idx2 += sqlite3GetVarint32(&aKey2[idx2], &serial_type2);
         1739  +    idx2 += GetVarint( aKey2+idx2, serial_type2 );
  1722   1740       if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
  1723   1741   
  1724   1742       /* Assert that there is enough space left in each key for the blob of
  1725   1743       ** data to go with the serial type just read. This assert may fail if
  1726   1744       ** the file is corrupted.  Then read the value from each key into mem1
  1727   1745       ** and mem2 respectively.
  1728   1746       */

Changes to SQLite.Interop/src/vdbemem.c.

   747    747   const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
   748    748     if( !pVal ) return 0;
   749    749     assert( enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE || enc==SQLITE_UTF8);
   750    750   
   751    751     if( pVal->flags&MEM_Null ){
   752    752       return 0;
   753    753     }
          754  +  assert( (MEM_Blob>>3) == MEM_Str );
          755  +  pVal->flags |= (pVal->flags & MEM_Blob)>>3;
   754    756     if( pVal->flags&MEM_Str ){
   755    757       sqlite3VdbeChangeEncoding(pVal, enc);
   756    758     }else if( !(pVal->flags&MEM_Blob) ){
   757    759       sqlite3VdbeMemStringify(pVal, enc);
   758    760     }
   759         -  assert(pVal->enc==enc || sqlite3ThreadData()->mallocFailed);
          761  +  assert(pVal->enc==enc || sqlite3ThreadDataReadOnly()->mallocFailed);
   760    762     return (const void *)(pVal->enc==enc ? (pVal->z) : 0);
   761    763   }
   762    764   
   763    765   /*
   764    766   ** Create a new sqlite3_value object.
   765    767   */
   766    768   sqlite3_value* sqlite3ValueNew(void){

Changes to SQLite.Interop/src/where.c.

    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  This module is reponsible for
    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   **
    19         -** $Id: where.c,v 1.15 2006/01/11 03:22:30 rmsimpson Exp $
           19  +** $Id: where.c,v 1.16 2006/01/12 20:54:08 rmsimpson Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   
    23     23   /*
    24     24   ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
    25     25   */
    26     26   #define BMS  (sizeof(Bitmask)*8)
................................................................................
   563    563     WhereTerm *pTerm = &pWC->a[idxTerm];
   564    564     Expr *pExpr = pTerm->pExpr;
   565    565     Bitmask prereqLeft;
   566    566     Bitmask prereqAll;
   567    567     int nPattern;
   568    568     int isComplete;
   569    569   
   570         -  if( sqlite3ThreadData()->mallocFailed ) return;
          570  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ) return;
   571    571     prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
   572    572     if( pExpr->op==TK_IN ){
   573    573       assert( pExpr->pRight==0 );
   574    574       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->pList)
   575    575                             | exprSelectTableUsage(pMaskSet, pExpr->pSelect);
   576    576     }else{
   577    577       pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
................................................................................
  1434   1434     whereClauseInit(&wc, pParse);
  1435   1435     whereSplit(&wc, pWhere, TK_AND);
  1436   1436       
  1437   1437     /* Allocate and initialize the WhereInfo structure that will become the
  1438   1438     ** return value.
  1439   1439     */
  1440   1440     pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
  1441         -  if( sqlite3ThreadData()->mallocFailed ){
         1441  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
  1442   1442       goto whereBeginNoMem;
  1443   1443     }
  1444   1444     pWInfo->pParse = pParse;
  1445   1445     pWInfo->pTabList = pTabList;
  1446   1446     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
  1447   1447   
  1448   1448     /* Special case: a WHERE clause that is constant.  Evaluate the
................................................................................
  1458   1458     ** want to analyze these virtual terms, so start analyzing at the end
  1459   1459     ** and work forward so that the added virtual terms are never processed.
  1460   1460     */
  1461   1461     for(i=0; i<pTabList->nSrc; i++){
  1462   1462       createMask(&maskSet, pTabList->a[i].iCursor);
  1463   1463     }
  1464   1464     exprAnalyzeAll(pTabList, &maskSet, &wc);
  1465         -  if( sqlite3ThreadData()->mallocFailed ){
         1465  +  if( sqlite3ThreadDataReadOnly()->mallocFailed ){
  1466   1466       goto whereBeginNoMem;
  1467   1467     }
  1468   1468   
  1469   1469     /* Chose the best index to use for each table in the FROM clause.
  1470   1470     **
  1471   1471     ** This loop fills in the following fields:
  1472   1472     **