/ Check-in [573d92ab]
Login

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

Overview
Comment:Begin adding support for the SQLITE_OMIT_WSD macro. Some (many) WSD variables still need wrappers added to them. (CVS 5652)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 573d92abb9adb1c321ebc2fcadcf14374213b093
User & Date: danielk1977 2008-09-01 18:34:20
Context
2008-09-01
19:14
Modify the aBuiltinFunc array in func.c to use the WSD macros. (CVS 5653) check-in: 846fff5f user: danielk1977 tags: trunk
18:34
Begin adding support for the SQLITE_OMIT_WSD macro. Some (many) WSD variables still need wrappers added to them. (CVS 5652) check-in: 573d92ab user: danielk1977 tags: trunk
17:23
Remove an unused function declaration from a header file. Ticket #3349. (CVS 5651) check-in: 7a577428 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/fts3/fts3_icu.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file implements a tokenizer for fts3 based on the ICU library.
    13     13   ** 
    14         -** $Id: fts3_icu.c,v 1.2 2007/10/24 21:52:37 shess Exp $
           14  +** $Id: fts3_icu.c,v 1.3 2008/09/01 18:34:20 danielk1977 Exp $
    15     15   */
    16     16   
    17     17   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
    18     18   #ifdef SQLITE_ENABLE_ICU
    19     19   
    20     20   #include <assert.h>
    21     21   #include <string.h>
................................................................................
   108    108   
   109    109     UChar32 c;
   110    110     int iInput = 0;
   111    111     int iOut = 0;
   112    112   
   113    113     *ppCursor = 0;
   114    114   
   115         -  if( -1 == nInput ) nInput = strlen(nInput);
          115  +  if( nInput<0 ){
          116  +    nInput = strlen(zInput);
          117  +  }
   116    118     nChar = nInput+1;
   117    119     pCsr = (IcuCursor *)sqlite3_malloc(
   118    120         sizeof(IcuCursor) +                /* IcuCursor */
   119    121         nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
   120    122         (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
   121    123     );
   122    124     if( !pCsr ){

Changes to main.mk.

   220    220     $(TOP)/src/test_mutex.c \
   221    221     $(TOP)/src/test_onefile.c \
   222    222     $(TOP)/src/test_osinst.c \
   223    223     $(TOP)/src/test_schema.c \
   224    224     $(TOP)/src/test_server.c \
   225    225     $(TOP)/src/test_tclvar.c \
   226    226     $(TOP)/src/test_thread.c \
          227  +  $(TOP)/src/test_wsd.c \
   227    228   
   228    229   #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c
   229    230   #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c
   230    231   
   231    232   TESTSRC2 = \
   232    233     $(TOP)/src/attach.c $(TOP)/src/btree.c $(TOP)/src/build.c $(TOP)/src/date.c  \
   233    234     $(TOP)/src/expr.c $(TOP)/src/func.c $(TOP)/src/insert.c $(TOP)/src/os.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.504 2008/08/27 15:16:34 danielk1977 Exp $
           12  +** $Id: btree.c,v 1.505 2008/09/01 18:34:20 danielk1977 Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** See the header comment on "btreeInt.h" for additional information.
    16     16   ** Including a description of file format and an overview of operation.
    17     17   */
    18     18   #include "btreeInt.h"
    19     19   
................................................................................
  1336   1336   #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
  1337   1337       /* Add the new BtShared object to the linked list sharable BtShareds.
  1338   1338       */
  1339   1339       if( p->sharable ){
  1340   1340         sqlite3_mutex *mutexShared;
  1341   1341         pBt->nRef = 1;
  1342   1342         mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  1343         -      if( SQLITE_THREADSAFE && sqlite3Config.bCoreMutex ){
         1343  +      if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
  1344   1344           pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
  1345   1345           if( pBt->mutex==0 ){
  1346   1346             rc = SQLITE_NOMEM;
  1347   1347             db->mallocFailed = 0;
  1348   1348             goto btree_open_out;
  1349   1349           }
  1350   1350         }

Changes to src/callback.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains functions used to access the internal hash tables
    14     14   ** of user defined functions and collation sequences.
    15     15   **
    16         -** $Id: callback.c,v 1.29 2008/08/21 20:21:35 drh Exp $
           16  +** $Id: callback.c,v 1.30 2008/09/01 18:34:20 danielk1977 Exp $
    17     17   */
    18     18   
    19     19   #include "sqliteInt.h"
    20     20   
    21     21   /*
    22     22   ** Invoke the 'collation needed' callback to request a collation sequence
    23     23   ** in the database text encoding of name zName, length nName.
................................................................................
   353    353     }
   354    354   
   355    355     /* If the createFlag parameter is false and no match was found amongst
   356    356     ** the custom functions stored in sqlite3.aFunc, try to find a built-in
   357    357     ** function to use.
   358    358     */ 
   359    359     if( !createFlag && !pBest ){
   360         -    p = functionSearch(&sqlite3GlobalFunctions, h, zName, nName);
          360  +    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
          361  +    p = functionSearch(pHash, h, zName, nName);
   361    362       while( p ){
   362    363         int score = matchQuality(p, nArg, enc);
   363    364         if( score>bestScore ){
   364    365           pBest = p;
   365    366           bestScore = score;
   366    367         }
   367    368         p = p->pNext;

Changes to src/date.c.

    12     12   ** This file contains the C functions that implement date and time
    13     13   ** functions for SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: date.c,v 1.88 2008/08/21 20:21:35 drh Exp $
           19  +** $Id: date.c,v 1.89 2008/09/01 18:34:20 danielk1977 Exp $
    20     20   **
    21     21   ** SQLite processes all times and dates as Julian Day numbers.  The
    22     22   ** dates and times are stored as the number of days since noon
    23     23   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    24     24   ** calendar system. 
    25     25   **
    26     26   ** 1970-01-01 00:00:00 is JD 2440587.5
................................................................................
  1049   1049   
  1050   1050   /*
  1051   1051   ** This function registered all of the above C functions as SQL
  1052   1052   ** functions.  This should be the only routine in this file with
  1053   1053   ** external linkage.
  1054   1054   */
  1055   1055   void sqlite3RegisterDateTimeFunctions(void){
  1056         -  static FuncDef aDateTimeFuncs[] = {
         1056  +  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
  1057   1057   #ifndef SQLITE_OMIT_DATETIME_FUNCS
  1058   1058       FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
  1059   1059       FUNCTION(date,             -1, 0, 0, dateFunc      ),
  1060   1060       FUNCTION(time,             -1, 0, 0, timeFunc      ),
  1061   1061       FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
  1062   1062       FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
  1063   1063       FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
................................................................................
  1066   1066   #else
  1067   1067       FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
  1068   1068       FUNCTION(current_timestamp, 0, "%Y-%m-%d",          0, currentTimeFunc),
  1069   1069       FUNCTION(current_date,      0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
  1070   1070   #endif
  1071   1071     };
  1072   1072     int i;
         1073  +  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
         1074  +  FuncDef *aFunc = &GLOBAL(FuncDef, aDateTimeFuncs);
  1073   1075   
  1074   1076     for(i=0; i<ArraySize(aDateTimeFuncs); i++){
  1075         -    sqlite3FuncDefInsert(&sqlite3GlobalFunctions, &aDateTimeFuncs[i]);
         1077  +    sqlite3FuncDefInsert(pHash, &aFunc[i]);
  1076   1078     }
  1077   1079   }

Changes to src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.199 2008/08/21 20:21:35 drh Exp $
           19  +** $Id: func.c,v 1.200 2008/09/01 18:34:20 danielk1977 Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <ctype.h>
    23     23   #include <stdlib.h>
    24     24   #include <assert.h>
    25     25   #include "vdbeInt.h"
    26     26   
................................................................................
  1371   1371   ** a consequence of calling sqlite3_initialize()).
  1372   1372   **
  1373   1373   ** After this routine runs
  1374   1374   */
  1375   1375   void sqlite3RegisterGlobalFunctions(void){
  1376   1376     int i;
  1377   1377     for(i=0; i<ArraySize(aBuiltinFunc); i++){
  1378         -    sqlite3FuncDefInsert(&sqlite3GlobalFunctions, &aBuiltinFunc[i]);
         1378  +    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
         1379  +    sqlite3FuncDefInsert(pHash, &aBuiltinFunc[i]);
  1379   1380     }
  1380   1381     sqlite3RegisterDateTimeFunctions();
  1381   1382   }

Changes to src/global.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   **
    13     13   ** This file contains definitions of global variables and contants.
    14     14   **
    15         -** $Id: global.c,v 1.6 2008/08/21 20:21:35 drh Exp $
           15  +** $Id: global.c,v 1.7 2008/09/01 18:34:20 danielk1977 Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /* An array to map all upper-case characters into their corresponding
    21     21   ** lower-case character. 
    22     22   **
................................................................................
    62     62   #endif
    63     63   };
    64     64   
    65     65   /*
    66     66   ** The following singleton contains the global configuration for
    67     67   ** the SQLite library.
    68     68   */
    69         -struct Sqlite3Config sqlite3Config = {
           69  +SQLITE_WSD struct Sqlite3Config sqlite3Config = {
    70     70      1,                /* bMemstat */
    71     71      1,                /* bCoreMutex */
    72     72      1,                /* bFullMutex */
    73     73      0x7ffffffe,       /* mxStrlen */
    74     74      100,              /* szLookaside */
    75     75      500,              /* nLookaside */
    76     76      /* Other fields all default to zero */
................................................................................
    78     78   
    79     79   
    80     80   /*
    81     81   ** Hash table for global functions - functions common to all
    82     82   ** database connections.  After initialization, this table is
    83     83   ** read-only.
    84     84   */
    85         -FuncDefHash sqlite3GlobalFunctions;
           85  +SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;

Changes to src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.494 2008/08/27 19:01:58 danielk1977 Exp $
           17  +** $Id: main.c,v 1.495 2008/09/01 18:34:20 danielk1977 Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <ctype.h>
    21     21   
    22     22   #ifdef SQLITE_ENABLE_FTS3
    23     23   # include "fts3.h"
    24     24   #endif
................................................................................
    81     81   **    *  Calls to this routine from Y must block until the outer-most
    82     82   **       call by X completes.
    83     83   **
    84     84   **    *  Recursive calls to this routine from thread X return immediately
    85     85   **       without blocking.
    86     86   */
    87     87   int sqlite3_initialize(void){
    88         -  static int inProgress = 0;        /* Prevent recursion */
    89         -  sqlite3_mutex *pMaster;           /* The main static mutex */
    90         -  int rc;                           /* Result code */
           88  +  SQLITE_WSD static int inProgress = 0;        /* Prevent recursion */
           89  +  sqlite3_mutex *pMaster;                      /* The main static mutex */
           90  +  int rc;                                      /* Result code */
           91  +
           92  +#ifdef SQLITE_OMIT_WSD
           93  +  rc = sqlite3_wsd_init(1024, 256);
           94  +  if( rc!=SQLITE_OK ){
           95  +    return rc;
           96  +  }
           97  +#endif
    91     98   
    92     99     /* If SQLite is already completely initialized, then this call
    93    100     ** to sqlite3_initialize() should be a no-op.  But the initialization
    94    101     ** must be complete.  So isInit must not be set until the very end
    95    102     ** of this routine.
    96    103     */
    97         -  if( sqlite3Config.isInit ) return SQLITE_OK;
          104  +  if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
    98    105   
    99    106     /* Make sure the mutex subsystem is initialized.  If unable to 
   100    107     ** initialize the mutex subsystem, return early with the error.
   101    108     ** If the system is so sick that we are unable to allocate a mutex,
   102    109     ** there is not much SQLite is going to be able to do.
   103    110     **
   104    111     ** The mutex subsystem must take care of serializing its own
................................................................................
   111    118     ** This operation is protected by the STATIC_MASTER mutex.  Note that
   112    119     ** MutexAlloc() is called for a static mutex prior to initializing the
   113    120     ** malloc subsystem - this implies that the allocation of a static
   114    121     ** mutex must not require support from the malloc subsystem.
   115    122     */
   116    123     pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   117    124     sqlite3_mutex_enter(pMaster);
   118         -  if( !sqlite3Config.isMallocInit ){
          125  +  if( !sqlite3GlobalConfig.isMallocInit ){
   119    126       rc = sqlite3MallocInit();
   120    127     }
   121    128     if( rc==SQLITE_OK ){
   122         -    sqlite3Config.isMallocInit = 1;
   123         -    if( !sqlite3Config.pInitMutex ){
   124         -      sqlite3Config.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
   125         -      if( sqlite3Config.bCoreMutex && !sqlite3Config.pInitMutex ){
          129  +    sqlite3GlobalConfig.isMallocInit = 1;
          130  +    if( !sqlite3GlobalConfig.pInitMutex ){
          131  +      sqlite3GlobalConfig.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
          132  +      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
   126    133           rc = SQLITE_NOMEM;
   127    134         }
   128    135       }
   129         -    sqlite3Config.nRefInitMutex++;
          136  +    sqlite3GlobalConfig.nRefInitMutex++;
   130    137     }
   131    138     sqlite3_mutex_leave(pMaster);
   132    139   
   133    140     /* If unable to initialize the malloc subsystem, then return early.
   134    141     ** There is little hope of getting SQLite to run if the malloc
   135    142     ** subsystem cannot be initialized.
   136    143     */
................................................................................
   140    147   
   141    148     /* Do the rest of the initialization under the recursive mutex so
   142    149     ** that we will be able to handle recursive calls into
   143    150     ** sqlite3_initialize().  The recursive calls normally come through
   144    151     ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
   145    152     ** recursive calls might also be possible.
   146    153     */
   147         -  sqlite3_mutex_enter(sqlite3Config.pInitMutex);
   148         -  if( sqlite3Config.isInit==0 && inProgress==0 ){
   149         -    inProgress = 1;
   150         -    memset(&sqlite3GlobalFunctions, 0, sizeof(sqlite3GlobalFunctions));
          154  +  sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
          155  +  if( sqlite3GlobalConfig.isInit==0 && GLOBAL(int, inProgress)==0 ){
          156  +    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
          157  +    GLOBAL(int, inProgress) = 1;
          158  +    memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
   151    159       sqlite3RegisterGlobalFunctions();
   152    160       rc = sqlite3_os_init();
   153    161       if( rc==SQLITE_OK ){
   154    162         rc = sqlite3PcacheInitialize();
   155         -      sqlite3PCacheBufferSetup(sqlite3Config.pPage, sqlite3Config.szPage, 
   156         -          sqlite3Config.nPage);
          163  +      sqlite3PCacheBufferSetup(sqlite3GlobalConfig.pPage, sqlite3GlobalConfig.szPage, 
          164  +          sqlite3GlobalConfig.nPage);
   157    165       }
   158         -    inProgress = 0;
   159         -    sqlite3Config.isInit = (rc==SQLITE_OK ? 1 : 0);
          166  +    GLOBAL(int, inProgress) = 0;
          167  +    sqlite3GlobalConfig.isInit = (rc==SQLITE_OK ? 1 : 0);
   160    168     }
   161         -  sqlite3_mutex_leave(sqlite3Config.pInitMutex);
          169  +  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
   162    170   
   163    171     /* Go back under the static mutex and clean up the recursive
   164    172     ** mutex to prevent a resource leak.
   165    173     */
   166    174     sqlite3_mutex_enter(pMaster);
   167         -  sqlite3Config.nRefInitMutex--;
   168         -  if( sqlite3Config.nRefInitMutex<=0 ){
   169         -    assert( sqlite3Config.nRefInitMutex==0 );
   170         -    sqlite3_mutex_free(sqlite3Config.pInitMutex);
   171         -    sqlite3Config.pInitMutex = 0;
          175  +  sqlite3GlobalConfig.nRefInitMutex--;
          176  +  if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
          177  +    assert( sqlite3GlobalConfig.nRefInitMutex==0 );
          178  +    sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
          179  +    sqlite3GlobalConfig.pInitMutex = 0;
   172    180     }
   173    181     sqlite3_mutex_leave(pMaster);
   174    182   
   175    183     /* The following is just a sanity check to make sure SQLite has
   176    184     ** been compiled correctly.  It is important to run this code, but
   177    185     ** we don't want to run it too often and soak up CPU cycles for no
   178    186     ** reason.  So we run it once during initialization.
................................................................................
   195    203   /*
   196    204   ** Undo the effects of sqlite3_initialize().  Must not be called while
   197    205   ** there are outstanding database connections or memory allocations or
   198    206   ** while any part of SQLite is otherwise in use in any thread.  This
   199    207   ** routine is not threadsafe.  Not by a long shot.
   200    208   */
   201    209   int sqlite3_shutdown(void){
   202         -  sqlite3Config.isMallocInit = 0;
          210  +  sqlite3GlobalConfig.isMallocInit = 0;
   203    211     sqlite3PcacheShutdown();
   204         -  if( sqlite3Config.isInit ){
          212  +  if( sqlite3GlobalConfig.isInit ){
   205    213       sqlite3_os_end();
   206    214     }
   207         -  if( sqlite3Config.m.xShutdown ){
          215  +  if( sqlite3GlobalConfig.m.xShutdown ){
   208    216       sqlite3MallocEnd();
   209    217     }
   210         -  if( sqlite3Config.mutex.xMutexEnd ){
          218  +  if( sqlite3GlobalConfig.mutex.xMutexEnd ){
   211    219       sqlite3MutexEnd();
   212    220     }
   213         -  sqlite3Config.isInit = 0;
          221  +  sqlite3GlobalConfig.isInit = 0;
   214    222     return SQLITE_OK;
   215    223   }
   216    224   
   217    225   /*
   218    226   ** This API allows applications to modify the global configuration of
   219    227   ** the SQLite library at run-time.
   220    228   **
................................................................................
   225    233   */
   226    234   int sqlite3_config(int op, ...){
   227    235     va_list ap;
   228    236     int rc = SQLITE_OK;
   229    237   
   230    238     /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
   231    239     ** the SQLite library is in use. */
   232         -  if( sqlite3Config.isInit ) return SQLITE_MISUSE;
          240  +  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE;
   233    241   
   234    242     va_start(ap, op);
   235    243     switch( op ){
   236    244       case SQLITE_CONFIG_SINGLETHREAD: {
   237    245         /* Disable all mutexing */
   238         -      sqlite3Config.bCoreMutex = 0;
   239         -      sqlite3Config.bFullMutex = 0;
          246  +      sqlite3GlobalConfig.bCoreMutex = 0;
          247  +      sqlite3GlobalConfig.bFullMutex = 0;
   240    248         break;
   241    249       }
   242    250       case SQLITE_CONFIG_MULTITHREAD: {
   243    251         /* Disable mutexing of database connections */
   244    252         /* Enable mutexing of core data structures */
   245         -      sqlite3Config.bCoreMutex = 1;
   246         -      sqlite3Config.bFullMutex = 0;
          253  +      sqlite3GlobalConfig.bCoreMutex = 1;
          254  +      sqlite3GlobalConfig.bFullMutex = 0;
   247    255         break;
   248    256       }
   249    257       case SQLITE_CONFIG_SERIALIZED: {
   250    258         /* Enable all mutexing */
   251         -      sqlite3Config.bCoreMutex = 1;
   252         -      sqlite3Config.bFullMutex = 1;
          259  +      sqlite3GlobalConfig.bCoreMutex = 1;
          260  +      sqlite3GlobalConfig.bFullMutex = 1;
   253    261         break;
   254    262       }
   255    263       case SQLITE_CONFIG_MALLOC: {
   256    264         /* Specify an alternative malloc implementation */
   257         -      sqlite3Config.m = *va_arg(ap, sqlite3_mem_methods*);
          265  +      sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
   258    266         break;
   259    267       }
   260    268       case SQLITE_CONFIG_GETMALLOC: {
   261    269         /* Retrieve the current malloc() implementation */
   262         -      if( sqlite3Config.m.xMalloc==0 ) sqlite3MemSetDefault();
   263         -      *va_arg(ap, sqlite3_mem_methods*) = sqlite3Config.m;
          270  +      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
          271  +      *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
   264    272         break;
   265    273       }
   266    274       case SQLITE_CONFIG_MUTEX: {
   267    275         /* Specify an alternative mutex implementation */
   268         -      sqlite3Config.mutex = *va_arg(ap, sqlite3_mutex_methods*);
          276  +      sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
   269    277         break;
   270    278       }
   271    279       case SQLITE_CONFIG_GETMUTEX: {
   272    280         /* Retrieve the current mutex implementation */
   273         -      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3Config.mutex;
          281  +      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
   274    282         break;
   275    283       }
   276    284       case SQLITE_CONFIG_MEMSTATUS: {
   277    285         /* Enable or disable the malloc status collection */
   278         -      sqlite3Config.bMemstat = va_arg(ap, int);
          286  +      sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
   279    287         break;
   280    288       }
   281    289       case SQLITE_CONFIG_SCRATCH: {
   282    290         /* Designate a buffer for scratch memory space */
   283         -      sqlite3Config.pScratch = va_arg(ap, void*);
   284         -      sqlite3Config.szScratch = va_arg(ap, int);
   285         -      sqlite3Config.nScratch = va_arg(ap, int);
          291  +      sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
          292  +      sqlite3GlobalConfig.szScratch = va_arg(ap, int);
          293  +      sqlite3GlobalConfig.nScratch = va_arg(ap, int);
   286    294         break;
   287    295       }
   288    296       case SQLITE_CONFIG_PAGECACHE: {
   289    297         /* Designate a buffer for scratch memory space */
   290         -      sqlite3Config.pPage = va_arg(ap, void*);
   291         -      sqlite3Config.szPage = va_arg(ap, int);
   292         -      sqlite3Config.nPage = va_arg(ap, int);
          298  +      sqlite3GlobalConfig.pPage = va_arg(ap, void*);
          299  +      sqlite3GlobalConfig.szPage = va_arg(ap, int);
          300  +      sqlite3GlobalConfig.nPage = va_arg(ap, int);
   293    301         break;
   294    302       }
   295    303   
   296    304   #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
   297    305       case SQLITE_CONFIG_HEAP: {
   298    306         /* Designate a buffer for heap memory space */
   299         -      sqlite3Config.pHeap = va_arg(ap, void*);
   300         -      sqlite3Config.nHeap = va_arg(ap, int);
   301         -      sqlite3Config.mnReq = va_arg(ap, int);
          307  +      sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
          308  +      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
          309  +      sqlite3GlobalConfig.mnReq = va_arg(ap, int);
   302    310   
   303         -      if( sqlite3Config.pHeap==0 ){
          311  +      if( sqlite3GlobalConfig.pHeap==0 ){
   304    312           /* If the heap pointer is NULL, then restore the malloc implementation
   305    313           ** back to NULL pointers too.  This will cause the malloc to go
   306    314           ** back to its default implementation when sqlite3_initialize() is
   307    315           ** run.
   308    316           */
   309         -        memset(&sqlite3Config.m, 0, sizeof(sqlite3Config.m));
          317  +        memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
   310    318         }else{
   311    319           /* The heap pointer is not NULL, then install one of the
   312    320           ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
   313    321           ** ENABLE_MEMSYS5 is defined, return an error.
   314    322           ** the default case and return an error.
   315    323           */
   316    324   #ifdef SQLITE_ENABLE_MEMSYS3
   317         -        sqlite3Config.m = *sqlite3MemGetMemsys3();
          325  +        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
   318    326   #endif
   319    327   #ifdef SQLITE_ENABLE_MEMSYS5
   320         -        sqlite3Config.m = *sqlite3MemGetMemsys5();
          328  +        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
   321    329   #endif
   322    330         }
   323    331         break;
   324    332       }
   325    333   #endif
   326    334   
   327    335   #if defined(SQLITE_ENABLE_MEMSYS6)
   328    336       case SQLITE_CONFIG_CHUNKALLOC: {
   329         -      sqlite3Config.nSmall = va_arg(ap, int);
   330         -      sqlite3Config.m = *sqlite3MemGetMemsys6();
          337  +      sqlite3GlobalConfig.nSmall = va_arg(ap, int);
          338  +      sqlite3GlobalConfig.m = *sqlite3MemGetMemsys6();
   331    339         break;
   332    340       }
   333    341   #endif
   334    342   
   335    343       case SQLITE_CONFIG_LOOKASIDE: {
   336         -      sqlite3Config.szLookaside = va_arg(ap, int);
   337         -      sqlite3Config.nLookaside = va_arg(ap, int);
          344  +      sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
          345  +      sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
   338    346         break;
   339    347       }
   340    348   
   341    349       default: {
   342    350         rc = SQLITE_ERROR;
   343    351         break;
   344    352       }
................................................................................
  1441   1449                  SQLITE_OPEN_MASTER_JOURNAL |
  1442   1450                  SQLITE_OPEN_NOMUTEX
  1443   1451                );
  1444   1452   
  1445   1453     /* Allocate the sqlite data structure */
  1446   1454     db = sqlite3MallocZero( sizeof(sqlite3) );
  1447   1455     if( db==0 ) goto opendb_out;
  1448         -  if( sqlite3Config.bFullMutex && isThreadsafe ){
         1456  +  if( sqlite3GlobalConfig.bFullMutex && isThreadsafe ){
  1449   1457       db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
  1450   1458       if( db->mutex==0 ){
  1451   1459         sqlite3_free(db);
  1452   1460         db = 0;
  1453   1461         goto opendb_out;
  1454   1462       }
  1455   1463     }
................................................................................
  1594   1602   #ifdef SQLITE_DEFAULT_LOCKING_MODE
  1595   1603     db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
  1596   1604     sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
  1597   1605                             SQLITE_DEFAULT_LOCKING_MODE);
  1598   1606   #endif
  1599   1607   
  1600   1608     /* Enable the lookaside-malloc subsystem */
  1601         -  setupLookaside(db, 0, sqlite3Config.szLookaside, sqlite3Config.nLookaside);
         1609  +  setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, sqlite3GlobalConfig.nLookaside);
  1602   1610   
  1603   1611   opendb_out:
  1604   1612     if( db ){
  1605         -    assert( db->mutex!=0 || isThreadsafe==0 || sqlite3Config.bFullMutex==0 );
         1613  +    assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
  1606   1614       sqlite3_mutex_leave(db->mutex);
  1607   1615     }
  1608   1616     rc = sqlite3_errcode(db);
  1609   1617     if( rc==SQLITE_NOMEM ){
  1610   1618       sqlite3_close(db);
  1611   1619       db = 0;
  1612   1620     }else if( rc!=SQLITE_OK ){

Changes to src/malloc.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   **
    13     13   ** Memory allocation functions used throughout sqlite.
    14     14   **
    15         -** $Id: malloc.c,v 1.37 2008/08/29 17:56:13 danielk1977 Exp $
           15  +** $Id: malloc.c,v 1.38 2008/09/01 18:34:20 danielk1977 Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <stdarg.h>
    19     19   #include <ctype.h>
    20     20   
    21     21   /*
    22     22   ** This routine runs when the memory allocator sees that the
................................................................................
    88     88     */
    89     89     sqlite3_int64 alarmThreshold;
    90     90     void (*alarmCallback)(void*, sqlite3_int64,int);
    91     91     void *alarmArg;
    92     92     int alarmBusy;
    93     93   
    94     94     /*
    95         -  ** Pointers to the end of sqlite3Config.pScratch and
    96         -  ** sqlite3Config.pPage to a block of memory that records
           95  +  ** Pointers to the end of sqlite3GlobalConfig.pScratch and
           96  +  ** sqlite3GlobalConfig.pPage to a block of memory that records
    97     97     ** which pages are available.
    98     98     */
    99     99     u32 *aScratchFree;
   100    100     u32 *aPageFree;
   101    101   
   102    102     /* Number of free pages for scratch and page-cache memory */
   103    103     u32 nScratchFree;
................................................................................
   104    104     u32 nPageFree;
   105    105   } mem0;
   106    106   
   107    107   /*
   108    108   ** Initialize the memory allocation subsystem.
   109    109   */
   110    110   int sqlite3MallocInit(void){
   111         -  if( sqlite3Config.m.xMalloc==0 ){
          111  +  if( sqlite3GlobalConfig.m.xMalloc==0 ){
   112    112       sqlite3MemSetDefault();
   113    113     }
   114    114     memset(&mem0, 0, sizeof(mem0));
   115         -  if( sqlite3Config.bCoreMutex ){
          115  +  if( sqlite3GlobalConfig.bCoreMutex ){
   116    116       mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   117    117     }
   118         -  if( sqlite3Config.pScratch && sqlite3Config.szScratch>=100
   119         -      && sqlite3Config.nScratch>=0 ){
          118  +  if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
          119  +      && sqlite3GlobalConfig.nScratch>=0 ){
   120    120       int i;
   121         -    sqlite3Config.szScratch -= 4;
   122         -    mem0.aScratchFree = (u32*)&((char*)sqlite3Config.pScratch)
   123         -                  [sqlite3Config.szScratch*sqlite3Config.nScratch];
   124         -    for(i=0; i<sqlite3Config.nScratch; i++){ mem0.aScratchFree[i] = i; }
   125         -    mem0.nScratchFree = sqlite3Config.nScratch;
          121  +    sqlite3GlobalConfig.szScratch -= 4;
          122  +    mem0.aScratchFree = (u32*)&((char*)sqlite3GlobalConfig.pScratch)
          123  +                  [sqlite3GlobalConfig.szScratch*sqlite3GlobalConfig.nScratch];
          124  +    for(i=0; i<sqlite3GlobalConfig.nScratch; i++){ mem0.aScratchFree[i] = i; }
          125  +    mem0.nScratchFree = sqlite3GlobalConfig.nScratch;
   126    126     }else{
   127         -    sqlite3Config.pScratch = 0;
   128         -    sqlite3Config.szScratch = 0;
          127  +    sqlite3GlobalConfig.pScratch = 0;
          128  +    sqlite3GlobalConfig.szScratch = 0;
   129    129     }
   130         -  if( sqlite3Config.pPage && sqlite3Config.szPage>=512
   131         -      && sqlite3Config.nPage>=1 ){
          130  +  if( sqlite3GlobalConfig.pPage && sqlite3GlobalConfig.szPage>=512
          131  +      && sqlite3GlobalConfig.nPage>=1 ){
   132    132       int i;
   133    133       int overhead;
   134         -    int sz = sqlite3Config.szPage;
   135         -    int n = sqlite3Config.nPage;
          134  +    int sz = sqlite3GlobalConfig.szPage;
          135  +    int n = sqlite3GlobalConfig.nPage;
   136    136       overhead = (4*n + sz - 1)/sz;
   137         -    sqlite3Config.nPage -= overhead;
   138         -    mem0.aPageFree = (u32*)&((char*)sqlite3Config.pPage)
   139         -                  [sqlite3Config.szPage*sqlite3Config.nPage];
   140         -    for(i=0; i<sqlite3Config.nPage; i++){ mem0.aPageFree[i] = i; }
   141         -    mem0.nPageFree = sqlite3Config.nPage;
          137  +    sqlite3GlobalConfig.nPage -= overhead;
          138  +    mem0.aPageFree = (u32*)&((char*)sqlite3GlobalConfig.pPage)
          139  +                  [sqlite3GlobalConfig.szPage*sqlite3GlobalConfig.nPage];
          140  +    for(i=0; i<sqlite3GlobalConfig.nPage; i++){ mem0.aPageFree[i] = i; }
          141  +    mem0.nPageFree = sqlite3GlobalConfig.nPage;
   142    142     }else{
   143         -    sqlite3Config.pPage = 0;
   144         -    sqlite3Config.szPage = 0;
          143  +    sqlite3GlobalConfig.pPage = 0;
          144  +    sqlite3GlobalConfig.szPage = 0;
   145    145     }
   146         -  return sqlite3Config.m.xInit(sqlite3Config.m.pAppData);
          146  +  return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
   147    147   }
   148    148   
   149    149   /*
   150    150   ** Deinitialize the memory allocation subsystem.
   151    151   */
   152    152   void sqlite3MallocEnd(void){
   153         -  sqlite3Config.m.xShutdown(sqlite3Config.m.pAppData);
          153  +  sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
   154    154     memset(&mem0, 0, sizeof(mem0));
   155    155   }
   156    156   
   157    157   /*
   158    158   ** Return the amount of memory currently checked out.
   159    159   */
   160    160   sqlite3_int64 sqlite3_memory_used(void){
................................................................................
   216    216   ** Do a memory allocation with statistics and alarms.  Assume the
   217    217   ** lock is already held.
   218    218   */
   219    219   static int mallocWithAlarm(int n, void **pp){
   220    220     int nFull;
   221    221     void *p;
   222    222     assert( sqlite3_mutex_held(mem0.mutex) );
   223         -  nFull = sqlite3Config.m.xRoundup(n);
          223  +  nFull = sqlite3GlobalConfig.m.xRoundup(n);
   224    224     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
   225    225     if( mem0.alarmCallback!=0 ){
   226    226       int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   227    227       if( nUsed+nFull >= mem0.alarmThreshold ){
   228    228         sqlite3MallocAlarm(nFull);
   229    229       }
   230    230     }
   231         -  p = sqlite3Config.m.xMalloc(nFull);
          231  +  p = sqlite3GlobalConfig.m.xMalloc(nFull);
   232    232     if( p==0 && mem0.alarmCallback ){
   233    233       sqlite3MallocAlarm(nFull);
   234         -    p = sqlite3Config.m.xMalloc(nFull);
          234  +    p = sqlite3GlobalConfig.m.xMalloc(nFull);
   235    235     }
   236    236     if( p ){
   237    237       nFull = sqlite3MallocSize(p);
   238    238       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
   239    239     }
   240    240     *pp = p;
   241    241     return nFull;
................................................................................
   245    245   ** Allocate memory.  This routine is like sqlite3_malloc() except that it
   246    246   ** assumes the memory subsystem has already been initialized.
   247    247   */
   248    248   void *sqlite3Malloc(int n){
   249    249     void *p;
   250    250     if( n<=0 ){
   251    251       p = 0;
   252         -  }else if( sqlite3Config.bMemstat ){
          252  +  }else if( sqlite3GlobalConfig.bMemstat ){
   253    253       sqlite3_mutex_enter(mem0.mutex);
   254    254       mallocWithAlarm(n, &p);
   255    255       sqlite3_mutex_leave(mem0.mutex);
   256    256     }else{
   257         -    p = sqlite3Config.m.xMalloc(n);
          257  +    p = sqlite3GlobalConfig.m.xMalloc(n);
   258    258     }
   259    259     return p;
   260    260   }
   261    261   
   262    262   /*
   263    263   ** This version of the memory allocation is for use by the application.
   264    264   ** First make sure the memory subsystem is initialized, then do the
................................................................................
   298    298     /* Verify that no more than one scratch allocation per thread
   299    299     ** is outstanding at one time.  (This is only checked in the
   300    300     ** single-threaded case since checking in the multi-threaded case
   301    301     ** would be much more complicated.) */
   302    302     assert( scratchAllocOut==0 );
   303    303   #endif
   304    304   
   305         -  if( sqlite3Config.szScratch<n ){
          305  +  if( sqlite3GlobalConfig.szScratch<n ){
   306    306       goto scratch_overflow;
   307    307     }else{  
   308    308       sqlite3_mutex_enter(mem0.mutex);
   309    309       if( mem0.nScratchFree==0 ){
   310    310         sqlite3_mutex_leave(mem0.mutex);
   311    311         goto scratch_overflow;
   312    312       }else{
   313    313         int i;
   314    314         i = mem0.aScratchFree[--mem0.nScratchFree];
   315         -      i *= sqlite3Config.szScratch;
          315  +      i *= sqlite3GlobalConfig.szScratch;
   316    316         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
   317    317         sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   318    318         sqlite3_mutex_leave(mem0.mutex);
   319         -      p = (void*)&((char*)sqlite3Config.pScratch)[i];
          319  +      p = (void*)&((char*)sqlite3GlobalConfig.pScratch)[i];
   320    320       }
   321    321     }
   322    322   #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   323    323     scratchAllocOut = p!=0;
   324    324   #endif
   325    325   
   326    326     return p;
   327    327   
   328    328   scratch_overflow:
   329         -  if( sqlite3Config.bMemstat ){
          329  +  if( sqlite3GlobalConfig.bMemstat ){
   330    330       sqlite3_mutex_enter(mem0.mutex);
   331    331       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   332    332       n = mallocWithAlarm(n, &p);
   333    333       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
   334    334       sqlite3_mutex_leave(mem0.mutex);
   335    335     }else{
   336         -    p = sqlite3Config.m.xMalloc(n);
          336  +    p = sqlite3GlobalConfig.m.xMalloc(n);
   337    337     }
   338    338   #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   339    339     scratchAllocOut = p!=0;
   340    340   #endif
   341    341     return p;    
   342    342   }
   343    343   void sqlite3ScratchFree(void *p){
................................................................................
   348    348       ** is outstanding at one time.  (This is only checked in the
   349    349       ** single-threaded case since checking in the multi-threaded case
   350    350       ** would be much more complicated.) */
   351    351       assert( scratchAllocOut==1 );
   352    352       scratchAllocOut = 0;
   353    353   #endif
   354    354   
   355         -    if( sqlite3Config.pScratch==0
   356         -           || p<sqlite3Config.pScratch
          355  +    if( sqlite3GlobalConfig.pScratch==0
          356  +           || p<sqlite3GlobalConfig.pScratch
   357    357              || p>=(void*)mem0.aScratchFree ){
   358         -      if( sqlite3Config.bMemstat ){
          358  +      if( sqlite3GlobalConfig.bMemstat ){
   359    359           int iSize = sqlite3MallocSize(p);
   360    360           sqlite3_mutex_enter(mem0.mutex);
   361    361           sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
   362    362           sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
   363         -        sqlite3Config.m.xFree(p);
          363  +        sqlite3GlobalConfig.m.xFree(p);
   364    364           sqlite3_mutex_leave(mem0.mutex);
   365    365         }else{
   366         -        sqlite3Config.m.xFree(p);
          366  +        sqlite3GlobalConfig.m.xFree(p);
   367    367         }
   368    368       }else{
   369    369         int i;
   370         -      i = (u8 *)p - (u8 *)sqlite3Config.pScratch;
   371         -      i /= sqlite3Config.szScratch;
   372         -      assert( i>=0 && i<sqlite3Config.nScratch );
          370  +      i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
          371  +      i /= sqlite3GlobalConfig.szScratch;
          372  +      assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
   373    373         sqlite3_mutex_enter(mem0.mutex);
   374         -      assert( mem0.nScratchFree<sqlite3Config.nScratch );
          374  +      assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch );
   375    375         mem0.aScratchFree[mem0.nScratchFree++] = i;
   376    376         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
   377    377         sqlite3_mutex_leave(mem0.mutex);
   378    378       }
   379    379     }
   380    380   }
   381    381   
................................................................................
   388    388   #if 0
   389    389   void *sqlite3PageMalloc(int n){
   390    390     void *p;
   391    391     assert( n>0 );
   392    392     assert( (n & (n-1))==0 );
   393    393     assert( n>=512 && n<=32768 );
   394    394   
   395         -  if( sqlite3Config.szPage<n ){
          395  +  if( sqlite3GlobalConfig.szPage<n ){
   396    396       goto page_overflow;
   397    397     }else{  
   398    398       sqlite3_mutex_enter(mem0.mutex);
   399    399       if( mem0.nPageFree==0 ){
   400    400         sqlite3_mutex_leave(mem0.mutex);
   401    401         goto page_overflow;
   402    402       }else{
   403    403         int i;
   404    404         i = mem0.aPageFree[--mem0.nPageFree];
   405    405         sqlite3_mutex_leave(mem0.mutex);
   406         -      i *= sqlite3Config.szPage;
          406  +      i *= sqlite3GlobalConfig.szPage;
   407    407         sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
   408    408         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
   409         -      p = (void*)&((char*)sqlite3Config.pPage)[i];
          409  +      p = (void*)&((char*)sqlite3GlobalConfig.pPage)[i];
   410    410       }
   411    411     }
   412    412     return p;
   413    413   
   414    414   page_overflow:
   415         -  if( sqlite3Config.bMemstat ){
          415  +  if( sqlite3GlobalConfig.bMemstat ){
   416    416       sqlite3_mutex_enter(mem0.mutex);
   417    417       sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, n);
   418    418       n = mallocWithAlarm(n, &p);
   419    419       if( p ) sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, n);
   420    420       sqlite3_mutex_leave(mem0.mutex);
   421    421     }else{
   422         -    p = sqlite3Config.m.xMalloc(n);
          422  +    p = sqlite3GlobalConfig.m.xMalloc(n);
   423    423     }
   424    424     return p;    
   425    425   }
   426    426   void sqlite3PageFree(void *p){
   427    427     if( p ){
   428         -    if( sqlite3Config.pPage==0
   429         -           || p<sqlite3Config.pPage
          428  +    if( sqlite3GlobalConfig.pPage==0
          429  +           || p<sqlite3GlobalConfig.pPage
   430    430              || p>=(void*)mem0.aPageFree ){
   431    431         /* In this case, the page allocation was obtained from a regular 
   432    432         ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory 
   433    433         ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
   434    434         */
   435         -      if( sqlite3Config.bMemstat ){
          435  +      if( sqlite3GlobalConfig.bMemstat ){
   436    436           int iSize = sqlite3MallocSize(p);
   437    437           sqlite3_mutex_enter(mem0.mutex);
   438    438           sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
   439    439           sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
   440         -        sqlite3Config.m.xFree(p);
          440  +        sqlite3GlobalConfig.m.xFree(p);
   441    441           sqlite3_mutex_leave(mem0.mutex);
   442    442         }else{
   443         -        sqlite3Config.m.xFree(p);
          443  +        sqlite3GlobalConfig.m.xFree(p);
   444    444         }
   445    445       }else{
   446         -      /* The page allocation was allocated from the sqlite3Config.pPage
          446  +      /* The page allocation was allocated from the sqlite3GlobalConfig.pPage
   447    447         ** buffer. In this case all that is add the index of the page in
   448         -      ** the sqlite3Config.pPage array to the set of free indexes stored
          448  +      ** the sqlite3GlobalConfig.pPage array to the set of free indexes stored
   449    449         ** in the mem0.aPageFree[] array.
   450    450         */
   451    451         int i;
   452         -      i = (u8 *)p - (u8 *)sqlite3Config.pPage;
   453         -      i /= sqlite3Config.szPage;
   454         -      assert( i>=0 && i<sqlite3Config.nPage );
          452  +      i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pPage;
          453  +      i /= sqlite3GlobalConfig.szPage;
          454  +      assert( i>=0 && i<sqlite3GlobalConfig.nPage );
   455    455         sqlite3_mutex_enter(mem0.mutex);
   456         -      assert( mem0.nPageFree<sqlite3Config.nPage );
          456  +      assert( mem0.nPageFree<sqlite3GlobalConfig.nPage );
   457    457         mem0.aPageFree[mem0.nPageFree++] = i;
   458    458         sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
   459    459         sqlite3_mutex_leave(mem0.mutex);
   460    460   #if !defined(NDEBUG) && 0
   461    461         /* Assert that a duplicate was not just inserted into aPageFree[]. */
   462    462         for(i=0; i<mem0.nPageFree-1; i++){
   463    463           assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
................................................................................
   476    476   }
   477    477   
   478    478   /*
   479    479   ** Return the size of a memory allocation previously obtained from
   480    480   ** sqlite3Malloc() or sqlite3_malloc().
   481    481   */
   482    482   int sqlite3MallocSize(void *p){
   483         -  return sqlite3Config.m.xSize(p);
          483  +  return sqlite3GlobalConfig.m.xSize(p);
   484    484   }
   485    485   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   486    486     if( isLookaside(db, p) ){
   487    487       return db->lookaside.sz;
   488    488     }else{
   489         -    return sqlite3Config.m.xSize(p);
          489  +    return sqlite3GlobalConfig.m.xSize(p);
   490    490     }
   491    491   }
   492    492   
   493    493   /*
   494    494   ** Free memory previously obtained from sqlite3Malloc().
   495    495   */
   496    496   void sqlite3_free(void *p){
   497    497     if( p==0 ) return;
   498         -  if( sqlite3Config.bMemstat ){
          498  +  if( sqlite3GlobalConfig.bMemstat ){
   499    499       sqlite3_mutex_enter(mem0.mutex);
   500    500       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   501         -    sqlite3Config.m.xFree(p);
          501  +    sqlite3GlobalConfig.m.xFree(p);
   502    502       sqlite3_mutex_leave(mem0.mutex);
   503    503     }else{
   504         -    sqlite3Config.m.xFree(p);
          504  +    sqlite3GlobalConfig.m.xFree(p);
   505    505     }
   506    506   }
   507    507   
   508    508   /*
   509    509   ** Free memory that might be associated with a particular database
   510    510   ** connection.
   511    511   */
................................................................................
   530    530       return sqlite3Malloc(nBytes);
   531    531     }
   532    532     if( nBytes<=0 ){
   533    533       sqlite3_free(pOld);
   534    534       return 0;
   535    535     }
   536    536     nOld = sqlite3MallocSize(pOld);
   537         -  if( sqlite3Config.bMemstat ){
          537  +  if( sqlite3GlobalConfig.bMemstat ){
   538    538       sqlite3_mutex_enter(mem0.mutex);
   539    539       sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
   540         -    nNew = sqlite3Config.m.xRoundup(nBytes);
          540  +    nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
   541    541       if( nOld==nNew ){
   542    542         pNew = pOld;
   543    543       }else{
   544    544         if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= 
   545    545               mem0.alarmThreshold ){
   546    546           sqlite3MallocAlarm(nNew-nOld);
   547    547         }
   548         -      pNew = sqlite3Config.m.xRealloc(pOld, nNew);
          548  +      pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   549    549         if( pNew==0 && mem0.alarmCallback ){
   550    550           sqlite3MallocAlarm(nBytes);
   551         -        pNew = sqlite3Config.m.xRealloc(pOld, nNew);
          551  +        pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   552    552         }
   553    553         if( pNew ){
   554    554           nNew = sqlite3MallocSize(pNew);
   555    555           sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
   556    556         }
   557    557       }
   558    558       sqlite3_mutex_leave(mem0.mutex);
   559    559     }else{
   560         -    pNew = sqlite3Config.m.xRealloc(pOld, nBytes);
          560  +    pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nBytes);
   561    561     }
   562    562     return pNew;
   563    563   }
   564    564   
   565    565   /*
   566    566   ** The public interface to sqlite3Realloc.  Make sure that the memory
   567    567   ** subsystem is initialized prior to invoking sqliteRealloc.

Changes to src/mem1.c.

    13     13   ** This file contains low-level memory allocation drivers for when
    14     14   ** SQLite will use the standard C-library malloc/realloc/free interface
    15     15   ** to obtain the memory it needs.
    16     16   **
    17     17   ** This file contains implementations of the low-level memory allocation
    18     18   ** routines specified in the sqlite3_mem_methods object.
    19     19   **
    20         -** $Id: mem1.c,v 1.25 2008/07/25 08:49:00 danielk1977 Exp $
           20  +** $Id: mem1.c,v 1.26 2008/09/01 18:34:20 danielk1977 Exp $
    21     21   */
    22     22   #include "sqliteInt.h"
    23     23   
    24     24   /*
    25     25   ** This version of the memory allocator is the default.  It is
    26     26   ** used when no other memory allocator is specified using compile-time
    27     27   ** macros.
................................................................................
   134    134     return &defaultMethods;
   135    135   }
   136    136   
   137    137   /*
   138    138   ** This routine is the only routine in this file with external linkage.
   139    139   **
   140    140   ** Populate the low-level memory allocation function pointers in
   141         -** sqlite3Config.m with pointers to the routines in this file.
          141  +** sqlite3GlobalConfig.m with pointers to the routines in this file.
   142    142   */
   143    143   void sqlite3MemSetDefault(void){
   144    144     sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetDefault());
   145    145   }
   146    146   
   147    147   #endif /* SQLITE_SYSTEM_MALLOC */

Changes to src/mem2.c.

    15     15   ** to obtain the memory it needs while adding lots of additional debugging
    16     16   ** information to each allocation in order to help detect and fix memory
    17     17   ** leaks and memory usage errors.
    18     18   **
    19     19   ** This file contains implementations of the low-level memory allocation
    20     20   ** routines specified in the sqlite3_mem_methods object.
    21     21   **
    22         -** $Id: mem2.c,v 1.38 2008/08/12 15:04:59 danielk1977 Exp $
           22  +** $Id: mem2.c,v 1.39 2008/09/01 18:34:20 danielk1977 Exp $
    23     23   */
    24     24   #include "sqliteInt.h"
    25     25   
    26     26   /*
    27     27   ** This version of the memory allocator is used only if the
    28     28   ** SQLITE_MEMDEBUG macro is defined
    29     29   */
................................................................................
   181    181     return pHdr->iSize;
   182    182   }
   183    183   
   184    184   /*
   185    185   ** Initialize the memory allocation subsystem.
   186    186   */
   187    187   static int sqlite3MemInit(void *NotUsed){
   188         -  if( !sqlite3Config.bMemstat ){
          188  +  if( !sqlite3GlobalConfig.bMemstat ){
   189    189       /* If memory status is enabled, then the malloc.c wrapper will already
   190    190       ** hold the STATIC_MEM mutex when the routines here are invoked. */
   191    191       mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   192    192     }
   193    193     return SQLITE_OK;
   194    194   }
   195    195   
................................................................................
   266    266   /*
   267    267   ** Free memory.
   268    268   */
   269    269   static void sqlite3MemFree(void *pPrior){
   270    270     struct MemBlockHdr *pHdr;
   271    271     void **pBt;
   272    272     char *z;
   273         -  assert( sqlite3Config.bMemstat || mem.mutex!=0 );
          273  +  assert( sqlite3GlobalConfig.bMemstat || mem.mutex!=0 );
   274    274     pHdr = sqlite3MemsysGetHeader(pPrior);
   275    275     pBt = (void**)pHdr;
   276    276     pBt -= pHdr->nBacktraceSlots;
   277    277     sqlite3_mutex_enter(mem.mutex);
   278    278     if( pHdr->pPrev ){
   279    279       assert( pHdr->pPrev->pNext==pHdr );
   280    280       pHdr->pPrev->pNext = pHdr->pNext;
................................................................................
   336    336        0
   337    337     };
   338    338     return &defaultMethods;
   339    339   }
   340    340   
   341    341   /*
   342    342   ** Populate the low-level memory allocation function pointers in
   343         -** sqlite3Config.m with pointers to the routines in this file.
          343  +** sqlite3GlobalConfig.m with pointers to the routines in this file.
   344    344   */
   345    345   void sqlite3MemSetDefault(void){
   346    346     sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetDefault());
   347    347   }
   348    348   
   349    349   /*
   350    350   ** Set the number of backtrace levels kept for each allocation.

Changes to src/mem3.c.

    19     19   ** implementations. Once sqlite3_initialize() has been called,
    20     20   ** the amount of memory available to SQLite is fixed and cannot
    21     21   ** be changed.
    22     22   **
    23     23   ** This version of the memory allocation subsystem is included
    24     24   ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
    25     25   **
    26         -** $Id: mem3.c,v 1.20 2008/07/18 18:56:17 drh Exp $
           26  +** $Id: mem3.c,v 1.21 2008/09/01 18:34:20 danielk1977 Exp $
    27     27   */
    28     28   #include "sqliteInt.h"
    29     29   
    30     30   /*
    31     31   ** This version of the memory allocator is only built into the library
    32     32   ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
    33     33   ** mean that the library will use a memory-pool by default, just that
................................................................................
   213    213       memsys3LinkIntoList(i, &mem3.aiHash[hash]);
   214    214     }
   215    215   }
   216    216   
   217    217   /*
   218    218   ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
   219    219   ** will already be held (obtained by code in malloc.c) if
   220         -** sqlite3Config.bMemStat is true.
          220  +** sqlite3GlobalConfig.bMemStat is true.
   221    221   */
   222    222   static void memsys3Enter(void){
   223         -  if( sqlite3Config.bMemstat==0 && mem3.mutex==0 ){
          223  +  if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
   224    224       mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   225    225     }
   226    226     sqlite3_mutex_enter(mem3.mutex);
   227    227   }
   228    228   static void memsys3Leave(void){
   229    229     sqlite3_mutex_leave(mem3.mutex);
   230    230   }
................................................................................
   546    546     return p;
   547    547   }
   548    548   
   549    549   /*
   550    550   ** Initialize this module.
   551    551   */
   552    552   static int memsys3Init(void *NotUsed){
   553         -  if( !sqlite3Config.pHeap ){
          553  +  if( !sqlite3GlobalConfig.pHeap ){
   554    554       return SQLITE_ERROR;
   555    555     }
   556    556   
   557    557     /* Store a pointer to the memory block in global structure mem3. */
   558    558     assert( sizeof(Mem3Block)==8 );
   559         -  mem3.aPool = (Mem3Block *)sqlite3Config.pHeap;
   560         -  mem3.nPool = (sqlite3Config.nHeap / sizeof(Mem3Block)) - 2;
          559  +  mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
          560  +  mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
   561    561   
   562    562     /* Initialize the master block. */
   563    563     mem3.szMaster = mem3.nPool;
   564    564     mem3.mnMaster = mem3.szMaster;
   565    565     mem3.iMaster = 1;
   566    566     mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
   567    567     mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
................................................................................
   655    655   #endif
   656    656   
   657    657   /*
   658    658   ** This routine is the only routine in this file with external 
   659    659   ** linkage.
   660    660   **
   661    661   ** Populate the low-level memory allocation function pointers in
   662         -** sqlite3Config.m with pointers to the routines in this file. The
          662  +** sqlite3GlobalConfig.m with pointers to the routines in this file. The
   663    663   ** arguments specify the block of memory to manage.
   664    664   **
   665    665   ** This routine is only called by sqlite3_config(), and therefore
   666    666   ** is not required to be threadsafe (it is not).
   667    667   */
   668    668   const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
   669    669     static const sqlite3_mem_methods mempoolMethods = {

Changes to src/mem5.c.

    19     19   ** implementations. Once sqlite3_initialize() has been called,
    20     20   ** the amount of memory available to SQLite is fixed and cannot
    21     21   ** be changed.
    22     22   **
    23     23   ** This version of the memory allocation subsystem is included
    24     24   ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
    25     25   **
    26         -** $Id: mem5.c,v 1.11 2008/07/16 12:25:32 drh Exp $
           26  +** $Id: mem5.c,v 1.12 2008/09/01 18:34:20 danielk1977 Exp $
    27     27   */
    28     28   #include "sqliteInt.h"
    29     29   
    30     30   /*
    31     31   ** This version of the memory allocator is used only when 
    32     32   ** SQLITE_POW2_MEMORY_SIZE is defined.
    33     33   */
................................................................................
   177    177     }
   178    178     mem5.aiFreelist[iLogsize] = i;
   179    179   }
   180    180   
   181    181   /*
   182    182   ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
   183    183   ** will already be held (obtained by code in malloc.c) if
   184         -** sqlite3Config.bMemStat is true.
          184  +** sqlite3GlobalConfig.bMemStat is true.
   185    185   */
   186    186   static void memsys5Enter(void){
   187         -  if( sqlite3Config.bMemstat==0 && mem5.mutex==0 ){
          187  +  if( sqlite3GlobalConfig.bMemstat==0 && mem5.mutex==0 ){
   188    188       mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   189    189     }
   190    190     sqlite3_mutex_enter(mem5.mutex);
   191    191   }
   192    192   static void memsys5Leave(void){
   193    193     sqlite3_mutex_leave(mem5.mutex);
   194    194   }
................................................................................
   402    402   }
   403    403   
   404    404   /*
   405    405   ** Initialize this module.
   406    406   */
   407    407   static int memsys5Init(void *NotUsed){
   408    408     int ii;
   409         -  int nByte = sqlite3Config.nHeap;
   410         -  u8 *zByte = (u8 *)sqlite3Config.pHeap;
          409  +  int nByte = sqlite3GlobalConfig.nHeap;
          410  +  u8 *zByte = (u8 *)sqlite3GlobalConfig.pHeap;
   411    411     int nMinLog;                 /* Log of minimum allocation size in bytes*/
   412    412     int iOffset;
   413    413   
   414    414     if( !zByte ){
   415    415       return SQLITE_ERROR;
   416    416     }
   417    417   
   418         -  nMinLog = memsys5Log(sqlite3Config.mnReq);
          418  +  nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
   419    419     mem5.nAtom = (1<<nMinLog);
   420    420     while( sizeof(Mem5Link)>mem5.nAtom ){
   421    421       mem5.nAtom = mem5.nAtom << 1;
   422    422     }
   423    423   
   424    424     mem5.nBlock = (nByte / (mem5.nAtom+sizeof(u8)));
   425    425     mem5.zPool = zByte;

Changes to src/mem6.c.

    28     28   ** the same as that used by mem5.c. 
    29     29   **
    30     30   ** This strategy is designed to prevent the default memory allocation
    31     31   ** system (usually the system malloc) from suffering from heap 
    32     32   ** fragmentation. On some systems, heap fragmentation can cause a 
    33     33   ** significant real-time slowdown.
    34     34   **
    35         -** $Id: mem6.c,v 1.7 2008/07/28 19:34:53 drh Exp $
           35  +** $Id: mem6.c,v 1.8 2008/09/01 18:34:20 danielk1977 Exp $
    36     36   */
    37     37   
    38     38   #ifdef SQLITE_ENABLE_MEMSYS6
    39     39   
    40     40   #include "sqliteInt.h"
    41     41   
    42     42   /*
................................................................................
   452    452       return n;
   453    453     }else{
   454    454       return (1<<roundupLog2(n));
   455    455     }
   456    456   }
   457    457   
   458    458   static int memsys6Init(void *pCtx){
   459         -  u8 bMemstat = sqlite3Config.bMemstat;
          459  +  u8 bMemstat = sqlite3GlobalConfig.bMemstat;
   460    460     mem6.nMinAlloc = (1 << LOG2_MINALLOC);
   461    461     mem6.pChunk = 0;
   462         -  mem6.nThreshold = sqlite3Config.nSmall;
          462  +  mem6.nThreshold = sqlite3GlobalConfig.nSmall;
   463    463     if( mem6.nThreshold<=0 ){
   464    464       mem6.nThreshold = SMALL_MALLOC_DEFAULT_THRESHOLD;
   465    465     }
   466    466     mem6.nLogThreshold = roundupLog2(mem6.nThreshold) - LOG2_MINALLOC;
   467    467     if( !bMemstat ){
   468    468       mem6.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   469    469     }

Changes to src/mutex.c.

    15     15   ** exclusion and is thus suitable for use only in applications
    16     16   ** that use SQLite in a single thread.  But this implementation
    17     17   ** does do a lot of error checking on mutexes to make sure they
    18     18   ** are called correctly and at appropriate times.  Hence, this
    19     19   ** implementation is suitable for testing.
    20     20   ** debugging purposes
    21     21   **
    22         -** $Id: mutex.c,v 1.27 2008/06/19 08:51:24 danielk1977 Exp $
           22  +** $Id: mutex.c,v 1.28 2008/09/01 18:34:20 danielk1977 Exp $
    23     23   */
    24     24   #include "sqliteInt.h"
    25     25   
    26     26   #ifndef SQLITE_MUTEX_NOOP
    27     27   /*
    28     28   ** Initialize the mutex system.
    29     29   */
    30     30   int sqlite3MutexInit(void){ 
    31     31     int rc = SQLITE_OK;
    32         -  if( sqlite3Config.bCoreMutex ){
    33         -    if( !sqlite3Config.mutex.xMutexAlloc ){
           32  +  if( sqlite3GlobalConfig.bCoreMutex ){
           33  +    if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
    34     34         /* If the xMutexAlloc method has not been set, then the user did not
    35     35         ** install a mutex implementation via sqlite3_config() prior to 
    36     36         ** sqlite3_initialize() being called. This block copies pointers to
    37         -      ** the default implementation into the sqlite3Config structure.
           37  +      ** the default implementation into the sqlite3GlobalConfig structure.
    38     38         **
    39     39         ** The danger is that although sqlite3_config() is not a threadsafe
    40     40         ** API, sqlite3_initialize() is, and so multiple threads may be
    41     41         ** attempting to run this function simultaneously. To guard write
    42         -      ** access to the sqlite3Config structure, the 'MASTER' static mutex
           42  +      ** access to the sqlite3GlobalConfig structure, the 'MASTER' static mutex
    43     43         ** is obtained before modifying it.
    44     44         */
    45     45         sqlite3_mutex_methods *p = sqlite3DefaultMutex();
    46     46         sqlite3_mutex *pMaster = 0;
    47     47     
    48     48         rc = p->xMutexInit();
    49     49         if( rc==SQLITE_OK ){
    50     50           pMaster = p->xMutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
    51     51           assert(pMaster);
    52     52           p->xMutexEnter(pMaster);
    53         -        assert( sqlite3Config.mutex.xMutexAlloc==0 
    54         -             || sqlite3Config.mutex.xMutexAlloc==p->xMutexAlloc
           53  +        assert( sqlite3GlobalConfig.mutex.xMutexAlloc==0 
           54  +             || sqlite3GlobalConfig.mutex.xMutexAlloc==p->xMutexAlloc
    55     55           );
    56         -        if( !sqlite3Config.mutex.xMutexAlloc ){
    57         -          sqlite3Config.mutex = *p;
           56  +        if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
           57  +          sqlite3GlobalConfig.mutex = *p;
    58     58           }
    59     59           p->xMutexLeave(pMaster);
    60     60         }
    61     61       }else{
    62         -      rc = sqlite3Config.mutex.xMutexInit();
           62  +      rc = sqlite3GlobalConfig.mutex.xMutexInit();
    63     63       }
    64     64     }
    65     65   
    66     66     return rc;
    67     67   }
    68     68   
    69     69   /*
    70     70   ** Shutdown the mutex system. This call frees resources allocated by
    71     71   ** sqlite3MutexInit().
    72     72   */
    73     73   int sqlite3MutexEnd(void){
    74     74     int rc = SQLITE_OK;
    75         -  rc = sqlite3Config.mutex.xMutexEnd();
           75  +  rc = sqlite3GlobalConfig.mutex.xMutexEnd();
    76     76     return rc;
    77     77   }
    78     78   
    79     79   /*
    80     80   ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
    81     81   */
    82     82   sqlite3_mutex *sqlite3_mutex_alloc(int id){
    83     83   #ifndef SQLITE_OMIT_AUTOINIT
    84     84     if( sqlite3_initialize() ) return 0;
    85     85   #endif
    86         -  return sqlite3Config.mutex.xMutexAlloc(id);
           86  +  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
    87     87   }
    88     88   
    89     89   sqlite3_mutex *sqlite3MutexAlloc(int id){
    90         -  if( !sqlite3Config.bCoreMutex ){
           90  +  if( !sqlite3GlobalConfig.bCoreMutex ){
    91     91       return 0;
    92     92     }
    93         -  return sqlite3Config.mutex.xMutexAlloc(id);
           93  +  return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
    94     94   }
    95     95   
    96     96   /*
    97     97   ** Free a dynamic mutex.
    98     98   */
    99     99   void sqlite3_mutex_free(sqlite3_mutex *p){
   100    100     if( p ){
   101         -    sqlite3Config.mutex.xMutexFree(p);
          101  +    sqlite3GlobalConfig.mutex.xMutexFree(p);
   102    102     }
   103    103   }
   104    104   
   105    105   /*
   106    106   ** Obtain the mutex p. If some other thread already has the mutex, block
   107    107   ** until it can be obtained.
   108    108   */
   109    109   void sqlite3_mutex_enter(sqlite3_mutex *p){
   110    110     if( p ){
   111         -    sqlite3Config.mutex.xMutexEnter(p);
          111  +    sqlite3GlobalConfig.mutex.xMutexEnter(p);
   112    112     }
   113    113   }
   114    114   
   115    115   /*
   116    116   ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
   117    117   ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
   118    118   */
   119    119   int sqlite3_mutex_try(sqlite3_mutex *p){
   120    120     int rc = SQLITE_OK;
   121    121     if( p ){
   122         -    return sqlite3Config.mutex.xMutexTry(p);
          122  +    return sqlite3GlobalConfig.mutex.xMutexTry(p);
   123    123     }
   124    124     return rc;
   125    125   }
   126    126   
   127    127   /*
   128    128   ** The sqlite3_mutex_leave() routine exits a mutex that was previously
   129    129   ** entered by the same thread.  The behavior is undefined if the mutex 
   130    130   ** is not currently entered. If a NULL pointer is passed as an argument
   131    131   ** this function is a no-op.
   132    132   */
   133    133   void sqlite3_mutex_leave(sqlite3_mutex *p){
   134    134     if( p ){
   135         -    sqlite3Config.mutex.xMutexLeave(p);
          135  +    sqlite3GlobalConfig.mutex.xMutexLeave(p);
   136    136     }
   137    137   }
   138    138   
   139    139   #ifndef NDEBUG
   140    140   /*
   141    141   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   142    142   ** intended for use inside assert() statements.
   143    143   */
   144    144   int sqlite3_mutex_held(sqlite3_mutex *p){
   145         -  return p==0 || sqlite3Config.mutex.xMutexHeld(p);
          145  +  return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
   146    146   }
   147    147   int sqlite3_mutex_notheld(sqlite3_mutex *p){
   148         -  return p==0 || sqlite3Config.mutex.xMutexNotheld(p);
          148  +  return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
   149    149   }
   150    150   #endif
   151    151   
   152    152   #endif
   153    153   
   154    154   #ifdef SQLITE_MUTEX_NOOP_DEBUG
   155    155   /*

Changes to src/pcache.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file implements that page cache.
    13     13   **
    14         -** @(#) $Id: pcache.c,v 1.24 2008/08/29 09:10:03 danielk1977 Exp $
           14  +** @(#) $Id: pcache.c,v 1.25 2008/09/01 18:34:20 danielk1977 Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   /*
    19     19   ** A complete page cache is an instance of this structure.
    20     20   **
    21     21   ** A cache may only be deleted by its owner and while holding the
................................................................................
   602    602   **
   603    603   ** Initialize and shutdown the page cache subsystem. Neither of these 
   604    604   ** functions are threadsafe.
   605    605   */
   606    606   int sqlite3PcacheInitialize(void){
   607    607     assert( pcache.isInit==0 );
   608    608     memset(&pcache, 0, sizeof(pcache));
   609         -  if( sqlite3Config.bCoreMutex ){
          609  +  if( sqlite3GlobalConfig.bCoreMutex ){
   610    610       /* No need to check the return value of sqlite3_mutex_alloc(). 
   611    611       ** Allocating a static mutex cannot fail.
   612    612       */
   613    613       pcache.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
   614    614     }
   615    615     pcache.isInit = 1;
   616    616     return SQLITE_OK;

Changes to src/sqlite.h.in.

    26     26   ** on how SQLite interfaces are suppose to operate.
    27     27   **
    28     28   ** The name of this file under configuration management is "sqlite.h.in".
    29     29   ** The makefile makes some minor changes to this file (such as inserting
    30     30   ** the version number) and changes its name to "sqlite3.h" as
    31     31   ** part of the build process.
    32     32   **
    33         -** @(#) $Id: sqlite.h.in,v 1.394 2008/08/25 21:23:02 drh Exp $
           33  +** @(#) $Id: sqlite.h.in,v 1.395 2008/09/01 18:34:20 danielk1977 Exp $
    34     34   */
    35     35   #ifndef _SQLITE3_H_
    36     36   #define _SQLITE3_H_
    37     37   #include <stdarg.h>     /* Needed for the definition of va_list */
    38     38   
    39     39   /*
    40     40   ** Make sure we can call this stuff from C++.
................................................................................
  6193   6193   ** the resetFlg is true, then the highest instantaneous value is
  6194   6194   ** reset back down to the current value.
  6195   6195   **
  6196   6196   ** See also: [sqlite3_status()].
  6197   6197   */
  6198   6198   SQLITE_EXPERIMENTAL int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
  6199   6199   
         6200  +
         6201  +int sqlite3_wsd_init(int N, int J);
         6202  +void *sqlite3_wsd_find(void *K, int L);
         6203  +
  6200   6204   /*
  6201   6205   ** CAPI3REF: Status Parameters {H17250} <H17200>
  6202   6206   ** EXPERIMENTAL
  6203   6207   **
  6204   6208   ** These integer constants designate various run-time status parameters
  6205   6209   ** that can be returned by [sqlite3_status()].
  6206   6210   **

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.765 2008/09/01 15:52:11 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.766 2008/09/01 18:34:20 danielk1977 Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Include the configuration header output by 'configure' if we're using the
    21     21   ** autoconf-based build
................................................................................
   436    436   #define ArraySize(X)    (sizeof(X)/sizeof(X[0]))
   437    437   
   438    438   /*
   439    439   ** The following value as a destructor means to use sqlite3DbFree().
   440    440   ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
   441    441   */
   442    442   #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3DbFree)
          443  +
          444  +#ifdef SQLITE_OMIT_WSD
          445  +  #define SQLITE_WSD const
          446  +  #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
          447  +  #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
          448  +#else
          449  +  #define SQLITE_WSD 
          450  +  #define GLOBAL(t,v) v
          451  +  #define sqlite3GlobalConfig sqlite3Config
          452  +#endif
   443    453   
   444    454   /*
   445    455   ** Forward references to structures
   446    456   */
   447    457   typedef struct AggInfo AggInfo;
   448    458   typedef struct AuthContext AuthContext;
   449    459   typedef struct Bitvec Bitvec;
................................................................................
  2313   2323   void sqlite3ValueFree(sqlite3_value*);
  2314   2324   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  2315   2325   char *sqlite3Utf16to8(sqlite3 *, const void*, int);
  2316   2326   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  2317   2327   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  2318   2328   #ifndef SQLITE_AMALGAMATION
  2319   2329   extern const unsigned char sqlite3UpperToLower[];
  2320         -extern struct Sqlite3Config sqlite3Config;
  2321         -extern FuncDefHash sqlite3GlobalFunctions;
         2330  +extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
         2331  +extern SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  2322   2332   #endif
  2323   2333   void sqlite3RootPageMoved(Db*, int, int);
  2324   2334   void sqlite3Reindex(Parse*, Token*, Token*);
  2325   2335   void sqlite3AlterFunctions(sqlite3*);
  2326   2336   void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
  2327   2337   int sqlite3GetToken(const unsigned char *, int *);
  2328   2338   void sqlite3NestedParse(Parse*, const char*, ...);

Added src/test_wsd.c.

            1  +/*
            2  +** 2008 September 1
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** The code in this file contains sample implementations of the 
           14  +** sqlite3_wsd_init() and sqlite3_wsd_find() functions required if the
           15  +** SQLITE_OMIT_WSD symbol is defined at build time.
           16  +**
           17  +** $Id: test_wsd.c,v 1.1 2008/09/01 18:34:20 danielk1977 Exp $
           18  +*/
           19  +
           20  +#if defined(SQLITE_OMIT_WSD) && defined(SQLITE_TEST)
           21  +
           22  +#include "sqliteInt.h"
           23  +
           24  +#define PLS_HASHSIZE 43
           25  +
           26  +typedef struct ProcessLocalStorage ProcessLocalStorage;
           27  +typedef struct ProcessLocalVar ProcessLocalVar;
           28  +
           29  +struct ProcessLocalStorage {
           30  +  ProcessLocalVar *aData[PLS_HASHSIZE];
           31  +  int nFree;
           32  +  u8 *pFree;
           33  +};
           34  +
           35  +struct ProcessLocalVar {
           36  +  void *pKey;
           37  +  ProcessLocalVar *pNext;
           38  +};
           39  +
           40  +static ProcessLocalStorage *pGlobal = 0;
           41  +
           42  +int sqlite3_wsd_init(int N, int J){
           43  +  if( !pGlobal ){
           44  +    int nMalloc = N + sizeof(ProcessLocalStorage) + J*sizeof(ProcessLocalVar);
           45  +    pGlobal = (ProcessLocalStorage *)malloc(nMalloc);
           46  +    if( pGlobal ){
           47  +      memset(pGlobal, 0, sizeof(ProcessLocalStorage));
           48  +      pGlobal->nFree = nMalloc - sizeof(ProcessLocalStorage);
           49  +      pGlobal->pFree = (u8 *)&pGlobal[1];
           50  +    }
           51  +  }
           52  +
           53  +  return pGlobal ? SQLITE_OK : SQLITE_NOMEM;
           54  +}
           55  +
           56  +void *sqlite3_wsd_find(void *K, int L){
           57  +  int i;
           58  +  int iHash = 0;
           59  +  ProcessLocalVar *pVar;
           60  +
           61  +  /* Calculate a hash of K */
           62  +  for(i=0; i<sizeof(void*); i++){
           63  +    iHash = (iHash<<3) + ((unsigned char *)K)[i];
           64  +  }
           65  +  iHash = iHash%PLS_HASHSIZE;
           66  +
           67  +  /* Search the hash table for K. */
           68  +  for(pVar=pGlobal->aData[iHash]; pVar && pVar->pKey!=K; pVar=pVar->pNext);
           69  +
           70  +  /* If no entry for K was found, create and populate a new one. */
           71  +  if( !pVar ){
           72  +    int nByte = (sizeof(ProcessLocalVar) + L + 7)&~7;
           73  +    assert( pGlobal->nFree>=nByte );
           74  +    pVar = (ProcessLocalVar *)pGlobal->pFree;
           75  +    pVar->pKey = K;
           76  +    pVar->pNext = pGlobal->aData[iHash];
           77  +    pGlobal->aData[iHash] = pVar;
           78  +    pGlobal->nFree -= nByte;
           79  +    pGlobal->pFree += nByte;
           80  +    memcpy(&pVar[1], K, L);
           81  +  }
           82  +
           83  +  return (void *)&pVar[1];
           84  +}
           85  +
           86  +#endif
           87  +