/ Check-in [0b832e21]
Login

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

Overview
Comment:Modify sources to that they can be combined into a single sqlite3.c source file. Eliminate all type-pruned pointer warnings. (CVS 3722)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0b832e218ec12b0eb559e407d80aba6709e2ea85
User & Date: drh 2007-03-26 22:05:01
Context
2007-03-27
12:04
Get the -DSQLITE_OMIT_XFER_OPT option working. Run speed tests on a full regression. Add the script for generating sqlite3.c. (CVS 3723) check-in: 42c03851 user: drh tags: trunk
2007-03-26
22:05
Modify sources to that they can be combined into a single sqlite3.c source file. Eliminate all type-pruned pointer warnings. (CVS 3722) check-in: 0b832e21 user: drh tags: trunk
16:30
Set the same random seed for the speed tests. (CVS 3721) check-in: 8fe31705 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to main.mk.

   244    244   # build on the target system.  Some of the C source code and header
   245    245   # files are automatically generated.  This target takes care of
   246    246   # all that automatic generation.
   247    247   #
   248    248   target_source:	$(SRC)
   249    249   	rm -rf tsrc
   250    250   	mkdir tsrc
   251         -	cp $(SRC) tsrc
          251  +	cp $(SRC) $(TOP)/src/*.h tsrc
   252    252   	rm tsrc/sqlite.h.in tsrc/parse.y
   253    253   
   254    254   # Rules to build the LEMON compiler generator
   255    255   #
   256    256   lemon:	$(TOP)/tool/lemon.c $(TOP)/tool/lempar.c
   257    257   	$(BCC) -o lemon $(TOP)/tool/lemon.c
   258    258   	cp $(TOP)/tool/lempar.c .

Changes to 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.341 2007/03/19 17:44:27 danielk1977 Exp $
           12  +** $Id: btree.c,v 1.342 2007/03/26 22:05:01 drh 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.
................................................................................
  2265   2265         rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
  2266   2266       }
  2267   2267     }
  2268   2268     return rc;
  2269   2269   }
  2270   2270   
  2271   2271   /* Forward declaration required by autoVacuumCommit(). */
  2272         -static int allocatePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
         2272  +static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
  2273   2273   
  2274   2274   /*
  2275   2275   ** This routine is called prior to sqlite3PagerCommit when a transaction
  2276   2276   ** is commited for an auto-vacuum database.
  2277   2277   */
  2278   2278   static int autoVacuumCommit(BtShared *pBt, Pgno *nTrunc){
  2279   2279     Pager *pPager = pBt->pPager;
................................................................................
  2355   2355         continue;
  2356   2356       }
  2357   2357       rc = getPage(pBt, iDbPage, &pDbMemPage, 0);
  2358   2358       if( rc!=SQLITE_OK ) goto autovacuum_out;
  2359   2359   
  2360   2360       /* Find the next page in the free-list that is not already at the end 
  2361   2361       ** of the file. A page can be pulled off the free list using the 
  2362         -    ** allocatePage() routine.
         2362  +    ** allocateBtreePage() routine.
  2363   2363       */
  2364   2364       do{
  2365   2365         if( pFreeMemPage ){
  2366   2366           releasePage(pFreeMemPage);
  2367   2367           pFreeMemPage = 0;
  2368   2368         }
  2369         -      rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
         2369  +      rc = allocateBtreePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
  2370   2370         if( rc!=SQLITE_OK ){
  2371   2371           releasePage(pDbMemPage);
  2372   2372           goto autovacuum_out;
  2373   2373         }
  2374   2374         assert( iFreePage<=origSize );
  2375   2375       }while( iFreePage>finSize );
  2376   2376       releasePage(pFreeMemPage);
................................................................................
  3563   3563   ** attempt to keep related pages close to each other in the database file,
  3564   3564   ** which in turn can make database access faster.
  3565   3565   **
  3566   3566   ** If the "exact" parameter is not 0, and the page-number nearby exists 
  3567   3567   ** anywhere on the free-list, then it is guarenteed to be returned. This
  3568   3568   ** is only used by auto-vacuum databases when allocating a new table.
  3569   3569   */
  3570         -static int allocatePage(
         3570  +static int allocateBtreePage(
  3571   3571     BtShared *pBt, 
  3572   3572     MemPage **ppPage, 
  3573   3573     Pgno *pPgno, 
  3574   3574     Pgno nearby,
  3575   3575     u8 exact
  3576   3576   ){
  3577   3577     MemPage *pPage1;
................................................................................
  3970   3970     pPrior = &pCell[info.iOverflow];
  3971   3971   
  3972   3972     while( nPayload>0 ){
  3973   3973       if( spaceLeft==0 ){
  3974   3974   #ifndef SQLITE_OMIT_AUTOVACUUM
  3975   3975         Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
  3976   3976   #endif
  3977         -      rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
         3977  +      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
  3978   3978   #ifndef SQLITE_OMIT_AUTOVACUUM
  3979   3979         /* If the database supports auto-vacuum, and the second or subsequent
  3980   3980         ** overflow page is being allocated, add an entry to the pointer-map
  3981   3981         ** for that page now. The entry for the first overflow page will be
  3982   3982         ** added later, by the insertCell() routine.
  3983   3983         */
  3984   3984         if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){
................................................................................
  4302   4302     int parentIdx = pParent->nCell;   /* pParent new divider cell index */
  4303   4303     int parentSize;                   /* Size of new divider cell */
  4304   4304     u8 parentCell[64];                /* Space for the new divider cell */
  4305   4305   
  4306   4306     /* Allocate a new page. Insert the overflow cell from pPage
  4307   4307     ** into it. Then remove the overflow cell from pPage.
  4308   4308     */
  4309         -  rc = allocatePage(pBt, &pNew, &pgnoNew, 0, 0);
         4309  +  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
  4310   4310     if( rc!=SQLITE_OK ){
  4311   4311       return rc;
  4312   4312     }
  4313   4313     pCell = pPage->aOvfl[0].pCell;
  4314   4314     szCell = cellSizePtr(pPage, pCell);
  4315   4315     zeroPage(pNew, pPage->aData[0]);
  4316   4316     assemblePage(pNew, 1, &pCell, &szCell);
................................................................................
  4746   4746         pNew = apNew[i] = apOld[i];
  4747   4747         pgnoNew[i] = pgnoOld[i];
  4748   4748         apOld[i] = 0;
  4749   4749         rc = sqlite3PagerWrite(pNew->pDbPage);
  4750   4750         if( rc ) goto balance_cleanup;
  4751   4751       }else{
  4752   4752         assert( i>0 );
  4753         -      rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
         4753  +      rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
  4754   4754         if( rc ) goto balance_cleanup;
  4755   4755         apNew[i] = pNew;
  4756   4756       }
  4757   4757       nNew++;
  4758   4758       zeroPage(pNew, pageFlags);
  4759   4759     }
  4760   4760   
................................................................................
  5067   5067     u8 *cdata;          /* Content of the child page */
  5068   5068     int hdr;            /* Offset to page header in parent */
  5069   5069     int brk;            /* Offset to content of first cell in parent */
  5070   5070   
  5071   5071     assert( pPage->pParent==0 );
  5072   5072     assert( pPage->nOverflow>0 );
  5073   5073     pBt = pPage->pBt;
  5074         -  rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
         5074  +  rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
  5075   5075     if( rc ) return rc;
  5076   5076     assert( sqlite3PagerIswriteable(pChild->pDbPage) );
  5077   5077     usableSize = pBt->usableSize;
  5078   5078     data = pPage->aData;
  5079   5079     hdr = pPage->hdrOffset;
  5080   5080     brk = get2byte(&data[hdr+5]);
  5081   5081     cdata = pChild->aData;
................................................................................
  5403   5403     ** If an open cursor was using the page a problem would occur.
  5404   5404     */
  5405   5405     if( pBt->pCursor ){
  5406   5406       return SQLITE_LOCKED;
  5407   5407     }
  5408   5408   
  5409   5409   #ifdef SQLITE_OMIT_AUTOVACUUM
  5410         -  rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
         5410  +  rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
  5411   5411     if( rc ) return rc;
  5412   5412   #else
  5413   5413     if( pBt->autoVacuum ){
  5414   5414       Pgno pgnoMove;      /* Move a page here to make room for the root-page */
  5415   5415       MemPage *pPageMove; /* The page to move to. */
  5416   5416   
  5417   5417       /* Read the value of meta[3] from the database to determine where the
................................................................................
  5431   5431       }
  5432   5432       assert( pgnoRoot>=3 );
  5433   5433   
  5434   5434       /* Allocate a page. The page that currently resides at pgnoRoot will
  5435   5435       ** be moved to the allocated page (unless the allocated page happens
  5436   5436       ** to reside at pgnoRoot).
  5437   5437       */
  5438         -    rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
         5438  +    rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
  5439   5439       if( rc!=SQLITE_OK ){
  5440   5440         return rc;
  5441   5441       }
  5442   5442   
  5443   5443       if( pgnoMove!=pgnoRoot ){
  5444   5444         u8 eType;
  5445   5445         Pgno iPtrPage;
................................................................................
  5488   5488       rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
  5489   5489       if( rc ){
  5490   5490         releasePage(pRoot);
  5491   5491         return rc;
  5492   5492       }
  5493   5493   
  5494   5494     }else{
  5495         -    rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
         5495  +    rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
  5496   5496       if( rc ) return rc;
  5497   5497     }
  5498   5498   #endif
  5499   5499     assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
  5500   5500     zeroPage(pRoot, flags | PTF_LEAF);
  5501   5501     sqlite3PagerUnref(pRoot->pDbPage);
  5502   5502     *piTable = (int)pgnoRoot;

Changes to 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.415 2007/03/17 10:26:59 danielk1977 Exp $
           25  +** $Id: build.c,v 1.416 2007/03/26 22:05:01 drh 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.
................................................................................
    78     78       if( p->iDb==iDb && p->iTab==iTab ){
    79     79         p->isWriteLock = (p->isWriteLock || isWriteLock);
    80     80         return;
    81     81       }
    82     82     }
    83     83   
    84     84     nBytes = sizeof(TableLock) * (pParse->nTableLock+1);
    85         -  sqliteReallocOrFree((void **)&pParse->aTableLock, nBytes);
           85  +  sqliteReallocOrFree(&pParse->aTableLock, nBytes);
    86     86     if( pParse->aTableLock ){
    87     87       p = &pParse->aTableLock[pParse->nTableLock++];
    88     88       p->iDb = iDb;
    89     89       p->iTab = iTab;
    90     90       p->isWriteLock = isWriteLock;
    91     91       p->zName = zName;
    92     92     }
................................................................................
  2800   2800   **
  2801   2801   ** This routine allocates a new slot in the array, zeros it out,
  2802   2802   ** and returns its index.  If malloc fails a negative number is returned.
  2803   2803   **
  2804   2804   ** szEntry is the sizeof of a single array entry.  initSize is the 
  2805   2805   ** number of array entries allocated on the initial allocation.
  2806   2806   */
  2807         -int sqlite3ArrayAllocate(void **ppArray, int szEntry, int initSize){
         2807  +int sqlite3ArrayAllocate(void *ppArray, int szEntry, int initSize){
  2808   2808     char *p;
  2809         -  int *an = (int*)&ppArray[1];
         2809  +  void **pp = (void**)ppArray;
         2810  +  int *an = (int*)&pp[1];
  2810   2811     if( an[0]>=an[1] ){
  2811   2812       void *pNew;
  2812   2813       int newSize;
  2813   2814       newSize = an[1]*2 + initSize;
  2814         -    pNew = sqliteRealloc(*ppArray, newSize*szEntry);
         2815  +    pNew = sqliteRealloc(*pp, newSize*szEntry);
  2815   2816       if( pNew==0 ){
  2816   2817         return -1;
  2817   2818       }
  2818   2819       an[1] = newSize;
  2819         -    *ppArray = pNew;
         2820  +    *pp = pNew;
  2820   2821     }
  2821         -  p = *ppArray;
         2822  +  p = *pp;
  2822   2823     memset(&p[an[0]*szEntry], 0, szEntry);
  2823   2824     return an[0]++;
  2824   2825   }
  2825   2826   
  2826   2827   /*
  2827   2828   ** Append a new element to the given IdList.  Create a new IdList if
  2828   2829   ** need be.
................................................................................
  2832   2833   IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
  2833   2834     int i;
  2834   2835     if( pList==0 ){
  2835   2836       pList = sqliteMalloc( sizeof(IdList) );
  2836   2837       if( pList==0 ) return 0;
  2837   2838       pList->nAlloc = 0;
  2838   2839     }
  2839         -  i = sqlite3ArrayAllocate((void**)&pList->a, sizeof(pList->a[0]), 5);
         2840  +  i = sqlite3ArrayAllocate(&pList->a, sizeof(pList->a[0]), 5);
  2840   2841     if( i<0 ){
  2841   2842       sqlite3IdListDelete(pList);
  2842   2843       return 0;
  2843   2844     }
  2844   2845     pList->a[i].zName = sqlite3NameFromToken(pToken);
  2845   2846     return pList;
  2846   2847   }

Changes to 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.281 2007/03/12 23:48:53 drh Exp $
           15  +** $Id: expr.c,v 1.282 2007/03/26 22:05:01 drh 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   **
................................................................................
   400    400           break;
   401    401         }
   402    402       }
   403    403       if( i>=pParse->nVarExpr ){
   404    404         pExpr->iTable = ++pParse->nVar;
   405    405         if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
   406    406           pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
   407         -        sqliteReallocOrFree((void**)&pParse->apVarExpr,
          407  +        sqliteReallocOrFree(&pParse->apVarExpr,
   408    408                          pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
   409    409         }
   410    410         if( !sqlite3MallocFailed() ){
   411    411           assert( pParse->apVarExpr!=0 );
   412    412           pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
   413    413         }
   414    414       }
................................................................................
  2233   2233   
  2234   2234   /*
  2235   2235   ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
  2236   2236   ** the new element.  Return a negative number if malloc fails.
  2237   2237   */
  2238   2238   static int addAggInfoColumn(AggInfo *pInfo){
  2239   2239     int i;
  2240         -  i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
         2240  +  i = sqlite3ArrayAllocate(&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
  2241   2241     if( i<0 ){
  2242   2242       return -1;
  2243   2243     }
  2244   2244     return i;
  2245   2245   }    
  2246   2246   
  2247   2247   /*
  2248   2248   ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
  2249   2249   ** the new element.  Return a negative number if malloc fails.
  2250   2250   */
  2251   2251   static int addAggInfoFunc(AggInfo *pInfo){
  2252   2252     int i;
  2253         -  i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
         2253  +  i = sqlite3ArrayAllocate(&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
  2254   2254     if( i<0 ){
  2255   2255       return -1;
  2256   2256     }
  2257   2257     return i;
  2258   2258   }    
  2259   2259   
  2260   2260   /*

Changes to src/os_common.h.

    36     36   unsigned int sqlite3_pending_byte = 0x40000000;
    37     37   #endif
    38     38   
    39     39   int sqlite3_os_trace = 0;
    40     40   #ifdef SQLITE_DEBUG
    41     41   static int last_page = 0;
    42     42   #define SEEK(X)           last_page=(X)
    43         -#define TRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
    44         -#define TRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
    45         -#define TRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
    46         -#define TRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
    47         -#define TRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
    48         -#define TRACE6(X,Y,Z,A,B,C) if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
    49         -#define TRACE7(X,Y,Z,A,B,C,D) \
           43  +#define OSTRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
           44  +#define OSTRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
           45  +#define OSTRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
           46  +#define OSTRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
           47  +#define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
           48  +#define OSTRACE6(X,Y,Z,A,B,C) \
           49  +    if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
           50  +#define OSTRACE7(X,Y,Z,A,B,C,D) \
    50     51       if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
    51     52   #else
    52     53   #define SEEK(X)
    53         -#define TRACE1(X)
    54         -#define TRACE2(X,Y)
    55         -#define TRACE3(X,Y,Z)
    56         -#define TRACE4(X,Y,Z,A)
    57         -#define TRACE5(X,Y,Z,A,B)
    58         -#define TRACE6(X,Y,Z,A,B,C)
    59         -#define TRACE7(X,Y,Z,A,B,C,D)
           54  +#define OSTRACE1(X)
           55  +#define OSTRACE2(X,Y)
           56  +#define OSTRACE3(X,Y,Z)
           57  +#define OSTRACE4(X,Y,Z,A)
           58  +#define OSTRACE5(X,Y,Z,A,B)
           59  +#define OSTRACE6(X,Y,Z,A,B,C)
           60  +#define OSTRACE7(X,Y,Z,A,B,C,D)
    60     61   #endif
    61     62   
    62     63   /*
    63     64   ** Macros for performance tracing.  Normally turned off.  Only works
    64     65   ** on i486 hardware.
    65     66   */
    66     67   #ifdef SQLITE_PERFORMANCE_TRACE
................................................................................
   193    194   
   194    195   /*
   195    196   ** The default size of a disk sector
   196    197   */
   197    198   #ifndef PAGER_SECTOR_SIZE
   198    199   # define PAGER_SECTOR_SIZE 512
   199    200   #endif
   200         -

Changes to src/os_os2.c.

    58     58   /*
    59     59   ** Delete the named file
    60     60   */
    61     61   int sqlite3Os2Delete( const char *zFilename ){
    62     62     APIRET rc = NO_ERROR;
    63     63   
    64     64     rc = DosDelete( (PSZ)zFilename );
    65         -  TRACE2( "DELETE \"%s\"\n", zFilename );
           65  +  OSTRACE2( "DELETE \"%s\"\n", zFilename );
    66     66     return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
    67     67   }
    68     68   
    69     69   /*
    70     70   ** Return TRUE if the named file exists.
    71     71   */
    72     72   int sqlite3Os2FileExists( const char *zFilename ){
................................................................................
   123    123       *pReadonly = 0;
   124    124     }
   125    125     f.h = hf;
   126    126     f.locktype = NO_LOCK;
   127    127     f.delOnClose = 0;
   128    128     f.pathToDel = NULL;
   129    129     OpenCounter(+1);
   130         -  TRACE3( "OPEN R/W %d \"%s\"\n", hf, zFilename );
          130  +  OSTRACE3( "OPEN R/W %d \"%s\"\n", hf, zFilename );
   131    131     return allocateOs2File( &f, pld );
   132    132   }
   133    133   
   134    134   
   135    135   /*
   136    136   ** Attempt to open a new file for exclusive access by this process.
   137    137   ** The file will be opened for both reading and writing.  To avoid
................................................................................
   163    163   
   164    164     f.h = hf;
   165    165     f.locktype = NO_LOCK;
   166    166     f.delOnClose = delFlag ? 1 : 0;
   167    167     f.pathToDel = delFlag ? sqlite3OsFullPathname( zFilename ) : NULL;
   168    168     OpenCounter( +1 );
   169    169     if( delFlag ) DosForceDelete( sqlite3OsFullPathname( zFilename ) );
   170         -  TRACE3( "OPEN EX %d \"%s\"\n", hf, sqlite3OsFullPathname ( zFilename ) );
          170  +  OSTRACE3( "OPEN EX %d \"%s\"\n", hf, sqlite3OsFullPathname ( zFilename ) );
   171    171     return allocateOs2File( &f, pld );
   172    172   }
   173    173   
   174    174   /*
   175    175   ** Attempt to open a new file for read-only access.
   176    176   **
   177    177   ** On success, write the file handle into *id and return SQLITE_OK.
................................................................................
   193    193       return SQLITE_CANTOPEN;
   194    194     }
   195    195     f.h = hf;
   196    196     f.locktype = NO_LOCK;
   197    197     f.delOnClose = 0;
   198    198     f.pathToDel = NULL;
   199    199     OpenCounter( +1 );
   200         -  TRACE3( "OPEN RO %d \"%s\"\n", hf, zFilename );
          200  +  OSTRACE3( "OPEN RO %d \"%s\"\n", hf, zFilename );
   201    201     return allocateOs2File( &f, pld );
   202    202   }
   203    203   
   204    204   /*
   205    205   ** Attempt to open a file descriptor for the directory that contains a
   206    206   ** file.  This file descriptor can be used to fsync() the directory
   207    207   ** in order to make sure the creation of a new file is actually written
................................................................................
   257    257         sqlite3Randomness( 15, &zBuf[j] );
   258    258         for( i = 0; i < 15; i++, j++ ){
   259    259           zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   260    260         }
   261    261         zBuf[j] = 0;
   262    262         if( !sqlite3OsFileExists( zBuf ) ) break;
   263    263     }
   264         -  TRACE2( "TEMP FILENAME: %s\n", zBuf );
          264  +  OSTRACE2( "TEMP FILENAME: %s\n", zBuf );
   265    265     return SQLITE_OK;
   266    266   }
   267    267   
   268    268   /*
   269    269   ** Close a file.
   270    270   */
   271    271   int os2Close( OsFile **pld ){
   272    272     os2File *pFile;
   273    273     APIRET rc = NO_ERROR;
   274    274     if( pld && (pFile = (os2File*)*pld) != 0 ){
   275         -    TRACE2( "CLOSE %d\n", pFile->h );
          275  +    OSTRACE2( "CLOSE %d\n", pFile->h );
   276    276       rc = DosClose( pFile->h );
   277    277       pFile->locktype = NO_LOCK;
   278    278       if( pFile->delOnClose != 0 ){
   279    279           rc = DosForceDelete( pFile->pathToDel );
   280    280       }
   281    281       *pld = 0;
   282    282       OpenCounter( -1 );
................................................................................
   290    290   ** bytes were read successfully and SQLITE_IOERR if anything goes
   291    291   ** wrong.
   292    292   */
   293    293   int os2Read( OsFile *id, void *pBuf, int amt ){
   294    294     ULONG got;
   295    295     assert( id!=0 );
   296    296     SimulateIOError( return SQLITE_IOERR );
   297         -  TRACE3( "READ %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
          297  +  OSTRACE3( "READ %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
   298    298     DosRead( ((os2File*)id)->h, pBuf, amt, &got );
   299    299     if (got == (ULONG)amt)
   300    300       return SQLITE_OK;
   301    301     else if (got < 0)
   302    302       return SQLITE_IOERR_READ;
   303    303     else {
   304    304       memset(&((char*)pBuf)[got], 0, amt-got);
................................................................................
   312    312   */
   313    313   int os2Write( OsFile *id, const void *pBuf, int amt ){
   314    314     APIRET rc = NO_ERROR;
   315    315     ULONG wrote;
   316    316     assert( id!=0 );
   317    317     SimulateIOError( return SQLITE_IOERR );
   318    318     SimulateDiskfullError( return SQLITE_FULL );
   319         -  TRACE3( "WRITE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
          319  +  OSTRACE3( "WRITE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
   320    320     while( amt > 0 &&
   321    321         (rc = DosWrite( ((os2File*)id)->h, (PVOID)pBuf, amt, &wrote )) && wrote > 0 ){
   322    322         amt -= wrote;
   323    323         pBuf = &((char*)pBuf)[wrote];
   324    324     }
   325    325   
   326    326     return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
................................................................................
   330    330   ** Move the read/write pointer in a file.
   331    331   */
   332    332   int os2Seek( OsFile *id, i64 offset ){
   333    333     APIRET rc = NO_ERROR;
   334    334     ULONG filePointer = 0L;
   335    335     assert( id!=0 );
   336    336     rc = DosSetFilePtr( ((os2File*)id)->h, offset, FILE_BEGIN, &filePointer );
   337         -  TRACE3( "SEEK %d %lld\n", ((os2File*)id)->h, offset );
          337  +  OSTRACE3( "SEEK %d %lld\n", ((os2File*)id)->h, offset );
   338    338     return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   339    339   }
   340    340   
   341    341   /*
   342    342   ** Make sure all writes to a particular file are committed to disk.
   343    343   */
   344    344   int os2Sync( OsFile *id, int dataOnly ){
   345    345     assert( id!=0 );
   346         -  TRACE3( "SYNC %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
          346  +  OSTRACE3( "SYNC %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
   347    347     return DosResetBuffer( ((os2File*)id)->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   348    348   }
   349    349   
   350    350   /*
   351    351   ** Sync the directory zDirname. This is a no-op on operating systems other
   352    352   ** than UNIX.
   353    353   */
................................................................................
   359    359   /*
   360    360   ** Truncate an open file to a specified size
   361    361   */
   362    362   int os2Truncate( OsFile *id, i64 nByte ){
   363    363     APIRET rc = NO_ERROR;
   364    364     ULONG upperBits = nByte>>32;
   365    365     assert( id!=0 );
   366         -  TRACE3( "TRUNCATE %d %lld\n", ((os2File*)id)->h, nByte );
          366  +  OSTRACE3( "TRUNCATE %d %lld\n", ((os2File*)id)->h, nByte );
   367    367     SimulateIOError( return SQLITE_IOERR );
   368    368     rc = DosSetFilePtr( ((os2File*)id)->h, nByte, FILE_BEGIN, &upperBits );
   369    369     if( rc != NO_ERROR ){
   370    370       return SQLITE_IOERR;
   371    371     }
   372    372     rc = DosSetFilePtr( ((os2File*)id)->h, 0L, FILE_END, &upperBits );
   373    373     return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
................................................................................
   474    474     int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
   475    475     FILELOCK  LockArea,
   476    476               UnlockArea;
   477    477     os2File *pFile = (os2File*)id;
   478    478     memset(&LockArea, 0, sizeof(LockArea));
   479    479     memset(&UnlockArea, 0, sizeof(UnlockArea));
   480    480     assert( pFile!=0 );
   481         -  TRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
          481  +  OSTRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
   482    482   
   483    483     /* If there is already a lock of this type or more restrictive on the
   484    484     ** OsFile, do nothing. Don't use the end_lock: exit path, as
   485    485     ** sqlite3OsEnterMutex() hasn't been called yet.
   486    486     */
   487    487     if( pFile->locktype>=locktype ){
   488    488       return SQLITE_OK;
................................................................................
   509    509       UnlockArea.lOffset = 0L;
   510    510       UnlockArea.lRange = 0L;
   511    511   
   512    512       while( cnt-->0 && (res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L) )!=NO_ERROR ){
   513    513         /* Try 3 times to get the pending lock.  The pending lock might be
   514    514         ** held by another reader process who will release it momentarily.
   515    515         */
   516         -      TRACE2( "could not get a PENDING lock. cnt=%d\n", cnt );
          516  +      OSTRACE2( "could not get a PENDING lock. cnt=%d\n", cnt );
   517    517         DosSleep(1);
   518    518       }
   519    519       gotPendingLock = res;
   520    520     }
   521    521   
   522    522     /* Acquire a shared lock
   523    523     */
................................................................................
   551    551     }
   552    552   
   553    553     /* Acquire an EXCLUSIVE lock
   554    554     */
   555    555     if( locktype==EXCLUSIVE_LOCK && res ){
   556    556       assert( pFile->locktype>=SHARED_LOCK );
   557    557       res = unlockReadLock(pFile);
   558         -    TRACE2( "unreadlock = %d\n", res );
          558  +    OSTRACE2( "unreadlock = %d\n", res );
   559    559       LockArea.lOffset = SHARED_FIRST;
   560    560       LockArea.lRange = SHARED_SIZE;
   561    561       UnlockArea.lOffset = 0L;
   562    562       UnlockArea.lRange = 0L;
   563    563       res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
   564    564       if( res == NO_ERROR ){
   565    565         newLocktype = EXCLUSIVE_LOCK;
   566    566       }else{
   567         -      TRACE2( "error-code = %d\n", res );
          567  +      OSTRACE2( "error-code = %d\n", res );
   568    568       }
   569    569     }
   570    570   
   571    571     /* If we are holding a PENDING lock that ought to be released, then
   572    572     ** release it now.
   573    573     */
   574    574     if( gotPendingLock && locktype==SHARED_LOCK ){
................................................................................
   581    581   
   582    582     /* Update the state of the lock has held in the file descriptor then
   583    583     ** return the appropriate result code.
   584    584     */
   585    585     if( res == NO_ERROR ){
   586    586       rc = SQLITE_OK;
   587    587     }else{
   588         -    TRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
          588  +    OSTRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
   589    589              locktype, newLocktype );
   590    590       rc = SQLITE_BUSY;
   591    591     }
   592    592     pFile->locktype = newLocktype;
   593    593     return rc;
   594    594   }
   595    595   
................................................................................
   600    600   */
   601    601   int os2CheckReservedLock( OsFile *id ){
   602    602     APIRET rc = NO_ERROR;
   603    603     os2File *pFile = (os2File*)id;
   604    604     assert( pFile!=0 );
   605    605     if( pFile->locktype>=RESERVED_LOCK ){
   606    606       rc = 1;
   607         -    TRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, rc );
          607  +    OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, rc );
   608    608     }else{
   609    609       FILELOCK  LockArea,
   610    610                 UnlockArea;
   611    611       memset(&LockArea, 0, sizeof(LockArea));
   612    612       memset(&UnlockArea, 0, sizeof(UnlockArea));
   613    613       LockArea.lOffset = RESERVED_BYTE;
   614    614       LockArea.lRange = 1L;
................................................................................
   618    618       if( rc == NO_ERROR ){
   619    619         LockArea.lOffset = 0L;
   620    620         LockArea.lRange = 0L;
   621    621         UnlockArea.lOffset = RESERVED_BYTE;
   622    622         UnlockArea.lRange = 1L;
   623    623         rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
   624    624       }
   625         -    TRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, rc );
          625  +    OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, rc );
   626    626     }
   627    627     return rc;
   628    628   }
   629    629   
   630    630   /*
   631    631   ** Lower the locking level on file descriptor id to locktype.  locktype
   632    632   ** must be either NO_LOCK or SHARED_LOCK.
................................................................................
   644    644     os2File *pFile = (os2File*)id;
   645    645     FILELOCK  LockArea,
   646    646               UnlockArea;
   647    647     memset(&LockArea, 0, sizeof(LockArea));
   648    648     memset(&UnlockArea, 0, sizeof(UnlockArea));
   649    649     assert( pFile!=0 );
   650    650     assert( locktype<=SHARED_LOCK );
   651         -  TRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
          651  +  OSTRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
   652    652     type = pFile->locktype;
   653    653     if( type>=EXCLUSIVE_LOCK ){
   654    654       LockArea.lOffset = 0L;
   655    655       LockArea.lRange = 0L;
   656    656       UnlockArea.lOffset = SHARED_FIRST;
   657    657       UnlockArea.lRange = SHARED_SIZE;
   658    658       DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );

Changes to src/os_unix.c.

   749    749     if( threadsOverrideEachOthersLocks ){
   750    750       /* Ownership transfers not needed on this system */
   751    751       return SQLITE_OK;
   752    752     }
   753    753     hSelf = pthread_self();
   754    754     if( pthread_equal(pFile->tid, hSelf) ){
   755    755       /* We are still in the same thread */
   756         -    TRACE1("No-transfer, same thread\n");
          756  +    OSTRACE1("No-transfer, same thread\n");
   757    757       return SQLITE_OK;
   758    758     }
   759    759     if( pFile->locktype!=NO_LOCK ){
   760    760       /* We cannot change ownership while we are holding a lock! */
   761    761       return SQLITE_MISUSE;
   762    762     }
   763         -  TRACE4("Transfer ownership of %d from %d to %d\n", pFile->h,pFile->tid,hSelf);
          763  +  OSTRACE4("Transfer ownership of %d from %d to %d\n",
          764  +            pFile->h, pFile->tid, hSelf);
   764    765     pFile->tid = hSelf;
   765    766     if (pFile->pLock != NULL) {
   766    767       releaseLockInfo(pFile->pLock);
   767    768       rc = findLockInfo(pFile->h, &pFile->pLock, 0);
   768         -    TRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h,
          769  +    OSTRACE5("LOCK    %d is now %s(%s,%d)\n", pFile->h,
   769    770              locktypeName(pFile->locktype),
   770    771              locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
   771    772       return rc;
   772    773     } else {
   773    774       return SQLITE_OK;
   774    775     }
   775    776   }
................................................................................
   921    922     }
   922    923     SET_THREADID(pFile);
   923    924     assert( pFile->dirfd<0 );
   924    925     pFile->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0);
   925    926     if( pFile->dirfd<0 ){
   926    927       return SQLITE_CANTOPEN; 
   927    928     }
   928         -  TRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname);
          929  +  OSTRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname);
   929    930     return SQLITE_OK;
   930    931   }
   931    932   
   932    933   /*
   933    934   ** If the following global variable points to a string which is the
   934    935   ** name of a directory, then that directory will be used to store
   935    936   ** temporary files.
................................................................................
  1010   1011     newOffset = lseek(id->h, id->offset, SEEK_SET);
  1011   1012     if( newOffset!=id->offset ){
  1012   1013       return -1;
  1013   1014     }
  1014   1015     got = read(id->h, pBuf, cnt);
  1015   1016   #endif
  1016   1017     TIMER_END;
  1017         -  TRACE5("READ    %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
         1018  +  OSTRACE5("READ    %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
  1018   1019     if( got>0 ){
  1019   1020       id->offset += got;
  1020   1021     }
  1021   1022     return got;
  1022   1023   }
  1023   1024   
  1024   1025   /*
................................................................................
  1057   1058     newOffset = lseek(id->h, id->offset, SEEK_SET);
  1058   1059     if( newOffset!=id->offset ){
  1059   1060       return -1;
  1060   1061     }
  1061   1062     got = write(id->h, pBuf, cnt);
  1062   1063   #endif
  1063   1064     TIMER_END;
  1064         -  TRACE5("WRITE   %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
         1065  +  OSTRACE5("WRITE   %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
  1065   1066     if( got>0 ){
  1066   1067       id->offset += got;
  1067   1068     }
  1068   1069     return got;
  1069   1070   }
  1070   1071   
  1071   1072   
................................................................................
  1207   1208   ** the directory entry for the journal was never created) and the transaction
  1208   1209   ** will not roll back - possibly leading to database corruption.
  1209   1210   */
  1210   1211   static int unixSync(OsFile *id, int dataOnly){
  1211   1212     int rc;
  1212   1213     unixFile *pFile = (unixFile*)id;
  1213   1214     assert( pFile );
  1214         -  TRACE2("SYNC    %-3d\n", pFile->h);
         1215  +  OSTRACE2("SYNC    %-3d\n", pFile->h);
  1215   1216     rc = full_fsync(pFile->h, pFile->fullSync, dataOnly);
  1216   1217     SimulateIOError( rc=1 );
  1217   1218     if( rc ){
  1218   1219       return SQLITE_IOERR_FSYNC;
  1219   1220     }
  1220   1221     if( pFile->dirfd>=0 ){
  1221         -    TRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
         1222  +    OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
  1222   1223               HAVE_FULLFSYNC, pFile->fullSync);
  1223   1224   #ifndef SQLITE_DISABLE_DIRSYNC
  1224   1225       /* The directory sync is only attempted if full_fsync is
  1225   1226       ** turned off or unavailable.  If a full_fsync occurred above,
  1226   1227       ** then the directory sync is superfluous.
  1227   1228       */
  1228   1229       if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){
................................................................................
  1252   1253   int sqlite3UnixSyncDirectory(const char *zDirname){
  1253   1254   #ifdef SQLITE_DISABLE_DIRSYNC
  1254   1255     return SQLITE_OK;
  1255   1256   #else
  1256   1257     int fd;
  1257   1258     int r;
  1258   1259     fd = open(zDirname, O_RDONLY|O_BINARY, 0);
  1259         -  TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
         1260  +  OSTRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
  1260   1261     if( fd<0 ){
  1261   1262       return SQLITE_CANTOPEN; 
  1262   1263     }
  1263   1264     r = fsync(fd);
  1264   1265     close(fd);
  1265   1266     SimulateIOError( r=1 );
  1266   1267     if( r ){
................................................................................
  1331   1332       fcntl(pFile->h, F_GETLK, &lock);
  1332   1333       if( lock.l_type!=F_UNLCK ){
  1333   1334         r = 1;
  1334   1335       }
  1335   1336     }
  1336   1337     
  1337   1338     sqlite3OsLeaveMutex();
  1338         -  TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
         1339  +  OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
  1339   1340   
  1340   1341     return r;
  1341   1342   }
  1342   1343   
  1343   1344   /*
  1344   1345   ** Lock the file with the lock specified by parameter locktype - one
  1345   1346   ** of the following:
................................................................................
  1406   1407     int rc = SQLITE_OK;
  1407   1408     unixFile *pFile = (unixFile*)id;
  1408   1409     struct lockInfo *pLock = pFile->pLock;
  1409   1410     struct flock lock;
  1410   1411     int s;
  1411   1412   
  1412   1413     assert( pFile );
  1413         -  TRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,
         1414  +  OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,
  1414   1415         locktypeName(locktype), locktypeName(pFile->locktype),
  1415   1416         locktypeName(pLock->locktype), pLock->cnt , getpid());
  1416   1417   
  1417   1418     /* If there is already a lock of this type or more restrictive on the
  1418   1419     ** OsFile, do nothing. Don't use the end_lock: exit path, as
  1419   1420     ** sqlite3OsEnterMutex() hasn't been called yet.
  1420   1421     */
  1421   1422     if( pFile->locktype>=locktype ){
  1422         -    TRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
         1423  +    OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
  1423   1424               locktypeName(locktype));
  1424   1425       return SQLITE_OK;
  1425   1426     }
  1426   1427   
  1427   1428     /* Make sure the locking sequence is correct
  1428   1429     */
  1429   1430     assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
................................................................................
  1550   1551     }else if( locktype==EXCLUSIVE_LOCK ){
  1551   1552       pFile->locktype = PENDING_LOCK;
  1552   1553       pLock->locktype = PENDING_LOCK;
  1553   1554     }
  1554   1555   
  1555   1556   end_lock:
  1556   1557     sqlite3OsLeaveMutex();
  1557         -  TRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
         1558  +  OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
  1558   1559         rc==SQLITE_OK ? "ok" : "failed");
  1559   1560     return rc;
  1560   1561   }
  1561   1562   
  1562   1563   /*
  1563   1564   ** Lower the locking level on file descriptor pFile to locktype.  locktype
  1564   1565   ** must be either NO_LOCK or SHARED_LOCK.
................................................................................
  1569   1570   static int unixUnlock(OsFile *id, int locktype){
  1570   1571     struct lockInfo *pLock;
  1571   1572     struct flock lock;
  1572   1573     int rc = SQLITE_OK;
  1573   1574     unixFile *pFile = (unixFile*)id;
  1574   1575   
  1575   1576     assert( pFile );
  1576         -  TRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
         1577  +  OSTRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
  1577   1578         pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
  1578   1579   
  1579   1580     assert( locktype<=SHARED_LOCK );
  1580   1581     if( pFile->locktype<=locktype ){
  1581   1582       return SQLITE_OK;
  1582   1583     }
  1583   1584     if( CHECK_THREADID(pFile) ){
................................................................................
  1682   1683       close(id->h);
  1683   1684     }
  1684   1685     releaseLockInfo(id->pLock);
  1685   1686     releaseOpenCnt(id->pOpen);
  1686   1687   
  1687   1688     sqlite3OsLeaveMutex();
  1688   1689     id->isOpen = 0;
  1689         -  TRACE2("CLOSE   %-3d\n", id->h);
         1690  +  OSTRACE2("CLOSE   %-3d\n", id->h);
  1690   1691     OpenCounter(-1);
  1691   1692     sqlite3ThreadSafeFree(id);
  1692   1693     *pId = 0;
  1693   1694     return SQLITE_OK;
  1694   1695   }
  1695   1696   
  1696   1697   
................................................................................
  1729   1730     int                     err;
  1730   1731     
  1731   1732     pb.unLockFlag = setLockFlag ? 0 : 1;
  1732   1733     pb.startEndFlag = 0;
  1733   1734     pb.offset = offset;
  1734   1735     pb.length = length; 
  1735   1736     pb.fd = fd;
  1736         -  TRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n", 
         1737  +  OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n", 
  1737   1738       (setLockFlag?"ON":"OFF"), fd, offset, length);
  1738   1739     err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
  1739   1740     if ( err==-1 ) {
  1740         -    TRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno, 
         1741  +    OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno, 
  1741   1742         strerror(errno));
  1742   1743       return 1; // error
  1743   1744     } else {
  1744   1745       return 0;
  1745   1746     }
  1746   1747   }
  1747   1748   
................................................................................
  1773   1774         r = 1;
  1774   1775       } else {
  1775   1776         /* if we succeeded in taking the reserved lock, unlock it to restore
  1776   1777         ** the original state */
  1777   1778         _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
  1778   1779       }
  1779   1780     }
  1780         -  TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
         1781  +  OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
  1781   1782     
  1782   1783     return r;
  1783   1784   }
  1784   1785   
  1785   1786   /* AFP-style locking following the behavior of unixLock, see the unixLock 
  1786   1787   ** function comments for details of lock management. */
  1787   1788   static int afpUnixLock(OsFile *id, int locktype)
................................................................................
  1788   1789   {
  1789   1790     int rc = SQLITE_OK;
  1790   1791     unixFile *pFile = (unixFile*)id;
  1791   1792     afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  1792   1793     int gotPendingLock = 0;
  1793   1794     
  1794   1795     assert( pFile );
  1795         -  TRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
         1796  +  OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
  1796   1797            locktypeName(locktype), locktypeName(pFile->locktype), getpid());  
  1797   1798     /* If there is already a lock of this type or more restrictive on the
  1798   1799       ** OsFile, do nothing. Don't use the afp_end_lock: exit path, as
  1799   1800       ** sqlite3OsEnterMutex() hasn't been called yet.
  1800   1801       */
  1801   1802     if( pFile->locktype>=locktype ){
  1802         -    TRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
         1803  +    OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
  1803   1804              locktypeName(locktype));
  1804   1805       return SQLITE_OK;
  1805   1806     }
  1806   1807   
  1807   1808     /* Make sure the locking sequence is correct
  1808   1809       */
  1809   1810     assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
................................................................................
  1902   1903       pFile->locktype = locktype;
  1903   1904     }else if( locktype==EXCLUSIVE_LOCK ){
  1904   1905       pFile->locktype = PENDING_LOCK;
  1905   1906     }
  1906   1907     
  1907   1908   afp_end_lock:
  1908   1909       sqlite3OsLeaveMutex();
  1909         -  TRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
         1910  +  OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype), 
  1910   1911            rc==SQLITE_OK ? "ok" : "failed");
  1911   1912     return rc;
  1912   1913   }
  1913   1914   
  1914   1915   /*
  1915   1916    ** Lower the locking level on file descriptor pFile to locktype.  locktype
  1916   1917    ** must be either NO_LOCK or SHARED_LOCK.
................................................................................
  1921   1922   static int afpUnixUnlock(OsFile *id, int locktype) {
  1922   1923     struct flock lock;
  1923   1924     int rc = SQLITE_OK;
  1924   1925     unixFile *pFile = (unixFile*)id;
  1925   1926     afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  1926   1927   
  1927   1928     assert( pFile );
  1928         -  TRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
         1929  +  OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
  1929   1930            pFile->locktype, getpid());
  1930   1931     
  1931   1932     assert( locktype<=SHARED_LOCK );
  1932   1933     if( pFile->locktype<=locktype ){
  1933   1934       return SQLITE_OK;
  1934   1935     }
  1935   1936     if( CHECK_THREADID(pFile) ){
................................................................................
  1998   1999       sqlite3ThreadSafeFree(id->lockingContext);
  1999   2000     }
  2000   2001     
  2001   2002     if( id->dirfd>=0 ) close(id->dirfd);
  2002   2003     id->dirfd = -1;
  2003   2004     close(id->h);
  2004   2005     id->isOpen = 0;
  2005         -  TRACE2("CLOSE   %-3d\n", id->h);
         2006  +  OSTRACE2("CLOSE   %-3d\n", id->h);
  2006   2007     OpenCounter(-1);
  2007   2008     sqlite3ThreadSafeFree(id);
  2008   2009     *pId = 0;
  2009   2010     return SQLITE_OK;
  2010   2011   }
  2011   2012   
  2012   2013   
................................................................................
  2094   2095     if( id->dirfd>=0 ) close(id->dirfd);
  2095   2096     id->dirfd = -1;
  2096   2097     sqlite3OsEnterMutex();
  2097   2098     
  2098   2099     close(id->h);  
  2099   2100     sqlite3OsLeaveMutex();
  2100   2101     id->isOpen = 0;
  2101         -  TRACE2("CLOSE   %-3d\n", id->h);
         2102  +  OSTRACE2("CLOSE   %-3d\n", id->h);
  2102   2103     OpenCounter(-1);
  2103   2104     sqlite3ThreadSafeFree(id);
  2104   2105     *pId = 0;
  2105   2106     return SQLITE_OK;
  2106   2107   }
  2107   2108   
  2108   2109   #pragma mark Old-School .lock file based locking
................................................................................
  2213   2214     id->dirfd = -1;
  2214   2215     sqlite3OsEnterMutex();
  2215   2216     
  2216   2217     close(id->h);
  2217   2218     
  2218   2219     sqlite3OsLeaveMutex();
  2219   2220     id->isOpen = 0;
  2220         -  TRACE2("CLOSE   %-3d\n", id->h);
         2221  +  OSTRACE2("CLOSE   %-3d\n", id->h);
  2221   2222     OpenCounter(-1);
  2222   2223     sqlite3ThreadSafeFree(id);
  2223   2224     *pId = 0;
  2224   2225     return SQLITE_OK;
  2225   2226   }
  2226   2227   
  2227   2228   
................................................................................
  2255   2256     id->dirfd = -1;
  2256   2257     sqlite3OsEnterMutex();
  2257   2258     
  2258   2259     close(id->h);
  2259   2260     
  2260   2261     sqlite3OsLeaveMutex();
  2261   2262     id->isOpen = 0;
  2262         -  TRACE2("CLOSE   %-3d\n", id->h);
         2263  +  OSTRACE2("CLOSE   %-3d\n", id->h);
  2263   2264     OpenCounter(-1);
  2264   2265     sqlite3ThreadSafeFree(id);
  2265   2266     *pId = 0;
  2266   2267     return SQLITE_OK;
  2267   2268   }
  2268   2269   
  2269   2270   #endif /* SQLITE_ENABLE_LOCKING_STYLE */
................................................................................
  2584   2585     if( delFlag ){
  2585   2586       unlink(zFilename);
  2586   2587     }
  2587   2588     if( rc ){
  2588   2589       close(h);
  2589   2590       return SQLITE_NOMEM;
  2590   2591     }
  2591         -  TRACE3("OPEN    %-3d %s\n", h, zFilename);
         2592  +  OSTRACE3("OPEN    %-3d %s\n", h, zFilename);
  2592   2593     f.dirfd = -1;
  2593   2594     f.fullSync = 0;
  2594   2595     f.locktype = 0;
  2595   2596     f.offset = 0;
  2596   2597     f.h = h;
  2597   2598     SET_THREADID(&f);
  2598   2599     pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );

Changes to src/os_win.c.

   608    608       do{
   609    609         rc = DeleteFileA(zConverted);
   610    610       }while( rc==0 && GetFileAttributesA(zConverted)!=0xffffffff
   611    611               && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
   612    612   #endif
   613    613     }
   614    614     sqliteFree(zConverted);
   615         -  TRACE2("DELETE \"%s\"\n", zFilename);
          615  +  OSTRACE2("DELETE \"%s\"\n", zFilename);
   616    616     return rc!=0 ? SQLITE_OK : SQLITE_IOERR;
   617    617   }
   618    618   
   619    619   /*
   620    620   ** Return TRUE if the named file exists.
   621    621   */
   622    622   int sqlite3WinFileExists(const char *zFilename){
................................................................................
   734    734   
   735    735     sqliteFree(zConverted);
   736    736   
   737    737     f.h = h;
   738    738   #if OS_WINCE
   739    739     f.zDeleteOnClose = 0;
   740    740   #endif
   741         -  TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
          741  +  OSTRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
   742    742     return allocateWinFile(&f, pId);
   743    743   }
   744    744   
   745    745   
   746    746   /*
   747    747   ** Attempt to open a new file for exclusive access by this process.
   748    748   ** The file will be opened for both reading and writing.  To avoid
................................................................................
   815    815     f.hMutex = NULL;
   816    816   #endif
   817    817     sqliteFree(zConverted);
   818    818     if( h==INVALID_HANDLE_VALUE ){
   819    819       return SQLITE_CANTOPEN;
   820    820     }
   821    821     f.h = h;
   822         -  TRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
          822  +  OSTRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
   823    823     return allocateWinFile(&f, pId);
   824    824   }
   825    825   
   826    826   /*
   827    827   ** Attempt to open a new file for read-only access.
   828    828   **
   829    829   ** On success, write the file handle into *id and return SQLITE_OK.
................................................................................
   866    866       return SQLITE_CANTOPEN;
   867    867     }
   868    868     f.h = h;
   869    869   #if OS_WINCE
   870    870     f.zDeleteOnClose = 0;
   871    871     f.hMutex = NULL;
   872    872   #endif
   873         -  TRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
          873  +  OSTRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
   874    874     return allocateWinFile(&f, pId);
   875    875   }
   876    876   
   877    877   /*
   878    878   ** Attempt to open a file descriptor for the directory that contains a
   879    879   ** file.  This file descriptor can be used to fsync() the directory
   880    880   ** in order to make sure the creation of a new file is actually written
................................................................................
   951    951       sqlite3Randomness(15, &zBuf[j]);
   952    952       for(i=0; i<15; i++, j++){
   953    953         zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   954    954       }
   955    955       zBuf[j] = 0;
   956    956       if( !sqlite3OsFileExists(zBuf) ) break;
   957    957     }
   958         -  TRACE2("TEMP FILENAME: %s\n", zBuf);
          958  +  OSTRACE2("TEMP FILENAME: %s\n", zBuf);
   959    959     return SQLITE_OK; 
   960    960   }
   961    961   
   962    962   /*
   963    963   ** Close a file.
   964    964   **
   965    965   ** It is reported that an attempt to close a handle might sometimes
................................................................................
   971    971   */
   972    972   #define MX_CLOSE_ATTEMPT 3
   973    973   static int winClose(OsFile **pId){
   974    974     winFile *pFile;
   975    975     int rc = 1;
   976    976     if( pId && (pFile = (winFile*)*pId)!=0 ){
   977    977       int rc, cnt = 0;
   978         -    TRACE2("CLOSE %d\n", pFile->h);
          978  +    OSTRACE2("CLOSE %d\n", pFile->h);
   979    979       do{
   980    980         rc = CloseHandle(pFile->h);
   981    981       }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
   982    982   #if OS_WINCE
   983    983       winceDestroyLock(pFile);
   984    984       if( pFile->zDeleteOnClose ){
   985    985         DeleteFileW(pFile->zDeleteOnClose);
................................................................................
   998    998   ** bytes were read successfully and SQLITE_IOERR if anything goes
   999    999   ** wrong.
  1000   1000   */
  1001   1001   static int winRead(OsFile *id, void *pBuf, int amt){
  1002   1002     DWORD got;
  1003   1003     assert( id!=0 );
  1004   1004     SimulateIOError(return SQLITE_IOERR_READ);
  1005         -  TRACE3("READ %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
         1005  +  OSTRACE3("READ %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
  1006   1006     if( !ReadFile(((winFile*)id)->h, pBuf, amt, &got, 0) ){
  1007   1007       return SQLITE_IOERR_READ;
  1008   1008     }
  1009   1009     if( got==(DWORD)amt ){
  1010   1010       return SQLITE_OK;
  1011   1011     }else{
  1012   1012       memset(&((char*)pBuf)[got], 0, amt-got);
................................................................................
  1020   1020   */
  1021   1021   static int winWrite(OsFile *id, const void *pBuf, int amt){
  1022   1022     int rc = 0;
  1023   1023     DWORD wrote;
  1024   1024     assert( id!=0 );
  1025   1025     SimulateIOError(return SQLITE_IOERR_READ);
  1026   1026     SimulateDiskfullError(return SQLITE_FULL);
  1027         -  TRACE3("WRITE %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
         1027  +  OSTRACE3("WRITE %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
  1028   1028     assert( amt>0 );
  1029   1029     while( amt>0 && (rc = WriteFile(((winFile*)id)->h, pBuf, amt, &wrote, 0))!=0
  1030   1030            && wrote>0 ){
  1031   1031       amt -= wrote;
  1032   1032       pBuf = &((char*)pBuf)[wrote];
  1033   1033     }
  1034   1034     if( !rc || amt>(int)wrote ){
................................................................................
  1053   1053     DWORD rc;
  1054   1054     assert( id!=0 );
  1055   1055   #ifdef SQLITE_TEST
  1056   1056     if( offset ) SimulateDiskfullError(return SQLITE_FULL);
  1057   1057   #endif
  1058   1058     SEEK(offset/1024 + 1);
  1059   1059     rc = SetFilePointer(((winFile*)id)->h, lowerBits, &upperBits, FILE_BEGIN);
  1060         -  TRACE3("SEEK %d %lld\n", ((winFile*)id)->h, offset);
         1060  +  OSTRACE3("SEEK %d %lld\n", ((winFile*)id)->h, offset);
  1061   1061     if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
  1062   1062       return SQLITE_FULL;
  1063   1063     }
  1064   1064     return SQLITE_OK;
  1065   1065   }
  1066   1066   
  1067   1067   /*
  1068   1068   ** Make sure all writes to a particular file are committed to disk.
  1069   1069   */
  1070   1070   static int winSync(OsFile *id, int dataOnly){
  1071   1071     assert( id!=0 );
  1072         -  TRACE3("SYNC %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
         1072  +  OSTRACE3("SYNC %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
  1073   1073     if( FlushFileBuffers(((winFile*)id)->h) ){
  1074   1074       return SQLITE_OK;
  1075   1075     }else{
  1076   1076       return SQLITE_IOERR;
  1077   1077     }
  1078   1078   }
  1079   1079   
................................................................................
  1088   1088   
  1089   1089   /*
  1090   1090   ** Truncate an open file to a specified size
  1091   1091   */
  1092   1092   static int winTruncate(OsFile *id, i64 nByte){
  1093   1093     LONG upperBits = nByte>>32;
  1094   1094     assert( id!=0 );
  1095         -  TRACE3("TRUNCATE %d %lld\n", ((winFile*)id)->h, nByte);
         1095  +  OSTRACE3("TRUNCATE %d %lld\n", ((winFile*)id)->h, nByte);
  1096   1096     SimulateIOError(return SQLITE_IOERR_TRUNCATE);
  1097   1097     SetFilePointer(((winFile*)id)->h, nByte, &upperBits, FILE_BEGIN);
  1098   1098     SetEndOfFile(((winFile*)id)->h);
  1099   1099     return SQLITE_OK;
  1100   1100   }
  1101   1101   
  1102   1102   /*
................................................................................
  1216   1216     int rc = SQLITE_OK;    /* Return code from subroutines */
  1217   1217     int res = 1;           /* Result of a windows lock call */
  1218   1218     int newLocktype;       /* Set id->locktype to this value before exiting */
  1219   1219     int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
  1220   1220     winFile *pFile = (winFile*)id;
  1221   1221   
  1222   1222     assert( pFile!=0 );
  1223         -  TRACE5("LOCK %d %d was %d(%d)\n",
         1223  +  OSTRACE5("LOCK %d %d was %d(%d)\n",
  1224   1224             pFile->h, locktype, pFile->locktype, pFile->sharedLockByte);
  1225   1225   
  1226   1226     /* If there is already a lock of this type or more restrictive on the
  1227   1227     ** OsFile, do nothing. Don't use the end_lock: exit path, as
  1228   1228     ** sqlite3OsEnterMutex() hasn't been called yet.
  1229   1229     */
  1230   1230     if( pFile->locktype>=locktype ){
................................................................................
  1246   1246      || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
  1247   1247     ){
  1248   1248       int cnt = 3;
  1249   1249       while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
  1250   1250         /* Try 3 times to get the pending lock.  The pending lock might be
  1251   1251         ** held by another reader process who will release it momentarily.
  1252   1252         */
  1253         -      TRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
         1253  +      OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
  1254   1254         Sleep(1);
  1255   1255       }
  1256   1256       gotPendingLock = res;
  1257   1257     }
  1258   1258   
  1259   1259     /* Acquire a shared lock
  1260   1260     */
................................................................................
  1284   1284     }
  1285   1285   
  1286   1286     /* Acquire an EXCLUSIVE lock
  1287   1287     */
  1288   1288     if( locktype==EXCLUSIVE_LOCK && res ){
  1289   1289       assert( pFile->locktype>=SHARED_LOCK );
  1290   1290       res = unlockReadLock(pFile);
  1291         -    TRACE2("unreadlock = %d\n", res);
         1291  +    OSTRACE2("unreadlock = %d\n", res);
  1292   1292       res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
  1293   1293       if( res ){
  1294   1294         newLocktype = EXCLUSIVE_LOCK;
  1295   1295       }else{
  1296         -      TRACE2("error-code = %d\n", GetLastError());
         1296  +      OSTRACE2("error-code = %d\n", GetLastError());
  1297   1297       }
  1298   1298     }
  1299   1299   
  1300   1300     /* If we are holding a PENDING lock that ought to be released, then
  1301   1301     ** release it now.
  1302   1302     */
  1303   1303     if( gotPendingLock && locktype==SHARED_LOCK ){
................................................................................
  1306   1306   
  1307   1307     /* Update the state of the lock has held in the file descriptor then
  1308   1308     ** return the appropriate result code.
  1309   1309     */
  1310   1310     if( res ){
  1311   1311       rc = SQLITE_OK;
  1312   1312     }else{
  1313         -    TRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
         1313  +    OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
  1314   1314              locktype, newLocktype);
  1315   1315       rc = SQLITE_BUSY;
  1316   1316     }
  1317   1317     pFile->locktype = newLocktype;
  1318   1318     return rc;
  1319   1319   }
  1320   1320   
................................................................................
  1325   1325   */
  1326   1326   static int winCheckReservedLock(OsFile *id){
  1327   1327     int rc;
  1328   1328     winFile *pFile = (winFile*)id;
  1329   1329     assert( pFile!=0 );
  1330   1330     if( pFile->locktype>=RESERVED_LOCK ){
  1331   1331       rc = 1;
  1332         -    TRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
         1332  +    OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
  1333   1333     }else{
  1334   1334       rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
  1335   1335       if( rc ){
  1336   1336         UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
  1337   1337       }
  1338   1338       rc = !rc;
  1339         -    TRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
         1339  +    OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
  1340   1340     }
  1341   1341     return rc;
  1342   1342   }
  1343   1343   
  1344   1344   /*
  1345   1345   ** Lower the locking level on file descriptor id to locktype.  locktype
  1346   1346   ** must be either NO_LOCK or SHARED_LOCK.
................................................................................
  1354   1354   */
  1355   1355   static int winUnlock(OsFile *id, int locktype){
  1356   1356     int type;
  1357   1357     int rc = SQLITE_OK;
  1358   1358     winFile *pFile = (winFile*)id;
  1359   1359     assert( pFile!=0 );
  1360   1360     assert( locktype<=SHARED_LOCK );
  1361         -  TRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
         1361  +  OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
  1362   1362             pFile->locktype, pFile->sharedLockByte);
  1363   1363     type = pFile->locktype;
  1364   1364     if( type>=EXCLUSIVE_LOCK ){
  1365   1365       UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
  1366   1366       if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
  1367   1367         /* This should never happen.  We should always be able to
  1368   1368         ** reacquire the read lock */

Changes to 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.299 2007/03/26 15:46:01 danielk1977 Exp $
           21  +** @(#) $Id: pager.c,v 1.300 2007/03/26 22:05:02 drh 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>
    29     29   
    30     30   /*
    31     31   ** Macros for troubleshooting.  Normally turned off
    32     32   */
    33     33   #if 0
    34     34   #define sqlite3DebugPrintf printf
    35         -#define TRACE1(X)       sqlite3DebugPrintf(X)
    36         -#define TRACE2(X,Y)     sqlite3DebugPrintf(X,Y)
    37         -#define TRACE3(X,Y,Z)   sqlite3DebugPrintf(X,Y,Z)
    38         -#define TRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
    39         -#define TRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
           35  +#define PAGERTRACE1(X)       sqlite3DebugPrintf(X)
           36  +#define PAGERTRACE2(X,Y)     sqlite3DebugPrintf(X,Y)
           37  +#define PAGERTRACE3(X,Y,Z)   sqlite3DebugPrintf(X,Y,Z)
           38  +#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
           39  +#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
    40     40   #else
    41         -#define TRACE1(X)
    42         -#define TRACE2(X,Y)
    43         -#define TRACE3(X,Y,Z)
    44         -#define TRACE4(X,Y,Z,W)
    45         -#define TRACE5(X,Y,Z,W,V)
           41  +#define PAGERTRACE1(X)
           42  +#define PAGERTRACE2(X,Y)
           43  +#define PAGERTRACE3(X,Y,Z)
           44  +#define PAGERTRACE4(X,Y,Z,W)
           45  +#define PAGERTRACE5(X,Y,Z,W,V)
    46     46   #endif
    47     47   
    48     48   /*
    49         -** The following two macros are used within the TRACEX() macros above
           49  +** The following two macros are used within the PAGERTRACEX() macros above
    50     50   ** to print out file-descriptors. 
    51     51   **
    52     52   ** PAGERID() takes a pointer to a Pager struct as it's argument. The
    53     53   ** associated file-descriptor is returned. FILEHANDLEID() takes an OsFile
    54     54   ** struct as it's argument.
    55     55   */
    56     56   #define PAGERID(p) ((int)(p->fd))
................................................................................
  1078   1078     ** content.  Two conditions must be met before writing to the database
  1079   1079     ** files. (1) the database must be locked.  (2) we know that the original
  1080   1080     ** page content is in the main journal either because the page is not in
  1081   1081     ** cache or else it is marked as needSync==0.
  1082   1082     */
  1083   1083     pPg = pager_lookup(pPager, pgno);
  1084   1084     assert( pPager->state>=PAGER_EXCLUSIVE || pPg!=0 );
  1085         -  TRACE3("PLAYBACK %d page %d\n", PAGERID(pPager), pgno);
         1085  +  PAGERTRACE3("PLAYBACK %d page %d\n", PAGERID(pPager), pgno);
  1086   1086     if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
  1087   1087       rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
  1088   1088       if( rc==SQLITE_OK ){
  1089   1089         rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize);
  1090   1090       }
  1091   1091       if( pPg ){
  1092   1092         makeClean(pPg);
................................................................................
  1222   1222       char *zBuf = pPager->pTmpSpace;        /* Temp storage for one page */
  1223   1223       if( !pPg->dirty ) continue;
  1224   1224       if( (int)pPg->pgno <= pPager->origDbSize ){
  1225   1225         rc = sqlite3OsSeek(pPager->fd, pPager->pageSize*(i64)(pPg->pgno-1));
  1226   1226         if( rc==SQLITE_OK ){
  1227   1227           rc = sqlite3OsRead(pPager->fd, zBuf, pPager->pageSize);
  1228   1228         }
  1229         -      TRACE3("REFETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
         1229  +      PAGERTRACE3("REFETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
  1230   1230         if( rc ) break;
  1231   1231         CODEC1(pPager, zBuf, pPg->pgno, 2);
  1232   1232       }else{
  1233   1233         memset(zBuf, 0, pPager->pageSize);
  1234   1234       }
  1235   1235       if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), pPager->pageSize) ){
  1236   1236         memcpy(PGHDR_TO_DATA(pPg), zBuf, pPager->pageSize);
................................................................................
  1730   1730     if( !pPager || !zFullPathname || !pPager->pTmpSpace || rc!=SQLITE_OK ){
  1731   1731       sqlite3OsClose(&fd);
  1732   1732       sqliteFree(zFullPathname);
  1733   1733       sqliteFree(pPager);
  1734   1734       return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
  1735   1735     }
  1736   1736   
  1737         -  TRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
         1737  +  PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
  1738   1738     IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
  1739   1739     pPager->zFilename = (char*)&pPager[1];
  1740   1740     pPager->zDirectory = &pPager->zFilename[nameLen+1];
  1741   1741     pPager->zJournal = &pPager->zDirectory[nameLen+1];
  1742   1742     strcpy(pPager->zFilename, zFullPathname);
  1743   1743     strcpy(pPager->zDirectory, zFullPathname);
  1744   1744   
................................................................................
  1824   1824   ** and returned.
  1825   1825   */
  1826   1826   int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
  1827   1827     assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
  1828   1828     if( !pPager->memDb && pPager->nRef==0 ){
  1829   1829       pager_reset(pPager);
  1830   1830       pPager->pageSize = pageSize;
  1831         -    sqlite3ReallocOrFree((void **)&pPager->pTmpSpace, pageSize);
         1831  +    sqlite3ReallocOrFree(&pPager->pTmpSpace, pageSize);
  1832   1832     }
  1833   1833     return pPager->pageSize;
  1834   1834   }
  1835   1835   
  1836   1836   /*
  1837   1837   ** The following set of routines are used to disable the simulated
  1838   1838   ** I/O error mechanism.  These routines are used to avoid simulated
................................................................................
  2135   2135   
  2136   2136     disable_simulated_io_errors();
  2137   2137     pPager->errCode = 0;
  2138   2138     pPager->exclusiveMode = 0;
  2139   2139     pager_reset(pPager);
  2140   2140     pagerUnlockAndRollback(pPager);
  2141   2141     enable_simulated_io_errors();
  2142         -  TRACE2("CLOSE %d\n", PAGERID(pPager));
         2142  +  PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
  2143   2143     IOTRACE(("CLOSE %p\n", pPager))
  2144   2144     assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
  2145   2145     if( pPager->journalOpen ){
  2146   2146       sqlite3OsClose(&pPager->jfd);
  2147   2147     }
  2148   2148     sqliteFree(pPager->aInJournal);
  2149   2149     if( pPager->stmtOpen ){
................................................................................
  2281   2281         {
  2282   2282           /* Write the nRec value into the journal file header. If in
  2283   2283           ** full-synchronous mode, sync the journal first. This ensures that
  2284   2284           ** all data has really hit the disk before nRec is updated to mark
  2285   2285           ** it as a candidate for rollback. 
  2286   2286           */
  2287   2287           if( pPager->fullSync ){
  2288         -          TRACE2("SYNC journal of %d\n", PAGERID(pPager));
         2288  +          PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
  2289   2289             IOTRACE(("JSYNC %p\n", pPager))
  2290   2290             rc = sqlite3OsSync(pPager->jfd, 0);
  2291   2291             if( rc!=0 ) return rc;
  2292   2292           }
  2293   2293           rc = sqlite3OsSeek(pPager->jfd,
  2294   2294                              pPager->journalHdr + sizeof(aJournalMagic));
  2295   2295           if( rc ) return rc;
................................................................................
  2297   2297                     pPager->journalHdr + sizeof(aJournalMagic), 4))
  2298   2298           rc = write32bits(pPager->jfd, pPager->nRec);
  2299   2299           if( rc ) return rc;
  2300   2300   
  2301   2301           rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff);
  2302   2302           if( rc ) return rc;
  2303   2303         }
  2304         -      TRACE2("SYNC journal of %d\n", PAGERID(pPager));
         2304  +      PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
  2305   2305         IOTRACE(("JSYNC %d\n", pPager))
  2306   2306         rc = sqlite3OsSync(pPager->jfd, pPager->full_fsync);
  2307   2307         if( rc!=0 ) return rc;
  2308   2308         pPager->journalStarted = 1;
  2309   2309       }
  2310   2310       pPager->needSync = 0;
  2311   2311   
................................................................................
  2436   2436       /* If there are dirty pages in the page cache with page numbers greater
  2437   2437       ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
  2438   2438       ** make the file smaller (presumably by auto-vacuum code). Do not write
  2439   2439       ** any such pages to the file.
  2440   2440       */
  2441   2441       if( pList->pgno<=pPager->dbSize ){
  2442   2442         char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
  2443         -      TRACE3("STORE %d page %d\n", PAGERID(pPager), pList->pgno);
         2443  +      PAGERTRACE3("STORE %d page %d\n", PAGERID(pPager), pList->pgno);
  2444   2444         IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno))
  2445   2445         rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize);
  2446   2446         TEST_INCR(pPager->nWrite);
  2447   2447       }
  2448   2448   #ifndef NDEBUG
  2449   2449       else{
  2450         -      TRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
         2450  +      PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
  2451   2451       }
  2452   2452   #endif
  2453   2453       if( rc ) return rc;
  2454   2454       pList->dirty = 0;
  2455   2455   #ifdef SQLITE_CHECK_PAGES
  2456   2456       pList->pageHash = pager_pagehash(pList);
  2457   2457   #endif
................................................................................
  2903   2903         assert( MEMDB==0 );
  2904   2904         rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
  2905   2905         if( rc==SQLITE_OK ){
  2906   2906           rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg),
  2907   2907                                 pPager->pageSize);
  2908   2908         }
  2909   2909         IOTRACE(("PGIN %p %d\n", pPager, pgno))
  2910         -      TRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
         2910  +      PAGERTRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
  2911   2911         CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
  2912   2912         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
  2913   2913           pPg->pgno = 0;
  2914   2914           sqlite3PagerUnref(pPg);
  2915   2915           return rc;
  2916   2916         }else{
  2917   2917           TEST_INCR(pPager->nRead);
................................................................................
  3138   3138             rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
  3139   3139           }
  3140   3140         }
  3141   3141         if( rc!=SQLITE_OK ){
  3142   3142           return rc;
  3143   3143         }
  3144   3144         pPager->dirtyCache = 0;
  3145         -      TRACE2("TRANSACTION %d\n", PAGERID(pPager));
         3145  +      PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
  3146   3146         if( pPager->useJournal && !pPager->tempFile ){
  3147   3147           rc = pager_open_journal(pPager);
  3148   3148         }
  3149   3149       }
  3150   3150     }else if( pPager->journalOpen && pPager->journalOff==0 ){
  3151   3151       /* This happens when the pager was in exclusive-access mode last
  3152   3152       ** time a (read or write) transaction was successfully concluded
................................................................................
  3272   3272       ** the transaction journal if it is not there already.
  3273   3273       */
  3274   3274       if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
  3275   3275         if( (int)pPg->pgno <= pPager->origDbSize ){
  3276   3276           int szPg;
  3277   3277           if( MEMDB ){
  3278   3278             PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  3279         -          TRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
         3279  +          PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  3280   3280             assert( pHist->pOrig==0 );
  3281   3281             pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
  3282   3282             if( pHist->pOrig ){
  3283   3283               memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
  3284   3284             }
  3285   3285           }else{
  3286   3286             u32 cksum, saved;
................................................................................
  3297   3297             put32bits(pEnd, cksum);
  3298   3298             szPg = pPager->pageSize+8;
  3299   3299             put32bits(pData2, pPg->pgno);
  3300   3300             rc = sqlite3OsWrite(pPager->jfd, pData2, szPg);
  3301   3301             IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
  3302   3302                      pPager->journalOff, szPg))
  3303   3303             pPager->journalOff += szPg;
  3304         -          TRACE4("JOURNAL %d page %d needSync=%d\n",
         3304  +          PAGERTRACE4("JOURNAL %d page %d needSync=%d\n",
  3305   3305                     PAGERID(pPager), pPg->pgno, pPg->needSync);
  3306   3306             *(u32*)pEnd = saved;
  3307   3307   
  3308   3308   	  /* An error has occured writing to the journal file. The 
  3309   3309             ** transaction will be rolled back by the layer above.
  3310   3310             */
  3311   3311             if( rc!=SQLITE_OK ){
................................................................................
  3319   3319             if( pPager->stmtInUse ){
  3320   3320               pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3321   3321               page_add_to_stmt_list(pPg);
  3322   3322             }
  3323   3323           }
  3324   3324         }else{
  3325   3325           pPg->needSync = !pPager->journalStarted && !pPager->noSync;
  3326         -        TRACE4("APPEND %d page %d needSync=%d\n",
         3326  +        PAGERTRACE4("APPEND %d page %d needSync=%d\n",
  3327   3327                   PAGERID(pPager), pPg->pgno, pPg->needSync);
  3328   3328         }
  3329   3329         if( pPg->needSync ){
  3330   3330           pPager->needSync = 1;
  3331   3331         }
  3332   3332         pPg->inJournal = 1;
  3333   3333       }
................................................................................
  3342   3342         if( MEMDB ){
  3343   3343           PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  3344   3344           assert( pHist->pStmt==0 );
  3345   3345           pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
  3346   3346           if( pHist->pStmt ){
  3347   3347             memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
  3348   3348           }
  3349         -        TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
         3349  +        PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  3350   3350         }else{
  3351   3351           char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
  3352   3352           put32bits(pData2, pPg->pgno);
  3353   3353           rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4);
  3354         -        TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
         3354  +        PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
  3355   3355           if( rc!=SQLITE_OK ){
  3356   3356             return rc;
  3357   3357           }
  3358   3358           pPager->stmtNRec++;
  3359   3359           assert( pPager->aInStmt!=0 );
  3360   3360           pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3361   3361         }
................................................................................
  3518   3518         ** When the database file grows, we must make sure that the last page
  3519   3519         ** gets written at least once so that the disk file will be the correct
  3520   3520         ** size. If you do not write this page and the size of the file
  3521   3521         ** on the disk ends up being too small, that can lead to database
  3522   3522         ** corruption during the next transaction.
  3523   3523         */
  3524   3524       }else{
  3525         -      TRACE3("DONT_WRITE page %d of %d\n", pgno, PAGERID(pPager));
         3525  +      PAGERTRACE3("DONT_WRITE page %d of %d\n", pgno, PAGERID(pPager));
  3526   3526         IOTRACE(("CLEAN %p %d\n", pPager, pgno))
  3527   3527         makeClean(pPg);
  3528   3528   #ifdef SQLITE_CHECK_PAGES
  3529   3529         pPg->pageHash = pager_pagehash(pPg);
  3530   3530   #endif
  3531   3531       }
  3532   3532     }
................................................................................
  3548   3548       assert( pPager->aInJournal!=0 );
  3549   3549       pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3550   3550       pPg->inJournal = 1;
  3551   3551       if( pPager->stmtInUse ){
  3552   3552         pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3553   3553         page_add_to_stmt_list(pPg);
  3554   3554       }
  3555         -    TRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
         3555  +    PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
  3556   3556       IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
  3557   3557     }
  3558   3558     if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
  3559   3559       assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
  3560   3560       assert( pPager->aInStmt!=0 );
  3561   3561       pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
  3562   3562       page_add_to_stmt_list(pPg);
................................................................................
  3577   3577   
  3578   3578     if( pPager->errCode ){
  3579   3579       return pPager->errCode;
  3580   3580     }
  3581   3581     if( pPager->state<PAGER_RESERVED ){
  3582   3582       return SQLITE_ERROR;
  3583   3583     }
  3584         -  TRACE2("COMMIT %d\n", PAGERID(pPager));
         3584  +  PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
  3585   3585     if( MEMDB ){
  3586   3586       pPg = pager_get_all_dirty_pages(pPager);
  3587   3587       while( pPg ){
  3588   3588         clearHistory(PGHDR_TO_HIST(pPg, pPager));
  3589   3589         pPg->dirty = 0;
  3590   3590         pPg->inJournal = 0;
  3591   3591         pPg->inStmt = 0;
................................................................................
  3631   3631   ** process is writing trash into the journal file (SQLITE_CORRUPT) or
  3632   3632   ** unless a prior malloc() failed (SQLITE_NOMEM).  Appropriate error
  3633   3633   ** codes are returned for all these occasions.  Otherwise,
  3634   3634   ** SQLITE_OK is returned.
  3635   3635   */
  3636   3636   int sqlite3PagerRollback(Pager *pPager){
  3637   3637     int rc;
  3638         -  TRACE2("ROLLBACK %d\n", PAGERID(pPager));
         3638  +  PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
  3639   3639     if( MEMDB ){
  3640   3640       PgHdr *p;
  3641   3641       for(p=pPager->pAll; p; p=p->pNextAll){
  3642   3642         PgHistory *pHist;
  3643   3643         assert( !p->alwaysRollback );
  3644   3644         if( !p->dirty ){
  3645   3645           assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
................................................................................
  3646   3646           assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
  3647   3647           continue;
  3648   3648         }
  3649   3649   
  3650   3650         pHist = PGHDR_TO_HIST(p, pPager);
  3651   3651         if( pHist->pOrig ){
  3652   3652           memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
  3653         -        TRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
         3653  +        PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
  3654   3654         }else{
  3655         -        TRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
         3655  +        PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
  3656   3656         }
  3657   3657         clearHistory(pHist);
  3658   3658         p->dirty = 0;
  3659   3659         p->inJournal = 0;
  3660   3660         p->inStmt = 0;
  3661   3661         p->pPrevStmt = p->pNextStmt = 0;
  3662   3662         if( pPager->xReiniter ){
................................................................................
  3746   3746   ** changes of a single SQL command within a larger transaction.
  3747   3747   */
  3748   3748   int sqlite3PagerStmtBegin(Pager *pPager){
  3749   3749     int rc;
  3750   3750     assert( !pPager->stmtInUse );
  3751   3751     assert( pPager->state>=PAGER_SHARED );
  3752   3752     assert( pPager->dbSize>=0 );
  3753         -  TRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
         3753  +  PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
  3754   3754     if( MEMDB ){
  3755   3755       pPager->stmtInUse = 1;
  3756   3756       pPager->stmtSize = pPager->dbSize;
  3757   3757       return SQLITE_OK;
  3758   3758     }
  3759   3759     if( !pPager->journalOpen ){
  3760   3760       pPager->stmtAutoopen = 1;
................................................................................
  3794   3794   
  3795   3795   /*
  3796   3796   ** Commit a statement.
  3797   3797   */
  3798   3798   int sqlite3PagerStmtCommit(Pager *pPager){
  3799   3799     if( pPager->stmtInUse ){
  3800   3800       PgHdr *pPg, *pNext;
  3801         -    TRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
         3801  +    PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
  3802   3802       if( !MEMDB ){
  3803   3803         sqlite3OsSeek(pPager->stfd, 0);
  3804   3804         /* sqlite3OsTruncate(pPager->stfd, 0); */
  3805   3805         sqliteFree( pPager->aInStmt );
  3806   3806         pPager->aInStmt = 0;
  3807   3807       }
  3808   3808       for(pPg=pPager->pStmt; pPg; pPg=pNext){
................................................................................
  3826   3826   
  3827   3827   /*
  3828   3828   ** Rollback a statement.
  3829   3829   */
  3830   3830   int sqlite3PagerStmtRollback(Pager *pPager){
  3831   3831     int rc;
  3832   3832     if( pPager->stmtInUse ){
  3833         -    TRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
         3833  +    PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
  3834   3834       if( MEMDB ){
  3835   3835         PgHdr *pPg;
  3836   3836         for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){
  3837   3837           PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
  3838   3838           if( pHist->pStmt ){
  3839   3839             memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
  3840   3840             sqliteFree(pHist->pStmt);
................................................................................
  3943   3943   **
  3944   3944   ** If parameter nTrunc is non-zero, then the pager file is truncated to
  3945   3945   ** nTrunc pages (this is used by auto-vacuum databases).
  3946   3946   */
  3947   3947   int sqlite3PagerSync(Pager *pPager, const char *zMaster, Pgno nTrunc){
  3948   3948     int rc = SQLITE_OK;
  3949   3949   
  3950         -  TRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", 
         3950  +  PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", 
  3951   3951         pPager->zFilename, zMaster, nTrunc);
  3952   3952   
  3953   3953     /* If this is an in-memory db, or no pages have been written to, or this
  3954   3954     ** function has already been called, it is a no-op.
  3955   3955     */
  3956   3956     if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
  3957   3957       PgHdr *pPg;
................................................................................
  4039   4039   int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
  4040   4040     PgHdr *pPgOld; 
  4041   4041     int h;
  4042   4042     Pgno needSyncPgno = 0;
  4043   4043   
  4044   4044     assert( pPg->nRef>0 );
  4045   4045   
  4046         -  TRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", 
         4046  +  PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", 
  4047   4047         PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
  4048   4048     IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
  4049   4049   
  4050   4050     if( pPg->needSync ){
  4051   4051       needSyncPgno = pPg->pgno;
  4052   4052       assert( pPg->inJournal );
  4053   4053       assert( pPg->dirty );

Changes to 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.44 2007/03/14 15:37:04 danielk1977 Exp $
           16  +** $Id: prepare.c,v 1.45 2007/03/26 22:05:02 drh 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
................................................................................
   562    562   /*
   563    563   ** Rerun the compilation of a statement after a schema change.
   564    564   ** Return true if the statement was recompiled successfully.
   565    565   ** Return false if there is an error of some kind.
   566    566   */
   567    567   int sqlite3Reprepare(Vdbe *p){
   568    568     int rc;
   569         -  Vdbe *pNew;
          569  +  sqlite3_stmt *pNew;
   570    570     const char *zSql;
   571    571     sqlite3 *db;
   572    572     
   573    573     zSql = sqlite3VdbeGetSql(p);
   574    574     if( zSql==0 ){
   575    575       return 0;
   576    576     }
   577    577     db = sqlite3VdbeDb(p);
   578         -  rc = sqlite3Prepare(db, zSql, -1, 0, (sqlite3_stmt**)&pNew, 0);
          578  +  rc = sqlite3Prepare(db, zSql, -1, 0, &pNew, 0);
   579    579     if( rc ){
   580    580       assert( pNew==0 );
   581    581       return 0;
   582    582     }else{
   583    583       assert( pNew!=0 );
   584    584     }
   585         -  sqlite3VdbeSwap(pNew, p);
   586         -  sqlite3_transfer_bindings((sqlite3_stmt*)pNew, (sqlite3_stmt*)p);
   587         -  sqlite3VdbeResetStepResult(pNew);
   588         -  sqlite3VdbeFinalize(pNew);
          585  +  sqlite3VdbeSwap((Vdbe*)pNew, p);
          586  +  sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p);
          587  +  sqlite3VdbeResetStepResult((Vdbe*)pNew);
          588  +  sqlite3VdbeFinalize((Vdbe*)pNew);
   589    589     return 1;
   590    590   }
   591    591   
   592    592   
   593    593   /*
   594    594   ** Two versions of the official API.  Legacy and new use.  In the legacy
   595    595   ** version, the original SQL text is not saved in the prepared statement

Changes to 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.543 2007/03/24 16:45:05 danielk1977 Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.544 2007/03/26 22:05:02 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19         -/*
    20         -** Extra interface definitions for those who need them
    21         -*/
    22         -#ifdef SQLITE_EXTRA
    23         -# include "sqliteExtra.h"
           19  +#if defined(SQLITE_TCL) || defined(TCLSH)
           20  +# include <tcl.h>
    24     21   #endif
    25     22   
    26     23   /*
    27     24   ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
    28     25   ** Setting NDEBUG makes the code smaller and run faster.  So the following
    29     26   ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
    30     27   ** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
................................................................................
  1566   1563   void *sqlite3Malloc(int,int);
  1567   1564   void *sqlite3MallocRaw(int,int);
  1568   1565   void sqlite3Free(void*);
  1569   1566   void *sqlite3Realloc(void*,int);
  1570   1567   char *sqlite3StrDup(const char*);
  1571   1568   char *sqlite3StrNDup(const char*, int);
  1572   1569   # define sqlite3CheckMemory(a,b)
  1573         -void sqlite3ReallocOrFree(void**,int);
         1570  +void sqlite3ReallocOrFree(void*,int);
  1574   1571   void sqlite3FreeX(void*);
  1575   1572   void *sqlite3MallocX(int);
  1576   1573   int sqlite3AllocSize(void *);
  1577   1574   
  1578   1575   char *sqlite3MPrintf(const char*, ...);
  1579   1576   char *sqlite3VMPrintf(const char*, va_list);
  1580   1577   void sqlite3DebugPrintf(const char*, ...);
................................................................................
  1623   1620   #else
  1624   1621   # define sqlite3ViewGetColumnNames(A,B) 0
  1625   1622   #endif
  1626   1623   
  1627   1624   void sqlite3DropTable(Parse*, SrcList*, int, int);
  1628   1625   void sqlite3DeleteTable(sqlite3*, Table*);
  1629   1626   void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
  1630         -int sqlite3ArrayAllocate(void**,int,int);
         1627  +int sqlite3ArrayAllocate(void*,int,int);
  1631   1628   IdList *sqlite3IdListAppend(IdList*, Token*);
  1632   1629   int sqlite3IdListIndex(IdList*,const char*);
  1633   1630   SrcList *sqlite3SrcListAppend(SrcList*, Token*, Token*);
  1634   1631   SrcList *sqlite3SrcListAppendFromTerm(SrcList*, Token*, Token*, Token*,
  1635   1632                                         Select*, Expr*, IdList*);
  1636   1633   void sqlite3SrcListShiftJoinType(SrcList*);
  1637   1634   void sqlite3SrcListAssignCursors(Parse*, SrcList*);

Changes to src/test1.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Code for testing all sorts of SQLite interfaces.  This code
    13     13   ** is not included in the SQLite library.  It is used for automated
    14     14   ** testing of the SQLite library.
    15     15   **
    16         -** $Id: test1.c,v 1.229 2007/02/24 13:53:05 drh Exp $
           16  +** $Id: test1.c,v 1.230 2007/03/26 22:05:02 drh Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "tcl.h"
    20     20   #include "os.h"
    21     21   #include <stdlib.h>
    22     22   #include <string.h>
    23     23   
................................................................................
   108    108       case SQLITE_ROW:        zName = "SQLITE_ROW";         break;
   109    109       case SQLITE_DONE:       zName = "SQLITE_DONE";        break;
   110    110       case SQLITE_NOTADB:     zName = "SQLITE_NOTADB";      break;
   111    111       default:                zName = "SQLITE_Unknown";     break;
   112    112     }
   113    113     return zName;
   114    114   }
   115         -#define errorName sqlite3TestErrorName
          115  +#define t1ErrorName sqlite3TestErrorName
   116    116   
   117    117   /*
   118    118   ** Convert an sqlite3_stmt* into an sqlite3*.  This depends on the
   119    119   ** fact that the sqlite3* is the first field in the Vdbe structure.
   120    120   */
   121    121   #define StmtToDb(X)   sqlite3_db_handle(X)
   122    122   
................................................................................
   125    125   ** from sqlite3_errcode.
   126    126   */
   127    127   int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
   128    128     if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
   129    129       char zBuf[200];
   130    130       int r2 = sqlite3_errcode(db);
   131    131       sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
   132         -       errorName(rc), rc, errorName(r2), r2);
          132  +       t1ErrorName(rc), rc, t1ErrorName(r2), r2);
   133    133       Tcl_ResetResult(interp);
   134    134       Tcl_AppendResult(interp, zBuf, 0);
   135    135       return 1;
   136    136     }
   137    137     return 0;
   138    138   }
   139    139   
................................................................................
   486    486     if( argc!=2 ){
   487    487       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   488    488          " FILENAME\"", 0);
   489    489       return TCL_ERROR;
   490    490     }
   491    491     if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   492    492     rc = sqlite3_close(db);
   493         -  Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
          493  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
   494    494     return TCL_OK;
   495    495   }
   496    496   
   497    497   /*
   498    498   ** Implementation of the x_coalesce() function.
   499    499   ** Return the first argument non-NULL argument.
   500    500   */
   501         -static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          501  +static void t1_ifnullFunc(
          502  +  sqlite3_context *context,
          503  +  int argc,
          504  +  sqlite3_value **argv
          505  +){
   502    506     int i;
   503    507     for(i=0; i<argc; i++){
   504    508       if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
   505    509         sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
   506    510             sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
   507    511         break;
   508    512       }
................................................................................
   678    682     if( argc!=2 ){
   679    683       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   680    684          " DB\"", 0);
   681    685       return TCL_ERROR;
   682    686     }
   683    687     if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   684    688     rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0, 
   685         -        ifnullFunc, 0, 0);
          689  +        t1_ifnullFunc, 0, 0);
   686    690     if( rc==SQLITE_OK ){
   687    691       rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0, 
   688    692             hex8Func, 0, 0);
   689    693     }
   690    694     if( rc==SQLITE_OK ){
   691    695       rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0, 
   692    696             hex16Func, 0, 0);
................................................................................
   712    716                 sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
   713    717                 1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
   714    718       sqlite3ValueFree(pVal);
   715    719     }
   716    720   #endif
   717    721   
   718    722     if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
   719         -  Tcl_SetResult(interp, (char *)errorName(rc), 0);
          723  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
   720    724     return TCL_OK;
   721    725   }
   722    726   
   723    727   /*
   724    728   ** Routines to implement the x_count() aggregate function.
   725    729   **
   726    730   ** x_count() counts the number of non-null arguments.  But there are
................................................................................
   727    731   ** some twists for testing purposes.
   728    732   **
   729    733   ** If the argument to x_count() is 40 then a UTF-8 error is reported
   730    734   ** on the step function.  If x_count(41) is seen, then a UTF-16 error
   731    735   ** is reported on the step function.  If the total count is 42, then
   732    736   ** a UTF-8 error is reported on the finalize function.
   733    737   */
   734         -typedef struct CountCtx CountCtx;
   735         -struct CountCtx {
          738  +typedef struct t1CountCtx t1CountCtx;
          739  +struct t1CountCtx {
   736    740     int n;
   737    741   };
   738         -static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
   739         -  CountCtx *p;
          742  +static void t1CountStep(
          743  +  sqlite3_context *context,
          744  +  int argc,
          745  +  sqlite3_value **argv
          746  +){
          747  +  t1CountCtx *p;
   740    748     p = sqlite3_aggregate_context(context, sizeof(*p));
   741    749     if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
   742    750       p->n++;
   743    751     }
   744    752     if( argc>0 ){
   745    753       int v = sqlite3_value_int(argv[0]);
   746    754       if( v==40 ){
................................................................................
   749    757       }else if( v==41 ){
   750    758         const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
   751    759         sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
   752    760   #endif
   753    761       }
   754    762     }
   755    763   }   
   756         -static void countFinalize(sqlite3_context *context){
   757         -  CountCtx *p;
          764  +static void t1CountFinalize(sqlite3_context *context){
          765  +  t1CountCtx *p;
   758    766     p = sqlite3_aggregate_context(context, sizeof(*p));
   759    767     if( p ){
   760    768       if( p->n==42 ){
   761    769         sqlite3_result_error(context, "x_count totals to 42", -1);
   762    770       }else{
   763    771         sqlite3_result_int(context, p ? p->n : 0);
   764    772       }
................................................................................
   790    798     if( argc!=2 ){
   791    799       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
   792    800          " FILENAME\"", 0);
   793    801       return TCL_ERROR;
   794    802     }
   795    803     if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
   796    804     rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
   797         -      countStep,countFinalize);
          805  +      t1CountStep,t1CountFinalize);
   798    806     if( rc==SQLITE_OK ){
   799    807       sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
   800         -        countStep,countFinalize);
          808  +        t1CountStep,t1CountFinalize);
   801    809     }
   802    810     if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
   803    811     return TCL_OK;
   804    812   }
   805    813   
   806    814   
   807    815   /*
................................................................................
  1493   1501   
  1494   1502     if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1495   1503   
  1496   1504     if( pStmt ){
  1497   1505       db = StmtToDb(pStmt);
  1498   1506     }
  1499   1507     rc = sqlite3_finalize(pStmt);
  1500         -  Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         1508  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  1501   1509     if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1502   1510     return TCL_OK;
  1503   1511   }
  1504   1512   
  1505   1513   /*
  1506   1514   ** Usage:  sqlite3_reset  STMT 
  1507   1515   **
................................................................................
  1524   1532   
  1525   1533     if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  1526   1534   
  1527   1535     rc = sqlite3_reset(pStmt);
  1528   1536     if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
  1529   1537       return TCL_ERROR;
  1530   1538     }
  1531         -  Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         1539  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  1532   1540   /*
  1533   1541     if( rc ){
  1534   1542       return TCL_ERROR;
  1535   1543     }
  1536   1544   */
  1537   1545     return TCL_OK;
  1538   1546   }
................................................................................
  2048   2056     int i;
  2049   2057     if( objc!=1 ){
  2050   2058       Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
  2051   2059     }
  2052   2060   
  2053   2061     zCode = Tcl_GetString(objv[1]);
  2054   2062     for(i=0; i<200; i++){
  2055         -    if( 0==strcmp(errorName(i), zCode) ) break;
         2063  +    if( 0==strcmp(t1ErrorName(i), zCode) ) break;
  2056   2064     }
  2057   2065     Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
  2058   2066     return TCL_OK;
  2059   2067   }
  2060   2068   
  2061   2069   /*
  2062   2070   ** Usage:    breakpoint
................................................................................
  2500   2508     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  2501   2509     rc = sqlite3_errcode(db);
  2502   2510     if( (rc&0xff)==rc ){
  2503   2511       zBuf[0] = 0;
  2504   2512     }else{
  2505   2513       sprintf(zBuf,"+%d", rc>>8);
  2506   2514     }
  2507         -  Tcl_AppendResult(interp, (char *)errorName(rc), zBuf, 0);
         2515  +  Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
  2508   2516     return TCL_OK;
  2509   2517   }
  2510   2518   
  2511   2519   /*
  2512   2520   ** Usage:   test_errmsg DB
  2513   2521   **
  2514   2522   ** Returns the UTF-8 representation of the error message string for the
................................................................................
  2896   2904       return TCL_ERROR;
  2897   2905     }
  2898   2906   
  2899   2907     if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  2900   2908     rc = sqlite3_step(pStmt);
  2901   2909   
  2902   2910     /* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
  2903         -  Tcl_SetResult(interp, (char *)errorName(rc), 0);
         2911  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  2904   2912     return TCL_OK;
  2905   2913   }
  2906   2914   
  2907   2915   /*
  2908   2916   ** Usage: sqlite3_column_count STMT 
  2909   2917   **
  2910   2918   ** Return the number of columns returned by the sql statement STMT.
................................................................................
  3133   3141   #ifndef SQLITE_OMIT_GLOBALRECOVER
  3134   3142     int rc;
  3135   3143     if( objc!=1 ){
  3136   3144       Tcl_WrongNumArgs(interp, 1, objv, "");
  3137   3145       return TCL_ERROR;
  3138   3146     }
  3139   3147     rc = sqlite3_global_recover();
  3140         -  Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3148  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3141   3149   #endif
  3142   3150     return TCL_OK;
  3143   3151   }
  3144   3152   
  3145   3153   /*
  3146   3154   ** Usage: sqlite3_column_text STMT column
  3147   3155   **
................................................................................
  3231   3239       Tcl_AppendResult(interp, "wrong # args: should be \"", 
  3232   3240          Tcl_GetString(objv[0]), " filename", 0);
  3233   3241       return TCL_ERROR;
  3234   3242     }
  3235   3243   
  3236   3244     rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy);
  3237   3245     if( rc!=SQLITE_OK ){
  3238         -    Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3246  +    Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3239   3247       return TCL_ERROR;
  3240   3248     }
  3241   3249     sqlite3TestMakePointerStr(interp, zBuf, pFile);
  3242   3250     Tcl_SetResult(interp, zBuf, 0);
  3243   3251     return TCL_ERROR;
  3244   3252   }
  3245   3253   
................................................................................
  3262   3270     }
  3263   3271   
  3264   3272     if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
  3265   3273       return TCL_ERROR;
  3266   3274     }
  3267   3275     rc = sqlite3OsClose(&pFile);
  3268   3276     if( rc!=SQLITE_OK ){
  3269         -    Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3277  +    Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3270   3278       return TCL_ERROR;
  3271   3279     }
  3272   3280     return TCL_OK;
  3273   3281   }
  3274   3282   
  3275   3283   /*
  3276   3284   ** Usage:  sqlite3OsLock <file handle> <locktype>
................................................................................
  3310   3318       Tcl_AppendResult(interp, "wrong # args: should be \"", 
  3311   3319           Tcl_GetString(objv[0]), 
  3312   3320           " filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
  3313   3321       return TCL_ERROR;
  3314   3322     }
  3315   3323   
  3316   3324     if( rc!=SQLITE_OK ){
  3317         -    Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3325  +    Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3318   3326       return TCL_ERROR;
  3319   3327     }
  3320   3328     return TCL_OK;
  3321   3329   }
  3322   3330   
  3323   3331   /*
  3324   3332   ** Usage:  sqlite3OsUnlock <file handle>
................................................................................
  3339   3347     }
  3340   3348   
  3341   3349     if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
  3342   3350       return TCL_ERROR;
  3343   3351     }
  3344   3352     rc = sqlite3OsUnlock(pFile, NO_LOCK);
  3345   3353     if( rc!=SQLITE_OK ){
  3346         -    Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3354  +    Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3347   3355       return TCL_ERROR;
  3348   3356     }
  3349   3357     return TCL_OK;
  3350   3358   }
  3351   3359   
  3352   3360   /*
  3353   3361   ** Usage:  sqlite3OsTempFileName
................................................................................
  3359   3367     Tcl_Obj *CONST objv[]
  3360   3368   ){
  3361   3369     char zFile[SQLITE_TEMPNAME_SIZE];
  3362   3370     int rc;
  3363   3371   
  3364   3372     rc = sqlite3OsTempFileName(zFile);
  3365   3373     if( rc!=SQLITE_OK ){
  3366         -    Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3374  +    Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3367   3375       return TCL_ERROR;
  3368   3376     }
  3369   3377     Tcl_AppendResult(interp, zFile, 0);
  3370   3378     return TCL_OK;
  3371   3379   }
  3372   3380   #endif
  3373   3381   
................................................................................
  3488   3496     if( argc!=3 ){
  3489   3497       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  3490   3498           " DB function-name", 0);
  3491   3499       return TCL_ERROR;
  3492   3500     }
  3493   3501     if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  3494   3502     rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
  3495         -  Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3503  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3496   3504     return TCL_OK;
  3497   3505   }
  3498   3506   
  3499   3507   /*
  3500   3508   ** Usage: sqlite_delete_collation DB collation-name
  3501   3509   **
  3502   3510   ** Delete the collation sequence 'collation-name' from database handle 
................................................................................
  3514   3522     if( argc!=3 ){
  3515   3523       Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], 
  3516   3524           " DB function-name", 0);
  3517   3525       return TCL_ERROR;
  3518   3526     }
  3519   3527     if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  3520   3528     rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
  3521         -  Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
         3529  +  Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
  3522   3530     return TCL_OK;
  3523   3531   }
  3524   3532   
  3525   3533   /*
  3526   3534   ** Usage: sqlite3_get_autocommit DB
  3527   3535   **
  3528   3536   ** Return true if the database DB is currently in auto-commit mode.
................................................................................
  4216   4224     static int bitmask_size = sizeof(Bitmask)*8;
  4217   4225     int i;
  4218   4226     extern int sqlite3_os_trace;
  4219   4227     extern int sqlite3_where_trace;
  4220   4228     extern int sqlite3_sync_count, sqlite3_fullsync_count;
  4221   4229     extern int sqlite3_opentemp_count;
  4222   4230     extern int sqlite3_memUsed;
  4223         -  extern int sqlite3_malloc_id;
         4231  +  extern char *sqlite3_malloc_id;
  4224   4232     extern int sqlite3_memMax;
  4225   4233     extern int sqlite3_like_count;
  4226   4234     extern int sqlite3_tsd_count;
  4227   4235     extern int sqlite3_xferopt_count;
  4228   4236   #if OS_UNIX && defined(SQLITE_TEST) && defined(THREADSAFE) && THREADSAFE
  4229   4237     extern int threadsOverrideEachOthersLocks;
  4230   4238   #endif

Changes to src/test3.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Code for testing the btree.c module in SQLite.  This code
    13     13   ** is not included in the SQLite library.  It is used for automated
    14     14   ** testing of the SQLite library.
    15     15   **
    16         -** $Id: test3.c,v 1.71 2007/03/19 17:44:28 danielk1977 Exp $
           16  +** $Id: test3.c,v 1.72 2007/03/26 22:05:02 drh Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "pager.h"
    20     20   #include "btree.h"
    21     21   #include "tcl.h"
    22     22   #include <stdlib.h>
    23     23   #include <string.h>
................................................................................
  1225   1225     Tcl_AppendResult(interp, &zBuf[1], 0);
  1226   1226     return SQLITE_OK;
  1227   1227   }
  1228   1228   
  1229   1229   /*
  1230   1230   ** Copied from btree.c:
  1231   1231   */
  1232         -static u32 get4byte(unsigned char *p){
         1232  +static u32 t4Get4byte(unsigned char *p){
  1233   1233     return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
  1234   1234   }
  1235   1235   
  1236   1236   /*
  1237   1237   **   btree_ovfl_info  BTREE  CURSOR
  1238   1238   **
  1239   1239   ** Given a cursor, return the sequence of pages number that form the
................................................................................
  1287   1287       Tcl_DStringAppendElement(&str, zElem);
  1288   1288       if( sqlite3PagerGet(pPager, pgno, &pDbPage)!=SQLITE_OK ){
  1289   1289         Tcl_DStringFree(&str);
  1290   1290         Tcl_AppendResult(interp, "unable to get page ", zElem, 0);
  1291   1291         return TCL_ERROR;
  1292   1292       }
  1293   1293       pPage = sqlite3PagerGetData(pDbPage);
  1294         -    pgno = get4byte((unsigned char*)pPage);
         1294  +    pgno = t4Get4byte((unsigned char*)pPage);
  1295   1295       sqlite3PagerUnref(pDbPage);
  1296   1296     }
  1297   1297     Tcl_DStringResult(interp, &str);
  1298   1298     return SQLITE_OK;
  1299   1299   }
  1300   1300   
  1301   1301   /*

Changes to src/test_async.c.

   102    102   
   103    103   /* Forward references */
   104    104   typedef struct AsyncWrite AsyncWrite;
   105    105   typedef struct AsyncFile AsyncFile;
   106    106   
   107    107   /* Enable for debugging */
   108    108   static int sqlite3async_trace = 0;
   109         -# define TRACE(X) if( sqlite3async_trace ) asyncTrace X
          109  +# define ASYNC_TRACE(X) if( sqlite3async_trace ) asyncTrace X
   110    110   static void asyncTrace(const char *zFormat, ...){
   111    111     char *z;
   112    112     va_list ap;
   113    113     va_start(ap, zFormat);
   114    114     z = sqlite3_vmprintf(zFormat, ap);
   115    115     va_end(ap);
   116    116     fprintf(stderr, "[%d] %s", (int)pthread_self(), z);
................................................................................
   360    360     if( async.pQueueLast ){
   361    361       assert( async.pQueueFirst );
   362    362       async.pQueueLast->pNext = pWrite;
   363    363     }else{
   364    364       async.pQueueFirst = pWrite;
   365    365     }
   366    366     async.pQueueLast = pWrite;
   367         -  TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op],
          367  +  ASYNC_TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op],
   368    368            pWrite->pFile ? pWrite->pFile->zName : "-", pWrite->iOffset));
   369    369   
   370    370     if( pWrite->op==ASYNC_CLOSE ){
   371    371       async.nFile--;
   372    372       if( async.nFile==0 ){
   373    373         async.ioError = SQLITE_OK;
   374    374       }
................................................................................
   516    516       rc = sqlite3OsSeek(pBase, pFile->iOffset);
   517    517       if( rc!=SQLITE_OK ){
   518    518         goto asyncread_out;
   519    519       }
   520    520       nRead = MIN(filesize - pFile->iOffset, amt);
   521    521       if( nRead>0 ){
   522    522         rc = sqlite3OsRead(pBase, obuf, nRead);
   523         -      TRACE(("READ %s %d bytes at %d\n", pFile->zName, nRead, pFile->iOffset));
          523  +      ASYNC_TRACE(("READ %s %d bytes at %d\n", pFile->zName, nRead, pFile->iOffset));
   524    524       }
   525    525     }
   526    526   
   527    527     if( rc==SQLITE_OK ){
   528    528       AsyncWrite *p;
   529    529       i64 iOffset = pFile->iOffset;           /* Current seek offset */
   530    530   
................................................................................
   536    536   
   537    537           if( iBeginIn<0 ) iBeginIn = 0;
   538    538           if( iBeginOut<0 ) iBeginOut = 0;
   539    539           nCopy = MIN(p->nByte-iBeginIn, amt-iBeginOut);
   540    540   
   541    541           if( nCopy>0 ){
   542    542             memcpy(&((char *)obuf)[iBeginOut], &p->zBuf[iBeginIn], nCopy);
   543         -          TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset));
          543  +          ASYNC_TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset));
   544    544           }
   545    545         }
   546    546       }
   547    547   
   548    548       pFile->iOffset += (i64)amt;
   549    549     }
   550    550   
................................................................................
   622    622   ** as with standard (unmodified) SQLite as long as all connections 
   623    623   ** come from this one process.  Connections from external processes
   624    624   ** cannot see our internal hash table (obviously) and will thus not
   625    625   ** honor our locks.
   626    626   */
   627    627   static int asyncLock(OsFile *id, int lockType){
   628    628     AsyncFile *pFile = (AsyncFile*)id;
   629         -  TRACE(("LOCK %d (%s)\n", lockType, pFile->zName));
          629  +  ASYNC_TRACE(("LOCK %d (%s)\n", lockType, pFile->zName));
   630    630     pthread_mutex_lock(&async.lockMutex);
   631    631     sqlite3HashInsert(&async.aLock, pFile->zName, pFile->nName, (void*)lockType);
   632    632     pthread_mutex_unlock(&async.lockMutex);
   633    633     return SQLITE_OK;
   634    634   }
   635    635   static int asyncUnlock(OsFile *id, int lockType){
   636    636     return asyncLock(id, lockType);
................................................................................
   642    642   */
   643    643   static int asyncCheckReservedLock(OsFile *id){
   644    644     AsyncFile *pFile = (AsyncFile*)id;
   645    645     int rc;
   646    646     pthread_mutex_lock(&async.lockMutex);
   647    647     rc = (int)sqlite3HashFind(&async.aLock, pFile->zName, pFile->nName);
   648    648     pthread_mutex_unlock(&async.lockMutex);
   649         -  TRACE(("CHECK-LOCK %d (%s)\n", rc, pFile->zName));
          649  +  ASYNC_TRACE(("CHECK-LOCK %d (%s)\n", rc, pFile->zName));
   650    650     return rc>SHARED_LOCK;
   651    651   }
   652    652   
   653    653   static int asyncSectorSize(OsFile *id){
   654    654     /* TODO: This is tricky to implement, as this backend might not have
   655    655     ** an open file handle at this point.
   656    656     */
................................................................................
   822    822       if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, z) ){
   823    823         ret = 0;
   824    824       }else if( p->op==ASYNC_OPENEXCLUSIVE && 0==strcmp(p->zBuf, z) ){
   825    825         ret = 1;
   826    826       }
   827    827     }
   828    828   
   829         -  TRACE(("EXISTS: %s = %d\n", z, ret));
          829  +  ASYNC_TRACE(("EXISTS: %s = %d\n", z, ret));
   830    830     pthread_mutex_unlock(&async.queueMutex);
   831    831     return ret;
   832    832   }
   833    833   
   834    834   /*
   835    835   ** Call this routine to enable or disable the
   836    836   ** asynchronous IO features implemented in this file. 
................................................................................
   912    912       }
   913    913       while( (p = async.pQueueFirst)==0 ){
   914    914         pthread_cond_broadcast(&async.emptySignal);
   915    915         if( async.writerHaltWhenIdle ){
   916    916           pthread_mutex_unlock(&async.queueMutex);
   917    917           break;
   918    918         }else{
   919         -        TRACE(("IDLE\n"));
          919  +        ASYNC_TRACE(("IDLE\n"));
   920    920           pthread_cond_wait(&async.queueSignal, &async.queueMutex);
   921         -        TRACE(("WAKEUP\n"));
          921  +        ASYNC_TRACE(("WAKEUP\n"));
   922    922         }
   923    923       }
   924    924       if( p==0 ) break;
   925    925       holdingMutex = 1;
   926    926   
   927    927       /* Right now this thread is holding the mutex on the write-op queue.
   928    928       ** Variable 'p' points to the first entry in the write-op queue. In
................................................................................
   963    963   
   964    964       switch( p->op ){
   965    965         case ASYNC_NOOP:
   966    966           break;
   967    967   
   968    968         case ASYNC_WRITE:
   969    969           assert( pBase );
   970         -        TRACE(("WRITE %s %d bytes at %d\n",
          970  +        ASYNC_TRACE(("WRITE %s %d bytes at %d\n",
   971    971                   p->pFile->zName, p->nByte, p->iOffset));
   972    972           rc = sqlite3OsSeek(pBase, p->iOffset);
   973    973           if( rc==SQLITE_OK ){
   974    974             rc = sqlite3OsWrite(pBase, (const void *)(p->zBuf), p->nByte);
   975    975           }
   976    976           break;
   977    977   
   978    978         case ASYNC_SYNC:
   979    979           assert( pBase );
   980         -        TRACE(("SYNC %s\n", p->pFile->zName));
          980  +        ASYNC_TRACE(("SYNC %s\n", p->pFile->zName));
   981    981           rc = sqlite3OsSync(pBase, p->nByte);
   982    982           break;
   983    983   
   984    984         case ASYNC_TRUNCATE:
   985    985           assert( pBase );
   986         -        TRACE(("TRUNCATE %s to %d bytes\n", p->pFile->zName, p->iOffset));
          986  +        ASYNC_TRACE(("TRUNCATE %s to %d bytes\n", p->pFile->zName, p->iOffset));
   987    987           rc = sqlite3OsTruncate(pBase, p->iOffset);
   988    988           break;
   989    989   
   990    990         case ASYNC_CLOSE:
   991         -        TRACE(("CLOSE %s\n", p->pFile->zName));
          991  +        ASYNC_TRACE(("CLOSE %s\n", p->pFile->zName));
   992    992           sqlite3OsClose(&p->pFile->pBaseWrite);
   993    993           sqlite3OsClose(&p->pFile->pBaseRead);
   994    994           sqlite3OsFree(p->pFile);
   995    995           break;
   996    996   
   997    997         case ASYNC_OPENDIRECTORY:
   998    998           assert( pBase );
   999         -        TRACE(("OPENDIR %s\n", p->zBuf));
          999  +        ASYNC_TRACE(("OPENDIR %s\n", p->zBuf));
  1000   1000           sqlite3OsOpenDirectory(pBase, p->zBuf);
  1001   1001           break;
  1002   1002   
  1003   1003         case ASYNC_SETFULLSYNC:
  1004   1004           assert( pBase );
  1005         -        TRACE(("SETFULLSYNC %s %d\n", p->pFile->zName, p->nByte));
         1005  +        ASYNC_TRACE(("SETFULLSYNC %s %d\n", p->pFile->zName, p->nByte));
  1006   1006           sqlite3OsSetFullSync(pBase, p->nByte);
  1007   1007           break;
  1008   1008   
  1009   1009         case ASYNC_DELETE:
  1010         -        TRACE(("DELETE %s\n", p->zBuf));
         1010  +        ASYNC_TRACE(("DELETE %s\n", p->zBuf));
  1011   1011           rc = xOrigDelete(p->zBuf);
  1012   1012           break;
  1013   1013   
  1014   1014         case ASYNC_SYNCDIRECTORY:
  1015         -        TRACE(("SYNCDIR %s\n", p->zBuf));
         1015  +        ASYNC_TRACE(("SYNCDIR %s\n", p->zBuf));
  1016   1016           rc = xOrigSyncDirectory(p->zBuf);
  1017   1017           break;
  1018   1018   
  1019   1019         case ASYNC_OPENEXCLUSIVE: {
  1020   1020           AsyncFile *pFile = p->pFile;
  1021   1021           int delFlag = ((p->iOffset)?1:0);
  1022   1022           OsFile *pBase = 0;
  1023         -        TRACE(("OPEN %s delFlag=%d\n", p->zBuf, delFlag));
         1023  +        ASYNC_TRACE(("OPEN %s delFlag=%d\n", p->zBuf, delFlag));
  1024   1024           assert(pFile->pBaseRead==0 && pFile->pBaseWrite==0);
  1025   1025           rc = xOrigOpenExclusive(p->zBuf, &pBase, delFlag);
  1026   1026           assert( holdingMutex==0 );
  1027   1027           pthread_mutex_lock(&async.queueMutex);
  1028   1028           holdingMutex = 1;
  1029   1029           if( rc==SQLITE_OK ){
  1030   1030             pFile->pBaseRead = pBase;
................................................................................
  1039   1039       ** so that the AsyncWrite structure can be safely removed from the 
  1040   1040       ** global write-op queue.
  1041   1041       */
  1042   1042       if( !holdingMutex ){
  1043   1043         pthread_mutex_lock(&async.queueMutex);
  1044   1044         holdingMutex = 1;
  1045   1045       }
  1046         -    /* TRACE(("UNLINK %p\n", p)); */
         1046  +    /* ASYNC_TRACE(("UNLINK %p\n", p)); */
  1047   1047       if( p==async.pQueueLast ){
  1048   1048         async.pQueueLast = 0;
  1049   1049       }
  1050   1050       async.pQueueFirst = p->pNext;
  1051   1051       sqlite3OsFree(p);
  1052   1052       assert( holdingMutex );
  1053   1053   
................................................................................
  1232   1232     }
  1233   1233   
  1234   1234     while( cnt-- && !pthread_mutex_trylock(&async.writerMutex) ){
  1235   1235       pthread_mutex_unlock(&async.writerMutex);
  1236   1236       sched_yield();
  1237   1237     }
  1238   1238     if( cnt>=0 ){
  1239         -    TRACE(("WAIT\n"));
         1239  +    ASYNC_TRACE(("WAIT\n"));
  1240   1240       pthread_mutex_lock(&async.queueMutex);
  1241   1241       pthread_cond_broadcast(&async.queueSignal);
  1242   1242       pthread_mutex_unlock(&async.queueMutex);
  1243   1243       pthread_mutex_lock(&async.writerMutex);
  1244   1244       pthread_mutex_unlock(&async.writerMutex);
  1245   1245     }else{
  1246         -    TRACE(("NO-WAIT\n"));
         1246  +    ASYNC_TRACE(("NO-WAIT\n"));
  1247   1247     }
  1248   1248     return TCL_OK;
  1249   1249   }
  1250   1250   
  1251   1251   
  1252   1252   #endif  /* OS_UNIX and THREADSAFE and defined(SQLITE_ENABLE_REDEF_IO) */
  1253   1253   

Changes to 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.195 2007/03/19 17:44:28 danielk1977 Exp $
           17  +** $Id: util.c,v 1.196 2007/03/26 22:05:02 drh 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   /*
................................................................................
   657    657   void *sqlite3Malloc(int n, int doMemManage){
   658    658     void *p = sqlite3MallocRaw(n, doMemManage);
   659    659     if( p ){
   660    660       memset(p, 0, n);
   661    661     }
   662    662     return p;
   663    663   }
   664         -void sqlite3ReallocOrFree(void **pp, int n){
   665         -  void *p = sqlite3Realloc(*pp, n);
          664  +void sqlite3ReallocOrFree(void *pp, int n){
          665  +  char **x = (char**)pp;
          666  +  void *p = sqlite3Realloc(*x, n);
   666    667     if( !p ){
   667         -    sqlite3FreeX(*pp);
          668  +    sqlite3FreeX(*x);
   668    669     }
   669         -  *pp = p;
          670  +  *x = p;
   670    671   }
   671    672   
   672    673   /*
   673    674   ** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those
   674    675   ** rare scenarios where sqlite may allocate memory in one thread and free
   675    676   ** it in another. They are exactly the same as sqlite3Malloc() and 
   676    677   ** sqlite3Free() except that:
................................................................................
  1380   1381     }
  1381   1382     v = 0;
  1382   1383     while( *z ){
  1383   1384       v = (v<<4) + hexToInt(*z);
  1384   1385       z++;
  1385   1386     }
  1386   1387     if( sizeof(p)==sizeof(v) ){
  1387         -    p = *(void**)&v;
         1388  +    memcpy(&p, &v, sizeof(p));
  1388   1389     }else{
  1389   1390       assert( sizeof(p)==sizeof(v2) );
  1390   1391       v2 = (u32)v;
  1391         -    p = *(void**)&v2;
         1392  +    memcpy(&p, &v2, sizeof(p));
  1392   1393     }
  1393   1394     return p;
  1394   1395   }
  1395   1396   #endif
  1396   1397   
  1397   1398   /*
  1398   1399   ** Return a pointer to the ThreadData associated with the calling thread.

Changes to 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.591 2007/03/15 12:05:36 danielk1977 Exp $
           46  +** $Id: vdbe.c,v 1.592 2007/03/26 22:05:02 drh 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   /*
................................................................................
  4232   4232     int i = p->contextStackTop++;
  4233   4233     Context *pContext;
  4234   4234   
  4235   4235     assert( i>=0 );
  4236   4236     /* FIX ME: This should be allocated as part of the vdbe at compile-time */
  4237   4237     if( i>=p->contextStackDepth ){
  4238   4238       p->contextStackDepth = i+1;
  4239         -    sqliteReallocOrFree((void**)&p->contextStack, sizeof(Context)*(i+1));
         4239  +    sqliteReallocOrFree(&p->contextStack, sizeof(Context)*(i+1));
  4240   4240       if( p->contextStack==0 ) goto no_mem;
  4241   4241     }
  4242   4242     pContext = &p->contextStack[i];
  4243   4243     pContext->lastRowid = db->lastRowid;
  4244   4244     pContext->nChange = p->nChange;
  4245   4245     pContext->sFifo = p->sFifo;
  4246   4246     sqlite3VdbeFifoInit(&p->sFifo);

Changes to src/vdbeInt.h.

    27     27   
    28     28   /*
    29     29   ** The makefile scans the vdbe.c source file and creates the following
    30     30   ** array of string constants which are the names of all VDBE opcodes.  This
    31     31   ** array is defined in a separate source code file named opcode.c which is
    32     32   ** automatically generated by the makefile.
    33     33   */
    34         -extern char *sqlite3OpcodeNames[];
           34  +extern const char *const sqlite3OpcodeNames[];
    35     35   
    36     36   /*
    37     37   ** SQL is translated into a sequence of instructions to be
    38     38   ** executed by a virtual machine.  Each instruction is an instance
    39     39   ** of the following structure.
    40     40   */
    41     41   typedef struct VdbeOp Op;
................................................................................
   121    121   **
   122    122   ** Each value has a manifest type. The manifest type of the value stored
   123    123   ** in a Mem struct is returned by the MemType(Mem*) macro. The type is
   124    124   ** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or
   125    125   ** SQLITE_BLOB.
   126    126   */
   127    127   struct Mem {
   128         -  i64 i;              /* Integer value. Or FuncDef* when flags==MEM_Agg */
          128  +  union {
          129  +    i64 i;              /* Integer value. Or FuncDef* when flags==MEM_Agg */
          130  +    FuncDef *pDef;
          131  +  };
   129    132     double r;           /* Real value */
   130    133     char *z;            /* String or BLOB value */
   131    134     int n;              /* Number of characters in string value, including '\0' */
   132    135     u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   133    136     u8  type;           /* One of MEM_Null, MEM_Str, etc. */
   134    137     u8  enc;            /* TEXT_Utf8, TEXT_Utf16le, or TEXT_Utf16be */
   135    138     void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */

Changes to src/vdbeapi.c.

   337    337     if( (pMem->flags & MEM_Agg)==0 ){
   338    338       if( nByte==0 ){
   339    339         assert( pMem->flags==MEM_Null );
   340    340         pMem->z = 0;
   341    341       }else{
   342    342         pMem->flags = MEM_Agg;
   343    343         pMem->xDel = sqlite3FreeX;
   344         -      *(FuncDef**)&pMem->i = p->pFunc;
          344  +      pMem->pDef = p->pFunc;
   345    345         if( nByte<=NBFS ){
   346    346           pMem->z = pMem->zShort;
   347    347           memset(pMem->z, 0, nByte);
   348    348         }else{
   349    349           pMem->z = sqliteMalloc( nByte );
   350    350         }
   351    351       }
................................................................................
   439    439   ** If iCol is not valid, return a pointer to a Mem which has a value
   440    440   ** of NULL.
   441    441   */
   442    442   static Mem *columnMem(sqlite3_stmt *pStmt, int i){
   443    443     Vdbe *pVm = (Vdbe *)pStmt;
   444    444     int vals = sqlite3_data_count(pStmt);
   445    445     if( i>=vals || i<0 ){
   446         -    static const Mem nullMem = {0, 0.0, "", 0, MEM_Null, MEM_Null };
          446  +    static const Mem nullMem = {{0,}, 0.0, "", 0, MEM_Null, MEM_Null };
   447    447       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
   448    448       return (Mem*)&nullMem;
   449    449     }
   450    450     return &pVm->pTos[(1-vals)+i];
   451    451   }
   452    452   
   453    453   /*

Changes to src/vdbeaux.c.

   191    191   */
   192    192   int sqlite3VdbeMakeLabel(Vdbe *p){
   193    193     int i;
   194    194     i = p->nLabel++;
   195    195     assert( p->magic==VDBE_MAGIC_INIT );
   196    196     if( i>=p->nLabelAlloc ){
   197    197       p->nLabelAlloc = p->nLabelAlloc*2 + 10;
   198         -    sqliteReallocOrFree((void**)&p->aLabel,
   199         -                          p->nLabelAlloc*sizeof(p->aLabel[0]));
          198  +    sqliteReallocOrFree(&p->aLabel, p->nLabelAlloc*sizeof(p->aLabel[0]));
   200    199     }
   201    200     if( p->aLabel ){
   202    201       p->aLabel[i] = -1;
   203    202     }
   204    203     return -1-i;
   205    204   }
   206    205   
................................................................................
   719    718       Mem *pMem = p->aStack;
   720    719       pMem->flags = MEM_Int;
   721    720       pMem->type = SQLITE_INTEGER;
   722    721       pMem->i = i;                                /* Program counter */
   723    722       pMem++;
   724    723   
   725    724       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
   726         -    pMem->z = sqlite3OpcodeNames[pOp->opcode];  /* Opcode */
          725  +    pMem->z = (char*)sqlite3OpcodeNames[pOp->opcode];  /* Opcode */
   727    726       assert( pMem->z!=0 );
   728    727       pMem->n = strlen(pMem->z);
   729    728       pMem->type = SQLITE_TEXT;
   730    729       pMem->enc = SQLITE_UTF8;
   731    730       pMem++;
   732    731   
   733    732       pMem->flags = MEM_Int;
................................................................................
  1781   1780     int len;
  1782   1781   
  1783   1782     /* Integer and Real */
  1784   1783     if( serial_type<=7 && serial_type>0 ){
  1785   1784       u64 v;
  1786   1785       int i;
  1787   1786       if( serial_type==7 ){
  1788         -      v = *(u64*)&pMem->r;
         1787  +      assert( sizeof(v)==sizeof(pMem->r) );
         1788  +      memcpy(&v, &pMem->r, sizeof(v));
  1789   1789       }else{
  1790         -      v = *(u64*)&pMem->i;
         1790  +      v = pMem->i;
  1791   1791       }
  1792   1792       len = i = sqlite3VdbeSerialTypeLen(serial_type);
  1793   1793       while( i-- ){
  1794   1794         buf[i] = (v&0xFF);
  1795   1795         v >>= 8;
  1796   1796       }
  1797   1797       return len;
................................................................................
  1857   1857         u64 x;
  1858   1858         u32 y;
  1859   1859   #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
  1860   1860         /* Verify that integers and floating point values use the same
  1861   1861         ** byte order.  The byte order differs on some (broken) architectures.
  1862   1862         */
  1863   1863         static const u64 t1 = ((u64)0x3ff00000)<<32;
  1864         -      assert( 1.0==*(double*)&t1 );
         1864  +      static const double r1 = 1.0;
         1865  +      assert( sizeof(r1)==sizeof(t1) && memcmp(&r1, &t1, sizeof(r1))==0 );
  1865   1866   #endif
  1866   1867   
  1867   1868         x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
  1868   1869         y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
  1869   1870         x = (x<<32) | y;
  1870   1871         if( serial_type==6 ){
  1871   1872           pMem->i = *(i64*)&x;
  1872   1873           pMem->flags = MEM_Int;
  1873   1874         }else{
  1874         -        pMem->r = *(double*)&x;
         1875  +        assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
         1876  +        memcpy(&pMem->r, &x, sizeof(x));
         1877  +        /* pMem->r = *(double*)&x; */
  1875   1878           pMem->flags = MEM_Real;
  1876   1879         }
  1877   1880         return 8;
  1878   1881       }
  1879   1882       case 8:    /* Integer 0 */
  1880   1883       case 9: {  /* Integer 1 */
  1881   1884         pMem->i = serial_type-8;

Changes to src/vdbefifo.c.

    15     15   #include "sqliteInt.h"
    16     16   #include "vdbeInt.h"
    17     17   
    18     18   /*
    19     19   ** Allocate a new FifoPage and return a pointer to it.  Return NULL if
    20     20   ** we run out of memory.  Leave space on the page for nEntry entries.
    21     21   */
    22         -static FifoPage *allocatePage(int nEntry){
           22  +static FifoPage *allocateFifoPage(int nEntry){
    23     23     FifoPage *pPage;
    24     24     if( nEntry>32767 ){
    25     25       nEntry = 32767;
    26     26     }
    27     27     pPage = sqliteMallocRaw( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) );
    28     28     if( pPage ){
    29     29       pPage->nSlot = nEntry;
................................................................................
    46     46   ** normally.   SQLITE_NOMEM is returned if we are unable to allocate
    47     47   ** memory.
    48     48   */
    49     49   int sqlite3VdbeFifoPush(Fifo *pFifo, i64 val){
    50     50     FifoPage *pPage;
    51     51     pPage = pFifo->pLast;
    52     52     if( pPage==0 ){
    53         -    pPage = pFifo->pLast = pFifo->pFirst = allocatePage(20);
           53  +    pPage = pFifo->pLast = pFifo->pFirst = allocateFifoPage(20);
    54     54       if( pPage==0 ){
    55     55         return SQLITE_NOMEM;
    56     56       }
    57     57     }else if( pPage->iWrite>=pPage->nSlot ){
    58         -    pPage->pNext = allocatePage(pFifo->nEntry);
           58  +    pPage->pNext = allocateFifoPage(pFifo->nEntry);
    59     59       if( pPage->pNext==0 ){
    60     60         return SQLITE_NOMEM;
    61     61       }
    62     62       pPage = pFifo->pLast = pPage->pNext;
    63     63     }
    64     64     pPage->aSlot[pPage->iWrite++] = val;
    65     65     pFifo->nEntry++;

Changes to src/vdbemem.c.

   191    191   ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
   192    192   ** otherwise.
   193    193   */
   194    194   int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
   195    195     int rc = SQLITE_OK;
   196    196     if( pFunc && pFunc->xFinalize ){
   197    197       sqlite3_context ctx;
   198         -    assert( (pMem->flags & MEM_Null)!=0 || pFunc==*(FuncDef**)&pMem->i );
          198  +    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->pDef );
   199    199       ctx.s.flags = MEM_Null;
   200    200       ctx.s.z = pMem->zShort;
   201    201       ctx.pMem = pMem;
   202    202       ctx.pFunc = pFunc;
   203    203       ctx.isError = 0;
   204    204       pFunc->xFinalize(&ctx);
   205    205       if( pMem->z && pMem->z!=pMem->zShort ){
................................................................................
   221    221   ** inconsistent state, for example with (Mem.z==0) and
   222    222   ** (Mem.type==SQLITE_TEXT).
   223    223   */
   224    224   void sqlite3VdbeMemRelease(Mem *p){
   225    225     if( p->flags & (MEM_Dyn|MEM_Agg) ){
   226    226       if( p->xDel ){
   227    227         if( p->flags & MEM_Agg ){
   228         -        sqlite3VdbeMemFinalize(p, *(FuncDef**)&p->i);
          228  +        sqlite3VdbeMemFinalize(p, p->pDef);
   229    229           assert( (p->flags & MEM_Agg)==0 );
   230    230           sqlite3VdbeMemRelease(p);
   231    231         }else{
   232    232           p->xDel((void *)p->z);
   233    233         }
   234    234       }else{
   235    235         sqliteFree(p->z);

Changes to 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.239 2007/03/02 08:12:22 danielk1977 Exp $
           19  +** $Id: where.c,v 1.240 2007/03/26 22:05:02 drh 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)
................................................................................
    31     31   #define ARRAYSIZE(X)  (sizeof(X)/sizeof(X[0]))
    32     32   
    33     33   /*
    34     34   ** Trace output macros
    35     35   */
    36     36   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    37     37   int sqlite3_where_trace = 0;
    38         -# define TRACE(X)  if(sqlite3_where_trace) sqlite3DebugPrintf X
           38  +# define WHERETRACE(X)  if(sqlite3_where_trace) sqlite3DebugPrintf X
    39     39   #else
    40         -# define TRACE(X)
           40  +# define WHERETRACE(X)
    41     41   #endif
    42     42   
    43     43   /* Forward reference
    44     44   */
    45     45   typedef struct WhereClause WhereClause;
    46     46   typedef struct ExprMaskSet ExprMaskSet;
    47     47   
................................................................................
  1205   1205     ** allocated and initialized for this virtual table, then allocate
  1206   1206     ** and initialize it now
  1207   1207     */
  1208   1208     pIdxInfo = *ppIdxInfo;
  1209   1209     if( pIdxInfo==0 ){
  1210   1210       WhereTerm *pTerm;
  1211   1211       int nTerm;
  1212         -    TRACE(("Recomputing index info for %s...\n", pTab->zName));
         1212  +    WHERETRACE(("Recomputing index info for %s...\n", pTab->zName));
  1213   1213   
  1214   1214       /* Count the number of possible WHERE clause constraints referring
  1215   1215       ** to this virtual table */
  1216   1216       for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  1217   1217         if( pTerm->leftCursor != pSrc->iCursor ) continue;
  1218   1218         if( pTerm->eOperator==WO_IN ) continue;
  1219   1219         nTerm++;
................................................................................
  1338   1338     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / 2.0;
  1339   1339     nOrderBy = pIdxInfo->nOrderBy;
  1340   1340     if( pIdxInfo->nOrderBy && !orderByUsable ){
  1341   1341       *(int*)&pIdxInfo->nOrderBy = 0;
  1342   1342     }
  1343   1343   
  1344   1344     sqlite3SafetyOff(pParse->db);
  1345         -  TRACE(("xBestIndex for %s\n", pTab->zName));
         1345  +  WHERETRACE(("xBestIndex for %s\n", pTab->zName));
  1346   1346     TRACE_IDX_INPUTS(pIdxInfo);
  1347   1347     rc = pTab->pVtab->pModule->xBestIndex(pTab->pVtab, pIdxInfo);
  1348   1348     TRACE_IDX_OUTPUTS(pIdxInfo);
  1349   1349     if( rc!=SQLITE_OK ){
  1350   1350       if( rc==SQLITE_NOMEM ){
  1351   1351         sqlite3FailedMalloc();
  1352   1352       }else {
................................................................................
  1399   1399     Index *pProbe;              /* An index we are evaluating */
  1400   1400     int rev;                    /* True to scan in reverse order */
  1401   1401     int flags;                  /* Flags associated with pProbe */
  1402   1402     int nEq;                    /* Number of == or IN constraints */
  1403   1403     int eqTermMask;             /* Mask of valid equality operators */
  1404   1404     double cost;                /* Cost of using pProbe */
  1405   1405   
  1406         -  TRACE(("bestIndex: tbl=%s notReady=%x\n", pSrc->pTab->zName, notReady));
         1406  +  WHERETRACE(("bestIndex: tbl=%s notReady=%x\n", pSrc->pTab->zName, notReady));
  1407   1407     lowestCost = SQLITE_BIG_DBL;
  1408   1408     pProbe = pSrc->pTab->pIndex;
  1409   1409   
  1410   1410     /* If the table has no indices and there are no terms in the where
  1411   1411     ** clause that refer to the ROWID, then we will never be able to do
  1412   1412     ** anything other than a full table scan on this table.  We might as
  1413   1413     ** well put it first in the join order.  That way, perhaps it can be
................................................................................
  1430   1430       *ppIndex = 0;
  1431   1431       bestFlags = WHERE_ROWID_EQ;
  1432   1432       if( pTerm->eOperator & WO_EQ ){
  1433   1433         /* Rowid== is always the best pick.  Look no further.  Because only
  1434   1434         ** a single row is generated, output is always in sorted order */
  1435   1435         *pFlags = WHERE_ROWID_EQ | WHERE_UNIQUE;
  1436   1436         *pnEq = 1;
  1437         -      TRACE(("... best is rowid\n"));
         1437  +      WHERETRACE(("... best is rowid\n"));
  1438   1438         return 0.0;
  1439   1439       }else if( (pExpr = pTerm->pExpr)->pList!=0 ){
  1440   1440         /* Rowid IN (LIST): cost is NlogN where N is the number of list
  1441   1441         ** elements.  */
  1442   1442         lowestCost = pExpr->pList->nExpr;
  1443   1443         lowestCost *= estLog(lowestCost);
  1444   1444       }else{
  1445   1445         /* Rowid IN (SELECT): cost is NlogN where N is the number of rows
  1446   1446         ** in the result of the inner select.  We have no way to estimate
  1447   1447         ** that value so make a wild guess. */
  1448   1448         lowestCost = 200;
  1449   1449       }
  1450         -    TRACE(("... rowid IN cost: %.9g\n", lowestCost));
         1450  +    WHERETRACE(("... rowid IN cost: %.9g\n", lowestCost));
  1451   1451     }
  1452   1452   
  1453   1453     /* Estimate the cost of a table scan.  If we do not know how many
  1454   1454     ** entries are in the table, use 1 million as a guess.
  1455   1455     */
  1456   1456     cost = pProbe ? pProbe->aiRowEst[0] : 1000000;
  1457         -  TRACE(("... table scan base cost: %.9g\n", cost));
         1457  +  WHERETRACE(("... table scan base cost: %.9g\n", cost));
  1458   1458     flags = WHERE_ROWID_RANGE;
  1459   1459   
  1460   1460     /* Check for constraints on a range of rowids in a table scan.
  1461   1461     */
  1462   1462     pTerm = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE|WO_GT|WO_GE, 0);
  1463   1463     if( pTerm ){
  1464   1464       if( findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0) ){
................................................................................
  1465   1465         flags |= WHERE_TOP_LIMIT;
  1466   1466         cost /= 3;  /* Guess that rowid<EXPR eliminates two-thirds or rows */
  1467   1467       }
  1468   1468       if( findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0) ){
  1469   1469         flags |= WHERE_BTM_LIMIT;
  1470   1470         cost /= 3;  /* Guess that rowid>EXPR eliminates two-thirds of rows */
  1471   1471       }
  1472         -    TRACE(("... rowid range reduces cost to %.9g\n", cost));
         1472  +    WHERETRACE(("... rowid range reduces cost to %.9g\n", cost));
  1473   1473     }else{
  1474   1474       flags = 0;
  1475   1475     }
  1476   1476   
  1477   1477     /* If the table scan does not satisfy the ORDER BY clause, increase
  1478   1478     ** the cost by NlogN to cover the expense of sorting. */
  1479   1479     if( pOrderBy ){
................................................................................
  1480   1480       if( sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev) ){
  1481   1481         flags |= WHERE_ORDERBY|WHERE_ROWID_RANGE;
  1482   1482         if( rev ){
  1483   1483           flags |= WHERE_REVERSE;
  1484   1484         }
  1485   1485       }else{
  1486   1486         cost += cost*estLog(cost);
  1487         -      TRACE(("... sorting increases cost to %.9g\n", cost));
         1487  +      WHERETRACE(("... sorting increases cost to %.9g\n", cost));
  1488   1488       }
  1489   1489     }
  1490   1490     if( cost<lowestCost ){
  1491   1491       lowestCost = cost;
  1492   1492       bestFlags = flags;
  1493   1493     }
  1494   1494   
................................................................................
  1505   1505   
  1506   1506     /* Look at each index.
  1507   1507     */
  1508   1508     for(; pProbe; pProbe=pProbe->pNext){
  1509   1509       int i;                       /* Loop counter */
  1510   1510       double inMultiplier = 1;
  1511   1511   
  1512         -    TRACE(("... index %s:\n", pProbe->zName));
         1512  +    WHERETRACE(("... index %s:\n", pProbe->zName));
  1513   1513   
  1514   1514       /* Count the number of columns in the index that are satisfied
  1515   1515       ** by x=EXPR constraints or x IN (...) constraints.
  1516   1516       */
  1517   1517       flags = 0;
  1518   1518       for(i=0; i<pProbe->nColumn; i++){
  1519   1519         int j = pProbe->aiColumn[i];
................................................................................
  1532   1532       }
  1533   1533       cost = pProbe->aiRowEst[i] * inMultiplier * estLog(inMultiplier);
  1534   1534       nEq = i;
  1535   1535       if( pProbe->onError!=OE_None && (flags & WHERE_COLUMN_IN)==0
  1536   1536            && nEq==pProbe->nColumn ){
  1537   1537         flags |= WHERE_UNIQUE;
  1538   1538       }
  1539         -    TRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n", nEq, inMultiplier, cost));
         1539  +    WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n", nEq, inMultiplier, cost));
  1540   1540   
  1541   1541       /* Look for range constraints
  1542   1542       */
  1543   1543       if( nEq<pProbe->nColumn ){
  1544   1544         int j = pProbe->aiColumn[nEq];
  1545   1545         pTerm = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pProbe);
  1546   1546         if( pTerm ){
................................................................................
  1549   1549             flags |= WHERE_TOP_LIMIT;
  1550   1550             cost /= 3;
  1551   1551           }
  1552   1552           if( findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pProbe) ){
  1553   1553             flags |= WHERE_BTM_LIMIT;
  1554   1554             cost /= 3;
  1555   1555           }
  1556         -        TRACE(("...... range reduces cost to %.9g\n", cost));
         1556  +        WHERETRACE(("...... range reduces cost to %.9g\n", cost));
  1557   1557         }
  1558   1558       }
  1559   1559   
  1560   1560       /* Add the additional cost of sorting if that is a factor.
  1561   1561       */
  1562   1562       if( pOrderBy ){
  1563   1563         if( (flags & WHERE_COLUMN_IN)==0 &&
................................................................................
  1567   1567           }
  1568   1568           flags |= WHERE_ORDERBY;
  1569   1569           if( rev ){
  1570   1570             flags |= WHERE_REVERSE;
  1571   1571           }
  1572   1572         }else{
  1573   1573           cost += cost*estLog(cost);
  1574         -        TRACE(("...... orderby increases cost to %.9g\n", cost));
         1574  +        WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
  1575   1575         }
  1576   1576       }
  1577   1577   
  1578   1578       /* Check to see if we can get away with using just the index without
  1579   1579       ** ever reading the table.  If that is the case, then halve the
  1580   1580       ** cost of this index.
  1581   1581       */
................................................................................
  1587   1587           if( x<BMS-1 ){
  1588   1588             m &= ~(((Bitmask)1)<<x);
  1589   1589           }
  1590   1590         }
  1591   1591         if( m==0 ){
  1592   1592           flags |= WHERE_IDX_ONLY;
  1593   1593           cost /= 2;
  1594         -        TRACE(("...... idx-only reduces cost to %.9g\n", cost));
         1594  +        WHERETRACE(("...... idx-only reduces cost to %.9g\n", cost));
  1595   1595         }
  1596   1596       }
  1597   1597   
  1598   1598       /* If this index has achieved the lowest cost so far, then use it.
  1599   1599       */
  1600   1600       if( cost < lowestCost ){
  1601   1601         bestIdx = pProbe;
................................................................................
  1605   1605         bestNEq = nEq;
  1606   1606       }
  1607   1607     }
  1608   1608   
  1609   1609     /* Report the best result
  1610   1610     */
  1611   1611     *ppIndex = bestIdx;
  1612         -  TRACE(("best index is %s, cost=%.9g, flags=%x, nEq=%d\n",
         1612  +  WHERETRACE(("best index is %s, cost=%.9g, flags=%x, nEq=%d\n",
  1613   1613           bestIdx ? bestIdx->zName : "(none)", lowestCost, bestFlags, bestNEq));
  1614   1614     *pFlags = bestFlags | eqTermMask;
  1615   1615     *pnEq = bestNEq;
  1616   1616     return lowestCost;
  1617   1617   }
  1618   1618   
  1619   1619   
................................................................................
  1703   1703   
  1704   1704       assert( pX->op==TK_IN );
  1705   1705       sqlite3CodeSubselect(pParse, pX);
  1706   1706       iTab = pX->iTable;
  1707   1707       sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0);
  1708   1708       VdbeComment((v, "# %.*s", pX->span.n, pX->span.z));
  1709   1709       pLevel->nIn++;
  1710         -    sqliteReallocOrFree((void**)&pLevel->aInLoop,
  1711         -                                 sizeof(pLevel->aInLoop[0])*2*pLevel->nIn);
         1710  +    sqliteReallocOrFree(&pLevel->aInLoop,
         1711  +                        sizeof(pLevel->aInLoop[0])*2*pLevel->nIn);
  1712   1712       aIn = pLevel->aInLoop;
  1713   1713       if( aIn ){
  1714   1714         aIn += pLevel->nIn*2 - 2;
  1715   1715         aIn[0] = iTab;
  1716   1716         aIn[1] = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);
  1717   1717       }else{
  1718   1718         pLevel->nIn = 0;
................................................................................
  1999   1999     ** This loop also figures out the nesting order of tables in the FROM
  2000   2000     ** clause.
  2001   2001     */
  2002   2002     notReady = ~(Bitmask)0;
  2003   2003     pTabItem = pTabList->a;
  2004   2004     pLevel = pWInfo->a;
  2005   2005     andFlags = ~0;
  2006         -  TRACE(("*** Optimizer Start ***\n"));
         2006  +  WHERETRACE(("*** Optimizer Start ***\n"));
  2007   2007     for(i=iFrom=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
  2008   2008       Index *pIdx;                /* Index for FROM table at pTabItem */
  2009   2009       int flags;                  /* Flags asssociated with pIdx */
  2010   2010       int nEq;                    /* Number of == or IN constraints */
  2011   2011       double cost;                /* The cost for pIdx */
  2012   2012       int j;                      /* For looping over FROM tables */
  2013   2013       Index *pBest = 0;           /* The best index seen so far */
................................................................................
  2067   2067           bestFlags = flags;
  2068   2068           bestNEq = nEq;
  2069   2069           bestJ = j;
  2070   2070           pLevel->pBestIdx = pIndex;
  2071   2071         }
  2072   2072         if( doNotReorder ) break;
  2073   2073       }
  2074         -    TRACE(("*** Optimizer choose table %d for loop %d\n", bestJ,
         2074  +    WHERETRACE(("*** Optimizer choose table %d for loop %d\n", bestJ,
  2075   2075              pLevel-pWInfo->a));
  2076   2076       if( (bestFlags & WHERE_ORDERBY)!=0 ){
  2077   2077         *ppOrderBy = 0;
  2078   2078       }
  2079   2079       andFlags &= bestFlags;
  2080   2080       pLevel->flags = bestFlags;
  2081   2081       pLevel->pIdx = pBest;
................................................................................
  2086   2086         pLevel->iIdxCur = pParse->nTab++;
  2087   2087       }else{
  2088   2088         pLevel->iIdxCur = -1;
  2089   2089       }
  2090   2090       notReady &= ~getMask(&maskSet, pTabList->a[bestJ].iCursor);
  2091   2091       pLevel->iFrom = bestJ;
  2092   2092     }
  2093         -  TRACE(("*** Optimizer Finished ***\n"));
         2093  +  WHERETRACE(("*** Optimizer Finished ***\n"));
  2094   2094   
  2095   2095     /* If the total query only selects a single row, then the ORDER BY
  2096   2096     ** clause is irrelevant.
  2097   2097     */
  2098   2098     if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
  2099   2099       *ppOrderBy = 0;
  2100   2100     }