Diff
Not logged in

Differences From:

File src/sqlite3.c part of check-in [d8590e093f] - Merge in the latest SQLite updates. by drh on 2007-07-24 12:54:24. [view]

To:

File src/sqlite3.c part of check-in [c287665ba8] - Update the SQLite amalgamation file to version 3.5.0. by drh on 2007-09-14 14:59:38. Also file src/sqlite3.c part of check-in [f76192b245] - Pulled the latest CLI, website, and sqlite changes into the importer branch. by aku on 2007-09-17 01:00:32. [view]

@@ -1,7 +1,7 @@
 /******************************************************************************
 ** This file is an amalgamation of many separate C source files from SQLite
-** version 3.4.1.  By combining all the individual C code files into this
+** version 3.5.0.  By combining all the individual C code files into this
 ** single large file, the entire code can be compiled as a one translation
 ** unit.  This allows many compilers to do optimizations that would not be
 ** possible if the files were compiled separately.  Performance improvements
 ** of 5% are more are commonly seen when SQLite is compiled as a single
@@ -10,15 +10,15 @@
 ** This file is all you need to compile SQLite.  To use SQLite in other
 ** programs, you need this file and the "sqlite3.h" header file that defines
 ** the programming interface to the SQLite library.  (If you do not have
 ** the "sqlite3.h" header file at hand, you will find a copy in the first
-** 2702 lines past this header comment.)  Additional code files may be
+** 3539 lines past this header comment.)  Additional code files may be
 ** needed if you want a wrapper to interface SQLite with your choice of
 ** programming language.  The code for the "sqlite3" command-line shell
 ** is also in a separate file.  This file contains only code for the core
 ** SQLite library.
 **
-** This amalgamation was generated on 2007-07-24 12:53:26 UTC.
+** This amalgamation was generated on 2007-09-14 14:58:10 UTC.
 */
 #define SQLITE_AMALGAMATION 1
 #ifndef SQLITE_PRIVATE
 # define SQLITE_PRIVATE static
@@ -58,9 +58,9 @@
 ** The makefile makes some minor changes to this file (such as inserting
 ** the version number) and changes its name to "sqlite3.h" as
 ** part of the build process.
 **
-** @(#) $Id: sqlite.h.in,v 1.218 2007/07/19 12:41:40 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.259 2007/09/04 22:31:37 drh Exp $
 */
 #ifndef _SQLITE3_H_
 #define _SQLITE3_H_
 #include <stdarg.h>     /* Needed for the definition of va_list */
@@ -69,8 +69,16 @@
 ** Make sure we can call this stuff from C++.
 */
 #if 0
 extern "C" {
+#endif
+
+
+/*
+** Add the ability to override 'extern'
+*/
+#ifndef SQLITE_EXTERN
+# define SQLITE_EXTERN extern
 #endif
 
 /*
 ** Make sure these symbols where not defined by some previous header
@@ -109,10 +117,10 @@
 ** (SQLITE_VERSION_NUMBER>=3001001).
 **
 ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()].
 */
-#define SQLITE_VERSION         "3.4.1"
-#define SQLITE_VERSION_NUMBER 3004001
+#define SQLITE_VERSION         "3.5.0"
+#define SQLITE_VERSION_NUMBER 3005000
 
 /*
 ** CAPI3REF: Run-Time Library Version Numbers
 **
@@ -128,21 +136,46 @@
 ** a poiner to the sqlite3_version[] string constant.  The function
 ** is provided for DLL users who can only access functions and not
 ** constants within the DLL.
 */
-extern const char sqlite3_version[];
+SQLITE_EXTERN const char sqlite3_version[];
 SQLITE_API const char *sqlite3_libversion(void);
-int sqlite3_libversion_number(void);
+SQLITE_API int sqlite3_libversion_number(void);
+
+/*
+** CAPI3REF: Test To See If The Library Is Threadsafe
+**
+** This routine returns TRUE (nonzero) if SQLite was compiled with
+** all of its mutexes enabled and is thus threadsafe.  It returns
+** zero if the particular build is for single-threaded operation
+** only.
+**
+** Really all this routine does is return true if SQLite was compiled
+** with the -DSQLITE_THREADSAFE=1 option and false if
+** compiled with -DSQLITE_THREADSAFE=0.  If SQLite uses an
+** application-defined mutex subsystem, malloc subsystem, collating
+** sequence, VFS, SQL function, progress callback, commit hook,
+** extension, or other accessories and these add-ons are not
+** threadsafe, then clearly the combination will not be threadsafe
+** either.  Hence, this routine never reports that the library
+** is guaranteed to be threadsafe, only when it is guaranteed not
+** to be.
+**
+** This is an experimental API and may go away or change in future
+** releases.
+*/
+SQLITE_API int sqlite3_threadsafe(void);
 
 /*
 ** CAPI3REF: Database Connection Handle
 **
 ** Each open SQLite database is represented by pointer to an instance of the
 ** opaque structure named "sqlite3".  It is useful to think of an sqlite3
-** pointer as an object.  The [sqlite3_open] interface is its constructor
-** and [sqlite3_close] is its destructor.  There are many other interfaces
-** (such as [sqlite3_prepare_v2], [sqlite3_create_function], and
-** [sqlite3_busy_timeout] to name but three) that are methods on this
+** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
+** [sqlite3_open_v2()] interfaces are its constructors
+** and [sqlite3_close()] is its destructor.  There are many other interfaces
+** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and
+** [sqlite3_busy_timeout()] to name but three) that are methods on this
 ** object.
 */
 typedef struct sqlite3 sqlite3;
 
@@ -165,28 +198,37 @@
 #else
   typedef long long int sqlite_int64;
   typedef unsigned long long int sqlite_uint64;
 #endif
+typedef sqlite_int64 sqlite3_int64;
+typedef sqlite_uint64 sqlite3_uint64;
 
 /*
 ** If compiling for a processor that lacks floating point support,
 ** substitute integer for floating-point
 */
 #ifdef SQLITE_OMIT_FLOATING_POINT
-# define double sqlite_int64
+# define double sqlite3_int64
 #endif
 
 /*
 ** CAPI3REF: Closing A Database Connection
 **
 ** Call this function with a pointer to a structure that was previously
-** returned from [sqlite3_open()] and the corresponding database will by
+** returned from [sqlite3_open()], [sqlite3_open16()], or
+** [sqlite3_open_v2()] and the corresponding database will by
 ** closed.
 **
 ** All SQL statements prepared using [sqlite3_prepare_v2()] or
 ** [sqlite3_prepare16_v2()] must be destroyed using [sqlite3_finalize()]
 ** before this routine is called. Otherwise, SQLITE_BUSY is returned and the
 ** database connection remains open.
+**
+** Passing this routine a database connection that has already been
+** closed results in undefined behavior.  If other interfaces that
+** reference the same database connection are pending (either in the
+** same thread or in different threads) when this routine is called,
+** then the behavior is undefined and is almost certainly undesirable.
 */
 SQLITE_API int sqlite3_close(sqlite3 *);
 
 /*
@@ -209,9 +251,9 @@
 ** the callback function specified by the 3rd parameter is
 ** invoked once for each row of the query result.  This callback
 ** should normally return 0.  If the callback returns a non-zero
 ** value then the query is aborted, all subsequent SQL statements
-** are skipped and the sqlite3_exec() function returns the SQLITE_ABORT.
+** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT].
 **
 ** The 4th parameter to this interface is an arbitrary pointer that is
 ** passed through to the callback function as its first parameter.
 **
@@ -230,11 +272,10 @@
 ** If an error occurs while parsing or evaluating the SQL (but
 ** not while executing the callback) then an appropriate error
 ** message is written into memory obtained from [sqlite3_malloc()] and
 ** *errmsg is made to point to that message.  The calling function
-** is responsible for freeing the memory that holds the error
-** message.   Use [sqlite3_free()] for this.  If errmsg==NULL,
-** then no error message is ever written.
+** is responsible for freeing the memory using [sqlite3_free()].
+** If errmsg==NULL, then no error message is ever written.
 **
 ** The return value is is SQLITE_OK if there are no errors and
 ** some other [SQLITE_OK | return code] if there is an error.
 ** The particular return value depends on the type of error.
@@ -334,8 +375,378 @@
 #define SQLITE_IOERR_DELETE        (SQLITE_IOERR | (10<<8))
 #define SQLITE_IOERR_BLOCKED       (SQLITE_IOERR | (11<<8))
 
 /*
+** CAPI3REF: Flags For File Open Operations
+**
+** Combination of the following bit values are used as the
+** third argument to the [sqlite3_open_v2()] interface and
+** as fourth argument to the xOpen method of the
+** [sqlite3_vfs] object.
+**
+*/
+#define SQLITE_OPEN_READONLY         0x00000001
+#define SQLITE_OPEN_READWRITE        0x00000002
+#define SQLITE_OPEN_CREATE           0x00000004
+#define SQLITE_OPEN_DELETEONCLOSE    0x00000008
+#define SQLITE_OPEN_EXCLUSIVE        0x00000010
+#define SQLITE_OPEN_MAIN_DB          0x00000100
+#define SQLITE_OPEN_TEMP_DB          0x00000200
+#define SQLITE_OPEN_TRANSIENT_DB     0x00000400
+#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800
+#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000
+#define SQLITE_OPEN_SUBJOURNAL       0x00002000
+#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000
+
+/*
+** CAPI3REF: Device Characteristics
+**
+** The xDeviceCapabilities method of the [sqlite3_io_methods]
+** object returns an integer which is a vector of the following
+** bit values expressing I/O characteristics of the mass storage
+** device that holds the file that the [sqlite3_io_methods]
+** refers to.
+**
+** The SQLITE_IOCAP_ATOMIC property means that all writes of
+** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
+** mean that writes of blocks that are nnn bytes in size and
+** are aligned to an address which is an integer multiple of
+** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
+** that when data is appended to a file, the data is appended
+** first then the size of the file is extended, never the other
+** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
+** information is written to disk in the same order as calls
+** to xWrite().
+*/
+#define SQLITE_IOCAP_ATOMIC          0x00000001
+#define SQLITE_IOCAP_ATOMIC512       0x00000002
+#define SQLITE_IOCAP_ATOMIC1K        0x00000004
+#define SQLITE_IOCAP_ATOMIC2K        0x00000008
+#define SQLITE_IOCAP_ATOMIC4K        0x00000010
+#define SQLITE_IOCAP_ATOMIC8K        0x00000020
+#define SQLITE_IOCAP_ATOMIC16K       0x00000040
+#define SQLITE_IOCAP_ATOMIC32K       0x00000080
+#define SQLITE_IOCAP_ATOMIC64K       0x00000100
+#define SQLITE_IOCAP_SAFE_APPEND     0x00000200
+#define SQLITE_IOCAP_SEQUENTIAL      0x00000400
+
+/*
+** CAPI3REF: File Locking Levels
+**
+** SQLite uses one of the following integer values as the second
+** argument to calls it makes to the xLock() and xUnlock() methods
+** of an [sqlite3_io_methods] object.
+*/
+#define SQLITE_LOCK_NONE          0
+#define SQLITE_LOCK_SHARED        1
+#define SQLITE_LOCK_RESERVED      2
+#define SQLITE_LOCK_PENDING       3
+#define SQLITE_LOCK_EXCLUSIVE     4
+
+/*
+** CAPI3REF: Synchronization Type Flags
+**
+** When SQLite invokes the xSync() method of an [sqlite3_io_methods]
+** object it uses a combination of the following integer values as
+** the second argument.
+**
+** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
+** sync operation only needs to flush data to mass storage.  Inode
+** information need not be flushed.  The SQLITE_SYNC_NORMAL means
+** to use normal fsync() semantics.  The SQLITE_SYNC_FULL flag means
+** to use Mac OS-X style fullsync instead of fsync().
+*/
+#define SQLITE_SYNC_NORMAL        0x00002
+#define SQLITE_SYNC_FULL          0x00003
+#define SQLITE_SYNC_DATAONLY      0x00010
+
+
+/*
+** CAPI3REF: OS Interface Open File Handle
+**
+** An [sqlite3_file] object represents an open file in the OS
+** interface layer.  Individual OS interface implementations will
+** want to subclass this object by appending additional fields
+** for their own use.  The pMethods entry is a pointer to an
+** [sqlite3_io_methods] object that defines methods for performing
+** I/O operations on the open file.
+*/
+typedef struct sqlite3_file sqlite3_file;
+struct sqlite3_file {
+  const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
+};
+
+/*
+** CAPI3REF: OS Interface File Virtual Methods Object
+**
+** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to
+** an instance of the this object.  This object defines the
+** methods used to perform various operations against the open file.
+**
+** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
+** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
+*  The second choice is an
+** OS-X style fullsync.  The SQLITE_SYNC_DATA flag may be ORed in to
+** indicate that only the data of the file and not its inode needs to be
+** synced.
+**
+** The integer values to xLock() and xUnlock() are one of
+** <ul>
+** <li> [SQLITE_LOCK_NONE],
+** <li> [SQLITE_LOCK_SHARED],
+** <li> [SQLITE_LOCK_RESERVED],
+** <li> [SQLITE_LOCK_PENDING], or
+** <li> [SQLITE_LOCK_EXCLUSIVE].
+** </ul>
+** xLock() increases the lock. xUnlock() decreases the lock.
+** The xCheckReservedLock() method looks
+** to see if any database connection, either in this
+** process or in some other process, is holding an RESERVED,
+** PENDING, or EXCLUSIVE lock on the file.  It returns true
+** if such a lock exists and false if not.
+**
+** The xFileControl() method is a generic interface that allows custom
+** VFS implementations to directly control an open file using the
+** [sqlite3_file_control()] interface.  The second "op" argument
+** is an integer opcode.   The third
+** argument is a generic pointer which is intended to be a pointer
+** to a structure that may contain arguments or space in which to
+** write return values.  Potential uses for xFileControl() might be
+** functions to enable blocking locks with timeouts, to change the
+** locking strategy (for example to use dot-file locks), to inquire
+** about the status of a lock, or to break stale locks.  The SQLite
+** core reserves opcodes less than 100 for its own use.
+** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
+** Applications that define a custom xFileControl method should use opcodes
+** greater than 100 to avoid conflicts.
+**
+** The xSectorSize() method returns the sector size of the
+** device that underlies the file.  The sector size is the
+** minimum write that can be performed without disturbing
+** other bytes in the file.  The xDeviceCharacteristics()
+** method returns a bit vector describing behaviors of the
+** underlying device:
+**
+** <ul>
+** <li> [SQLITE_IOCAP_ATOMIC]
+** <li> [SQLITE_IOCAP_ATOMIC512]
+** <li> [SQLITE_IOCAP_ATOMIC1K]
+** <li> [SQLITE_IOCAP_ATOMIC2K]
+** <li> [SQLITE_IOCAP_ATOMIC4K]
+** <li> [SQLITE_IOCAP_ATOMIC8K]
+** <li> [SQLITE_IOCAP_ATOMIC16K]
+** <li> [SQLITE_IOCAP_ATOMIC32K]
+** <li> [SQLITE_IOCAP_ATOMIC64K]
+** <li> [SQLITE_IOCAP_SAFE_APPEND]
+** <li> [SQLITE_IOCAP_SEQUENTIAL]
+** </ul>
+**
+** The SQLITE_IOCAP_ATOMIC property means that all writes of
+** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
+** mean that writes of blocks that are nnn bytes in size and
+** are aligned to an address which is an integer multiple of
+** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
+** that when data is appended to a file, the data is appended
+** first then the size of the file is extended, never the other
+** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
+** information is written to disk in the same order as calls
+** to xWrite().
+*/
+typedef struct sqlite3_io_methods sqlite3_io_methods;
+struct sqlite3_io_methods {
+  int iVersion;
+  int (*xClose)(sqlite3_file*);
+  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
+  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
+  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
+  int (*xSync)(sqlite3_file*, int flags);
+  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
+  int (*xLock)(sqlite3_file*, int);
+  int (*xUnlock)(sqlite3_file*, int);
+  int (*xCheckReservedLock)(sqlite3_file*);
+  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
+  int (*xSectorSize)(sqlite3_file*);
+  int (*xDeviceCharacteristics)(sqlite3_file*);
+  /* Additional methods may be added in future releases */
+};
+
+/*
+** CAPI3REF: Standard File Control Opcodes
+**
+** These integer constants are opcodes for the xFileControl method
+** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()]
+** interface.
+**
+** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
+** opcode cases the xFileControl method to write the current state of
+** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
+** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
+** into an integer that the pArg argument points to.  This capability
+** is used during testing and only needs to be supported when SQLITE_TEST
+** is defined.
+*/
+#define SQLITE_FCNTL_LOCKSTATE        1
+
+/*
+** CAPI3REF: Mutex Handle
+**
+** The mutex module within SQLite defines [sqlite3_mutex] to be an
+** abstract type for a mutex object.  The SQLite core never looks
+** at the internal representation of an [sqlite3_mutex].  It only
+** deals with pointers to the [sqlite3_mutex] object.
+**
+** Mutexes are created using [sqlite3_mutex_alloc()].
+*/
+typedef struct sqlite3_mutex sqlite3_mutex;
+
+/*
+** CAPI3REF: OS Interface Object
+**
+** An instance of this object defines the interface between the
+** SQLite core and the underlying operating system.  The "vfs"
+** in the name of the object stands for "virtual file system".
+**
+** The iVersion field is initially 1 but may be larger for future
+** versions of SQLite.  Additional fields may be appended to this
+** object when the iVersion value is increased.
+**
+** The szOsFile field is the size of the subclassed [sqlite3_file]
+** structure used by this VFS.  mxPathname is the maximum length of
+** a pathname in this VFS.
+**
+** Registered vfs modules are kept on a linked list formed by
+** the pNext pointer.  The [sqlite3_vfs_register()]
+** and [sqlite3_vfs_unregister()] interfaces manage this list
+** in a thread-safe way.  The [sqlite3_vfs_find()] interface
+** searches the list.
+**
+** The pNext field is the only fields in the sqlite3_vfs
+** structure that SQLite will ever modify.  SQLite will only access
+** or modify this field while holding a particular static mutex.
+** The application should never modify anything within the sqlite3_vfs
+** object once the object has been registered.
+**
+** The zName field holds the name of the VFS module.  The name must
+** be unique across all VFS modules.
+**
+** SQLite will guarantee that the zFilename string passed to
+** xOpen() is a full pathname as generated by xFullPathname() and
+** that the string will be valid and unchanged until xClose() is
+** called.  So the [sqlite3_file] can store a pointer to the
+** filename if it needs to remember the filename for some reason.
+**
+** The flags argument to xOpen() is a copy of the flags argument
+** to [sqlite3_open_v2()].  If [sqlite3_open()] or [sqlite3_open16()]
+** is used, then flags is [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
+** If xOpen() opens a file read-only then it sets *pOutFlags to
+** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be
+** set.
+**
+** SQLite will also add one of the following flags to the xOpen()
+** call, depending on the object being opened:
+**
+** <ul>
+** <li>  [SQLITE_OPEN_MAIN_DB]
+** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
+** <li>  [SQLITE_OPEN_TEMP_DB]
+** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
+** <li>  [SQLITE_OPEN_TRANSIENT_DB]
+** <li>  [SQLITE_OPEN_SUBJOURNAL]
+** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
+** </ul>
+**
+** The file I/O implementation can use the object type flags to
+** changes the way it deals with files.  For example, an application
+** that does not care about crash recovery or rollback, might make
+** the open of a journal file a no-op.  Writes to this journal are
+** also a no-op.  Any attempt to read the journal return SQLITE_IOERR.
+** Or the implementation might recognize the a database file will
+** be doing page-aligned sector reads and writes in a random order
+** and set up its I/O subsystem accordingly.
+**
+** SQLite might also add one of the following flags to the xOpen
+** method:
+**
+** <ul>
+** <li> [SQLITE_OPEN_DELETEONCLOSE]
+** <li> [SQLITE_OPEN_EXCLUSIVE]
+** </ul>
+**
+** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
+** deleted when it is closed.  This will always be set for TEMP
+** databases and journals and for subjournals.  The
+** [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened
+** for exclusive access.  This flag is set for all files except
+** for the main database file.
+**
+** Space to hold the  [sqlite3_file] structure passed as the third
+** argument to xOpen is allocated by caller (the SQLite core).
+** szOsFile bytes are allocated for this object.  The xOpen method
+** fills in the allocated space.
+**
+** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
+** to test for the existance of a file,
+** or [SQLITE_ACCESS_READWRITE] to test to see
+** if a file is readable and writable, or [SQLITE_ACCESS_READ]
+** to test to see if a file is at least readable.  The file can be a
+** directory.
+**
+** SQLite will always allocate at least mxPathname+1 byte for
+** the output buffers for xGetTempName and xFullPathname.
+**
+** The xRandomness(), xSleep(), and xCurrentTime() interfaces
+** are not strictly a part of the filesystem, but they are
+** included in the VFS structure for completeness.
+** The xRandomness() function attempts to return nBytes bytes
+** of good-quality randomness into zOut.  The return value is
+** the actual number of bytes of randomness obtained.  The
+** xSleep() method cause the calling thread to sleep for at
+** least the number of microseconds given.  The xCurrentTime()
+** method returns a Julian Day Number for the current date and
+** time.
+*/
+typedef struct sqlite3_vfs sqlite3_vfs;
+struct sqlite3_vfs {
+  int iVersion;            /* Structure version number */
+  int szOsFile;            /* Size of subclassed sqlite3_file */
+  int mxPathname;          /* Maximum file pathname length */
+  sqlite3_vfs *pNext;      /* Next registered VFS */
+  const char *zName;       /* Name of this virtual file system */
+  void *pAppData;          /* Pointer to application-specific data */
+  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
+               int flags, int *pOutFlags);
+  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
+  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags);
+  int (*xGetTempName)(sqlite3_vfs*, char *zOut);
+  int (*xFullPathname)(sqlite3_vfs*, const char *zName, char *zOut);
+  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
+  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
+  void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol);
+  void (*xDlClose)(sqlite3_vfs*, void*);
+  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
+  int (*xSleep)(sqlite3_vfs*, int microseconds);
+  int (*xCurrentTime)(sqlite3_vfs*, double*);
+  /* New fields may be appended in figure versions.  The iVersion
+  ** value will increment whenever this happens. */
+};
+
+/*
+** CAPI3REF: Flags for the xAccess VFS method
+**
+** These integer constants can be used as the third parameter to
+** the xAccess method of an [sqlite3_vfs] object.  They determine
+** the kind of what kind of permissions the xAccess method is
+** looking for.  With SQLITE_ACCESS_EXISTS, the xAccess method
+** simply checks to see if the file exists.  With SQLITE_ACCESS_READWRITE,
+** the xAccess method checks to see if the file is both readable
+** and writable.  With SQLITE_ACCESS_READ the xAccess method
+** checks to see if the file is readable.
+*/
+#define SQLITE_ACCESS_EXISTS    0
+#define SQLITE_ACCESS_READWRITE 1
+#define SQLITE_ACCESS_READ      2
+
+/*
 ** CAPI3REF: Enable Or Disable Extended Result Codes
 **
 ** This routine enables or disables the
 ** [SQLITE_IOERR_READ | extended result codes] feature.
@@ -348,9 +759,9 @@
 ** The second argument is a boolean value that turns extended result
 ** codes on and off.  Extended result codes are off by default for
 ** backwards compatibility with older versions of SQLite.
 */
-int sqlite3_extended_result_codes(sqlite3*, int onoff);
+SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
 
 /*
 ** CAPI3REF: Last Insert Rowid
 **
@@ -369,10 +780,14 @@
 ** inserted row is returned by this routine as long as the trigger
 ** is running.  But once the trigger terminates, the value returned
 ** by this routine reverts to the last value inserted before the
 ** trigger fired.
-*/
-sqlite_int64 sqlite3_last_insert_rowid(sqlite3*);
+**
+** If another thread does a new insert on the same database connection
+** while this routine is running and thus changes the last insert rowid,
+** then the return value of this routine is undefined.
+*/
+SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
 
 /*
 ** CAPI3REF: Count The Number Of Rows Modified
 **
@@ -402,8 +817,12 @@
 ** this optimization, the change count for "DELETE FROM table" will be
 ** zero regardless of the number of elements that were originally in the
 ** table. To get an accurate count of the number of rows deleted, use
 ** "DELETE FROM table WHERE 1" instead.
+**
+** If another thread makes changes on the same database connection
+** while this routine is running then the return value of this routine
+** is undefined.
 */
 SQLITE_API int sqlite3_changes(sqlite3*);
 
 /*
@@ -413,9 +832,9 @@
 ** modified by INSERT, UPDATE or DELETE statements since the database handle
 ** was opened. This includes UPDATE, INSERT and DELETE statements executed
 ** as part of trigger programs. All changes are counted as soon as the
 ** statement that makes them is completed (when the statement handle is
-** passed to [sqlite3_reset()] or [sqlite_finalise()]).
+** passed to [sqlite3_reset()] or [sqlite3_finalize()]).
 **
 ** See also the [sqlite3_change()] interface.
 **
 ** SQLite implements the command "DELETE FROM table" without a WHERE clause
@@ -424,10 +843,14 @@
 ** this optimization, the change count for "DELETE FROM table" will be
 ** zero regardless of the number of elements that were originally in the
 ** table. To get an accurate count of the number of rows deleted, use
 ** "DELETE FROM table WHERE 1" instead.
-*/
-int sqlite3_total_changes(sqlite3*);
+**
+** If another thread makes changes on the same database connection
+** while this routine is running then the return value of this routine
+** is undefined.
+*/
+SQLITE_API int sqlite3_total_changes(sqlite3*);
 
 /*
 ** CAPI3REF: Interrupt A Long-Running Query
 **
@@ -437,9 +860,11 @@
 ** or Ctrl-C where the user wants a long query operation to halt
 ** immediately.
 **
 ** It is safe to call this routine from a thread different from the
-** thread that is currently running the database operation.
+** thread that is currently running the database operation.  But it
+** is not safe to call this routine with a database connection that
+** is closed or might close before sqlite3_interrupt() returns.
 **
 ** The SQL operation that is interrupted will return [SQLITE_INTERRUPT].
 ** If an interrupted operation was an update that is inside an
 ** explicit transaction, then the entire transaction will be rolled
@@ -530,10 +955,17 @@
 ** There can only be a single busy handler defined for each database
 ** connection.  Setting a new busy handler clears any previous one.
 ** Note that calling [sqlite3_busy_timeout()] will also set or clear
 ** the busy handler.
-*/
-int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
+**
+** When operating in [sqlite3_enable_shared_cache | shared cache mode],
+** only a single busy handler can be defined for each database file.
+** So if two database connections share a single cache, then changing
+** the busy handler on one connection will also change the busy
+** handler in the other connection.  The busy handler is invoked
+** in the thread that was running when the SQLITE_BUSY was hit.
+*/
+SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
 
 /*
 ** CAPI3REF: Set A Busy Timeout
 **
@@ -550,9 +982,9 @@
 ** connection.  If another busy handler was defined
 ** (using [sqlite3_busy_handler()]) prior to calling
 ** this routine, that other busy handler is cleared.
 */
-int sqlite3_busy_timeout(sqlite3*, int ms);
+SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
 
 /*
 ** CAPI3REF: Convenience Routines For Running Queries
 **
@@ -563,29 +995,29 @@
 ** query has finished.
 **
 ** As an example, suppose the query result where this table:
 **
-** <pre>
+** <blockquote><pre>
 **        Name        | Age
 **        -----------------------
 **        Alice       | 43
 **        Bob         | 28
 **        Cindy       | 21
-** </pre>
+** </pre></blockquote>
 **
 ** If the 3rd argument were &azResult then after the function returns
 ** azResult will contain the following data:
 **
-** <pre>
-**        azResult[0] = "Name";
-**        azResult[1] = "Age";
-**        azResult[2] = "Alice";
-**        azResult[3] = "43";
-**        azResult[4] = "Bob";
-**        azResult[5] = "28";
-**        azResult[6] = "Cindy";
-**        azResult[7] = "21";
-** </pre>
+** <blockquote><pre>
+**        azResult&#91;0] = "Name";
+**        azResult&#91;1] = "Age";
+**        azResult&#91;2] = "Alice";
+**        azResult&#91;3] = "43";
+**        azResult&#91;4] = "Bob";
+**        azResult&#91;5] = "28";
+**        azResult&#91;6] = "Cindy";
+**        azResult&#91;7] = "21";
+** </pre></blockquote>
 **
 ** Notice that there is an extra row of data containing the column
 ** headers.  But the *nrow return value is still 3.  *ncolumn is
 ** set to 2.  In general, the number of values inserted into azResult
@@ -599,17 +1031,17 @@
 ** the memory properly and safely.
 **
 ** The return value of this routine is the same as from [sqlite3_exec()].
 */
-int sqlite3_get_table(
+SQLITE_API int sqlite3_get_table(
   sqlite3*,              /* An open database */
   const char *sql,       /* SQL to be executed */
   char ***resultp,       /* Result written to a char *[]  that this points to */
   int *nrow,             /* Number of result rows written here */
   int *ncolumn,          /* Number of result columns written here */
   char **errmsg          /* Error msg written here */
 );
-void sqlite3_free_table(char **result);
+SQLITE_API void sqlite3_free_table(char **result);
 
 /*
 ** CAPI3REF: Formatted String Printing Functions
 **
@@ -616,9 +1048,9 @@
 ** These routines are workalikes of the "printf()" family of functions
 ** from the standard C library.
 **
 ** The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
-** results into memory obtained from [sqlite_malloc()].
+** results into memory obtained from [sqlite3_malloc()].
 ** The strings returned by these two routines should be
 ** released by [sqlite3_free()].  Both routines return a
 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
 ** memory to hold the resulting string.
@@ -644,9 +1076,9 @@
 **
 ** These routines all implement some additional formatting
 ** options that are useful for constructing SQL statements.
 ** All of the usual printf formatting options apply.  In addition, there
-** is are "%q" and "%Q" options.
+** is are "%q", "%Q", and "%z" options.
 **
 ** The %q option works like %s in that it substitutes a null-terminated
 ** string from the argument list.  But %q also doubles every '\'' character.
 ** %q is designed for use inside a string literal.  By doubling each '\''
@@ -697,25 +1129,119 @@
 ** </pre></blockquote>
 **
 ** The code above will render a correct SQL statement in the zSQL
 ** variable even if the zText variable is a NULL pointer.
+**
+** The "%z" formatting option works exactly like "%s" with the
+** addition that after the string has been read and copied into
+** the result, [sqlite3_free()] is called on the input string.
 */
 SQLITE_API char *sqlite3_mprintf(const char*,...);
 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
 
 /*
-** CAPI3REF: Memory Allocation Functions
-**
-** SQLite uses its own memory allocator.  On some installations, this
-** memory allocator is identical to the standard malloc()/realloc()/free()
-** and can be used interchangable.  On others, the implementations are
-** different.  For maximum portability, it is best not to mix calls
-** to the standard malloc/realloc/free with the sqlite versions.
+** CAPI3REF: Memory Allocation Subsystem
+**
+** The SQLite core uses these three routines for all of its own
+** internal memory allocation needs. (See the exception below.)
+** The default implementation
+** of the memory allocation subsystem uses the malloc(), realloc()
+** and free() provided by the standard C library.  However, if
+** SQLite is compiled with the following C preprocessor macro
+**
+** <blockquote> SQLITE_OMIT_MEMORY_ALLOCATION </blockquote>
+**
+** then no implementation is provided for these routines by
+** SQLite.  The application that links against SQLite is
+** expected to provide its own implementation.  If the application
+** does provide its own implementation for these routines, then
+** it must also provide an implementations for
+** [sqlite3_memory_alarm()], [sqlite3_memory_used()], and
+** [sqlite3_memory_highwater()].  The alternative implementations
+** for these last three routines need not actually work, but
+** stub functions at least are needed to statisfy the linker.
+** SQLite never calls [sqlite3_memory_highwater()] itself, but
+** the symbol is included in a table as part of the
+** [sqlite3_load_extension()] interface.  The
+** [sqlite3_memory_alarm()] and [sqlite3_memory_used()] interfaces
+** are called by [sqlite3_soft_heap_limit()] and working implementations
+** of both routines must be provided if [sqlite3_soft_heap_limit()]
+** is to operate correctly.
+**
+** <b>Exception:</b> The windows OS interface layer calls
+** the system malloc() and free() directly when converting
+** filenames between the UTF-8 encoding used by SQLite
+** and whatever filename encoding is used by the particular windows
+** installation.  Memory allocation errors are detected, but
+** they are reported back as [SQLITE_CANTOPEN] or
+** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
 */
 SQLITE_API void *sqlite3_malloc(int);
 SQLITE_API void *sqlite3_realloc(void*, int);
 SQLITE_API void sqlite3_free(void*);
+
+/*
+** CAPI3REF: Memory Allocator Statistics
+**
+** In addition to the basic three allocation routines
+** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()],
+** the memory allocation subsystem included with the SQLite
+** sources provides the interfaces shown below.
+**
+** The first of these two routines returns the amount of memory
+** currently outstanding (malloced but not freed).  The second
+** returns the largest instantaneous amount of outstanding
+** memory.  The highwater mark is reset if the argument is
+** true.
+**
+** The implementation of these routines in the SQLite core
+** is omitted if the application is compiled with the
+** SQLITE_OMIT_MEMORY_ALLOCATION macro defined.  In that case,
+** the application that links SQLite must provide its own
+** alternative implementation.  See the documentation on
+** [sqlite3_malloc()] for additional information.
+*/
+SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
+SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
+
+/*
+** CAPI3REF: Memory Allocation Alarms
+**
+** The [sqlite3_memory_alarm] routine is used to register
+** a callback on memory allocation events.
+**
+** This routine registers or clears a callbacks that fires when
+** the amount of memory allocated exceeds iThreshold.  Only
+** a single callback can be registered at a time.  Each call
+** to [sqlite3_memory_alarm()] overwrites the previous callback.
+** The callback is disabled by setting xCallback to a NULL
+** pointer.
+**
+** The parameters to the callback are the pArg value, the
+** amount of memory currently in use, and the size of the
+** allocation that provoked the callback.  The callback will
+** presumably invoke [sqlite3_free()] to free up memory space.
+** The callback may invoke [sqlite3_malloc()] or [sqlite3_realloc()]
+** but if it does, no additional callbacks will be invoked by
+** the recursive calls.
+**
+** The [sqlite3_soft_heap_limit()] interface works by registering
+** a memory alarm at the soft heap limit and invoking
+** [sqlite3_release_memory()] in the alarm callback.  Application
+** programs should not attempt to use the [sqlite3_memory_alarm()]
+** interface because doing so will interfere with the
+** [sqlite3_soft_heap_limit()] module.  This interface is exposed
+** only so that applications can provide their own
+** alternative implementation when the SQLite core is
+** compiled with SQLITE_OMIT_MEMORY_ALLOCATION.
+*/
+SQLITE_API int sqlite3_memory_alarm(
+  void(*xCallback)(void *pArg, sqlite3_int64 used, int N),
+  void *pArg,
+  sqlite3_int64 iThreshold
+);
+
 
 /*
 ** CAPI3REF: Compile-Time Authorization Callbacks
 ***
@@ -768,9 +1294,9 @@
 ** Note that the authorizer callback is invoked only during
 ** [sqlite3_prepare()] or its variants.  Authorization is not
 ** performed during statement evaluation in [sqlite3_step()].
 */
-int sqlite3_set_authorizer(
+SQLITE_API int sqlite3_set_authorizer(
   sqlite3*,
   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
   void *pUserData
 );
@@ -855,9 +1381,9 @@
 ** is subject to change.
 */
 SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
 SQLITE_API void *sqlite3_profile(sqlite3*,
-   void(*xProfile)(void*,const char*,sqlite_uint64), void*);
+   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
 
 /*
 ** CAPI3REF: Query Progress Callbacks
 **
@@ -888,33 +1414,73 @@
 ** [sqlite3_get_table()] call returns SQLITE_INTERRUPT.   This feature
 ** can be used, for example, to implement the "Cancel" button on a
 ** progress dialog box in a GUI.
 */
-void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
+SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
 
 /*
 ** CAPI3REF: Opening A New Database Connection
 **
 ** Open the sqlite database file "filename".  The "filename" is UTF-8
-** encoded for sqlite3_open() and UTF-16 encoded in the native byte order
-** for sqlite3_open16().  An [sqlite3*] handle is returned in *ppDb, even
+** encoded for [sqlite3_open()] and [sqlite3_open_v2()] and UTF-16 encoded
+** in the native byte order for [sqlite3_open16()].
+** An [sqlite3*] handle is returned in *ppDb, even
 ** if an error occurs. If the database is opened (or created) successfully,
-** then SQLITE_OK is returned. Otherwise an error code is returned. The
-** sqlite3_errmsg() or sqlite3_errmsg16()  routines can be used to obtain
+** then [SQLITE_OK] is returned. Otherwise an error code is returned. The
+** [sqlite3_errmsg()] or [sqlite3_errmsg16()]  routines can be used to obtain
 ** an English language description of the error.
 **
-** If the database file does not exist, then a new database will be created
-** as needed.  The default encoding for the database will be UTF-8 if
-** sqlite3_open() is called and UTF-16 if sqlite3_open16 is used.
+** The default encoding for the database will be UTF-8 if
+** [sqlite3_open()] or [sqlite3_open_v2()] is called and
+** UTF-16 if [sqlite3_open16()] is used.
 **
 ** Whether or not an error occurs when it is opened, resources associated
 ** with the [sqlite3*] handle should be released by passing it to
-** sqlite3_close() when it is no longer required.
-**
-** Note to windows users:  The encoding used for the filename argument
-** of sqlite3_open() must be UTF-8, not whatever codepage is currently
-** defined.  Filenames containing international characters must be converted
-** to UTF-8 prior to passing them into sqlite3_open().
+** [sqlite3_close()] when it is no longer required.
+**
+** The [sqlite3_open_v2()] interface works like [sqlite3_open()] except that
+** provides two additional parameters for additional control over the
+** new database connection.  The flags parameter can be one of:
+**
+** <ol>
+** <li>  [SQLITE_OPEN_READONLY]
+** <li>  [SQLITE_OPEN_READWRITE]
+** <li>  [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]
+** </ol>
+**
+** The first value opens the database read-only.  If the database does
+** not previously exist, an error is returned.  The second option opens
+** the database for reading and writing if possible, or reading only if
+** if the file is write protected.  In either case the database must already
+** exist or an error is returned.  The third option opens the database
+** for reading and writing and creates it if it does not already exist.
+** The third options is behavior that is always used for [sqlite3_open()]
+** and [sqlite3_open16()].
+**
+** If the filename is ":memory:", then an private
+** in-memory database is created for the connection.  This in-memory
+** database will vanish when the database connection is closed.  Future
+** version of SQLite might make use of additional special filenames
+** that begin with the ":" character.  It is recommended that
+** when a database filename really does begin with
+** ":" that you prefix the filename with a pathname like "./" to
+** avoid ambiguity.
+**
+** If the filename is an empty string, then a private temporary
+** on-disk database will be created.  This private database will be
+** automatically deleted as soon as the database connection is closed.
+**
+** The fourth parameter to sqlite3_open_v2() is the name of the
+** [sqlite3_vfs] object that defines the operating system
+** interface that the new database connection should use.  If the
+** fourth parameter is a NULL pointer then the default [sqlite3_vfs]
+** object is used.
+**
+** <b>Note to windows users:</b>  The encoding used for the filename argument
+** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever
+** codepage is currently defined.  Filenames containing international
+** characters must be converted to UTF-8 prior to passing them into
+** [sqlite3_open()] or [sqlite3_open_v2()].
 */
 SQLITE_API int sqlite3_open(
   const char *filename,   /* Database filename (UTF-8) */
   sqlite3 **ppDb          /* OUT: SQLite db handle */
@@ -921,8 +1487,14 @@
 );
 SQLITE_API int sqlite3_open16(
   const void *filename,   /* Database filename (UTF-16) */
   sqlite3 **ppDb          /* OUT: SQLite db handle */
+);
+SQLITE_API int sqlite3_open_v2(
+  const char *filename,   /* Database filename (UTF-8) */
+  sqlite3 **ppDb,         /* OUT: SQLite db handle */
+  int flags,              /* Flags */
+  const char *zVfs        /* Name of VFS module to use */
 );
 
 /*
 ** CAPI3REF: Error Codes And Messages
@@ -944,10 +1516,13 @@
 ** by [sqlite3_errcode()], [sqlite3_errmsg()], and [sqlite3_errmsg16()]
 ** (overwriting the previous values). Note that calls to [sqlite3_errcode()],
 ** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the
 ** results of future invocations.  Calls to API routines that do not return
-** an error code (examples: [sqlite3_data_count()] or [sqlite3_mprintf()]) do
-** not change the error code returned by this routine.
+** an error code (example: [sqlite3_data_count()]) do not
+** change the error code returned by this routine.  Interfaces that are
+** not associated with a specific database connection (examples:
+** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change
+** the return code.
 **
 ** Assuming no other intervening sqlite3_* API calls are made, the error
 ** code returned by this function is associated with the same error as
 ** the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()].
@@ -987,9 +1562,10 @@
 ** To execute an SQL query, it must first be compiled into a byte-code
 ** program using one of these routines.
 **
 ** The first argument "db" is an [sqlite3 | SQLite database handle]
-** obtained from a prior call to [sqlite3_open()] or [sqlite3_open16()].
+** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()]
+** or [sqlite3_open16()].
 ** The second argument "zSql" is the statement to be compiled, encoded
 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
 ** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2()
 ** use UTF-16.
@@ -1055,9 +1631,9 @@
   int nByte,              /* Maximum length of zSql in bytes. */
   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
 );
-int sqlite3_prepare_v2(
+SQLITE_API int sqlite3_prepare_v2(
   sqlite3 *db,            /* Database handle */
   const char *zSql,       /* SQL statement, UTF-8 encoded */
   int nByte,              /* Maximum length of zSql in bytes. */
   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
@@ -1069,9 +1645,9 @@
   int nByte,              /* Maximum length of zSql in bytes. */
   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
 );
-int sqlite3_prepare16_v2(
+SQLITE_API int sqlite3_prepare16_v2(
   sqlite3 *db,            /* Database handle */
   const void *zSql,       /* SQL statement, UTF-16 encoded */
   int nByte,              /* Maximum length of zSql in bytes. */
   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
@@ -1155,9 +1731,10 @@
 ** is filled with zeros.  A zeroblob uses a fixed amount of memory
 ** (just an integer to hold it size) while it is being processed.
 ** Zeroblobs are intended to serve as place-holders for BLOBs whose
 ** content is later written using
-** [sqlite3_blob_open | increment BLOB I/O] routines.
+** [sqlite3_blob_open | increment BLOB I/O] routines.  A negative
+** value for the zeroblob results in a zero-length BLOB.
 **
 ** The sqlite3_bind_*() routines must be called after
 ** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and
 ** before [sqlite3_step()].
@@ -1169,17 +1746,17 @@
 ** index is out of range.  [SQLITE_NOMEM] is returned if malloc fails.
 ** [SQLITE_MISUSE] is returned if these routines are called on a virtual
 ** machine that is the wrong state or which has already been finalized.
 */
-int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
-int sqlite3_bind_double(sqlite3_stmt*, int, double);
-int sqlite3_bind_int(sqlite3_stmt*, int, int);
-int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite_int64);
-int sqlite3_bind_null(sqlite3_stmt*, int);
-int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
-int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
-int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
-int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
+SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
+SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
+SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
+SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
+SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
+SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
+SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
+SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
+SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
 
 /*
 ** CAPI3REF: Number Of Host Parameters
 **
@@ -1192,10 +1769,14 @@
 ** of unique host parameter names.  If host parameters of the form "?NNN"
 ** are used (where NNN is an integer) then there might be gaps in the
 ** numbering and the value returned by this interface is the index of the
 ** host parameter with the largest index value.
-*/
-int sqlite3_bind_parameter_count(sqlite3_stmt*);
+**
+** The prepared statement must not be [sqlite3_finalize | finalized]
+** prior to this routine returnning.  Otherwise the results are undefined
+** and probably undesirable.
+*/
+SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
 
 /*
 ** CAPI3REF: Name Of A Host Parameter
 **
@@ -1213,9 +1794,9 @@
 ** then NULL is returned.  The returned string is always in the
 ** UTF-8 encoding even if the named parameter was originally specified
 ** as UTF-16 in [sqlite3_prepare16()] or [sqlite3_prepare16_v2()].
 */
-const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
+SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
 
 /*
 ** CAPI3REF: Index Of A Parameter With A Given Name
 **
@@ -1222,9 +1803,9 @@
 ** This routine returns the index of a host parameter with the given name.
 ** The name must match exactly.  If no parameter with the given name is
 ** found, return 0.  Parameter names must be UTF8.
 */
-int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
+SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
 
 /*
 ** CAPI3REF: Reset All Bindings On A Prepared Statement
 **
@@ -1232,9 +1813,9 @@
 ** reset the [sqlite3_bind_blob | bindings] on a
 ** [sqlite3_stmt | prepared statement].  Use this routine to
 ** reset all host parameters to NULL.
 */
-int sqlite3_clear_bindings(sqlite3_stmt*);
+SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
 
 /*
 ** CAPI3REF: Number Of Columns In A Result Set
 **
@@ -1242,9 +1823,9 @@
 ** [sqlite3_stmt | compiled SQL statement]. This routine returns 0
 ** if pStmt is an SQL statement that does not return data (for
 ** example an UPDATE).
 */
-int sqlite3_column_count(sqlite3_stmt *pStmt);
+SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
 
 /*
 ** CAPI3REF: Column Names In A Result Set
 **
@@ -1251,19 +1832,23 @@
 ** These routines return the name assigned to a particular column
 ** in the result set of a SELECT statement.  The sqlite3_column_name()
 ** interface returns a pointer to a UTF8 string and sqlite3_column_name16()
 ** returns a pointer to a UTF16 string.  The first parameter is the
-** [sqlite_stmt | prepared statement] that implements the SELECT statement.
+** [sqlite3_stmt | prepared statement] that implements the SELECT statement.
 ** The second parameter is the column number.  The left-most column is
 ** number 0.
 **
 ** The returned string pointer is valid until either the
-** [sqlite_stmt | prepared statement] is destroyed by [sqlite3_finalize()]
+** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()]
 ** or until the next call sqlite3_column_name() or sqlite3_column_name16()
 ** on the same column.
-*/
-const char *sqlite3_column_name(sqlite3_stmt*, int N);
-const void *sqlite3_column_name16(sqlite3_stmt*, int N);
+**
+** If sqlite3_malloc() fails during the processing of either routine
+** (for example during a conversion from UTF-8 to UTF-16) then a
+** NULL pointer is returned.
+*/
+SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
+SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
 
 /*
 ** CAPI3REF: Source Of Data In A Query Result
 **
@@ -1296,15 +1881,19 @@
 ** encoded strings, the other functions return UTF-8.
 **
 ** These APIs are only available if the library was compiled with the
 ** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined.
-*/
-const char *sqlite3_column_database_name(sqlite3_stmt*,int);
-const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
-const char *sqlite3_column_table_name(sqlite3_stmt*,int);
-const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
-const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
-const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
+**
+** If two or more threads call one or more of these routines against the same
+** prepared statement and column at the same time then the results are
+** undefined.
+*/
+SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
+SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
+SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
+SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
+SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
+SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
 
 /*
 ** CAPI3REF: Declared Datatype Of A Query Result
 **
@@ -1333,10 +1922,10 @@
 ** strongly typed, but the typing is dynamic not static.  Type
 ** is associated with individual values, not with the containers
 ** used to hold those values.
 */
-const char *sqlite3_column_decltype(sqlite3_stmt *, int i);
-const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
+SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *, int i);
+SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
 
 /*
 ** CAPI3REF:  Evaluate An SQL Statement
 **
@@ -1382,13 +1971,13 @@
 ** the VM. More information may be found by calling [sqlite3_errmsg()].
 ** With the legacy interface, a more specific error code (example:
 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
 ** can be obtained by calling [sqlite3_reset()] on the
-** [sqlite_stmt | prepared statement].  In the "v2" interface,
+** [sqlite3_stmt | prepared statement].  In the "v2" interface,
 ** the more specific error code is returned directly by sqlite3_step().
 **
 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
-** Perhaps it was called on a [sqlite_stmt | prepared statement] that has
+** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has
 ** already been [sqlite3_finalize | finalized] or on one that had
 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
 ** be the case that the same database connection is being used by two or
 ** more threads at the same moment in time.
@@ -1417,12 +2006,12 @@
 ** After a call to [sqlite3_step()] that returns [SQLITE_ROW], this routine
 ** will return the same value as the [sqlite3_column_count()] function.
 ** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or
 ** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been
-** called on the [sqlite_stmt | prepared statement] for the first time,
+** called on the [sqlite3_stmt | prepared statement] for the first time,
 ** this routine returns zero.
 */
-int sqlite3_data_count(sqlite3_stmt *pStmt);
+SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
 
 /*
 ** CAPI3REF: Fundamental Datatypes
 **
@@ -1456,19 +2045,29 @@
 
 /*
 ** CAPI3REF: Results Values From A Query
 **
-** These routines return information about the information
-** in a single column of the current result row of a query.  In every
+** These routines return information about
+** a single column of the current result row of a query.  In every
 ** case the first argument is a pointer to the
 ** [sqlite3_stmt | SQL statement] that is being
-** evaluate (the [sqlite_stmt*] that was returned from
+** evaluated (the [sqlite3_stmt*] that was returned from
 ** [sqlite3_prepare_v2()] or one of its variants) and
 ** the second argument is the index of the column for which information
-** should be returned.  The left-most column has an index of 0.
+** should be returned.  The left-most column of the result set
+** has an index of 0.
 **
 ** If the SQL statement is not currently point to a valid row, or if the
 ** the column index is out of range, the result is undefined.
+** These routines may only be called when the most recent call to
+** [sqlite3_step()] has returned [SQLITE_ROW] and neither
+** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently.
+** If any of these routines are called after [sqlite3_reset()] or
+** [sqlite3_finalize()] or after [sqlite3_step()] has returned
+** something other than [SQLITE_ROW], the results are undefined.
+** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
+** are called from a different thread while any of these routines
+** are pending, then the results are undefined.
 **
 ** The sqlite3_column_type() routine returns
 ** [SQLITE_INTEGER | datatype code] for the initial data type
 ** of the result column.  The returned value is one of [SQLITE_INTEGER],
@@ -1489,8 +2088,13 @@
 ** The value returned does not include the zero terminator at the end
 ** of the string.  For clarity: the value returned is the number of
 ** bytes in the string, not the number of characters.
 **
+** Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
+** even zero-length strings, are always zero terminated.  The return
+** value from sqlite3_column_blob() for a zero-length blob is an arbitrary
+** pointer, possibly even a NULL pointer.
+**
 ** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes()
 ** but leaves the result in UTF-16 instead of UTF-8.
 ** The zero terminator is not included in this count.
 **
@@ -1501,10 +2105,9 @@
 ** are applied:
 **
 ** <blockquote>
 ** <table border="1">
-** <tr><th> Internal <th> Requested <th>
-** <tr><th>  Type    <th>    Type   <th> Conversion
+** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
 **
 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
 ** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
@@ -1570,19 +2173,32 @@
 ** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to
 ** find the size of the result.  Do not mix call to sqlite3_column_text() or
 ** sqlite3_column_blob() with calls to sqlite3_column_bytes16().  And do not
 ** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes().
-*/
-const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
-int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
-int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
-double sqlite3_column_double(sqlite3_stmt*, int iCol);
-int sqlite3_column_int(sqlite3_stmt*, int iCol);
-sqlite_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
-const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
-const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
-int sqlite3_column_type(sqlite3_stmt*, int iCol);
-sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
+**
+** The pointers returned are valid until a type conversion occurs as
+** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
+** [sqlite3_finalize()] is called.  The memory space used to hold strings
+** and blobs is freed automatically.  Do <b>not</b> pass the pointers returned
+** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
+** [sqlite3_free()].
+**
+** If a memory allocation error occurs during the evaluation of any
+** of these routines, a default value is returned.  The default value
+** is either the integer 0, the floating point number 0.0, or a NULL
+** pointer.  Subsequent calls to [sqlite3_errcode()] will return
+** [SQLITE_NOMEM].
+*/
+SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
+SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
+SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
+SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
+SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
+SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
+SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
+SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
+SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
+SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
 
 /*
 ** CAPI3REF: Destroy A Prepared Statement Object
 **
@@ -1606,9 +2222,9 @@
 /*
 ** CAPI3REF: Reset A Prepared Statement Object
 **
 ** The sqlite3_reset() function is called to reset a
-** [sqlite_stmt | compiled SQL statement] object.
+** [sqlite3_stmt | compiled SQL statement] object.
 ** back to it's initial state, ready to be re-executed.
 ** Any SQL statement variables that had values bound to them using
 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
 ** Use [sqlite3_clear_bindings()] to reset the bindings.
@@ -1645,9 +2261,9 @@
 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
 ** its parameters.  Any SQL function implementation should be able to work
 ** work with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
 ** more efficient with one encoding than another.  It is allowed to
-** invoke sqlite_create_function() or sqlite3_create_function16() multiple
+** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
 ** times with the same function but with different values of eTextRep.
 ** When multiple implementations of the same function are available, SQLite
 ** will pick the one that involves the least amount of data conversion.
 ** If there is only a single implementation which does not care what
@@ -1655,9 +2271,9 @@
 ** [SQLITE_ANY].
 **
 ** The fifth parameter is an arbitrary pointer.  The implementation
 ** of the function can gain access to this pointer using
-** [sqlite_user_data()].
+** [sqlite3_user_data()].
 **
 ** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
 ** pointers to C-language functions that implement the SQL
 ** function or aggregate. A scalar SQL function requires an implementation of
@@ -1672,9 +2288,9 @@
 ** arguments or differing perferred text encodings.  SQLite will use
 ** the implementation most closely matches the way in which the
 ** SQL function is used.
 */
-int sqlite3_create_function(
+SQLITE_API int sqlite3_create_function(
   sqlite3 *,
   const char *zFunctionName,
   int nArg,
   int eTextRep,
@@ -1682,9 +2298,9 @@
   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   void (*xFinal)(sqlite3_context*)
 );
-int sqlite3_create_function16(
+SQLITE_API int sqlite3_create_function16(
   sqlite3*,
   const void *zFunctionName,
   int nArg,
   int eTextRep,
@@ -1715,13 +2331,13 @@
 ** these functions.  However, new development projects should avoid
 ** the use of these functions.  To help encourage people to avoid
 ** using these functions, we are not going to tell you want they do.
 */
-int sqlite3_aggregate_count(sqlite3_context*);
+SQLITE_API int sqlite3_aggregate_count(sqlite3_context*);
 SQLITE_API int sqlite3_expired(sqlite3_stmt*);
-int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
-int sqlite3_global_recover(void);
-
+SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
+SQLITE_API int sqlite3_global_recover(void);
+SQLITE_API void sqlite3_thread_cleanup(void);
 
 /*
 ** CAPI3REF: Obtaining SQL Function Parameter Values
 **
@@ -1757,23 +2373,29 @@
 **
 ** Please pay particular attention to the fact that the pointer that
 ** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or
 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
-** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite_value_text()],
+** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
 ** or [sqlite3_value_text16()].
-*/
-const void *sqlite3_value_blob(sqlite3_value*);
-int sqlite3_value_bytes(sqlite3_value*);
-int sqlite3_value_bytes16(sqlite3_value*);
-double sqlite3_value_double(sqlite3_value*);
-int sqlite3_value_int(sqlite3_value*);
-sqlite_int64 sqlite3_value_int64(sqlite3_value*);
-const unsigned char *sqlite3_value_text(sqlite3_value*);
-const void *sqlite3_value_text16(sqlite3_value*);
-const void *sqlite3_value_text16le(sqlite3_value*);
-const void *sqlite3_value_text16be(sqlite3_value*);
-int sqlite3_value_type(sqlite3_value*);
-int sqlite3_value_numeric_type(sqlite3_value*);
+**
+** These routines must be called from the same thread as
+** the SQL function that supplied the sqlite3_value* parameters.
+** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()]
+** interface, then these routines should be called from the same thread
+** that ran [sqlite3_column_value()].
+*/
+SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
+SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
+SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
+SQLITE_API double sqlite3_value_double(sqlite3_value*);
+SQLITE_API int sqlite3_value_int(sqlite3_value*);
+SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
+SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
+SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
+SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
+SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
+SQLITE_API int sqlite3_value_type(sqlite3_value*);
+SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
 
 /*
 ** CAPI3REF: Obtain Aggregate Function Context
 **
@@ -1790,10 +2412,13 @@
 ** The first parameter should be a copy of the
 ** [sqlite3_context | SQL function context] that is the first
 ** parameter to the callback routine that implements the aggregate
 ** function.
-*/
-void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
+**
+** This routine must be called from the same thread in which
+** the aggregate SQL function is running.
+*/
+SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
 
 /*
 ** CAPI3REF: User Data For Functions
 **
@@ -1800,10 +2425,13 @@
 ** The pUserData parameter to the [sqlite3_create_function()]
 ** and [sqlite3_create_function16()] routines
 ** used to register user functions is available to
 ** the implementation of the function using this call.
-*/
-void *sqlite3_user_data(sqlite3_context*);
+**
+** This routine must be called from the same thread in which
+** the SQL function is running.
+*/
+SQLITE_API void *sqlite3_user_data(sqlite3_context*);
 
 /*
 ** CAPI3REF: Function Auxiliary Data
 **
@@ -1832,11 +2460,14 @@
 **
 ** In practice, meta-data is preserved between function calls for
 ** expressions that are constant at compile time. This includes literal
 ** values and SQL variables.
-*/
-void *sqlite3_get_auxdata(sqlite3_context*, int);
-void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
+**
+** These routines must be called from the same thread in which
+** the SQL function is running.
+*/
+SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int);
+SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
 
 
 /*
 ** CAPI3REF: Constants Defining Special Destructor Behavior
@@ -1878,23 +2509,27 @@
 **
 ** The sqlite3_result_toobig() cause the function implementation
 ** to throw and error indicating that a string or BLOB is to long
 ** to represent.
-*/
-void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
-void sqlite3_result_double(sqlite3_context*, double);
-void sqlite3_result_error(sqlite3_context*, const char*, int);
-void sqlite3_result_error16(sqlite3_context*, const void*, int);
-void sqlite3_result_error_toobig(sqlite3_context*);
-void sqlite3_result_int(sqlite3_context*, int);
-void sqlite3_result_int64(sqlite3_context*, sqlite_int64);
-void sqlite3_result_null(sqlite3_context*);
-void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
-void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
-void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
-void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
-void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
-void sqlite3_result_zeroblob(sqlite3_context*, int n);
+**
+** These routines must be called from within the same thread as
+** the SQL function associated with the [sqlite3_context] pointer.
+*/
+SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
+SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
+SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
+SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
+SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
+SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
+SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
+SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
+SQLITE_API void sqlite3_result_null(sqlite3_context*);
+SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
+SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
+SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
+SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
+SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
+SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
 
 /*
 ** CAPI3REF: Define New Collating Sequences
 **
@@ -1936,24 +2571,24 @@
 ** The sqlite3_create_collation_v2() interface is experimental and
 ** subject to change in future releases.  The other collation creation
 ** functions are stable.
 */
-int sqlite3_create_collation(
+SQLITE_API int sqlite3_create_collation(
   sqlite3*,
   const char *zName,
   int eTextRep,
   void*,
   int(*xCompare)(void*,int,const void*,int,const void*)
 );
-int sqlite3_create_collation_v2(
+SQLITE_API int sqlite3_create_collation_v2(
   sqlite3*,
   const char *zName,
   int eTextRep,
   void*,
   int(*xCompare)(void*,int,const void*,int,const void*),
   void(*xDestroy)(void*)
 );
-int sqlite3_create_collation16(
+SQLITE_API int sqlite3_create_collation16(
   sqlite3*,
   const char *zName,
   int eTextRep,
   void*,
@@ -1985,14 +2620,14 @@
 ** The callback function should register the desired collation using
 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
 ** [sqlite3_create_collation_v2()].
 */
-int sqlite3_collation_needed(
+SQLITE_API int sqlite3_collation_needed(
   sqlite3*,
   void*,
   void(*)(void*,sqlite3*,int eTextRep,const char*)
 );
-int sqlite3_collation_needed16(
+SQLITE_API int sqlite3_collation_needed16(
   sqlite3*,
   void*,
   void(*)(void*,sqlite3*,int eTextRep,const void*)
 );
@@ -2031,8 +2666,11 @@
 ** If the operating system does not support sleep requests with
 ** millisecond time resolution, then the time will be rounded up to
 ** the nearest second. The number of milliseconds of sleep actually
 ** requested from the operating system is returned.
+**
+** SQLite implements this interface by calling the xSleep()
+** method of the default [sqlite3_vfs] object.
 */
 SQLITE_API int sqlite3_sleep(int);
 
 /*
@@ -2043,24 +2681,28 @@
 ** created by SQLite will be placed in that directory.  If this variable
 ** is NULL pointer, then SQLite does a search for an appropriate temporary
 ** file directory.
 **
-** Once [sqlite3_open()] has been called, changing this variable will
-** invalidate the current temporary database, if any.  Generally speaking,
-** it is not safe to invoke this routine after [sqlite3_open()] has
-** been called.
-*/
-extern char *sqlite3_temp_directory;
-
-/*
-** CAPI3REF:  Test To See If The Databse Is In Auto-Commit Mode
+** It is not safe to modify this variable once a database connection
+** has been opened.  It is intended that this variable be set once
+** as part of process initialization and before any SQLite interface
+** routines have been call and remain unchanged thereafter.
+*/
+SQLITE_EXTERN char *sqlite3_temp_directory;
+
+/*
+** CAPI3REF:  Test To See If The Database Is In Auto-Commit Mode
 **
 ** Test to see whether or not the database connection is in autocommit
 ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
 ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
 ** by the next COMMIT or ROLLBACK.
-*/
-int sqlite3_get_autocommit(sqlite3*);
+**
+** If another thread changes the autocommit status of the database
+** connection while this routine is running, then the return value
+** is undefined.
+*/
+SQLITE_API int sqlite3_get_autocommit(sqlite3*);
 
 /*
 ** CAPI3REF:  Find The Database Handle Associated With A Prepared Statement
 **
@@ -2069,9 +2711,9 @@
 ** This is the same database handle that was
 ** the first argument to the [sqlite3_prepare_v2()] or its variants
 ** that was used to create the statement in the first place.
 */
-sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
+SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
 
 
 /*
 ** CAPI3REF: Commit And Rollback Notification Callbacks
@@ -2094,10 +2736,10 @@
 ** back because the database connection is closed.
 **
 ** These are experimental interfaces and are subject to change.
 */
-void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
-void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
+SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
+SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
 
 /*
 ** CAPI3REF: Data Change Notification Callbacks
 **
@@ -2121,11 +2763,11 @@
 **
 ** If another function was previously registered, its pArg value is returned.
 ** Otherwise NULL is returned.
 */
-void *sqlite3_update_hook(
+SQLITE_API void *sqlite3_update_hook(
   sqlite3*,
-  void(*)(void *,int ,char const *,char const *,sqlite_int64),
+  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
   void*
 );
 
 /*
@@ -2135,100 +2777,77 @@
 ** and schema data structures between connections to the same database.
 ** Sharing is enabled if the argument is true and disabled if the argument
 ** is false.
 **
-** Cache sharing is enabled and disabled on a thread-by-thread basis.
-** Each call to this routine enables or disables cache sharing only for
-** connections created in the same thread in which this routine is called.
-** There is no mechanism for sharing cache between database connections
-** running in different threads.
-**
-** Sharing must be disabled prior to shutting down a thread or else
-** the thread will leak memory.  Call this routine with an argument of
-** 0 to turn off sharing.  Or use the sqlite3_thread_cleanup() API.
-**
-** This routine must not be called when any database connections
-** are active in the current thread.  Enabling or disabling shared
-** cache while there are active database connections will result
-** in memory corruption.
-**
-** When the shared cache is enabled, the
-** following routines must always be called from the same thread:
-** [sqlite3_open()], [sqlite3_prepare_v2()], [sqlite3_step()],
-** [sqlite3_reset()], [sqlite3_finalize()], and [sqlite3_close()].
-** This is due to the fact that the shared cache makes use of
-** thread-specific storage so that it will be available for sharing
-** with other connections.
+** Beginning in SQLite version 3.5.0, cache sharing is enabled and disabled
+** for an entire process.  In prior versions of SQLite, sharing was
+** enabled or disabled for each thread separately.
+**
+** The cache sharing mode set by this interface effects all subsequent
+** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
+** Existing database connections continue use the sharing mode that was
+** in effect at the time they were opened.
 **
 ** Virtual tables cannot be used with a shared cache.  When shared
-** cache is enabled, the sqlite3_create_module() API used to register
+** cache is enabled, the [sqlite3_create_module()] API used to register
 ** virtual tables will always return an error.
 **
 ** This routine returns [SQLITE_OK] if shared cache was
 ** enabled or disabled successfully.  An [SQLITE_ERROR | error code]
 ** is returned otherwise.
 **
-** Shared cache is disabled by default for backward compatibility.
-*/
-int sqlite3_enable_shared_cache(int);
+** Shared cache is disabled by default.  But this might change in
+** future releases of SQLite.  Applications that care about shared
+** cache setting should set it explicitly.
+*/
+SQLITE_API int sqlite3_enable_shared_cache(int);
 
 /*
 ** CAPI3REF:  Attempt To Free Heap Memory
 **
 ** Attempt to free N bytes of heap memory by deallocating non-essential
 ** memory allocations held by the database library (example: memory
 ** used to cache database pages to improve performance).
-**
-** This function is not a part of standard builds.  It is only created
-** if SQLite is compiled with the SQLITE_ENABLE_MEMORY_MANAGEMENT macro.
-*/
-int sqlite3_release_memory(int);
+*/
+SQLITE_API int sqlite3_release_memory(int);
 
 /*
 ** CAPI3REF:  Impose A Limit On Heap Size
 **
-** Place a "soft" limit on the amount of heap memory that may be allocated by
-** SQLite within the current thread. If an internal allocation is requested
-** that would exceed the specified limit, [sqlite3_release_memory()] is invoked
-** one or more times to free up some space before the allocation is made.
-**
-** The limit is called "soft", because if [sqlite3_release_memory()] cannot free
-** sufficient memory to prevent the limit from being exceeded, the memory is
-** allocated anyway and the current operation proceeds.
-**
-** Prior to shutting down a thread sqlite3_soft_heap_limit() must be set to
-** zero (the default) or else the thread will leak memory. Alternatively, use
-** the [sqlite3_thread_cleanup()] API.
+** Place a "soft" limit on the amount of heap memory that may be allocated
+** by SQLite.  If an internal allocation is requested
+** that would exceed the specified limit, [sqlite3_release_memory()] is
+** invoked one or more times to free up some space before the allocation
+** is made.
+**
+** The limit is called "soft", because if [sqlite3_release_memory()] cannot
+** free sufficient memory to prevent the limit from being exceeded,
+** the memory is allocated anyway and the current operation proceeds.
 **
 ** A negative or zero value for N means that there is no soft heap limit and
-** [sqlite3_release_memory()] will only be called when memory is exhaused.
+** [sqlite3_release_memory()] will only be called when memory is exhausted.
 ** The default value for the soft heap limit is zero.
 **
 ** SQLite makes a best effort to honor the soft heap limit.  But if it
 ** is unable to reduce memory usage below the soft limit, execution will
 ** continue without error or notification.  This is why the limit is
 ** called a "soft" limit.  It is advisory only.
 **
-** This function is only available if the library was compiled with the
-** SQLITE_ENABLE_MEMORY_MANAGEMENT option set.
-** memory-management has been enabled.
-*/
-void sqlite3_soft_heap_limit(int);
-
-/*
-** CAPI3REF:  Clean Up Thread Local Storage
-**
-** This routine makes sure that all thread-local storage has been
-** deallocated for the current thread.
-**
-** This routine is not technically necessary.  All thread-local storage
-** will be automatically deallocated once memory-management and
-** shared-cache are disabled and the soft heap limit has been set
-** to zero.  This routine is provided as a convenience for users who
-** want to make absolutely sure they have not forgotten something
-** prior to killing off a thread.
-*/
-void sqlite3_thread_cleanup(void);
+** The soft heap limit is implemented using the [sqlite3_memory_alarm()]
+** interface.  Only a single memory alarm is available in the default
+** implementation.  This means that if the application also uses the
+** memory alarm interface it will interfere with the operation of the
+** soft heap limit and undefined behavior will result.
+**
+** Prior to SQLite version 3.5.0, this routine only constrained the memory
+** allocated by a single thread - the same thread in which this routine
+** runs.  Beginning with SQLite version 3.5.0, the soft heap limit is
+** applied to all threads.  The value specified for the soft heap limit
+** is an upper bound on the total memory allocation for all threads.  In
+** version 3.5.0 there is no mechanism for limiting the heap usage for
+** individual threads.
+*/
+SQLITE_API void sqlite3_soft_heap_limit(int);
 
 /*
 ** CAPI3REF:  Extract Metadata About A Column Of A Table
 **
@@ -2292,9 +2911,9 @@
 **
 ** This API is only available if the library was compiled with the
 ** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined.
 */
-int sqlite3_table_column_metadata(
+SQLITE_API int sqlite3_table_column_metadata(
   sqlite3 *db,                /* Connection handle */
   const char *zDbName,        /* Database name or NULL */
   const char *zTableName,     /* Table name */
   const char *zColumnName,    /* Column name */
@@ -2320,9 +2939,9 @@
 **
 ** Extension loading must be enabled using [sqlite3_enable_load_extension()]
 ** prior to calling this API or an error will be returned.
 */
-int sqlite3_load_extension(
+SQLITE_API int sqlite3_load_extension(
   sqlite3 *db,          /* Load the extension into this database connection */
   const char *zFile,    /* Name of the shared library containing extension */
   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
   char **pzErrMsg       /* Put error message here if not 0 */
@@ -2339,16 +2958,16 @@
 **
 ** Call this routine with onoff==1 to turn extension loading on
 ** and call it with onoff==0 to turn it back off again.
 */
-int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
+SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
 
 /*
 ** CAPI3REF: Make Arrangements To Automatically Load An Extension
 **
 ** Register an extension entry point that is automatically invoked
 ** whenever a new database connection is opened using
-** [sqlite3_open()] or [sqlite3_open16()].
+** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()].
 **
 ** This API can be invoked at program startup in order to register
 ** one or more statically linked extensions that will be available
 ** to all new database connections.
@@ -2366,9 +2985,9 @@
 **
 ** This interface is experimental and is subject to change or
 ** removal in future releases of SQLite.
 */
-int sqlite3_auto_extension(void *xEntryPoint);
+SQLITE_API int sqlite3_auto_extension(void *xEntryPoint);
 
 
 /*
 ** CAPI3REF: Reset Automatic Extension Loading
@@ -2381,9 +3000,9 @@
 **
 ** This interface is experimental and is subject to change or
 ** removal in future releases of SQLite.
 */
-void sqlite3_reset_auto_extension(void);
+SQLITE_API void sqlite3_reset_auto_extension(void);
 
 
 /*
 ****** EXPERIMENTAL - subject to change without notice **************
@@ -2426,10 +3045,10 @@
                 int argc, sqlite3_value **argv);
   int (*xNext)(sqlite3_vtab_cursor*);
   int (*xEof)(sqlite3_vtab_cursor*);
   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
-  int (*xRowid)(sqlite3_vtab_cursor*, sqlite_int64 *pRowid);
-  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite_int64 *);
+  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
+  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
   int (*xBegin)(sqlite3_vtab *pVTab);
   int (*xSync)(sqlite3_vtab *pVTab);
   int (*xCommit)(sqlite3_vtab *pVTab);
   int (*xRollback)(sqlite3_vtab *pVTab);
@@ -2524,9 +3143,9 @@
 ** connection.  Module names must be registered before creating new
 ** virtual tables on the module, or before using preexisting virtual
 ** tables of the module.
 */
-int sqlite3_create_module(
+SQLITE_API int sqlite3_create_module(
   sqlite3 *db,               /* SQLite connection to register module with */
   const char *zName,         /* Name of the module */
   const sqlite3_module *,    /* Methods for the module */
   void *                     /* Client data for xCreate/xConnect */
@@ -2536,9 +3155,9 @@
 ** This routine is identical to the sqlite3_create_module() method above,
 ** except that it allows a destructor function to be specified. It is
 ** even more experimental than the rest of the virtual tables API.
 */
-int sqlite3_create_module_v2(
+SQLITE_API int sqlite3_create_module_v2(
   sqlite3 *db,               /* SQLite connection to register module with */
   const char *zName,         /* Name of the module */
   const sqlite3_module *,    /* Methods for the module */
   void *,                    /* Client data for xCreate/xConnect */
@@ -2587,9 +3206,9 @@
 ** The xCreate and xConnect methods of a module use the following API
 ** to declare the format (the names and datatypes of the columns) of
 ** the virtual tables they implement.
 */
-int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable);
+SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable);
 
 /*
 ** Virtual tables can provide alternative implementations of functions
 ** using the xFindFunction method.  But global versions of those functions
@@ -2605,9 +3224,9 @@
 **
 ** This API should be considered part of the virtual table interface,
 ** which is experimental and subject to change.
 */
-int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
+SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
 
 /*
 ** The interface to the virtual-table mechanism defined above (back up
 ** to a comment remarkably similar to this one) is currently considered
@@ -2627,9 +3246,9 @@
 ** represent an blob-handle.  A blob-handle is created by
 ** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()].
 ** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
 ** can be used to read or write small subsections of the blob.
-** The [sqltie3_blob_size()] interface returns the size of the
+** The [sqlite3_blob_bytes()] interface returns the size of the
 ** blob in bytes.
 */
 typedef struct sqlite3_blob sqlite3_blob;
 
@@ -2654,14 +3273,14 @@
 ** any value written to *ppBlob should not be used by the caller.
 ** This function sets the database-handle error code and message
 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()].
 */
-int sqlite3_blob_open(
+SQLITE_API int sqlite3_blob_open(
   sqlite3*,
   const char *zDb,
   const char *zTable,
   const char *zColumn,
-  sqlite_int64 iRow,
+  sqlite3_int64 iRow,
   int flags,
   sqlite3_blob **ppBlob
 );
 
@@ -2669,17 +3288,17 @@
 ** CAPI3REF:  Close A BLOB Handle
 **
 ** Close an open [sqlite3_blob | blob handle].
 */
-int sqlite3_blob_close(sqlite3_blob *);
+SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
 
 /*
 ** CAPI3REF:  Return The Size Of An Open BLOB
 **
 ** Return the size in bytes of the blob accessible via the open
 ** [sqlite3_blob | blob-handle] passed as an argument.
 */
-int sqlite3_blob_bytes(sqlite3_blob *);
+SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
 
 /*
 ** CAPI3REF:  Read Data From A BLOB Incrementally
 **
@@ -2691,9 +3310,9 @@
 ** On success, SQLITE_OK is returned. Otherwise, an
 ** [SQLITE_ERROR | SQLite error code] or an
 ** [SQLITE_IOERR_READ | extended error code] is returned.
 */
-int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset);
+SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset);
 
 /*
 ** CAPI3REF:  Write Data Into A BLOB Incrementally
 **
@@ -2714,9 +3333,227 @@
 ** On success, SQLITE_OK is returned. Otherwise, an
 ** [SQLITE_ERROR | SQLite error code] or an
 ** [SQLITE_IOERR_READ | extended error code] is returned.
 */
-int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
+SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
+
+/*
+** CAPI3REF:  Virtual File System Objects
+**
+** A virtual filesystem (VFS) is an [sqlite3_vfs] object
+** that SQLite uses to interact
+** with the underlying operating system.  Most builds come with a
+** single default VFS that is appropriate for the host computer.
+** New VFSes can be registered and existing VFSes can be unregistered.
+** The following interfaces are provided.
+**
+** The sqlite3_vfs_find() interface returns a pointer to a VFS given its
+** name.  Names are case sensitive.  If there is no match, a NULL
+** pointer is returned.  If zVfsName is NULL then the default
+** VFS is returned.
+**
+** New VFSes are registered with sqlite3_vfs_register().  Each
+** new VFS becomes the default VFS if the makeDflt flag is set.
+** The same VFS can be registered multiple times without injury.
+** To make an existing VFS into the default VFS, register it again
+** with the makeDflt flag set.  If two different VFSes with the
+** same name are registered, the behavior is undefined.  If a
+** VFS is registered with a name that is NULL or an empty string,
+** then the behavior is undefined.
+**
+** Unregister a VFS with the sqlite3_vfs_unregister() interface.
+** If the default VFS is unregistered, another VFS is chosen as
+** the default.  The choice for the new VFS is arbitrary.
+*/
+SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
+SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
+SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
+
+/*
+** CAPI3REF: Mutexes
+**
+** The SQLite core uses these routines for thread
+** synchronization.  Though they are intended for internal
+** use by SQLite, code that links against SQLite is
+** permitted to use any of these routines.
+**
+** The SQLite source code contains multiple implementations
+** of these mutex routines.  An appropriate implementation
+** is selected automatically at compile-time.  The following
+** implementations are available in the SQLite core:
+**
+** <ul>
+** <li>   SQLITE_MUTEX_OS2
+** <li>   SQLITE_MUTEX_PTHREAD
+** <li>   SQLITE_MUTEX_W32
+** <li>   SQLITE_MUTEX_NOOP
+** </ul>
+**
+** The SQLITE_MUTEX_NOOP implementation is a set of routines
+** that does no real locking and is appropriate for use in
+** a single-threaded application.  The SQLITE_MUTEX_OS2,
+** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
+** are appropriate for use on os/2, unix, and windows.
+**
+** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
+** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
+** implementation is included with the library.  The
+** mutex interface routines defined here become external
+** references in the SQLite library for which implementations
+** must be provided by the application.  This facility allows an
+** application that links against SQLite to provide its own mutex
+** implementation without having to modify the SQLite core.
+**
+** The sqlite3_mutex_alloc() routine allocates a new
+** mutex and returns a pointer to it.  If it returns NULL
+** that means that a mutex could not be allocated.  SQLite
+** will unwind its stack and return an error.  The argument
+** to sqlite3_mutex_alloc() is one of these integer constants:
+**
+** <ul>
+** <li>  SQLITE_MUTEX_FAST
+** <li>  SQLITE_MUTEX_RECURSIVE
+** <li>  SQLITE_MUTEX_STATIC_MASTER
+** <li>  SQLITE_MUTEX_STATIC_MEM
+** <li>  SQLITE_MUTEX_STATIC_MEM2
+** <li>  SQLITE_MUTEX_STATIC_PRNG
+** <li>  SQLITE_MUTEX_STATIC_LRU
+** </ul>
+**
+** The first two constants cause sqlite3_mutex_alloc() to create
+** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
+** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
+** The mutex implementation does not need to make a distinction
+** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
+** not want to.  But SQLite will only request a recursive mutex in
+** cases where it really needs one.  If a faster non-recursive mutex
+** implementation is available on the host platform, the mutex subsystem
+** might return such a mutex in response to SQLITE_MUTEX_FAST.
+**
+** The other allowed parameters to sqlite3_mutex_alloc() each return
+** a pointer to a static preexisting mutex.  Four static mutexes are
+** used by the current version of SQLite.  Future versions of SQLite
+** may add additional static mutexes.  Static mutexes are for internal
+** use by SQLite only.  Applications that use SQLite mutexes should
+** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
+** SQLITE_MUTEX_RECURSIVE.
+**
+** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
+** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
+** returns a different mutex on every call.  But for the static
+** mutex types, the same mutex is returned on every call that has
+** the same type number.
+**
+** The sqlite3_mutex_free() routine deallocates a previously
+** allocated dynamic mutex.  SQLite is careful to deallocate every
+** dynamic mutex that it allocates.  The dynamic mutexes must not be in
+** use when they are deallocated.  Attempting to deallocate a static
+** mutex results in undefined behavior.  SQLite never deallocates
+** a static mutex.
+**
+** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
+** to enter a mutex.  If another thread is already within the mutex,
+** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
+** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
+** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
+** be entered multiple times by the same thread.  In such cases the,
+** mutex must be exited an equal number of times before another thread
+** can enter.  If the same thread tries to enter any other kind of mutex
+** more than once, the behavior is undefined.   SQLite will never exhibit
+** such behavior in its own use of mutexes.
+**
+** Some systems (ex: windows95) do not the operation implemented by
+** sqlite3_mutex_try().  On those systems, sqlite3_mutex_try() will
+** always return SQLITE_BUSY.  The SQLite core only ever uses
+** sqlite3_mutex_try() as an optimization so this is acceptable behavior.
+**
+** The sqlite3_mutex_leave() routine exits a mutex that was
+** previously entered by the same thread.  The behavior
+** is undefined if the mutex is not currently entered by the
+** calling thread or is not currently allocated.  SQLite will
+** never do either.
+**
+** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
+*/
+SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
+SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
+SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
+SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
+SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
+
+/*
+** CAPI3REF: Mutex Verifcation Routines
+**
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
+** are intended for use inside assert() statements.  The SQLite core
+** never uses these routines except inside an assert() and applications
+** are advised to follow the lead of the core.  The core only
+** provides implementations for these routines when it is compiled
+** with the SQLITE_DEBUG flag.  External mutex implementations
+** are only required to provide these routines if SQLITE_DEBUG is
+** defined and if NDEBUG is not defined.
+**
+** These routines should return true if the mutex in their argument
+** is held or not held, respectively, by the calling thread.
+**
+** The implementation is not required to provided versions of these
+** routines that actually work.
+** If the implementation does not provide working
+** versions of these routines, it should at least provide stubs
+** that always return true so that one does not get spurious
+** assertion failures.
+**
+** If the argument to sqlite3_mutex_held() is a NULL pointer then
+** the routine should return 1.  This seems counter-intuitive since
+** clearly the mutex cannot be held if it does not exist.  But the
+** the reason the mutex does not exist is because the build is not
+** using mutexes.  And we do not want the assert() containing the
+** call to sqlite3_mutex_held() to fail, so a non-zero return is
+** the appropriate thing to do.  The sqlite3_mutex_notheld()
+** interface should also return 1 when given a NULL pointer.
+*/
+SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
+SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
+
+/*
+** CAPI3REF: Mutex Types
+**
+** The [sqlite3_mutex_alloc()] interface takes a single argument
+** which is one of these integer constants.
+*/
+#define SQLITE_MUTEX_FAST             0
+#define SQLITE_MUTEX_RECURSIVE        1
+#define SQLITE_MUTEX_STATIC_MASTER    2
+#define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
+#define SQLITE_MUTEX_STATIC_MEM2      4  /* sqlite3_release_memory() */
+#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
+#define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
+
+/*
+** CAPI3REF: Low-Level Control Of Database Files
+**
+** The [sqlite3_file_control()] interface makes a direct call to the
+** xFileControl method for the [sqlite3_io_methods] object associated
+** with a particular database identified by the second argument.  The
+** name of the database is the name assigned to the database by the
+** <a href="lang_attach.html">ATTACH</a> SQL command that opened the
+** database.  To control the main database file, use the name "main"
+** or a NULL pointer.  The third and fourth parameters to this routine
+** are passed directly through to the second and third parameters of
+** the xFileControl method.  The return value of the xFileControl
+** method becomes the return value of this routine.
+**
+** If the second parameter (zDbName) does not match the name of any
+** open database file, then SQLITE_ERROR is returned.  This error
+** code is not remembered and will not be recalled by [sqlite3_errcode()]
+** or [sqlite3_errmsg()].  The underlying xFileControl method might
+** also return SQLITE_ERROR.  There is no way to distinguish between
+** an incorrect zDbName and an SQLITE_ERROR return from the underlying
+** xFileControl method.
+**
+** See also: [SQLITE_FCNTL_LOCKSTATE]
+*/
+SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
 
 /*
 ** Undo the hack that converts floating point types to integer for
 ** builds on processors without floating point support.
@@ -2749,9 +3586,9 @@
 ** There is only one exported symbol in this file - the function
 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
 ** All other code has file scope.
 **
-** $Id: date.c,v 1.66 2007/05/08 21:56:00 drh Exp $
+** $Id: date.c,v 1.73 2007/09/12 17:01:45 danielk1977 Exp $
 **
 ** SQLite processes all times and dates as Julian Day numbers.  The
 ** dates and times are stored as the number of days since noon
 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
@@ -2793,9 +3630,9 @@
 **
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.580 2007/07/23 19:31:17 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.608 2007/09/03 15:19:35 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
@@ -2813,9 +3650,9 @@
 *************************************************************************
 **
 ** This file defines various limits of what SQLite can process.
 **
-** @(#) $Id: sqliteLimit.h,v 1.1 2007/06/19 15:23:48 drh Exp $
+** @(#) $Id: sqliteLimit.h,v 1.2 2007/08/24 11:52:29 danielk1977 Exp $
 */
 
 /*
 ** The maximum length of a TEXT or BLOB in bytes.   This also
@@ -2931,8 +3768,19 @@
 #ifndef SQLITE_DEFAULT_PAGE_SIZE
 # define SQLITE_DEFAULT_PAGE_SIZE 1024
 #endif
 
+/*
+** Ordinarily, if no value is explicitly provided, SQLite creates databases
+** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
+** device characteristics (sector-size and atomic write() support),
+** SQLite may choose a larger value. This constant is the maximum value
+** SQLite will choose on it's own.
+*/
+#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
+# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
+#endif
+
 /* Maximum page size.  The upper bound on this value is 32768.  This a limit
 ** imposed by the necessity of storing the value in a 2-byte unsigned integer
 ** and the fact that the page size must be a power of 2.
 */
@@ -2961,8 +3809,51 @@
 
 /************** End of sqliteLimit.h *****************************************/
 /************** Continuing where we left off in sqliteInt.h ******************/
 
+/*
+** For testing purposes, the various size limit constants are really
+** variables that we can modify in the testfixture.
+*/
+#ifdef SQLITE_TEST
+  #undef SQLITE_MAX_LENGTH
+  #undef SQLITE_MAX_COLUMN
+  #undef SQLITE_MAX_SQL_LENGTH
+  #undef SQLITE_MAX_EXPR_DEPTH
+  #undef SQLITE_MAX_COMPOUND_SELECT
+  #undef SQLITE_MAX_VDBE_OP
+  #undef SQLITE_MAX_FUNCTION_ARG
+  #undef SQLITE_MAX_VARIABLE_NUMBER
+  #undef SQLITE_MAX_PAGE_SIZE
+  #undef SQLITE_MAX_PAGE_COUNT
+  #undef SQLITE_MAX_LIKE_PATTERN_LENGTH
+
+  #define SQLITE_MAX_LENGTH              sqlite3MAX_LENGTH
+  #define SQLITE_MAX_COLUMN              sqlite3MAX_COLUMN
+  #define SQLITE_MAX_SQL_LENGTH          sqlite3MAX_SQL_LENGTH
+  #define SQLITE_MAX_EXPR_DEPTH          sqlite3MAX_EXPR_DEPTH
+  #define SQLITE_MAX_COMPOUND_SELECT     sqlite3MAX_COMPOUND_SELECT
+  #define SQLITE_MAX_VDBE_OP             sqlite3MAX_VDBE_OP
+  #define SQLITE_MAX_FUNCTION_ARG        sqlite3MAX_FUNCTION_ARG
+  #define SQLITE_MAX_VARIABLE_NUMBER     sqlite3MAX_VARIABLE_NUMBER
+  #define SQLITE_MAX_PAGE_SIZE           sqlite3MAX_PAGE_SIZE
+  #define SQLITE_MAX_PAGE_COUNT          sqlite3MAX_PAGE_COUNT
+  #define SQLITE_MAX_LIKE_PATTERN_LENGTH sqlite3MAX_LIKE_PATTERN_LENGTH
+
+  extern int sqlite3MAX_LENGTH;
+  extern int sqlite3MAX_COLUMN;
+  extern int sqlite3MAX_SQL_LENGTH;
+  extern int sqlite3MAX_EXPR_DEPTH;
+  extern int sqlite3MAX_COMPOUND_SELECT;
+  extern int sqlite3MAX_VDBE_OP;
+  extern int sqlite3MAX_FUNCTION_ARG;
+  extern int sqlite3MAX_VARIABLE_NUMBER;
+  extern int sqlite3MAX_PAGE_SIZE;
+  extern int sqlite3MAX_PAGE_COUNT;
+  extern int sqlite3MAX_LIKE_PATTERN_LENGTH;
+#endif
+
+#define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
 
 #if defined(SQLITE_TCL) || defined(TCLSH)
 # include <tcl.h>
 #endif
@@ -2975,8 +3866,21 @@
 ** feature.
 */
 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
 # define NDEBUG 1
+#endif
+
+/*
+** The SQLITE_THREADSAFE macro must be defined as either 0 or 1.
+** Older versions of SQLite used an optional THREADSAFE macro.
+** We support that for legacy
+*/
+#if !defined(SQLITE_THREADSAFE)
+#if defined(THREADSAFE)
+# define SQLITE_THREADSAFE THREADSAFE
+#else
+# define SQLITE_THREADSAFE 1
+#endif
 #endif
 
 /*
 ** These #defines should enable >2GB file support on Posix if the
@@ -3016,9 +3920,9 @@
 *************************************************************************
 ** This is the header file for the generic hash-table implemenation
 ** used in SQLite.
 **
-** $Id: hash.h,v 1.9 2006/02/14 10:48:39 danielk1977 Exp $
+** $Id: hash.h,v 1.11 2007/09/04 14:31:47 danielk1977 Exp $
 */
 #ifndef _SQLITE_HASH_H_
 #define _SQLITE_HASH_H_
 
@@ -3037,12 +3941,10 @@
 struct Hash {
   char keyClass;          /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
   char copyKey;           /* True if copy of key made on insert */
   int count;              /* Number of entries in this table */
-  HashElem *first;        /* The first element of the array */
-  void *(*xMalloc)(int);  /* malloc() function to use */
-  void (*xFree)(void *);  /* free() function to use */
   int htsize;             /* Number of buckets in the hash table */
+  HashElem *first;        /* The first element of the array */
   struct _ht {            /* the hash table */
     int count;               /* Number of entries with this hash */
     HashElem *chain;         /* Pointer to first entry with this hash */
   } *ht;
@@ -3087,8 +3989,9 @@
 */
 SQLITE_PRIVATE void sqlite3HashInit(Hash*, int keytype, int copyKey);
 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const void *pKey, int nKey, void *pData);
 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const void *pKey, int nKey);
+SQLITE_PRIVATE HashElem *sqlite3HashFindElem(const Hash*, const void *pKey, int nKey);
 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
 
 /*
 ** Macros for looping over all elements of a hash table.  The idiom is
@@ -3398,9 +4301,13 @@
 /*
 ** Macros to determine whether the machine is big or little endian,
 ** evaluated at runtime.
 */
-extern const int sqlite3one;
+#ifdef SQLITE_AMALGAMATION
+SQLITE_PRIVATE const int sqlite3One;
+#else
+SQLITE_PRIVATE const int sqlite3one;
+#endif
 #if defined(i386) || defined(__i386__) || defined(_M_IX86)
 # define SQLITE_BIGENDIAN    0
 # define SQLITE_LITTLEENDIAN 1
 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
@@ -3429,8 +4336,217 @@
 /*
 ** Defer sourcing vdbe.h and btree.h until after the "u8" and
 ** "BusyHandler typedefs.
 */
+/************** Include btree.h in the middle of sqliteInt.h *****************/
+/************** Begin file btree.h *******************************************/
+/*
+** 2001 September 15
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This header file defines the interface that the sqlite B-Tree file
+** subsystem.  See comments in the source code for a detailed description
+** of what each interface routine does.
+**
+** @(#) $Id: btree.h,v 1.93 2007/09/03 15:19:35 drh Exp $
+*/
+#ifndef _BTREE_H_
+#define _BTREE_H_
+
+/* TODO: This definition is just included so other modules compile. It
+** needs to be revisited.
+*/
+#define SQLITE_N_BTREE_META 10
+
+/*
+** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
+** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
+*/
+#ifndef SQLITE_DEFAULT_AUTOVACUUM
+  #define SQLITE_DEFAULT_AUTOVACUUM 0
+#endif
+
+#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
+#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
+#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
+
+/*
+** Forward declarations of structure
+*/
+typedef struct Btree Btree;
+typedef struct BtCursor BtCursor;
+typedef struct BtShared BtShared;
+typedef struct BtreeMutexArray BtreeMutexArray;
+
+/*
+** This structure records all of the Btrees that need to hold
+** a mutex before we enter sqlite3VdbeExec().  The Btrees are
+** are placed in aBtree[] in order of aBtree[]->pBt.  That way,
+** we can always lock and unlock them all quickly.
+*/
+struct BtreeMutexArray {
+  int nMutex;
+  Btree *aBtree[SQLITE_MAX_ATTACHED+1];
+};
+
+
+SQLITE_PRIVATE int sqlite3BtreeOpen(
+  const char *zFilename,   /* Name of database file to open */
+  sqlite3 *db,             /* Associated database connection */
+  Btree **,                /* Return open Btree* here */
+  int flags,               /* Flags */
+  int vfsFlags             /* Flags passed through to VFS open */
+);
+
+/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
+** following values.
+**
+** NOTE:  These values must match the corresponding PAGER_ values in
+** pager.h.
+*/
+#define BTREE_OMIT_JOURNAL  1  /* Do not use journal.  No argument */
+#define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
+#define BTREE_MEMORY        4  /* In-memory DB.  No argument */
+#define BTREE_READONLY      8  /* Open the database in read-only mode */
+#define BTREE_READWRITE    16  /* Open for both reading and writing */
+#define BTREE_CREATE       32  /* Create the database if it does not exist */
+
+/* Additional values for the 4th argument of sqlite3BtreeOpen that
+** are not associated with PAGER_ values.
+*/
+#define BTREE_PRIVATE      64  /* Never share with other connections */
+
+SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeSetBusyHandler(Btree*,BusyHandler*);
+SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
+SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int);
+SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree*,int,int);
+SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
+SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
+SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
+SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
+SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
+SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeCommitStmt(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeRollbackStmt(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
+SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeIsInStmt(Btree*);
+SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
+SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
+SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *);
+SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *, int, u8);
+
+SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
+SQLITE_PRIVATE const char *sqlite3BtreeGetDirname(Btree *);
+SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
+SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
+
+SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
+
+/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
+** of the following flags:
+*/
+#define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
+#define BTREE_ZERODATA   2    /* Table has keys only - no data */
+#define BTREE_LEAFDATA   4    /* Data stored in leaves only.  Implies INTKEY */
+
+SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
+SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int);
+SQLITE_PRIVATE int sqlite3BtreeGetMeta(Btree*, int idx, u32 *pValue);
+SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
+SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
+
+SQLITE_PRIVATE int sqlite3BtreeCursor(
+  Btree*,                              /* BTree containing table to open */
+  int iTable,                          /* Index of root page */
+  int wrFlag,                          /* 1 for writing.  0 for read-only */
+  int(*)(void*,int,const void*,int,const void*),  /* Key comparison function */
+  void*,                               /* First argument to compare function */
+  BtCursor **ppCursor                  /* Returned cursor */
+);
+
+SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
+SQLITE_PRIVATE int sqlite3BtreeMoveto(BtCursor*,const void *pKey,i64 nKey,int bias,int *pRes);
+SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
+SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
+                                  const void *pData, int nData,
+                                  int nZero, int bias);
+SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
+SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
+SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
+SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
+SQLITE_PRIVATE int sqlite3BtreeFlags(BtCursor*);
+SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
+SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
+SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
+SQLITE_PRIVATE sqlite3 *sqlite3BtreeCursorDb(const BtCursor*);
+SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
+SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
+SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
+SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
+
+SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
+SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
+
+SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
+SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
+
+#ifdef SQLITE_TEST
+SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
+SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
+SQLITE_PRIVATE int sqlite3BtreePageDump(Btree*, int, int recursive);
+#endif
+
+/*
+** If we are not using shared cache, then there is no need to
+** use mutexes to access the BtShared structures.  So make the
+** Enter and Leave procedures no-ops.
+*/
+#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
+SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
+SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
+SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
+SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
+SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
+SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
+SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
+SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
+SQLITE_PRIVATE   void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
+SQLITE_PRIVATE   void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
+SQLITE_PRIVATE   void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
+#else
+# define sqlite3BtreeEnter(X)
+# define sqlite3BtreeLeave(X)
+# define sqlite3BtreeHoldsMutex(X) 1
+# define sqlite3BtreeEnterCursor(X)
+# define sqlite3BtreeLeaveCursor(X)
+# define sqlite3BtreeEnterAll(X)
+# define sqlite3BtreeLeaveAll(X)
+# define sqlite3BtreeHoldsAllMutexes(X) 1
+# define sqlite3BtreeMutexArrayEnter(X)
+# define sqlite3BtreeMutexArrayLeave(X)
+# define sqlite3BtreeMutexArrayInsert(X,Y)
+#endif
+
+
+#endif /* _BTREE_H_ */
+
+/************** End of btree.h ***********************************************/
+/************** Continuing where we left off in sqliteInt.h ******************/
 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
 /************** Begin file vdbe.h ********************************************/
 /*
 ** 2001 September 15
@@ -3448,9 +4564,9 @@
 ** This header defines the interface to the virtual database engine
 ** or VDBE.  The VDBE implements an abstract machine that runs a
 ** simple program to access and modify the underlying database.
 **
-** $Id: vdbe.h,v 1.110 2007/05/08 21:45:28 drh Exp $
+** $Id: vdbe.h,v 1.113 2007/08/30 01:19:59 drh Exp $
 */
 #ifndef _SQLITE_VDBE_H_
 #define _SQLITE_VDBE_H_
 
@@ -3716,8 +4832,9 @@
 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, const char *zP1, int N);
+SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int);
@@ -3747,162 +4864,8 @@
 #endif
 
 /************** End of vdbe.h ************************************************/
 /************** Continuing where we left off in sqliteInt.h ******************/
-/************** Include btree.h in the middle of sqliteInt.h *****************/
-/************** Begin file btree.h *******************************************/
-/*
-** 2001 September 15
-**
-** The author disclaims copyright to this source code.  In place of
-** a legal notice, here is a blessing:
-**
-**    May you do good and not evil.
-**    May you find forgiveness for yourself and forgive others.
-**    May you share freely, never taking more than you give.
-**
-*************************************************************************
-** This header file defines the interface that the sqlite B-Tree file
-** subsystem.  See comments in the source code for a detailed description
-** of what each interface routine does.
-**
-** @(#) $Id: btree.h,v 1.82 2007/05/08 21:45:27 drh Exp $
-*/
-#ifndef _BTREE_H_
-#define _BTREE_H_
-
-/* TODO: This definition is just included so other modules compile. It
-** needs to be revisited.
-*/
-#define SQLITE_N_BTREE_META 10
-
-/*
-** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
-** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
-*/
-#ifndef SQLITE_DEFAULT_AUTOVACUUM
-  #define SQLITE_DEFAULT_AUTOVACUUM 0
-#endif
-
-#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
-#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
-#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
-
-/*
-** Forward declarations of structure
-*/
-typedef struct Btree Btree;
-typedef struct BtCursor BtCursor;
-typedef struct BtShared BtShared;
-
-
-SQLITE_PRIVATE int sqlite3BtreeOpen(
-  const char *zFilename,   /* Name of database file to open */
-  sqlite3 *db,             /* Associated database connection */
-  Btree **,                /* Return open Btree* here */
-  int flags                /* Flags */
-);
-
-/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
-** following values.
-**
-** NOTE:  These values must match the corresponding PAGER_ values in
-** pager.h.
-*/
-#define BTREE_OMIT_JOURNAL  1  /* Do not use journal.  No argument */
-#define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
-#define BTREE_MEMORY        4  /* In-memory DB.  No argument */
-
-SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeSetBusyHandler(Btree*,BusyHandler*);
-SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
-SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int);
-SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree*,int,int);
-SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
-SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
-SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
-SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
-SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
-SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeCommitStmt(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeRollbackStmt(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
-SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeIsInStmt(Btree*);
-SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
-SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
-SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *);
-SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *, int, u8);
-
-SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
-SQLITE_PRIVATE const char *sqlite3BtreeGetDirname(Btree *);
-SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
-SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
-
-SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
-
-/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
-** of the following flags:
-*/
-#define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
-#define BTREE_ZERODATA   2    /* Table has keys only - no data */
-#define BTREE_LEAFDATA   4    /* Data stored in leaves only.  Implies INTKEY */
-
-SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
-SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int);
-SQLITE_PRIVATE int sqlite3BtreeGetMeta(Btree*, int idx, u32 *pValue);
-SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
-
-SQLITE_PRIVATE int sqlite3BtreeCursor(
-  Btree*,                              /* BTree containing table to open */
-  int iTable,                          /* Index of root page */
-  int wrFlag,                          /* 1 for writing.  0 for read-only */
-  int(*)(void*,int,const void*,int,const void*),  /* Key comparison function */
-  void*,                               /* First argument to compare function */
-  BtCursor **ppCursor                  /* Returned cursor */
-);
-
-SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
-SQLITE_PRIVATE int sqlite3BtreeMoveto(BtCursor*,const void *pKey,i64 nKey,int bias,int *pRes);
-SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
-SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
-                                  const void *pData, int nData,
-                                  int nZero, int bias);
-SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
-SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
-SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
-SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
-SQLITE_PRIVATE int sqlite3BtreeFlags(BtCursor*);
-SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
-SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
-SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
-SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
-SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
-SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
-SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
-
-SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
-SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
-
-SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
-SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
-
-#ifdef SQLITE_TEST
-SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
-SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
-SQLITE_PRIVATE int sqlite3BtreePageDump(Btree*, int, int recursive);
-#endif
-
-#endif /* _BTREE_H_ */
-
-/************** End of btree.h ***********************************************/
-/************** Continuing where we left off in sqliteInt.h ******************/
 /************** Include pager.h in the middle of sqliteInt.h *****************/
 /************** Begin file pager.h *******************************************/
 /*
 ** 2001 September 15
@@ -3918,9 +4881,9 @@
 ** This header file defines the interface that the sqlite page cache
 ** subsystem.  The page cache subsystem reads and writes a file a page
 ** at a time and provides a journal for rollback.
 **
-** @(#) $Id: pager.h,v 1.61 2007/05/08 21:45:28 drh Exp $
+** @(#) $Id: pager.h,v 1.67 2007/09/03 15:19:35 drh Exp $
 */
 
 #ifndef _PAGER_H_
 #define _PAGER_H_
@@ -3959,14 +4922,13 @@
 /*
 ** See source code comments for a detailed description of the following
 ** routines:
 */
-SQLITE_PRIVATE int sqlite3PagerOpen(Pager **ppPager, const char *zFilename,
-                     int nExtra, int flags);
+SQLITE_PRIVATE int sqlite3PagerOpen(sqlite3_vfs *, Pager **ppPager, const char*, int,int,int);
 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, BusyHandler *pBusyHandler);
 SQLITE_PRIVATE void sqlite3PagerSetDestructor(Pager*, void(*)(DbPage*,int));
 SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*,int));
-SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, int);
+SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*);
 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
@@ -3991,8 +4953,10 @@
 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
+SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
+SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
 SQLITE_PRIVATE const char *sqlite3PagerDirname(Pager*);
 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno);
@@ -4010,12 +4974,8 @@
 
 #if !defined(NDEBUG) || defined(SQLITE_TEST)
 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
-#endif
-
-#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
-SQLITE_PRIVATE   int sqlite3PagerLockstate(Pager*);
 #endif
 
 #ifdef SQLITE_TEST
 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
@@ -4035,76 +4995,8 @@
 
 /************** End of pager.h ***********************************************/
 /************** Continuing where we left off in sqliteInt.h ******************/
 
-#ifdef SQLITE_MEMDEBUG
-/*
-** The following global variables are used for testing and debugging
-** only.  They only work if SQLITE_MEMDEBUG is defined.
-*/
-extern int sqlite3_nMalloc;      /* Number of sqliteMalloc() calls */
-extern int sqlite3_nFree;        /* Number of sqliteFree() calls */
-extern int sqlite3_iMallocFail;  /* Fail sqliteMalloc() after this many calls */
-extern int sqlite3_iMallocReset; /* Set iMallocFail to this when it reaches 0 */
-
-extern void *sqlite3_pFirst;         /* Pointer to linked list of allocations */
-extern int sqlite3_nMaxAlloc;        /* High water mark of ThreadData.nAlloc */
-extern int sqlite3_mallocDisallowed; /* assert() in sqlite3Malloc() if set */
-extern int sqlite3_isFail;           /* True if all malloc calls should fail */
-extern const char *sqlite3_zFile;    /* Filename to associate debug info with */
-extern int sqlite3_iLine;            /* Line number for debug info */
-
-#define ENTER_MALLOC (sqlite3_zFile = __FILE__, sqlite3_iLine = __LINE__)
-#define sqliteMalloc(x)          (ENTER_MALLOC, sqlite3Malloc(x,1))
-#define sqliteMallocRaw(x)       (ENTER_MALLOC, sqlite3MallocRaw(x,1))
-#define sqliteRealloc(x,y)       (ENTER_MALLOC, sqlite3Realloc(x,y))
-#define sqliteStrDup(x)          (ENTER_MALLOC, sqlite3StrDup(x))
-#define sqliteStrNDup(x,y)       (ENTER_MALLOC, sqlite3StrNDup(x,y))
-#define sqliteReallocOrFree(x,y) (ENTER_MALLOC, sqlite3ReallocOrFree(x,y))
-
-#else
-
-#define ENTER_MALLOC 0
-#define sqliteMalloc(x)          sqlite3Malloc(x,1)
-#define sqliteMallocRaw(x)       sqlite3MallocRaw(x,1)
-#define sqliteRealloc(x,y)       sqlite3Realloc(x,y)
-#define sqliteStrDup(x)          sqlite3StrDup(x)
-#define sqliteStrNDup(x,y)       sqlite3StrNDup(x,y)
-#define sqliteReallocOrFree(x,y) sqlite3ReallocOrFree(x,y)
-
-#endif
-
-/* Variable sqlite3_mallocHasFailed is set to true after a malloc()
-** failure occurs.
-**
-** The sqlite3MallocFailed() macro returns true if a malloc has failed
-** in this thread since the last call to sqlite3ApiExit(), or false
-** otherwise.
-*/
-extern int sqlite3_mallocHasFailed;
-#define sqlite3MallocFailed() (sqlite3_mallocHasFailed && sqlite3OsInMutex(1))
-
-#define sqliteFree(x)          sqlite3FreeX(x)
-#define sqliteAllocSize(x)     sqlite3AllocSize(x)
-
-/*
-** An instance of this structure might be allocated to store information
-** specific to a single thread.
-*/
-struct ThreadData {
-  int dummy;               /* So that this structure is never empty */
-
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  int nSoftHeapLimit;      /* Suggested max mem allocation.  No limit if <0 */
-  int nAlloc;              /* Number of bytes currently allocated */
-  Pager *pPager;           /* Linked list of all pagers in this thread */
-#endif
-
-#ifndef SQLITE_OMIT_SHARED_CACHE
-  u8 useSharedData;        /* True if shared pagers and schemas are enabled */
-  BtShared *pBtree;        /* Linked list of all currently open BTrees */
-#endif
-};
 
 /*
 ** Name of the master database table.  The master database table
 ** is a special table that holds the names and attributes of all
@@ -4150,9 +5042,8 @@
 typedef struct NameContext NameContext;
 typedef struct Parse Parse;
 typedef struct Select Select;
 typedef struct SrcList SrcList;
-typedef struct ThreadData ThreadData;
 typedef struct Table Table;
 typedef struct TableLock TableLock;
 typedef struct Token Token;
 typedef struct TriggerStack TriggerStack;
@@ -4177,15 +5068,20 @@
 **
 ** This header file (together with is companion C source-code file
 ** "os.c") attempt to abstract the underlying operating system so that
 ** the SQLite library will work on both POSIX and windows systems.
+**
+** This header file is #include-ed by sqliteInt.h and thus ends up
+** being included by every source file.
 */
 #ifndef _SQLITE_OS_H_
 #define _SQLITE_OS_H_
 
 /*
 ** Figure out if we are dealing with Unix, Windows, or some other
-** operating system.
+** operating system.  After the following block of preprocess macros,
+** all of OS_UNIX, OS_WIN, OS_OS2, and OS_OTHER will defined to either
+** 1 or 0.  One of the four will be 1.  The other three will be 0.
 */
 #if defined(OS_OTHER)
 # if OS_OTHER==1
 #   undef OS_UNIX
@@ -4222,8 +5118,9 @@
 # ifndef OS_WIN
 #  define OS_WIN 0
 # endif
 #endif
+
 
 
 /*
 ** Define the maximum size of a temporary filename
@@ -4267,9 +5164,9 @@
 ** OS's standard temporary file directory, and are deleted prior to exit.
 ** If sqlite is being embedded in another program, you may wish to change the
 ** prefix to reflect your program's name, so that if your program exits
 ** prematurely, old temporary files can be easily identified. This can be done
-** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
+** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
 **
 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
 ** Mcafee started using SQLite in their anti-virus product and it
 ** started putting files with the "sqlite" name in the c:/temp folder.
@@ -4281,93 +5178,11 @@
 ** anybody smart enough to figure out the code is also likely smart
 ** enough to know that calling the developer will not help get rid
 ** of the file.
 */
-#ifndef TEMP_FILE_PREFIX
-# define TEMP_FILE_PREFIX "etilqs_"
-#endif
-
-/*
-** Define the interfaces for Unix, Windows, and OS/2.
-*/
-#if OS_UNIX
-#define sqlite3OsOpenReadWrite      sqlite3UnixOpenReadWrite
-#define sqlite3OsOpenExclusive      sqlite3UnixOpenExclusive
-#define sqlite3OsOpenReadOnly       sqlite3UnixOpenReadOnly
-#define sqlite3OsDelete             sqlite3UnixDelete
-#define sqlite3OsFileExists         sqlite3UnixFileExists
-#define sqlite3OsFullPathname       sqlite3UnixFullPathname
-#define sqlite3OsIsDirWritable      sqlite3UnixIsDirWritable
-#define sqlite3OsSyncDirectory      sqlite3UnixSyncDirectory
-#define sqlite3OsTempFileName       sqlite3UnixTempFileName
-#define sqlite3OsRandomSeed         sqlite3UnixRandomSeed
-#define sqlite3OsSleep              sqlite3UnixSleep
-#define sqlite3OsCurrentTime        sqlite3UnixCurrentTime
-#define sqlite3OsEnterMutex         sqlite3UnixEnterMutex
-#define sqlite3OsLeaveMutex         sqlite3UnixLeaveMutex
-#define sqlite3OsInMutex            sqlite3UnixInMutex
-#define sqlite3OsThreadSpecificData sqlite3UnixThreadSpecificData
-#define sqlite3OsMalloc             sqlite3GenericMalloc
-#define sqlite3OsRealloc            sqlite3GenericRealloc
-#define sqlite3OsFree               sqlite3GenericFree
-#define sqlite3OsAllocationSize     sqlite3GenericAllocationSize
-#define sqlite3OsDlopen             sqlite3UnixDlopen
-#define sqlite3OsDlsym              sqlite3UnixDlsym
-#define sqlite3OsDlclose            sqlite3UnixDlclose
-#endif
-#if OS_WIN
-#define sqlite3OsOpenReadWrite      sqlite3WinOpenReadWrite
-#define sqlite3OsOpenExclusive      sqlite3WinOpenExclusive
-#define sqlite3OsOpenReadOnly       sqlite3WinOpenReadOnly
-#define sqlite3OsDelete             sqlite3WinDelete
-#define sqlite3OsFileExists         sqlite3WinFileExists
-#define sqlite3OsFullPathname       sqlite3WinFullPathname
-#define sqlite3OsIsDirWritable      sqlite3WinIsDirWritable
-#define sqlite3OsSyncDirectory      sqlite3WinSyncDirectory
-#define sqlite3OsTempFileName       sqlite3WinTempFileName
-#define sqlite3OsRandomSeed         sqlite3WinRandomSeed
-#define sqlite3OsSleep              sqlite3WinSleep
-#define sqlite3OsCurrentTime        sqlite3WinCurrentTime
-#define sqlite3OsEnterMutex         sqlite3WinEnterMutex
-#define sqlite3OsLeaveMutex         sqlite3WinLeaveMutex
-#define sqlite3OsInMutex            sqlite3WinInMutex
-#define sqlite3OsThreadSpecificData sqlite3WinThreadSpecificData
-#define sqlite3OsMalloc             sqlite3GenericMalloc
-#define sqlite3OsRealloc            sqlite3GenericRealloc
-#define sqlite3OsFree               sqlite3GenericFree
-#define sqlite3OsAllocationSize     sqlite3GenericAllocationSize
-#define sqlite3OsDlopen             sqlite3WinDlopen
-#define sqlite3OsDlsym              sqlite3WinDlsym
-#define sqlite3OsDlclose            sqlite3WinDlclose
-#endif
-#if OS_OS2
-#define sqlite3OsOpenReadWrite      sqlite3Os2OpenReadWrite
-#define sqlite3OsOpenExclusive      sqlite3Os2OpenExclusive
-#define sqlite3OsOpenReadOnly       sqlite3Os2OpenReadOnly
-#define sqlite3OsDelete             sqlite3Os2Delete
-#define sqlite3OsFileExists         sqlite3Os2FileExists
-#define sqlite3OsFullPathname       sqlite3Os2FullPathname
-#define sqlite3OsIsDirWritable      sqlite3Os2IsDirWritable
-#define sqlite3OsSyncDirectory      sqlite3Os2SyncDirectory
-#define sqlite3OsTempFileName       sqlite3Os2TempFileName
-#define sqlite3OsRandomSeed         sqlite3Os2RandomSeed
-#define sqlite3OsSleep              sqlite3Os2Sleep
-#define sqlite3OsCurrentTime        sqlite3Os2CurrentTime
-#define sqlite3OsEnterMutex         sqlite3Os2EnterMutex
-#define sqlite3OsLeaveMutex         sqlite3Os2LeaveMutex
-#define sqlite3OsInMutex            sqlite3Os2InMutex
-#define sqlite3OsThreadSpecificData sqlite3Os2ThreadSpecificData
-#define sqlite3OsMalloc             sqlite3GenericMalloc
-#define sqlite3OsRealloc            sqlite3GenericRealloc
-#define sqlite3OsFree               sqlite3GenericFree
-#define sqlite3OsAllocationSize     sqlite3GenericAllocationSize
-#define sqlite3OsDlopen             sqlite3Os2Dlopen
-#define sqlite3OsDlsym              sqlite3Os2Dlsym
-#define sqlite3OsDlclose            sqlite3Os2Dlclose
-#endif
-
-
-
+#ifndef SQLITE_TEMP_FILE_PREFIX
+# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
+#endif
 
 /*
 ** If using an alternative OS interface, then we must have an "os_other.h"
 ** header file available for that interface.  Presumably the "os_other.h"
@@ -4376,49 +5191,8 @@
 #if OS_OTHER
 # include "os_other.h"
 #endif
 
-
-
-/*
-** Forward declarations
-*/
-typedef struct OsFile OsFile;
-typedef struct IoMethod IoMethod;
-
-/*
-** An instance of the following structure contains pointers to all
-** methods on an OsFile object.
-*/
-struct IoMethod {
-  int (*xClose)(OsFile**);
-  int (*xOpenDirectory)(OsFile*, const char*);
-  int (*xRead)(OsFile*, void*, int amt);
-  int (*xWrite)(OsFile*, const void*, int amt);
-  int (*xSeek)(OsFile*, i64 offset);
-  int (*xTruncate)(OsFile*, i64 size);
-  int (*xSync)(OsFile*, int);
-  void (*xSetFullSync)(OsFile *id, int setting);
-  int (*xFileHandle)(OsFile *id);
-  int (*xFileSize)(OsFile*, i64 *pSize);
-  int (*xLock)(OsFile*, int);
-  int (*xUnlock)(OsFile*, int);
-  int (*xLockState)(OsFile *id);
-  int (*xCheckReservedLock)(OsFile *id);
-  int (*xSectorSize)(OsFile *id);
-};
-
-/*
-** The OsFile object describes an open disk file in an OS-dependent way.
-** The version of OsFile defined here is a generic version.  Each OS
-** implementation defines its own subclass of this structure that contains
-** additional information needed to handle file I/O.  But the pMethod
-** entry (pointing to the virtual function table) always occurs first
-** so that we can always find the appropriate methods.
-*/
-struct OsFile {
-  IoMethod const *pMethod;
-};
 
 /*
 ** The following values may be passed as the second argument to
 ** sqlite3OsLock(). The various locks exhibit the following semantics:
@@ -4501,9 +5275,9 @@
 */
 #ifndef SQLITE_TEST
 #define PENDING_BYTE      0x40000000  /* First byte past the 1GB boundary */
 #else
-extern unsigned int sqlite3_pending_byte;
+SQLITE_API extern unsigned int sqlite3_pending_byte;
 #define PENDING_BYTE sqlite3_pending_byte
 #endif
 
 #define RESERVED_BYTE     (PENDING_BYTE+1)
@@ -4510,210 +5284,150 @@
 #define SHARED_FIRST      (PENDING_BYTE+2)
 #define SHARED_SIZE       510
 
 /*
-** Prototypes for operating system interface routines.
-*/
-SQLITE_PRIVATE int sqlite3OsClose(OsFile**);
-SQLITE_PRIVATE int sqlite3OsOpenDirectory(OsFile*, const char*);
-SQLITE_PRIVATE int sqlite3OsRead(OsFile*, void*, int amt);
-SQLITE_PRIVATE int sqlite3OsWrite(OsFile*, const void*, int amt);
-SQLITE_PRIVATE int sqlite3OsSeek(OsFile*, i64 offset);
-SQLITE_PRIVATE int sqlite3OsTruncate(OsFile*, i64 size);
-SQLITE_PRIVATE int sqlite3OsSync(OsFile*, int);
-SQLITE_PRIVATE void sqlite3OsSetFullSync(OsFile *id, int setting);
-SQLITE_PRIVATE int sqlite3OsFileSize(OsFile*, i64 *pSize);
-SQLITE_PRIVATE int sqlite3OsLock(OsFile*, int);
-SQLITE_PRIVATE int sqlite3OsUnlock(OsFile*, int);
-SQLITE_PRIVATE int sqlite3OsCheckReservedLock(OsFile *id);
-SQLITE_PRIVATE int sqlite3OsOpenReadWrite(const char*, OsFile**, int*);
-SQLITE_PRIVATE int sqlite3OsOpenExclusive(const char*, OsFile**, int);
-SQLITE_PRIVATE int sqlite3OsOpenReadOnly(const char*, OsFile**);
-SQLITE_PRIVATE int sqlite3OsDelete(const char*);
-SQLITE_PRIVATE int sqlite3OsFileExists(const char*);
-SQLITE_PRIVATE char *sqlite3OsFullPathname(const char*);
-SQLITE_PRIVATE int sqlite3OsIsDirWritable(char*);
-SQLITE_PRIVATE int sqlite3OsSyncDirectory(const char*);
-SQLITE_PRIVATE int sqlite3OsSectorSize(OsFile *id);
-SQLITE_PRIVATE int sqlite3OsTempFileName(char*);
-SQLITE_PRIVATE int sqlite3OsRandomSeed(char*);
-SQLITE_PRIVATE int sqlite3OsSleep(int ms);
-SQLITE_PRIVATE int sqlite3OsCurrentTime(double*);
-SQLITE_PRIVATE void sqlite3OsEnterMutex(void);
-SQLITE_PRIVATE void sqlite3OsLeaveMutex(void);
-SQLITE_PRIVATE int sqlite3OsInMutex(int);
-SQLITE_PRIVATE ThreadData *sqlite3OsThreadSpecificData(int);
-SQLITE_PRIVATE void *sqlite3OsMalloc(int);
-SQLITE_PRIVATE void *sqlite3OsRealloc(void *, int);
-SQLITE_PRIVATE void sqlite3OsFree(void *);
-SQLITE_PRIVATE int sqlite3OsAllocationSize(void *);
-SQLITE_PRIVATE void *sqlite3OsDlopen(const char*);
-SQLITE_PRIVATE void *sqlite3OsDlsym(void*, const char*);
-SQLITE_PRIVATE int sqlite3OsDlclose(void*);
-
-#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
-SQLITE_PRIVATE   int sqlite3OsFileHandle(OsFile *id);
-SQLITE_PRIVATE   int sqlite3OsLockState(OsFile *id);
-#endif
-
-/*
-** If the SQLITE_ENABLE_REDEF_IO macro is defined, then the OS-layer
-** interface routines are not called directly but are invoked using
-** pointers to functions.  This allows the implementation of various
-** OS-layer interface routines to be modified at run-time.  There are
-** obscure but legitimate reasons for wanting to do this.  But for
-** most users, a direct call to the underlying interface is preferable
-** so the the redefinable I/O interface is turned off by default.
-*/
-#ifdef SQLITE_ENABLE_REDEF_IO
-
-/*
-** When redefinable I/O is enabled, a single global instance of the
-** following structure holds pointers to the routines that SQLite
-** uses to talk with the underlying operating system.  Modify this
-** structure (before using any SQLite API!) to accomodate perculiar
-** operating system interfaces or behaviors.
-*/
-struct sqlite3OsVtbl {
-  int (*xOpenReadWrite)(const char*, OsFile**, int*);
-  int (*xOpenExclusive)(const char*, OsFile**, int);
-  int (*xOpenReadOnly)(const char*, OsFile**);
-
-  int (*xDelete)(const char*);
-  int (*xFileExists)(const char*);
-  char *(*xFullPathname)(const char*);
-  int (*xIsDirWritable)(char*);
-  int (*xSyncDirectory)(const char*);
-  int (*xTempFileName)(char*);
-
-  int (*xRandomSeed)(char*);
-  int (*xSleep)(int ms);
-  int (*xCurrentTime)(double*);
-
-  void (*xEnterMutex)(void);
-  void (*xLeaveMutex)(void);
-  int (*xInMutex)(int);
-  ThreadData *(*xThreadSpecificData)(int);
-
-  void *(*xMalloc)(int);
-  void *(*xRealloc)(void *, int);
-  void (*xFree)(void *);
-  int (*xAllocationSize)(void *);
-
-  void *(*xDlopen)(const char*);
-  void *(*xDlsym)(void*, const char*);
-  int (*xDlclose)(void*);
-};
-
-/* Macro used to comment out routines that do not exists when there is
-** no disk I/O or extension loading
-*/
-#ifdef SQLITE_OMIT_DISKIO
-# define IF_DISKIO(X)  0
-#else
-# define IF_DISKIO(X)  X
-#endif
-#ifdef SQLITE_OMIT_LOAD_EXTENSION
-# define IF_DLOPEN(X)  0
-#else
-# define IF_DLOPEN(X)  X
-#endif
-
-
-#if defined(_SQLITE_OS_C_) || defined(SQLITE_AMALGAMATION)
-  /*
-  ** The os.c file implements the global virtual function table.
-  ** We have to put this file here because the initializers
-  ** (ex: sqlite3OsRandomSeed) are macros that are about to be
-  ** redefined.
-  */
-  struct sqlite3OsVtbl sqlite3Os = {
-    IF_DISKIO( sqlite3OsOpenReadWrite ),
-    IF_DISKIO( sqlite3OsOpenExclusive ),
-    IF_DISKIO( sqlite3OsOpenReadOnly ),
-    IF_DISKIO( sqlite3OsDelete ),
-    IF_DISKIO( sqlite3OsFileExists ),
-    IF_DISKIO( sqlite3OsFullPathname ),
-    IF_DISKIO( sqlite3OsIsDirWritable ),
-    IF_DISKIO( sqlite3OsSyncDirectory ),
-    IF_DISKIO( sqlite3OsTempFileName ),
-    sqlite3OsRandomSeed,
-    sqlite3OsSleep,
-    sqlite3OsCurrentTime,
-    sqlite3OsEnterMutex,
-    sqlite3OsLeaveMutex,
-    sqlite3OsInMutex,
-    sqlite3OsThreadSpecificData,
-    sqlite3OsMalloc,
-    sqlite3OsRealloc,
-    sqlite3OsFree,
-    sqlite3OsAllocationSize,
-    IF_DLOPEN( sqlite3OsDlopen ),
-    IF_DLOPEN( sqlite3OsDlsym ),
-    IF_DLOPEN( sqlite3OsDlclose ),
-  };
-#else
-  /*
-  ** Files other than os.c just reference the global virtual function table.
-  */
-  extern struct sqlite3OsVtbl sqlite3Os;
-#endif /* _SQLITE_OS_C_ */
-
-
-/* This additional API routine is available with redefinable I/O */
-struct sqlite3OsVtbl *sqlite3_os_switch(void);
-
-
-/*
-** Redefine the OS interface to go through the virtual function table
-** rather than calling routines directly.
-*/
-#undef sqlite3OsOpenReadWrite
-#undef sqlite3OsOpenExclusive
-#undef sqlite3OsOpenReadOnly
-#undef sqlite3OsDelete
-#undef sqlite3OsFileExists
-#undef sqlite3OsFullPathname
-#undef sqlite3OsIsDirWritable
-#undef sqlite3OsSyncDirectory
-#undef sqlite3OsTempFileName
-#undef sqlite3OsRandomSeed
-#undef sqlite3OsSleep
-#undef sqlite3OsCurrentTime
-#undef sqlite3OsEnterMutex
-#undef sqlite3OsLeaveMutex
-#undef sqlite3OsInMutex
-#undef sqlite3OsThreadSpecificData
-#undef sqlite3OsMalloc
-#undef sqlite3OsRealloc
-#undef sqlite3OsFree
-#undef sqlite3OsAllocationSize
-#define sqlite3OsOpenReadWrite      sqlite3Os.xOpenReadWrite
-#define sqlite3OsOpenExclusive      sqlite3Os.xOpenExclusive
-#define sqlite3OsOpenReadOnly       sqlite3Os.xOpenReadOnly
-#define sqlite3OsDelete             sqlite3Os.xDelete
-#define sqlite3OsFileExists         sqlite3Os.xFileExists
-#define sqlite3OsFullPathname       sqlite3Os.xFullPathname
-#define sqlite3OsIsDirWritable      sqlite3Os.xIsDirWritable
-#define sqlite3OsSyncDirectory      sqlite3Os.xSyncDirectory
-#define sqlite3OsTempFileName       sqlite3Os.xTempFileName
-#define sqlite3OsRandomSeed         sqlite3Os.xRandomSeed
-#define sqlite3OsSleep              sqlite3Os.xSleep
-#define sqlite3OsCurrentTime        sqlite3Os.xCurrentTime
-#define sqlite3OsEnterMutex         sqlite3Os.xEnterMutex
-#define sqlite3OsLeaveMutex         sqlite3Os.xLeaveMutex
-#define sqlite3OsInMutex            sqlite3Os.xInMutex
-#define sqlite3OsThreadSpecificData sqlite3Os.xThreadSpecificData
-#define sqlite3OsMalloc             sqlite3Os.xMalloc
-#define sqlite3OsRealloc            sqlite3Os.xRealloc
-#define sqlite3OsFree               sqlite3Os.xFree
-#define sqlite3OsAllocationSize     sqlite3Os.xAllocationSize
-
-#endif /* SQLITE_ENABLE_REDEF_IO */
+** Functions for accessing sqlite3_file methods
+*/
+SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
+SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
+SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
+SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
+SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
+SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
+SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
+SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
+SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id);
+SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
+SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
+SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
+
+/*
+** Functions for accessing sqlite3_vfs methods
+*/
+SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
+SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
+SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int);
+SQLITE_PRIVATE int sqlite3OsGetTempName(sqlite3_vfs *, char *);
+SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, char *);
+SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
+SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
+SQLITE_PRIVATE void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *);
+SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
+SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
+SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
+SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *, double*);
+
+/*
+** Convenience functions for opening and closing files using
+** sqlite3_malloc() to obtain space for the file-handle structure.
+*/
+SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
+SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
+
+/*
+** Each OS-specific backend defines an instance of the following
+** structure for returning a pointer to its sqlite3_vfs.  If OS_OTHER
+** is defined (meaning that the application-defined OS interface layer
+** is used) then there is no default VFS.   The application must
+** register one or more VFS structures using sqlite3_vfs_register()
+** before attempting to use SQLite.
+*/
+#if OS_UNIX || OS_WIN || OS_OS2
+SQLITE_PRIVATE sqlite3_vfs *sqlite3OsDefaultVfs(void);
+#else
+# define sqlite3OsDefaultVfs(X) 0
+#endif
 
 #endif /* _SQLITE_OS_H_ */
 
 /************** End of os.h **************************************************/
+/************** Continuing where we left off in sqliteInt.h ******************/
+/************** Include mutex.h in the middle of sqliteInt.h *****************/
+/************** Begin file mutex.h *******************************************/
+/*
+** 2007 August 28
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+**
+** This file contains the common header for all mutex implementations.
+** The sqliteInt.h header #includes this file so that it is available
+** to all source files.  We break it out in an effort to keep the code
+** better organized.
+**
+** NOTE:  source files should *not* #include this header file directly.
+** Source files should #include the sqliteInt.h file and let that file
+** include this one indirectly.
+**
+** $Id: mutex.h,v 1.2 2007/08/30 14:10:30 drh Exp $
+*/
+
+
+#ifdef SQLITE_MUTEX_APPDEF
+/*
+** If SQLITE_MUTEX_APPDEF is defined, then this whole module is
+** omitted and equivalent functionality must be provided by the
+** application that links against the SQLite library.
+*/
+#else
+/*
+** Figure out what version of the code to use.  The choices are
+**
+**   SQLITE_MUTEX_NOOP         For single-threaded applications that
+**                             do not desire error checking.
+**
+**   SQLITE_MUTEX_NOOP_DEBUG   For single-threaded applications with
+**                             error checking to help verify that mutexes
+**                             are being used correctly even though they
+**                             are not needed.  Used when SQLITE_DEBUG is
+**                             defined on single-threaded builds.
+**
+**   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
+**
+**   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
+**
+**   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
+*/
+#define SQLITE_MUTEX_NOOP 1   /* The default */
+#if defined(SQLITE_DEBUG) && !SQLITE_THREADSAFE
+# undef SQLITE_MUTEX_NOOP
+# define SQLITE_MUTEX_NOOP_DEBUG
+#endif
+#if defined(SQLITE_MUTEX_NOOP) && SQLITE_THREADSAFE && OS_UNIX
+# undef SQLITE_MUTEX_NOOP
+# define SQLITE_MUTEX_PTHREADS
+#endif
+#if defined(SQLITE_MUTEX_NOOP) && SQLITE_THREADSAFE && OS_WIN
+# undef SQLITE_MUTEX_NOOP
+# define SQLITE_MUTEX_W32
+#endif
+#if defined(SQLITE_MUTEX_NOOP) && SQLITE_THREADSAFE && OS_OS2
+# undef SQLITE_MUTEX_NOOP
+# define SQLITE_MUTEX_OS2
+#endif
+
+#ifdef SQLITE_MUTEX_NOOP
+/*
+** If this is a no-op implementation, implement everything as macros.
+*/
+#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
+#define sqlite3_mutex_free(X)
+#define sqlite3_mutex_enter(X)
+#define sqlite3_mutex_try(X)      SQLITE_OK
+#define sqlite3_mutex_leave(X)
+#define sqlite3_mutex_held(X)     1
+#define sqlite3_mutex_notheld(X)  1
+#endif
+
+#endif /* SQLITE_MUTEX_APPDEF */
+
+/************** End of mutex.h ***********************************************/
 /************** Continuing where we left off in sqliteInt.h ******************/
 
 /*
 ** Each database file to be accessed by the system is an instance
@@ -4809,22 +5523,26 @@
 ** internal function sqlite3Error() is used to set these variables
 ** consistently.
 */
 struct sqlite3 {
+  sqlite3_vfs *pVfs;            /* OS Interface */
   int nDb;                      /* Number of backends currently in use */
   Db *aDb;                      /* All backends */
   int flags;                    /* Miscellanous flags. See below */
+  int openFlags;                /* Flags passed to sqlite3_vfs.xOpen() */
   int errCode;                  /* Most recent error code (SQLITE_*) */
   int errMask;                  /* & result codes with this before returning */
   u8 autoCommit;                /* The auto-commit flag. */
   u8 temp_store;                /* 1: file 2: memory 0: default */
+  u8 mallocFailed;              /* True if we have seen a malloc failure */
   int nTable;                   /* Number of tables in the database */
   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
   i64 lastRowid;                /* ROWID of most recent insert (see above) */
   i64 priorNewRowid;            /* Last randomly generated ROWID */
   int magic;                    /* Magic number for detect library misuse */
   int nChange;                  /* Value returned by sqlite3_changes() */
   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
+  sqlite3_mutex *mutex;         /* Connection mutex */
   struct sqlite3InitInfo {      /* Information used during initialization */
     int iDb;                    /* When back is being initialized */
     int newTnum;                /* Rootpage of table being initialized */
     u8 busy;                    /* TRUE if currently initializing */
@@ -4913,8 +5631,10 @@
 #define SQLITE_FullFSync      0x00010000  /* Use full fsync on the backend */
 #define SQLITE_LoadExtension  0x00020000  /* Enable load_extension */
 
 #define SQLITE_RecoveryMode   0x00040000  /* Ignore schema errors */
+#define SQLITE_SharedCache    0x00080000  /* Cache sharing is enabled */
+#define SQLITE_Vtab           0x00100000  /* There exists a virtual table */
 
 /*
 ** Possible values for the sqlite.magic field.
 ** The numbers are obtained at random and have no special meaning, other
@@ -5213,8 +5933,9 @@
 ** otherwise be equal, then return a result as if the second key
 ** were larger.
 */
 struct KeyInfo {
+  sqlite3 *db;        /* The database connection */
   u8 enc;             /* Text encoding - one of the TEXT_Utf* values */
   u8 incrKey;         /* Increase 2nd key by epsilon before comparison */
   int nField;         /* Number of entries in aColl[] */
   u8 *aSortOrder;     /* If defined an aSortOrder[i] is true, sort DESC */
@@ -5385,9 +6106,9 @@
   Select *pSelect;       /* When the expression is a sub-select.  Also the
                          ** right side of "<expr> IN (<select>)" */
   Table *pTab;           /* Table for OP_Column expressions. */
   Schema *pSchema;
-#if SQLITE_MAX_EXPR_DEPTH>0
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
   int nHeight;           /* Height of the tree headed by this node */
 #endif
 };
 
@@ -5733,9 +6454,9 @@
   Token sArg;                /* Complete text of a module argument */
   u8 declareVtab;            /* True if inside sqlite3_declare_vtab() */
   Table *pVirtualLock;       /* Require virtual table lock on this table */
 #endif
-#if SQLITE_MAX_EXPR_DEPTH>0
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
   int nHeight;            /* Expression tree height of current sub-select */
 #endif
 };
 
@@ -5845,13 +6566,13 @@
   int orconf;          /* OE_Rollback etc. */
   Trigger *pTrig;      /* The trigger that this step is a part of */
 
   Select *pSelect;     /* Valid for SELECT and sometimes
-			  INSERT steps (when pExprList == 0) */
+                          INSERT steps (when pExprList == 0) */
   Token target;        /* Valid for DELETE, UPDATE, INSERT steps */
   Expr *pWhere;        /* Valid for DELETE, UPDATE steps */
   ExprList *pExprList; /* Valid for UPDATE statements and sometimes
-			   INSERT steps (when pSelect == 0)         */
+                           INSERT steps (when pSelect == 0)         */
   IdList *pIdList;     /* Valid for INSERT statements only */
   TriggerStep *pNext;  /* Next in the link-list */
   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
 };
@@ -5917,15 +6638,8 @@
   int rc;             /* Result code stored here */
 } InitData;
 
 /*
- * This global flag is set for performance testing of triggers. When it is set
- * SQLite will perform the overhead of building new and old trigger references
- * even when no triggers exist
- */
-extern int sqlite3_always_code_trigger_setup;
-
-/*
 ** Assuming zIn points to the first byte of a UTF-8 character,
 ** advance zIn to point to the first byte of the next UTF-8 character.
 */
 #define SQLITE_SKIP_UTF8(zIn) {                        \
@@ -5953,23 +6667,20 @@
 SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
 SQLITE_PRIVATE int sqlite3StrNICmp(const char *, const char *, int);
 SQLITE_PRIVATE int sqlite3IsNumber(const char*, int*, u8);
 
-SQLITE_PRIVATE void *sqlite3Malloc(int,int);
-SQLITE_PRIVATE void *sqlite3MallocRaw(int,int);
-SQLITE_PRIVATE void *sqlite3Realloc(void*,int);
+SQLITE_PRIVATE void *sqlite3MallocZero(unsigned);
+SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, unsigned);
+SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, unsigned);
 SQLITE_PRIVATE char *sqlite3StrDup(const char*);
 SQLITE_PRIVATE char *sqlite3StrNDup(const char*, int);
-# define sqlite3CheckMemory(a,b)
-SQLITE_PRIVATE void *sqlite3ReallocOrFree(void*,int);
-SQLITE_PRIVATE void sqlite3FreeX(void*);
-SQLITE_PRIVATE void *sqlite3MallocX(int);
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-SQLITE_PRIVATE   int sqlite3AllocSize(void *);
-#endif
-
-SQLITE_PRIVATE char *sqlite3MPrintf(const char*, ...);
-SQLITE_PRIVATE char *sqlite3VMPrintf(const char*, va_list);
+SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
+SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
+SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
+SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
+
+SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
+SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
 SQLITE_PRIVATE   void *sqlite3TextToPtr(const char*);
 #endif
@@ -5976,21 +6687,21 @@
 SQLITE_PRIVATE void sqlite3SetString(char **, ...);
 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
 SQLITE_PRIVATE void sqlite3ErrorClear(Parse*);
 SQLITE_PRIVATE void sqlite3Dequote(char*);
-SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
+SQLITE_PRIVATE void sqlite3DequoteExpr(sqlite3*, Expr*);
 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
-SQLITE_PRIVATE Expr *sqlite3Expr(int, Expr*, Expr*, const Token*);
-SQLITE_PRIVATE Expr *sqlite3ExprOrFree(int, Expr*, Expr*, const Token*);
+SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*, int, Expr*, Expr*, const Token*);
+SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
 SQLITE_PRIVATE Expr *sqlite3RegisterExpr(Parse*,Token*);
-SQLITE_PRIVATE Expr *sqlite3ExprAnd(Expr*, Expr*);
+SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
 SQLITE_PRIVATE void sqlite3ExprSpan(Expr*,Token*,Token*);
-SQLITE_PRIVATE Expr *sqlite3ExprFunction(ExprList*, Token*);
+SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
 SQLITE_PRIVATE void sqlite3ExprDelete(Expr*);
-SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(ExprList*,Expr*,Token*);
+SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*,Token*);
 SQLITE_PRIVATE void sqlite3ExprListDelete(ExprList*);
 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
@@ -6019,13 +6730,13 @@
 
 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
-SQLITE_PRIVATE void *sqlite3ArrayAllocate(void*,int,int,int*,int*,int*);
-SQLITE_PRIVATE IdList *sqlite3IdListAppend(IdList*, Token*);
+SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
+SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
-SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(SrcList*, Token*, Token*);
-SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(SrcList*, Token*, Token*, Token*,
+SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
+SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*, Token*,
                                       Select*, Expr*, IdList*);
 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
 SQLITE_PRIVATE void sqlite3IdListDelete(IdList*);
@@ -6033,10 +6744,10 @@
 SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
                         Token*, int, int);
 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, int, int, Select*, int, int*, char *aff);
-SQLITE_PRIVATE Select *sqlite3SelectNew(ExprList*,SrcList*,Expr*,ExprList*,Expr*,ExprList*,
-                        int,Expr*,Expr*);
+SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
+                         Expr*,ExprList*,int,Expr*,Expr*);
 SQLITE_PRIVATE void sqlite3SelectDelete(Select*);
 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
@@ -6056,16 +6767,15 @@
 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
-SQLITE_PRIVATE char *sqlite3NameFromToken(Token*);
-SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
-int sqliteFuncId(Token*);
+SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
+SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
 SQLITE_PRIVATE int sqlite3ExprResolveNames(NameContext *, Expr *);
 SQLITE_PRIVATE int sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 SQLITE_PRIVATE int sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
-SQLITE_PRIVATE Expr *sqlite3CreateIdExpr(const char*);
+SQLITE_PRIVATE Expr *sqlite3CreateIdExpr(Parse *, const char*);
 SQLITE_PRIVATE void sqlite3Randomness(int, void*);
 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
@@ -6082,14 +6792,14 @@
 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int);
 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, char*, int, int, int, int);
 SQLITE_PRIVATE void sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
-SQLITE_PRIVATE Expr *sqlite3ExprDup(Expr*);
-SQLITE_PRIVATE void sqlite3TokenCopy(Token*, Token*);
-SQLITE_PRIVATE ExprList *sqlite3ExprListDup(ExprList*);
-SQLITE_PRIVATE SrcList *sqlite3SrcListDup(SrcList*);
-SQLITE_PRIVATE IdList *sqlite3IdListDup(IdList*);
-SQLITE_PRIVATE Select *sqlite3SelectDup(Select*);
+SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*);
+SQLITE_PRIVATE void sqlite3TokenCopy(sqlite3*,Token*, Token*);
+SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*);
+SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*);
+SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
+SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*);
 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(sqlite3*);
 SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*);
@@ -6107,12 +6817,13 @@
 SQLITE_PRIVATE   int sqlite3CodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int,
                            int, int);
   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(TriggerStep*);
-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(Select*);
-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(Token*, IdList*, ExprList*,Select*,int);
-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(Token*, ExprList*, Expr*, int);
-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(Token*, Expr*);
+SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
+SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
+                                        ExprList*,Select*,int);
+SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, int);
+SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
 SQLITE_PRIVATE   void sqlite3DeleteTrigger(Trigger*);
 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
 #else
 # define sqlite3TriggersExist(A,B,C,D,E,F) 0
@@ -6138,9 +6849,9 @@
 #endif
 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
 SQLITE_PRIVATE int sqlite3BtreeFactory(const sqlite3 *db, const char *zFilename,
-                       int omitJournal, int nCache, Btree **ppBtree);
+                       int omitJournal, int nCache, int flags, Btree **ppBtree);
 SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
@@ -6163,9 +6874,9 @@
 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*);
 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
-SQLITE_PRIVATE void *sqlite3HexToBlob(const char *z);
+SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z);
 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char *,int,int);
@@ -6174,19 +6885,21 @@
 SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Parse *pParse, Expr *, Token *);
 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
-SQLITE_PRIVATE void sqlite3Utf16Substr(sqlite3_context *,int,sqlite3_value **);
 
 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
-SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, void(*)(void*));
+SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
+                        void(*)(void*));
 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
-SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(void);
-SQLITE_PRIVATE char *sqlite3Utf16to8(const void*, int);
-SQLITE_PRIVATE int sqlite3ValueFromExpr(Expr *, u8, u8, sqlite3_value **);
+SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
+SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int);
+SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
-extern const unsigned char sqlite3UpperToLower[];
+#ifndef SQLITE_AMALGAMATION
+SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
+#endif
 SQLITE_PRIVATE void sqlite3RootPageMoved(Db*, int, int);
 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3*);
 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
@@ -6206,24 +6919,21 @@
 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
-SQLITE_PRIVATE ThreadData *sqlite3ThreadData(void);
-SQLITE_PRIVATE const ThreadData *sqlite3ThreadDataReadOnly(void);
-SQLITE_PRIVATE void sqlite3ReleaseThreadData(void);
 SQLITE_PRIVATE void sqlite3AttachFunctions(sqlite3 *);
 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
 SQLITE_PRIVATE void sqlite3SchemaFree(void *);
-SQLITE_PRIVATE Schema *sqlite3SchemaGet(Btree *);
+SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
   void (*)(sqlite3_context*,int,sqlite3_value **),
   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*));
 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
-SQLITE_PRIVATE void sqlite3FailedMalloc(void);
 SQLITE_PRIVATE void sqlite3AbortOtherActiveVdbes(sqlite3 *, Vdbe *);
 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
+
 
 /*
 ** The interface to the LEMON-generated parser
 */
@@ -6248,25 +6958,23 @@
 #ifdef SQLITE_TEST
 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
 #endif
 
+/*
+** The MallocDisallow() and MallocAllow() routines are like asserts.
+** Call them around a section of code that you do not expect to do
+** any memory allocation.
+*/
 #ifdef SQLITE_MEMDEBUG
 SQLITE_PRIVATE   void sqlite3MallocDisallow(void);
 SQLITE_PRIVATE   void sqlite3MallocAllow(void);
-SQLITE_PRIVATE   int sqlite3TestMallocFail(void);
-#else
-  #define sqlite3TestMallocFail() 0
-  #define sqlite3MallocDisallow()
-  #define sqlite3MallocAllow()
-#endif
-
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-SQLITE_PRIVATE   void *sqlite3ThreadSafeMalloc(int);
-SQLITE_PRIVATE   void sqlite3ThreadSafeFree(void *);
-#else
-  #define sqlite3ThreadSafeMalloc sqlite3MallocX
-  #define sqlite3ThreadSafeFree sqlite3FreeX
-#endif
+SQLITE_PRIVATE   void sqlite3MallocBenignFailure(int);
+#else
+# define sqlite3MallocDisallow()
+# define sqlite3MallocAllow()
+# define sqlite3MallocBenignFailure(x)
+#endif
+
 
 #ifdef SQLITE_OMIT_VIRTUALTABLE
 #  define sqlite3VtabClear(X)
 #  define sqlite3VtabSync(X,Y) (Y)
@@ -6287,24 +6995,30 @@
 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, sqlite3_vtab *);
-SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(FuncDef*, int nArg, Expr*);
+SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, int, const char*);
-CollSeq* sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
-
-#if SQLITE_MAX_EXPR_DEPTH>0
+SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
+
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE
+SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
+SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
+SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
+#else
+  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
+#endif
+
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Expr *);
 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
 #else
   #define sqlite3ExprSetHeight(x)
 #endif
 
-SQLITE_PRIVATE u32 sqlite3Get2byte(const u8*);
-SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
-SQLITE_PRIVATE void sqlite3Put2byte(u8*, u32);
+SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
 
 #ifdef SQLITE_SSE
 #include "sseInt.h"
@@ -6325,9 +7039,9 @@
 #else
 # define IOTRACE(A)
 # define sqlite3VdbeIOTraceSql(X)
 #endif
-extern void (*sqlite3_io_trace)(const char*,...);
+SQLITE_EXTERN void (*sqlite3_io_trace)(const char*,...);
 
 #endif
 
 /************** End of sqliteInt.h *******************************************/
@@ -6585,17 +7299,21 @@
 ** (":SS.FFF") is option.  The year and date can be omitted as long
 ** as there is a time string.  The time string can be omitted as long
 ** as there is a year and date.
 */
-static int parseDateOrTime(const char *zDate, DateTime *p){
+static int parseDateOrTime(
+  sqlite3_context *context,
+  const char *zDate,
+  DateTime *p
+){
   memset(p, 0, sizeof(*p));
   if( parseYyyyMmDd(zDate,p)==0 ){
     return 0;
   }else if( parseHhMmSs(zDate, p)==0 ){
     return 0;
   }else if( sqlite3StrICmp(zDate,"now")==0){
     double r;
-    sqlite3OsCurrentTime(&r);
+    sqlite3OsCurrentTime((sqlite3_vfs *)sqlite3_user_data(context), &r);
     p->rJD = r;
     p->validJD = 1;
     return 0;
   }else if( sqlite3IsNumber(zDate, 0, SQLITE_UTF8) ){
@@ -6705,17 +7423,17 @@
   }
 #else
   {
     struct tm *pTm;
-    sqlite3OsEnterMutex();
+    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
     pTm = localtime(&t);
     y.Y = pTm->tm_year + 1900;
     y.M = pTm->tm_mon + 1;
     y.D = pTm->tm_mday;
     y.h = pTm->tm_hour;
     y.m = pTm->tm_min;
     y.s = pTm->tm_sec;
-    sqlite3OsLeaveMutex();
+    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
   }
 #endif
   y.validYMD = 1;
   y.validHMS = 1;
@@ -6933,13 +7651,19 @@
 ** argv[1] and following are modifiers.  Parse them all and write
 ** the resulting time into the DateTime structure p.  Return 0
 ** on success and 1 if there are any errors.
 */
-static int isDate(int argc, sqlite3_value **argv, DateTime *p){
+static int isDate(
+  sqlite3_context *context,
+  int argc,
+  sqlite3_value **argv,
+  DateTime *p
+){
   int i;
   const unsigned char *z;
   if( argc==0 ) return 1;
-  if( (z = sqlite3_value_text(argv[0]))==0 || parseDateOrTime((char*)z, p) ){
+  z = sqlite3_value_text(argv[0]);
+  if( !z || parseDateOrTime(context, (char*)z, p) ){
     return 1;
   }
   for(i=1; i<argc; i++){
     if( (z = sqlite3_value_text(argv[i]))==0 || parseModifier((char*)z, p) ){
@@ -6965,9 +7689,9 @@
   int argc,
   sqlite3_value **argv
 ){
   DateTime x;
-  if( isDate(argc, argv, &x)==0 ){
+  if( isDate(context, argc, argv, &x)==0 ){
     computeJD(&x);
     sqlite3_result_double(context, x.rJD);
   }
 }
@@ -6982,9 +7706,9 @@
   int argc,
   sqlite3_value **argv
 ){
   DateTime x;
-  if( isDate(argc, argv, &x)==0 ){
+  if( isDate(context, argc, argv, &x)==0 ){
     char zBuf[100];
     computeYMD_HMS(&x);
     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
@@ -7002,9 +7726,9 @@
   int argc,
   sqlite3_value **argv
 ){
   DateTime x;
-  if( isDate(argc, argv, &x)==0 ){
+  if( isDate(context, argc, argv, &x)==0 ){
     char zBuf[100];
     computeHMS(&x);
     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
@@ -7021,9 +7745,9 @@
   int argc,
   sqlite3_value **argv
 ){
   DateTime x;
-  if( isDate(argc, argv, &x)==0 ){
+  if( isDate(context, argc, argv, &x)==0 ){
     char zBuf[100];
     computeYMD(&x);
     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
@@ -7059,9 +7783,9 @@
   int i, j;
   char *z;
   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
   char zBuf[100];
-  if( zFmt==0 || isDate(argc-1, argv+1, &x) ) return;
+  if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
   for(i=0, n=1; zFmt[i]; i++, n++){
     if( zFmt[i]=='%' ){
       switch( zFmt[i+1] ){
         case 'd':
@@ -7099,9 +7823,9 @@
   }else if( n>SQLITE_MAX_LENGTH ){
     sqlite3_result_error_toobig(context);
     return;
   }else{
-    z = sqliteMalloc( n );
+    z = sqlite3_malloc( n );
     if( z==0 ) return;
   }
   computeJD(&x);
   computeYMD_HMS(&x);
@@ -7162,9 +7886,9 @@
   }
   z[j] = 0;
   sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
   if( z!=zBuf ){
-    sqliteFree(z);
+    sqlite3_free(z);
   }
 }
 
 /*
@@ -7176,9 +7900,9 @@
   sqlite3_context *context,
   int argc,
   sqlite3_value **argv
 ){
-  sqlite3_value *pVal = sqlite3ValueNew();
+  sqlite3_value *pVal = sqlite3ValueNew(0);
   if( pVal ){
     sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
     timeFunc(context, 1, &pVal);
     sqlite3ValueFree(pVal);
@@ -7194,9 +7918,9 @@
   sqlite3_context *context,
   int argc,
   sqlite3_value **argv
 ){
-  sqlite3_value *pVal = sqlite3ValueNew();
+  sqlite3_value *pVal = sqlite3ValueNew(0);
   if( pVal ){
     sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
     dateFunc(context, 1, &pVal);
     sqlite3ValueFree(pVal);
@@ -7212,9 +7936,9 @@
   sqlite3_context *context,
   int argc,
   sqlite3_value **argv
 ){
-  sqlite3_value *pVal = sqlite3ValueNew();
+  sqlite3_value *pVal = sqlite3ValueNew(0);
   if( pVal ){
     sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
     datetimeFunc(context, 1, &pVal);
     sqlite3ValueFree(pVal);
@@ -7261,12 +7985,12 @@
   }
 #else
   {
     struct tm *pTm;
-    sqlite3OsEnterMutex();
+    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
     pTm = gmtime(&t);
     strftime(zBuf, 20, zFormat, pTm);
-    sqlite3OsLeaveMutex();
+    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
   }
 #endif
 
   sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
@@ -7297,9 +8021,9 @@
   int i;
 
   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
     sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
-        SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
+        SQLITE_UTF8, (void *)(db->pVfs), aFuncs[i].xFunc, 0, 0);
   }
 #else
   static const struct {
      char *zName;
@@ -7319,9 +8043,9 @@
 }
 
 /************** End of date.c ************************************************/
 /************** Begin file os.c **********************************************/
-/*
+ /*
 ** 2005 November 29
 **
 ** The author disclaims copyright to this source code.  In place of
 ** a legal notice, here is a blessing:
@@ -7339,796 +8063,1809 @@
 #undef _SQLITE_OS_C_
 
 /*
 ** The following routines are convenience wrappers around methods
-** of the OsFile object.  This is mostly just syntactic sugar.  All
+** of the sqlite3_file object.  This is mostly just syntactic sugar. All
 ** of this would be completely automatic if SQLite were coded using
 ** C++ instead of plain old C.
 */
-SQLITE_PRIVATE int sqlite3OsClose(OsFile **pId){
-  OsFile *id;
-  if( pId!=0 && (id = *pId)!=0 ){
-    return id->pMethod->xClose(pId);
-  }else{
-    return SQLITE_OK;
-  }
-}
-SQLITE_PRIVATE int sqlite3OsOpenDirectory(OsFile *id, const char *zName){
-  return id->pMethod->xOpenDirectory(id, zName);
-}
-SQLITE_PRIVATE int sqlite3OsRead(OsFile *id, void *pBuf, int amt){
-  return id->pMethod->xRead(id, pBuf, amt);
-}
-SQLITE_PRIVATE int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){
-  return id->pMethod->xWrite(id, pBuf, amt);
-}
-SQLITE_PRIVATE int sqlite3OsSeek(OsFile *id, i64 offset){
-  return id->pMethod->xSeek(id, offset);
-}
-SQLITE_PRIVATE int sqlite3OsTruncate(OsFile *id, i64 size){
-  return id->pMethod->xTruncate(id, size);
-}
-SQLITE_PRIVATE int sqlite3OsSync(OsFile *id, int fullsync){
-  return id->pMethod->xSync(id, fullsync);
-}
-SQLITE_PRIVATE void sqlite3OsSetFullSync(OsFile *id, int value){
-  id->pMethod->xSetFullSync(id, value);
-}
-SQLITE_PRIVATE int sqlite3OsFileSize(OsFile *id, i64 *pSize){
-  return id->pMethod->xFileSize(id, pSize);
-}
-SQLITE_PRIVATE int sqlite3OsLock(OsFile *id, int lockType){
-  return id->pMethod->xLock(id, lockType);
-}
-SQLITE_PRIVATE int sqlite3OsUnlock(OsFile *id, int lockType){
-  return id->pMethod->xUnlock(id, lockType);
-}
-SQLITE_PRIVATE int sqlite3OsCheckReservedLock(OsFile *id){
-  return id->pMethod->xCheckReservedLock(id);
-}
-SQLITE_PRIVATE int sqlite3OsSectorSize(OsFile *id){
-  int (*xSectorSize)(OsFile*) = id->pMethod->xSectorSize;
-  return xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE;
-}
-
-#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
-  /* These methods are currently only used for testing and debugging. */
-  int sqlite3OsFileHandle(OsFile *id){
-    return id->pMethod->xFileHandle(id);
-  }
-  int sqlite3OsLockState(OsFile *id){
-    return id->pMethod->xLockState(id);
-  }
-#endif
-
-#ifdef SQLITE_ENABLE_REDEF_IO
-/*
-** A function to return a pointer to the virtual function table.
-** This routine really does not accomplish very much since the
-** virtual function table is a global variable and anybody who
-** can call this function can just as easily access the variable
-** for themselves.  Nevertheless, we include this routine for
-** backwards compatibility with an earlier redefinable I/O
-** interface design.
-*/
-struct sqlite3OsVtbl *sqlite3_os_switch(void){
-  return &sqlite3Os;
-}
-#endif
+SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
+  int rc = SQLITE_OK;
+  if( pId->pMethods ){
+    rc = pId->pMethods->xClose(pId);
+    pId->pMethods = 0;
+  }
+  return rc;
+}
+SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
+  return id->pMethods->xRead(id, pBuf, amt, offset);
+}
+SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
+  return id->pMethods->xWrite(id, pBuf, amt, offset);
+}
+SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
+  return id->pMethods->xTruncate(id, size);
+}
+SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
+  return id->pMethods->xSync(id, flags);
+}
+SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
+  return id->pMethods->xFileSize(id, pSize);
+}
+SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
+  return id->pMethods->xLock(id, lockType);
+}
+SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
+  return id->pMethods->xUnlock(id, lockType);
+}
+SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id){
+  return id->pMethods->xCheckReservedLock(id);
+}
+SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
+  return id->pMethods->xFileControl(id,op,pArg);
+}
+
+#ifdef SQLITE_TEST
+  /* The following two variables are used to override the values returned
+  ** by the xSectorSize() and xDeviceCharacteristics() vfs methods for
+  ** testing purposes. They are usually set by a test command implemented
+  ** in test6.c.
+  */
+  int sqlite3_test_sector_size = 0;
+  int sqlite3_test_device_characteristics = 0;
+  int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
+    int dc = id->pMethods->xDeviceCharacteristics(id);
+    return dc | sqlite3_test_device_characteristics;
+  }
+  int sqlite3OsSectorSize(sqlite3_file *id){
+    if( sqlite3_test_sector_size==0 ){
+      int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
+      return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
+    }
+    return sqlite3_test_sector_size;
+  }
+#else
+  int sqlite3OsSectorSize(sqlite3_file *id){
+    int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
+    return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
+  }
+  int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
+    return id->pMethods->xDeviceCharacteristics(id);
+  }
+#endif
+
+/*
+** The next group of routines are convenience wrappers around the
+** VFS methods.
+*/
+SQLITE_PRIVATE int sqlite3OsOpen(
+  sqlite3_vfs *pVfs,
+  const char *zPath,
+  sqlite3_file *pFile,
+  int flags,
+  int *pFlagsOut
+){
+  return pVfs->xOpen(pVfs, zPath, pFile, flags, pFlagsOut);
+}
+SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
+  return pVfs->xDelete(pVfs, zPath, dirSync);
+}
+SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
+  return pVfs->xAccess(pVfs, zPath, flags);
+}
+SQLITE_PRIVATE int sqlite3OsGetTempName(sqlite3_vfs *pVfs, char *zBufOut){
+  return pVfs->xGetTempName(pVfs, zBufOut);
+}
+SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *pVfs, const char *zPath, char *zPathOut){
+  return pVfs->xFullPathname(pVfs, zPath, zPathOut);
+}
+SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
+  return pVfs->xDlOpen(pVfs, zPath);
+}
+SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
+  pVfs->xDlError(pVfs, nByte, zBufOut);
+}
+SQLITE_PRIVATE void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
+  return pVfs->xDlSym(pVfs, pHandle, zSymbol);
+}
+SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
+  pVfs->xDlClose(pVfs, pHandle);
+}
+SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
+  return pVfs->xRandomness(pVfs, nByte, zBufOut);
+}
+SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
+  return pVfs->xSleep(pVfs, nMicro);
+}
+SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
+  return pVfs->xCurrentTime(pVfs, pTimeOut);
+}
+
+SQLITE_PRIVATE int sqlite3OsOpenMalloc(
+  sqlite3_vfs *pVfs,
+  const char *zFile,
+  sqlite3_file **ppFile,
+  int flags,
+  int *pOutFlags
+){
+  int rc = SQLITE_NOMEM;
+  sqlite3_file *pFile;
+  pFile = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile);
+  if( pFile ){
+    rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
+    if( rc!=SQLITE_OK ){
+      sqlite3_free(pFile);
+    }else{
+      *ppFile = pFile;
+    }
+  }
+  return rc;
+}
+SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
+  int rc = SQLITE_OK;
+  if( pFile ){
+    rc = sqlite3OsClose(pFile);
+    sqlite3_free(pFile);
+  }
+  return rc;
+}
+
+/*
+** The list of all registered VFS implementations.  This list is
+** initialized to the single VFS returned by sqlite3OsDefaultVfs()
+** upon the first call to sqlite3_vfs_find().
+*/
+static sqlite3_vfs *vfsList = 0;
+
+/*
+** Locate a VFS by name.  If no name is given, simply return the
+** first VFS on the list.
+*/
+SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
+  sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+  sqlite3_vfs *pVfs;
+  static int isInit = 0;
+  sqlite3_mutex_enter(mutex);
+  if( !isInit ){
+    vfsList = sqlite3OsDefaultVfs();
+    isInit = 1;
+  }
+  for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
+    if( zVfs==0 ) break;
+    if( strcmp(zVfs, pVfs->zName)==0 ) break;
+  }
+  sqlite3_mutex_leave(mutex);
+  return pVfs;
+}
+
+/*
+** Unlink a VFS from the linked list
+*/
+static void vfsUnlink(sqlite3_vfs *pVfs){
+  assert( sqlite3_mutex_held(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)) );
+  if( vfsList==pVfs ){
+    vfsList = pVfs->pNext;
+  }else{
+    sqlite3_vfs *p = vfsList;
+    while( p->pNext && p->pNext!=pVfs ){
+      p = p->pNext;
+    }
+    if( p->pNext==pVfs ){
+      p->pNext = pVfs->pNext;
+    }
+  }
+}
+
+/*
+** Register a VFS with the system.  It is harmless to register the same
+** VFS multiple times.  The new VFS becomes the default if makeDflt is
+** true.
+*/
+SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
+  sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+  sqlite3_vfs_find(0);  /* Make sure we are initialized */
+  sqlite3_mutex_enter(mutex);
+  vfsUnlink(pVfs);
+  if( makeDflt || vfsList==0 ){
+    pVfs->pNext = vfsList;
+    vfsList = pVfs;
+  }else{
+    pVfs->pNext = vfsList->pNext;
+    vfsList->pNext = pVfs;
+  }
+  assert(vfsList);
+  sqlite3_mutex_leave(mutex);
+  return SQLITE_OK;
+}
+
+/*
+** Unregister a VFS so that it is no longer accessible.
+*/
+SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
+  sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+  sqlite3_mutex_enter(mutex);
+  vfsUnlink(pVfs);
+  assert(vfsList);
+  sqlite3_mutex_leave(mutex);
+  return SQLITE_OK;
+}
 
 /************** End of os.c **************************************************/
-/************** Begin file malloc.c ******************************************/
-/*
-** 2001 September 15
-**
-** The author disclaims copyright to this source code.  In place of
-** a legal notice, here is a blessing:
-**
-**    May you do good and not evil.
-**    May you find forgiveness for yourself and forgive others.
-**    May you share freely, never taking more than you give.
-**
-*************************************************************************
-** Memory allocation functions used throughout sqlite.
-**
-**
-** $Id: malloc.c,v 1.3 2007/06/15 20:29:20 drh Exp $
-*/
-
-/*
-** MALLOC WRAPPER ARCHITECTURE
-**
-** The sqlite code accesses dynamic memory allocation/deallocation by invoking
-** the following six APIs (which may be implemented as macros).
-**
-**     sqlite3Malloc()
-**     sqlite3MallocRaw()
-**     sqlite3Realloc()
-**     sqlite3ReallocOrFree()
-**     sqlite3Free()
-**     sqlite3AllocSize()
-**
-** The function sqlite3FreeX performs the same task as sqlite3Free and is
-** guaranteed to be a real function. The same holds for sqlite3MallocX
-**
-** The above APIs are implemented in terms of the functions provided in the
-** operating-system interface. The OS interface is never accessed directly
-** by code outside of this file.
-**
-**     sqlite3OsMalloc()
-**     sqlite3OsRealloc()
-**     sqlite3OsFree()
-**     sqlite3OsAllocationSize()
-**
-** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke
-** sqlite3_release_memory() if a call to sqlite3OsMalloc() or
-** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is
-** exceeded). Function sqlite3Malloc() usually invokes
-** sqlite3MallocRaw().
-**
-** MALLOC TEST WRAPPER ARCHITECTURE
-**
-** The test wrapper provides extra test facilities to ensure the library
-** does not leak memory and handles the failure of the underlying OS level
-** allocation system correctly. It is only present if the library is
-** compiled with the SQLITE_MEMDEBUG macro set.
-**
-**     * Guardposts to detect overwrites.
-**     * Ability to cause a specific Malloc() or Realloc() to fail.
-**     * Audit outstanding memory allocations (i.e check for leaks).
-*/
-
-#define MAX(x,y) ((x)>(y)?(x):(y))
-
-#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
-/*
-** Set the soft heap-size limit for the current thread. Passing a negative
-** value indicates no limit.
-*/
-void sqlite3_soft_heap_limit(int n){
-  ThreadData *pTd = sqlite3ThreadData();
-  if( pTd ){
-    pTd->nSoftHeapLimit = n;
-  }
-  sqlite3ReleaseThreadData();
-}
-
-/*
-** Release memory held by SQLite instances created by the current thread.
-*/
-int sqlite3_release_memory(int n){
-  return sqlite3PagerReleaseMemory(n);
-}
-#else
-/* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version
-** of sqlite3_release_memory() to be used by other code in this file.
-** This is done for no better reason than to reduce the number of
-** pre-processor #ifndef statements.
-*/
-#define sqlite3_release_memory(x) 0    /* 0 == no memory freed */
-#endif
-
-#ifdef SQLITE_MEMDEBUG
-/*--------------------------------------------------------------------------
-** Begin code for memory allocation system test layer.
-**
-** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro.
-**
-** SQLITE_MEMDEBUG==1    -> Fence-posting only (thread safe)
-** SQLITE_MEMDEBUG==2    -> Fence-posting + linked list of allocations (not ts)
-** SQLITE_MEMDEBUG==3    -> Above + backtraces (not thread safe, req. glibc)
-*/
-
-/* Figure out whether or not to store backtrace() information for each malloc.
-** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or
-** greater and glibc is in use. If we don't want to use backtrace(), then just
-** define it as an empty macro and set the amount of space reserved to 0.
-*/
-#if defined(__GLIBC__) && SQLITE_MEMDEBUG>2
-  extern int backtrace(void **, int);
-  #define TESTALLOC_STACKSIZE 128
-  #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*))
-#else
-  #define backtrace(x, y)
-  #define TESTALLOC_STACKSIZE 0
-  #define TESTALLOC_STACKFRAMES 0
-#endif
-
-/*
-** Number of 32-bit guard words.  This should probably be a multiple of
-** 2 since on 64-bit machines we want the value returned by sqliteMalloc()
-** to be 8-byte aligned.
-*/
-#ifndef TESTALLOC_NGUARD
-# define TESTALLOC_NGUARD 2
-#endif
-
-/*
-** Size reserved for storing file-name along with each malloc()ed blob.
-*/
-#define TESTALLOC_FILESIZE 64
-
-/*
-** Size reserved for storing the user string. Each time a Malloc() or Realloc()
-** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by
-** sqlite3_malloc_id are stored along with the other test system metadata.
-*/
-#define TESTALLOC_USERSIZE 64
-const char *sqlite3_malloc_id = 0;
-
-/*
-** Blocks used by the test layer have the following format:
-**
-**        <sizeof(void *) pNext pointer>
-**        <sizeof(void *) pPrev pointer>
-**        <TESTALLOC_NGUARD 32-bit guard words>
-**            <The application level allocation>
-**        <TESTALLOC_NGUARD 32-bit guard words>
-**        <32-bit line number>
-**        <TESTALLOC_FILESIZE bytes containing null-terminated file name>
-**        <TESTALLOC_STACKSIZE bytes of backtrace() output>
-*/
-
-#define TESTALLOC_OFFSET_GUARD1(p)    (sizeof(void *) * 2)
-#define TESTALLOC_OFFSET_DATA(p) ( \
-  TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \
-)
-#define TESTALLOC_OFFSET_GUARD2(p) ( \
-  TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \
-)
-#define TESTALLOC_OFFSET_LINENUMBER(p) ( \
-  TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \
-)
-#define TESTALLOC_OFFSET_FILENAME(p) ( \
-  TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \
-)
-#define TESTALLOC_OFFSET_USER(p) ( \
-  TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \
-)
-#define TESTALLOC_OFFSET_STACK(p) ( \
-  TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \
-  (TESTALLOC_OFFSET_USER(p) % 8) \
-)
-
-#define TESTALLOC_OVERHEAD ( \
-  sizeof(void *)*2 +                   /* pPrev and pNext pointers */   \
-  TESTALLOC_NGUARD*sizeof(u32)*2 +              /* Guard words */       \
-  sizeof(u32) + TESTALLOC_FILESIZE +   /* File and line number */       \
-  TESTALLOC_USERSIZE +                 /* User string */                \
-  TESTALLOC_STACKSIZE                  /* backtrace() stack */          \
-)
-
-
-/*
-** For keeping track of the number of mallocs and frees.   This
-** is used to check for memory leaks.  The iMallocFail and iMallocReset
-** values are used to simulate malloc() failures during testing in
-** order to verify that the library correctly handles an out-of-memory
-** condition.
-*/
-int sqlite3_nMalloc;         /* Number of sqliteMalloc() calls */
-int sqlite3_nFree;           /* Number of sqliteFree() calls */
-int sqlite3_memUsed;         /* TODO Total memory obtained from malloc */
-int sqlite3_memMax;          /* TODO Mem usage high-water mark */
-int sqlite3_iMallocFail;     /* Fail sqliteMalloc() after this many calls */
-int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
-
-void *sqlite3_pFirst = 0;         /* Pointer to linked list of allocations */
-int sqlite3_nMaxAlloc = 0;        /* High water mark of ThreadData.nAlloc */
-int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */
-int sqlite3_isFail = 0;           /* True if all malloc calls should fail */
-const char *sqlite3_zFile = 0;    /* Filename to associate debug info with */
-int sqlite3_iLine = 0;            /* Line number for debug info */
-int sqlite3_mallocfail_trace = 0; /* Print a msg on malloc fail if true */
-
-/*
-** Check for a simulated memory allocation failure.  Return true if
-** the failure should be simulated.  Return false to proceed as normal.
-*/
-SQLITE_PRIVATE int sqlite3TestMallocFail(){
-  if( sqlite3_isFail ){
-    return 1;
-  }
-  if( sqlite3_iMallocFail>=0 ){
-    sqlite3_iMallocFail--;
-    if( sqlite3_iMallocFail==0 ){
-      sqlite3_iMallocFail = sqlite3_iMallocReset;
-      sqlite3_isFail = 1;
-      if( sqlite3_mallocfail_trace ){
-         sqlite3DebugPrintf("###_malloc_fails_###\n");
-      }
-      return 1;
-    }
-  }
-  return 0;
-}
-
-/*
-** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc().
-** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the
-** values set by the applyGuards() function.
-*/
-static void checkGuards(u32 *p)
-{
-  int i;
-  char *zAlloc = (char *)p;
+/************** Begin file mem1.c ********************************************/
+/*
+** 2007 August 14
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains the C functions that implement a memory
+** allocation subsystem for use by SQLite.
+**
+** $Id: mem1.c,v 1.10 2007/09/02 17:50:35 drh Exp $
+*/
+
+/*
+** This version of the memory allocator is the default.  It is
+** used when no other memory allocator is specified using compile-time
+** macros.
+*/
+#if !defined(SQLITE_MEMDEBUG) && !defined(SQLITE_OMIT_MEMORY_ALLOCATION)
+
+/*
+** We will eventually construct multiple memory allocation subsystems
+** suitable for use in various contexts:
+**
+**    *  Normal multi-threaded builds
+**    *  Normal single-threaded builds
+**    *  Debugging builds
+**
+** This initial version is suitable for use in normal multi-threaded
+** builds.  We envision that alternative versions will be stored in
+** separate source files.  #ifdefs will be used to select the code from
+** one of the various memN.c source files for use in any given build.
+*/
+
+/*
+** All of the static variables used by this module are collected
+** into a single structure named "mem".  This is to keep the
+** static variables organized and to reduce namespace pollution
+** when this module is combined with other in the amalgamation.
+*/
+static struct {
+  /*
+  ** The alarm callback and its arguments.  The mem.mutex lock will
+  ** be held while the callback is running.  Recursive calls into
+  ** the memory subsystem are allowed, but no new callbacks will be
+  ** issued.  The alarmBusy variable is set to prevent recursive
+  ** callbacks.
+  */
+  sqlite3_int64 alarmThreshold;
+  void (*alarmCallback)(void*, sqlite3_int64,int);
+  void *alarmArg;
+  int alarmBusy;
+
+  /*
+  ** Mutex to control access to the memory allocation subsystem.
+  */
+  sqlite3_mutex *mutex;
+
+  /*
+  ** Current allocation and high-water mark.
+  */
+  sqlite3_int64 nowUsed;
+  sqlite3_int64 mxUsed;
+
+
+} mem;
+
+/*
+** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
+*/
+static void enterMem(void){
+  if( mem.mutex==0 ){
+    mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
+  }
+  sqlite3_mutex_enter(mem.mutex);
+}
+
+/*
+** Return the amount of memory currently checked out.
+*/
+SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
+  sqlite3_int64 n;
+  enterMem();
+  n = mem.nowUsed;
+  sqlite3_mutex_leave(mem.mutex);
+  return n;
+}
+
+/*
+** Return the maximum amount of memory that has ever been
+** checked out since either the beginning of this process
+** or since the most recent reset.
+*/
+SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
+  sqlite3_int64 n;
+  enterMem();
+  n = mem.mxUsed;
+  if( resetFlag ){
+    mem.mxUsed = mem.nowUsed;
+  }
+  sqlite3_mutex_leave(mem.mutex);
+  return n;
+}
+
+/*
+** Change the alarm callback
+*/
+SQLITE_API int sqlite3_memory_alarm(
+  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
+  void *pArg,
+  sqlite3_int64 iThreshold
+){
+  enterMem();
+  mem.alarmCallback = xCallback;
+  mem.alarmArg = pArg;
+  mem.alarmThreshold = iThreshold;
+  sqlite3_mutex_leave(mem.mutex);
+  return SQLITE_OK;
+}
+
+/*
+** Trigger the alarm
+*/
+static void sqlite3MemsysAlarm(int nByte){
+  void (*xCallback)(void*,sqlite3_int64,int);
+  sqlite3_int64 nowUsed;
+  void *pArg;
+  if( mem.alarmCallback==0 || mem.alarmBusy  ) return;
+  mem.alarmBusy = 1;
+  xCallback = mem.alarmCallback;
+  nowUsed = mem.nowUsed;
+  pArg = mem.alarmArg;
+  sqlite3_mutex_leave(mem.mutex);
+  xCallback(pArg, nowUsed, nByte);
+  sqlite3_mutex_enter(mem.mutex);
+  mem.alarmBusy = 0;
+}
+
+/*
+** Allocate nBytes of memory
+*/
+SQLITE_API void *sqlite3_malloc(int nBytes){
+  sqlite3_int64 *p = 0;
+  if( nBytes>0 ){
+    enterMem();
+    if( mem.alarmCallback!=0 && mem.nowUsed+nBytes>=mem.alarmThreshold ){
+      sqlite3MemsysAlarm(nBytes);
+    }
+    p = malloc(nBytes+8);
+    if( p==0 ){
+      sqlite3MemsysAlarm(nBytes);
+      p = malloc(nBytes+8);
+    }
+    if( p ){
+      p[0] = nBytes;
+      p++;
+      mem.nowUsed += nBytes;
+      if( mem.nowUsed>mem.mxUsed ){
+        mem.mxUsed = mem.nowUsed;
+      }
+    }
+    sqlite3_mutex_leave(mem.mutex);
+  }
+  return (void*)p;
+}
+
+/*
+** Free memory.
+*/
+SQLITE_API void sqlite3_free(void *pPrior){
+  sqlite3_int64 *p;
+  int nByte;
+  if( pPrior==0 ){
+    return;
+  }
+  assert( mem.mutex!=0 );
+  p = pPrior;
+  p--;
+  nByte = (int)*p;
+  sqlite3_mutex_enter(mem.mutex);
+  mem.nowUsed -= nByte;
+  free(p);
+  sqlite3_mutex_leave(mem.mutex);
+}
+
+/*
+** Change the size of an existing memory allocation
+*/
+SQLITE_API void *sqlite3_realloc(void *pPrior, int nBytes){
+  int nOld;
+  sqlite3_int64 *p;
+  if( pPrior==0 ){
+    return sqlite3_malloc(nBytes);
+  }
+  if( nBytes<=0 ){
+    sqlite3_free(pPrior);
+    return 0;
+  }
+  p = pPrior;
+  p--;
+  nOld = (int)p[0];
+  assert( mem.mutex!=0 );
+  sqlite3_mutex_enter(mem.mutex);
+  if( mem.nowUsed+nBytes-nOld>=mem.alarmThreshold ){
+    sqlite3MemsysAlarm(nBytes-nOld);
+  }
+  p = realloc(p, nBytes+8);
+  if( p==0 ){
+    sqlite3MemsysAlarm(nBytes);
+    p = realloc(p, nBytes+8);
+  }
+  if( p ){
+    p[0] = nBytes;
+    p++;
+    mem.nowUsed += nBytes-nOld;
+    if( mem.nowUsed>mem.mxUsed ){
+      mem.mxUsed = mem.nowUsed;
+    }
+  }
+  sqlite3_mutex_leave(mem.mutex);
+  return (void*)p;
+}
+
+#endif /* !SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */
+
+/************** End of mem1.c ************************************************/
+/************** Begin file mem2.c ********************************************/
+/*
+** 2007 August 15
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains the C functions that implement a memory
+** allocation subsystem for use by SQLite.
+**
+** $Id: mem2.c,v 1.13 2007/09/01 09:02:54 danielk1977 Exp $
+*/
+
+/*
+** This version of the memory allocator is used only if the
+** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION
+** is not defined.
+*/
+#if defined(SQLITE_MEMDEBUG) && !defined(SQLITE_OMIT_MEMORY_ALLOCATION)
+
+/*
+** We will eventually construct multiple memory allocation subsystems
+** suitable for use in various contexts:
+**
+**    *  Normal multi-threaded builds
+**    *  Normal single-threaded builds
+**    *  Debugging builds
+**
+** This version is suitable for use in debugging builds.
+**
+** Features:
+**
+**    * Every allocate has guards at both ends.
+**    * New allocations are initialized with randomness
+**    * Allocations are overwritten with randomness when freed
+**    * Optional logs of malloc activity generated
+**    * Summary of outstanding allocations with backtraces to the
+**      point of allocation.
+**    * The ability to simulate memory allocation failure
+*/
+
+/*
+** The backtrace functionality is only available with GLIBC
+*/
+#ifdef __GLIBC__
+  extern int backtrace(void**,int);
+  extern void backtrace_symbols_fd(void*const*,int,int);
+#else
+# define backtrace(A,B) 0
+# define backtrace_symbols_fd(A,B,C)
+#endif
+
+/*
+** Each memory allocation looks like this:
+**
+**  ------------------------------------------------------------------------
+**  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
+**  ------------------------------------------------------------------------
+**
+** The application code sees only a pointer to the allocation.  We have
+** to back up from the allocation pointer to find the MemBlockHdr.  The
+** MemBlockHdr tells us the size of the allocation and the number of
+** backtrace pointers.  There is also a guard word at the end of the
+** MemBlockHdr.
+*/
+struct MemBlockHdr {
+  struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
+  int iSize;                          /* Size of this allocation */
+  char nBacktrace;                    /* Number of backtraces on this alloc */
+  char nBacktraceSlots;               /* Available backtrace slots */
+  short nTitle;                       /* Bytes of title; includes '\0' */
+  int iForeGuard;                     /* Guard word for sanity */
+};
+
+/*
+** Guard words
+*/
+#define FOREGUARD 0x80F5E153
+#define REARGUARD 0xE4676B53
+
+/*
+** All of the static variables used by this module are collected
+** into a single structure named "mem".  This is to keep the
+** static variables organized and to reduce namespace pollution
+** when this module is combined with other in the amalgamation.
+*/
+static struct {
+  /*
+  ** The alarm callback and its arguments.  The mem.mutex lock will
+  ** be held while the callback is running.  Recursive calls into
+  ** the memory subsystem are allowed, but no new callbacks will be
+  ** issued.  The alarmBusy variable is set to prevent recursive
+  ** callbacks.
+  */
+  sqlite3_int64 alarmThreshold;
+  void (*alarmCallback)(void*, sqlite3_int64, int);
+  void *alarmArg;
+  int alarmBusy;
+
+  /*
+  ** Mutex to control access to the memory allocation subsystem.
+  */
+  sqlite3_mutex *mutex;
+
+  /*
+  ** Current allocation and high-water mark.
+  */
+  sqlite3_int64 nowUsed;
+  sqlite3_int64 mxUsed;
+
+  /*
+  ** Head and tail of a linked list of all outstanding allocations
+  */
+  struct MemBlockHdr *pFirst;
+  struct MemBlockHdr *pLast;
+
+  /*
+  ** The number of levels of backtrace to save in new allocations.
+  */
+  int nBacktrace;
+
+  /*
+  ** Title text to insert in front of each block
+  */
+  int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
+  char zTitle[100];  /* The title text */
+
+  /*
+  ** These values are used to simulate malloc failures.  When
+  ** iFail is 1, simulate a malloc failures and reset the value
+  ** to iReset.
+  */
+  int iFail;    /* Decrement and fail malloc when this is 1 */
+  int iReset;   /* When malloc fails set iiFail to this value */
+  int iFailCnt;         /* Number of failures */
+  int iBenignFailCnt;   /* Number of benign failures */
+  int iNextIsBenign;    /* True if the next call to malloc may fail benignly */
+
+  /*
+  ** sqlite3MallocDisallow() increments the following counter.
+  ** sqlite3MallocAllow() decrements it.
+  */
+  int disallow; /* Do not allow memory allocation */
+
+
+} mem;
+
+
+/*
+** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
+*/
+static void enterMem(void){
+  if( mem.mutex==0 ){
+    mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM);
+  }
+  sqlite3_mutex_enter(mem.mutex);
+}
+
+/*
+** Return the amount of memory currently checked out.
+*/
+SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
+  sqlite3_int64 n;
+  enterMem();
+  n = mem.nowUsed;
+  sqlite3_mutex_leave(mem.mutex);
+  return n;
+}
+
+/*
+** Return the maximum amount of memory that has ever been
+** checked out since either the beginning of this process
+** or since the most recent reset.
+*/
+SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
+  sqlite3_int64 n;
+  enterMem();
+  n = mem.mxUsed;
+  if( resetFlag ){
+    mem.mxUsed = mem.nowUsed;
+  }
+  sqlite3_mutex_leave(mem.mutex);
+  return n;
+}
+
+/*
+** Change the alarm callback
+*/
+SQLITE_API int sqlite3_memory_alarm(
+  void(*xCallback)(void *pArg, sqlite3_int64 used, int N),
+  void *pArg,
+  sqlite3_int64 iThreshold
+){
+  enterMem();
+  mem.alarmCallback = xCallback;
+  mem.alarmArg = pArg;
+  mem.alarmThreshold = iThreshold;
+  sqlite3_mutex_leave(mem.mutex);
+  return SQLITE_OK;
+}
+
+/*
+** Trigger the alarm
+*/
+static void sqlite3MemsysAlarm(int nByte){
+  void (*xCallback)(void*,sqlite3_int64,int);
+  sqlite3_int64 nowUsed;
+  void *pArg;
+  if( mem.alarmCallback==0 || mem.alarmBusy  ) return;
+  mem.alarmBusy = 1;
+  xCallback = mem.alarmCallback;
+  nowUsed = mem.nowUsed;
+  pArg = mem.alarmArg;
+  sqlite3_mutex_leave(mem.mutex);
+  xCallback(pArg, nowUsed, nByte);
+  sqlite3_mutex_enter(mem.mutex);
+  mem.alarmBusy = 0;
+}
+
+/*
+** Given an allocation, find the MemBlockHdr for that allocation.
+**
+** This routine checks the guards at either end of the allocation and
+** if they are incorrect it asserts.
+*/
+static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
+  struct MemBlockHdr *p;
+  int *pInt;
+
+  p = (struct MemBlockHdr*)pAllocation;
+  p--;
+  assert( p->iForeGuard==FOREGUARD );
+  assert( (p->iSize & 3)==0 );
+  pInt = (int*)pAllocation;
+  assert( pInt[p->iSize/sizeof(int)]==REARGUARD );
+  return p;
+}
+
+/*
+** This routine is called once the first time a simulated memory
+** failure occurs.  The sole purpose of this routine is to provide
+** a convenient place to set a debugger breakpoint when debugging
+** errors related to malloc() failures.
+*/
+static void sqlite3MemsysFailed(void){
+  mem.iFailCnt = 0;
+  mem.iBenignFailCnt = 0;
+}
+
+/*
+** Allocate nByte bytes of memory.
+*/
+SQLITE_API void *sqlite3_malloc(int nByte){
+  struct MemBlockHdr *pHdr;
+  void **pBt;
   char *z;
-
-  /* First set of guard words */
-  z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
-  for(i=0; i<TESTALLOC_NGUARD; i++){
-    assert(((u32 *)z)[i]==0xdead1122);
-  }
-
-  /* Second set of guard words */
-  z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
-  for(i=0; i<TESTALLOC_NGUARD; i++){
-    u32 guard = 0;
-    memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32));
-    assert(guard==0xdead3344);
-  }
-}
-
-/*
-** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The
-** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as
-** guard-posts.
-*/
-static void applyGuards(u32 *p)
-{
-  int i;
+  int *pInt;
+  void *p = 0;
+  int totalSize;
+
+  if( nByte>0 ){
+    enterMem();
+    assert( mem.disallow==0 );
+    if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
+      sqlite3MemsysAlarm(nByte);
+    }
+    nByte = (nByte+3)&~3;
+    totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
+                 mem.nBacktrace*sizeof(void*) + mem.nTitle;
+    if( mem.iFail>0 ){
+      if( mem.iFail==1 ){
+        p = 0;
+        mem.iFail = mem.iReset;
+        if( mem.iFailCnt==0 ){
+          sqlite3MemsysFailed();  /* A place to set a breakpoint */
+        }
+        mem.iFailCnt++;
+        if( mem.iNextIsBenign ){
+          mem.iBenignFailCnt++;
+        }
+      }else{
+        p = malloc(totalSize);
+        mem.iFail--;
+      }
+    }else{
+      p = malloc(totalSize);
+      if( p==0 ){
+        sqlite3MemsysAlarm(nByte);
+        p = malloc(totalSize);
+      }
+    }
+    if( p ){
+      z = p;
+      pBt = (void**)&z[mem.nTitle];
+      pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
+      pHdr->pNext = 0;
+      pHdr->pPrev = mem.pLast;
+      if( mem.pLast ){
+        mem.pLast->pNext = pHdr;
+      }else{
+        mem.pFirst = pHdr;
+      }
+      mem.pLast = pHdr;
+      pHdr->iForeGuard = FOREGUARD;
+      pHdr->nBacktraceSlots = mem.nBacktrace;
+      pHdr->nTitle = mem.nTitle;
+      if( mem.nBacktrace ){
+        void *aAddr[40];
+        pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
+        memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
+      }else{
+        pHdr->nBacktrace = 0;
+      }
+      if( mem.nTitle ){
+        memcpy(z, mem.zTitle, mem.nTitle);
+      }
+      pHdr->iSize = nByte;
+      pInt = (int*)&pHdr[1];
+      pInt[nByte/sizeof(int)] = REARGUARD;
+      memset(pInt, 0x65, nByte);
+      mem.nowUsed += nByte;
+      if( mem.nowUsed>mem.mxUsed ){
+        mem.mxUsed = mem.nowUsed;
+      }
+      p = (void*)pInt;
+    }
+    sqlite3_mutex_leave(mem.mutex);
+  }
+  mem.iNextIsBenign = 0;
+  return p;
+}
+
+/*
+** Free memory.
+*/
+SQLITE_API void sqlite3_free(void *pPrior){
+  struct MemBlockHdr *pHdr;
+  void **pBt;
   char *z;
-  char *zAlloc = (char *)p;
-
-  /* First set of guard words */
-  z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
-  for(i=0; i<TESTALLOC_NGUARD; i++){
-    ((u32 *)z)[i] = 0xdead1122;
-  }
-
-  /* Second set of guard words */
-  z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
-  for(i=0; i<TESTALLOC_NGUARD; i++){
-    static const int guard = 0xdead3344;
-    memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32));
-  }
-
-  /* Line number */
-  z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)];             /* Guard words */
-  z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
-  memcpy(z, &sqlite3_iLine, sizeof(u32));
-
-  /* File name */
-  z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
-  strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE);
-  z[TESTALLOC_FILESIZE - 1] = '\0';
-
-  /* User string */
-  z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
-  z[0] = 0;
-  if( sqlite3_malloc_id ){
-    strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE);
-    z[TESTALLOC_USERSIZE-1] = 0;
-  }
-
-  /* backtrace() stack */
-  z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
-  backtrace((void **)z, TESTALLOC_STACKFRAMES);
-
-  /* Sanity check to make sure checkGuards() is working */
-  checkGuards(p);
-}
-
-/*
-** The argument is a malloc()ed pointer as returned by the test-wrapper.
-** Return a pointer to the Os level allocation.
-*/
-static void *getOsPointer(void *p)
-{
-  char *z = (char *)p;
-  return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]);
-}
-
-
-#if SQLITE_MEMDEBUG>1
-/*
-** The argument points to an Os level allocation. Link it into the threads list
-** of allocations.
-*/
-static void linkAlloc(void *p){
-  void **pp = (void **)p;
-  pp[0] = 0;
-  pp[1] = sqlite3_pFirst;
-  if( sqlite3_pFirst ){
-    ((void **)sqlite3_pFirst)[0] = p;
-  }
-  sqlite3_pFirst = p;
-}
-
-/*
-** The argument points to an Os level allocation. Unlinke it from the threads
-** list of allocations.
-*/
-static void unlinkAlloc(void *p)
-{
-  void **pp = (void **)p;
-  if( p==sqlite3_pFirst ){
-    assert(!pp[0]);
-    assert(!pp[1] || ((void **)(pp[1]))[0]==p);
-    sqlite3_pFirst = pp[1];
-    if( sqlite3_pFirst ){
-      ((void **)sqlite3_pFirst)[0] = 0;
-    }
-  }else{
-    void **pprev = pp[0];
-    void **pnext = pp[1];
-    assert(pprev);
-    assert(pprev[1]==p);
-    pprev[1] = (void *)pnext;
-    if( pnext ){
-      assert(pnext[0]==p);
-      pnext[0] = (void *)pprev;
-    }
-  }
-}
-
-/*
-** Pointer p is a pointer to an OS level allocation that has just been
-** realloc()ed. Set the list pointers that point to this entry to it's new
-** location.
-*/
-static void relinkAlloc(void *p)
-{
-  void **pp = (void **)p;
-  if( pp[0] ){
-    ((void **)(pp[0]))[1] = p;
-  }else{
-    sqlite3_pFirst = p;
-  }
-  if( pp[1] ){
-    ((void **)(pp[1]))[0] = p;
-  }
-}
-#else
-#define linkAlloc(x)
-#define relinkAlloc(x)
-#define unlinkAlloc(x)
-#endif
-
-/*
-** This function sets the result of the Tcl interpreter passed as an argument
-** to a list containing an entry for each currently outstanding call made to
-** sqliteMalloc and friends by the current thread. Each list entry is itself a
-** list, consisting of the following (in order):
-**
-**     * The number of bytes allocated
-**     * The __FILE__ macro at the time of the sqliteMalloc() call.
-**     * The __LINE__ macro ...
-**     * The value of the sqlite3_malloc_id variable ...
-**     * The output of backtrace() (if available) ...
-**
-** Todo: We could have a version of this function that outputs to stdout,
-** to debug memory leaks when Tcl is not available.
-*/
-#if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1
-SQLITE_PRIVATE int sqlite3OutstandingMallocs(Tcl_Interp *interp){
-  void *p;
-  Tcl_Obj *pRes = Tcl_NewObj();
-  Tcl_IncrRefCount(pRes);
-
-
-  for(p=sqlite3_pFirst; p; p=((void **)p)[1]){
-    Tcl_Obj *pEntry = Tcl_NewObj();
-    Tcl_Obj *pStack = Tcl_NewObj();
-    char *z;
-    u32 iLine;
-    int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD;
-    char *zAlloc = (char *)p;
-    int i;
-
-    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes));
-
-    z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
-    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
-
-    z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
-    memcpy(&iLine, z, sizeof(u32));
-    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine));
-
-    z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
-    Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
-
-    z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
-    for(i=0; i<TESTALLOC_STACKFRAMES; i++){
-      char zHex[128];
-      sqlite3_snprintf(sizeof(zHex), zHex, "%p", ((void **)z)[i]);
-      Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1));
-    }
-
-    Tcl_ListObjAppendElement(0, pEntry, pStack);
-    Tcl_ListObjAppendElement(0, pRes, pEntry);
-  }
-
-  Tcl_ResetResult(interp);
-  Tcl_SetObjResult(interp, pRes);
-  Tcl_DecrRefCount(pRes);
-  return TCL_OK;
-}
-#endif
-
-/*
-** This is the test layer's wrapper around sqlite3OsMalloc().
-*/
-static void * OSMALLOC(int n){
-  sqlite3OsEnterMutex();
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  sqlite3_nMaxAlloc =
-      MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
-#endif
-  assert( !sqlite3_mallocDisallowed );
-  if( !sqlite3TestMallocFail() ){
-    u32 *p;
-    p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
-    assert(p);
-    sqlite3_nMalloc++;
-    applyGuards(p);
-    linkAlloc(p);
-    sqlite3OsLeaveMutex();
-    return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
-  }
-  sqlite3OsLeaveMutex();
-  return 0;
-}
-
-static int OSSIZEOF(void *p){
+  if( pPrior==0 ){
+    return;
+  }
+  assert( mem.mutex!=0 );
+  pHdr = sqlite3MemsysGetHeader(pPrior);
+  pBt = (void**)pHdr;
+  pBt -= pHdr->nBacktraceSlots;
+  sqlite3_mutex_enter(mem.mutex);
+  mem.nowUsed -= pHdr->iSize;
+  if( pHdr->pPrev ){
+    assert( pHdr->pPrev->pNext==pHdr );
+    pHdr->pPrev->pNext = pHdr->pNext;
+  }else{
+    assert( mem.pFirst==pHdr );
+    mem.pFirst = pHdr->pNext;
+  }
+  if( pHdr->pNext ){
+    assert( pHdr->pNext->pPrev==pHdr );
+    pHdr->pNext->pPrev = pHdr->pPrev;
+  }else{
+    assert( mem.pLast==pHdr );
+    mem.pLast = pHdr->pPrev;
+  }
+  z = (char*)pBt;
+  z -= pHdr->nTitle;
+  memset(z, 0x2b, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
+                  pHdr->iSize + sizeof(int) + pHdr->nTitle);
+  free(z);
+  sqlite3_mutex_leave(mem.mutex);
+}
+
+/*
+** Change the size of an existing memory allocation.
+**
+** For this debugging implementation, we *always* make a copy of the
+** allocation into a new place in memory.  In this way, if the
+** higher level code is using pointer to the old allocation, it is
+** much more likely to break and we are much more liking to find
+** the error.
+*/
+SQLITE_API void *sqlite3_realloc(void *pPrior, int nByte){
+  struct MemBlockHdr *pOldHdr;
+  void *pNew;
+  if( pPrior==0 ){
+    return sqlite3_malloc(nByte);
+  }
+  if( nByte<=0 ){
+    sqlite3_free(pPrior);
+    return 0;
+  }
+  assert( mem.disallow==0 );
+  pOldHdr = sqlite3MemsysGetHeader(pPrior);
+  pNew = sqlite3_malloc(nByte);
+  if( pNew ){
+    memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
+    if( nByte>pOldHdr->iSize ){
+      memset(&((char*)pNew)[pOldHdr->iSize], 0x2b, nByte - pOldHdr->iSize);
+    }
+    sqlite3_free(pPrior);
+  }
+  return pNew;
+}
+
+/*
+** Set the number of backtrace levels kept for each allocation.
+** A value of zero turns of backtracing.  The number is always rounded
+** up to a multiple of 2.
+*/
+SQLITE_API void sqlite3_memdebug_backtrace(int depth){
+  if( depth<0 ){ depth = 0; }
+  if( depth>20 ){ depth = 20; }
+  depth = (depth+1)&0xfe;
+  mem.nBacktrace = depth;
+}
+
+/*
+** Set the title string for subsequent allocations.
+*/
+SQLITE_API void sqlite3_memdebug_settitle(const char *zTitle){
+  int n = strlen(zTitle) + 1;
+  enterMem();
+  if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
+  memcpy(mem.zTitle, zTitle, n);
+  mem.zTitle[n] = 0;
+  mem.nTitle = (n+3)&~3;
+  sqlite3_mutex_leave(mem.mutex);
+}
+
+/*
+** Open the file indicated and write a log of all unfreed memory
+** allocations into that log.
+*/
+SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){
+  FILE *out;
+  struct MemBlockHdr *pHdr;
+  void **pBt;
+  out = fopen(zFilename, "w");
+  if( out==0 ){
+    fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
+                    zFilename);
+    return;
+  }
+  for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
+    char *z = (char*)pHdr;
+    z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
+    fprintf(out, "**** %d bytes at %p from %s ****\n",
+            pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
+    if( pHdr->nBacktrace ){
+      fflush(out);
+      pBt = (void**)pHdr;
+      pBt -= pHdr->nBacktraceSlots;
+      backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
+      fprintf(out, "\n");
+    }
+  }
+  fclose(out);
+}
+
+/*
+** This routine is used to simulate malloc failures.
+**
+** After calling this routine, there will be iFail successful
+** memory allocations and then a failure.  If iRepeat is 1
+** all subsequent memory allocations will fail.  If iRepeat is
+** 0, only a single allocation will fail.  If iRepeat is negative
+** then the previous setting for iRepeat is unchanged.
+**
+** Each call to this routine overrides the previous.  To disable
+** the simulated allocation failure mechanism, set iFail to -1.
+**
+** This routine returns the number of simulated failures that have
+** occurred since the previous call.
+*/
+SQLITE_API int sqlite3_memdebug_fail(int iFail, int iRepeat, int *piBenign){
+  int n = mem.iFailCnt;
+  if( piBenign ){
+    *piBenign = mem.iBenignFailCnt;
+  }
+  mem.iFail = iFail+1;
+  if( iRepeat>=0 ){
+    mem.iReset = iRepeat;
+  }
+  mem.iFailCnt = 0;
+  mem.iBenignFailCnt = 0;
+  return n;
+}
+
+SQLITE_API int sqlite3_memdebug_pending(){
+  return (mem.iFail-1);
+}
+
+SQLITE_PRIVATE void sqlite3MallocBenignFailure(int isBenign){
+  if( isBenign ){
+    mem.iNextIsBenign = 1;
+  }
+}
+
+/*
+** The following two routines are used to assert that no memory
+** allocations occur between one call and the next.  The use of
+** these routines does not change the computed results in any way.
+** These routines are like asserts.
+*/
+SQLITE_PRIVATE void sqlite3MallocDisallow(void){
+  assert( mem.mutex!=0 );
+  sqlite3_mutex_enter(mem.mutex);
+  mem.disallow++;
+  sqlite3_mutex_leave(mem.mutex);
+}
+SQLITE_PRIVATE void sqlite3MallocAllow(void){
+  assert( mem.mutex );
+  sqlite3_mutex_enter(mem.mutex);
+  assert( mem.disallow>0 );
+  mem.disallow--;
+  sqlite3_mutex_leave(mem.mutex);
+}
+
+#endif /* SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */
+
+/************** End of mem2.c ************************************************/
+/************** Begin file mutex.c *******************************************/
+/*
+** 2007 August 14
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains the C functions that implement mutexes.
+**
+** The implementation in this file does not provide any mutual
+** exclusion and is thus suitable for use only in applications
+** that use SQLite in a single thread.  But this implementation
+** does do a lot of error checking on mutexes to make sure they
+** are called correctly and at appropriate times.  Hence, this
+** implementation is suitable for testing.
+** debugging purposes
+**
+** $Id: mutex.c,v 1.16 2007/09/10 16:13:00 danielk1977 Exp $
+*/
+
+#ifdef SQLITE_MUTEX_NOOP_DEBUG
+/*
+** In this implementation, mutexes do not provide any mutual exclusion.
+** But the error checking is provided.  This implementation is useful
+** for test purposes.
+*/
+
+/*
+** The mutex object
+*/
+struct sqlite3_mutex {
+  int id;     /* The mutex type */
+  int cnt;    /* Number of entries without a matching leave */
+};
+
+/*
+** The sqlite3_mutex_alloc() routine allocates a new
+** mutex and returns a pointer to it.  If it returns NULL
+** that means that a mutex could not be allocated.
+*/
+SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
+  static sqlite3_mutex aStatic[5];
+  sqlite3_mutex *pNew = 0;
+  switch( id ){
+    case SQLITE_MUTEX_FAST:
+    case SQLITE_MUTEX_RECURSIVE: {
+      pNew = sqlite3_malloc(sizeof(*pNew));
+      if( pNew ){
+        pNew->id = id;
+        pNew->cnt = 0;
+      }
+      break;
+    }
+    default: {
+      assert( id-2 >= 0 );
+      assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) );
+      pNew = &aStatic[id-2];
+      pNew->id = id;
+      break;
+    }
+  }
+  return pNew;
+}
+
+/*
+** This routine deallocates a previously allocated mutex.
+*/
+SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
+  assert( p );
+  assert( p->cnt==0 );
+  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
+  sqlite3_free(p);
+}
+
+/*
+** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
+** to enter a mutex.  If another thread is already within the mutex,
+** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
+** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
+** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
+** be entered multiple times by the same thread.  In such cases the,
+** mutex must be exited an equal number of times before another thread
+** can enter.  If the same thread tries to enter any other kind of mutex
+** more than once, the behavior is undefined.
+*/
+SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  p->cnt++;
+}
+SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  p->cnt++;
+  return SQLITE_OK;
+}
+
+/*
+** The sqlite3_mutex_leave() routine exits a mutex that was
+** previously entered by the same thread.  The behavior
+** is undefined if the mutex is not currently entered or
+** is not currently allocated.  SQLite will never do either.
+*/
+SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
+  assert( p );
+  assert( sqlite3_mutex_held(p) );
+  p->cnt--;
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+}
+
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use inside assert() statements.
+*/
+SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
+  return p==0 || p->cnt>0;
+}
+SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
+  return p==0 || p->cnt==0;
+}
+#endif /* SQLITE_MUTEX_NOOP_DEBUG */
+
+/************** End of mutex.c ***********************************************/
+/************** Begin file mutex_os2.c ***************************************/
+/*
+** 2007 August 28
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains the C functions that implement mutexes for OS/2
+**
+** $Id: mutex_os2.c,v 1.1 2007/08/28 16:34:43 drh Exp $
+*/
+
+
+/*
+** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
+** See the mutex.h file for details.
+*/
+#ifdef SQLITE_MUTEX_OS2
+
+/**** FIX ME:
+***** This is currently a no-op implementation suitable for use
+***** in single-threaded applications only.  Somebody please replace
+***** this with a real mutex implementation for OS/2.
+****/
+
+/*
+** The mutex object
+*/
+struct sqlite3_mutex {
+  int id;     /* The mutex type */
+  int cnt;    /* Number of entries without a matching leave */
+};
+
+/*
+** The sqlite3_mutex_alloc() routine allocates a new
+** mutex and returns a pointer to it.  If it returns NULL
+** that means that a mutex could not be allocated.
+*/
+SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
+  static sqlite3_mutex aStatic[4];
+  sqlite3_mutex *pNew = 0;
+  switch( id ){
+    case SQLITE_MUTEX_FAST:
+    case SQLITE_MUTEX_RECURSIVE: {
+      pNew = sqlite3_malloc(sizeof(*pNew));
+      if( pNew ){
+        pNew->id = id;
+        pNew->cnt = 0;
+      }
+      break;
+    }
+    default: {
+      assert( id-2 >= 0 );
+      assert( id-2 < sizeof(aStatic)/sizeof(aStatic[0]) );
+      pNew = &aStatic[id-2];
+      pNew->id = id;
+      break;
+    }
+  }
+  return pNew;
+}
+
+/*
+** This routine deallocates a previously allocated mutex.
+*/
+SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
+  assert( p );
+  assert( p->cnt==0 );
+  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
+  sqlite3_free(p);
+}
+
+/*
+** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
+** to enter a mutex.  If another thread is already within the mutex,
+** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
+** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
+** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
+** be entered multiple times by the same thread.  In such cases the,
+** mutex must be exited an equal number of times before another thread
+** can enter.  If the same thread tries to enter any other kind of mutex
+** more than once, the behavior is undefined.
+*/
+SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  p->cnt++;
+}
+SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  p->cnt++;
+  return SQLITE_OK;
+}
+
+/*
+** The sqlite3_mutex_leave() routine exits a mutex that was
+** previously entered by the same thread.  The behavior
+** is undefined if the mutex is not currently entered or
+** is not currently allocated.  SQLite will never do either.
+*/
+SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
+  assert( p );
+  assert( sqlite3_mutex_held(p) );
+  p->cnt--;
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+}
+
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use inside assert() statements.
+*/
+SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
+  return p==0 || p->cnt>0;
+}
+SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
+  return p==0 || p->cnt==0;
+}
+#endif /* SQLITE_MUTEX_OS2 */
+
+/************** End of mutex_os2.c *******************************************/
+/************** Begin file mutex_unix.c **************************************/
+/*
+** 2007 August 28
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains the C functions that implement mutexes for pthreads
+**
+** $Id: mutex_unix.c,v 1.2 2007/08/28 22:24:35 drh Exp $
+*/
+
+/*
+** The code in this file is only used if we are compiling threadsafe
+** under unix with pthreads.
+**
+** Note that this implementation requires a version of pthreads that
+** supports recursive mutexes.
+*/
+#ifdef SQLITE_MUTEX_PTHREADS
+
+#include <pthread.h>
+
+/*
+** Each recursive mutex is an instance of the following structure.
+*/
+struct sqlite3_mutex {
+  pthread_mutex_t mutex;     /* Mutex controlling the lock */
+  int id;                    /* Mutex type */
+  int nRef;                  /* Number of entrances */
+  pthread_t owner;           /* Thread that is within this mutex */
+#ifdef SQLITE_DEBUG
+  int trace;                 /* True to trace changes */
+#endif
+};
+
+/*
+** The sqlite3_mutex_alloc() routine allocates a new
+** mutex and returns a pointer to it.  If it returns NULL
+** that means that a mutex could not be allocated.  SQLite
+** will unwind its stack and return an error.  The argument
+** to sqlite3_mutex_alloc() is one of these integer constants:
+**
+** <ul>
+** <li>  SQLITE_MUTEX_FAST
+** <li>  SQLITE_MUTEX_RECURSIVE
+** <li>  SQLITE_MUTEX_STATIC_MASTER
+** <li>  SQLITE_MUTEX_STATIC_MEM
+** <li>  SQLITE_MUTEX_STATIC_MEM2
+** <li>  SQLITE_MUTEX_STATIC_PRNG
+** <li>  SQLITE_MUTEX_STATIC_LRU
+** </ul>
+**
+** The first two constants cause sqlite3_mutex_alloc() to create
+** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
+** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
+** The mutex implementation does not need to make a distinction
+** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
+** not want to.  But SQLite will only request a recursive mutex in
+** cases where it really needs one.  If a faster non-recursive mutex
+** implementation is available on the host platform, the mutex subsystem
+** might return such a mutex in response to SQLITE_MUTEX_FAST.
+**
+** The other allowed parameters to sqlite3_mutex_alloc() each return
+** a pointer to a static preexisting mutex.  Three static mutexes are
+** used by the current version of SQLite.  Future versions of SQLite
+** may add additional static mutexes.  Static mutexes are for internal
+** use by SQLite only.  Applications that use SQLite mutexes should
+** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
+** SQLITE_MUTEX_RECURSIVE.
+**
+** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
+** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
+** returns a different mutex on every call.  But for the static
+** mutex types, the same mutex is returned on every call that has
+** the same type number.
+*/
+SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int iType){
+  static sqlite3_mutex staticMutexes[] = {
+    { PTHREAD_MUTEX_INITIALIZER, },
+    { PTHREAD_MUTEX_INITIALIZER, },
+    { PTHREAD_MUTEX_INITIALIZER, },
+    { PTHREAD_MUTEX_INITIALIZER, },
+    { PTHREAD_MUTEX_INITIALIZER, },
+  };
+  sqlite3_mutex *p;
+  switch( iType ){
+    case SQLITE_MUTEX_RECURSIVE: {
+      p = sqlite3MallocZero( sizeof(*p) );
+      if( p ){
+        pthread_mutexattr_t recursiveAttr;
+        pthread_mutexattr_init(&recursiveAttr);
+        pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
+        pthread_mutex_init(&p->mutex, &recursiveAttr);
+        pthread_mutexattr_destroy(&recursiveAttr);
+        p->id = iType;
+      }
+      break;
+    }
+    case SQLITE_MUTEX_FAST: {
+      p = sqlite3MallocZero( sizeof(*p) );
+      if( p ){
+        p->id = iType;
+        pthread_mutex_init(&p->mutex, 0);
+      }
+      break;
+    }
+    default: {
+      assert( iType-2 >= 0 );
+      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
+      p = &staticMutexes[iType-2];
+      p->id = iType;
+      break;
+    }
+  }
+  return p;
+}
+
+
+/*
+** This routine deallocates a previously
+** allocated mutex.  SQLite is careful to deallocate every
+** mutex that it allocates.
+*/
+SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
+  assert( p );
+  assert( p->nRef==0 );
+  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
+  pthread_mutex_destroy(&p->mutex);
+  sqlite3_free(p);
+}
+
+/*
+** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
+** to enter a mutex.  If another thread is already within the mutex,
+** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
+** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
+** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
+** be entered multiple times by the same thread.  In such cases the,
+** mutex must be exited an equal number of times before another thread
+** can enter.  If the same thread tries to enter any other kind of mutex
+** more than once, the behavior is undefined.
+*/
+SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  pthread_mutex_lock(&p->mutex);
+  p->owner = pthread_self();
+  p->nRef++;
+#ifdef SQLITE_DEBUG
+  if( p->trace ){
+    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
+  }
+#endif
+}
+SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
+  int rc;
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  if( pthread_mutex_trylock(&p->mutex)==0 ){
+    p->owner = pthread_self();
+    p->nRef++;
+    rc = SQLITE_OK;
+#ifdef SQLITE_DEBUG
+    if( p->trace ){
+      printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
+    }
+#endif
+  }else{
+    rc = SQLITE_BUSY;
+  }
+  return rc;
+}
+
+/*
+** The sqlite3_mutex_leave() routine exits a mutex that was
+** previously entered by the same thread.  The behavior
+** is undefined if the mutex is not currently entered or
+** is not currently allocated.  SQLite will never do either.
+*/
+SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
+  assert( p );
+  assert( sqlite3_mutex_held(p) );
+  p->nRef--;
+  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
+#ifdef SQLITE_DEBUG
+  if( p->trace ){
+    printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
+  }
+#endif
+  pthread_mutex_unlock(&p->mutex);
+}
+
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use only inside assert() statements.  On some platforms,
+** there might be race conditions that can cause these routines to
+** deliver incorrect results.  In particular, if pthread_equal() is
+** not an atomic operation, then these routines might delivery
+** incorrect results.  On most platforms, pthread_equal() is a
+** comparison of two integers and is therefore atomic.  But we are
+** told that HPUX is not such a platform.  If so, then these routines
+** will not always work correctly on HPUX.
+**
+** On those platforms where pthread_equal() is not atomic, SQLite
+** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
+** make sure no assert() statements are evaluated and hence these
+** routines are never called.
+*/
+#ifndef NDEBUG
+SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
+  return p==0 || (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
+}
+SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
+  return p==0 || p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
+}
+#endif
+#endif /* SQLITE_MUTEX_PTHREAD */
+
+/************** End of mutex_unix.c ******************************************/
+/************** Begin file mutex_w32.c ***************************************/
+/*
+** 2007 August 14
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** This file contains the C functions that implement mutexes for win32
+**
+** $Id: mutex_w32.c,v 1.4 2007/09/05 14:30:42 drh Exp $
+*/
+
+/*
+** The code in this file is only used if we are compiling multithreaded
+** on a win32 system.
+*/
+#ifdef SQLITE_MUTEX_W32
+
+/*
+** Each recursive mutex is an instance of the following structure.
+*/
+struct sqlite3_mutex {
+  CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
+  int id;                    /* Mutex type */
+  int nRef;                  /* Number of enterances */
+  DWORD owner;               /* Thread holding this mutex */
+};
+
+/*
+** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
+** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
+**
+** Here is an interesting observation:  Win95, Win98, and WinME lack
+** the LockFileEx() API.  But we can still statically link against that
+** API as long as we don't call it win running Win95/98/ME.  A call to
+** this routine is used to determine if the host is Win95/98/ME or
+** WinNT/2K/XP so that we will know whether or not we can safely call
+** the LockFileEx() API.
+*/
+#if OS_WINCE
+# define mutexIsNT()  (1)
+#else
+  static int mutexIsNT(void){
+    static int osType = 0;
+    if( osType==0 ){
+      OSVERSIONINFO sInfo;
+      sInfo.dwOSVersionInfoSize = sizeof(sInfo);
+      GetVersionEx(&sInfo);
+      osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
+    }
+    return osType==2;
+  }
+#endif /* OS_WINCE */
+
+
+/*
+** The sqlite3_mutex_alloc() routine allocates a new
+** mutex and returns a pointer to it.  If it returns NULL
+** that means that a mutex could not be allocated.  SQLite
+** will unwind its stack and return an error.  The argument
+** to sqlite3_mutex_alloc() is one of these integer constants:
+**
+** <ul>
+** <li>  SQLITE_MUTEX_FAST               0
+** <li>  SQLITE_MUTEX_RECURSIVE          1
+** <li>  SQLITE_MUTEX_STATIC_MASTER      2
+** <li>  SQLITE_MUTEX_STATIC_MEM         3
+** <li>  SQLITE_MUTEX_STATIC_PRNG        4
+** </ul>
+**
+** The first two constants cause sqlite3_mutex_alloc() to create
+** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
+** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
+** The mutex implementation does not need to make a distinction
+** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
+** not want to.  But SQLite will only request a recursive mutex in
+** cases where it really needs one.  If a faster non-recursive mutex
+** implementation is available on the host platform, the mutex subsystem
+** might return such a mutex in response to SQLITE_MUTEX_FAST.
+**
+** The other allowed parameters to sqlite3_mutex_alloc() each return
+** a pointer to a static preexisting mutex.  Three static mutexes are
+** used by the current version of SQLite.  Future versions of SQLite
+** may add additional static mutexes.  Static mutexes are for internal
+** use by SQLite only.  Applications that use SQLite mutexes should
+** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
+** SQLITE_MUTEX_RECURSIVE.
+**
+** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
+** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
+** returns a different mutex on every call.  But for the static
+** mutex types, the same mutex is returned on every call that has
+** the same type number.
+*/
+SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int iType){
+  sqlite3_mutex *p;
+
+  switch( iType ){
+    case SQLITE_MUTEX_FAST:
+    case SQLITE_MUTEX_RECURSIVE: {
+      p = sqlite3MallocZero( sizeof(*p) );
+      if( p ){
+        p->id = iType;
+        InitializeCriticalSection(&p->mutex);
+      }
+      break;
+    }
+    default: {
+      static sqlite3_mutex staticMutexes[5];
+      static int isInit = 0;
+      while( !isInit ){
+        static long lock = 0;
+        if( InterlockedIncrement(&lock)==1 ){
+          int i;
+          for(i=0; i<sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++){
+            InitializeCriticalSection(&staticMutexes[i].mutex);
+          }
+          isInit = 1;
+        }else{
+          Sleep(1);
+        }
+      }
+      assert( iType-2 >= 0 );
+      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
+      p = &staticMutexes[iType-2];
+      p->id = iType;
+      break;
+    }
+  }
+  return p;
+}
+
+
+/*
+** This routine deallocates a previously
+** allocated mutex.  SQLite is careful to deallocate every
+** mutex that it allocates.
+*/
+SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
+  assert( p );
+  assert( p->nRef==0 );
+  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
+  DeleteCriticalSection(&p->mutex);
+  sqlite3_free(p);
+}
+
+/*
+** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
+** to enter a mutex.  If another thread is already within the mutex,
+** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
+** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
+** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
+** be entered multiple times by the same thread.  In such cases the,
+** mutex must be exited an equal number of times before another thread
+** can enter.  If the same thread tries to enter any other kind of mutex
+** more than once, the behavior is undefined.
+*/
+SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  EnterCriticalSection(&p->mutex);
+  p->owner = GetCurrentThreadId();
+  p->nRef++;
+}
+SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
+  int rc;
+  assert( p );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
+    p->owner = GetCurrentThreadId();
+    p->nRef++;
+    rc = SQLITE_OK;
+  }else{
+    rc = SQLITE_BUSY;
+  }
+  return rc;
+}
+
+/*
+** The sqlite3_mutex_leave() routine exits a mutex that was
+** previously entered by the same thread.  The behavior
+** is undefined if the mutex is not currently entered or
+** is not currently allocated.  SQLite will never do either.
+*/
+SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
+  assert( p->nRef>0 );
+  assert( p->owner==GetCurrentThreadId() );
+  p->nRef--;
+  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
+  LeaveCriticalSection(&p->mutex);
+}
+
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use only inside assert() statements.
+*/
+SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
+  return p==0 || (p->nRef!=0 && p->owner==GetCurrentThreadId());
+}
+SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
+  return p==0 || p->nRef==0 || p->owner!=GetCurrentThreadId();
+}
+#endif /* SQLITE_MUTEX_W32 */
+
+/************** End of mutex_w32.c *******************************************/
+/************** Begin file malloc.c ******************************************/
+/*
+** 2001 September 15
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** Memory allocation functions used throughout sqlite.
+**
+**
+** $Id: malloc.c,v 1.13 2007/08/29 14:06:23 danielk1977 Exp $
+*/
+
+/*
+** This routine runs when the memory allocator sees that the
+** total memory allocation is about to exceed the soft heap
+** limit.
+*/
+static void softHeapLimitEnforcer(
+  void *NotUsed,
+  sqlite3_int64 inUse,
+  int allocSize
+){
+  sqlite3_release_memory(allocSize);
+}
+
+/*
+** Set the soft heap-size limit for the current thread. Passing a
+** zero or negative value indicates no limit.
+*/
+SQLITE_API void sqlite3_soft_heap_limit(int n){
+  sqlite3_uint64 iLimit;
+  int overage;
+  if( n<0 ){
+    iLimit = 0;
+  }else{
+    iLimit = n;
+  }
+  if( iLimit>0 ){
+    sqlite3_memory_alarm(softHeapLimitEnforcer, 0, iLimit);
+  }else{
+    sqlite3_memory_alarm(0, 0, 0);
+  }
+  overage = sqlite3_memory_used() - n;
+  if( overage>0 ){
+    sqlite3_release_memory(overage);
+  }
+}
+
+/*
+** Release memory held by SQLite instances created by the current thread.
+*/
+SQLITE_API int sqlite3_release_memory(int n){
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  return sqlite3PagerReleaseMemory(n);
+#else
+  return SQLITE_OK;
+#endif
+}
+
+
+/*
+** Allocate and zero memory.
+*/
+SQLITE_PRIVATE void *sqlite3MallocZero(unsigned n){
+  void *p = sqlite3_malloc(n);
   if( p ){
-    u32 *pOs = (u32 *)getOsPointer(p);
-    return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD;
-  }
-  return 0;
-}
-
-/*
-** This is the test layer's wrapper around sqlite3OsFree(). The argument is a
-** pointer to the space allocated for the application to use.
-*/
-static void OSFREE(void *pFree){
-  u32 *p;         /* Pointer to the OS-layer allocation */
-  sqlite3OsEnterMutex();
-  p = (u32 *)getOsPointer(pFree);
-  checkGuards(p);
-  unlinkAlloc(p);
-  memset(pFree, 0x55, OSSIZEOF(pFree));
-  sqlite3OsFree(p);
-  sqlite3_nFree++;
-  sqlite3OsLeaveMutex();
-}
-
-/*
-** This is the test layer's wrapper around sqlite3OsRealloc().
-*/
-static void * OSREALLOC(void *pRealloc, int n){
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  sqlite3_nMaxAlloc =
-      MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
-#endif
-  assert( !sqlite3_mallocDisallowed );
-  if( !sqlite3TestMallocFail() ){
-    u32 *p = (u32 *)getOsPointer(pRealloc);
-    checkGuards(p);
-    p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
-    applyGuards(p);
-    relinkAlloc(p);
-    return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
-  }
-  return 0;
-}
-
-static void OSMALLOC_FAILED(){
-  sqlite3_isFail = 0;
-}
-
-#else
-/* Define macros to call the sqlite3OsXXX interface directly if
-** the SQLITE_MEMDEBUG macro is not defined.
-*/
-#define OSMALLOC(x)        sqlite3OsMalloc(x)
-#define OSREALLOC(x,y)     sqlite3OsRealloc(x,y)
-#define OSFREE(x)          sqlite3OsFree(x)
-#define OSSIZEOF(x)        sqlite3OsAllocationSize(x)
-#define OSMALLOC_FAILED()
-
-#endif  /* SQLITE_MEMDEBUG */
-/*
-** End code for memory allocation system test layer.
-**--------------------------------------------------------------------------*/
-
-/*
-** This routine is called when we are about to allocate n additional bytes
-** of memory.  If the new allocation will put is over the soft allocation
-** limit, then invoke sqlite3_release_memory() to try to release some
-** memory before continuing with the allocation.
-**
-** This routine also makes sure that the thread-specific-data (TSD) has
-** be allocated.  If it has not and can not be allocated, then return
-** false.  The updateMemoryUsedCount() routine below will deallocate
-** the TSD if it ought to be.
-**
-** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
-** a no-op
-*/
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-static int enforceSoftLimit(int n){
-  ThreadData *pTsd = sqlite3ThreadData();
-  if( pTsd==0 ){
-    return 0;
-  }
-  assert( pTsd->nAlloc>=0 );
-  if( n>0 && pTsd->nSoftHeapLimit>0 ){
-    while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){}
-  }
-  return 1;
-}
-#else
-# define enforceSoftLimit(X)  1
-#endif
-
-/*
-** Update the count of total outstanding memory that is held in
-** thread-specific-data (TSD).  If after this update the TSD is
-** no longer being used, then deallocate it.
-**
-** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
-** a no-op
-*/
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-static void updateMemoryUsedCount(int n){
-  ThreadData *pTsd = sqlite3ThreadData();
-  if( pTsd ){
-    pTsd->nAlloc += n;
-    assert( pTsd->nAlloc>=0 );
-    if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
-      sqlite3ReleaseThreadData();
-    }
-  }
-}
-#else
-#define updateMemoryUsedCount(x)  /* no-op */
-#endif
-
-/*
-** Allocate and return N bytes of uninitialised memory by calling
-** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory
-** by calling sqlite3_release_memory().
-*/
-SQLITE_PRIVATE void *sqlite3MallocRaw(int n, int doMemManage){
-  void *p = 0;
-  if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){
-    while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}
-    if( !p ){
-      sqlite3FailedMalloc();
-      OSMALLOC_FAILED();
-    }else if( doMemManage ){
-      updateMemoryUsedCount(OSSIZEOF(p));
-    }
-  }
-  return p;
-}
-
-/*
-** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The
-** pointer to the new allocation is returned.  If the Realloc() call fails,
-** attempt to free memory by calling sqlite3_release_memory().
-*/
-SQLITE_PRIVATE void *sqlite3Realloc(void *p, int n){
-  if( sqlite3MallocFailed() ){
-    return 0;
-  }
-
-  if( !p ){
-    return sqlite3Malloc(n, 1);
-  }else{
-    void *np = 0;
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-    int origSize = OSSIZEOF(p);
-#endif
-    if( enforceSoftLimit(n - origSize) ){
-      while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}
-      if( !np ){
-        sqlite3FailedMalloc();
-        OSMALLOC_FAILED();
-      }else{
-        updateMemoryUsedCount(OSSIZEOF(np) - origSize);
-      }
-    }
-    return np;
-  }
-}
-
-/*
-** Free the memory pointed to by p. p must be either a NULL pointer or a
-** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().
-*/
-SQLITE_PRIVATE void sqlite3FreeX(void *p){
-  if( p ){
-    updateMemoryUsedCount(0 - OSSIZEOF(p));
-    OSFREE(p);
-  }
-}
-
-/*
-** A version of sqliteMalloc() that is always a function, not a macro.
-** Currently, this is used only to alloc to allocate the parser engine.
-*/
-SQLITE_PRIVATE void *sqlite3MallocX(int n){
-  return sqliteMalloc(n);
-}
-
-/*
-** sqlite3Malloc
-** sqlite3ReallocOrFree
-**
-** These two are implemented as wrappers around sqlite3MallocRaw(),
-** sqlite3Realloc() and sqlite3Free().
-*/
-SQLITE_PRIVATE void *sqlite3Malloc(int n, int doMemManage){
-  void *p = sqlite3MallocRaw(n, doMemManage);
+    memset(p, 0, n);
+  }
+  return p;
+}
+
+/*
+** Allocate and zero memory.  If the allocation fails, make
+** the mallocFailed flag in the connection pointer.
+*/
+SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){
+  void *p = sqlite3DbMallocRaw(db, n);
   if( p ){
     memset(p, 0, n);
   }
   return p;
 }
-SQLITE_PRIVATE void *sqlite3ReallocOrFree(void *p, int n){
+
+/*
+** Allocate and zero memory.  If the allocation fails, make
+** the mallocFailed flag in the connection pointer.
+*/
+SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){
+  void *p = 0;
+  if( !db || db->mallocFailed==0 ){
+    p = sqlite3_malloc(n);
+    if( !p && db ){
+      db->mallocFailed = 1;
+    }
+  }
+  return p;
+}
+
+/*
+** Resize the block of memory pointed to by p to n bytes. If the
+** resize fails, set the mallocFailed flag inthe connection object.
+*/
+SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
+  void *pNew = 0;
+  if( db->mallocFailed==0 ){
+    pNew = sqlite3_realloc(p, n);
+    if( !pNew ){
+      db->mallocFailed = 1;
+    }
+  }
+  return pNew;
+}
+
+/*
+** Attempt to reallocate p.  If the reallocation fails, then free p
+** and set the mallocFailed flag in the database connection.
+*/
+SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
   void *pNew;
-  pNew = sqlite3Realloc(p, n);
+  pNew = sqlite3DbRealloc(db, p, n);
   if( !pNew ){
-    sqlite3FreeX(p);
+    sqlite3_free(p);
   }
   return pNew;
 }
-
-/*
-** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those
-** rare scenarios where sqlite may allocate memory in one thread and free
-** it in another. They are exactly the same as sqlite3Malloc() and
-** sqlite3Free() except that:
-**
-**   * The allocated memory is not included in any calculations with
-**     respect to the soft-heap-limit, and
-**
-**   * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(),
-**     not sqlite3Free(). Calling sqlite3Free() on memory obtained from
-**     ThreadSafeMalloc() will cause an error somewhere down the line.
-*/
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-SQLITE_PRIVATE void *sqlite3ThreadSafeMalloc(int n){
-  (void)ENTER_MALLOC;
-  return sqlite3Malloc(n, 0);
-}
-SQLITE_PRIVATE void sqlite3ThreadSafeFree(void *p){
-  (void)ENTER_MALLOC;
-  if( p ){
-    OSFREE(p);
-  }
-}
-#endif
-
-
-/*
-** Return the number of bytes allocated at location p. p must be either
-** a NULL pointer (in which case 0 is returned) or a pointer returned by
-** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
-**
-** The number of bytes allocated does not include any overhead inserted by
-** any malloc() wrapper functions that may be called. So the value returned
-** is the number of bytes that were available to SQLite using pointer p,
-** regardless of how much memory was actually allocated.
-*/
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-SQLITE_PRIVATE int sqlite3AllocSize(void *p){
-  return OSSIZEOF(p);
-}
-#endif
 
 /*
 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
@@ -8140,19 +9877,34 @@
   char *zNew;
   int n;
   if( z==0 ) return 0;
   n = strlen(z)+1;
-  zNew = sqlite3MallocRaw(n, 1);
+  zNew = sqlite3_malloc(n);
   if( zNew ) memcpy(zNew, z, n);
   return zNew;
 }
 SQLITE_PRIVATE char *sqlite3StrNDup(const char *z, int n){
   char *zNew;
   if( z==0 ) return 0;
-  zNew = sqlite3MallocRaw(n+1, 1);
+  zNew = sqlite3_malloc(n+1);
   if( zNew ){
     memcpy(zNew, z, n);
     zNew[n] = 0;
+  }
+  return zNew;
+}
+
+SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
+  char *zNew = sqlite3StrDup(z);
+  if( z && !zNew ){
+    db->mallocFailed = 1;
+  }
+  return zNew;
+}
+SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
+  char *zNew = sqlite3StrNDup(z, n);
+  if( z && !zNew ){
+    db->mallocFailed = 1;
   }
   return zNew;
 }
 
@@ -8175,10 +9927,10 @@
   while( (z = va_arg(ap, const char*))!=0 ){
     nByte += strlen(z);
   }
   va_end(ap);
-  sqliteFree(*pz);
-  *pz = zResult = sqliteMallocRaw( nByte );
+  sqlite3_free(*pz);
+  *pz = zResult = sqlite3_malloc(nByte);
   if( zResult==0 ){
     return;
   }
   *zResult = 0;
@@ -8194,10 +9946,10 @@
 
 
 /*
 ** This function must be called before exiting any API function (i.e.
-** returning control to the user) that has called sqlite3Malloc or
-** sqlite3Realloc.
+** returning control to the user) that has called sqlite3_malloc or
+** sqlite3_realloc.
 **
 ** The returned value is normally a copy of the second argument to this
 ** function. However, if a malloc() failure has occured since the previous
 ** invocation SQLITE_NOMEM is returned instead.
@@ -8205,49 +9957,22 @@
 ** If the first argument, db, is not NULL and a malloc() error has occured,
 ** then the connection error-code (the value returned by sqlite3_errcode())
 ** is set to SQLITE_NOMEM.
 */
-int sqlite3_mallocHasFailed = 0;
 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
-  if( sqlite3MallocFailed() ){
-    sqlite3_mallocHasFailed = 0;
-    sqlite3OsLeaveMutex();
+  /* If the db handle is not NULL, then we must hold the connection handle
+  ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
+  ** is unsafe, as is the call to sqlite3Error().
+  */
+  assert( !db || sqlite3_mutex_held(db->mutex) );
+  if( db && db->mallocFailed ){
     sqlite3Error(db, SQLITE_NOMEM, 0);
+    db->mallocFailed = 0;
     rc = SQLITE_NOMEM;
   }
   return rc & (db ? db->errMask : 0xff);
 }
 
-/*
-** Set the "malloc has failed" condition to true for this thread.
-*/
-SQLITE_PRIVATE void sqlite3FailedMalloc(){
-  if( !sqlite3MallocFailed() ){
-    sqlite3OsEnterMutex();
-    assert( sqlite3_mallocHasFailed==0 );
-    sqlite3_mallocHasFailed = 1;
-  }
-}
-
-#ifdef SQLITE_MEMDEBUG
-/*
-** This function sets a flag in the thread-specific-data structure that will
-** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
-*/
-SQLITE_PRIVATE void sqlite3MallocDisallow(){
-  assert( sqlite3_mallocDisallowed>=0 );
-  sqlite3_mallocDisallowed++;
-}
-
-/*
-** This function clears the flag set in the thread-specific-data structure set
-** by sqlite3MallocDisallow().
-*/
-SQLITE_PRIVATE void sqlite3MallocAllow(){
-  assert( sqlite3_mallocDisallowed>0 );
-  sqlite3_mallocDisallowed--;
-}
-#endif
 
 /************** End of malloc.c **********************************************/
 /************** Begin file printf.c ******************************************/
 /*
@@ -8363,9 +10088,9 @@
 static const et_info fmtinfo[] = {
   {  'd', 10, 1, etRADIX,      0,  0 },
   {  's',  0, 4, etSTRING,     0,  0 },
   {  'g',  0, 1, etGENERIC,    30, 0 },
-  {  'z',  0, 6, etDYNSTRING,  0,  0 },
+  {  'z',  0, 4, etDYNSTRING,  0,  0 },
   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
   {  'c',  0, 0, etCHARX,      0,  0 },
@@ -8877,9 +10602,9 @@
         }
         needQuote = !isnull && xtype==etSQLESCAPE2;
         n += i + 1 + needQuote*2;
         if( n>etBUFSIZE ){
-          bufpt = zExtra = sqliteMalloc( n );
+          bufpt = zExtra = sqlite3_malloc( n );
           if( bufpt==0 ) return -1;
         }else{
           bufpt = buf;
         }
@@ -8951,9 +10676,9 @@
         if( nspace>0 ) (*func)(arg,spaces,nspace);
       }
     }
     if( zExtra ){
-      sqliteFree(zExtra);
+      sqlite3_free(zExtra);
     }
   }/* End for loop over the format string */
   return errorflag ? -1 : count;
 } /* End of function */
@@ -8968,8 +10693,9 @@
   int  nChar;      /* Length of the string so far */
   int  nTotal;     /* Output size if unconstrained */
   int  nAlloc;     /* Amount of space allocated in zText */
   void *(*xRealloc)(void*,int);  /* Function used to realloc memory */
+  int  iMallocFailed;            /* True if xRealloc() has failed */
 };
 
 /*
 ** This function implements the callback from vxprintf.
@@ -8978,32 +10704,41 @@
 ** the sgMprintf structure pointed to by "arg".
 */
 static void mout(void *arg, const char *zNewText, int nNewChar){
   struct sgMprintf *pM = (struct sgMprintf*)arg;
+  if( pM->iMallocFailed ) return;
   pM->nTotal += nNewChar;
-  if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
-    if( pM->xRealloc==0 ){
-      nNewChar =  pM->nAlloc - pM->nChar - 1;
-    }else{
-      int nAlloc = pM->nChar + nNewChar*2 + 1;
-      if( pM->zText==pM->zBase ){
-        pM->zText = pM->xRealloc(0, nAlloc);
-        if( pM->zText && pM->nChar ){
-          memcpy(pM->zText, pM->zBase, pM->nChar);
-        }
-      }else{
-        char *zNew;
-        zNew = pM->xRealloc(pM->zText, nAlloc);
-        if( zNew ){
-          pM->zText = zNew;
-        }else{
-          return;
-        }
-      }
-      pM->nAlloc = nAlloc;
-    }
-  }
   if( pM->zText ){
+    if( pM->nChar + nNewChar + 1 > pM->nAlloc ){
+      if( pM->xRealloc==0 ){
+        nNewChar =  pM->nAlloc - pM->nChar - 1;
+      }else{
+        int nAlloc = pM->nChar + nNewChar*2 + 1;
+        if( pM->zText==pM->zBase ){
+          pM->zText = pM->xRealloc(0, nAlloc);
+          if( pM->zText==0 ){
+            pM->nAlloc = 0;
+            pM->iMallocFailed = 1;
+            return;
+          }else if( pM->nChar ){
+            memcpy(pM->zText, pM->zBase, pM->nChar);
+          }
+        }else{
+          char *zNew;
+          zNew = pM->xRealloc(pM->zText, nAlloc);
+          if( zNew ){
+            pM->zText = zNew;
+          }else{
+            pM->iMallocFailed = 1;
+            pM->xRealloc(pM->zText, 0);
+            pM->zText = 0;
+            pM->nAlloc = 0;
+            return;
+          }
+        }
+        pM->nAlloc = nAlloc;
+      }
+    }
     if( nNewChar>0 ){
       memcpy(&pM->zText[pM->nChar], zNewText, nNewChar);
       pM->nChar += nNewChar;
     }
@@ -9015,9 +10750,9 @@
 ** This routine is a wrapper around xprintf() that invokes mout() as
 ** the consumer.
 */
 static char *base_vprintf(
-  void *(*xRealloc)(void*,int),   /* Routine to realloc memory. May be NULL */
+  void *(*xRealloc)(void*, int),  /* realloc() function. May be NULL */
   int useInternal,                /* Use internal %-conversions if true */
   char *zInitBuf,                 /* Initially write here, before mallocing */
   int nInitBuf,                   /* Size of zInitBuf[] */
   const char *zFormat,            /* format string */
@@ -9027,17 +10762,21 @@
   sM.zBase = sM.zText = zInitBuf;
   sM.nChar = sM.nTotal = 0;
   sM.nAlloc = nInitBuf;
   sM.xRealloc = xRealloc;
+  sM.iMallocFailed = 0;
   vxprintf(mout, &sM, useInternal, zFormat, ap);
-  if( xRealloc ){
+  assert(sM.iMallocFailed==0 || sM.zText==0);
+  if( xRealloc && !sM.iMallocFailed ){
     if( sM.zText==sM.zBase ){
       sM.zText = xRealloc(0, sM.nChar+1);
       if( sM.zText ){
         memcpy(sM.zText, sM.zBase, sM.nChar+1);
       }
     }else if( sM.nAlloc>sM.nChar+10 ){
-      char *zNew = xRealloc(sM.zText, sM.nChar+1);
+      char *zNew;
+      sqlite3MallocBenignFailure(1);
+      zNew = xRealloc(sM.zText, sM.nChar+1);
       if( zNew ){
         sM.zText = zNew;
       }
     }
@@ -9048,31 +10787,39 @@
 /*
 ** Realloc that is a real function, not a macro.
 */
 static void *printf_realloc(void *old, int size){
-  return sqliteRealloc(old,size);
+  return sqlite3_realloc(old, size);
 }
 
 /*
 ** Print into memory obtained from sqliteMalloc().  Use the internal
 ** %-conversion extensions.
 */
-SQLITE_PRIVATE char *sqlite3VMPrintf(const char *zFormat, va_list ap){
+SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
+  char *z;
   char zBase[SQLITE_PRINT_BUF_SIZE];
-  return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
+  z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
+  if( z==0 && db!=0 ){
+    db->mallocFailed = 1;
+  }
+  return z;
 }
 
 /*
 ** Print into memory obtained from sqliteMalloc().  Use the internal
 ** %-conversion extensions.
 */
-SQLITE_PRIVATE char *sqlite3MPrintf(const char *zFormat, ...){
+SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
   va_list ap;
   char *z;
   char zBase[SQLITE_PRINT_BUF_SIZE];
   va_start(ap, zFormat);
   z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap);
   va_end(ap);
+  if( z==0 && db!=0 ){
+    db->mallocFailed = 1;
+  }
   return z;
 }
 
 /*
@@ -9153,9 +10900,9 @@
 **
 ** Random numbers are used by some of the database backends in order
 ** to generate random integer keys for tables or random filenames.
 **
-** $Id: random.c,v 1.16 2007/01/05 14:38:56 drh Exp $
+** $Id: random.c,v 1.20 2007/08/21 13:51:23 drh Exp $
 */
 
 
 /*
@@ -9199,9 +10946,9 @@
     int i;
     char k[256];
     prng.j = 0;
     prng.i = 0;
-    sqlite3OsRandomSeed(k);
+    sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
     for(i=0; i<256; i++){
       prng.s[i] = i;
     }
     for(i=0; i<256; i++){
@@ -9228,13 +10975,17 @@
 ** Return N random bytes.
 */
 SQLITE_PRIVATE void sqlite3Randomness(int N, void *pBuf){
   unsigned char *zBuf = pBuf;
-  sqlite3OsEnterMutex();
+  static sqlite3_mutex *mutex = 0;
+  if( mutex==0 ){
+    mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG);
+  }
+  sqlite3_mutex_enter(mutex);
   while( N-- ){
     *(zBuf++) = randomByte();
   }
-  sqlite3OsLeaveMutex();
+  sqlite3_mutex_leave(mutex);
 }
 
 /************** End of random.c **********************************************/
 /************** Begin file utf.c *********************************************/
@@ -9251,9 +11002,9 @@
 *************************************************************************
 ** This file contains routines used to translate between UTF-8,
 ** UTF-16, UTF-16BE, and UTF-16LE.
 **
-** $Id: utf.c,v 1.52 2007/07/23 19:12:42 drh Exp $
+** $Id: utf.c,v 1.58 2007/09/12 17:01:45 danielk1977 Exp $
 **
 ** Notes on UTF-8:
 **
 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
@@ -9303,15 +11054,8 @@
 */
 #define keyToInt(X)   (X)
 #define intToKey(X)   (X)
 
-/*
-** The makefile scans the vdbe.c source file and creates the following
-** array of string constants which are the names of all VDBE opcodes.  This
-** array is defined in a separate source code file named opcode.c which is
-** automatically generated by the makefile.
-*/
-extern const char *const sqlite3OpcodeNames[];
 
 /*
 ** SQL is translated into a sequence of instructions to be
 ** executed by a virtual machine.  Each instruction is an instance
@@ -9408,8 +11152,9 @@
     i64 i;              /* Integer value. Or FuncDef* when flags==MEM_Agg */
     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   } u;
   double r;           /* Real value */
+  sqlite3 *db;        /* The associated database connection */
   char *z;            /* String or BLOB value */
   int n;              /* Number of characters in string value, including '\0' */
   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
@@ -9601,9 +11346,9 @@
   int rc;                 /* Value to return */
   unsigned uniqueCnt;     /* Used by OP_MakeRecord when P2!=0 */
   int errorAction;        /* Recovery action to do in case of an error */
   int inTempTrans;        /* True if temp database is transactioned */
-  int returnStack[100];   /* Return address stack for OP_Gosub & OP_Return */
+  int returnStack[25];    /* Return address stack for OP_Gosub & OP_Return */
   int returnDepth;        /* Next unused element in returnStack[] */
   int nResColumn;         /* Number of columns in one row of the result set */
   char **azResColumn;     /* Values for one row of result */
   int popStack;           /* Pop the stack this much on entry to VdbeExec() */
@@ -9616,8 +11361,10 @@
   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   u8 inVtabMethod;        /* See comments above */
   int nChange;            /* Number of db changes made since last reset */
   i64 startTime;          /* Time when query started - used for profiling */
+  int btreeMask;          /* Bitmask of db->aDb[] entries referenced */
+  BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
   int nSql;             /* Number of bytes in zSql */
   char *zSql;           /* Text of the SQL statement that generated this */
 #ifdef SQLITE_DEBUG
   FILE *trace;        /* Write an execution trace here, if not NULL */
@@ -9652,9 +11399,9 @@
 SQLITE_PRIVATE int sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
 
 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
-SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(Cursor*, int , const unsigned char*, int*);
+SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(Cursor*,int,const unsigned char*,int*);
 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(void*,int,const void*,int, const void*);
 SQLITE_PRIVATE int sqlite3VdbeIdxRowidLen(const u8*);
@@ -9683,8 +11430,10 @@
 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
+SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
+
 #ifndef NDEBUG
 SQLITE_PRIVATE   void sqlite3VdbeMemSanity(Mem*);
 SQLITE_PRIVATE   int sqlite3VdbeOpcodeNoPush(u8);
 #endif
@@ -9713,15 +11462,15 @@
 /*
 ** The following constant value is used by the SQLITE_BIGENDIAN and
 ** SQLITE_LITTLEENDIAN macros.
 */
-const int sqlite3one = 1;
+SQLITE_PRIVATE const int sqlite3one = 1;
 
 /*
 ** This lookup table is used to help decode the first byte of
 ** a multi-byte UTF8 character.
 */
-const unsigned char sqlite3UtfTrans1[] = {
+static const unsigned char sqlite3UtfTrans1[] = {
   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
@@ -9866,8 +11615,9 @@
   unsigned char *zTerm;                 /* End of input */
   unsigned char *z;                     /* Output iterator */
   unsigned int c;
 
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   assert( pMem->flags&MEM_Str );
   assert( pMem->enc!=desiredEnc );
   assert( pMem->enc!=0 );
   assert( pMem->n>=0 );
@@ -9924,16 +11674,18 @@
   /* Set zIn to point at the start of the input buffer and zTerm to point 1
   ** byte past the end.
   **
   ** Variable zOut is set to point at the output buffer. This may be space
-  ** obtained from malloc(), or Mem.zShort, if it large enough and not in
-  ** use, or the zShort array on the stack (see above).
+  ** obtained from sqlite3_malloc(), or Mem.zShort, if it large enough and
+  ** not in use, or the zShort array on the stack (see above).
   */
   zIn = (u8*)pMem->z;
   zTerm = &zIn[pMem->n];
   if( len>NBFS ){
-    zOut = sqliteMallocRaw(len);
-    if( !zOut ) return SQLITE_NOMEM;
+    zOut = sqlite3DbMallocRaw(pMem->db, len);
+    if( !zOut ){
+      return SQLITE_NOMEM;
+    }
   }else{
     zOut = zShort;
   }
   z = zOut;
@@ -10034,9 +11786,10 @@
       void (*xDel)(void*) = pMem->xDel;
       char *z = pMem->z;
       pMem->z = 0;
       pMem->xDel = 0;
-      rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, SQLITE_TRANSIENT);
+      rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom,
+          SQLITE_TRANSIENT);
       xDel(z);
     }else{
       rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom,
           SQLITE_TRANSIENT);
@@ -10069,24 +11822,55 @@
   }
   return r;
 }
 
+/* This test function is not currently used by the automated test-suite.
+** Hence it is only available in debug builds.
+*/
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
+/*
+** Translate UTF-8 to UTF-8.
+**
+** This has the effect of making sure that the string is well-formed
+** UTF-8.  Miscoded characters are removed.
+**
+** The translation is done in-place (since it is impossible for the
+** correct UTF-8 encoding to be longer than a malformed encoding).
+*/
+SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
+  unsigned char *zOut = zIn;
+  unsigned char *zStart = zIn;
+  unsigned char *zTerm;
+  u32 c;
+
+  while( zIn[0] ){
+    c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
+    if( c!=0xfffd ){
+      WRITE_UTF8(zOut, c);
+    }
+  }
+  *zOut = 0;
+  return zOut - zStart;
+}
+#endif
+
 #ifndef SQLITE_OMIT_UTF16
 /*
 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
-** Memory to hold the UTF-8 string is obtained from malloc and must be
-** freed by the calling function.
+** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
+** be freed by the calling function.
 **
 ** NULL is returned if there is an allocation error.
 */
-SQLITE_PRIVATE char *sqlite3Utf16to8(const void *z, int nByte){
+SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte){
   Mem m;
   memset(&m, 0, sizeof(m));
+  m.db = db;
   sqlite3VdbeMemSetStr(&m, z, nByte, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
-  assert( (m.flags & MEM_Term)!=0 || sqlite3MallocFailed() );
-  assert( (m.flags & MEM_Str)!=0 || sqlite3MallocFailed() );
-  return (m.flags & MEM_Dyn)!=0 ? m.z : sqliteStrDup(m.z);
+  assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
+  assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
+  return (m.flags & MEM_Dyn)!=0 ? m.z : sqlite3DbStrDup(db, m.z);
 }
 
 /*
 ** pZ is a UTF-16 encoded unicode string. If nChar is less than zero,
@@ -10120,35 +11904,8 @@
     }
   }
   return (z-(char const *)zIn)-((c==0)?2:0);
 }
-
-#if defined(SQLITE_TEST)
-/*
-** Translate UTF-8 to UTF-8.
-**
-** This has the effect of making sure that the string is well-formed
-** UTF-8.  Miscoded characters are removed.
-**
-** The translation is done in-place (since it is impossible for the
-** correct UTF-8 encoding to be longer than a malformed encoding).
-*/
-SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
-  unsigned char *zOut = zIn;
-  unsigned char *zStart = zIn;
-  unsigned char *zTerm;
-  u32 c;
-
-  while( zIn[0] ){
-    c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn);
-    if( c!=0xfffd ){
-      WRITE_UTF8(zOut, c);
-    }
-  }
-  *zOut = 0;
-  return zOut - zStart;
-}
-#endif
 
 #if defined(SQLITE_TEST)
 /*
 ** This routine is called from the TCL test function "translate_selftest".
@@ -10220,9 +11977,9 @@
 **
 ** This file contains functions for allocating memory, comparing
 ** strings, and stuff like that.
 **
-** $Id: util.c,v 1.207 2007/06/26 00:37:28 drh Exp $
+** $Id: util.c,v 1.212 2007/09/01 10:01:13 danielk1977 Exp $
 */
 
 
 /*
@@ -10246,17 +12003,17 @@
 ** should be called with err_code set to SQLITE_OK and zFormat set
 ** to NULL.
 */
 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
-  if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
+  if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
     db->errCode = err_code;
     if( zFormat ){
       char *z;
       va_list ap;
       va_start(ap, zFormat);
-      z = sqlite3VMPrintf(zFormat, ap);
+      z = sqlite3VMPrintf(db, zFormat, ap);
       va_end(ap);
-      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
+      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3_free);
     }else{
       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
     }
   }
@@ -10281,11 +12038,11 @@
 */
 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
   va_list ap;
   pParse->nErr++;
-  sqliteFree(pParse->zErrMsg);
+  sqlite3_free(pParse->zErrMsg);
   va_start(ap, zFormat);
-  pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
+  pParse->zErrMsg = sqlite3VMPrintf(pParse->db, zFormat, ap);
   va_end(ap);
   if( pParse->rc==SQLITE_OK ){
     pParse->rc = SQLITE_ERROR;
   }
@@ -10294,9 +12051,9 @@
 /*
 ** Clear the error message in pParse, if any
 */
 SQLITE_PRIVATE void sqlite3ErrorClear(Parse *pParse){
-  sqliteFree(pParse->zErrMsg);
+  sqlite3_free(pParse->zErrMsg);
   pParse->zErrMsg = 0;
   pParse->nErr = 0;
 }
 
@@ -10339,9 +12096,9 @@
 
 /* An array to map all upper-case characters into their corresponding
 ** lower-case character.
 */
-const unsigned char sqlite3UpperToLower[] = {
+SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
 #ifdef SQLITE_ASCII
       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
@@ -10623,12 +12380,18 @@
   }else if( zNum[0]=='+' ){
     zNum++;
   }
   while( zNum[0]=='0' ) zNum++;
-  for(i=0; i<10 && (c = zNum[i] - '0')>=0 && c<=9; i++){
+  for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
     v = v*10 + c;
   }
-  if( i>9 ){
+
+  /* The longest decimal representation of a 32 bit integer is 10 digits:
+  **
+  **             1234567890
+  **     2^31 -> 2147483648
+  */
+  if( i>10 ){
     return 0;
   }
   if( v-neg>2147483647 ){
     return 0;
@@ -10833,15 +12596,15 @@
 ** value.  Return a pointer to its binary value.  Space to hold the
 ** binary value has been obtained from malloc and must be freed by
 ** the calling routine.
 */
-SQLITE_PRIVATE void *sqlite3HexToBlob(const char *z){
+SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z){
   char *zBlob;
   int i;
   int n = strlen(z);
   if( n%2 ) return 0;
 
-  zBlob = (char *)sqliteMalloc(n/2);
+  zBlob = (char *)sqlite3DbMallocRaw(db, n/2);
   if( zBlob ){
     for(i=0; i<n; i+=2){
       zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
     }
@@ -10902,39 +12665,8 @@
     return 1;
   }
 }
 
-/*
-** Return a pointer to the ThreadData associated with the calling thread.
-*/
-SQLITE_PRIVATE ThreadData *sqlite3ThreadData(){
-  ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
-  if( !p ){
-    sqlite3FailedMalloc();
-  }
-  return p;
-}
-
-/*
-** Return a pointer to the ThreadData associated with the calling thread.
-** If no ThreadData has been allocated to this thread yet, return a pointer
-** to a substitute ThreadData structure that is all zeros.
-*/
-SQLITE_PRIVATE const ThreadData *sqlite3ThreadDataReadOnly(){
-  static const ThreadData zeroData = {0};  /* Initializer to silence warnings
-                                           ** from broken compilers */
-  const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
-  return pTd ? pTd : &zeroData;
-}
-
-/*
-** Check to see if the ThreadData for this thread is all zero.  If it
-** is, then deallocate it.
-*/
-SQLITE_PRIVATE void sqlite3ReleaseThreadData(){
-  sqlite3OsThreadSpecificData(-1);
-}
-
 /************** End of util.c ************************************************/
 /************** Begin file hash.c ********************************************/
 /*
 ** 2001 September 22
@@ -10949,9 +12681,9 @@
 *************************************************************************
 ** This is the implementation of generic hash-tables
 ** used in SQLite.
 **
-** $Id: hash.c,v 1.19 2007/03/31 03:59:24 drh Exp $
+** $Id: hash.c,v 1.24 2007/09/04 14:31:47 danielk1977 Exp $
 */
 
 /* Turn bulk memory into a hash table object by initializing the
 ** fields of the Hash structure.
@@ -10976,10 +12708,8 @@
   pNew->first = 0;
   pNew->count = 0;
   pNew->htsize = 0;
   pNew->ht = 0;
-  pNew->xMalloc = sqlite3MallocX;
-  pNew->xFree = sqlite3FreeX;
 }
 
 /* Remove all entries from a hash table.  Reclaim all memory.
 ** Call this routine to delete a hash table or to reset a hash table
@@ -10990,17 +12720,17 @@
 
   assert( pH!=0 );
   elem = pH->first;
   pH->first = 0;
-  if( pH->ht ) pH->xFree(pH->ht);
+  if( pH->ht ) sqlite3_free(pH->ht);
   pH->ht = 0;
   pH->htsize = 0;
   while( elem ){
     HashElem *next_elem = elem->next;
     if( pH->copyKey && elem->pKey ){
-      pH->xFree(elem->pKey);
-    }
-    pH->xFree(elem);
+      sqlite3_free(elem->pKey);
+    }
+    sqlite3_free(elem);
     elem = next_elem;
   }
   pH->count = 0;
 }
@@ -11151,19 +12881,27 @@
 
 
 /* Resize the hash table so that it cantains "new_size" buckets.
 ** "new_size" must be a power of 2.  The hash table might fail
-** to resize if sqliteMalloc() fails.
+** to resize if sqlite3_malloc() fails.
 */
 static void rehash(Hash *pH, int new_size){
   struct _ht *new_ht;            /* The new hash table */
   HashElem *elem, *next_elem;    /* For looping over existing elements */
   int (*xHash)(const void*,int); /* The hash function */
 
   assert( (new_size & (new_size-1))==0 );
-  new_ht = (struct _ht *)pH->xMalloc( new_size*sizeof(struct _ht) );
+
+  /* There is a call to sqlite3_malloc() inside rehash(). If there is
+  ** already an allocation at pH->ht, then if this malloc() fails it
+  ** is benign (since failing to resize a hash table is a performance
+  ** hit only, not a fatal error).
+  */
+  sqlite3MallocBenignFailure(pH->htsize>0);
+
+  new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) );
   if( new_ht==0 ) return;
-  if( pH->ht ) pH->xFree(pH->ht);
+  if( pH->ht ) sqlite3_free(pH->ht);
   pH->ht = new_ht;
   pH->htsize = new_size;
   xHash = hashFunction(pH->keyClass);
   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
@@ -11227,11 +12965,11 @@
   if( pEntry->count<=0 ){
     pEntry->chain = 0;
   }
   if( pH->copyKey ){
-    pH->xFree(elem->pKey);
-  }
-  pH->xFree( elem );
+    sqlite3_free(elem->pKey);
+  }
+  sqlite3_free( elem );
   pH->count--;
   if( pH->count<=0 ){
     assert( pH->first==0 );
     assert( pH->count==0 );
@@ -11239,12 +12977,13 @@
   }
 }
 
 /* Attempt to locate an element of the hash table pH with a key
-** that matches pKey,nKey.  Return the data for this element if it is
-** found, or NULL if there is no match.
-*/
-SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){
+** that matches pKey,nKey.  Return a pointer to the corresponding
+** HashElem structure for this element if it is found, or NULL
+** otherwise.
+*/
+SQLITE_PRIVATE HashElem *sqlite3HashFindElem(const Hash *pH, const void *pKey, int nKey){
   int h;             /* A hash on key */
   HashElem *elem;    /* The element that matches key */
   int (*xHash)(const void*,int);  /* The hash function */
 
@@ -11253,8 +12992,18 @@
   assert( xHash!=0 );
   h = (*xHash)(pKey,nKey);
   assert( (pH->htsize & (pH->htsize-1))==0 );
   elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1));
+  return elem;
+}
+
+/* Attempt to locate an element of the hash table pH with a key
+** that matches pKey,nKey.  Return the data for this element if it is
+** found, or NULL if there is no match.
+*/
+SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const void *pKey, int nKey){
+  HashElem *elem;    /* The element that matches key */
+  elem = sqlite3HashFindElem(pH, pKey, nKey);
   return elem ? elem->data : 0;
 }
 
 /* Insert an element into the hash table pH.  The key is pKey,nKey
@@ -11291,18 +13040,22 @@
     if( data==0 ){
       removeElementGivenHash(pH,elem,h);
     }else{
       elem->data = data;
+      if( !pH->copyKey ){
+        elem->pKey = (void *)pKey;
+      }
+      assert(nKey==elem->nKey);
     }
     return old_data;
   }
   if( data==0 ) return 0;
-  new_elem = (HashElem*)pH->xMalloc( sizeof(HashElem) );
+  new_elem = (HashElem*)sqlite3_malloc( sizeof(HashElem) );
   if( new_elem==0 ) return data;
   if( pH->copyKey && pKey!=0 ){
-    new_elem->pKey = pH->xMalloc( nKey );
+    new_elem->pKey = sqlite3_malloc( nKey );
     if( new_elem->pKey==0 ){
-      pH->xFree(new_elem);
+      sqlite3_free(new_elem);
       return data;
     }
     memcpy((void*)new_elem->pKey, pKey, nKey);
   }else{
@@ -11314,11 +13067,11 @@
     rehash(pH,8);
     if( pH->htsize==0 ){
       pH->count = 0;
       if( pH->copyKey ){
-        pH->xFree(new_elem->pKey);
-      }
-      pH->xFree(new_elem);
+        sqlite3_free(new_elem->pKey);
+      }
+      sqlite3_free(new_elem);
       return data;
     }
   }
   if( pH->count > pH->htsize ){
@@ -11336,152 +13089,155 @@
 /************** Begin file opcodes.c *****************************************/
 /* Automatically generated.  Do not edit */
 /* See the mkopcodec.awk script for details. */
 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
-const char *const sqlite3OpcodeNames[] = { "?",
- /*   1 */ "MemLoad",
- /*   2 */ "VNext",
- /*   3 */ "Column",
- /*   4 */ "SetCookie",
- /*   5 */ "IfMemPos",
- /*   6 */ "Sequence",
- /*   7 */ "MoveGt",
- /*   8 */ "RowKey",
- /*   9 */ "OpenWrite",
- /*  10 */ "If",
- /*  11 */ "Pop",
- /*  12 */ "VRowid",
- /*  13 */ "CollSeq",
- /*  14 */ "OpenRead",
- /*  15 */ "Expire",
- /*  16 */ "Not",
- /*  17 */ "AutoCommit",
- /*  18 */ "IntegrityCk",
- /*  19 */ "Sort",
- /*  20 */ "Function",
- /*  21 */ "Noop",
- /*  22 */ "Return",
- /*  23 */ "NewRowid",
- /*  24 */ "IfMemNeg",
- /*  25 */ "Variable",
- /*  26 */ "String",
- /*  27 */ "RealAffinity",
- /*  28 */ "VRename",
- /*  29 */ "ParseSchema",
- /*  30 */ "VOpen",
- /*  31 */ "Close",
- /*  32 */ "CreateIndex",
- /*  33 */ "IsUnique",
- /*  34 */ "NotFound",
- /*  35 */ "Int64",
- /*  36 */ "MustBeInt",
- /*  37 */ "Halt",
- /*  38 */ "Rowid",
- /*  39 */ "IdxLT",
- /*  40 */ "AddImm",
- /*  41 */ "Statement",
- /*  42 */ "RowData",
- /*  43 */ "MemMax",
- /*  44 */ "Push",
- /*  45 */ "NotExists",
- /*  46 */ "MemIncr",
- /*  47 */ "Gosub",
- /*  48 */ "Integer",
- /*  49 */ "MemInt",
- /*  50 */ "Prev",
- /*  51 */ "VColumn",
- /*  52 */ "CreateTable",
- /*  53 */ "Last",
- /*  54 */ "IncrVacuum",
- /*  55 */ "IdxRowid",
- /*  56 */ "MakeIdxRec",
- /*  57 */ "ResetCount",
- /*  58 */ "FifoWrite",
- /*  59 */ "Callback",
- /*  60 */ "Or",
- /*  61 */ "And",
- /*  62 */ "ContextPush",
- /*  63 */ "DropTrigger",
- /*  64 */ "DropIndex",
- /*  65 */ "IsNull",
- /*  66 */ "NotNull",
- /*  67 */ "Ne",
- /*  68 */ "Eq",
- /*  69 */ "Gt",
- /*  70 */ "Le",
- /*  71 */ "Lt",
- /*  72 */ "Ge",
- /*  73 */ "IdxGE",
- /*  74 */ "BitAnd",
- /*  75 */ "BitOr",
- /*  76 */ "ShiftLeft",
- /*  77 */ "ShiftRight",
- /*  78 */ "Add",
- /*  79 */ "Subtract",
- /*  80 */ "Multiply",
- /*  81 */ "Divide",
- /*  82 */ "Remainder",
- /*  83 */ "Concat",
- /*  84 */ "IdxDelete",
- /*  85 */ "Negative",
- /*  86 */ "Vacuum",
- /*  87 */ "BitNot",
- /*  88 */ "String8",
- /*  89 */ "MoveLe",
- /*  90 */ "IfNot",
- /*  91 */ "DropTable",
- /*  92 */ "MakeRecord",
- /*  93 */ "Delete",
- /*  94 */ "AggFinal",
- /*  95 */ "Dup",
- /*  96 */ "Goto",
- /*  97 */ "TableLock",
- /*  98 */ "FifoRead",
- /*  99 */ "Clear",
- /* 100 */ "IdxGT",
- /* 101 */ "MoveLt",
- /* 102 */ "VerifyCookie",
- /* 103 */ "AggStep",
- /* 104 */ "Pull",
- /* 105 */ "SetNumColumns",
- /* 106 */ "AbsValue",
- /* 107 */ "Transaction",
- /* 108 */ "VFilter",
- /* 109 */ "VDestroy",
- /* 110 */ "ContextPop",
- /* 111 */ "Next",
- /* 112 */ "IdxInsert",
- /* 113 */ "Distinct",
- /* 114 */ "Insert",
- /* 115 */ "Destroy",
- /* 116 */ "ReadCookie",
- /* 117 */ "ForceInt",
- /* 118 */ "LoadAnalysis",
- /* 119 */ "Explain",
- /* 120 */ "IfMemZero",
- /* 121 */ "OpenPseudo",
- /* 122 */ "OpenEphemeral",
- /* 123 */ "Null",
- /* 124 */ "Blob",
- /* 125 */ "Real",
- /* 126 */ "HexBlob",
- /* 127 */ "MemStore",
- /* 128 */ "Rewind",
- /* 129 */ "MoveGe",
- /* 130 */ "VBegin",
- /* 131 */ "VUpdate",
- /* 132 */ "VCreate",
- /* 133 */ "MemMove",
- /* 134 */ "MemNull",
- /* 135 */ "Found",
- /* 136 */ "NullRow",
- /* 137 */ "NotUsed_137",
- /* 138 */ "ToText",
- /* 139 */ "ToBlob",
- /* 140 */ "ToNumeric",
- /* 141 */ "ToInt",
- /* 142 */ "ToReal",
-};
+SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
+ static const char *const azName[] = { "?",
+     /*   1 */ "MemLoad",
+     /*   2 */ "VNext",
+     /*   3 */ "Column",
+     /*   4 */ "SetCookie",
+     /*   5 */ "IfMemPos",
+     /*   6 */ "Sequence",
+     /*   7 */ "MoveGt",
+     /*   8 */ "RowKey",
+     /*   9 */ "OpenWrite",
+     /*  10 */ "If",
+     /*  11 */ "Pop",
+     /*  12 */ "VRowid",
+     /*  13 */ "CollSeq",
+     /*  14 */ "OpenRead",
+     /*  15 */ "Expire",
+     /*  16 */ "Not",
+     /*  17 */ "AutoCommit",
+     /*  18 */ "IntegrityCk",
+     /*  19 */ "Sort",
+     /*  20 */ "Function",
+     /*  21 */ "Noop",
+     /*  22 */ "Return",
+     /*  23 */ "NewRowid",
+     /*  24 */ "IfMemNeg",
+     /*  25 */ "Variable",
+     /*  26 */ "String",
+     /*  27 */ "RealAffinity",
+     /*  28 */ "VRename",
+     /*  29 */ "ParseSchema",
+     /*  30 */ "VOpen",
+     /*  31 */ "Close",
+     /*  32 */ "CreateIndex",
+     /*  33 */ "IsUnique",
+     /*  34 */ "NotFound",
+     /*  35 */ "Int64",
+     /*  36 */ "MustBeInt",
+     /*  37 */ "Halt",
+     /*  38 */ "Rowid",
+     /*  39 */ "IdxLT",
+     /*  40 */ "AddImm",
+     /*  41 */ "Statement",
+     /*  42 */ "RowData",
+     /*  43 */ "MemMax",
+     /*  44 */ "Push",
+     /*  45 */ "NotExists",
+     /*  46 */ "MemIncr",
+     /*  47 */ "Gosub",
+     /*  48 */ "Integer",
+     /*  49 */ "MemInt",
+     /*  50 */ "Prev",
+     /*  51 */ "VColumn",
+     /*  52 */ "CreateTable",
+     /*  53 */ "Last",
+     /*  54 */ "IncrVacuum",
+     /*  55 */ "IdxRowid",
+     /*  56 */ "MakeIdxRec",
+     /*  57 */ "ResetCount",
+     /*  58 */ "FifoWrite",
+     /*  59 */ "Callback",
+     /*  60 */ "Or",
+     /*  61 */ "And",
+     /*  62 */ "ContextPush",
+     /*  63 */ "DropTrigger",
+     /*  64 */ "DropIndex",
+     /*  65 */ "IsNull",
+     /*  66 */ "NotNull",
+     /*  67 */ "Ne",
+     /*  68 */ "Eq",
+     /*  69 */ "Gt",
+     /*  70 */ "Le",
+     /*  71 */ "Lt",
+     /*  72 */ "Ge",
+     /*  73 */ "IdxGE",
+     /*  74 */ "BitAnd",
+     /*  75 */ "BitOr",
+     /*  76 */ "ShiftLeft",
+     /*  77 */ "ShiftRight",
+     /*  78 */ "Add",
+     /*  79 */ "Subtract",
+     /*  80 */ "Multiply",
+     /*  81 */ "Divide",
+     /*  82 */ "Remainder",
+     /*  83 */ "Concat",
+     /*  84 */ "IdxDelete",
+     /*  85 */ "Negative",
+     /*  86 */ "Vacuum",
+     /*  87 */ "BitNot",
+     /*  88 */ "String8",
+     /*  89 */ "MoveLe",
+     /*  90 */ "IfNot",
+     /*  91 */ "DropTable",
+     /*  92 */ "MakeRecord",
+     /*  93 */ "Delete",
+     /*  94 */ "AggFinal",
+     /*  95 */ "Dup",
+     /*  96 */ "Goto",
+     /*  97 */ "TableLock",
+     /*  98 */ "FifoRead",
+     /*  99 */ "Clear",
+     /* 100 */ "IdxGT",
+     /* 101 */ "MoveLt",
+     /* 102 */ "VerifyCookie",
+     /* 103 */ "AggStep",
+     /* 104 */ "Pull",
+     /* 105 */ "SetNumColumns",
+     /* 106 */ "AbsValue",
+     /* 107 */ "Transaction",
+     /* 108 */ "VFilter",
+     /* 109 */ "VDestroy",
+     /* 110 */ "ContextPop",
+     /* 111 */ "Next",
+     /* 112 */ "IdxInsert",
+     /* 113 */ "Distinct",
+     /* 114 */ "Insert",
+     /* 115 */ "Destroy",
+     /* 116 */ "ReadCookie",
+     /* 117 */ "ForceInt",
+     /* 118 */ "LoadAnalysis",
+     /* 119 */ "Explain",
+     /* 120 */ "IfMemZero",
+     /* 121 */ "OpenPseudo",
+     /* 122 */ "OpenEphemeral",
+     /* 123 */ "Null",
+     /* 124 */ "Blob",
+     /* 125 */ "Real",
+     /* 126 */ "HexBlob",
+     /* 127 */ "MemStore",
+     /* 128 */ "Rewind",
+     /* 129 */ "MoveGe",
+     /* 130 */ "VBegin",
+     /* 131 */ "VUpdate",
+     /* 132 */ "VCreate",
+     /* 133 */ "MemMove",
+     /* 134 */ "MemNull",
+     /* 135 */ "Found",
+     /* 136 */ "NullRow",
+     /* 137 */ "NotUsed_137",
+     /* 138 */ "ToText",
+     /* 139 */ "ToBlob",
+     /* 140 */ "ToNumeric",
+     /* 141 */ "ToInt",
+     /* 142 */ "ToReal",
+  };
+  return azName[i];
+}
 #endif
 
 /************** End of opcodes.c *********************************************/
 /************** Begin file os_os2.c ******************************************/
@@ -11549,13 +13305,13 @@
  * When testing, this global variable stores the location of the
  * pending-byte in the database file.
  */
 #ifdef SQLITE_TEST
-unsigned int sqlite3_pending_byte = 0x40000000;
-#endif
-
-int sqlite3_os_trace = 0;
-#ifdef SQLITE_DEBUG
+SQLITE_API unsigned int sqlite3_pending_byte = 0x40000000;
+#endif
+
+#ifdef SQLITE_DEBUG
+SQLITE_API int sqlite3_os_trace = 0;
 #define OSTRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
 #define OSTRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
 #define OSTRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
 #define OSTRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
@@ -11602,13 +13358,13 @@
 ** of code will give us the ability to simulate a disk I/O error.  This
 ** is used for testing the I/O recovery logic.
 */
 #ifdef SQLITE_TEST
-int sqlite3_io_error_hit = 0;
-int sqlite3_io_error_pending = 0;
-int sqlite3_io_error_persist = 0;
-int sqlite3_diskfull_pending = 0;
-int sqlite3_diskfull = 0;
+SQLITE_API int sqlite3_io_error_hit = 0;
+SQLITE_API int sqlite3_io_error_pending = 0;
+SQLITE_API int sqlite3_io_error_persist = 0;
+SQLITE_API int sqlite3_diskfull_pending = 0;
+SQLITE_API int sqlite3_diskfull = 0;
 #define SimulateIOError(CODE)  \
   if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
      if( sqlite3_io_error_pending-- == 1 \
          || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
@@ -11636,83 +13392,12 @@
 /*
 ** When testing, keep a count of the number of open files.
 */
 #ifdef SQLITE_TEST
-int sqlite3_open_file_count = 0;
+SQLITE_API int sqlite3_open_file_count = 0;
 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
 #else
 #define OpenCounter(X)
-#endif
-
-/*
-** sqlite3GenericMalloc
-** sqlite3GenericRealloc
-** sqlite3GenericOsFree
-** sqlite3GenericAllocationSize
-**
-** Implementation of the os level dynamic memory allocation interface in terms
-** of the standard malloc(), realloc() and free() found in many operating
-** systems. No rocket science here.
-**
-** There are two versions of these four functions here. The version
-** implemented here is only used if memory-management or memory-debugging is
-** enabled. This version allocates an extra 8-bytes at the beginning of each
-** block and stores the size of the allocation there.
-**
-** If neither memory-management or debugging is enabled, the second
-** set of implementations is used instead.
-*/
-#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || defined (SQLITE_MEMDEBUG)
-SQLITE_PRIVATE void *sqlite3GenericMalloc(int n){
-  char *p = (char *)malloc(n+8);
-  assert(n>0);
-  assert(sizeof(int)<=8);
-  if( p ){
-    *(int *)p = n;
-    p += 8;
-  }
-  return (void *)p;
-}
-SQLITE_PRIVATE void *sqlite3GenericRealloc(void *p, int n){
-  char *p2 = ((char *)p - 8);
-  assert(n>0);
-  p2 = (char*)realloc(p2, n+8);
-  if( p2 ){
-    *(int *)p2 = n;
-    p2 += 8;
-  }
-  return (void *)p2;
-}
-SQLITE_PRIVATE void sqlite3GenericFree(void *p){
-  assert(p);
-  free((void *)((char *)p - 8));
-}
-SQLITE_PRIVATE int sqlite3GenericAllocationSize(void *p){
-  return p ? *(int *)((char *)p - 8) : 0;
-}
-#else
-SQLITE_PRIVATE void *sqlite3GenericMalloc(int n){
-  char *p = (char *)malloc(n);
-  return (void *)p;
-}
-SQLITE_PRIVATE void *sqlite3GenericRealloc(void *p, int n){
-  assert(n>0);
-  p = realloc(p, n);
-  return p;
-}
-SQLITE_PRIVATE void sqlite3GenericFree(void *p){
-  assert(p);
-  free(p);
-}
-/* Never actually used, but needed for the linker */
-SQLITE_PRIVATE int sqlite3GenericAllocationSize(void *p){ return 0; }
-#endif
-
-/*
-** The default size of a disk sector
-*/
-#ifndef PAGER_SECTOR_SIZE
-# define PAGER_SECTOR_SIZE 512
 #endif
 
 /************** End of os_common.h *******************************************/
 /************** Continuing where we left off in os_os2.c *********************/
@@ -11925,8 +13610,15 @@
            sprintf( (char*)zTempPath, "%c:", (char)( 'A' + ulDriveNum - 1 ) );
       }
     }
   }
+  /* strip off a trailing slashes or backslashes, otherwise we would get *
+   * multiple (back)slashes which causes DosOpen() to fail               */
+  j = strlen(zTempPath);
+  while( j > 0 && zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' ){
+      j--;
+  }
+  zTempPath[j] = '\0';
   for(;;){
       sprintf( zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath );
       j = strlen( zBuf );
       sqlite3Randomness( 15, &zBuf[j] );
@@ -12602,9 +14294,9 @@
 ** The following variable, if set to a non-zero value, becomes the result
 ** returned from sqlite3OsCurrentTime().  This is used for testing.
 */
 #ifdef SQLITE_TEST
-int sqlite3_current_time = 0;
+SQLITE_API int sqlite3_current_time = 0;
 #endif
 
 /*
 ** Find the current time (in Universal Coordinated Time).  Write the
@@ -12650,9 +14342,9 @@
 ** Use this to verify that we are not leaking thread-specific-data.
 ** Ticket #1601
 */
 #ifdef SQLITE_TEST
-int sqlite3_tsd_count = 0;
+SQLITE_API int sqlite3_tsd_count = 0;
 # define TSD_COUNTER_INCR InterlockedIncrement( &sqlite3_tsd_count )
 # define TSD_COUNTER_DECR InterlockedDecrement( &sqlite3_tsd_count )
 #else
 # define TSD_COUNTER_INCR  /* no-op */
@@ -12771,13 +14463,9 @@
 /*
 ** If we are to be thread-safe, include the pthreads header and define
 ** the SQLITE_UNIX_THREADS macro.
 */
-#ifndef THREADSAFE
-# define THREADSAFE 1
-#endif
-#if THREADSAFE
-# include <pthread.h>
+#if SQLITE_THREADSAFE
 # define SQLITE_UNIX_THREADS 1
 #endif
 
 /*
@@ -12786,49 +14474,39 @@
 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
 #endif
 
-
-
-/*
-** The unixFile structure is subclass of OsFile specific for the unix
+/*
+** Maximum supported path-length.
+*/
+#define MAX_PATHNAME 512
+
+
+/*
+** The unixFile structure is subclass of sqlite3_file specific for the unix
 ** protability layer.
 */
 typedef struct unixFile unixFile;
 struct unixFile {
-  IoMethod const *pMethod;  /* Always the first entry */
+  sqlite3_io_methods const *pMethod;  /* Always the first entry */
+#ifdef SQLITE_TEST
+  /* In test mode, increase the size of this structure a bit so that
+  ** it is larger than the struct CrashFile defined in test6.c.
+  */
+  char aPadding[32];
+#endif
   struct openCnt *pOpen;    /* Info about all open fd's on this inode */
   struct lockInfo *pLock;   /* Info about locks on this inode */
 #ifdef SQLITE_ENABLE_LOCKING_STYLE
   void *lockingContext;     /* Locking style specific state */
 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
   int h;                    /* The file descriptor */
   unsigned char locktype;   /* The type of lock held on this fd */
-  unsigned char isOpen;     /* True if needs to be closed */
-  unsigned char fullSync;   /* Use F_FULLSYNC if available */
   int dirfd;                /* File descriptor for the directory */
-  i64 offset;               /* Seek offset */
-#ifdef SQLITE_UNIX_THREADS
-  pthread_t tid;            /* The thread that "owns" this OsFile */
-#endif
-};
-
-/*
-** Provide the ability to override some OS-layer functions during
-** testing.  This is used to simulate OS crashes to verify that
-** commits are atomic even in the event of an OS crash.
-*/
-#ifdef SQLITE_CRASH_TEST
-  extern int sqlite3CrashTestEnable;
-  extern int sqlite3CrashOpenReadWrite(const char*, OsFile**, int*);
-  extern int sqlite3CrashOpenExclusive(const char*, OsFile**, int);
-  extern int sqlite3CrashOpenReadOnly(const char*, OsFile**, int);
-# define CRASH_TEST_OVERRIDE(X,A,B,C) \
-    if(sqlite3CrashTestEnable){ return X(A,B,C); }
-#else
-# define CRASH_TEST_OVERRIDE(X,A,B,C)  /* no-op */
-#endif
-
+#if SQLITE_THREADSAFE
+  pthread_t tid;            /* The thread that "owns" this unixFile */
+#endif
+};
 
 /*
 ** Include code that is common to all os_*.c files
 */
@@ -12868,13 +14546,13 @@
  * When testing, this global variable stores the location of the
  * pending-byte in the database file.
  */
 #ifdef SQLITE_TEST
-unsigned int sqlite3_pending_byte = 0x40000000;
-#endif
-
-int sqlite3_os_trace = 0;
-#ifdef SQLITE_DEBUG
+SQLITE_API unsigned int sqlite3_pending_byte = 0x40000000;
+#endif
+
+#ifdef SQLITE_DEBUG
+SQLITE_API int sqlite3_os_trace = 0;
 #define OSTRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
 #define OSTRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
 #define OSTRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
 #define OSTRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
@@ -12921,13 +14599,13 @@
 ** of code will give us the ability to simulate a disk I/O error.  This
 ** is used for testing the I/O recovery logic.
 */
 #ifdef SQLITE_TEST
-int sqlite3_io_error_hit = 0;
-int sqlite3_io_error_pending = 0;
-int sqlite3_io_error_persist = 0;
-int sqlite3_diskfull_pending = 0;
-int sqlite3_diskfull = 0;
+SQLITE_API int sqlite3_io_error_hit = 0;
+SQLITE_API int sqlite3_io_error_pending = 0;
+SQLITE_API int sqlite3_io_error_persist = 0;
+SQLITE_API int sqlite3_diskfull_pending = 0;
+SQLITE_API int sqlite3_diskfull = 0;
 #define SimulateIOError(CODE)  \
   if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
      if( sqlite3_io_error_pending-- == 1 \
          || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
@@ -12955,95 +14633,16 @@
 /*
 ** When testing, keep a count of the number of open files.
 */
 #ifdef SQLITE_TEST
-int sqlite3_open_file_count = 0;
+SQLITE_API int sqlite3_open_file_count = 0;
 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
 #else
 #define OpenCounter(X)
 #endif
 
-/*
-** sqlite3GenericMalloc
-** sqlite3GenericRealloc
-** sqlite3GenericOsFree
-** sqlite3GenericAllocationSize
-**
-** Implementation of the os level dynamic memory allocation interface in terms
-** of the standard malloc(), realloc() and free() found in many operating
-** systems. No rocket science here.
-**
-** There are two versions of these four functions here. The version
-** implemented here is only used if memory-management or memory-debugging is
-** enabled. This version allocates an extra 8-bytes at the beginning of each
-** block and stores the size of the allocation there.
-**
-** If neither memory-management or debugging is enabled, the second
-** set of implementations is used instead.
-*/
-#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || defined (SQLITE_MEMDEBUG)
-SQLITE_PRIVATE void *sqlite3GenericMalloc(int n){
-  char *p = (char *)malloc(n+8);
-  assert(n>0);
-  assert(sizeof(int)<=8);
-  if( p ){
-    *(int *)p = n;
-    p += 8;
-  }
-  return (void *)p;
-}
-SQLITE_PRIVATE void *sqlite3GenericRealloc(void *p, int n){
-  char *p2 = ((char *)p - 8);
-  assert(n>0);
-  p2 = (char*)realloc(p2, n+8);
-  if( p2 ){
-    *(int *)p2 = n;
-    p2 += 8;
-  }
-  return (void *)p2;
-}
-SQLITE_PRIVATE void sqlite3GenericFree(void *p){
-  assert(p);
-  free((void *)((char *)p - 8));
-}
-SQLITE_PRIVATE int sqlite3GenericAllocationSize(void *p){
-  return p ? *(int *)((char *)p - 8) : 0;
-}
-#else
-SQLITE_PRIVATE void *sqlite3GenericMalloc(int n){
-  char *p = (char *)malloc(n);
-  return (void *)p;
-}
-SQLITE_PRIVATE void *sqlite3GenericRealloc(void *p, int n){
-  assert(n>0);
-  p = realloc(p, n);
-  return p;
-}
-SQLITE_PRIVATE void sqlite3GenericFree(void *p){
-  assert(p);
-  free(p);
-}
-/* Never actually used, but needed for the linker */
-SQLITE_PRIVATE int sqlite3GenericAllocationSize(void *p){ return 0; }
-#endif
-
-/*
-** The default size of a disk sector
-*/
-#ifndef PAGER_SECTOR_SIZE
-# define PAGER_SECTOR_SIZE 512
-#endif
-
 /************** End of os_common.h *******************************************/
 /************** Continuing where we left off in os_unix.c ********************/
-
-/*
-** Do not include any of the File I/O interface procedures if the
-** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
-** will be in-memory only)
-*/
-#ifndef SQLITE_OMIT_DISKIO
-
 
 /*
 ** Define various macros that are missing from some systems.
 */
@@ -13074,31 +14673,31 @@
 /*
 ** The threadid macro resolves to the thread-id or to 0.  Used for
 ** testing and debugging only.
 */
-#ifdef SQLITE_UNIX_THREADS
+#if SQLITE_THREADSAFE
 #define threadid pthread_self()
 #else
 #define threadid 0
 #endif
 
 /*
-** Set or check the OsFile.tid field.  This field is set when an OsFile
-** is first opened.  All subsequent uses of the OsFile verify that the
-** same thread is operating on the OsFile.  Some operating systems do
+** Set or check the unixFile.tid field.  This field is set when an unixFile
+** is first opened.  All subsequent uses of the unixFile verify that the
+** same thread is operating on the unixFile.  Some operating systems do
 ** not allow locks to be overridden by other threads and that restriction
 ** means that sqlite3* database handles cannot be moved from one thread
 ** to another.  This logic makes sure a user does not try to do that
 ** by mistake.
 **
-** Version 3.3.1 (2006-01-15):  OsFiles can be moved from one thread to
+** Version 3.3.1 (2006-01-15):  unixFile can be moved from one thread to
 ** another as long as we are running on a system that supports threads
 ** overriding each others locks (which now the most common behavior)
-** or if no locks are held.  But the OsFile.pLock field needs to be
+** or if no locks are held.  But the unixFile.pLock field needs to be
 ** recomputed because its key includes the thread-id.  See the
 ** transferOwnership() function below for additional information
 */
-#if defined(SQLITE_UNIX_THREADS)
+#if SQLITE_THREADSAFE
 # define SET_THREADID(X)   (X)->tid = pthread_self()
 # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
                             !pthread_equal((X)->tid, pthread_self()))
 #else
@@ -13140,13 +14739,13 @@
 ** created or removed, we have to look at our own internal record of the
 ** locks to see if another thread has previously set a lock on that same
 ** inode.
 **
-** The OsFile structure for POSIX is no longer just an integer file
+** The sqlite3_file structure for POSIX is no longer just an integer file
 ** descriptor.  It is now a structure that holds the integer file
 ** descriptor and a pointer to a structure that describes the internal
 ** locks on the corresponding inode.  There is one locking structure
-** per inode, so if the same inode is opened twice, both OsFile structures
+** per inode, so if the same inode is opened twice, both unixFile structures
 ** point to the same locking structure.  The locking structure keeps
 ** a reference count (so we will know when to delete it) and a "cnt"
 ** field that tells us its internal lock status.  cnt==0 means the
 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
@@ -13163,13 +14762,13 @@
 ** an abomination.)
 **
 ** If you close a file descriptor that points to a file that has locks,
 ** all locks on that file that are owned by the current process are
-** released.  To work around this problem, each OsFile structure contains
+** released.  To work around this problem, each unixFile structure contains
 ** a pointer to an openCnt structure.  There is one openCnt structure
-** per open inode, which means that multiple OsFiles can point to a single
-** openCnt.  When an attempt is made to close an OsFile, if there are
-** other OsFiles open on the same inode that are holding locks, the call
+** per open inode, which means that multiple unixFile can point to a single
+** openCnt.  When an attempt is made to close an unixFile, if there are
+** other unixFile open on the same inode that are holding locks, the call
 ** to close() the file descriptor is deferred until all of the locks clear.
 ** The openCnt structure keeps a list of file descriptors that need to
 ** be closed and that list is walked (and cleared) when the last lock
 ** clears.
@@ -13217,9 +14816,9 @@
 */
 struct lockKey {
   dev_t dev;       /* Device number */
   ino_t ino;       /* Inode number */
-#ifdef SQLITE_UNIX_THREADS
+#if SQLITE_THREADSAFE
   pthread_t tid;   /* Thread ID or zero if threads can override each other */
 #endif
 };
 
@@ -13227,11 +14826,11 @@
 ** An instance of the following structure is allocated for each open
 ** inode on each thread with a different process ID.  (Threads have
 ** different process IDs on linux, but not on most other unixes.)
 **
-** A single inode can have multiple file descriptors, so each OsFile
+** A single inode can have multiple file descriptors, so each unixFile
 ** structure contains a pointer to an instance of this object and this
-** object keeps a count of the number of OsFiles pointing to it.
+** object keeps a count of the number of unixFile pointing to it.
 */
 struct lockInfo {
   struct lockKey key;  /* The lookup key */
   int cnt;             /* Number of SHARED locks held */
@@ -13268,12 +14867,10 @@
 ** These hash tables map inodes and file descriptors (really, lockKey and
 ** openKey structures) into lockInfo and openCnt structures.  Access to
 ** these hash tables must be protected by a mutex.
 */
-static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0,
-    sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
-static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0,
-    sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
+static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
+static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
 
 #ifdef SQLITE_ENABLE_LOCKING_STYLE
 /*
 ** The locking styles are associated with the different file locking
@@ -13290,18 +14887,28 @@
 ** UNSUPPORTED means that no locking will be attempted, this is only used for
 **   file systems that are known to be unsupported
 */
 typedef enum {
-	posixLockingStyle = 0,       /* standard posix-advisory locks */
-	afpLockingStyle,             /* use afp locks */
-	flockLockingStyle,           /* use flock() */
-	dotlockLockingStyle,         /* use <file>.lock files */
-	noLockingStyle,              /* useful for read-only file system */
-	unsupportedLockingStyle      /* indicates unsupported file system */
+        posixLockingStyle = 0,       /* standard posix-advisory locks */
+        afpLockingStyle,             /* use afp locks */
+        flockLockingStyle,           /* use flock() */
+        dotlockLockingStyle,         /* use <file>.lock files */
+        noLockingStyle,              /* useful for read-only file system */
+        unsupportedLockingStyle      /* indicates unsupported file system */
 } sqlite3LockingStyle;
 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
 
-#ifdef SQLITE_UNIX_THREADS
+/*
+** Helper functions to obtain and relinquish the global mutex.
+*/
+static void enterMutex(){
+  sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+}
+static void leaveMutex(){
+  sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+}
+
+#if SQLITE_THREADSAFE
 /*
 ** This variable records whether or not threads can override each others
 ** locks.
 **
@@ -13436,36 +15043,34 @@
   pthread_join(t[1], 0);
   close(fd);
   threadsOverrideEachOthersLocks =  d[0].result==0 && d[1].result==0;
 }
-#endif /* SQLITE_UNIX_THREADS */
+#endif /* SQLITE_THREADSAFE */
 
 /*
 ** Release a lockInfo structure previously allocated by findLockInfo().
 */
 static void releaseLockInfo(struct lockInfo *pLock){
-  assert( sqlite3OsInMutex(1) );
   if (pLock == NULL)
     return;
   pLock->nRef--;
   if( pLock->nRef==0 ){
     sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
-    sqlite3ThreadSafeFree(pLock);
+    sqlite3_free(pLock);
   }
 }
 
 /*
 ** Release a openCnt structure previously allocated by findLockInfo().
 */
 static void releaseOpenCnt(struct openCnt *pOpen){
-  assert( sqlite3OsInMutex(1) );
   if (pOpen == NULL)
     return;
   pOpen->nRef--;
   if( pOpen->nRef==0 ){
     sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
     free(pOpen->aPending);
-    sqlite3ThreadSafeFree(pOpen);
+    sqlite3_free(pOpen);
   }
 }
 
 #ifdef SQLITE_ENABLE_LOCKING_STYLE
@@ -13472,10 +15077,12 @@
 /*
 ** Tests a byte-range locking query to see if byte range locks are
 ** supported, if not we fall back to dotlockLockingStyle.
 */
-static sqlite3LockingStyle sqlite3TestLockingStyle(const char *filePath,
-  int fd) {
+static sqlite3LockingStyle sqlite3TestLockingStyle(
+  const char *filePath,
+  int fd
+){
   /* test byte-range lock using fcntl */
   struct flock lockInfo;
 
   lockInfo.l_len = 1;
@@ -13482,9 +15089,9 @@
   lockInfo.l_start = 0;
   lockInfo.l_whence = SEEK_SET;
   lockInfo.l_type = F_RDLCK;
 
-  if (fcntl(fd, F_GETLK, &lockInfo) != -1) {
+  if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
     return posixLockingStyle;
   }
 
   /* testing for flock can give false positives.  So if if the above test
@@ -13499,10 +15106,12 @@
 ** appropriate locking style based on it's value.  These values and
 ** assignments are based on Darwin/OSX behavior and have not been tested on
 ** other systems.
 */
-static sqlite3LockingStyle sqlite3DetectLockingStyle(const char *filePath,
-  int fd) {
+static sqlite3LockingStyle sqlite3DetectLockingStyle(
+  const char *filePath,
+  int fd
+){
 
 #ifdef SQLITE_FIXED_LOCKING_STYLE
   return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
 #else
@@ -13515,9 +15124,9 @@
     return noLockingStyle;
 
   if( (!strcmp(fsInfo.f_fstypename, "hfs")) ||
     (!strcmp(fsInfo.f_fstypename, "ufs")) )
-		return posixLockingStyle;
+                return posixLockingStyle;
 
   if(!strcmp(fsInfo.f_fstypename, "afpfs"))
     return afpLockingStyle;
 
@@ -13559,13 +15168,12 @@
   struct openCnt *pOpen;
   rc = fstat(fd, &statbuf);
   if( rc!=0 ) return 1;
 
-  assert( sqlite3OsInMutex(1) );
   memset(&key1, 0, sizeof(key1));
   key1.dev = statbuf.st_dev;
   key1.ino = statbuf.st_ino;
-#ifdef SQLITE_UNIX_THREADS
+#if SQLITE_THREADSAFE
   if( threadsOverrideEachOthersLocks<0 ){
     testThreadLockingBehavior(fd);
   }
   key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
@@ -13575,9 +15183,9 @@
   key2.ino = statbuf.st_ino;
   pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
   if( pLock==0 ){
     struct lockInfo *pOld;
-    pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) );
+    pLock = sqlite3_malloc( sizeof(*pLock) );
     if( pLock==0 ){
       rc = 1;
       goto exit_findlockinfo;
     }
@@ -13587,9 +15195,9 @@
     pLock->locktype = 0;
     pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
     if( pOld!=0 ){
       assert( pOld==pLock );
-      sqlite3ThreadSafeFree(pLock);
+      sqlite3_free(pLock);
       rc = 1;
       goto exit_findlockinfo;
     }
   }else{
@@ -13599,9 +15207,9 @@
   if( ppOpen!=0 ){
     pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
     if( pOpen==0 ){
       struct openCnt *pOld;
-      pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) );
+      pOpen = sqlite3_malloc( sizeof(*pOpen) );
       if( pOpen==0 ){
         releaseLockInfo(pLock);
         rc = 1;
         goto exit_findlockinfo;
@@ -13613,9 +15221,9 @@
       pOpen->aPending = 0;
       pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
       if( pOld!=0 ){
         assert( pOld==pOpen );
-        sqlite3ThreadSafeFree(pOpen);
+        sqlite3_free(pOpen);
         releaseLockInfo(pLock);
         rc = 1;
         goto exit_findlockinfo;
       }
@@ -13659,9 +15267,9 @@
 ** Ownership transfer is only allowed if the unixFile is currently unlocked.
 ** If the unixFile is locked and an ownership is wrong, then return
 ** SQLITE_MISUSE.  SQLITE_OK is returned if everything works.
 */
-#ifdef SQLITE_UNIX_THREADS
+#if SQLITE_THREADSAFE
 static int transferOwnership(unixFile *pFile){
   int rc;
   pthread_t hSelf;
   if( threadsOverrideEachOthersLocks ){
@@ -13697,242 +15305,31 @@
 # define transferOwnership(X) SQLITE_OK
 #endif
 
 /*
-** Delete the named file
-*/
-SQLITE_PRIVATE int sqlite3UnixDelete(const char *zFilename){
-  SimulateIOError(return SQLITE_IOERR_DELETE);
-  unlink(zFilename);
-  return SQLITE_OK;
-}
-
-/*
-** Return TRUE if the named file exists.
-*/
-SQLITE_PRIVATE int sqlite3UnixFileExists(const char *zFilename){
-  return access(zFilename, 0)==0;
-}
-
-/* Forward declaration */
-static int allocateUnixFile(
-  int h,                    /* File descriptor of the open file */
-  OsFile **pId,             /* Write the real file descriptor here */
-  const char *zFilename,    /* Name of the file being opened */
-  int delFlag               /* If true, make sure the file deletes on close */
-);
-
-/*
-** Attempt to open a file for both reading and writing.  If that
-** fails, try opening it read-only.  If the file does not exist,
-** try to create it.
-**
-** On success, a handle for the open file is written to *id
-** and *pReadonly is set to 0 if the file was opened for reading and
-** writing or 1 if the file was opened read-only.  The function returns
-** SQLITE_OK.
-**
-** On failure, the function returns SQLITE_CANTOPEN and leaves
-** *id and *pReadonly unchanged.
-*/
-SQLITE_PRIVATE int sqlite3UnixOpenReadWrite(
-  const char *zFilename,
-  OsFile **pId,
-  int *pReadonly
-){
-  int h;
-
-  CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadWrite, zFilename, pId, pReadonly);
-  assert( 0==*pId );
-  h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY,
-                        SQLITE_DEFAULT_FILE_PERMISSIONS);
-  if( h<0 ){
-#ifdef EISDIR
-    if( errno==EISDIR ){
-      return SQLITE_CANTOPEN;
-    }
-#endif
-    h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
-    if( h<0 ){
-      return SQLITE_CANTOPEN;
-    }
-    *pReadonly = 1;
-  }else{
-    *pReadonly = 0;
-  }
-  return allocateUnixFile(h, pId, zFilename, 0);
-}
-
-
-/*
-** Attempt to open a new file for exclusive access by this process.
-** The file will be opened for both reading and writing.  To avoid
-** a potential security problem, we do not allow the file to have
-** previously existed.  Nor do we allow the file to be a symbolic
-** link.
-**
-** If delFlag is true, then make arrangements to automatically delete
-** the file when it is closed.
-**
-** On success, write the file handle into *id and return SQLITE_OK.
-**
-** On failure, return SQLITE_CANTOPEN.
-*/
-SQLITE_PRIVATE int sqlite3UnixOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){
-  int h;
-
-  CRASH_TEST_OVERRIDE(sqlite3CrashOpenExclusive, zFilename, pId, delFlag);
-  assert( 0==*pId );
-  h = open(zFilename,
-                O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY,
-                delFlag ? 0600 : SQLITE_DEFAULT_FILE_PERMISSIONS);
-  if( h<0 ){
-    return SQLITE_CANTOPEN;
-  }
-  return allocateUnixFile(h, pId, zFilename, delFlag);
-}
-
-/*
-** Attempt to open a new file for read-only access.
-**
-** On success, write the file handle into *id and return SQLITE_OK.
-**
-** On failure, return SQLITE_CANTOPEN.
-*/
-SQLITE_PRIVATE int sqlite3UnixOpenReadOnly(const char *zFilename, OsFile **pId){
-  int h;
-
-  CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadOnly, zFilename, pId, 0);
-  assert( 0==*pId );
-  h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
-  if( h<0 ){
-    return SQLITE_CANTOPEN;
-  }
-  return allocateUnixFile(h, pId, zFilename, 0);
-}
-
-/*
-** Attempt to open a file descriptor for the directory that contains a
-** file.  This file descriptor can be used to fsync() the directory
-** in order to make sure the creation of a new file is actually written
-** to disk.
-**
-** This routine is only meaningful for Unix.  It is a no-op under
-** windows since windows does not support hard links.
-**
-** If FULL_FSYNC is enabled, this function is not longer useful,
-** a FULL_FSYNC sync applies to all pending disk operations.
-**
-** On success, a handle for a previously open file at *id is
-** updated with the new directory file descriptor and SQLITE_OK is
-** returned.
-**
-** On failure, the function returns SQLITE_CANTOPEN and leaves
-** *id unchanged.
-*/
-static int unixOpenDirectory(
-  OsFile *id,
-  const char *zDirname
-){
-  int h;
-  unixFile *pFile = (unixFile*)id;
-  assert( pFile!=0 );
-  SET_THREADID(pFile);
-  assert( pFile->dirfd<0 );
-  pFile->dirfd = h = open(zDirname, O_RDONLY|O_BINARY, 0);
-  if( h<0 ){
-    return SQLITE_CANTOPEN;
-  }
-#ifdef FD_CLOEXEC
-  fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
-#endif
-  OSTRACE3("OPENDIR %-3d %s\n", h, zDirname);
-  return SQLITE_OK;
-}
-
-/*
-** Create a temporary file name in zBuf.  zBuf must be big enough to
-** hold at least SQLITE_TEMPNAME_SIZE characters.
-*/
-SQLITE_PRIVATE int sqlite3UnixTempFileName(char *zBuf){
-  static const char *azDirs[] = {
-     0,
-     "/var/tmp",
-     "/usr/tmp",
-     "/tmp",
-     ".",
-  };
-  static const unsigned char zChars[] =
-    "abcdefghijklmnopqrstuvwxyz"
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-    "0123456789";
-  int i, j;
-  struct stat buf;
-  const char *zDir = ".";
-  azDirs[0] = sqlite3_temp_directory;
-  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
-    if( azDirs[i]==0 ) continue;
-    if( stat(azDirs[i], &buf) ) continue;
-    if( !S_ISDIR(buf.st_mode) ) continue;
-    if( access(azDirs[i], 07) ) continue;
-    zDir = azDirs[i];
-    break;
-  }
-  do{
-    sqlite3_snprintf(SQLITE_TEMPNAME_SIZE, zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
-    j = strlen(zBuf);
-    sqlite3Randomness(15, &zBuf[j]);
-    for(i=0; i<15; i++, j++){
-      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
-    }
-    zBuf[j] = 0;
-  }while( access(zBuf,0)==0 );
-  return SQLITE_OK;
-}
-
-/*
-** Check that a given pathname is a directory and is writable
-**
-*/
-SQLITE_PRIVATE int sqlite3UnixIsDirWritable(char *zBuf){
-#ifndef SQLITE_OMIT_PAGER_PRAGMAS
-  struct stat buf;
-  if( zBuf==0 ) return 0;
-  if( zBuf[0]==0 ) return 0;
-  if( stat(zBuf, &buf) ) return 0;
-  if( !S_ISDIR(buf.st_mode) ) return 0;
-  if( access(zBuf, 07) ) return 0;
-#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
-  return 1;
-}
-
-/*
-** Seek to the offset in id->offset then read cnt bytes into pBuf.
-** Return the number of bytes actually read.  Update the offset.
-*/
-static int seekAndRead(unixFile *id, void *pBuf, int cnt){
+** Seek to the offset passed as the second argument, then read cnt
+** bytes into pBuf. Return the number of bytes actually read.
+*/
+static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
   int got;
   i64 newOffset;
   TIMER_START;
 #if defined(USE_PREAD)
-  got = pread(id->h, pBuf, cnt, id->offset);
+  got = pread(id->h, pBuf, cnt, offset);
   SimulateIOError( got = -1 );
 #elif defined(USE_PREAD64)
-  got = pread64(id->h, pBuf, cnt, id->offset);
+  got = pread64(id->h, pBuf, cnt, offset);
   SimulateIOError( got = -1 );
 #else
-  newOffset = lseek(id->h, id->offset, SEEK_SET);
+  newOffset = lseek(id->h, offset, SEEK_SET);
   SimulateIOError( newOffset-- );
-  if( newOffset!=id->offset ){
+  if( newOffset!=offset ){
     return -1;
   }
   got = read(id->h, pBuf, cnt);
 #endif
   TIMER_END;
-  OSTRACE5("READ    %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
-  if( got>0 ){
-    id->offset += got;
-  }
+  OSTRACE5("READ    %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
   return got;
 }
 
 /*
@@ -13939,12 +15336,17 @@
 ** Read data from a file into a buffer.  Return SQLITE_OK if all
 ** bytes were read successfully and SQLITE_IOERR if anything goes
 ** wrong.
 */
-static int unixRead(OsFile *id, void *pBuf, int amt){
+static int unixRead(
+  sqlite3_file *id,
+  void *pBuf,
+  int amt,
+  sqlite3_int64 offset
+){
   int got;
   assert( id );
-  got = seekAndRead((unixFile*)id, pBuf, amt);
+  got = seekAndRead((unixFile*)id, offset, pBuf, amt);
   if( got==amt ){
     return SQLITE_OK;
   }else if( got<0 ){
     return SQLITE_IOERR_READ;
@@ -13957,28 +15359,25 @@
 /*
 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
 ** Return the number of bytes actually read.  Update the offset.
 */
-static int seekAndWrite(unixFile *id, const void *pBuf, int cnt){
+static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
   int got;
   i64 newOffset;
   TIMER_START;
 #if defined(USE_PREAD)
-  got = pwrite(id->h, pBuf, cnt, id->offset);
+  got = pwrite(id->h, pBuf, cnt, offset);
 #elif defined(USE_PREAD64)
-  got = pwrite64(id->h, pBuf, cnt, id->offset);
-#else
-  newOffset = lseek(id->h, id->offset, SEEK_SET);
-  if( newOffset!=id->offset ){
+  got = pwrite64(id->h, pBuf, cnt, offset);
+#else
+  newOffset = lseek(id->h, offset, SEEK_SET);
+  if( newOffset!=offset ){
     return -1;
   }
   got = write(id->h, pBuf, cnt);
 #endif
   TIMER_END;
-  OSTRACE5("WRITE   %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
-  if( got>0 ){
-    id->offset += got;
-  }
+  OSTRACE5("WRITE   %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
   return got;
 }
 
 
@@ -13985,14 +15384,20 @@
 /*
 ** Write data from a buffer into a file.  Return SQLITE_OK on success
 ** or some other error code on failure.
 */
-static int unixWrite(OsFile *id, const void *pBuf, int amt){
+static int unixWrite(
+  sqlite3_file *id,
+  const void *pBuf,
+  int amt,
+  sqlite3_int64 offset
+){
   int wrote = 0;
   assert( id );
   assert( amt>0 );
-  while( amt>0 && (wrote = seekAndWrite((unixFile*)id, pBuf, amt))>0 ){
+  while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
     amt -= wrote;
+    offset += wrote;
     pBuf = &((char*)pBuf)[wrote];
   }
   SimulateIOError(( wrote=(-1), amt=1 ));
   SimulateDiskfullError(( wrote=0, amt=1 ));
@@ -14005,27 +15410,15 @@
   }
   return SQLITE_OK;
 }
 
-/*
-** Move the read/write pointer in a file.
-*/
-static int unixSeek(OsFile *id, i64 offset){
-  assert( id );
-#ifdef SQLITE_TEST
-  if( offset ) SimulateDiskfullError(return SQLITE_FULL);
-#endif
-  ((unixFile*)id)->offset = offset;
-  return SQLITE_OK;
-}
-
 #ifdef SQLITE_TEST
 /*
 ** Count the number of fullsyncs and normal syncs.  This is used to test
 ** that syncs and fullsyncs are occuring at the right times.
 */
-int sqlite3_sync_count = 0;
-int sqlite3_fullsync_count = 0;
+SQLITE_API int sqlite3_sync_count = 0;
+SQLITE_API int sqlite3_fullsync_count = 0;
 #endif
 
 /*
 ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
@@ -14119,27 +15512,36 @@
 ** SQLite to access the file will not know that the journal exists (because
 ** the directory entry for the journal was never created) and the transaction
 ** will not roll back - possibly leading to database corruption.
 */
-static int unixSync(OsFile *id, int dataOnly){
-  int rc;
-  unixFile *pFile = (unixFile*)id;
+static int unixSync(sqlite3_file *id, int flags){
+  int rc;
+  unixFile *pFile = (unixFile*)id;
+
+  int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
+  int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
+
+  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
+  assert((flags&0x0F)==SQLITE_SYNC_NORMAL
+      || (flags&0x0F)==SQLITE_SYNC_FULL
+  );
+
   assert( pFile );
   OSTRACE2("SYNC    %-3d\n", pFile->h);
-  rc = full_fsync(pFile->h, pFile->fullSync, dataOnly);
+  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
   SimulateIOError( rc=1 );
   if( rc ){
     return SQLITE_IOERR_FSYNC;
   }
   if( pFile->dirfd>=0 ){
     OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
-            HAVE_FULLFSYNC, pFile->fullSync);
+            HAVE_FULLFSYNC, isFullsync);
 #ifndef SQLITE_DISABLE_DIRSYNC
     /* The directory sync is only attempted if full_fsync is
     ** turned off or unavailable.  If a full_fsync occurred above,
     ** then the directory sync is superfluous.
     */
-    if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){
+    if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
        /*
        ** We have received multiple reports of fsync() returning
        ** errors when applied to directories on certain file systems.
        ** A failed directory sync is not a big deal.  So it seems
@@ -14154,41 +15556,11 @@
   return SQLITE_OK;
 }
 
 /*
-** Sync the directory zDirname. This is a no-op on operating systems other
-** than UNIX.
-**
-** This is used to make sure the master journal file has truely been deleted
-** before making changes to individual journals on a multi-database commit.
-** The F_FULLFSYNC option is not needed here.
-*/
-SQLITE_PRIVATE int sqlite3UnixSyncDirectory(const char *zDirname){
-#ifdef SQLITE_DISABLE_DIRSYNC
-  return SQLITE_OK;
-#else
-  int fd;
-  int r;
-  fd = open(zDirname, O_RDONLY|O_BINARY, 0);
-  OSTRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
-  if( fd<0 ){
-    return SQLITE_CANTOPEN;
-  }
-  r = fsync(fd);
-  close(fd);
-  SimulateIOError( r=1 );
-  if( r ){
-    return SQLITE_IOERR_DIR_FSYNC;
-  }else{
-    return SQLITE_OK;
-  }
-#endif
-}
-
-/*
 ** Truncate an open file to a specified size
 */
-static int unixTruncate(OsFile *id, i64 nByte){
+static int unixTruncate(sqlite3_file *id, i64 nByte){
   int rc;
   assert( id );
   rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
   SimulateIOError( rc=1 );
@@ -14201,9 +15573,9 @@
 
 /*
 ** Determine the current size of a file in bytes
 */
-static int unixFileSize(OsFile *id, i64 *pSize){
+static int unixFileSize(sqlite3_file *id, i64 *pSize){
   int rc;
   struct stat buf;
   assert( id );
   rc = fstat(((unixFile*)id)->h, &buf);
@@ -14220,14 +15592,14 @@
 ** file by this or any other process. If such a lock is held, return
 ** non-zero.  If the file is unlocked or holds only SHARED locks, then
 ** return zero.
 */
-static int unixCheckReservedLock(OsFile *id){
+static int unixCheckReservedLock(sqlite3_file *id){
   int r = 0;
   unixFile *pFile = (unixFile*)id;
 
   assert( pFile );
-  sqlite3OsEnterMutex(); /* Because pFile->pLock is shared across threads */
+  enterMutex(); /* Because pFile->pLock is shared across threads */
 
   /* Check if a thread in this process holds such a lock */
   if( pFile->pLock->locktype>SHARED_LOCK ){
     r = 1;
@@ -14246,9 +15618,9 @@
       r = 1;
     }
   }
 
-  sqlite3OsLeaveMutex();
+  leaveMutex();
   OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
 
   return r;
 }
@@ -14276,9 +15648,9 @@
 **
 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
 ** routine to lower a locking level.
 */
-static int unixLock(OsFile *id, int locktype){
+static int unixLock(sqlite3_file *id, int locktype){
   /* The following describes the implementation of the various locks and
   ** lock transitions in terms of the POSIX advisory shared and exclusive
   ** lock primitives (called read-locks and write-locks below, to avoid
   ** confusion with SQLite lock names). The algorithms are complicated
@@ -14327,10 +15699,10 @@
       locktypeName(locktype), locktypeName(pFile->locktype),
       locktypeName(pLock->locktype), pLock->cnt , getpid());
 
   /* If there is already a lock of this type or more restrictive on the
-  ** OsFile, do nothing. Don't use the end_lock: exit path, as
-  ** sqlite3OsEnterMutex() hasn't been called yet.
+  ** unixFile, do nothing. Don't use the end_lock: exit path, as
+  ** enterMutex() hasn't been called yet.
   */
   if( pFile->locktype>=locktype ){
     OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
             locktypeName(locktype));
@@ -14344,20 +15716,20 @@
   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
 
   /* This mutex is needed because pFile->pLock is shared across threads
   */
-  sqlite3OsEnterMutex();
+  enterMutex();
 
   /* Make sure the current thread owns the pFile.
   */
   rc = transferOwnership(pFile);
   if( rc!=SQLITE_OK ){
-    sqlite3OsLeaveMutex();
+    leaveMutex();
     return rc;
   }
   pLock = pFile->pLock;
 
-  /* If some thread using this PID has a lock via a different OsFile*
+  /* If some thread using this PID has a lock via a different unixFile*
   ** handle that precludes the requested lock, return BUSY.
   */
   if( (pFile->locktype!=pLock->locktype &&
           (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
@@ -14465,9 +15837,9 @@
     pLock->locktype = PENDING_LOCK;
   }
 
 end_lock:
-  sqlite3OsLeaveMutex();
+  leaveMutex();
   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype),
       rc==SQLITE_OK ? "ok" : "failed");
   return rc;
 }
@@ -14478,9 +15850,9 @@
 **
 ** If the locking level of the file descriptor is already at or below
 ** the requested locking level, this routine is a no-op.
 */
-static int unixUnlock(OsFile *id, int locktype){
+static int unixUnlock(sqlite3_file *id, int locktype){
   struct lockInfo *pLock;
   struct flock lock;
   int rc = SQLITE_OK;
   unixFile *pFile = (unixFile*)id;
@@ -14495,9 +15867,9 @@
   }
   if( CHECK_THREADID(pFile) ){
     return SQLITE_MISUSE;
   }
-  sqlite3OsEnterMutex();
+  enterMutex();
   pLock = pFile->pLock;
   assert( pLock->cnt!=0 );
   if( pFile->locktype>SHARED_LOCK ){
     assert( pLock->locktype==pFile->locktype );
@@ -14556,54 +15928,51 @@
       pOpen->nPending = 0;
       pOpen->aPending = 0;
     }
   }
-  sqlite3OsLeaveMutex();
+  leaveMutex();
   pFile->locktype = locktype;
   return rc;
 }
 
 /*
 ** Close a file.
 */
-static int unixClose(OsFile **pId){
-  unixFile *id = (unixFile*)*pId;
-
-  if( !id ) return SQLITE_OK;
-  unixUnlock(*pId, NO_LOCK);
-  if( id->dirfd>=0 ) close(id->dirfd);
-  id->dirfd = -1;
-  sqlite3OsEnterMutex();
-
-  if( id->pOpen->nLock ){
+static int unixClose(sqlite3_file *id){
+  unixFile *pFile = (unixFile *)id;
+  if( !pFile ) return SQLITE_OK;
+  unixUnlock(id, NO_LOCK);
+  if( pFile->dirfd>=0 ) close(pFile->dirfd);
+  pFile->dirfd = -1;
+  enterMutex();
+
+  if( pFile->pOpen->nLock ){
     /* If there are outstanding locks, do not actually close the file just
     ** yet because that would clear those locks.  Instead, add the file
     ** descriptor to pOpen->aPending.  It will be automatically closed when
     ** the last lock is cleared.
     */
     int *aNew;
-    struct openCnt *pOpen = id->pOpen;
+    struct openCnt *pOpen = pFile->pOpen;
     aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
     if( aNew==0 ){
       /* If a malloc fails, just leak the file descriptor */
     }else{
       pOpen->aPending = aNew;
-      pOpen->aPending[pOpen->nPending] = id->h;
+      pOpen->aPending[pOpen->nPending] = pFile->h;
       pOpen->nPending++;
     }
   }else{
     /* There are no outstanding locks so we can close the file immediately */
-    close(id->h);
-  }
-  releaseLockInfo(id->pLock);
-  releaseOpenCnt(id->pOpen);
-
-  sqlite3OsLeaveMutex();
-  id->isOpen = 0;
-  OSTRACE2("CLOSE   %-3d\n", id->h);
+    close(pFile->h);
+  }
+  releaseLockInfo(pFile->pLock);
+  releaseOpenCnt(pFile->pOpen);
+
+  leaveMutex();
+  OSTRACE2("CLOSE   %-3d\n", pFile->h);
   OpenCounter(-1);
-  sqlite3ThreadSafeFree(id);
-  *pId = 0;
+  memset(pFile, 0, sizeof(unixFile));
   return SQLITE_OK;
 }
 
 
@@ -14628,18 +15997,24 @@
   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
   int fd;                           /* file desc to assoc this lock with */
 };
 
-#define afpfsByteRangeLock2FSCTL	_IOWR('z', 23, struct ByteRangeLockPB2)
-
-/* return 0 on success, 1 on failure.  To match the behavior of the
-  normal posix file locking (used in unixLock for example), we should
-  provide 'richer' return codes - specifically to differentiate between
-  'file busy' and 'file system error' results */
-static int _AFPFSSetLock(const char *path, int fd, unsigned long long offset,
-                         unsigned long long length, int setLockFlag)
-{
-  struct ByteRangeLockPB2	pb;
+#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
+
+/*
+** Return 0 on success, 1 on failure.  To match the behavior of the
+** normal posix file locking (used in unixLock for example), we should
+** provide 'richer' return codes - specifically to differentiate between
+** 'file busy' and 'file system error' results.
+*/
+static int _AFPFSSetLock(
+  const char *path,
+  int fd,
+  unsigned long long offset,
+  unsigned long long length,
+  int setLockFlag
+){
+  struct ByteRangeLockPB2       pb;
   int                     err;
 
   pb.unLockFlag = setLockFlag ? 0 : 1;
   pb.startEndFlag = 0;
@@ -14663,9 +16038,9 @@
  ** file by this or any other process. If such a lock is held, return
  ** non-zero.  If the file is unlocked or holds only SHARED locks, then
  ** return zero.
  */
-static int afpUnixCheckReservedLock(OsFile *id){
+static int afpUnixCheckReservedLock(sqlite3_file *id){
   int r = 0;
   unixFile *pFile = (unixFile*)id;
 
   assert( pFile );
@@ -14696,9 +16071,9 @@
 }
 
 /* AFP-style locking following the behavior of unixLock, see the unixLock
 ** function comments for details of lock management. */
-static int afpUnixLock(OsFile *id, int locktype)
+static int afpUnixLock(sqlite3_file *id, int locktype)
 {
   int rc = SQLITE_OK;
   unixFile *pFile = (unixFile*)id;
   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
@@ -14707,10 +16082,10 @@
   assert( pFile );
   OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
          locktypeName(locktype), locktypeName(pFile->locktype), getpid());
   /* If there is already a lock of this type or more restrictive on the
-    ** OsFile, do nothing. Don't use the afp_end_lock: exit path, as
-    ** sqlite3OsEnterMutex() hasn't been called yet.
+    ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
+    ** enterMutex() hasn't been called yet.
     */
   if( pFile->locktype>=locktype ){
     OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,
            locktypeName(locktype));
@@ -14724,15 +16099,15 @@
   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
 
   /* This mutex is needed because pFile->pLock is shared across threads
     */
-  sqlite3OsEnterMutex();
+  enterMutex();
 
   /* Make sure the current thread owns the pFile.
     */
   rc = transferOwnership(pFile);
   if( rc!=SQLITE_OK ){
-    sqlite3OsLeaveMutex();
+    leaveMutex();
     return rc;
   }
 
   /* A PENDING lock is needed before acquiring a SHARED lock and before
@@ -14817,9 +16192,9 @@
     pFile->locktype = PENDING_LOCK;
   }
 
 afp_end_lock:
-    sqlite3OsLeaveMutex();
+    leaveMutex();
   OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype),
          rc==SQLITE_OK ? "ok" : "failed");
   return rc;
 }
@@ -14830,9 +16205,9 @@
  **
  ** If the locking level of the file descriptor is already at or below
  ** the requested locking level, this routine is a no-op.
  */
-static int afpUnixUnlock(OsFile *id, int locktype) {
+static int afpUnixUnlock(sqlite3_file *id, int locktype) {
   struct flock lock;
   int rc = SQLITE_OK;
   unixFile *pFile = (unixFile*)id;
   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
@@ -14847,9 +16222,9 @@
   }
   if( CHECK_THREADID(pFile) ){
     return SQLITE_MISUSE;
   }
-  sqlite3OsEnterMutex();
+  enterMutex();
   if( pFile->locktype>SHARED_LOCK ){
     if( locktype==SHARED_LOCK ){
       int failed = 0;
 
@@ -14891,35 +16266,32 @@
     }
   }
   if (rc == SQLITE_OK)
     pFile->locktype = locktype;
-  sqlite3OsLeaveMutex();
+  leaveMutex();
   return rc;
 }
 
 /*
  ** Close a file & cleanup AFP specific locking context
  */
-static int afpUnixClose(OsFile **pId) {
-  unixFile *id = (unixFile*)*pId;
-
-  if( !id ) return SQLITE_OK;
+static int afpUnixClose(sqlite3_file *id) {
+  unixFile *pFile = (unixFile*)pId;
+
+  if( !pFile ) return SQLITE_OK;
   afpUnixUnlock(*pId, NO_LOCK);
   /* free the AFP locking structure */
-  if (id->lockingContext != NULL) {
-    if (((afpLockingContext *)id->lockingContext)->filePath != NULL)
-      sqlite3ThreadSafeFree(((afpLockingContext*)id->lockingContext)->filePath);
-    sqlite3ThreadSafeFree(id->lockingContext);
-  }
-
-  if( id->dirfd>=0 ) close(id->dirfd);
-  id->dirfd = -1;
-  close(id->h);
-  id->isOpen = 0;
-  OSTRACE2("CLOSE   %-3d\n", id->h);
-  OpenCounter(-1);
-  sqlite3ThreadSafeFree(id);
-  *pId = 0;
+  if (pFile->lockingContext != NULL) {
+    if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL)
+      sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath);
+    sqlite3_free(pFile->lockingContext);
+  }
+
+  if( pFile->dirfd>=0 ) close(pFile->dirfd);
+  pFile->dirfd = -1;
+  close(pFile->h);
+  OSTRACE2("CLOSE   %-3d\n", pFile->h);
+  OpenCounter(-1);
   return SQLITE_OK;
 }
 
 
@@ -14929,9 +16301,9 @@
  ** The flockLockingContext is not used
  */
 typedef void flockLockingContext;
 
-static int flockUnixCheckReservedLock(OsFile *id) {
+static int flockUnixCheckReservedLock(sqlite3_file *id) {
   unixFile *pFile = (unixFile*)id;
 
   if (pFile->locktype == RESERVED_LOCK) {
     return 1; /* already have a reserved lock */
@@ -14946,9 +16318,9 @@
     return 1; /* someone else might have it reserved */
   }
 }
 
-static int flockUnixLock(OsFile *id, int locktype) {
+static int flockUnixLock(sqlite3_file *id, int locktype) {
   unixFile *pFile = (unixFile*)id;
 
   /* if we already have a lock, it is exclusive.
   ** Just adjust level and punt on outta here. */
@@ -14968,9 +16340,9 @@
     return SQLITE_OK;
   }
 }
 
-static int flockUnixUnlock(OsFile *id, int locktype) {
+static int flockUnixUnlock(sqlite3_file *id, int locktype) {
   unixFile *pFile = (unixFile*)id;
 
   assert( locktype<=SHARED_LOCK );
 
@@ -14997,25 +16369,22 @@
 
 /*
  ** Close a file.
  */
-static int flockUnixClose(OsFile **pId) {
-  unixFile *id = (unixFile*)*pId;
-
-  if( !id ) return SQLITE_OK;
+static int flockUnixClose(sqlite3_file *pId) {
+  unixFile *pFile = (unixFile*)*pId;
+
+  if( !pFile ) return SQLITE_OK;
   flockUnixUnlock(*pId, NO_LOCK);
 
-  if( id->dirfd>=0 ) close(id->dirfd);
-  id->dirfd = -1;
-  sqlite3OsEnterMutex();
-
-  close(id->h);
-  sqlite3OsLeaveMutex();
-  id->isOpen = 0;
-  OSTRACE2("CLOSE   %-3d\n", id->h);
-  OpenCounter(-1);
-  sqlite3ThreadSafeFree(id);
-  *pId = 0;
+  if( pFile->dirfd>=0 ) close(pFile->dirfd);
+  pFile->dirfd = -1;
+  enterMutex();
+
+  close(pFile->h);
+  leaveMutex();
+  OSTRACE2("CLOSE   %-3d\n", pFile->h);
+  OpenCounter(-1);
   return SQLITE_OK;
 }
 
 #pragma mark Old-School .lock file based locking
@@ -15029,9 +16398,9 @@
   char *lockPath;
 };
 
 
-static int dotlockUnixCheckReservedLock(OsFile *id) {
+static int dotlockUnixCheckReservedLock(sqlite3_file *id) {
   unixFile *pFile = (unixFile*)id;
   dotlockLockingContext *context =
     (dotlockLockingContext *) pFile->lockingContext;
 
@@ -15047,9 +16416,9 @@
       return 0;
   }
 }
 
-static int dotlockUnixLock(OsFile *id, int locktype) {
+static int dotlockUnixLock(sqlite3_file *id, int locktype) {
   unixFile *pFile = (unixFile*)id;
   dotlockLockingContext *context =
     (dotlockLockingContext *) pFile->lockingContext;
 
@@ -15081,9 +16450,9 @@
   pFile->locktype = locktype;
   return SQLITE_OK;
 }
 
-static int dotlockUnixUnlock(OsFile *id, int locktype) {
+static int dotlockUnixUnlock(sqlite3_file *id, int locktype) {
   unixFile *pFile = (unixFile*)id;
   dotlockLockingContext *context =
     (dotlockLockingContext *) pFile->lockingContext;
 
@@ -15108,33 +16477,30 @@
 
 /*
  ** Close a file.
  */
-static int dotlockUnixClose(OsFile **pId) {
-  unixFile *id = (unixFile*)*pId;
-
-  if( !id ) return SQLITE_OK;
+static int dotlockUnixClose(sqlite3_file *id) {
+  unixFile *pFile = (unixFile*)id;
+
+  if( !pFile ) return SQLITE_OK;
   dotlockUnixUnlock(*pId, NO_LOCK);
   /* free the dotlock locking structure */
-  if (id->lockingContext != NULL) {
-    if (((dotlockLockingContext *)id->lockingContext)->lockPath != NULL)
-      sqlite3ThreadSafeFree( ( (dotlockLockingContext *)
-        id->lockingContext)->lockPath);
-    sqlite3ThreadSafeFree(id->lockingContext);
-  }
-
-  if( id->dirfd>=0 ) close(id->dirfd);
-  id->dirfd = -1;
-  sqlite3OsEnterMutex();
-
-  close(id->h);
-
-  sqlite3OsLeaveMutex();
-  id->isOpen = 0;
-  OSTRACE2("CLOSE   %-3d\n", id->h);
-  OpenCounter(-1);
-  sqlite3ThreadSafeFree(id);
-  *pId = 0;
+  if (pFile->lockingContext != NULL) {
+    if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL)
+      sqlite3_free( ( (dotlockLockingContext *)
+        pFile->lockingContext)->lockPath);
+    sqlite3_free(pFile->lockingContext);
+  }
+
+  if( pFile->dirfd>=0 ) close(pFile->dirfd);
+  pFile->dirfd = -1;
+  enterMutex();
+
+  close(pFile->h);
+
+  leaveMutex();
+  OSTRACE2("CLOSE   %-3d\n", pFile->h);
+  OpenCounter(-1);
   return SQLITE_OK;
 }
 
 
@@ -15144,114 +16510,53 @@
  ** The nolockLockingContext is void
  */
 typedef void nolockLockingContext;
 
-static int nolockUnixCheckReservedLock(OsFile *id) {
-  return 0;
-}
-
-static int nolockUnixLock(OsFile *id, int locktype) {
-  return SQLITE_OK;
-}
-
-static int nolockUnixUnlock(OsFile *id, int locktype) {
+static int nolockUnixCheckReservedLock(sqlite3_file *id) {
+  return 0;
+}
+
+static int nolockUnixLock(sqlite3_file *id, int locktype) {
+  return SQLITE_OK;
+}
+
+static int nolockUnixUnlock(sqlite3_file *id, int locktype) {
   return SQLITE_OK;
 }
 
 /*
  ** Close a file.
  */
-static int nolockUnixClose(OsFile **pId) {
-  unixFile *id = (unixFile*)*pId;
-
-  if( !id ) return SQLITE_OK;
-  if( id->dirfd>=0 ) close(id->dirfd);
-  id->dirfd = -1;
-  sqlite3OsEnterMutex();
-
-  close(id->h);
-
-  sqlite3OsLeaveMutex();
-  id->isOpen = 0;
-  OSTRACE2("CLOSE   %-3d\n", id->h);
-  OpenCounter(-1);
-  sqlite3ThreadSafeFree(id);
-  *pId = 0;
+static int nolockUnixClose(sqlite3_file *id) {
+  unixFile *pFile = (unixFile*)id;
+
+  if( !pFile ) return SQLITE_OK;
+  if( pFile->dirfd>=0 ) close(pFile->dirfd);
+  pFile->dirfd = -1;
+  enterMutex();
+
+  close(pFile->h);
+
+  leaveMutex();
+  OSTRACE2("CLOSE   %-3d\n", pFile->h);
+  OpenCounter(-1);
   return SQLITE_OK;
 }
 
 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
 
-/*
-** Turn a relative pathname into a full pathname.  Return a pointer
-** to the full pathname stored in space obtained from sqliteMalloc().
-** The calling function is responsible for freeing this space once it
-** is no longer needed.
-*/
-SQLITE_PRIVATE char *sqlite3UnixFullPathname(const char *zRelative){
-  char *zFull = 0;
-  if( zRelative[0]=='/' ){
-    sqlite3SetString(&zFull, zRelative, (char*)0);
-  }else{
-    char *zBuf = sqliteMalloc(5000);
-    if( zBuf==0 ){
-      return 0;
-    }
-    zBuf[0] = 0;
-    sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative,
-                    (char*)0);
-    sqliteFree(zBuf);
-  }
-
-#if 0
-  /*
-  ** Remove "/./" path elements and convert "/A/./" path elements
-  ** to just "/".
-  */
-  if( zFull ){
-    int i, j;
-    for(i=j=0; zFull[i]; i++){
-      if( zFull[i]=='/' ){
-        if( zFull[i+1]=='/' ) continue;
-        if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
-          i += 1;
-          continue;
-        }
-        if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
-          while( j>0 && zFull[j-1]!='/' ){ j--; }
-          i += 3;
-          continue;
-        }
-      }
-      zFull[j++] = zFull[i];
-    }
-    zFull[j] = 0;
-  }
-#endif
-
-  return zFull;
-}
-
-/*
-** Change the value of the fullsync flag in the given file descriptor.
-*/
-static void unixSetFullSync(OsFile *id, int v){
-  ((unixFile*)id)->fullSync = v;
-}
-
-/*
-** Return the underlying file handle for an OsFile
-*/
-static int unixFileHandle(OsFile *id){
-  return ((unixFile*)id)->h;
-}
-
-/*
-** Return an integer that indices the type of lock currently held
-** by this handle.  (Used for testing and analysis only.)
-*/
-static int unixLockState(OsFile *id){
-  return ((unixFile*)id)->locktype;
+
+/*
+** Information and control of an open file handle.
+*/
+static int unixFileControl(sqlite3_file *id, int op, void *pArg){
+  switch( op ){
+    case SQLITE_FCNTL_LOCKSTATE: {
+      *(int*)pArg = ((unixFile*)id)->locktype;
+      return SQLITE_OK;
+    }
+  }
+  return SQLITE_ERROR;
 }
 
 /*
 ** Return the sector size in bytes of the underlying block device for
@@ -15262,121 +16567,118 @@
 ** if two files are created in the same file-system directory (i.e.
 ** a database and it's journal file) that the sector size will be the
 ** same for both.
 */
-static int unixSectorSize(OsFile *id){
+static int unixSectorSize(sqlite3_file *id){
   return SQLITE_DEFAULT_SECTOR_SIZE;
 }
 
 /*
-** This vector defines all the methods that can operate on an OsFile
+** Return the device characteristics for the file. This is always 0.
+*/
+static int unixDeviceCharacteristics(sqlite3_file *id){
+  return 0;
+}
+
+/*
+** This vector defines all the methods that can operate on an sqlite3_file
 ** for unix.
 */
-static const IoMethod sqlite3UnixIoMethod = {
-  unixClose,
-  unixOpenDirectory,
+static const sqlite3_io_methods sqlite3UnixIoMethod = {
+  1,                        /* iVersion */
+  unixClose,
   unixRead,
   unixWrite,
-  unixSeek,
   unixTruncate,
   unixSync,
-  unixSetFullSync,
-  unixFileHandle,
   unixFileSize,
   unixLock,
   unixUnlock,
-  unixLockState,
   unixCheckReservedLock,
+  unixFileControl,
   unixSectorSize,
+  unixDeviceCharacteristics
 };
 
 #ifdef SQLITE_ENABLE_LOCKING_STYLE
 /*
- ** This vector defines all the methods that can operate on an OsFile
- ** for unix with AFP style file locking.
- */
-static const IoMethod sqlite3AFPLockingUnixIoMethod = {
-    afpUnixClose,
-    unixOpenDirectory,
-    unixRead,
-    unixWrite,
-    unixSeek,
-    unixTruncate,
-    unixSync,
-    unixSetFullSync,
-    unixFileHandle,
-    unixFileSize,
-    afpUnixLock,
-    afpUnixUnlock,
-    unixLockState,
-    afpUnixCheckReservedLock,
-    unixSectorSize,
-};
-
-/*
- ** This vector defines all the methods that can operate on an OsFile
- ** for unix with flock() style file locking.
- */
-static const IoMethod sqlite3FlockLockingUnixIoMethod = {
-    flockUnixClose,
-    unixOpenDirectory,
-    unixRead,
-    unixWrite,
-    unixSeek,
-    unixTruncate,
-    unixSync,
-    unixSetFullSync,
-    unixFileHandle,
-    unixFileSize,
-    flockUnixLock,
-    flockUnixUnlock,
-    unixLockState,
-    flockUnixCheckReservedLock,
-    unixSectorSize,
-};
-
-/*
- ** This vector defines all the methods that can operate on an OsFile
- ** for unix with dotlock style file locking.
- */
-static const IoMethod sqlite3DotlockLockingUnixIoMethod = {
-    dotlockUnixClose,
-    unixOpenDirectory,
-    unixRead,
-    unixWrite,
-    unixSeek,
-    unixTruncate,
-    unixSync,
-    unixSetFullSync,
-    unixFileHandle,
-    unixFileSize,
-    dotlockUnixLock,
-    dotlockUnixUnlock,
-    unixLockState,
-    dotlockUnixCheckReservedLock,
-    unixSectorSize,
-};
-
-/*
- ** This vector defines all the methods that can operate on an OsFile
- ** for unix with dotlock style file locking.
- */
-static const IoMethod sqlite3NolockLockingUnixIoMethod = {
-  nolockUnixClose,
-  unixOpenDirectory,
+** This vector defines all the methods that can operate on an sqlite3_file
+** for unix with AFP style file locking.
+*/
+static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
+  1,                        /* iVersion */
+  unixClose,
+  unixRead,
+  unixWrite,
+  unixTruncate,
+  unixSync,
+  unixFileSize,
+  afpUnixLock,
+  afpUnixUnlock,
+  afpUnixCheckReservedLock,
+  unixFileControl,
+  unixSectorSize,
+  unixDeviceCharacteristics
+};
+
+/*
+** This vector defines all the methods that can operate on an sqlite3_file
+** for unix with flock() style file locking.
+*/
+static const sqlite3_io_methods sqlite3FlockLockingUnixIoMethod = {
+  1,                        /* iVersion */
+  flockUnixClose,
+  unixRead,
+  unixWrite,
+  unixTruncate,
+  unixSync,
+  unixFileSize,
+  flockUnixLock,
+  flockUnixUnlock,
+  flockUnixCheckReservedLock,
+  unixFileControl,
+  unixSectorSize,
+  unixDeviceCharacteristics
+};
+
+/*
+** This vector defines all the methods that can operate on an sqlite3_file
+** for unix with dotlock style file locking.
+*/
+static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = {
+  1,                        /* iVersion */
+  dotlockUnixClose,
+  unixRead,
+  unixWrite,
+  unixTruncate,
+  unixSync,
+  unixFileSize,
+  dotlockUnixLock,
+  dotlockUnixUnlock,
+  dotlockUnixCheckReservedLock,
+  unixFileControl,
+  unixSectorSize,
+  unixDeviceCharacteristics
+};
+
+/*
+** This vector defines all the methods that can operate on an sqlite3_file
+** for unix with dotlock style file locking.
+*/
+static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = {
+  1,                        /* iVersion */
+  nolockUnixClose,
   unixRead,
   unixWrite,
-  unixSeek,
   unixTruncate,
   unixSync,
-  unixSetFullSync,
-  unixFileHandle,
   unixFileSize,
   nolockUnixLock,
   nolockUnixUnlock,
-  unixLockState,
   nolockUnixCheckReservedLock,
+  unixFileControl,
   unixSectorSize,
+  unixDeviceCharacteristics
 };
 
 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
 
@@ -15386,68 +16688,62 @@
 ** If we run out of memory, close the file and return an error.
 */
 #ifdef SQLITE_ENABLE_LOCKING_STYLE
 /*
- ** When locking extensions are enabled, the filepath and locking style
- ** are needed to determine the unixFile pMethod to use for locking operations.
- ** The locking-style specific lockingContext data structure is created
- ** and assigned here also.
- */
-static int allocateUnixFile(
+** When locking extensions are enabled, the filepath and locking style
+** are needed to determine the unixFile pMethod to use for locking operations.
+** The locking-style specific lockingContext data structure is created
+** and assigned here also.
+*/
+static int fillInUnixFile(
   int h,                  /* Open file descriptor of file being opened */
-  OsFile **pId,           /* Write completed initialization here */
-  const char *zFilename,  /* Name of the file being opened */
-  int delFlag             /* Delete-on-or-before-close flag */
+  int dirfd,              /* Directory file descriptor */
+  sqlite3_file *pId,      /* Write completed initialization here */
+  const char *zFilename,  /* Name of the file being opened */
 ){
   sqlite3LockingStyle lockingStyle;
-  unixFile *pNew;
-  unixFile f;
-  int rc;
-
-  memset(&f, 0, sizeof(f));
+  unixFile *pNew = (unixFile *)pId;
+  int rc;
+
+  memset(pNew, 0, sizeof(unixFile));
   lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
   if ( lockingStyle == posixLockingStyle ) {
-    sqlite3OsEnterMutex();
-    rc = findLockInfo(h, &f.pLock, &f.pOpen);
-    sqlite3OsLeaveMutex();
+    enterMutex();
+    rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
+    leaveMutex();
     if( rc ){
       close(h);
       unlink(zFilename);
       return SQLITE_NOMEM;
     }
   } else {
     /*  pLock and pOpen are only used for posix advisory locking */
-    f.pLock = NULL;
-    f.pOpen = NULL;
-  }
-  if( delFlag ){
-    unlink(zFilename);
-  }
-  f.dirfd = -1;
-  f.h = h;
-  SET_THREADID(&f);
-  pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
+    pNew->pLock = NULL;
+    pNew->pOpen = NULL;
+  }
+  pNew->dirfd = -1;
+  pNew->h = h;
+  SET_THREADID(pNew);
+  pNew = sqlite3_malloc( sizeof(unixFile) );
   if( pNew==0 ){
     close(h);
-    sqlite3OsEnterMutex();
-    releaseLockInfo(f.pLock);
-    releaseOpenCnt(f.pOpen);
-    sqlite3OsLeaveMutex();
-    *pId = 0;
-    return SQLITE_NOMEM;
-  }else{
-    *pNew = f;
+    enterMutex();
+    releaseLockInfo(pNew->pLock);
+    releaseOpenCnt(pNew->pOpen);
+    leaveMutex();
+    return SQLITE_NOMEM;
+  }else{
     switch(lockingStyle) {
       case afpLockingStyle: {
         /* afp locking uses the file path so it needs to be included in
         ** the afpLockingContext */
         int nFilename;
         pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
         pNew->lockingContext =
-          sqlite3ThreadSafeMalloc(sizeof(afpLockingContext));
+          sqlite3_malloc(sizeof(afpLockingContext));
         nFilename = strlen(zFilename)+1;
         ((afpLockingContext *)pNew->lockingContext)->filePath =
-          sqlite3ThreadSafeMalloc(nFilename);
+          sqlite3_malloc(nFilename);
         memcpy(((afpLockingContext *)pNew->lockingContext)->filePath,
                zFilename, nFilename);
         srandomdev();
         break;
@@ -15460,13 +16756,13 @@
         /* dotlock locking uses the file path so it needs to be included in
          ** the dotlockLockingContext */
         int nFilename;
         pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
-        pNew->lockingContext = sqlite3ThreadSafeMalloc(
+        pNew->lockingContext = sqlite3_malloc(
           sizeof(dotlockLockingContext));
         nFilename = strlen(zFilename) + 6;
         ((dotlockLockingContext *)pNew->lockingContext)->lockPath =
-            sqlite3ThreadSafeMalloc( nFilename );
+            sqlite3_malloc( nFilename );
         sqlite3_snprintf(nFilename,
                 ((dotlockLockingContext *)pNew->lockingContext)->lockPath,
                 "%s.lock", zFilename);
         break;
@@ -15479,66 +16775,345 @@
       case unsupportedLockingStyle:
       default:
         pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
     }
-    *pId = (OsFile*)pNew;
     OpenCounter(+1);
     return SQLITE_OK;
   }
 }
 #else /* SQLITE_ENABLE_LOCKING_STYLE */
-static int allocateUnixFile(
+static int fillInUnixFile(
   int h,                 /* Open file descriptor on file being opened */
-  OsFile **pId,          /* Write the resul unixFile structure here */
-  const char *zFilename, /* Name of the file being opened */
-  int delFlag            /* If true, delete the file on or before closing */
-){
-  unixFile *pNew;
-  unixFile f;
+  int dirfd,
+  sqlite3_file *pId,     /* Write to the unixFile structure here */
+  const char *zFilename  /* Name of the file being opened */
+){
+  unixFile *pNew = (unixFile *)pId;
   int rc;
 
 #ifdef FD_CLOEXEC
   fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
 #endif
-  memset(&f, 0, sizeof(f));
-  sqlite3OsEnterMutex();
-  rc = findLockInfo(h, &f.pLock, &f.pOpen);
-  sqlite3OsLeaveMutex();
-  if( delFlag ){
-    unlink(zFilename);
-  }
+
+  enterMutex();
+  rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
+  leaveMutex();
   if( rc ){
     close(h);
     return SQLITE_NOMEM;
   }
+
   OSTRACE3("OPEN    %-3d %s\n", h, zFilename);
-  f.dirfd = -1;
-  f.h = h;
-  SET_THREADID(&f);
-  pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
-  if( pNew==0 ){
-    close(h);
-    sqlite3OsEnterMutex();
-    releaseLockInfo(f.pLock);
-    releaseOpenCnt(f.pOpen);
-    sqlite3OsLeaveMutex();
-    *pId = 0;
-    return SQLITE_NOMEM;
-  }else{
-    *pNew = f;
-    pNew->pMethod = &sqlite3UnixIoMethod;
-    *pId = (OsFile*)pNew;
-    OpenCounter(+1);
-    return SQLITE_OK;
-  }
+  pNew->dirfd = -1;
+  pNew->h = h;
+  pNew->dirfd = dirfd;
+  SET_THREADID(pNew);
+
+  pNew->pMethod = &sqlite3UnixIoMethod;
+  OpenCounter(+1);
+  return SQLITE_OK;
 }
 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
 
-#endif /* SQLITE_OMIT_DISKIO */
-/***************************************************************************
-** Everything above deals with file I/O.  Everything that follows deals
-** with other miscellanous aspects of the operating system interface
-****************************************************************************/
+/*
+** Open a file descriptor to the directory containing file zFilename.
+** If successful, *pFd is set to the opened file descriptor and
+** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
+** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
+** value.
+**
+** If SQLITE_OK is returned, the caller is responsible for closing
+** the file descriptor *pFd using close().
+*/
+static int openDirectory(const char *zFilename, int *pFd){
+  int ii;
+  int fd;
+  char zDirname[MAX_PATHNAME+1];
+
+  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
+  for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
+  if( ii>0 ){
+    zDirname[ii] = '\0';
+    fd = open(zDirname, O_RDONLY|O_BINARY, 0);
+    if( fd>0 ){
+#ifdef FD_CLOEXEC
+      fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
+#endif
+      OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
+    }
+  }
+  *pFd = fd;
+  return (fd>0?SQLITE_OK:SQLITE_CANTOPEN);
+}
+
+/*
+** Open the file zPath.
+**
+** Previously, the SQLite OS layer used three functions in place of this
+** one:
+**
+**     sqlite3OsOpenReadWrite();
+**     sqlite3OsOpenReadOnly();
+**     sqlite3OsOpenExclusive();
+**
+** These calls correspond to the following combinations of flags:
+**
+**     ReadWrite() ->     (READWRITE | CREATE)
+**     ReadOnly()  ->     (READONLY)
+**     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
+**
+** The old OpenExclusive() accepted a boolean argument - "delFlag". If
+** true, the file was configured to be automatically deleted when the
+** file handle closed. To achieve the same effect using this new
+** interface, add the DELETEONCLOSE flag to those specified above for
+** OpenExclusive().
+*/
+static int unixOpen(
+  sqlite3_vfs *pVfs,
+  const char *zPath,
+  sqlite3_file *pFile,
+  int flags,
+  int *pOutFlags
+){
+  int fd = 0;                    /* File descriptor returned by open() */
+  int dirfd = -1;                /* Directory file descriptor */
+  int oflags = 0;                /* Flags to pass to open() */
+  int eType = flags&0xFFFFFF00;  /* Type of file to open */
+
+  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
+  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
+  int isCreate     = (flags & SQLITE_OPEN_CREATE);
+  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
+  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
+
+  /* If creating a master or main-file journal, this function will open
+  ** a file-descriptor on the directory too. The first time unixSync()
+  ** is called the directory file descriptor will be fsync()ed and close()d.
+  */
+  int isOpenDirectory = (isCreate &&
+      (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
+  );
+
+  /* Check the following statements are true:
+  **
+  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
+  **   (b) if CREATE is set, then READWRITE must also be set, and
+  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
+  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
+  */
+  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
+  assert(isCreate==0 || isReadWrite);
+  assert(isExclusive==0 || isCreate);
+  assert(isDelete==0 || isCreate);
+
+
+  /* The main DB, main journal, and master journal are never automatically
+  ** deleted
+  */
+  assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
+  assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
+  assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
+
+  /* Assert that the upper layer has set one of the "file-type" flags. */
+  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
+       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
+       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
+       || eType==SQLITE_OPEN_TRANSIENT_DB
+  );
+
+  if( isReadonly )  oflags |= O_RDONLY;
+  if( isReadWrite ) oflags |= O_RDWR;
+  if( isCreate )    oflags |= O_CREAT;
+  if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
+  oflags |= (O_LARGEFILE|O_BINARY);
+
+  memset(pFile, 0, sizeof(unixFile));
+  fd = open(zPath, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
+  if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
+    /* Failed to open the file for read/write access. Try read-only. */
+    flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
+    flags |= SQLITE_OPEN_READONLY;
+    return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
+  }
+  if( fd<0 ){
+    return SQLITE_CANTOPEN;
+  }
+  if( isDelete ){
+    unlink(zPath);
+  }
+  if( pOutFlags ){
+    *pOutFlags = flags;
+  }
+
+  assert(fd!=0);
+  if( isOpenDirectory ){
+    int rc = openDirectory(zPath, &dirfd);
+    if( rc!=SQLITE_OK ){
+      close(fd);
+      return rc;
+    }
+  }
+  return fillInUnixFile(fd, dirfd, pFile, zPath);
+}
+
+/*
+** Delete the file at zPath. If the dirSync argument is true, fsync()
+** the directory after deleting the file.
+*/
+static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
+  int rc = SQLITE_OK;
+  SimulateIOError(return SQLITE_IOERR_DELETE);
+  unlink(zPath);
+  if( dirSync ){
+    int fd;
+    rc = openDirectory(zPath, &fd);
+    if( rc==SQLITE_OK ){
+      if( fsync(fd) ){
+        rc = SQLITE_IOERR_DIR_FSYNC;
+      }
+      close(fd);
+    }
+  }
+  return rc;
+}
+
+/*
+** Test the existance of or access permissions of file zPath. The
+** test performed depends on the value of flags:
+**
+**     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
+**     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
+**     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
+**
+** Otherwise return 0.
+*/
+static int unixAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
+  int amode;
+  switch( flags ){
+    case SQLITE_ACCESS_EXISTS:
+      amode = F_OK;
+      break;
+    case SQLITE_ACCESS_READWRITE:
+      amode = W_OK|R_OK;
+      break;
+    case SQLITE_ACCESS_READ:
+      amode = R_OK;
+      break;
+
+    default:
+      assert(!"Invalid flags argument");
+  }
+  return (access(zPath, amode)==0);
+}
+
+/*
+** Create a temporary file name in zBuf.  zBuf must be allocated
+** by the calling process and must be big enough to hold at least
+** pVfs->mxPathname bytes.
+*/
+static int unixGetTempName(sqlite3_vfs *pVfs, char *zBuf){
+  static const char *azDirs[] = {
+     0,
+     "/var/tmp",
+     "/usr/tmp",
+     "/tmp",
+     ".",
+  };
+  static const unsigned char zChars[] =
+    "abcdefghijklmnopqrstuvwxyz"
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+    "0123456789";
+  int i, j;
+  struct stat buf;
+  const char *zDir = ".";
+
+  /* It's odd to simulate an io-error here, but really this is just
+  ** using the io-error infrastructure to test that SQLite handles this
+  ** function failing.
+  */
+  SimulateIOError( return SQLITE_ERROR );
+
+  azDirs[0] = sqlite3_temp_directory;
+  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
+    if( azDirs[i]==0 ) continue;
+    if( stat(azDirs[i], &buf) ) continue;
+    if( !S_ISDIR(buf.st_mode) ) continue;
+    if( access(azDirs[i], 07) ) continue;
+    zDir = azDirs[i];
+    break;
+  }
+  do{
+    assert( pVfs->mxPathname==MAX_PATHNAME );
+    sqlite3_snprintf(MAX_PATHNAME-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
+    j = strlen(zBuf);
+    sqlite3Randomness(15, &zBuf[j]);
+    for(i=0; i<15; i++, j++){
+      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
+    }
+    zBuf[j] = 0;
+  }while( access(zBuf,0)==0 );
+  return SQLITE_OK;
+}
+
+
+/*
+** Turn a relative pathname into a full pathname. The relative path
+** is stored as a nul-terminated string in the buffer pointed to by
+** zPath.
+**
+** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
+** (in this case, MAX_PATHNAME bytes). The full-path is written to
+** this buffer before returning.
+*/
+static int unixFullPathname(sqlite3_vfs *pVfs, const char *zPath, char *zOut){
+
+  /* It's odd to simulate an io-error here, but really this is just
+  ** using the io-error infrastructure to test that SQLite handles this
+  ** function failing. This function could fail if, for example, the
+  ** current working directly has been unlinked.
+  */
+  SimulateIOError( return SQLITE_ERROR );
+
+  assert( pVfs->mxPathname==MAX_PATHNAME );
+  zOut[MAX_PATHNAME-1] = '\0';
+  if( zPath[0]=='/' ){
+    sqlite3_snprintf(MAX_PATHNAME, zOut, "%s", zPath);
+  }else{
+    int nCwd;
+    if( getcwd(zOut, MAX_PATHNAME-1)==0 ){
+      return SQLITE_CANTOPEN;
+    }
+    nCwd = strlen(zOut);
+    sqlite3_snprintf(MAX_PATHNAME-nCwd, &zOut[nCwd], "/%s", zPath);
+  }
+  return SQLITE_OK;
+
+#if 0
+  /*
+  ** Remove "/./" path elements and convert "/A/./" path elements
+  ** to just "/".
+  */
+  if( zFull ){
+    int i, j;
+    for(i=j=0; zFull[i]; i++){
+      if( zFull[i]=='/' ){
+        if( zFull[i+1]=='/' ) continue;
+        if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
+          i += 1;
+          continue;
+        }
+        if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
+          while( j>0 && zFull[j-1]!='/' ){ j--; }
+          i += 3;
+          continue;
+        }
+      }
+      zFull[j++] = zFull[i];
+    }
+    zFull[j] = 0;
+  }
+#endif
+}
 
 
 #ifndef SQLITE_OMIT_LOAD_EXTENSION
 /*
@@ -15545,25 +17120,48 @@
 ** Interfaces for opening a shared library, finding entry points
 ** within the shared library, and closing the shared library.
 */
 #include <dlfcn.h>
-SQLITE_PRIVATE void *sqlite3UnixDlopen(const char *zFilename){
+static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
 }
-SQLITE_PRIVATE void *sqlite3UnixDlsym(void *pHandle, const char *zSymbol){
+
+/*
+** SQLite calls this function immediately after a call to unixDlSym() or
+** unixDlOpen() fails (returns a null pointer). If a more detailed error
+** message is available, it is written to zBufOut. If no error message
+** is available, zBufOut is left unmodified and SQLite uses a default
+** error message.
+*/
+static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
+  char *zErr;
+  enterMutex();
+  zErr = dlerror();
+  if( zErr ){
+    sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
+  }
+  leaveMutex();
+}
+static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
   return dlsym(pHandle, zSymbol);
 }
-SQLITE_PRIVATE int sqlite3UnixDlclose(void *pHandle){
-  return dlclose(pHandle);
-}
-#endif /* SQLITE_OMIT_LOAD_EXTENSION */
-
-/*
-** Get information to seed the random number generator.  The seed
-** is written into the buffer zBuf[256].  The calling function must
-** supply a sufficiently large buffer.
-*/
-SQLITE_PRIVATE int sqlite3UnixRandomSeed(char *zBuf){
+static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
+  dlclose(pHandle);
+}
+#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
+  #define unixDlOpen  0
+  #define unixDlError 0
+  #define unixDlSym   0
+  #define unixDlClose 0
+#endif
+
+/*
+** Write nBuf bytes of random data to the supplied buffer zBuf.
+*/
+static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
+
+  assert(nBuf>=(sizeof(time_t)+sizeof(int)));
+
   /* We have to initialize zBuf to prevent valgrind from reporting
   ** errors.  The reports issued by valgrind are incorrect - we would
   ** prefer that the randomness be increased by making use of the
   ** uninitialized space in zBuf - but valgrind errors tend to worry
@@ -15574,9 +17172,9 @@
   ** When testing, initializing zBuf[] to zero is all we do.  That means
   ** that we always use the same random number sequence.  This makes the
   ** tests repeatable.
   */
-  memset(zBuf, 0, 256);
+  memset(zBuf, 0, nBuf);
 #if !defined(SQLITE_TEST)
   {
     int pid, fd;
     fd = open("/dev/urandom", O_RDONLY);
@@ -15584,236 +17182,35 @@
       time_t t;
       time(&t);
       memcpy(zBuf, &t, sizeof(t));
       pid = getpid();
-      memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
-    }else{
-      read(fd, zBuf, 256);
+      memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
+    }else{
+      read(fd, zBuf, nBuf);
       close(fd);
     }
   }
 #endif
   return SQLITE_OK;
 }
 
+
 /*
 ** Sleep for a little while.  Return the amount of time slept.
-** The argument is the number of milliseconds we want to sleep.
-*/
-SQLITE_PRIVATE int sqlite3UnixSleep(int ms){
+** The argument is the number of microseconds we want to sleep.
+** The return value is the number of microseconds of sleep actually
+** requested from the underlying operating system, a number which
+** might be greater than or equal to the argument, but not less
+** than the argument.
+*/
+static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
 #if defined(HAVE_USLEEP) && HAVE_USLEEP
-  usleep(ms*1000);
-  return ms;
-#else
-  sleep((ms+999)/1000);
-  return 1000*((ms+999)/1000);
-#endif
-}
-
-/*
-** Static variables used for thread synchronization.
-**
-** inMutex      the nesting depth of the recursive mutex.  The thread
-**              holding mutexMain can read this variable at any time.
-**              But is must hold mutexAux to change this variable.  Other
-**              threads must hold mutexAux to read the variable and can
-**              never write.
-**
-** mutexOwner   The thread id of the thread holding mutexMain.  Same
-**              access rules as for inMutex.
-**
-** mutexOwnerValid   True if the value in mutexOwner is valid.  The same
-**                   access rules apply as for inMutex.
-**
-** mutexMain    The main mutex.  Hold this mutex in order to get exclusive
-**              access to SQLite data structures.
-**
-** mutexAux     An auxiliary mutex needed to access variables defined above.
-**
-** Mutexes are always acquired in this order: mutexMain mutexAux.   It
-** is not necessary to acquire mutexMain in order to get mutexAux - just
-** do not attempt to acquire them in the reverse order: mutexAux mutexMain.
-** Either get the mutexes with mutexMain first or get mutexAux only.
-**
-** When running on a platform where the three variables inMutex, mutexOwner,
-** and mutexOwnerValid can be set atomically, the mutexAux is not required.
-** On many systems, all three are 32-bit integers and writing to a 32-bit
-** integer is atomic.  I think.  But there are no guarantees.  So it seems
-** safer to protect them using mutexAux.
-*/
-static int inMutex = 0;
-#ifdef SQLITE_UNIX_THREADS
-static pthread_t mutexOwner;          /* Thread holding mutexMain */
-static int mutexOwnerValid = 0;       /* True if mutexOwner is valid */
-static pthread_mutex_t mutexMain = PTHREAD_MUTEX_INITIALIZER; /* The mutex */
-static pthread_mutex_t mutexAux = PTHREAD_MUTEX_INITIALIZER;  /* Aux mutex */
-#endif
-
-/*
-** The following pair of routine implement mutual exclusion for
-** multi-threaded processes.  Only a single thread is allowed to
-** executed code that is surrounded by EnterMutex() and LeaveMutex().
-**
-** SQLite uses only a single Mutex.  There is not much critical
-** code and what little there is executes quickly and without blocking.
-**
-** As of version 3.3.2, this mutex must be recursive.
-*/
-SQLITE_PRIVATE void sqlite3UnixEnterMutex(){
-#ifdef SQLITE_UNIX_THREADS
-  pthread_mutex_lock(&mutexAux);
-  if( !mutexOwnerValid || !pthread_equal(mutexOwner, pthread_self()) ){
-    pthread_mutex_unlock(&mutexAux);
-    pthread_mutex_lock(&mutexMain);
-    assert( inMutex==0 );
-    assert( !mutexOwnerValid );
-    pthread_mutex_lock(&mutexAux);
-    mutexOwner = pthread_self();
-    mutexOwnerValid = 1;
-  }
-  inMutex++;
-  pthread_mutex_unlock(&mutexAux);
-#else
-  inMutex++;
-#endif
-}
-SQLITE_PRIVATE void sqlite3UnixLeaveMutex(){
-  assert( inMutex>0 );
-#ifdef SQLITE_UNIX_THREADS
-  pthread_mutex_lock(&mutexAux);
-  inMutex--;
-  assert( pthread_equal(mutexOwner, pthread_self()) );
-  if( inMutex==0 ){
-    assert( mutexOwnerValid );
-    mutexOwnerValid = 0;
-    pthread_mutex_unlock(&mutexMain);
-  }
-  pthread_mutex_unlock(&mutexAux);
-#else
-  inMutex--;
-#endif
-}
-
-/*
-** Return TRUE if the mutex is currently held.
-**
-** If the thisThrd parameter is true, return true only if the
-** calling thread holds the mutex.  If the parameter is false, return
-** true if any thread holds the mutex.
-*/
-SQLITE_PRIVATE int sqlite3UnixInMutex(int thisThrd){
-#ifdef SQLITE_UNIX_THREADS
-  int rc;
-  pthread_mutex_lock(&mutexAux);
-  rc = inMutex>0 && (thisThrd==0 || pthread_equal(mutexOwner,pthread_self()));
-  pthread_mutex_unlock(&mutexAux);
-  return rc;
-#else
-  return inMutex>0;
-#endif
-}
-
-/*
-** Remember the number of thread-specific-data blocks allocated.
-** Use this to verify that we are not leaking thread-specific-data.
-** Ticket #1601
-*/
-#ifdef SQLITE_TEST
-int sqlite3_tsd_count = 0;
-# ifdef SQLITE_UNIX_THREADS
-    static pthread_mutex_t tsd_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
-#   define TSD_COUNTER(N) \
-             pthread_mutex_lock(&tsd_counter_mutex); \
-             sqlite3_tsd_count += N; \
-             pthread_mutex_unlock(&tsd_counter_mutex);
-# else
-#   define TSD_COUNTER(N)  sqlite3_tsd_count += N
-# endif
-#else
-# define TSD_COUNTER(N)  /* no-op */
-#endif
-
-/*
-** If called with allocateFlag>0, then return a pointer to thread
-** specific data for the current thread.  Allocate and zero the
-** thread-specific data if it does not already exist.
-**
-** If called with allocateFlag==0, then check the current thread
-** specific data.  Return it if it exists.  If it does not exist,
-** then return NULL.
-**
-** If called with allocateFlag<0, check to see if the thread specific
-** data is allocated and is all zero.  If it is then deallocate it.
-** Return a pointer to the thread specific data or NULL if it is
-** unallocated or gets deallocated.
-*/
-SQLITE_PRIVATE ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){
-  static const ThreadData zeroData = {0};  /* Initializer to silence warnings
-                                           ** from broken compilers */
-#ifdef SQLITE_UNIX_THREADS
-  static pthread_key_t key;
-  static int keyInit = 0;
-  ThreadData *pTsd;
-
-  if( !keyInit ){
-    sqlite3OsEnterMutex();
-    if( !keyInit ){
-      int rc;
-      rc = pthread_key_create(&key, 0);
-      if( rc ){
-        sqlite3OsLeaveMutex();
-        return 0;
-      }
-      keyInit = 1;
-    }
-    sqlite3OsLeaveMutex();
-  }
-
-  pTsd = pthread_getspecific(key);
-  if( allocateFlag>0 ){
-    if( pTsd==0 ){
-      if( !sqlite3TestMallocFail() ){
-        pTsd = sqlite3OsMalloc(sizeof(zeroData));
-      }
-#ifdef SQLITE_MEMDEBUG
-      sqlite3_isFail = 0;
-#endif
-      if( pTsd ){
-        *pTsd = zeroData;
-        pthread_setspecific(key, pTsd);
-        TSD_COUNTER(+1);
-      }
-    }
-  }else if( pTsd!=0 && allocateFlag<0
-            && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
-    sqlite3OsFree(pTsd);
-    pthread_setspecific(key, 0);
-    TSD_COUNTER(-1);
-    pTsd = 0;
-  }
-  return pTsd;
-#else
-  static ThreadData *pTsd = 0;
-  if( allocateFlag>0 ){
-    if( pTsd==0 ){
-      if( !sqlite3TestMallocFail() ){
-        pTsd = sqlite3OsMalloc( sizeof(zeroData) );
-      }
-#ifdef SQLITE_MEMDEBUG
-      sqlite3_isFail = 0;
-#endif
-      if( pTsd ){
-        *pTsd = zeroData;
-        TSD_COUNTER(+1);
-      }
-    }
-  }else if( pTsd!=0 && allocateFlag<0
-            && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
-    sqlite3OsFree(pTsd);
-    TSD_COUNTER(-1);
-    pTsd = 0;
-  }
-  return pTsd;
+  usleep(microseconds);
+  return microseconds;
+#else
+  int seconds = (microseconds+999999)/1000000;
+  sleep(seconds);
+  return seconds*1000000;
 #endif
 }
 
 /*
@@ -15820,17 +17217,17 @@
 ** The following variable, if set to a non-zero value, becomes the result
 ** returned from sqlite3OsCurrentTime().  This is used for testing.
 */
 #ifdef SQLITE_TEST
-int sqlite3_current_time = 0;
+SQLITE_API int sqlite3_current_time = 0;
 #endif
 
 /*
 ** Find the current time (in Universal Coordinated Time).  Write the
 ** current time and date as a Julian Day number into *prNow and
 ** return 0.  Return 1 if the time and date cannot be found.
 */
-SQLITE_PRIVATE int sqlite3UnixCurrentTime(double *prNow){
+static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
 #ifdef NO_GETTOD
   time_t t;
   time(&t);
   *prNow = t/86400.0 + 2440587.5;
@@ -15844,8 +17241,40 @@
     *prNow = sqlite3_current_time/86400.0 + 2440587.5;
   }
 #endif
   return 0;
+}
+
+/*
+** Return a pointer to the sqlite3DefaultVfs structure.   We use
+** a function rather than give the structure global scope because
+** some compilers (MSVC) do not allow forward declarations of
+** initialized structures.
+*/
+SQLITE_PRIVATE sqlite3_vfs *sqlite3OsDefaultVfs(void){
+  static sqlite3_vfs unixVfs = {
+    1,                  /* iVersion */
+    sizeof(unixFile),   /* szOsFile */
+    MAX_PATHNAME,       /* mxPathname */
+    0,                  /* pNext */
+    "unix",             /* zName */
+    0,                  /* pAppData */
+
+    unixOpen,           /* xOpen */
+    unixDelete,         /* xDelete */
+    unixAccess,         /* xAccess */
+    unixGetTempName,    /* xGetTempName */
+    unixFullPathname,   /* xFullPathname */
+    unixDlOpen,         /* xDlOpen */
+    unixDlError,        /* xDlError */
+    unixDlSym,          /* xDlSym */
+    unixDlClose,        /* xDlClose */
+    unixRandomness,     /* xRandomness */
+    unixSleep,          /* xSleep */
+    unixCurrentTime     /* xCurrentTime */
+  };
+
+  return &unixVfs;
 }
 
 #endif /* OS_UNIX */
 
@@ -15865,8 +17294,35 @@
 **
 ** This file contains code that is specific to windows.
 */
 #if OS_WIN               /* This file is used for windows only */
+
+
+/*
+** A Note About Memory Allocation:
+**
+** This driver uses malloc()/free() directly rather than going through
+** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
+** are designed for use on embedded systems where memory is scarce and
+** malloc failures happen frequently.  Win32 does not typically run on
+** embedded systems, and when it does the developers normally have bigger
+** problems to worry about than running out of memory.  So there is not
+** a compelling need to use the wrappers.
+**
+** But there is a good reason to not use the wrappers.  If we use the
+** wrappers then we will get simulated malloc() failures within this
+** driver.  And that causes all kinds of problems for our tests.  We
+** could enhance SQLite to deal with simulated malloc failures within
+** the OS driver, but the code to deal with those failure would not
+** be exercised on Linux (which does not need to malloc() in the driver)
+** and so we would have difficulty writing coverage tests for that
+** code.  Better to leave the code out, we think.
+**
+** The point of this discussion is as follows:  When creating a new
+** OS layer for an embedded system, if you use this file as an example,
+** avoid the use of malloc()/free().  Those routines work ok on windows
+** desktops but not so well in embedded systems.
+*/
 
 #include <winbase.h>
 
 #ifdef __CYGWIN__
@@ -15919,13 +17375,13 @@
  * When testing, this global variable stores the location of the
  * pending-byte in the database file.
  */
 #ifdef SQLITE_TEST
-unsigned int sqlite3_pending_byte = 0x40000000;
-#endif
-
-int sqlite3_os_trace = 0;
-#ifdef SQLITE_DEBUG
+SQLITE_API unsigned int sqlite3_pending_byte = 0x40000000;
+#endif
+
+#ifdef SQLITE_DEBUG
+SQLITE_API int sqlite3_os_trace = 0;
 #define OSTRACE1(X)         if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
 #define OSTRACE2(X,Y)       if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
 #define OSTRACE3(X,Y,Z)     if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
 #define OSTRACE4(X,Y,Z,A)   if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
@@ -15972,13 +17428,13 @@
 ** of code will give us the ability to simulate a disk I/O error.  This
 ** is used for testing the I/O recovery logic.
 */
 #ifdef SQLITE_TEST
-int sqlite3_io_error_hit = 0;
-int sqlite3_io_error_pending = 0;
-int sqlite3_io_error_persist = 0;
-int sqlite3_diskfull_pending = 0;
-int sqlite3_diskfull = 0;
+SQLITE_API int sqlite3_io_error_hit = 0;
+SQLITE_API int sqlite3_io_error_pending = 0;
+SQLITE_API int sqlite3_io_error_persist = 0;
+SQLITE_API int sqlite3_diskfull_pending = 0;
+SQLITE_API int sqlite3_diskfull = 0;
 #define SimulateIOError(CODE)  \
   if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \
      if( sqlite3_io_error_pending-- == 1 \
          || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \
@@ -16006,83 +17462,12 @@
 /*
 ** When testing, keep a count of the number of open files.
 */
 #ifdef SQLITE_TEST
-int sqlite3_open_file_count = 0;
+SQLITE_API int sqlite3_open_file_count = 0;
 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
 #else
 #define OpenCounter(X)
-#endif
-
-/*
-** sqlite3GenericMalloc
-** sqlite3GenericRealloc
-** sqlite3GenericOsFree
-** sqlite3GenericAllocationSize
-**
-** Implementation of the os level dynamic memory allocation interface in terms
-** of the standard malloc(), realloc() and free() found in many operating
-** systems. No rocket science here.
-**
-** There are two versions of these four functions here. The version
-** implemented here is only used if memory-management or memory-debugging is
-** enabled. This version allocates an extra 8-bytes at the beginning of each
-** block and stores the size of the allocation there.
-**
-** If neither memory-management or debugging is enabled, the second
-** set of implementations is used instead.
-*/
-#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || defined (SQLITE_MEMDEBUG)
-SQLITE_PRIVATE void *sqlite3GenericMalloc(int n){
-  char *p = (char *)malloc(n+8);
-  assert(n>0);
-  assert(sizeof(int)<=8);
-  if( p ){
-    *(int *)p = n;
-    p += 8;
-  }
-  return (void *)p;
-}
-SQLITE_PRIVATE void *sqlite3GenericRealloc(void *p, int n){
-  char *p2 = ((char *)p - 8);
-  assert(n>0);
-  p2 = (char*)realloc(p2, n+8);
-  if( p2 ){
-    *(int *)p2 = n;
-    p2 += 8;
-  }
-  return (void *)p2;
-}
-SQLITE_PRIVATE void sqlite3GenericFree(void *p){
-  assert(p);
-  free((void *)((char *)p - 8));
-}
-SQLITE_PRIVATE int sqlite3GenericAllocationSize(void *p){
-  return p ? *(int *)((char *)p - 8) : 0;
-}
-#else
-SQLITE_PRIVATE void *sqlite3GenericMalloc(int n){
-  char *p = (char *)malloc(n);
-  return (void *)p;
-}
-SQLITE_PRIVATE void *sqlite3GenericRealloc(void *p, int n){
-  assert(n>0);
-  p = realloc(p, n);
-  return p;
-}
-SQLITE_PRIVATE void sqlite3GenericFree(void *p){
-  assert(p);
-  free(p);
-}
-/* Never actually used, but needed for the linker */
-SQLITE_PRIVATE int sqlite3GenericAllocationSize(void *p){ return 0; }
-#endif
-
-/*
-** The default size of a disk sector
-*/
-#ifndef PAGER_SECTOR_SIZE
-# define PAGER_SECTOR_SIZE 512
 #endif
 
 /************** End of os_common.h *******************************************/
 /************** Continuing where we left off in os_win.c *********************/
@@ -16111,14 +17496,14 @@
 } winceLock;
 #endif
 
 /*
-** The winFile structure is a subclass of OsFile specific to the win32
+** The winFile structure is a subclass of sqlite3_file* specific to the win32
 ** portability layer.
 */
 typedef struct winFile winFile;
 struct winFile {
-  IoMethod const *pMethod;/* Must be first */
+  const sqlite3_io_methods *pMethod;/* Must be first */
   HANDLE h;               /* Handle for accessing the file */
   unsigned char locktype; /* Type of lock currently held on this file */
   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
 #if OS_WINCE
@@ -16131,15 +17516,8 @@
 };
 
 
 /*
-** Do not include any of the File I/O interface procedures if the
-** SQLITE_OMIT_DISKIO macro is defined (indicating that there database
-** will be in-memory only)
-*/
-#ifndef SQLITE_OMIT_DISKIO
-
-/*
 ** The following variable is (normally) set once and never changes
 ** thereafter.  It records whether the operating system is Win95
 ** or WinNT.
 **
@@ -16149,9 +17527,13 @@
 **
 ** In order to facilitate testing on a WinNT system, the test fixture
 ** can manually set this value to 1 to emulate Win98 behavior.
 */
-int sqlite3_os_type = 0;
+#ifdef SQLITE_TEST
+SQLITE_API int sqlite3_os_type = 0;
+#else
+static int sqlite3_os_type = 0;
+#endif
 
 /*
 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
@@ -16179,44 +17561,44 @@
 
 /*
 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
 **
-** Space to hold the returned string is obtained from sqliteMalloc.
+** Space to hold the returned string is obtained from malloc.
 */
 static WCHAR *utf8ToUnicode(const char *zFilename){
   int nChar;
   WCHAR *zWideFilename;
 
   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
-  zWideFilename = sqliteMalloc( nChar*sizeof(zWideFilename[0]) );
+  zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
   if( zWideFilename==0 ){
     return 0;
   }
   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
   if( nChar==0 ){
-    sqliteFree(zWideFilename);
+    free(zWideFilename);
     zWideFilename = 0;
   }
   return zWideFilename;
 }
 
 /*
 ** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
-** obtained from sqliteMalloc().
+** obtained from malloc().
 */
 static char *unicodeToUtf8(const WCHAR *zWideFilename){
   int nByte;
   char *zFilename;
 
   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
-  zFilename = sqliteMalloc( nByte );
+  zFilename = malloc( nByte );
   if( zFilename==0 ){
     return 0;
   }
   nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
                               0, 0);
   if( nByte == 0 ){
-    sqliteFree(zFilename);
+    free(zFilename);
     zFilename = 0;
   }
   return zFilename;
 }
@@ -16225,23 +17607,23 @@
 ** Convert an ansi string to microsoft unicode, based on the
 ** current codepage settings for file apis.
 **
 ** Space to hold the returned string is obtained
-** from sqliteMalloc.
+** from malloc.
 */
 static WCHAR *mbcsToUnicode(const char *zFilename){
   int nByte;
   WCHAR *zMbcsFilename;
   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
 
   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
-  zMbcsFilename = sqliteMalloc( nByte*sizeof(zMbcsFilename[0]) );
+  zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
   if( zMbcsFilename==0 ){
     return 0;
   }
   nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
   if( nByte==0 ){
-    sqliteFree(zMbcsFilename);
+    free(zMbcsFilename);
     zMbcsFilename = 0;
   }
   return zMbcsFilename;
 }
@@ -16250,32 +17632,32 @@
 ** Convert microsoft unicode to multibyte character string, based on the
 ** user's Ansi codepage.
 **
 ** Space to hold the returned string is obtained from
-** sqliteMalloc().
+** malloc().
 */
 static char *unicodeToMbcs(const WCHAR *zWideFilename){
   int nByte;
   char *zFilename;
   int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
 
   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
-  zFilename = sqliteMalloc( nByte );
+  zFilename = malloc( nByte );
   if( zFilename==0 ){
     return 0;
   }
   nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
                               0, 0);
   if( nByte == 0 ){
-    sqliteFree(zFilename);
+    free(zFilename);
     zFilename = 0;
   }
   return zFilename;
 }
 
 /*
 ** Convert multibyte character string to UTF-8.  Space to hold the
-** returned string is obtained from sqliteMalloc().
+** returned string is obtained from malloc().
 */
 static char *mbcsToUtf8(const char *zFilename){
   char *zFilenameUtf8;
   WCHAR *zTmpWide;
@@ -16284,15 +17666,15 @@
   if( zTmpWide==0 ){
     return 0;
   }
   zFilenameUtf8 = unicodeToUtf8(zTmpWide);
-  sqliteFree(zTmpWide);
+  free(zTmpWide);
   return zFilenameUtf8;
 }
 
 /*
 ** Convert UTF-8 to multibyte character string.  Space to hold the
-** returned string is obtained from sqliteMalloc().
+** returned string is obtained from malloc().
 */
 static char *utf8ToMbcs(const char *zFilename){
   char *zFilenameMbcs;
   WCHAR *zTmpWide;
@@ -16301,9 +17683,9 @@
   if( zTmpWide==0 ){
     return 0;
   }
   zFilenameMbcs = unicodeToMbcs(zTmpWide);
-  sqliteFree(zTmpWide);
+  free(zTmpWide);
   return zFilenameMbcs;
 }
 
 #if OS_WINCE
@@ -16380,9 +17762,9 @@
 
   /* Create/open the named mutex */
   pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
   if (!pFile->hMutex){
-    sqliteFree(zName);
+    free(zName);
     return FALSE;
   }
 
   /* Acquire the mutex before continuing */
@@ -16402,9 +17784,9 @@
   if (GetLastError() == ERROR_ALREADY_EXISTS){
     bInit = FALSE;
   }
 
-  sqliteFree(zName);
+  free(zName);
 
   /* If we succeeded in making the shared memory handle, map it. */
   if (pFile->hShared){
     pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
@@ -16461,9 +17843,9 @@
     CloseHandle(pFile->hShared);
 
     if( pFile->zDeleteOnClose ){
       DeleteFileW(pFile->zDeleteOnClose);
-      sqliteFree(pFile->zDeleteOnClose);
+      free(pFile->zDeleteOnClose);
       pFile->zDeleteOnClose = 0;
     }
 
     /* Done with the mutex */
@@ -16616,624 +17998,213 @@
 ** End of the special code for wince
 *****************************************************************************/
 #endif /* OS_WINCE */
 
-/*
-** Convert a UTF-8 filename into whatever form the underlying
-** operating system wants filenames in.  Space to hold the result
-** is obtained from sqliteMalloc and must be freed by the calling
-** function.
-*/
-static void *convertUtf8Filename(const char *zFilename){
-  void *zConverted = 0;
-  if( isNT() ){
-    zConverted = utf8ToUnicode(zFilename);
-  }else{
-    zConverted = utf8ToMbcs(zFilename);
-  }
-  /* caller will handle out of memory */
-  return zConverted;
-}
-
-/*
-** Delete the named file.
-**
-** Note that windows does not allow a file to be deleted if some other
-** process has it open.  Sometimes a virus scanner or indexing program
-** will open a journal file shortly after it is created in order to do
-** whatever it is it does.  While this other process is holding the
-** file open, we will be unable to delete it.  To work around this
-** problem, we delay 100 milliseconds and try to delete again.  Up
-** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
-** up and returning an error.
-*/
-#define MX_DELETION_ATTEMPTS 3
-SQLITE_PRIVATE int sqlite3WinDelete(const char *zFilename){
-  int cnt = 0;
-  int rc;
-  void *zConverted = convertUtf8Filename(zFilename);
-  if( zConverted==0 ){
-    return SQLITE_NOMEM;
-  }
-  SimulateIOError(return SQLITE_IOERR_DELETE);
-  if( isNT() ){
-    do{
-      rc = DeleteFileW(zConverted);
-    }while( rc==0 && GetFileAttributesW(zConverted)!=0xffffffff
-            && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
-  }else{
+/*****************************************************************************
+** The next group of routines implement the I/O methods specified
+** by the sqlite3_io_methods object.
+******************************************************************************/
+
+/*
+** Close a file.
+**
+** It is reported that an attempt to close a handle might sometimes
+** fail.  This is a very unreasonable result, but windows is notorious
+** for being unreasonable so I do not doubt that it might happen.  If
+** the close fails, we pause for 100 milliseconds and try again.  As
+** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
+** giving up and returning an error.
+*/
+#define MX_CLOSE_ATTEMPT 3
+static int winClose(sqlite3_file *id){
+  int rc, cnt = 0;
+  winFile *pFile = (winFile*)id;
+  OSTRACE2("CLOSE %d\n", pFile->h);
+  do{
+    rc = CloseHandle(pFile->h);
+  }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
 #if OS_WINCE
-    return SQLITE_NOMEM;
-#else
-    do{
-      rc = DeleteFileA(zConverted);
-    }while( rc==0 && GetFileAttributesA(zConverted)!=0xffffffff
-            && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
-#endif
-  }
-  sqliteFree(zConverted);
-  OSTRACE2("DELETE \"%s\"\n", zFilename);
-  return rc!=0 ? SQLITE_OK : SQLITE_IOERR;
-}
-
-/*
-** Return TRUE if the named file exists.
-*/
-SQLITE_PRIVATE int sqlite3WinFileExists(const char *zFilename){
-  int exists = 0;
-  void *zConverted = convertUtf8Filename(zFilename);
-  if( zConverted==0 ){
-    return SQLITE_NOMEM;
-  }
-  if( isNT() ){
-    exists = GetFileAttributesW((WCHAR*)zConverted) != 0xffffffff;
-  }else{
-#if OS_WINCE
-    return SQLITE_NOMEM;
-#else
-    exists = GetFileAttributesA((char*)zConverted) != 0xffffffff;
-#endif
-  }
-  sqliteFree(zConverted);
-  return exists;
-}
-
-/* Forward declaration */
-static int allocateWinFile(winFile *pInit, OsFile **pId);
-
-/*
-** Attempt to open a file for both reading and writing.  If that
-** fails, try opening it read-only.  If the file does not exist,
-** try to create it.
-**
-** On success, a handle for the open file is written to *id
-** and *pReadonly is set to 0 if the file was opened for reading and
-** writing or 1 if the file was opened read-only.  The function returns
-** SQLITE_OK.
-**
-** On failure, the function returns SQLITE_CANTOPEN and leaves
-** *id and *pReadonly unchanged.
-*/
-SQLITE_PRIVATE int sqlite3WinOpenReadWrite(
-  const char *zFilename,
-  OsFile **pId,
-  int *pReadonly
-){
-  winFile f;
-  HANDLE h;
-  void *zConverted = convertUtf8Filename(zFilename);
-  if( zConverted==0 ){
-    return SQLITE_NOMEM;
-  }
-  assert( *pId==0 );
-
+  winceDestroyLock(pFile);
+#endif
+  OpenCounter(-1);
+  return rc ? SQLITE_OK : SQLITE_IOERR;
+}
+
+/*
+** Some microsoft compilers lack this definition.
+*/
+#ifndef INVALID_SET_FILE_POINTER
+# define INVALID_SET_FILE_POINTER ((DWORD)-1)
+#endif
+
+/*
+** Read data from a file into a buffer.  Return SQLITE_OK if all
+** bytes were read successfully and SQLITE_IOERR if anything goes
+** wrong.
+*/
+static int winRead(
+  sqlite3_file *id,          /* File to read from */
+  void *pBuf,                /* Write content into this buffer */
+  int amt,                   /* Number of bytes to read */
+  sqlite3_int64 offset       /* Begin reading at this offset */
+){
+  LONG upperBits = (offset>>32) & 0x7fffffff;
+  LONG lowerBits = offset & 0xffffffff;
+  DWORD rc;
+  DWORD got;
+  winFile *pFile = (winFile*)id;
+  assert( id!=0 );
+  SimulateIOError(return SQLITE_IOERR_READ);
+  OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype);
+  rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
+  if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
+    return SQLITE_FULL;
+  }
+  if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){
+    return SQLITE_IOERR_READ;
+  }
+  if( got==(DWORD)amt ){
+    return SQLITE_OK;
+  }else{
+    memset(&((char*)pBuf)[got], 0, amt-got);
+    return SQLITE_IOERR_SHORT_READ;
+  }
+}
+
+/*
+** Write data from a buffer into a file.  Return SQLITE_OK on success
+** or some other error code on failure.
+*/
+static int winWrite(
+  sqlite3_file *id,         /* File to write into */
+  const void *pBuf,         /* The bytes to be written */
+  int amt,                  /* Number of bytes to write */
+  sqlite3_int64 offset      /* Offset into the file to begin writing at */
+){
+  LONG upperBits = (offset>>32) & 0x7fffffff;
+  LONG lowerBits = offset & 0xffffffff;
+  DWORD rc;
+  DWORD wrote;
+  winFile *pFile = (winFile*)id;
+  assert( id!=0 );
+  SimulateIOError(return SQLITE_IOERR_WRITE);
+  SimulateDiskfullError(return SQLITE_FULL);
+  OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype);
+  rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
+  if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
+    return SQLITE_FULL;
+  }
+  assert( amt>0 );
+  while(
+     amt>0
+     && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0
+     && wrote>0
+  ){
+    amt -= wrote;
+    pBuf = &((char*)pBuf)[wrote];
+  }
+  if( !rc || amt>(int)wrote ){
+    return SQLITE_FULL;
+  }
+  return SQLITE_OK;
+}
+
+/*
+** Truncate an open file to a specified size
+*/
+static int winTruncate(sqlite3_file *id, i64 nByte){
+  LONG upperBits = (nByte>>32) & 0x7fffffff;
+  LONG lowerBits = nByte & 0xffffffff;
+  winFile *pFile = (winFile*)id;
+  OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte);
+  SimulateIOError(return SQLITE_IOERR_TRUNCATE);
+  SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
+  SetEndOfFile(pFile->h);
+  return SQLITE_OK;
+}
+
+#ifdef SQLITE_TEST
+/*
+** Count the number of fullsyncs and normal syncs.  This is used to test
+** that syncs and fullsyncs are occuring at the right times.
+*/
+SQLITE_API int sqlite3_sync_count = 0;
+SQLITE_API int sqlite3_fullsync_count = 0;
+#endif
+
+/*
+** Make sure all writes to a particular file are committed to disk.
+*/
+static int winSync(sqlite3_file *id, int flags){
+  winFile *pFile = (winFile*)id;
+  OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype);
+#ifdef SQLITE_TEST
+  if( flags & SQLITE_SYNC_FULL ){
+    sqlite3_fullsync_count++;
+  }
+  sqlite3_sync_count++;
+#endif
+  if( FlushFileBuffers(pFile->h) ){
+    return SQLITE_OK;
+  }else{
+    return SQLITE_IOERR;
+  }
+}
+
+/*
+** Determine the current size of a file in bytes
+*/
+static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
+  winFile *pFile = (winFile*)id;
+  DWORD upperBits, lowerBits;
+  SimulateIOError(return SQLITE_IOERR_FSTAT);
+  lowerBits = GetFileSize(pFile->h, &upperBits);
+  *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
+  return SQLITE_OK;
+}
+
+/*
+** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
+*/
+#ifndef LOCKFILE_FAIL_IMMEDIATELY
+# define LOCKFILE_FAIL_IMMEDIATELY 1
+#endif
+
+/*
+** Acquire a reader lock.
+** Different API routines are called depending on whether or not this
+** is Win95 or WinNT.
+*/
+static int getReadLock(winFile *pFile){
+  int res;
   if( isNT() ){
-    h = CreateFileW((WCHAR*)zConverted,
-       GENERIC_READ | GENERIC_WRITE,
-       FILE_SHARE_READ | FILE_SHARE_WRITE,
-       NULL,
-       OPEN_ALWAYS,
-       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
-       NULL
-    );
-    if( h==INVALID_HANDLE_VALUE ){
-      h = CreateFileW((WCHAR*)zConverted,
-         GENERIC_READ,
-         FILE_SHARE_READ | FILE_SHARE_WRITE,
-         NULL,
-         OPEN_ALWAYS,
-         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
-         NULL
-      );
-      if( h==INVALID_HANDLE_VALUE ){
-        sqliteFree(zConverted);
-        return SQLITE_CANTOPEN;
-      }
-      *pReadonly = 1;
-    }else{
-      *pReadonly = 0;
-    }
-#if OS_WINCE
-    if (!winceCreateLock(zFilename, &f)){
-      CloseHandle(h);
-      sqliteFree(zConverted);
-      return SQLITE_CANTOPEN;
-    }
-#endif
-  }else{
-#if OS_WINCE
-    return SQLITE_NOMEM;
-#else
-    h = CreateFileA((char*)zConverted,
-       GENERIC_READ | GENERIC_WRITE,
-       FILE_SHARE_READ | FILE_SHARE_WRITE,
-       NULL,
-       OPEN_ALWAYS,
-       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
-       NULL
-    );
-    if( h==INVALID_HANDLE_VALUE ){
-      h = CreateFileA((char*)zConverted,
-         GENERIC_READ,
-         FILE_SHARE_READ | FILE_SHARE_WRITE,
-         NULL,
-         OPEN_ALWAYS,
-         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
-         NULL
-      );
-      if( h==INVALID_HANDLE_VALUE ){
-        sqliteFree(zConverted);
-        return SQLITE_CANTOPEN;
-      }
-      *pReadonly = 1;
-    }else{
-      *pReadonly = 0;
-    }
-#endif /* OS_WINCE */
-  }
-
-  sqliteFree(zConverted);
-
-  f.h = h;
-#if OS_WINCE
-  f.zDeleteOnClose = 0;
-#endif
-  OSTRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
-  return allocateWinFile(&f, pId);
-}
-
-
-/*
-** Attempt to open a new file for exclusive access by this process.
-** The file will be opened for both reading and writing.  To avoid
-** a potential security problem, we do not allow the file to have
-** previously existed.  Nor do we allow the file to be a symbolic
-** link.
-**
-** If delFlag is true, then make arrangements to automatically delete
-** the file when it is closed.
-**
-** On success, write the file handle into *id and return SQLITE_OK.
-**
-** On failure, return SQLITE_CANTOPEN.
-**
-** Sometimes if we have just deleted a prior journal file, windows
-** will fail to open a new one because there is a "pending delete".
-** To work around this bug, we pause for 100 milliseconds and attempt
-** a second open after the first one fails.  The whole operation only
-** fails if both open attempts are unsuccessful.
-*/
-SQLITE_PRIVATE int sqlite3WinOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){
-  winFile f;
-  HANDLE h;
-  DWORD fileflags;
-  void *zConverted = convertUtf8Filename(zFilename);
-  if( zConverted==0 ){
-    return SQLITE_NOMEM;
-  }
-  assert( *pId == 0 );
-  fileflags = FILE_FLAG_RANDOM_ACCESS;
-#if !OS_WINCE
-  if( delFlag ){
-    fileflags |= FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE;
-  }
-#endif
+    OVERLAPPED ovlp;
+    ovlp.Offset = SHARED_FIRST;
+    ovlp.OffsetHigh = 0;
+    ovlp.hEvent = 0;
+    res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
+                     0, SHARED_SIZE, 0, &ovlp);
+  }else{
+    int lk;
+    sqlite3Randomness(sizeof(lk), &lk);
+    pFile->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
+    res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
+  }
+  return res;
+}
+
+/*
+** Undo a readlock
+*/
+static int unlockReadLock(winFile *pFile){
+  int res;
   if( isNT() ){
-    int cnt = 0;
-    do{
-      h = CreateFileW((WCHAR*)zConverted,
-         GENERIC_READ | GENERIC_WRITE,
-         0,
-         NULL,
-         CREATE_ALWAYS,
-         fileflags,
-         NULL
-      );
-    }while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) );
-  }else{
-#if OS_WINCE
-    return SQLITE_NOMEM;
-#else
-    int cnt = 0;
-    do{
-      h = CreateFileA((char*)zConverted,
-        GENERIC_READ | GENERIC_WRITE,
-        0,
-        NULL,
-        CREATE_ALWAYS,
-        fileflags,
-        NULL
-      );
-    }while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) );
-#endif /* OS_WINCE */
-  }
-#if OS_WINCE
-  if( delFlag && h!=INVALID_HANDLE_VALUE ){
-    f.zDeleteOnClose = zConverted;
-    zConverted = 0;
-  }
-  f.hMutex = NULL;
-#endif
-  sqliteFree(zConverted);
-  if( h==INVALID_HANDLE_VALUE ){
-    return SQLITE_CANTOPEN;
-  }
-  f.h = h;
-  OSTRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
-  return allocateWinFile(&f, pId);
-}
-
-/*
-** Attempt to open a new file for read-only access.
-**
-** On success, write the file handle into *id and return SQLITE_OK.
-**
-** On failure, return SQLITE_CANTOPEN.
-*/
-SQLITE_PRIVATE int sqlite3WinOpenReadOnly(const char *zFilename, OsFile **pId){
-  winFile f;
-  HANDLE h;
-  void *zConverted = convertUtf8Filename(zFilename);
-  if( zConverted==0 ){
-    return SQLITE_NOMEM;
-  }
-  assert( *pId==0 );
-  if( isNT() ){
-    h = CreateFileW((WCHAR*)zConverted,
-       GENERIC_READ,
-       0,
-       NULL,
-       OPEN_EXISTING,
-       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
-       NULL
-    );
-  }else{
-#if OS_WINCE
-    return SQLITE_NOMEM;
-#else
-    h = CreateFileA((char*)zConverted,
-       GENERIC_READ,
-       0,
-       NULL,
-       OPEN_EXISTING,
-       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
-       NULL
-    );
-#endif
-  }
-  sqliteFree(zConverted);
-  if( h==INVALID_HANDLE_VALUE ){
-    return SQLITE_CANTOPEN;
-  }
-  f.h = h;
-#if OS_WINCE
-  f.zDeleteOnClose = 0;
-  f.hMutex = NULL;
-#endif
-  OSTRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
-  return allocateWinFile(&f, pId);
-}
-
-/*
-** Attempt to open a file descriptor for the directory that contains a
-** file.  This file descriptor can be used to fsync() the directory
-** in order to make sure the creation of a new file is actually written
-** to disk.
-**
-** This routine is only meaningful for Unix.  It is a no-op under
-** windows since windows does not support hard links.
-**
-** On success, a handle for a previously open file is at *id is
-** updated with the new directory file descriptor and SQLITE_OK is
-** returned.
-**
-** On failure, the function returns SQLITE_CANTOPEN and leaves
-** *id unchanged.
-*/
-static int winOpenDirectory(
-  OsFile *id,
-  const char *zDirname
-){
-  return SQLITE_OK;
-}
-
-/*
-** Create a temporary file name in zBuf.  zBuf must be big enough to
-** hold at least SQLITE_TEMPNAME_SIZE characters.
-*/
-SQLITE_PRIVATE int sqlite3WinTempFileName(char *zBuf){
-  static char zChars[] =
-    "abcdefghijklmnopqrstuvwxyz"
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-    "0123456789";
-  int i, j;
-  char zTempPath[SQLITE_TEMPNAME_SIZE];
-  if( sqlite3_temp_directory ){
-    strncpy(zTempPath, sqlite3_temp_directory, SQLITE_TEMPNAME_SIZE-30);
-    zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0;
-  }else if( isNT() ){
-    char *zMulti;
-    WCHAR zWidePath[SQLITE_TEMPNAME_SIZE];
-    GetTempPathW(SQLITE_TEMPNAME_SIZE-30, zWidePath);
-    zMulti = unicodeToUtf8(zWidePath);
-    if( zMulti ){
-      strncpy(zTempPath, zMulti, SQLITE_TEMPNAME_SIZE-30);
-      zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0;
-      sqliteFree(zMulti);
-    }else{
-      return SQLITE_NOMEM;
-    }
-  }else{
-    char *zUtf8;
-    char zMbcsPath[SQLITE_TEMPNAME_SIZE];
-    GetTempPathA(SQLITE_TEMPNAME_SIZE-30, zMbcsPath);
-    zUtf8 = mbcsToUtf8(zMbcsPath);
-    if( zUtf8 ){
-      strncpy(zTempPath, zUtf8, SQLITE_TEMPNAME_SIZE-30);
-      zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0;
-      sqliteFree(zUtf8);
-    }else{
-      return SQLITE_NOMEM;
-    }
-  }
-  for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
-  zTempPath[i] = 0;
-  for(;;){
-    sqlite3_snprintf(SQLITE_TEMPNAME_SIZE, zBuf,
-                     "%s\\"TEMP_FILE_PREFIX, zTempPath);
-    j = strlen(zBuf);
-    sqlite3Randomness(15, &zBuf[j]);
-    for(i=0; i<15; i++, j++){
-      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
-    }
-    zBuf[j] = 0;
-    if( !sqlite3OsFileExists(zBuf) ) break;
-  }
-  OSTRACE2("TEMP FILENAME: %s\n", zBuf);
-  return SQLITE_OK;
-}
-
-/*
-** Close a file.
-**
-** It is reported that an attempt to close a handle might sometimes
-** fail.  This is a very unreasonable result, but windows is notorious
-** for being unreasonable so I do not doubt that it might happen.  If
-** the close fails, we pause for 100 milliseconds and try again.  As
-** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
-** giving up and returning an error.
-*/
-#define MX_CLOSE_ATTEMPT 3
-static int winClose(OsFile **pId){
-  winFile *pFile;
-  int rc = 1;
-  if( pId && (pFile = (winFile*)*pId)!=0 ){
-    int rc, cnt = 0;
-    OSTRACE2("CLOSE %d\n", pFile->h);
-    do{
-      rc = CloseHandle(pFile->h);
-    }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
-#if OS_WINCE
-    winceDestroyLock(pFile);
-#endif
-    OpenCounter(-1);
-    sqliteFree(pFile);
-    *pId = 0;
-  }
-  return rc ? SQLITE_OK : SQLITE_IOERR;
-}
-
-/*
-** Read data from a file into a buffer.  Return SQLITE_OK if all
-** bytes were read successfully and SQLITE_IOERR if anything goes
-** wrong.
-*/
-static int winRead(OsFile *id, void *pBuf, int amt){
-  DWORD got;
-  assert( id!=0 );
-  SimulateIOError(return SQLITE_IOERR_READ);
-  OSTRACE3("READ %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
-  if( !ReadFile(((winFile*)id)->h, pBuf, amt, &got, 0) ){
-    return SQLITE_IOERR_READ;
-  }
-  if( got==(DWORD)amt ){
-    return SQLITE_OK;
-  }else{
-    memset(&((char*)pBuf)[got], 0, amt-got);
-    return SQLITE_IOERR_SHORT_READ;
-  }
-}
-
-/*
-** Write data from a buffer into a file.  Return SQLITE_OK on success
-** or some other error code on failure.
-*/
-static int winWrite(OsFile *id, const void *pBuf, int amt){
-  int rc = 0;
-  DWORD wrote;
-  assert( id!=0 );
-  SimulateIOError(return SQLITE_IOERR_READ);
-  SimulateDiskfullError(return SQLITE_FULL);
-  OSTRACE3("WRITE %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
-  assert( amt>0 );
-  while( amt>0 && (rc = WriteFile(((winFile*)id)->h, pBuf, amt, &wrote, 0))!=0
-         && wrote>0 ){
-    amt -= wrote;
-    pBuf = &((char*)pBuf)[wrote];
-  }
-  if( !rc || amt>(int)wrote ){
-    return SQLITE_FULL;
-  }
-  return SQLITE_OK;
-}
-
-/*
-** Some microsoft compilers lack this definition.
-*/
-#ifndef INVALID_SET_FILE_POINTER
-# define INVALID_SET_FILE_POINTER ((DWORD)-1)
-#endif
-
-/*
-** Move the read/write pointer in a file.
-*/
-static int winSeek(OsFile *id, i64 offset){
-  LONG upperBits = offset>>32;
-  LONG lowerBits = offset & 0xffffffff;
-  DWORD rc;
-  assert( id!=0 );
-#ifdef SQLITE_TEST
-  if( offset ) SimulateDiskfullError(return SQLITE_FULL);
-#endif
-  rc = SetFilePointer(((winFile*)id)->h, lowerBits, &upperBits, FILE_BEGIN);
-  OSTRACE3("SEEK %d %lld\n", ((winFile*)id)->h, offset);
-  if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
-    return SQLITE_FULL;
-  }
-  return SQLITE_OK;
-}
-
-/*
-** Make sure all writes to a particular file are committed to disk.
-*/
-static int winSync(OsFile *id, int dataOnly){
-  assert( id!=0 );
-  OSTRACE3("SYNC %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
-  if( FlushFileBuffers(((winFile*)id)->h) ){
-    return SQLITE_OK;
-  }else{
-    return SQLITE_IOERR;
-  }
-}
-
-/*
-** Sync the directory zDirname. This is a no-op on operating systems other
-** than UNIX.
-*/
-SQLITE_PRIVATE int sqlite3WinSyncDirectory(const char *zDirname){
-  SimulateIOError(return SQLITE_IOERR_READ);
-  return SQLITE_OK;
-}
-
-/*
-** Truncate an open file to a specified size
-*/
-static int winTruncate(OsFile *id, i64 nByte){
-  LONG upperBits = nByte>>32;
-  assert( id!=0 );
-  OSTRACE3("TRUNCATE %d %lld\n", ((winFile*)id)->h, nByte);
-  SimulateIOError(return SQLITE_IOERR_TRUNCATE);
-  SetFilePointer(((winFile*)id)->h, nByte, &upperBits, FILE_BEGIN);
-  SetEndOfFile(((winFile*)id)->h);
-  return SQLITE_OK;
-}
-
-/*
-** Determine the current size of a file in bytes
-*/
-static int winFileSize(OsFile *id, i64 *pSize){
-  DWORD upperBits, lowerBits;
-  assert( id!=0 );
-  SimulateIOError(return SQLITE_IOERR_FSTAT);
-  lowerBits = GetFileSize(((winFile*)id)->h, &upperBits);
-  *pSize = (((i64)upperBits)<<32) + lowerBits;
-  return SQLITE_OK;
-}
-
-/*
-** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
-*/
-#ifndef LOCKFILE_FAIL_IMMEDIATELY
-# define LOCKFILE_FAIL_IMMEDIATELY 1
-#endif
-
-/*
-** Acquire a reader lock.
-** Different API routines are called depending on whether or not this
-** is Win95 or WinNT.
-*/
-static int getReadLock(winFile *id){
-  int res;
-  if( isNT() ){
-    OVERLAPPED ovlp;
-    ovlp.Offset = SHARED_FIRST;
-    ovlp.OffsetHigh = 0;
-    ovlp.hEvent = 0;
-    res = LockFileEx(id->h, LOCKFILE_FAIL_IMMEDIATELY, 0, SHARED_SIZE,0,&ovlp);
-  }else{
-    int lk;
-    sqlite3Randomness(sizeof(lk), &lk);
-    id->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
-    res = LockFile(id->h, SHARED_FIRST+id->sharedLockByte, 0, 1, 0);
-  }
-  return res;
-}
-
-/*
-** Undo a readlock
-*/
-static int unlockReadLock(winFile *pFile){
-  int res;
-  if( isNT() ){
     res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   }else{
     res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
   }
   return res;
 }
-
-#ifndef SQLITE_OMIT_PAGER_PRAGMAS
-/*
-** Check that a given pathname is a directory and is writable
-**
-*/
-SQLITE_PRIVATE int sqlite3WinIsDirWritable(char *zDirname){
-  int fileAttr;
-  void *zConverted;
-  if( zDirname==0 ) return 0;
-  if( !isNT() && strlen(zDirname)>MAX_PATH ) return 0;
-
-  zConverted = convertUtf8Filename(zDirname);
-  if( zConverted==0 ){
-    return SQLITE_NOMEM;
-  }
-  if( isNT() ){
-    fileAttr = GetFileAttributesW((WCHAR*)zConverted);
-  }else{
-#if OS_WINCE
-    return 0;
-#else
-    fileAttr = GetFileAttributesA((char*)zConverted);
-#endif
-  }
-  sqliteFree(zConverted);
-  if( fileAttr == 0xffffffff ) return 0;
-  if( (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY ){
-    return 0;
-  }
-  return 1;
-}
-#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
 
 /*
 ** Lock the file with the lock specified by parameter locktype - one
 ** of the following:
@@ -17259,12 +18230,12 @@
 ** erases all locks at once and returns us immediately to locking level 0.
 ** It is not possible to lower the locking level one step at a time.  You
 ** must go straight to locking level 0.
 */
-static int winLock(OsFile *id, int locktype){
+static int winLock(sqlite3_file *id, int locktype){
   int rc = SQLITE_OK;    /* Return code from subroutines */
   int res = 1;           /* Result of a windows lock call */
-  int newLocktype;       /* Set id->locktype to this value before exiting */
+  int newLocktype;       /* Set pFile->locktype to this value before exiting */
   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
   winFile *pFile = (winFile*)id;
 
   assert( pFile!=0 );
@@ -17371,9 +18342,9 @@
 ** This routine checks if there is a RESERVED lock held on the specified
 ** file by this or any other process. If such a lock is held, return
 ** non-zero, otherwise zero.
 */
-static int winCheckReservedLock(OsFile *id){
+static int winCheckReservedLock(sqlite3_file *id){
   int rc;
   winFile *pFile = (winFile*)id;
   assert( pFile!=0 );
   if( pFile->locktype>=RESERVED_LOCK ){
@@ -17400,12 +18371,12 @@
 ** It is not possible for this routine to fail if the second argument
 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
 ** might return SQLITE_IOERR;
 */
-static int winUnlock(OsFile *id, int locktype){
+static int winUnlock(sqlite3_file *id, int locktype){
   int type;
-  int rc = SQLITE_OK;
   winFile *pFile = (winFile*)id;
+  int rc = SQLITE_OK;
   assert( pFile!=0 );
   assert( locktype<=SHARED_LOCK );
   OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
           pFile->locktype, pFile->sharedLockByte);
@@ -17431,77 +18402,18 @@
   return rc;
 }
 
 /*
-** Turn a relative pathname into a full pathname.  Return a pointer
-** to the full pathname stored in space obtained from sqliteMalloc().
-** The calling function is responsible for freeing this space once it
-** is no longer needed.
-*/
-SQLITE_PRIVATE char *sqlite3WinFullPathname(const char *zRelative){
-  char *zFull;
-#if defined(__CYGWIN__)
-  int nByte;
-  nByte = strlen(zRelative) + MAX_PATH + 1001;
-  zFull = sqliteMalloc( nByte );
-  if( zFull==0 ) return 0;
-  if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0;
-#elif OS_WINCE
-  /* WinCE has no concept of a relative pathname, or so I am told. */
-  zFull = sqliteStrDup(zRelative);
-#else
-  int nByte;
-  void *zConverted;
-  zConverted = convertUtf8Filename(zRelative);
-  if( isNT() ){
-    WCHAR *zTemp;
-    nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
-    zTemp = sqliteMalloc( nByte*sizeof(zTemp[0]) );
-    if( zTemp==0 ){
-      sqliteFree(zConverted);
-      return 0;
-    }
-    GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
-    sqliteFree(zConverted);
-    zFull = unicodeToUtf8(zTemp);
-    sqliteFree(zTemp);
-  }else{
-    char *zTemp;
-    nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
-    zTemp = sqliteMalloc( nByte*sizeof(zTemp[0]) );
-    if( zTemp==0 ){
-      sqliteFree(zConverted);
-      return 0;
-    }
-    GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
-    sqliteFree(zConverted);
-    zFull = mbcsToUtf8(zTemp);
-    sqliteFree(zTemp);
-  }
-#endif
-  return zFull;
-}
-
-/*
-** The fullSync option is meaningless on windows.   This is a no-op.
-*/
-static void winSetFullSync(OsFile *id, int v){
-  return;
-}
-
-/*
-** Return the underlying file handle for an OsFile
-*/
-static int winFileHandle(OsFile *id){
-  return (int)((winFile*)id)->h;
-}
-
-/*
-** Return an integer that indices the type of lock currently held
-** by this handle.  (Used for testing and analysis only.)
-*/
-static int winLockState(OsFile *id){
-  return ((winFile*)id)->locktype;
+** Control and query of the open file handle.
+*/
+static int winFileControl(sqlite3_file *id, int op, void *pArg){
+  switch( op ){
+    case SQLITE_FCNTL_LOCKSTATE: {
+      *(int*)pArg = ((winFile*)id)->locktype;
+      return SQLITE_OK;
+    }
+  }
+  return SQLITE_ERROR;
 }
 
 /*
 ** Return the sector size in bytes of the underlying block device for
@@ -17512,73 +18424,378 @@
 ** if two files are created in the same file-system directory (i.e.
 ** a database and it's journal file) that the sector size will be the
 ** same for both.
 */
-static int winSectorSize(OsFile *id){
+static int winSectorSize(sqlite3_file *id){
   return SQLITE_DEFAULT_SECTOR_SIZE;
 }
 
 /*
-** This vector defines all the methods that can operate on an OsFile
-** for win32.
-*/
-static const IoMethod sqlite3WinIoMethod = {
-  winClose,
-  winOpenDirectory,
+** Return a vector of device characteristics.
+*/
+static int winDeviceCharacteristics(sqlite3_file *id){
+  return 0;
+}
+
+/*
+** This vector defines all the methods that can operate on an
+** sqlite3_file for win32.
+*/
+static const sqlite3_io_methods winIoMethod = {
+  1,                        /* iVersion */
+  winClose,
   winRead,
   winWrite,
-  winSeek,
   winTruncate,
   winSync,
-  winSetFullSync,
-  winFileHandle,
   winFileSize,
   winLock,
   winUnlock,
-  winLockState,
   winCheckReservedLock,
+  winFileControl,
   winSectorSize,
-};
-
-/*
-** Allocate memory for an OsFile.  Initialize the new OsFile
-** to the value given in pInit and return a pointer to the new
-** OsFile.  If we run out of memory, close the file and return NULL.
-*/
-static int allocateWinFile(winFile *pInit, OsFile **pId){
-  winFile *pNew;
-  pNew = sqliteMalloc( sizeof(*pNew) );
-  if( pNew==0 ){
-    CloseHandle(pInit->h);
-#if OS_WINCE
-    sqliteFree(pInit->zDeleteOnClose);
-#endif
-    *pId = 0;
-    return SQLITE_NOMEM;
-  }else{
-    *pNew = *pInit;
-    pNew->pMethod = &sqlite3WinIoMethod;
-    pNew->locktype = NO_LOCK;
-    pNew->sharedLockByte = 0;
-    *pId = (OsFile*)pNew;
-    OpenCounter(+1);
-    return SQLITE_OK;
-  }
-}
-
-
-#endif /* SQLITE_OMIT_DISKIO */
+  winDeviceCharacteristics
+};
+
 /***************************************************************************
-** Everything above deals with file I/O.  Everything that follows deals
-** with other miscellanous aspects of the operating system interface
+** Here ends the I/O methods that form the sqlite3_io_methods object.
+**
+** The next block of code implements the VFS methods.
 ****************************************************************************/
 
-#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
+/*
+** Convert a UTF-8 filename into whatever form the underlying
+** operating system wants filenames in.  Space to hold the result
+** is obtained from malloc and must be freed by the calling
+** function.
+*/
+static void *convertUtf8Filename(const char *zFilename){
+  void *zConverted = 0;
+  if( isNT() ){
+    zConverted = utf8ToUnicode(zFilename);
+  }else{
+    zConverted = utf8ToMbcs(zFilename);
+  }
+  /* caller will handle out of memory */
+  return zConverted;
+}
+
+/*
+** Open a file.
+*/
+static int winOpen(
+  sqlite3_vfs *pVfs,        /* Not used */
+  const char *zName,        /* Name of the file (UTF-8) */
+  sqlite3_file *id,         /* Write the SQLite file handle here */
+  int flags,                /* Open mode flags */
+  int *pOutFlags            /* Status return flags */
+){
+  HANDLE h;
+  DWORD dwDesiredAccess;
+  DWORD dwShareMode;
+  DWORD dwCreationDisposition;
+  DWORD dwFlagsAndAttributes = 0;
+  winFile *pFile = (winFile*)id;
+  void *zConverted = convertUtf8Filename(zName);
+  if( zConverted==0 ){
+    return SQLITE_NOMEM;
+  }
+
+  if( flags & SQLITE_OPEN_READWRITE ){
+    dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
+  }else{
+    dwDesiredAccess = GENERIC_READ;
+  }
+  if( flags & SQLITE_OPEN_CREATE ){
+    dwCreationDisposition = OPEN_ALWAYS;
+  }else{
+    dwCreationDisposition = OPEN_EXISTING;
+  }
+  if( flags & SQLITE_OPEN_MAIN_DB ){
+    dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
+  }else{
+    dwShareMode = 0;
+  }
+  if( flags & (SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_TEMP_JOURNAL
+                    | SQLITE_OPEN_SUBJOURNAL) ){
+    dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
+                               | FILE_ATTRIBUTE_HIDDEN
+                               | FILE_FLAG_DELETE_ON_CLOSE;
+  }else{
+    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
+  }
+  if( flags & (SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB) ){
+    dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
+  }else{
+    dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
+  }
+  if( isNT() ){
+    h = CreateFileW((WCHAR*)zConverted,
+       dwDesiredAccess,
+       dwShareMode,
+       NULL,
+       dwCreationDisposition,
+       dwFlagsAndAttributes,
+       NULL
+    );
+  }else{
+#if OS_WINCE
+    return SQLITE_NOMEM;
+#else
+    h = CreateFileA((char*)zConverted,
+       dwDesiredAccess,
+       dwShareMode,
+       NULL,
+       dwCreationDisposition,
+       dwFlagsAndAttributes,
+       NULL
+    );
+#endif
+  }
+  if( h==INVALID_HANDLE_VALUE ){
+    free(zConverted);
+    if( flags & SQLITE_OPEN_READWRITE ){
+      return winOpen(0, zName, id,
+             ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
+    }else{
+      return SQLITE_CANTOPEN;
+    }
+  }
+  if( pOutFlags ){
+    if( flags & SQLITE_OPEN_READWRITE ){
+      *pOutFlags = SQLITE_OPEN_READWRITE;
+    }else{
+      *pOutFlags = SQLITE_OPEN_READONLY;
+    }
+  }
+  memset(pFile, 0, sizeof(*pFile));
+  pFile->pMethod = &winIoMethod;
+  pFile->h = h;
+#if OS_WINCE
+  if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
+               (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
+       && !winceCreateLock(zFilename, &f)
+  ){
+    CloseHandle(h);
+    free(zConverted);
+    return SQLITE_CANTOPEN;
+  }
+  if( dwFlagsAndAttributes & FILE_FLAG_DELETEONCLOSE ){
+    pFile->zDeleteOnClose = zConverted;
+  }else
+#endif
+  {
+    free(zConverted);
+  }
+  OpenCounter(+1);
+  return SQLITE_OK;
+}
+
+/*
+** Delete the named file.
+**
+** Note that windows does not allow a file to be deleted if some other
+** process has it open.  Sometimes a virus scanner or indexing program
+** will open a journal file shortly after it is created in order to do
+** whatever it is it does.  While this other process is holding the
+** file open, we will be unable to delete it.  To work around this
+** problem, we delay 100 milliseconds and try to delete again.  Up
+** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
+** up and returning an error.
+*/
+#define MX_DELETION_ATTEMPTS 3
+static int winDelete(
+  sqlite3_vfs *pVfs,          /* Not used on win32 */
+  const char *zFilename,      /* Name of file to delete */
+  int syncDir                 /* Not used on win32 */
+){
+  int cnt = 0;
+  int rc;
+  void *zConverted = convertUtf8Filename(zFilename);
+  if( zConverted==0 ){
+    return SQLITE_NOMEM;
+  }
+  SimulateIOError(return SQLITE_IOERR_DELETE);
+  if( isNT() ){
+    do{
+      rc = DeleteFileW(zConverted);
+    }while( rc==0 && GetFileAttributesW(zConverted)!=0xffffffff
+            && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
+  }else{
+#if OS_WINCE
+    return SQLITE_NOMEM;
+#else
+    do{
+      rc = DeleteFileA(zConverted);
+    }while( rc==0 && GetFileAttributesA(zConverted)!=0xffffffff
+            && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
+#endif
+  }
+  free(zConverted);
+  OSTRACE2("DELETE \"%s\"\n", zFilename);
+  return rc!=0 ? SQLITE_OK : SQLITE_IOERR;
+}
+
+/*
+** Check the existance and status of a file.
+*/
+static int winAccess(
+  sqlite3_vfs *pVfs,         /* Not used on win32 */
+  const char *zFilename,     /* Name of file to check */
+  int flags                  /* Type of test to make on this file */
+){
+  DWORD attr;
+  int rc;
+  void *zConverted = convertUtf8Filename(zFilename);
+  if( zConverted==0 ){
+    return SQLITE_NOMEM;
+  }
+  if( isNT() ){
+    attr = GetFileAttributesW((WCHAR*)zConverted);
+  }else{
+#if OS_WINCE
+    return SQLITE_NOMEM;
+#else
+    attr = GetFileAttributesA((char*)zConverted);
+#endif
+  }
+  free(zConverted);
+  switch( flags ){
+    case SQLITE_ACCESS_READ:
+    case SQLITE_ACCESS_EXISTS:
+      rc = attr!=0xffffffff;
+      break;
+    case SQLITE_ACCESS_READWRITE:
+      rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
+      break;
+    default:
+      assert(!"Invalid flags argument");
+  }
+  return rc;
+}
+
+
+/*
+** Create a temporary file name in zBuf.  zBuf must be big enough to
+** hold at pVfs->mxPathname characters.
+*/
+static int winGetTempName(sqlite3_vfs *pVfs, char *zBuf){
+  static char zChars[] =
+    "abcdefghijklmnopqrstuvwxyz"
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+    "0123456789";
+  int i, j;
+  char zTempPath[MAX_PATH+1];
+  if( sqlite3_temp_directory ){
+    sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
+  }else if( isNT() ){
+    char *zMulti;
+    WCHAR zWidePath[MAX_PATH];
+    GetTempPathW(MAX_PATH-30, zWidePath);
+    zMulti = unicodeToUtf8(zWidePath);
+    if( zMulti ){
+      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
+      free(zMulti);
+    }else{
+      return SQLITE_NOMEM;
+    }
+  }else{
+    char *zUtf8;
+    char zMbcsPath[MAX_PATH];
+    GetTempPathA(MAX_PATH-30, zMbcsPath);
+    zUtf8 = mbcsToUtf8(zMbcsPath);
+    if( zUtf8 ){
+      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
+      free(zUtf8);
+    }else{
+      return SQLITE_NOMEM;
+    }
+  }
+  for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
+  zTempPath[i] = 0;
+  sqlite3_snprintf(pVfs->mxPathname-30, zBuf,
+                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
+  j = strlen(zBuf);
+  sqlite3Randomness(20, &zBuf[j]);
+  for(i=0; i<20; i++, j++){
+    zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
+  }
+  zBuf[j] = 0;
+  OSTRACE2("TEMP FILENAME: %s\n", zBuf);
+  return SQLITE_OK;
+}
+
+/*
+** Turn a relative pathname into a full pathname.  Write the full
+** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
+** bytes in size.
+*/
+static int winFullPathname(
+  sqlite3_vfs *pVfs,
+  const char *zRelative,
+  char *zFull
+){
+
+#if defined(__CYGWIN__)
+  cygwin_conv_to_full_win32_path(zRelative, zFull);
+  return SQLITE_OK;
+#endif
+
+#if OS_WINCE
+  /* WinCE has no concept of a relative pathname, or so I am told. */
+  sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
+#endif
+
+#if !OS_WINCE && !defined(__CYGWIN__)
+  int nByte;
+  void *zConverted;
+  char *zOut;
+  zConverted = convertUtf8Filename(zRelative);
+  if( isNT() ){
+    WCHAR *zTemp;
+    nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
+    zTemp = malloc( nByte*sizeof(zTemp[0]) );
+    if( zTemp==0 ){
+      free(zConverted);
+      return SQLITE_NOMEM;
+    }
+    GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
+    free(zConverted);
+    zOut = unicodeToUtf8(zTemp);
+    free(zTemp);
+  }else{
+    char *zTemp;
+    nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
+    zTemp = malloc( nByte*sizeof(zTemp[0]) );
+    if( zTemp==0 ){
+      free(zConverted);
+      return SQLITE_NOMEM;
+    }
+    GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
+    free(zConverted);
+    zOut = mbcsToUtf8(zTemp);
+    free(zTemp);
+  }
+  if( zOut ){
+    sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
+    free(zOut);
+    return SQLITE_OK;
+  }else{
+    return SQLITE_NOMEM;
+  }
+#endif
+}
+
+#ifndef SQLITE_OMIT_LOAD_EXTENSION
+/*
+** Interfaces for opening a shared library, finding entry points
+** within the shared library, and closing the shared library.
+*/
 /*
 ** Interfaces for opening a shared library, finding entry points
 ** within the shared library, and closing the shared library.
 */
-SQLITE_PRIVATE void *sqlite3WinDlopen(const char *zFilename){
+static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
   HANDLE h;
   void *zConverted = convertUtf8Filename(zFilename);
   if( zConverted==0 ){
     return 0;
@@ -17591,13 +18808,23 @@
 #else
     h = LoadLibraryA((char*)zConverted);
 #endif
   }
-  sqliteFree(zConverted);
-  return (void*)h;
-
-}
-SQLITE_PRIVATE void *sqlite3WinDlsym(void *pHandle, const char *zSymbol){
+  free(zConverted);
+  return (void*)h;
+}
+static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
+  FormatMessage(
+    FORMAT_MESSAGE_FROM_SYSTEM,
+    NULL,
+    GetLastError(),
+    0,
+    zBufOut,
+    nBuf-1,
+    0
+  );
+}
+void *winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
 #if OS_WINCE
   /* The GetProcAddressA() routine is only available on wince. */
   return GetProcAddressA((HANDLE)pHandle, zSymbol);
 #else
@@ -17605,120 +18832,72 @@
   ** an Ansi string regardless of the _UNICODE setting */
   return GetProcAddress((HANDLE)pHandle, zSymbol);
 #endif
 }
-SQLITE_PRIVATE int sqlite3WinDlclose(void *pHandle){
-  return FreeLibrary((HANDLE)pHandle);
-}
-#endif /* !SQLITE_OMIT_LOAD_EXTENSION */
-
-/*
-** Get information to seed the random number generator.  The seed
-** is written into the buffer zBuf[256].  The calling function must
-** supply a sufficiently large buffer.
-*/
-SQLITE_PRIVATE int sqlite3WinRandomSeed(char *zBuf){
-  /* We have to initialize zBuf to prevent valgrind from reporting
-  ** errors.  The reports issued by valgrind are incorrect - we would
-  ** prefer that the randomness be increased by making use of the
-  ** uninitialized space in zBuf - but valgrind errors tend to worry
-  ** some users.  Rather than argue, it seems easier just to initialize
-  ** the whole array and silence valgrind, even if that means less randomness
-  ** in the random seed.
-  **
-  ** When testing, initializing zBuf[] to zero is all we do.  That means
-  ** that we always use the same random number sequence.* This makes the
-  ** tests repeatable.
-  */
-  memset(zBuf, 0, 256);
-  GetSystemTime((LPSYSTEMTIME)zBuf);
-  return SQLITE_OK;
-}
+void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
+  FreeLibrary((HANDLE)pHandle);
+}
+#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
+  #define winDlOpen  0
+  #define winDlError 0
+  #define winDlSym   0
+  #define winDlClose 0
+#endif
+
+
+/*
+** Write up to nBuf bytes of randomness into zBuf.
+*/
+static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
+  int n = 0;
+  if( sizeof(SYSTEMTIME)<=nBuf-n ){
+    SYSTEMTIME x;
+    GetSystemTime(&x);
+    memcpy(&zBuf[n], &x, sizeof(x));
+    n += sizeof(x);
+  }
+  if( sizeof(DWORD)<=nBuf-n ){
+    DWORD pid = GetCurrentProcessId();
+    memcpy(&zBuf[n], &pid, sizeof(pid));
+    n += sizeof(pid);
+  }
+  if( sizeof(DWORD)<=nBuf-n ){
+    DWORD cnt = GetTickCount();
+    memcpy(&zBuf[n], &cnt, sizeof(cnt));
+    n += sizeof(cnt);
+  }
+  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
+    LARGE_INTEGER i;
+    QueryPerformanceCounter(&i);
+    memcpy(&zBuf[n], &i, sizeof(i));
+    n += sizeof(i);
+  }
+  return n;
+}
+
 
 /*
 ** Sleep for a little while.  Return the amount of time slept.
 */
-SQLITE_PRIVATE int sqlite3WinSleep(int ms){
-  Sleep(ms);
-  return ms;
-}
-
-/*
-** Static variables used for thread synchronization
-*/
-static int inMutex = 0;
-#ifdef SQLITE_W32_THREADS
-  static DWORD mutexOwner;
-  static CRITICAL_SECTION cs;
-#endif
-
-/*
-** The following pair of routines implement mutual exclusion for
-** multi-threaded processes.  Only a single thread is allowed to
-** executed code that is surrounded by EnterMutex() and LeaveMutex().
-**
-** SQLite uses only a single Mutex.  There is not much critical
-** code and what little there is executes quickly and without blocking.
-**
-** Version 3.3.1 and earlier used a simple mutex.  Beginning with
-** version 3.3.2, a recursive mutex is required.
-*/
-SQLITE_PRIVATE void sqlite3WinEnterMutex(){
-#ifdef SQLITE_W32_THREADS
-  static int isInit = 0;
-  while( !isInit ){
-    static long lock = 0;
-    if( InterlockedIncrement(&lock)==1 ){
-      InitializeCriticalSection(&cs);
-      isInit = 1;
-    }else{
-      Sleep(1);
-    }
-  }
-  EnterCriticalSection(&cs);
-  mutexOwner = GetCurrentThreadId();
-#endif
-  inMutex++;
-}
-SQLITE_PRIVATE void sqlite3WinLeaveMutex(){
-  assert( inMutex );
-  inMutex--;
-#ifdef SQLITE_W32_THREADS
-  assert( mutexOwner==GetCurrentThreadId() );
-  LeaveCriticalSection(&cs);
-#endif
-}
-
-/*
-** Return TRUE if the mutex is currently held.
-**
-** If the thisThreadOnly parameter is true, return true if and only if the
-** calling thread holds the mutex.  If the parameter is false, return
-** true if any thread holds the mutex.
-*/
-SQLITE_PRIVATE int sqlite3WinInMutex(int thisThreadOnly){
-#ifdef SQLITE_W32_THREADS
-  return inMutex>0 && (thisThreadOnly==0 || mutexOwner==GetCurrentThreadId());
-#else
-  return inMutex>0;
-#endif
-}
-
+static int winSleep(sqlite3_vfs *pVfs, int microsec){
+  Sleep((microsec+999)/1000);
+  return ((microsec+999)/1000)*1000;
+}
 
 /*
 ** The following variable, if set to a non-zero value, becomes the result
 ** returned from sqlite3OsCurrentTime().  This is used for testing.
 */
 #ifdef SQLITE_TEST
-int sqlite3_current_time = 0;
+SQLITE_API int sqlite3_current_time = 0;
 #endif
 
 /*
 ** Find the current time (in Universal Coordinated Time).  Write the
 ** current time and date as a Julian Day number into *prNow and
 ** return 0.  Return 1 if the time and date cannot be found.
 */
-SQLITE_PRIVATE int sqlite3WinCurrentTime(double *prNow){
+int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
   FILETIME ft;
   /* FILETIME structure is a 64-bit value representing the number of
      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
   */
@@ -17739,75 +18918,41 @@
 #endif
   return 0;
 }
 
-/*
-** Remember the number of thread-specific-data blocks allocated.
-** Use this to verify that we are not leaking thread-specific-data.
-** Ticket #1601
-*/
-#ifdef SQLITE_TEST
-int sqlite3_tsd_count = 0;
-# define TSD_COUNTER_INCR InterlockedIncrement(&sqlite3_tsd_count)
-# define TSD_COUNTER_DECR InterlockedDecrement(&sqlite3_tsd_count)
-#else
-# define TSD_COUNTER_INCR  /* no-op */
-# define TSD_COUNTER_DECR  /* no-op */
-#endif
-
-
-
-/*
-** If called with allocateFlag>1, then return a pointer to thread
-** specific data for the current thread.  Allocate and zero the
-** thread-specific data if it does not already exist necessary.
-**
-** If called with allocateFlag==0, then check the current thread
-** specific data.  Return it if it exists.  If it does not exist,
-** then return NULL.
-**
-** If called with allocateFlag<0, check to see if the thread specific
-** data is allocated and is all zero.  If it is then deallocate it.
-** Return a pointer to the thread specific data or NULL if it is
-** unallocated or gets deallocated.
-*/
-SQLITE_PRIVATE ThreadData *sqlite3WinThreadSpecificData(int allocateFlag){
-  static int key;
-  static int keyInit = 0;
-  static const ThreadData zeroData = {0};
-  ThreadData *pTsd;
-
-  if( !keyInit ){
-    sqlite3OsEnterMutex();
-    if( !keyInit ){
-      key = TlsAlloc();
-      if( key==0xffffffff ){
-        sqlite3OsLeaveMutex();
-        return 0;
-      }
-      keyInit = 1;
-    }
-    sqlite3OsLeaveMutex();
-  }
-  pTsd = TlsGetValue(key);
-  if( allocateFlag>0 ){
-    if( !pTsd ){
-      pTsd = sqlite3OsMalloc( sizeof(zeroData) );
-      if( pTsd ){
-        *pTsd = zeroData;
-        TlsSetValue(key, pTsd);
-        TSD_COUNTER_INCR;
-      }
-    }
-  }else if( pTsd!=0 && allocateFlag<0
-              && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
-    sqlite3OsFree(pTsd);
-    TlsSetValue(key, 0);
-    TSD_COUNTER_DECR;
-    pTsd = 0;
-  }
-  return pTsd;
-}
+
+/*
+** Return a pointer to the sqlite3DefaultVfs structure.   We use
+** a function rather than give the structure global scope because
+** some compilers (MSVC) do not allow forward declarations of
+** initialized structures.
+*/
+SQLITE_PRIVATE sqlite3_vfs *sqlite3OsDefaultVfs(void){
+  static sqlite3_vfs winVfs = {
+    1,                 /* iVersion */
+    sizeof(winFile),   /* szOsFile */
+    MAX_PATH,          /* mxPathname */
+    0,                 /* pNext */
+    "win32",           /* zName */
+    0,                 /* pAppData */
+
+    winOpen,           /* xOpen */
+    winDelete,         /* xDelete */
+    winAccess,         /* xAccess */
+    winGetTempName,    /* xGetTempName */
+    winFullPathname,   /* xFullPathname */
+    winDlOpen,         /* xDlOpen */
+    winDlError,        /* xDlError */
+    winDlSym,          /* xDlSym */
+    winDlClose,        /* xDlClose */
+    winRandomness,     /* xRandomness */
+    winSleep,          /* xSleep */
+    winCurrentTime     /* xCurrentTime */
+  };
+
+  return &winVfs;
+}
+
 #endif /* OS_WIN */
 
 /************** End of os_win.c **********************************************/
 /************** Begin file pager.c *******************************************/
@@ -17830,9 +18975,9 @@
 ** locking to prevent two processes from writing the same database
 ** file simultaneously, or one process from reading the database while
 ** another is writing.
 **
-** @(#) $Id: pager.c,v 1.351 2007/07/20 00:33:36 drh Exp $
+** @(#) $Id: pager.c,v 1.388 2007/09/10 06:12:04 danielk1977 Exp $
 */
 #ifndef SQLITE_OMIT_DISKIO
 
 /*
@@ -17857,9 +19002,9 @@
 ** The following two macros are used within the PAGERTRACEX() macros above
 ** to print out file-descriptors.
 **
 ** PAGERID() takes a pointer to a Pager struct as it's argument. The
-** associated file-descriptor is returned. FILEHANDLEID() takes an OsFile
+** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
 ** struct as it's argument.
 */
 #define PAGERID(p) ((int)(p->fd))
 #define FILEHANDLEID(fd) ((int)fd)
@@ -17939,8 +19084,42 @@
 ** This macro rounds values up so that if the value is an address it
 ** is guaranteed to be an address that is aligned to an 8-byte boundary.
 */
 #define FORCE_ALIGNMENT(X)   (((X)+7)&~7)
+
+typedef struct PgHdr PgHdr;
+
+/*
+** Each pager stores all currently unreferenced pages in a list sorted
+** in least-recently-used (LRU) order (i.e. the first item on the list has
+** not been referenced in a long time, the last item has been recently
+** used). An instance of this structure is included as part of each
+** pager structure for this purpose (variable Pager.lru).
+**
+** Additionally, if memory-management is enabled, all unreferenced pages
+** are stored in a global LRU list (global variable sqlite3LruPageList).
+**
+** In both cases, the PagerLruList.pFirstSynced variable points to
+** the first page in the corresponding list that does not require an
+** fsync() operation before it's memory can be reclaimed. If no such
+** page exists, PagerLruList.pFirstSynced is set to NULL.
+*/
+typedef struct PagerLruList PagerLruList;
+struct PagerLruList {
+  PgHdr *pFirst;         /* First page in LRU list */
+  PgHdr *pLast;          /* Last page in LRU list (the most recently used) */
+  PgHdr *pFirstSynced;   /* First page in list with PgHdr.needSync==0 */
+};
+
+/*
+** The following structure contains the next and previous pointers used
+** to link a PgHdr structure into a PagerLruList linked list.
+*/
+typedef struct PagerLruLink PagerLruLink;
+struct PagerLruLink {
+  PgHdr *pNext;
+  PgHdr *pPrev;
+};
 
 /*
 ** Each in-memory image of a page begins with the following header.
 ** This header is only visible to this pager module.  The client
@@ -18030,14 +19209,13 @@
 **     make any assumptions about the content nevertheless.)  If the
 **     content is needed in the future, it should be read from the
 **     original database file.
 */
-typedef struct PgHdr PgHdr;
 struct PgHdr {
   Pager *pPager;                 /* The pager to which this page belongs */
   Pgno pgno;                     /* The page number for this page */
   PgHdr *pNextHash, *pPrevHash;  /* Hash collision chain for PgHdr.pgno */
-  PgHdr *pNextFree, *pPrevFree;  /* Freelist of pages where nRef==0 */
+  PagerLruLink free;             /* Next and previous free pages */
   PgHdr *pNextAll;               /* A list of all pages */
   u8 inJournal;                  /* TRUE if has been written to journal */
   u8 dirty;                      /* TRUE if we need to write back changes */
   u8 needSync;                   /* Sync journal before writing this page */
@@ -18044,14 +19222,16 @@
   u8 alwaysRollback;             /* Disable DontRollback() for this page */
   u8 needRead;                   /* Read content if PagerWrite() is called */
   short int nRef;                /* Number of users of this page */
   PgHdr *pDirty, *pPrevDirty;    /* Dirty pages */
-  u32 notUsed;                   /* Buffer space */
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  PagerLruLink gfree;            /* Global list of nRef==0 pages */
+#endif
 #ifdef SQLITE_CHECK_PAGES
   u32 pageHash;
 #endif
-  /* pPager->pageSize bytes of page data follow this header */
-  /* Pager.nExtra bytes of local data follow the page data */
+  void *pData;                   /* Page data */
+  /* Pager.nExtra bytes of local data appended to this header */
 };
 
 /*
 ** For an in-memory only database, some extra information is recorded about
@@ -18086,13 +19266,12 @@
 /*
 ** Convert a pointer to a PgHdr into a pointer to its data
 ** and back again.
 */
-#define PGHDR_TO_DATA(P)  ((void*)(&(P)[1]))
-#define DATA_TO_PGHDR(D)  (&((PgHdr*)(D))[-1])
-#define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize])
+#define PGHDR_TO_DATA(P)    ((P)->pData)
+#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
 #define PGHDR_TO_HIST(P,PGR)  \
-            ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
+            ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
 
 /*
 ** A open page cache is an instance of the following structure.
 **
@@ -18104,8 +19283,9 @@
 ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
 ** APIs, they may still be used successfully.
 */
 struct Pager {
+  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
   u8 journalOpen;             /* True if journal file descriptors is valid */
   u8 journalStarted;          /* True if header of journal is synced */
   u8 useJournal;              /* Use a rollback journal on this file */
   u8 noReadlock;              /* Do not bother to obtain readlocks */
@@ -18113,9 +19293,9 @@
   u8 stmtInUse;               /* True we are in a statement subtransaction */
   u8 stmtAutoopen;            /* Open stmt journal when main journal is opened*/
   u8 noSync;                  /* Do not sync the journal if true */
   u8 fullSync;                /* Do extra syncs of the journal for robustness */
-  u8 full_fsync;              /* Use F_FULLFSYNC when available */
+  u8 sync_flags;              /* One of SYNC_NORMAL or SYNC_FULL */
   u8 state;                   /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
   u8 tempFile;                /* zFilename is a temporary file */
   u8 readOnly;                /* True for a read-only database */
   u8 needSync;                /* True if an fsync() is needed on the journal */
@@ -18125,8 +19305,9 @@
   u8 setMaster;               /* True if a m-j name has been written to jrnl */
   u8 doNotSync;               /* Boolean. While true, do not spill the cache */
   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
   u8 changeCountDone;         /* Set after incrementing the change-counter */
+  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
   int errCode;                /* One of several kinds of errors */
   int dbSize;                 /* Number of pages in the file */
   int origDbSize;             /* dbSize before the current change */
   int stmtSize;               /* Size of database (in pages) at stmt_begin() */
@@ -18143,13 +19324,13 @@
   u8 *aInStmt;                /* One bit for each page in the database */
   char *zFilename;            /* Name of the database file */
   char *zJournal;             /* Name of the journal file */
   char *zDirectory;           /* Directory hold database and journal files */
-  OsFile *fd, *jfd;           /* File descriptors for database and journal */
-  OsFile *stfd;               /* File descriptor for the statement subjournal*/
+  char *zStmtJrnl;            /* Name of the statement journal file */
+  sqlite3_file *fd, *jfd;     /* File descriptors for database and journal */
+  sqlite3_file *stfd;         /* File descriptor for the statement subjournal*/
   BusyHandler *pBusyHandler;  /* Pointer to sqlite.busyHandler */
-  PgHdr *pFirst, *pLast;      /* List of free pages */
-  PgHdr *pFirstSynced;        /* First free page with PgHdr.needSync==0 */
+  PagerLruList lru;           /* LRU list of free pages */
   PgHdr *pAll;                /* List of all pages */
   PgHdr *pStmt;               /* List of pages in the statement subjournal */
   PgHdr *pDirty;              /* List of all dirty pages */
   i64 journalOff;             /* Current byte offset in the journal file */
@@ -18170,9 +19351,12 @@
 #endif
   int nHash;                  /* Size of the pager hash table */
   PgHdr **aHash;              /* Hash table to map page number to PgHdr */
 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  Pager *pNext;               /* Linked list of pagers in this thread */
+  Pager *pNext;               /* Doubly linked list of pagers on which */
+  Pager *pPrev;               /* sqlite3_release_memory() will work */
+  int iInUseMM;               /* Non-zero if unavailable to MM */
+  int iInUseDB;               /* Non-zero if in sqlite3_release_memory() */
 #endif
   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
   char dbFileVers[16];        /* Changes whenever database file changes */
 };
@@ -18182,17 +19366,27 @@
 ** testing purposes only.  These variables do not exist in
 ** a non-testing build.  These variables are not thread-safe.
 */
 #ifdef SQLITE_TEST
-int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
-int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
-int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
-int sqlite3_pager_pgfree_count = 0;    /* Number of cache pages freed */
+SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
+SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
+SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
+SQLITE_API int sqlite3_pager_pgfree_count = 0;    /* Number of cache pages freed */
 # define PAGER_INCR(v)  v++
 #else
 # define PAGER_INCR(v)
 #endif
 
+/*
+** The following variable points to the head of a double-linked list
+** of all pagers that are eligible for page stealing by the
+** sqlite3_release_memory() interface.  Access to this list is
+** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
+*/
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+static Pager *sqlite3PagerList = 0;
+static PagerLruList sqlite3LruPageList = {0, 0, 0};
+#endif
 
 
 /*
 ** Journal files begin with the following magic string.  The data
@@ -18261,8 +19455,40 @@
 */
 #define PAGER_MAX_PGNO 2147483647
 
 /*
+** The pagerEnter() and pagerLeave() routines acquire and release
+** a mutex on each pager.  The mutex is recursive.
+**
+** This is a special-purpose mutex.  It only provides mutual exclusion
+** between the Btree and the Memory Management sqlite3_release_memory()
+** function.  It does not prevent, for example, two Btrees from accessing
+** the same pager at the same time.  Other general-purpose mutexes in
+** the btree layer handle that chore.
+*/
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  static void pagerEnter(Pager *p){
+    p->iInUseDB++;
+    if( p->iInUseMM && p->iInUseDB==1 ){
+      sqlite3_mutex *mutex;
+      mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
+      p->iInUseDB = 0;
+      sqlite3_mutex_enter(mutex);
+      p->iInUseDB = 1;
+      sqlite3_mutex_leave(mutex);
+    }
+    assert( p->iInUseMM==0 );
+  }
+  static void pagerLeave(Pager *p){
+    p->iInUseDB--;
+    assert( p->iInUseDB>=0 );
+  }
+#else
+# define pagerEnter(X)
+# define pagerLeave(X)
+#endif
+
+/*
 ** Enable reference count tracking (for debugging) here:
 */
 #ifdef SQLITE_DEBUG
   int pager3_refinfo_enable = 0;
@@ -18280,8 +19506,132 @@
 # define REFINFO(X)
 #endif
 
 /*
+** Add page pPg to the end of the linked list managed by structure
+** pList (pPg becomes the last entry in the list - the most recently
+** used). Argument pLink should point to either pPg->free or pPg->gfree,
+** depending on whether pPg is being added to the pager-specific or
+** global LRU list.
+*/
+static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
+  pLink->pNext = 0;
+  pLink->pPrev = pList->pLast;
+
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  assert(pLink==&pPg->free || pLink==&pPg->gfree);
+  assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
+#endif
+
+  if( pList->pLast ){
+    int iOff = (char *)pLink - (char *)pPg;
+    PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
+    pLastLink->pNext = pPg;
+  }else{
+    assert(!pList->pFirst);
+    pList->pFirst = pPg;
+  }
+
+  pList->pLast = pPg;
+  if( !pList->pFirstSynced && pPg->needSync==0 ){
+    pList->pFirstSynced = pPg;
+  }
+}
+
+/*
+** Remove pPg from the list managed by the structure pointed to by pList.
+**
+** Argument pLink should point to either pPg->free or pPg->gfree, depending
+** on whether pPg is being added to the pager-specific or global LRU list.
+*/
+static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
+  int iOff = (char *)pLink - (char *)pPg;
+
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  assert(pLink==&pPg->free || pLink==&pPg->gfree);
+  assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
+#endif
+
+  if( pPg==pList->pFirst ){
+    pList->pFirst = pLink->pNext;
+  }
+  if( pPg==pList->pLast ){
+    pList->pLast = pLink->pPrev;
+  }
+  if( pLink->pPrev ){
+    PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
+    pPrevLink->pNext = pLink->pNext;
+  }
+  if( pLink->pNext ){
+    PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
+    pNextLink->pPrev = pLink->pPrev;
+  }
+  if( pPg==pList->pFirstSynced ){
+    PgHdr *p = pLink->pNext;
+    while( p && p->needSync ){
+      PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
+      p = pL->pNext;
+    }
+    pList->pFirstSynced = p;
+  }
+
+  pLink->pNext = pLink->pPrev = 0;
+}
+
+/*
+** Add page pPg to the list of free pages for the pager. If
+** memory-management is enabled, also add the page to the global
+** list of free pages.
+*/
+static void lruListAdd(PgHdr *pPg){
+  listAdd(&pPg->pPager->lru, &pPg->free, pPg);
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  if( !pPg->pPager->memDb ){
+    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+    listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
+    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+  }
+#endif
+}
+
+/*
+** Remove page pPg from the list of free pages for the associated pager.
+** If memory-management is enabled, also remove pPg from the global list
+** of free pages.
+*/
+static void lruListRemove(PgHdr *pPg){
+  listRemove(&pPg->pPager->lru, &pPg->free, pPg);
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  if( !pPg->pPager->memDb ){
+    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+    listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
+    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+  }
+#endif
+}
+
+/*
+** This function is called just after the needSync flag has been cleared
+** from all pages managed by pPager (usually because the journal file
+** has just been synced). It updates the pPager->lru.pFirstSynced variable
+** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
+** variable also.
+*/
+static void lruListSetFirstSynced(Pager *pPager){
+  pPager->lru.pFirstSynced = pPager->lru.pFirst;
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
+  if( !pPager->memDb ){
+    PgHdr *p;
+    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+    for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
+    assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
+    sqlite3LruPageList.pFirstSynced = p;
+    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+  }
+#endif
+}
+
+/*
 ** Return true if page *pPg has already been written to the statement
 ** journal (or statement snapshot has been created, if *pPg is part
 ** of an in-memory database).
 */
@@ -18302,14 +19652,17 @@
 */
 static void pager_resize_hash_table(Pager *pPager, int N){
   PgHdr **aHash, *pPg;
   assert( N>0 && (N&(N-1))==0 );
-  aHash = sqliteMalloc( sizeof(aHash[0])*N );
+  pagerLeave(pPager);
+  sqlite3MallocBenignFailure((int)pPager->aHash);
+  aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
+  pagerEnter(pPager);
   if( aHash==0 ){
     /* Failure to rehash is not an error.  It is only a performance hit. */
     return;
   }
-  sqliteFree(pPager->aHash);
+  sqlite3_free(pPager->aHash);
   pPager->nHash = N;
   pPager->aHash = aHash;
   for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
     int h;
@@ -18333,11 +19686,11 @@
 ** error code is something goes wrong.
 **
 ** All values are stored on disk as big-endian.
 */
-static int read32bits(OsFile *fd, u32 *pRes){
+static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
   unsigned char ac[4];
-  int rc = sqlite3OsRead(fd, ac, sizeof(ac));
+  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
   if( rc==SQLITE_OK ){
     *pRes = sqlite3Get4byte(ac);
   }
   return rc;
@@ -18351,13 +19704,59 @@
 /*
 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
 ** on success or an error code is something goes wrong.
 */
-static int write32bits(OsFile *fd, u32 val){
+static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
   char ac[4];
   put32bits(ac, val);
-  return sqlite3OsWrite(fd, ac, 4);
-}
+  return sqlite3OsWrite(fd, ac, 4, offset);
+}
+
+/*
+** If file pFd is open, call sqlite3OsUnlock() on it.
+*/
+static int osUnlock(sqlite3_file *pFd, int eLock){
+  if( !pFd->pMethods ){
+    return SQLITE_OK;
+  }
+  return sqlite3OsUnlock(pFd, eLock);
+}
+
+/*
+** This function determines whether or not the atomic-write optimization
+** can be used with this pager. The optimization can be used if:
+**
+**  (a) the value returned by OsDeviceCharacteristics() indicates that
+**      a database page may be written atomically, and
+**  (b) the value returned by OsSectorSize() is less than or equal
+**      to the page size.
+**
+** If the optimization cannot be used, 0 is returned. If it can be used,
+** then the value returned is the size of the journal file when it
+** contains rollback data for exactly one page.
+*/
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE
+static int jrnlBufferSize(Pager *pPager){
+  int dc;           /* Device characteristics */
+  int nSector;      /* Sector size */
+  int nPage;        /* Page size */
+  sqlite3_file *fd = pPager->fd;
+
+  if( fd->pMethods ){
+    dc = sqlite3OsDeviceCharacteristics(fd);
+    nSector = sqlite3OsSectorSize(fd);
+    nPage = pPager->pageSize;
+  }
+
+  assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
+  assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
+
+  if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
+    return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
+  }
+  return 0;
+}
+#endif
 
 /*
 ** This function should be called when an error occurs within the pager
 ** code. The first argument is a pointer to the pager structure, the
@@ -18369,9 +19768,13 @@
 ** will immediately return the same error code.
 */
 static int pager_error(Pager *pPager, int rc){
   int rc2 = rc & 0xff;
-  assert( pPager->errCode==SQLITE_FULL || pPager->errCode==SQLITE_OK );
+  assert(
+       pPager->errCode==SQLITE_FULL ||
+       pPager->errCode==SQLITE_OK ||
+       (pPager->errCode & 0xff)==SQLITE_IOERR
+  );
   if(
     rc2==SQLITE_FULL ||
     rc2==SQLITE_IOERR ||
     rc2==SQLITE_CORRUPT
@@ -18423,68 +19826,63 @@
 
 /*
 ** When this is called the journal file for pager pPager must be open.
 ** The master journal file name is read from the end of the file and
-** written into memory obtained from sqliteMalloc(). *pzMaster is
-** set to point at the memory and SQLITE_OK returned. The caller must
-** sqliteFree() *pzMaster.
-**
-** If no master journal file name is present *pzMaster is set to 0 and
+** written into memory supplied by the caller.
+**
+** zMaster must point to a buffer of at least nMaster bytes allocated by
+** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
+** enough space to write the master journal name). If the master journal
+** name in the journal is longer than nMaster bytes (including a
+** nul-terminator), then this is handled as if no master journal name
+** were present in the journal.
+**
+** If no master journal file name is present zMaster[0] is set to 0 and
 ** SQLITE_OK returned.
 */
-static int readMasterJournal(OsFile *pJrnl, char **pzMaster){
+static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
   int rc;
   u32 len;
   i64 szJ;
   u32 cksum;
   int i;
   unsigned char aMagic[8]; /* A buffer to hold the magic header */
 
-  *pzMaster = 0;
+  zMaster[0] = '\0';
 
   rc = sqlite3OsFileSize(pJrnl, &szJ);
   if( rc!=SQLITE_OK || szJ<16 ) return rc;
 
-  rc = sqlite3OsSeek(pJrnl, szJ-16);
+  rc = read32bits(pJrnl, szJ-16, &len);
   if( rc!=SQLITE_OK ) return rc;
 
-  rc = read32bits(pJrnl, &len);
+  if( len>=nMaster ){
+    return SQLITE_OK;
+  }
+
+  rc = read32bits(pJrnl, szJ-12, &cksum);
   if( rc!=SQLITE_OK ) return rc;
 
-  rc = read32bits(pJrnl, &cksum);
-  if( rc!=SQLITE_OK ) return rc;
-
-  rc = sqlite3OsRead(pJrnl, aMagic, 8);
+  rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
   if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
 
-  rc = sqlite3OsSeek(pJrnl, szJ-16-len);
-  if( rc!=SQLITE_OK ) return rc;
-
-  *pzMaster = (char *)sqliteMalloc(len+1);
-  if( !*pzMaster ){
-    return SQLITE_NOMEM;
-  }
-  rc = sqlite3OsRead(pJrnl, *pzMaster, len);
-  if( rc!=SQLITE_OK ){
-    sqliteFree(*pzMaster);
-    *pzMaster = 0;
-    return rc;
-  }
+  rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
+  if( rc!=SQLITE_OK ){
+    return rc;
+  }
+  zMaster[len] = '\0';
 
   /* See if the checksum matches the master journal name */
   for(i=0; i<len; i++){
-    cksum -= (*pzMaster)[i];
-  }
+    cksum -= zMaster[i];
+   }
   if( cksum ){
     /* If the checksum doesn't add up, then one or more of the disk sectors
     ** containing the master journal filename is corrupted. This means
     ** definitely roll back, so just return SQLITE_OK and report a (nul)
     ** master-journal filename.
     */
-    sqliteFree(*pzMaster);
-    *pzMaster = 0;
-  }else{
-    (*pzMaster)[len] = '\0';
+    zMaster[0] = '\0';
   }
 
   return SQLITE_OK;
 }
@@ -18503,9 +19901,9 @@
 ** 100                       512
 ** 2000                      2048
 **
 */
-static int seekJournalHdr(Pager *pPager){
+static void seekJournalHdr(Pager *pPager){
   i64 offset = 0;
   i64 c = pPager->journalOff;
   if( c ){
     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
@@ -18513,9 +19911,8 @@
   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
   assert( offset>=c );
   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
   pPager->journalOff = offset;
-  return sqlite3OsSeek(pPager->jfd, pPager->journalOff);
 }
 
 /*
 ** The journal file must be open when this routine is called. A journal
@@ -18538,25 +19935,42 @@
   if( pPager->stmtHdrOff==0 ){
     pPager->stmtHdrOff = pPager->journalOff;
   }
 
-  rc = seekJournalHdr(pPager);
-  if( rc ) return rc;
-
+  seekJournalHdr(pPager);
   pPager->journalHdr = pPager->journalOff;
-  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
-
-  /* FIX ME:
-  **
-  ** Possibly for a pager not in no-sync mode, the journal magic should not
-  ** be written until nRec is filled in as part of next syncJournal().
-  **
-  ** Actually maybe the whole journal header should be delayed until that
-  ** point. Think about this.
-  */
+
   memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
-  /* The nRec Field. 0xFFFFFFFF for no-sync journals. */
-  put32bits(&zHeader[sizeof(aJournalMagic)], pPager->noSync ? 0xffffffff : 0);
+
+  /*
+  ** Write the nRec Field - the number of page records that follow this
+  ** journal header. Normally, zero is written to this value at this time.
+  ** After the records are added to the journal (and the journal synced,
+  ** if in full-sync mode), the zero is overwritten with the true number
+  ** of records (see syncJournal()).
+  **
+  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
+  ** reading the journal this value tells SQLite to assume that the
+  ** rest of the journal file contains valid page records. This assumption
+  ** is dangerous, as if a failure occured whilst writing to the journal
+  ** file it may contain some garbage data. There are two scenarios
+  ** where this risk can be ignored:
+  **
+  **   * When the pager is in no-sync mode. Corruption can follow a
+  **     power failure in this case anyway.
+  **
+  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
+  **     that garbage data is never appended to the journal file.
+  */
+  assert(pPager->fd->pMethods||pPager->noSync);
+  if( (pPager->noSync)
+   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
+  ){
+    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
+  }else{
+    put32bits(&zHeader[sizeof(aJournalMagic)], 0);
+  }
+
   /* The random check-hash initialiser */
   sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
   /* The initial database size */
@@ -18563,19 +19977,17 @@
   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
   /* The assumed sector size for this process */
   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
   IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
-  rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader));
+  rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
+  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
 
   /* The journal header has been written successfully. Seek the journal
   ** file descriptor to the end of the journal header sector.
   */
   if( rc==SQLITE_OK ){
     IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
-    rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff-1);
-    if( rc==SQLITE_OK ){
-      rc = sqlite3OsWrite(pPager->jfd, "\000", 1);
-    }
+    rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
   }
   return rc;
 }
 
@@ -18602,30 +20014,31 @@
   u32 *pDbSize
 ){
   int rc;
   unsigned char aMagic[8]; /* A buffer to hold the magic header */
-
-  rc = seekJournalHdr(pPager);
-  if( rc ) return rc;
-
+  i64 jrnlOff;
+
+  seekJournalHdr(pPager);
   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
     return SQLITE_DONE;
   }
-
-  rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic));
-  if( rc ) return rc;
+  jrnlOff = pPager->journalOff;
+
+  rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
+  if( rc ) return rc;
+  jrnlOff += sizeof(aMagic);
 
   if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
     return SQLITE_DONE;
   }
 
-  rc = read32bits(pPager->jfd, pNRec);
-  if( rc ) return rc;
-
-  rc = read32bits(pPager->jfd, &pPager->cksumInit);
-  if( rc ) return rc;
-
-  rc = read32bits(pPager->jfd, pDbSize);
+  rc = read32bits(pPager->jfd, jrnlOff, pNRec);
+  if( rc ) return rc;
+
+  rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
+  if( rc ) return rc;
+
+  rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
   if( rc ) return rc;
 
   /* Update the assumed sector-size to match the value used by
   ** the process that created this journal. If this journal was
@@ -18632,14 +20045,13 @@
   ** created by a process other than this one, then this routine
   ** is being called from within pager_playback(). The local value
   ** of Pager.sectorSize is restored at the end of that routine.
   */
-  rc = read32bits(pPager->jfd, (u32 *)&pPager->sectorSize);
+  rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
   if( rc ) return rc;
 
   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
-  rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff);
-  return rc;
+  return SQLITE_OK;
 }
 
 
 /*
@@ -18664,8 +20076,9 @@
 static int writeMasterJournal(Pager *pPager, const char *zMaster){
   int rc;
   int len;
   int i;
+  i64 jrnlOff;
   u32 cksum = 0;
   char zBuf[sizeof(aJournalMagic)+2*4];
 
   if( !zMaster || pPager->setMaster) return SQLITE_OK;
@@ -18680,23 +20093,25 @@
   ** the master journal name. This is in case the previous page written to
   ** the journal has already been synced.
   */
   if( pPager->fullSync ){
-    rc = seekJournalHdr(pPager);
-    if( rc!=SQLITE_OK ) return rc;
-  }
+    seekJournalHdr(pPager);
+  }
+  jrnlOff = pPager->journalOff;
   pPager->journalOff += (len+20);
 
-  rc = write32bits(pPager->jfd, PAGER_MJ_PGNO(pPager));
+  rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
   if( rc!=SQLITE_OK ) return rc;
-
-  rc = sqlite3OsWrite(pPager->jfd, zMaster, len);
+  jrnlOff += 4;
+
+  rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
   if( rc!=SQLITE_OK ) return rc;
+  jrnlOff += len;
 
   put32bits(zBuf, len);
   put32bits(&zBuf[4], cksum);
   memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
-  rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic));
+  rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
   pPager->needSync = !pPager->noSync;
   return rc;
 }
 
@@ -18743,9 +20158,9 @@
 */
 static void pager_unlock(Pager *pPager){
   if( !pPager->exclusiveMode ){
     if( !MEMDB ){
-      sqlite3OsUnlock(pPager->fd, NO_LOCK);
+      osUnlock(pPager->fd, NO_LOCK);
       pPager->dbSize = -1;
       IOTRACE(("UNLOCK %p\n", pPager))
     }
     pPager->state = PAGER_UNLOCK;
@@ -18782,17 +20197,19 @@
   for(pPg=pPager->pAll; pPg; pPg=pNext){
     IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
     PAGER_INCR(sqlite3_pager_pgfree_count);
     pNext = pPg->pNextAll;
-    sqliteFree(pPg);
-  }
-  pPager->pStmt = 0;
-  pPager->pFirst = 0;
-  pPager->pFirstSynced = 0;
-  pPager->pLast = 0;
+    lruListRemove(pPg);
+    sqlite3_free(pPg->pData);
+    sqlite3_free(pPg);
+  }
+  assert(pPager->lru.pFirst==0);
+  assert(pPager->lru.pFirstSynced==0);
+  assert(pPager->lru.pLast==0);
+  pPager->pStmt = 0;
   pPager->pAll = 0;
   pPager->nHash = 0;
-  sqliteFree(pPager->aHash);
+  sqlite3_free(pPager->aHash);
   pPager->nPage = 0;
   pPager->aHash = 0;
   pPager->nRef = 0;
 }
@@ -18823,25 +20240,24 @@
     return SQLITE_OK;
   }
   sqlite3PagerStmtCommit(pPager);
   if( pPager->stmtOpen && !pPager->exclusiveMode ){
-    sqlite3OsClose(&pPager->stfd);
+    sqlite3OsClose(pPager->stfd);
     pPager->stmtOpen = 0;
   }
   if( pPager->journalOpen ){
     if( pPager->exclusiveMode
           && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
-      sqlite3OsSeek(pPager->jfd, 0);
       pPager->journalOff = 0;
       pPager->journalStarted = 0;
     }else{
-      sqlite3OsClose(&pPager->jfd);
+      sqlite3OsClose(pPager->jfd);
       pPager->journalOpen = 0;
       if( rc==SQLITE_OK ){
-        rc = sqlite3OsDelete(pPager->zJournal);
-      }
-    }
-    sqliteFree( pPager->aInJournal );
+        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
+      }
+    }
+    sqlite3_free( pPager->aInJournal );
     pPager->aInJournal = 0;
     for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
       pPg->inJournal = 0;
       pPg->dirty = 0;
@@ -18859,17 +20275,17 @@
     assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
   }
 
   if( !pPager->exclusiveMode ){
-    rc2 = sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
+    rc2 = osUnlock(pPager->fd, SHARED_LOCK);
     pPager->state = PAGER_SHARED;
   }else if( pPager->state==PAGER_SYNCED ){
     pPager->state = PAGER_EXCLUSIVE;
   }
   pPager->origDbSize = 0;
   pPager->setMaster = 0;
   pPager->needSync = 0;
-  pPager->pFirstSynced = pPager->pFirst;
+  lruListSetFirstSynced(pPager);
   pPager->dbSize = -1;
 
   return (rc==SQLITE_OK?rc2:rc);
 }
@@ -18914,9 +20330,14 @@
 ** If useCksum==0 it means this journal does not use checksums.  Checksums
 ** are not used in statement journals because statement journals do not
 ** need to survive power failures.
 */
-static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int useCksum){
+static int pager_playback_one_page(
+  Pager *pPager,
+  sqlite3_file *jfd,
+  i64 offset,
+  int useCksum
+){
   int rc;
   PgHdr *pPg;                   /* An existing page in the cache */
   Pgno pgno;                    /* The page number of a page in journal */
   u32 cksum;                    /* Checksum used for sanity checking */
@@ -18927,11 +20348,11 @@
   */
   assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
   assert( aData );
 
-  rc = read32bits(jfd, &pgno);
+  rc = read32bits(jfd, offset, &pgno);
   if( rc!=SQLITE_OK ) return rc;
-  rc = sqlite3OsRead(jfd, aData, pPager->pageSize);
+  rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
   if( rc!=SQLITE_OK ) return rc;
   pPager->journalOff += pPager->pageSize + 4;
 
   /* Sanity checking on the page.  This is more important that I originally
@@ -18945,9 +20366,9 @@
   if( pgno>(unsigned)pPager->dbSize ){
     return SQLITE_OK;
   }
   if( useCksum ){
-    rc = read32bits(jfd, &cksum);
+    rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
     if( rc ) return rc;
     pPager->journalOff += 4;
     if( pager_cksum(pPager, aData)!=cksum ){
       return SQLITE_DONE;
@@ -18987,12 +20408,10 @@
   pPg = pager_lookup(pPager, pgno);
   PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
                PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
   if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
-    rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
-    if( rc==SQLITE_OK ){
-      rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize);
-    }
+    i64 offset = (pgno-1)*(i64)pPager->pageSize;
+    rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
     if( pPg ){
       makeClean(pPg);
     }
   }
@@ -19030,69 +20449,81 @@
 ** file that referred to the master journal file has just been rolled back.
 ** This routine checks if it is possible to delete the master journal file,
 ** and does so if it is.
 **
+** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
+** available for use within this function.
+**
+**
 ** The master journal file contains the names of all child journals.
 ** To tell if a master journal can be deleted, check to each of the
 ** children.  If all children are either missing or do not refer to
 ** a different master journal, then this master journal can be deleted.
 */
-static int pager_delmaster(const char *zMaster){
+static int pager_delmaster(Pager *pPager, const char *zMaster){
+  sqlite3_vfs *pVfs = pPager->pVfs;
   int rc;
   int master_open = 0;
-  OsFile *master = 0;
+  sqlite3_file *pMaster;
+  sqlite3_file *pJournal;
   char *zMasterJournal = 0; /* Contents of master journal file */
   i64 nMasterJournal;       /* Size of master journal file */
 
   /* Open the master journal file exclusively in case some other process
   ** is running this routine also. Not that it makes too much difference.
   */
-  rc = sqlite3OsOpenReadOnly(zMaster, &master);
-  assert( rc!=SQLITE_OK || master );
+  pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
+  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
+  if( !pMaster ){
+    rc = SQLITE_NOMEM;
+  }else{
+    int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
+    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
+  }
   if( rc!=SQLITE_OK ) goto delmaster_out;
   master_open = 1;
-  rc = sqlite3OsFileSize(master, &nMasterJournal);
+
+  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
   if( rc!=SQLITE_OK ) goto delmaster_out;
 
   if( nMasterJournal>0 ){
     char *zJournal;
     char *zMasterPtr = 0;
+    int nMasterPtr = pPager->pVfs->mxPathname+1;
 
     /* Load the entire master journal file into space obtained from
-    ** sqliteMalloc() and pointed to by zMasterJournal.
-    */
-    zMasterJournal = (char *)sqliteMalloc(nMasterJournal);
+    ** sqlite3_malloc() and pointed to by zMasterJournal.
+    */
+    zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
     if( !zMasterJournal ){
       rc = SQLITE_NOMEM;
       goto delmaster_out;
     }
-    rc = sqlite3OsRead(master, zMasterJournal, nMasterJournal);
+    zMasterPtr = &zMasterJournal[nMasterJournal];
+    rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
     if( rc!=SQLITE_OK ) goto delmaster_out;
 
     zJournal = zMasterJournal;
     while( (zJournal-zMasterJournal)<nMasterJournal ){
-      if( sqlite3OsFileExists(zJournal) ){
+      if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
         /* One of the journals pointed to by the master journal exists.
         ** Open it and check if it points at the master journal. If
         ** so, return without deleting the master journal file.
         */
-        OsFile *journal = 0;
         int c;
-
-        rc = sqlite3OsOpenReadOnly(zJournal, &journal);
-        assert( rc!=SQLITE_OK || journal );
+        int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
+        rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
+        if( rc!=SQLITE_OK ){
+          goto delmaster_out;
+        }
+
+        rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
+        sqlite3OsClose(pJournal);
         if( rc!=SQLITE_OK ){
           goto delmaster_out;
         }
 
-        rc = readMasterJournal(journal, &zMasterPtr);
-        sqlite3OsClose(&journal);
-        if( rc!=SQLITE_OK ){
-          goto delmaster_out;
-        }
-
-        c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0;
-        sqliteFree(zMasterPtr);
+        c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
         if( c ){
           /* We have a match. Do not delete the master journal file. */
           goto delmaster_out;
         }
@@ -19100,17 +20531,18 @@
       zJournal += (strlen(zJournal)+1);
     }
   }
 
-  rc = sqlite3OsDelete(zMaster);
+  rc = sqlite3OsDelete(pVfs, zMaster, 0);
 
 delmaster_out:
   if( zMasterJournal ){
-    sqliteFree(zMasterJournal);
+    sqlite3_free(zMasterJournal);
   }
   if( master_open ){
-    sqlite3OsClose(&master);
-  }
+    sqlite3OsClose(pMaster);
+  }
+  sqlite3_free(pMaster);
   return rc;
 }
 
 
@@ -19121,9 +20553,9 @@
 ** indicated. Also truncate the cached representation of the file.
 */
 static int pager_truncate(Pager *pPager, int nPage){
   int rc = SQLITE_OK;
-  if( pPager->state>=PAGER_EXCLUSIVE ){
+  if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
     rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
   }
   if( rc==SQLITE_OK ){
     pPager->dbSize = nPage;
@@ -19138,9 +20570,16 @@
 ** The sector size is the larger of the sector size reported
 ** by sqlite3OsSectorSize() and the pageSize.
 */
 static void setSectorSize(Pager *pPager){
-  pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
+  assert(pPager->fd->pMethods||pPager->tempFile);
+  if( !pPager->tempFile ){
+    /* Sector size doesn't matter for temporary files. Also, the file
+    ** may not have been opened yet, in whcih case the OsSectorSize()
+    ** call will segfault.
+    */
+    pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
+  }
   if( pPager->sectorSize<pPager->pageSize ){
     pPager->sectorSize = pPager->pageSize;
   }
 }
@@ -19198,8 +20637,9 @@
 ** If an I/O or malloc() error occurs, the journal-file is not deleted
 ** and an error code is returned.
 */
 static int pager_playback(Pager *pPager, int isHot){
+  sqlite3_vfs *pVfs = pPager->pVfs;
   i64 szJ;                 /* Size of the journal file in bytes */
   u32 nRec;                /* Number of Records in the journal */
   int i;                   /* Loop counter */
   Pgno mxPg = 0;           /* Size of the original file in pages */
@@ -19219,18 +20659,20 @@
   ** If a master journal file name is specified, but the file is not
   ** present on disk, then the journal is not hot and does not need to be
   ** played back.
   */
-  rc = readMasterJournal(pPager->jfd, &zMaster);
+  zMaster = pPager->pTmpSpace;
+  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
   assert( rc!=SQLITE_DONE );
-  if( rc!=SQLITE_OK || (zMaster && !sqlite3OsFileExists(zMaster)) ){
-    sqliteFree(zMaster);
+  if( rc!=SQLITE_OK
+   || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
+  ){
     zMaster = 0;
     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
     goto end_playback;
   }
-  sqlite3OsSeek(pPager->jfd, 0);
   pPager->journalOff = 0;
+  zMaster = 0;
 
   /* This loop terminates either when the readJournalHdr() call returns
   ** SQLITE_DONE or an IO error occurs. */
   while( 1 ){
@@ -19258,12 +20700,17 @@
       nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
     }
 
     /* If nRec is 0 and this rollback is of a transaction created by this
-    ** process. In this case the rest of the journal file consists of
-    ** journalled copies of pages that need to be read back into the cache.
-    */
-    if( nRec==0 && !isHot ){
+    ** process and if this is the final header in the journal, then it means
+    ** that this part of the journal was being filled but has not yet been
+    ** synced to disk.  Compute the number of pages based on the remaining
+    ** size of the file.
+    **
+    ** The third term of the test was added to fix ticket #2565.
+    */
+    if( nRec==0 && !isHot &&
+        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
       nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
     }
 
     /* If this is the first header read from the journal, truncate the
@@ -19278,9 +20725,9 @@
 
     /* Copy original pages out of the journal and back into the database file.
     */
     for(i=0; i<nRec; i++){
-      rc = pager_playback_one_page(pPager, pPager->jfd, 1);
+      rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
       if( rc!=SQLITE_OK ){
         if( rc==SQLITE_DONE ){
           rc = SQLITE_OK;
           pPager->journalOff = szJ;
@@ -19295,18 +20742,19 @@
   assert( 0 );
 
 end_playback:
   if( rc==SQLITE_OK ){
+    zMaster = pPager->pTmpSpace;
+    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
+  }
+  if( rc==SQLITE_OK ){
     rc = pager_end_transaction(pPager);
   }
-  if( zMaster ){
+  if( rc==SQLITE_OK && zMaster[0] ){
     /* If there was a master journal and this routine will return success,
     ** see if it is possible to delete the master journal.
     */
-    if( rc==SQLITE_OK ){
-      rc = pager_delmaster(zMaster);
-    }
-    sqliteFree(zMaster);
+    rc = pager_delmaster(pPager, zMaster);
   }
 
   /* The Pager.sectorSize variable may have been updated while rolling
   ** back a journal created by a process with a different sector size
@@ -19365,18 +20813,18 @@
 
   /* Figure out how many records are in the statement journal.
   */
   assert( pPager->stmtInUse && pPager->journalOpen );
-  sqlite3OsSeek(pPager->stfd, 0);
   nRec = pPager->stmtNRec;
 
   /* Copy original pages out of the statement journal and back into the
   ** database file.  Note that the statement journal omits checksums from
   ** each record since power-failure recovery is not important to statement
   ** journals.
   */
-  for(i=nRec-1; i>=0; i--){
-    rc = pager_playback_one_page(pPager, pPager->stfd, 0);
+  for(i=0; i<nRec; i++){
+    i64 offset = i*(4+pPager->pageSize);
+    rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
     assert( rc!=SQLITE_DONE );
     if( rc!=SQLITE_OK ) goto end_stmt_playback;
   }
 
@@ -19387,16 +20835,12 @@
   **
   ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
   ** of the first journal header written during this statement transaction.
   */
-  rc = sqlite3OsSeek(pPager->jfd, pPager->stmtJSize);
-  if( rc!=SQLITE_OK ){
-    goto end_stmt_playback;
-  }
   pPager->journalOff = pPager->stmtJSize;
   pPager->cksumInit = pPager->stmtCksum;
   while( pPager->journalOff < hdrOff ){
-    rc = pager_playback_one_page(pPager, pPager->jfd, 1);
+    rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
     assert( rc!=SQLITE_DONE );
     if( rc!=SQLITE_OK ) goto end_stmt_playback;
   }
 
@@ -19411,9 +20855,9 @@
     if( nJRec==0 ){
       nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
     }
     for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
-      rc = pager_playback_one_page(pPager, pPager->jfd, 1);
+      rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
       assert( rc!=SQLITE_DONE );
       if( rc!=SQLITE_OK ) goto end_stmt_playback;
     }
   }
@@ -19468,9 +20912,9 @@
 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
   pPager->noSync =  level==1 || pPager->tempFile;
   pPager->fullSync = level==3 && !pPager->tempFile;
-  pPager->full_fsync = full_fsync;
+  pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
   if( pPager->noSync ) pPager->needSync = 0;
 }
 #endif
 
@@ -19479,34 +20923,35 @@
 ** attempts to open a temporary file.  This information is used for
 ** testing and analysis only.
 */
 #ifdef SQLITE_TEST
-int sqlite3_opentemp_count = 0;
+SQLITE_API int sqlite3_opentemp_count = 0;
 #endif
 
 /*
 ** Open a temporary file.
 **
 ** Write the file descriptor into *fd.  Return SQLITE_OK on success or some
-** other error code if we fail.
-**
-** The OS will automatically delete the temporary file when it is
-** closed.
-*/
-static int sqlite3PagerOpentemp(OsFile **pFd){
-  int cnt = 8;
-  int rc;
-  char zFile[SQLITE_TEMPNAME_SIZE];
+** other error code if we fail. The OS will automatically delete the temporary
+** file when it is closed.
+*/
+static int sqlite3PagerOpentemp(
+  sqlite3_vfs *pVfs,    /* The virtual file system layer */
+  sqlite3_file *pFile,  /* Write the file descriptor here */
+  char *zFilename,      /* Name of the file.  Might be NULL */
+  int vfsFlags          /* Flags passed through to the VFS */
+){
+  int rc;
+  assert( zFilename!=0 );
 
 #ifdef SQLITE_TEST
   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
 #endif
-  do{
-    cnt--;
-    sqlite3OsTempFileName(zFile);
-    rc = sqlite3OsOpenExclusive(zFile, pFd, 1);
-    assert( rc!=SQLITE_OK || *pFd );
-  }while( cnt>0 && rc!=SQLITE_OK && rc!=SQLITE_NOMEM );
+
+  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
+            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
+  rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
+  assert( rc!=SQLITE_OK || pFile->pMethods );
   return rc;
 }
 
 /*
@@ -19523,126 +20968,174 @@
 ** It is never written to disk.  This can be used to implement an
 ** in-memory database.
 */
 SQLITE_PRIVATE int sqlite3PagerOpen(
+  sqlite3_vfs *pVfs,       /* The virtual file system to use */
   Pager **ppPager,         /* Return the Pager structure here */
   const char *zFilename,   /* Name of the database file to open */
   int nExtra,              /* Extra bytes append to each in-memory page */
-  int flags                /* flags controlling this file */
-){
-  Pager *pPager = 0;
-  char *zFullPathname = 0;
-  int nameLen;  /* Compiler is wrong. This is always initialized before use */
-  OsFile *fd = 0;
+  int flags,               /* flags controlling this file */
+  int vfsFlags             /* flags passed through to sqlite3_vfs.xOpen() */
+){
+  u8 *pPtr;
+  Pager *pPager = 0;
   int rc = SQLITE_OK;
   int i;
   int tempFile = 0;
   int memDb = 0;
   int readOnly = 0;
   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
   int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
-  char zTemp[SQLITE_TEMPNAME_SIZE];
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to
-  ** malloc() must have already been made by this thread before it gets
-  ** to this point. This means the ThreadData must have been allocated already
-  ** so that ThreadData.nAlloc can be set. It would be nice to assert
-  ** that ThreadData.nAlloc is non-zero, but alas this breaks test cases
-  ** written to invoke the pager directly.
-  */
-  ThreadData *pTsd = sqlite3ThreadData();
-  assert( pTsd );
-#endif
-
-  /* We used to test if malloc() had already failed before proceeding.
-  ** But the way this function is used in SQLite means that can never
-  ** happen. Furthermore, if the malloc-failed flag is already set,
-  ** either the call to sqliteStrDup() or sqliteMalloc() below will
-  ** fail shortly and SQLITE_NOMEM returned anyway.
-  */
+  int journalFileSize = sqlite3JournalSize(pVfs);
+  int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
+  char *zPathname;
+  int nPathname;
+
+  /* The default return is a NULL pointer */
   *ppPager = 0;
 
-  /* Open the pager file and set zFullPathname to point at malloc()ed
-  ** memory containing the complete filename (i.e. including the directory).
-  */
+  /* Compute the full pathname */
+  zPathname = sqlite3_malloc(pVfs->mxPathname+1);
+  if( zPathname==0 ){
+    return SQLITE_NOMEM;
+  }
   if( zFilename && zFilename[0] ){
 #ifndef SQLITE_OMIT_MEMORYDB
     if( strcmp(zFilename,":memory:")==0 ){
       memDb = 1;
-      zFullPathname = sqliteStrDup("");
+      zPathname[0] = 0;
     }else
 #endif
     {
-      zFullPathname = sqlite3OsFullPathname(zFilename);
-      if( zFullPathname ){
-        rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly);
-        assert( rc!=SQLITE_OK || fd );
-      }
-    }
-  }else{
-    rc = sqlite3PagerOpentemp(&fd);
-    sqlite3OsTempFileName(zTemp);
-    zFilename = zTemp;
-    zFullPathname = sqlite3OsFullPathname(zFilename);
-    if( rc==SQLITE_OK ){
-      tempFile = 1;
-    }
-  }
-
-  /* Allocate the Pager structure. As part of the same allocation, allocate
-  ** space for the full paths of the file, directory and journal
-  ** (Pager.zFilename, Pager.zDirectory and Pager.zJournal).
-  */
-  if( zFullPathname ){
-    nameLen = strlen(zFullPathname);
-    pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
-    if( pPager && rc==SQLITE_OK ){
-      pPager->pTmpSpace = (char *)sqliteMallocRaw(SQLITE_DEFAULT_PAGE_SIZE);
-    }
-  }
-
-
-  /* If an error occured in either of the blocks above, free the memory
-  ** pointed to by zFullPathname, free the Pager structure and close the
-  ** file. Since the pager is not allocated there is no need to set
+      rc = sqlite3OsFullPathname(pVfs, zFilename, zPathname);
+    }
+  }else{
+    rc = sqlite3OsGetTempName(pVfs, zPathname);
+  }
+  if( rc!=SQLITE_OK ){
+    sqlite3_free(zPathname);
+    return rc;
+  }
+  nPathname = strlen(zPathname);
+
+  /* Allocate memory for the pager structure */
+  pPager = sqlite3MallocZero(
+    sizeof(*pPager) +           /* Pager structure */
+    journalFileSize +           /* The journal file structure */
+    pVfs->szOsFile * 2 +        /* The db and stmt journal files */
+    4*nPathname + 40            /* zFilename, zDirectory, zJournal, zStmtJrnl */
+  );
+  if( !pPager ){
+    sqlite3_free(zPathname);
+    return SQLITE_NOMEM;
+  }
+  pPtr = (u8 *)&pPager[1];
+  pPager->vfsFlags = vfsFlags;
+  pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
+  pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
+  pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
+  pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
+  pPager->zDirectory = &pPager->zFilename[nPathname+1];
+  pPager->zJournal = &pPager->zDirectory[nPathname+1];
+  pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
+  pPager->pVfs = pVfs;
+  memcpy(pPager->zFilename, zPathname, nPathname+1);
+  sqlite3_free(zPathname);
+
+  /* Open the pager file.
+  */
+  if( zFilename && zFilename[0] && !memDb ){
+    if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
+      rc = SQLITE_CANTOPEN;
+    }else{
+      int fout = 0;
+      rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
+                         pPager->vfsFlags, &fout);
+      readOnly = (fout&SQLITE_OPEN_READONLY);
+
+      /* If the file was successfully opened for read/write access,
+      ** choose a default page size in case we have to create the
+      ** database file. The default page size is the maximum of:
+      **
+      **    + SQLITE_DEFAULT_PAGE_SIZE,
+      **    + The value returned by sqlite3OsSectorSize()
+      **    + The largest page size that can be written atomically.
+      */
+      if( rc==SQLITE_OK && !readOnly ){
+        int iSectorSize = sqlite3OsSectorSize(pPager->fd);
+        if( nDefaultPage<iSectorSize ){
+          nDefaultPage = iSectorSize;
+        }
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE
+        {
+          int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
+          int ii;
+          assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
+          assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
+          assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
+          for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
+            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
+          }
+        }
+#endif
+        if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
+          nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
+        }
+      }
+    }
+  }else if( !memDb ){
+    /* If a temporary file is requested, it is not opened immediately.
+    ** In this case we accept the default page size and delay actually
+    ** opening the file until the first call to OsWrite().
+    */
+    tempFile = 1;
+    pPager->state = PAGER_EXCLUSIVE;
+  }
+
+  if( pPager && rc==SQLITE_OK ){
+    pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
+  }
+
+  /* If an error occured in either of the blocks above.
+  ** Free the Pager structure and close the file.
+  ** Since the pager is not allocated there is no need to set
   ** any Pager.errMask variables.
   */
-  if( !pPager || !zFullPathname || !pPager->pTmpSpace || rc!=SQLITE_OK ){
-    sqlite3OsClose(&fd);
-    sqliteFree(zFullPathname);
-    sqliteFree(pPager);
+  if( !pPager || !pPager->pTmpSpace ){
+    sqlite3OsClose(pPager->fd);
+    sqlite3_free(pPager);
     return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
   }
 
-  PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
-  IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
-  pPager->zFilename = (char*)&pPager[1];
-  pPager->zDirectory = &pPager->zFilename[nameLen+1];
-  pPager->zJournal = &pPager->zDirectory[nameLen+1];
-  memcpy(pPager->zFilename, zFullPathname, nameLen+1);
-  memcpy(pPager->zDirectory, zFullPathname, nameLen+1);
-
-  for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
+  PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
+  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
+
+  /* Fill in Pager.zDirectory[] */
+  memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
+  for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
   if( i>0 ) pPager->zDirectory[i-1] = 0;
-  memcpy(pPager->zJournal, zFullPathname,nameLen);
-  sqliteFree(zFullPathname);
-  memcpy(&pPager->zJournal[nameLen], "-journal",sizeof("-journal"));
-  pPager->fd = fd;
+
+  /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */
+  memcpy(pPager->zJournal, pPager->zFilename, nPathname);
+  memcpy(&pPager->zJournal[nPathname], "-journal", 9);
+  memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname);
+  memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10);
+
   /* pPager->journalOpen = 0; */
   pPager->useJournal = useJournal && !memDb;
   pPager->noReadlock = noReadlock && readOnly;
   /* pPager->stmtOpen = 0; */
   /* pPager->stmtInUse = 0; */
   /* pPager->nRef = 0; */
   pPager->dbSize = memDb-1;
-  pPager->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
+  pPager->pageSize = nDefaultPage;
   /* pPager->stmtSize = 0; */
   /* pPager->stmtJSize = 0; */
   /* pPager->nPage = 0; */
   pPager->mxPage = 100;
   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
-  assert( PAGER_UNLOCK==0 );
   /* pPager->state = PAGER_UNLOCK; */
+  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
   /* pPager->errMask = 0; */
   pPager->tempFile = tempFile;
   assert( tempFile==PAGER_LOCKINGMODE_NORMAL
           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
@@ -19652,22 +21145,35 @@
   pPager->readOnly = readOnly;
   /* pPager->needSync = 0; */
   pPager->noSync = pPager->tempFile || !useJournal;
   pPager->fullSync = (pPager->noSync?0:1);
+  pPager->sync_flags = SQLITE_SYNC_NORMAL;
   /* pPager->pFirst = 0; */
   /* pPager->pFirstSynced = 0; */
   /* pPager->pLast = 0; */
   pPager->nExtra = FORCE_ALIGNMENT(nExtra);
-  assert(fd||memDb);
+  assert(pPager->fd->pMethods||memDb||tempFile);
   if( !memDb ){
     setSectorSize(pPager);
   }
   /* pPager->pBusyHandler = 0; */
   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
   *ppPager = pPager;
 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  pPager->pNext = pTsd->pPager;
-  pTsd->pPager = pPager;
+  pPager->iInUseMM = 0;
+  pPager->iInUseDB = 0;
+  if( !memDb ){
+    sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
+    sqlite3_mutex_enter(mutex);
+    pPager->pNext = sqlite3PagerList;
+    if( sqlite3PagerList ){
+      assert( sqlite3PagerList->pPrev==0 );
+      sqlite3PagerList->pPrev = pPager;
+    }
+    pPager->pPrev = 0;
+    sqlite3PagerList = pPager;
+    sqlite3_mutex_leave(mutex);
+  }
 #endif
   return SQLITE_OK;
 }
 
@@ -19701,20 +21207,34 @@
   pPager->xReiniter = xReinit;
 }
 
 /*
-** Set the page size.  Return the new size.  If the suggest new page
-** size is inappropriate, then an alternative page size is selected
-** and returned.
-*/
-SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
-  assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
-  if( !pPager->memDb && pPager->nRef==0 ){
-    pager_reset(pPager);
-    pPager->pageSize = pageSize;
-    pPager->pTmpSpace = sqlite3ReallocOrFree(pPager->pTmpSpace, pageSize);
-  }
-  return pPager->pageSize;
+** Set the page size to *pPageSize. If the suggest new page size is
+** inappropriate, then an alternative page size is set to that
+** value before returning.
+*/
+SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
+  int rc = SQLITE_OK;
+  u16 pageSize = *pPageSize;
+  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
+  if( pageSize && pageSize!=pPager->pageSize
+   && !pPager->memDb && pPager->nRef==0
+  ){
+    char *pNew = (char *)sqlite3_malloc(pageSize);
+    if( !pNew ){
+      rc = SQLITE_NOMEM;
+    }else{
+      pagerEnter(pPager);
+      pager_reset(pPager);
+      pPager->pageSize = pageSize;
+      setSectorSize(pPager);
+      sqlite3_free(pPager->pTmpSpace);
+      pPager->pTmpSpace = pNew;
+      pagerLeave(pPager);
+    }
+  }
+  *pPageSize = pPager->pageSize;
+  return rc;
 }
 
 /*
 ** Attempt to set the maximum database page count if mxPage is positive.
@@ -19739,10 +21259,10 @@
 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
 ** and generate no code.
 */
 #ifdef SQLITE_TEST
-extern int sqlite3_io_error_pending;
-extern int sqlite3_io_error_hit;
+SQLITE_API extern int sqlite3_io_error_pending;
+SQLITE_API extern int sqlite3_io_error_hit;
 static int saved_cnt;
 void disable_simulated_io_errors(void){
   saved_cnt = sqlite3_io_error_pending;
   sqlite3_io_error_pending = -1;
@@ -19767,14 +21287,12 @@
 */
 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
   int rc = SQLITE_OK;
   memset(pDest, 0, N);
-  if( MEMDB==0 ){
-    disable_simulated_io_errors();
-    sqlite3OsSeek(pPager->fd, 0);
-    enable_simulated_io_errors();
+  assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
+  if( pPager->fd->pMethods ){
     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
-    rc = sqlite3OsRead(pPager->fd, pDest, N);
+    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
     if( rc==SQLITE_IOERR_SHORT_READ ){
       rc = SQLITE_OK;
     }
   }
@@ -19790,9 +21308,9 @@
 ** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
 ** file is 4096 bytes, 5 is returned instead of 4.
 */
 SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager){
-  i64 n;
+  i64 n = 0;
   int rc;
   assert( pPager!=0 );
   if( pPager->errCode ){
     return 0;
@@ -19799,9 +21317,11 @@
   }
   if( pPager->dbSize>=0 ){
     n = pPager->dbSize;
   } else {
-    if( (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
+    assert(pPager->fd->pMethods||pPager->tempFile);
+    if( (pPager->fd->pMethods)
+     && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
       pager_error(pPager, rc);
       return 0;
     }
     if( n>0 && n<pPager->pageSize ){
@@ -19827,10 +21347,10 @@
 /*
 ** Clear a PgHistory block
 */
 static void clearHistory(PgHistory *pHist){
-  sqliteFree(pHist->pOrig);
-  sqliteFree(pHist->pStmt);
+  sqlite3_free(pHist->pOrig);
+  sqlite3_free(pHist->pStmt);
   pHist->pOrig = 0;
   pHist->pStmt = 0;
 }
 #else
@@ -19876,29 +21396,10 @@
 */
 static void unlinkPage(PgHdr *pPg){
   Pager *pPager = pPg->pPager;
 
-  /* Keep the pFirstSynced pointer pointing at the first synchronized page */
-  if( pPg==pPager->pFirstSynced ){
-    PgHdr *p = pPg->pNextFree;
-    while( p && p->needSync ){ p = p->pNextFree; }
-    pPager->pFirstSynced = p;
-  }
-
-  /* Unlink from the freelist */
-  if( pPg->pPrevFree ){
-    pPg->pPrevFree->pNextFree = pPg->pNextFree;
-  }else{
-    assert( pPager->pFirst==pPg );
-    pPager->pFirst = pPg->pNextFree;
-  }
-  if( pPg->pNextFree ){
-    pPg->pNextFree->pPrevFree = pPg->pPrevFree;
-  }else{
-    assert( pPager->pLast==pPg );
-    pPager->pLast = pPg->pPrevFree;
-  }
-  pPg->pNextFree = pPg->pPrevFree = 0;
+  /* Unlink from free page list */
+  lruListRemove(pPg);
 
   /* Unlink from the pgno hash table */
   unlinkHashChain(pPager, pPg);
 }
@@ -19932,9 +21433,10 @@
       IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
       PAGER_INCR(sqlite3_pager_pgfree_count);
       unlinkPage(pPg);
       makeClean(pPg);
-      sqliteFree(pPg);
+      sqlite3_free(pPg->pData);
+      sqlite3_free(pPg);
       pPager->nPage--;
     }
   }
 }
@@ -19990,15 +21492,19 @@
     pPager->dbSize = nPage;
     pager_truncate_cache(pPager);
     return SQLITE_OK;
   }
+  pagerEnter(pPager);
   rc = syncJournal(pPager);
+  pagerLeave(pPager);
   if( rc!=SQLITE_OK ){
     return rc;
   }
 
   /* Get an exclusive lock on the database before truncating. */
+  pagerEnter(pPager);
   rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
+  pagerLeave(pPager);
   if( rc!=SQLITE_OK ){
     return rc;
   }
 
@@ -20021,16 +21527,21 @@
 ** to the caller.
 */
 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  /* A malloc() cannot fail in sqlite3ThreadData() as one or more calls to
-  ** malloc() must have already been made by this thread before it gets
-  ** to this point. This means the ThreadData must have been allocated already
-  ** so that ThreadData.nAlloc can be set.
-  */
-  ThreadData *pTsd = sqlite3ThreadData();
-  assert( pPager );
-  assert( pTsd && pTsd->nAlloc );
+  if( !MEMDB ){
+    sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
+    sqlite3_mutex_enter(mutex);
+    if( pPager->pPrev ){
+      pPager->pPrev->pNext = pPager->pNext;
+    }else{
+      sqlite3PagerList = pPager->pNext;
+    }
+    if( pPager->pNext ){
+      pPager->pNext->pPrev = pPager->pPrev;
+    }
+    sqlite3_mutex_leave(mutex);
+  }
 #endif
 
   disable_simulated_io_errors();
   pPager->errCode = 0;
@@ -20041,36 +21552,24 @@
   PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
   IOTRACE(("CLOSE %p\n", pPager))
   assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
   if( pPager->journalOpen ){
-    sqlite3OsClose(&pPager->jfd);
-  }
-  sqliteFree(pPager->aInJournal);
+    sqlite3OsClose(pPager->jfd);
+  }
+  sqlite3_free(pPager->aInJournal);
   if( pPager->stmtOpen ){
-    sqlite3OsClose(&pPager->stfd);
-  }
-  sqlite3OsClose(&pPager->fd);
+    sqlite3OsClose(pPager->stfd);
+  }
+  sqlite3OsClose(pPager->fd);
   /* Temp files are automatically deleted by the OS
   ** if( pPager->tempFile ){
   **   sqlite3OsDelete(pPager->zFilename);
   ** }
   */
 
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
-  /* Remove the pager from the linked list of pagers starting at
-  ** ThreadData.pPager if memory-management is enabled.
-  */
-  if( pPager==pTsd->pPager ){
-    pTsd->pPager = pPager->pNext;
-  }else{
-    Pager *pTmp;
-    for(pTmp = pTsd->pPager; pTmp->pNext!=pPager; pTmp=pTmp->pNext){}
-    pTmp->pNext = pPager->pNext;
-  }
-#endif
-  sqliteFree(pPager->aHash);
-  sqliteFree(pPager->pTmpSpace);
-  sqliteFree(pPager);
+  sqlite3_free(pPager->aHash);
+  sqlite3_free(pPager->pTmpSpace);
+  sqlite3_free(pPager);
   return SQLITE_OK;
 }
 
 #if !defined(NDEBUG) || defined(SQLITE_TEST)
@@ -20093,23 +21592,9 @@
 */
 static void _page_ref(PgHdr *pPg){
   if( pPg->nRef==0 ){
     /* The page is currently on the freelist.  Remove it. */
-    if( pPg==pPg->pPager->pFirstSynced ){
-      PgHdr *p = pPg->pNextFree;
-      while( p && p->needSync ){ p = p->pNextFree; }
-      pPg->pPager->pFirstSynced = p;
-    }
-    if( pPg->pPrevFree ){
-      pPg->pPrevFree->pNextFree = pPg->pNextFree;
-    }else{
-      pPg->pPager->pFirst = pPg->pNextFree;
-    }
-    if( pPg->pNextFree ){
-      pPg->pNextFree->pPrevFree = pPg->pPrevFree;
-    }else{
-      pPg->pPager->pLast = pPg->pPrevFree;
-    }
+    lruListRemove(pPg);
     pPg->pPager->nRef++;
   }
   pPg->nRef++;
   REFINFO(pPg);
@@ -20131,9 +21616,11 @@
 ** Increment the reference count for a page.  The input pointer is
 ** a reference to the page data.
 */
 SQLITE_PRIVATE int sqlite3PagerRef(DbPage *pPg){
+  pagerEnter(pPg->pPager);
   page_ref(pPg);
+  pagerLeave(pPg->pPager);
   return SQLITE_OK;
 }
 
 /*
@@ -20150,9 +21637,14 @@
 ** If the sync mode is FULL, two syncs will occur.  First the whole journal
 ** is synced, then the nRec field is updated, then a second sync occurs.
 **
 ** For temporary databases, we do not care if we are able to rollback
-** after a power failure, so sync occurs.
+** after a power failure, so no sync occurs.
+**
+** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
+** the database is stored, then OsSync() is never called on the journal
+** file. In this case all that is required is to update the nRec field in
+** the journal header.
 **
 ** This routine clears the needSync field of every page current held in
 ** memory.
 */
@@ -20159,14 +21651,17 @@
 static int syncJournal(Pager *pPager){
   PgHdr *pPg;
   int rc = SQLITE_OK;
 
+
   /* Sync the journal before modifying the main database
   ** (assuming there is a journal and it needs to be synced.)
   */
   if( pPager->needSync ){
     if( !pPager->tempFile ){
+      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
       assert( pPager->journalOpen );
+
       /* assert( !pPager->noSync ); // noSync might be set if synchronous
       ** was turned off after the transaction was started.  Ticket #615 */
 #ifndef NDEBUG
       {
@@ -20178,35 +21673,41 @@
         if( rc!=0 ) return rc;
         assert( pPager->journalOff==jSz );
       }
 #endif
-      {
+      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
         /* Write the nRec value into the journal file header. If in
         ** full-synchronous mode, sync the journal first. This ensures that
         ** all data has really hit the disk before nRec is updated to mark
         ** it as a candidate for rollback.
-        */
-        if( pPager->fullSync ){
+        **
+        ** This is not required if the persistent media supports the
+        ** SAFE_APPEND property. Because in this case it is not possible
+        ** for garbage data to be appended to the file, the nRec field
+        ** is populated with 0xFFFFFFFF when the journal header is written
+        ** and never needs to be updated.
+        */
+        i64 jrnlOff;
+        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
           PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
           IOTRACE(("JSYNC %p\n", pPager))
-          rc = sqlite3OsSync(pPager->jfd, 0);
+          rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
           if( rc!=0 ) return rc;
         }
-        rc = sqlite3OsSeek(pPager->jfd,
-                           pPager->journalHdr + sizeof(aJournalMagic));
-        if( rc ) return rc;
-        IOTRACE(("JHDR %p %lld %d\n", pPager,
-                  pPager->journalHdr + sizeof(aJournalMagic), 4))
-        rc = write32bits(pPager->jfd, pPager->nRec);
-        if( rc ) return rc;
-
-        rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff);
+
+        jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
+        IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
+        rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
         if( rc ) return rc;
       }
-      PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
-      IOTRACE(("JSYNC %p\n", pPager))
-      rc = sqlite3OsSync(pPager->jfd, pPager->full_fsync);
-      if( rc!=0 ) return rc;
+      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
+        PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
+        IOTRACE(("JSYNC %p\n", pPager))
+        rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
+          (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
+        );
+        if( rc!=0 ) return rc;
+      }
       pPager->journalStarted = 1;
     }
     pPager->needSync = 0;
 
@@ -20214,9 +21715,9 @@
     */
     for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
       pPg->needSync = 0;
     }
-    pPager->pFirstSynced = pPager->pFirst;
+    lruListSetFirstSynced(pPager);
   }
 
 #ifndef NDEBUG
   /* If the Pager.needSync flag is clear then the PgHdr.needSync
@@ -20226,9 +21727,9 @@
   else{
     for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
       assert( pPg->needSync==0 );
     }
-    assert( pPager->pFirstSynced==pPager->pFirst );
+    assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
   }
 #endif
 
   return rc;
@@ -20314,8 +21815,9 @@
 ** as clean.
 */
 static int pager_write_pagelist(PgHdr *pList){
   Pager *pPager;
+  PgHdr *p;
   int rc;
 
   if( pList==0 ) return SQLITE_OK;
   pPager = pList->pPager;
@@ -20341,23 +21843,34 @@
     return rc;
   }
 
   pList = sort_pagelist(pList);
+  for(p=pList; p; p=p->pDirty){
+    assert( p->dirty );
+    p->dirty = 0;
+  }
   while( pList ){
-    assert( pList->dirty );
-    rc = sqlite3OsSeek(pPager->fd, (pList->pgno-1)*(i64)pPager->pageSize);
-    if( rc ) return rc;
+
+    /* If the file has not yet been opened, open it now. */
+    if( !pPager->fd->pMethods ){
+      assert(pPager->tempFile);
+      rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
+                                pPager->vfsFlags);
+      if( rc ) return rc;
+    }
+
     /* If there are dirty pages in the page cache with page numbers greater
     ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
     ** make the file smaller (presumably by auto-vacuum code). Do not write
     ** any such pages to the file.
     */
     if( pList->pgno<=pPager->dbSize ){
+      i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
       char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
       PAGERTRACE4("STORE %d page %d hash(%08x)\n",
                    PAGERID(pPager), pList->pgno, pager_pagehash(pList));
       IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
-      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize);
+      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
       PAGER_INCR(sqlite3_pager_writedb_count);
       PAGER_INCR(pPager->nWrite);
       if( pList->pgno==1 ){
         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
@@ -20368,9 +21881,8 @@
       PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
     }
 #endif
     if( rc ) return rc;
-    pList->dirty = 0;
 #ifdef SQLITE_CHECK_PAGES
     pList->pageHash = pager_pagehash(pList);
 #endif
     pList = pList->pDirty;
@@ -20395,17 +21907,18 @@
 ** exists, that is probably an old journal left over from a prior
 ** database with the same name.  Just delete the journal.
 */
 static int hasHotJournal(Pager *pPager){
+  sqlite3_vfs *pVfs = pPager->pVfs;
   if( !pPager->useJournal ) return 0;
-  if( !sqlite3OsFileExists(pPager->zJournal) ){
+  if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
     return 0;
   }
   if( sqlite3OsCheckReservedLock(pPager->fd) ){
     return 0;
   }
   if( sqlite3PagerPagecount(pPager)==0 ){
-    sqlite3OsDelete(pPager->zJournal);
+    sqlite3OsDelete(pVfs, pPager->zJournal, 0);
     return 0;
   }else{
     return 1;
   }
@@ -20416,30 +21929,35 @@
 **
 ** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
 ** does not set the pPager->errCode variable.
 */
-static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
+static int pager_recycle(Pager *pPager, PgHdr **ppPg){
   PgHdr *pPg;
   *ppPg = 0;
 
+  /* It is illegal to call this function unless the pager object
+  ** pointed to by pPager has at least one free page (page with nRef==0).
+  */
   assert(!MEMDB);
+  assert(pPager->lru.pFirst);
 
   /* Find a page to recycle.  Try to locate a page that does not
   ** require us to do an fsync() on the journal.
   */
-  pPg = pPager->pFirstSynced;
+  pPg = pPager->lru.pFirstSynced;
 
   /* If we could not find a page that does not require an fsync()
   ** on the journal file then fsync the journal file.  This is a
   ** very slow operation, so we work hard to avoid it.  But sometimes
   ** it can't be helped.
   */
-  if( pPg==0 && pPager->pFirst && syncOk && !MEMDB){
+  if( pPg==0 && pPager->lru.pFirst){
+    int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
     int rc = syncJournal(pPager);
     if( rc!=0 ){
       return rc;
     }
-    if( pPager->fullSync ){
+    if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
       /* If in full-sync mode, write a new journal header into the
       ** journal file. This is done to avoid ever modifying a journal
       ** header that is involved in the rollback of pages that have
       ** already been written to the database (in case the header is
@@ -20452,12 +21970,9 @@
       if( rc!=0 ){
         return rc;
       }
     }
-    pPg = pPager->pFirst;
-  }
-  if( pPg==0 ){
-    return SQLITE_OK;
+    pPg = pPager->lru.pFirst;
   }
 
   assert( pPg->nRef==0 );
 
@@ -20469,8 +21984,9 @@
     makeClean(pPg);
     pPg->dirty = 1;
     pPg->pDirty = 0;
     rc = pager_write_pagelist( pPg );
+    pPg->dirty = 0;
     if( rc!=SQLITE_OK ){
       return rc;
     }
   }
@@ -20497,107 +22013,138 @@
   *ppPg = pPg;
   return SQLITE_OK;
 }
 
+#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
 /*
 ** This function is called to free superfluous dynamically allocated memory
 ** held by the pager system. Memory in use by any SQLite pager allocated
-** by the current thread may be sqliteFree()ed.
+** by the current thread may be sqlite3_free()ed.
 **
 ** nReq is the number of bytes of memory required. Once this much has
-** been released, the function returns. A negative value for nReq means
-** free as much memory as possible. The return value is the total number
+** been released, the function returns. The return value is the total number
 ** of bytes of memory released.
 */
-#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
 SQLITE_PRIVATE int sqlite3PagerReleaseMemory(int nReq){
-  const ThreadData *pTsdro = sqlite3ThreadDataReadOnly();
-  int nReleased = 0;
-  int i;
-
-  /* If the the global mutex is held, this subroutine becomes a
-  ** o-op; zero bytes of memory are freed.  This is because
-  ** some of the code invoked by this function may also
-  ** try to obtain the mutex, resulting in a deadlock.
-  */
-  if( sqlite3OsInMutex(0) ){
-    return 0;
-  }
-
-  /* Outermost loop runs for at most two iterations. First iteration we
-  ** try to find memory that can be released without calling fsync(). Second
-  ** iteration (which only runs if the first failed to free nReq bytes of
-  ** memory) is permitted to call fsync(). This is of course much more
-  ** expensive.
-  */
-  for(i=0; i<=1; i++){
-
-    /* Loop through all the SQLite pagers opened by the current thread. */
-    Pager *pPager = pTsdro->pPager;
-    for( ; pPager && (nReq<0 || nReleased<nReq); pPager=pPager->pNext){
-      PgHdr *pPg;
-      int rc;
-
-      if( MEMDB ){
-        continue;
-      }
-
-      /* For each pager, try to free as many pages as possible (without
-      ** calling fsync() if this is the first iteration of the outermost
-      ** loop).
-      */
-      while( SQLITE_OK==(rc = pager_recycle(pPager, i, &pPg)) && pPg) {
-        /* We've found a page to free. At this point the page has been
-        ** removed from the page hash-table, free-list and synced-list
-        ** (pFirstSynced). It is still in the all pages (pAll) list.
-        ** Remove it from this list before freeing.
-        **
-        ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
-        ** probably is though.
-        */
-        PgHdr *pTmp;
-        assert( pPg );
-        if( pPg==pPager->pAll ){
-           pPager->pAll = pPg->pNextAll;
-        }else{
-          for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
-          pTmp->pNextAll = pPg->pNextAll;
-        }
-        nReleased += sqliteAllocSize(pPg);
-        IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
-        PAGER_INCR(sqlite3_pager_pgfree_count);
-        sqliteFree(pPg);
-      }
-
-      if( rc!=SQLITE_OK ){
-        /* An error occured whilst writing to the database file or
-        ** journal in pager_recycle(). The error is not returned to the
-        ** caller of this function. Instead, set the Pager.errCode variable.
-        ** The error will be returned to the user (or users, in the case
-        ** of a shared pager cache) of the pager for which the error occured.
-        */
-        assert( (rc&0xff)==SQLITE_IOERR || rc==SQLITE_FULL );
-        assert( pPager->state>=PAGER_RESERVED );
-        pager_error(pPager, rc);
-      }
-    }
-  }
-
+  int nReleased = 0;          /* Bytes of memory released so far */
+  sqlite3_mutex *mutex;       /* The MEM2 mutex */
+  Pager *pPager;              /* For looping over pagers */
+  int rc = SQLITE_OK;
+
+  /* Acquire the memory-management mutex
+  */
+  mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
+  sqlite3_mutex_enter(mutex);
+
+  /* Signal all database connections that memory management wants
+  ** to have access to the pagers.
+  */
+  for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
+     pPager->iInUseMM = 1;
+  }
+
+  while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
+    PgHdr *pPg;
+    PgHdr *pRecycled;
+
+    /* Try to find a page to recycle that does not require a sync(). If
+    ** this is not possible, find one that does require a sync().
+    */
+    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+    pPg = sqlite3LruPageList.pFirstSynced;
+    while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
+      pPg = pPg->gfree.pNext;
+    }
+    if( !pPg ){
+      pPg = sqlite3LruPageList.pFirst;
+      while( pPg && pPg->pPager->iInUseDB ){
+        pPg = pPg->gfree.pNext;
+      }
+    }
+    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
+
+    /* If pPg==0, then the block above has failed to find a page to
+    ** recycle. In this case return early - no further memory will
+    ** be released.
+    */
+    if( !pPg ) break;
+
+    pPager = pPg->pPager;
+    assert(!pPg->needSync || pPg==pPager->lru.pFirst);
+    assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
+
+    rc = pager_recycle(pPager, &pRecycled);
+    assert(pRecycled==pPg || rc!=SQLITE_OK);
+    if( rc==SQLITE_OK ){
+      /* We've found a page to free. At this point the page has been
+      ** removed from the page hash-table, free-list and synced-list
+      ** (pFirstSynced). It is still in the all pages (pAll) list.
+      ** Remove it from this list before freeing.
+      **
+      ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
+      ** probably is though.
+      */
+      PgHdr *pTmp;
+      assert( pPg );
+      if( pPg==pPager->pAll ){
+         pPager->pAll = pPg->pNextAll;
+      }else{
+        for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
+        pTmp->pNextAll = pPg->pNextAll;
+      }
+      nReleased += (
+          sizeof(*pPg) + pPager->pageSize
+          + sizeof(u32) + pPager->nExtra
+          + MEMDB*sizeof(PgHistory)
+      );
+      IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
+      PAGER_INCR(sqlite3_pager_pgfree_count);
+      sqlite3_free(pPg->pData);
+      sqlite3_free(pPg);
+      pPager->nPage--;
+    }else{
+      /* An error occured whilst writing to the database file or
+      ** journal in pager_recycle(). The error is not returned to the
+      ** caller of this function. Instead, set the Pager.errCode variable.
+      ** The error will be returned to the user (or users, in the case
+      ** of a shared pager cache) of the pager for which the error occured.
+      */
+      assert(
+          (rc&0xff)==SQLITE_IOERR ||
+          rc==SQLITE_FULL ||
+          rc==SQLITE_BUSY
+      );
+      assert( pPager->state>=PAGER_RESERVED );
+      pager_error(pPager, rc);
+    }
+  }
+
+  /* Clear the memory management flags and release the mutex
+  */
+  for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
+     pPager->iInUseMM = 0;
+  }
+  sqlite3_mutex_leave(mutex);
+
+  /* Return the number of bytes released
+  */
   return nReleased;
 }
-#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT && !SQLITE_OMIT_DISKIO */
+#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
 
 /*
 ** Read the content of page pPg out of the database file.
 */
 static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
   int rc;
+  i64 offset;
   assert( MEMDB==0 );
-  rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
-  if( rc==SQLITE_OK ){
-    rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg),
-                          pPager->pageSize);
-  }
+  assert(pPager->fd->pMethods||pPager->tempFile);
+  if( !pPager->fd->pMethods ){
+    return SQLITE_IOERR_SHORT_READ;
+  }
+  offset = (pgno-1)*(i64)pPager->pageSize;
+  rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
   PAGER_INCR(sqlite3_pager_readdb_count);
   PAGER_INCR(pPager->nRead);
   IOTRACE(("PGIN %p %d\n", pPager, pgno));
   if( pgno==1 ){
@@ -20623,8 +22170,9 @@
 static int pagerSharedLock(Pager *pPager){
   int rc = SQLITE_OK;
 
   if( pPager->state==PAGER_UNLOCK ){
+    sqlite3_vfs *pVfs = pPager->pVfs;
     if( !MEMDB ){
       assert( pPager->nRef==0 );
       if( !pPager->noReadlock ){
         rc = pager_wait_on_lock(pPager, SHARED_LOCK);
@@ -20663,28 +22211,29 @@
         ** journal file is never open unless the main database file holds
         ** a write lock, so there is never any chance of two or more
         ** processes opening the journal at the same time.
         **
-	** Open the journal for read/write access. This is because in
-	** exclusive-access mode the file descriptor will be kept open and
+        ** Open the journal for read/write access. This is because in
+        ** exclusive-access mode the file descriptor will be kept open and
         ** possibly used for a transaction later on. On some systems, the
         ** OsTruncate() call used in exclusive-access mode also requires
         ** a read/write file handle.
         */
         rc = SQLITE_BUSY;
-        if( sqlite3OsFileExists(pPager->zJournal) ){
-          int ro;
+        if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
+          int fout = 0;
+          int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
           assert( !pPager->tempFile );
-          rc = sqlite3OsOpenReadWrite(pPager->zJournal, &pPager->jfd, &ro);
-          assert( rc!=SQLITE_OK || pPager->jfd );
-          if( ro ){
+          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, &fout);
+          assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
+          if( fout&SQLITE_OPEN_READONLY ){
             rc = SQLITE_BUSY;
-            sqlite3OsClose(&pPager->jfd);
+            sqlite3OsClose(pPager->jfd);
           }
         }
         if( rc!=SQLITE_OK ){
           pager_unlock(pPager);
-          return SQLITE_BUSY;
+          return (rc==SQLITE_NOMEM?rc:SQLITE_BUSY);
         }
         pPager->journalOpen = 1;
         pPager->journalStarted = 0;
         pPager->journalOff = 0;
@@ -20728,13 +22277,9 @@
         }
 
         if( pPager->dbSize>0 ){
           IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
-          rc = sqlite3OsSeek(pPager->fd, 24);
-          if( rc!=SQLITE_OK ){
-            return rc;
-          }
-          rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers));
+          rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
           if( rc!=SQLITE_OK ){
             return rc;
           }
         }else{
@@ -20789,15 +22334,16 @@
 */
 static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
   int rc = SQLITE_OK;
   PgHdr *pPg;
+  void *pData;
 
   /* Create a new PgHdr if any of the four conditions defined
-  ** above is met: */
+  ** above are met: */
   if( pPager->nPage<pPager->mxPage
-   || pPager->pFirst==0
+   || pPager->lru.pFirst==0
    || MEMDB
-   || (pPager->pFirstSynced==0 && pPager->doNotSync)
+   || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
   ){
     if( pPager->nPage>=pPager->nHash ){
       pager_resize_hash_table(pPager,
          pPager->nHash<256 ? 256 : pPager->nHash*2);
@@ -20805,11 +22351,19 @@
         rc = SQLITE_NOMEM;
         goto pager_allocate_out;
       }
     }
-    pPg = sqliteMallocRaw( sizeof(*pPg) + pPager->pageSize
-                            + sizeof(u32) + pPager->nExtra
+    pagerLeave(pPager);
+    pPg = sqlite3_malloc( sizeof(*pPg) + sizeof(u32) + pPager->nExtra
                             + MEMDB*sizeof(PgHistory) );
+    if( pPg ){
+      pData = sqlite3_malloc( pPager->pageSize );
+      if( pData==0 ){
+        sqlite3_free(pPg);
+        pPg = 0;
+      }
+    }
+    pagerEnter(pPager);
     if( pPg==0 ){
       rc = SQLITE_NOMEM;
       goto pager_allocate_out;
     }
@@ -20816,15 +22370,16 @@
     memset(pPg, 0, sizeof(*pPg));
     if( MEMDB ){
       memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
     }
+    pPg->pData = pData;
     pPg->pPager = pPager;
     pPg->pNextAll = pPager->pAll;
     pPager->pAll = pPg;
     pPager->nPage++;
   }else{
     /* Recycle an existing page with a zero ref-count. */
-    rc = pager_recycle(pPager, 1, &pPg);
+    rc = pager_recycle(pPager, &pPg);
     if( rc==SQLITE_BUSY ){
       rc = SQLITE_IOERR_BLOCKED;
     }
     if( rc!=SQLITE_OK ){
@@ -20889,9 +22444,9 @@
 ** sqlite3PagerWrite() is called on this page or if this routine is
 ** called again with noContent==0, that means that the content is needed
 ** and the disk read should occur at that point.
 */
-SQLITE_PRIVATE int sqlite3PagerAcquire(
+static int pagerAcquire(
   Pager *pPager,      /* The pager open on the database file */
   Pgno pgno,          /* Page number to fetch */
   DbPage **ppPage,    /* Write a pointer to the page here */
   int noContent       /* Do not bother reading content from disk if true */
@@ -20939,9 +22494,11 @@
 
     pPg->pgno = pgno;
     assert( !MEMDB || pgno>pPager->stmtSize );
     if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
+#if 0
       sqlite3CheckMemory(pPager->aInJournal, pgno/8);
+#endif
       assert( pPager->journalOpen );
       pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
       pPg->needSync = 0;
     }else{
@@ -21012,8 +22569,21 @@
   }
   *ppPage = pPg;
   return SQLITE_OK;
 }
+SQLITE_PRIVATE int sqlite3PagerAcquire(
+  Pager *pPager,      /* The pager open on the database file */
+  Pgno pgno,          /* Page number to fetch */
+  DbPage **ppPage,    /* Write a pointer to the page here */
+  int noContent       /* Do not bother reading content from disk if true */
+){
+  int rc;
+  pagerEnter(pPager);
+  rc = pagerAcquire(pPager, pgno, ppPage, noContent);
+  pagerLeave(pPager);
+  return rc;
+}
+
 
 /*
 ** Acquire a page if it is already in the in-memory cache.  Do
 ** not read the page from disk.  Return a pointer to the page,
@@ -21025,23 +22595,22 @@
 ** returns NULL if the page is not in cache or if a disk I/O error
 ** has ever happened.
 */
 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
-  PgHdr *pPg;
+  PgHdr *pPg = 0;
 
   assert( pPager!=0 );
   assert( pgno!=0 );
 
+  pagerEnter(pPager);
   if( pPager->state==PAGER_UNLOCK ){
     assert( !pPager->pAll || pPager->exclusiveMode );
-    return 0;
-  }
-  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
-    return 0;
-  }
-  pPg = pager_lookup(pPager, pgno);
-  if( pPg==0 ) return 0;
-  page_ref(pPg);
+  }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
+    /* Do nothing */
+  }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
+    page_ref(pPg);
+  }
+  pagerLeave(pPager);
   return pPg;
 }
 
 /*
@@ -21056,8 +22625,9 @@
 
   /* Decrement the reference count for this page
   */
   assert( pPg->nRef>0 );
+  pagerEnter(pPg->pPager);
   pPg->nRef--;
   REFINFO(pPg);
 
   CHECK_PAGE(pPg);
@@ -21065,21 +22635,11 @@
   /* When the number of references to a page reach 0, call the
   ** destructor and add the page to the freelist.
   */
   if( pPg->nRef==0 ){
-    Pager *pPager;
-    pPager = pPg->pPager;
-    pPg->pNextFree = 0;
-    pPg->pPrevFree = pPager->pLast;
-    pPager->pLast = pPg;
-    if( pPg->pPrevFree ){
-      pPg->pPrevFree->pNextFree = pPg;
-    }else{
-      pPager->pFirst = pPg;
-    }
-    if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
-      pPager->pFirstSynced = pPg;
-    }
+    Pager *pPager = pPg->pPager;
+
+    lruListAdd(pPg);
     if( pPager->xDestructor ){
       pPager->xDestructor(pPg, pPager->pageSize);
     }
 
@@ -21091,8 +22651,9 @@
     if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
       pagerUnlockAndRollback(pPager);
     }
   }
+  pagerLeave(pPg->pPager);
   return SQLITE_OK;
 }
 
 /*
@@ -21102,35 +22663,48 @@
 ** Return SQLITE_OK if everything.  Return an error code and release the
 ** write lock if anything goes wrong.
 */
 static int pager_open_journal(Pager *pPager){
+  sqlite3_vfs *pVfs = pPager->pVfs;
+  int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
+
   int rc;
   assert( !MEMDB );
   assert( pPager->state>=PAGER_RESERVED );
   assert( pPager->journalOpen==0 );
   assert( pPager->useJournal );
   assert( pPager->aInJournal==0 );
   sqlite3PagerPagecount(pPager);
-  pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
+  pagerLeave(pPager);
+  pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
+  pagerEnter(pPager);
   if( pPager->aInJournal==0 ){
     rc = SQLITE_NOMEM;
     goto failed_to_open_journal;
   }
-  rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,
-                                 pPager->tempFile);
-  assert( rc!=SQLITE_OK || pPager->jfd );
+
+  if( pPager->tempFile ){
+    flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
+  }else{
+    flags |= (SQLITE_OPEN_MAIN_JOURNAL);
+  }
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE
+  rc = sqlite3JournalOpen(
+      pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
+  );
+#else
+  rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
+#endif
+  assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
   pPager->journalOff = 0;
   pPager->setMaster = 0;
   pPager->journalHdr = 0;
   if( rc!=SQLITE_OK ){
     if( rc==SQLITE_NOMEM ){
-      sqlite3OsDelete(pPager->zJournal);
+      sqlite3OsDelete(pVfs, pPager->zJournal, 0);
     }
     goto failed_to_open_journal;
   }
-  sqlite3OsSetFullSync(pPager->jfd, pPager->full_fsync);
-  sqlite3OsSetFullSync(pPager->fd, pPager->full_fsync);
-  sqlite3OsOpenDirectory(pPager->jfd, pPager->zDirectory);
   pPager->journalOpen = 1;
   pPager->journalStarted = 0;
   pPager->needSync = 0;
   pPager->alwaysRollback = 0;
@@ -21154,9 +22728,9 @@
   }
   return rc;
 
 failed_to_open_journal:
-  sqliteFree(pPager->aInJournal);
+  sqlite3_free(pPager->aInJournal);
   pPager->aInJournal = 0;
   return rc;
 }
 
@@ -21189,8 +22763,9 @@
 */
 SQLITE_PRIVATE int sqlite3PagerBegin(DbPage *pPg, int exFlag){
   Pager *pPager = pPg->pPager;
   int rc = SQLITE_OK;
+  pagerEnter(pPager);
   assert( pPg->nRef>0 );
   assert( pPager->state!=PAGER_UNLOCK );
   if( pPager->state==PAGER_SHARED ){
     assert( pPager->aInJournal==0 );
@@ -21205,8 +22780,9 @@
           rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
         }
       }
       if( rc!=SQLITE_OK ){
+        pagerLeave(pPager);
         return rc;
       }
       pPager->dirtyCache = 0;
       PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
@@ -21223,9 +22799,11 @@
     assert( pPager->nRec==0 );
     assert( pPager->origDbSize==0 );
     assert( pPager->aInJournal==0 );
     sqlite3PagerPagecount(pPager);
-    pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
+    pagerLeave(pPager);
+    pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
+    pagerEnter(pPager);
     if( !pPager->aInJournal ){
       rc = SQLITE_NOMEM;
     }else{
       pPager->origDbSize = pPager->dbSize;
@@ -21232,8 +22810,9 @@
       rc = writeJournalHdr(pPager);
     }
   }
   assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
+  pagerLeave(pPager);
   return rc;
 }
 
 /*
@@ -21260,13 +22839,16 @@
 static void makeClean(PgHdr *pPg){
   if( pPg->dirty ){
     pPg->dirty = 0;
     if( pPg->pDirty ){
+      assert( pPg->pDirty->pPrevDirty==pPg );
       pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
     }
     if( pPg->pPrevDirty ){
+      assert( pPg->pPrevDirty->pDirty==pPg );
       pPg->pPrevDirty->pDirty = pPg->pDirty;
     }else{
+      assert( pPg->pPager->pDirty==pPg );
       pPg->pPager->pDirty = pPg->pDirty;
     }
   }
 }
@@ -21353,42 +22935,43 @@
     ** the transaction journal if it is not there already.
     */
     if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
       if( (int)pPg->pgno <= pPager->origDbSize ){
-        int szPg;
         if( MEMDB ){
           PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
           PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
           assert( pHist->pOrig==0 );
-          pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
+          pHist->pOrig = sqlite3_malloc( pPager->pageSize );
           if( pHist->pOrig ){
             memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
           }
         }else{
-          u32 cksum, saved;
-          char *pData2, *pEnd;
+          u32 cksum;
+          char *pData2;
+
           /* We should never write to the journal file the page that
           ** contains the database locks.  The following assert verifies
           ** that we do not. */
           assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
           pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
           cksum = pager_cksum(pPager, (u8*)pData2);
-          pEnd = pData2 + pPager->pageSize;
-          pData2 -= 4;
-          saved = *(u32*)pEnd;
-          put32bits(pEnd, cksum);
-          szPg = pPager->pageSize+8;
-          put32bits(pData2, pPg->pgno);
-          rc = sqlite3OsWrite(pPager->jfd, pData2, szPg);
+          rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
+          if( rc==SQLITE_OK ){
+            rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
+                                pPager->journalOff + 4);
+            pPager->journalOff += pPager->pageSize+4;
+          }
+          if( rc==SQLITE_OK ){
+            rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
+            pPager->journalOff += 4;
+          }
           IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
-                   pPager->journalOff, szPg));
-          PAGER_INCR(sqlite3_pager_writej_count);
-          pPager->journalOff += szPg;
+                   pPager->journalOff, pPager->pageSize));
+          PAGER_INCR(sqlite3_pager_writej_count);
           PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
                PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
-          *(u32*)pEnd = saved;
-
-	  /* An error has occured writing to the journal file. The
+
+          /* An error has occured writing to the journal file. The
           ** transaction will be rolled back by the layer above.
           */
           if( rc!=SQLITE_OK ){
             return rc;
@@ -21425,18 +23008,21 @@
       assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
       if( MEMDB ){
         PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
         assert( pHist->pStmt==0 );
-        pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
+        pHist->pStmt = sqlite3_malloc( pPager->pageSize );
         if( pHist->pStmt ){
           memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
         }
         PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
         page_add_to_stmt_list(pPg);
       }else{
-        char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
-        put32bits(pData2, pPg->pgno);
-        rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4);
+        i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
+        char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
+        rc = write32bits(pPager->stfd, offset, pPg->pgno);
+        if( rc==SQLITE_OK ){
+          rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
+        }
         PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
         if( rc!=SQLITE_OK ){
           return rc;
         }
@@ -21475,13 +23061,15 @@
   PgHdr *pPg = pDbPage;
   Pager *pPager = pPg->pPager;
   Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
 
+  pagerEnter(pPager);
   if( !MEMDB && nPagePerSector>1 ){
     Pgno nPageCount;          /* Total number of pages in database file */
     Pgno pg1;                 /* First page of the sector pPg is located on. */
     int nPage;                /* Number of pages starting at pg1 to journal */
     int ii;
+    int needSync = 0;
 
     /* Set the doNotSync flag to 1. This is because we cannot allow a journal
     ** header to be written between the pages journaled by this function.
     */
@@ -21507,27 +23095,49 @@
     assert((pg1+nPage)>pPg->pgno);
 
     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
       Pgno pg = pg1+ii;
+      PgHdr *pPage;
       if( !pPager->aInJournal || pg==pPg->pgno ||
           pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
       ) {
         if( pg!=PAGER_MJ_PGNO(pPager) ){
-          PgHdr *pPage;
           rc = sqlite3PagerGet(pPager, pg, &pPage);
           if( rc==SQLITE_OK ){
             rc = pager_write(pPage);
+            if( pPage->needSync ){
+              needSync = 1;
+            }
             sqlite3PagerUnref(pPage);
           }
         }
-      }
+      }else if( (pPage = pager_lookup(pPager, pg)) ){
+        if( pPage->needSync ){
+          needSync = 1;
+        }
+      }
+    }
+
+    /* If the PgHdr.needSync flag is set for any of the nPage pages
+    ** starting at pg1, then it needs to be set for all of them. Because
+    ** writing to any of these nPage pages may damage the others, the
+    ** journal file must contain sync()ed copies of all of them
+    ** before any of them can be written out to the database file.
+    */
+    if( needSync ){
+      for(ii=0; ii<nPage && needSync; ii++){
+        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
+        if( pPage ) pPage->needSync = 1;
+      }
+      assert(pPager->needSync);
     }
 
     assert( pPager->doNotSync==1 );
     pPager->doNotSync = 0;
   }else{
     rc = pager_write(pDbPage);
   }
+  pagerLeave(pPager);
   return rc;
 }
 
 /*
@@ -21549,8 +23159,9 @@
 SQLITE_PRIVATE int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
   PgHdr *pPg;
   int rc;
 
+  pagerEnter(pPager);
   rc = sqlite3PagerGet(pPager, pgno, &pPg);
   if( rc==SQLITE_OK ){
     rc = sqlite3PagerWrite(pPg);
     if( rc==SQLITE_OK ){
@@ -21557,8 +23168,9 @@
       memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
     }
     sqlite3PagerUnref(pPg);
   }
+  pagerLeave(pPager);
   return rc;
 }
 #endif
 
@@ -21590,8 +23202,9 @@
   PgHdr *pPg = pDbPage;
   Pager *pPager = pPg->pPager;
 
   if( MEMDB ) return;
+  pagerEnter(pPager);
   pPg->alwaysRollback = 1;
   if( pPg->dirty && !pPager->stmtInUse ){
     assert( pPager->state>=PAGER_SHARED );
     if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
@@ -21611,8 +23224,9 @@
       pPg->pageHash = pager_pagehash(pPg);
 #endif
     }
   }
+  pagerLeave(pPager);
 }
 
 /*
 ** A call to this routine tells the pager that if a rollback occurs,
@@ -21627,8 +23241,9 @@
 */
 SQLITE_PRIVATE void sqlite3PagerDontRollback(DbPage *pPg){
   Pager *pPager = pPg->pPager;
 
+  pagerEnter(pPager);
   assert( pPager->state>=PAGER_RESERVED );
   if( pPager->journalOpen==0 ) return;
   if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
   if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
@@ -21649,36 +23264,46 @@
     assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
     assert( pPager->aInStmt!=0 );
     pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
   }
+  pagerLeave(pPager);
 }
 
 
 /*
 ** This routine is called to increment the database file change-counter,
 ** stored at byte 24 of the pager file.
 */
-static int pager_incr_changecounter(Pager *pPager){
+static int pager_incr_changecounter(Pager *pPager, int isDirect){
   PgHdr *pPgHdr;
   u32 change_counter;
-  int rc;
+  int rc = SQLITE_OK;
 
   if( !pPager->changeCountDone ){
     /* Open page 1 of the file for writing. */
     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
     if( rc!=SQLITE_OK ) return rc;
-    rc = sqlite3PagerWrite(pPgHdr);
-    if( rc!=SQLITE_OK ) return rc;
+
+    if( !isDirect ){
+      rc = sqlite3PagerWrite(pPgHdr);
+      if( rc!=SQLITE_OK ) return rc;
+    }
 
     /* Increment the value just read and write it back to byte 24. */
     change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
     change_counter++;
     put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
+
+    if( isDirect && pPager->fd->pMethods ){
+      const void *zBuf = PGHDR_TO_DATA(pPgHdr);
+      rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
+    }
+
     /* Release the page reference. */
     sqlite3PagerUnref(pPgHdr);
     pPager->changeCountDone = 1;
   }
-  return SQLITE_OK;
+  return rc;
 }
 
 /*
 ** Sync the database file for the pager pPager. zMaster points to the name
@@ -21701,15 +23326,54 @@
   int rc = SQLITE_OK;
 
   PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
       pPager->zFilename, zMaster, nTrunc);
+  pagerEnter(pPager);
 
   /* If this is an in-memory db, or no pages have been written to, or this
   ** function has already been called, it is a no-op.
   */
   if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
     PgHdr *pPg;
-    assert( pPager->journalOpen );
+
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE
+    /* The atomic-write optimization can be used if all of the
+    ** following are true:
+    **
+    **    + The file-system supports the atomic-write property for
+    **      blocks of size page-size, and
+    **    + This commit is not part of a multi-file transaction, and
+    **    + Exactly one page has been modified and store in the journal file.
+    **
+    ** If the optimization can be used, then the journal file will never
+    ** be created for this transaction.
+    */
+    int useAtomicWrite = (
+        !zMaster &&
+        pPager->journalOff==jrnlBufferSize(pPager) &&
+        nTrunc==0 &&
+        (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
+    );
+    if( useAtomicWrite ){
+      /* Update the nRec field in the journal file. */
+      int offset = pPager->journalHdr + sizeof(aJournalMagic);
+      assert(pPager->nRec==1);
+      rc = write32bits(pPager->jfd, offset, pPager->nRec);
+
+      /* Update the db file change counter. The following call will modify
+      ** the in-memory representation of page 1 to include the updated
+      ** change counter and then write page 1 directly to the database
+      ** file. Because of the atomic-write property of the host file-system,
+      ** this is safe.
+      */
+      rc = pager_incr_changecounter(pPager, 1);
+    }else{
+      rc = sqlite3JournalCreate(pPager->jfd);
+      if( rc!=SQLITE_OK ) goto sync_exit;
+    }
+
+    if( !useAtomicWrite )
+#endif
 
     /* If a master journal file name has already been written to the
     ** journal file, then no sync is required. This happens when it is
     ** written, then the process fails to upgrade from a RESERVED to an
@@ -21716,9 +23380,10 @@
     ** EXCLUSIVE lock. The next time the process tries to commit the
     ** transaction the m-j name will have already been written.
     */
     if( !pPager->setMaster ){
-      rc = pager_incr_changecounter(pPager);
+      assert( pPager->journalOpen );
+      rc = pager_incr_changecounter(pPager, 0);
       if( rc!=SQLITE_OK ) goto sync_exit;
 #ifndef SQLITE_OMIT_AUTOVACUUM
       if( nTrunc!=0 ){
         /* If this transaction has made the database smaller, then all pages
@@ -21740,10 +23405,10 @@
 #endif
       rc = writeMasterJournal(pPager, zMaster);
       if( rc!=SQLITE_OK ) goto sync_exit;
       rc = syncJournal(pPager);
-      if( rc!=SQLITE_OK ) goto sync_exit;
-    }
+    }
+    if( rc!=SQLITE_OK ) goto sync_exit;
 
 #ifndef SQLITE_OMIT_AUTOVACUUM
     if( nTrunc!=0 ){
       rc = sqlite3PagerTruncate(pPager, nTrunc);
@@ -21753,14 +23418,18 @@
 
     /* Write all dirty pages to the database file */
     pPg = pager_get_all_dirty_pages(pPager);
     rc = pager_write_pagelist(pPg);
-    if( rc!=SQLITE_OK ) goto sync_exit;
+    if( rc!=SQLITE_OK ){
+      while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; }
+      pPager->pDirty = pPg;
+      goto sync_exit;
+    }
     pPager->pDirty = 0;
 
     /* Sync the database file. */
     if( !pPager->noSync ){
-      rc = sqlite3OsSync(pPager->fd, 0);
+      rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
     }
     IOTRACE(("DBSYNC %p\n", pPager))
 
     pPager->state = PAGER_SYNCED;
@@ -21776,8 +23445,9 @@
      * better to return SQLITE_BUSY.
      */
     rc = SQLITE_BUSY;
   }
+  pagerLeave(pPager);
   return rc;
 }
 
 
@@ -21797,8 +23467,9 @@
   }
   if( pPager->state<PAGER_RESERVED ){
     return SQLITE_ERROR;
   }
+  pagerEnter(pPager);
   PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
   if( MEMDB ){
     pPg = pager_get_all_dirty_pages(pPager);
     while( pPg ){
@@ -21826,9 +23497,11 @@
   }
   assert( pPager->journalOpen || !pPager->dirtyCache );
   assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
   rc = pager_end_transaction(pPager);
-  return pager_error(pPager, rc);
+  rc = pager_error(pPager, rc);
+  pagerLeave(pPager);
+  return rc;
 }
 
 /*
 ** Rollback all changes.  The database falls back to PAGER_SHARED mode.
@@ -21880,17 +23553,20 @@
     pPager->state = PAGER_SHARED;
     return SQLITE_OK;
   }
 
+  pagerEnter(pPager);
   if( !pPager->dirtyCache || !pPager->journalOpen ){
     rc = pager_end_transaction(pPager);
+    pagerLeave(pPager);
     return rc;
   }
 
   if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
     if( pPager->state>=PAGER_EXCLUSIVE ){
       pager_playback(pPager, 0);
     }
+    pagerLeave(pPager);
     return pPager->errCode;
   }
   if( pPager->state==PAGER_RESERVED ){
     int rc2;
@@ -21908,9 +23584,11 @@
   /* If an error occurs during a ROLLBACK, we can no longer trust the pager
   ** cache. So call pager_error() on the way out to make any error
   ** persistent.
   */
-  return pager_error(pPager, rc);
+  rc = pager_error(pPager, rc);
+  pagerLeave(pPager);
+  return rc;
 }
 
 /*
 ** Return TRUE if the database file is opened read-only.  Return FALSE
@@ -21954,9 +23632,9 @@
 ** This routine should be called with the transaction journal already
 ** open.  A new statement journal is created that can be used to rollback
 ** changes of a single SQL command within a larger transaction.
 */
-SQLITE_PRIVATE int sqlite3PagerStmtBegin(Pager *pPager){
+static int pagerStmtBegin(Pager *pPager){
   int rc;
   assert( !pPager->stmtInUse );
   assert( pPager->state>=PAGER_SHARED );
   assert( pPager->dbSize>=0 );
@@ -21970,9 +23648,11 @@
     pPager->stmtAutoopen = 1;
     return SQLITE_OK;
   }
   assert( pPager->journalOpen );
-  pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 );
+  pagerLeave(pPager);
+  pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
+  pagerEnter(pPager);
   if( pPager->aInStmt==0 ){
     /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
     return SQLITE_NOMEM;
   }
@@ -21985,10 +23665,13 @@
   pPager->stmtSize = pPager->dbSize;
   pPager->stmtHdrOff = 0;
   pPager->stmtCksum = pPager->cksumInit;
   if( !pPager->stmtOpen ){
-    rc = sqlite3PagerOpentemp(&pPager->stfd);
-    if( rc ) goto stmt_begin_failed;
+    rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
+                              SQLITE_OPEN_SUBJOURNAL);
+    if( rc ){
+      goto stmt_begin_failed;
+    }
     pPager->stmtOpen = 1;
     pPager->stmtNRec = 0;
   }
   pPager->stmtInUse = 1;
@@ -21995,25 +23678,32 @@
   return SQLITE_OK;
 
 stmt_begin_failed:
   if( pPager->aInStmt ){
-    sqliteFree(pPager->aInStmt);
+    sqlite3_free(pPager->aInStmt);
     pPager->aInStmt = 0;
   }
+  return rc;
+}
+SQLITE_PRIVATE int sqlite3PagerStmtBegin(Pager *pPager){
+  int rc;
+  pagerEnter(pPager);
+  rc = pagerStmtBegin(pPager);
+  pagerLeave(pPager);
   return rc;
 }
 
 /*
 ** Commit a statement.
 */
 SQLITE_PRIVATE int sqlite3PagerStmtCommit(Pager *pPager){
+  pagerEnter(pPager);
   if( pPager->stmtInUse ){
     PgHdr *pPg, *pNext;
     PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
     if( !MEMDB ){
-      sqlite3OsSeek(pPager->stfd, 0);
       /* sqlite3OsTruncate(pPager->stfd, 0); */
-      sqliteFree( pPager->aInStmt );
+      sqlite3_free( pPager->aInStmt );
       pPager->aInStmt = 0;
     }else{
       for(pPg=pPager->pStmt; pPg; pPg=pNext){
         PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
@@ -22020,9 +23710,9 @@
         pNext = pHist->pNextStmt;
         assert( pHist->inStmt );
         pHist->inStmt = 0;
         pHist->pPrevStmt = pHist->pNextStmt = 0;
-        sqliteFree(pHist->pStmt);
+        sqlite3_free(pHist->pStmt);
         pHist->pStmt = 0;
       }
     }
     pPager->stmtNRec = 0;
@@ -22029,8 +23719,9 @@
     pPager->stmtInUse = 0;
     pPager->pStmt = 0;
   }
   pPager->stmtAutoopen = 0;
+  pagerLeave(pPager);
   return SQLITE_OK;
 }
 
 /*
@@ -22037,8 +23728,9 @@
 ** Rollback a statement.
 */
 SQLITE_PRIVATE int sqlite3PagerStmtRollback(Pager *pPager){
   int rc;
+  pagerEnter(pPager);
   if( pPager->stmtInUse ){
     PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
     if( MEMDB ){
       PgHdr *pPg;
@@ -22046,9 +23738,9 @@
       for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
         pHist = PGHDR_TO_HIST(pPg, pPager);
         if( pHist->pStmt ){
           memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
-          sqliteFree(pHist->pStmt);
+          sqlite3_free(pHist->pStmt);
           pHist->pStmt = 0;
         }
       }
       pPager->dbSize = pPager->stmtSize;
@@ -22061,8 +23753,9 @@
   }else{
     rc = SQLITE_OK;
   }
   pPager->stmtAutoopen = 0;
+  pagerLeave(pPager);
   return rc;
 }
 
 /*
@@ -22069,8 +23762,24 @@
 ** Return the full pathname of the database file.
 */
 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
   return pPager->zFilename;
+}
+
+/*
+** Return the VFS structure for the pager.
+*/
+SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
+  return pPager->pVfs;
+}
+
+/*
+** Return the file handle for the database file associated
+** with the pager.  This might return NULL if the file has
+** not yet been opened.
+*/
+SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
+  return pPager->fd;
 }
 
 /*
 ** Return the directory of the database file.
@@ -22130,8 +23839,9 @@
   PgHdr *pPgOld;  /* The page being overwritten. */
   int h;
   Pgno needSyncPgno = 0;
 
+  pagerEnter(pPager);
   assert( pPg->nRef>0 );
 
   PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
       PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
@@ -22206,8 +23916,9 @@
     makeDirty(pPgHdr);
     sqlite3PagerUnref(pPgHdr);
   }
 
+  pagerLeave(pPager);
   return SQLITE_OK;
 }
 #endif
 
@@ -22248,19 +23959,8 @@
   }
   return (int)pPager->exclusiveMode;
 }
 
-#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
-/*
-** Return the current state of the file lock for the given pager.
-** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
-** PENDING_LOCK, or EXCLUSIVE_LOCK.
-*/
-SQLITE_PRIVATE int sqlite3PagerLockstate(Pager *pPager){
-  return sqlite3OsLockState(pPager->fd);
-}
-#endif
-
 #ifdef SQLITE_DEBUG
 /*
 ** Print a listing of all referenced pages and their ref count.
 */
@@ -22276,27 +23976,29 @@
 
 #endif /* SQLITE_OMIT_DISKIO */
 
 /************** End of pager.c ***********************************************/
-/************** Begin file btree.c *******************************************/
-/*
-** 2004 April 6
-**
-** The author disclaims copyright to this source code.  In place of
-** a legal notice, here is a blessing:
-**
-**    May you do good and not evil.
-**    May you find forgiveness for yourself and forgive others.
-**    May you share freely, never taking more than you give.
-**
-*************************************************************************
-** $Id: btree.c,v 1.393 2007/07/23 19:26:17 drh Exp $
-**
-** This file implements a external (disk-based) database using BTrees.
-** See the header comment on "btreeInt.h" for additional information.
-** Including a description of file format and an overview of operation.
-*/
-/************** Include btreeInt.h in the middle of btree.c ******************/
+/************** Begin file btmutex.c *****************************************/
+/*
+** 2007 August 27
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+**
+** $Id: btmutex.c,v 1.7 2007/08/30 01:19:59 drh Exp $
+**
+** This file contains code used to implement mutexes on Btree objects.
+** This code really belongs in btree.c.  But btree.c is getting too
+** big and we want to break it down some.  This packaged seemed like
+** a good breakout.
+*/
+/************** Include btreeInt.h in the middle of btmutex.c ****************/
 /************** Begin file btreeInt.h ****************************************/
 /*
 ** 2004 April 6
 **
@@ -22307,9 +24009,9 @@
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** $Id: btreeInt.h,v 1.5 2007/06/15 12:06:59 drh Exp $
+** $Id: btreeInt.h,v 1.13 2007/08/30 01:19:59 drh Exp $
 **
 ** This file implements a external (disk-based) database using BTrees.
 ** For a detailed discussion of BTrees, refer to
 **
@@ -22541,9 +24243,9 @@
 #endif
 
 /*
 ** Page type flags.  An ORed combination of these flags appear as the
-** first byte of every BTree page.
+** first byte of on-disk image of every BTree page.
 */
 #define PTF_INTKEY    0x01
 #define PTF_ZERODATA  0x02
 #define PTF_LEAFDATA  0x04
@@ -22557,8 +24259,11 @@
 ** The pParent field points back to the parent page.  This allows us to
 ** walk up the BTree from any leaf to the root.  Care must be taken to
 ** unref() the parent page pointer when this page is no longer referenced.
 ** The pageDestructor() routine handles that chore.
+**
+** Access to all fields of this structure is controlled by the mutex
+** stored in MemPage.pBt->mutex.
 */
 struct MemPage {
   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
   u8 idxShift;         /* True if Cell indices have changed */
@@ -22569,10 +24274,10 @@
   u8 leafData;         /* True if tables stores data on leaves only */
   u8 hasData;          /* True if this page stores data */
   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
-  u16 maxLocal;        /* Copy of Btree.maxLocal or Btree.maxLeaf */
-  u16 minLocal;        /* Copy of Btree.minLocal or Btree.minLeaf */
+  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
+  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
   u16 cellOffset;      /* Index in aData of first cell pointer */
   u16 idxParent;       /* Index in parent of this node */
   u16 nFree;           /* Number of free bytes on the page */
   u16 nCell;           /* Number of cells on this page, local and ovfl */
@@ -22579,10 +24284,10 @@
   struct _OvflCell {   /* Cells that will not fit on aData[] */
     u8 *pCell;          /* Pointers to the body of the overflow cell */
     u16 idx;            /* Insert this cell before idx-th non-overflow cell */
   } aOvfl[5];
-  BtShared *pBt;       /* Pointer back to BTree structure */
-  u8 *aData;           /* Pointer back to the start of the page */
+  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
+  u8 *aData;           /* Pointer to disk image of the page data */
   DbPage *pDbPage;     /* Pager page handle */
   Pgno pgno;           /* Page number for this page */
   MemPage *pParent;    /* The parent of this page.  NULL for root */
 };
@@ -22593,29 +24298,67 @@
 ** that extra information.
 */
 #define EXTRA_SIZE sizeof(MemPage)
 
-/* Btree handle */
+/* A Btree handle
+**
+** A database connection contains a pointer to an instance of
+** this object for every database file that it has open.  This structure
+** is opaque to the database connection.  The database connection cannot
+** see the internals of this structure and only deals with pointers to
+** this structure.
+**
+** For some database files, the same underlying database cache might be
+** shared between multiple connections.  In that case, each contection
+** has it own pointer to this object.  But each instance of this object
+** points to the same BtShared object.  The database cache and the
+** schema associated with the database file are all contained within
+** the BtShared object.
+**
+** All fields in this structure are accessed under sqlite3.mutex.
+** The pBt pointer itself may not be changed while there exists cursors
+** in the referenced BtShared that point back to this Btree since those
+** cursors have to do go through this Btree to find their BtShared and
+** they often do so without holding sqlite3.mutex.
+*/
 struct Btree {
-  sqlite3 *pSqlite;
-  BtShared *pBt;
-  u8 inTrans;            /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
+  sqlite3 *pSqlite;  /* The database connection holding this btree */
+  BtShared *pBt;     /* Sharable content of this btree */
+  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
+  u8 sharable;       /* True if we can share pBt with other pSqlite */
+  u8 locked;         /* True if pSqlite currently has pBt locked */
+  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
+  Btree *pNext;      /* List of other sharable Btrees from the same pSqlite */
+  Btree *pPrev;      /* Back pointer of the same list */
 };
 
 /*
 ** Btree.inTrans may take one of the following values.
 **
 ** If the shared-data extension is enabled, there may be multiple users
 ** of the Btree structure. At most one of these may open a write transaction,
-** but any number may have active read transactions. Variable Btree.pDb
-** points to the handle that owns any current write-transaction.
+** but any number may have active read transactions.
 */
 #define TRANS_NONE  0
 #define TRANS_READ  1
 #define TRANS_WRITE 2
 
 /*
-** Everything we need to know about an open database
+** An instance of this object represents a single database file.
+**
+** A single database file can be in use as the same time by two
+** or more database connections.  When two or more connections are
+** sharing the same database file, each connection has it own
+** private Btree object for the file and each of those Btrees points
+** to this one BtShared object.  BtShared.nRef is the number of
+** connections currently sharing this database file.
+**
+** Fields in this structure are accessed under the BtShared.mutex
+** mutex, except for nRef and pNext which are accessed under the
+** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
+** may not be modified once it is initially set as long as nRef>0.
+** The pSchema field may be set once under BtShared.mutex and
+** thereafter is unchanged as long as nRef>0.
 */
 struct BtShared {
   Pager *pPager;        /* The page cache */
   BtCursor *pCursor;    /* A list of all open cursors */
@@ -22638,15 +24381,16 @@
   int maxLeaf;          /* Maximum local payload in a LEAFDATA table */
   int minLeaf;          /* Minimum local payload in a LEAFDATA table */
   BusyHandler *pBusyHandler;   /* Callback for when there is lock contention */
   u8 inTransaction;     /* Transaction state */
-  int nRef;             /* Number of references to this structure */
   int nTransaction;     /* Number of open transactions (read + write) */
   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
-#ifndef SQLITE_OMIT_SHARED_CACHE
-  BtLock *pLock;        /* List of locks held on this shared-btree struct */
-  BtShared *pNext;      /* Next in ThreadData.pBtree linked list */
+  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
+#ifndef SQLITE_OMIT_SHARED_CACHE
+  int nRef;             /* Number of references to this structure */
+  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
+  BtLock *pLock;        /* List of locks held on this shared-btree struct */
 #endif
 };
 
 /*
@@ -22666,14 +24410,24 @@
   u16 nSize;     /* Size of the cell content on the main b-tree page */
 };
 
 /*
-** A cursor is a pointer to a particular entry in the BTree.
+** A cursor is a pointer to a particular entry within a particular
+** b-tree within a database file.
+**
 ** The entry is identified by its MemPage and the index in
 ** MemPage.aCell[] of the entry.
+**
+** When a single database file can shared by two more database connections,
+** but cursors cannot be shared.  Each cursor is associated with a
+** particular database connection identified BtCursor.pBtree.pSqlite.
+**
+** Fields in this structure are accessed under the BtShared.mutex
+** found at self->pBt->mutex.
 */
 struct BtCursor {
   Btree *pBtree;            /* The Btree to which this cursor belongs */
+  BtShared *pBt;            /* The BtShared this cursor points to */
   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
   int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
   void *pArg;               /* First arg to xCompare() */
   Pgno pgnoRoot;            /* The root page of this tree */
@@ -22707,12 +24461,20 @@
 **   modified since the cursor was last used. The cursor position is saved
 **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
 **   this state, restoreOrClearCursorPosition() can be called to attempt to
 **   seek the cursor to the saved position.
+**
+** CURSOR_FAULT:
+**   A unrecoverable error (an I/O error or a malloc failure) has occurred
+**   on a different connection that shares the BtShared cache with this
+**   cursor.  The error has left the cache in an inconsistent state.
+**   Do nothing else with this cursor.  Any attempt to use the cursor
+**   should return the error code stored in BtCursor.skip
 */
 #define CURSOR_INVALID           0
 #define CURSOR_VALID             1
 #define CURSOR_REQUIRESEEK       2
+#define CURSOR_FAULT             3
 
 /*
 ** The TRACE macro will print high-level status information about the
 ** btree operation when the global variable sqlite3_btree_trace is
@@ -22823,10 +24585,8 @@
 /* A bunch of assert() statements to check the transaction state variables
 ** of handle p (type Btree*) are internally consistent.
 */
 #define btreeIntegrity(p) \
-  assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \
-  assert( p->pBt->nTransaction<=p->pBt->nRef ); \
   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
   assert( p->pBt->inTransaction>=p->inTrans );
 
 
@@ -22873,24 +24633,340 @@
 SQLITE_PRIVATE int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
 SQLITE_PRIVATE int sqlite3BtreeInitPage(MemPage *pPage, MemPage *pParent);
 SQLITE_PRIVATE void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
 SQLITE_PRIVATE void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
+#ifdef SQLITE_TEST
 SQLITE_PRIVATE u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell);
+#endif
 SQLITE_PRIVATE int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur);
 SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
 SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
 SQLITE_PRIVATE int sqlite3BtreeIsRootPage(MemPage *pPage);
 SQLITE_PRIVATE void sqlite3BtreeMoveToParent(BtCursor *pCur);
 
 /************** End of btreeInt.h ********************************************/
-/************** Continuing where we left off in btree.c **********************/
+/************** Continuing where we left off in btmutex.c ********************/
+#if SQLITE_THREADSAFE && !defined(SQLITE_OMIT_SHARED_CACHE)
+
+
+/*
+** Enter a mutex on the given BTree object.
+**
+** If the object is not sharable, then no mutex is ever required
+** and this routine is a no-op.  The underlying mutex is non-recursive.
+** But we keep a reference count in Btree.wantToLock so the behavior
+** of this interface is recursive.
+**
+** To avoid deadlocks, multiple Btrees are locked in the same order
+** by all database connections.  The p->pNext is a list of other
+** Btrees belonging to the same database connection as the p Btree
+** which need to be locked after p.  If we cannot get a lock on
+** p, then first unlock all of the others on p->pNext, then wait
+** for the lock to become available on p, then relock all of the
+** subsequent Btrees that desire a lock.
+*/
+SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
+  Btree *pLater;
+
+  /* Some basic sanity checking on the Btree.  The list of Btrees
+  ** connected by pNext and pPrev should be in sorted order by
+  ** Btree.pBt value. All elements of the list should belong to
+  ** the same connection. Only shared Btrees are on the list. */
+  assert( p->pNext==0 || p->pNext->pBt>p->pBt );
+  assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
+  assert( p->pNext==0 || p->pNext->pSqlite==p->pSqlite );
+  assert( p->pPrev==0 || p->pPrev->pSqlite==p->pSqlite );
+  assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
+
+  /* Check for locking consistency */
+  assert( !p->locked || p->wantToLock>0 );
+  assert( p->sharable || p->wantToLock==0 );
+
+  /* We should already hold a lock on the database connection */
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+
+  if( !p->sharable ) return;
+  p->wantToLock++;
+  if( p->locked ) return;
+
+  /* In most cases, we should be able to acquire the lock we
+  ** want without having to go throught the ascending lock
+  ** procedure that follows.  Just be sure not to block.
+  */
+  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
+    p->locked = 1;
+    return;
+  }
+
+  /* To avoid deadlock, first release all locks with a larger
+  ** BtShared address.  Then acquire our lock.  Then reacquire
+  ** the other BtShared locks that we used to hold in ascending
+  ** order.
+  */
+  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
+    assert( pLater->sharable );
+    assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
+    assert( !pLater->locked || pLater->wantToLock>0 );
+    if( pLater->locked ){
+      sqlite3_mutex_leave(pLater->pBt->mutex);
+      pLater->locked = 0;
+    }
+  }
+  sqlite3_mutex_enter(p->pBt->mutex);
+  p->locked = 1;
+  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
+    if( pLater->wantToLock ){
+      sqlite3_mutex_enter(pLater->pBt->mutex);
+      pLater->locked = 1;
+    }
+  }
+}
+
+/*
+** Exit the recursive mutex on a Btree.
+*/
+SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
+  if( p->sharable ){
+    assert( p->wantToLock>0 );
+    p->wantToLock--;
+    if( p->wantToLock==0 ){
+      assert( p->locked );
+      sqlite3_mutex_leave(p->pBt->mutex);
+      p->locked = 0;
+    }
+  }
+}
+
+#ifndef NDEBUG
+/*
+** Return true if the BtShared mutex is held on the btree.
+**
+** This routine makes no determination one why or another if the
+** database connection mutex is held.
+**
+** This routine is used only from within assert() statements.
+*/
+SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
+  return (p->sharable==0 ||
+             (p->locked && p->wantToLock && sqlite3_mutex_held(p->pBt->mutex)));
+}
+#endif
+
+
+#ifndef SQLITE_OMIT_INCRBLOB
+/*
+** Enter and leave a mutex on a Btree given a cursor owned by that
+** Btree.  These entry points are used by incremental I/O and can be
+** omitted if that module is not used.
+*/
+SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
+  sqlite3BtreeEnter(pCur->pBtree);
+}
+SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
+  sqlite3BtreeLeave(pCur->pBtree);
+}
+#endif /* SQLITE_OMIT_INCRBLOB */
+
+
+/*
+** Enter the mutex on every Btree associated with a database
+** connection.  This is needed (for example) prior to parsing
+** a statement since we will be comparing table and column names
+** against all schemas and we do not want those schemas being
+** reset out from under us.
+**
+** There is a corresponding leave-all procedures.
+**
+** Enter the mutexes in accending order by BtShared pointer address
+** to avoid the possibility of deadlock when two threads with
+** two or more btrees in common both try to lock all their btrees
+** at the same instant.
+*/
+SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
+  int i;
+  Btree *p, *pLater;
+  assert( sqlite3_mutex_held(db->mutex) );
+  for(i=0; i<db->nDb; i++){
+    p = db->aDb[i].pBt;
+    if( p && p->sharable ){
+      p->wantToLock++;
+      if( !p->locked ){
+        assert( p->wantToLock==1 );
+        while( p->pPrev ) p = p->pPrev;
+        while( p->locked && p->pNext ) p = p->pNext;
+        for(pLater = p->pNext; pLater; pLater=pLater->pNext){
+          if( pLater->locked ){
+            sqlite3_mutex_leave(pLater->pBt->mutex);
+            pLater->locked = 0;
+          }
+        }
+        while( p ){
+          sqlite3_mutex_enter(p->pBt->mutex);
+          p->locked++;
+          p = p->pNext;
+        }
+      }
+    }
+  }
+}
+SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
+  int i;
+  Btree *p;
+  assert( sqlite3_mutex_held(db->mutex) );
+  for(i=0; i<db->nDb; i++){
+    p = db->aDb[i].pBt;
+    if( p && p->sharable ){
+      assert( p->wantToLock>0 );
+      p->wantToLock--;
+      if( p->wantToLock==0 ){
+        assert( p->locked );
+        sqlite3_mutex_leave(p->pBt->mutex);
+        p->locked = 0;
+      }
+    }
+  }
+}
+
+#ifndef NDEBUG
+/*
+** Return true if the current thread holds the database connection
+** mutex and all required BtShared mutexes.
+**
+** This routine is used inside assert() statements only.
+*/
+SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
+  int i;
+  if( !sqlite3_mutex_held(db->mutex) ){
+    return 0;
+  }
+  for(i=0; i<db->nDb; i++){
+    Btree *p;
+    p = db->aDb[i].pBt;
+    if( p && p->sharable &&
+         (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
+      return 0;
+    }
+  }
+  return 1;
+}
+#endif /* NDEBUG */
+
+/*
+** Potentially dd a new Btree pointer to a BtreeMutexArray.
+** Really only add the Btree if it can possibly be shared with
+** another database connection.
+**
+** The Btrees are kept in sorted order by pBtree->pBt.  That
+** way when we go to enter all the mutexes, we can enter them
+** in order without every having to backup and retry and without
+** worrying about deadlock.
+**
+** The number of shared btrees will always be small (usually 0 or 1)
+** so an insertion sort is an adequate algorithm here.
+*/
+SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
+  int i, j;
+  BtShared *pBt;
+  if( pBtree==0 || pBtree->sharable==0 ) return;
+#ifndef NDEBUG
+  {
+    for(i=0; i<pArray->nMutex; i++){
+      assert( pArray->aBtree[i]!=pBtree );
+    }
+  }
+#endif
+  assert( pArray->nMutex>=0 );
+  assert( pArray->nMutex<sizeof(pArray->aBtree)/sizeof(pArray->aBtree[0])-1 );
+  pBt = pBtree->pBt;
+  for(i=0; i<pArray->nMutex; i++){
+    assert( pArray->aBtree[i]!=pBtree );
+    if( pArray->aBtree[i]->pBt>pBt ){
+      for(j=pArray->nMutex; j>i; j--){
+        pArray->aBtree[j] = pArray->aBtree[j-1];
+      }
+      pArray->aBtree[i] = pBtree;
+      pArray->nMutex++;
+      return;
+    }
+  }
+  pArray->aBtree[pArray->nMutex++] = pBtree;
+}
+
+/*
+** Enter the mutex of every btree in the array.  This routine is
+** called at the beginning of sqlite3VdbeExec().  The mutexes are
+** exited at the end of the same function.
+*/
+SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
+  int i;
+  for(i=0; i<pArray->nMutex; i++){
+    Btree *p = pArray->aBtree[i];
+    /* Some basic sanity checking */
+    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
+    assert( !p->locked || p->wantToLock>0 );
+
+    /* We should already hold a lock on the database connection */
+    assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+
+    p->wantToLock++;
+    if( !p->locked && p->sharable ){
+      sqlite3_mutex_enter(p->pBt->mutex);
+      p->locked = 1;
+    }
+  }
+}
+
+/*
+** Leave the mutex of every btree in the group.
+*/
+SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
+  int i;
+  for(i=0; i<pArray->nMutex; i++){
+    Btree *p = pArray->aBtree[i];
+    /* Some basic sanity checking */
+    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
+    assert( p->locked || !p->sharable );
+    assert( p->wantToLock>0 );
+
+    /* We should already hold a lock on the database connection */
+    assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+
+    p->wantToLock--;
+    if( p->wantToLock==0 && p->locked ){
+      sqlite3_mutex_leave(p->pBt->mutex);
+      p->locked = 0;
+    }
+  }
+}
+
+
+#endif  /* SQLITE_THREADSAFE && !SQLITE_OMIT_SHARED_CACHE */
+
+/************** End of btmutex.c *********************************************/
+/************** Begin file btree.c *******************************************/
+/*
+** 2004 April 6
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** $Id: btree.c,v 1.426 2007/09/12 17:01:45 danielk1977 Exp $
+**
+** This file implements a external (disk-based) database using BTrees.
+** See the header comment on "btreeInt.h" for additional information.
+** Including a description of file format and an overview of operation.
+*/
 
 /*
 ** The header string that appears at the beginning of every
 ** SQLite database.
 */
 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
-
 
 /*
 ** Set this global variable to 1 to enable tracing using the TRACE
 ** macro.
@@ -22897,8 +24973,42 @@
 */
 #if SQLITE_TEST
 int sqlite3_btree_trace=0;  /* True to enable tracing */
 #endif
+
+
+
+#ifndef SQLITE_OMIT_SHARED_CACHE
+/*
+** A flag to indicate whether or not shared cache is enabled.  Also,
+** a list of BtShared objects that are eligible for participation
+** in shared cache.  The variables have file scope during normal builds,
+** but the test harness needs to access these variables so we make them
+** global for test builds.
+*/
+#ifdef SQLITE_TEST
+SQLITE_PRIVATE BtShared *sqlite3SharedCacheList = 0;
+SQLITE_PRIVATE int sqlite3SharedCacheEnabled = 0;
+#else
+static BtShared *sqlite3SharedCacheList = 0;
+static int sqlite3SharedCacheEnabled = 0;
+#endif
+#endif /* SQLITE_OMIT_SHARED_CACHE */
+
+#ifndef SQLITE_OMIT_SHARED_CACHE
+/*
+** Enable or disable the shared pager and schema features.
+**
+** This routine has no effect on existing database connections.
+** The shared cache setting effects only future calls to
+** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
+*/
+SQLITE_API int sqlite3_enable_shared_cache(int enable){
+  sqlite3SharedCacheEnabled = enable;
+  return SQLITE_OK;
+}
+#endif
+
 
 /*
 ** Forward declaration
 */
@@ -22916,10 +25026,11 @@
   */
   #define queryTableLock(a,b,c) SQLITE_OK
   #define lockTable(a,b,c) SQLITE_OK
   #define unlockAllTables(a)
-#else
-
+#endif
+
+#ifndef SQLITE_OMIT_SHARED_CACHE
 /*
 ** Query to see if btree handle p may obtain a lock of type eLock
 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
 ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
@@ -22928,10 +25039,12 @@
 static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
   BtShared *pBt = p->pBt;
   BtLock *pIter;
 
+  assert( sqlite3BtreeHoldsMutex(p) );
+
   /* This is a no-op if the shared-cache is not enabled */
-  if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
+  if( !p->sharable ){
     return SQLITE_OK;
   }
 
   /* This (along with lockTable()) is where the ReadUncommitted flag is
@@ -22962,9 +25075,11 @@
     }
   }
   return SQLITE_OK;
 }
-
+#endif /* !SQLITE_OMIT_SHARED_CACHE */
+
+#ifndef SQLITE_OMIT_SHARED_CACHE
 /*
 ** Add a lock on the table with root-page iTable to the shared-btree used
 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
 ** WRITE_LOCK.
@@ -22976,10 +25091,12 @@
   BtShared *pBt = p->pBt;
   BtLock *pLock = 0;
   BtLock *pIter;
 
+  assert( sqlite3BtreeHoldsMutex(p) );
+
   /* This is a no-op if the shared-cache is not enabled */
-  if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
+  if( !p->sharable ){
     return SQLITE_OK;
   }
 
   assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
@@ -23009,9 +25126,9 @@
   /* If the above search did not find a BtLock struct associating Btree p
   ** with table iTable, allocate one and link it into the list.
   */
   if( !pLock ){
-    pLock = (BtLock *)sqliteMalloc(sizeof(BtLock));
+    pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
     if( !pLock ){
       return SQLITE_NOMEM;
     }
     pLock->iTable = iTable;
@@ -23030,27 +25147,26 @@
   }
 
   return SQLITE_OK;
 }
-
+#endif /* !SQLITE_OMIT_SHARED_CACHE */
+
+#ifndef SQLITE_OMIT_SHARED_CACHE
 /*
 ** Release all the table locks (locks obtained via calls to the lockTable()
 ** procedure) held by Btree handle p.
 */
 static void unlockAllTables(Btree *p){
   BtLock **ppIter = &p->pBt->pLock;
 
-  /* If the shared-cache extension is not enabled, there should be no
-  ** locks in the BtShared.pLock list, making this procedure a no-op. Assert
-  ** that this is the case.
-  */
-  assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter );
+  assert( sqlite3BtreeHoldsMutex(p) );
+  assert( p->sharable || 0==*ppIter );
 
   while( *ppIter ){
     BtLock *pLock = *ppIter;
     if( pLock->pBtree==p ){
       *ppIter = pLock->pNext;
-      sqliteFree(pLock);
+      sqlite3_free(pLock);
     }else{
       ppIter = &pLock->pNext;
     }
   }
@@ -23058,14 +25174,25 @@
 #endif /* SQLITE_OMIT_SHARED_CACHE */
 
 static void releasePage(MemPage *pPage);  /* Forward reference */
 
+/*
+** Verify that the cursor holds a mutex on the BtShared
+*/
+#ifndef NDEBUG
+static int cursorHoldsMutex(BtCursor *p){
+  return sqlite3_mutex_held(p->pBt->mutex);
+}
+#endif
+
+
 #ifndef SQLITE_OMIT_INCRBLOB
 /*
 ** Invalidate the overflow page-list cache for cursor pCur, if any.
 */
 static void invalidateOverflowCache(BtCursor *pCur){
-  sqliteFree(pCur->aOverflow);
+  assert( cursorHoldsMutex(pCur) );
+  sqlite3_free(pCur->aOverflow);
   pCur->aOverflow = 0;
 }
 
 /*
@@ -23073,8 +25200,9 @@
 ** on the shared btree structure pBt.
 */
 static void invalidateAllOverflowCache(BtShared *pBt){
   BtCursor *p;
+  assert( sqlite3_mutex_held(pBt->mutex) );
   for(p=pBt->pCursor; p; p=p->pNext){
     invalidateOverflowCache(p);
   }
 }
@@ -23091,8 +25219,9 @@
   int rc;
 
   assert( CURSOR_VALID==pCur->eState );
   assert( 0==pCur->pKey );
+  assert( cursorHoldsMutex(pCur) );
 
   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
 
   /* If this is an intKey table, then the above call to BtreeKeySize()
@@ -23101,15 +25230,15 @@
   ** table, then malloc space for and store the pCur->nKey bytes of key
   ** data.
   */
   if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
-    void *pKey = sqliteMalloc(pCur->nKey);
+    void *pKey = sqlite3_malloc(pCur->nKey);
     if( pKey ){
       rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
       if( rc==SQLITE_OK ){
         pCur->pKey = pKey;
       }else{
-        sqliteFree(pKey);
+        sqlite3_free(pKey);
       }
     }else{
       rc = SQLITE_NOMEM;
     }
@@ -23132,8 +25261,10 @@
 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
 */
 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
   BtCursor *p;
+  assert( sqlite3_mutex_held(pBt->mutex) );
+  assert( pExcept==0 || pExcept->pBt==pBt );
   for(p=pBt->pCursor; p; p=p->pNext){
     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
         p->eState==CURSOR_VALID ){
       int rc = saveCursorPosition(p);
@@ -23148,9 +25279,10 @@
 /*
 ** Clear the current cursor position.
 */
 static void clearCursorPosition(BtCursor *pCur){
-  sqliteFree(pCur->pKey);
+  assert( cursorHoldsMutex(pCur) );
+  sqlite3_free(pCur->pKey);
   pCur->pKey = 0;
   pCur->eState = CURSOR_INVALID;
 }
 
@@ -23166,9 +25298,13 @@
 ** and the cursor state set to CURSOR_INVALID.
 */
 SQLITE_PRIVATE int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){
   int rc;
-  assert( pCur->eState==CURSOR_REQUIRESEEK );
+  assert( cursorHoldsMutex(pCur) );
+  assert( pCur->eState>=CURSOR_REQUIRESEEK );
+  if( pCur->eState==CURSOR_FAULT ){
+    return pCur->skip;
+  }
 #ifndef SQLITE_OMIT_INCRBLOB
   if( pCur->isIncrblobHandle ){
     return SQLITE_ABORT;
   }
@@ -23175,17 +25311,17 @@
 #endif
   pCur->eState = CURSOR_INVALID;
   rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
   if( rc==SQLITE_OK ){
-    sqliteFree(pCur->pKey);
+    sqlite3_free(pCur->pKey);
     pCur->pKey = 0;
     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
   }
   return rc;
 }
 
 #define restoreOrClearCursorPosition(p) \
-  (p->eState==CURSOR_REQUIRESEEK ? \
+  (p->eState>=CURSOR_REQUIRESEEK ? \
          sqlite3BtreeRestoreOrClearCursorPosition(p) : \
          SQLITE_OK)
 
 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -23194,11 +25330,13 @@
 ** number for the pointer-map page that contains the entry for the
 ** input page number.
 */
 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
-  int nPagesPerMapPage = (pBt->usableSize/5)+1;
-  int iPtrMap = (pgno-2)/nPagesPerMapPage;
-  int ret = (iPtrMap*nPagesPerMapPage) + 2;
+  int nPagesPerMapPage, iPtrMap, ret;
+  assert( sqlite3_mutex_held(pBt->mutex) );
+  nPagesPerMapPage = (pBt->usableSize/5)+1;
+  iPtrMap = (pgno-2)/nPagesPerMapPage;
+  ret = (iPtrMap*nPagesPerMapPage) + 2;
   if( ret==PENDING_BYTE_PAGE(pBt) ){
     ret++;
   }
   return ret;
@@ -23217,8 +25355,9 @@
   Pgno iPtrmap;     /* The pointer map page number */
   int offset;       /* Offset in pointer map page */
   int rc;
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   /* The master-journal page number must never be used as a pointer map page */
   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
 
   assert( pBt->autoVacuum );
@@ -23259,8 +25398,10 @@
   u8 *pPtrmap;       /* Pointer map page data */
   int offset;        /* Offset of entry in pointer map */
   int rc;
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
+
   iPtrmap = PTRMAP_PAGENO(pBt, key);
   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
   if( rc!=0 ){
     return rc;
@@ -23287,20 +25428,23 @@
 ** This routine works only for pages that do not contain overflow cells.
 */
 #define findCell(pPage, iCell) \
   ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)]))
+#ifdef SQLITE_TEST
 SQLITE_PRIVATE u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){
   assert( iCell>=0 );
   assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) );
   return findCell(pPage, iCell);
 }
+#endif
 
 /*
 ** This a more complex version of sqlite3BtreeFindCell() that works for
 ** pages that do contain overflow cells.  See insert
 */
 static u8 *findOverflowCell(MemPage *pPage, int iCell){
   int i;
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   for(i=pPage->nOverflow-1; i>=0; i--){
     int k;
     struct _OvflCell *pOvfl;
     pOvfl = &pPage->aOvfl[i];
@@ -23330,8 +25474,10 @@
   CellInfo *pInfo         /* Fill in this structure */
 ){
   int n;                  /* Number bytes in cell content header */
   u32 nPayload;           /* Number of bytes of cell payload */
+
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 
   pInfo->pCell = pCell;
   assert( pPage->leaf==0 || pPage->leaf==1 );
   n = pPage->childPtrSize;
@@ -23443,8 +25589,9 @@
 ** for the overflow page.
 */
 static int ptrmapPutOvfl(MemPage *pPage, int iCell){
   u8 *pCell;
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   pCell = findOverflowCell(pPage, iCell);
   return ptrmapPutOvflPtr(pPage, pCell);
 }
 #endif
@@ -23472,9 +25619,10 @@
   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   assert( pPage->pBt!=0 );
   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
   assert( pPage->nOverflow==0 );
-  temp = sqliteMalloc( pPage->pBt->pageSize );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
+  temp = sqlite3_malloc( pPage->pBt->pageSize );
   if( temp==0 ) return SQLITE_NOMEM;
   data = pPage->aData;
   hdr = pPage->hdrOffset;
   cellOffset = pPage->cellOffset;
@@ -23500,9 +25648,9 @@
   data[hdr+2] = 0;
   data[hdr+7] = 0;
   addr = cellOffset+2*nCell;
   memset(&data[addr], 0, brk-addr);
-  sqliteFree(temp);
+  sqlite3_free(temp);
   return SQLITE_OK;
 }
 
 /*
@@ -23528,8 +25676,9 @@
 
   data = pPage->aData;
   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   assert( pPage->pBt );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   if( nByte<4 ) nByte = 4;
   if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
   pPage->nFree -= nByte;
   hdr = pPage->hdrOffset;
@@ -23586,8 +25735,9 @@
   assert( pPage->pBt!=0 );
   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
   assert( (start + size)<=pPage->pBt->usableSize );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   if( size<4 ) size = 4;
 
 #ifdef SQLITE_SECURE_DELETE
   /* Overwrite deleted information with zeros when the SECURE_DELETE
@@ -23646,8 +25796,9 @@
 static void decodeFlags(MemPage *pPage, int flagByte){
   BtShared *pBt;     /* A copy of pPage->pBt */
 
   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
   pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
   pPage->leaf = (flagByte & PTF_LEAF)!=0;
   pPage->childPtrSize = 4*(pPage->leaf==0);
@@ -23692,10 +25843,12 @@
 
   pBt = pPage->pBt;
   assert( pBt!=0 );
   assert( pParent==0 || pParent->pBt==pBt );
+  assert( sqlite3_mutex_held(pBt->mutex) );
   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
-  assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] );
+  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
+  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
   if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
     /* The parent page should never change unless the file is corrupt */
     return SQLITE_CORRUPT_BKPT;
   }
@@ -23760,10 +25913,12 @@
   int hdr = pPage->hdrOffset;
   int first;
 
   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
-  assert( &data[pBt->pageSize] == (unsigned char*)pPage );
+  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
+  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
+  assert( sqlite3_mutex_held(pBt->mutex) );
   memset(&data[hdr], 0, pBt->usableSize - hdr);
   data[hdr] = flags;
   first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
   memset(&data[hdr+1], 0, 4);
@@ -23799,8 +25954,9 @@
   int rc;
   MemPage *pPage;
   DbPage *pDbPage;
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
   if( rc ) return rc;
   pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
   pPage->aData = sqlite3PagerGetData(pDbPage);
@@ -23823,8 +25979,9 @@
   MemPage **ppPage,    /* Write the page pointer here */
   MemPage *pParent     /* Parent of the page */
 ){
   int rc;
+  assert( sqlite3_mutex_held(pBt->mutex) );
   if( pgno==0 ){
     return SQLITE_CORRUPT_BKPT;
   }
   rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
@@ -23841,9 +25998,11 @@
 static void releasePage(MemPage *pPage){
   if( pPage ){
     assert( pPage->aData );
     assert( pPage->pBt );
-    assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage );
+    assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
+    assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
+    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
     sqlite3PagerUnref(pPage->pDbPage);
   }
 }
 
@@ -23855,10 +26014,12 @@
 static void pageDestructor(DbPage *pData, int pageSize){
   MemPage *pPage;
   assert( (pageSize & 7)==0 );
   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
+  assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) );
   if( pPage->pParent ){
     MemPage *pParent = pPage->pParent;
+    assert( pParent->pBt==pPage->pBt );
     pPage->pParent = 0;
     releasePage(pParent);
   }
   pPage->isInit = 0;
@@ -23876,8 +26037,9 @@
   MemPage *pPage;
   assert( (pageSize & 7)==0 );
   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
   if( pPage->isInit ){
+    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
     pPage->isInit = 0;
     sqlite3BtreeInitPage(pPage, pPage->pParent);
   }
 }
@@ -23887,23 +26049,24 @@
 **
 ** zFilename is the name of the database file.  If zFilename is NULL
 ** a new database with a random name is created.  This randomly named
 ** database file will be deleted when sqlite3BtreeClose() is called.
+** If zFilename is ":memory:" then an in-memory database is created
+** that is automatically destroyed when it is closed.
 */
 SQLITE_PRIVATE int sqlite3BtreeOpen(
   const char *zFilename,  /* Name of the file containing the BTree database */
   sqlite3 *pSqlite,       /* Associated database handle */
   Btree **ppBtree,        /* Pointer to new Btree object written here */
-  int flags               /* Options */
-){
-  BtShared *pBt;          /* Shared part of btree structure */
+  int flags,              /* Options */
+  int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
+){
+  sqlite3_vfs *pVfs;      /* The VFS to use for this btree */
+  BtShared *pBt = 0;      /* Shared part of btree structure */
   Btree *p;               /* Handle to return */
   int rc = SQLITE_OK;
   int nReserve;
   unsigned char zDbHeader[100];
-#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
-  const ThreadData *pTsdro;
-#endif
 
   /* Set the variable isMemdb to true for an in-memory database, or
   ** false for a file-based database. This symbol is only required if
   ** either of the shared-data or autovacuum features are compiled
@@ -23916,127 +26079,241 @@
     const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
   #endif
 #endif
 
-  p = sqliteMalloc(sizeof(Btree));
+  assert( pSqlite!=0 );
+  assert( sqlite3_mutex_held(pSqlite->mutex) );
+
+  pVfs = pSqlite->pVfs;
+  p = sqlite3MallocZero(sizeof(Btree));
   if( !p ){
     return SQLITE_NOMEM;
   }
   p->inTrans = TRANS_NONE;
   p->pSqlite = pSqlite;
 
-  /* Try to find an existing Btree structure opened on zFilename. */
 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
-  pTsdro = sqlite3ThreadDataReadOnly();
-  if( pTsdro->useSharedData && zFilename && !isMemdb ){
-    char *zFullPathname = sqlite3OsFullPathname(zFilename);
-    if( !zFullPathname ){
-      sqliteFree(p);
-      return SQLITE_NOMEM;
-    }
-    for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){
-      assert( pBt->nRef>0 );
-      if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) ){
-        p->pBt = pBt;
-        *ppBtree = p;
-        pBt->nRef++;
-        sqliteFree(zFullPathname);
-        return SQLITE_OK;
-      }
-    }
-    sqliteFree(zFullPathname);
-  }
-#endif
-
-  /*
-  ** The following asserts make sure that structures used by the btree are
-  ** the right size.  This is to guard against size changes that result
-  ** when compiling on a different architecture.
-  */
-  assert( sizeof(i64)==8 || sizeof(i64)==4 );
-  assert( sizeof(u64)==8 || sizeof(u64)==4 );
-  assert( sizeof(u32)==4 );
-  assert( sizeof(u16)==2 );
-  assert( sizeof(Pgno)==4 );
-
-  pBt = sqliteMalloc( sizeof(*pBt) );
+  /*
+  ** If this Btree is a candidate for shared cache, try to find an
+  ** existing BtShared object that we can share with
+  */
+  if( (flags & BTREE_PRIVATE)==0
+   && isMemdb==0
+   && (pSqlite->flags & SQLITE_Vtab)==0
+   && zFilename && zFilename[0]
+  ){
+    if( sqlite3SharedCacheEnabled ){
+      char *zFullPathname = (char *)sqlite3_malloc(pVfs->mxPathname);
+      sqlite3_mutex *mutexShared;
+      p->sharable = 1;
+      if( pSqlite ){
+        pSqlite->flags |= SQLITE_SharedCache;
+      }
+      if( !zFullPathname ){
+        sqlite3_free(p);
+        return SQLITE_NOMEM;
+      }
+      sqlite3OsFullPathname(pVfs, zFilename, zFullPathname);
+      mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+      sqlite3_mutex_enter(mutexShared);
+      for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
+        assert( pBt->nRef>0 );
+        if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
+                 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
+          p->pBt = pBt;
+          pBt->nRef++;
+          break;
+        }
+      }
+      sqlite3_mutex_leave(mutexShared);
+      sqlite3_free(zFullPathname);
+    }
+#ifdef SQLITE_DEBUG
+    else{
+      /* In debug mode, we mark all persistent databases as sharable
+      ** even when they are not.  This exercises the locking code and
+      ** gives more opportunity for asserts(sqlite3_mutex_held())
+      ** statements to find locking problems.
+      */
+      p->sharable = 1;
+    }
+#endif
+  }
+#endif
   if( pBt==0 ){
-    rc = SQLITE_NOMEM;
-    goto btree_open_out;
-  }
-  rc = sqlite3PagerOpen(&pBt->pPager, zFilename, EXTRA_SIZE, flags);
-  if( rc==SQLITE_OK ){
-    rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
-  }
-  if( rc!=SQLITE_OK ){
-    goto btree_open_out;
-  }
-  p->pBt = pBt;
-
-  sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
-  sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
-  pBt->pCursor = 0;
-  pBt->pPage1 = 0;
-  pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
-  pBt->pageSize = get2byte(&zDbHeader[16]);
-  if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
-       || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
-    pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
-    pBt->maxEmbedFrac = 64;   /* 25% */
-    pBt->minEmbedFrac = 32;   /* 12.5% */
-    pBt->minLeafFrac = 32;    /* 12.5% */
-#ifndef SQLITE_OMIT_AUTOVACUUM
-    /* If the magic name ":memory:" will create an in-memory database, then
-    ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
-    ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
-    ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
-    ** regular file-name. In this case the auto-vacuum applies as per normal.
-    */
-    if( zFilename && !isMemdb ){
-      pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
-      pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
-    }
-#endif
-    nReserve = 0;
-  }else{
-    nReserve = zDbHeader[20];
-    pBt->maxEmbedFrac = zDbHeader[21];
-    pBt->minEmbedFrac = zDbHeader[22];
-    pBt->minLeafFrac = zDbHeader[23];
-    pBt->pageSizeFixed = 1;
-#ifndef SQLITE_OMIT_AUTOVACUUM
-    pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
-    pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
-#endif
-  }
-  pBt->usableSize = pBt->pageSize - nReserve;
-  assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
-  sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize);
+    /*
+    ** The following asserts make sure that structures used by the btree are
+    ** the right size.  This is to guard against size changes that result
+    ** when compiling on a different architecture.
+    */
+    assert( sizeof(i64)==8 || sizeof(i64)==4 );
+    assert( sizeof(u64)==8 || sizeof(u64)==4 );
+    assert( sizeof(u32)==4 );
+    assert( sizeof(u16)==2 );
+    assert( sizeof(Pgno)==4 );
+
+    pBt = sqlite3MallocZero( sizeof(*pBt) );
+    if( pBt==0 ){
+      rc = SQLITE_NOMEM;
+      goto btree_open_out;
+    }
+    rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
+                          EXTRA_SIZE, flags, vfsFlags);
+    if( rc==SQLITE_OK ){
+      rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
+    }
+    if( rc!=SQLITE_OK ){
+      goto btree_open_out;
+    }
+    p->pBt = pBt;
+
+    sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
+    sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
+    pBt->pCursor = 0;
+    pBt->pPage1 = 0;
+    pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
+    pBt->pageSize = get2byte(&zDbHeader[16]);
+    if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
+         || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
+      pBt->pageSize = 0;
+      sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
+      pBt->maxEmbedFrac = 64;   /* 25% */
+      pBt->minEmbedFrac = 32;   /* 12.5% */
+      pBt->minLeafFrac = 32;    /* 12.5% */
+#ifndef SQLITE_OMIT_AUTOVACUUM
+      /* If the magic name ":memory:" will create an in-memory database, then
+      ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
+      ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
+      ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
+      ** regular file-name. In this case the auto-vacuum applies as per normal.
+      */
+      if( zFilename && !isMemdb ){
+        pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
+        pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
+      }
+#endif
+      nReserve = 0;
+    }else{
+      nReserve = zDbHeader[20];
+      pBt->maxEmbedFrac = zDbHeader[21];
+      pBt->minEmbedFrac = zDbHeader[22];
+      pBt->minLeafFrac = zDbHeader[23];
+      pBt->pageSizeFixed = 1;
+#ifndef SQLITE_OMIT_AUTOVACUUM
+      pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
+      pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
+#endif
+    }
+    pBt->usableSize = pBt->pageSize - nReserve;
+    assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
+    sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
+
+#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
+    /* Add the new BtShared object to the linked list sharable BtShareds.
+    */
+    if( p->sharable ){
+      sqlite3_mutex *mutexShared;
+      pBt->nRef = 1;
+      mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+      if( SQLITE_THREADSAFE ){
+        pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
+        if( pBt->mutex==0 ){
+          rc = SQLITE_NOMEM;
+          pSqlite->mallocFailed = 0;
+          goto btree_open_out;
+        }
+      }
+      sqlite3_mutex_enter(mutexShared);
+      pBt->pNext = sqlite3SharedCacheList;
+      sqlite3SharedCacheList = pBt;
+      sqlite3_mutex_leave(mutexShared);
+    }
+#endif
+  }
 
 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
-  /* Add the new btree to the linked list starting at ThreadData.pBtree.
-  ** There is no chance that a malloc() may fail inside of the
-  ** sqlite3ThreadData() call, as the ThreadData structure must have already
-  ** been allocated for pTsdro->useSharedData to be non-zero.
-  */
-  if( pTsdro->useSharedData && zFilename && !isMemdb ){
-    pBt->pNext = pTsdro->pBtree;
-    sqlite3ThreadData()->pBtree = pBt;
-  }
-#endif
-  pBt->nRef = 1;
+  /* If the new Btree uses a sharable pBtShared, then link the new
+  ** Btree into the list of all sharable Btrees for the same connection.
+  ** The list is kept in ascending order by pBt address.
+  */
+  if( p->sharable ){
+    int i;
+    Btree *pSib;
+    for(i=0; i<pSqlite->nDb; i++){
+      if( (pSib = pSqlite->aDb[i].pBt)!=0 && pSib->sharable ){
+        while( pSib->pPrev ){ pSib = pSib->pPrev; }
+        if( p->pBt<pSib->pBt ){
+          p->pNext = pSib;
+          p->pPrev = 0;
+          pSib->pPrev = p;
+        }else{
+          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
+            pSib = pSib->pNext;
+          }
+          p->pNext = pSib->pNext;
+          p->pPrev = pSib;
+          if( p->pNext ){
+            p->pNext->pPrev = p;
+          }
+          pSib->pNext = p;
+        }
+        break;
+      }
+    }
+  }
+#endif
   *ppBtree = p;
 
 btree_open_out:
   if( rc!=SQLITE_OK ){
     if( pBt && pBt->pPager ){
       sqlite3PagerClose(pBt->pPager);
     }
-    sqliteFree(pBt);
-    sqliteFree(p);
+    sqlite3_free(pBt);
+    sqlite3_free(p);
     *ppBtree = 0;
   }
   return rc;
+}
+
+/*
+** Decrement the BtShared.nRef counter.  When it reaches zero,
+** remove the BtShared structure from the sharing list.  Return
+** true if the BtShared.nRef counter reaches zero and return
+** false if it is still positive.
+*/
+static int removeFromSharingList(BtShared *pBt){
+#ifndef SQLITE_OMIT_SHARED_CACHE
+  sqlite3_mutex *pMaster;
+  BtShared *pList;
+  int removed = 0;
+
+  assert( sqlite3_mutex_notheld(pBt->mutex) );
+  pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+  sqlite3_mutex_enter(pMaster);
+  pBt->nRef--;
+  if( pBt->nRef<=0 ){
+    if( sqlite3SharedCacheList==pBt ){
+      sqlite3SharedCacheList = pBt->pNext;
+    }else{
+      pList = sqlite3SharedCacheList;
+      while( pList && pList->pNext!=pBt ){
+        pList=pList->pNext;
+      }
+      if( pList ){
+        pList->pNext = pBt->pNext;
+      }
+    }
+    if( SQLITE_THREADSAFE ){
+      sqlite3_mutex_free(pBt->mutex);
+    }
+    removed = 1;
+  }
+  sqlite3_mutex_leave(pMaster);
+  return removed;
+#else
+  return 1;
+#endif
 }
 
 /*
 ** Close an open database and invalidate all cursors.
@@ -24044,13 +26321,11 @@
 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
   BtShared *pBt = p->pBt;
   BtCursor *pCur;
 
-#ifndef SQLITE_OMIT_SHARED_CACHE
-  ThreadData *pTsd;
-#endif
-
   /* Close all cursors opened via this handle.  */
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+  sqlite3BtreeEnter(p);
   pCur = pBt->pCursor;
   while( pCur ){
     BtCursor *pTmp = pCur;
     pCur = pCur->pNext;
@@ -24063,47 +26338,38 @@
   ** The call to sqlite3BtreeRollback() drops any table-locks held by
   ** this handle.
   */
   sqlite3BtreeRollback(p);
-  sqliteFree(p);
-
-#ifndef SQLITE_OMIT_SHARED_CACHE
+  sqlite3BtreeLeave(p);
+
   /* If there are still other outstanding references to the shared-btree
   ** structure, return now. The remainder of this procedure cleans
   ** up the shared-btree.
   */
-  assert( pBt->nRef>0 );
-  pBt->nRef--;
-  if( pBt->nRef ){
-    return SQLITE_OK;
-  }
-
-  /* Remove the shared-btree from the thread wide list. Call
-  ** ThreadDataReadOnly() and then cast away the const property of the
-  ** pointer to avoid allocating thread data if it is not really required.
-  */
-  pTsd = (ThreadData *)sqlite3ThreadDataReadOnly();
-  if( pTsd->pBtree==pBt ){
-    assert( pTsd==sqlite3ThreadData() );
-    pTsd->pBtree = pBt->pNext;
-  }else{
-    BtShared *pPrev;
-    for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){}
-    if( pPrev ){
-      assert( pTsd==sqlite3ThreadData() );
-      pPrev->pNext = pBt->pNext;
-    }
-  }
-#endif
-
-  /* Close the pager and free the shared-btree structure */
-  assert( !pBt->pCursor );
-  sqlite3PagerClose(pBt->pPager);
-  if( pBt->xFreeSchema && pBt->pSchema ){
-    pBt->xFreeSchema(pBt->pSchema);
-  }
-  sqliteFree(pBt->pSchema);
-  sqliteFree(pBt);
+  assert( p->wantToLock==0 && p->locked==0 );
+  if( !p->sharable || removeFromSharingList(pBt) ){
+    /* The pBt is no longer on the sharing list, so we can access
+    ** it without having to hold the mutex.
+    **
+    ** Clean out and delete the BtShared object.
+    */
+    assert( !pBt->pCursor );
+    sqlite3PagerClose(pBt->pPager);
+    if( pBt->xFreeSchema && pBt->pSchema ){
+      pBt->xFreeSchema(pBt->pSchema);
+    }
+    sqlite3_free(pBt->pSchema);
+    sqlite3_free(pBt);
+  }
+
+#ifndef SQLITE_OMIT_SHARED_CACHE
+  assert( p->wantToLock==0 );
+  assert( p->locked==0 );
+  if( p->pPrev ) p->pPrev->pNext = p->pNext;
+  if( p->pNext ) p->pNext->pPrev = p->pPrev;
+#endif
+
+  sqlite3_free(p);
   return SQLITE_OK;
 }
 
 /*
@@ -24110,10 +26376,13 @@
 ** Change the busy handler callback function.
 */
 SQLITE_PRIVATE int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){
   BtShared *pBt = p->pBt;
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+  sqlite3BtreeEnter(p);
   pBt->pBusyHandler = pHandler;
   sqlite3PagerSetBusyhandler(pBt->pPager, pHandler);
+  sqlite3BtreeLeave(p);
   return SQLITE_OK;
 }
 
 /*
@@ -24132,9 +26401,12 @@
 ** normally a worry.
 */
 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
   BtShared *pBt = p->pBt;
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+  sqlite3BtreeEnter(p);
   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
+  sqlite3BtreeLeave(p);
   return SQLITE_OK;
 }
 
 /*
@@ -24147,9 +26419,12 @@
 */
 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
   BtShared *pBt = p->pBt;
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+  sqlite3BtreeEnter(p);
   sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
+  sqlite3BtreeLeave(p);
   return SQLITE_OK;
 }
 #endif
 
@@ -24158,10 +26433,15 @@
 ** words, return TRUE if no sync() occurs on the disk files.
 */
 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
   BtShared *pBt = p->pBt;
+  int rc;
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+  sqlite3BtreeEnter(p);
   assert( pBt && pBt->pPager );
-  return sqlite3PagerNosync(pBt->pPager);
+  rc = sqlite3PagerNosync(pBt->pPager);
+  sqlite3BtreeLeave(p);
+  return rc;
 }
 
 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
 /*
@@ -24179,10 +26459,13 @@
 ** If parameter nReserve is less than zero, then the number of reserved
 ** bytes per page is left unchanged.
 */
 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
-  BtShared *pBt = p->pBt;
+  int rc = SQLITE_OK;
+  BtShared *pBt = p->pBt;
+  sqlite3BtreeEnter(p);
   if( pBt->pageSizeFixed ){
+    sqlite3BtreeLeave(p);
     return SQLITE_READONLY;
   }
   if( nReserve<0 ){
     nReserve = pBt->pageSize - pBt->usableSize;
@@ -24190,12 +26473,14 @@
   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
         ((pageSize-1)&pageSize)==0 ){
     assert( (pageSize & 7)==0 );
     assert( !pBt->pPage1 && !pBt->pCursor );
-    pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize);
+    pBt->pageSize = pageSize;
+    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
   }
   pBt->usableSize = pBt->pageSize - nReserve;
-  return SQLITE_OK;
+  sqlite3BtreeLeave(p);
+  return rc;
 }
 
 /*
 ** Return the currently defined page size
@@ -24203,9 +26488,13 @@
 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
   return p->pBt->pageSize;
 }
 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
-  return p->pBt->pageSize - p->pBt->usableSize;
+  int n;
+  sqlite3BtreeEnter(p);
+  n = p->pBt->pageSize - p->pBt->usableSize;
+  sqlite3BtreeLeave(p);
+  return n;
 }
 
 /*
 ** Set the maximum page count for a database if mxPage is positive.
@@ -24212,9 +26501,13 @@
 ** No changes are made if mxPage is 0 or negative.
 ** Regardless of the value of mxPage, return the maximum page count.
 */
 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
-  return sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
+  int n;
+  sqlite3BtreeEnter(p);
+  n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
+  sqlite3BtreeLeave(p);
+  return n;
 }
 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
 
 /*
@@ -24227,14 +26520,19 @@
 #ifdef SQLITE_OMIT_AUTOVACUUM
   return SQLITE_READONLY;
 #else
   BtShared *pBt = p->pBt;
+  int rc = SQLITE_OK;
   int av = (autoVacuum?1:0);
+
+  sqlite3BtreeEnter(p);
   if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
-    return SQLITE_READONLY;
-  }
-  pBt->autoVacuum = av;
-  return SQLITE_OK;
+    rc = SQLITE_READONLY;
+  }else{
+    pBt->autoVacuum = av;
+  }
+  sqlite3BtreeLeave(p);
+  return rc;
 #endif
 }
 
 /*
@@ -24244,13 +26542,17 @@
 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
 #ifdef SQLITE_OMIT_AUTOVACUUM
   return BTREE_AUTOVACUUM_NONE;
 #else
-  return (
+  int rc;
+  sqlite3BtreeEnter(p);
+  rc = (
     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
     BTREE_AUTOVACUUM_INCR
   );
+  sqlite3BtreeLeave(p);
+  return rc;
 #endif
 }
 
 
@@ -24265,8 +26567,10 @@
 */
 static int lockBtree(BtShared *pBt){
   int rc, pageSize;
   MemPage *pPage1;
+
+  assert( sqlite3_mutex_held(pBt->mutex) );
   if( pBt->pPage1 ) return SQLITE_OK;
   rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
   if( rc!=SQLITE_OK ) return rc;
 
@@ -24286,9 +26590,11 @@
     if( page1[19]>1 ){
       goto page1_init_failed;
     }
     pageSize = get2byte(&page1[16]);
-    if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ){
+    if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
+        (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
+    ){
       goto page1_init_failed;
     }
     assert( (pageSize & 7)==0 );
     pBt->pageSize = pageSize;
@@ -24340,8 +26646,10 @@
 ** busy callback if there is lock contention.
 */
 static int lockBtreeWithRetry(Btree *pRef){
   int rc = SQLITE_OK;
+
+  assert( sqlite3BtreeHoldsMutex(pRef) );
   if( pRef->inTrans==TRANS_NONE ){
     u8 inTransaction = pRef->pBt->inTransaction;
     btreeIntegrity(pRef);
     rc = sqlite3BtreeBeginTrans(pRef, 0);
@@ -24366,13 +26674,14 @@
 **
 ** If there is a transaction in progress, this routine is a no-op.
 */
 static void unlockBtreeIfUnused(BtShared *pBt){
+  assert( sqlite3_mutex_held(pBt->mutex) );
   if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
     if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
       if( pBt->pPage1->aData==0 ){
         MemPage *pPage = pBt->pPage1;
-        pPage->aData = &((u8*)pPage)[-pBt->pageSize];
+        pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
         pPage->pBt = pBt;
         pPage->pgno = 1;
       }
       releasePage(pBt->pPage1);
@@ -24389,8 +26698,10 @@
 static int newDatabase(BtShared *pBt){
   MemPage *pP1;
   unsigned char *data;
   int rc;
+
+  assert( sqlite3_mutex_held(pBt->mutex) );
   if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK;
   pP1 = pBt->pPage1;
   assert( pP1!=0 );
   data = pP1->aData;
@@ -24455,29 +26766,32 @@
 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
   BtShared *pBt = p->pBt;
   int rc = SQLITE_OK;
 
+  sqlite3BtreeEnter(p);
   btreeIntegrity(p);
 
   /* If the btree is already in a write-transaction, or it
   ** is already in a read-transaction and a read-transaction
   ** is requested, this is a no-op.
   */
   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
-    return SQLITE_OK;
+    goto trans_begun;
   }
 
   /* Write transactions are not possible on a read-only database */
   if( pBt->readOnly && wrflag ){
-    return SQLITE_READONLY;
+    rc = SQLITE_READONLY;
+    goto trans_begun;
   }
 
   /* If another database handle has already opened a write transaction
   ** on this shared-btree structure and a second write transaction is
   ** requested, return SQLITE_BUSY.
   */
   if( pBt->inTransaction==TRANS_WRITE && wrflag ){
-    return SQLITE_BUSY;
+    rc = SQLITE_BUSY;
+    goto trans_begun;
   }
 
   do {
     if( pBt->pPage1==0 ){
@@ -24512,9 +26826,12 @@
       pBt->inTransaction = p->inTrans;
     }
   }
 
+
+trans_begun:
   btreeIntegrity(p);
+  sqlite3BtreeLeave(p);
   return rc;
 }
 
 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -24531,8 +26848,9 @@
   BtShared *pBt = pPage->pBt;
   int isInitOrig = pPage->isInit;
   Pgno pgno = pPage->pgno;
 
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   rc = sqlite3BtreeInitPage(pPage, pPage->pParent);
   if( rc!=SQLITE_OK ){
     goto set_child_ptrmaps_out;
   }
@@ -24578,8 +26896,9 @@
 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
 **                   overflow page in the list.
 */
 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   if( eType==PTRMAP_OVERFLOW2 ){
     /* The pointer is always the first 4 bytes of the page in this case.  */
     if( get4byte(pPage->aData)!=iFrom ){
       return SQLITE_CORRUPT_BKPT;
@@ -24643,8 +26962,10 @@
   int rc;
 
   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
+  assert( sqlite3_mutex_held(pBt->mutex) );
+  assert( pDbPage->pBt==pBt );
 
   /* Move page iDbPage from it's current location to page number iFreePage */
   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
       iDbPage, iFreePage, iPtrPage, eType));
@@ -24721,8 +27042,9 @@
 static int incrVacuumStep(BtShared *pBt, Pgno nFin){
   Pgno iLastPg;             /* Last page in the database */
   Pgno nFreeList;           /* Number of pages still on the free-list */
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   iLastPg = pBt->nTrunc;
   if( iLastPg==0 ){
     iLastPg = sqlite3PagerPagecount(pBt->pPager);
   }
@@ -24815,15 +27137,21 @@
 ** SQLITE_DONE is returned. If it is not finished, but no error occured,
 ** SQLITE_OK is returned. Otherwise an SQLite error code.
 */
 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
-  BtShared *pBt = p->pBt;
+  int rc;
+  BtShared *pBt = p->pBt;
+
+  sqlite3BtreeEnter(p);
   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
   if( !pBt->autoVacuum ){
-    return SQLITE_DONE;
-  }
-  invalidateAllOverflowCache(pBt);
-  return incrVacuumStep(pBt, 0);
+    rc = SQLITE_DONE;
+  }else{
+    invalidateAllOverflowCache(pBt);
+    rc = incrVacuumStep(pBt, 0);
+  }
+  sqlite3BtreeLeave(p);
+  return rc;
 }
 
 /*
 ** This routine is called prior to sqlite3PagerCommit when a transaction
@@ -24840,8 +27168,9 @@
 #ifndef NDEBUG
   int nRef = sqlite3PagerRefcount(pPager);
 #endif
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   invalidateAllOverflowCache(pBt);
   assert(pBt->autoVacuum);
   if( !pBt->incrVacuum ){
     Pgno nFin = 0;
@@ -24927,17 +27256,20 @@
   int rc = SQLITE_OK;
   if( p->inTrans==TRANS_WRITE ){
     BtShared *pBt = p->pBt;
     Pgno nTrunc = 0;
+    sqlite3BtreeEnter(p);
 #ifndef SQLITE_OMIT_AUTOVACUUM
     if( pBt->autoVacuum ){
       rc = autoVacuumCommit(pBt, &nTrunc);
       if( rc!=SQLITE_OK ){
+        sqlite3BtreeLeave(p);
         return rc;
       }
     }
 #endif
     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc);
+    sqlite3BtreeLeave(p);
   }
   return rc;
 }
 
@@ -24957,8 +27289,9 @@
 */
 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p){
   BtShared *pBt = p->pBt;
 
+  sqlite3BtreeEnter(p);
   btreeIntegrity(p);
 
   /* If the handle has a write-transaction open, commit the shared-btrees
   ** transaction and set the shared state to TRANS_READ.
@@ -24968,8 +27301,9 @@
     assert( pBt->inTransaction==TRANS_WRITE );
     assert( pBt->nTransaction>0 );
     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
     if( rc!=SQLITE_OK ){
+      sqlite3BtreeLeave(p);
       return rc;
     }
     pBt->inTransaction = TRANS_READ;
     pBt->inStmt = 0;
@@ -24994,8 +27328,9 @@
   p->inTrans = TRANS_NONE;
   unlockBtreeIfUnused(pBt);
 
   btreeIntegrity(p);
+  sqlite3BtreeLeave(p);
   return SQLITE_OK;
 }
 
 /*
@@ -25002,12 +27337,14 @@
 ** Do both phases of a commit.
 */
 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
   int rc;
+  sqlite3BtreeEnter(p);
   rc = sqlite3BtreeCommitPhaseOne(p, 0);
   if( rc==SQLITE_OK ){
     rc = sqlite3BtreeCommitPhaseTwo(p);
   }
+  sqlite3BtreeLeave(p);
   return rc;
 }
 
 #ifndef NDEBUG
@@ -25014,18 +27351,51 @@
 /*
 ** Return the number of write-cursors open on this handle. This is for use
 ** in assert() expressions, so it is only compiled if NDEBUG is not
 ** defined.
+**
+** For the purposes of this routine, a write-cursor is any cursor that
+** is capable of writing to the databse.  That means the cursor was
+** originally opened for writing and the cursor has not be disabled
+** by having its state changed to CURSOR_FAULT.
 */
 static int countWriteCursors(BtShared *pBt){
   BtCursor *pCur;
   int r = 0;
   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
-    if( pCur->wrFlag ) r++;
+    if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
   }
   return r;
 }
 #endif
+
+/*
+** This routine sets the state to CURSOR_FAULT and the error
+** code to errCode for every cursor on BtShared that pBtree
+** references.
+**
+** Every cursor is tripped, including cursors that belong
+** to other database connections that happen to be sharing
+** the cache with pBtree.
+**
+** This routine gets called when a rollback occurs.
+** All cursors using the same cache must be tripped
+** to prevent them from trying to use the btree after
+** the rollback.  The rollback may have deleted tables
+** or moved root pages, so it is not sufficient to
+** save the state of the cursor.  The cursor must be
+** invalidated.
+*/
+SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
+  BtCursor *p;
+  sqlite3BtreeEnter(pBtree);
+  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
+    clearCursorPosition(p);
+    p->eState = CURSOR_FAULT;
+    p->skip = errCode;
+  }
+  sqlite3BtreeLeave(pBtree);
+}
 
 /*
 ** Rollback the transaction in progress.  All cursors will be
 ** invalided by this operation.  Any attempt to use a cursor
@@ -25039,8 +27409,9 @@
   int rc;
   BtShared *pBt = p->pBt;
   MemPage *pPage1;
 
+  sqlite3BtreeEnter(p);
   rc = saveAllCursors(pBt, 0, 0);
 #ifndef SQLITE_OMIT_SHARED_CACHE
   if( rc!=SQLITE_OK ){
     /* This is a horrible situation. An IO or malloc() error occured whilst
@@ -25049,14 +27420,9 @@
     ** the cache may be internally inconsistent (not contain valid trees) so
     ** we cannot simply return the error to the caller. Instead, abort
     ** all queries that may be using any of the cursors that failed to save.
     */
-    while( pBt->pCursor ){
-      sqlite3 *db = pBt->pCursor->pBtree->pSqlite;
-      if( db ){
-        sqlite3AbortOtherActiveVdbes(db, 0);
-      }
-    }
+    sqlite3BtreeTripAllCursors(p, rc);
   }
 #endif
   btreeIntegrity(p);
   unlockAllTables(p);
@@ -25096,8 +27462,9 @@
   pBt->inStmt = 0;
   unlockBtreeIfUnused(pBt);
 
   btreeIntegrity(p);
+  sqlite3BtreeLeave(p);
   return rc;
 }
 
 /*
@@ -25117,14 +27484,17 @@
 */
 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p){
   int rc;
   BtShared *pBt = p->pBt;
+  sqlite3BtreeEnter(p);
   if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
-    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
-  }
-  assert( pBt->inTransaction==TRANS_WRITE );
-  rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
-  pBt->inStmt = 1;
+    rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+  }else{
+    assert( pBt->inTransaction==TRANS_WRITE );
+    rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
+    pBt->inStmt = 1;
+  }
+  sqlite3BtreeLeave(p);
   return rc;
 }
 
 
@@ -25134,14 +27504,16 @@
 */
 SQLITE_PRIVATE int sqlite3BtreeCommitStmt(Btree *p){
   int rc;
   BtShared *pBt = p->pBt;
+  sqlite3BtreeEnter(p);
   if( pBt->inStmt && !pBt->readOnly ){
     rc = sqlite3PagerStmtCommit(pBt->pPager);
   }else{
     rc = SQLITE_OK;
   }
   pBt->inStmt = 0;
+  sqlite3BtreeLeave(p);
   return rc;
 }
 
 /*
@@ -25154,15 +27526,17 @@
 */
 SQLITE_PRIVATE int sqlite3BtreeRollbackStmt(Btree *p){
   int rc = SQLITE_OK;
   BtShared *pBt = p->pBt;
+  sqlite3BtreeEnter(p);
   sqlite3MallocDisallow();
   if( pBt->inStmt && !pBt->readOnly ){
     rc = sqlite3PagerStmtRollback(pBt->pPager);
     assert( countWriteCursors(pBt)==0 );
     pBt->inStmt = 0;
   }
   sqlite3MallocAllow();
+  sqlite3BtreeLeave(p);
   return rc;
 }
 
 /*
@@ -25214,9 +27588,9 @@
 ** in incorrect operations.  If the comparison function is NULL, a
 ** default comparison function is used.  The comparison function is
 ** always ignored for INTKEY tables.
 */
-SQLITE_PRIVATE int sqlite3BtreeCursor(
+static int btreeCursor(
   Btree *p,                                   /* The btree */
   int iTable,                                 /* Root page of table to open */
   int wrFlag,                                 /* 1 to write. 0 read-only */
   int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
@@ -25226,8 +27600,9 @@
   int rc;
   BtCursor *pCur;
   BtShared *pBt = p->pBt;
 
+  assert( sqlite3BtreeHoldsMutex(p) );
   *ppCur = 0;
   if( wrFlag ){
     if( pBt->readOnly ){
       return SQLITE_READONLY;
@@ -25245,9 +27620,9 @@
     if( pBt->readOnly && wrFlag ){
       return SQLITE_READONLY;
     }
   }
-  pCur = sqliteMalloc( sizeof(*pCur) );
+  pCur = sqlite3MallocZero( sizeof(*pCur) );
   if( pCur==0 ){
     rc = SQLITE_NOMEM;
     goto create_cursor_exception;
   }
@@ -25267,8 +27642,9 @@
   */
   pCur->xCompare = xCmp ? xCmp : dfltCompare;
   pCur->pArg = pArg;
   pCur->pBtree = p;
+  pCur->pBt = pBt;
   pCur->wrFlag = wrFlag;
   pCur->pNext = pBt->pCursor;
   if( pCur->pNext ){
     pCur->pNext->pPrev = pCur;
@@ -25277,23 +27653,42 @@
   pCur->eState = CURSOR_INVALID;
   *ppCur = pCur;
 
   return SQLITE_OK;
+
 create_cursor_exception:
   if( pCur ){
     releasePage(pCur->pPage);
-    sqliteFree(pCur);
+    sqlite3_free(pCur);
   }
   unlockBtreeIfUnused(pBt);
   return rc;
 }
+SQLITE_PRIVATE int sqlite3BtreeCursor(
+  Btree *p,                                   /* The btree */
+  int iTable,                                 /* Root page of table to open */
+  int wrFlag,                                 /* 1 to write. 0 read-only */
+  int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
+  void *pArg,                                 /* First arg to xCompare() */
+  BtCursor **ppCur                            /* Write new cursor here */
+){
+  int rc;
+  sqlite3BtreeEnter(p);
+  rc = btreeCursor(p, iTable, wrFlag, xCmp, pArg, ppCur);
+  sqlite3BtreeLeave(p);
+  return rc;
+}
+
 
 /*
 ** Close a cursor.  The read lock on the database file is released
 ** when the last cursor is closed.
 */
 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
-  BtShared *pBt = pCur->pBtree->pBt;
+  BtShared *pBt = pCur->pBt;
+  Btree *pBtree = pCur->pBtree;
+
+  sqlite3BtreeEnter(pBtree);
   clearCursorPosition(pCur);
   if( pCur->pPrev ){
     pCur->pPrev->pNext = pCur->pNext;
   }else{
@@ -25304,9 +27699,10 @@
   }
   releasePage(pCur->pPage);
   unlockBtreeIfUnused(pBt);
   invalidateOverflowCache(pCur);
-  sqliteFree(pCur);
+  sqlite3_free(pCur);
+  sqlite3BtreeLeave(pBtree);
   return SQLITE_OK;
 }
 
 /*
@@ -25313,8 +27709,9 @@
 ** Make a temporary cursor by filling in the fields of pTempCur.
 ** The temporary cursor is not on the cursor list for the Btree.
 */
 SQLITE_PRIVATE void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
+  assert( cursorHoldsMutex(pCur) );
   memcpy(pTempCur, pCur, sizeof(*pCur));
   pTempCur->pNext = 0;
   pTempCur->pPrev = 0;
   if( pTempCur->pPage ){
@@ -25326,8 +27723,9 @@
 ** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
 ** function above.
 */
 SQLITE_PRIVATE void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
+  assert( cursorHoldsMutex(pCur) );
   if( pCur->pPage ){
     sqlite3PagerUnref(pCur->pPage->pDbPage);
   }
 }
@@ -25384,9 +27782,12 @@
 ** For a table with the INTKEY flag set, this routine returns the key
 ** itself, not the number of bytes in the key.
 */
 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
-  int rc = restoreOrClearCursorPosition(pCur);
+  int rc;
+
+  assert( cursorHoldsMutex(pCur) );
+  rc = restoreOrClearCursorPosition(pCur);
   if( rc==SQLITE_OK ){
     assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
     if( pCur->eState==CURSOR_INVALID ){
       *pSize = 0;
@@ -25405,9 +27806,12 @@
 ** pointing to an entry (which can happen, for example, if
 ** the database is empty) then *pSize is set to 0.
 */
 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
-  int rc = restoreOrClearCursorPosition(pCur);
+  int rc;
+
+  assert( cursorHoldsMutex(pCur) );
+  rc = restoreOrClearCursorPosition(pCur);
   if( rc==SQLITE_OK ){
     assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
     if( pCur->eState==CURSOR_INVALID ){
       /* Not pointing at a valid entry - set *pSize to 0. */
@@ -25445,8 +27849,9 @@
 ){
   Pgno next = 0;
   int rc;
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   /* One of these must not be NULL. Otherwise, why call this function? */
   assert(ppPage || pPgnoNext);
 
   /* If pPgnoNext is NULL, then this function is being called to obtain
@@ -25577,15 +27982,16 @@
   unsigned char *aPayload;
   int rc = SQLITE_OK;
   u32 nKey;
   int iIdx = 0;
-  MemPage *pPage = pCur->pPage;        /* Btree page of current cursor entry */
-  BtShared *pBt = pCur->pBtree->pBt;   /* Btree this cursor belongs to */
+  MemPage *pPage = pCur->pPage;     /* Btree page of current cursor entry */
+  BtShared *pBt = pCur->pBt;        /* Btree this cursor belongs to */
 
   assert( pPage );
   assert( pCur->eState==CURSOR_VALID );
   assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
   assert( offset>=0 );
+  assert( cursorHoldsMutex(pCur) );
 
   getCellInfo(pCur);
   aPayload = pCur->info.pCell + pCur->info.nHeader;
   nKey = (pPage->intKey ? 0 : pCur->info.nKey);
@@ -25627,9 +28033,9 @@
     ** (the cache is lazily populated).
     */
     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
-      pCur->aOverflow = (Pgno *)sqliteMalloc(sizeof(Pgno)*nOvfl);
+      pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
       if( nOvfl && !pCur->aOverflow ){
         rc = SQLITE_NOMEM;
       }
     }
@@ -25657,10 +28063,10 @@
 
       if( offset>=ovflSize ){
         /* The only reason to read this page is to obtain the page
         ** number for the next page in the overflow chain. The page
-	** data is not required. So first try to lookup the overflow
-	** page-list cache, if any, then fall back to the getOverflowPage()
+        ** data is not required. So first try to lookup the overflow
+        ** page-list cache, if any, then fall back to the getOverflowPage()
         ** function.
         */
 #ifndef SQLITE_OMIT_INCRBLOB
         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
@@ -25707,9 +28113,12 @@
 ** wrong.  An error is returned if "offset+amt" is larger than
 ** the available payload.
 */
 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
-  int rc = restoreOrClearCursorPosition(pCur);
+  int rc;
+
+  assert( cursorHoldsMutex(pCur) );
+  rc = restoreOrClearCursorPosition(pCur);
   if( rc==SQLITE_OK ){
     assert( pCur->eState==CURSOR_VALID );
     assert( pCur->pPage!=0 );
     if( pCur->pPage->intKey ){
@@ -25731,9 +28140,12 @@
 ** wrong.  An error is returned if "offset+amt" is larger than
 ** the available payload.
 */
 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
-  int rc = restoreOrClearCursorPosition(pCur);
+  int rc;
+
+  assert( cursorHoldsMutex(pCur) );
+  rc = restoreOrClearCursorPosition(pCur);
   if( rc==SQLITE_OK ){
     assert( pCur->eState==CURSOR_VALID );
     assert( pCur->pPage!=0 );
     assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
@@ -25772,8 +28184,9 @@
   int nLocal;
 
   assert( pCur!=0 && pCur->pPage!=0 );
   assert( pCur->eState==CURSOR_VALID );
+  assert( cursorHoldsMutex(pCur) );
   pPage = pCur->pPage;
   assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
   getCellInfo(pCur);
   aPayload = pCur->info.pCell;
@@ -25802,20 +28215,25 @@
 ** many bytes of the key or data as are available on the local
 ** b-tree page.  Write the number of available bytes into *pAmt.
 **
 ** The pointer returned is ephemeral.  The key/data may move
-** or be destroyed on the next call to any Btree routine.
+** or be destroyed on the next call to any Btree routine,
+** including calls from other threads against the same cache.
+** Hence, a mutex on the BtShared should be held prior to calling
+** this routine.
 **
 ** These routines is used to get quick access to key and data
 ** in the common case where no overflow pages are used.
 */
 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
+  assert( cursorHoldsMutex(pCur) );
   if( pCur->eState==CURSOR_VALID ){
     return (const void*)fetchPayload(pCur, pAmt, 0);
   }
   return 0;
 }
 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
+  assert( cursorHoldsMutex(pCur) );
   if( pCur->eState==CURSOR_VALID ){
     return (const void*)fetchPayload(pCur, pAmt, 1);
   }
   return 0;
@@ -25829,10 +28247,11 @@
 static int moveToChild(BtCursor *pCur, u32 newPgno){
   int rc;
   MemPage *pNewPage;
   MemPage *pOldPage;
-  BtShared *pBt = pCur->pBtree->pBt;
-
+  BtShared *pBt = pCur->pBt;
+
+  assert( cursorHoldsMutex(pCur) );
   assert( pCur->eState==CURSOR_VALID );
   rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
   if( rc ) return rc;
   pNewPage->idxParent = pCur->idx;
@@ -25857,9 +28276,12 @@
 ** virtual root page is the page that the right-pointer of page
 ** 1 is pointing to.
 */
 SQLITE_PRIVATE int sqlite3BtreeIsRootPage(MemPage *pPage){
-  MemPage *pParent = pPage->pParent;
+  MemPage *pParent;
+
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
+  pParent = pPage->pParent;
   if( pParent==0 ) return 1;
   if( pParent->pgno>1 ) return 0;
   if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
   return 0;
@@ -25877,8 +28299,9 @@
   MemPage *pParent;
   MemPage *pPage;
   int idxParent;
 
+  assert( cursorHoldsMutex(pCur) );
   assert( pCur->eState==CURSOR_VALID );
   pPage = pCur->pPage;
   assert( pPage!=0 );
   assert( !sqlite3BtreeIsRootPage(pPage) );
@@ -25898,11 +28321,19 @@
 */
 static int moveToRoot(BtCursor *pCur){
   MemPage *pRoot;
   int rc = SQLITE_OK;
-  BtShared *pBt = pCur->pBtree->pBt;
-
-  if( pCur->eState==CURSOR_REQUIRESEEK ){
+  Btree *p = pCur->pBtree;
+  BtShared *pBt = p->pBt;
+
+  assert( cursorHoldsMutex(pCur) );
+  assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
+  assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
+  assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
+  if( pCur->eState>=CURSOR_REQUIRESEEK ){
+    if( pCur->eState==CURSOR_FAULT ){
+      return pCur->skip;
+    }
     clearCursorPosition(pCur);
   }
   pRoot = pCur->pPage;
   if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
@@ -25939,19 +28370,19 @@
 ** in ascending order.
 */
 static int moveToLeftmost(BtCursor *pCur){
   Pgno pgno;
-  int rc;
+  int rc = SQLITE_OK;
   MemPage *pPage;
 
+  assert( cursorHoldsMutex(pCur) );
   assert( pCur->eState==CURSOR_VALID );
-  while( !(pPage = pCur->pPage)->leaf ){
+  while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
     assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
     pgno = get4byte(findCell(pPage, pCur->idx));
     rc = moveToChild(pCur, pgno);
-    if( rc ) return rc;
-  }
-  return SQLITE_OK;
+  }
+  return rc;
 }
 
 /*
 ** Move the cursor down to the right-most leaf entry beneath the
@@ -25964,20 +28395,22 @@
 ** key in ascending order.
 */
 static int moveToRightmost(BtCursor *pCur){
   Pgno pgno;
-  int rc;
+  int rc = SQLITE_OK;
   MemPage *pPage;
 
+  assert( cursorHoldsMutex(pCur) );
   assert( pCur->eState==CURSOR_VALID );
-  while( !(pPage = pCur->pPage)->leaf ){
+  while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
     pCur->idx = pPage->nCell;
     rc = moveToChild(pCur, pgno);
-    if( rc ) return rc;
-  }
-  pCur->idx = pPage->nCell - 1;
-  pCur->info.nSize = 0;
+  }
+  if( rc==SQLITE_OK ){
+    pCur->idx = pPage->nCell - 1;
+    pCur->info.nSize = 0;
+  }
   return SQLITE_OK;
 }
 
 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
@@ -25985,18 +28418,23 @@
 ** or set *pRes to 1 if the table is empty.
 */
 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
   int rc;
+
+  assert( cursorHoldsMutex(pCur) );
+  assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
   rc = moveToRoot(pCur);
-  if( rc ) return rc;
-  if( pCur->eState==CURSOR_INVALID ){
-    assert( pCur->pPage->nCell==0 );
-    *pRes = 1;
-    return SQLITE_OK;
-  }
-  assert( pCur->pPage->nCell>0 );
-  *pRes = 0;
-  rc = moveToLeftmost(pCur);
+  if( rc==SQLITE_OK ){
+    if( pCur->eState==CURSOR_INVALID ){
+      assert( pCur->pPage->nCell==0 );
+      *pRes = 1;
+      rc = SQLITE_OK;
+    }else{
+      assert( pCur->pPage->nCell>0 );
+      *pRes = 0;
+      rc = moveToLeftmost(pCur);
+    }
+  }
   return rc;
 }
 
 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
@@ -26004,18 +28442,22 @@
 ** or set *pRes to 1 if the table is empty.
 */
 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
   int rc;
+
+  assert( cursorHoldsMutex(pCur) );
+  assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
   rc = moveToRoot(pCur);
-  if( rc ) return rc;
-  if( CURSOR_INVALID==pCur->eState ){
-    assert( pCur->pPage->nCell==0 );
-    *pRes = 1;
-    return SQLITE_OK;
-  }
-  assert( pCur->eState==CURSOR_VALID );
-  *pRes = 0;
-  rc = moveToRightmost(pCur);
+  if( rc==SQLITE_OK ){
+    if( CURSOR_INVALID==pCur->eState ){
+      assert( pCur->pPage->nCell==0 );
+      *pRes = 1;
+    }else{
+      assert( pCur->eState==CURSOR_VALID );
+      *pRes = 0;
+      rc = moveToRightmost(pCur);
+    }
+  }
   return rc;
 }
 
 /* Move the cursor so that it points to an entry near pKey/nKey.
@@ -26043,8 +28485,9 @@
 **                  exactly matches pKey.
 **
 **     *pRes>0      The cursor is left pointing at an entry that
 **                  is larger than pKey.
+**
 */
 SQLITE_PRIVATE int sqlite3BtreeMoveto(
   BtCursor *pCur,        /* The cursor to be moved */
   const void *pKey,      /* The key content for indices.  Not used by tables */
@@ -26052,10 +28495,15 @@
   int biasRight,         /* If true, bias the search to the high end */
   int *pRes              /* Search result flag */
 ){
   int rc;
+
+  assert( cursorHoldsMutex(pCur) );
+  assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
   rc = moveToRoot(pCur);
-  if( rc ) return rc;
+  if( rc ){
+    return rc;
+  }
   assert( pCur->pPage );
   assert( pCur->pPage->isInit );
   if( pCur->eState==CURSOR_INVALID ){
     *pRes = -1;
@@ -26102,14 +28550,16 @@
         nCellKey = pCur->info.nKey;
         if( available>=nCellKey ){
           c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
         }else{
-          pCellKey = sqliteMallocRaw( nCellKey );
+          pCellKey = sqlite3_malloc( nCellKey );
           if( pCellKey==0 ) return SQLITE_NOMEM;
           rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
           c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
-          sqliteFree(pCellKey);
-          if( rc ) return rc;
+          sqlite3_free(pCellKey);
+          if( rc ){
+            return rc;
+          }
         }
       }
       if( c==0 ){
         if( pPage->leafData && !pPage->leaf ){
@@ -26154,8 +28604,9 @@
   }
   /* NOT REACHED */
 }
 
+
 /*
 ** Return TRUE if the cursor is not pointing at an entry of the table.
 **
 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
@@ -26170,17 +28621,26 @@
   return (CURSOR_VALID!=pCur->eState);
 }
 
 /*
+** Return the database connection handle for a cursor.
+*/
+SQLITE_PRIVATE sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
+  assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
+  return pCur->pBtree->pSqlite;
+}
+
+/*
 ** Advance the cursor to the next entry in the database.  If
 ** successful then set *pRes=0.  If the cursor
 ** was already pointing to the last entry in the database before
 ** this routine was called, then set *pRes=1.
 */
-SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
+static int btreeNext(BtCursor *pCur, int *pRes){
   int rc;
   MemPage *pPage;
 
+  assert( cursorHoldsMutex(pCur) );
   rc = restoreOrClearCursorPosition(pCur);
   if( rc!=SQLITE_OK ){
     return rc;
   }
@@ -26233,20 +28693,28 @@
   }
   rc = moveToLeftmost(pCur);
   return rc;
 }
+SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
+  int rc;
+  assert( cursorHoldsMutex(pCur) );
+  rc = btreeNext(pCur, pRes);
+  return rc;
+}
+
 
 /*
 ** Step the cursor to the back to the previous entry in the database.  If
 ** successful then set *pRes=0.  If the cursor
 ** was already pointing to the first entry in the database before
 ** this routine was called, then set *pRes=1.
 */
-SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
+static int btreePrevious(BtCursor *pCur, int *pRes){
   int rc;
   Pgno pgno;
   MemPage *pPage;
 
+  assert( cursorHoldsMutex(pCur) );
   rc = restoreOrClearCursorPosition(pCur);
   if( rc!=SQLITE_OK ){
     return rc;
   }
@@ -26266,9 +28734,11 @@
   assert( pCur->idx>=0 );
   if( !pPage->leaf ){
     pgno = get4byte( findCell(pPage, pCur->idx) );
     rc = moveToChild(pCur, pgno);
-    if( rc ) return rc;
+    if( rc ){
+      return rc;
+    }
     rc = moveToRightmost(pCur);
   }else{
     while( pCur->idx==0 ){
       if( sqlite3BtreeIsRootPage(pPage) ){
@@ -26287,8 +28757,14 @@
       rc = SQLITE_OK;
     }
   }
   *pRes = 0;
+  return rc;
+}
+SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
+  int rc;
+  assert( cursorHoldsMutex(pCur) );
+  rc = btreePrevious(pCur, pRes);
   return rc;
 }
 
 /*
@@ -26325,8 +28801,9 @@
   int k;     /* Number of leaves on the trunk of the freelist */
   MemPage *pTrunk = 0;
   MemPage *pPrevTrunk = 0;
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   pPage1 = pBt->pPage1;
   n = get4byte(&pPage1->aData[36]);
   if( n>0 ){
     /* There are pages on the freelist.  Reuse one of those pages. */
@@ -26557,8 +29034,9 @@
   MemPage *pPage1 = pBt->pPage1;
   int rc, n, k;
 
   /* Prepare the page for freeing */
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   assert( pPage->pgno>1 );
   pPage->isInit = 0;
   releasePage(pPage->pParent);
   pPage->pParent = 0;
@@ -26605,14 +29083,17 @@
     if( k>=pBt->usableSize/4 - 8 ){
       /* The trunk is full.  Turn the page being freed into a new
       ** trunk page with no leaves. */
       rc = sqlite3PagerWrite(pPage->pDbPage);
-      if( rc ) return rc;
-      put4byte(pPage->aData, pTrunk->pgno);
-      put4byte(&pPage->aData[4], 0);
-      put4byte(&pPage1->aData[32], pPage->pgno);
-      TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
-              pPage->pgno, pTrunk->pgno));
+      if( rc==SQLITE_OK ){
+        put4byte(pPage->aData, pTrunk->pgno);
+        put4byte(&pPage->aData[4], 0);
+        put4byte(&pPage1->aData[32], pPage->pgno);
+        TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
+                pPage->pgno, pTrunk->pgno));
+      }
+    }else if( k<0 ){
+      rc = SQLITE_CORRUPT;
     }else{
       /* Add the newly freed page as a leaf on the current trunk */
       rc = sqlite3PagerWrite(pTrunk->pDbPage);
       if( rc==SQLITE_OK ){
@@ -26639,8 +29120,9 @@
   int rc;
   int nOvfl;
   int ovflPageSize;
 
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
   if( info.iOverflow==0 ){
     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
   }
@@ -26694,8 +29176,10 @@
   BtShared *pBt = pPage->pBt;
   Pgno pgnoOvfl = 0;
   int nHeader;
   CellInfo info;
+
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 
   /* Fill in the header. */
   nHeader = 0;
   if( !pPage->leaf ){
@@ -26807,16 +29291,17 @@
 static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
   MemPage *pThis;
   DbPage *pDbPage;
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   assert( pNewParent!=0 );
   if( pgno==0 ) return SQLITE_OK;
   assert( pBt->pPager!=0 );
   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
   if( pDbPage ){
     pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
     if( pThis->isInit ){
-      assert( pThis->aData==(sqlite3PagerGetData(pDbPage)) );
+      assert( pThis->aData==sqlite3PagerGetData(pDbPage) );
       if( pThis->pParent!=pNewParent ){
         if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
         pThis->pParent = pNewParent;
         sqlite3PagerRef(pNewParent->pDbPage);
@@ -26850,8 +29335,9 @@
   int i;
   BtShared *pBt = pPage->pBt;
   int rc = SQLITE_OK;
 
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   if( pPage->leaf ) return SQLITE_OK;
 
   for(i=0; i<pPage->nCell; i++){
     u8 *pCell = findCell(pPage, i);
@@ -26884,8 +29370,9 @@
 
   assert( idx>=0 && idx<pPage->nCell );
   assert( sz==cellSize(pPage, idx) );
   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   data = pPage->aData;
   ptr = &data[pPage->cellOffset + 2*idx];
   pc = get2byte(ptr);
   assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
@@ -26936,9 +29423,9 @@
   u8 *ptr;          /* Used for moving information around in data[] */
 
   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
   assert( sz==cellSizePtr(pPage, pCell) );
-  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   if( pPage->nOverflow || sz+2>pPage->nFree ){
     if( pTemp ){
       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
       pCell = pTemp;
@@ -26948,16 +29435,21 @@
     pPage->aOvfl[j].pCell = pCell;
     pPage->aOvfl[j].idx = i;
     pPage->nFree = 0;
   }else{
+    int rc = sqlite3PagerWrite(pPage->pDbPage);
+    if( rc!=SQLITE_OK ){
+      return rc;
+    }
+    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
     data = pPage->aData;
     hdr = pPage->hdrOffset;
     top = get2byte(&data[hdr+5]);
     cellOffset = pPage->cellOffset;
     end = cellOffset + 2*pPage->nCell + 2;
     ins = cellOffset + 2*i;
     if( end > top - sz ){
-      int rc = defragmentPage(pPage);
+      rc = defragmentPage(pPage);
       if( rc!=SQLITE_OK ) return rc;
       top = get2byte(&data[hdr+5]);
       assert( end + sz <= top );
     }
@@ -26983,9 +29475,9 @@
       sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
       if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
         Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
-        int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
+        rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
         if( rc!=SQLITE_OK ) return rc;
       }
     }
 #endif
@@ -27011,8 +29503,9 @@
   int cellbody;     /* Address of next cell body */
   u8 *data;         /* Data for the page */
 
   assert( pPage->nOverflow==0 );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   totalSize = 0;
   for(i=0; i<nCell; i++){
     totalSize += aSize[i];
   }
@@ -27084,8 +29577,10 @@
   BtShared *pBt = pPage->pBt;
   int parentIdx = pParent->nCell;   /* pParent new divider cell index */
   int parentSize;                   /* Size of new divider cell */
   u8 parentCell[64];                /* Space for the new divider cell */
+
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 
   /* Allocate a new page. Insert the overflow cell from pPage
   ** into it. Then remove the overflow cell from pPage.
   */
@@ -27209,13 +29704,15 @@
 #ifndef SQLITE_OMIT_AUTOVACUUM
   u8 *aFrom = 0;
 #endif
 
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
+
   /*
   ** Find the parent page.
   */
   assert( pPage->isInit );
-  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
+  assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
   pBt = pPage->pBt;
   pParent = pPage->pParent;
   assert( pParent );
   if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
@@ -27246,8 +29743,12 @@
     */
     return balance_quick(pPage, pParent);
   }
 #endif
+
+  if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
+    return rc;
+  }
 
   /*
   ** Find the cell in the parent page whose left child points back
   ** to pPage.  The "idx" variable is the index of that cell.  If pPage
@@ -27316,9 +29817,9 @@
 
   /*
   ** Allocate space for memory structures
   */
-  apCell = sqliteMallocRaw(
+  apCell = sqlite3_malloc(
        nMaxCells*sizeof(u8*)                           /* apCell */
      + nMaxCells*sizeof(int)                           /* szCell */
      + ROUND8(sizeof(MemPage))*NB                      /* aCopy */
      + pBt->pageSize*(5+NB)                            /* aSpace */
@@ -27349,14 +29850,12 @@
   ** that the original pages since the original pages will be in the
   ** process of being overwritten.
   */
   for(i=0; i<nOld; i++){
-    MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize];
-    p->aData = &((u8*)p)[-pBt->pageSize];
-    memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage));
-    /* The memcpy() above changes the value of p->aData so we have to
-    ** set it again. */
-    p->aData = &((u8*)p)[-pBt->pageSize];
+    MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
+    memcpy(p, apOld[i], sizeof(MemPage));
+    p->aData = (void*)&p[1];
+    memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
   }
 
   /*
   ** Load pointers to all cells on sibling pages and the divider cells
@@ -27637,9 +30136,9 @@
       if( !pNew->leaf ){
         memcpy(&pNew->aData[8], pCell, 4);
         pTemp = 0;
       }else if( leafData ){
-	/* If the tree is a leaf-data tree, and the siblings are leaves,
+        /* If the tree is a leaf-data tree, and the siblings are leaves,
         ** then there is no divider cell in apCell[]. Instead, the divider
         ** cell consists of the integer key for the right-most cell of
         ** the sibling-page assembled above only.
         */
@@ -27727,9 +30226,9 @@
   /*
   ** Cleanup before returning.
   */
 balance_cleanup:
-  sqliteFree(apCell);
+  sqlite3_free(apCell);
   for(i=0; i<nOld; i++){
     releasePage(apOld[i]);
   }
   for(i=0; i<nNew; i++){
@@ -27756,11 +30255,12 @@
   int *szCell;                 /* Local size of all cells */
 
   assert( pPage->pParent==0 );
   assert( pPage->nCell==0 );
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   pBt = pPage->pBt;
   mxCellPerPage = MX_CELL(pBt);
-  apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
+  apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
   if( apCell==0 ) return SQLITE_NOMEM;
   szCell = (int*)&apCell[mxCellPerPage];
   if( pPage->leaf ){
     /* The table is completely empty */
@@ -27829,13 +30329,12 @@
         }
       }
     }
 #endif
-    if( rc!=SQLITE_OK ) goto end_shallow_balance;
     releasePage(pChild);
   }
 end_shallow_balance:
-  sqliteFree(apCell);
+  sqlite3_free(apCell);
   return rc;
 }
 
 
@@ -27861,8 +30360,9 @@
 
   assert( pPage->pParent==0 );
   assert( pPage->nOverflow>0 );
   pBt = pPage->pBt;
+  assert( sqlite3_mutex_held(pBt->mutex) );
   rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
   if( rc ) return rc;
   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
   usableSize = pBt->usableSize;
@@ -27909,10 +30409,12 @@
 ** required, call the appropriate balancing routine.
 */
 static int balance(MemPage *pPage, int insert){
   int rc = SQLITE_OK;
+  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   if( pPage->pParent==0 ){
-    if( pPage->nOverflow>0 ){
+    rc = sqlite3PagerWrite(pPage->pDbPage);
+    if( rc==SQLITE_OK && pPage->nOverflow>0 ){
       rc = balance_deeper(pPage);
     }
     if( rc==SQLITE_OK && pPage->nCell==0 ){
       rc = balance_shallower(pPage);
@@ -27945,8 +30447,9 @@
 static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
   BtCursor *p;
   BtShared *pBt = pBtree->pBt;
   sqlite3 *db = pBtree->pSqlite;
+  assert( sqlite3BtreeHoldsMutex(pBtree) );
   for(p=pBt->pCursor; p; p=p->pNext){
     if( p==pExclude ) continue;
     if( p->eState!=CURSOR_VALID ) continue;
     if( p->pgnoRoot!=pgnoRoot ) continue;
@@ -27982,22 +30485,28 @@
   int rc;
   int loc;
   int szNew;
   MemPage *pPage;
-  BtShared *pBt = pCur->pBtree->pBt;
+  Btree *p = pCur->pBtree;
+  BtShared *pBt = p->pBt;
   unsigned char *oldCell;
   unsigned char *newCell = 0;
 
+  assert( cursorHoldsMutex(pCur) );
   if( pBt->inTransaction!=TRANS_WRITE ){
     /* Must start a transaction before doing an insert */
-    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+    rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+    return rc;
   }
   assert( !pBt->readOnly );
   if( !pCur->wrFlag ){
     return SQLITE_PERM;   /* Cursor not open for writing */
   }
   if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
+  }
+  if( pCur->eState==CURSOR_FAULT ){
+    return pCur->skip;
   }
 
   /* Save the positions of any other cursors open on this table */
   clearCursorPosition(pCur);
@@ -28014,11 +30523,9 @@
   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
           pCur->pgnoRoot, nKey, nData, pPage->pgno,
           loc==0 ? "overwrite" : "new entry"));
   assert( pPage->isInit );
-  rc = sqlite3PagerWrite(pPage->pDbPage);
-  if( rc ) return rc;
-  newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
+  newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) );
   if( newCell==0 ) return SQLITE_NOMEM;
   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
   if( rc ) goto end_insert;
   assert( szNew==cellSizePtr(pPage, newCell) );
@@ -28025,8 +30532,12 @@
   assert( szNew<=MX_CELL_SIZE(pBt) );
   if( loc==0 && CURSOR_VALID==pCur->eState ){
     int szOld;
     assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
+    rc = sqlite3PagerWrite(pPage->pDbPage);
+    if( rc ){
+      goto end_insert;
+    }
     oldCell = findCell(pPage, pCur->idx);
     if( !pPage->leaf ){
       memcpy(newCell, oldCell, 4);
     }
@@ -28049,9 +30560,9 @@
   if( rc==SQLITE_OK ){
     moveToRoot(pCur);
   }
 end_insert:
-  sqliteFree(newCell);
+  sqlite3_free(newCell);
   return rc;
 }
 
 /*
@@ -28062,16 +30573,22 @@
   MemPage *pPage = pCur->pPage;
   unsigned char *pCell;
   int rc;
   Pgno pgnoChild = 0;
-  BtShared *pBt = pCur->pBtree->pBt;
-
+  Btree *p = pCur->pBtree;
+  BtShared *pBt = p->pBt;
+
+  assert( cursorHoldsMutex(pCur) );
   assert( pPage->isInit );
   if( pBt->inTransaction!=TRANS_WRITE ){
     /* Must start a transaction before doing a delete */
-    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+    rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+    return rc;
   }
   assert( !pBt->readOnly );
+  if( pCur->eState==CURSOR_FAULT ){
+    return pCur->skip;
+  }
   if( pCur->idx >= pPage->nCell ){
     return SQLITE_ERROR;  /* The cursor is not pointing to anything */
   }
   if( !pCur->wrFlag ){
@@ -28102,9 +30619,11 @@
   if( !pPage->leaf ){
     pgnoChild = get4byte(pCell);
   }
   rc = clearCell(pPage, pCell);
-  if( rc ) return rc;
+  if( rc ){
+    return rc;
+  }
 
   if( !pPage->leaf ){
     /*
     ** The entry we are about to delete is not a leaf so if we do not
@@ -28132,9 +30651,9 @@
       dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
       pNext = findCell(leafCur.pPage, leafCur.idx);
       szNext = cellSizePtr(leafCur.pPage, pNext);
       assert( MX_CELL_SIZE(pBt)>=szNext+4 );
-      tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
+      tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) );
       if( tempCell==0 ){
         rc = SQLITE_NOMEM;
       }
     }
@@ -28148,9 +30667,9 @@
     if( rc==SQLITE_OK ){
       dropCell(leafCur.pPage, leafCur.idx, szNext);
       rc = balance(leafCur.pPage, 0);
     }
-    sqliteFree(tempCell);
+    sqlite3_free(tempCell);
     sqlite3BtreeReleaseTempCursor(&leafCur);
   }else{
     TRACE(("DELETE: table=%d delete from leaf %d\n",
        pCur->pgnoRoot, pPage->pgno));
@@ -28173,22 +30692,27 @@
 **
 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
 **     BTREE_ZERODATA                  Used for SQL indices
 */
-SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
+static int btreeCreateTable(Btree *p, int *piTable, int flags){
   BtShared *pBt = p->pBt;
   MemPage *pRoot;
   Pgno pgnoRoot;
   int rc;
+
+  assert( sqlite3BtreeHoldsMutex(p) );
   if( pBt->inTransaction!=TRANS_WRITE ){
     /* Must start a transaction first */
-    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+    rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+    return rc;
   }
   assert( !pBt->readOnly );
 
 #ifdef SQLITE_OMIT_AUTOVACUUM
   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
-  if( rc ) return rc;
+  if( rc ){
+    return rc;
+  }
 #else
   if( pBt->autoVacuum ){
     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
     MemPage *pPageMove; /* The page to move to. */
@@ -28204,9 +30728,11 @@
     ** root page of the new table should go. meta[3] is the largest root-page
     ** created so far, so the new root-page is (meta[3]+1).
     */
     rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
-    if( rc!=SQLITE_OK ) return rc;
+    if( rc!=SQLITE_OK ){
+      return rc;
+    }
     pgnoRoot++;
 
     /* The new root-page may not be allocated on a pointer-map page, or the
     ** PENDING_BYTE page.
@@ -28297,8 +30823,15 @@
   sqlite3PagerUnref(pRoot->pDbPage);
   *piTable = (int)pgnoRoot;
   return SQLITE_OK;
 }
+SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
+  int rc;
+  sqlite3BtreeEnter(p);
+  rc = btreeCreateTable(p, piTable, flags);
+  sqlite3BtreeLeave(p);
+  return rc;
+}
 
 /*
 ** Erase the given database page and all its children.  Return
 ** the page to the freelist.
@@ -28313,8 +30846,9 @@
   int rc;
   unsigned char *pCell;
   int i;
 
+  assert( sqlite3_mutex_held(pBt->mutex) );
   if( pgno>sqlite3PagerPagecount(pBt->pPager) ){
     return SQLITE_CORRUPT_BKPT;
   }
 
@@ -28355,22 +30889,20 @@
 */
 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable){
   int rc;
   BtShared *pBt = p->pBt;
+  sqlite3BtreeEnter(p);
   if( p->inTrans!=TRANS_WRITE ){
-    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
-  }
-  rc = checkReadLocks(p, iTable, 0);
-  if( rc ){
-    return rc;
-  }
-
-  /* Save the position of all cursors open on this table */
-  if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
-    return rc;
-  }
-
-  return clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
+    rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+  }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){
+    /* nothing to do */
+  }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
+    /* nothing to do */
+  }else{
+    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
+  }
+  sqlite3BtreeLeave(p);
+  return rc;
 }
 
 /*
 ** Erase all information in a table and add the root of the table to
@@ -28391,13 +30923,14 @@
 ** the move.  If no page gets moved, *piMoved is set to 0.
 ** The last root page is recorded in meta[3] and the value of
 ** meta[3] is updated by this procedure.
 */
-SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
+static int btreeDropTable(Btree *p, int iTable, int *piMoved){
   int rc;
   MemPage *pPage = 0;
   BtShared *pBt = p->pBt;
 
+  assert( sqlite3BtreeHoldsMutex(p) );
   if( p->inTrans!=TRANS_WRITE ){
     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
   }
 
@@ -28497,8 +31030,15 @@
     releasePage(pPage);
   }
   return rc;
 }
+SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
+  int rc;
+  sqlite3BtreeEnter(p);
+  rc = btreeDropTable(p, iTable, piMoved);
+  sqlite3BtreeLeave(p);
+  return rc;
+}
 
 
 /*
 ** Read the meta-information out of a database file.  Meta[0]
@@ -28515,21 +31055,27 @@
   int rc;
   unsigned char *pP1;
   BtShared *pBt = p->pBt;
 
+  sqlite3BtreeEnter(p);
+
   /* Reading a meta-data value requires a read-lock on page 1 (and hence
   ** the sqlite_master table. We grab this lock regardless of whether or
   ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
   ** 1 is treated as a special case by queryTableLock() and lockTable()).
   */
   rc = queryTableLock(p, 1, READ_LOCK);
   if( rc!=SQLITE_OK ){
+    sqlite3BtreeLeave(p);
     return rc;
   }
 
   assert( idx>=0 && idx<=15 );
   rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
-  if( rc ) return rc;
+  if( rc ){
+    sqlite3BtreeLeave(p);
+    return rc;
+  }
   pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
   *pMeta = get4byte(&pP1[36 + idx*4]);
   sqlite3PagerUnref(pDbPage);
 
@@ -28541,8 +31087,9 @@
 #endif
 
   /* Grab the read-lock on page 1. */
   rc = lockTable(p, 1, READ_LOCK);
+  sqlite3BtreeLeave(p);
   return rc;
 }
 
 /*
@@ -28553,22 +31100,28 @@
   BtShared *pBt = p->pBt;
   unsigned char *pP1;
   int rc;
   assert( idx>=1 && idx<=15 );
+  sqlite3BtreeEnter(p);
   if( p->inTrans!=TRANS_WRITE ){
-    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
-  }
-  assert( pBt->pPage1!=0 );
-  pP1 = pBt->pPage1->aData;
-  rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
-  if( rc ) return rc;
-  put4byte(&pP1[36 + idx*4], iMeta);
-  if( idx==7 ){
-    assert( pBt->autoVacuum || iMeta==0 );
-    assert( iMeta==0 || iMeta==1 );
-    pBt->incrVacuum = iMeta;
-  }
-  return SQLITE_OK;
+    rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
+  }else{
+    assert( pBt->pPage1!=0 );
+    pP1 = pBt->pPage1->aData;
+    rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
+    if( rc==SQLITE_OK ){
+      put4byte(&pP1[36 + idx*4], iMeta);
+#ifndef SQLITE_OMIT_AUTOVACUUM
+      if( idx==7 ){
+        assert( pBt->autoVacuum || iMeta==0 );
+        assert( iMeta==0 || iMeta==1 );
+        pBt->incrVacuum = iMeta;
+      }
+#endif
+    }
+  }
+  sqlite3BtreeLeave(p);
+  return rc;
 }
 
 /*
 ** Return the flag byte at the beginning of the page that the cursor
@@ -28578,8 +31131,10 @@
   /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
   ** restoreOrClearCursorPosition() here.
   */
   MemPage *pPage = pCur->pPage;
+  assert( cursorHoldsMutex(pCur) );
+  assert( pPage->pBt==pCur->pBt );
   return pPage ? pPage->aData[pPage->hdrOffset] : 0;
 }
 
 
@@ -28606,20 +31161,20 @@
   if( !pCheck->mxErr ) return;
   pCheck->mxErr--;
   pCheck->nErr++;
   va_start(ap, zFormat);
-  zMsg2 = sqlite3VMPrintf(zFormat, ap);
+  zMsg2 = sqlite3VMPrintf(0, zFormat, ap);
   va_end(ap);
   if( zMsg1==0 ) zMsg1 = "";
   if( pCheck->zErrMsg ){
     char *zOld = pCheck->zErrMsg;
     pCheck->zErrMsg = 0;
     sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
-    sqliteFree(zOld);
+    sqlite3_free(zOld);
   }else{
     sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
   }
-  sqliteFree(zMsg2);
+  sqlite3_free(zMsg2);
 }
 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 
 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
@@ -28860,9 +31415,9 @@
   /* Check for complete coverage of the page
   */
   data = pPage->aData;
   hdr = pPage->hdrOffset;
-  hit = sqliteMalloc( usableSize );
+  hit = sqlite3MallocZero( usableSize );
   if( hit ){
     memset(hit, 1, get2byte(&data[hdr+5]));
     nCell = get2byte(&data[hdr+3]);
     cellStart = hdr + 12 - 4*pPage->leaf;
@@ -28903,9 +31458,9 @@
           "Fragmented space is %d byte reported as %d on page %d",
           cnt, data[hdr+7], iPage);
     }
   }
-  sqliteFree(hit);
+  sqlite3_free(hit);
 
   releasePage(pPage);
   return depth+1;
 }
@@ -28933,11 +31488,13 @@
   int nRef;
   IntegrityCk sCheck;
   BtShared *pBt = p->pBt;
 
+  sqlite3BtreeEnter(p);
   nRef = sqlite3PagerRefcount(pBt->pPager);
   if( lockBtreeWithRetry(p)!=SQLITE_OK ){
-    return sqliteStrDup("Unable to acquire a read lock on the database");
+    sqlite3BtreeLeave(p);
+    return sqlite3StrDup("Unable to acquire a read lock on the database");
   }
   sCheck.pBt = pBt;
   sCheck.pPager = pBt->pPager;
   sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager);
@@ -28950,15 +31507,17 @@
   }
 #endif
   if( sCheck.nPage==0 ){
     unlockBtreeIfUnused(pBt);
-    return 0;
-  }
-  sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
+    sqlite3BtreeLeave(p);
+    return 0;
+  }
+  sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
   if( !sCheck.anRef ){
     unlockBtreeIfUnused(pBt);
     *pnErr = 1;
-    return sqlite3MPrintf("Unable to malloc %d bytes",
+    sqlite3BtreeLeave(p);
+    return sqlite3MPrintf(p->pSqlite, "Unable to malloc %d bytes",
         (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
   }
   for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
   i = PENDING_BYTE_PAGE(pBt);
@@ -29017,16 +31576,20 @@
   }
 
   /* Clean  up and report errors.
   */
-  sqliteFree(sCheck.anRef);
+  sqlite3BtreeLeave(p);
+  sqlite3_free(sCheck.anRef);
   *pnErr = sCheck.nErr;
   return sCheck.zErrMsg;
 }
 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 
 /*
 ** Return the full pathname of the underlying database file.
+**
+** The pager filename is invariant as long as the pager is
+** open so it is safe to access without the BtShared mutex.
 */
 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
   assert( p->pBt->pPager!=0 );
   return sqlite3PagerFilename(p->pBt->pPager);
@@ -29033,8 +31596,11 @@
 }
 
 /*
 ** Return the pathname of the directory that contains the database file.
+**
+** The pager directory name is invariant as long as the pager is
+** open so it is safe to access without the BtShared mutex.
 */
 SQLITE_PRIVATE const char *sqlite3BtreeGetDirname(Btree *p){
   assert( p->pBt->pPager!=0 );
   return sqlite3PagerDirname(p->pBt->pPager);
@@ -29043,8 +31609,11 @@
 /*
 ** Return the pathname of the journal file for this database. The return
 ** value of this routine is the same regardless of whether the journal file
 ** has been created or not.
+**
+** The pager journal filename is invariant as long as the pager is
+** open so it is safe to access without the BtShared mutex.
 */
 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
   assert( p->pBt->pPager!=0 );
   return sqlite3PagerJournalname(p->pBt->pPager);
@@ -29057,9 +31626,9 @@
 **
 ** The size of file pBtFrom may be reduced by this operation.
 ** If anything goes wrong, the transaction on pBtFrom is rolled back.
 */
-SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
+static int btreeCopyFile(Btree *pTo, Btree *pFrom){
   int rc = SQLITE_OK;
   Pgno i, nPage, nToPage, iSkip;
 
   BtShared *pBtTo = pTo->pBt;
@@ -29109,28 +31678,41 @@
     sqlite3BtreeRollback(pTo);
   }
   return rc;
 }
+SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
+  int rc;
+  sqlite3BtreeEnter(pTo);
+  sqlite3BtreeEnter(pFrom);
+  rc = btreeCopyFile(pTo, pFrom);
+  sqlite3BtreeLeave(pFrom);
+  sqlite3BtreeLeave(pTo);
+  return rc;
+}
+
 #endif /* SQLITE_OMIT_VACUUM */
 
 /*
 ** Return non-zero if a transaction is active.
 */
 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
+  assert( p==0 || sqlite3_mutex_held(p->pSqlite->mutex) );
   return (p && (p->inTrans==TRANS_WRITE));
 }
 
 /*
 ** Return non-zero if a statement transaction is active.
 */
 SQLITE_PRIVATE int sqlite3BtreeIsInStmt(Btree *p){
+  assert( sqlite3BtreeHoldsMutex(p) );
   return (p->pBt && p->pBt->inStmt);
 }
 
 /*
 ** Return non-zero if a read (or write) transaction is active.
 */
 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
   return (p && (p->inTrans!=TRANS_NONE));
 }
 
 /*
@@ -29145,17 +31727,19 @@
 ** of memory returned.
 **
 ** Just before the shared-btree is closed, the function passed as the
 ** xFree argument when the memory allocation was made is invoked on the
-** blob of allocated memory. This function should not call sqliteFree()
+** blob of allocated memory. This function should not call sqlite3_free()
 ** on the memory, the btree layer does that.
 */
 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
   BtShared *pBt = p->pBt;
+  sqlite3BtreeEnter(p);
   if( !pBt->pSchema ){
-    pBt->pSchema = sqliteMalloc(nBytes);
+    pBt->pSchema = sqlite3MallocZero(nBytes);
     pBt->xFreeSchema = xFree;
   }
+  sqlite3BtreeLeave(p);
   return pBt->pSchema;
 }
 
 /*
@@ -29162,9 +31746,14 @@
 ** Return true if another user of the same shared btree as the argument
 ** handle holds an exclusive lock on the sqlite_master table.
 */
 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
-  return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
+  int rc;
+  assert( sqlite3_mutex_held(p->pSqlite->mutex) );
+  sqlite3BtreeEnter(p);
+  rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
+  sqlite3BtreeLeave(p);
+  return rc;
 }
 
 
 #ifndef SQLITE_OMIT_SHARED_CACHE
@@ -29175,12 +31764,14 @@
 */
 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
   int rc = SQLITE_OK;
   u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK);
+  sqlite3BtreeEnter(p);
   rc = queryTableLock(p, iTab, lockType);
   if( rc==SQLITE_OK ){
     rc = lockTable(p, iTab, lockType);
   }
+  sqlite3BtreeLeave(p);
   return rc;
 }
 #endif
 
@@ -29192,12 +31783,17 @@
 ** Only the data content may only be modified, it is not possible
 ** to change the length of the data stored.
 */
 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
-
+  assert( cursorHoldsMutex(pCsr) );
+  assert( sqlite3_mutex_held(pCsr->pBtree->pSqlite->mutex) );
   assert(pCsr->isIncrblobHandle);
-  if( pCsr->eState==CURSOR_REQUIRESEEK ){
-    return SQLITE_ABORT;
+  if( pCsr->eState>=CURSOR_REQUIRESEEK ){
+    if( pCsr->eState==CURSOR_FAULT ){
+      return pCsr->skip;
+    }else{
+      return SQLITE_ABORT;
+    }
   }
 
   /* Check some preconditions:
   **   (a) the cursor is open for writing,
@@ -29206,10 +31802,10 @@
   */
   if( !pCsr->wrFlag ){
     return SQLITE_READONLY;
   }
-  assert( !pCsr->pBtree->pBt->readOnly
-          && pCsr->pBtree->pBt->inTransaction==TRANS_WRITE );
+  assert( !pCsr->pBt->readOnly
+          && pCsr->pBt->inTransaction==TRANS_WRITE );
   if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){
     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
   }
   if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){
@@ -29229,8 +31825,10 @@
 ** accessPayload() (the worker function for sqlite3BtreeData() and
 ** sqlite3BtreePutData()).
 */
 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
+  assert( cursorHoldsMutex(pCur) );
+  assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
   assert(!pCur->isIncrblobHandle);
   assert(!pCur->aOverflow);
   pCur->isIncrblobHandle = 1;
 }
@@ -29261,9 +31859,9 @@
   FifoPage *pPage;
   if( nEntry>32767 ){
     nEntry = 32767;
   }
-  pPage = sqliteMallocRaw( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) );
+  pPage = sqlite3_malloc( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) );
   if( pPage ){
     pPage->nSlot = nEntry;
     pPage->iWrite = 0;
     pPage->iRead = 0;
@@ -29324,9 +31922,9 @@
   *pVal = pPage->aSlot[pPage->iRead++];
   pFifo->nEntry--;
   if( pPage->iRead>=pPage->iWrite ){
     pFifo->pFirst = pPage->pNext;
-    sqliteFree(pPage);
+    sqlite3_free(pPage);
     if( pFifo->nEntry==0 ){
       assert( pFifo->pLast==pPage );
       pFifo->pLast = 0;
     }else{
@@ -29345,9 +31943,9 @@
 SQLITE_PRIVATE void sqlite3VdbeFifoClear(Fifo *pFifo){
   FifoPage *pPage, *pNextPage;
   for(pPage=pFifo->pFirst; pPage; pPage=pNextPage){
     pNextPage = pPage->pNext;
-    sqliteFree(pPage);
+    sqlite3_free(pPage);
   }
   sqlite3VdbeFifoInit(pFifo);
 }
 
@@ -29394,12 +31992,12 @@
   int rc;
   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
     return SQLITE_OK;
   }
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
 #ifdef SQLITE_OMIT_UTF16
   return SQLITE_ERROR;
 #else
-
 
   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
   ** then the encoding of the value may not have changed.
   */
@@ -29418,16 +32016,17 @@
 */
 SQLITE_PRIVATE int sqlite3VdbeMemDynamicify(Mem *pMem){
   int n;
   u8 *z;
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   expandBlob(pMem);
   if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
     return SQLITE_OK;
   }
   assert( (pMem->flags & MEM_Dyn)==0 );
   n = pMem->n;
   assert( pMem->flags & (MEM_Str|MEM_Blob) );
-  z = sqliteMallocRaw( n+2 );
+  z = sqlite3DbMallocRaw(pMem->db, n+2 );
   if( z==0 ){
     return SQLITE_NOMEM;
   }
   pMem->flags |= MEM_Dyn|MEM_Term;
@@ -29451,9 +32050,10 @@
     int nByte;
     assert( (pMem->flags & MEM_Blob)!=0 );
     nByte = pMem->n + pMem->u.i;
     if( nByte<=0 ) nByte = 1;
-    pNew = sqliteMalloc(nByte);
+    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
+    pNew = sqlite3DbMallocRaw(pMem->db, nByte);
     if( pNew==0 ){
       return SQLITE_NOMEM;
     }
     memcpy(pNew, pMem->z, pMem->n);
@@ -29478,8 +32078,9 @@
 */
 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
   int n;
   u8 *z;
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   expandBlob(pMem);
   if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
     return SQLITE_OK;
   }
@@ -29488,9 +32089,9 @@
   if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
     z = (u8*)pMem->zShort;
     pMem->flags |= MEM_Short|MEM_Term;
   }else{
-    z = sqliteMallocRaw( n+2 );
+    z = sqlite3DbMallocRaw(pMem->db, n+2 );
     if( z==0 ){
       return SQLITE_NOMEM;
     }
     pMem->flags |= MEM_Dyn|MEM_Term;
@@ -29508,8 +32109,9 @@
 /*
 ** Make sure the given Mem is \u0000 terminated.
 */
 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
     return SQLITE_OK;   /* Nothing to do */
   }
   if( pMem->flags & (MEM_Static|MEM_Ephem) ){
@@ -29516,18 +32118,19 @@
     return sqlite3VdbeMemMakeWriteable(pMem);
   }else{
     char *z;
     sqlite3VdbeMemExpandBlob(pMem);
-    z = sqliteMalloc(pMem->n+2);
-
-    if( !z ) return SQLITE_NOMEM;
+    z = sqlite3DbMallocRaw(pMem->db, pMem->n+2);
+    if( !z ){
+       return SQLITE_NOMEM;
+    }
     memcpy(z, pMem->z, pMem->n);
     z[pMem->n] = 0;
     z[pMem->n+1] = 0;
     if( pMem->xDel ){
       pMem->xDel(pMem->z);
     }else{
-      sqliteFree(pMem->z);
+      sqlite3_free(pMem->z);
     }
     pMem->xDel = 0;
     pMem->z = z;
     pMem->flags |= MEM_Term;
@@ -29552,8 +32155,9 @@
   int rc = SQLITE_OK;
   int fg = pMem->flags;
   char *z = pMem->zShort;
 
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   assert( !(fg&MEM_Zero) );
   assert( !(fg&(MEM_Str|MEM_Blob)) );
   assert( fg&(MEM_Int|MEM_Real) );
 
@@ -29589,24 +32193,24 @@
   int rc = SQLITE_OK;
   if( pFunc && pFunc->xFinalize ){
     sqlite3_context ctx;
     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
+    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
     ctx.s.flags = MEM_Null;
     ctx.s.z = pMem->zShort;
+    ctx.s.db = pMem->db;
     ctx.pMem = pMem;
     ctx.pFunc = pFunc;
     ctx.isError = 0;
     pFunc->xFinalize(&ctx);
     if( pMem->z && pMem->z!=pMem->zShort ){
-      sqliteFree( pMem->z );
+      sqlite3_free( pMem->z );
     }
     *pMem = ctx.s;
     if( pMem->flags & MEM_Short ){
       pMem->z = pMem->zShort;
     }
-    if( ctx.isError ){
-      rc = SQLITE_ERROR;
-    }
+    rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
   }
   return rc;
 }
 
@@ -29615,8 +32219,9 @@
 ** inconsistent state, for example with (Mem.z==0) and
 ** (Mem.type==SQLITE_TEXT).
 */
 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
+  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
   if( p->flags & (MEM_Dyn|MEM_Agg) ){
     if( p->xDel ){
       if( p->flags & MEM_Agg ){
         sqlite3VdbeMemFinalize(p, p->u.pDef);
@@ -29625,9 +32230,9 @@
       }else{
         p->xDel((void *)p->z);
       }
     }else{
-      sqliteFree(p->z);
+      sqlite3_free(p->z);
     }
     p->z = 0;
     p->xDel = 0;
   }
@@ -29643,9 +32248,11 @@
 **
 ** If pMem is a string, its encoding might be changed.
 */
 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
-  int flags = pMem->flags;
+  int flags;
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
+  flags = pMem->flags;
   if( flags & MEM_Int ){
     return pMem->u.i;
   }else if( flags & MEM_Real ){
     return (i64)pMem->r;
@@ -29670,8 +32277,9 @@
 ** value.  If it is a string or blob, try to convert it to a double.
 ** If it is a NULL, return 0.0.
 */
 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   if( pMem->flags & MEM_Real ){
     return pMem->r;
   }else if( pMem->flags & MEM_Int ){
     return (double)pMem->u.i;
@@ -29695,8 +32303,9 @@
 ** MEM_Int if we can.
 */
 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
   assert( pMem->flags & MEM_Real );
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   pMem->u.i = pMem->r;
   if( ((double)pMem->u.i)==pMem->r ){
     pMem->flags |= MEM_Int;
   }
@@ -29705,8 +32314,9 @@
 /*
 ** Convert pMem to type integer.  Invalidate any prior representations.
 */
 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   pMem->u.i = sqlite3VdbeIntValue(pMem);
   sqlite3VdbeMemRelease(pMem);
   pMem->flags = MEM_Int;
   return SQLITE_OK;
@@ -29716,8 +32326,9 @@
 ** Convert pMem so that it is of type MEM_Real.
 ** Invalidate any prior representations.
 */
 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   pMem->r = sqlite3VdbeRealValue(pMem);
   sqlite3VdbeMemRelease(pMem);
   pMem->flags = MEM_Real;
   return SQLITE_OK;
@@ -29731,8 +32342,9 @@
   double r1, r2;
   i64 i;
   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
   assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   r1 = sqlite3VdbeRealValue(pMem);
   i = (i64)r1;
   r2 = (double)i;
   if( r1==r2 ){
@@ -29854,8 +32466,11 @@
 ** to allocate enough space to make a copy.
 */
 SQLITE_PRIVATE int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
   int rc;
+  assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
+  assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
+  assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
   if( pTo->flags & MEM_Dyn ){
     sqlite3VdbeMemRelease(pTo);
   }
   memcpy(pTo, pFrom, sizeof(Mem));
@@ -29881,15 +32496,15 @@
   int n,              /* Bytes in string, or negative */
   u8 enc,             /* Encoding of z.  0 for BLOBs */
   void (*xDel)(void*) /* Destructor function */
 ){
+  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   sqlite3VdbeMemRelease(pMem);
   if( !z ){
     pMem->flags = MEM_Null;
     pMem->type = SQLITE_NULL;
     return SQLITE_OK;
   }
-
   pMem->z = (char *)z;
   if( xDel==SQLITE_STATIC ){
     pMem->flags = MEM_Static;
   }else if( xDel==SQLITE_TRANSIENT ){
@@ -30080,24 +32695,28 @@
   Mem *pMem         /* OUT: Return data in this Mem structure. */
 ){
   char *zData;       /* Data from the btree layer */
   int available = 0; /* Number of bytes available on the local btree page */
-
+  sqlite3 *db;       /* Database connection */
+
+  db = sqlite3BtreeCursorDb(pCur);
+  assert( sqlite3_mutex_held(db->mutex) );
   if( key ){
     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
   }else{
     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
   }
   assert( zData!=0 );
 
+  pMem->db = db;
   pMem->n = amt;
   if( offset+amt<=available ){
     pMem->z = &zData[offset];
     pMem->flags = MEM_Blob|MEM_Ephem;
   }else{
     int rc;
     if( amt>NBFS-2 ){
-      zData = (char *)sqliteMallocRaw(amt+2);
+      zData = (char *)sqlite3DbMallocRaw(db, amt+2);
       if( !zData ){
         return SQLITE_NOMEM;
       }
       pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
@@ -30120,9 +32739,9 @@
     if( rc!=SQLITE_OK ){
       if( amt>NBFS-2 ){
         assert( zData!=pMem->zShort );
         assert( pMem->flags & MEM_Dyn );
-        sqliteFree(zData);
+        sqlite3_free(zData);
       } else {
         assert( zData==pMem->zShort );
         assert( pMem->flags & MEM_Short );
       }
@@ -30193,8 +32812,10 @@
 ** boundary.
 */
 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
   if( !pVal ) return 0;
+
+  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
 
   if( pVal->flags&MEM_Null ){
     return 0;
@@ -30215,9 +32836,10 @@
     assert( (pVal->flags&MEM_Blob)==0 );
     sqlite3VdbeMemStringify(pVal, enc);
     assert( 0==(1&(int)pVal->z) );
   }
-  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || sqlite3MallocFailed() );
+  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
+              || pVal->db->mallocFailed );
   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
     return pVal->z;
   }else{
     return 0;
@@ -30226,13 +32848,14 @@
 
 /*
 ** Create a new sqlite3_value object.
 */
-SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(void){
-  Mem *p = sqliteMalloc(sizeof(*p));
+SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
+  Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
   if( p ){
     p->flags = MEM_Null;
     p->type = SQLITE_NULL;
+    p->db = db;
   }
   return p;
 }
 
@@ -30246,12 +32869,13 @@
 ** the value by passing it to sqlite3ValueFree() later on. If the expression
 ** cannot be converted to a value, then *ppVal is set to NULL.
 */
 SQLITE_PRIVATE int sqlite3ValueFromExpr(
-  Expr *pExpr,
-  u8 enc,
-  u8 affinity,
-  sqlite3_value **ppVal
+  sqlite3 *db,              /* The database connection */
+  Expr *pExpr,              /* The expression to evaluate */
+  u8 enc,                   /* Encoding to use */
+  u8 affinity,              /* Affinity to use */
+  sqlite3_value **ppVal     /* Write the new value here */
 ){
   int op;
   char *zVal = 0;
   sqlite3_value *pVal = 0;
@@ -30262,42 +32886,43 @@
   }
   op = pExpr->op;
 
   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
-    zVal = sqliteStrNDup((char*)pExpr->token.z, pExpr->token.n);
-    pVal = sqlite3ValueNew();
+    zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n);
+    pVal = sqlite3ValueNew(db);
     if( !zVal || !pVal ) goto no_mem;
     sqlite3Dequote(zVal);
-    sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX);
+    sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free);
     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
     }else{
       sqlite3ValueApplyAffinity(pVal, affinity, enc);
     }
   }else if( op==TK_UMINUS ) {
-    if( SQLITE_OK==sqlite3ValueFromExpr(pExpr->pLeft, enc, affinity, &pVal) ){
+    if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
       pVal->u.i = -1 * pVal->u.i;
       pVal->r = -1.0 * pVal->r;
     }
   }
 #ifndef SQLITE_OMIT_BLOB_LITERAL
   else if( op==TK_BLOB ){
     int nVal;
-    pVal = sqlite3ValueNew();
-    zVal = sqliteStrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
+    pVal = sqlite3ValueNew(db);
+    zVal = sqlite3StrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
     if( !zVal || !pVal ) goto no_mem;
     sqlite3Dequote(zVal);
     nVal = strlen(zVal)/2;
-    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(zVal), nVal, 0, sqlite3FreeX);
-    sqliteFree(zVal);
+    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal), nVal,0,sqlite3_free);
+    sqlite3_free(zVal);
   }
 #endif
 
   *ppVal = pVal;
   return SQLITE_OK;
 
 no_mem:
-  sqliteFree(zVal);
+  db->mallocFailed = 1;
+  sqlite3_free(zVal);
   sqlite3ValueFree(pVal);
   *ppVal = 0;
   return SQLITE_NOMEM;
 }
@@ -30305,13 +32930,13 @@
 /*
 ** Change the string value of an sqlite3_value object
 */
 SQLITE_PRIVATE void sqlite3ValueSetStr(
-  sqlite3_value *v,
-  int n,
-  const void *z,
-  u8 enc,
-  void (*xDel)(void*)
+  sqlite3_value *v,     /* Value to be set */
+  int n,                /* Length of string z */
+  const void *z,        /* Text of the new string */
+  u8 enc,               /* Encoding to use */
+  void (*xDel)(void*)   /* Destructor for the string */
 ){
   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
 }
 
@@ -30320,9 +32945,9 @@
 */
 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
   if( !v ) return;
   sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
-  sqliteFree(v);
+  sqlite3_free(v);
 }
 
 /*
 ** Return the number of bytes in the sqlite3_value object assuming
@@ -30359,15 +32984,16 @@
 ** But that file was getting too big so this subroutines were split out.
 */
 
 
+
 /*
 ** When debugging the code generator in a symbolic debugger, one can
 ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
 ** as they are added to the instruction stream.
 */
 #ifdef SQLITE_DEBUG
-int sqlite3_vdbe_addop_trace = 0;
+SQLITE_API int sqlite3_vdbe_addop_trace = 0;
 #endif
 
 
 /*
@@ -30374,9 +33000,9 @@
 ** Create a new virtual database engine.
 */
 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
   Vdbe *p;
-  p = sqliteMalloc( sizeof(Vdbe) );
+  p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
   if( p==0 ) return 0;
   p->db = db;
   if( db->pVdbe ){
     db->pVdbe->pPrev = p;
@@ -30393,9 +33019,9 @@
 */
 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
   if( p==0 ) return;
   assert( p->zSql==0 );
-  p->zSql = sqlite3StrNDup(z, n);
+  p->zSql = sqlite3DbStrNDup(p->db, z, n);
 }
 
 /*
 ** Return the SQL associated with a prepared statement
@@ -30454,9 +33080,9 @@
   if( runMode || p->nOpAlloc<N ){
     VdbeOp *pNew;
     int nNew = N + 100*(!runMode);
     int oldSize = p->nOpAlloc;
-    pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op));
+    pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
     if( pNew ){
       p->nOpAlloc = nNew;
       p->aOp = pNew;
       if( nNew>oldSize ){
@@ -30489,9 +33115,9 @@
   i = p->nOp;
   assert( p->magic==VDBE_MAGIC_INIT );
   if( p->nOpAlloc<=i ){
     resizeOpArray(p, i+1);
-    if( sqlite3MallocFailed() ){
+    if( p->db->mallocFailed ){
       return 0;
     }
   }
   p->nOp++;
@@ -30536,9 +33162,9 @@
   i = p->nLabel++;
   assert( p->magic==VDBE_MAGIC_INIT );
   if( i>=p->nLabelAlloc ){
     p->nLabelAlloc = p->nLabelAlloc*2 + 10;
-    p->aLabel = sqliteReallocOrFree(p->aLabel,
+    p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
                                     p->nLabelAlloc*sizeof(p->aLabel[0]));
   }
   if( p->aLabel ){
     p->aLabel[i] = -1;
@@ -30664,9 +33290,9 @@
     if( pOp->p2>=0 ) continue;
     assert( -1-pOp->p2<p->nLabel );
     pOp->p2 = aLabel[-1-pOp->p2];
   }
-  sqliteFree(p->aLabel);
+  sqlite3_free(p->aLabel);
   p->aLabel = 0;
 
   *pMaxFuncArgs = nMaxArgs;
   *pMaxStack = nMaxStack;
@@ -30700,9 +33326,9 @@
 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
   int addr;
   assert( p->magic==VDBE_MAGIC_INIT );
   resizeOpArray(p, p->nOp + nOp);
-  if( sqlite3MallocFailed() ){
+  if( p->db->mallocFailed ){
     return 0;
   }
   addr = p->nOp;
   if( nOp>0 ){
@@ -30766,9 +33392,9 @@
 ** the FuncDef is not ephermal, then do nothing.
 */
 static void freeEphemeralFunction(FuncDef *pDef){
   if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
-    sqliteFree(pDef);
+    sqlite3_free(pDef);
   }
 }
 
 /*
@@ -30779,9 +33405,9 @@
     switch( p3type ){
       case P3_DYNAMIC:
       case P3_KEYINFO:
       case P3_KEYINFO_HANDOFF: {
-        sqliteFree(p3);
+        sqlite3_free(p3);
         break;
       }
       case P3_MPRINTF: {
         sqlite3_free(p3);
@@ -30790,9 +33416,9 @@
       case P3_VDBEFUNC: {
         VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
         freeEphemeralFunction(pVdbeFunc->pFunc);
         sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
-        sqliteFree(pVdbeFunc);
+        sqlite3_free(pVdbeFunc);
         break;
       }
       case P3_FUNCDEF: {
         freeEphemeralFunction((FuncDef*)p3);
@@ -30828,17 +33454,17 @@
 ** static array using sqlite3VdbeAddOpList but we want to make a
 ** few minor changes to the program.
 **
 ** If n>=0 then the P3 operand is dynamic, meaning that a copy of
-** the string is made into memory obtained from sqliteMalloc().
+** the string is made into memory obtained from sqlite3_malloc().
 ** A value of n==0 means copy bytes of zP3 up to and including the
 ** first null byte.  If n>0 then copy n+1 bytes of zP3.
 **
 ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
 ** A copy is made of the KeyInfo structure into memory obtained from
-** sqliteMalloc, to be freed when the Vdbe is finalized.
+** sqlite3_malloc, to be freed when the Vdbe is finalized.
 ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
-** stored in memory that the caller has obtained from sqliteMalloc. The
+** stored in memory that the caller has obtained from sqlite3_malloc. The
 ** caller should not free the allocation, it will be freed when the Vdbe is
 ** finalized.
 **
 ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
@@ -30849,9 +33475,9 @@
 */
 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
   Op *pOp;
   assert( p==0 || p->magic==VDBE_MAGIC_INIT );
-  if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
+  if( p==0 || p->aOp==0 || p->db->mallocFailed ){
     if (n != P3_KEYINFO) {
       freeP3(n, (void*)*(char**)&zP3);
     }
     return;
@@ -30871,9 +33497,9 @@
     int nField, nByte;
 
     nField = ((KeyInfo*)zP3)->nField;
     nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
-    pKeyInfo = sqliteMallocRaw( nByte );
+    pKeyInfo = sqlite3_malloc( nByte );
     pOp->p3 = (char*)pKeyInfo;
     if( pKeyInfo ){
       unsigned char *aSortOrder;
       memcpy(pKeyInfo, zP3, nByte);
@@ -30883,8 +33509,9 @@
         memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
       }
       pOp->p3type = P3_KEYINFO;
     }else{
+      p->db->mallocFailed = 1;
       pOp->p3type = P3_NOTUSED;
     }
   }else if( n==P3_KEYINFO_HANDOFF ){
     pOp->p3 = (char*)zP3;
@@ -30893,9 +33520,9 @@
     pOp->p3 = (char*)zP3;
     pOp->p3type = n;
   }else{
     if( n==0 ) n = strlen(zP3);
-    pOp->p3 = sqliteStrNDup(zP3, n);
+    pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n);
     pOp->p3type = P3_DYNAMIC;
   }
 }
 
@@ -30906,11 +33533,11 @@
 */
 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
   va_list ap;
   assert( p->nOp>0 || p->aOp==0 );
-  assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || sqlite3MallocFailed() );
+  assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed );
   va_start(ap, zFormat);
-  sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC);
+  sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC);
   va_end(ap);
 }
 #endif
 
@@ -30918,9 +33545,9 @@
 ** Return the opcode for a given address.
 */
 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
   assert( p->magic==VDBE_MAGIC_INIT );
-  assert( (addr>=0 && addr<p->nOp) || sqlite3MallocFailed() );
+  assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
   return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
 }
 
 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
@@ -30994,8 +33621,23 @@
   return zP3;
 }
 #endif
 
+/*
+** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
+**
+*/
+SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
+  int mask;
+  assert( i>=0 && i<p->db->nDb );
+  assert( i<sizeof(p->btreeMask)*8 );
+  mask = 1<<i;
+  if( (p->btreeMask & mask)==0 ){
+    p->btreeMask |= mask;
+    sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
+  }
+}
+
 
 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 /*
 ** Print a single opcode.  This routine is used for debugging only.
@@ -31006,9 +33648,9 @@
   static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
   if( pOut==0 ) pOut = stdout;
   zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
   fprintf(pOut, zFormat1,
-      pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3);
+      pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3);
   fflush(pOut);
 }
 #endif
 
@@ -31017,8 +33659,9 @@
 */
 static void releaseMemArray(Mem *p, int N){
   if( p ){
     while( N-->0 ){
+      assert( N<2 || p[0].db==p[1].db );
       sqlite3VdbeMemRelease(p++);
     }
   }
 }
@@ -31070,9 +33713,9 @@
     pMem->u.i = i;                                /* Program counter */
     pMem++;
 
     pMem->flags = MEM_Static|MEM_Str|MEM_Term;
-    pMem->z = (char*)sqlite3OpcodeNames[pOp->opcode];  /* Opcode */
+    pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
     assert( pMem->z!=0 );
     pMem->n = strlen(pMem->z);
     pMem->type = SQLITE_TEXT;
     pMem->enc = SQLITE_UTF8;
@@ -31132,10 +33775,11 @@
   if( sqlite3_io_trace==0 ) return;
   if( nOp<1 ) return;
   pOp = &p->aOp[nOp-1];
   if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
-    char *z = sqlite3StrDup(pOp->p3);
     int i, j;
+    char z[1000];
+    sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3);
     for(i=0; isspace((unsigned char)z[i]); i++){}
     for(j=0; z[i]; i++){
       if( isspace((unsigned char)z[i]) ){
         if( z[i-1]!=' ' ){
@@ -31146,9 +33790,8 @@
       }
     }
     z[j] = 0;
     sqlite3_io_trace("SQL %s\n", z);
-    sqliteFree(z);
   }
 }
 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
 
@@ -31169,8 +33812,9 @@
   int nCursor,                   /* Number of cursors to allocate */
   int isExplain                  /* True if the EXPLAIN keywords is present */
 ){
   int n;
+  sqlite3 *db = p->db;
 
   assert( p!=0 );
   assert( p->magic==VDBE_MAGIC_INIT );
 
@@ -31203,17 +33847,17 @@
     assert( nStack<p->nOp );
     if( isExplain ){
       nStack = 10;
     }
-    p->aStack = sqliteMalloc(
+    p->aStack = sqlite3DbMallocZero(db,
         nStack*sizeof(p->aStack[0])    /* aStack */
       + nArg*sizeof(Mem*)              /* apArg */
       + nVar*sizeof(Mem)               /* aVar */
       + nVar*sizeof(char*)             /* azVar */
       + nMem*sizeof(Mem)               /* aMem */
       + nCursor*sizeof(Cursor*)        /* apCsr */
     );
-    if( !sqlite3MallocFailed() ){
+    if( !db->mallocFailed ){
       p->aMem = &p->aStack[nStack];
       p->nMem = nMem;
       p->aVar = &p->aMem[nMem];
       p->nVar = nVar;
@@ -31223,13 +33867,18 @@
       p->apCsr = (Cursor**)&p->azVar[nVar];
       p->nCursor = nCursor;
       for(n=0; n<nVar; n++){
         p->aVar[n].flags = MEM_Null;
+        p->aVar[n].db = db;
+      }
+      for(n=0; n<nStack; n++){
+        p->aStack[n].db = db;
       }
     }
   }
   for(n=0; n<p->nMem; n++){
     p->aMem[n].flags = MEM_Null;
+    p->aMem[n].db = db;
   }
 
   p->pTos = &p->aStack[-1];
   p->pc = -1;
@@ -31255,9 +33904,9 @@
 #endif
 }
 
 /*
-** Close a cursor and release all the resources that cursor happens
+** Close a VDBE cursor and release all the resources that cursor happens
 ** to hold.
 */
 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
   if( pCx==0 ){
@@ -31279,22 +33928,24 @@
     sqlite3SafetyOn(p->db);
     p->inVtabMethod = 0;
   }
 #endif
-  sqliteFree(pCx->pData);
-  sqliteFree(pCx->aType);
-  sqliteFree(pCx);
-}
-
-/*
-** Close all cursors
-*/
-static void closeAllCursors(Vdbe *p){
+  sqlite3_free(pCx->pData);
+  sqlite3_free(pCx->aType);
+  sqlite3_free(pCx);
+}
+
+/*
+** Close all cursors except for VTab cursors that are currently
+** in use.
+*/
+static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
   int i;
   if( p->apCsr==0 ) return;
   for(i=0; i<p->nCursor; i++){
-    if( !p->inVtabMethod || (p->apCsr[i] && !p->apCsr[i]->pVtabCursor) ){
-      sqlite3VdbeFreeCursor(p, p->apCsr[i]);
+    Cursor *pC = p->apCsr[i];
+    if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
+      sqlite3VdbeFreeCursor(p, pC);
       p->apCsr[i] = 0;
     }
   }
 }
@@ -31311,21 +33962,21 @@
   if( p->aStack ){
     releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
     p->pTos = &p->aStack[-1];
   }
-  closeAllCursors(p);
+  closeAllCursorsExceptActiveVtabs(p);
   releaseMemArray(p->aMem, p->nMem);
   sqlite3VdbeFifoClear(&p->sFifo);
   if( p->contextStack ){
     for(i=0; i<p->contextStackTop; i++){
       sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
     }
-    sqliteFree(p->contextStack);
+    sqlite3_free(p->contextStack);
   }
   p->contextStack = 0;
   p->contextStackDepth = 0;
   p->contextStackTop = 0;
-  sqliteFree(p->zErrMsg);
+  sqlite3_free(p->zErrMsg);
   p->zErrMsg = 0;
   p->resOnStack = 0;
 }
 
@@ -31337,16 +33988,19 @@
 */
 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
   Mem *pColName;
   int n;
+
   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
-  sqliteFree(p->aColName);
+  sqlite3_free(p->aColName);
   n = nResColumn*COLNAME_N;
   p->nResColumn = nResColumn;
-  p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
+  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
   if( p->aColName==0 ) return;
   while( n-- > 0 ){
-    (pColName++)->flags = MEM_Null;
+    pColName->flags = MEM_Null;
+    pColName->db = p->db;
+    pColName++;
   }
 }
 
 /*
@@ -31356,17 +34010,17 @@
 ** This call must be made after a call to sqlite3VdbeSetNumCols().
 **
 ** If N==P3_STATIC  it means that zName is a pointer to a constant static
 ** string and we can just copy the pointer. If it is P3_DYNAMIC, then
-** the string is freed using sqliteFree() when the vdbe is finished with
+** the string is freed using sqlite3_free() when the vdbe is finished with
 ** it. Otherwise, N bytes of zName are copied.
 */
 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
   int rc;
   Mem *pColName;
   assert( idx<p->nResColumn );
   assert( var<COLNAME_N );
-  if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
+  if( p->db->mallocFailed ) return SQLITE_NOMEM;
   assert( p->aColName!=0 );
   pColName = &(p->aColName[idx+var*p->nResColumn]);
   if( N==P3_DYNAMIC || N==P3_STATIC ){
     rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
@@ -31410,9 +34064,9 @@
   ** file is required for an atomic commit.
   */
   for(i=0; i<db->nDb; i++){
     Btree *pBt = db->aDb[i].pBt;
-    if( pBt && sqlite3BtreeIsInTrans(pBt) ){
+    if( sqlite3BtreeIsInTrans(pBt) ){
       needXcommit = 1;
       if( i!=1 ) nTrans++;
     }
   }
@@ -31465,28 +34119,33 @@
   ** committed atomicly.
   */
 #ifndef SQLITE_OMIT_DISKIO
   else{
+    sqlite3_vfs *pVfs = db->pVfs;
     int needSync = 0;
     char *zMaster = 0;   /* File-name for the master journal */
     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
-    OsFile *master = 0;
+    sqlite3_file *pMaster = 0;
+    i64 offset = 0;
 
     /* Select a master journal file name */
     do {
       u32 random;
-      sqliteFree(zMaster);
+      sqlite3_free(zMaster);
       sqlite3Randomness(sizeof(random), &random);
-      zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff);
+      zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
       if( !zMaster ){
         return SQLITE_NOMEM;
       }
-    }while( sqlite3OsFileExists(zMaster) );
+    }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
 
     /* Open the master journal. */
-    rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
-    if( rc!=SQLITE_OK ){
-      sqliteFree(zMaster);
+    rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
+        SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
+        SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
+    );
+    if( rc!=SQLITE_OK ){
+      sqlite3_free(zMaster);
       return rc;
     }
 
     /* Write the name of each database file in the transaction into the new
@@ -31497,35 +34156,35 @@
     */
     for(i=0; i<db->nDb; i++){
       Btree *pBt = db->aDb[i].pBt;
       if( i==1 ) continue;   /* Ignore the TEMP database */
-      if( pBt && sqlite3BtreeIsInTrans(pBt) ){
+      if( sqlite3BtreeIsInTrans(pBt) ){
         char const *zFile = sqlite3BtreeGetJournalname(pBt);
         if( zFile[0]==0 ) continue;  /* Ignore :memory: databases */
         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
           needSync = 1;
         }
-        rc = sqlite3OsWrite(master, zFile, strlen(zFile)+1);
+        rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
+        offset += strlen(zFile)+1;
         if( rc!=SQLITE_OK ){
-          sqlite3OsClose(&master);
-          sqlite3OsDelete(zMaster);
-          sqliteFree(zMaster);
+          sqlite3OsCloseFree(pMaster);
+          sqlite3OsDelete(pVfs, zMaster, 0);
+          sqlite3_free(zMaster);
           return rc;
         }
       }
     }
 
-
-    /* Sync the master journal file. Before doing this, open the directory
-    ** the master journal file is store in so that it gets synced too.
+    /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
+    ** flag is set this is not required.
     */
     zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
-    rc = sqlite3OsOpenDirectory(master, zMainFile);
-    if( rc!=SQLITE_OK ||
-          (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){
-      sqlite3OsClose(&master);
-      sqlite3OsDelete(zMaster);
-      sqliteFree(zMaster);
+    if( (needSync
+     && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
+     && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
+      sqlite3OsCloseFree(pMaster);
+      sqlite3OsDelete(pVfs, zMaster, 0);
+      sqlite3_free(zMaster);
       return rc;
     }
 
     /* Sync all the db files involved in the transaction. The same call
@@ -31539,37 +34198,26 @@
     ** file before the failure occured.
     */
     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
       Btree *pBt = db->aDb[i].pBt;
-      if( pBt && sqlite3BtreeIsInTrans(pBt) ){
+      if( pBt ){
         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
       }
     }
-    sqlite3OsClose(&master);
-    if( rc!=SQLITE_OK ){
-      sqliteFree(zMaster);
+    sqlite3OsCloseFree(pMaster);
+    if( rc!=SQLITE_OK ){
+      sqlite3_free(zMaster);
       return rc;
     }
 
     /* Delete the master journal file. This commits the transaction. After
     ** doing this the directory is synced again before any individual
     ** transaction files are deleted.
     */
-    rc = sqlite3OsDelete(zMaster);
-    sqliteFree(zMaster);
+    rc = sqlite3OsDelete(pVfs, zMaster, 1);
+    sqlite3_free(zMaster);
     zMaster = 0;
     if( rc ){
-      return rc;
-    }
-    rc = sqlite3OsSyncDirectory(zMainFile);
-    if( rc!=SQLITE_OK ){
-      /* This is not good. The master journal file has been deleted, but
-      ** the directory sync failed. There is no completely safe course of
-      ** action from here. The individual journals contain the name of the
-      ** master journal file, but there is no way of knowing if that
-      ** master journal exists now or if it will exist after the operating
-      ** system crash that may follow the fsync() failure.
-      */
       return rc;
     }
 
     /* All files and directories have already been synced, so the following
@@ -31621,23 +34269,30 @@
 #define checkActiveVdbeCnt(x)
 #endif
 
 /*
-** Find every active VM other than pVdbe and change its status to
-** aborted.  This happens when one VM causes a rollback due to an
-** ON CONFLICT ROLLBACK clause (for example).  The other VMs must be
-** aborted so that they do not have data rolled out from underneath
-** them leading to a segfault.
-*/
-SQLITE_PRIVATE void sqlite3AbortOtherActiveVdbes(sqlite3 *db, Vdbe *pExcept){
-  Vdbe *pOther;
-  for(pOther=db->pVdbe; pOther; pOther=pOther->pNext){
-    if( pOther==pExcept ) continue;
-    if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue;
-    checkActiveVdbeCnt(db);
-    closeAllCursors(pOther);
-    checkActiveVdbeCnt(db);
-    pOther->aborted = 1;
+** For every Btree that in database connection db which
+** has been modified, "trip" or invalidate each cursor in
+** that Btree might have been modified so that the cursor
+** can never be used again.  This happens when a rollback
+*** occurs.  We have to trip all the other cursors, even
+** cursor from other VMs in different database connections,
+** so that none of them try to use the data at which they
+** were pointing and which now may have been changed due
+** to the rollback.
+**
+** Remember that a rollback can delete tables complete and
+** reorder rootpages.  So it is not sufficient just to save
+** the state of the cursor.  We have to invalidate the cursor
+** so that it is never used again.
+*/
+void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
+  int i;
+  for(i=0; i<db->nDb; i++){
+    Btree *p = db->aDb[i].pBt;
+    if( p && sqlite3BtreeIsInTrans(p) ){
+      sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
+    }
   }
 }
 
 /*
@@ -31645,9 +34300,10 @@
 ** has made changes and is in autocommit mode, then commit those
 ** changes.  If a rollback is needed, then do the rollback.
 **
 ** This routine is the only way to move the state of a VM from
-** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.
+** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
+** call this on a VM that is in the SQLITE_MAGIC_HALT state.
 **
 ** Return an error code.  If the commit could not complete because of
 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
 ** means the close did not happen and needs to be repeated.
@@ -31685,25 +34341,24 @@
   ** No error:
   **
   */
 
-  if( sqlite3MallocFailed() ){
+  if( p->db->mallocFailed ){
     p->rc = SQLITE_NOMEM;
   }
-  if( p->magic!=VDBE_MAGIC_RUN ){
-    /* Already halted.  Nothing to do. */
-    assert( p->magic==VDBE_MAGIC_HALT );
-#ifndef SQLITE_OMIT_VIRTUALTABLE
-    closeAllCursors(p);
-#endif
+  closeAllCursorsExceptActiveVtabs(p);
+  if( p->magic!=VDBE_MAGIC_RUN ){
     return SQLITE_OK;
   }
-  closeAllCursors(p);
   checkActiveVdbeCnt(db);
 
   /* No commit or rollback needed if the program never started */
   if( p->pc>=0 ){
     int mrc;   /* Primary error code from p->rc */
+
+    /* Lock all btrees used by the statement */
+    sqlite3BtreeMutexArrayEnter(&p->aMutex);
+
     /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */
     mrc = p->rc & 0xff;
     isSpecialError = (
         (mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR || mrc==SQLITE_INTERRUPT)?1:0);
@@ -31746,8 +34401,9 @@
             break;
         }
       }
 
+
       /* If the query was read-only, we need do no rollback at all. Otherwise,
       ** proceed with the special handling.
       */
       if( !isReadOnly ){
@@ -31759,9 +34415,9 @@
         }else{
           /* We are forced to roll back the active transaction. Before doing
           ** so, abort any other statements this handle currently has active.
           */
-          sqlite3AbortOtherActiveVdbes(db, p);
+          invalidateCursorsOnModifiedBtrees(db);
           sqlite3RollbackAll(db);
           db->autoCommit = 1;
         }
       }
@@ -31774,14 +34430,15 @@
     ** above has occured.
     */
     if( db->autoCommit && db->activeVdbeCnt==1 ){
       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
-	/* The auto-commit flag is true, and the vdbe program was
+        /* The auto-commit flag is true, and the vdbe program was
         ** successful or hit an 'OR FAIL' constraint. This means a commit
         ** is required.
         */
         int rc = vdbeCommit(db);
         if( rc==SQLITE_BUSY ){
+          sqlite3BtreeMutexArrayLeave(&p->aMutex);
           return SQLITE_BUSY;
         }else if( rc!=SQLITE_OK ){
           p->rc = rc;
           sqlite3RollbackAll(db);
@@ -31798,9 +34455,9 @@
         }
       }else if( p->errorAction==OE_Abort ){
         xFunc = sqlite3BtreeRollbackStmt;
       }else{
-        sqlite3AbortOtherActiveVdbes(db, p);
+        invalidateCursorsOnModifiedBtrees(db);
         sqlite3RollbackAll(db);
         db->autoCommit = 1;
       }
     }
@@ -31842,8 +34499,11 @@
     if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
       sqlite3ResetInternalSchema(db, 0);
       db->flags = (db->flags | SQLITE_InternChanges);
     }
+
+    /* Release the locks */
+    sqlite3BtreeMutexArrayLeave(&p->aMutex);
   }
 
   /* We have successfully halted and closed the VM.  Record this fact. */
   if( p->pc>=0 ){
@@ -31850,11 +34510,16 @@
     db->activeVdbeCnt--;
   }
   p->magic = VDBE_MAGIC_HALT;
   checkActiveVdbeCnt(db);
-
-  return SQLITE_OK;
-}
+  if( p->db->mallocFailed ){
+    p->rc = SQLITE_NOMEM;
+  }
+  checkActiveVdbeCnt(db);
+
+  return SQLITE_OK;
+}
+
 
 /*
 ** Each VDBE holds the result of the most recent sqlite3_step() call
 ** in p->rc.  This routine sets that result back to SQLITE_OK.
@@ -31892,9 +34557,9 @@
   ** instructions yet, leave the main database error information unchanged.
   */
   if( p->pc>=0 ){
     if( p->zErrMsg ){
-      sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
+      sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
       db->errCode = p->rc;
       p->zErrMsg = 0;
     }else if( p->rc ){
       sqlite3Error(db, p->rc, 0);
@@ -31939,11 +34604,8 @@
   }
 #endif
   p->magic = VDBE_MAGIC_INIT;
   p->aborted = 0;
-  if( p->rc==SQLITE_SCHEMA ){
-    sqlite3ResetInternalSchema(db, 0);
-  }
   return p->rc & db->errMask;
 }
 
 /*
@@ -32001,18 +34663,18 @@
     for(i=0; i<p->nOp; i++){
       Op *pOp = &p->aOp[i];
       freeP3(pOp->p3type, pOp->p3);
     }
-    sqliteFree(p->aOp);
+    sqlite3_free(p->aOp);
   }
   releaseMemArray(p->aVar, p->nVar);
-  sqliteFree(p->aLabel);
-  sqliteFree(p->aStack);
+  sqlite3_free(p->aLabel);
+  sqlite3_free(p->aStack);
   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
-  sqliteFree(p->aColName);
-  sqliteFree(p->zSql);
+  sqlite3_free(p->aColName);
+  sqlite3_free(p->zSql);
   p->magic = VDBE_MAGIC_DEAD;
-  sqliteFree(p);
+  sqlite3_free(p);
 }
 
 /*
 ** If a MoveTo operation is pending on the given cursor, then do that
@@ -32155,13 +34817,26 @@
 ** Developers using SQLite on an ARM7 should compile and run their
 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
 ** enabled, some asserts below will ensure that the byte order of
 ** floating point values is correct.
+**
+** (2007-08-30)  Frank van Vugt has studied this problem closely
+** and has send his findings to the SQLite developers.  Frank
+** writes that some Linux kernels offer floating point hardware
+** emulation that uses only 32-bit mantissas instead of a full
+** 48-bits as required by the IEEE standard.  (This is the
+** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
+** byte swapping becomes very complicated.  To avoid problems,
+** the necessary byte swapping is carried out using a 64-bit integer
+** rather than a 64-bit float.  Frank assures us that the code here
+** works for him.  We, the developers, have no way to independently
+** verify this, but Frank seems to know what he is talking about
+** so we trust him.
 */
 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
-static double floatSwap(double in){
+static u64 floatSwap(u64 in){
   union {
-    double r;
+    u64 r;
     u32 i[2];
   } u;
   u32 t;
 
@@ -32203,10 +34878,10 @@
     u64 v;
     int i;
     if( serial_type==7 ){
       assert( sizeof(v)==sizeof(pMem->r) );
-      swapMixedEndianFloat(pMem->r);
       memcpy(&v, &pMem->r, sizeof(v));
+      swapMixedEndianFloat(v);
     }else{
       v = pMem->u.i;
     }
     len = i = sqlite3VdbeSerialTypeLen(serial_type);
@@ -32294,11 +34969,11 @@
       ** endian.
       */
       static const u64 t1 = ((u64)0x3ff00000)<<32;
       static const double r1 = 1.0;
-      double r2 = r1;
-      swapMixedEndianFloat(r2);
-      assert( sizeof(r2)==sizeof(t1) && memcmp(&r2, &t1, sizeof(r1))==0 );
+      u64 t2 = t1;
+      swapMixedEndianFloat(t2);
+      assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
 #endif
 
       x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
       y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
@@ -32307,10 +34982,10 @@
         pMem->u.i = *(i64*)&x;
         pMem->flags = MEM_Int;
       }else{
         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
-        memcpy(&pMem->r, &x, sizeof(x));
-        swapMixedEndianFloat(pMem->r);
+        swapMixedEndianFloat(x);
+        memcpy(&pMem->r, &x, sizeof(x));
         pMem->flags = MEM_Real;
       }
       return 8;
     }
@@ -32377,9 +35052,11 @@
 
   Mem mem1;
   Mem mem2;
   mem1.enc = pKeyInfo->enc;
+  mem1.db = pKeyInfo->db;
   mem2.enc = pKeyInfo->enc;
+  mem2.db = pKeyInfo->db;
 
   idx1 = GetVarint(aKey1, szHdr1);
   d1 = szHdr1;
   idx2 = GetVarint(aKey2, szHdr2);
@@ -32517,8 +35194,9 @@
 ** This routine sets the value to be returned by subsequent calls to
 ** sqlite3_changes() on the database handle 'db'.
 */
 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
+  assert( sqlite3_mutex_held(db->mutex) );
   db->nChange = nChange;
   db->nTotalChange += nChange;
 }
 
@@ -32584,13 +35262,75 @@
   Vdbe *p = (Vdbe*)pStmt;
   return p==0 || p->expired;
 }
 
+/*
+** The following routine destroys a virtual machine that is created by
+** the sqlite3_compile() routine. The integer returned is an SQLITE_
+** success/failure code that describes the result of executing the virtual
+** machine.
+**
+** This routine sets the error code and string returned by
+** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
+*/
+SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
+  int rc;
+  if( pStmt==0 ){
+    rc = SQLITE_OK;
+  }else{
+    Vdbe *v = (Vdbe*)pStmt;
+    sqlite3_mutex *mutex = v->db->mutex;
+    sqlite3_mutex_enter(mutex);
+    rc = sqlite3VdbeFinalize(v);
+    sqlite3_mutex_leave(mutex);
+  }
+  return rc;
+}
+
+/*
+** Terminate the current execution of an SQL statement and reset it
+** back to its starting state so that it can be reused. A success code from
+** the prior execution is returned.
+**
+** This routine sets the error code and string returned by
+** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
+*/
+SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
+  int rc;
+  if( pStmt==0 ){
+    rc = SQLITE_OK;
+  }else{
+    Vdbe *v = (Vdbe*)pStmt;
+    sqlite3_mutex_enter(v->db->mutex);
+    rc = sqlite3VdbeReset(v);
+    sqlite3VdbeMakeReady(v, -1, 0, 0, 0);
+    assert( (rc & (v->db->errMask))==rc );
+    sqlite3_mutex_leave(v->db->mutex);
+  }
+  return rc;
+}
+
+/*
+** Set all the parameters in the compiled SQL statement to NULL.
+*/
+SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
+  int i;
+  int rc = SQLITE_OK;
+  Vdbe *v = (Vdbe*)pStmt;
+  sqlite3_mutex_enter(v->db->mutex);
+  for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
+    rc = sqlite3_bind_null(pStmt, i);
+  }
+  sqlite3_mutex_leave(v->db->mutex);
+  return rc;
+}
+
+
 /**************************** sqlite3_value_  *******************************
 ** The following routines extract information from a Mem or sqlite3_value
 ** structure.
 */
-const void *sqlite3_value_blob(sqlite3_value *pVal){
+SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
   Mem *p = (Mem*)pVal;
   if( p->flags & (MEM_Blob|MEM_Str) ){
     sqlite3VdbeMemExpandBlob(p);
     p->flags &= ~MEM_Str;
@@ -32599,123 +35339,143 @@
   }else{
     return sqlite3_value_text(pVal);
   }
 }
-int sqlite3_value_bytes(sqlite3_value *pVal){
+SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
 }
-int sqlite3_value_bytes16(sqlite3_value *pVal){
+SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
 }
-double sqlite3_value_double(sqlite3_value *pVal){
+SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
   return sqlite3VdbeRealValue((Mem*)pVal);
 }
-int sqlite3_value_int(sqlite3_value *pVal){
+SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
   return sqlite3VdbeIntValue((Mem*)pVal);
 }
-sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
+SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
   return sqlite3VdbeIntValue((Mem*)pVal);
 }
-const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
+SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
 }
 #ifndef SQLITE_OMIT_UTF16
-const void *sqlite3_value_text16(sqlite3_value* pVal){
+SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
 }
-const void *sqlite3_value_text16be(sqlite3_value *pVal){
+SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
 }
-const void *sqlite3_value_text16le(sqlite3_value *pVal){
+SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
 }
 #endif /* SQLITE_OMIT_UTF16 */
-int sqlite3_value_type(sqlite3_value* pVal){
+SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
   return pVal->type;
 }
-/* sqlite3_value_numeric_type() defined in vdbe.c */
 
 /**************************** sqlite3_result_  *******************************
 ** The following routines are used by user-defined functions to specify
 ** the function result.
 */
-void sqlite3_result_blob(
+SQLITE_API void sqlite3_result_blob(
   sqlite3_context *pCtx,
   const void *z,
   int n,
   void (*xDel)(void *)
 ){
   assert( n>=0 );
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel);
 }
-void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
+SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
 }
-void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
+SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   pCtx->isError = 1;
   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
 }
 #ifndef SQLITE_OMIT_UTF16
-void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
+SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   pCtx->isError = 1;
   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
 }
 #endif
-void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
+SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
 }
-void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
+SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
 }
-void sqlite3_result_null(sqlite3_context *pCtx){
+SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetNull(&pCtx->s);
 }
-void sqlite3_result_text(
+SQLITE_API void sqlite3_result_text(
   sqlite3_context *pCtx,
   const char *z,
   int n,
   void (*xDel)(void *)
 ){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel);
 }
 #ifndef SQLITE_OMIT_UTF16
-void sqlite3_result_text16(
+SQLITE_API void sqlite3_result_text16(
   sqlite3_context *pCtx,
   const void *z,
   int n,
   void (*xDel)(void *)
 ){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel);
 }
-void sqlite3_result_text16be(
+SQLITE_API void sqlite3_result_text16be(
   sqlite3_context *pCtx,
   const void *z,
   int n,
   void (*xDel)(void *)
 ){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel);
 }
-void sqlite3_result_text16le(
+SQLITE_API void sqlite3_result_text16le(
   sqlite3_context *pCtx,
   const void *z,
   int n,
   void (*xDel)(void *)
 ){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel);
 }
 #endif /* SQLITE_OMIT_UTF16 */
-void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
+SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemCopy(&pCtx->s, pValue);
 }
-void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
+SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
 }
 
 /* Force an SQLITE_TOOBIG error. */
-void sqlite3_result_error_toobig(sqlite3_context *pCtx){
+SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1);
 }
 
+/* An SQLITE_NOMEM error. */
+SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
+  sqlite3VdbeMemSetNull(&pCtx->s);
+  pCtx->isError = 1;
+  pCtx->s.db->mallocFailed = 1;
+}
 
 /*
 ** Execute the statement pStmt, either until a row of data is ready, the
 ** statement is completely executed or an error occurs.
@@ -32729,9 +35489,10 @@
   sqlite3 *db;
   int rc;
 
   /* Assert that malloc() has not failed */
-  assert( !sqlite3MallocFailed() );
+  db = p->db;
+  assert( !db->mallocFailed );
 
   if( p==0 || p->magic!=VDBE_MAGIC_RUN ){
     return SQLITE_MISUSE;
   }
@@ -32744,9 +35505,8 @@
     }
     rc = SQLITE_ERROR;
     goto end_of_step;
   }
-  db = p->db;
   if( sqlite3SafetyOn(db) ){
     p->rc = SQLITE_MISUSE;
     return SQLITE_MISUSE;
   }
@@ -32775,9 +35535,9 @@
       }
     }
     if( db->xProfile && !db->init.busy ){
       double rNow;
-      sqlite3OsCurrentTime(&rNow);
+      sqlite3OsCurrentTime(db->pVfs, &rNow);
       p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0;
     }
 #endif
 
@@ -32812,9 +35572,9 @@
   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){
     double rNow;
     u64 elapseTime;
 
-    sqlite3OsCurrentTime(&rNow);
+    sqlite3OsCurrentTime(db->pVfs, &rNow);
     elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime;
     assert( p->nOp>0 );
     assert( p->aOp[p->nOp-1].opcode==OP_Noop );
     assert( p->aOp[p->nOp-1].p3!=0 );
@@ -32845,21 +35605,29 @@
 ** call sqlite3Reprepare() and try again.
 */
 #ifdef SQLITE_OMIT_PARSER
 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
-  return sqlite3Step((Vdbe*)pStmt);
+  int rc;
+  Vdbe *v;
+  v = (Vdbe*)pStmt;
+  sqlite3_mutex_enter(v->db->mutex);
+  rc = sqlite3Step(v);
+  sqlite3_mutex_leave(v->db->mutex);
+  return rc;
 }
 #else
 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
   int cnt = 0;
   int rc;
   Vdbe *v = (Vdbe*)pStmt;
+  sqlite3_mutex_enter(v->db->mutex);
   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
          && cnt++ < 5
          && sqlite3Reprepare(v) ){
     sqlite3_reset(pStmt);
     v->expired = 0;
   }
+  sqlite3_mutex_leave(v->db->mutex);
   return rc;
 }
 #endif
 
@@ -32866,9 +35634,9 @@
 /*
 ** Extract the user data from a sqlite3_context structure and return a
 ** pointer to it.
 */
-void *sqlite3_user_data(sqlite3_context *p){
+SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
   assert( p && p->pFunc );
   return p->pFunc->pUserData;
 }
 
@@ -32886,35 +35654,37 @@
   sqlite3_value **argv       /* Value of each argument */
 ){
   const char *zName = context->pFunc->zName;
   char *zErr;
-  zErr = sqlite3MPrintf(
+  zErr = sqlite3MPrintf(0,
       "unable to use function %s in the requested context", zName);
   sqlite3_result_error(context, zErr, -1);
-  sqliteFree(zErr);
+  sqlite3_free(zErr);
 }
 
 /*
 ** Allocate or return the aggregate context for a user function.  A new
 ** context is allocated on the first call.  Subsequent calls return the
 ** same context that was returned on prior calls.
 */
-void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
-  Mem *pMem = p->pMem;
+SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
+  Mem *pMem;
   assert( p && p->pFunc && p->pFunc->xStep );
+  assert( sqlite3_mutex_held(p->s.db->mutex) );
+  pMem = p->pMem;
   if( (pMem->flags & MEM_Agg)==0 ){
     if( nByte==0 ){
       assert( pMem->flags==MEM_Null );
       pMem->z = 0;
     }else{
       pMem->flags = MEM_Agg;
-      pMem->xDel = sqlite3FreeX;
+      pMem->xDel = sqlite3_free;
       pMem->u.pDef = p->pFunc;
       if( nByte<=NBFS ){
         pMem->z = pMem->zShort;
         memset(pMem->z, 0, nByte);
       }else{
-        pMem->z = sqliteMalloc( nByte );
+        pMem->z = sqlite3DbMallocZero(p->s.db, nByte);
       }
     }
   }
   return (void*)pMem->z;
@@ -32923,10 +35693,13 @@
 /*
 ** Return the auxilary data pointer, if any, for the iArg'th argument to
 ** the user-function defined by pCtx.
 */
-void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
-  VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc;
+SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
+  VdbeFunc *pVdbeFunc;
+
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
+  pVdbeFunc = pCtx->pVdbeFunc;
   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
     return 0;
   }
   return pVdbeFunc->apAux[iArg].pAux;
@@ -32936,26 +35709,29 @@
 ** Set the auxilary data pointer and delete function, for the iArg'th
 ** argument to the user-function defined by pCtx. Any previous value is
 ** deleted by calling the delete function specified when it was set.
 */
-void sqlite3_set_auxdata(
+SQLITE_API void sqlite3_set_auxdata(
   sqlite3_context *pCtx,
   int iArg,
   void *pAux,
   void (*xDelete)(void*)
 ){
   struct AuxData *pAuxData;
   VdbeFunc *pVdbeFunc;
-  if( iArg<0 ) return;
-
+  if( iArg<0 ) goto failed;
+
+  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   pVdbeFunc = pCtx->pVdbeFunc;
   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
+    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
-    pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc);
-    if( !pVdbeFunc ) return;
+    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
+    if( !pVdbeFunc ){
+      goto failed;
+    }
     pCtx->pVdbeFunc = pVdbeFunc;
-    memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0,
-             sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux));
+    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
     pVdbeFunc->nAux = iArg+1;
     pVdbeFunc->pFunc = pCtx->pFunc;
   }
 
@@ -32964,8 +35740,14 @@
     pAuxData->xDelete(pAuxData->pAux);
   }
   pAuxData->pAux = pAux;
   pAuxData->xDelete = xDelete;
+  return;
+
+failed:
+  if( xDelete ){
+    xDelete(pAux);
+  }
 }
 
 /*
 ** Return the number of times the Step function of a aggregate has been
@@ -32975,17 +35757,17 @@
 ** provide only to avoid breaking legacy code.  New aggregate function
 ** implementations should keep their own counts within their aggregate
 ** context.
 */
-int sqlite3_aggregate_count(sqlite3_context *p){
+SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
   assert( p && p->pFunc && p->pFunc->xStep );
   return p->pMem->n;
 }
 
 /*
 ** Return the number of columns in the result set for the statement pStmt.
 */
-int sqlite3_column_count(sqlite3_stmt *pStmt){
+SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
   Vdbe *pVm = (Vdbe *)pStmt;
   return pVm ? pVm->nResColumn : 0;
 }
 
@@ -32992,9 +35774,9 @@
 /*
 ** Return the number of values available from the current row of the
 ** currently executing statement pStmt.
 */
-int sqlite3_data_count(sqlite3_stmt *pStmt){
+SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
   Vdbe *pVm = (Vdbe *)pStmt;
   if( pVm==0 || !pVm->resOnStack ) return 0;
   return pVm->nResColumn;
 }
@@ -33006,16 +35788,26 @@
 ** If iCol is not valid, return a pointer to a Mem which has a value
 ** of NULL.
 */
 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
-  Vdbe *pVm = (Vdbe *)pStmt;
-  int vals = sqlite3_data_count(pStmt);
-  if( pVm==0 || pVm->resOnStack==0 || i>=pVm->nResColumn || i<0 ){
-    static const Mem nullMem = {{0}, 0.0, "", 0, MEM_Null, SQLITE_NULL };
-    sqlite3Error(pVm->db, SQLITE_RANGE, 0);
-    return (Mem*)&nullMem;
-  }
-  return &pVm->pTos[(1-vals)+i];
+  Vdbe *pVm;
+  int vals;
+  Mem *pOut;
+
+  pVm = (Vdbe *)pStmt;
+  if( pVm && pVm->resOnStack && i<pVm->nResColumn && i>=0 ){
+    sqlite3_mutex_enter(pVm->db->mutex);
+    vals = sqlite3_data_count(pStmt);
+    pOut = &pVm->pTos[(1-vals)+i];
+  }else{
+    static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL };
+    if( pVm->db ){
+      sqlite3_mutex_enter(pVm->db->mutex);
+      sqlite3Error(pVm->db, SQLITE_RANGE, 0);
+    }
+    pOut = (Mem*)&nullMem;
+  }
+  return pOut;
 }
 
 /*
 ** This function is called after invoking an sqlite3_value_XXX function on a
@@ -33023,9 +35815,9 @@
 ** select list of a SELECT statement) that may cause a malloc() failure. If
 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
 ** code of statement pStmt set to SQLITE_NOMEM.
 **
-** Specificly, this is called from within:
+** Specifically, this is called from within:
 **
 **     sqlite3_column_int()
 **     sqlite3_column_int64()
 **     sqlite3_column_text()
@@ -33043,16 +35835,19 @@
   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
   ** and _finalize() will return NOMEM.
   */
   Vdbe *p = (Vdbe *)pStmt;
-  p->rc = sqlite3ApiExit(0, p->rc);
+  if( p ){
+    p->rc = sqlite3ApiExit(p->db, p->rc);
+    sqlite3_mutex_leave(p->db->mutex);
+  }
 }
 
 /**************************** sqlite3_column_  *******************************
 ** The following routines are used to access elements of the current row
 ** in the result set.
 */
-const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
+SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
   const void *val;
   val = sqlite3_value_blob( columnMem(pStmt,i) );
   /* Even though there is no encoding conversion, value_blob() might
   ** need to call malloc() to expand the result of a zeroblob()
@@ -33060,50 +35855,54 @@
   */
   columnMallocFailure(pStmt);
   return val;
 }
-int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
+SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
   columnMallocFailure(pStmt);
   return val;
 }
-int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
+SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
   columnMallocFailure(pStmt);
   return val;
 }
-double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
+SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
   double val = sqlite3_value_double( columnMem(pStmt,i) );
   columnMallocFailure(pStmt);
   return val;
 }
-int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
+SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
   int val = sqlite3_value_int( columnMem(pStmt,i) );
   columnMallocFailure(pStmt);
   return val;
 }
-sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
+SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
   columnMallocFailure(pStmt);
   return val;
 }
-const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
+SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
   columnMallocFailure(pStmt);
   return val;
 }
-sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
-  return columnMem(pStmt, i);
-}
-#ifndef SQLITE_OMIT_UTF16
-const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
+SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
+  sqlite3_value *pOut = columnMem(pStmt, i);
+  columnMallocFailure(pStmt);
+  return pOut;
+}
+#ifndef SQLITE_OMIT_UTF16
+SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
   columnMallocFailure(pStmt);
   return val;
 }
 #endif /* SQLITE_OMIT_UTF16 */
-int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
-  return sqlite3_value_type( columnMem(pStmt,i) );
+SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
+  int iType = sqlite3_value_type( columnMem(pStmt,i) );
+  columnMallocFailure(pStmt);
+  return iType;
 }
 
 /* The following function is experimental and subject to change or
 ** removal */
@@ -33133,35 +35932,43 @@
   int N,
   const void *(*xFunc)(Mem*),
   int useType
 ){
-  const void *ret;
+  const void *ret = 0;
   Vdbe *p = (Vdbe *)pStmt;
-  int n = sqlite3_column_count(pStmt);
-
-  if( p==0 || N>=n || N<0 ){
-    return 0;
-  }
-  N += useType*n;
-  ret = xFunc(&p->aColName[N]);
-
-  /* A malloc may have failed inside of the xFunc() call. If this is the case,
-  ** clear the mallocFailed flag and return NULL.
-  */
-  sqlite3ApiExit(0, 0);
+  int n;
+
+
+  if( p!=0 ){
+    n = sqlite3_column_count(pStmt);
+    if( N<n && N>=0 ){
+      N += useType*n;
+      sqlite3_mutex_enter(p->db->mutex);
+      ret = xFunc(&p->aColName[N]);
+
+      /* A malloc may have failed inside of the xFunc() call. If this
+      ** is the case, clear the mallocFailed flag and return NULL.
+      */
+      if( p->db && p->db->mallocFailed ){
+        p->db->mallocFailed = 0;
+        ret = 0;
+      }
+      sqlite3_mutex_leave(p->db->mutex);
+    }
+  }
   return ret;
 }
 
 /*
 ** Return the name of the Nth column of the result set returned by SQL
 ** statement pStmt.
 */
-const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
+SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
 }
 #ifndef SQLITE_OMIT_UTF16
-const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
+SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
 }
 #endif
@@ -33169,14 +35976,14 @@
 /*
 ** Return the column declaration type (if applicable) of the 'i'th column
 ** of the result set of SQL statement pStmt.
 */
-const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
+SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
 }
 #ifndef SQLITE_OMIT_UTF16
-const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
+SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
 }
 #endif /* SQLITE_OMIT_UTF16 */
@@ -33186,14 +35993,14 @@
 ** Return the name of the database from which a result column derives.
 ** NULL is returned if the result column is an expression or constant or
 ** anything else which is not an unabiguous reference to a database column.
 */
-const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
+SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
 }
 #ifndef SQLITE_OMIT_UTF16
-const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
+SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
 }
 #endif /* SQLITE_OMIT_UTF16 */
@@ -33202,14 +36009,14 @@
 ** Return the name of the table from which a result column derives.
 ** NULL is returned if the result column is an expression or constant or
 ** anything else which is not an unabiguous reference to a database column.
 */
-const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
+SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
 }
 #ifndef SQLITE_OMIT_UTF16
-const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
+SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
 }
 #endif /* SQLITE_OMIT_UTF16 */
@@ -33218,14 +36025,14 @@
 ** Return the name of the table column from which a result column derives.
 ** NULL is returned if the result column is an expression or constant or
 ** anything else which is not an unabiguous reference to a database column.
 */
-const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
+SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
 }
 #ifndef SQLITE_OMIT_UTF16
-const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
+SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
   return columnName(
       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
 }
 #endif /* SQLITE_OMIT_UTF16 */
@@ -33265,38 +36072,42 @@
 /*
 ** Bind a text or BLOB value.
 */
 static int bindText(
-  sqlite3_stmt *pStmt,
-  int i,
-  const void *zData,
-  int nData,
-  void (*xDel)(void*),
-  int encoding
+  sqlite3_stmt *pStmt,   /* The statement to bind against */
+  int i,                 /* Index of the parameter to bind */
+  const void *zData,     /* Pointer to the data to be bound */
+  int nData,             /* Number of bytes of data to be bound */
+  void (*xDel)(void*),   /* Destructor for the data */
+  int encoding           /* Encoding for the data */
 ){
   Vdbe *p = (Vdbe *)pStmt;
   Mem *pVar;
   int rc;
 
+  if( p==0 ){
+    return SQLITE_MISUSE;
+  }
+  sqlite3_mutex_enter(p->db->mutex);
   rc = vdbeUnbind(p, i);
-  if( rc || zData==0 ){
-    return rc;
-  }
-  pVar = &p->aVar[i-1];
-  rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
-  if( rc==SQLITE_OK && encoding!=0 ){
-    rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
-  }
-
-  sqlite3Error(((Vdbe *)pStmt)->db, rc, 0);
-  return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc);
+  if( rc==SQLITE_OK && zData!=0 ){
+    pVar = &p->aVar[i-1];
+    rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
+    if( rc==SQLITE_OK && encoding!=0 ){
+      rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
+    }
+    sqlite3Error(p->db, rc, 0);
+    rc = sqlite3ApiExit(p->db, rc);
+  }
+  sqlite3_mutex_leave(p->db->mutex);
+  return rc;
 }
 
 
 /*
 ** Bind a blob value to an SQL statement variable.
 */
-int sqlite3_bind_blob(
+SQLITE_API int sqlite3_bind_blob(
   sqlite3_stmt *pStmt,
   int i,
   const void *zData,
   int nData,
@@ -33303,33 +36114,42 @@
   void (*xDel)(void*)
 ){
   return bindText(pStmt, i, zData, nData, xDel, 0);
 }
-int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
+SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
   int rc;
   Vdbe *p = (Vdbe *)pStmt;
+  sqlite3_mutex_enter(p->db->mutex);
   rc = vdbeUnbind(p, i);
   if( rc==SQLITE_OK ){
     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
   }
-  return rc;
-}
-int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
+  sqlite3_mutex_leave(p->db->mutex);
+  return rc;
+}
+SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
   return sqlite3_bind_int64(p, i, (i64)iValue);
 }
-int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
+SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
   int rc;
   Vdbe *p = (Vdbe *)pStmt;
+  sqlite3_mutex_enter(p->db->mutex);
   rc = vdbeUnbind(p, i);
   if( rc==SQLITE_OK ){
     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
   }
-  return rc;
-}
-int sqlite3_bind_null(sqlite3_stmt* p, int i){
-  return vdbeUnbind((Vdbe *)p, i);
-}
-int sqlite3_bind_text(
+  sqlite3_mutex_leave(p->db->mutex);
+  return rc;
+}
+SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
+  int rc;
+  Vdbe *p = (Vdbe*)pStmt;
+  sqlite3_mutex_enter(p->db->mutex);
+  rc = vdbeUnbind(p, i);
+  sqlite3_mutex_leave(p->db->mutex);
+  return rc;
+}
+SQLITE_API int sqlite3_bind_text(
   sqlite3_stmt *pStmt,
   int i,
   const char *zData,
   int nData,
@@ -33337,9 +36157,9 @@
 ){
   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
 }
 #ifndef SQLITE_OMIT_UTF16
-int sqlite3_bind_text16(
+SQLITE_API int sqlite3_bind_text16(
   sqlite3_stmt *pStmt,
   int i,
   const void *zData,
   int nData,
@@ -33347,32 +36167,36 @@
 ){
   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
 }
 #endif /* SQLITE_OMIT_UTF16 */
-int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
+SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
   int rc;
   Vdbe *p = (Vdbe *)pStmt;
+  sqlite3_mutex_enter(p->db->mutex);
   rc = vdbeUnbind(p, i);
   if( rc==SQLITE_OK ){
-    sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
-  }
-  return rc;
-}
-int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
+    rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue);
+  }
+  sqlite3_mutex_leave(p->db->mutex);
+  return rc;
+}
+SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
   int rc;
   Vdbe *p = (Vdbe *)pStmt;
+  sqlite3_mutex_enter(p->db->mutex);
   rc = vdbeUnbind(p, i);
   if( rc==SQLITE_OK ){
     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
   }
+  sqlite3_mutex_leave(p->db->mutex);
   return rc;
 }
 
 /*
 ** Return the number of wildcards that can be potentially bound to.
 ** This routine is added to support DBD::SQLite.
 */
-int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
+SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
   Vdbe *p = (Vdbe*)pStmt;
   return p ? p->nVar : 0;
 }
 
@@ -33382,17 +36206,21 @@
 ** exist.
 */
 static void createVarMap(Vdbe *p){
   if( !p->okVar ){
-    int j;
-    Op *pOp;
-    for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
-      if( pOp->opcode==OP_Variable ){
-        assert( pOp->p1>0 && pOp->p1<=p->nVar );
-        p->azVar[pOp->p1-1] = pOp->p3;
-      }
-    }
-    p->okVar = 1;
+    sqlite3_mutex_enter(p->db->mutex);
+    if( !p->okVar ){
+      int j;
+      Op *pOp;
+      for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){
+        if( pOp->opcode==OP_Variable ){
+          assert( pOp->p1>0 && pOp->p1<=p->nVar );
+          p->azVar[pOp->p1-1] = pOp->p3;
+        }
+      }
+      p->okVar = 1;
+    }
+    sqlite3_mutex_leave(p->db->mutex);
   }
 }
 
 /*
@@ -33400,9 +36228,9 @@
 ** is out of range or if the wildcard is unnamed.
 **
 ** The result is always UTF-8.
 */
-const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
+SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
   Vdbe *p = (Vdbe*)pStmt;
   if( p==0 || i<1 || i>p->nVar ){
     return 0;
   }
@@ -33414,9 +36242,9 @@
 ** Given a wildcard parameter name, return the index of the variable
 ** with that name.  If there is no variable with the given name,
 ** return 0.
 */
-int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
+SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
   Vdbe *p = (Vdbe*)pStmt;
   int i;
   if( p==0 ){
     return 0;
@@ -33437,24 +36265,27 @@
 ** Transfer all bindings from the first statement over to the second.
 ** If the two statements contain a different number of bindings, then
 ** an SQLITE_ERROR is returned.
 */
-int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
+SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
   Vdbe *pFrom = (Vdbe*)pFromStmt;
   Vdbe *pTo = (Vdbe*)pToStmt;
   int i, rc = SQLITE_OK;
   if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT)
-    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){
+    || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT)
+    || pTo->db!=pFrom->db ){
     return SQLITE_MISUSE;
   }
   if( pFrom->nVar!=pTo->nVar ){
     return SQLITE_ERROR;
   }
+  sqlite3_mutex_enter(pTo->db->mutex);
   for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){
     sqlite3MallocDisallow();
     rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
     sqlite3MallocAllow();
   }
+  sqlite3_mutex_leave(pTo->db->mutex);
   assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
   return rc;
 }
 
@@ -33463,9 +36294,9 @@
 ** in the argument belongs.  This is the same database handle that was
 ** the first argument to the sqlite3_prepare() that was used to create
 ** the statement in the first place.
 */
-sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
+SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
   return pStmt ? ((Vdbe*)pStmt)->db : 0;
 }
 
 /************** End of vdbeapi.c *********************************************/
@@ -33514,9 +36345,9 @@
 ** of the code in this file is, therefore, important.  See other comments
 ** in this file for details.  If in doubt, do not deviate from existing
 ** commenting and indentation practices when changing or adding code.
 **
-** $Id: vdbe.c,v 1.638 2007/07/22 19:10:21 drh Exp $
+** $Id: vdbe.c,v 1.650 2007/09/03 15:19:36 drh Exp $
 */
 
 /*
 ** The following global variable is incremented every time a cursor
@@ -33525,9 +36356,9 @@
 ** working correctly.  This variable has no function other than to
 ** help verify the correct operation of the library.
 */
 #ifdef SQLITE_TEST
-int sqlite3_search_count = 0;
+SQLITE_API int sqlite3_search_count = 0;
 #endif
 
 /*
 ** When this global variable is positive, it gets decremented once before
@@ -33537,9 +36368,9 @@
 ** This facility is used for testing purposes only.  It does not function
 ** in an ordinary build.
 */
 #ifdef SQLITE_TEST
-int sqlite3_interrupt_count = 0;
+SQLITE_API int sqlite3_interrupt_count = 0;
 #endif
 
 /*
 ** The next global variable is incremented each type the OP_Sort opcode
@@ -33548,9 +36379,9 @@
 ** has no function other than to help verify the correct operation of the
 ** library.
 */
 #ifdef SQLITE_TEST
-int sqlite3_sort_count = 0;
+SQLITE_API int sqlite3_sort_count = 0;
 #endif
 
 /*
 ** The next global variable records the size of the largest MEM_Blob
@@ -33559,9 +36390,9 @@
 ** is working correctly.   This variable has no function other than to
 ** help verify the correct operation of the library.
 */
 #ifdef SQLITE_TEST
-int sqlite3_max_blobsize = 0;
+SQLITE_API int sqlite3_max_blobsize = 0;
 #endif
 
 /*
 ** Release the memory associated with the given stack level.  This
@@ -33575,17 +36406,8 @@
 */
 #define Stringify(P, enc) \
    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
      { goto no_mem; }
-
-/*
-** Convert the given stack entity into a string that has been obtained
-** from sqliteMalloc().  This is different from Stringify() above in that
-** Stringify() will use the NBFS bytes of static string space if the string
-** will fit but this routine always mallocs for space.
-** Return non-zero if we run out of memory.
-*/
-#define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P)
 
 /*
 ** The header of a record consists of a sequence variable-length integers.
 ** These integers are almost always small and are encoded as a single byte.
@@ -33672,9 +36494,9 @@
   assert( iCur<p->nCursor );
   if( p->apCsr[iCur] ){
     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   }
-  p->apCsr[iCur] = pCx = sqliteMalloc( sizeof(Cursor) );
+  p->apCsr[iCur] = pCx = sqlite3MallocZero( sizeof(Cursor) );
   if( pCx ){
     pCx->iDb = iDb;
   }
   return pCx;
@@ -33722,9 +36544,13 @@
 **
 ** SQLITE_AFF_NONE:
 **    No-op.  pRec is unchanged.
 */
-static void applyAffinity(Mem *pRec, char affinity, u8 enc){
+static void applyAffinity(
+  Mem *pRec,          /* The value to apply affinity to */
+  char affinity,      /* The affinity to be applied */
+  u8 enc              /* Use this text encoding */
+){
   if( affinity==SQLITE_AFF_TEXT ){
     /* Only attempt the conversion to TEXT if there is an integer or real
     ** representation (blob and NULL do not get converted) but no string
     ** representation.
@@ -33750,9 +36576,9 @@
 ** loss of information and return the revised type of the argument.
 **
 ** This is an EXPERIMENTAL api and is subject to change or removal.
 */
-int sqlite3_value_numeric_type(sqlite3_value *pVal){
+SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
   Mem *pMem = (Mem*)pVal;
   applyNumericAffinity(pMem);
   storeTypeInfo(pMem, 0);
   return pMem->type;
@@ -33761,9 +36587,13 @@
 /*
 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
 ** not the internal Mem* type.
 */
-SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *pVal, u8 affinity, u8 enc){
+SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
+  sqlite3_value *pVal,
+  u8 affinity,
+  u8 enc
+){
   applyAffinity((Mem *)pVal, affinity, enc);
 }
 
 #ifdef SQLITE_DEBUG
@@ -33895,9 +36725,9 @@
 ** will either invoke the busy callback (if there is one) or it will
 ** return SQLITE_BUSY.
 **
 ** If an error occurs, an error message is written to memory obtained
-** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
+** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
 **
 ** If the callback ever returns non-zero, then the program exits
 ** immediately.  There will be no error message but the p->rc field is
@@ -33933,8 +36763,9 @@
 
   if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
   assert( db->magic==SQLITE_MAGIC_BUSY );
   pTos = p->pTos;
+  sqlite3BtreeMutexArrayEnter(&p->aMutex);
   if( p->rc==SQLITE_NOMEM ){
     /* This happens if a malloc() inside a call to sqlite3_column_text() or
     ** sqlite3_column_text16() failed.  */
     goto no_mem;
@@ -33951,9 +36782,9 @@
   CHECK_FOR_INTERRUPT;
   sqlite3VdbeIOTraceSql(p);
 #ifdef SQLITE_DEBUG
   if( (p->db->flags & SQLITE_VdbeListing)!=0
-    || sqlite3OsFileExists("vdbe_explain")
+    || sqlite3OsAccess(db->pVfs, "vdbe_explain", SQLITE_ACCESS_EXISTS)
   ){
     int i;
     printf("VDBE Program Listing:\n");
     sqlite3VdbePrintSql(p);
@@ -33960,16 +36791,16 @@
     for(i=0; i<p->nOp; i++){
       sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
     }
   }
-  if( sqlite3OsFileExists("vdbe_trace") ){
+  if( sqlite3OsAccess(db->pVfs, "vdbe_trace", SQLITE_ACCESS_EXISTS) ){
     p->trace = stdout;
   }
 #endif
   for(pc=p->pc; rc==SQLITE_OK; pc++){
     assert( pc>=0 && pc<p->nOp );
     assert( pTos<=&p->aStack[pc] );
-    if( sqlite3MallocFailed() ) goto no_mem;
+    if( db->mallocFailed ) goto no_mem;
 #ifdef VDBE_PROFILE
     origPc = pc;
     start = hwtime();
 #endif
@@ -33984,9 +36815,10 @@
         sqlite3VdbePrintSql(p);
       }
       sqlite3VdbePrintOp(p->trace, pc, pOp);
     }
-    if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){
+    if( p->trace==0 && pc==0
+     && sqlite3OsAccess(db->pVfs, "vdbe_sqltrace", SQLITE_ACCESS_EXISTS) ){
       sqlite3VdbePrintSql(p);
     }
 #endif
 
@@ -34152,12 +36984,13 @@
   }
   rc = sqlite3VdbeHalt(p);
   assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
   if( rc==SQLITE_BUSY ){
-    p->rc = SQLITE_BUSY;
-    return SQLITE_BUSY;
-  }
-  return p->rc ? SQLITE_ERROR : SQLITE_DONE;
+    p->rc = rc = SQLITE_BUSY;
+  }else{
+    rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
+  }
+  goto vdbe_return;
 }
 
 /* Opcode: Integer P1 * *
 **
@@ -34223,9 +37056,9 @@
     if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem;
     pTos->flags &= ~(MEM_Dyn);
     pTos->flags |= MEM_Static;
     if( pOp->p3type==P3_DYNAMIC ){
-      sqliteFree(pOp->p3);
+      sqlite3_free(pOp->p3);
     }
     pOp->p3type = P3_DYNAMIC;
     pOp->p3 = pTos->z;
     pOp->p1 = pTos->n;
@@ -34277,18 +37110,18 @@
   pOp->p1 = strlen(pOp->p3)/2;
   assert( SQLITE_MAX_SQL_LENGTH < SQLITE_MAX_LENGTH );
   assert( pOp->p1 < SQLITE_MAX_LENGTH );
   if( pOp->p1 ){
-    char *zBlob = sqlite3HexToBlob(pOp->p3);
+    char *zBlob = sqlite3HexToBlob(db, pOp->p3);
     if( !zBlob ) goto no_mem;
     if( pOp->p3type==P3_DYNAMIC ){
-      sqliteFree(pOp->p3);
+      sqlite3_free(pOp->p3);
     }
     pOp->p3 = zBlob;
     pOp->p3type = P3_DYNAMIC;
   }else{
     if( pOp->p3type==P3_DYNAMIC ){
-      sqliteFree(pOp->p3);
+      sqlite3_free(pOp->p3);
     }
     pOp->p3type = P3_STATIC;
     pOp->p3 = "";
   }
@@ -34470,9 +37303,10 @@
   p->nCallback++;
   p->popStack = pOp->p1;
   p->pc = pc + 1;
   p->pTos = pTos;
-  return SQLITE_ROW;
+  rc = SQLITE_ROW;
+  goto vdbe_return;
 }
 
 /* Opcode: Concat P1 P2 *
 **
@@ -34481,9 +37315,9 @@
 ** are popped from the stack if P2==0 and retained if P2==1.  If
 ** any element of the stack is NULL, then the result is NULL.
 **
 ** When P1==1, this routine makes a copy of the top stack element
-** into memory obtained from sqliteMalloc().
+** into memory obtained from sqlite3_malloc().
 */
 case OP_Concat: {           /* same as TK_CONCAT */
   char *zNew;
   i64 nByte;
@@ -34522,9 +37356,9 @@
     */
     if( nByte+2>SQLITE_MAX_LENGTH ){
       goto too_big;
     }
-    zNew = sqliteMallocRaw( nByte+2 );
+    zNew = sqlite3DbMallocRaw(db, nByte+2 );
     if( zNew==0 ) goto no_mem;
     j = 0;
     pTerm = &pTos[1-nField];
     for(i=j=0; i<nField; i++, pTerm++){
@@ -34745,8 +37579,9 @@
 
   ctx.s.flags = MEM_Null;
   ctx.s.z = 0;
   ctx.s.xDel = 0;
+  ctx.s.db = db;
   ctx.isError = 0;
   if( ctx.pFunc->needCollSeq ){
     assert( pOp>p->aOp );
     assert( pOp[-1].p3type==P3_COLLSEQ );
@@ -34755,9 +37590,21 @@
   }
   if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
   (*ctx.pFunc->xFunc)(&ctx, n, apVal);
   if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
-  if( sqlite3MallocFailed() ) goto no_mem;
+  if( db->mallocFailed ){
+    /* Even though a malloc() has failed, the implementation of the
+    ** user function may have called an sqlite3_result_XXX() function
+    ** to return a value. The following call releases any resources
+    ** associated with such a value.
+    **
+    ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
+    ** fails also (the if(...) statement above). But if people are
+    ** misusing sqlite, they have bigger problems than a leaked value.
+    */
+    sqlite3VdbeMemRelease(&ctx.s);
+    goto no_mem;
+  }
   popStack(&pTos, n);
 
   /* If any auxilary data functions have been called by this user function,
   ** immediately call the destructor for any non-static values.
@@ -35529,9 +38376,9 @@
     int avail;       /* Number of bytes of available data */
 
     aType = pC->aType;
     if( aType==0 ){
-      pC->aType = aType = sqliteMallocRaw( 2*nField*sizeof(aType) );
+      pC->aType = aType = sqlite3DbMallocRaw(db, 2*nField*sizeof(aType) );
     }
     if( aType==0 ){
       goto no_mem;
     }
@@ -35625,9 +38472,9 @@
     if( zRec ){
       zData = &zRec[aOffset[p2]];
     }else{
       len = sqlite3VdbeSerialTypeLen(aType[p2]);
-      rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,&sMem);
+      rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
       if( rc!=SQLITE_OK ){
         goto op_column_out;
       }
       zData = sMem.z;
@@ -35800,9 +38647,9 @@
   }
 
   /* Allocate space for the new record. */
   if( nByte>sizeof(zTemp) ){
-    zNewRecord = sqliteMallocRaw(nByte);
+    zNewRecord = sqlite3DbMallocRaw(db, nByte);
     if( !zNewRecord ){
       goto no_mem;
     }
   }else{
@@ -35872,8 +38719,9 @@
   int i = pOp->p1;
   Btree *pBt;
   if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0 && !(db->autoCommit) ){
     assert( sqlite3BtreeIsInTrans(pBt) );
+    assert( (p->btreeMask & (1<<i))!=0 );
     if( !sqlite3BtreeIsInStmt(pBt) ){
       rc = sqlite3BtreeBeginStmt(pBt);
       p->openedStatement = 1;
     }
@@ -35916,17 +38764,18 @@
       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
         p->pTos = pTos;
         p->pc = pc;
         db->autoCommit = 1-i;
-        p->rc = SQLITE_BUSY;
-        return SQLITE_BUSY;
+        p->rc = rc = SQLITE_BUSY;
+        goto vdbe_return;
       }
     }
     if( p->rc==SQLITE_OK ){
-      return SQLITE_DONE;
-    }else{
-      return SQLITE_ERROR;
-    }
+      rc = SQLITE_DONE;
+    }else{
+      rc = SQLITE_ERROR;
+    }
+    goto vdbe_return;
   }else{
     sqlite3SetString(&p->zErrMsg,
         (!i)?"cannot start a transaction within a transaction":(
         (rollback)?"cannot rollback - no transaction is active":
@@ -35961,17 +38810,18 @@
   int i = pOp->p1;
   Btree *pBt;
 
   assert( i>=0 && i<db->nDb );
+  assert( (p->btreeMask & (1<<i))!=0 );
   pBt = db->aDb[i].pBt;
 
   if( pBt ){
     rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
     if( rc==SQLITE_BUSY ){
       p->pc = pc;
-      p->rc = SQLITE_BUSY;
+      p->rc = rc = SQLITE_BUSY;
       p->pTos = pTos;
-      return SQLITE_BUSY;
+      goto vdbe_return;
     }
     if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
       goto abort_due_to_error;
     }
@@ -36007,8 +38857,9 @@
     iCookie *= -1;
   }
   assert( iDb>=0 && iDb<db->nDb );
   assert( db->aDb[iDb].pBt!=0 );
+  assert( (p->btreeMask & (1<<iDb))!=0 );
   /* The indexing of meta values at the schema layer is off by one from
   ** the indexing in the btree layer.  The btree considers meta[0] to
   ** be the number of free pages in the database (a read-only value)
   ** and meta[1] to be the schema cookie.  The schema layer considers
@@ -36035,8 +38886,9 @@
 case OP_SetCookie: {       /* no-push */
   Db *pDb;
   assert( pOp->p2<SQLITE_N_BTREE_META );
   assert( pOp->p1>=0 && pOp->p1<db->nDb );
+  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
   pDb = &db->aDb[pOp->p1];
   assert( pDb->pBt!=0 );
   assert( pTos>=p->aStack );
   sqlite3VdbeMemIntegerify(pTos);
@@ -36079,8 +38931,9 @@
 case OP_VerifyCookie: {       /* no-push */
   int iMeta;
   Btree *pBt;
   assert( pOp->p1>=0 && pOp->p1<db->nDb );
+  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
   pBt = db->aDb[pOp->p1].pBt;
   if( pBt ){
     rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
   }else{
@@ -36087,9 +38940,10 @@
     rc = SQLITE_OK;
     iMeta = 0;
   }
   if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
-    sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0);
+    sqlite3_free(p->zErrMsg);
+    p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
     /* If the schema-cookie from the database file matches the cookie
     ** stored with the in-memory representation of the schema, do
     ** not reload the schema from the database file.
     **
@@ -36169,8 +39023,9 @@
   iDb = pTos->u.i;
   assert( (pTos->flags & MEM_Dyn)==0 );
   pTos--;
   assert( iDb>=0 && iDb<db->nDb );
+  assert( (p->btreeMask & (1<<iDb))!=0 );
   pDb = &db->aDb[iDb];
   pX = pDb->pBt;
   assert( pX!=0 );
   if( pOp->opcode==OP_OpenWrite ){
@@ -36209,11 +39064,11 @@
   }
   switch( rc ){
     case SQLITE_BUSY: {
       p->pc = pc;
-      p->rc = SQLITE_BUSY;
+      p->rc = rc = SQLITE_BUSY;
       p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
-      return SQLITE_BUSY;
+      goto vdbe_return;
     }
     case SQLITE_OK: {
       int flags = sqlite3BtreeFlags(pCur->pCursor);
       /* Sanity checking.  Only the lower four bits of the flags byte should
@@ -36272,13 +39127,21 @@
 */
 case OP_OpenEphemeral: {       /* no-push */
   int i = pOp->p1;
   Cursor *pCx;
+  static const int openFlags =
+      SQLITE_OPEN_READWRITE |
+      SQLITE_OPEN_CREATE |
+      SQLITE_OPEN_EXCLUSIVE |
+      SQLITE_OPEN_DELETEONCLOSE |
+      SQLITE_OPEN_TRANSIENT_DB;
+
   assert( i>=0 );
   pCx = allocateCursor(p, i, -1);
   if( pCx==0 ) goto no_mem;
   pCx->nullRow = 1;
-  rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, &pCx->pBt);
+  rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
+                           &pCx->pBt);
   if( rc==SQLITE_OK ){
     rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
   }
   if( rc==SQLITE_OK ){
@@ -36530,14 +39393,17 @@
   assert( pTos>=p->aStack );
   assert( i>=0 && i<p->nCursor );
   assert( p->apCsr[i]!=0 );
   if( (pC = p->apCsr[i])->pCursor!=0 ){
-    int res, rx;
+    int res;
     assert( pC->isTable==0 );
     assert( pTos->flags & MEM_Blob );
     Stringify(pTos, encoding);
-    rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
-    alreadyExists = rx==SQLITE_OK && res==0;
+    rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
+    if( rc!=SQLITE_OK ){
+      break;
+    }
+    alreadyExists = (res==0);
     pC->deferredMoveto = 0;
     pC->cacheStatus = CACHE_STALE;
   }
   if( pOp->opcode==OP_Found ){
@@ -36922,16 +39788,16 @@
     }else{
       assert( pTos->flags & (MEM_Blob|MEM_Str) );
     }
     if( pC->pseudoTable ){
-      sqliteFree(pC->pData);
+      sqlite3_free(pC->pData);
       pC->iKey = iKey;
       pC->nData = pTos->n;
       if( pTos->flags & MEM_Dyn ){
         pC->pData = pTos->z;
         pTos->flags = MEM_Null;
       }else{
-        pC->pData = sqliteMallocRaw( pC->nData+2 );
+        pC->pData = sqlite3_malloc( pC->nData+2 );
         if( !pC->pData ) goto no_mem;
         memcpy(pC->pData, pTos->z, pC->nData);
         pC->pData[pC->nData] = 0;
         pC->pData[pC->nData+1] = 0;
@@ -37098,9 +39964,9 @@
     if( n<=NBFS ){
       pTos->flags = MEM_Blob | MEM_Short;
       pTos->z = pTos->zShort;
     }else{
-      char *z = sqliteMallocRaw( n );
+      char *z = sqlite3_malloc( n );
       if( z==0 ) goto no_mem;
       pTos->flags = MEM_Blob | MEM_Dyn;
       pTos->xDel = 0;
       pTos->z = z;
@@ -37524,8 +40390,9 @@
   if( iCnt>1 ){
     rc = SQLITE_LOCKED;
   }else{
     assert( iCnt==1 );
+    assert( (p->btreeMask & (1<<pOp->p2))!=0 );
     rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved);
     pTos++;
     pTos->flags = MEM_Int;
     pTos->u.i = iMoved;
@@ -37585,8 +40452,9 @@
       goto abort_due_to_error;
     }
   }
 #endif
+  assert( (p->btreeMask & (1<<pOp->p2))!=0 );
   rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
   break;
 }
 
@@ -37615,8 +40483,9 @@
   int pgno;
   int flags;
   Db *pDb;
   assert( pOp->p1>=0 && pOp->p1<db->nDb );
+  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
   pDb = &db->aDb[pOp->p1];
   assert( pDb->pBt!=0 );
   if( pOp->opcode==OP_CreateTable ){
     /* flags = BTREE_INTKEY; */
@@ -37660,23 +40529,22 @@
   zMaster = SCHEMA_TABLE(iDb);
   initData.db = db;
   initData.iDb = pOp->p1;
   initData.pzErrMsg = &p->zErrMsg;
-  zSql = sqlite3MPrintf(
+  zSql = sqlite3MPrintf(db,
      "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
      db->aDb[iDb].zName, zMaster, pOp->p3);
   if( zSql==0 ) goto no_mem;
   sqlite3SafetyOff(db);
   assert( db->init.busy==0 );
   db->init.busy = 1;
-  assert( !sqlite3MallocFailed() );
+  assert( !db->mallocFailed );
   rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
   if( rc==SQLITE_ABORT ) rc = initData.rc;
-  sqliteFree(zSql);
+  sqlite3_free(zSql);
   db->init.busy = 0;
   sqlite3SafetyOn(db);
   if( rc==SQLITE_NOMEM ){
-    sqlite3FailedMalloc();
     goto no_mem;
   }
   break;
 }
@@ -37765,9 +40633,9 @@
   for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){
     if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
   }
   assert( nRoot>0 );
-  aRoot = sqliteMallocRaw( sizeof(int)*(nRoot+1) );
+  aRoot = sqlite3_malloc( sizeof(int)*(nRoot+1) );
   if( aRoot==0 ) goto no_mem;
   j = pOp->p1;
   assert( j>=0 && j<p->nMem );
   pnErr = &p->aMem[j];
@@ -37777,8 +40645,10 @@
   }
   aRoot[j] = 0;
   popStack(&pTos, nRoot);
   pTos++;
+  assert( pOp->p2>=0 && pOp->p2<db->nDb );
+  assert( (p->btreeMask & (1<<pOp->p2))!=0 );
   z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot,
                                  pnErr->u.i, &nErr);
   pnErr->u.i -= nErr;
   if( nErr==0 ){
@@ -37791,9 +40661,9 @@
     pTos->xDel = 0;
   }
   pTos->enc = SQLITE_UTF8;
   sqlite3VdbeChangeEncoding(pTos, encoding);
-  sqliteFree(aRoot);
+  sqlite3_free(aRoot);
   break;
 }
 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
 
@@ -37804,9 +40674,11 @@
 */
 case OP_FifoWrite: {        /* no-push */
   assert( pTos>=p->aStack );
   sqlite3VdbeMemIntegerify(pTos);
-  sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i);
+  if( sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i)==SQLITE_NOMEM ){
+    goto no_mem;
+  }
   assert( (pTos->flags & MEM_Dyn)==0 );
   pTos--;
   break;
 }
@@ -37844,9 +40716,9 @@
   assert( i>=0 );
   /* FIX ME: This should be allocated as part of the vdbe at compile-time */
   if( i>=p->contextStackDepth ){
     p->contextStackDepth = i+1;
-    p->contextStack = sqliteReallocOrFree(p->contextStack,
+    p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
                                           sizeof(Context)*(i+1));
     if( p->contextStack==0 ) goto no_mem;
   }
   pContext = &p->contextStack[i];
@@ -38076,8 +40948,9 @@
   pMem->n++;
   ctx.s.flags = MEM_Null;
   ctx.s.z = 0;
   ctx.s.xDel = 0;
+  ctx.s.db = db;
   ctx.isError = 0;
   ctx.pColl = 0;
   if( ctx.pFunc->needCollSeq ){
     assert( pOp>p->aOp );
@@ -38148,8 +41021,9 @@
 case OP_IncrVacuum: {        /* no-push */
   Btree *pBt;
 
   assert( pOp->p1>=0 && pOp->p1<db->nDb );
+  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
   pBt = db->aDb[pOp->p1].pBt;
   rc = sqlite3BtreeIncrVacuum(pBt);
   if( rc==SQLITE_DONE ){
     pc = pOp->p2 - 1;
@@ -38199,8 +41073,10 @@
   u8 isWriteLock = (p1<0);
   if( isWriteLock ){
     p1 = (-1*p1)-1;
   }
+  assert( p1>=0 && p1<db->nDb );
+  assert( (p->btreeMask & (1<<p1))!=0 );
   rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
   if( rc==SQLITE_LOCKED ){
     const char *z = (const char *)pOp->p3;
     sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0);
@@ -38274,8 +41150,9 @@
     if( pCur ){
       pCur->pVtabCursor = pVtabCursor;
       pCur->pModule = pVtabCursor->pVtab->pModule;
     }else{
+      db->mallocFailed = 1;
       pModule->xClose(pVtabCursor);
     }
   }
   break;
@@ -38398,8 +41275,9 @@
   } else {
     sqlite3_context sContext;
     memset(&sContext, 0, sizeof(sContext));
     sContext.s.flags = MEM_Null;
+    sContext.s.db = db;
     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
     rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
 
     /* Copy the result of the function to the top of the stack. We
@@ -38598,8 +41476,9 @@
     ** of the stack is set to 0. This is technically invalid for a memory
     ** cell, so avoid calling MemSanity() in this case.
     */
     if( pTos>=p->aStack && pTos->flags ){
+      assert( pTos->db==db );
       sqlite3VdbeMemSanity(pTos);
       assert( !sqlite3VdbeMemTooBig(pTos) );
     }
     assert( pc>=-1 && pc<p->nOp );
@@ -38642,8 +41521,14 @@
     rc = SQLITE_DONE;
   }
   sqlite3VdbeHalt(p);
   p->pTos = pTos;
+
+  /* This is the only way out of this procedure.  We have to
+  ** release the mutexes on btrees that were acquired at the
+  ** top. */
+vdbe_return:
+  sqlite3BtreeMutexArrayLeave(&p->aMutex);
   return rc;
 
   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
   ** is encountered.
@@ -38655,8 +41540,9 @@
 
   /* Jump to here if a malloc() fails.
   */
 no_mem:
+  db->mallocFailed = 1;
   sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
   rc = SQLITE_NOMEM;
   goto vdbe_halt;
 
@@ -38670,9 +41556,9 @@
   ** should hold the error number.
   */
 abort_due_to_error:
   if( p->zErrMsg==0 ){
-    if( sqlite3MallocFailed() ) rc = SQLITE_NOMEM;
+    if( db->mallocFailed ) rc = SQLITE_NOMEM;
     sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
   }
   goto vdbe_halt;
 
@@ -38706,9 +41592,9 @@
 *************************************************************************
 **
 ** This file contains code used to implement incremental BLOB I/O.
 **
-** $Id: vdbeblob.c,v 1.11 2007/06/27 00:36:14 drh Exp $
+** $Id: vdbeblob.c,v 1.16 2007/08/30 01:19:59 drh Exp $
 */
 
 
 #ifndef SQLITE_OMIT_INCRBLOB
@@ -38722,14 +41608,15 @@
   int nByte;              /* Size of open blob, in bytes */
   int iOffset;            /* Byte offset of blob in cursor data */
   BtCursor *pCsr;         /* Cursor pointing at blob row */
   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
+  sqlite3 *db;            /* The associated database */
 };
 
 /*
 ** Open a blob handle.
 */
-int sqlite3_blob_open(
+SQLITE_API int sqlite3_blob_open(
   sqlite3* db,            /* The database connection */
   const char *zDb,        /* The attached database containing the blob */
   const char *zTable,     /* The table containing the blob */
   const char *zColumn,    /* The column containing the blob */
@@ -38779,8 +41666,9 @@
   int rc = SQLITE_OK;
   char zErr[128];
 
   zErr[0] = 0;
+  sqlite3_mutex_enter(db->mutex);
   do {
     Parse sParse;
     Table *pTab;
 
@@ -38788,19 +41676,22 @@
     sParse.db = db;
 
     rc = sqlite3SafetyOn(db);
     if( rc!=SQLITE_OK ){
+      sqlite3_mutex_leave(db->mutex);
       return rc;
     }
 
+    sqlite3BtreeEnterAll(db);
     pTab = sqlite3LocateTable(&sParse, zTable, zDb);
     if( !pTab ){
       if( sParse.zErrMsg ){
         sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg);
       }
-      sqliteFree(sParse.zErrMsg);
+      sqlite3_free(sParse.zErrMsg);
       rc = SQLITE_ERROR;
       sqlite3SafetyOff(db);
+      sqlite3BtreeLeaveAll(db);
       goto blob_open_out;
     }
 
     /* Now search pTab for the exact column. */
@@ -38812,8 +41703,9 @@
     if( iCol==pTab->nCol ){
       sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn);
       rc = SQLITE_ERROR;
       sqlite3SafetyOff(db);
+      sqlite3BtreeLeaveAll(db);
       goto blob_open_out;
     }
 
     /* If the value is being opened for writing, check that the
@@ -38829,8 +41721,9 @@
             sqlite3_snprintf(sizeof(zErr), zErr,
                              "cannot open indexed column for writing");
             rc = SQLITE_ERROR;
             sqlite3SafetyOff(db);
+            sqlite3BtreeLeaveAll(db);
             goto blob_open_out;
           }
         }
       }
@@ -38848,8 +41741,11 @@
       /* Configure the OP_VerifyCookie */
       sqlite3VdbeChangeP1(v, 1, iDb);
       sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
 
+      /* Make sure a mutex is held on the table to be accessed */
+      sqlite3VdbeUsesBtree(v, iDb);
+
       /* Configure the db number pushed onto the stack */
       sqlite3VdbeChangeP1(v, 2, iDb);
 
       /* Remove either the OP_OpenWrite or OpenRead. Set the P2
@@ -38865,15 +41761,16 @@
       ** we can invoke OP_Column to fill in the vdbe cursors type
       ** and offset cache without causing any IO.
       */
       sqlite3VdbeChangeP2(v, 5, pTab->nCol+1);
-      if( !sqlite3MallocFailed() ){
+      if( !db->mallocFailed ){
         sqlite3VdbeMakeReady(v, 1, 0, 1, 0);
       }
     }
 
+    sqlite3BtreeLeaveAll(db);
     rc = sqlite3SafetyOff(db);
-    if( rc!=SQLITE_OK || sqlite3MallocFailed() ){
+    if( rc!=SQLITE_OK || db->mallocFailed ){
       goto blob_open_out;
     }
 
     sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow);
@@ -38900,19 +41797,22 @@
       );
       rc = SQLITE_ERROR;
       goto blob_open_out;
     }
-    pBlob = (Incrblob *)sqliteMalloc(sizeof(Incrblob));
-    if( sqlite3MallocFailed() ){
-      sqliteFree(pBlob);
+    pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
+    if( db->mallocFailed ){
+      sqlite3_free(pBlob);
       goto blob_open_out;
     }
     pBlob->flags = flags;
     pBlob->pCsr =  v->apCsr[0]->pCursor;
+    sqlite3BtreeEnterCursor(pBlob->pCsr);
     sqlite3BtreeCacheOverflow(pBlob->pCsr);
+    sqlite3BtreeLeaveCursor(pBlob->pCsr);
     pBlob->pStmt = (sqlite3_stmt *)v;
     pBlob->iOffset = v->apCsr[0]->aOffset[iCol];
     pBlob->nByte = sqlite3VdbeSerialTypeLen(type);
+    pBlob->db = db;
     *ppBlob = (sqlite3_blob *)pBlob;
     rc = SQLITE_OK;
   }else if( rc==SQLITE_OK ){
     sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow);
@@ -38920,27 +41820,33 @@
   }
 
 blob_open_out:
   zErr[sizeof(zErr)-1] = '\0';
-  if( rc!=SQLITE_OK || sqlite3MallocFailed() ){
+  if( rc!=SQLITE_OK || db->mallocFailed ){
     sqlite3_finalize((sqlite3_stmt *)v);
   }
   sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr));
-  return sqlite3ApiExit(db, rc);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 /*
 ** Close a blob handle that was previously created using
 ** sqlite3_blob_open().
 */
-int sqlite3_blob_close(sqlite3_blob *pBlob){
+SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
   Incrblob *p = (Incrblob *)pBlob;
-  sqlite3_stmt *pStmt = p->pStmt;
-  sqliteFree(p);
-  return sqlite3_finalize(pStmt);
-}
-
-
+  int rc;
+
+  rc = sqlite3_finalize(p->pStmt);
+  sqlite3_free(p);
+  return rc;
+}
+
+/*
+** Perform a read or write operation on a blob
+*/
 static int blobReadWrite(
   sqlite3_blob *pBlob,
   void *z,
   int n,
@@ -38948,62 +41854,312 @@
   int (*xCall)(BtCursor*, u32, u32, void*)
 ){
   int rc;
   Incrblob *p = (Incrblob *)pBlob;
-  Vdbe *v = (Vdbe *)(p->pStmt);
-  sqlite3 *db;
-
-  /* If there is no statement handle, then the blob-handle has
-  ** already been invalidated. Return SQLITE_ABORT in this case.
-  */
-  if( !v ) return SQLITE_ABORT;
+  Vdbe *v;
+  sqlite3 *db = p->db;
 
   /* Request is out of range. Return a transient error. */
   if( (iOffset+n)>p->nByte ){
     return SQLITE_ERROR;
   }
-
-  /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
-  ** returned, clean-up the statement handle.
-  */
-  db = v->db;
-  rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
-  if( rc==SQLITE_ABORT ){
-    sqlite3VdbeFinalize(v);
-    p->pStmt = 0;
-  }else{
-    db->errCode = rc;
-    v->rc = rc;
-  }
-
-  return sqlite3ApiExit(db, rc);
+  sqlite3_mutex_enter(db->mutex);
+
+  /* If there is no statement handle, then the blob-handle has
+  ** already been invalidated. Return SQLITE_ABORT in this case.
+  */
+  v = (Vdbe*)p->pStmt;
+  if( v==0 ){
+    rc = SQLITE_ABORT;
+  }else{
+    /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
+    ** returned, clean-up the statement handle.
+    */
+    assert( db == v->db );
+    sqlite3BtreeEnterCursor(p->pCsr);
+    rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
+    sqlite3BtreeLeaveCursor(p->pCsr);
+    if( rc==SQLITE_ABORT ){
+      sqlite3VdbeFinalize(v);
+      p->pStmt = 0;
+    }else{
+      db->errCode = rc;
+      v->rc = rc;
+    }
+  }
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 /*
 ** Read data from a blob handle.
 */
-int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
+SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
 }
 
 /*
 ** Write data to a blob handle.
 */
-int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
+SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
 }
 
 /*
 ** Query a blob handle for the size of the data.
-*/
-int sqlite3_blob_bytes(sqlite3_blob *pBlob){
+**
+** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
+** so no mutex is required for access.
+*/
+SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
   Incrblob *p = (Incrblob *)pBlob;
   return p->nByte;
 }
 
 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
 
 /************** End of vdbeblob.c ********************************************/
+/************** Begin file journal.c *****************************************/
+/*
+** 2007 August 22
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+**
+** @(#) $Id: journal.c,v 1.7 2007/09/06 13:49:37 drh Exp $
+*/
+
+#ifdef SQLITE_ENABLE_ATOMIC_WRITE
+
+/*
+** This file implements a special kind of sqlite3_file object used
+** by SQLite to create journal files if the atomic-write optimization
+** is enabled.
+**
+** The distinctive characteristic of this sqlite3_file is that the
+** actual on disk file is created lazily. When the file is created,
+** the caller specifies a buffer size for an in-memory buffer to
+** be used to service read() and write() requests. The actual file
+** on disk is not created or populated until either:
+**
+**   1) The in-memory representation grows too large for the allocated
+**      buffer, or
+**   2) The xSync() method is called.
+*/
+
+
+
+/*
+** A JournalFile object is a subclass of sqlite3_file used by
+** as an open file handle for journal files.
+*/
+struct JournalFile {
+  sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
+  int nBuf;                       /* Size of zBuf[] in bytes */
+  char *zBuf;                     /* Space to buffer journal writes */
+  int iSize;                      /* Amount of zBuf[] currently used */
+  int flags;                      /* xOpen flags */
+  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
+  sqlite3_file *pReal;            /* The "real" underlying file descriptor */
+  const char *zJournal;           /* Name of the journal file */
+};
+typedef struct JournalFile JournalFile;
+
+/*
+** If it does not already exists, create and populate the on-disk file
+** for JournalFile p.
+*/
+static int createFile(JournalFile *p){
+  int rc = SQLITE_OK;
+  if( !p->pReal ){
+    sqlite3_file *pReal = (sqlite3_file *)&p[1];
+    rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
+    if( rc==SQLITE_OK ){
+      p->pReal = pReal;
+      if( p->iSize>0 ){
+        assert(p->iSize<=p->nBuf);
+        rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
+      }
+    }
+  }
+  return rc;
+}
+
+/*
+** Close the file.
+*/
+static int jrnlClose(sqlite3_file *pJfd){
+  JournalFile *p = (JournalFile *)pJfd;
+  if( p->pReal ){
+    sqlite3OsClose(p->pReal);
+  }
+  sqlite3_free(p->zBuf);
+  return SQLITE_OK;
+}
+
+/*
+** Read data from the file.
+*/
+static int jrnlRead(
+  sqlite3_file *pJfd,    /* The journal file from which to read */
+  void *zBuf,            /* Put the results here */
+  int iAmt,              /* Number of bytes to read */
+  sqlite_int64 iOfst     /* Begin reading at this offset */
+){
+  int rc = SQLITE_OK;
+  JournalFile *p = (JournalFile *)pJfd;
+  if( p->pReal ){
+    rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
+  }else{
+    assert( iAmt+iOfst<=p->iSize );
+    memcpy(zBuf, &p->zBuf[iOfst], iAmt);
+  }
+  return rc;
+}
+
+/*
+** Write data to the file.
+*/
+static int jrnlWrite(
+  sqlite3_file *pJfd,    /* The journal file into which to write */
+  const void *zBuf,      /* Take data to be written from here */
+  int iAmt,              /* Number of bytes to write */
+  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
+){
+  int rc = SQLITE_OK;
+  JournalFile *p = (JournalFile *)pJfd;
+  if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
+    rc = createFile(p);
+  }
+  if( rc==SQLITE_OK ){
+    if( p->pReal ){
+      rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
+    }else{
+      memcpy(&p->zBuf[iOfst], zBuf, iAmt);
+      if( p->iSize<(iOfst+iAmt) ){
+        p->iSize = (iOfst+iAmt);
+      }
+    }
+  }
+  return rc;
+}
+
+/*
+** Truncate the file.
+*/
+static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
+  int rc = SQLITE_OK;
+  JournalFile *p = (JournalFile *)pJfd;
+  if( p->pReal ){
+    rc = sqlite3OsTruncate(p->pReal, size);
+  }else if( size<p->iSize ){
+    p->iSize = size;
+  }
+  return rc;
+}
+
+/*
+** Sync the file.
+*/
+static int jrnlSync(sqlite3_file *pJfd, int flags){
+  int rc;
+  JournalFile *p = (JournalFile *)pJfd;
+  rc = createFile(p);
+  if( rc==SQLITE_OK ){
+    rc = sqlite3OsSync(p->pReal, flags);
+  }
+  return rc;
+}
+
+/*
+** Query the size of the file in bytes.
+*/
+static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
+  int rc = SQLITE_OK;
+  JournalFile *p = (JournalFile *)pJfd;
+  if( p->pReal ){
+    rc = sqlite3OsFileSize(p->pReal, pSize);
+  }else{
+    *pSize = (sqlite_int64) p->iSize;
+  }
+  return rc;
+}
+
+/*
+** Table of methods for JournalFile sqlite3_file object.
+*/
+static struct sqlite3_io_methods JournalFileMethods = {
+  1,             /* iVersion */
+  jrnlClose,     /* xClose */
+  jrnlRead,      /* xRead */
+  jrnlWrite,     /* xWrite */
+  jrnlTruncate,  /* xTruncate */
+  jrnlSync,      /* xSync */
+  jrnlFileSize,  /* xFileSize */
+  0,             /* xLock */
+  0,             /* xUnlock */
+  0,             /* xCheckReservedLock */
+  0,             /* xFileControl */
+  0,             /* xSectorSize */
+  0              /* xDeviceCharacteristics */
+};
+
+/*
+** Open a journal file.
+*/
+SQLITE_PRIVATE int sqlite3JournalOpen(
+  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
+  const char *zName,         /* Name of the journal file */
+  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
+  int flags,                 /* Opening flags */
+  int nBuf                   /* Bytes buffered before opening the file */
+){
+  JournalFile *p = (JournalFile *)pJfd;
+  memset(p, 0, sqlite3JournalSize(pVfs));
+  if( nBuf>0 ){
+    p->zBuf = sqlite3MallocZero(nBuf);
+    if( !p->zBuf ){
+      return SQLITE_NOMEM;
+    }
+  }else{
+    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
+  }
+  p->pMethod = &JournalFileMethods;
+  p->nBuf = nBuf;
+  p->flags = flags;
+  p->zJournal = zName;
+  p->pVfs = pVfs;
+  return SQLITE_OK;
+}
+
+/*
+** If the argument p points to a JournalFile structure, and the underlying
+** file has not yet been created, create it now.
+*/
+SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
+  if( p->pMethods!=&JournalFileMethods ){
+    return SQLITE_OK;
+  }
+  return createFile((JournalFile *)p);
+}
+
+/*
+** Return the number of bytes required to store a JournalFile that uses vfs
+** pVfs to create the underlying on-disk files.
+*/
+SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
+  return (pVfs->szOsFile+sizeof(JournalFile));
+}
+#endif
+
+/************** End of journal.c *********************************************/
 /************** Begin file expr.c ********************************************/
 /*
 ** 2001 September 15
 **
@@ -39017,9 +42173,9 @@
 *************************************************************************
 ** This file contains routines used for analyzing expressions and
 ** for generating VDBE code that evaluates expressions in SQLite.
 **
-** $Id: expr.c,v 1.301 2007/07/23 22:51:15 drh Exp $
+** $Id: expr.c,v 1.312 2007/09/01 18:24:55 danielk1977 Exp $
 */
 
 /*
 ** Return the 'affinity' of the expression pExpr if any.
@@ -39180,9 +42336,9 @@
 **
 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
 ** it is not considered.
 */
-CollSeq* sqlite3BinaryCompareCollSeq(
+SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
   Parse *pParse,
   Expr *pLeft,
   Expr *pRight
 ){
@@ -39220,14 +42376,20 @@
 }
 
 /*
 ** Construct a new expression node and return a pointer to it.  Memory
-** for this node is obtained from sqliteMalloc().  The calling function
+** for this node is obtained from sqlite3_malloc().  The calling function
 ** is responsible for making sure the node eventually gets freed.
 */
-SQLITE_PRIVATE Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
+SQLITE_PRIVATE Expr *sqlite3Expr(
+  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
+  int op,                 /* Expression opcode */
+  Expr *pLeft,            /* Left operand */
+  Expr *pRight,           /* Right operand */
+  const Token *pToken     /* Argument token */
+){
   Expr *pNew;
-  pNew = sqliteMalloc( sizeof(Expr) );
+  pNew = sqlite3DbMallocZero(db, sizeof(Expr));
   if( pNew==0 ){
     /* When malloc fails, delete pLeft and pRight. Expressions passed to
     ** this function must always be allocated with sqlite3Expr() for this
     ** reason.
@@ -39261,18 +42423,19 @@
   return pNew;
 }
 
 /*
-** Works like sqlite3Expr() but frees its pLeft and pRight arguments
-** if it fails due to a malloc problem.
-*/
-SQLITE_PRIVATE Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
-  Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
-  if( pNew==0 ){
-    sqlite3ExprDelete(pLeft);
-    sqlite3ExprDelete(pRight);
-  }
-  return pNew;
+** Works like sqlite3Expr() except that it takes an extra Parse*
+** argument and notifies the associated connection object if malloc fails.
+*/
+SQLITE_PRIVATE Expr *sqlite3PExpr(
+  Parse *pParse,          /* Parsing context */
+  int op,                 /* Expression opcode */
+  Expr *pLeft,            /* Left operand */
+  Expr *pRight,           /* Right operand */
+  const Token *pToken     /* Argument token */
+){
+  return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
 }
 
 /*
 ** When doing a nested parse, you can include terms in an expression
@@ -39290,12 +42453,12 @@
   Expr *p;
   int depth;
   if( pParse->nested==0 ){
     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
-    return sqlite3Expr(TK_NULL, 0, 0, 0);
+    return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
   }
   if( v==0 ) return 0;
-  p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
+  p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
   if( p==0 ){
     return 0;  /* Malloc failed */
   }
   depth = atoi((char*)&pToken->z[1]);
@@ -39308,15 +42471,15 @@
 /*
 ** Join two expressions using an AND operator.  If either expression is
 ** NULL, then just return the other expression.
 */
-SQLITE_PRIVATE Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
+SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
   if( pLeft==0 ){
     return pRight;
   }else if( pRight==0 ){
     return pLeft;
   }else{
-    return sqlite3Expr(TK_AND, pLeft, pRight, 0);
+    return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
   }
 }
 
 /*
@@ -39325,9 +42488,9 @@
 */
 SQLITE_PRIVATE void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
   assert( pRight!=0 );
   assert( pLeft!=0 );
-  if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
+  if( pExpr && pRight->z && pLeft->z ){
     assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
     if( pLeft->dyn==0 && pRight->dyn==0 ){
       pExpr->span.z = pLeft->z;
       pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
@@ -39340,12 +42503,12 @@
 /*
 ** Construct a new expression node for a function with multiple
 ** arguments.
 */
-SQLITE_PRIVATE Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
+SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
   Expr *pNew;
   assert( pToken );
-  pNew = sqliteMalloc( sizeof(Expr) );
+  pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
   if( pNew==0 ){
     sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
     return 0;
   }
@@ -39376,8 +42539,10 @@
 ** assigned.
 */
 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
   Token *pToken;
+  sqlite3 *db = pParse->db;
+
   if( pExpr==0 ) return;
   pToken = &pExpr->token;
   assert( pToken->n>=1 );
   assert( pToken->z!=0 );
@@ -39416,12 +42581,16 @@
     if( i>=pParse->nVarExpr ){
       pExpr->iTable = ++pParse->nVar;
       if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
         pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
-        pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
-                       pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
-      }
-      if( !sqlite3MallocFailed() ){
+        pParse->apVarExpr =
+            sqlite3DbReallocOrFree(
+              db,
+              pParse->apVarExpr,
+              pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
+            );
+      }
+      if( !db->mallocFailed ){
         assert( pParse->apVarExpr!=0 );
         pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
       }
     }
@@ -39435,28 +42604,28 @@
 ** Recursively delete an expression tree.
 */
 SQLITE_PRIVATE void sqlite3ExprDelete(Expr *p){
   if( p==0 ) return;
-  if( p->span.dyn ) sqliteFree((char*)p->span.z);
-  if( p->token.dyn ) sqliteFree((char*)p->token.z);
+  if( p->span.dyn ) sqlite3_free((char*)p->span.z);
+  if( p->token.dyn ) sqlite3_free((char*)p->token.z);
   sqlite3ExprDelete(p->pLeft);
   sqlite3ExprDelete(p->pRight);
   sqlite3ExprListDelete(p->pList);
   sqlite3SelectDelete(p->pSelect);
-  sqliteFree(p);
+  sqlite3_free(p);
 }
 
 /*
 ** The Expr.token field might be a string literal that is quoted.
 ** If so, remove the quotation marks.
 */
-SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){
+SQLITE_PRIVATE void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
   if( ExprHasAnyProperty(p, EP_Dequoted) ){
     return;
   }
   ExprSetProperty(p, EP_Dequoted);
   if( p->token.dyn==0 ){
-    sqlite3TokenCopy(&p->token, &p->token);
+    sqlite3TokenCopy(db, &p->token, &p->token);
   }
   sqlite3Dequote((char*)p->token.z);
 }
 
@@ -39472,64 +42641,65 @@
 ** by subsequent calls to sqlite*ListAppend() routines.
 **
 ** Any tables that the SrcList might point to are not duplicated.
 */
-SQLITE_PRIVATE Expr *sqlite3ExprDup(Expr *p){
+SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
   Expr *pNew;
   if( p==0 ) return 0;
-  pNew = sqliteMallocRaw( sizeof(*p) );
+  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
   if( pNew==0 ) return 0;
   memcpy(pNew, p, sizeof(*pNew));
   if( p->token.z!=0 ){
-    pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
+    pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
     pNew->token.dyn = 1;
   }else{
     assert( pNew->token.z==0 );
   }
   pNew->span.z = 0;
-  pNew->pLeft = sqlite3ExprDup(p->pLeft);
-  pNew->pRight = sqlite3ExprDup(p->pRight);
-  pNew->pList = sqlite3ExprListDup(p->pList);
-  pNew->pSelect = sqlite3SelectDup(p->pSelect);
+  pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
+  pNew->pRight = sqlite3ExprDup(db, p->pRight);
+  pNew->pList = sqlite3ExprListDup(db, p->pList);
+  pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
   return pNew;
 }
-SQLITE_PRIVATE void sqlite3TokenCopy(Token *pTo, Token *pFrom){
-  if( pTo->dyn ) sqliteFree((char*)pTo->z);
+SQLITE_PRIVATE void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
+  if( pTo->dyn ) sqlite3_free((char*)pTo->z);
   if( pFrom->z ){
     pTo->n = pFrom->n;
-    pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
+    pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
     pTo->dyn = 1;
   }else{
     pTo->z = 0;
   }
 }
-SQLITE_PRIVATE ExprList *sqlite3ExprListDup(ExprList *p){
+SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
   ExprList *pNew;
   struct ExprList_item *pItem, *pOldItem;
   int i;
   if( p==0 ) return 0;
-  pNew = sqliteMalloc( sizeof(*pNew) );
+  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
   if( pNew==0 ) return 0;
+  pNew->iECursor = 0;
   pNew->nExpr = pNew->nAlloc = p->nExpr;
-  pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
+  pNew->a = pItem = sqlite3DbMallocRaw(db,  p->nExpr*sizeof(p->a[0]) );
   if( pItem==0 ){
-    sqliteFree(pNew);
+    sqlite3_free(pNew);
     return 0;
   }
   pOldItem = p->a;
   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
     Expr *pNewExpr, *pOldExpr;
-    pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
+    pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
     if( pOldExpr->span.z!=0 && pNewExpr ){
       /* Always make a copy of the span for top-level expressions in the
       ** expression list.  The logic in SELECT processing that determines
       ** the names of columns in the result set needs this information */
-      sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
+      sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
     }
     assert( pNewExpr==0 || pNewExpr->span.z!=0
             || pOldExpr->span.z==0
-            || sqlite3MallocFailed() );
-    pItem->zName = sqliteStrDup(pOldItem->zName);
+            || db->mallocFailed );
+    pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
     pItem->sortOrder = pOldItem->sortOrder;
     pItem->isAgg = pOldItem->isAgg;
     pItem->done = 0;
   }
@@ -39543,74 +42713,74 @@
 ** called with a NULL argument.
 */
 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
  || !defined(SQLITE_OMIT_SUBQUERY)
-SQLITE_PRIVATE SrcList *sqlite3SrcListDup(SrcList *p){
+SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
   SrcList *pNew;
   int i;
   int nByte;
   if( p==0 ) return 0;
   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
-  pNew = sqliteMallocRaw( nByte );
+  pNew = sqlite3DbMallocRaw(db, nByte );
   if( pNew==0 ) return 0;
   pNew->nSrc = pNew->nAlloc = p->nSrc;
   for(i=0; i<p->nSrc; i++){
     struct SrcList_item *pNewItem = &pNew->a[i];
     struct SrcList_item *pOldItem = &p->a[i];
     Table *pTab;
-    pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
-    pNewItem->zName = sqliteStrDup(pOldItem->zName);
-    pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
+    pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
+    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
+    pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
     pNewItem->jointype = pOldItem->jointype;
     pNewItem->iCursor = pOldItem->iCursor;
     pNewItem->isPopulated = pOldItem->isPopulated;
     pTab = pNewItem->pTab = pOldItem->pTab;
     if( pTab ){
       pTab->nRef++;
     }
-    pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
-    pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
-    pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
+    pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
+    pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
+    pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
     pNewItem->colUsed = pOldItem->colUsed;
   }
   return pNew;
 }
-SQLITE_PRIVATE IdList *sqlite3IdListDup(IdList *p){
+SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
   IdList *pNew;
   int i;
   if( p==0 ) return 0;
-  pNew = sqliteMallocRaw( sizeof(*pNew) );
+  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
   if( pNew==0 ) return 0;
   pNew->nId = pNew->nAlloc = p->nId;
-  pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
+  pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
   if( pNew->a==0 ){
-    sqliteFree(pNew);
+    sqlite3_free(pNew);
     return 0;
   }
   for(i=0; i<p->nId; i++){
     struct IdList_item *pNewItem = &pNew->a[i];
     struct IdList_item *pOldItem = &p->a[i];
-    pNewItem->zName = sqliteStrDup(pOldItem->zName);
+    pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
     pNewItem->idx = pOldItem->idx;
   }
   return pNew;
 }
-SQLITE_PRIVATE Select *sqlite3SelectDup(Select *p){
+SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p){
   Select *pNew;
   if( p==0 ) return 0;
-  pNew = sqliteMallocRaw( sizeof(*p) );
+  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
   if( pNew==0 ) return 0;
   pNew->isDistinct = p->isDistinct;
-  pNew->pEList = sqlite3ExprListDup(p->pEList);
-  pNew->pSrc = sqlite3SrcListDup(p->pSrc);
-  pNew->pWhere = sqlite3ExprDup(p->pWhere);
-  pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
-  pNew->pHaving = sqlite3ExprDup(p->pHaving);
-  pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
+  pNew->pEList = sqlite3ExprListDup(db, p->pEList);
+  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
+  pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
+  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
+  pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
+  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
   pNew->op = p->op;
-  pNew->pPrior = sqlite3SelectDup(p->pPrior);
-  pNew->pLimit = sqlite3ExprDup(p->pLimit);
-  pNew->pOffset = sqlite3ExprDup(p->pOffset);
+  pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
+  pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
+  pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
   pNew->iLimit = -1;
   pNew->iOffset = -1;
   pNew->isResolved = p->isResolved;
   pNew->isAgg = p->isAgg;
@@ -39622,9 +42792,9 @@
   pNew->addrOpenEphm[2] = -1;
   return pNew;
 }
 #else
-SQLITE_PRIVATE Select *sqlite3SelectDup(Select *p){
+SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p){
   assert( p==0 );
   return 0;
 }
 #endif
@@ -39633,11 +42803,17 @@
 /*
 ** Add a new element to the end of an expression list.  If pList is
 ** initially NULL, then create a new expression list.
 */
-SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
+SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
+  Parse *pParse,          /* Parsing context */
+  ExprList *pList,        /* List to which to append. Might be NULL */
+  Expr *pExpr,            /* Expression to be appended */
+  Token *pName            /* AS keyword for the expression */
+){
+  sqlite3 *db = pParse->db;
   if( pList==0 ){
-    pList = sqliteMalloc( sizeof(ExprList) );
+    pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
     if( pList==0 ){
       goto no_mem;
     }
     assert( pList->nAlloc==0 );
@@ -39644,9 +42820,9 @@
   }
   if( pList->nAlloc<=pList->nExpr ){
     struct ExprList_item *a;
     int n = pList->nAlloc*2 + 4;
-    a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
+    a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
     if( a==0 ){
       goto no_mem;
     }
     pList->a = a;
@@ -39655,9 +42831,9 @@
   assert( pList->a!=0 );
   if( pExpr || pName ){
     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
     memset(pItem, 0, sizeof(*pItem));
-    pItem->zName = sqlite3NameFromToken(pName);
+    pItem->zName = sqlite3NameFromToken(db, pName);
     pItem->pExpr = pExpr;
   }
   return pList;
 
@@ -39683,9 +42859,9 @@
   }
 }
 
 
-#if SQLITE_MAX_EXPR_DEPTH>0
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
 /* The following three functions, heightOfExpr(), heightOfExprList()
 ** and heightOfSelect(), are used to determine the maximum height
 ** of any expression tree referenced by the structure passed as the
 ** first argument.
@@ -39759,12 +42935,12 @@
   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
   assert( pList->nExpr<=pList->nAlloc );
   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
     sqlite3ExprDelete(pItem->pExpr);
-    sqliteFree(pItem->zName);
-  }
-  sqliteFree(pList->a);
-  sqliteFree(pList);
+    sqlite3_free(pItem->zName);
+  }
+  sqlite3_free(pList->a);
+  sqlite3_free(pList);
 }
 
 /*
 ** Walk an expression tree.  Call xFunc for each node visited.
@@ -39999,12 +43175,12 @@
   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   NameContext *pTopNC = pNC;        /* First namecontext in the list */
 
   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
-  zDb = sqlite3NameFromToken(pDbToken);
-  zTab = sqlite3NameFromToken(pTableToken);
-  zCol = sqlite3NameFromToken(pColumnToken);
-  if( sqlite3MallocFailed() ){
+  zDb = sqlite3NameFromToken(db, pDbToken);
+  zTab = sqlite3NameFromToken(db, pTableToken);
+  zCol = sqlite3NameFromToken(db, pColumnToken);
+  if( db->mallocFailed ){
     goto lookupname_end;
   }
 
   pExpr->iTable = -1;
@@ -40150,20 +43326,20 @@
           assert( pExpr->pSelect==0 );
           pOrig = pEList->a[j].pExpr;
           if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
-            sqliteFree(zCol);
+            sqlite3_free(zCol);
             return 2;
           }
-          pDup = sqlite3ExprDup(pOrig);
+          pDup = sqlite3ExprDup(db, pOrig);
           if( pExpr->flags & EP_ExpCollate ){
             pDup->pColl = pExpr->pColl;
             pDup->flags |= EP_ExpCollate;
           }
-          if( pExpr->span.dyn ) sqliteFree((char*)pExpr->span.z);
-          if( pExpr->token.dyn ) sqliteFree((char*)pExpr->token.z);
+          if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
+          if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
           memcpy(pExpr, pDup, sizeof(*pExpr));
-          sqliteFree(pDup);
+          sqlite3_free(pDup);
           cnt = 1;
           pMatch = 0;
           assert( zTab==0 && zDb==0 );
           goto lookupname_end_2;
@@ -40189,9 +43365,9 @@
   ** Because no reference was made to outer contexts, the pNC->nRef
   ** fields are not changed in any context.
   */
   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
-    sqliteFree(zCol);
+    sqlite3_free(zCol);
     return 0;
   }
 
   /*
@@ -40206,13 +43382,17 @@
       sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
     }else if( zTab ){
       sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
     }else{
-      z = sqliteStrDup(zCol);
-    }
-    sqlite3ErrorMsg(pParse, zErr, z);
-    sqliteFree(z);
-    pTopNC->nErr++;
+      z = sqlite3StrDup(zCol);
+    }
+    if( z ){
+      sqlite3ErrorMsg(pParse, zErr, z);
+      sqlite3_free(z);
+      pTopNC->nErr++;
+    }else{
+      db->mallocFailed = 1;
+    }
   }
 
   /* If a column from a table in pSrcList is referenced, then record
   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
@@ -40231,17 +43411,17 @@
 
 lookupname_end:
   /* Clean up and return
   */
-  sqliteFree(zDb);
-  sqliteFree(zTab);
+  sqlite3_free(zDb);
+  sqlite3_free(zTab);
   sqlite3ExprDelete(pExpr->pLeft);
   pExpr->pLeft = 0;
   sqlite3ExprDelete(pExpr->pRight);
   pExpr->pRight = 0;
   pExpr->op = TK_COLUMN;
 lookupname_end_2:
-  sqliteFree(zCol);
+  sqlite3_free(zCol);
   if( cnt==1 ){
     assert( pNC!=0 );
     sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
     if( pMatch && !pMatch->pSelect ){
@@ -40459,9 +43639,9 @@
   Expr *pExpr             /* The expression to be analyzed. */
 ){
   int savedHasAgg;
   if( pExpr==0 ) return 0;
-#if SQLITE_MAX_EXPR_DEPTH>0
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
   if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
     sqlite3ErrorMsg(pNC->pParse,
        "Expression tree is too large (maximum depth %d)",
        SQLITE_MAX_EXPR_DEPTH
@@ -40472,9 +43652,9 @@
 #endif
   savedHasAgg = pNC->hasAgg;
   pNC->hasAgg = 0;
   walkExprTree(pExpr, nameResolverStep, pNC);
-#if SQLITE_MAX_EXPR_DEPTH>0
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
   pNC->pParse->nHeight -= pExpr->nHeight;
 #endif
   if( pNC->nErr>0 ){
     ExprSetProperty(pExpr, EP_Error);
@@ -40530,9 +43710,9 @@
   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
     int mem = pParse->nMem++;
     sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
     testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
-    assert( testAddr>0 || sqlite3MallocFailed() );
+    assert( testAddr>0 || pParse->db->mallocFailed );
     sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
   }
 
   switch( pExpr->op ){
@@ -40581,9 +43761,9 @@
         }
       }else if( pExpr->pList ){
         /* Case 2:     expr IN (exprlist)
         **
-	** For each expression, build an index key from the evaluation and
+        ** For each expression, build an index key from the evaluation and
         ** store it in the temporary table. If <expr> is a column, then use
         ** that columns affinity when building index keys. If <expr> is not
         ** a column, use numeric affinity.
         */
@@ -40642,9 +43822,9 @@
         sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
         VdbeComment((v, "# Init EXISTS result"));
       }
       sqlite3ExprDelete(pSel->pLimit);
-      pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
+      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
       if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
         return;
       }
       break;
@@ -40663,9 +43843,9 @@
 ** Generate an instruction that will put the integer describe by
 ** text z[0..n-1] on the stack.
 */
 static void codeInteger(Vdbe *v, const char *z, int n){
-  assert( z || sqlite3MallocFailed() );
+  assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
   if( z ){
     int i;
     if( sqlite3GetInt32(z, &i) ){
       sqlite3VdbeAddOp(v, OP_Integer, i, 0);
@@ -40754,9 +43934,9 @@
     case TK_FLOAT:
     case TK_STRING: {
       assert( TK_FLOAT==OP_Real );
       assert( TK_STRING==OP_String8 );
-      sqlite3DequoteExpr(pExpr);
+      sqlite3DequoteExpr(pParse->db, pExpr);
       sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
       break;
     }
     case TK_NULL: {
@@ -40857,15 +44037,15 @@
       Expr *pLeft = pExpr->pLeft;
       assert( pLeft );
       if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
         Token *p = &pLeft->token;
-        char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
+        char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z);
         if( pLeft->op==TK_FLOAT ){
           sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
         }else{
           codeInteger(v, z, p->n+1);
         }
-        sqliteFree(z);
+        sqlite3_free(z);
         break;
       }
       /* Fall through into TK_NOT */
     }
@@ -40909,10 +44089,12 @@
       int nId;
       const char *zId;
       int constMask = 0;
       int i;
-      u8 enc = ENC(pParse->db);
+      sqlite3 *db = pParse->db;
+      u8 enc = ENC(db);
       CollSeq *pColl = 0;
+
       zId = (char*)pExpr->token.z;
       nId = pExpr->token.n;
       pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
       assert( pDef!=0 );
@@ -40930,11 +44112,11 @@
       ** "glob(B,A).  We want to use the A in "A glob B" to test
       ** for function overloading.  But we use the B term in "glob(B,A)".
       */
       if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
-        pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
+        pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
       }else if( nExpr>0 ){
-        pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
+        pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
       }
 #endif
       for(i=0; i<nExpr && i<32; i++){
         if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
@@ -41061,15 +44243,15 @@
     case TK_RAISE: {
       if( !pParse->trigStack ){
         sqlite3ErrorMsg(pParse,
                        "RAISE() may only be used within a trigger-program");
-	return;
+        return;
       }
       if( pExpr->iColumn!=OE_Ignore ){
          assert( pExpr->iColumn==OE_Rollback ||
                  pExpr->iColumn == OE_Abort ||
                  pExpr->iColumn == OE_Fail );
-         sqlite3DequoteExpr(pExpr);
+         sqlite3DequoteExpr(pParse->db, pExpr);
          sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
                         (char*)pExpr->token.z, pExpr->token.n);
       } else {
          assert( pExpr->iColumn == OE_Ignore );
@@ -41392,11 +44574,12 @@
 /*
 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
 ** the new element.  Return a negative number if malloc fails.
 */
-static int addAggInfoColumn(AggInfo *pInfo){
+static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
   int i;
   pInfo->aCol = sqlite3ArrayAllocate(
+       db,
        pInfo->aCol,
        sizeof(pInfo->aCol[0]),
        3,
        &pInfo->nColumn,
@@ -41409,11 +44592,12 @@
 /*
 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
 ** the new element.  Return a negative number if malloc fails.
 */
-static int addAggInfoFunc(AggInfo *pInfo){
+static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
   int i;
   pInfo->aFunc = sqlite3ArrayAllocate(
+       db,
        pInfo->aFunc,
        sizeof(pInfo->aFunc[0]),
        3,
        &pInfo->nFunc,
@@ -41435,9 +44619,8 @@
   NameContext *pNC = (NameContext *)pArg;
   Parse *pParse = pNC->pParse;
   SrcList *pSrcList = pNC->pSrcList;
   AggInfo *pAggInfo = pNC->pAggInfo;
-
 
   switch( pExpr->op ){
     case TK_AGG_COLUMN:
     case TK_COLUMN: {
@@ -41461,9 +44644,11 @@
                   pCol->iColumn==pExpr->iColumn ){
                 break;
               }
             }
-            if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
+            if( (k>=pAggInfo->nColumn)
+             && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
+            ){
               pCol = &pAggInfo->aCol[k];
               pCol->pTab = pExpr->pTab;
               pCol->iTable = pExpr->iTable;
               pCol->iColumn = pExpr->iColumn;
@@ -41518,9 +44703,9 @@
         if( i>=pAggInfo->nFunc ){
           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
           */
           u8 enc = ENC(pParse->db);
-          i = addAggInfoFunc(pAggInfo);
+          i = addAggInfoFunc(pParse->db, pAggInfo);
           if( i>=0 ){
             pItem = &pAggInfo->aFunc[i];
             pItem->pExpr = pExpr;
             pItem->iMem = pParse->nMem++;
@@ -41605,9 +44790,9 @@
 *************************************************************************
 ** This file contains C code routines that used to generate VDBE code
 ** that implements the ALTER TABLE command.
 **
-** $Id: alter.c,v 1.27 2007/06/27 17:09:24 danielk1977 Exp $
+** $Id: alter.c,v 1.32 2007/08/29 14:06:23 danielk1977 Exp $
 */
 
 /*
 ** The code in this file only exists if we are not omitting the
@@ -41642,8 +44827,10 @@
   unsigned char const *zCsr = zSql;
   int len = 0;
   char *zRet;
 
+  sqlite3 *db = sqlite3_user_data(context);
+
   /* The principle used to locate the table name in the CREATE TABLE
   ** statement is that the table name is the first token that is immediatedly
   ** followed by a left parenthesis - TK_LP - or "USING" TK_USING.
   */
@@ -41667,11 +44854,11 @@
       } while( token==TK_SPACE );
       assert( len>0 );
     } while( token!=TK_LP && token!=TK_USING );
 
-    zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
+    zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql,
        zTableName, tname.z+tname.n);
-    sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
+    sqlite3_result_text(context, zRet, -1, sqlite3_free);
   }
 }
 
 #ifndef SQLITE_OMIT_TRIGGER
@@ -41695,8 +44882,10 @@
   int dist = 3;
   unsigned char const *zCsr = zSql;
   int len = 0;
   char *zRet;
+
+  sqlite3 *db = sqlite3_user_data(context);
 
   /* The principle used to locate the table name in the CREATE TRIGGER
   ** statement is that the table name is the first token that is immediatedly
   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
@@ -41740,11 +44929,11 @@
 
     /* Variable tname now contains the token that is the old table-name
     ** in the CREATE TRIGGER statement.
     */
-    zRet = sqlite3MPrintf("%.*s%Q%s", tname.z - zSql, zSql,
+    zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql,
        zTableName, tname.z+tname.n);
-    sqlite3_result_text(context, zRet, -1, sqlite3FreeX);
+    sqlite3_result_text(context, zRet, -1, sqlite3_free);
   }
 }
 #endif   /* !SQLITE_OMIT_TRIGGER */
 
@@ -41765,9 +44954,9 @@
   int i;
 
   for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
     sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
-        SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
+        SQLITE_UTF8, (void *)db, aFuncs[i].xFunc, 0, 0);
   }
 }
 
 /*
@@ -41787,16 +44976,17 @@
   ** that is not part of the temp-db schema, add a clause to the WHERE
   ** expression being built up in zWhere.
   */
   if( pTab->pSchema!=pTempSchema ){
+    sqlite3 *db = pParse->db;
     for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
       if( pTrig->pSchema==pTempSchema ){
         if( !zWhere ){
-          zWhere = sqlite3MPrintf("name=%Q", pTrig->name);
+          zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
         }else{
           tmp = zWhere;
-          zWhere = sqlite3MPrintf("%s OR name=%Q", zWhere, pTrig->name);
-          sqliteFree(tmp);
+          zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
+          sqlite3_free(tmp);
         }
       }
     }
   }
@@ -41820,8 +45010,9 @@
 #endif
 
   v = sqlite3GetVdbe(pParse);
   if( !v ) return;
+  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   assert( iDb>=0 );
 
 #ifndef SQLITE_OMIT_TRIGGER
@@ -41836,9 +45027,9 @@
   /* Drop the table and index from the internal schema */
   sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
 
   /* Reload the table, index and permanent trigger schemas. */
-  zWhere = sqlite3MPrintf("tbl_name=%Q", zName);
+  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
   if( !zWhere ) return;
   sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
 
 #ifndef SQLITE_OMIT_TRIGGER
@@ -41872,18 +45063,19 @@
   char *zWhere = 0;         /* Where clause to locate temp triggers */
 #endif
   int isVirtualRename = 0;  /* True if this is a v-table with an xRename() */
 
-  if( sqlite3MallocFailed() ) goto exit_rename_table;
+  if( db->mallocFailed ) goto exit_rename_table;
   assert( pSrc->nSrc==1 );
+  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 
   pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   if( !pTab ) goto exit_rename_table;
   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   zDb = db->aDb[iDb].zName;
 
   /* Get a NULL terminated version of the new table name. */
-  zName = sqlite3NameFromToken(pName);
+  zName = sqlite3NameFromToken(db, pName);
   if( !zName ) goto exit_rename_table;
 
   /* Check that a table or index named 'zName' does not already exist
   ** in database iDb. If so, this is an error.
@@ -41995,9 +45187,9 @@
         "UPDATE sqlite_temp_master SET "
             "sql = sqlite_rename_trigger(sql, %Q), "
             "tbl_name = %Q "
             "WHERE %s;", zName, zName, zWhere);
-    sqliteFree(zWhere);
+    sqlite3_free(zWhere);
   }
 #endif
 
   /* Drop and reload the internal table schema. */
@@ -42004,9 +45196,9 @@
   reloadTableSchema(pParse, pTab, zName);
 
 exit_rename_table:
   sqlite3SrcListDelete(pSrc);
-  sqliteFree(zName);
+  sqlite3_free(zName);
 }
 
 
 /*
@@ -42025,19 +45217,22 @@
   const char *zTab;         /* Table name */
   char *zCol;               /* Null-terminated column definition */
   Column *pCol;             /* The new column */
   Expr *pDflt;              /* Default value for the new column */
+  sqlite3 *db;              /* The database connection; */
 
   if( pParse->nErr ) return;
   pNew = pParse->pNewTable;
   assert( pNew );
 
-  iDb = sqlite3SchemaToIndex(pParse->db, pNew->pSchema);
-  zDb = pParse->db->aDb[iDb].zName;
+  db = pParse->db;
+  assert( sqlite3BtreeHoldsAllMutexes(db) );
+  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
+  zDb = db->aDb[iDb].zName;
   zTab = pNew->zName;
   pCol = &pNew->aCol[pNew->nCol-1];
   pDflt = pCol->pDflt;
-  pTab = sqlite3FindTable(pParse->db, zTab, zDb);
+  pTab = sqlite3FindTable(db, zTab, zDb);
   assert( pTab );
 
 #ifndef SQLITE_OMIT_AUTHORIZATION
   /* Invoke the authorization callback. */
@@ -42076,10 +45271,10 @@
   ** can handle (i.e. not CURRENT_TIME etc.)
   */
   if( pDflt ){
     sqlite3_value *pVal;
-    if( sqlite3ValueFromExpr(pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
-      /* malloc() has failed */
+    if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
+      db->mallocFailed = 1;
       return;
     }
     if( !pVal ){
       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
@@ -42088,9 +45283,9 @@
     sqlite3ValueFree(pVal);
   }
 
   /* Modify the CREATE TABLE statement. */
-  zCol = sqliteStrNDup((char*)pColDef->z, pColDef->n);
+  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
   if( zCol ){
     char *zEnd = &zCol[pColDef->n-1];
     while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
       *zEnd-- = '\0';
@@ -42101,9 +45296,9 @@
         "WHERE type = 'table' AND name = %Q",
       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
       zTab
     );
-    sqliteFree(zCol);
+    sqlite3_free(zCol);
   }
 
   /* If the default value of the new column is NULL, then set the file
   ** format to 2. If the default value of the new column is not NULL,
@@ -42136,12 +45331,14 @@
   Vdbe *v;
   int iDb;
   int i;
   int nAlloc;
+  sqlite3 *db = pParse->db;
 
   /* Look up the table being altered. */
   assert( pParse->pNewTable==0 );
-  if( sqlite3MallocFailed() ) goto exit_begin_add_column;
+  assert( sqlite3BtreeHoldsAllMutexes(db) );
+  if( db->mallocFailed ) goto exit_begin_add_column;
   pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   if( !pTab ) goto exit_begin_add_column;
 
 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -42157,43 +45354,44 @@
     goto exit_begin_add_column;
   }
 
   assert( pTab->addColOffset>0 );
-  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
+  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 
   /* Put a copy of the Table struct in Parse.pNewTable for the
   ** sqlite3AddColumn() function and friends to modify.
   */
-  pNew = (Table *)sqliteMalloc(sizeof(Table));
+  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
   if( !pNew ) goto exit_begin_add_column;
   pParse->pNewTable = pNew;
   pNew->nRef = 1;
   pNew->nCol = pTab->nCol;
   assert( pNew->nCol>0 );
   nAlloc = (((pNew->nCol-1)/8)*8)+8;
   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
-  pNew->aCol = (Column *)sqliteMalloc(sizeof(Column)*nAlloc);
-  pNew->zName = sqliteStrDup(pTab->zName);
+  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
+  pNew->zName = sqlite3DbStrDup(db, pTab->zName);
   if( !pNew->aCol || !pNew->zName ){
+    db->mallocFailed = 1;
     goto exit_begin_add_column;
   }
   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
   for(i=0; i<pNew->nCol; i++){
     Column *pCol = &pNew->aCol[i];
-    pCol->zName = sqliteStrDup(pCol->zName);
+    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
     pCol->zColl = 0;
     pCol->zType = 0;
     pCol->pDflt = 0;
   }
-  pNew->pSchema = pParse->db->aDb[iDb].pSchema;
+  pNew->pSchema = db->aDb[iDb].pSchema;
   pNew->addColOffset = pTab->addColOffset;
   pNew->nRef = 1;
 
   /* Begin a transaction and increment the schema cookie.  */
   sqlite3BeginWriteOperation(pParse, 0, iDb);
   v = sqlite3GetVdbe(pParse);
   if( !v ) goto exit_begin_add_column;
-  sqlite3ChangeCookie(pParse->db, v, iDb);
+  sqlite3ChangeCookie(db, v, iDb);
 
 exit_begin_add_column:
   sqlite3SrcListDelete(pSrc);
   return;
@@ -42214,9 +45412,9 @@
 **
 *************************************************************************
 ** This file contains code associated with the ANALYZE command.
 **
-** @(#) $Id: analyze.c,v 1.19 2007/06/20 13:37:31 drh Exp $
+** @(#) $Id: analyze.c,v 1.23 2007/08/29 17:43:20 drh Exp $
 */
 #ifndef SQLITE_OMIT_ANALYZE
 
 /*
@@ -42239,8 +45437,10 @@
   Table *pStat;
   Vdbe *v = sqlite3GetVdbe(pParse);
 
   if( v==0 ) return;
+  assert( sqlite3BtreeHoldsAllMutexes(db) );
+  assert( sqlite3VdbeDb(v)==db );
   pDb = &db->aDb[iDb];
   if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){
     /* The sqlite_stat1 tables does not exist.  Create it.
     ** Note that a side-effect of the CREATE TABLE statement is to leave
@@ -42302,9 +45502,9 @@
   if( v==0 || pTab==0 || pTab->pIndex==0 ){
     /* Do no analysis for tables that have no indices */
     return;
   }
-
+  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   assert( iDb>=0 );
 #ifndef SQLITE_OMIT_AUTHORIZATION
   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
@@ -42460,8 +45660,9 @@
   int iDb;
   int iStatCur;
 
   assert( pTab!=0 );
+  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   sqlite3BeginWriteOperation(pParse, 0, iDb);
   iStatCur = pParse->nTab++;
   openStatTable(pParse, iDb, iStatCur, pTab->zName);
@@ -42490,8 +45691,9 @@
   Token *pTableName;
 
   /* Read the database schema. If an error occurs, leave an error message
   ** and code in pParse and return NULL. */
+  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
     return;
   }
 
@@ -42506,11 +45708,11 @@
     iDb = sqlite3FindDb(db, pName1);
     if( iDb>=0 ){
       analyzeDatabase(pParse, iDb);
     }else{
-      z = sqlite3NameFromToken(pName1);
+      z = sqlite3NameFromToken(db, pName1);
       pTab = sqlite3LocateTable(pParse, z, 0);
-      sqliteFree(z);
+      sqlite3_free(z);
       if( pTab ){
         analyzeTable(pParse, pTab);
       }
     }
@@ -42518,12 +45720,12 @@
     /* Form 3: Analyze the fully qualified table name */
     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
     if( iDb>=0 ){
       zDb = db->aDb[iDb].zName;
-      z = sqlite3NameFromToken(pTableName);
+      z = sqlite3NameFromToken(db, pTableName);
       if( z ){
         pTab = sqlite3LocateTable(pParse, z, zDb);
-        sqliteFree(z);
+        sqlite3_free(z);
         if( pTab ){
           analyzeTable(pParse, pTab);
         }
       }
@@ -42584,8 +45786,12 @@
   HashElem *i;
   char *zSql;
   int rc;
 
+  assert( iDb>=0 && iDb<db->nDb );
+  assert( db->aDb[iDb].pBt!=0 );
+  assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
+
   /* Clear any prior statistics */
   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
     Index *pIdx = sqliteHashData(i);
     sqlite3DefaultRowEst(pIdx);
@@ -42599,14 +45805,14 @@
   }
 
 
   /* Load new statistics out of the sqlite_stat1 table */
-  zSql = sqlite3MPrintf("SELECT idx, stat FROM %Q.sqlite_stat1",
+  zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1",
                         sInfo.zDatabase);
   sqlite3SafetyOff(db);
   rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
   sqlite3SafetyOn(db);
-  sqliteFree(zSql);
+  sqlite3_free(zSql);
   return rc;
 }
 
 
@@ -42626,9 +45832,9 @@
 **
 *************************************************************************
 ** This file contains code used to implement the ATTACH and DETACH commands.
 **
-** $Id: attach.c,v 1.60 2007/05/09 20:31:30 drh Exp $
+** $Id: attach.c,v 1.62 2007/09/03 15:19:35 drh Exp $
 */
 
 #ifndef SQLITE_OMIT_ATTACH
 /*
@@ -42687,10 +45893,10 @@
   sqlite3 *db = sqlite3_user_data(context);
   const char *zName;
   const char *zFile;
   Db *aNew;
-  char zErr[128];
   char *zErrDyn = 0;
+  char zErr[128];
 
   zFile = (const char *)sqlite3_value_text(argv[0]);
   zName = (const char *)sqlite3_value_text(argv[1]);
   if( zFile==0 ) zFile = "";
@@ -42716,9 +45922,10 @@
   }
   for(i=0; i<db->nDb; i++){
     char *z = db->aDb[i].zName;
     if( z && zName && sqlite3StrICmp(z, zName)==0 ){
-      sqlite3_snprintf(sizeof(zErr), zErr, "database %s is already in use", zName);
+      sqlite3_snprintf(sizeof(zErr), zErr,
+                       "database %s is already in use", zName);
       goto attach_error;
     }
   }
 
@@ -42725,16 +45932,18 @@
   /* Allocate the new entry in the db->aDb[] array and initialise the schema
   ** hash tables.
   */
   if( db->aDb==db->aDbStatic ){
-    aNew = sqliteMalloc( sizeof(db->aDb[0])*3 );
+    aNew = sqlite3_malloc( sizeof(db->aDb[0])*3 );
     if( aNew==0 ){
+      db->mallocFailed = 1;
       return;
     }
     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
   }else{
-    aNew = sqliteRealloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
+    aNew = sqlite3_realloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
     if( aNew==0 ){
+      db->mallocFailed = 1;
       return;
     }
   }
   db->aDb = aNew;
@@ -42744,11 +45953,13 @@
   /* Open the database file. If the btree is successfully opened, use
   ** it to obtain the database schema. At this point the schema may
   ** or may not be initialised.
   */
-  rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE, &aNew->pBt);
+  rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE,
+                           db->openFlags | SQLITE_OPEN_MAIN_DB,
+                           &aNew->pBt);
   if( rc==SQLITE_OK ){
-    aNew->pSchema = sqlite3SchemaGet(aNew->pBt);
+    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
     if( !aNew->pSchema ){
       rc = SQLITE_NOMEM;
     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
       sqlite3_snprintf(sizeof(zErr), zErr,
@@ -42756,9 +45967,9 @@
       goto attach_error;
     }
     sqlite3PagerLockingMode(sqlite3BtreePager(aNew->pBt), db->dfltLockMode);
   }
-  aNew->zName = sqliteStrDup(zName);
+  aNew->zName = sqlite3DbStrDup(db, zName);
   aNew->safety_level = 3;
 
 #if SQLITE_HAS_CODEC
   {
@@ -42769,9 +45980,9 @@
     int t = sqlite3_value_type(argv[2]);
     switch( t ){
       case SQLITE_INTEGER:
       case SQLITE_FLOAT:
-        zErrDyn = sqliteStrDup("Invalid key value");
+        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
         rc = SQLITE_ERROR;
         break;
 
       case SQLITE_TEXT:
@@ -42810,9 +46021,9 @@
     }
     sqlite3ResetInternalSchema(db, 0);
     db->nDb = iDb;
     if( rc==SQLITE_NOMEM ){
-      sqlite3FailedMalloc();
+      db->mallocFailed = 1;
       sqlite3_snprintf(sizeof(zErr),zErr, "out of memory");
     }else{
       sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile);
     }
@@ -42824,9 +46035,9 @@
 attach_error:
   /* Return an error if we get here */
   if( zErrDyn ){
     sqlite3_result_error(context, zErrDyn, -1);
-    sqliteFree(zErrDyn);
+    sqlite3_free(zErrDyn);
   }else{
     zErr[sizeof(zErr)-1] = 0;
     sqlite3_result_error(context, zErr, -1);
   }
@@ -42906,16 +46117,16 @@
   FuncDef *pFunc;
   sqlite3* db = pParse->db;
 
 #ifndef SQLITE_OMIT_AUTHORIZATION
-  assert( sqlite3MallocFailed() || pAuthArg );
+  assert( db->mallocFailed || pAuthArg );
   if( pAuthArg ){
-    char *zAuthArg = sqlite3NameFromToken(&pAuthArg->span);
+    char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span);
     if( !zAuthArg ){
       goto attach_end;
     }
     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
-    sqliteFree(zAuthArg);
+    sqlite3_free(zAuthArg);
     if(rc!=SQLITE_OK ){
       goto attach_end;
     }
   }
@@ -42937,9 +46148,9 @@
   sqlite3ExprCode(pParse, pFilename);
   sqlite3ExprCode(pParse, pDbname);
   sqlite3ExprCode(pParse, pKey);
 
-  assert( v || sqlite3MallocFailed() );
+  assert( v || db->mallocFailed );
   if( v ){
     sqlite3VdbeAddOp(v, OP_Function, 0, nFunc);
     pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
     sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF);
@@ -43038,9 +46249,9 @@
   if( pList==0 ) return 0;
   zDb = pFix->zDb;
   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
     if( pItem->zDatabase==0 ){
-      pItem->zDatabase = sqliteStrDup(zDb);
+      pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
     }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
       sqlite3ErrorMsg(pFix->pParse,
          "%s %T cannot reference objects in database %s",
          pFix->zType, pFix->pName, pItem->zDatabase);
@@ -43147,9 +46358,9 @@
 ** API.  This facility is an optional feature of the library.  Embedded
 ** systems that do not need this facility may omit it by recompiling
 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
 **
-** $Id: auth.c,v 1.26 2007/05/14 11:34:47 drh Exp $
+** $Id: auth.c,v 1.28 2007/09/01 18:24:55 danielk1977 Exp $
 */
 
 /*
 ** All of the code in this file may be omitted by defining a single
@@ -43201,16 +46412,18 @@
 **
 ** Setting the auth function to NULL disables this hook.  The default
 ** setting of the auth function is NULL.
 */
-int sqlite3_set_authorizer(
+SQLITE_API int sqlite3_set_authorizer(
   sqlite3 *db,
   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
   void *pArg
 ){
+  sqlite3_mutex_enter(db->mutex);
   db->xAuth = xAuth;
   db->pAuthArg = pArg;
   sqlite3ExpirePreparedStatements(db);
+  sqlite3_mutex_leave(db->mutex);
   return SQLITE_OK;
 }
 
 /*
@@ -43239,9 +46452,9 @@
   SrcList *pTabList     /* All table that pExpr might refer to */
 ){
   sqlite3 *db = pParse->db;
   int rc;
-  Table *pTab;          /* The table being read */
+  Table *pTab = 0;      /* The table being read */
   const char *zCol;     /* Name of the column of the table */
   int iSrc;             /* Index in pTabList->a[] of table being read */
   const char *zDBase;   /* Name of database being accessed */
   TriggerStack *pStack; /* The stack of current triggers */
@@ -43265,10 +46478,8 @@
     ** of a trigger.
     */
     assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
     pTab = pStack->pTab;
-  }else{
-    return;
   }
   if( pTab==0 ) return;
   if( pExpr->iColumn>=0 ){
     assert( pExpr->iColumn<pTab->nCol );
@@ -43390,9 +46601,9 @@
 **     BEGIN TRANSACTION
 **     COMMIT
 **     ROLLBACK
 **
-** $Id: build.c,v 1.433 2007/07/02 19:31:27 drh Exp $
+** $Id: build.c,v 1.444 2007/09/03 15:19:35 drh Exp $
 */
 
 /*
 ** This routine is called when a new SQL statement is beginning to
@@ -43435,9 +46646,9 @@
   int i;
   int nBytes;
   TableLock *p;
 
-  if( 0==sqlite3ThreadDataReadOnly()->useSharedData || iDb<0 ){
+  if( iDb<0 ){
     return;
   }
 
   for(i=0; i<pParse->nTableLock; i++){
@@ -43448,15 +46659,19 @@
     }
   }
 
   nBytes = sizeof(TableLock) * (pParse->nTableLock+1);
-  pParse->aTableLock = sqliteReallocOrFree(pParse->aTableLock, nBytes);
+  pParse->aTableLock =
+      sqlite3DbReallocOrFree(pParse->db, pParse->aTableLock, nBytes);
   if( pParse->aTableLock ){
     p = &pParse->aTableLock[pParse->nTableLock++];
     p->iDb = iDb;
     p->iTab = iTab;
     p->isWriteLock = isWriteLock;
     p->zName = zName;
+  }else{
+    pParse->nTableLock = 0;
+    pParse->db->mallocFailed = 1;
   }
 }
 
 /*
@@ -43465,9 +46680,8 @@
 */
 static void codeTableLocks(Parse *pParse){
   int i;
   Vdbe *pVdbe;
-  assert( sqlite3ThreadDataReadOnly()->useSharedData || pParse->nTableLock==0 );
 
   if( 0==(pVdbe = sqlite3GetVdbe(pParse)) ){
     return;
   }
@@ -43498,9 +46712,10 @@
 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
   sqlite3 *db;
   Vdbe *v;
 
-  if( sqlite3MallocFailed() ) return;
+  db = pParse->db;
+  if( db->mallocFailed ) return;
   if( pParse->nested ) return;
   if( !pParse->pVdbe ){
     if( pParse->rc==SQLITE_OK && pParse->nErr ){
       pParse->rc = SQLITE_ERROR;
@@ -43510,9 +46725,8 @@
 
   /* Begin by generating some termination code at the end of the
   ** vdbe program
   */
-  db = pParse->db;
   v = sqlite3GetVdbe(pParse);
   if( v ){
     sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
 
@@ -43527,8 +46741,9 @@
       int iDb;
       sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
         if( (mask & pParse->cookieMask)==0 ) continue;
+        sqlite3VdbeUsesBtree(v, iDb);
         sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
         sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
       }
 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -43559,9 +46774,9 @@
 
 
   /* Get the VDBE program ready for execution
   */
-  if( v && pParse->nErr==0 && !sqlite3MallocFailed() ){
+  if( v && pParse->nErr==0 && !db->mallocFailed ){
 #ifdef SQLITE_DEBUG
     FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
     sqlite3VdbeTrace(v, trace);
 #endif
@@ -43600,18 +46815,19 @@
 
   if( pParse->nErr ) return;
   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
   va_start(ap, zFormat);
-  zSql = sqlite3VMPrintf(zFormat, ap);
+  zSql = sqlite3VMPrintf(pParse->db, zFormat, ap);
   va_end(ap);
   if( zSql==0 ){
+    pParse->db->mallocFailed = 1;
     return;   /* A malloc must have failed */
   }
   pParse->nested++;
   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
   memset(&pParse->nVar, 0, SAVE_SZ);
   sqlite3RunParser(pParse, zSql, 0);
-  sqliteFree(zSql);
+  sqlite3_free(zSql);
   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
   pParse->nested--;
 }
 
@@ -43702,10 +46918,10 @@
 /*
 ** Reclaim the memory used by an index
 */
 static void freeIndex(Index *p){
-  sqliteFree(p->zColAff);
-  sqliteFree(p);
+  sqlite3_free(p->zColAff);
+  sqlite3_free(p);
 }
 
 /*
 ** Remove the given index from the index hash table, and free
@@ -43792,9 +47008,9 @@
   }
   for(i=j=2; i<db->nDb; i++){
     struct Db *pDb = &db->aDb[i];
     if( pDb->pBt==0 ){
-      sqliteFree(pDb->zName);
+      sqlite3_free(pDb->zName);
       pDb->zName = 0;
       continue;
     }
     if( j<i ){
@@ -43805,9 +47021,9 @@
   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
   db->nDb = j;
   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
-    sqliteFree(db->aDb);
+    sqlite3_free(db->aDb);
     db->aDb = db->aDbStatic;
   }
 }
 
@@ -43826,14 +47042,14 @@
   Column *pCol;
   assert( pTable!=0 );
   if( (pCol = pTable->aCol)!=0 ){
     for(i=0; i<pTable->nCol; i++, pCol++){
-      sqliteFree(pCol->zName);
+      sqlite3_free(pCol->zName);
       sqlite3ExprDelete(pCol->pDflt);
-      sqliteFree(pCol->zType);
-      sqliteFree(pCol->zColl);
-    }
-    sqliteFree(pTable->aCol);
+      sqlite3_free(pCol->zType);
+      sqlite3_free(pCol->zColl);
+    }
+    sqlite3_free(pTable->aCol);
   }
   pTable->aCol = 0;
   pTable->nCol = 0;
 }
@@ -43876,23 +47092,23 @@
   for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
     pNextFKey = pFKey->pNextFrom;
     assert( sqlite3HashFind(&pTable->pSchema->aFKey,
                            pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
-    sqliteFree(pFKey);
+    sqlite3_free(pFKey);
   }
 #endif
 
   /* Delete the Table structure itself.
   */
   sqliteResetColumnNames(pTable);
-  sqliteFree(pTable->zName);
-  sqliteFree(pTable->zColAff);
+  sqlite3_free(pTable->zName);
+  sqlite3_free(pTable->zColAff);
   sqlite3SelectDelete(pTable->pSelect);
 #ifndef SQLITE_OMIT_CHECK
   sqlite3ExprDelete(pTable->pCheck);
 #endif
   sqlite3VtabClear(pTable);
-  sqliteFree(pTable);
+  sqlite3_free(pTable);
 }
 
 /*
 ** Unlink the given table from the hash tables and the delete the
@@ -43937,12 +47153,12 @@
 ** Tokens are often just pointers into the original SQL text and so
 ** are not \000 terminated and are not persistent.  The returned string
 ** is \000 terminated and is persistent.
 */
-SQLITE_PRIVATE char *sqlite3NameFromToken(Token *pName){
+SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
   char *zName;
   if( pName ){
-    zName = sqliteStrNDup((char*)pName->z, pName->n);
+    zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
     sqlite3Dequote(zName);
   }else{
     zName = 0;
   }
@@ -43972,9 +47188,9 @@
   int n;         /* Number of characters in the name */
   Db *pDb;       /* A database whose name space is being searched */
   char *zName;   /* Name we are searching for */
 
-  zName = sqlite3NameFromToken(pName);
+  zName = sqlite3NameFromToken(db, pName);
   if( zName ){
     n = strlen(zName);
     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
       if( (!OMIT_TEMPDB || i!=1 ) && n==strlen(pDb->zName) &&
@@ -43981,9 +47197,9 @@
           0==sqlite3StrICmp(pDb->zName, zName) ){
         break;
       }
     }
-    sqliteFree(zName);
+    sqlite3_free(zName);
   }
   return i;
 }
 
@@ -44104,9 +47320,9 @@
   }
   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
 
   pParse->sNameToken = *pName;
-  zName = sqlite3NameFromToken(pName);
+  zName = sqlite3NameFromToken(db, pName);
   if( zName==0 ) return;
   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
     goto begin_table_error;
   }
@@ -44161,10 +47377,11 @@
       goto begin_table_error;
     }
   }
 
-  pTable = sqliteMalloc( sizeof(Table) );
+  pTable = sqlite3DbMallocZero(db, sizeof(Table));
   if( pTable==0 ){
+    db->mallocFailed = 1;
     pParse->rc = SQLITE_NOMEM;
     pParse->nErr++;
     goto begin_table_error;
   }
@@ -44207,8 +47424,9 @@
     /* If the file format and encoding in the database have not been set,
     ** set them now.
     */
     sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1);   /* file_format */
+    sqlite3VdbeUsesBtree(v, iDb);
     lbl = sqlite3VdbeMakeLabel(v);
     sqlite3VdbeAddOp(v, OP_If, 0, lbl);
     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
                   1 : SQLITE_MAX_FILE_FORMAT;
@@ -44247,9 +47465,9 @@
   return;
 
   /* If an error occurs, we jump here */
 begin_table_error:
-  sqliteFree(zName);
+  sqlite3_free(zName);
   return;
 }
 
 /*
@@ -44282,22 +47500,22 @@
   if( p->nCol+1>SQLITE_MAX_COLUMN ){
     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
     return;
   }
-  z = sqlite3NameFromToken(pName);
+  z = sqlite3NameFromToken(pParse->db, pName);
   if( z==0 ) return;
   for(i=0; i<p->nCol; i++){
     if( STRICMP(z, p->aCol[i].zName) ){
       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
-      sqliteFree(z);
+      sqlite3_free(z);
       return;
     }
   }
   if( (p->nCol & 0x7)==0 ){
     Column *aNew;
-    aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
+    aNew = sqlite3DbRealloc(pParse->db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
     if( aNew==0 ){
-      sqliteFree(z);
+      sqlite3_free(z);
       return;
     }
     p->aCol = aNew;
   }
@@ -44407,10 +47625,10 @@
   if( (p = pParse->pNewTable)==0 ) return;
   i = p->nCol-1;
   if( i<0 ) return;
   pCol = &p->aCol[i];
-  sqliteFree(pCol->zType);
-  pCol->zType = sqlite3NameFromToken(pType);
+  sqlite3_free(pCol->zType);
+  pCol->zType = sqlite3NameFromToken(pParse->db, pType);
   pCol->affinity = sqlite3AffinityType(pType);
 }
 
 /*
@@ -44432,12 +47650,13 @@
       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
           pCol->zName);
     }else{
       Expr *pCopy;
+      sqlite3 *db = pParse->db;
       sqlite3ExprDelete(pCol->pDflt);
-      pCol->pDflt = pCopy = sqlite3ExprDup(pExpr);
+      pCol->pDflt = pCopy = sqlite3ExprDup(db, pExpr);
       if( pCopy ){
-        sqlite3TokenCopy(&pCopy->span, &pExpr->span);
+        sqlite3TokenCopy(db, &pCopy->span, &pExpr->span);
       }
     }
   }
   sqlite3ExprDelete(pExpr);
@@ -44525,13 +47744,15 @@
   Expr *pCheckExpr  /* The check expression */
 ){
 #ifndef SQLITE_OMIT_CHECK
   Table *pTab = pParse->pNewTable;
+  sqlite3 *db = pParse->db;
   if( pTab && !IN_DECLARE_VTAB ){
     /* The CHECK expression must be duplicated so that tokens refer
     ** to malloced space and not the (ephemeral) text of the CREATE TABLE
     ** statement */
-    pTab->pCheck = sqlite3ExprAnd(pTab->pCheck, sqlite3ExprDup(pCheckExpr));
+    pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck,
+                                  sqlite3ExprDup(db, pCheckExpr));
   }
 #endif
   sqlite3ExprDelete(pCheckExpr);
 }
@@ -44548,9 +47769,9 @@
   i = p->nCol-1;
 
   if( sqlite3LocateCollSeq(pParse, zType, nType) ){
     Index *pIdx;
-    p->aCol[i].zColl = sqliteStrNDup(zType, nType);
+    p->aCol[i].zColl = sqlite3DbStrNDup(pParse->db, zType, nType);
 
     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
     ** then an index may have been created on this column before the
     ** collation type was added. Correct this if it is the case.
@@ -44692,9 +47913,9 @@
     zSep2 = ",\n  ";
     zEnd = "\n)";
   }
   n += 35 + 6*p->nCol;
-  zStmt = sqliteMallocRaw( n );
+  zStmt = sqlite3_malloc( n );
   if( zStmt==0 ) return 0;
   sqlite3_snprintf(n, zStmt,
                   !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE ");
   k = strlen(zStmt);
@@ -44745,9 +47966,9 @@
   Table *p;
   sqlite3 *db = pParse->db;
   int iDb;
 
-  if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3MallocFailed() ) {
+  if( (pEnd==0 && pSelect==0) || pParse->nErr || db->mallocFailed ) {
     return;
   }
   p = pParse->pNewTable;
   if( p==0 ) return;
@@ -44857,12 +48078,14 @@
     }
 
     /* Compute the complete text of the CREATE statement */
     if( pSelect ){
-      zStmt = createTableStmt(p, p->pSchema==pParse->db->aDb[1].pSchema);
+      zStmt = createTableStmt(p, p->pSchema==db->aDb[1].pSchema);
     }else{
       n = pEnd->z - pParse->sNameToken.z + 1;
-      zStmt = sqlite3MPrintf("CREATE %s %.*s", zType2, n, pParse->sNameToken.z);
+      zStmt = sqlite3MPrintf(db,
+          "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
+      );
     }
 
     /* A slot for the record has already been allocated in the
     ** SQLITE_MASTER table.  We just need to update that slot with all
@@ -44879,9 +48102,9 @@
       p->zName,
       p->zName,
       zStmt
     );
-    sqliteFree(zStmt);
+    sqlite3_free(zStmt);
     sqlite3ChangeCookie(db, v, iDb);
 
 #ifndef SQLITE_OMIT_AUTOINCREMENT
     /* Check to see if we need to create an sqlite_sequence table for
@@ -44899,9 +48122,9 @@
 #endif
 
     /* Reparse everything to update our internal data structures */
     sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
-        sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC);
+        sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P3_DYNAMIC);
   }
 
 
   /* Add the table to the in-memory representation of the database.
@@ -44912,15 +48135,20 @@
     Schema *pSchema = p->pSchema;
     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, strlen(p->zName)+1,p);
     if( pOld ){
       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
+      db->mallocFailed = 1;
       return;
     }
 #ifndef SQLITE_OMIT_FOREIGN_KEY
     for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
+      void *data;
       int nTo = strlen(pFKey->zTo) + 1;
       pFKey->pNextTo = sqlite3HashFind(&pSchema->aFKey, pFKey->zTo, nTo);
-      sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey);
+      data = sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey);
+      if( data==(void *)pFKey ){
+        db->mallocFailed = 1;
+      }
     }
 #endif
     pParse->pNewTable = 0;
     db->nTable++;
@@ -44960,8 +48188,9 @@
   Token sEnd;
   DbFixer sFix;
   Token *pName;
   int iDb;
+  sqlite3 *db = pParse->db;
 
   if( pParse->nVar>0 ){
     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
     sqlite3SelectDelete(pSelect);
@@ -44973,9 +48202,9 @@
     sqlite3SelectDelete(pSelect);
     return;
   }
   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
-  iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
+  iDb = sqlite3SchemaToIndex(db, p->pSchema);
   if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
     && sqlite3FixSelect(&sFix, pSelect)
   ){
     sqlite3SelectDelete(pSelect);
@@ -44986,14 +48215,14 @@
   ** This will force all the Expr.token.z values to be dynamically
   ** allocated rather than point to the input string - which means that
   ** they will persist after the current sqlite3_exec() call returns.
   */
-  p->pSelect = sqlite3SelectDup(pSelect);
+  p->pSelect = sqlite3SelectDup(db, pSelect);
   sqlite3SelectDelete(pSelect);
-  if( sqlite3MallocFailed() ){
-    return;
-  }
-  if( !pParse->db->init.busy ){
+  if( db->mallocFailed ){
+    return;
+  }
+  if( !db->init.busy ){
     sqlite3ViewGetColumnNames(pParse, p);
   }
 
   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
@@ -45026,8 +48255,9 @@
   Table *pSelTab;   /* A fake table from which we get the result set */
   Select *pSel;     /* Copy of the SELECT that implements the view */
   int nErr = 0;     /* Number of errors encountered */
   int n;            /* Temporarily holds the number of cursors assigned */
+  sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
 
   assert( pTable );
 
 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -45066,9 +48296,9 @@
   ** to be permanent.  So the computation is done on a copy of the SELECT
   ** statement that defines the view.
   */
   assert( pTable->pSelect );
-  pSel = sqlite3SelectDup(pTable->pSelect);
+  pSel = sqlite3SelectDup(db, pTable->pSelect);
   if( pSel ){
     n = pParse->nTab;
     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
     pTable->nCol = -1;
@@ -45245,9 +48475,9 @@
   Vdbe *v;
   sqlite3 *db = pParse->db;
   int iDb;
 
-  if( pParse->nErr || sqlite3MallocFailed() ){
+  if( pParse->nErr || db->mallocFailed ){
     goto exit_drop_table;
   }
   assert( pName->nSrc==1 );
   pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
@@ -45448,10 +48678,12 @@
     for(i=0; i<pToCol->nExpr; i++){
       nByte += strlen(pToCol->a[i].zName) + 1;
     }
   }
-  pFKey = sqliteMalloc( nByte );
-  if( pFKey==0 ) goto fk_end;
+  pFKey = sqlite3DbMallocZero(pParse->db, nByte );
+  if( pFKey==0 ){
+    goto fk_end;
+  }
   pFKey->pFrom = p;
   pFKey->pNextFrom = p->pFKey;
   z = (char*)&pFKey[1];
   pFKey->aCol = (struct sColMap*)z;
@@ -45500,9 +48732,9 @@
   p->pFKey = pFKey;
   pFKey = 0;
 
 fk_end:
-  sqliteFree(pFKey);
+  sqlite3_free(pFKey);
 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
   sqlite3ExprListDelete(pFromCol);
   sqlite3ExprListDelete(pToCol);
 }
@@ -45541,13 +48773,14 @@
   int addr1;                     /* Address of top of loop */
   int tnum;                      /* Root page of index */
   Vdbe *v;                       /* Generate code into this virtual machine */
   KeyInfo *pKey;                 /* KeyInfo for index */
-  int iDb = sqlite3SchemaToIndex(pParse->db, pIndex->pSchema);
+  sqlite3 *db = pParse->db;      /* The database connection */
+  int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
 
 #ifndef SQLITE_OMIT_AUTHORIZATION
   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
-      pParse->db->aDb[iDb].zName ) ){
+      db->aDb[iDb].zName ) ){
     return;
   }
 #endif
 
@@ -45577,9 +48810,9 @@
     sqlite3VdbeAddOp(v, OP_AddImm, 1, 0);
     sqlite3VdbeAddOp(v, OP_IsUnique, iIdx, addr2);
     sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort,
                     "indexed columns are not unique", P3_STATIC);
-    assert( sqlite3MallocFailed() || addr2==sqlite3VdbeCurrentAddr(v) );
+    assert( db->mallocFailed || addr2==sqlite3VdbeCurrentAddr(v) );
   }
   sqlite3VdbeAddOp(v, OP_IdxInsert, iIdx, 0);
   sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1);
   sqlite3VdbeJumpHere(v, addr1);
@@ -45627,9 +48860,9 @@
   int nCol;
   int nExtra = 0;
   char *zExtra;
 
-  if( pParse->nErr || sqlite3MallocFailed() || IN_DECLARE_VTAB ){
+  if( pParse->nErr || db->mallocFailed || IN_DECLARE_VTAB ){
     goto exit_create_index;
   }
 
   /*
@@ -45705,9 +48938,9 @@
   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
   ** own name.
   */
   if( pName ){
-    zName = sqlite3NameFromToken(pName);
+    zName = sqlite3NameFromToken(db, pName);
     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
     if( zName==0 ) goto exit_create_index;
     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
       goto exit_create_index;
@@ -45732,9 +48965,12 @@
     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
     sqlite3_snprintf(sizeof(zBuf),zBuf,"_%d",n);
     zName = 0;
     sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
-    if( zName==0 ) goto exit_create_index;
+    if( zName==0 ){
+      db->mallocFailed = 1;
+      goto exit_create_index;
+    }
   }
 
   /* Check for authorization to create an index.
   */
@@ -45758,9 +48994,9 @@
   */
   if( pList==0 ){
     nullId.z = (u8*)pTab->aCol[pTab->nCol-1].zName;
     nullId.n = strlen((char*)nullId.z);
-    pList = sqlite3ExprListAppend(0, 0, &nullId);
+    pList = sqlite3ExprListAppend(pParse, 0, 0, &nullId);
     if( pList==0 ) goto exit_create_index;
     pList->a[0].sortOrder = sortOrder;
   }
 
@@ -45778,9 +49014,9 @@
   ** Allocate the index structure.
   */
   nName = strlen(zName);
   nCol = pList->nExpr;
-  pIndex = sqliteMalloc(
+  pIndex = sqlite3DbMallocZero(db,
       sizeof(Index) +              /* Index structure  */
       sizeof(int)*nCol +           /* Index.aiColumn   */
       sizeof(int)*(nCol+1) +       /* Index.aiRowEst   */
       sizeof(char *)*nCol +        /* Index.azColl     */
@@ -45787,9 +49023,11 @@
       sizeof(u8)*nCol +            /* Index.aSortOrder */
       nName + 1 +                  /* Index.zName      */
       nExtra                       /* Collation sequence names */
   );
-  if( sqlite3MallocFailed() ) goto exit_create_index;
+  if( db->mallocFailed ){
+    goto exit_create_index;
+  }
   pIndex->azColl = (char**)(&pIndex[1]);
   pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
   pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
   pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
@@ -45914,8 +49152,9 @@
     p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
                          pIndex->zName, strlen(pIndex->zName)+1, pIndex);
     if( p ){
       assert( p==pIndex );  /* Malloc must have failed */
+      db->mallocFailed = 1;
       goto exit_create_index;
     }
     db->flags |= SQLITE_InternChanges;
     if( pTblName!=0 ){
@@ -45957,9 +49196,9 @@
     ** the zStmt variable
     */
     if( pStart && pEnd ){
       /* A named index with an explicit CREATE INDEX statement */
-      zStmt = sqlite3MPrintf("CREATE%s INDEX %.*s",
+      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
         onError==OE_None ? "" : " UNIQUE",
         pEnd->z - pName->z + 1,
         pName->z);
     }else{
@@ -45977,9 +49216,9 @@
         pTab->zName,
         zStmt
     );
     sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
-    sqliteFree(zStmt);
+    sqlite3_free(zStmt);
 
     /* Fill the index with data and reparse the schema. Code an OP_Expire
     ** to invalidate all pre-compiled statements.
     */
@@ -45986,9 +49225,9 @@
     if( pTblName ){
       sqlite3RefillIndex(pParse, pIndex, iMem);
       sqlite3ChangeCookie(db, v, iDb);
       sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
-         sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC);
+         sqlite3MPrintf(db, "name='%q'", pIndex->zName), P3_DYNAMIC);
       sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
     }
   }
 
@@ -46019,9 +49258,9 @@
     freeIndex(pIndex);
   }
   sqlite3ExprListDelete(pList);
   sqlite3SrcListDelete(pTblName);
-  sqliteFree(zName);
+  sqlite3_free(zName);
   return;
 }
 
 /*
@@ -46032,8 +49271,9 @@
   Vdbe *v;
   v = sqlite3GetVdbe(pParse);
   if( v ){
     sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1);
+    sqlite3VdbeUsesBtree(v, iDb);
     sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
     sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3);
     sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0);
     sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
@@ -46084,9 +49324,9 @@
   Vdbe *v;
   sqlite3 *db = pParse->db;
   int iDb;
 
-  if( pParse->nErr || sqlite3MallocFailed() ){
+  if( pParse->nErr || db->mallocFailed ){
     goto exit_drop_index;
   }
   assert( pName->nSrc==1 );
   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
@@ -46154,8 +49394,9 @@
 ** might be the same as the pArray parameter or it might be a different
 ** pointer if the array was resized.
 */
 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
+  sqlite3 *db,      /* Connection to notify of malloc failures */
   void *pArray,     /* Array of objects.  Might be reallocated */
   int szEntry,      /* Size of each object in the array */
   int initSize,     /* Suggested initial allocation, in elements */
   int *pnEntry,     /* Number of objects currently in use */
@@ -46166,9 +49407,9 @@
   if( *pnEntry >= *pnAlloc ){
     void *pNew;
     int newSize;
     newSize = (*pnAlloc)*2 + initSize;
-    pNew = sqliteRealloc(pArray, newSize*szEntry);
+    pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
     if( pNew==0 ){
       *pIdx = -1;
       return pArray;
     }
@@ -46187,16 +49428,17 @@
 ** need be.
 **
 ** A new IdList is returned, or NULL if malloc() fails.
 */
-SQLITE_PRIVATE IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
+SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
   int i;
   if( pList==0 ){
-    pList = sqliteMalloc( sizeof(IdList) );
+    pList = sqlite3DbMallocZero(db, sizeof(IdList) );
     if( pList==0 ) return 0;
     pList->nAlloc = 0;
   }
   pList->a = sqlite3ArrayAllocate(
+      db,
       pList->a,
       sizeof(pList->a[0]),
       5,
       &pList->nId,
@@ -46206,9 +49448,9 @@
   if( i<0 ){
     sqlite3IdListDelete(pList);
     return 0;
   }
-  pList->a[i].zName = sqlite3NameFromToken(pToken);
+  pList->a[i].zName = sqlite3NameFromToken(db, pToken);
   return pList;
 }
 
 /*
@@ -46217,12 +49459,12 @@
 SQLITE_PRIVATE void sqlite3IdListDelete(IdList *pList){
   int i;
   if( pList==0 ) return;
   for(i=0; i<pList->nId; i++){
-    sqliteFree(pList->a[i].zName);
-  }
-  sqliteFree(pList->a);
-  sqliteFree(pList);
+    sqlite3_free(pList->a[i].zName);
+  }
+  sqlite3_free(pList->a);
+  sqlite3_free(pList);
 }
 
 /*
 ** Return the index in pList of the identifier named zId.  Return -1
@@ -46252,28 +49494,33 @@
 ** or with NULL if no database is specified.
 **
 ** In other words, if call like this:
 **
-**         sqlite3SrcListAppend(A,B,0);
+**         sqlite3SrcListAppend(D,A,B,0);
 **
 ** Then B is a table name and the database name is unspecified.  If called
 ** like this:
 **
-**         sqlite3SrcListAppend(A,B,C);
+**         sqlite3SrcListAppend(D,A,B,C);
 **
 ** Then C is the table name and B is the database name.
 */
-SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
+SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
+  sqlite3 *db,        /* Connection to notify of malloc failures */
+  SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
+  Token *pTable,      /* Table to append */
+  Token *pDatabase    /* Database of the table */
+){
   struct SrcList_item *pItem;
   if( pList==0 ){
-    pList = sqliteMalloc( sizeof(SrcList) );
+    pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
     if( pList==0 ) return 0;
     pList->nAlloc = 1;
   }
   if( pList->nSrc>=pList->nAlloc ){
     SrcList *pNew;
     pList->nAlloc *= 2;
-    pNew = sqliteRealloc(pList,
+    pNew = sqlite3DbRealloc(db, pList,
                sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
     if( pNew==0 ){
       sqlite3SrcListDelete(pList);
       return 0;
@@ -46289,10 +49536,10 @@
     Token *pTemp = pDatabase;
     pDatabase = pTable;
     pTable = pTemp;
   }
-  pItem->zName = sqlite3NameFromToken(pTable);
-  pItem->zDatabase = sqlite3NameFromToken(pDatabase);
+  pItem->zName = sqlite3NameFromToken(db, pTable);
+  pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
   pItem->iCursor = -1;
   pItem->isPopulated = 0;
   pList->nSrc++;
   return pList;
@@ -46303,9 +49550,9 @@
 */
 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
   int i;
   struct SrcList_item *pItem;
-  assert(pList || sqlite3MallocFailed() );
+  assert(pList || pParse->db->mallocFailed );
   if( pList ){
     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
       if( pItem->iCursor>=0 ) break;
       pItem->iCursor = pParse->nTab++;
@@ -46323,17 +49570,17 @@
   int i;
   struct SrcList_item *pItem;
   if( pList==0 ) return;
   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
-    sqliteFree(pItem->zDatabase);
-    sqliteFree(pItem->zName);
-    sqliteFree(pItem->zAlias);
+    sqlite3_free(pItem->zDatabase);
+    sqlite3_free(pItem->zName);
+    sqlite3_free(pItem->zAlias);
     sqlite3DeleteTable(pItem->pTab);
     sqlite3SelectDelete(pItem->pSelect);
     sqlite3ExprDelete(pItem->pOn);
     sqlite3IdListDelete(pItem->pUsing);
   }
-  sqliteFree(pList);
+  sqlite3_free(pList);
 }
 
 /*
 ** This routine is called by the parser to add a new term to the
@@ -46351,8 +49598,9 @@
 ** Return a new SrcList which encodes is the FROM with the new
 ** term added.
 */
 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
+  Parse *pParse,          /* Parsing context */
   SrcList *p,             /* The left part of the FROM clause already seen */
   Token *pTable,          /* Name of the table to add to the FROM clause */
   Token *pDatabase,       /* Name of the database containing pTable */
   Token *pAlias,          /* The right-hand side of the AS subexpression */
@@ -46360,9 +49608,10 @@
   Expr *pOn,              /* The ON clause of a join */
   IdList *pUsing          /* The USING clause of a join */
 ){
   struct SrcList_item *pItem;
-  p = sqlite3SrcListAppend(p, pTable, pDatabase);
+  sqlite3 *db = pParse->db;
+  p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
   if( p==0 || p->nSrc==0 ){
     sqlite3ExprDelete(pOn);
     sqlite3IdListDelete(pUsing);
     sqlite3SelectDelete(pSubquery);
@@ -46369,9 +49618,9 @@
     return p;
   }
   pItem = &p->a[p->nSrc-1];
   if( pAlias && pAlias->n ){
-    pItem->zAlias = sqlite3NameFromToken(pAlias);
+    pItem->zAlias = sqlite3NameFromToken(db, pAlias);
   }
   pItem->pSelect = pSubquery;
   pItem->pOn = pOn;
   pItem->pUsing = pUsing;
@@ -46411,16 +49660,17 @@
   Vdbe *v;
   int i;
 
   if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
-  if( pParse->nErr || sqlite3MallocFailed() ) return;
+  if( pParse->nErr || db->mallocFailed ) return;
   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
 
   v = sqlite3GetVdbe(pParse);
   if( !v ) return;
   if( type!=TK_DEFERRED ){
     for(i=0; i<db->nDb; i++){
       sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
+      sqlite3VdbeUsesBtree(v, i);
     }
   }
   sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
 }
@@ -46432,9 +49682,9 @@
   sqlite3 *db;
   Vdbe *v;
 
   if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
-  if( pParse->nErr || sqlite3MallocFailed() ) return;
+  if( pParse->nErr || db->mallocFailed ) return;
   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
 
   v = sqlite3GetVdbe(pParse);
   if( v ){
@@ -46449,9 +49699,9 @@
   sqlite3 *db;
   Vdbe *v;
 
   if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
-  if( pParse->nErr || sqlite3MallocFailed() ) return;
+  if( pParse->nErr || db->mallocFailed ) return;
   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
 
   v = sqlite3GetVdbe(pParse);
   if( v ){
@@ -46465,9 +49715,17 @@
 */
 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
   sqlite3 *db = pParse->db;
   if( db->aDb[1].pBt==0 && !pParse->explain ){
-    int rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE,
+    int rc;
+    static const int flags =
+          SQLITE_OPEN_READWRITE |
+          SQLITE_OPEN_CREATE |
+          SQLITE_OPEN_EXCLUSIVE |
+          SQLITE_OPEN_DELETEONCLOSE |
+          SQLITE_OPEN_TEMP_DB;
+
+    rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, flags,
                                  &db->aDb[1].pBt);
     if( rc!=SQLITE_OK ){
       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
         "file for storing temporary tables");
@@ -46661,29 +49919,29 @@
   }else if( pName2==0 || pName2->z==0 ){
     assert( pName1->z );
     pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0);
     if( pColl ){
-      char *zColl = sqliteStrNDup((const char *)pName1->z, pName1->n);
+      char *zColl = sqlite3DbStrNDup(db, (const char *)pName1->z, pName1->n);
       if( zColl ){
         reindexDatabases(pParse, zColl);
-        sqliteFree(zColl);
+        sqlite3_free(zColl);
       }
       return;
     }
   }
   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
   if( iDb<0 ) return;
-  z = sqlite3NameFromToken(pObjName);
+  z = sqlite3NameFromToken(db, pObjName);
   if( z==0 ) return;
   zDb = db->aDb[iDb].zName;
   pTab = sqlite3FindTable(db, z, zDb);
   if( pTab ){
     reindexTable(pParse, pTab, 0);
-    sqliteFree(z);
+    sqlite3_free(z);
     return;
   }
   pIndex = sqlite3FindIndex(db, z, zDb);
-  sqliteFree(z);
+  sqlite3_free(z);
   if( pIndex ){
     sqlite3BeginWriteOperation(pParse, 0, iDb);
     sqlite3RefillIndex(pParse, pIndex, -1);
     return;
@@ -46696,9 +49954,9 @@
 ** Return a dynamicly allocated KeyInfo structure that can be used
 ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
 **
 ** If successful, a pointer to the new structure is returned. In this case
-** the caller is responsible for calling sqliteFree() on the returned
+** the caller is responsible for calling sqlite3_free() on the returned
 ** pointer. If an error occurs (out of memory or missing collation
 ** sequence), NULL is returned and the state of pParse updated to reflect
 ** the error.
 */
@@ -46705,11 +49963,12 @@
 SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
   int i;
   int nCol = pIdx->nColumn;
   int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
-  KeyInfo *pKey = (KeyInfo *)sqliteMalloc(nBytes);
+  KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(pParse->db, nBytes);
 
   if( pKey ){
+    pKey->db = pParse->db;
     pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
     assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
     for(i=0; i<nCol; i++){
       char *zColl = pIdx->azColl[i];
@@ -46720,9 +49979,9 @@
     pKey->nField = nCol;
   }
 
   if( pParse->nErr ){
-    sqliteFree(pKey);
+    sqlite3_free(pKey);
     pKey = 0;
   }
   return pKey;
 }
@@ -46743,9 +50002,9 @@
 **
 ** This file contains functions used to access the internal hash tables
 ** of user defined functions and collation sequences.
 **
-** $Id: callback.c,v 1.18 2007/05/07 09:32:45 danielk1977 Exp $
+** $Id: callback.c,v 1.23 2007/08/29 12:31:26 danielk1977 Exp $
 */
 
 
 /*
@@ -46756,17 +50015,17 @@
 static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
   assert( !db->xCollNeeded || !db->xCollNeeded16 );
   if( nName<0 ) nName = strlen(zName);
   if( db->xCollNeeded ){
-    char *zExternal = sqliteStrNDup(zName, nName);
+    char *zExternal = sqlite3DbStrNDup(db, zName, nName);
     if( !zExternal ) return;
     db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal);
-    sqliteFree(zExternal);
+    sqlite3_free(zExternal);
   }
 #ifndef SQLITE_OMIT_UTF16
   if( db->xCollNeeded16 ){
     char const *zExternal;
-    sqlite3_value *pTmp = sqlite3ValueNew();
+    sqlite3_value *pTmp = sqlite3ValueNew(db);
     sqlite3ValueSetStr(pTmp, nName, zName, SQLITE_UTF8, SQLITE_STATIC);
     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
     if( zExternal ){
       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
@@ -46891,9 +50150,9 @@
   if( nName<0 ) nName = strlen(zName);
   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
 
   if( 0==pColl && create ){
-    pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
+    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
     if( pColl ){
       CollSeq *pDel = 0;
       pColl[0].zName = (char*)&pColl[3];
       pColl[0].enc = SQLITE_UTF8;
@@ -46908,11 +50167,12 @@
       /* If a malloc() failure occured in sqlite3HashInsert(), it will
       ** return the pColl pointer to be deleted (because it wasn't added
       ** to the hash table).
       */
-      assert( !pDel || (sqlite3MallocFailed() && pDel==pColl) );
-      if( pDel ){
-        sqliteFree(pDel);
+      assert( pDel==0 || pDel==pColl );
+      if( pDel!=0 ){
+        db->mallocFailed = 1;
+        sqlite3_free(pDel);
         pColl = 0;
       }
     }
   }
@@ -47032,16 +50292,17 @@
   ** exact match for the name, number of arguments and encoding, then add a
   ** new entry to the hash table and return it.
   */
   if( createFlag && bestmatch<6 &&
-      (pBest = sqliteMalloc(sizeof(*pBest)+nName))!=0 ){
+      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName))!=0 ){
     pBest->nArg = nArg;
     pBest->pNext = pFirst;
     pBest->iPrefEnc = enc;
     memcpy(pBest->zName, zName, nName);
     pBest->zName[nName] = 0;
     if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
-      sqliteFree(pBest);
+      db->mallocFailed = 1;
+      sqlite3_free(pBest);
       return 0;
     }
   }
 
@@ -47052,9 +50313,9 @@
 }
 
 /*
 ** Free all resources held by the schema structure. The void* argument points
-** at a Schema struct. This function does not call sqliteFree() on the
+** at a Schema struct. This function does not call sqlite3_free() on the
 ** pointer itself, it just cleans up subsiduary resources (i.e. the contents
 ** of the schema hash tables).
 */
 SQLITE_PRIVATE void sqlite3SchemaFree(void *p){
@@ -47085,16 +50346,18 @@
 /*
 ** Find and return the schema associated with a BTree.  Create
 ** a new one if necessary.
 */
-SQLITE_PRIVATE Schema *sqlite3SchemaGet(Btree *pBt){
+SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
   Schema * p;
   if( pBt ){
-    p = (Schema *)sqlite3BtreeSchema(pBt,sizeof(Schema),sqlite3SchemaFree);
-  }else{
-    p = (Schema *)sqliteMalloc(sizeof(Schema));
-  }
-  if( p && 0==p->file_format ){
+    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree);
+  }else{
+    p = (Schema *)sqlite3MallocZero(sizeof(Schema));
+  }
+  if( !p ){
+    db->mallocFailed = 1;
+  }else if ( 0==p->file_format ){
     sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
     sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
     sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
     sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
@@ -47103,9 +50366,9 @@
   return p;
 }
 
 /************** End of callback.c ********************************************/
-/************** Begin file complete.c ****************************************/
+/************** Begin file delete.c ******************************************/
 /*
 ** 2001 September 15
 **
 ** The author disclaims copyright to this source code.  In place of
@@ -47115,277 +50378,12 @@
 **    May you find forgiveness for yourself and forgive others.
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** An tokenizer for SQL
-**
-** This file contains C code that implements the sqlite3_complete() API.
-** This code used to be part of the tokenizer.c source file.  But by
-** separating it out, the code will be automatically omitted from
-** static links that do not use it.
-**
-** $Id: complete.c,v 1.3 2006/01/18 15:25:17 danielk1977 Exp $
-*/
-#ifndef SQLITE_OMIT_COMPLETE
-
-/*
-** This is defined in tokenize.c.  We just have to import the definition.
-*/
-extern const char sqlite3IsIdChar[];
-#define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20]))
-
-
-/*
-** Token types used by the sqlite3_complete() routine.  See the header
-** comments on that procedure for additional information.
-*/
-#define tkSEMI    0
-#define tkWS      1
-#define tkOTHER   2
-#define tkEXPLAIN 3
-#define tkCREATE  4
-#define tkTEMP    5
-#define tkTRIGGER 6
-#define tkEND     7
-
-/*
-** Return TRUE if the given SQL string ends in a semicolon.
-**
-** Special handling is require for CREATE TRIGGER statements.
-** Whenever the CREATE TRIGGER keywords are seen, the statement
-** must end with ";END;".
-**
-** This implementation uses a state machine with 7 states:
-**
-**   (0) START     At the beginning or end of an SQL statement.  This routine
-**                 returns 1 if it ends in the START state and 0 if it ends
-**                 in any other state.
-**
-**   (1) NORMAL    We are in the middle of statement which ends with a single
-**                 semicolon.
-**
-**   (2) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
-**                 a statement.
-**
-**   (3) CREATE    The keyword CREATE has been seen at the beginning of a
-**                 statement, possibly preceeded by EXPLAIN and/or followed by
-**                 TEMP or TEMPORARY
-**
-**   (4) TRIGGER   We are in the middle of a trigger definition that must be
-**                 ended by a semicolon, the keyword END, and another semicolon.
-**
-**   (5) SEMI      We've seen the first semicolon in the ";END;" that occurs at
-**                 the end of a trigger definition.
-**
-**   (6) END       We've seen the ";END" of the ";END;" that occurs at the end
-**                 of a trigger difinition.
-**
-** Transitions between states above are determined by tokens extracted
-** from the input.  The following tokens are significant:
-**
-**   (0) tkSEMI      A semicolon.
-**   (1) tkWS        Whitespace
-**   (2) tkOTHER     Any other SQL token.
-**   (3) tkEXPLAIN   The "explain" keyword.
-**   (4) tkCREATE    The "create" keyword.
-**   (5) tkTEMP      The "temp" or "temporary" keyword.
-**   (6) tkTRIGGER   The "trigger" keyword.
-**   (7) tkEND       The "end" keyword.
-**
-** Whitespace never causes a state transition and is always ignored.
-**
-** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
-** to recognize the end of a trigger can be omitted.  All we have to do
-** is look for a semicolon that is not part of an string or comment.
-*/
-SQLITE_API int sqlite3_complete(const char *zSql){
-  u8 state = 0;   /* Current state, using numbers defined in header comment */
-  u8 token;       /* Value of the next token */
-
-#ifndef SQLITE_OMIT_TRIGGER
-  /* A complex statement machine used to detect the end of a CREATE TRIGGER
-  ** statement.  This is the normal case.
-  */
-  static const u8 trans[7][8] = {
-                     /* Token:                                                */
-     /* State:       **  SEMI  WS  OTHER EXPLAIN  CREATE  TEMP  TRIGGER  END  */
-     /* 0   START: */ {    0,  0,     1,      2,      3,    1,       1,   1,  },
-     /* 1  NORMAL: */ {    0,  1,     1,      1,      1,    1,       1,   1,  },
-     /* 2 EXPLAIN: */ {    0,  2,     1,      1,      3,    1,       1,   1,  },
-     /* 3  CREATE: */ {    0,  3,     1,      1,      1,    3,       4,   1,  },
-     /* 4 TRIGGER: */ {    5,  4,     4,      4,      4,    4,       4,   4,  },
-     /* 5    SEMI: */ {    5,  5,     4,      4,      4,    4,       4,   6,  },
-     /* 6     END: */ {    0,  6,     4,      4,      4,    4,       4,   4,  },
-  };
-#else
-  /* If triggers are not suppored by this compile then the statement machine
-  ** used to detect the end of a statement is much simplier
-  */
-  static const u8 trans[2][3] = {
-                     /* Token:           */
-     /* State:       **  SEMI  WS  OTHER */
-     /* 0   START: */ {    0,  0,     1, },
-     /* 1  NORMAL: */ {    0,  1,     1, },
-  };
-#endif /* SQLITE_OMIT_TRIGGER */
-
-  while( *zSql ){
-    switch( *zSql ){
-      case ';': {  /* A semicolon */
-        token = tkSEMI;
-        break;
-      }
-      case ' ':
-      case '\r':
-      case '\t':
-      case '\n':
-      case '\f': {  /* White space is ignored */
-        token = tkWS;
-        break;
-      }
-      case '/': {   /* C-style comments */
-        if( zSql[1]!='*' ){
-          token = tkOTHER;
-          break;
-        }
-        zSql += 2;
-        while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
-        if( zSql[0]==0 ) return 0;
-        zSql++;
-        token = tkWS;
-        break;
-      }
-      case '-': {   /* SQL-style comments from "--" to end of line */
-        if( zSql[1]!='-' ){
-          token = tkOTHER;
-          break;
-        }
-        while( *zSql && *zSql!='\n' ){ zSql++; }
-        if( *zSql==0 ) return state==0;
-        token = tkWS;
-        break;
-      }
-      case '[': {   /* Microsoft-style identifiers in [...] */
-        zSql++;
-        while( *zSql && *zSql!=']' ){ zSql++; }
-        if( *zSql==0 ) return 0;
-        token = tkOTHER;
-        break;
-      }
-      case '`':     /* Grave-accent quoted symbols used by MySQL */
-      case '"':     /* single- and double-quoted strings */
-      case '\'': {
-        int c = *zSql;
-        zSql++;
-        while( *zSql && *zSql!=c ){ zSql++; }
-        if( *zSql==0 ) return 0;
-        token = tkOTHER;
-        break;
-      }
-      default: {
-        int c;
-        if( IdChar((u8)*zSql) ){
-          /* Keywords and unquoted identifiers */
-          int nId;
-          for(nId=1; IdChar(zSql[nId]); nId++){}
-#ifdef SQLITE_OMIT_TRIGGER
-          token = tkOTHER;
-#else
-          switch( *zSql ){
-            case 'c': case 'C': {
-              if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
-                token = tkCREATE;
-              }else{
-                token = tkOTHER;
-              }
-              break;
-            }
-            case 't': case 'T': {
-              if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
-                token = tkTRIGGER;
-              }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
-                token = tkTEMP;
-              }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
-                token = tkTEMP;
-              }else{
-                token = tkOTHER;
-              }
-              break;
-            }
-            case 'e':  case 'E': {
-              if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
-                token = tkEND;
-              }else
-#ifndef SQLITE_OMIT_EXPLAIN
-              if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
-                token = tkEXPLAIN;
-              }else
-#endif
-              {
-                token = tkOTHER;
-              }
-              break;
-            }
-            default: {
-              token = tkOTHER;
-              break;
-            }
-          }
-#endif /* SQLITE_OMIT_TRIGGER */
-          zSql += nId-1;
-        }else{
-          /* Operators and special symbols */
-          token = tkOTHER;
-        }
-        break;
-      }
-    }
-    state = trans[state][token];
-    zSql++;
-  }
-  return state==0;
-}
-
-#ifndef SQLITE_OMIT_UTF16
-/*
-** This routine is the same as the sqlite3_complete() routine described
-** above, except that the parameter is required to be UTF-16 encoded, not
-** UTF-8.
-*/
-SQLITE_API int sqlite3_complete16(const void *zSql){
-  sqlite3_value *pVal;
-  char const *zSql8;
-  int rc = 0;
-
-  pVal = sqlite3ValueNew();
-  sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
-  zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
-  if( zSql8 ){
-    rc = sqlite3_complete(zSql8);
-  }
-  sqlite3ValueFree(pVal);
-  return sqlite3ApiExit(0, rc);
-}
-#endif /* SQLITE_OMIT_UTF16 */
-#endif /* SQLITE_OMIT_COMPLETE */
-
-/************** End of complete.c ********************************************/
-/************** Begin file delete.c ******************************************/
-/*
-** 2001 September 15
-**
-** The author disclaims copyright to this source code.  In place of
-** a legal notice, here is a blessing:
-**
-**    May you do good and not evil.
-**    May you find forgiveness for yourself and forgive others.
-**    May you share freely, never taking more than you give.
-**
-*************************************************************************
 ** This file contains C code routines that are called by the parser
 ** in order to generate code for DELETE FROM statements.
 **
-** $Id: delete.c,v 1.129 2007/04/16 15:06:25 danielk1977 Exp $
+** $Id: delete.c,v 1.130 2007/08/16 04:30:40 drh Exp $
 */
 
 /*
 ** Look up every table that is named in pSrc.  If any table is not found,
@@ -47485,12 +50483,12 @@
   int triggers_exist = 0;      /* True if any triggers exist */
 #endif
 
   sContext.pParse = 0;
-  if( pParse->nErr || sqlite3MallocFailed() ){
+  db = pParse->db;
+  if( pParse->nErr || db->mallocFailed ){
     goto delete_from_cleanup;
   }
-  db = pParse->db;
   assert( pTabList->nSrc==1 );
 
   /* Locate the table which we want to delete.  This table has to be
   ** put in an SrcList structure because some of the subroutines we
@@ -47566,9 +50564,9 @@
   /* If we are trying to delete from a view, realize that view into
   ** a ephemeral table.
   */
   if( isView ){
-    Select *pView = sqlite3SelectDup(pTab->pSelect);
+    Select *pView = sqlite3SelectDup(db, pTab->pSelect);
     sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
     sqlite3SelectDelete(pView);
   }
 
@@ -47856,11 +50854,10 @@
 ** There is only one exported symbol in this file - the function
 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
 ** All other code has file scope.
 **
-** $Id: func.c,v 1.162 2007/07/23 19:12:42 drh Exp $
-*/
-/* #include <math.h> */
+** $Id: func.c,v 1.174 2007/09/03 11:04:22 danielk1977 Exp $
+*/
 
 
 /*
 ** Return the collating function associated with a function.
@@ -48071,8 +51068,21 @@
   sqlite3_result_double(context, r);
 }
 
 /*
+** Allocate nByte bytes of space using sqlite3_malloc(). If the
+** allocation fails, call sqlite3_result_error_nomem() to notify
+** the database handle that malloc() has failed.
+*/
+static void *contextMalloc(sqlite3_context *context, int nByte){
+  char *z = sqlite3_malloc(nByte);
+  if( !z && nByte>0 ){
+    sqlite3_result_error_nomem(context);
+  }
+  return z;
+}
+
+/*
 ** Implementation of the upper() and lower() SQL functions.
 */
 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   char *z1;
@@ -48083,9 +51093,9 @@
   n = sqlite3_value_bytes(argv[0]);
   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
   assert( z2==(char*)sqlite3_value_text(argv[0]) );
   if( z2 ){
-    z1 = sqlite3_malloc(n+1);
+    z1 = contextMalloc(context, n+1);
     if( z1 ){
       memcpy(z1, z2, n+1);
       for(i=0; z1[i]; i++){
         z1[i] = toupper(z1[i]);
@@ -48103,9 +51113,9 @@
   n = sqlite3_value_bytes(argv[0]);
   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
   assert( z2==(char*)sqlite3_value_text(argv[0]) );
   if( z2 ){
-    z1 = sqlite3_malloc(n+1);
+    z1 = contextMalloc(context, n+1);
     if( z1 ){
       memcpy(z1, z2, n+1);
       for(i=0; z1[i]; i++){
         z1[i] = tolower(z1[i]);
@@ -48168,12 +51178,12 @@
   if( n>SQLITE_MAX_LENGTH ){
     sqlite3_result_error_toobig(context);
     return;
   }
-  p = sqliteMalloc(n);
+  p = contextMalloc(context, n);
   if( p ){
     sqlite3Randomness(n, p);
-    sqlite3_result_blob(context, (char*)p, n, sqlite3FreeX);
+    sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
   }
 }
 
 /*
@@ -48374,9 +51384,9 @@
 ** just a variation of LIKE) gets called.  This is used for testing
 ** only.
 */
 #ifdef SQLITE_TEST
-int sqlite3_like_count = 0;
+SQLITE_API int sqlite3_like_count = 0;
 #endif
 
 
 /*
@@ -48501,12 +51511,10 @@
       if( 2*nBlob+4>SQLITE_MAX_LENGTH ){
         sqlite3_result_error_toobig(context);
         return;
       }
-      zText = (char *)sqliteMalloc((2*nBlob)+4);
-      if( !zText ){
-        sqlite3_result_error(context, "out of memory", -1);
-      }else{
+      zText = (char *)contextMalloc(context, (2*nBlob)+4);
+      if( zText ){
         int i;
         for(i=0; i<nBlob; i++){
           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
@@ -48515,9 +51523,9 @@
         zText[(nBlob*2)+3] = '\0';
         zText[0] = 'X';
         zText[1] = '\'';
         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
-        sqliteFree(zText);
+        sqlite3_free(zText);
       }
       break;
     }
     case SQLITE_TEXT: {
@@ -48531,21 +51539,21 @@
       if( i+n+3>SQLITE_MAX_LENGTH ){
         sqlite3_result_error_toobig(context);
         return;
       }
-      z = sqliteMalloc( i+n+3 );
-      if( z==0 ) return;
-      z[0] = '\'';
-      for(i=0, j=1; zArg[i]; i++){
-        z[j++] = zArg[i];
-        if( zArg[i]=='\'' ){
-          z[j++] = '\'';
-        }
-      }
-      z[j++] = '\'';
-      z[j] = 0;
-      sqlite3_result_text(context, z, j, SQLITE_TRANSIENT);
-      sqliteFree(z);
+      z = contextMalloc(context, i+n+3);
+      if( z ){
+        z[0] = '\'';
+        for(i=0, j=1; zArg[i]; i++){
+          z[j++] = zArg[i];
+          if( zArg[i]=='\'' ){
+            z[j++] = '\'';
+          }
+        }
+        z[j++] = '\'';
+        z[j] = 0;
+        sqlite3_result_text(context, z, j, sqlite3_free);
+      }
     }
   }
 }
 
@@ -48568,17 +51576,18 @@
     sqlite3_result_error_toobig(context);
     return;
   }
   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
-  z = zHex = sqlite3_malloc(n*2 + 1);
-  if( zHex==0 ) return;
-  for(i=0; i<n; i++, pBlob++){
-    unsigned char c = *pBlob;
-    *(z++) = hexdigits[(c>>4)&0xf];
-    *(z++) = hexdigits[c&0xf];
-  }
-  *z = 0;
-  sqlite3_result_text(context, zHex, n*2, sqlite3_free);
+  z = zHex = contextMalloc(context, n*2 + 1);
+  if( zHex ){
+    for(i=0; i<n; i++, pBlob++){
+      unsigned char c = *pBlob;
+      *(z++) = hexdigits[(c>>4)&0xf];
+      *(z++) = hexdigits[c&0xf];
+    }
+    *z = 0;
+    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
+  }
 }
 
 /*
 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
@@ -48634,9 +51643,9 @@
   nRep = sqlite3_value_bytes(argv[2]);
   assert( zRep==sqlite3_value_text(argv[2]) );
   nOut = nStr + 1;
   assert( nOut<SQLITE_MAX_LENGTH );
-  zOut = sqlite3_malloc((int)nOut);
+  zOut = contextMalloc(context, (int)nOut);
   if( zOut==0 ){
     return;
   }
   loopLimit = nStr - nPattern;
@@ -48643,16 +51652,20 @@
   for(i=j=0; i<=loopLimit; i++){
     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
       zOut[j++] = zStr[i];
     }else{
+      u8 *zOld;
       nOut += nRep - nPattern;
       if( nOut>=SQLITE_MAX_LENGTH ){
         sqlite3_result_error_toobig(context);
         sqlite3_free(zOut);
         return;
       }
+      zOld = zOut;
       zOut = sqlite3_realloc(zOut, (int)nOut);
       if( zOut==0 ){
+        sqlite3_result_error_nomem(context);
+        sqlite3_free(zOld);
         return;
       }
       memcpy(&zOut[j], zRep, nRep);
       j += nRep;
@@ -48706,9 +51719,9 @@
     for(z=zCharSet, nChar=0; *z; nChar++){
       SQLITE_SKIP_UTF8(z);
     }
     if( nChar>0 ){
-      azChar = sqlite3_malloc( nChar*(sizeof(char*)+1) );
+      azChar = contextMalloc(context, nChar*(sizeof(char*)+1));
       if( azChar==0 ){
         return;
       }
       aLen = (unsigned char*)&azChar[nChar];
@@ -48836,22 +51849,20 @@
      "0123456789"
      ".-!,:*^+=_|?/<> ";
   int iMin, iMax, n, r, i;
   unsigned char zBuf[1000];
-  if( argc>=1 ){
-    iMin = sqlite3_value_int(argv[0]);
-    if( iMin<0 ) iMin = 0;
-    if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
-  }else{
-    iMin = 1;
-  }
-  if( argc>=2 ){
-    iMax = sqlite3_value_int(argv[1]);
-    if( iMax<iMin ) iMax = iMin;
-    if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
-  }else{
-    iMax = 50;
-  }
+
+  /* It used to be possible to call randstr() with any number of arguments,
+  ** but now it is registered with SQLite as requiring exactly 2.
+  */
+  assert(argc==2);
+
+  iMin = sqlite3_value_int(argv[0]);
+  if( iMin<0 ) iMin = 0;
+  if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
+  iMax = sqlite3_value_int(argv[1]);
+  if( iMax<iMin ) iMax = iMin;
+  if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
   n = iMin;
   if( iMax>iMin ){
     sqlite3Randomness(sizeof(r), &r);
     r &= 0x7fffffff;
@@ -48883,9 +51894,9 @@
 static void destructor(void *p){
   char *zVal = (char *)p;
   assert(zVal);
   zVal--;
-  sqliteFree(zVal);
+  sqlite3_free(zVal);
   test_destructor_count_var--;
 }
 static void test_destructor(
   sqlite3_context *pCtx,
@@ -48899,12 +51910,14 @@
   test_destructor_count_var++;
   assert( nArg==1 );
   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
   len = sqlite3ValueBytes(argv[0], ENC(db));
-  zVal = sqliteMalloc(len+3);
-  zVal[len] = 0;
-  zVal[len-1] = 0;
-  assert( zVal );
+  zVal = contextMalloc(pCtx, len+3);
+  if( !zVal ){
+    return;
+  }
+  zVal[len+1] = 0;
+  zVal[len+2] = 0;
   zVal++;
   memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
   if( ENC(db)==SQLITE_UTF8 ){
     sqlite3_result_text(pCtx, zVal, -1, destructor);
@@ -48936,17 +51949,18 @@
 ** call) then the result for that argument is 0.  If there is a prior
 ** registration, the result for that argument is 1.  The overall result
 ** is the individual argument results separated by spaces.
 */
-static void free_test_auxdata(void *p) {sqliteFree(p);}
+static void free_test_auxdata(void *p) {sqlite3_free(p);}
 static void test_auxdata(
   sqlite3_context *pCtx,
   int nArg,
   sqlite3_value **argv
 ){
   int i;
-  char *zRet = sqliteMalloc(nArg*2);
+  char *zRet = contextMalloc(pCtx, nArg*2);
   if( !zRet ) return;
+  memset(zRet, 0, nArg*2);
   for(i=0; i<nArg; i++){
     char const *z = (char*)sqlite3_value_text(argv[i]);
     if( z ){
       char *zAux = sqlite3_get_auxdata(pCtx, i);
@@ -48955,11 +51969,15 @@
         if( strcmp(zAux, z) ){
           sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
           return;
         }
-      }else{
+      }else {
         zRet[i*2] = '0';
-        zAux = sqliteStrDup(z);
+      }
+
+      zAux = contextMalloc(pCtx, strlen(z)+1);
+      if( zAux ){
+        strcpy(zAux, z);
         sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
       }
       zRet[i*2+1] = ' ';
     }
@@ -49241,13 +52259,13 @@
       }
     }
   }
   sqlite3RegisterDateTimeFunctions(db);
-  if( !sqlite3MallocFailed() ){
+  if( !db->mallocFailed ){
     int rc = sqlite3_overload_function(db, "MATCH", 2);
     assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
     if( rc==SQLITE_NOMEM ){
-      sqlite3FailedMalloc();
+      db->mallocFailed = 1;
     }
   }
 #ifdef SQLITE_SSE
   (void)sqlite3SseFunctions(db);
@@ -49339,9 +52357,9 @@
 *************************************************************************
 ** This file contains C code routines that are called by the parser
 ** to handle INSERT statements in SQLite.
 **
-** $Id: insert.c,v 1.188 2007/07/23 19:39:47 drh Exp $
+** $Id: insert.c,v 1.192 2007/09/03 17:30:07 danielk1977 Exp $
 */
 
 /*
 ** Set P3 of the most recently inserted opcode to a column affinity
@@ -49367,9 +52385,10 @@
     ** up.
     */
     int n;
     Table *pTab = pIdx->pTable;
-    pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
+    sqlite3 *db = sqlite3VdbeDb(v);
+    pIdx->zColAff = (char *)sqlite3DbMallocZero(db, pIdx->nColumn+1);
     if( !pIdx->zColAff ){
       return;
     }
     for(n=0; n<pIdx->nColumn; n++){
@@ -49405,10 +52424,11 @@
   */
   if( !pTab->zColAff ){
     char *zColAff;
     int i;
-
-    zColAff = (char *)sqliteMalloc(pTab->nCol+1);
+    sqlite3 *db = sqlite3VdbeDb(v);
+
+    zColAff = (char *)sqlite3DbMallocZero(db, pTab->nCol+1);
     if( !zColAff ){
       return;
     }
 
@@ -49682,12 +52702,12 @@
   int isView;                 /* True if attempting to insert into a view */
   int triggers_exist = 0;     /* True if there are FOR EACH ROW triggers */
 #endif
 
-  if( pParse->nErr || sqlite3MallocFailed() ){
+  db = pParse->db;
+  if( pParse->nErr || db->mallocFailed ){
     goto insert_cleanup;
   }
-  db = pParse->db;
 
   /* Locate the table into which we will be inserting new information.
   */
   assert( pTabList->nSrc==1 );
@@ -49788,9 +52808,9 @@
     iInsertBlock = sqlite3VdbeMakeLabel(v);
 
     /* Resolve the expressions in the SELECT statement and execute it. */
     rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
-    if( rc || pParse->nErr || sqlite3MallocFailed() ){
+    if( rc || pParse->nErr || db->mallocFailed ){
       goto insert_cleanup;
     }
 
     iCleanup = sqlite3VdbeMakeLabel(v);
@@ -50617,9 +53637,9 @@
 ** transfer optimization is used.  This is used for testing
 ** purposes only - to make sure the transfer optimization really
 ** is happening when it is suppose to.
 */
-int sqlite3_xferopt_count;
+SQLITE_API int sqlite3_xferopt_count;
 #endif /* SQLITE_TEST */
 
 
 #ifndef SQLITE_OMIT_XFER_OPT
@@ -50742,11 +53762,9 @@
   }
   if( onError!=OE_Abort && onError!=OE_Rollback ){
     return 0;   /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
   }
-  if( pSelect->pSrc==0 ){
-    return 0;   /* SELECT must have a FROM clause */
-  }
+  assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
   if( pSelect->pSrc->nSrc!=1 ){
     return 0;   /* FROM clause must have exactly one term */
   }
   if( pSelect->pSrc->a[0].pSelect ){
@@ -50849,8 +53867,9 @@
   sqlite3_xferopt_count++;
 #endif
   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
   v = sqlite3GetVdbe(pParse);
+  sqlite3CodeVerifySchema(pParse, iDbSrc);
   iSrc = pParse->nTab++;
   iDest = pParse->nTab++;
   counterMem = autoIncBegin(pParse, iDbDest, pDest);
   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
@@ -50947,9 +53966,9 @@
 ** implement the programmer interface to the library.  Routines in
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: legacy.c,v 1.18 2007/05/04 13:15:56 drh Exp $
+** $Id: legacy.c,v 1.22 2007/08/29 12:31:26 danielk1977 Exp $
 */
 
 
 /*
@@ -50977,8 +53996,10 @@
   int nRetry = 0;
   int nCallback;
 
   if( zSql==0 ) return SQLITE_OK;
+
+  sqlite3_mutex_enter(db->mutex);
   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
     int nCol;
     char **azVals = 0;
 
@@ -50996,9 +54017,9 @@
 
     nCallback = 0;
 
     nCol = sqlite3_column_count(pStmt);
-    azCols = sqliteMalloc(2*nCol*sizeof(const char *) + 1);
+    azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char *) + 1);
     if( azCols==0 ){
       goto exec_out;
     }
 
@@ -51038,17 +54059,17 @@
         break;
       }
     }
 
-    sqliteFree(azCols);
+    sqlite3_free(azCols);
     azCols = 0;
   }
 
 exec_out:
   if( pStmt ) sqlite3_finalize(pStmt);
-  if( azCols ) sqliteFree(azCols);
-
-  rc = sqlite3ApiExit(0, rc);
+  if( azCols ) sqlite3_free(azCols);
+
+  rc = sqlite3ApiExit(db, rc);
   if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
     int nErrMsg = 1 + strlen(sqlite3_errmsg(db));
     *pzErrMsg = sqlite3_malloc(nErrMsg);
     if( *pzErrMsg ){
@@ -51058,8 +54079,9 @@
     *pzErrMsg = 0;
   }
 
   assert( (rc&db->errMask)==rc );
+  sqlite3_mutex_leave(db->mutex);
   return rc;
 }
 
 /************** End of legacy.c **********************************************/
@@ -51099,9 +54121,9 @@
 ** an SQLite instance.  Shared libraries that intend to be loaded
 ** as extensions by SQLite should #include this file instead of
 ** sqlite3.h.
 **
-** @(#) $Id: sqlite3ext.h,v 1.12 2007/07/20 10:48:36 drh Exp $
+** @(#) $Id: sqlite3ext.h,v 1.17 2007/08/31 16:11:36 drh Exp $
 */
 #ifndef _SQLITE3EXT_H_
 #define _SQLITE3EXT_H_
 
@@ -51232,13 +54254,41 @@
   const void * (*value_text16be)(sqlite3_value*);
   const void * (*value_text16le)(sqlite3_value*);
   int  (*value_type)(sqlite3_value*);
   char *(*vmprintf)(const char*,va_list);
+  /* Added ??? */
   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
+  /* Added by 3.3.13 */
   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
   int (*clear_bindings)(sqlite3_stmt*);
+  /* Added by 3.4.1 */
   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *));
+  /* Added by 3.5.0 */
+  int (*bind_zeroblob)(sqlite3_stmt*,int,int);
+  int (*blob_bytes)(sqlite3_blob*);
+  int (*blob_close)(sqlite3_blob*);
+  int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**);
+  int (*blob_read)(sqlite3_blob*,void*,int,int);
+  int (*blob_write)(sqlite3_blob*,const void*,int,int);
+  int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*));
+  int (*file_control)(sqlite3*,const char*,int,void*);
+  sqlite3_int64 (*memory_highwater)(int);
+  sqlite3_int64 (*memory_used)(void);
+  sqlite3_mutex *(*mutex_alloc)(int);
+  void (*mutex_enter)(sqlite3_mutex*);
+  void (*mutex_free)(sqlite3_mutex*);
+  void (*mutex_leave)(sqlite3_mutex*);
+  int (*mutex_try)(sqlite3_mutex*);
+  int (*open_v2)(const char*,sqlite3**,int,const char*);
+  int (*release_memory)(int);
+  void (*result_error_nomem)(sqlite3_context*);
+  void (*result_error_toobig)(sqlite3_context*);
+  int (*sleep)(int);
+  void (*soft_heap_limit)(int);
+  sqlite3_vfs *(*vfs_find)(const char*);
+  int (*vfs_register)(sqlite3_vfs*,int);
+  int (*vfs_unregister)(sqlite3_vfs*);
 };
 
 /*
 ** The following macros redefine the API routines so that they are
@@ -51373,8 +54423,32 @@
 #define sqlite3_overload_function      sqlite3_api->overload_function
 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
 #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
+#define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
+#define sqlite3_blob_bytes             sqlite3_api->blob_bytes
+#define sqlite3_blob_close             sqlite3_api->blob_close
+#define sqlite3_blob_open              sqlite3_api->blob_open
+#define sqlite3_blob_read              sqlite3_api->blob_read
+#define sqlite3_blob_write             sqlite3_api->blob_write
+#define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
+#define sqlite3_file_control           sqlite3_api->file_control
+#define sqlite3_memory_highwater       sqlite3_api->memory_highwater
+#define sqlite3_memory_used            sqlite3_api->memory_used
+#define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
+#define sqlite3_mutex_enter            sqlite3_api->mutex_enter
+#define sqlite3_mutex_free             sqlite3_api->mutex_free
+#define sqlite3_mutex_leave            sqlite3_api->mutex_leave
+#define sqlite3_mutex_try              sqlite3_api->mutex_try
+#define sqlite3_open_v2                sqlite3_api->open_v2
+#define sqlite3_release_memory         sqlite3_api->release_memory
+#define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
+#define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
+#define sqlite3_sleep                  sqlite3_api->sleep
+#define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
+#define sqlite3_vfs_find               sqlite3_api->vfs_find
+#define sqlite3_vfs_register           sqlite3_api->vfs_register
+#define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
 #endif /* SQLITE_CORE */
 
 #define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api;
 #define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
@@ -51471,9 +54545,9 @@
 ** intend to use is supported by the library.  Extensions should
 ** also check to make sure that the pointer to the function is
 ** not NULL before calling it.
 */
-const sqlite3_api_routines sqlite3_apis = {
+SQLITE_API const sqlite3_api_routines sqlite3_apis = {
   sqlite3_aggregate_context,
   sqlite3_aggregate_count,
   sqlite3_bind_blob,
   sqlite3_bind_double,
@@ -51609,8 +54683,43 @@
   ** Added for 3.4.1
   */
   sqlite3_create_module_v2,
 
+  /*
+  ** Added for 3.5.0
+  */
+  sqlite3_bind_zeroblob,
+  sqlite3_blob_bytes,
+  sqlite3_blob_close,
+  sqlite3_blob_open,
+  sqlite3_blob_read,
+  sqlite3_blob_write,
+  sqlite3_create_collation_v2,
+  sqlite3_file_control,
+  sqlite3_memory_highwater,
+  sqlite3_memory_used,
+#ifdef SQLITE_MUTEX_NOOP
+  0,
+  0,
+  0,
+  0,
+  0,
+#else
+  sqlite3_mutex_alloc,
+  sqlite3_mutex_enter,
+  sqlite3_mutex_free,
+  sqlite3_mutex_leave,
+  sqlite3_mutex_try,
+#endif
+  sqlite3_open_v2,
+  sqlite3_release_memory,
+  sqlite3_result_error_nomem,
+  sqlite3_result_error_toobig,
+  sqlite3_sleep,
+  sqlite3_soft_heap_limit,
+  sqlite3_vfs_find,
+  sqlite3_vfs_register,
+  sqlite3_vfs_unregister,
 };
 
 /*
 ** Attempt to load an SQLite extension library contained in the file
@@ -51623,14 +54732,15 @@
 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
 ** error message text.  The calling function should free this memory
 ** by calling sqlite3_free().
 */
-int sqlite3_load_extension(
+static int sqlite3LoadExtension(
   sqlite3 *db,          /* Load the extension into this database connection */
   const char *zFile,    /* Name of the shared library containing extension */
   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
   char **pzErrMsg       /* Put error message here if not 0 */
 ){
+  sqlite3_vfs *pVfs = db->pVfs;
   void *handle;
   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   char *zErrmsg = 0;
   void **aHandle;
@@ -51651,47 +54761,68 @@
   if( zProc==0 ){
     zProc = "sqlite3_extension_init";
   }
 
-  handle = sqlite3OsDlopen(zFile);
+  handle = sqlite3OsDlOpen(pVfs, zFile);
   if( handle==0 ){
     if( pzErrMsg ){
-      *pzErrMsg = sqlite3_mprintf("unable to open shared library [%s]", zFile);
+      char zErr[256];
+      zErr[sizeof(zErr)-1] = '\0';
+      sqlite3_snprintf(sizeof(zErr)-1, zErr,
+          "unable to open shared library [%s]", zFile);
+      sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
+      *pzErrMsg = sqlite3DbStrDup(db, zErr);
     }
     return SQLITE_ERROR;
   }
   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
-                   sqlite3OsDlsym(handle, zProc);
+                   sqlite3OsDlSym(pVfs, handle, zProc);
   if( xInit==0 ){
     if( pzErrMsg ){
-       *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
-                                   zProc, zFile);
-    }
-    sqlite3OsDlclose(handle);
+      char zErr[256];
+      zErr[sizeof(zErr)-1] = '\0';
+      sqlite3_snprintf(sizeof(zErr)-1, zErr,
+          "no entry point [%s] in shared library [%s]", zProc,zFile);
+      sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
+      *pzErrMsg = sqlite3DbStrDup(db, zErr);
+      sqlite3OsDlClose(pVfs, handle);
+    }
     return SQLITE_ERROR;
   }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
     if( pzErrMsg ){
       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
     }
     sqlite3_free(zErrmsg);
-    sqlite3OsDlclose(handle);
+    sqlite3OsDlClose(pVfs, handle);
     return SQLITE_ERROR;
   }
 
   /* Append the new shared library handle to the db->aExtension array. */
   db->nExtension++;
-  aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
+  aHandle = sqlite3DbMallocZero(db, sizeof(handle)*db->nExtension);
   if( aHandle==0 ){
     return SQLITE_NOMEM;
   }
   if( db->nExtension>0 ){
     memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
   }
-  sqliteFree(db->aExtension);
+  sqlite3_free(db->aExtension);
   db->aExtension = aHandle;
 
   db->aExtension[db->nExtension-1] = handle;
   return SQLITE_OK;
+}
+SQLITE_API int sqlite3_load_extension(
+  sqlite3 *db,          /* Load the extension into this database connection */
+  const char *zFile,    /* Name of the shared library containing extension */
+  const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
+  char **pzErrMsg       /* Put error message here if not 0 */
+){
+  int rc;
+  sqlite3_mutex_enter(db->mutex);
+  rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 /*
 ** Call this routine when the database connection is closing in order
@@ -51698,73 +54829,82 @@
 ** to clean up loaded extensions
 */
 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
   int i;
+  assert( sqlite3_mutex_held(db->mutex) );
   for(i=0; i<db->nExtension; i++){
-    sqlite3OsDlclose(db->aExtension[i]);
-  }
-  sqliteFree(db->aExtension);
+    sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
+  }
+  sqlite3_free(db->aExtension);
 }
 
 /*
 ** Enable or disable extension loading.  Extension loading is disabled by
 ** default so as not to open security holes in older applications.
 */
-int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
+SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
+  sqlite3_mutex_enter(db->mutex);
   if( onoff ){
     db->flags |= SQLITE_LoadExtension;
   }else{
     db->flags &= ~SQLITE_LoadExtension;
   }
-  return SQLITE_OK;
-}
-
-/*
-** A list of automatically loaded extensions.
-**
-** This list is shared across threads, so be sure to hold the
-** mutex while accessing or changing it.
-*/
-static int nAutoExtension = 0;
-static void **aAutoExtension = 0;
+  sqlite3_mutex_leave(db->mutex);
+  return SQLITE_OK;
+}
+
+/*
+** The following object holds the list of automatically loaded
+** extensions.
+**
+** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
+** mutex must be held while accessing this list.
+*/
+static struct {
+  int nExt;        /* Number of entries in aExt[] */
+  void **aExt;     /* Pointers to the extension init functions */
+} autoext = { 0, 0 };
 
 
 /*
 ** Register a statically linked extension that is automatically
 ** loaded by every new database connection.
 */
-int sqlite3_auto_extension(void *xInit){
+SQLITE_API int sqlite3_auto_extension(void *xInit){
   int i;
   int rc = SQLITE_OK;
-  sqlite3OsEnterMutex();
-  for(i=0; i<nAutoExtension; i++){
-    if( aAutoExtension[i]==xInit ) break;
-  }
-  if( i==nAutoExtension ){
-    nAutoExtension++;
-    aAutoExtension = sqlite3Realloc( aAutoExtension,
-                                     nAutoExtension*sizeof(aAutoExtension[0]) );
-    if( aAutoExtension==0 ){
-      nAutoExtension = 0;
+  sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+  sqlite3_mutex_enter(mutex);
+  for(i=0; i<autoext.nExt; i++){
+    if( autoext.aExt[i]==xInit ) break;
+  }
+  if( i==autoext.nExt ){
+    int nByte = (autoext.nExt+1)*sizeof(autoext.aExt[0]);
+    void **aNew;
+    aNew = sqlite3_realloc(autoext.aExt, nByte);
+    if( aNew==0 ){
       rc = SQLITE_NOMEM;
     }else{
-      aAutoExtension[nAutoExtension-1] = xInit;
-    }
-  }
-  sqlite3OsLeaveMutex();
+      autoext.aExt = aNew;
+      autoext.aExt[autoext.nExt] = xInit;
+      autoext.nExt++;
+    }
+  }
+  sqlite3_mutex_leave(mutex);
   assert( (rc&0xff)==rc );
   return rc;
 }
 
 /*
 ** Reset the automatic extension loading mechanism.
 */
-void sqlite3_reset_auto_extension(void){
-  sqlite3OsEnterMutex();
-  sqliteFree(aAutoExtension);
-  aAutoExtension = 0;
-  nAutoExtension = 0;
-  sqlite3OsLeaveMutex();
+SQLITE_API void sqlite3_reset_auto_extension(void){
+  sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+  sqlite3_mutex_enter(mutex);
+  sqlite3_free(autoext.aExt);
+  autoext.aExt = 0;
+  autoext.nExt = 0;
+  sqlite3_mutex_leave(mutex);
 }
 
 /*
 ** Load all automatic extensions.
@@ -51774,28 +54914,30 @@
   int go = 1;
   int rc = SQLITE_OK;
   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
 
-  if( nAutoExtension==0 ){
+  if( autoext.nExt==0 ){
     /* Common case: early out without every having to acquire a mutex */
     return SQLITE_OK;
   }
   for(i=0; go; i++){
     char *zErrmsg = 0;
-    sqlite3OsEnterMutex();
-    if( i>=nAutoExtension ){
+    sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
+    sqlite3_mutex_enter(mutex);
+    if( i>=autoext.nExt ){
       xInit = 0;
       go = 0;
     }else{
       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
-              aAutoExtension[i];
-    }
-    sqlite3OsLeaveMutex();
+              autoext.aExt[i];
+    }
+    sqlite3_mutex_leave(mutex);
     if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
       sqlite3Error(db, SQLITE_ERROR,
             "automatic extension loading failed: %s", zErrmsg);
       go = 0;
       rc = SQLITE_ERROR;
+      sqlite3_free(zErrmsg);
     }
   }
   return rc;
 }
@@ -51816,17 +54958,14 @@
 **
 *************************************************************************
 ** This file contains code used to implement the PRAGMA command.
 **
-** $Id: pragma.c,v 1.142 2007/06/26 10:38:55 danielk1977 Exp $
+** $Id: pragma.c,v 1.149 2007/08/31 18:34:59 drh Exp $
 */
 
 /* Ignore this whole file if pragmas are disabled
 */
 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
-
-#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
-#endif
 
 /*
 ** Interpret the given string as a safety level.  Return 0 for OFF,
 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
@@ -52064,14 +55203,14 @@
   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
     return;
   }
 
-  zLeft = sqlite3NameFromToken(pId);
+  zLeft = sqlite3NameFromToken(db, pId);
   if( !zLeft ) return;
   if( minusFlag ){
-    zRight = sqlite3MPrintf("-%T", pValue);
-  }else{
-    zRight = sqlite3NameFromToken(pValue);
+    zRight = sqlite3MPrintf(db, "-%T", pValue);
+  }else{
+    zRight = sqlite3NameFromToken(db, pValue);
   }
 
   zDb = ((iDb>0)?pDb->zName:0);
   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
@@ -52106,8 +55245,9 @@
       { OP_Callback,    1, 0,        0},
     };
     int addr;
     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
+    sqlite3VdbeUsesBtree(v, iDb);
     if( !zRight ){
       sqlite3VdbeSetNumCols(v, 1);
       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC);
       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
@@ -52142,9 +55282,14 @@
     if( !zRight ){
       int size = pBt ? sqlite3BtreeGetPageSize(pBt) : 0;
       returnSingleInt(pParse, "page_size", size);
     }else{
-      sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1);
+      /* Malloc may fail when setting the page-size, as there is an internal
+      ** buffer that the pager module resizes using sqlite3_realloc().
+      */
+      if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, atoi(zRight), -1) ){
+        db->mallocFailed = 1;
+      }
     }
   }else
 
   /*
@@ -52261,8 +55406,9 @@
           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
+          sqlite3VdbeUsesBtree(v, iDb);
         }
       }
     }
   }else
@@ -52357,9 +55503,11 @@
         sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0);
         sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
       }
     }else{
-      if( zRight[0] && !sqlite3OsIsDirWritable(zRight) ){
+      if( zRight[0]
+       && !sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE)
+      ){
         sqlite3ErrorMsg(pParse, "not a writable directory");
         goto pragma_out;
       }
       if( TEMP_STORE==0
@@ -52367,9 +55515,9 @@
        || (TEMP_STORE==2 && db->temp_store==1)
       ){
         invalidateTempStorage(pParse);
       }
-      sqliteFree(sqlite3_temp_directory);
+      sqlite3_free(sqlite3_temp_directory);
       if( zRight[0] ){
         sqlite3_temp_directory = zRight;
         zRight = 0;
       }else{
@@ -52664,9 +55812,9 @@
       if( cnt==0 ) continue;
       sqlite3VdbeAddOp(v, OP_IntegrityCk, 0, i);
       addr = sqlite3VdbeAddOp(v, OP_IsNull, -1, 0);
       sqlite3VdbeOp3(v, OP_String8, 0, 0,
-         sqlite3MPrintf("*** in database %s ***\n", db->aDb[i].zName),
+         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
          P3_DYNAMIC);
       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
       sqlite3VdbeAddOp(v, OP_Concat, 0, 0);
       sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
@@ -52847,8 +55995,9 @@
    || sqlite3StrICmp(zLeft, "freelist_count")==0
   ){
 
     int iCookie;   /* Cookie index. 0 for schema-cookie, 6 for user-cookie. */
+    sqlite3VdbeUsesBtree(v, iDb);
     switch( zLeft[0] ){
       case 's': case 'S':
         iCookie = 0;
         break;
@@ -52904,18 +56053,20 @@
     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P3_STATIC);
     for(i=0; i<db->nDb; i++){
       Btree *pBt;
       Pager *pPager;
+      const char *zState = "unknown";
+      int j;
       if( db->aDb[i].zName==0 ) continue;
       sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC);
       pBt = db->aDb[i].pBt;
       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
-        sqlite3VdbeOp3(v, OP_String8, 0, 0, "closed", P3_STATIC);
-      }else{
-        int j = sqlite3PagerLockstate(pPager);
-        sqlite3VdbeOp3(v, OP_String8, 0, 0,
-            (j>=0 && j<=4) ? azLockName[j] : "unknown", P3_STATIC);
-      }
+        zState = "closed";
+      }else if( sqlite3_file_control(db, db->aDb[i].zName,
+                                     SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
+         zState = azLockName[j];
+      }
+      sqlite3VdbeOp3(v, OP_String8, 0, 0, zState, P3_STATIC);
       sqlite3VdbeAddOp(v, OP_Callback, 2, 0);
     }
   }else
 #endif
@@ -52973,10 +56124,10 @@
     }
 #endif
   }
 pragma_out:
-  sqliteFree(zLeft);
-  sqliteFree(zRight);
+  sqlite3_free(zLeft);
+  sqlite3_free(zRight);
 }
 
 #endif /* SQLITE_OMIT_PRAGMA || SQLITE_OMIT_PARSER */
 
@@ -52996,17 +56147,17 @@
 ** This file contains the implementation of the sqlite3_prepare()
 ** interface, and routines that contribute to loading the database schema
 ** from disk.
 **
-** $Id: prepare.c,v 1.51 2007/06/24 10:14:00 danielk1977 Exp $
+** $Id: prepare.c,v 1.60 2007/08/29 12:31:27 danielk1977 Exp $
 */
 
 /*
 ** Fill the InitData structure with an error message that indicates
 ** that the database is corrupt.
 */
 static void corruptSchema(InitData *pData, const char *zExtra){
-  if( !sqlite3MallocFailed() ){
+  if( !pData->db->mallocFailed ){
     sqlite3SetString(pData->pzErrMsg, "malformed database schema",
        zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
   }
   pData->rc = SQLITE_CORRUPT;
@@ -53028,11 +56179,12 @@
   InitData *pData = (InitData*)pInit;
   sqlite3 *db = pData->db;
   int iDb = pData->iDb;
 
+  assert( sqlite3_mutex_held(db->mutex) );
   pData->rc = SQLITE_OK;
   DbClearProperty(db, iDb, DB_Empty);
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     corruptSchema(pData, 0);
     return SQLITE_NOMEM;
   }
 
@@ -53059,9 +56211,9 @@
     assert( rc!=SQLITE_OK || zErr==0 );
     if( SQLITE_OK!=rc ){
       pData->rc = rc;
       if( rc==SQLITE_NOMEM ){
-        sqlite3FailedMalloc();
+        db->mallocFailed = 1;
       }else if( rc!=SQLITE_INTERRUPT ){
         corruptSchema(pData, zErr);
       }
       sqlite3_free(zErr);
@@ -53137,8 +56289,9 @@
 #endif
 
   assert( iDb>=0 && iDb<db->nDb );
   assert( db->aDb[iDb].pSchema );
+  assert( sqlite3_mutex_held(db->mutex) );
 
   /* zMasterSchema and zInitScript are set to point at the master schema
   ** and initialisation script appropriate for the database being
   ** initialised. zMasterName is the name of the master table.
@@ -53161,9 +56314,10 @@
   initData.pzErrMsg = pzErrMsg;
   rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
   if( rc ){
     sqlite3SafetyOn(db);
-    return initData.rc;
+    rc = initData.rc;
+    goto error_out;
   }
   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
   if( pTab ){
     pTab->readOnly = 1;
@@ -53178,12 +56332,14 @@
       DbSetProperty(db, 1, DB_SchemaLoaded);
     }
     return SQLITE_OK;
   }
+  sqlite3BtreeEnter(pDb->pBt);
   rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
   if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
     sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
-    return rc;
+    sqlite3BtreeLeave(pDb->pBt);
+    goto error_out;
   }
 
   /* Get the database meta information.
   **
@@ -53209,9 +56365,10 @@
     }
     if( rc ){
       sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
       sqlite3BtreeCloseCursor(curMain);
-      return rc;
+      sqlite3BtreeLeave(pDb->pBt);
+      goto error_out;
     }
   }else{
     memset(meta, 0, sizeof(meta));
   }
@@ -53232,8 +56389,9 @@
       if( meta[4]!=ENC(db) ){
         sqlite3BtreeCloseCursor(curMain);
         sqlite3SetString(pzErrMsg, "attached databases must use the same"
             " text encoding as main database", (char*)0);
+        sqlite3BtreeLeave(pDb->pBt);
         return SQLITE_ERROR;
       }
     }
   }else{
@@ -53258,8 +56416,9 @@
   }
   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
     sqlite3BtreeCloseCursor(curMain);
     sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
+    sqlite3BtreeLeave(pDb->pBt);
     return SQLITE_ERROR;
   }
 
 
@@ -53270,24 +56429,24 @@
     /* For an empty database, there is nothing to read */
     rc = SQLITE_OK;
   }else{
     char *zSql;
-    zSql = sqlite3MPrintf(
+    zSql = sqlite3MPrintf(db,
         "SELECT name, rootpage, sql FROM '%q'.%s",
         db->aDb[iDb].zName, zMasterName);
     sqlite3SafetyOff(db);
     rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
     if( rc==SQLITE_ABORT ) rc = initData.rc;
     sqlite3SafetyOn(db);
-    sqliteFree(zSql);
+    sqlite3_free(zSql);
 #ifndef SQLITE_OMIT_ANALYZE
     if( rc==SQLITE_OK ){
       sqlite3AnalysisLoad(db, iDb);
     }
 #endif
     sqlite3BtreeCloseCursor(curMain);
   }
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
     rc = SQLITE_NOMEM;
     sqlite3ResetInternalSchema(db, 0);
   }
@@ -53301,8 +56460,14 @@
     ** even when it's contents have been corrupted.
     */
     DbSetProperty(db, iDb, DB_SchemaLoaded);
     rc = SQLITE_OK;
+  }
+  sqlite3BtreeLeave(pDb->pBt);
+
+error_out:
+  if( rc==SQLITE_NOMEM ){
+    db->mallocFailed = 1;
   }
   return rc;
 }
 
@@ -53317,10 +56482,11 @@
 ** file was of zero-length, then the DB_Empty flag is also set.
 */
 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
   int i, rc;
-  int called_initone = 0;
-
+  int commit_internal = !(db->flags&SQLITE_InternChanges);
+
+  assert( sqlite3_mutex_held(db->mutex) );
   if( db->init.busy ) return SQLITE_OK;
   rc = SQLITE_OK;
   db->init.busy = 1;
   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
@@ -53328,9 +56494,8 @@
     rc = sqlite3InitOne(db, i, pzErrMsg);
     if( rc ){
       sqlite3ResetInternalSchema(db, i);
     }
-    called_initone = 1;
   }
 
   /* Once all the other databases have been initialised, load the schema
   ** for the TEMP database. This is loaded last, as the TEMP database
@@ -53341,14 +56506,13 @@
     rc = sqlite3InitOne(db, 1, pzErrMsg);
     if( rc ){
       sqlite3ResetInternalSchema(db, 1);
     }
-    called_initone = 1;
   }
 #endif
 
   db->init.busy = 0;
-  if( rc==SQLITE_OK && called_initone ){
+  if( rc==SQLITE_OK && commit_internal ){
     sqlite3CommitInternalChanges(db);
   }
 
   return rc;
@@ -53360,8 +56524,9 @@
 */
 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
   int rc = SQLITE_OK;
   sqlite3 *db = pParse->db;
+  assert( sqlite3_mutex_held(db->mutex) );
   if( !db->init.busy ){
     rc = sqlite3Init(db, &pParse->zErrMsg);
   }
   if( rc!=SQLITE_OK ){
@@ -53382,8 +56547,9 @@
   BtCursor *curTemp;
   int cookie;
   int allOk = 1;
 
+  assert( sqlite3_mutex_held(db->mutex) );
   for(iDb=0; allOk && iDb<db->nDb; iDb++){
     Btree *pBt;
     pBt = db->aDb[iDb].pBt;
     if( pBt==0 ) continue;
@@ -53393,8 +56559,11 @@
       if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
         allOk = 0;
       }
       sqlite3BtreeCloseCursor(curTemp);
+    }
+    if( rc==SQLITE_NOMEM ){
+      db->mallocFailed = 1;
     }
   }
   return allOk;
 }
@@ -53418,8 +56587,9 @@
   ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
   ** more likely to cause a segfault than -1 (of course there are assert()
   ** statements too, but it never hurts to play the odds).
   */
+  assert( sqlite3_mutex_held(db->mutex) );
   if( pSchema ){
     for(i=0; i<db->nDb; i++){
       if( db->aDb[i].pSchema==pSchema ){
         break;
@@ -53445,27 +56615,30 @@
   char *zErrMsg = 0;
   int rc = SQLITE_OK;
   int i;
 
-  /* Assert that malloc() has not failed */
-  assert( !sqlite3MallocFailed() );
-
   assert( ppStmt );
   *ppStmt = 0;
   if( sqlite3SafetyOn(db) ){
     return SQLITE_MISUSE;
   }
+  assert( !db->mallocFailed );
+  assert( sqlite3_mutex_held(db->mutex) );
 
   /* If any attached database schemas are locked, do not proceed with
   ** compilation. Instead return SQLITE_LOCKED immediately.
   */
   for(i=0; i<db->nDb; i++) {
     Btree *pBt = db->aDb[i].pBt;
-    if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
-      const char *zDb = db->aDb[i].zName;
-      sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
-      sqlite3SafetyOff(db);
-      return SQLITE_LOCKED;
+    if( pBt ){
+      int rc;
+      rc = sqlite3BtreeSchemaLocked(pBt);
+      if( rc ){
+        const char *zDb = db->aDb[i].zName;
+        sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
+        sqlite3SafetyOff(db);
+        return SQLITE_LOCKED;
+      }
     }
   }
 
   memset(&sParse, 0, sizeof(sParse));
@@ -53474,19 +56647,19 @@
     char *zSqlCopy;
     if( nBytes>SQLITE_MAX_SQL_LENGTH ){
       return SQLITE_TOOBIG;
     }
-    zSqlCopy = sqlite3StrNDup(zSql, nBytes);
+    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
     if( zSqlCopy ){
       sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
-      sqliteFree(zSqlCopy);
+      sqlite3_free(zSqlCopy);
     }
     sParse.zTail = &zSql[nBytes];
   }else{
     sqlite3RunParser(&sParse, zSql, &zErrMsg);
   }
 
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     sParse.rc = SQLITE_NOMEM;
   }
   if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
   if( sParse.checkSchema && !schemaIsValid(db) ){
@@ -53494,9 +56667,9 @@
   }
   if( sParse.rc==SQLITE_SCHEMA ){
     sqlite3ResetInternalSchema(db, 0);
   }
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     sParse.rc = SQLITE_NOMEM;
   }
   if( pzTail ){
     *pzTail = sParse.zTail;
@@ -53527,9 +56700,9 @@
 
   if( saveSqlFlag ){
     sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
   }
-  if( rc!=SQLITE_OK || sqlite3MallocFailed() ){
+  if( rc!=SQLITE_OK || db->mallocFailed ){
     sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
     assert(!(*ppStmt));
   }else{
     *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
@@ -53536,16 +56709,35 @@
   }
 
   if( zErrMsg ){
     sqlite3Error(db, rc, "%s", zErrMsg);
-    sqliteFree(zErrMsg);
+    sqlite3_free(zErrMsg);
   }else{
     sqlite3Error(db, rc, 0);
   }
 
   rc = sqlite3ApiExit(db, rc);
-  sqlite3ReleaseThreadData();
+  /* sqlite3ReleaseThreadData(); */
   assert( (rc&db->errMask)==rc );
+  return rc;
+}
+static int sqlite3LockAndPrepare(
+  sqlite3 *db,              /* Database handle. */
+  const char *zSql,         /* UTF-8 encoded SQL statement. */
+  int nBytes,               /* Length of zSql in bytes. */
+  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
+  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
+  const char **pzTail       /* OUT: End of parsed string */
+){
+  int rc;
+  if( sqlite3SafetyCheck(db) ){
+    return SQLITE_MISUSE;
+  }
+  sqlite3_mutex_enter(db->mutex);
+  sqlite3BtreeEnterAll(db);
+  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
+  sqlite3BtreeLeaveAll(db);
+  sqlite3_mutex_leave(db->mutex);
   return rc;
 }
 
 /*
@@ -53558,14 +56750,16 @@
   sqlite3_stmt *pNew;
   const char *zSql;
   sqlite3 *db;
 
+  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
   zSql = sqlite3VdbeGetSql(p);
   if( zSql==0 ){
     return 0;
   }
   db = sqlite3VdbeDb(p);
-  rc = sqlite3Prepare(db, zSql, -1, 0, &pNew, 0);
+  assert( sqlite3_mutex_held(db->mutex) );
+  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0);
   if( rc ){
     assert( pNew==0 );
     return 0;
   }else{
@@ -53593,18 +56787,18 @@
   int nBytes,               /* Length of zSql in bytes. */
   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   const char **pzTail       /* OUT: End of parsed string */
 ){
-  return sqlite3Prepare(db,zSql,nBytes,0,ppStmt,pzTail);
-}
-int sqlite3_prepare_v2(
+  return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail);
+}
+SQLITE_API int sqlite3_prepare_v2(
   sqlite3 *db,              /* Database handle. */
   const char *zSql,         /* UTF-8 encoded SQL statement. */
   int nBytes,               /* Length of zSql in bytes. */
   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   const char **pzTail       /* OUT: End of parsed string */
 ){
-  return sqlite3Prepare(db,zSql,nBytes,1,ppStmt,pzTail);
+  return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail);
 }
 
 
 #ifndef SQLITE_OMIT_UTF16
@@ -53629,11 +56823,12 @@
 
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
-  zSql8 = sqlite3Utf16to8(zSql, nBytes);
+  sqlite3_mutex_enter(db->mutex);
+  zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
   if( zSql8 ){
-    rc = sqlite3Prepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
+    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
   }
 
   if( zTail8 && pzTail ){
     /* If sqlite3_prepare returns a tail pointer, we calculate the
@@ -53643,10 +56838,12 @@
     */
     int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8);
     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
   }
-  sqliteFree(zSql8);
-  return sqlite3ApiExit(db, rc);
+  sqlite3_free(zSql8);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 /*
 ** Two versions of the official API.  Legacy and new use.  In the legacy
@@ -53664,9 +56861,9 @@
   const void **pzTail       /* OUT: End of parsed string */
 ){
   return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
 }
-int sqlite3_prepare16_v2(
+SQLITE_API int sqlite3_prepare16_v2(
   sqlite3 *db,              /* Database handle. */
   const void *zSql,         /* UTF-8 encoded SQL statement. */
   int nBytes,               /* Length of zSql in bytes. */
   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
@@ -53692,9 +56889,9 @@
 *************************************************************************
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.354 2007/07/18 18:17:12 drh Exp $
+** $Id: select.c,v 1.359 2007/08/31 17:42:48 danielk1977 Exp $
 */
 
 
 /*
@@ -53718,8 +56915,9 @@
 ** Allocate a new Select structure and return a pointer to that
 ** structure.
 */
 SQLITE_PRIVATE Select *sqlite3SelectNew(
+  Parse *pParse,        /* Parsing context */
   ExprList *pEList,     /* which columns to include in the result */
   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
   Expr *pWhere,         /* the WHERE clause */
   ExprList *pGroupBy,   /* the GROUP BY clause */
@@ -53730,16 +56928,17 @@
   Expr *pOffset         /* OFFSET value.  NULL means no offset */
 ){
   Select *pNew;
   Select standin;
-  pNew = sqliteMalloc( sizeof(*pNew) );
+  sqlite3 *db = pParse->db;
+  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
   assert( !pOffset || pLimit );   /* Can't have OFFSET without LIMIT. */
   if( pNew==0 ){
     pNew = &standin;
     memset(pNew, 0, sizeof(*pNew));
   }
   if( pEList==0 ){
-    pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0);
+    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0,0,0), 0);
   }
   pNew->pEList = pEList;
   pNew->pSrc = pSrc;
   pNew->pWhere = pWhere;
@@ -53768,9 +56967,9 @@
 */
 SQLITE_PRIVATE void sqlite3SelectDelete(Select *p){
   if( p ){
     clearSelect(p);
-    sqliteFree(p);
+    sqlite3_free(p);
   }
 }
 
 /*
@@ -53870,23 +57069,25 @@
 ** to by z. For example;
 **
 **    {a"bc}  ->  {"a""bc"}
 */
-static void setQuotedToken(Token *p, const char *z){
-  p->z = (u8 *)sqlite3MPrintf("\"%w\"", z);
+static void setQuotedToken(Parse *pParse, Token *p, const char *z){
+  p->z = (u8 *)sqlite3MPrintf(0, "\"%w\"", z);
   p->dyn = 1;
   if( p->z ){
     p->n = strlen((char *)p->z);
+  }else{
+    pParse->db->mallocFailed = 1;
   }
 }
 
 /*
 ** Create an expression node for an identifier with the name of zName
 */
-SQLITE_PRIVATE Expr *sqlite3CreateIdExpr(const char *zName){
+SQLITE_PRIVATE Expr *sqlite3CreateIdExpr(Parse *pParse, const char *zName){
   Token dummy;
   setToken(&dummy, zName);
-  return sqlite3Expr(TK_ID, 0, 0, &dummy);
+  return sqlite3PExpr(pParse, TK_ID, 0, 0, &dummy);
 }
 
 
 /*
@@ -53893,8 +57094,9 @@
 ** Add a term to the WHERE expression in *ppExpr that requires the
 ** zCol column to be equal in the two tables pTab1 and pTab2.
 */
 static void addWhereTerm(
+  Parse *pParse,           /* Parsing context */
   const char *zCol,        /* Name of the column */
   const Table *pTab1,      /* First table */
   const char *zAlias1,     /* Alias for first table.  May be NULL */
   const Table *pTab2,      /* Second table */
@@ -53905,26 +57107,26 @@
   Expr *pE1a, *pE1b, *pE1c;
   Expr *pE2a, *pE2b, *pE2c;
   Expr *pE;
 
-  pE1a = sqlite3CreateIdExpr(zCol);
-  pE2a = sqlite3CreateIdExpr(zCol);
+  pE1a = sqlite3CreateIdExpr(pParse, zCol);
+  pE2a = sqlite3CreateIdExpr(pParse, zCol);
   if( zAlias1==0 ){
     zAlias1 = pTab1->zName;
   }
-  pE1b = sqlite3CreateIdExpr(zAlias1);
+  pE1b = sqlite3CreateIdExpr(pParse, zAlias1);
   if( zAlias2==0 ){
     zAlias2 = pTab2->zName;
   }
-  pE2b = sqlite3CreateIdExpr(zAlias2);
-  pE1c = sqlite3ExprOrFree(TK_DOT, pE1b, pE1a, 0);
-  pE2c = sqlite3ExprOrFree(TK_DOT, pE2b, pE2a, 0);
-  pE = sqlite3ExprOrFree(TK_EQ, pE1c, pE2c, 0);
+  pE2b = sqlite3CreateIdExpr(pParse, zAlias2);
+  pE1c = sqlite3PExpr(pParse, TK_DOT, pE1b, pE1a, 0);
+  pE2c = sqlite3PExpr(pParse, TK_DOT, pE2b, pE2a, 0);
+  pE = sqlite3PExpr(pParse, TK_EQ, pE1c, pE2c, 0);
   if( pE ){
     ExprSetProperty(pE, EP_FromJoin);
     pE->iRightJoinTable = iRightJoinTable;
   }
-  pE = sqlite3ExprAnd(*ppExpr, pE);
+  pE = sqlite3ExprAnd(pParse->db,*ppExpr, pE);
   if( pE ){
     *ppExpr = pE;
   }
 }
@@ -54004,9 +57206,9 @@
       }
       for(j=0; j<pLeftTab->nCol; j++){
         char *zName = pLeftTab->aCol[j].zName;
         if( columnIndex(pRightTab, zName)>=0 ){
-          addWhereTerm(zName, pLeftTab, pLeft->zAlias,
+          addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias,
                               pRightTab, pRight->zAlias,
                               pRight->iCursor, &p->pWhere);
 
         }
@@ -54025,9 +57227,9 @@
     ** an AND operator.
     */
     if( pRight->pOn ){
       setJoinExpr(pRight->pOn, pRight->iCursor);
-      p->pWhere = sqlite3ExprAnd(p->pWhere, pRight->pOn);
+      p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
       pRight->pOn = 0;
     }
 
     /* Create extra terms on the WHERE clause for each column named
@@ -54045,9 +57247,9 @@
           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
             "not present in both tables", zName);
           return 1;
         }
-        addWhereTerm(zName, pLeftTab, pLeft->zAlias,
+        addWhereTerm(pParse, zName, pLeftTab, pLeft->zAlias,
                             pRightTab, pRight->zAlias,
                             pRight->iCursor, &p->pWhere);
       }
     }
@@ -54368,9 +57570,9 @@
   struct ExprList_item *pItem;
   int i;
 
   nExpr = pList->nExpr;
-  pInfo = sqliteMalloc( sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
+  pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
   if( pInfo ){
     pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
     pInfo->nField = nExpr;
     pInfo->enc = ENC(db);
@@ -54671,9 +57873,9 @@
   }
 #endif
 
   assert( v!=0 );
-  if( pParse->colNamesSet || v==0 || sqlite3MallocFailed() ) return;
+  if( pParse->colNamesSet || v==0 || db->mallocFailed ) return;
   pParse->colNamesSet = 1;
   fullNames = (db->flags & SQLITE_FullColNames)!=0;
   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
   sqlite3VdbeSetNumCols(v, pEList->nExpr);
@@ -54755,8 +57957,9 @@
   Table *pTab;
   int i, j;
   ExprList *pEList;
   Column *aCol, *pCol;
+  sqlite3 *db = pParse->db;
 
   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
   if( prepSelectStmt(pParse, pSelect) ){
     return 0;
@@ -54763,18 +57966,18 @@
   }
   if( sqlite3SelectResolve(pParse, pSelect, 0) ){
     return 0;
   }
-  pTab = sqliteMalloc( sizeof(Table) );
+  pTab = sqlite3DbMallocZero(db, sizeof(Table) );
   if( pTab==0 ){
     return 0;
   }
   pTab->nRef = 1;
-  pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
+  pTab->zName = zTabName ? sqlite3DbStrDup(db, zTabName) : 0;
   pEList = pSelect->pEList;
   pTab->nCol = pEList->nExpr;
   assert( pTab->nCol>0 );
-  pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
+  pTab->aCol = aCol = sqlite3DbMallocZero(db, sizeof(pTab->aCol[0])*pTab->nCol);
   for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){
     Expr *p, *pR;
     char *zType;
     char *zName;
@@ -54788,26 +57991,27 @@
     p = pEList->a[i].pExpr;
     assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 );
     if( (zName = pEList->a[i].zName)!=0 ){
       /* If the column contains an "AS <name>" phrase, use <name> as the name */
-      zName = sqliteStrDup(zName);
+      zName = sqlite3DbStrDup(db, zName);
     }else if( p->op==TK_DOT
               && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
       /* For columns of the from A.B use B as the name */
-      zName = sqlite3MPrintf("%T", &pR->token);
+      zName = sqlite3MPrintf(db, "%T", &pR->token);
     }else if( p->span.z && p->span.z[0] ){
       /* Use the original text of the column expression as its name */
-      zName = sqlite3MPrintf("%T", &p->span);
+      zName = sqlite3MPrintf(db, "%T", &p->span);
     }else{
       /* If all else fails, make up a name */
-      zName = sqlite3MPrintf("column%d", i+1);
-    }
-    sqlite3Dequote(zName);
-    if( sqlite3MallocFailed() ){
-      sqliteFree(zName);
+      zName = sqlite3MPrintf(db, "column%d", i+1);
+    }
+    if( !zName || db->mallocFailed ){
+      db->mallocFailed = 1;
+      sqlite3_free(zName);
       sqlite3DeleteTable(pTab);
       return 0;
     }
+    sqlite3Dequote(zName);
 
     /* Make sure the column name is unique.  If the name is not unique,
     ** append a integer to the name so that it becomes unique.
     */
@@ -54814,9 +58018,9 @@
     nName = strlen(zName);
     for(j=cnt=0; j<i; j++){
       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
         zName[nName] = 0;
-        zName = sqlite3MPrintf("%z:%d", zName, ++cnt);
+        zName = sqlite3MPrintf(db, "%z:%d", zName, ++cnt);
         j = -1;
         if( zName==0 ) break;
       }
     }
@@ -54826,14 +58030,14 @@
     ** column.
     */
     memset(&sNC, 0, sizeof(sNC));
     sNC.pSrcList = pSelect->pSrc;
-    zType = sqliteStrDup(columnType(&sNC, p, 0, 0, 0));
+    zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
     pCol->zType = zType;
     pCol->affinity = sqlite3ExprAffinity(p);
     pColl = sqlite3ExprCollSeq(pParse, p);
     if( pColl ){
-      pCol->zColl = sqliteStrDup(pColl->zName);
+      pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
     }
   }
   pTab->iPKey = -1;
   return pTab;
@@ -54869,10 +58073,11 @@
   int i, j, k, rc;
   SrcList *pTabList;
   ExprList *pEList;
   struct SrcList_item *pFrom;
-
-  if( p==0 || p->pSrc==0 || sqlite3MallocFailed() ){
+  sqlite3 *db = pParse->db;
+
+  if( p==0 || p->pSrc==0 || db->mallocFailed ){
     return 1;
   }
   pTabList = p->pSrc;
   pEList = p->pEList;
@@ -54899,9 +58104,9 @@
       /* A sub-query in the FROM clause of a SELECT */
       assert( pFrom->pSelect!=0 );
       if( pFrom->zAlias==0 ){
         pFrom->zAlias =
-          sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom->pSelect);
+          sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pFrom->pSelect);
       }
       assert( pFrom->pTab==0 );
       pFrom->pTab = pTab =
         sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect);
@@ -54934,9 +58139,9 @@
         ** copied by the outer view so we can skip the copy step here
         ** in the inner view.
         */
         if( pFrom->pSelect==0 ){
-          pFrom->pSelect = sqlite3SelectDup(pTab->pSelect);
+          pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect);
         }
       }
 #endif
     }
@@ -54980,9 +58185,9 @@
       if( pE->op!=TK_ALL &&
            (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
         /* This particular expression does not need to be expanded.
         */
-        pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0);
+        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0);
         if( pNew ){
           pNew->a[pNew->nExpr-1].zName = a[k].zName;
         }else{
           rc = 1;
@@ -54994,9 +58199,9 @@
         ** expanded. */
         int tableSeen = 0;      /* Set to 1 when TABLE matches */
         char *zTName;            /* text of name of TABLE */
         if( pE->op==TK_DOT && pE->pLeft ){
-          zTName = sqlite3NameFromToken(&pE->pLeft->token);
+          zTName = sqlite3NameFromToken(db, &pE->pLeft->token);
         }else{
           zTName = 0;
         }
         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
@@ -55036,17 +58241,18 @@
                 ** using clause from the table on the right. */
                 continue;
               }
             }
-            pRight = sqlite3Expr(TK_ID, 0, 0, 0);
+            pRight = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
             if( pRight==0 ) break;
-            setQuotedToken(&pRight->token, zName);
+            setQuotedToken(pParse, &pRight->token, zName);
             if( zTabName && (longNames || pTabList->nSrc>1) ){
-              Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, 0);
-              pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0);
+              Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
+              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
               if( pExpr==0 ) break;
-              setQuotedToken(&pLeft->token, zTabName);
-              setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName));
+              setQuotedToken(pParse, &pLeft->token, zTabName);
+              setToken(&pExpr->span,
+                  sqlite3MPrintf(db, "%s.%s", zTabName, zName));
               pExpr->span.dyn = 1;
               pExpr->token.z = 0;
               pExpr->token.n = 0;
               pExpr->token.dyn = 0;
@@ -55055,11 +58261,11 @@
               pExpr->span = pExpr->token;
               pExpr->span.dyn = 0;
             }
             if( longNames ){
-              pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span);
+              pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pExpr->span);
             }else{
-              pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token);
+              pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pRight->token);
             }
           }
         }
         if( !tableSeen ){
@@ -55069,9 +58275,9 @@
             sqlite3ErrorMsg(pParse, "no tables specified");
           }
           rc = 1;
         }
-        sqliteFree(zTName);
+        sqlite3_free(zTName);
       }
     }
     sqlite3ExprListDelete(pEList);
     p->pEList = pNew;
@@ -55079,9 +58285,9 @@
   if( p->pEList && p->pEList->nExpr>SQLITE_MAX_COLUMN ){
     sqlite3ErrorMsg(pParse, "too many columns in result set");
     rc = SQLITE_ERROR;
   }
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     rc = SQLITE_NOMEM;
   }
   return rc;
 }
@@ -55109,8 +58315,9 @@
 ){
   int nErr = 0;
   int i, j;
   ExprList *pEList;
+  sqlite3 *db = pParse->db;
 
   if( pSelect==0 || pOrderBy==0 ) return 1;
   if( mustComplete ){
     for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
@@ -55141,25 +58348,25 @@
       }
       if( !mustComplete ) continue;
       iCol--;
     }
-    if( iCol<0 && (zLabel = sqlite3NameFromToken(&pE->token))!=0 ){
+    if( iCol<0 && (zLabel = sqlite3NameFromToken(db, &pE->token))!=0 ){
       for(j=0, pItem=pEList->a; j<pEList->nExpr; j++, pItem++){
         char *zName;
         int isMatch;
         if( pItem->zName ){
-          zName = sqlite3StrDup(pItem->zName);
+          zName = sqlite3DbStrDup(db, pItem->zName);
         }else{
-          zName = sqlite3NameFromToken(&pItem->pExpr->token);
+          zName = sqlite3NameFromToken(db, &pItem->pExpr->token);
         }
         isMatch = zName && sqlite3StrICmp(zName, zLabel)==0;
-        sqliteFree(zName);
+        sqlite3_free(zName);
         if( isMatch ){
           iCol = j;
           break;
         }
       }
-      sqliteFree(zLabel);
+      sqlite3_free(zLabel);
     }
     if( iCol>=0 ){
       pE->op = TK_COLUMN;
       pE->iColumn = iCol;
@@ -55641,9 +58848,10 @@
     CollSeq **aCopy;              /* A copy of pKeyInfo->aColl[] */
 
     assert( p->pRightmost==p );
     nKeyCol = nCol + (pOrderBy ? pOrderBy->nExpr : 0);
-    pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1));
+    pKeyInfo = sqlite3DbMallocZero(pParse->db,
+                       sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1));
     if( !pKeyInfo ){
       rc = SQLITE_NOMEM;
       goto multi_select_end;
     }
@@ -55716,9 +58924,9 @@
       pKeyInfo = 0;
       generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm);
     }
 
-    sqliteFree(pKeyInfo);
+    sqlite3_free(pKeyInfo);
   }
 
 multi_select_end:
   return rc;
@@ -55725,8 +58933,12 @@
 }
 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 
 #ifndef SQLITE_OMIT_VIEW
+/* Forward Declarations */
+static void substExprList(sqlite3*, ExprList*, int, ExprList*);
+static void substSelect(sqlite3*, Select *, int, ExprList *);
+
 /*
 ** Scan through the expression pExpr.  Replace every reference to
 ** a column in table number iTable with a copy of the iColumn-th
 ** entry in pEList.  (But leave references to the ROWID column
@@ -55738,11 +58950,14 @@
 ** FORM clause entry is iTable.  This routine make the necessary
 ** changes to pExpr so that it refers directly to the source table
 ** of the subquery rather the result set of the subquery.
 */
-static void substExprList(ExprList*,int,ExprList*);  /* Forward Decl */
-static void substSelect(Select *, int, ExprList *);  /* Forward Decl */
-static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){
+static void substExpr(
+  sqlite3 *db,        /* Report malloc errors to this connection */
+  Expr *pExpr,        /* Expr in which substitution occurs */
+  int iTable,         /* Table to be substituted */
+  ExprList *pEList    /* Substitute expressions */
+){
   if( pExpr==0 ) return;
   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
     if( pExpr->iColumn<0 ){
       pExpr->op = TK_NULL;
@@ -55753,44 +58968,54 @@
       pNew = pEList->a[pExpr->iColumn].pExpr;
       assert( pNew!=0 );
       pExpr->op = pNew->op;
       assert( pExpr->pLeft==0 );
-      pExpr->pLeft = sqlite3ExprDup(pNew->pLeft);
+      pExpr->pLeft = sqlite3ExprDup(db, pNew->pLeft);
       assert( pExpr->pRight==0 );
-      pExpr->pRight = sqlite3ExprDup(pNew->pRight);
+      pExpr->pRight = sqlite3ExprDup(db, pNew->pRight);
       assert( pExpr->pList==0 );
-      pExpr->pList = sqlite3ExprListDup(pNew->pList);
+      pExpr->pList = sqlite3ExprListDup(db, pNew->pList);
       pExpr->iTable = pNew->iTable;
       pExpr->pTab = pNew->pTab;
       pExpr->iColumn = pNew->iColumn;
       pExpr->iAgg = pNew->iAgg;
-      sqlite3TokenCopy(&pExpr->token, &pNew->token);
-      sqlite3TokenCopy(&pExpr->span, &pNew->span);
-      pExpr->pSelect = sqlite3SelectDup(pNew->pSelect);
+      sqlite3TokenCopy(db, &pExpr->token, &pNew->token);
+      sqlite3TokenCopy(db, &pExpr->span, &pNew->span);
+      pExpr->pSelect = sqlite3SelectDup(db, pNew->pSelect);
       pExpr->flags = pNew->flags;
     }
   }else{
-    substExpr(pExpr->pLeft, iTable, pEList);
-    substExpr(pExpr->pRight, iTable, pEList);
-    substSelect(pExpr->pSelect, iTable, pEList);
-    substExprList(pExpr->pList, iTable, pEList);
-  }
-}
-static void substExprList(ExprList *pList, int iTable, ExprList *pEList){
+    substExpr(db, pExpr->pLeft, iTable, pEList);
+    substExpr(db, pExpr->pRight, iTable, pEList);
+    substSelect(db, pExpr->pSelect, iTable, pEList);
+    substExprList(db, pExpr->pList, iTable, pEList);
+  }
+}
+static void substExprList(
+  sqlite3 *db,         /* Report malloc errors here */
+  ExprList *pList,     /* List to scan and in which to make substitutes */
+  int iTable,          /* Table to be substituted */
+  ExprList *pEList     /* Substitute values */
+){
   int i;
   if( pList==0 ) return;
   for(i=0; i<pList->nExpr; i++){
-    substExpr(pList->a[i].pExpr, iTable, pEList);
-  }
-}
-static void substSelect(Select *p, int iTable, ExprList *pEList){
+    substExpr(db, pList->a[i].pExpr, iTable, pEList);
+  }
+}
+static void substSelect(
+  sqlite3 *db,         /* Report malloc errors here */
+  Select *p,           /* SELECT statement in which to make substitutions */
+  int iTable,          /* Table to be replaced */
+  ExprList *pEList     /* Substitute values */
+){
   if( !p ) return;
-  substExprList(p->pEList, iTable, pEList);
-  substExprList(p->pGroupBy, iTable, pEList);
-  substExprList(p->pOrderBy, iTable, pEList);
-  substExpr(p->pHaving, iTable, pEList);
-  substExpr(p->pWhere, iTable, pEList);
-  substSelect(p->pPrior, iTable, pEList);
+  substExprList(db, p->pEList, iTable, pEList);
+  substExprList(db, p->pGroupBy, iTable, pEList);
+  substExprList(db, p->pOrderBy, iTable, pEList);
+  substExpr(db, p->pHaving, iTable, pEList);
+  substExpr(db, p->pWhere, iTable, pEList);
+  substSelect(db, p->pPrior, iTable, pEList);
 }
 #endif /* !defined(SQLITE_OMIT_VIEW) */
 
 #ifndef SQLITE_OMIT_VIEW
@@ -55871,8 +59096,9 @@
 ** All of the expression analysis must occur on both the outer query and
 ** the subquery before this routine runs.
 */
 static int flattenSubquery(
+  sqlite3 *db,         /* Database connection */
   Select *p,           /* The parent or outer SELECT statement */
   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
   int isAgg,           /* True if outer SELECT uses aggregate functions */
   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
@@ -55968,15 +59194,15 @@
     int nSubSrc = pSubSrc->nSrc;
     int jointype = pSubitem->jointype;
 
     sqlite3DeleteTable(pSubitem->pTab);
-    sqliteFree(pSubitem->zDatabase);
-    sqliteFree(pSubitem->zName);
-    sqliteFree(pSubitem->zAlias);
+    sqlite3_free(pSubitem->zDatabase);
+    sqlite3_free(pSubitem->zName);
+    sqlite3_free(pSubitem->zAlias);
     if( nSubSrc>1 ){
       int extra = nSubSrc - 1;
       for(i=1; i<nSubSrc; i++){
-        pSrc = sqlite3SrcListAppend(pSrc, 0, 0);
+        pSrc = sqlite3SrcListAppend(db, pSrc, 0, 0);
       }
       p->pSrc = pSrc;
       for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
         pSrc->a[i] = pSrc->a[i-extra];
@@ -56004,39 +59230,41 @@
   pList = p->pEList;
   for(i=0; i<pList->nExpr; i++){
     Expr *pExpr;
     if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
-      pList->a[i].zName = sqliteStrNDup((char*)pExpr->span.z, pExpr->span.n);
-    }
-  }
-  substExprList(p->pEList, iParent, pSub->pEList);
+      pList->a[i].zName =
+             sqlite3DbStrNDup(db, (char*)pExpr->span.z, pExpr->span.n);
+    }
+  }
+  substExprList(db, p->pEList, iParent, pSub->pEList);
   if( isAgg ){
-    substExprList(p->pGroupBy, iParent, pSub->pEList);
-    substExpr(p->pHaving, iParent, pSub->pEList);
+    substExprList(db, p->pGroupBy, iParent, pSub->pEList);
+    substExpr(db, p->pHaving, iParent, pSub->pEList);
   }
   if( pSub->pOrderBy ){
     assert( p->pOrderBy==0 );
     p->pOrderBy = pSub->pOrderBy;
     pSub->pOrderBy = 0;
   }else if( p->pOrderBy ){
-    substExprList(p->pOrderBy, iParent, pSub->pEList);
+    substExprList(db, p->pOrderBy, iParent, pSub->pEList);
   }
   if( pSub->pWhere ){
-    pWhere = sqlite3ExprDup(pSub->pWhere);
+    pWhere = sqlite3ExprDup(db, pSub->pWhere);
   }else{
     pWhere = 0;
   }
   if( subqueryIsAgg ){
     assert( p->pHaving==0 );
     p->pHaving = p->pWhere;
     p->pWhere = pWhere;
-    substExpr(p->pHaving, iParent, pSub->pEList);
-    p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving));
+    substExpr(db, p->pHaving, iParent, pSub->pEList);
+    p->pHaving = sqlite3ExprAnd(db, p->pHaving,
+                                sqlite3ExprDup(db, pSub->pHaving));
     assert( p->pGroupBy==0 );
-    p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy);
-  }else{
-    substExpr(p->pWhere, iParent, pSub->pEList);
-    p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere);
+    p->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy);
+  }else{
+    substExpr(db, p->pWhere, iParent, pSub->pEList);
+    p->pWhere = sqlite3ExprAnd(db, p->pWhere, pWhere);
   }
 
   /* The flattened query is distinct if either the inner or the
   ** outer query is distinct.
@@ -56250,9 +59478,10 @@
       if( iCol>0 && iCol<=pEList->nExpr ){
         CollSeq *pColl = pE->pColl;
         int flags = pE->flags & EP_ExpCollate;
         sqlite3ExprDelete(pE);
-        pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr);
+        pE = sqlite3ExprDup(pParse->db, pEList->a[iCol-1].pExpr);
+        pOrderBy->a[i].pExpr = pE;
         if( pColl && flags ){
           pE->pColl = pColl;
           pE->flags |= flags;
         }
@@ -56369,9 +59598,9 @@
       return SQLITE_ERROR;
     }
   }
 
-  if( sqlite3MallocFailed() ){
+  if( pParse->db->mallocFailed ){
     return SQLITE_NOMEM;
   }
 
   /* Make sure the GROUP BY clause does not contain aggregate functions.
@@ -56576,10 +59805,12 @@
   int rc = 1;            /* Value to return from this function */
   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
   AggInfo sAggInfo;      /* Information used by aggregate queries */
   int iEnd;              /* Address of the end of the query */
-
-  if( p==0 || sqlite3MallocFailed() || pParse->nErr ){
+  sqlite3 *db;           /* The database connection */
+
+  db = pParse->db;
+  if( p==0 || db->mallocFailed || pParse->nErr ){
     return 1;
   }
   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
   memset(&sAggInfo, 0, sizeof(sAggInfo));
@@ -56664,9 +59895,9 @@
       needRestoreContext = 1;
     }else{
       needRestoreContext = 0;
     }
-#if SQLITE_MAX_EXPR_DEPTH>0
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
     /* Increment Parse.nHeight by the height of the largest expression
     ** tree refered to by this, the parent select. The child select
     ** may contain expression trees of at most
     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
@@ -56676,9 +59907,9 @@
     pParse->nHeight += sqlite3SelectExprHeight(p);
 #endif
     sqlite3Select(pParse, pItem->pSelect, SRT_EphemTab,
                  pItem->iCursor, p, i, &isAgg, 0);
-#if SQLITE_MAX_EXPR_DEPTH>0
+#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
     pParse->nHeight -= sqlite3SelectExprHeight(p);
 #endif
     if( needRestoreContext ){
       pParse->zAuthContext = zSavedAuthContext;
@@ -56706,9 +59937,9 @@
   ** If flattening is a possiblity, do so and return immediately.
   */
 #ifndef SQLITE_OMIT_VIEW
   if( pParent && pParentAgg &&
-      flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){
+      flattenSubquery(db, pParent, parentTab, *pParentAgg, isAgg) ){
     if( isAgg ) *pParentAgg = 1;
     goto select_end;
   }
 #endif
@@ -56833,9 +60064,9 @@
       if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){
         goto select_end;
       }
     }
-    if( sqlite3MallocFailed() ) goto select_end;
+    if( db->mallocFailed ) goto select_end;
 
     /* Processing for aggregates with GROUP BY is very different and
     ** much more complex tha aggregates without a GROUP BY.
     */
@@ -57082,10 +60313,10 @@
   if( rc==SQLITE_OK && eDest==SRT_Callback ){
     generateColumnNames(pParse, pTabList, pEList);
   }
 
-  sqliteFree(sAggInfo.aCol);
-  sqliteFree(sAggInfo.aFunc);
+  sqlite3_free(sAggInfo.aCol);
+  sqlite3_free(sAggInfo.aFunc);
   return rc;
 }
 
 #if defined(SQLITE_DEBUG)
@@ -57302,9 +60533,9 @@
 ** from malloc().  But the caller cannot free this memory directly.
 ** Instead, the entire table should be passed to sqlite3_free_table() when
 ** the calling procedure is finished using it.
 */
-int sqlite3_get_table(
+SQLITE_API int sqlite3_get_table(
   sqlite3 *db,                /* The database on which the SQL executes */
   const char *zSql,           /* The SQL to be executed */
   char ***pazResult,          /* Write the result table here */
   int *pnRow,                 /* Write the number of rows in the result here */
@@ -57327,8 +60558,13 @@
   res.azResult = sqlite3_malloc( sizeof(char*)*res.nAlloc );
   if( res.azResult==0 ) return SQLITE_NOMEM;
   res.azResult[0] = 0;
   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
+#ifndef NDEBUG
+  sqlite3_mutex_enter(db->mutex);
+  assert((rc&db->errMask)==rc && (res.rc&db->errMask)==res.rc);
+  sqlite3_mutex_leave(db->mutex);
+#endif
   if( res.azResult ){
     assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
     res.azResult[0] = (char*)res.nData;
   }
@@ -57338,17 +60574,19 @@
       if( pzErrMsg ){
         sqlite3_free(*pzErrMsg);
         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
       }
-      sqliteFree(res.zErrMsg);
-    }
+      sqlite3_free(res.zErrMsg);
+    }
+    sqlite3_mutex_enter(db->mutex);
     db->errCode = res.rc;
-    return res.rc & db->errMask;
-  }
-  sqliteFree(res.zErrMsg);
+    sqlite3_mutex_leave(db->mutex);
+    return res.rc;
+  }
+  sqlite3_free(res.zErrMsg);
   if( rc!=SQLITE_OK ){
     sqlite3_free_table(&res.azResult[1]);
-    return rc & db->errMask;
+    return rc;
   }
   if( res.nAlloc>res.nData ){
     char **azNew;
     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) );
@@ -57361,15 +60599,15 @@
   }
   *pazResult = &res.azResult[1];
   if( pnColumn ) *pnColumn = res.nColumn;
   if( pnRow ) *pnRow = res.nRow;
-  return rc & db->errMask;
+  return rc;
 }
 
 /*
 ** This routine frees the space the sqlite3_get_table() malloced.
 */
-void sqlite3_free_table(
+SQLITE_API void sqlite3_free_table(
   char **azResult            /* Result returned from from sqlite3_get_table() */
 ){
   if( azResult ){
     int i, n;
@@ -57406,15 +60644,15 @@
   while( pTriggerStep ){
     TriggerStep * pTmp = pTriggerStep;
     pTriggerStep = pTriggerStep->pNext;
 
-    if( pTmp->target.dyn ) sqliteFree((char*)pTmp->target.z);
+    if( pTmp->target.dyn ) sqlite3_free((char*)pTmp->target.z);
     sqlite3ExprDelete(pTmp->pWhere);
     sqlite3ExprListDelete(pTmp->pExprList);
     sqlite3SelectDelete(pTmp->pSelect);
     sqlite3IdListDelete(pTmp->pIdList);
 
-    sqliteFree(pTmp);
+    sqlite3_free(pTmp);
   }
 }
 
 /*
@@ -57468,9 +60706,9 @@
   ** then set iDb to 1 to create the trigger in the temporary database.
   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
   ** exist, the error is caught by the block below.
   */
-  if( !pTableName || sqlite3MallocFailed() ){
+  if( !pTableName || db->mallocFailed ){
     goto trigger_cleanup;
   }
   pTab = sqlite3SrcListLookup(pParse, pTableName);
   if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
@@ -57477,9 +60715,9 @@
     iDb = 1;
   }
 
   /* Ensure the table name matches database name and that the table exists */
-  if( sqlite3MallocFailed() ) goto trigger_cleanup;
+  if( db->mallocFailed ) goto trigger_cleanup;
   assert( pTableName->nSrc==1 );
   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) &&
       sqlite3FixSrcList(&sFix, pTableName) ){
     goto trigger_cleanup;
@@ -57495,9 +60733,9 @@
   }
 
   /* Check that the trigger name is not reserved and that no trigger of the
   ** specified name exists */
-  zName = sqlite3NameFromToken(pName);
+  zName = sqlite3NameFromToken(db, pName);
   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
     goto trigger_cleanup;
   }
   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), zName,strlen(zName)) ){
@@ -57553,25 +60791,25 @@
     tr_tm = TK_BEFORE;
   }
 
   /* Build the Trigger object */
-  pTrigger = (Trigger*)sqliteMalloc(sizeof(Trigger));
+  pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
   if( pTrigger==0 ) goto trigger_cleanup;
   pTrigger->name = zName;
   zName = 0;
-  pTrigger->table = sqliteStrDup(pTableName->a[0].zName);
+  pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
   pTrigger->pSchema = db->aDb[iDb].pSchema;
   pTrigger->pTabSchema = pTab->pSchema;
   pTrigger->op = op;
   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
-  pTrigger->pWhen = sqlite3ExprDup(pWhen);
-  pTrigger->pColumns = sqlite3IdListDup(pColumns);
-  sqlite3TokenCopy(&pTrigger->nameToken,pName);
+  pTrigger->pWhen = sqlite3ExprDup(db, pWhen);
+  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
+  sqlite3TokenCopy(db, &pTrigger->nameToken,pName);
   assert( pParse->pNewTrigger==0 );
   pParse->pNewTrigger = pTrigger;
 
 trigger_cleanup:
-  sqliteFree(zName);
+  sqlite3_free(zName);
   sqlite3SrcListDelete(pTableName);
   sqlite3IdListDelete(pColumns);
   sqlite3ExprDelete(pWhen);
   if( !pParse->pNewTrigger ){
@@ -57638,10 +60876,11 @@
     sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0);
     sqlite3VdbeChangeP3(v, addr+6, (char*)pAll->z, pAll->n);
     sqlite3ChangeCookie(db, v, iDb);
     sqlite3VdbeAddOp(v, OP_Close, 0, 0);
-    sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
-       sqlite3MPrintf("type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC);
+    sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, sqlite3MPrintf(
+        db, "type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC
+    );
   }
 
   if( db->init.busy ){
     int n;
@@ -57649,9 +60888,10 @@
     Trigger *pDel;
     pDel = sqlite3HashInsert(&db->aDb[iDb].pSchema->trigHash,
                      pTrig->name, strlen(pTrig->name), pTrig);
     if( pDel ){
-      assert( sqlite3MallocFailed() && pDel==pTrig );
+      assert( pDel==pTrig );
+      db->mallocFailed = 1;
       goto triggerfinish_cleanup;
     }
     n = strlen(pTrig->table) + 1;
     pTab = sqlite3HashFind(&pTrig->pTabSchema->tblHash, pTrig->table, n);
@@ -57669,37 +60909,37 @@
 
 /*
 ** Make a copy of all components of the given trigger step.  This has
 ** the effect of copying all Expr.token.z values into memory obtained
-** from sqliteMalloc().  As initially created, the Expr.token.z values
+** from sqlite3_malloc().  As initially created, the Expr.token.z values
 ** all point to the input string that was fed to the parser.  But that
 ** string is ephemeral - it will go away as soon as the sqlite3_exec()
 ** call that started the parser exits.  This routine makes a persistent
 ** copy of all the Expr.token.z strings so that the TriggerStep structure
 ** will be valid even after the sqlite3_exec() call returns.
 */
-static void sqlitePersistTriggerStep(TriggerStep *p){
+static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
   if( p->target.z ){
-    p->target.z = (u8*)sqliteStrNDup((char*)p->target.z, p->target.n);
+    p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
     p->target.dyn = 1;
   }
   if( p->pSelect ){
-    Select *pNew = sqlite3SelectDup(p->pSelect);
+    Select *pNew = sqlite3SelectDup(db, p->pSelect);
     sqlite3SelectDelete(p->pSelect);
     p->pSelect = pNew;
   }
   if( p->pWhere ){
-    Expr *pNew = sqlite3ExprDup(p->pWhere);
+    Expr *pNew = sqlite3ExprDup(db, p->pWhere);
     sqlite3ExprDelete(p->pWhere);
     p->pWhere = pNew;
   }
   if( p->pExprList ){
-    ExprList *pNew = sqlite3ExprListDup(p->pExprList);
+    ExprList *pNew = sqlite3ExprListDup(db, p->pExprList);
     sqlite3ExprListDelete(p->pExprList);
     p->pExprList = pNew;
   }
   if( p->pIdList ){
-    IdList *pNew = sqlite3IdListDup(p->pIdList);
+    IdList *pNew = sqlite3IdListDup(db, p->pIdList);
     sqlite3IdListDelete(p->pIdList);
     p->pIdList = pNew;
   }
 }
@@ -57710,10 +60950,10 @@
 **
 ** The parser calls this routine when it finds a SELECT statement in
 ** body of a TRIGGER.
 */
-SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(Select *pSelect){
-  TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
+SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
+  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   if( pTriggerStep==0 ) {
     sqlite3SelectDelete(pSelect);
     return 0;
   }
@@ -57720,9 +60960,9 @@
 
   pTriggerStep->op = TK_SELECT;
   pTriggerStep->pSelect = pSelect;
   pTriggerStep->orconf = OE_Default;
-  sqlitePersistTriggerStep(pTriggerStep);
+  sqlitePersistTriggerStep(db, pTriggerStep);
 
   return pTriggerStep;
 }
 
@@ -57733,31 +60973,33 @@
 ** The parser calls this routine when it sees an INSERT inside the
 ** body of a trigger.
 */
 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
+  sqlite3 *db,        /* The database connection */
   Token *pTableName,  /* Name of the table into which we insert */
   IdList *pColumn,    /* List of columns in pTableName to insert into */
   ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
   Select *pSelect,    /* A SELECT statement that supplies values */
   int orconf          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
 ){
-  TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
+  TriggerStep *pTriggerStep;
 
   assert(pEList == 0 || pSelect == 0);
-  assert(pEList != 0 || pSelect != 0);
-
+  assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
+
+  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   if( pTriggerStep ){
     pTriggerStep->op = TK_INSERT;
     pTriggerStep->pSelect = pSelect;
     pTriggerStep->target  = *pTableName;
     pTriggerStep->pIdList = pColumn;
     pTriggerStep->pExprList = pEList;
     pTriggerStep->orconf = orconf;
-    sqlitePersistTriggerStep(pTriggerStep);
+    sqlitePersistTriggerStep(db, pTriggerStep);
   }else{
     sqlite3IdListDelete(pColumn);
     sqlite3ExprListDelete(pEList);
-    sqlite3SelectDup(pSelect);
+    sqlite3SelectDelete(pSelect);
   }
 
   return pTriggerStep;
 }
@@ -57767,14 +61009,15 @@
 ** a pointer to that trigger step.  The parser calls this routine when it
 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
 */
 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
+  sqlite3 *db,         /* The database connection */
   Token *pTableName,   /* Name of the table to be updated */
   ExprList *pEList,    /* The SET clause: list of column and new values */
   Expr *pWhere,        /* The WHERE clause */
   int orconf           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
 ){
-  TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
+  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   if( pTriggerStep==0 ){
      sqlite3ExprListDelete(pEList);
      sqlite3ExprDelete(pWhere);
      return 0;
@@ -57784,9 +61027,9 @@
   pTriggerStep->target  = *pTableName;
   pTriggerStep->pExprList = pEList;
   pTriggerStep->pWhere = pWhere;
   pTriggerStep->orconf = orconf;
-  sqlitePersistTriggerStep(pTriggerStep);
+  sqlitePersistTriggerStep(db, pTriggerStep);
 
   return pTriggerStep;
 }
 
@@ -57794,10 +61037,14 @@
 ** Construct a trigger step that implements a DELETE statement and return
 ** a pointer to that trigger step.  The parser calls this routine when it
 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
 */
-SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(Token *pTableName, Expr *pWhere){
-  TriggerStep *pTriggerStep = sqliteMalloc(sizeof(TriggerStep));
+SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
+  sqlite3 *db,            /* Database connection */
+  Token *pTableName,      /* The table from which rows are deleted */
+  Expr *pWhere            /* The WHERE clause */
+){
+  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   if( pTriggerStep==0 ){
     sqlite3ExprDelete(pWhere);
     return 0;
   }
@@ -57805,9 +61052,9 @@
   pTriggerStep->op = TK_DELETE;
   pTriggerStep->target  = *pTableName;
   pTriggerStep->pWhere = pWhere;
   pTriggerStep->orconf = OE_Default;
-  sqlitePersistTriggerStep(pTriggerStep);
+  sqlitePersistTriggerStep(db, pTriggerStep);
 
   return pTriggerStep;
 }
 
@@ -57816,14 +61063,14 @@
 */
 SQLITE_PRIVATE void sqlite3DeleteTrigger(Trigger *pTrigger){
   if( pTrigger==0 ) return;
   sqlite3DeleteTriggerStep(pTrigger->step_list);
-  sqliteFree(pTrigger->name);
-  sqliteFree(pTrigger->table);
+  sqlite3_free(pTrigger->name);
+  sqlite3_free(pTrigger->table);
   sqlite3ExprDelete(pTrigger->pWhen);
   sqlite3IdListDelete(pTrigger->pColumns);
-  if( pTrigger->nameToken.dyn ) sqliteFree((char*)pTrigger->nameToken.z);
-  sqliteFree(pTrigger);
+  if( pTrigger->nameToken.dyn ) sqlite3_free((char*)pTrigger->nameToken.z);
+  sqlite3_free(pTrigger);
 }
 
 /*
 ** This function is called to drop a trigger from the database schema.
@@ -57840,9 +61087,9 @@
   const char *zName;
   int nName;
   sqlite3 *db = pParse->db;
 
-  if( sqlite3MallocFailed() ) goto drop_trigger_cleanup;
+  if( db->mallocFailed ) goto drop_trigger_cleanup;
   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
     goto drop_trigger_cleanup;
   }
 
@@ -58029,11 +61276,11 @@
   if( iDb==0 || iDb>=2 ){
     assert( iDb<pParse->db->nDb );
     sDb.z = (u8*)pParse->db->aDb[iDb].zName;
     sDb.n = strlen((char*)sDb.z);
-    pSrc = sqlite3SrcListAppend(0, &sDb, &pStep->target);
+    pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
   } else {
-    pSrc = sqlite3SrcListAppend(0, &pStep->target, 0);
+    pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
   }
   return pSrc;
 }
 
@@ -58048,8 +61295,9 @@
 ){
   TriggerStep * pTriggerStep = pStepList;
   int orconf;
   Vdbe *v = pParse->pVdbe;
+  sqlite3 *db = pParse->db;
 
   assert( pTriggerStep!=0 );
   assert( v!=0 );
   sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0);
@@ -58058,23 +61306,23 @@
     orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin;
     pParse->trigStack->orconf = orconf;
     switch( pTriggerStep->op ){
       case TK_SELECT: {
-	Select *ss = sqlite3SelectDup(pTriggerStep->pSelect);
+        Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect);
         if( ss ){
           sqlite3SelectResolve(pParse, ss, 0);
           sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0);
           sqlite3SelectDelete(ss);
         }
-	break;
+        break;
       }
       case TK_UPDATE: {
         SrcList *pSrc;
         pSrc = targetSrcList(pParse, pTriggerStep);
         sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
         sqlite3Update(pParse, pSrc,
-		sqlite3ExprListDup(pTriggerStep->pExprList),
-		sqlite3ExprDup(pTriggerStep->pWhere), orconf);
+                sqlite3ExprListDup(db, pTriggerStep->pExprList),
+                sqlite3ExprDup(db, pTriggerStep->pWhere), orconf);
         sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
         break;
       }
       case TK_INSERT: {
@@ -58081,19 +61329,20 @@
         SrcList *pSrc;
         pSrc = targetSrcList(pParse, pTriggerStep);
         sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
         sqlite3Insert(pParse, pSrc,
-          sqlite3ExprListDup(pTriggerStep->pExprList),
-          sqlite3SelectDup(pTriggerStep->pSelect),
-          sqlite3IdListDup(pTriggerStep->pIdList), orconf);
+          sqlite3ExprListDup(db, pTriggerStep->pExprList),
+          sqlite3SelectDup(db, pTriggerStep->pSelect),
+          sqlite3IdListDup(db, pTriggerStep->pIdList), orconf);
         sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
         break;
       }
       case TK_DELETE: {
         SrcList *pSrc;
         sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0);
         pSrc = targetSrcList(pParse, pTriggerStep);
-        sqlite3DeleteFrom(pParse, pSrc, sqlite3ExprDup(pTriggerStep->pWhere));
+        sqlite3DeleteFrom(pParse, pSrc,
+                          sqlite3ExprDup(db, pTriggerStep->pWhere));
         sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0);
         break;
       }
       default:
@@ -58190,9 +61439,9 @@
       sqlite3AuthContextPush(pParse, &sContext, p->name);
 
       /* code the WHEN clause */
       endTrigger = sqlite3VdbeMakeLabel(pParse->pVdbe);
-      whenExpr = sqlite3ExprDup(p->pWhen);
+      whenExpr = sqlite3ExprDup(pParse->db, p->pWhen);
       if( sqlite3ExprResolveNames(&sNC, whenExpr) ){
         pParse->trigStack = trigStackEntry.pNext;
         sqlite3ExprDelete(whenExpr);
         return 1;
@@ -58228,9 +61477,9 @@
 *************************************************************************
 ** This file contains C code routines that are called by the parser
 ** to handle UPDATE statements.
 **
-** $Id: update.c,v 1.138 2007/06/25 16:29:34 danielk1977 Exp $
+** $Id: update.c,v 1.140 2007/08/16 10:09:03 danielk1977 Exp $
 */
 
 #ifndef SQLITE_OMIT_VIRTUALTABLE
 /* Forward declaration */
@@ -58275,9 +61524,9 @@
     sqlite3_value *pValue;
     u8 enc = ENC(sqlite3VdbeDb(v));
     Column *pCol = &pTab->aCol[i];
     assert( i<pTab->nCol );
-    sqlite3ValueFromExpr(pCol->pDflt, enc, pCol->affinity, &pValue);
+    sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, pCol->affinity, &pValue);
     if( pValue ){
       sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM);
     }else{
       VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName));
@@ -58330,12 +61579,12 @@
   int newIdx      = -1;  /* index of trigger "new" temp table       */
   int oldIdx      = -1;  /* index of trigger "old" temp table       */
 
   sContext.pParse = 0;
-  if( pParse->nErr || sqlite3MallocFailed() ){
+  db = pParse->db;
+  if( pParse->nErr || db->mallocFailed ){
     goto update_cleanup;
   }
-  db = pParse->db;
   assert( pTabList->nSrc==1 );
 
   /* Locate the table which we want to update.
   */
@@ -58363,9 +61612,9 @@
   }
   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
     goto update_cleanup;
   }
-  aXRef = sqliteMallocRaw( sizeof(int) * pTab->nCol );
+  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
   if( aXRef==0 ) goto update_cleanup;
   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
 
   /* If there are FOR EACH ROW triggers, allocate cursors for the
@@ -58450,9 +61699,9 @@
     }
     if( i<pIdx->nColumn ) nIdx++;
   }
   if( nIdxTotal>0 ){
-    apIdx = sqliteMallocRaw( sizeof(Index*) * nIdx + nIdxTotal );
+    apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal );
     if( apIdx==0 ) goto update_cleanup;
     aIdxUsed = (char*)&apIdx[nIdx];
   }
   for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
@@ -58506,9 +61755,9 @@
   ** a ephemeral table.
   */
   if( isView ){
     Select *pView;
-    pView = sqlite3SelectDup(pTab->pSelect);
+    pView = sqlite3SelectDup(db, pTab->pSelect);
     sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0);
     sqlite3SelectDelete(pView);
   }
 
@@ -58740,10 +61989,10 @@
   }
 
 update_cleanup:
   sqlite3AuthContextPop(&sContext);
-  sqliteFree(apIdx);
-  sqliteFree(aXRef);
+  sqlite3_free(apIdx);
+  sqlite3_free(aXRef);
   sqlite3SrcListDelete(pTabList);
   sqlite3ExprListDelete(pChanges);
   sqlite3ExprDelete(pWhere);
   return;
@@ -58784,26 +62033,29 @@
   Expr *pExpr;              /* Temporary expression */
   int ephemTab;             /* Table holding the result of the SELECT */
   int i;                    /* Loop counter */
   int addr;                 /* Address of top of loop */
+  sqlite3 *db = pParse->db; /* Database connection */
 
   /* Construct the SELECT statement that will find the new values for
   ** all updated rows.
   */
-  pEList = sqlite3ExprListAppend(0, sqlite3CreateIdExpr("_rowid_"), 0);
+  pEList = sqlite3ExprListAppend(pParse, 0,
+                                 sqlite3CreateIdExpr(pParse, "_rowid_"), 0);
   if( pRowid ){
-    pEList = sqlite3ExprListAppend(pEList, sqlite3ExprDup(pRowid), 0);
+    pEList = sqlite3ExprListAppend(pParse, pEList,
+                                   sqlite3ExprDup(db, pRowid), 0);
   }
   assert( pTab->iPKey<0 );
   for(i=0; i<pTab->nCol; i++){
     if( aXRef[i]>=0 ){
-      pExpr = sqlite3ExprDup(pChanges->a[aXRef[i]].pExpr);
-    }else{
-      pExpr = sqlite3CreateIdExpr(pTab->aCol[i].zName);
-    }
-    pEList = sqlite3ExprListAppend(pEList, pExpr, 0);
-  }
-  pSelect = sqlite3SelectNew(pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
+      pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr);
+    }else{
+      pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
+    }
+    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0);
+  }
+  pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
 
   /* Create the ephemeral table into which the update results will
   ** be stored.
   */
@@ -58859,9 +62111,9 @@
 **
 ** Most of the code in this file may be omitted by defining the
 ** SQLITE_OMIT_VACUUM macro.
 **
-** $Id: vacuum.c,v 1.69 2007/03/27 16:19:52 danielk1977 Exp $
+** $Id: vacuum.c,v 1.73 2007/08/29 12:31:28 danielk1977 Exp $
 */
 
 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
 /*
@@ -58868,8 +62120,11 @@
 ** Execute zSql on database db. Return an error code.
 */
 static int execSql(sqlite3 *db, const char *zSql){
   sqlite3_stmt *pStmt;
+  if( !zSql ){
+    return SQLITE_NOMEM;
+  }
   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
     return sqlite3_errcode(db);
   }
   while( SQLITE_ROW==sqlite3_step(pStmt) ){}
@@ -58954,9 +62209,9 @@
   assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
   pTemp = db->aDb[db->nDb-1].pBt;
   sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain),
      sqlite3BtreeGetReserve(pMain));
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     rc = SQLITE_NOMEM;
     goto end_of_vacuum;
   }
   assert( sqlite3BtreeGetPageSize(pTemp)==sqlite3BtreeGetPageSize(pMain) );
@@ -59090,11 +62345,9 @@
   */
   db->autoCommit = 1;
 
   if( pDb ){
-    sqlite3MallocDisallow();
-    sqlite3BtreeClose(pDb->pBt);
-    sqlite3MallocAllow();
+    sqlite3BtreeClose(pDb->pBt);
     pDb->pBt = 0;
     pDb->pSchema = 0;
   }
 
@@ -59118,9 +62371,9 @@
 **
 *************************************************************************
 ** This file contains code used to help implement virtual tables.
 **
-** $Id: vtab.c,v 1.48 2007/06/26 10:38:55 danielk1977 Exp $
+** $Id: vtab.c,v 1.57 2007/09/04 15:38:58 danielk1977 Exp $
 */
 #ifndef SQLITE_OMIT_VIRTUALTABLE
 
 static int createModule(
@@ -59129,10 +62382,14 @@
   const sqlite3_module *pModule,  /* The definition of the module */
   void *pAux,                     /* Context pointer for xCreate/xConnect */
   void (*xDestroy)(void *)        /* Module destructor function */
 ) {
-  int nName = strlen(zName);
-  Module *pMod = (Module *)sqliteMallocRaw(sizeof(Module) + nName + 1);
+  int rc, nName;
+  Module *pMod;
+
+  sqlite3_mutex_enter(db->mutex);
+  nName = strlen(zName);
+  pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
   if( pMod ){
     char *zCopy = (char *)(&pMod[1]);
     memcpy(zCopy, zName, nName+1);
     pMod->zName = zCopy;
@@ -59142,19 +62399,21 @@
     pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
     if( pMod && pMod->xDestroy ){
       pMod->xDestroy(pMod->pAux);
     }
-    sqliteFree(pMod);
+    sqlite3_free(pMod);
     sqlite3ResetInternalSchema(db, 0);
   }
-  return sqlite3ApiExit(db, SQLITE_OK);
+  rc = sqlite3ApiExit(db, SQLITE_OK);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 
 /*
 ** External API function used to create a new virtual-table module.
 */
-int sqlite3_create_module(
+SQLITE_API int sqlite3_create_module(
   sqlite3 *db,                    /* Database in which module is registered */
   const char *zName,              /* Name assigned to this module */
   const sqlite3_module *pModule,  /* The definition of the module */
   void *pAux                      /* Context pointer for xCreate/xConnect */
@@ -59164,9 +62423,9 @@
 
 /*
 ** External API function used to create a new virtual-table module.
 */
-int sqlite3_create_module_v2(
+SQLITE_API int sqlite3_create_module_v2(
   sqlite3 *db,                    /* Database in which module is registered */
   const char *zName,              /* Name assigned to this module */
   const sqlite3_module *pModule,  /* The definition of the module */
   void *pAux,                     /* Context pointer for xCreate/xConnect */
@@ -59220,11 +62479,11 @@
   }
   if( p->azModuleArg ){
     int i;
     for(i=0; i<p->nModuleArg; i++){
-      sqliteFree(p->azModuleArg[i]);
-    }
-    sqliteFree(p->azModuleArg);
+      sqlite3_free(p->azModuleArg[i]);
+    }
+    sqlite3_free(p->azModuleArg);
   }
 }
 
 /*
@@ -59232,20 +62491,20 @@
 ** The string is not copied - the pointer is stored.  The
 ** string will be freed automatically when the table is
 ** deleted.
 */
-static void addModuleArgument(Table *pTable, char *zArg){
+static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
   int i = pTable->nModuleArg++;
   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
   char **azModuleArg;
-  azModuleArg = sqliteRealloc(pTable->azModuleArg, nBytes);
+  azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
   if( azModuleArg==0 ){
     int j;
     for(j=0; j<i; j++){
-      sqliteFree(pTable->azModuleArg[j]);
-    }
-    sqliteFree(zArg);
-    sqliteFree(pTable->azModuleArg);
+      sqlite3_free(pTable->azModuleArg[j]);
+    }
+    sqlite3_free(zArg);
+    sqlite3_free(pTable->azModuleArg);
     pTable->nModuleArg = 0;
   }else{
     azModuleArg[i] = zArg;
     azModuleArg[i+1] = 0;
@@ -59265,29 +62524,29 @@
   Token *pModuleName    /* Name of the module for the virtual table */
 ){
   int iDb;              /* The database the table is being created in */
   Table *pTable;        /* The new virtual table */
-
-#ifndef SQLITE_OMIT_SHARED_CACHE
-  if( sqlite3ThreadDataReadOnly()->useSharedData ){
+  sqlite3 *db;          /* Database connection */
+
+  if( pParse->db->flags & SQLITE_SharedCache ){
     sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
     return;
   }
-#endif
 
   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
   pTable = pParse->pNewTable;
   if( pTable==0 || pParse->nErr ) return;
   assert( 0==pTable->pIndex );
 
-  iDb = sqlite3SchemaToIndex(pParse->db, pTable->pSchema);
+  db = pParse->db;
+  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
   assert( iDb>=0 );
 
   pTable->isVirtual = 1;
   pTable->nModuleArg = 0;
-  addModuleArgument(pTable, sqlite3NameFromToken(pModuleName));
-  addModuleArgument(pTable, sqlite3StrDup(pParse->db->aDb[iDb].zName));
-  addModuleArgument(pTable, sqlite3StrDup(pTable->zName));
+  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
+  addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
+  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
   pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
 
 #ifndef SQLITE_OMIT_AUTHORIZATION
   /* Creating a virtual table invokes the authorization callback twice.
@@ -59310,9 +62569,10 @@
 static void addArgumentToVtab(Parse *pParse){
   if( pParse->sArg.z && pParse->pNewTable ){
     const char *z = (const char*)pParse->sArg.z;
     int n = pParse->sArg.n;
-    addModuleArgument(pParse->pNewTable, sqliteStrNDup(z, n));
+    sqlite3 *db = pParse->db;
+    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
   }
 }
 
 /*
@@ -59352,9 +62612,9 @@
     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
     if( pEnd ){
       pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
     }
-    zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
+    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
 
     /* A slot for the record has already been allocated in the
     ** SQLITE_MASTER table.  We just need to update that slot with all
     ** the information we've collected.
@@ -59373,14 +62633,14 @@
       pTab->zName,
       pTab->zName,
       zStmt
     );
-    sqliteFree(zStmt);
+    sqlite3_free(zStmt);
     v = sqlite3GetVdbe(pParse);
     sqlite3ChangeCookie(db, v, iDb);
 
     sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
-    zWhere = sqlite3MPrintf("name='%q'", pTab->zName);
+    zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
     sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC);
     sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
   }
 
@@ -59394,8 +62654,9 @@
     const char *zName = pTab->zName;
     int nName = strlen(zName) + 1;
     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
     if( pOld ){
+      db->mallocFailed = 1;
       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
       return;
     }
     pSchema->db = pParse->db;
@@ -59441,13 +62702,13 @@
   char **pzErr
 ){
   int rc;
   int rc2;
-  sqlite3_vtab *pVtab;
+  sqlite3_vtab *pVtab = 0;
   const char *const*azArg = (const char *const*)pTab->azModuleArg;
   int nArg = pTab->nModuleArg;
   char *zErr = 0;
-  char *zModuleName = sqlite3MPrintf("%s", pTab->zName);
+  char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
 
   if( !zModuleName ){
     return SQLITE_NOMEM;
   }
@@ -59457,33 +62718,33 @@
 
   db->pVTab = pTab;
   rc = sqlite3SafetyOff(db);
   assert( rc==SQLITE_OK );
-  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab, &zErr);
-  rc2 = sqlite3SafetyOn(db);
-  pVtab = pTab->pVtab;
+  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr);
+  rc2 = sqlite3SafetyOn(db);
   if( rc==SQLITE_OK && pVtab ){
     pVtab->pModule = pMod->pModule;
     pVtab->nRef = 1;
+    pTab->pVtab = pVtab;
   }
 
   if( SQLITE_OK!=rc ){
     if( zErr==0 ){
-      *pzErr = sqlite3MPrintf("vtable constructor failed: %s", zModuleName);
+      *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
     }else {
-      *pzErr = sqlite3MPrintf("%s", zErr);
+      *pzErr = sqlite3MPrintf(db, "%s", zErr);
       sqlite3_free(zErr);
     }
   }else if( db->pVTab ){
     const char *zFormat = "vtable constructor did not declare schema: %s";
-    *pzErr = sqlite3MPrintf(zFormat, pTab->zName);
+    *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
     rc = SQLITE_ERROR;
   }
   if( rc==SQLITE_OK ){
     rc = rc2;
   }
   db->pVTab = 0;
-  sqliteFree(zModuleName);
+  sqlite3_free(zModuleName);
 
   /* If everything went according to plan, loop through the columns
   ** of the table to see if any of them contain the token "hidden".
   ** If so, set the Column.isHidden flag and remove the token from
@@ -59550,9 +62811,9 @@
     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
     if( rc!=SQLITE_OK ){
       sqlite3ErrorMsg(pParse, "%s", zErr);
     }
-    sqliteFree(zErr);
+    sqlite3_free(zErr);
   }
 
   return rc;
 }
@@ -59566,9 +62827,9 @@
   /* Grow the sqlite3.aVTrans array if required */
   if( (db->nVTrans%ARRAY_INCR)==0 ){
     sqlite3_vtab **aVTrans;
     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
-    aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes);
+    aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
     if( !aVTrans ){
       return SQLITE_NOMEM;
     }
     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
@@ -59586,9 +62847,9 @@
 ** of the virtual table named zTab in database iDb.
 **
 ** If an error occurs, *pzErr is set to point an an English language
 ** description of the error and an SQLITE_XXX error code is returned.
-** In this case the caller must call sqliteFree() on *pzErr.
+** In this case the caller must call sqlite3_free() on *pzErr.
 */
 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
   int rc = SQLITE_OK;
   Table *pTab;
@@ -59604,9 +62865,9 @@
   ** invoke it now. If the module has not been registered, return an
   ** error. Otherwise, do nothing.
   */
   if( !pMod ){
-    *pzErr = sqlite3MPrintf("no such module: %s", zModule);
+    *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
     rc = SQLITE_ERROR;
   }else{
     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
   }
@@ -59622,17 +62883,20 @@
 ** This function is used to set the schema of a virtual table.  It is only
 ** valid to call this function from within the xCreate() or xConnect() of a
 ** virtual table module.
 */
-int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
+SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
   Parse sParse;
 
   int rc = SQLITE_OK;
-  Table *pTab = db->pVTab;
+  Table *pTab;
   char *zErr = 0;
 
+  sqlite3_mutex_enter(db->mutex);
+  pTab = db->pVTab;
   if( !pTab ){
     sqlite3Error(db, SQLITE_MISUSE, 0);
+    sqlite3_mutex_leave(db->mutex);
     return SQLITE_MISUSE;
   }
   assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
 
@@ -59652,9 +62916,9 @@
     sParse.pNewTable->aCol = 0;
     db->pVTab = 0;
   } else {
     sqlite3Error(db, SQLITE_ERROR, zErr);
-    sqliteFree(zErr);
+    sqlite3_free(zErr);
     rc = SQLITE_ERROR;
   }
   sParse.declareVtab = 0;
 
@@ -59662,9 +62926,11 @@
   sqlite3DeleteTable(sParse.pNewTable);
   sParse.pNewTable = 0;
 
   assert( (rc&0xff)==rc );
-  return sqlite3ApiExit(db, rc);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 /*
 ** This function is invoked by the vdbe to call the xDestroy method
@@ -59713,9 +62979,9 @@
       x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
       if( x ) x(pVtab);
       sqlite3VtabUnlock(db, pVtab);
     }
-    sqliteFree(db->aVTrans);
+    sqlite3_free(db->aVTrans);
     db->nVTrans = 0;
     db->aVTrans = 0;
   }
 }
@@ -59830,8 +63096,9 @@
 ** new FuncDef structure that is marked as ephemeral using the
 ** SQLITE_FUNC_EPHEM flag.
 */
 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
+  sqlite3 *db,    /* Database connection for reporting malloc problems */
   FuncDef *pDef,  /* Function to possibly overload */
   int nArg,       /* Number of arguments to the function */
   Expr *pExpr     /* First argument to the function */
 ){
@@ -59860,21 +63127,23 @@
 
   /* Call the xFuncFunction method on the virtual table implementation
   ** to see if the implementation wants to overload this function
   */
-  zLowerName = sqlite3StrDup(pDef->zName);
-  for(z=(unsigned char*)zLowerName; *z; z++){
-    *z = sqlite3UpperToLower[*z];
-  }
-  rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
-  sqliteFree(zLowerName);
+  zLowerName = sqlite3DbStrDup(db, pDef->zName);
+  if( zLowerName ){
+    for(z=(unsigned char*)zLowerName; *z; z++){
+      *z = sqlite3UpperToLower[*z];
+    }
+    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
+    sqlite3_free(zLowerName);
+  }
   if( rc==0 ){
     return pDef;
   }
 
   /* Create a new ephemeral function definition for the overloaded
   ** function */
-  pNew = sqliteMalloc( sizeof(*pNew) + strlen(pDef->zName) );
+  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
   if( pNew==0 ){
     return pDef;
   }
   *pNew = *pDef;
@@ -59906,9 +63175,9 @@
 ** rows.  Indices are selected and used to speed the search when doing
 ** so is applicable.  Because this module is responsible for selecting
 ** indices, you might also think of this module as the "query optimizer".
 **
-** $Id: where.c,v 1.253 2007/06/11 12:56:15 drh Exp $
+** $Id: where.c,v 1.261 2007/09/13 17:54:40 drh Exp $
 */
 
 /*
 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
@@ -59918,9 +63187,9 @@
 /*
 ** Trace output macros
 */
 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
-int sqlite3_where_trace = 0;
+SQLITE_API int sqlite3_where_trace = 0;
 # define WHERETRACE(X)  if(sqlite3_where_trace) sqlite3DebugPrintf X
 #else
 # define WHERETRACE(X)
 #endif
@@ -60096,9 +63365,9 @@
       sqlite3ExprDelete(a->pExpr);
     }
   }
   if( pWC->a!=pWC->aStatic ){
-    sqliteFree(pWC->a);
+    sqlite3_free(pWC->a);
   }
 }
 
 /*
@@ -60117,18 +63386,19 @@
   WhereTerm *pTerm;
   int idx;
   if( pWC->nTerm>=pWC->nSlot ){
     WhereTerm *pOld = pWC->a;
-    pWC->a = sqliteMalloc( sizeof(pWC->a[0])*pWC->nSlot*2 );
+    pWC->a = sqlite3_malloc( sizeof(pWC->a[0])*pWC->nSlot*2 );
     if( pWC->a==0 ){
+      pWC->pParse->db->mallocFailed = 1;
       if( flags & TERM_DYNAMIC ){
         sqlite3ExprDelete(p);
       }
       return 0;
     }
     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
     if( pOld!=pWC->aStatic ){
-      sqliteFree(pOld);
+      sqlite3_free(pOld);
     }
     pWC->nSlot *= 2;
   }
   pTerm = &pWC->a[idx = pWC->nTerm];
@@ -60238,17 +63508,16 @@
   }
   return mask;
 }
 static Bitmask exprSelectTableUsage(ExprMaskSet *pMaskSet, Select *pS){
-  Bitmask mask;
-  if( pS==0 ){
-    mask = 0;
-  }else{
-    mask = exprListTableUsage(pMaskSet, pS->pEList);
+  Bitmask mask = 0;
+  while( pS ){
+    mask |= exprListTableUsage(pMaskSet, pS->pEList);
     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
     mask |= exprTableUsage(pMaskSet, pS->pWhere);
     mask |= exprTableUsage(pMaskSet, pS->pHaving);
+    pS = pS->pPrior;
   }
   return mask;
 }
 
@@ -60272,12 +63541,24 @@
 
 /*
 ** Commute a comparision operator.  Expressions of the form "X op Y"
 ** are converted into "Y op X".
+**
+** If a collation sequence is associated with either the left or right
+** side of the comparison, it remains associated with the same side after
+** the commutation. So "Y collate NOCASE op X" becomes
+** "X collate NOCASE op Y". This is because any collation sequence on
+** the left hand side of a comparison overrides any collation sequence
+** attached to the right. For the same reason the EP_ExpCollate flag
+** is not commuted.
 */
 static void exprCommute(Expr *pExpr){
+  u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
+  u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
   SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
+  pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
+  pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
   if( pExpr->op>=TK_GT ){
     assert( TK_LT==TK_GT+2 );
     assert( TK_GE==TK_LE+2 );
@@ -60427,9 +63708,9 @@
   if( (pColl->type!=SQLITE_COLL_BINARY || noCase) &&
       (pColl->type!=SQLITE_COLL_NOCASE || !noCase) ){
     return 0;
   }
-  sqlite3DequoteExpr(pRight);
+  sqlite3DequoteExpr(db, pRight);
   z = (char *)pRight->token.z;
   for(cnt=0; (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2]; cnt++){}
   if( cnt==0 || 255==(u8)z[cnt] ){
     return 0;
@@ -60592,10 +63873,12 @@
   Bitmask prereqAll;
   int nPattern;
   int isComplete;
   int op;
-
-  if( sqlite3MallocFailed() ) return;
+  Parse *pParse = pWC->pParse;
+  sqlite3 *db = pParse->db;
+
+  if( db->mallocFailed ) return;
   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
   op = pExpr->op;
   if( op==TK_IN ){
     assert( pExpr->pRight==0 );
@@ -60626,10 +63909,10 @@
       WhereTerm *pNew;
       Expr *pDup;
       if( pTerm->leftCursor>=0 ){
         int idxNew;
-        pDup = sqlite3ExprDup(pExpr);
-        if( sqlite3MallocFailed() ){
+        pDup = sqlite3ExprDup(db, pExpr);
+        if( db->mallocFailed ){
           sqlite3ExprDelete(pDup);
           return;
         }
         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
@@ -60665,10 +63948,10 @@
     assert( pList->nExpr==2 );
     for(i=0; i<2; i++){
       Expr *pNewExpr;
       int idxNew;
-      pNewExpr = sqlite3Expr(ops[i], sqlite3ExprDup(pExpr->pLeft),
-                             sqlite3ExprDup(pList->a[i].pExpr), 0);
+      pNewExpr = sqlite3Expr(db, ops[i], sqlite3ExprDup(db, pExpr->pLeft),
+                             sqlite3ExprDup(db, pList->a[i].pExpr), 0);
       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
       exprAnalyze(pSrc, pWC, idxNew);
       pTerm = &pWC->a[idxTerm];
       pWC->a[idxNew].iParent = idxTerm;
@@ -60726,15 +64009,15 @@
       Expr *pNew, *pDup;
       Expr *pLeft = 0;
       for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
         if( (pOrTerm->flags & TERM_OR_OK)==0 ) continue;
-        pDup = sqlite3ExprDup(pOrTerm->pExpr->pRight);
-        pList = sqlite3ExprListAppend(pList, pDup, 0);
+        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight);
+        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup, 0);
         pLeft = pOrTerm->pExpr->pLeft;
       }
       assert( pLeft!=0 );
-      pDup = sqlite3ExprDup(pLeft);
-      pNew = sqlite3Expr(TK_IN, pDup, 0, 0);
+      pDup = sqlite3ExprDup(db, pLeft);
+      pNew = sqlite3Expr(db, TK_IN, pDup, 0, 0);
       if( pNew ){
         int idxNew;
         transferJoinMarkings(pNew, pExpr);
         pNew->pList = pList;
@@ -60755,31 +64038,31 @@
 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
   /* Add constraints to reduce the search space on a LIKE or GLOB
   ** operator.
   */
-  if( isLikeOrGlob(pWC->pParse->db, pExpr, &nPattern, &isComplete) ){
+  if( isLikeOrGlob(db, pExpr, &nPattern, &isComplete) ){
     Expr *pLeft, *pRight;
     Expr *pStr1, *pStr2;
     Expr *pNewExpr1, *pNewExpr2;
     int idxNew1, idxNew2;
 
     pLeft = pExpr->pList->a[1].pExpr;
     pRight = pExpr->pList->a[0].pExpr;
-    pStr1 = sqlite3Expr(TK_STRING, 0, 0, 0);
+    pStr1 = sqlite3PExpr(pParse, TK_STRING, 0, 0, 0);
     if( pStr1 ){
-      sqlite3TokenCopy(&pStr1->token, &pRight->token);
+      sqlite3TokenCopy(db, &pStr1->token, &pRight->token);
       pStr1->token.n = nPattern;
       pStr1->flags = EP_Dequoted;
     }
-    pStr2 = sqlite3ExprDup(pStr1);
+    pStr2 = sqlite3ExprDup(db, pStr1);
     if( pStr2 ){
       assert( pStr2->token.dyn );
       ++*(u8*)&pStr2->token.z[nPattern-1];
     }
-    pNewExpr1 = sqlite3Expr(TK_GE, sqlite3ExprDup(pLeft), pStr1, 0);
+    pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft), pStr1, 0);
     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
     exprAnalyze(pSrc, pWC, idxNew1);
-    pNewExpr2 = sqlite3Expr(TK_LT, sqlite3ExprDup(pLeft), pStr2, 0);
+    pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft), pStr2, 0);
     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
     exprAnalyze(pSrc, pWC, idxNew2);
     pTerm = &pWC->a[idxTerm];
     if( isComplete ){
@@ -60808,9 +64091,9 @@
     prereqExpr = exprTableUsage(pMaskSet, pRight);
     prereqColumn = exprTableUsage(pMaskSet, pLeft);
     if( (prereqExpr & prereqColumn)==0 ){
       Expr *pNewExpr;
-      pNewExpr = sqlite3Expr(TK_MATCH, 0, sqlite3ExprDup(pRight), 0);
+      pNewExpr = sqlite3Expr(db, TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0);
       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
       pNewTerm = &pWC->a[idxNew];
       pNewTerm->prereqRight = prereqExpr;
       pNewTerm->leftCursor = pLeft->iTable;
@@ -61134,9 +64417,9 @@
     }
 
     /* Allocate the sqlite3_index_info structure
     */
-    pIdxInfo = sqliteMalloc( sizeof(*pIdxInfo)
+    pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
                              + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
                              + sizeof(*pIdxOrderBy)*nOrderBy );
     if( pIdxInfo==0 ){
       sqlite3ErrorMsg(pParse, "out of memory");
@@ -61252,9 +64535,9 @@
   rc = pTab->pVtab->pModule->xBestIndex(pTab->pVtab, pIdxInfo);
   TRACE_IDX_OUTPUTS(pIdxInfo);
   if( rc!=SQLITE_OK ){
     if( rc==SQLITE_NOMEM ){
-      sqlite3FailedMalloc();
+      pParse->db->mallocFailed = 1;
     }else {
       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
     }
     sqlite3SafetyOn(pParse->db);
@@ -61440,9 +64723,9 @@
     if( pProbe->onError!=OE_None && (flags & WHERE_COLUMN_IN)==0
          && nEq==pProbe->nColumn ){
       flags |= WHERE_UNIQUE;
     }
-    WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n", nEq, inMultiplier, cost));
+    WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n",nEq,inMultiplier,cost));
 
     /* Look for range constraints
     */
     if( nEq<pProbe->nColumn ){
@@ -61501,12 +64784,11 @@
     }
 
     /* If this index has achieved the lowest cost so far, then use it.
     */
-    if( cost < lowestCost ){
+    if( flags && cost < lowestCost ){
       bestIdx = pProbe;
       lowestCost = cost;
-      assert( flags!=0 );
       bestFlags = flags;
       bestNEq = nEq;
     }
   }
@@ -61613,9 +64895,9 @@
     if( pLevel->nIn==0 ){
       pLevel->nxt = sqlite3VdbeMakeLabel(v);
     }
     pLevel->nIn++;
-    pLevel->aInLoop = sqliteReallocOrFree(pLevel->aInLoop,
+    pLevel->aInLoop = sqlite3DbReallocOrFree(pParse->db, pLevel->aInLoop,
                                     sizeof(pLevel->aInLoop[0])*pLevel->nIn);
     pIn = pLevel->aInLoop;
     if( pIn ){
       pIn += pLevel->nIn - 1;
@@ -61710,9 +64992,9 @@
 ** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
 ** overwrites the previous.  This information is used for testing and
 ** analysis only.
 */
-char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
+SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
 static int nQPlan = 0;              /* Next free slow in _query_plan[] */
 
 #endif /* SQLITE_TEST */
 
@@ -61732,12 +65014,12 @@
           ** to the vdbe layer for deletion.
           */
           sqlite3_free(pInfo->idxStr);
         }
-        sqliteFree(pInfo);
-      }
-    }
-    sqliteFree(pWInfo);
+        sqlite3_free(pInfo);
+      }
+    }
+    sqlite3_free(pWInfo);
   }
 }
 
 
@@ -61846,8 +65128,9 @@
   struct SrcList_item *pTabItem;  /* A single entry from pTabList */
   WhereLevel *pLevel;             /* A single level in the pWInfo list */
   int iFrom;                      /* First unused FROM clause element */
   int andFlags;              /* AND-ed combination of all wc.a[].flags */
+  sqlite3 *db;               /* Database connection */
 
   /* The number of tables in the FROM clause is limited by the number of
   ** bits in a Bitmask
   */
@@ -61865,10 +65148,12 @@
 
   /* Allocate and initialize the WhereInfo structure that will become the
   ** return value.
   */
-  pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
-  if( sqlite3MallocFailed() ){
+  db = pParse->db;
+  pWInfo = sqlite3DbMallocZero(db,
+                      sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
+  if( db->mallocFailed ){
     goto whereBeginNoMem;
   }
   pWInfo->nLevel = pTabList->nSrc;
   pWInfo->pParse = pParse;
@@ -61891,9 +65176,9 @@
   for(i=0; i<pTabList->nSrc; i++){
     createMask(&maskSet, pTabList->a[i].iCursor);
   }
   exprAnalyzeAll(pTabList, &wc);
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     goto whereBeginNoMem;
   }
 
   /* Chose the best index to use for each table in the FROM clause.
@@ -62023,26 +65308,26 @@
 #ifndef SQLITE_OMIT_EXPLAIN
     if( pParse->explain==2 ){
       char *zMsg;
       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
-      zMsg = sqlite3MPrintf("TABLE %s", pItem->zName);
+      zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName);
       if( pItem->zAlias ){
-        zMsg = sqlite3MPrintf("%z AS %s", zMsg, pItem->zAlias);
+        zMsg = sqlite3MPrintf(db, "%z AS %s", zMsg, pItem->zAlias);
       }
       if( (pIx = pLevel->pIdx)!=0 ){
-        zMsg = sqlite3MPrintf("%z WITH INDEX %s", zMsg, pIx->zName);
+        zMsg = sqlite3MPrintf(db, "%z WITH INDEX %s", zMsg, pIx->zName);
       }else if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
-        zMsg = sqlite3MPrintf("%z USING PRIMARY KEY", zMsg);
+        zMsg = sqlite3MPrintf(db, "%z USING PRIMARY KEY", zMsg);
       }
 #ifndef SQLITE_OMIT_VIRTUALTABLE
       else if( pLevel->pBestIdx ){
         sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
-        zMsg = sqlite3MPrintf("%z VIRTUAL TABLE INDEX %d:%s", zMsg,
+        zMsg = sqlite3MPrintf(db, "%z VIRTUAL TABLE INDEX %d:%s", zMsg,
                     pBestIdx->idxNum, pBestIdx->idxStr);
       }
 #endif
       if( pLevel->flags & WHERE_ORDERBY ){
-        zMsg = sqlite3MPrintf("%z ORDER BY", zMsg);
+        zMsg = sqlite3MPrintf(db, "%z ORDER BY", zMsg);
       }
       sqlite3VdbeOp3(v, OP_Explain, i, pLevel->iFrom, zMsg, P3_DYNAMIC);
     }
 #endif /* SQLITE_OMIT_EXPLAIN */
@@ -62568,9 +65853,9 @@
         sqlite3VdbeJumpHere(v, pIn->topAddr+1);
         sqlite3VdbeAddOp(v, OP_Next, pIn->iCur, pIn->topAddr);
         sqlite3VdbeJumpHere(v, pIn->topAddr-1);
       }
-      sqliteFree(pLevel->aInLoop);
+      sqlite3_free(pLevel->aInLoop);
     }
     sqlite3VdbeResolveLabel(v, pLevel->brk);
     if( pLevel->iLeftJoin ){
       int addr;
@@ -64703,15 +67988,15 @@
 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy172);}
         break;
       case 53:
 {
-  Expr *p = sqlite3Expr(TK_UMINUS, yymsp[0].minor.yy172, 0, 0);
+  Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy172, 0, 0);
   sqlite3AddDefaultValue(pParse,p);
 }
         break;
       case 54:
 {
-  Expr *p = sqlite3Expr(TK_STRING, 0, 0, &yymsp[0].minor.yy410);
+  Expr *p = sqlite3PExpr(pParse, TK_STRING, 0, 0, &yymsp[0].minor.yy410);
   sqlite3AddDefaultValue(pParse,p);
 }
         break;
       case 56:
@@ -64846,9 +68131,9 @@
 {yygotominor.yy46 = TK_ALL;}
         break;
       case 110:
 {
-  yygotominor.yy219 = sqlite3SelectNew(yymsp[-6].minor.yy174,yymsp[-5].minor.yy373,yymsp[-4].minor.yy172,yymsp[-3].minor.yy174,yymsp[-2].minor.yy172,yymsp[-1].minor.yy174,yymsp[-7].minor.yy46,yymsp[0].minor.yy234.pLimit,yymsp[0].minor.yy234.pOffset);
+  yygotominor.yy219 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy174,yymsp[-5].minor.yy373,yymsp[-4].minor.yy172,yymsp[-3].minor.yy174,yymsp[-2].minor.yy172,yymsp[-1].minor.yy174,yymsp[-7].minor.yy46,yymsp[0].minor.yy234.pLimit,yymsp[0].minor.yy234.pOffset);
 }
         break;
       case 114:
       case 237:
@@ -64862,28 +68147,30 @@
 {yygotominor.yy174 = 0;}
         break;
       case 116:
 {
-   yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-2].minor.yy174,yymsp[-1].minor.yy172,yymsp[0].minor.yy410.n?&yymsp[0].minor.yy410:0);
+   yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy174,yymsp[-1].minor.yy172,yymsp[0].minor.yy410.n?&yymsp[0].minor.yy410:0);
 }
         break;
       case 117:
 {
-  yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-1].minor.yy174, sqlite3Expr(TK_ALL, 0, 0, 0), 0);
+  Expr *p = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy174, p, 0);
 }
         break;
       case 118:
 {
-  Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0);
-  Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &yymsp[-2].minor.yy410);
-  yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-3].minor.yy174, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0);
+  Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0);
+  Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410);
+  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy174, pDot, 0);
 }
         break;
       case 121:
 {yygotominor.yy410.n = 0;}
         break;
       case 122:
-{yygotominor.yy373 = sqliteMalloc(sizeof(*yygotominor.yy373));}
+{yygotominor.yy373 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy373));}
         break;
       case 123:
 {
   yygotominor.yy373 = yymsp[0].minor.yy373;
@@ -64900,27 +68187,27 @@
 {yygotominor.yy373 = 0;}
         break;
       case 126:
 {
-  yygotominor.yy373 = sqlite3SrcListAppendFromTerm(yymsp[-5].minor.yy373,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,0,yymsp[-1].minor.yy172,yymsp[0].minor.yy432);
+  yygotominor.yy373 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy373,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,0,yymsp[-1].minor.yy172,yymsp[0].minor.yy432);
 }
         break;
       case 127:
 {
-    yygotominor.yy373 = sqlite3SrcListAppendFromTerm(yymsp[-6].minor.yy373,0,0,&yymsp[-2].minor.yy410,yymsp[-4].minor.yy219,yymsp[-1].minor.yy172,yymsp[0].minor.yy432);
+    yygotominor.yy373 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy373,0,0,&yymsp[-2].minor.yy410,yymsp[-4].minor.yy219,yymsp[-1].minor.yy172,yymsp[0].minor.yy432);
   }
         break;
       case 129:
 {
      sqlite3SrcListShiftJoinType(yymsp[0].minor.yy373);
-     yygotominor.yy219 = sqlite3SelectNew(0,yymsp[0].minor.yy373,0,0,0,0,0,0,0);
+     yygotominor.yy219 = sqlite3SelectNew(pParse,0,yymsp[0].minor.yy373,0,0,0,0,0,0,0);
   }
         break;
       case 130:
 {yygotominor.yy410.z=0; yygotominor.yy410.n=0;}
         break;
       case 132:
-{yygotominor.yy373 = sqlite3SrcListAppend(0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);}
+{yygotominor.yy373 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);}
         break;
       case 133:
 { yygotominor.yy46 = JT_INNER; }
         break;
@@ -64965,15 +68252,15 @@
 {yygotominor.yy174 = yymsp[0].minor.yy174;}
         break;
       case 143:
 {
-  yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-3].minor.yy174,yymsp[-1].minor.yy172,0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy174,yymsp[-1].minor.yy172,0);
   if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46;
 }
         break;
       case 144:
 {
-  yygotominor.yy174 = sqlite3ExprListAppend(0,yymsp[-1].minor.yy172,0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy172,0);
   if( yygotominor.yy174 && yygotominor.yy174->a ) yygotominor.yy174->a[0].sortOrder = yymsp[0].minor.yy46;
 }
         break;
       case 146:
@@ -65004,12 +68291,12 @@
   sqlite3Update(pParse,yymsp[-3].minor.yy373,yymsp[-1].minor.yy174,yymsp[0].minor.yy172,yymsp[-4].minor.yy46);
 }
         break;
       case 161:
-{yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-4].minor.yy174,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);}
+{yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);}
         break;
       case 162:
-{yygotominor.yy174 = sqlite3ExprListAppend(0,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);}
+{yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);}
         break;
       case 163:
 {sqlite3Insert(pParse, yymsp[-5].minor.yy373, yymsp[-1].minor.yy174, 0, yymsp[-4].minor.yy432, yymsp[-7].minor.yy46);}
         break;
@@ -65020,46 +68307,46 @@
 {sqlite3Insert(pParse, yymsp[-3].minor.yy373, 0, 0, yymsp[-2].minor.yy432, yymsp[-5].minor.yy46);}
         break;
       case 168:
       case 231:
-{yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-2].minor.yy174,yymsp[0].minor.yy172,0);}
+{yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy174,yymsp[0].minor.yy172,0);}
         break;
       case 169:
       case 232:
-{yygotominor.yy174 = sqlite3ExprListAppend(0,yymsp[0].minor.yy172,0);}
+{yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy172,0);}
         break;
       case 172:
-{yygotominor.yy432 = sqlite3IdListAppend(yymsp[-2].minor.yy432,&yymsp[0].minor.yy410);}
+{yygotominor.yy432 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy432,&yymsp[0].minor.yy410);}
         break;
       case 173:
-{yygotominor.yy432 = sqlite3IdListAppend(0,&yymsp[0].minor.yy410);}
+{yygotominor.yy432 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy410);}
         break;
       case 175:
 {yygotominor.yy172 = yymsp[-1].minor.yy172; sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); }
         break;
       case 176:
       case 181:
       case 182:
-{yygotominor.yy172 = sqlite3Expr(yymsp[0].major, 0, 0, &yymsp[0].minor.yy0);}
+{yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[0].major, 0, 0, &yymsp[0].minor.yy0);}
         break;
       case 177:
       case 178:
-{yygotominor.yy172 = sqlite3Expr(TK_ID, 0, 0, &yymsp[0].minor.yy0);}
+{yygotominor.yy172 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);}
         break;
       case 179:
 {
-  Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &yymsp[-2].minor.yy410);
-  Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &yymsp[0].minor.yy410);
-  yygotominor.yy172 = sqlite3Expr(TK_DOT, temp1, temp2, 0);
+  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410);
+  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy410);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
 }
         break;
       case 180:
 {
-  Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &yymsp[-4].minor.yy410);
-  Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &yymsp[-2].minor.yy410);
-  Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &yymsp[0].minor.yy410);
-  Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
-  yygotominor.yy172 = sqlite3Expr(TK_DOT, temp1, temp4, 0);
+  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy410);
+  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410);
+  Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy410);
+  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
 }
         break;
       case 183:
 {yygotominor.yy172 = sqlite3RegisterExpr(pParse, &yymsp[0].minor.yy0);}
@@ -65066,9 +68353,9 @@
         break;
       case 184:
 {
   Token *pToken = &yymsp[0].minor.yy0;
-  Expr *pExpr = yygotominor.yy172 = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
+  Expr *pExpr = yygotominor.yy172 = sqlite3PExpr(pParse, TK_VARIABLE, 0, 0, pToken);
   sqlite3ExprAssignVarNumber(pParse, pExpr);
 }
         break;
       case 185:
@@ -65077,9 +68364,9 @@
 }
         break;
       case 186:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_CAST, yymsp[-3].minor.yy172, 0, &yymsp[-1].minor.yy410);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy172, 0, &yymsp[-1].minor.yy410);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
 }
         break;
       case 187:
@@ -65086,9 +68373,9 @@
 {
   if( yymsp[-1].minor.yy174 && yymsp[-1].minor.yy174->nExpr>SQLITE_MAX_FUNCTION_ARG ){
     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
   }
-  yygotominor.yy172 = sqlite3ExprFunction(yymsp[-1].minor.yy174, &yymsp[-4].minor.yy0);
+  yygotominor.yy172 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy174, &yymsp[-4].minor.yy0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
   if( yymsp[-2].minor.yy46 && yygotominor.yy172 ){
     yygotominor.yy172->flags |= EP_Distinct;
   }
@@ -65095,17 +68382,17 @@
 }
         break;
       case 188:
 {
-  yygotominor.yy172 = sqlite3ExprFunction(0, &yymsp[-3].minor.yy0);
+  yygotominor.yy172 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
 }
         break;
       case 189:
 {
   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
   ** treated as functions that return constants */
-  yygotominor.yy172 = sqlite3ExprFunction(0,&yymsp[0].minor.yy0);
+  yygotominor.yy172 = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
   if( yygotominor.yy172 ){
     yygotominor.yy172->op = TK_CONST_FUNC;
     yygotominor.yy172->span = yymsp[0].minor.yy0;
   }
@@ -65118,9 +68405,9 @@
       case 194:
       case 195:
       case 196:
       case 197:
-{yygotominor.yy172 = sqlite3Expr(yymsp[-1].major, yymsp[-2].minor.yy172, yymsp[0].minor.yy172, 0);}
+{yygotominor.yy172 = sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy172,yymsp[0].minor.yy172,0);}
         break;
       case 198:
       case 200:
 {yygotominor.yy72.eOperator = yymsp[0].minor.yy0; yygotominor.yy72.not = 0;}
@@ -65131,91 +68418,91 @@
         break;
       case 204:
 {
   ExprList *pList;
-  pList = sqlite3ExprListAppend(0, yymsp[-1].minor.yy172, 0);
-  pList = sqlite3ExprListAppend(pList, yymsp[-3].minor.yy172, 0);
+  pList = sqlite3ExprListAppend(pParse,0, yymsp[-1].minor.yy172, 0);
+  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-3].minor.yy172, 0);
   if( yymsp[0].minor.yy172 ){
-    pList = sqlite3ExprListAppend(pList, yymsp[0].minor.yy172, 0);
-  }
-  yygotominor.yy172 = sqlite3ExprFunction(pList, &yymsp[-2].minor.yy72.eOperator);
-  if( yymsp[-2].minor.yy72.not ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0);
+    pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy172, 0);
+  }
+  yygotominor.yy172 = sqlite3ExprFunction(pParse, pList, &yymsp[-2].minor.yy72.eOperator);
+  if( yymsp[-2].minor.yy72.not ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172, &yymsp[-3].minor.yy172->span, &yymsp[-1].minor.yy172->span);
   if( yygotominor.yy172 ) yygotominor.yy172->flags |= EP_InfixFunc;
 }
         break;
       case 205:
 {
-  yygotominor.yy172 = sqlite3Expr(yymsp[0].major, yymsp[-1].minor.yy172, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[0].major, yymsp[-1].minor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy172->span,&yymsp[0].minor.yy0);
 }
         break;
       case 206:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_ISNULL, yymsp[-2].minor.yy172, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_ISNULL, yymsp[-2].minor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0);
 }
         break;
       case 207:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_NOTNULL, yymsp[-2].minor.yy172, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOTNULL, yymsp[-2].minor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0);
 }
         break;
       case 208:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_NOTNULL, yymsp[-3].minor.yy172, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOTNULL, yymsp[-3].minor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,&yymsp[0].minor.yy0);
 }
         break;
       case 209:
 {
-  yygotominor.yy172 = sqlite3Expr(yymsp[-1].major, yymsp[0].minor.yy172, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span);
 }
         break;
       case 210:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_UMINUS, yymsp[0].minor.yy172, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span);
 }
         break;
       case 211:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_UPLUS, yymsp[0].minor.yy172, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_UPLUS, yymsp[0].minor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span);
 }
         break;
       case 214:
 {
-  ExprList *pList = sqlite3ExprListAppend(0, yymsp[-2].minor.yy172, 0);
-  pList = sqlite3ExprListAppend(pList, yymsp[0].minor.yy172, 0);
-  yygotominor.yy172 = sqlite3Expr(TK_BETWEEN, yymsp[-4].minor.yy172, 0, 0);
+  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy172, 0);
+  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy172, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy172, 0, 0);
   if( yygotominor.yy172 ){
     yygotominor.yy172->pList = pList;
   }else{
     sqlite3ExprListDelete(pList);
   }
-  if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0);
+  if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
   sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy172->span);
 }
         break;
       case 217:
 {
-    yygotominor.yy172 = sqlite3Expr(TK_IN, yymsp[-4].minor.yy172, 0, 0);
+    yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy172, 0, 0);
     if( yygotominor.yy172 ){
       yygotominor.yy172->pList = yymsp[-1].minor.yy174;
       sqlite3ExprSetHeight(yygotominor.yy172);
     }else{
       sqlite3ExprListDelete(yymsp[-1].minor.yy174);
     }
-    if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0);
+    if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
     sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0);
   }
         break;
       case 218:
 {
-    yygotominor.yy172 = sqlite3Expr(TK_SELECT, 0, 0, 0);
+    yygotominor.yy172 = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
     if( yygotominor.yy172 ){
       yygotominor.yy172->pSelect = yymsp[-1].minor.yy219;
       sqlite3ExprSetHeight(yygotominor.yy172);
     }else{
@@ -65225,36 +68512,36 @@
   }
         break;
       case 219:
 {
-    yygotominor.yy172 = sqlite3Expr(TK_IN, yymsp[-4].minor.yy172, 0, 0);
+    yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy172, 0, 0);
     if( yygotominor.yy172 ){
       yygotominor.yy172->pSelect = yymsp[-1].minor.yy219;
       sqlite3ExprSetHeight(yygotominor.yy172);
     }else{
       sqlite3SelectDelete(yymsp[-1].minor.yy219);
     }
-    if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0);
+    if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
     sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0);
   }
         break;
       case 220:
 {
-    SrcList *pSrc = sqlite3SrcListAppend(0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);
-    yygotominor.yy172 = sqlite3Expr(TK_IN, yymsp[-3].minor.yy172, 0, 0);
+    SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);
+    yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy172, 0, 0);
     if( yygotominor.yy172 ){
-      yygotominor.yy172->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0);
+      yygotominor.yy172->pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
       sqlite3ExprSetHeight(yygotominor.yy172);
     }else{
       sqlite3SrcListDelete(pSrc);
     }
-    if( yymsp[-2].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0);
+    if( yymsp[-2].minor.yy46 ) yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy172, 0, 0);
     sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,yymsp[0].minor.yy410.z?&yymsp[0].minor.yy410:&yymsp[-1].minor.yy410);
   }
         break;
       case 221:
 {
-    Expr *p = yygotominor.yy172 = sqlite3Expr(TK_EXISTS, 0, 0, 0);
+    Expr *p = yygotominor.yy172 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
     if( p ){
       p->pSelect = yymsp[-1].minor.yy219;
       sqlite3ExprSpan(p,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
       sqlite3ExprSetHeight(yygotominor.yy172);
@@ -65264,9 +68551,9 @@
   }
         break;
       case 222:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_CASE, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, 0);
   if( yygotominor.yy172 ){
     yygotominor.yy172->pList = yymsp[-2].minor.yy174;
     sqlite3ExprSetHeight(yygotominor.yy172);
   }else{
@@ -65276,21 +68563,22 @@
 }
         break;
       case 223:
 {
-  yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-4].minor.yy174, yymsp[-2].minor.yy172, 0);
-  yygotominor.yy174 = sqlite3ExprListAppend(yygotominor.yy174, yymsp[0].minor.yy172, 0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174, yymsp[-2].minor.yy172, 0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,yygotominor.yy174, yymsp[0].minor.yy172, 0);
 }
         break;
       case 224:
 {
-  yygotominor.yy174 = sqlite3ExprListAppend(0, yymsp[-2].minor.yy172, 0);
-  yygotominor.yy174 = sqlite3ExprListAppend(yygotominor.yy174, yymsp[0].minor.yy172, 0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy172, 0);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,yygotominor.yy174, yymsp[0].minor.yy172, 0);
 }
         break;
       case 233:
 {
-  sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy410, &yymsp[-5].minor.yy410, sqlite3SrcListAppend(0,&yymsp[-3].minor.yy410,0), yymsp[-1].minor.yy174, yymsp[-9].minor.yy46,
+  sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy410, &yymsp[-5].minor.yy410,
+                     sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy410,0), yymsp[-1].minor.yy174, yymsp[-9].minor.yy46,
                       &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy46);
 }
         break;
       case 234:
@@ -65303,12 +68591,12 @@
       case 238:
 {
   Expr *p = 0;
   if( yymsp[-1].minor.yy410.n>0 ){
-    p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
+    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)yymsp[-1].minor.yy410.z, yymsp[-1].minor.yy410.n);
   }
-  yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-4].minor.yy174, p, &yymsp[-2].minor.yy410);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174, p, &yymsp[-2].minor.yy410);
   sqlite3ExprListCheckLength(pParse, yygotominor.yy174, SQLITE_MAX_COLUMN, "index");
   if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46;
 }
         break;
@@ -65315,12 +68603,12 @@
       case 239:
 {
   Expr *p = 0;
   if( yymsp[-1].minor.yy410.n>0 ){
-    p = sqlite3Expr(TK_COLUMN, 0, 0, 0);
+    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
     if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)yymsp[-1].minor.yy410.z, yymsp[-1].minor.yy410.n);
   }
-  yygotominor.yy174 = sqlite3ExprListAppend(0, p, &yymsp[-2].minor.yy410);
+  yygotominor.yy174 = sqlite3ExprListAppend(pParse,0, p, &yymsp[-2].minor.yy410);
   sqlite3ExprListCheckLength(pParse, yygotominor.yy174, SQLITE_MAX_COLUMN, "index");
   if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46;
 }
         break;
@@ -65402,25 +68690,25 @@
       case 272:
 { yygotominor.yy243 = 0; }
         break;
       case 273:
-{ yygotominor.yy243 = sqlite3TriggerUpdateStep(&yymsp[-3].minor.yy410, yymsp[-1].minor.yy174, yymsp[0].minor.yy172, yymsp[-4].minor.yy46); }
+{ yygotominor.yy243 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-3].minor.yy410, yymsp[-1].minor.yy174, yymsp[0].minor.yy172, yymsp[-4].minor.yy46); }
         break;
       case 274:
-{yygotominor.yy243 = sqlite3TriggerInsertStep(&yymsp[-5].minor.yy410, yymsp[-4].minor.yy432, yymsp[-1].minor.yy174, 0, yymsp[-7].minor.yy46);}
+{yygotominor.yy243 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy410, yymsp[-4].minor.yy432, yymsp[-1].minor.yy174, 0, yymsp[-7].minor.yy46);}
         break;
       case 275:
-{yygotominor.yy243 = sqlite3TriggerInsertStep(&yymsp[-2].minor.yy410, yymsp[-1].minor.yy432, 0, yymsp[0].minor.yy219, yymsp[-4].minor.yy46);}
+{yygotominor.yy243 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy410, yymsp[-1].minor.yy432, 0, yymsp[0].minor.yy219, yymsp[-4].minor.yy46);}
         break;
       case 276:
-{yygotominor.yy243 = sqlite3TriggerDeleteStep(&yymsp[-1].minor.yy410, yymsp[0].minor.yy172);}
+{yygotominor.yy243 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-1].minor.yy410, yymsp[0].minor.yy172);}
         break;
       case 277:
-{yygotominor.yy243 = sqlite3TriggerSelectStep(yymsp[0].minor.yy219); }
+{yygotominor.yy243 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy219); }
         break;
       case 278:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_RAISE, 0, 0, 0);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
   if( yygotominor.yy172 ){
     yygotominor.yy172->iColumn = OE_Ignore;
     sqlite3ExprSpan(yygotominor.yy172, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0);
   }
@@ -65427,9 +68715,9 @@
 }
         break;
       case 279:
 {
-  yygotominor.yy172 = sqlite3Expr(TK_RAISE, 0, 0, &yymsp[-1].minor.yy410);
+  yygotominor.yy172 = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy410);
   if( yygotominor.yy172 ) {
     yygotominor.yy172->iColumn = yymsp[-3].minor.yy46;
     sqlite3ExprSpan(yygotominor.yy172, &yymsp[-5].minor.yy0, &yymsp[0].minor.yy0);
   }
@@ -65770,9 +69058,9 @@
 ** This file contains C code that splits an SQL input string up into
 ** individual tokens and sends those tokens one-by-one over to the
 ** parser for analysis.
 **
-** $Id: tokenize.c,v 1.131 2007/07/23 19:31:17 drh Exp $
+** $Id: tokenize.c,v 1.136 2007/08/27 23:26:59 drh Exp $
 */
 
 /*
 ** The charMap() macro maps alphabetic characters into their
@@ -65823,9 +69111,9 @@
 /***** This file contains automatically generated code ******
 **
 ** The code in this file has been automatically generated by
 **
-**     $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.30 2007/05/04 18:30:41 drh Exp $
+**     $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.31 2007/07/30 18:26:20 rse Exp $
 **
 ** The code in this file implements a function that determines whether
 ** or not a given identifier is really an SQL keyword.  The same thing
 ** might be implemented more directly using a hand-written hash table.
@@ -65953,9 +69241,9 @@
 ** SQLite will allow '$' in identifiers for compatibility.
 ** But the feature is undocumented.
 */
 #ifdef SQLITE_ASCII
-const char sqlite3IsIdChar[] = {
+SQLITE_PRIVATE const char sqlite3IsAsciiIdChar[] = {
 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
     0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
@@ -65962,12 +69250,12 @@
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
 };
-#define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20]))
+#define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
 #endif
 #ifdef SQLITE_EBCDIC
-const char sqlite3IsIdChar[] = {
+SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
@@ -65980,9 +69268,9 @@
     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
 };
-#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsIdChar[c-0x40]))
+#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
 #endif
 
 
 /*
@@ -66251,9 +69539,9 @@
 /*
 ** Run the parser on the given SQL string.  The parser structure is
 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
 ** and pzErrMsg!=NULL then an error message might be written into
-** memory obtained from malloc() and *pzErrMsg made to point to that
+** memory obtained from sqlite3_malloc() and *pzErrMsg made to point to that
 ** error message.  Or maybe not.
 */
 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
   int nErr = 0;
@@ -66267,10 +69555,11 @@
     db->u1.isInterrupted = 0;
   }
   pParse->rc = SQLITE_OK;
   i = 0;
-  pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3MallocX);
+  pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3_malloc);
   if( pEngine==0 ){
+    db->mallocFailed = 1;
     return SQLITE_NOMEM;
   }
   assert( pParse->sLastToken.dyn==0 );
   assert( pParse->pNewTable==0 );
@@ -66279,9 +69568,9 @@
   assert( pParse->nVarExpr==0 );
   assert( pParse->nVarExprAlloc==0 );
   assert( pParse->apVarExpr==0 );
   pParse->zTail = pParse->zSql = zSql;
-  while( !sqlite3MallocFailed() && zSql[i]!=0 ){
+  while( !db->mallocFailed && zSql[i]!=0 ){
     assert( i>=0 );
     pParse->sLastToken.z = (u8*)&zSql[i];
     assert( pParse->sLastToken.dyn==0 );
     pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
@@ -66301,10 +69590,10 @@
         break;
       }
       case TK_ILLEGAL: {
         if( pzErrMsg ){
-          sqliteFree(*pzErrMsg);
-          *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
+          sqlite3_free(*pzErrMsg);
+          *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
                           &pParse->sLastToken);
         }
         nErr++;
         goto abort_parse;
@@ -66330,10 +69619,10 @@
       pParse->zTail = &zSql[i];
     }
     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
   }
-  sqlite3ParserFree(pEngine, sqlite3FreeX);
-  if( sqlite3MallocFailed() ){
+  sqlite3ParserFree(pEngine, sqlite3_free);
+  if( db->mallocFailed ){
     pParse->rc = SQLITE_NOMEM;
   }
   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
     sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
@@ -66341,9 +69630,9 @@
   if( pParse->zErrMsg ){
     if( pzErrMsg && *pzErrMsg==0 ){
       *pzErrMsg = pParse->zErrMsg;
     }else{
-      sqliteFree(pParse->zErrMsg);
+      sqlite3_free(pParse->zErrMsg);
     }
     pParse->zErrMsg = 0;
     if( !nErr ) nErr++;
   }
@@ -66352,9 +69641,9 @@
     pParse->pVdbe = 0;
   }
 #ifndef SQLITE_OMIT_SHARED_CACHE
   if( pParse->nested==0 ){
-    sqliteFree(pParse->aTableLock);
+    sqlite3_free(pParse->aTableLock);
     pParse->aTableLock = 0;
     pParse->nTableLock = 0;
   }
 #endif
@@ -66367,16 +69656,289 @@
     sqlite3DeleteTable(pParse->pNewTable);
   }
 
   sqlite3DeleteTrigger(pParse->pNewTrigger);
-  sqliteFree(pParse->apVarExpr);
+  sqlite3_free(pParse->apVarExpr);
   if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
     pParse->rc = SQLITE_ERROR;
   }
   return nErr;
 }
 
 /************** End of tokenize.c ********************************************/
+/************** Begin file complete.c ****************************************/
+/*
+** 2001 September 15
+**
+** The author disclaims copyright to this source code.  In place of
+** a legal notice, here is a blessing:
+**
+**    May you do good and not evil.
+**    May you find forgiveness for yourself and forgive others.
+**    May you share freely, never taking more than you give.
+**
+*************************************************************************
+** An tokenizer for SQL
+**
+** This file contains C code that implements the sqlite3_complete() API.
+** This code used to be part of the tokenizer.c source file.  But by
+** separating it out, the code will be automatically omitted from
+** static links that do not use it.
+**
+** $Id: complete.c,v 1.6 2007/08/27 23:26:59 drh Exp $
+*/
+#ifndef SQLITE_OMIT_COMPLETE
+
+/*
+** This is defined in tokenize.c.  We just have to import the definition.
+*/
+#ifndef SQLITE_AMALGAMATION
+#ifdef SQLITE_ASCII
+SQLITE_PRIVATE const char sqlite3IsAsciiIdChar[];
+#define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
+#endif
+#ifdef SQLITE_EBCDIC
+SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
+#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
+#endif
+#endif /* SQLITE_AMALGAMATION */
+
+
+/*
+** Token types used by the sqlite3_complete() routine.  See the header
+** comments on that procedure for additional information.
+*/
+#define tkSEMI    0
+#define tkWS      1
+#define tkOTHER   2
+#define tkEXPLAIN 3
+#define tkCREATE  4
+#define tkTEMP    5
+#define tkTRIGGER 6
+#define tkEND     7
+
+/*
+** Return TRUE if the given SQL string ends in a semicolon.
+**
+** Special handling is require for CREATE TRIGGER statements.
+** Whenever the CREATE TRIGGER keywords are seen, the statement
+** must end with ";END;".
+**
+** This implementation uses a state machine with 7 states:
+**
+**   (0) START     At the beginning or end of an SQL statement.  This routine
+**                 returns 1 if it ends in the START state and 0 if it ends
+**                 in any other state.
+**
+**   (1) NORMAL    We are in the middle of statement which ends with a single
+**                 semicolon.
+**
+**   (2) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
+**                 a statement.
+**
+**   (3) CREATE    The keyword CREATE has been seen at the beginning of a
+**                 statement, possibly preceeded by EXPLAIN and/or followed by
+**                 TEMP or TEMPORARY
+**
+**   (4) TRIGGER   We are in the middle of a trigger definition that must be
+**                 ended by a semicolon, the keyword END, and another semicolon.
+**
+**   (5) SEMI      We've seen the first semicolon in the ";END;" that occurs at
+**                 the end of a trigger definition.
+**
+**   (6) END       We've seen the ";END" of the ";END;" that occurs at the end
+**                 of a trigger difinition.
+**
+** Transitions between states above are determined by tokens extracted
+** from the input.  The following tokens are significant:
+**
+**   (0) tkSEMI      A semicolon.
+**   (1) tkWS        Whitespace
+**   (2) tkOTHER     Any other SQL token.
+**   (3) tkEXPLAIN   The "explain" keyword.
+**   (4) tkCREATE    The "create" keyword.
+**   (5) tkTEMP      The "temp" or "temporary" keyword.
+**   (6) tkTRIGGER   The "trigger" keyword.
+**   (7) tkEND       The "end" keyword.
+**
+** Whitespace never causes a state transition and is always ignored.
+**
+** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
+** to recognize the end of a trigger can be omitted.  All we have to do
+** is look for a semicolon that is not part of an string or comment.
+*/
+SQLITE_API int sqlite3_complete(const char *zSql){
+  u8 state = 0;   /* Current state, using numbers defined in header comment */
+  u8 token;       /* Value of the next token */
+
+#ifndef SQLITE_OMIT_TRIGGER
+  /* A complex statement machine used to detect the end of a CREATE TRIGGER
+  ** statement.  This is the normal case.
+  */
+  static const u8 trans[7][8] = {
+                     /* Token:                                                */
+     /* State:       **  SEMI  WS  OTHER EXPLAIN  CREATE  TEMP  TRIGGER  END  */
+     /* 0   START: */ {    0,  0,     1,      2,      3,    1,       1,   1,  },
+     /* 1  NORMAL: */ {    0,  1,     1,      1,      1,    1,       1,   1,  },
+     /* 2 EXPLAIN: */ {    0,  2,     1,      1,      3,    1,       1,   1,  },
+     /* 3  CREATE: */ {    0,  3,     1,      1,      1,    3,       4,   1,  },
+     /* 4 TRIGGER: */ {    5,  4,     4,      4,      4,    4,       4,   4,  },
+     /* 5    SEMI: */ {    5,  5,     4,      4,      4,    4,       4,   6,  },
+     /* 6     END: */ {    0,  6,     4,      4,      4,    4,       4,   4,  },
+  };
+#else
+  /* If triggers are not suppored by this compile then the statement machine
+  ** used to detect the end of a statement is much simplier
+  */
+  static const u8 trans[2][3] = {
+                     /* Token:           */
+     /* State:       **  SEMI  WS  OTHER */
+     /* 0   START: */ {    0,  0,     1, },
+     /* 1  NORMAL: */ {    0,  1,     1, },
+  };
+#endif /* SQLITE_OMIT_TRIGGER */
+
+  while( *zSql ){
+    switch( *zSql ){
+      case ';': {  /* A semicolon */
+        token = tkSEMI;
+        break;
+      }
+      case ' ':
+      case '\r':
+      case '\t':
+      case '\n':
+      case '\f': {  /* White space is ignored */
+        token = tkWS;
+        break;
+      }
+      case '/': {   /* C-style comments */
+        if( zSql[1]!='*' ){
+          token = tkOTHER;
+          break;
+        }
+        zSql += 2;
+        while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
+        if( zSql[0]==0 ) return 0;
+        zSql++;
+        token = tkWS;
+        break;
+      }
+      case '-': {   /* SQL-style comments from "--" to end of line */
+        if( zSql[1]!='-' ){
+          token = tkOTHER;
+          break;
+        }
+        while( *zSql && *zSql!='\n' ){ zSql++; }
+        if( *zSql==0 ) return state==0;
+        token = tkWS;
+        break;
+      }
+      case '[': {   /* Microsoft-style identifiers in [...] */
+        zSql++;
+        while( *zSql && *zSql!=']' ){ zSql++; }
+        if( *zSql==0 ) return 0;
+        token = tkOTHER;
+        break;
+      }
+      case '`':     /* Grave-accent quoted symbols used by MySQL */
+      case '"':     /* single- and double-quoted strings */
+      case '\'': {
+        int c = *zSql;
+        zSql++;
+        while( *zSql && *zSql!=c ){ zSql++; }
+        if( *zSql==0 ) return 0;
+        token = tkOTHER;
+        break;
+      }
+      default: {
+        int c;
+        if( IdChar((u8)*zSql) ){
+          /* Keywords and unquoted identifiers */
+          int nId;
+          for(nId=1; IdChar(zSql[nId]); nId++){}
+#ifdef SQLITE_OMIT_TRIGGER
+          token = tkOTHER;
+#else
+          switch( *zSql ){
+            case 'c': case 'C': {
+              if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
+                token = tkCREATE;
+              }else{
+                token = tkOTHER;
+              }
+              break;
+            }
+            case 't': case 'T': {
+              if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
+                token = tkTRIGGER;
+              }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
+                token = tkTEMP;
+              }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
+                token = tkTEMP;
+              }else{
+                token = tkOTHER;
+              }
+              break;
+            }
+            case 'e':  case 'E': {
+              if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
+                token = tkEND;
+              }else
+#ifndef SQLITE_OMIT_EXPLAIN
+              if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
+                token = tkEXPLAIN;
+              }else
+#endif
+              {
+                token = tkOTHER;
+              }
+              break;
+            }
+            default: {
+              token = tkOTHER;
+              break;
+            }
+          }
+#endif /* SQLITE_OMIT_TRIGGER */
+          zSql += nId-1;
+        }else{
+          /* Operators and special symbols */
+          token = tkOTHER;
+        }
+        break;
+      }
+    }
+    state = trans[state][token];
+    zSql++;
+  }
+  return state==0;
+}
+
+#ifndef SQLITE_OMIT_UTF16
+/*
+** This routine is the same as the sqlite3_complete() routine described
+** above, except that the parameter is required to be UTF-16 encoded, not
+** UTF-8.
+*/
+SQLITE_API int sqlite3_complete16(const void *zSql){
+  sqlite3_value *pVal;
+  char const *zSql8;
+  int rc = SQLITE_NOMEM;
+
+  pVal = sqlite3ValueNew(0);
+  sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
+  zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
+  if( zSql8 ){
+    rc = sqlite3_complete(zSql8);
+  }
+  sqlite3ValueFree(pVal);
+  return sqlite3ApiExit(0, rc);
+}
+#endif /* SQLITE_OMIT_UTF16 */
+#endif /* SQLITE_OMIT_COMPLETE */
+
+/************** End of complete.c ********************************************/
 /************** Begin file main.c ********************************************/
 /*
 ** 2001 September 15
 **
@@ -66392,25 +69954,26 @@
 ** implement the programmer interface to the library.  Routines in
 ** other files are for internal use by SQLite and should not be
 ** accessed by users of the library.
 **
-** $Id: main.c,v 1.377 2007/06/22 15:21:16 danielk1977 Exp $
+** $Id: main.c,v 1.404 2007/09/03 15:19:35 drh Exp $
 */
 
 /*
 ** The version of the library
 */
-const char sqlite3_version[] = SQLITE_VERSION;
+SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
-int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
+SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
+SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
 
 /*
 ** If the following function pointer is not NULL and if
 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
 ** I/O active are written using this function.  These messages
 ** are intended for debugging activity only.
 */
-void (*sqlite3_io_trace)(const char*, ...) = 0;
+SQLITE_API void (*sqlite3_io_trace)(const char*, ...) = 0;
 
 /*
 ** If the following global variable points to a string which is the
 ** name of a directory, then that directory will be used to store
@@ -66417,9 +69980,9 @@
 ** temporary files.
 **
 ** See also the "PRAGMA temp_store_directory" SQL command.
 */
-char *sqlite3_temp_directory = 0;
+SQLITE_API char *sqlite3_temp_directory = 0;
 
 
 /*
 ** This is the default collating function named "BINARY" which is always
@@ -66463,9 +70026,9 @@
 
 /*
 ** Return the ROWID of the most recent insert
 */
-sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
+SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
   return db->lastRowid;
 }
 
 /*
@@ -66477,9 +70040,9 @@
 
 /*
 ** Return the number of changes since the database handle was opened.
 */
-int sqlite3_total_changes(sqlite3 *db){
+SQLITE_API int sqlite3_total_changes(sqlite3 *db){
   return db->nTotalChange;
 }
 
 /*
@@ -66494,8 +70057,9 @@
   }
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
+  sqlite3_mutex_enter(db->mutex);
 
 #ifdef SQLITE_SSE
   {
     extern void sqlite3SseCleanup(sqlite3*);
@@ -66517,8 +70081,9 @@
   /* If there are any outstanding VMs, return SQLITE_BUSY. */
   if( db->pVdbe ){
     sqlite3Error(db, SQLITE_BUSY,
         "Unable to close due to unfinalised statements");
+    sqlite3_mutex_leave(db->mutex);
     return SQLITE_BUSY;
   }
   assert( !sqlite3SafetyCheck(db) );
 
@@ -66531,8 +70096,9 @@
   ** true. It's hard to see how to cause it without messing with threads.
   */
   if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
     /* printf("DID NOT CLOSE\n"); fflush(stdout); */
+    sqlite3_mutex_leave(db->mutex);
     return SQLITE_ERROR;
   }
 
   for(j=0; j<db->nDb; j++){
@@ -66551,9 +70117,9 @@
   for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
     FuncDef *pFunc, *pNext;
     for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
       pNext = pFunc->pNext;
-      sqliteFree(pFunc);
+      sqlite3_free(pFunc);
     }
   }
 
   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
@@ -66563,9 +70129,9 @@
       if( pColl[j].xDel ){
         pColl[j].xDel(pColl[j].pUser);
       }
     }
-    sqliteFree(pColl);
+    sqlite3_free(pColl);
   }
   sqlite3HashClear(&db->aCollSeq);
 #ifndef SQLITE_OMIT_VIRTUALTABLE
   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
@@ -66572,9 +70138,9 @@
     Module *pMod = (Module *)sqliteHashData(i);
     if( pMod->xDestroy ){
       pMod->xDestroy(pMod->pAux);
     }
-    sqliteFree(pMod);
+    sqlite3_free(pMod);
   }
   sqlite3HashClear(&db->aModule);
 #endif
 
@@ -66592,11 +70158,12 @@
   ** So it needs to be freed here. Todo: Why not roll the temp schema into
   ** the same sqliteMalloc() as the one that allocates the database
   ** structure?
   */
-  sqliteFree(db->aDb[1].pSchema);
-  sqliteFree(db);
-  sqlite3ReleaseThreadData();
+  sqlite3_free(db->aDb[1].pSchema);
+  sqlite3_mutex_leave(db->mutex);
+  sqlite3_mutex_free(db->mutex);
+  sqlite3_free(db);
   return SQLITE_OK;
 }
 
 /*
@@ -66604,8 +70171,9 @@
 */
 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){
   int i;
   int inTrans = 0;
+  assert( sqlite3_mutex_held(db->mutex) );
   for(i=0; i<db->nDb; i++){
     if( db->aDb[i].pBt ){
       if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
         inTrans = 1;
@@ -66615,8 +70183,9 @@
     }
   }
   sqlite3VtabRollback(db);
   if( db->flags&SQLITE_InternChanges ){
+    sqlite3ExpirePreparedStatements(db);
     sqlite3ResetInternalSchema(db, 0);
   }
 
   /* If one has been configured, invoke the rollback-hook callback */
@@ -66678,9 +70247,10 @@
      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
   static const u8 totals[] =
      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
 # define NDELAY (sizeof(delays)/sizeof(delays[0]))
-  int timeout = ((sqlite3 *)ptr)->busyTimeout;
+  sqlite3 *db = (sqlite3 *)ptr;
+  int timeout = db->busyTimeout;
   int delay, prior;
 
   assert( count>=0 );
   if( count < NDELAY ){
@@ -66693,16 +70263,17 @@
   if( prior + delay > timeout ){
     delay = timeout - prior;
     if( delay<=0 ) return 0;
   }
-  sqlite3OsSleep(delay);
+  sqlite3OsSleep(db->pVfs, delay*1000);
   return 1;
 #else
+  sqlite3 *db = (sqlite3 *)ptr;
   int timeout = ((sqlite3 *)ptr)->busyTimeout;
   if( (count+1)*1000 > timeout ){
     return 0;
   }
-  sqlite3OsSleep(1000);
+  sqlite3OsSleep(db->pVfs, 1000000);
   return 1;
 #endif
 }
 
@@ -66728,19 +70299,21 @@
 /*
 ** This routine sets the busy callback for an Sqlite database to the
 ** given callback function with the given argument.
 */
-int sqlite3_busy_handler(
+SQLITE_API int sqlite3_busy_handler(
   sqlite3 *db,
   int (*xBusy)(void*,int),
   void *pArg
 ){
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
+  sqlite3_mutex_enter(db->mutex);
   db->busyHandler.xFunc = xBusy;
   db->busyHandler.pArg = pArg;
   db->busyHandler.nBusy = 0;
+  sqlite3_mutex_leave(db->mutex);
   return SQLITE_OK;
 }
 
 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -66748,15 +70321,16 @@
 ** This routine sets the progress callback for an Sqlite database to the
 ** given callback function with the given argument. The progress callback will
 ** be invoked every nOps opcodes.
 */
-void sqlite3_progress_handler(
+SQLITE_API void sqlite3_progress_handler(
   sqlite3 *db,
   int nOps,
   int (*xProgress)(void*),
   void *pArg
 ){
   if( !sqlite3SafetyCheck(db) ){
+    sqlite3_mutex_enter(db->mutex);
     if( nOps>0 ){
       db->xProgress = xProgress;
       db->nProgressOps = nOps;
       db->pProgressArg = pArg;
@@ -66764,8 +70338,9 @@
       db->xProgress = 0;
       db->nProgressOps = 0;
       db->pProgressArg = 0;
     }
+    sqlite3_mutex_leave(db->mutex);
   }
 }
 #endif
 
@@ -66773,9 +70348,9 @@
 /*
 ** This routine installs a default busy handler that waits for the
 ** specified number of milliseconds before returning 0.
 */
-int sqlite3_busy_timeout(sqlite3 *db, int ms){
+SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
   if( ms>0 ){
@@ -66795,32 +70370,8 @@
     db->u1.isInterrupted = 1;
   }
 }
 
-/*
-** Memory allocation routines that use SQLites internal memory
-** memory allocator.  Depending on how SQLite is compiled, the
-** internal memory allocator might be just an alias for the
-** system default malloc/realloc/free.  Or the built-in allocator
-** might do extra stuff like put sentinals around buffers to
-** check for overruns or look for memory leaks.
-**
-** Use sqlite3_free() to free memory returned by sqlite3_mprintf().
-*/
-SQLITE_API void sqlite3_free(void *p){ if( p ) sqlite3OsFree(p); }
-SQLITE_API void *sqlite3_malloc(int nByte){ return nByte>0 ? sqlite3OsMalloc(nByte) : 0; }
-SQLITE_API void *sqlite3_realloc(void *pOld, int nByte){
-  if( pOld ){
-    if( nByte>0 ){
-      return sqlite3OsRealloc(pOld, nByte);
-    }else{
-      sqlite3OsFree(pOld);
-      return 0;
-    }
-  }else{
-    return sqlite3_malloc(nByte);
-  }
-}
 
 /*
 ** This function is exactly the same as sqlite3_create_function(), except
 ** that it is designed to be called by internal code. The difference is
@@ -66839,8 +70390,9 @@
 ){
   FuncDef *p;
   int nName;
 
+  assert( sqlite3_mutex_held(db->mutex) );
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
   if( zFunctionName==0 ||
@@ -66866,12 +70418,15 @@
   }else if( enc==SQLITE_ANY ){
     int rc;
     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
          pUserData, xFunc, xStep, xFinal);
-    if( rc!=SQLITE_OK ) return rc;
-    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
-        pUserData, xFunc, xStep, xFinal);
-    if( rc!=SQLITE_OK ) return rc;
+    if( rc==SQLITE_OK ){
+      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
+          pUserData, xFunc, xStep, xFinal);
+    }
+    if( rc!=SQLITE_OK ){
+      return rc;
+    }
     enc = SQLITE_UTF16BE;
   }
 #else
   enc = SQLITE_UTF8;
@@ -66886,31 +70441,33 @@
   if( p && p->iPrefEnc==enc && p->nArg==nArg ){
     if( db->activeVdbeCnt ){
       sqlite3Error(db, SQLITE_BUSY,
         "Unable to delete/modify user-function due to active statements");
-      assert( !sqlite3MallocFailed() );
+      assert( !db->mallocFailed );
       return SQLITE_BUSY;
     }else{
       sqlite3ExpirePreparedStatements(db);
     }
   }
 
   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
-  if( p ){
-    p->flags = 0;
-    p->xFunc = xFunc;
-    p->xStep = xStep;
-    p->xFinalize = xFinal;
-    p->pUserData = pUserData;
-    p->nArg = nArg;
-  }
+  assert(p || db->mallocFailed);
+  if( !p ){
+    return SQLITE_NOMEM;
+  }
+  p->flags = 0;
+  p->xFunc = xFunc;
+  p->xStep = xStep;
+  p->xFinalize = xFinal;
+  p->pUserData = pUserData;
+  p->nArg = nArg;
   return SQLITE_OK;
 }
 
 /*
 ** Create new user functions.
 */
-int sqlite3_create_function(
+SQLITE_API int sqlite3_create_function(
   sqlite3 *db,
   const char *zFunctionName,
   int nArg,
   int enc,
@@ -66919,16 +70476,18 @@
   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
   void (*xFinal)(sqlite3_context*)
 ){
   int rc;
-  assert( !sqlite3MallocFailed() );
+  sqlite3_mutex_enter(db->mutex);
+  assert( !db->mallocFailed );
   rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
-
-  return sqlite3ApiExit(db, rc);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 #ifndef SQLITE_OMIT_UTF16
-int sqlite3_create_function16(
+SQLITE_API int sqlite3_create_function16(
   sqlite3 *db,
   const void *zFunctionName,
   int nArg,
   int eTextRep,
@@ -66938,15 +70497,16 @@
   void (*xFinal)(sqlite3_context*)
 ){
   int rc;
   char *zFunc8;
-  assert( !sqlite3MallocFailed() );
-
-  zFunc8 = sqlite3Utf16to8(zFunctionName, -1);
+  sqlite3_mutex_enter(db->mutex);
+  assert( !db->mallocFailed );
+  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
-  sqliteFree(zFunc8);
-
-  return sqlite3ApiExit(db, rc);
+  sqlite3_free(zFunc8);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 #endif
 
 
@@ -66961,19 +70521,23 @@
 ** should call this routine to make sure the global function exists.
 ** A global function must exist in order for name resolution to work
 ** properly.
 */
-int sqlite3_overload_function(
+SQLITE_API int sqlite3_overload_function(
   sqlite3 *db,
   const char *zName,
   int nArg
 ){
   int nName = strlen(zName);
+  int rc;
+  sqlite3_mutex_enter(db->mutex);
   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
     sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
                       0, sqlite3InvalidFunction, 0, 0);
   }
-  return sqlite3ApiExit(db, SQLITE_OK);
+  rc = sqlite3ApiExit(db, SQLITE_OK);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 #ifndef SQLITE_OMIT_TRACE
 /*
@@ -66984,11 +70548,14 @@
 ** trace is a pointer to a function that is invoked at the start of each
 ** SQL statement.
 */
 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
-  void *pOld = db->pTraceArg;
+  void *pOld;
+  sqlite3_mutex_enter(db->mutex);
+  pOld = db->pTraceArg;
   db->xTrace = xTrace;
   db->pTraceArg = pArg;
+  sqlite3_mutex_leave(db->mutex);
   return pOld;
 }
 /*
 ** Register a profile function.  The pArg from the previously registered
@@ -67002,11 +70569,14 @@
   sqlite3 *db,
   void (*xProfile)(void*,const char*,sqlite_uint64),
   void *pArg
 ){
-  void *pOld = db->pProfileArg;
+  void *pOld;
+  sqlite3_mutex_enter(db->mutex);
+  pOld = db->pProfileArg;
   db->xProfile = xProfile;
   db->pProfileArg = pArg;
+  sqlite3_mutex_leave(db->mutex);
   return pOld;
 }
 #endif /* SQLITE_OMIT_TRACE */
 
@@ -67015,46 +70585,55 @@
 ** Register a function to be invoked when a transaction comments.
 ** If the invoked function returns non-zero, then the commit becomes a
 ** rollback.
 */
-void *sqlite3_commit_hook(
+SQLITE_API void *sqlite3_commit_hook(
   sqlite3 *db,              /* Attach the hook to this database */
   int (*xCallback)(void*),  /* Function to invoke on each commit */
   void *pArg                /* Argument to the function */
 ){
-  void *pOld = db->pCommitArg;
+  void *pOld;
+  sqlite3_mutex_enter(db->mutex);
+  pOld = db->pCommitArg;
   db->xCommitCallback = xCallback;
   db->pCommitArg = pArg;
+  sqlite3_mutex_leave(db->mutex);
   return pOld;
 }
 
 /*
 ** Register a callback to be invoked each time a row is updated,
 ** inserted or deleted using this database connection.
 */
-void *sqlite3_update_hook(
+SQLITE_API void *sqlite3_update_hook(
   sqlite3 *db,              /* Attach the hook to this database */
   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
   void *pArg                /* Argument to the function */
 ){
-  void *pRet = db->pUpdateArg;
+  void *pRet;
+  sqlite3_mutex_enter(db->mutex);
+  pRet = db->pUpdateArg;
   db->xUpdateCallback = xCallback;
   db->pUpdateArg = pArg;
+  sqlite3_mutex_leave(db->mutex);
   return pRet;
 }
 
 /*
 ** Register a callback to be invoked each time a transaction is rolled
 ** back by this database connection.
 */
-void *sqlite3_rollback_hook(
+SQLITE_API void *sqlite3_rollback_hook(
   sqlite3 *db,              /* Attach the hook to this database */
   void (*xCallback)(void*), /* Callback function */
   void *pArg                /* Argument to the function */
 ){
-  void *pRet = db->pRollbackArg;
+  void *pRet;
+  sqlite3_mutex_enter(db->mutex);
+  pRet = db->pRollbackArg;
   db->xRollbackCallback = xCallback;
   db->pRollbackArg = pArg;
+  sqlite3_mutex_leave(db->mutex);
   return pRet;
 }
 
 /*
@@ -67086,19 +70665,21 @@
   const sqlite3 *db,        /* Main database when opening aux otherwise 0 */
   const char *zFilename,    /* Name of the file containing the BTree database */
   int omitJournal,          /* if TRUE then do not journal this file */
   int nCache,               /* How many pages in the page cache */
+  int vfsFlags,             /* Flags passed through to vfsOpen */
   Btree **ppBtree           /* Pointer to new Btree object written here */
 ){
-  int btree_flags = 0;
+  int btFlags = 0;
   int rc;
 
+  assert( sqlite3_mutex_held(db->mutex) );
   assert( ppBtree != 0);
   if( omitJournal ){
-    btree_flags |= BTREE_OMIT_JOURNAL;
+    btFlags |= BTREE_OMIT_JOURNAL;
   }
   if( db->flags & SQLITE_NoReadlock ){
-    btree_flags |= BTREE_NO_READLOCK;
+    btFlags |= BTREE_NO_READLOCK;
   }
   if( zFilename==0 ){
 #if TEMP_STORE==0
     /* Do nothing */
@@ -67115,9 +70696,12 @@
 #endif
 #endif /* SQLITE_OMIT_MEMORYDB */
   }
 
-  rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btree_flags);
+  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
+    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
+  }
+  rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
   if( rc==SQLITE_OK ){
     sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
     sqlite3BtreeSetCacheSize(*ppBtree, nCache);
   }
@@ -67129,19 +70713,21 @@
 ** error.
 */
 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
   const char *z;
-  assert( !sqlite3MallocFailed() );
   if( !db ){
     return sqlite3ErrStr(SQLITE_NOMEM);
   }
   if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
     return sqlite3ErrStr(SQLITE_MISUSE);
   }
+  sqlite3_mutex_enter(db->mutex);
+  assert( !db->mallocFailed );
   z = (char*)sqlite3_value_text(db->pErr);
   if( z==0 ){
     z = sqlite3ErrStr(db->errCode);
   }
+  sqlite3_mutex_leave(db->mutex);
   return z;
 }
 
 #ifndef SQLITE_OMIT_UTF16
@@ -67169,22 +70755,24 @@
     0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
   };
 
   const void *z;
-  assert( !sqlite3MallocFailed() );
   if( !db ){
     return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   }
   if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
     return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   }
+  sqlite3_mutex_enter(db->mutex);
+  assert( !db->mallocFailed );
   z = sqlite3_value_text16(db->pErr);
   if( z==0 ){
     sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
          SQLITE_UTF8, SQLITE_STATIC);
     z = sqlite3_value_text16(db->pErr);
   }
   sqlite3ApiExit(0, 0);
+  sqlite3_mutex_leave(db->mutex);
   return z;
 }
 #endif /* SQLITE_OMIT_UTF16 */
 
@@ -67192,9 +70780,9 @@
 ** Return the most recent error code generated by an SQLite routine. If NULL is
 ** passed to this function, we assume a malloc() failed during sqlite3_open().
 */
 SQLITE_API int sqlite3_errcode(sqlite3 *db){
-  if( !db || sqlite3MallocFailed() ){
+  if( !db || db->mallocFailed ){
     return SQLITE_NOMEM;
   }
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
@@ -67219,8 +70807,9 @@
 
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
+  assert( sqlite3_mutex_held(db->mutex) );
 
   /* If SQLITE_UTF16 is specified as the encoding type, transform this
   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
@@ -67287,23 +70876,30 @@
 ** is UTF-8 encoded.
 */
 static int openDatabase(
   const char *zFilename, /* Database filename UTF-8 encoded */
-  sqlite3 **ppDb         /* OUT: Returned database handle */
+  sqlite3 **ppDb,        /* OUT: Returned database handle */
+  unsigned flags,        /* Operational flags */
+  const char *zVfs       /* Name of the VFS to use */
 ){
   sqlite3 *db;
   int rc;
   CollSeq *pColl;
 
-  assert( !sqlite3MallocFailed() );
-
   /* Allocate the sqlite data structure */
-  db = sqliteMalloc( sizeof(sqlite3) );
+  db = sqlite3MallocZero( sizeof(sqlite3) );
   if( db==0 ) goto opendb_out;
+  db->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
+  if( db->mutex==0 ){
+    sqlite3_free(db);
+    db = 0;
+    goto opendb_out;
+  }
+  sqlite3_mutex_enter(db->mutex);
   db->errMask = 0xff;
   db->priorNewRowid = 0;
-  db->magic = SQLITE_MAGIC_BUSY;
   db->nDb = 2;
+  db->magic = SQLITE_MAGIC_BUSY;
   db->aDb = db->aDbStatic;
   db->autoCommit = 1;
   db->flags |= SQLITE_ShortColNames
 #if SQLITE_DEFAULT_FILE_FORMAT<4
@@ -67318,8 +70914,16 @@
 #ifndef SQLITE_OMIT_VIRTUALTABLE
   sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0);
 #endif
 
+  db->pVfs = sqlite3_vfs_find(zVfs);
+  if( !db->pVfs ){
+    rc = SQLITE_ERROR;
+    db->magic = SQLITE_MAGIC_CLOSED;
+    sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)"));
+    goto opendb_out;
+  }
+
   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
   ** and UTF-16, so add a version for each to avoid any unnecessary
   ** conversions. The only error that can occur here is a malloc() failure.
   */
@@ -67327,9 +70931,9 @@
       createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) ||
       createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) ||
       (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0
   ){
-    assert( sqlite3MallocFailed() );
+    assert( db->mallocFailed );
     db->magic = SQLITE_MAGIC_CLOSED;
     goto opendb_out;
   }
 
@@ -67343,17 +70947,19 @@
     pColl->type = SQLITE_COLL_NOCASE;
   }
 
   /* Open the backend database driver */
+  db->openFlags = flags;
   rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE,
+                           flags | SQLITE_OPEN_MAIN_DB,
                            &db->aDb[0].pBt);
   if( rc!=SQLITE_OK ){
     sqlite3Error(db, rc, 0);
     db->magic = SQLITE_MAGIC_CLOSED;
     goto opendb_out;
   }
-  db->aDb[0].pSchema = sqlite3SchemaGet(db->aDb[0].pBt);
-  db->aDb[1].pSchema = sqlite3SchemaGet(0);
+  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
+  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
 
 
   /* The default safety_level for the main database is 'full'; for the temp
   ** database it is 'NONE'. This matches the pager layer defaults.
@@ -67365,9 +70971,9 @@
   db->aDb[1].safety_level = 1;
 #endif
 
   db->magic = SQLITE_MAGIC_OPEN;
-  if( sqlite3MallocFailed() ){
+  if( db->mallocFailed ){
     goto opendb_out;
   }
 
   /* Register all built-in functions, but do not attempt to read the
@@ -67385,23 +70991,30 @@
     goto opendb_out;
   }
 
 #ifdef SQLITE_ENABLE_FTS1
-  if( !sqlite3MallocFailed() ){
+  if( !db->mallocFailed ){
     extern int sqlite3Fts1Init(sqlite3*);
     rc = sqlite3Fts1Init(db);
   }
 #endif
 
 #ifdef SQLITE_ENABLE_FTS2
-  if( !sqlite3MallocFailed() && rc==SQLITE_OK ){
+  if( !db->mallocFailed && rc==SQLITE_OK ){
     extern int sqlite3Fts2Init(sqlite3*);
     rc = sqlite3Fts2Init(db);
   }
 #endif
 
+#ifdef SQLITE_ENABLE_FTS3
+  if( !db->mallocFailed && rc==SQLITE_OK ){
+    extern int sqlite3Fts3Init(sqlite3*);
+    rc = sqlite3Fts3Init(db);
+  }
+#endif
+
 #ifdef SQLITE_ENABLE_ICU
-  if( !sqlite3MallocFailed() && rc==SQLITE_OK ){
+  if( !db->mallocFailed && rc==SQLITE_OK ){
     extern int sqlite3IcuInit(sqlite3*);
     rc = sqlite3IcuInit(db);
   }
 #endif
@@ -67417,8 +71030,11 @@
                           SQLITE_DEFAULT_LOCKING_MODE);
 #endif
 
 opendb_out:
+  if( db && db->mutex ){
+    sqlite3_mutex_leave(db->mutex);
+  }
   if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
     sqlite3_close(db);
     db = 0;
   }
@@ -67432,9 +71048,18 @@
 SQLITE_API int sqlite3_open(
   const char *zFilename,
   sqlite3 **ppDb
 ){
-  return openDatabase(zFilename, ppDb);
+  return openDatabase(zFilename, ppDb,
+                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
+}
+SQLITE_API int sqlite3_open_v2(
+  const char *filename,   /* Database filename (UTF-8) */
+  sqlite3 **ppDb,         /* OUT: SQLite db handle */
+  int flags,              /* Flags */
+  const char *zVfs        /* Name of VFS module to use */
+){
+  return openDatabase(filename, ppDb, flags, zVfs);
 }
 
 #ifndef SQLITE_OMIT_UTF16
 /*
@@ -67444,19 +71069,20 @@
   const void *zFilename,
   sqlite3 **ppDb
 ){
   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
-  int rc = SQLITE_OK;
   sqlite3_value *pVal;
+  int rc = SQLITE_NOMEM;
 
   assert( zFilename );
   assert( ppDb );
   *ppDb = 0;
-  pVal = sqlite3ValueNew();
+  pVal = sqlite3ValueNew(0);
   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
   if( zFilename8 ){
-    rc = openDatabase(zFilename8, ppDb);
+    rc = openDatabase(zFilename8, ppDb,
+                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
     if( rc==SQLITE_OK && *ppDb ){
       rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
       if( rc!=SQLITE_OK ){
         sqlite3_close(*ppDb);
@@ -67470,66 +71096,30 @@
 }
 #endif /* SQLITE_OMIT_UTF16 */
 
 /*
-** The following routine destroys a virtual machine that is created by
-** the sqlite3_compile() routine. The integer returned is an SQLITE_
-** success/failure code that describes the result of executing the virtual
-** machine.
-**
-** This routine sets the error code and string returned by
-** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
-*/
-SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
+** Register a new collation sequence with the database handle db.
+*/
+SQLITE_API int sqlite3_create_collation(
+  sqlite3* db,
+  const char *zName,
+  int enc,
+  void* pCtx,
+  int(*xCompare)(void*,int,const void*,int,const void*)
+){
   int rc;
-  if( pStmt==0 ){
-    rc = SQLITE_OK;
-  }else{
-    rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
-  }
-  return rc;
-}
-
-/*
-** Terminate the current execution of an SQL statement and reset it
-** back to its starting state so that it can be reused. A success code from
-** the prior execution is returned.
-**
-** This routine sets the error code and string returned by
-** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
-*/
-SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
-  int rc;
-  if( pStmt==0 ){
-    rc = SQLITE_OK;
-  }else{
-    rc = sqlite3VdbeReset((Vdbe*)pStmt);
-    sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0);
-    assert( (rc & (sqlite3_db_handle(pStmt)->errMask))==rc );
-  }
+  sqlite3_mutex_enter(db->mutex);
+  assert( !db->mallocFailed );
+  rc = createCollation(db, zName, enc, pCtx, xCompare, 0);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
   return rc;
 }
 
 /*
 ** Register a new collation sequence with the database handle db.
 */
-int sqlite3_create_collation(
-  sqlite3* db,
-  const char *zName,
-  int enc,
-  void* pCtx,
-  int(*xCompare)(void*,int,const void*,int,const void*)
-){
-  int rc;
-  assert( !sqlite3MallocFailed() );
-  rc = createCollation(db, zName, enc, pCtx, xCompare, 0);
-  return sqlite3ApiExit(db, rc);
-}
-
-/*
-** Register a new collation sequence with the database handle db.
-*/
-int sqlite3_create_collation_v2(
+SQLITE_API int sqlite3_create_collation_v2(
   sqlite3* db,
   const char *zName,
   int enc,
   void* pCtx,
@@ -67536,18 +71126,21 @@
   int(*xCompare)(void*,int,const void*,int,const void*),
   void(*xDel)(void*)
 ){
   int rc;
-  assert( !sqlite3MallocFailed() );
+  sqlite3_mutex_enter(db->mutex);
+  assert( !db->mallocFailed );
   rc = createCollation(db, zName, enc, pCtx, xCompare, xDel);
-  return sqlite3ApiExit(db, rc);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 #ifndef SQLITE_OMIT_UTF16
 /*
 ** Register a new collation sequence with the database handle db.
 */
-int sqlite3_create_collation16(
+SQLITE_API int sqlite3_create_collation16(
   sqlite3* db,
   const char *zName,
   int enc,
   void* pCtx,
@@ -67554,33 +71147,38 @@
   int(*xCompare)(void*,int,const void*,int,const void*)
 ){
   int rc = SQLITE_OK;
   char *zName8;
-  assert( !sqlite3MallocFailed() );
-  zName8 = sqlite3Utf16to8(zName, -1);
+  sqlite3_mutex_enter(db->mutex);
+  assert( !db->mallocFailed );
+  zName8 = sqlite3Utf16to8(db, zName, -1);
   if( zName8 ){
     rc = createCollation(db, zName8, enc, pCtx, xCompare, 0);
-    sqliteFree(zName8);
-  }
-  return sqlite3ApiExit(db, rc);
+    sqlite3_free(zName8);
+  }
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 #endif /* SQLITE_OMIT_UTF16 */
 
 /*
 ** Register a collation sequence factory callback with the database handle
 ** db. Replace any previously installed collation sequence factory.
 */
-int sqlite3_collation_needed(
+SQLITE_API int sqlite3_collation_needed(
   sqlite3 *db,
   void *pCollNeededArg,
   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
 ){
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
+  sqlite3_mutex_enter(db->mutex);
   db->xCollNeeded = xCollNeeded;
   db->xCollNeeded16 = 0;
   db->pCollNeededArg = pCollNeededArg;
+  sqlite3_mutex_leave(db->mutex);
   return SQLITE_OK;
 }
 
 #ifndef SQLITE_OMIT_UTF16
@@ -67587,19 +71185,21 @@
 /*
 ** Register a collation sequence factory callback with the database handle
 ** db. Replace any previously installed collation sequence factory.
 */
-int sqlite3_collation_needed16(
+SQLITE_API int sqlite3_collation_needed16(
   sqlite3 *db,
   void *pCollNeededArg,
   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
 ){
   if( sqlite3SafetyCheck(db) ){
     return SQLITE_MISUSE;
   }
+  sqlite3_mutex_enter(db->mutex);
   db->xCollNeeded = 0;
   db->xCollNeeded16 = xCollNeeded16;
   db->pCollNeededArg = pCollNeededArg;
+  sqlite3_mutex_leave(db->mutex);
   return SQLITE_OK;
 }
 #endif /* SQLITE_OMIT_UTF16 */
 
@@ -67607,9 +71207,9 @@
 /*
 ** This function is now an anachronism. It used to be used to recover from a
 ** malloc() failure, but SQLite now does this automatically.
 */
-int sqlite3_global_recover(){
+SQLITE_API int sqlite3_global_recover(){
   return SQLITE_OK;
 }
 #endif
 
@@ -67620,9 +71220,9 @@
 ** by the next COMMIT or ROLLBACK.
 **
 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
 */
-int sqlite3_get_autocommit(sqlite3 *db){
+SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
   return db->autoCommit;
 }
 
 #ifdef SQLITE_DEBUG
@@ -67635,55 +71235,24 @@
   return SQLITE_CORRUPT;
 }
 #endif
 
-
-#ifndef SQLITE_OMIT_SHARED_CACHE
-/*
-** Enable or disable the shared pager and schema features for the
-** current thread.
-**
-** This routine should only be called when there are no open
-** database connections.
-*/
-int sqlite3_enable_shared_cache(int enable){
-  ThreadData *pTd = sqlite3ThreadData();
-  if( pTd ){
-    /* It is only legal to call sqlite3_enable_shared_cache() when there
-    ** are no currently open b-trees that were opened by the calling thread.
-    ** This condition is only easy to detect if the shared-cache were
-    ** previously enabled (and is being disabled).
-    */
-    if( pTd->pBtree && !enable ){
-      assert( pTd->useSharedData );
-      return SQLITE_MISUSE;
-    }
-
-    pTd->useSharedData = enable;
-    sqlite3ReleaseThreadData();
-  }
-  return sqlite3ApiExit(0, SQLITE_OK);
-}
-#endif
-
 /*
 ** This is a convenience routine that makes sure that all thread-specific
 ** data for this thread has been deallocated.
+**
+** SQLite no longer uses thread-specific data so this routine is now a
+** no-op.  It is retained for historical compatibility.
 */
-void sqlite3_thread_cleanup(void){
-  ThreadData *pTd = sqlite3OsThreadSpecificData(0);
-  if( pTd ){
-    memset(pTd, 0, sizeof(*pTd));
-    sqlite3OsThreadSpecificData(-1);
-  }
+SQLITE_API void sqlite3_thread_cleanup(void){
 }
 
 /*
 ** Return meta information about a specific column of a database table.
 ** See comment in sqlite3.h (sqlite.h.in) for details.
 */
 #ifdef SQLITE_ENABLE_COLUMN_METADATA
-int sqlite3_table_column_metadata(
+SQLITE_API int sqlite3_table_column_metadata(
   sqlite3 *db,                /* Connection handle */
   const char *zDbName,        /* Database name or NULL */
   const char *zTableName,     /* Table name */
   const char *zColumnName,    /* Column name */
@@ -67708,8 +71277,9 @@
   /* Ensure the database schema has been loaded */
   if( sqlite3SafetyOn(db) ){
     return SQLITE_MISUSE;
   }
+  sqlite3_mutex_enter(db->mutex);
   rc = sqlite3Init(db, &zErrMsg);
   if( SQLITE_OK!=rc ){
     goto error_out;
   }
@@ -67784,37 +71354,70 @@
         zColumnName, 0);
     rc = SQLITE_ERROR;
   }
   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
-  sqliteFree(zErrMsg);
-  return sqlite3ApiExit(db, rc);
+  sqlite3_free(zErrMsg);
+  rc = sqlite3ApiExit(db, rc);
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 #endif
-
-/*
-** Set all the parameters in the compiled SQL statement to NULL.
-*/
-int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
-  int i;
-  int rc = SQLITE_OK;
-  for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){
-    rc = sqlite3_bind_null(pStmt, i);
-  }
-  return rc;
-}
 
 /*
 ** Sleep for a little while.  Return the amount of time slept.
 */
 SQLITE_API int sqlite3_sleep(int ms){
-  return sqlite3OsSleep(ms);
+  sqlite3_vfs *pVfs;
+  int rc;
+  pVfs = sqlite3_vfs_find(0);
+
+  /* This function works in milliseconds, but the underlying OsSleep()
+  ** API uses microseconds. Hence the 1000's.
+  */
+  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
+  return rc;
 }
 
 /*
 ** Enable or disable the extended result codes.
 */
-int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
+SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
+  sqlite3_mutex_enter(db->mutex);
   db->errMask = onoff ? 0xffffffff : 0xff;
+  sqlite3_mutex_leave(db->mutex);
   return SQLITE_OK;
+}
+
+/*
+** Invoke the xFileControl method on a particular database.
+*/
+SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
+  int rc = SQLITE_ERROR;
+  int iDb;
+  sqlite3_mutex_enter(db->mutex);
+  if( zDbName==0 ){
+    iDb = 0;
+  }else{
+    for(iDb=0; iDb<db->nDb; iDb++){
+      if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
+    }
+  }
+  if( iDb<db->nDb ){
+    Btree *pBtree = db->aDb[iDb].pBt;
+    if( pBtree ){
+      Pager *pPager;
+      sqlite3BtreeEnter(pBtree);
+      pPager = sqlite3BtreePager(pBtree);
+      if( pPager ){
+        sqlite3_file *fd = sqlite3PagerFile(pPager);
+        if( fd ){
+          rc = sqlite3OsFileControl(fd, op, pArg);
+        }
+      }
+      sqlite3BtreeLeave(pBtree);
+    }
+  }
+  sqlite3_mutex_leave(db->mutex);
+  return rc;
 }
 
 /************** End of main.c ************************************************/