Fossil SCM

Update the version of SQLite used internally to 3.6.6.1.

drh 2008-11-22 15:40 trunk
Commit dec2c22d9d0e1d9a7114000f4c5599e2824298c8
2 files changed +2597 -1491 +168 -9
+2597 -1491
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.6.4. By combining all the individual C code files into this
3
+** version 3.6.6.1. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a one translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% are more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -9,17 +9,17 @@
99
**
1010
** This file is all you need to compile SQLite. To use SQLite in other
1111
** programs, you need this file and the "sqlite3.h" header file that defines
1212
** the programming interface to the SQLite library. (If you do not have
1313
** the "sqlite3.h" header file at hand, you will find a copy in the first
14
-** 6569 lines past this header comment.) Additional code files may be
14
+** 6728 lines past this header comment.) Additional code files may be
1515
** needed if you want a wrapper to interface SQLite with your choice of
1616
** programming language. The code for the "sqlite3" command-line shell
1717
** is also in a separate file. This file contains only code for the core
1818
** SQLite library.
1919
**
20
-** This amalgamation was generated on 2008-11-10 00:14:36 UTC.
20
+** This amalgamation was generated on 2008-11-22 14:31:32 UTC.
2121
*/
2222
#define SQLITE_CORE 1
2323
#define SQLITE_AMALGAMATION 1
2424
#ifndef SQLITE_PRIVATE
2525
# define SQLITE_PRIVATE static
@@ -39,11 +39,11 @@
3939
** May you share freely, never taking more than you give.
4040
**
4141
*************************************************************************
4242
** Internal interface definitions for SQLite.
4343
**
44
-** @(#) $Id: sqliteInt.h,v 1.788 2008/11/05 16:37:35 drh Exp $
44
+** @(#) $Id: sqliteInt.h,v 1.798 2008/11/19 16:52:44 danielk1977 Exp $
4545
*/
4646
#ifndef _SQLITEINT_H_
4747
#define _SQLITEINT_H_
4848
4949
/*
@@ -485,11 +485,11 @@
485485
** The name of this file under configuration management is "sqlite.h.in".
486486
** The makefile makes some minor changes to this file (such as inserting
487487
** the version number) and changes its name to "sqlite3.h" as
488488
** part of the build process.
489489
**
490
-** @(#) $Id: sqlite.h.in,v 1.409 2008/11/07 00:06:18 drh Exp $
490
+** @(#) $Id: sqlite.h.in,v 1.415 2008/11/19 01:20:26 drh Exp $
491491
*/
492492
#ifndef _SQLITE3_H_
493493
#define _SQLITE3_H_
494494
#include <stdarg.h> /* Needed for the definition of va_list */
495495
@@ -562,12 +562,12 @@
562562
**
563563
** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer
564564
** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z
565565
** are the major version, minor version, and release number.
566566
*/
567
-#define SQLITE_VERSION "3.6.4"
568
-#define SQLITE_VERSION_NUMBER 3006004
567
+#define SQLITE_VERSION "3.6.6.1"
568
+#define SQLITE_VERSION_NUMBER 3006006
569569
570570
/*
571571
** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
572572
** KEYWORDS: sqlite3_version
573573
**
@@ -1042,11 +1042,11 @@
10421042
**
10431043
** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
10441044
** sync operation only needs to flush data to mass storage. Inode
10451045
** information need not be flushed. The SQLITE_SYNC_NORMAL flag means
10461046
** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means
1047
-** to use Mac OS-X style fullsync instead of fsync().
1047
+** to use Mac OS X style fullsync instead of fsync().
10481048
*/
10491049
#define SQLITE_SYNC_NORMAL 0x00002
10501050
#define SQLITE_SYNC_FULL 0x00003
10511051
#define SQLITE_SYNC_DATAONLY 0x00010
10521052
@@ -1074,11 +1074,11 @@
10741074
** This object defines the methods used to perform various operations
10751075
** against the open file represented by the [sqlite3_file] object.
10761076
**
10771077
** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
10781078
** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1079
-** The second choice is a Mac OS-X style fullsync. The [SQLITE_SYNC_DATAONLY]
1079
+** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
10801080
** flag may be ORed in to indicate that only the data of the file
10811081
** and not its inode needs to be synced.
10821082
**
10831083
** The integer values to xLock() and xUnlock() are one of
10841084
** <ul>
@@ -1729,11 +1729,14 @@
17291729
** scratch buffers or if no scratch buffer space is specified, then SQLite
17301730
** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
17311731
**
17321732
** <dt>SQLITE_CONFIG_PAGECACHE</dt>
17331733
** <dd>This option specifies a static memory buffer that SQLite can use for
1734
-** the database page cache. There are three arguments: A pointer to the
1734
+** the database page cache with the default page cache implemenation.
1735
+** This configuration should not be used if an application-define page
1736
+** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1737
+** There are three arguments to this option: A pointer to the
17351738
** memory, the size of each page buffer (sz), and the number of pages (N).
17361739
** The sz argument must be a power of two between 512 and 32768. The first
17371740
** argument should point to an allocation of at least sz*N bytes of memory.
17381741
** SQLite will use the memory provided by the first argument to satisfy its
17391742
** memory needs for the first N pages that it adds to cache. If additional
@@ -1774,10 +1777,21 @@
17741777
** <dd>This option takes two arguments that determine the default
17751778
** memory allcation lookaside optimization. The first argument is the
17761779
** size of each lookaside buffer slot and the second is the number of
17771780
** slots allocated to each database connection.</dd>
17781781
**
1782
+** <dt>SQLITE_CONFIG_PCACHE</dt>
1783
+** <dd>This option takes a single argument which is a pointer to
1784
+** an [sqlite3_pcache_methods] object. This object specifies the interface
1785
+** to a custom page cache implementation. SQLite makes a copy of the
1786
+** object and uses it for page cache memory allocations.</dd>
1787
+**
1788
+** <dt>SQLITE_CONFIG_GETPCACHE</dt>
1789
+** <dd>This option takes a single argument which is a pointer to an
1790
+** [sqlite3_pcache_methods] object. SQLite copies of the current
1791
+** page cache implementation into that object.</dd>
1792
+**
17791793
** </dl>
17801794
*/
17811795
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
17821796
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
17831797
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1787,12 +1801,14 @@
17871801
#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
17881802
#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
17891803
#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
17901804
#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
17911805
#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1792
-#define SQLITE_CONFIG_CHUNKALLOC 12 /* int threshold */
1806
+/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
17931807
#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
1808
+#define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
1809
+#define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
17941810
17951811
/*
17961812
** CAPI3REF: Configuration Options {H10170} <S20000>
17971813
** EXPERIMENTAL
17981814
**
@@ -2838,11 +2854,11 @@
28382854
#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
28392855
#define SQLITE_REINDEX 27 /* Index Name NULL */
28402856
#define SQLITE_ANALYZE 28 /* Table Name NULL */
28412857
#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
28422858
#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
2843
-#define SQLITE_FUNCTION 31 /* Function Name NULL */
2859
+#define SQLITE_FUNCTION 31 /* NULL Function Name */
28442860
#define SQLITE_COPY 0 /* No longer used */
28452861
28462862
/*
28472863
** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400>
28482864
** EXPERIMENTAL
@@ -4606,11 +4622,11 @@
46064622
**
46074623
** These functions are [deprecated]. In order to maintain
46084624
** backwards compatibility with older code, these functions continue
46094625
** to be supported. However, new applications should avoid
46104626
** the use of these functions. To help encourage people to avoid
4611
-** using these functions, we are not going to tell you want they do.
4627
+** using these functions, we are not going to tell you what they do.
46124628
*/
46134629
#ifndef SQLITE_OMIT_DEPRECATED
46144630
SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
46154631
SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
46164632
SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
@@ -7010,10 +7026,153 @@
70107026
** </dl>
70117027
*/
70127028
#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
70137029
#define SQLITE_STMTSTATUS_SORT 2
70147030
7031
+/*
7032
+** CAPI3REF: Custom Page Cache Object
7033
+** EXPERIMENTAL
7034
+**
7035
+** The sqlite3_pcache type is opaque. It is implemented by
7036
+** the pluggable module. The SQLite core has no knowledge of
7037
+** its size or internal structure and never deals with the
7038
+** sqlite3_pcache object except by holding and passing pointers
7039
+** to the object.
7040
+**
7041
+** See [sqlite3_pcache_methods] for additional information.
7042
+*/
7043
+typedef struct sqlite3_pcache sqlite3_pcache;
7044
+
7045
+/*
7046
+** CAPI3REF: Application Defined Page Cache.
7047
+** EXPERIMENTAL
7048
+**
7049
+** The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
7050
+** register an alternative page cache implementation by passing in an
7051
+** instance of the sqlite3_pcache_methods structure. The majority of the
7052
+** heap memory used by sqlite is used by the page cache to cache data read
7053
+** from, or ready to be written to, the database file. By implementing a
7054
+** custom page cache using this API, an application can control more
7055
+** precisely the amount of memory consumed by sqlite, the way in which
7056
+** said memory is allocated and released, and the policies used to
7057
+** determine exactly which parts of a database file are cached and for
7058
+** how long.
7059
+**
7060
+** The contents of the structure are copied to an internal buffer by sqlite
7061
+** within the call to [sqlite3_config].
7062
+**
7063
+** The xInit() method is called once for each call to [sqlite3_initialize()]
7064
+** (usually only once during the lifetime of the process). It is passed
7065
+** a copy of the sqlite3_pcache_methods.pArg value. It can be used to set
7066
+** up global structures and mutexes required by the custom page cache
7067
+** implementation. The xShutdown() method is called from within
7068
+** [sqlite3_shutdown()], if the application invokes this API. It can be used
7069
+** to clean up any outstanding resources before process shutdown, if required.
7070
+**
7071
+** The xCreate() method is used to construct a new cache instance. The
7072
+** first parameter, szPage, is the size in bytes of the pages that must
7073
+** be allocated by the cache. szPage will not be a power of two. The
7074
+** second argument, bPurgeable, is true if the cache being created will
7075
+** be used to cache database pages read from a file stored on disk, or
7076
+** false if it is used for an in-memory database. The cache implementation
7077
+** does not have to do anything special based on the value of bPurgeable,
7078
+** it is purely advisory.
7079
+**
7080
+** The xCachesize() method may be called at any time by SQLite to set the
7081
+** suggested maximum cache-size (number of pages stored by) the cache
7082
+** instance passed as the first argument. This is the value configured using
7083
+** the SQLite "[PRAGMA cache_size]" command. As with the bPurgeable parameter,
7084
+** the implementation is not required to do anything special with this
7085
+** value, it is advisory only.
7086
+**
7087
+** The xPagecount() method should return the number of pages currently
7088
+** stored in the cache supplied as an argument.
7089
+**
7090
+** The xFetch() method is used to fetch a page and return a pointer to it.
7091
+** A 'page', in this context, is a buffer of szPage bytes aligned at an
7092
+** 8-byte boundary. The page to be fetched is determined by the key. The
7093
+** mimimum key value is 1. After it has been retrieved using xFetch, the page
7094
+** is considered to be pinned.
7095
+**
7096
+** If the requested page is already in the page cache, then a pointer to
7097
+** the cached buffer should be returned with its contents intact. If the
7098
+** page is not already in the cache, then the expected behaviour of the
7099
+** cache is determined by the value of the createFlag parameter passed
7100
+** to xFetch, according to the following table:
7101
+**
7102
+** <table border=1 width=85% align=center>
7103
+** <tr><th>createFlag<th>Expected Behaviour
7104
+** <tr><td>0<td>NULL should be returned. No new cache entry is created.
7105
+** <tr><td>1<td>If createFlag is set to 1, this indicates that
7106
+** SQLite is holding pinned pages that can be unpinned
7107
+** by writing their contents to the database file (a
7108
+** relatively expensive operation). In this situation the
7109
+** cache implementation has two choices: it can return NULL,
7110
+** in which case SQLite will attempt to unpin one or more
7111
+** pages before re-requesting the same page, or it can
7112
+** allocate a new page and return a pointer to it. If a new
7113
+** page is allocated, then it must be completely zeroed before
7114
+** it is returned.
7115
+** <tr><td>2<td>If createFlag is set to 2, then SQLite is not holding any
7116
+** pinned pages associated with the specific cache passed
7117
+** as the first argument to xFetch() that can be unpinned. The
7118
+** cache implementation should attempt to allocate a new
7119
+** cache entry and return a pointer to it. Again, the new
7120
+** page should be zeroed before it is returned. If the xFetch()
7121
+** method returns NULL when createFlag==2, SQLite assumes that
7122
+** a memory allocation failed and returns SQLITE_NOMEM to the
7123
+** user.
7124
+** </table>
7125
+**
7126
+** xUnpin() is called by SQLite with a pointer to a currently pinned page
7127
+** as its second argument. If the third parameter, discard, is non-zero,
7128
+** then the page should be evicted from the cache. In this case SQLite
7129
+** assumes that the next time the page is retrieved from the cache using
7130
+** the xFetch() method, it will be zeroed. If the discard parameter is
7131
+** zero, then the page is considered to be unpinned. The cache implementation
7132
+** may choose to reclaim (free or recycle) unpinned pages at any time.
7133
+** SQLite assumes that next time the page is retrieved from the cache
7134
+** it will either be zeroed, or contain the same data that it did when it
7135
+** was unpinned.
7136
+**
7137
+** The cache is not required to perform any reference counting. A single
7138
+** call to xUnpin() unpins the page regardless of the number of prior calls
7139
+** to xFetch().
7140
+**
7141
+** The xRekey() method is used to change the key value associated with the
7142
+** page passed as the second argument from oldKey to newKey. If the cache
7143
+** previously contains an entry associated with newKey, it should be
7144
+** discarded. Any prior cache entry associated with newKey is guaranteed not
7145
+** to be pinned.
7146
+**
7147
+** When SQLite calls the xTruncate() method, the cache must discard all
7148
+** existing cache entries with page numbers (keys) greater than or equal
7149
+** to the value of the iLimit parameter passed to xTruncate(). If any
7150
+** of these pages are pinned, they are implicitly unpinned, meaning that
7151
+** they can be safely discarded.
7152
+**
7153
+** The xDestroy() method is used to delete a cache allocated by xCreate().
7154
+** All resources associated with the specified cache should be freed. After
7155
+** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
7156
+** handle invalid, and will not use it with any other sqlite3_pcache_methods
7157
+** functions.
7158
+*/
7159
+typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
7160
+struct sqlite3_pcache_methods {
7161
+ void *pArg;
7162
+ int (*xInit)(void*);
7163
+ void (*xShutdown)(void*);
7164
+ sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
7165
+ void (*xCachesize)(sqlite3_pcache*, int nCachesize);
7166
+ int (*xPagecount)(sqlite3_pcache*);
7167
+ void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
7168
+ void (*xUnpin)(sqlite3_pcache*, void*, int discard);
7169
+ void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
7170
+ void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
7171
+ void (*xDestroy)(sqlite3_pcache*);
7172
+};
7173
+
70157174
/*
70167175
** Undo the hack that converts floating point types to integer for
70177176
** builds on processors without floating point support.
70187177
*/
70197178
#ifdef SQLITE_OMIT_FLOATING_POINT
@@ -7479,11 +7638,11 @@
74797638
74807639
/*
74817640
** A convenience macro that returns the number of elements in
74827641
** an array.
74837642
*/
7484
-#define ArraySize(X) (sizeof(X)/sizeof(X[0]))
7643
+#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
74857644
74867645
/*
74877646
** The following value as a destructor means to use sqlite3DbFree().
74887647
** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
74897648
*/
@@ -7498,11 +7657,11 @@
74987657
** macro is used for this purpose. And instead of referencing the variable
74997658
** directly, we use its constant as a key to lookup the run-time allocated
75007659
** buffer that holds real variable. The constant is also the initializer
75017660
** for the run-time allocated buffer.
75027661
**
7503
-** In the usually case where WSD is supported, the SQLITE_WSD and GLOBAL
7662
+** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
75047663
** macros become no-ops and have zero performance impact.
75057664
*/
75067665
#ifdef SQLITE_OMIT_WSD
75077666
#define SQLITE_WSD const
75087667
#define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
@@ -7513,10 +7672,29 @@
75137672
#define SQLITE_WSD
75147673
#define GLOBAL(t,v) v
75157674
#define sqlite3GlobalConfig sqlite3Config
75167675
#endif
75177676
7677
+/*
7678
+** The following macros are used to suppress compiler warnings and to
7679
+** make it clear to human readers when a function parameter is deliberately
7680
+** left unused within the body of a function. This usually happens when
7681
+** a function is called via a function pointer. For example the
7682
+** implementation of an SQL aggregate step callback may not use the
7683
+** parameter indicating the number of arguments passed to the aggregate,
7684
+** if it knows that this is enforced elsewhere.
7685
+**
7686
+** When a function parameter is not used at all within the body of a function,
7687
+** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
7688
+** However, these macros may also be used to suppress warnings related to
7689
+** parameters that may or may not be used depending on compilation options.
7690
+** For example those parameters only used in assert() statements. In these
7691
+** cases the parameters are named as per the usual conventions.
7692
+*/
7693
+#define UNUSED_PARAMETER(x) (void)(x)
7694
+#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
7695
+
75187696
/*
75197697
** Forward references to structures
75207698
*/
75217699
typedef struct AggInfo AggInfo;
75227700
typedef struct AuthContext AuthContext;
@@ -8195,11 +8373,11 @@
81958373
*************************************************************************
81968374
** This header file defines the interface that the sqlite page cache
81978375
** subsystem. The page cache subsystem reads and writes a file a page
81988376
** at a time and provides a journal for rollback.
81998377
**
8200
-** @(#) $Id: pager.h,v 1.86 2008/10/17 18:51:53 danielk1977 Exp $
8378
+** @(#) $Id: pager.h,v 1.87 2008/11/19 10:22:33 danielk1977 Exp $
82018379
*/
82028380
82038381
#ifndef _PAGER_H_
82048382
#define _PAGER_H_
82058383
@@ -8255,11 +8433,11 @@
82558433
/*
82568434
** See source code comments for a detailed description of the following
82578435
** routines:
82588436
*/
82598437
SQLITE_PRIVATE int sqlite3PagerOpen(sqlite3_vfs *, Pager **ppPager, const char*, int,int,int);
8260
-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, BusyHandler *pBusyHandler);
8438
+SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
82618439
SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*));
82628440
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*);
82638441
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
82648442
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
82658443
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
@@ -8341,11 +8519,11 @@
83418519
**
83428520
*************************************************************************
83438521
** This header file defines the interface that the sqlite page cache
83448522
** subsystem.
83458523
**
8346
-** @(#) $Id: pcache.h,v 1.14 2008/10/17 18:51:53 danielk1977 Exp $
8524
+** @(#) $Id: pcache.h,v 1.16 2008/11/19 16:52:44 danielk1977 Exp $
83478525
*/
83488526
83498527
#ifndef _PCACHE_H_
83508528
83518529
typedef struct PgHdr PgHdr;
@@ -8363,29 +8541,23 @@
83638541
Pager *pPager; /* The pager this page is part of */
83648542
#ifdef SQLITE_CHECK_PAGES
83658543
u32 pageHash; /* Hash of page content */
83668544
#endif
83678545
u16 flags; /* PGHDR flags defined below */
8546
+
83688547
/**********************************************************************
83698548
** Elements above are public. All that follows is private to pcache.c
83708549
** and should not be accessed by other modules.
83718550
*/
83728551
i16 nRef; /* Number of users of this page */
83738552
PCache *pCache; /* Cache that owns this page */
83748553
8375
- /**********************************************************************
8376
- ** Elements above are accessible at any time by the owner of the cache
8377
- ** without the need for a mutex. The elements that follow can only be
8378
- ** accessed while holding the SQLITE_MUTEX_STATIC_LRU mutex.
8379
- */
8380
- PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
8381
- PgHdr *pNext, *pPrev; /* List of clean or dirty pages */
8382
- PgHdr *pNextLru, *pPrevLru; /* Part of global LRU list */
8554
+ PgHdr *pDirtyNext; /* Next element in list of dirty pages */
8555
+ PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
83838556
};
83848557
83858558
/* Bit values for PgHdr.flags */
8386
-#define PGHDR_IN_JOURNAL 0x001 /* Page is in rollback journal */
83878559
#define PGHDR_DIRTY 0x002 /* Page has changed */
83888560
#define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
83898561
** writing this page to the database */
83908562
#define PGHDR_NEED_READ 0x008 /* Content is unread */
83918563
#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
@@ -8397,12 +8569,10 @@
83978569
83988570
/* Page cache buffer management:
83998571
** These routines implement SQLITE_CONFIG_PAGECACHE.
84008572
*/
84018573
SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
8402
-SQLITE_PRIVATE void *sqlite3PCacheMalloc(int sz);
8403
-SQLITE_PRIVATE void sqlite3PCacheFree(void*);
84048574
84058575
/* Create a new pager cache.
84068576
** Under memory stress, invoke xStress to try to make pages clean.
84078577
** Only clean and unpinned pages can be reclaimed.
84088578
*/
@@ -8445,21 +8615,11 @@
84458615
84468616
/* Reset and close the cache object */
84478617
SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
84488618
84498619
/* Clear flags from pages of the page cache */
8450
-SQLITE_PRIVATE void sqlite3PcacheClearFlags(PCache*, int mask);
8451
-
8452
-/* Assert flags settings on all pages. Debugging only */
8453
-#ifndef NDEBUG
8454
-SQLITE_PRIVATE void sqlite3PcacheAssertFlags(PCache*, int trueMask, int falseMask);
8455
-#else
8456
-# define sqlite3PcacheAssertFlags(A,B,C)
8457
-#endif
8458
-
8459
-/* Return true if the number of dirty pages is 0 or 1 */
8460
-SQLITE_PRIVATE int sqlite3PcacheZeroOrOneDirtyPages(PCache*);
8620
+SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
84618621
84628622
/* Discard the contents of the cache */
84638623
SQLITE_PRIVATE int sqlite3PcacheClear(PCache*);
84648624
84658625
/* Return the total number of outstanding page references */
@@ -8472,34 +8632,38 @@
84728632
84738633
/* Return the total number of pages stored in the cache */
84748634
SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
84758635
84768636
#ifdef SQLITE_CHECK_PAGES
8477
-/* Iterate through all pages currently stored in the cache. This interface
8478
-** is only available if SQLITE_CHECK_PAGES is defined when the library is
8479
-** built.
8637
+/* Iterate through all dirty pages currently stored in the cache. This
8638
+** interface is only available if SQLITE_CHECK_PAGES is defined when the
8639
+** library is built.
84808640
*/
8481
-SQLITE_PRIVATE void sqlite3PcacheIterate(PCache *pCache, void (*xIter)(PgHdr *));
8641
+SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
84828642
#endif
84838643
84848644
/* Set and get the suggested cache-size for the specified pager-cache.
84858645
**
84868646
** If no global maximum is configured, then the system attempts to limit
84878647
** the total number of pages cached by purgeable pager-caches to the sum
84888648
** of the suggested cache-sizes.
84898649
*/
8490
-SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
84918650
SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
8651
+#ifdef SQLITE_TEST
8652
+SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8653
+#endif
84928654
84938655
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
84948656
/* Try to return memory used by the pcache module to the main memory heap */
84958657
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
84968658
#endif
84978659
84988660
#ifdef SQLITE_TEST
84998661
SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
85008662
#endif
8663
+
8664
+SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
85018665
85028666
#endif /* _PCACHE_H_ */
85038667
85048668
/************** End of pcache.h **********************************************/
85058669
/************** Continuing where we left off in sqliteInt.h ******************/
@@ -9017,11 +9181,11 @@
90179181
int nextPagesize; /* Pagesize after VACUUM if >0 */
90189182
int nTable; /* Number of tables in the database */
90199183
CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
90209184
i64 lastRowid; /* ROWID of most recent insert (see above) */
90219185
i64 priorNewRowid; /* Last randomly generated ROWID */
9022
- int magic; /* Magic number for detect library misuse */
9186
+ u32 magic; /* Magic number for detect library misuse */
90239187
int nChange; /* Value returned by sqlite3_changes() */
90249188
int nTotalChange; /* Value returned by sqlite3_total_changes() */
90259189
sqlite3_mutex *mutex; /* Connection mutex */
90269190
int aLimit[SQLITE_N_LIMIT]; /* Limits */
90279191
struct sqlite3InitInfo { /* Information used during initialization */
@@ -9180,17 +9344,17 @@
91809344
** available as the function user-data (sqlite3_user_data()). The
91819345
** FuncDef.flags variable is set to the value passed as the flags
91829346
** parameter.
91839347
*/
91849348
#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
9185
- {nArg, SQLITE_UTF8, bNC*8, SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName}
9349
+ {nArg, SQLITE_UTF8, bNC*8, SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0}
91869350
#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
9187
- {nArg, SQLITE_UTF8, bNC*8, pArg, 0, xFunc, 0, 0, #zName}
9351
+ {nArg, SQLITE_UTF8, bNC*8, pArg, 0, xFunc, 0, 0, #zName, 0}
91889352
#define LIKEFUNC(zName, nArg, arg, flags) \
9189
- {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName}
9353
+ {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0}
91909354
#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
9191
- {nArg, SQLITE_UTF8, nc*8, SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal, #zName}
9355
+ {nArg, SQLITE_UTF8, nc*8, SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0}
91929356
91939357
91949358
/*
91959359
** Each SQLite module (virtual table definition) is defined by an
91969360
** instance of the following structure, stored in the sqlite3.aModule
@@ -9755,10 +9919,15 @@
97559919
** tables in a join to 32 instead of 64. But it also reduces the size
97569920
** of the library by 738 bytes on ix86.
97579921
*/
97589922
typedef u64 Bitmask;
97599923
9924
+/*
9925
+** The number of bits in a Bitmask. "BMS" means "BitMask Size".
9926
+*/
9927
+#define BMS ((int)(sizeof(Bitmask)*8))
9928
+
97609929
/*
97619930
** The following structure describes the FROM clause of a SELECT statement.
97629931
** Each table or subquery in the FROM clause is a separate element of
97639932
** the SrcList.a[] array.
97649933
**
@@ -10290,10 +10459,11 @@
1029010459
int mxStrlen; /* Maximum string length */
1029110460
int szLookaside; /* Default lookaside buffer size */
1029210461
int nLookaside; /* Default lookaside buffer count */
1029310462
sqlite3_mem_methods m; /* Low-level memory allocation interface */
1029410463
sqlite3_mutex_methods mutex; /* Low-level mutex interface */
10464
+ sqlite3_pcache_methods pcache; /* Low-level page-cache interface */
1029510465
void *pHeap; /* Heap storage space */
1029610466
int nHeap; /* Size of pHeap[] */
1029710467
int mnReq, mxReq; /* Min and max heap requests sizes */
1029810468
void *pScratch; /* Scratch memory */
1029910469
int szScratch; /* Size of each scratch buffer */
@@ -10304,11 +10474,10 @@
1030410474
int isInit; /* True after initialization has finished */
1030510475
int inProgress; /* True while initialization in progress */
1030610476
int isMallocInit; /* True after malloc is initialized */
1030710477
sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
1030810478
int nRefInitMutex; /* Number of users of pInitMutex */
10309
- int nSmall; /* alloc size threshold used by mem6.c */
1031010479
int mxParserStack; /* maximum depth of the parser stack */
1031110480
int sharedCacheEnabled; /* true if shared-cache mode enabled */
1031210481
};
1031310482
1031410483
/*
@@ -10387,14 +10556,20 @@
1038710556
SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
1038810557
SQLITE_PRIVATE void sqlite3ScratchFree(void*);
1038910558
SQLITE_PRIVATE void *sqlite3PageMalloc(int);
1039010559
SQLITE_PRIVATE void sqlite3PageFree(void*);
1039110560
SQLITE_PRIVATE void sqlite3MemSetDefault(void);
10392
-SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
10393
-SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
1039410561
SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
1039510562
SQLITE_PRIVATE int sqlite3MemoryAlarm(void (*)(void*, sqlite3_int64, int), void*, sqlite3_int64);
10563
+
10564
+#ifdef SQLITE_ENABLE_MEMSYS3
10565
+SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
10566
+#endif
10567
+#ifdef SQLITE_ENABLE_MEMSYS5
10568
+SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
10569
+#endif
10570
+
1039610571
1039710572
#ifndef SQLITE_MUTEX_OMIT
1039810573
SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void);
1039910574
SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
1040010575
SQLITE_PRIVATE int sqlite3MutexInit(void);
@@ -10477,10 +10652,11 @@
1047710652
SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
1047810653
SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
1047910654
SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
1048010655
SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
1048110656
SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
10657
+SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
1048210658
SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
1048310659
SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
1048410660
Token*, Select*, Expr*, IdList*);
1048510661
SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
1048610662
SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
@@ -10549,11 +10725,11 @@
1054910725
SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int);
1055010726
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
1055110727
SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
1055210728
SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
1055310729
int*,int,int,int,int);
10554
-SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*,int,int,int,int);
10730
+SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
1055510731
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
1055610732
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
1055710733
SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*);
1055810734
SQLITE_PRIVATE void sqlite3TokenCopy(sqlite3*,Token*, Token*);
1055910735
SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*);
@@ -10563,11 +10739,10 @@
1056310739
SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
1056410740
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
1056510741
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
1056610742
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
1056710743
SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
10568
-SQLITE_PRIVATE int sqlite3GetBuiltinFunction(const char *, int, FuncDef **);
1056910744
#ifdef SQLITE_DEBUG
1057010745
SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*);
1057110746
SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3*);
1057210747
#else
1057310748
# define sqlite3SafetyOn(A) 0
@@ -10585,11 +10760,11 @@
1058510760
SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
1058610761
Expr*,int, int);
1058710762
SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
1058810763
SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
1058910764
SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
10590
-SQLITE_PRIVATE int sqlite3TriggersExist(Parse*, Table*, int, ExprList*);
10765
+SQLITE_PRIVATE int sqlite3TriggersExist(Table*, int, ExprList*);
1059110766
SQLITE_PRIVATE int sqlite3CodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int,
1059210767
int, int, u32*, u32*);
1059310768
void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
1059410769
SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
1059510770
SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
@@ -10598,11 +10773,11 @@
1059810773
SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, int);
1059910774
SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
1060010775
SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
1060110776
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
1060210777
#else
10603
-# define sqlite3TriggersExist(A,B,C,D,E,F) 0
10778
+# define sqlite3TriggersExist(B,C,D,E,F) 0
1060410779
# define sqlite3DeleteTrigger(A,B)
1060510780
# define sqlite3DropTriggerPtr(A,B)
1060610781
# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
1060710782
# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I,J,K) 0
1060810783
#endif
@@ -10666,12 +10841,12 @@
1066610841
**
1066710842
** x = getVarint32( A, B );
1066810843
** x = putVarint32( A, B );
1066910844
**
1067010845
*/
10671
-#define getVarint32(A,B) ((*(A)<(unsigned char)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), &(B)))
10672
-#define putVarint32(A,B) (((B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
10846
+#define getVarint32(A,B) ((*(A)<(unsigned char)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
10847
+#define putVarint32(A,B) (((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
1067310848
#define getVarint sqlite3GetVarint
1067410849
#define putVarint sqlite3PutVarint
1067510850
1067610851
1067710852
SQLITE_PRIVATE void sqlite3IndexAffinityStr(Vdbe *, Index *);
@@ -10778,15 +10953,17 @@
1077810953
#ifdef SQLITE_OMIT_VIRTUALTABLE
1077910954
# define sqlite3VtabClear(X)
1078010955
# define sqlite3VtabSync(X,Y) SQLITE_OK
1078110956
# define sqlite3VtabRollback(X)
1078210957
# define sqlite3VtabCommit(X)
10958
+# define sqlite3VtabInSync(db) 0
1078310959
#else
1078410960
SQLITE_PRIVATE void sqlite3VtabClear(Table*);
1078510961
SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
1078610962
SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
1078710963
SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
10964
+# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
1078810965
#endif
1078910966
SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
1079010967
SQLITE_PRIVATE void sqlite3VtabLock(sqlite3_vtab*);
1079110968
SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3*, sqlite3_vtab*);
1079210969
SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
@@ -11107,11 +11284,11 @@
1110711284
**
1110811285
** There is only one exported symbol in this file - the function
1110911286
** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
1111011287
** All other code has file scope.
1111111288
**
11112
-** $Id: date.c,v 1.92 2008/10/13 15:35:09 drh Exp $
11289
+** $Id: date.c,v 1.94 2008/11/19 09:05:27 danielk1977 Exp $
1111311290
**
1111411291
** SQLite processes all times and dates as Julian Day numbers. The
1111511292
** dates and times are stored as the number of days since noon
1111611293
** in Greenwich on November 24, 4714 B.C. according to the Gregorian
1111711294
** calendar system.
@@ -11609,11 +11786,11 @@
1160911786
int rc = 1;
1161011787
int n;
1161111788
double r;
1161211789
char *z, zBuf[30];
1161311790
z = zBuf;
11614
- for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){
11791
+ for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
1161511792
z[n] = tolower(zMod[n]);
1161611793
}
1161711794
z[n] = 0;
1161811795
switch( z[0] ){
1161911796
#ifndef SQLITE_OMIT_LOCALTIME
@@ -11977,11 +12154,11 @@
1197712154
i++;
1197812155
}
1197912156
}
1198012157
if( n<sizeof(zBuf) ){
1198112158
z = zBuf;
11982
- }else if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
12159
+ }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
1198312160
sqlite3_result_error_toobig(context);
1198412161
return;
1198512162
}else{
1198612163
z = sqlite3DbMallocRaw(db, n);
1198712164
if( z==0 ){
@@ -12056,13 +12233,14 @@
1205612233
**
1205712234
** This function returns the same value as time('now').
1205812235
*/
1205912236
static void ctimeFunc(
1206012237
sqlite3_context *context,
12061
- int argc,
12062
- sqlite3_value **argv
12238
+ int NotUsed,
12239
+ sqlite3_value **NotUsed2
1206312240
){
12241
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
1206412242
timeFunc(context, 0, 0);
1206512243
}
1206612244
1206712245
/*
1206812246
** current_date()
@@ -12069,13 +12247,14 @@
1206912247
**
1207012248
** This function returns the same value as date('now').
1207112249
*/
1207212250
static void cdateFunc(
1207312251
sqlite3_context *context,
12074
- int argc,
12075
- sqlite3_value **argv
12252
+ int NotUsed,
12253
+ sqlite3_value **NotUsed2
1207612254
){
12255
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
1207712256
dateFunc(context, 0, 0);
1207812257
}
1207912258
1208012259
/*
1208112260
** current_timestamp()
@@ -12082,13 +12261,14 @@
1208212261
**
1208312262
** This function returns the same value as datetime('now').
1208412263
*/
1208512264
static void ctimestampFunc(
1208612265
sqlite3_context *context,
12087
- int argc,
12088
- sqlite3_value **argv
12266
+ int NotUsed,
12267
+ sqlite3_value **NotUsed2
1208912268
){
12269
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
1209012270
datetimeFunc(context, 0, 0);
1209112271
}
1209212272
#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
1209312273
1209412274
#ifdef SQLITE_OMIT_DATETIME_FUNCS
@@ -12623,11 +12803,11 @@
1262312803
** to obtain the memory it needs.
1262412804
**
1262512805
** This file contains implementations of the low-level memory allocation
1262612806
** routines specified in the sqlite3_mem_methods object.
1262712807
**
12628
-** $Id: mem1.c,v 1.27 2008/10/28 18:58:20 drh Exp $
12808
+** $Id: mem1.c,v 1.28 2008/11/19 09:05:27 danielk1977 Exp $
1262912809
*/
1263012810
1263112811
/*
1263212812
** This version of the memory allocator is the default. It is
1263312813
** used when no other memory allocator is specified using compile-time
@@ -12715,17 +12895,19 @@
1271512895
1271612896
/*
1271712897
** Initialize this module.
1271812898
*/
1271912899
static int sqlite3MemInit(void *NotUsed){
12900
+ UNUSED_PARAMETER(NotUsed);
1272012901
return SQLITE_OK;
1272112902
}
1272212903
1272312904
/*
1272412905
** Deinitialize this module.
1272512906
*/
1272612907
static void sqlite3MemShutdown(void *NotUsed){
12908
+ UNUSED_PARAMETER(NotUsed);
1272712909
return;
1272812910
}
1272912911
1273012912
/*
1273112913
** This routine is the only routine in this file with external linkage.
@@ -13213,11 +13395,11 @@
1321313395
** be changed.
1321413396
**
1321513397
** This version of the memory allocation subsystem is included
1321613398
** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
1321713399
**
13218
-** $Id: mem3.c,v 1.23 2008/09/02 17:52:52 danielk1977 Exp $
13400
+** $Id: mem3.c,v 1.25 2008/11/19 16:52:44 danielk1977 Exp $
1321913401
*/
1322013402
1322113403
/*
1322213404
** This version of the memory allocator is only built into the library
1322313405
** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
@@ -13440,11 +13622,11 @@
1344013622
/*
1344113623
** Chunk i is a free chunk that has been unlinked. Adjust its
1344213624
** size parameters for check-out and return a pointer to the
1344313625
** user portion of the chunk.
1344413626
*/
13445
-static void *memsys3Checkout(u32 i, int nBlock){
13627
+static void *memsys3Checkout(u32 i, u32 nBlock){
1344613628
u32 x;
1344713629
assert( sqlite3_mutex_held(mem3.mutex) );
1344813630
assert( i>=1 );
1344913631
assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
1345013632
assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
@@ -13458,11 +13640,11 @@
1345813640
/*
1345913641
** Carve a piece off of the end of the mem3.iMaster free chunk.
1346013642
** Return a pointer to the new allocation. Or, if the master chunk
1346113643
** is not large enough, return 0.
1346213644
*/
13463
-static void *memsys3FromMaster(int nBlock){
13645
+static void *memsys3FromMaster(u32 nBlock){
1346413646
assert( sqlite3_mutex_held(mem3.mutex) );
1346513647
assert( mem3.szMaster>=nBlock );
1346613648
if( nBlock>=mem3.szMaster-1 ){
1346713649
/* Use the entire master */
1346813650
void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
@@ -13544,12 +13726,12 @@
1354413726
** This function assumes that the necessary mutexes, if any, are
1354513727
** already held by the caller. Hence "Unsafe".
1354613728
*/
1354713729
static void *memsys3MallocUnsafe(int nByte){
1354813730
u32 i;
13549
- int nBlock;
13550
- int toFree;
13731
+ u32 nBlock;
13732
+ u32 toFree;
1355113733
1355213734
assert( sqlite3_mutex_held(mem3.mutex) );
1355313735
assert( sizeof(Mem3Block)==8 );
1355413736
if( nByte<=12 ){
1355513737
nBlock = 2;
@@ -13741,10 +13923,11 @@
1374113923
1374213924
/*
1374313925
** Initialize this module.
1374413926
*/
1374513927
static int memsys3Init(void *NotUsed){
13928
+ UNUSED_PARAMETER(NotUsed);
1374613929
if( !sqlite3GlobalConfig.pHeap ){
1374713930
return SQLITE_ERROR;
1374813931
}
1374913932
1375013933
/* Store a pointer to the memory block in global structure mem3. */
@@ -13765,10 +13948,11 @@
1376513948
1376613949
/*
1376713950
** Deinitialize this module.
1376813951
*/
1376913952
static void memsys3Shutdown(void *NotUsed){
13953
+ UNUSED_PARAMETER(NotUsed);
1377013954
return;
1377113955
}
1377213956
1377313957
1377413958
@@ -13777,11 +13961,11 @@
1377713961
** allocations into that log.
1377813962
*/
1377913963
SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
1378013964
#ifdef SQLITE_DEBUG
1378113965
FILE *out;
13782
- int i, j;
13966
+ u32 i, j;
1378313967
u32 size;
1378413968
if( zFilename==0 || zFilename[0]==0 ){
1378513969
out = stdout;
1378613970
}else{
1378713971
out = fopen(zFilename, "w");
@@ -13842,10 +14026,12 @@
1384214026
if( out==stdout ){
1384314027
fflush(stdout);
1384414028
}else{
1384514029
fclose(out);
1384614030
}
14031
+#else
14032
+ UNUSED_PARAMETER(zFilename);
1384714033
#endif
1384814034
}
1384914035
1385014036
/*
1385114037
** This routine is the only routine in this file with external
@@ -13899,11 +14085,11 @@
1389914085
** be changed.
1390014086
**
1390114087
** This version of the memory allocation subsystem is included
1390214088
** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
1390314089
**
13904
-** $Id: mem5.c,v 1.15 2008/10/28 18:58:20 drh Exp $
14090
+** $Id: mem5.c,v 1.19 2008/11/19 16:52:44 danielk1977 Exp $
1390514091
*/
1390614092
1390714093
/*
1390814094
** This version of the memory allocator is used only when
1390914095
** SQLITE_ENABLE_MEMSYS5 is defined.
@@ -14083,16 +14269,15 @@
1408314269
int iFullSz; /* Size of allocation rounded up to power of 2 */
1408414270
int iLogsize; /* Log2 of iFullSz/POW2_MIN */
1408514271
1408614272
/* Keep track of the maximum allocation request. Even unfulfilled
1408714273
** requests are counted */
14088
- if( nByte>mem5.maxRequest ){
14274
+ if( (u32)nByte>mem5.maxRequest ){
1408914275
mem5.maxRequest = nByte;
1409014276
}
1409114277
1409214278
/* Round nByte up to the next valid power of two */
14093
- if( nByte>POW2_MAX ) return 0;
1409414279
for(iFullSz=mem5.nAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
1409514280
1409614281
/* Make sure mem5.aiFreelist[iLogsize] contains at least one free
1409714282
** block. If not, then split a block of the next larger power of
1409814283
** two in order to create a new free block of size iLogsize.
@@ -14140,16 +14325,16 @@
1414014325
assert( ((u8 *)pOld-mem5.zPool)%mem5.nAtom==0 );
1414114326
assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
1414214327
1414314328
iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
1414414329
size = 1<<iLogsize;
14145
- assert( iBlock+size-1<mem5.nBlock );
14330
+ assert( iBlock+size-1<(u32)mem5.nBlock );
1414614331
1414714332
mem5.aCtrl[iBlock] |= CTRL_FREE;
1414814333
mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
1414914334
assert( mem5.currentCount>0 );
14150
- assert( mem5.currentOut>=0 );
14335
+ assert( mem5.currentOut>=(size*mem5.nAtom) );
1415114336
mem5.currentCount--;
1415214337
mem5.currentOut -= size*mem5.nAtom;
1415314338
assert( mem5.currentOut>0 || mem5.currentCount==0 );
1415414339
assert( mem5.currentCount>0 || mem5.currentOut==0 );
1415514340
@@ -14255,17 +14440,19 @@
1425514440
int nByte = sqlite3GlobalConfig.nHeap;
1425614441
u8 *zByte = (u8 *)sqlite3GlobalConfig.pHeap;
1425714442
int nMinLog; /* Log of minimum allocation size in bytes*/
1425814443
int iOffset;
1425914444
14445
+ UNUSED_PARAMETER(NotUsed);
14446
+
1426014447
if( !zByte ){
1426114448
return SQLITE_ERROR;
1426214449
}
1426314450
1426414451
nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
1426514452
mem5.nAtom = (1<<nMinLog);
14266
- while( sizeof(Mem5Link)>mem5.nAtom ){
14453
+ while( (int)sizeof(Mem5Link)>mem5.nAtom ){
1426714454
mem5.nAtom = mem5.nAtom << 1;
1426814455
}
1426914456
1427014457
mem5.nBlock = (nByte / (mem5.nAtom+sizeof(u8)));
1427114458
mem5.zPool = zByte;
@@ -14291,10 +14478,11 @@
1429114478
1429214479
/*
1429314480
** Deinitialize this module.
1429414481
*/
1429514482
static void memsys5Shutdown(void *NotUsed){
14483
+ UNUSED_PARAMETER(NotUsed);
1429614484
return;
1429714485
}
1429814486
1429914487
/*
1430014488
** Open the file indicated and write a log of all unfreed memory
@@ -14334,10 +14522,12 @@
1433414522
if( out==stdout ){
1433514523
fflush(stdout);
1433614524
}else{
1433714525
fclose(out);
1433814526
}
14527
+#else
14528
+ UNUSED_PARAMETER(zFilename);
1433914529
#endif
1434014530
}
1434114531
1434214532
/*
1434314533
** This routine is the only routine in this file with external
@@ -14985,11 +15175,11 @@
1498515175
** May you share freely, never taking more than you give.
1498615176
**
1498715177
*************************************************************************
1498815178
** This file contains the C functions that implement mutexes for pthreads
1498915179
**
14990
-** $Id: mutex_unix.c,v 1.13 2008/07/16 12:33:24 drh Exp $
15180
+** $Id: mutex_unix.c,v 1.15 2008/11/17 19:18:55 danielk1977 Exp $
1499115181
*/
1499215182
1499315183
/*
1499415184
** The code in this file is only used if we are compiling threadsafe
1499515185
** under unix with pthreads.
@@ -15034,11 +15224,11 @@
1503415224
** On those platforms where pthread_equal() is not atomic, SQLite
1503515225
** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
1503615226
** make sure no assert() statements are evaluated and hence these
1503715227
** routines are never called.
1503815228
*/
15039
-#ifndef NDEBUG
15229
+#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
1504015230
static int pthreadMutexHeld(sqlite3_mutex *p){
1504115231
return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
1504215232
}
1504315233
static int pthreadMutexNotheld(sqlite3_mutex *p){
1504415234
return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
@@ -15130,11 +15320,11 @@
1513015320
}
1513115321
break;
1513215322
}
1513315323
default: {
1513415324
assert( iType-2 >= 0 );
15135
- assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
15325
+ assert( iType-2 < ArraySize(staticMutexes) );
1513615326
p = &staticMutexes[iType-2];
1513715327
p->id = iType;
1513815328
break;
1513915329
}
1514015330
}
@@ -15312,11 +15502,11 @@
1531215502
** May you share freely, never taking more than you give.
1531315503
**
1531415504
*************************************************************************
1531515505
** This file contains the C functions that implement mutexes for win32
1531615506
**
15317
-** $Id: mutex_w32.c,v 1.11 2008/06/26 10:41:19 danielk1977 Exp $
15507
+** $Id: mutex_w32.c,v 1.12 2008/11/10 20:01:41 shane Exp $
1531815508
*/
1531915509
1532015510
/*
1532115511
** The code in this file is only used if we are compiling multithreaded
1532215512
** on a win32 system.
@@ -15341,11 +15531,18 @@
1534115531
** the LockFileEx() API. But we can still statically link against that
1534215532
** API as long as we don't call it win running Win95/98/ME. A call to
1534315533
** this routine is used to determine if the host is Win95/98/ME or
1534415534
** WinNT/2K/XP so that we will know whether or not we can safely call
1534515535
** the LockFileEx() API.
15536
+**
15537
+** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
15538
+** which is only available if your application was compiled with
15539
+** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
15540
+** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
15541
+** this out as well.
1534615542
*/
15543
+#if 0
1534715544
#if SQLITE_OS_WINCE
1534815545
# define mutexIsNT() (1)
1534915546
#else
1535015547
static int mutexIsNT(void){
1535115548
static int osType = 0;
@@ -15356,11 +15553,11 @@
1535615553
osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
1535715554
}
1535815555
return osType==2;
1535915556
}
1536015557
#endif /* SQLITE_OS_WINCE */
15361
-
15558
+#endif
1536215559
1536315560
#ifdef SQLITE_DEBUG
1536415561
/*
1536515562
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
1536615563
** intended for use only inside assert() statements.
@@ -15559,23 +15756,24 @@
1555915756
**
1556015757
*************************************************************************
1556115758
**
1556215759
** Memory allocation functions used throughout sqlite.
1556315760
**
15564
-** $Id: malloc.c,v 1.45 2008/10/12 00:27:53 shane Exp $
15761
+** $Id: malloc.c,v 1.48 2008/11/19 09:05:27 danielk1977 Exp $
1556515762
*/
1556615763
1556715764
/*
1556815765
** This routine runs when the memory allocator sees that the
1556915766
** total memory allocation is about to exceed the soft heap
1557015767
** limit.
1557115768
*/
1557215769
static void softHeapLimitEnforcer(
1557315770
void *NotUsed,
15574
- sqlite3_int64 inUse,
15771
+ sqlite3_int64 NotUsed2,
1557515772
int allocSize
1557615773
){
15774
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
1557715775
sqlite3_release_memory(allocSize);
1557815776
}
1557915777
1558015778
/*
1558115779
** Set the soft heap-size limit for the library. Passing a zero or
@@ -15613,10 +15811,11 @@
1561315811
nRet += sqlite3VdbeReleaseMemory(n);
1561415812
#endif
1561515813
nRet += sqlite3PcacheReleaseMemory(n-nRet);
1561615814
return nRet;
1561715815
#else
15816
+ UNUSED_PARAMETER(n);
1561815817
return SQLITE_OK;
1561915818
#endif
1562015819
}
1562115820
1562215821
/*
@@ -15646,11 +15845,11 @@
1564615845
** sqlite3GlobalConfig.pPage to a block of memory that records
1564715846
** which pages are available.
1564815847
*/
1564915848
u32 *aScratchFree;
1565015849
u32 *aPageFree;
15651
-} mem0 = { 62560955 };
15850
+} mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 };
1565215851
1565315852
#define mem0 GLOBAL(struct Mem0Global, mem0)
1565415853
1565515854
/*
1565615855
** Initialize the memory allocation subsystem.
@@ -15931,11 +16130,11 @@
1593116130
int i;
1593216131
i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
1593316132
i /= sqlite3GlobalConfig.szScratch;
1593416133
assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
1593516134
sqlite3_mutex_enter(mem0.mutex);
15936
- assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch );
16135
+ assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
1593716136
mem0.aScratchFree[mem0.nScratchFree++] = i;
1593816137
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
1593916138
sqlite3_mutex_leave(mem0.mutex);
1594016139
}
1594116140
}
@@ -16339,11 +16538,11 @@
1633916538
** the public domain. The original comments are included here for
1634016539
** completeness. They are very out-of-date but might be useful as
1634116540
** an historical reference. Most of the "enhancements" have been backed
1634216541
** out so that the functionality is now the same as standard printf().
1634316542
**
16344
-** $Id: printf.c,v 1.94 2008/08/22 14:08:36 drh Exp $
16543
+** $Id: printf.c,v 1.96 2008/11/20 18:20:28 drh Exp $
1634516544
**
1634616545
**************************************************************************
1634716546
**
1634816547
** The following modules is an enhanced replacement for the "printf" subroutines
1634916548
** found in the standard C library. The following enhancements are
@@ -16470,11 +16669,10 @@
1647016669
{ 'p', 16, 0, etPOINTER, 0, 1 },
1647116670
{ 'T', 0, 2, etTOKEN, 0, 0 },
1647216671
{ 'S', 0, 2, etSRCLIST, 0, 0 },
1647316672
{ 'r', 10, 3, etORDINAL, 0, 0 },
1647416673
};
16475
-#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
1647616674
1647716675
/*
1647816676
** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
1647916677
** conversions will work.
1648016678
*/
@@ -16507,11 +16705,11 @@
1650716705
/*
1650816706
** Append N space characters to the given string buffer.
1650916707
*/
1651016708
static void appendSpace(StrAccum *pAccum, int N){
1651116709
static const char zSpaces[] = " ";
16512
- while( N>=sizeof(zSpaces)-1 ){
16710
+ while( N>=(int)sizeof(zSpaces)-1 ){
1651316711
sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
1651416712
N -= sizeof(zSpaces)-1;
1651516713
}
1651616714
if( N>0 ){
1651716715
sqlite3StrAccumAppend(pAccum, zSpaces, N);
@@ -16670,11 +16868,11 @@
1667016868
}else{
1667116869
flag_long = flag_longlong = 0;
1667216870
}
1667316871
/* Fetch the info entry for the field */
1667416872
infop = 0;
16675
- for(idx=0; idx<etNINFO; idx++){
16873
+ for(idx=0; idx<ArraySize(fmtinfo); idx++){
1667616874
if( c==fmtinfo[idx].fmttype ){
1667716875
infop = &fmtinfo[idx];
1667816876
if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
1667916877
xtype = infop->type;
1668016878
}else{
@@ -16979,11 +17177,14 @@
1697917177
}
1698017178
needQuote = !isnull && xtype==etSQLESCAPE2;
1698117179
n += i + 1 + needQuote*2;
1698217180
if( n>etBUFSIZE ){
1698317181
bufpt = zExtra = sqlite3Malloc( n );
16984
- if( bufpt==0 ) return;
17182
+ if( bufpt==0 ){
17183
+ pAccum->mallocFailed = 1;
17184
+ return;
17185
+ }
1698517186
}else{
1698617187
bufpt = buf;
1698717188
}
1698817189
j = 0;
1698917190
if( needQuote ) bufpt[j++] = q;
@@ -17471,11 +17672,11 @@
1747117672
** VDBE. This information used to all be at the top of the single
1747217673
** source code file "vdbe.c". When that file became too big (over
1747317674
** 6000 lines long) it was split up into several smaller files and
1747417675
** this header information was factored out.
1747517676
**
17476
-** $Id: vdbeInt.h,v 1.157 2008/11/05 16:37:35 drh Exp $
17677
+** $Id: vdbeInt.h,v 1.158 2008/11/17 15:31:48 danielk1977 Exp $
1747717678
*/
1747817679
#ifndef _VDBEINT_H_
1747917680
#define _VDBEINT_H_
1748017681
1748117682
/*
@@ -17752,11 +17953,11 @@
1775217953
VdbeCursor **apCsr; /* One element of this array for each open cursor */
1775317954
int nVar; /* Number of entries in aVar[] */
1775417955
Mem *aVar; /* Values for the OP_Variable opcode. */
1775517956
char **azVar; /* Name of variables */
1775617957
int okVar; /* True if azVar[] has been initialized */
17757
- int magic; /* Magic number for sanity checking */
17958
+ u32 magic; /* Magic number for sanity checking */
1775817959
int nMem; /* Number of memory locations currently allocated */
1775917960
Mem *aMem; /* The memory locations */
1776017961
int nCallback; /* Number of callbacks invoked so far */
1776117962
int cacheCtr; /* VdbeCursor row cache generation counter */
1776217963
Fifo sFifo; /* A list of ROWIDs */
@@ -18392,11 +18593,11 @@
1839218593
** Utility functions used throughout sqlite.
1839318594
**
1839418595
** This file contains functions for allocating memory, comparing
1839518596
** strings, and stuff like that.
1839618597
**
18397
-** $Id: util.c,v 1.241 2008/07/28 19:34:54 drh Exp $
18598
+** $Id: util.c,v 1.242 2008/11/17 19:18:55 danielk1977 Exp $
1839818599
*/
1839918600
1840018601
1840118602
/*
1840218603
** Return true if the floating point value is Not a Number (NaN).
@@ -18432,11 +18633,11 @@
1843218633
** to exceed the SQLITE_LIMIT_LENGTH setting.
1843318634
*/
1843418635
SQLITE_PRIVATE int sqlite3Strlen(sqlite3 *db, const char *z){
1843518636
const char *z2 = z;
1843618637
int len;
18437
- size_t x;
18638
+ int x;
1843818639
while( *z2 ){ z2++; }
1843918640
x = z2 - z;
1844018641
len = 0x7fffffff & x;
1844118642
if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){
1844218643
return db->aLimit[SQLITE_LIMIT_LENGTH];
@@ -19309,19 +19510,19 @@
1930919510
** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
1931019511
** open properly and is not fit for general use but which can be
1931119512
** used as an argument to sqlite3_errmsg() or sqlite3_close().
1931219513
*/
1931319514
SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
19314
- int magic;
19515
+ u32 magic;
1931519516
if( db==0 ) return 0;
1931619517
magic = db->magic;
1931719518
if( magic!=SQLITE_MAGIC_OPEN &&
1931819519
magic!=SQLITE_MAGIC_BUSY ) return 0;
1931919520
return 1;
1932019521
}
1932119522
SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
19322
- int magic;
19523
+ u32 magic;
1932319524
if( db==0 ) return 0;
1932419525
magic = db->magic;
1932519526
if( magic!=SQLITE_MAGIC_SICK &&
1932619527
magic!=SQLITE_MAGIC_OPEN &&
1932719528
magic!=SQLITE_MAGIC_BUSY ) return 0;
@@ -19801,11 +20002,11 @@
1980120002
**
1980220003
******************************************************************************
1980320004
**
1980420005
** This file contains code that is specific to OS/2.
1980520006
**
19806
-** $Id: os_os2.c,v 1.58 2008/11/07 00:06:18 drh Exp $
20007
+** $Id: os_os2.c,v 1.59 2008/11/18 23:03:40 pweilbacher Exp $
1980720008
*/
1980820009
1980920010
1981020011
#if SQLITE_OS_OS2
1981120012
@@ -20216,11 +20417,18 @@
2021620417
if( flags & SQLITE_SYNC_FULL){
2021720418
sqlite3_fullsync_count++;
2021820419
}
2021920420
sqlite3_sync_count++;
2022020421
#endif
20422
+ /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
20423
+ ** no-op
20424
+ */
20425
+#ifdef SQLITE_NO_SYNC
20426
+ return SQLITE_OK;
20427
+#else
2022120428
return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
20429
+#endif
2022220430
}
2022320431
2022420432
/*
2022520433
** Determine the current size of a file in bytes
2022620434
*/
@@ -21149,11 +21357,11 @@
2114921357
**
2115021358
******************************************************************************
2115121359
**
2115221360
** This file contains code that is specific to Unix systems.
2115321361
**
21154
-** $Id: os_unix.c,v 1.208 2008/11/07 00:06:18 drh Exp $
21362
+** $Id: os_unix.c,v 1.216 2008/11/19 16:52:44 danielk1977 Exp $
2115521363
*/
2115621364
#if SQLITE_OS_UNIX /* This file is used on unix only */
2115721365
2115821366
/*
2115921367
** If SQLITE_ENABLE_LOCKING_STYLE is defined and is non-zero, then several
@@ -21161,11 +21369,12 @@
2116121369
**
2116221370
** * POSIX locking (the default),
2116321371
** * No locking,
2116421372
** * Dot-file locking,
2116521373
** * flock() locking,
21166
-** * AFP locking (OSX only).
21374
+** * AFP locking (OSX only),
21375
+** * Named POSIX semaphores (VXWorks only).
2116721376
**
2116821377
** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by
2116921378
** default on a Mac and disabled on all other posix platforms.
2117021379
*/
2117121380
#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
@@ -21174,10 +21383,20 @@
2117421383
# else
2117521384
# define SQLITE_ENABLE_LOCKING_STYLE 0
2117621385
# endif
2117721386
#endif
2117821387
21388
+/*
21389
+** Define the IS_VXWORKS pre-processor macro to 1 if building on
21390
+** vxworks, or 0 otherwise.
21391
+*/
21392
+#if defined(__RTP__) || defined(_WRS_KERNEL)
21393
+# define IS_VXWORKS 1
21394
+#else
21395
+# define IS_VXWORKS 0
21396
+#endif
21397
+
2117921398
/*
2118021399
** These #defines should enable >2GB file support on Posix if the
2118121400
** underlying operating system supports it. If the OS lacks
2118221401
** large file support, these should be no-ops.
2118321402
**
@@ -21206,13 +21425,19 @@
2120621425
#include <unistd.h>
2120721426
#include <sys/time.h>
2120821427
#include <errno.h>
2120921428
2121021429
#if SQLITE_ENABLE_LOCKING_STYLE
21211
-#include <sys/ioctl.h>
21212
-#include <sys/param.h>
21213
-#include <sys/mount.h>
21430
+# include <sys/ioctl.h>
21431
+# if IS_VXWORKS
21432
+# define lstat stat
21433
+# include <semaphore.h>
21434
+# include <limits.h>
21435
+# else
21436
+# include <sys/param.h>
21437
+# include <sys/mount.h>
21438
+# endif
2121421439
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2121521440
2121621441
/*
2121721442
** If we are to be thread-safe, include the pthreads header and define
2121821443
** the SQLITE_UNIX_THREADS macro.
@@ -21257,10 +21482,14 @@
2125721482
int dirfd; /* File descriptor for the directory */
2125821483
#if SQLITE_THREADSAFE
2125921484
pthread_t tid; /* The thread that "owns" this unixFile */
2126021485
#endif
2126121486
int lastErrno; /* The unix errno from the last I/O error */
21487
+#if IS_VXWORKS
21488
+ int isDelete; /* Delete on close if true */
21489
+ char *zRealpath;
21490
+#endif
2126221491
};
2126321492
2126421493
/*
2126521494
** Include code that is common to all os_*.c files
2126621495
*/
@@ -21667,11 +21896,15 @@
2166721896
** each others locks then tid is always set to zero. tid is omitted
2166821897
** if we compile without threading support.
2166921898
*/
2167021899
struct lockKey {
2167121900
dev_t dev; /* Device number */
21901
+#if IS_VXWORKS
21902
+ void *rnam; /* Realname since inode unusable */
21903
+#else
2167221904
ino_t ino; /* Inode number */
21905
+#endif
2167321906
#if SQLITE_THREADSAFE
2167421907
pthread_t tid; /* Thread ID or zero if threads can override each other */
2167521908
#endif
2167621909
};
2167721910
@@ -21697,11 +21930,15 @@
2169721930
** to locate a particular openCnt structure given its inode. This
2169821931
** is the same as the lockKey except that the thread ID is omitted.
2169921932
*/
2170021933
struct openKey {
2170121934
dev_t dev; /* Device number */
21935
+#if IS_VXWORKS
21936
+ void *rnam; /* Realname since inode unusable */
21937
+#else
2170221938
ino_t ino; /* Inode number */
21939
+#endif
2170321940
};
2170421941
2170521942
/*
2170621943
** An instance of the following structure is allocated for each open
2170721944
** inode. This structure keeps track of the number of locks on that
@@ -21713,10 +21950,14 @@
2171321950
struct openKey key; /* The lookup key */
2171421951
int nRef; /* Number of pointers to this structure */
2171521952
int nLock; /* Number of outstanding locks */
2171621953
int nPending; /* Number of pending close() operations */
2171721954
int *aPending; /* Malloced space holding fd's awaiting a close() */
21955
+#if IS_VXWORKS
21956
+ sem_t *pSem; /* Named POSIX semaphore */
21957
+ char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
21958
+#endif
2171821959
struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
2171921960
};
2172021961
2172121962
/*
2172221963
** List of all lockInfo and openCnt objects. This used to be a hash
@@ -21724,10 +21965,22 @@
2172421965
** never exceeds a few thousand. And lookup is not on a critical
2172521966
** path oo a simple linked list will suffice.
2172621967
*/
2172721968
static struct lockInfo *lockList = 0;
2172821969
static struct openCnt *openList = 0;
21970
+
21971
+#if IS_VXWORKS
21972
+/*
21973
+** This hash table is used to bind the canonical file name to a
21974
+** unixFile structure and use the hash key (= canonical name)
21975
+** instead of the Inode number of the file to find the matching
21976
+** lockInfo and openCnt structures. It also helps to make the
21977
+** name of the semaphore when LOCKING_STYLE_NAMEDSEM is used
21978
+** for the file.
21979
+*/
21980
+static Hash nameHash;
21981
+#endif
2172921982
2173021983
/*
2173121984
** The locking styles are associated with the different file locking
2173221985
** capabilities supported by different file systems.
2173321986
**
@@ -21737,18 +21990,21 @@
2173721990
** DOTLOCK isn't a true locking style, it refers to the use of a special
2173821991
** file named the same as the database file with a '.lock' extension, this
2173921992
** can be used on file systems that do not offer any reliable file locking
2174021993
** NO locking means that no locking will be attempted, this is only used for
2174121994
** read-only file systems currently
21995
+** NAMEDSEM is similar to DOTLOCK but uses a named semaphore instead of an
21996
+** indicator file.
2174221997
** UNSUPPORTED means that no locking will be attempted, this is only used for
2174321998
** file systems that are known to be unsupported
2174421999
*/
2174522000
#define LOCKING_STYLE_POSIX 1
2174622001
#define LOCKING_STYLE_NONE 2
2174722002
#define LOCKING_STYLE_DOTFILE 3
2174822003
#define LOCKING_STYLE_FLOCK 4
2174922004
#define LOCKING_STYLE_AFP 5
22005
+#define LOCKING_STYLE_NAMEDSEM 6
2175022006
2175122007
/*
2175222008
** Only set the lastErrno if the error code is a real error and not
2175322009
** a normal expected return code of SQLITE_BUSY or SQLITE_OK
2175422010
*/
@@ -21859,20 +22115,23 @@
2185922115
return s;
2186022116
}
2186122117
#define fcntl lockTrace
2186222118
#endif /* SQLITE_LOCK_TRACE */
2186322119
22120
+#ifdef __linux__
2186422121
/*
21865
-** The testThreadLockingBehavior() routine launches two separate
21866
-** threads on this routine. This routine attempts to lock a file
21867
-** descriptor then returns. The success or failure of that attempt
21868
-** allows the testThreadLockingBehavior() procedure to determine
21869
-** whether or not threads can override each others locks.
21870
-*/
22122
+** This function is used as the main routine for a thread launched by
22123
+** testThreadLockingBehavior(). It tests whether the shared-lock obtained
22124
+** by the main thread in testThreadLockingBehavior() conflicts with a
22125
+** hypothetical write-lock obtained by this thread on the same file.
22126
+**
22127
+** The write-lock is not actually acquired, as this is not possible if
22128
+** the file is open in read-only mode (see ticket #3472).
22129
+*/
2187122130
static void *threadLockingTest(void *pArg){
2187222131
struct threadTestData *pData = (struct threadTestData*)pArg;
21873
- pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
22132
+ pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
2187422133
return pArg;
2187522134
}
2187622135
2187722136
/*
2187822137
** This procedure attempts to determine whether or not threads
@@ -21879,30 +22138,43 @@
2187922138
** can override each others locks then sets the
2188022139
** threadsOverrideEachOthersLocks variable appropriately.
2188122140
*/
2188222141
static void testThreadLockingBehavior(int fd_orig){
2188322142
int fd;
21884
- struct threadTestData d[2];
21885
- pthread_t t[2];
22143
+ int rc;
22144
+ struct threadTestData d;
22145
+ struct flock l;
22146
+ pthread_t t;
2188622147
2188722148
fd = dup(fd_orig);
2188822149
if( fd<0 ) return;
21889
- memset(d, 0, sizeof(d));
21890
- d[0].fd = fd;
21891
- d[0].lock.l_type = F_RDLCK;
21892
- d[0].lock.l_len = 1;
21893
- d[0].lock.l_start = 0;
21894
- d[0].lock.l_whence = SEEK_SET;
21895
- d[1] = d[0];
21896
- d[1].lock.l_type = F_WRLCK;
21897
- pthread_create(&t[0], 0, threadLockingTest, &d[0]);
21898
- pthread_create(&t[1], 0, threadLockingTest, &d[1]);
21899
- pthread_join(t[0], 0);
21900
- pthread_join(t[1], 0);
22150
+ memset(&l, 0, sizeof(l));
22151
+ l.l_type = F_RDLCK;
22152
+ l.l_len = 1;
22153
+ l.l_start = 0;
22154
+ l.l_whence = SEEK_SET;
22155
+ rc = fcntl(fd_orig, F_SETLK, &l);
22156
+ if( rc!=0 ) return;
22157
+ memset(&d, 0, sizeof(d));
22158
+ d.fd = fd;
22159
+ d.lock = l;
22160
+ d.lock.l_type = F_WRLCK;
22161
+ pthread_create(&t, 0, threadLockingTest, &d);
22162
+ pthread_join(t, 0);
2190122163
close(fd);
21902
- threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
22164
+ if( d.result!=0 ) return;
22165
+ threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
2190322166
}
22167
+#else
22168
+/*
22169
+** On anything other than linux, assume threads override each others locks.
22170
+*/
22171
+static void testThreadLockingBehavior(int fd_orig){
22172
+ threadsOverrideEachOthersLocks = 1;
22173
+}
22174
+#endif /* __linux__ */
22175
+
2190422176
#endif /* SQLITE_THREADSAFE */
2190522177
2190622178
/*
2190722179
** Release a lockInfo structure previously allocated by findLockInfo().
2190822180
*/
@@ -21947,15 +22219,108 @@
2194722219
sqlite3_free(pOpen->aPending);
2194822220
sqlite3_free(pOpen);
2194922221
}
2195022222
}
2195122223
}
22224
+
22225
+#if IS_VXWORKS
22226
+/*
22227
+** Implementation of a realpath() like function for vxWorks
22228
+** to determine canonical path name from given name. It does
22229
+** not support symlinks. Neither does it handle volume prefixes.
22230
+*/
22231
+char *
22232
+vxrealpath(const char *pathname, int dostat)
22233
+{
22234
+ struct stat sbuf;
22235
+ int len;
22236
+ char *where, *ptr, *last;
22237
+ char *result, *curpath, *workpath, *namebuf;
22238
+
22239
+ len = pathconf(pathname, _PC_PATH_MAX);
22240
+ if( len<0 ){
22241
+ len = PATH_MAX;
22242
+ }
22243
+ result = sqlite3_malloc(len * 4);
22244
+ if( !result ){
22245
+ return 0;
22246
+ }
22247
+ curpath = result + len;
22248
+ workpath = curpath + len;
22249
+ namebuf = workpath + len;
22250
+ strcpy(curpath, pathname);
22251
+ if( *pathname!='/' ){
22252
+ if( !getcwd(workpath, len) ){
22253
+ sqlite3_free(result);
22254
+ return 0;
22255
+ }
22256
+ }else{
22257
+ *workpath = '\0';
22258
+ }
22259
+ where = curpath;
22260
+ while( *where ){
22261
+ if( !strcmp(where, ".") ){
22262
+ where++;
22263
+ continue;
22264
+ }
22265
+ if( !strncmp(where, "./", 2) ){
22266
+ where += 2;
22267
+ continue;
22268
+ }
22269
+ if( !strncmp(where, "../", 3) ){
22270
+ where += 3;
22271
+ ptr = last = workpath;
22272
+ while( *ptr ){
22273
+ if( *ptr=='/' ){
22274
+ last = ptr;
22275
+ }
22276
+ ptr++;
22277
+ }
22278
+ *last = '\0';
22279
+ continue;
22280
+ }
22281
+ ptr = strchr(where, '/');
22282
+ if( !ptr ){
22283
+ ptr = where + strlen(where) - 1;
22284
+ }else{
22285
+ *ptr = '\0';
22286
+ }
22287
+ strcpy(namebuf, workpath);
22288
+ for( last = namebuf; *last; last++ ){
22289
+ continue;
22290
+ }
22291
+ if( *--last!='/' ){
22292
+ strcat(namebuf, "/");
22293
+ }
22294
+ strcat(namebuf, where);
22295
+ where = ++ptr;
22296
+ if( dostat ){
22297
+ if( stat(namebuf, &sbuf)==-1 ){
22298
+ sqlite3_free(result);
22299
+ return 0;
22300
+ }
22301
+ if( (sbuf.st_mode & S_IFDIR)==S_IFDIR ){
22302
+ strcpy(workpath, namebuf);
22303
+ continue;
22304
+ }
22305
+ if( *where ){
22306
+ sqlite3_free(result);
22307
+ return 0;
22308
+ }
22309
+ }
22310
+ strcpy(workpath, namebuf);
22311
+ }
22312
+ strcpy(result, workpath);
22313
+ return result;
22314
+}
22315
+#endif
2195222316
2195322317
#if SQLITE_ENABLE_LOCKING_STYLE
2195422318
/*
2195522319
** Tests a byte-range locking query to see if byte range locks are
2195622320
** supported, if not we fall back to dotlockLockingStyle.
22321
+** On vxWorks we fall back to namedsemLockingStyle.
2195722322
*/
2195822323
static int testLockingStyle(int fd){
2195922324
struct flock lockInfo;
2196022325
2196122326
/* Test byte-range lock using fcntl(). If the call succeeds,
@@ -21968,13 +22333,14 @@
2196822333
if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
2196922334
return LOCKING_STYLE_POSIX;
2197022335
}
2197122336
2197222337
/* Testing for flock() can give false positives. So if if the above
21973
- ** test fails, then we fall back to using dot-file style locking.
21974
- */
21975
- return LOCKING_STYLE_DOTFILE;
22338
+ ** test fails, then we fall back to using dot-file style locking (or
22339
+ ** named-semaphore locking on vxworks).
22340
+ */
22341
+ return (IS_VXWORKS ? LOCKING_STYLE_NAMEDSEM : LOCKING_STYLE_DOTFILE);
2197622342
}
2197722343
#endif
2197822344
2197922345
/*
2198022346
** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
@@ -21985,16 +22351,27 @@
2198522351
** other systems.
2198622352
**
2198722353
** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
2198822354
** returns LOCKING_STYLE_POSIX.
2198922355
*/
22356
+#if SQLITE_ENABLE_LOCKING_STYLE
2199022357
static int detectLockingStyle(
2199122358
sqlite3_vfs *pVfs,
2199222359
const char *filePath,
2199322360
int fd
2199422361
){
21995
-#if SQLITE_ENABLE_LOCKING_STYLE
22362
+#if IS_VXWORKS
22363
+ if( !filePath ){
22364
+ return LOCKING_STYLE_NONE;
22365
+ }
22366
+ if( pVfs->pAppData ){
22367
+ return SQLITE_PTR_TO_INT(pVfs->pAppData);
22368
+ }
22369
+ if (access(filePath, 0) != -1){
22370
+ return testLockingStyle(fd);
22371
+ }
22372
+#else
2199622373
struct Mapping {
2199722374
const char *zFilesystem;
2199822375
int eLockingStyle;
2199922376
} aMap[] = {
2200022377
{ "hfs", LOCKING_STYLE_POSIX },
@@ -22030,13 +22407,16 @@
2203022407
}
2203122408
}
2203222409
2203322410
/* Default case. Handles, amongst others, "nfs". */
2203422411
return testLockingStyle(fd);
22035
-#endif
22412
+#endif /* if IS_VXWORKS */
2203622413
return LOCKING_STYLE_POSIX;
2203722414
}
22415
+#else
22416
+ #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX
22417
+#endif /* ifdef SQLITE_ENABLE_LOCKING_STYLE */
2203822418
2203922419
/*
2204022420
** Given a file descriptor, locate lockInfo and openCnt structures that
2204122421
** describes that file descriptor. Create new ones if necessary. The
2204222422
** return values might be uninitialized if an error occurs.
@@ -22043,10 +22423,13 @@
2204322423
**
2204422424
** Return an appropriate error code.
2204522425
*/
2204622426
static int findLockInfo(
2204722427
int fd, /* The file descriptor used in the key */
22428
+#if IS_VXWORKS
22429
+ void *rnam, /* vxWorks realname */
22430
+#endif
2204822431
struct lockInfo **ppLock, /* Return the lockInfo structure here */
2204922432
struct openCnt **ppOpen /* Return the openCnt structure here */
2205022433
){
2205122434
int rc;
2205222435
struct lockKey key1;
@@ -22080,20 +22463,28 @@
2208022463
}
2208122464
}
2208222465
2208322466
memset(&key1, 0, sizeof(key1));
2208422467
key1.dev = statbuf.st_dev;
22468
+#if IS_VXWORKS
22469
+ key1.rnam = rnam;
22470
+#else
2208522471
key1.ino = statbuf.st_ino;
22472
+#endif
2208622473
#if SQLITE_THREADSAFE
2208722474
if( threadsOverrideEachOthersLocks<0 ){
2208822475
testThreadLockingBehavior(fd);
2208922476
}
2209022477
key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
2209122478
#endif
2209222479
memset(&key2, 0, sizeof(key2));
2209322480
key2.dev = statbuf.st_dev;
22481
+#if IS_VXWORKS
22482
+ key2.rnam = rnam;
22483
+#else
2209422484
key2.ino = statbuf.st_ino;
22485
+#endif
2209522486
pLock = lockList;
2209622487
while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
2209722488
pLock = pLock->pNext;
2209822489
}
2209922490
if( pLock==0 ){
@@ -22133,10 +22524,14 @@
2213322524
pOpen->aPending = 0;
2213422525
pOpen->pNext = openList;
2213522526
pOpen->pPrev = 0;
2213622527
if( openList ) openList->pPrev = pOpen;
2213722528
openList = pOpen;
22529
+#if IS_VXWORKS
22530
+ pOpen->pSem = NULL;
22531
+ pOpen->aSemName[0] = '\0';
22532
+#endif
2213822533
}else{
2213922534
pOpen->nRef++;
2214022535
}
2214122536
*ppOpen = pOpen;
2214222537
}
@@ -22197,11 +22592,15 @@
2219722592
OSTRACE4("Transfer ownership of %d from %d to %d\n",
2219822593
pFile->h, pFile->tid, hSelf);
2219922594
pFile->tid = hSelf;
2220022595
if (pFile->pLock != NULL) {
2220122596
releaseLockInfo(pFile->pLock);
22597
+#if IS_VXWORKS
22598
+ rc = findLockInfo(pFile->h, pFile->zRealpath, &pFile->pLock, 0);
22599
+#else
2220222600
rc = findLockInfo(pFile->h, &pFile->pLock, 0);
22601
+#endif
2220322602
OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
2220422603
locktypeName(pFile->locktype),
2220522604
locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
2220622605
return rc;
2220722606
} else {
@@ -22366,10 +22765,24 @@
2236622765
** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
2236722766
** or power failure will likely corrupt the database file.
2236822767
*/
2236922768
static int full_fsync(int fd, int fullSync, int dataOnly){
2237022769
int rc;
22770
+
22771
+ /* The following "ifdef/elif/else/" block has the same structure as
22772
+ ** the one below. It is replicated here solely to avoid cluttering
22773
+ ** up the real code with the UNUSED_PARAMETER() macros.
22774
+ */
22775
+#ifdef SQLITE_NO_SYNC
22776
+ UNUSED_PARAMETER(fd);
22777
+ UNUSED_PARAMETER(fullSync);
22778
+ UNUSED_PARAMETER(dataOnly);
22779
+#elif HAVE_FULLFSYNC
22780
+ UNUSED_PARAMETER(dataOnly);
22781
+#else
22782
+ UNUSED_PARAMETER(fullSync);
22783
+#endif
2237122784
2237222785
/* Record the number of times that we do a normal fsync() and
2237322786
** FULLSYNC. This is used during testing to verify that this procedure
2237422787
** gets called with the correct arguments.
2237522788
*/
@@ -22381,13 +22794,11 @@
2238122794
/* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
2238222795
** no-op
2238322796
*/
2238422797
#ifdef SQLITE_NO_SYNC
2238522798
rc = SQLITE_OK;
22386
-#else
22387
-
22388
-#if HAVE_FULLFSYNC
22799
+#elif HAVE_FULLFSYNC
2238922800
if( fullSync ){
2239022801
rc = fcntl(fd, F_FULLFSYNC, 0);
2239122802
}else{
2239222803
rc = 1;
2239322804
}
@@ -22402,16 +22813,21 @@
2240222813
if( rc ) rc = fsync(fd);
2240322814
2240422815
#else
2240522816
if( dataOnly ){
2240622817
rc = fdatasync(fd);
22818
+ if( IS_VXWORKS && rc==-1 && errno==ENOTSUP ){
22819
+ rc = fsync(fd);
22820
+ }
2240722821
}else{
2240822822
rc = fsync(fd);
2240922823
}
22410
-#endif /* HAVE_FULLFSYNC */
22411
-#endif /* defined(SQLITE_NO_SYNC) */
22824
+#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
2241222825
22826
+ if( IS_VXWORKS && rc!= -1 ){
22827
+ rc = 0;
22828
+ }
2241322829
return rc;
2241422830
}
2241522831
2241622832
/*
2241722833
** Make sure all writes to a particular file are committed to disk.
@@ -22996,10 +23412,29 @@
2299623412
close(pFile->dirfd);
2299723413
}
2299823414
if( pFile->h>=0 ){
2299923415
close(pFile->h);
2300023416
}
23417
+#if IS_VXWORKS
23418
+ if( pFile->isDelete && pFile->zRealpath ){
23419
+ unlink(pFile->zRealpath);
23420
+ }
23421
+ if( pFile->zRealpath ){
23422
+ HashElem *pElem;
23423
+ int n = strlen(pFile->zRealpath) + 1;
23424
+ pElem = sqlite3HashFindElem(&nameHash, pFile->zRealpath, n);
23425
+ if( pElem ){
23426
+ long cnt = (long)pElem->data;
23427
+ cnt--;
23428
+ if( cnt==0 ){
23429
+ sqlite3HashInsert(&nameHash, pFile->zRealpath, n, 0);
23430
+ }else{
23431
+ pElem->data = (void*)cnt;
23432
+ }
23433
+ }
23434
+ }
23435
+#endif
2300123436
OSTRACE2("CLOSE %-3d\n", pFile->h);
2300223437
OpenCounter(-1);
2300323438
memset(pFile, 0, sizeof(unixFile));
2300423439
}
2300523440
return SQLITE_OK;
@@ -23039,10 +23474,12 @@
2303923474
return SQLITE_OK;
2304023475
}
2304123476
2304223477
2304323478
#if SQLITE_ENABLE_LOCKING_STYLE
23479
+
23480
+#if !IS_VXWORKS
2304423481
#pragma mark AFP Support
2304523482
2304623483
/*
2304723484
** The afpLockingContext structure contains all afp lock specific state
2304823485
*/
@@ -23485,10 +23922,12 @@
2348523922
if( id ){
2348623923
flockUnlock(id, NO_LOCK);
2348723924
}
2348823925
return closeUnixFile(id);
2348923926
}
23927
+
23928
+#endif /* !IS_VXWORKS */
2349023929
2349123930
#pragma mark Old-School .lock file based locking
2349223931
2349323932
/* Dotlock-style reserved lock checking following the behavior of
2349423933
** unixCheckReservedLock, see the unixCheckReservedLock function comments */
@@ -23514,11 +23953,11 @@
2351423953
if( lstat(zLockFile, &statBuf)==0 ){
2351523954
/* file exists, someone else has the lock */
2351623955
reserved = 1;
2351723956
}else{
2351823957
/* file does not exist, we could have it if we want it */
23519
- int tErrno = errno;
23958
+ int tErrno = errno;
2352023959
if( ENOENT != tErrno ){
2352123960
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2352223961
pFile->lastErrno = tErrno;
2352323962
}
2352423963
}
@@ -23537,13 +23976,14 @@
2353723976
2353823977
/* if we already have a lock, it is exclusive.
2353923978
** Just adjust level and punt on outta here. */
2354023979
if (pFile->locktype > NO_LOCK) {
2354123980
pFile->locktype = locktype;
23542
-
23981
+#if !IS_VXWORKS
2354323982
/* Always update the timestamp on the old file */
2354423983
utimes(zLockFile, NULL);
23984
+#endif
2354523985
rc = SQLITE_OK;
2354623986
goto dotlock_end_lock;
2354723987
}
2354823988
2354923989
/* check to see if lock file already exists */
@@ -23614,44 +24054,179 @@
2361424054
2361524055
/*
2361624056
** Close a file.
2361724057
*/
2361824058
static int dotlockClose(sqlite3_file *id) {
24059
+ int rc;
2361924060
if( id ){
2362024061
unixFile *pFile = (unixFile*)id;
2362124062
dotlockUnlock(id, NO_LOCK);
2362224063
sqlite3_free(pFile->lockingContext);
2362324064
}
23624
- return closeUnixFile(id);
24065
+ if( IS_VXWORKS ) enterMutex();
24066
+ rc = closeUnixFile(id);
24067
+ if( IS_VXWORKS ) leaveMutex();
24068
+ return rc;
2362524069
}
2362624070
24071
+#if IS_VXWORKS
24072
+
24073
+#pragma mark POSIX/vxWorks named semaphore based locking
24074
+
24075
+/* Namedsem-style reserved lock checking following the behavior of
24076
+** unixCheckReservedLock, see the unixCheckReservedLock function comments */
24077
+static int namedsemCheckReservedLock(sqlite3_file *id, int *pResOut) {
24078
+ int rc = SQLITE_OK;
24079
+ int reserved = 0;
24080
+ unixFile *pFile = (unixFile*)id;
24081
+
24082
+ SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24083
+
24084
+ assert( pFile );
24085
+
24086
+ /* Check if a thread in this process holds such a lock */
24087
+ if( pFile->locktype>SHARED_LOCK ){
24088
+ reserved = 1;
24089
+ }
24090
+
24091
+ /* Otherwise see if some other process holds it. */
24092
+ if( !reserved ){
24093
+ sem_t *pSem = pFile->pOpen->pSem;
24094
+ struct stat statBuf;
24095
+
24096
+ if( sem_trywait(pSem)==-1 ){
24097
+ int tErrno = errno;
24098
+ if( EAGAIN != tErrno ){
24099
+ rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
24100
+ pFile->lastErrno = tErrno;
24101
+ } else {
24102
+ /* someone else has the lock when we are in NO_LOCK */
24103
+ reserved = (pFile->locktype < SHARED_LOCK);
24104
+ }
24105
+ }else{
24106
+ /* we could have it if we want it */
24107
+ sem_post(pSem);
24108
+ }
24109
+ }
24110
+ OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
24111
+
24112
+ *pResOut = reserved;
24113
+ return rc;
24114
+}
24115
+
24116
+static int namedsemLock(sqlite3_file *id, int locktype) {
24117
+ unixFile *pFile = (unixFile*)id;
24118
+ int fd;
24119
+ sem_t *pSem = pFile->pOpen->pSem;
24120
+ int rc = SQLITE_OK;
24121
+
24122
+ /* if we already have a lock, it is exclusive.
24123
+ ** Just adjust level and punt on outta here. */
24124
+ if (pFile->locktype > NO_LOCK) {
24125
+ pFile->locktype = locktype;
24126
+ rc = SQLITE_OK;
24127
+ goto namedsem_end_lock;
24128
+ }
24129
+
24130
+ /* lock semaphore now but bail out when already locked. */
24131
+ if( sem_trywait(pSem)==-1 ){
24132
+ rc = SQLITE_BUSY;
24133
+ goto namedsem_end_lock;
24134
+ }
24135
+
24136
+ /* got it, set the type and return ok */
24137
+ pFile->locktype = locktype;
24138
+
24139
+ namedsem_end_lock:
24140
+ return rc;
24141
+}
24142
+
24143
+static int namedsemUnlock(sqlite3_file *id, int locktype) {
24144
+ unixFile *pFile = (unixFile*)id;
24145
+ sem_t *pSem = pFile->pOpen->pSem;
24146
+
24147
+ assert( pFile );
24148
+ assert( pSem );
24149
+ OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
24150
+ pFile->locktype, getpid());
24151
+ assert( locktype<=SHARED_LOCK );
24152
+
24153
+ /* no-op if possible */
24154
+ if( pFile->locktype==locktype ){
24155
+ return SQLITE_OK;
24156
+ }
24157
+
24158
+ /* shared can just be set because we always have an exclusive */
24159
+ if (locktype==SHARED_LOCK) {
24160
+ pFile->locktype = locktype;
24161
+ return SQLITE_OK;
24162
+ }
24163
+
24164
+ /* no, really unlock. */
24165
+ if ( sem_post(pSem)==-1 ) {
24166
+ int rc, tErrno = errno;
24167
+ rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24168
+ if( IS_LOCK_ERROR(rc) ){
24169
+ pFile->lastErrno = tErrno;
24170
+ }
24171
+ return rc;
24172
+ }
24173
+ pFile->locktype = NO_LOCK;
24174
+ return SQLITE_OK;
24175
+}
24176
+
24177
+/*
24178
+ ** Close a file.
24179
+ */
24180
+static int namedsemClose(sqlite3_file *id) {
24181
+ if( id ){
24182
+ unixFile *pFile = (unixFile*)id;
24183
+ namedsemUnlock(id, NO_LOCK);
24184
+ assert( pFile );
24185
+ enterMutex();
24186
+ releaseLockInfo(pFile->pLock);
24187
+ releaseOpenCnt(pFile->pOpen);
24188
+ closeUnixFile(id);
24189
+ leaveMutex();
24190
+ }
24191
+ return SQLITE_OK;
24192
+}
24193
+
24194
+#endif /* IS_VXWORKS */
2362724195
2362824196
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2362924197
2363024198
/*
2363124199
** The nolockLockingContext is void
2363224200
*/
2363324201
typedef void nolockLockingContext;
2363424202
23635
-static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
24203
+static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
24204
+ UNUSED_PARAMETER(NotUsed);
2363624205
*pResOut = 0;
2363724206
return SQLITE_OK;
2363824207
}
2363924208
23640
-static int nolockLock(sqlite3_file *id, int locktype) {
24209
+static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
24210
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
2364124211
return SQLITE_OK;
2364224212
}
2364324213
23644
-static int nolockUnlock(sqlite3_file *id, int locktype) {
24214
+static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
24215
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
2364524216
return SQLITE_OK;
2364624217
}
2364724218
2364824219
/*
2364924220
** Close a file.
2365024221
*/
2365124222
static int nolockClose(sqlite3_file *id) {
23652
- return closeUnixFile(id);
24223
+ int rc;
24224
+ if( IS_VXWORKS ) enterMutex();
24225
+ rc = closeUnixFile(id);
24226
+ if( IS_VXWORKS ) leaveMutex();
24227
+ return rc;
2365324228
}
2365424229
2365524230
2365624231
/*
2365724232
** Information and control of an open file handle.
@@ -23674,18 +24249,20 @@
2367424249
** SQLite code assumes this function cannot fail. It also assumes that
2367524250
** if two files are created in the same file-system directory (i.e.
2367624251
** a database and its journal file) that the sector size will be the
2367724252
** same for both.
2367824253
*/
23679
-static int unixSectorSize(sqlite3_file *id){
24254
+static int unixSectorSize(sqlite3_file *NotUsed){
24255
+ UNUSED_PARAMETER(NotUsed);
2368024256
return SQLITE_DEFAULT_SECTOR_SIZE;
2368124257
}
2368224258
2368324259
/*
23684
-** Return the device characteristics for the file. This is always 0.
24260
+** Return the device characteristics for the file. This is always 0 for unix.
2368524261
*/
23686
-static int unixDeviceCharacteristics(sqlite3_file *id){
24262
+static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
24263
+ UNUSED_PARAMETER(NotUsed);
2368724264
return 0;
2368824265
}
2368924266
2369024267
/*
2369124268
** Initialize the contents of the unixFile structure pointed to by pId.
@@ -23699,11 +24276,12 @@
2369924276
sqlite3_vfs *pVfs, /* Pointer to vfs object */
2370024277
int h, /* Open file descriptor of file being opened */
2370124278
int dirfd, /* Directory file descriptor */
2370224279
sqlite3_file *pId, /* Write to the unixFile structure here */
2370324280
const char *zFilename, /* Name of the file being opened */
23704
- int noLock /* Omit locking if true */
24281
+ int noLock, /* Omit locking if true */
24282
+ int isDelete /* Delete on close if true */
2370524283
){
2370624284
int eLockingStyle;
2370724285
unixFile *pNew = (unixFile *)pId;
2370824286
int rc = SQLITE_OK;
2370924287
@@ -23730,12 +24308,19 @@
2373024308
static sqlite3_io_methods aIoMethod[] = {
2373124309
IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
2373224310
,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2373324311
#if SQLITE_ENABLE_LOCKING_STYLE
2373424312
,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
24313
+#if IS_VXWORKS
24314
+ ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
24315
+ ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
24316
+ ,IOMETHODS(namedsemClose, namedsemLock, namedsemUnlock, namedsemCheckReservedLock)
24317
+#else
2373524318
,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
2373624319
,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
24320
+ ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
24321
+#endif
2373724322
#endif
2373824323
};
2373924324
/* The order of the IOMETHODS macros above is important. It must be the
2374024325
** same order as the LOCKING_STYLE numbers
2374124326
*/
@@ -23742,18 +24327,63 @@
2374224327
assert(LOCKING_STYLE_POSIX==1);
2374324328
assert(LOCKING_STYLE_NONE==2);
2374424329
assert(LOCKING_STYLE_DOTFILE==3);
2374524330
assert(LOCKING_STYLE_FLOCK==4);
2374624331
assert(LOCKING_STYLE_AFP==5);
24332
+ assert(LOCKING_STYLE_NAMEDSEM==6);
2374724333
2374824334
assert( pNew->pLock==NULL );
2374924335
assert( pNew->pOpen==NULL );
2375024336
24337
+ /* Parameter isDelete is only used on vxworks. Parameter pVfs is only
24338
+ ** used if ENABLE_LOCKING_STYLE is defined. Express this explicitly
24339
+ ** here to prevent compiler warnings about unused parameters.
24340
+ */
24341
+ if( !IS_VXWORKS ) UNUSED_PARAMETER(isDelete);
24342
+ if( !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(pVfs);
24343
+ if( !IS_VXWORKS && !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(zFilename);
24344
+
2375124345
OSTRACE3("OPEN %-3d %s\n", h, zFilename);
2375224346
pNew->h = h;
2375324347
pNew->dirfd = dirfd;
2375424348
SET_THREADID(pNew);
24349
+
24350
+#if IS_VXWORKS
24351
+ {
24352
+ HashElem *pElem;
24353
+ char *zRealname = vxrealpath(zFilename, 1);
24354
+ int n;
24355
+ pNew->zRealpath = 0;
24356
+ if( !zRealname ){
24357
+ rc = SQLITE_NOMEM;
24358
+ eLockingStyle = LOCKING_STYLE_NONE;
24359
+ }else{
24360
+ n = strlen(zRealname) + 1;
24361
+ enterMutex();
24362
+ pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
24363
+ if( pElem ){
24364
+ long cnt = (long)pElem->data;
24365
+ cnt++;
24366
+ pNew->zRealpath = pElem->pKey;
24367
+ pElem->data = (void*)cnt;
24368
+ }else{
24369
+ if( sqlite3HashInsert(&nameHash, zRealname, n, (void*)1)==0 ){
24370
+ pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
24371
+ if( pElem ){
24372
+ pNew->zRealpath = pElem->pKey;
24373
+ }else{
24374
+ sqlite3HashInsert(&nameHash, zRealname, n, 0);
24375
+ rc = SQLITE_NOMEM;
24376
+ eLockingStyle = LOCKING_STYLE_NONE;
24377
+ }
24378
+ }
24379
+ }
24380
+ leaveMutex();
24381
+ sqlite3_free(zRealname);
24382
+ }
24383
+ }
24384
+#endif
2375524385
2375624386
if( noLock ){
2375724387
eLockingStyle = LOCKING_STYLE_NONE;
2375824388
}else{
2375924389
eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
@@ -23761,16 +24391,22 @@
2376124391
2376224392
switch( eLockingStyle ){
2376324393
2376424394
case LOCKING_STYLE_POSIX: {
2376524395
enterMutex();
24396
+#if IS_VXWORKS
24397
+ rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
24398
+#else
2376624399
rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
24400
+#endif
2376724401
leaveMutex();
2376824402
break;
2376924403
}
2377024404
2377124405
#if SQLITE_ENABLE_LOCKING_STYLE
24406
+
24407
+#if !IS_VXWORKS
2377224408
case LOCKING_STYLE_AFP: {
2377324409
/* AFP locking uses the file path so it needs to be included in
2377424410
** the afpLockingContext.
2377524411
*/
2377624412
afpLockingContext *pCtx;
@@ -23784,10 +24420,11 @@
2378424420
pCtx->filePath = zFilename;
2378524421
srandomdev();
2378624422
}
2378724423
break;
2378824424
}
24425
+#endif
2378924426
2379024427
case LOCKING_STYLE_DOTFILE: {
2379124428
/* Dotfile locking uses the file path so it needs to be included in
2379224429
** the dotlockLockingContext
2379324430
*/
@@ -23801,18 +24438,49 @@
2380124438
sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
2380224439
}
2380324440
pNew->lockingContext = zLockFile;
2380424441
break;
2380524442
}
24443
+
24444
+#if IS_VXWORKS
24445
+ case LOCKING_STYLE_NAMEDSEM: {
24446
+ /* Named semaphore locking uses the file path so it needs to be
24447
+ ** included in the namedsemLockingContext
24448
+ */
24449
+ enterMutex();
24450
+ rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
24451
+ if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
24452
+ char *zSemName = pNew->pOpen->aSemName;
24453
+ int n;
24454
+ sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem", pNew->zRealpath);
24455
+ for( n=0; zSemName[n]; n++ )
24456
+ if( zSemName[n]=='/' ) zSemName[n] = '_';
24457
+ pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
24458
+ if( pNew->pOpen->pSem == SEM_FAILED ){
24459
+ rc = SQLITE_NOMEM;
24460
+ pNew->pOpen->aSemName[0] = '\0';
24461
+ }
24462
+ }
24463
+ leaveMutex();
24464
+ break;
24465
+ }
24466
+#endif
2380624467
2380724468
case LOCKING_STYLE_FLOCK:
2380824469
case LOCKING_STYLE_NONE:
2380924470
break;
2381024471
#endif
2381124472
}
2381224473
2381324474
pNew->lastErrno = 0;
24475
+#if IS_VXWORKS
24476
+ if( rc!=SQLITE_OK ){
24477
+ unlink(zFilename);
24478
+ isDelete = 0;
24479
+ }
24480
+ pNew->isDelete = isDelete;
24481
+#endif
2381424482
if( rc!=SQLITE_OK ){
2381524483
if( dirfd>=0 ) close(dirfd);
2381624484
close(h);
2381724485
}else{
2381824486
pNew->pMethod = &aIoMethod[eLockingStyle-1];
@@ -23878,11 +24546,11 @@
2387824546
** function failing.
2387924547
*/
2388024548
SimulateIOError( return SQLITE_IOERR );
2388124549
2388224550
azDirs[0] = sqlite3_temp_directory;
23883
- for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
24551
+ for(i=0; i<ArraySize(azDirs); i++){
2388424552
if( azDirs[i]==0 ) continue;
2388524553
if( stat(azDirs[i], &buf) ) continue;
2388624554
if( !S_ISDIR(buf.st_mode) ) continue;
2388724555
if( access(azDirs[i], 07) ) continue;
2388824556
zDir = azDirs[i];
@@ -23890,11 +24558,11 @@
2389024558
}
2389124559
2389224560
/* Check that the output buffer is large enough for the temporary file
2389324561
** name. If it is not, return SQLITE_ERROR.
2389424562
*/
23895
- if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
24563
+ if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
2389624564
return SQLITE_ERROR;
2389724565
}
2389824566
2389924567
do{
2390024568
sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
@@ -24007,10 +24675,11 @@
2400724675
if( isCreate ) oflags |= O_CREAT;
2400824676
if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
2400924677
oflags |= (O_LARGEFILE|O_BINARY);
2401024678
2401124679
fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
24680
+ OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags);
2401224681
if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
2401324682
/* Failed to open the file for read/write access. Try read-only. */
2401424683
flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2401524684
flags |= SQLITE_OPEN_READONLY;
2401624685
return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
@@ -24017,11 +24686,15 @@
2401724686
}
2401824687
if( fd<0 ){
2401924688
return SQLITE_CANTOPEN;
2402024689
}
2402124690
if( isDelete ){
24691
+#if IS_VXWORKS
24692
+ zPath = zName;
24693
+#else
2402224694
unlink(zName);
24695
+#endif
2402324696
}
2402424697
if( pOutFlags ){
2402524698
*pOutFlags = flags;
2402624699
}
2402724700
@@ -24037,27 +24710,33 @@
2403724710
#ifdef FD_CLOEXEC
2403824711
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2403924712
#endif
2404024713
2404124714
noLock = eType!=SQLITE_OPEN_MAIN_DB;
24042
- return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock);
24715
+ return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
2404324716
}
2404424717
2404524718
/*
2404624719
** Delete the file at zPath. If the dirSync argument is true, fsync()
2404724720
** the directory after deleting the file.
2404824721
*/
24049
-static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
24722
+static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
2405024723
int rc = SQLITE_OK;
24724
+ UNUSED_PARAMETER(NotUsed);
2405124725
SimulateIOError(return SQLITE_IOERR_DELETE);
2405224726
unlink(zPath);
2405324727
#ifndef SQLITE_DISABLE_DIRSYNC
2405424728
if( dirSync ){
2405524729
int fd;
2405624730
rc = openDirectory(zPath, &fd);
2405724731
if( rc==SQLITE_OK ){
24058
- if( fsync(fd) ){
24732
+#if IS_VXWORKS
24733
+ if( fsync(fd)==-1 )
24734
+#else
24735
+ if( fsync(fd) )
24736
+#endif
24737
+ {
2405924738
rc = SQLITE_IOERR_DIR_FSYNC;
2406024739
}
2406124740
close(fd);
2406224741
}
2406324742
}
@@ -24074,16 +24753,17 @@
2407424753
** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
2407524754
**
2407624755
** Otherwise return 0.
2407724756
*/
2407824757
static int unixAccess(
24079
- sqlite3_vfs *pVfs,
24758
+ sqlite3_vfs *NotUsed,
2408024759
const char *zPath,
2408124760
int flags,
2408224761
int *pResOut
2408324762
){
2408424763
int amode = 0;
24764
+ UNUSED_PARAMETER(NotUsed);
2408524765
SimulateIOError( return SQLITE_IOERR_ACCESS; );
2408624766
switch( flags ){
2408724767
case SQLITE_ACCESS_EXISTS:
2408824768
amode = F_OK;
2408924769
break;
@@ -24124,10 +24804,24 @@
2412424804
** current working directly has been unlinked.
2412524805
*/
2412624806
SimulateIOError( return SQLITE_ERROR );
2412724807
2412824808
assert( pVfs->mxPathname==MAX_PATHNAME );
24809
+ UNUSED_PARAMETER(pVfs);
24810
+
24811
+#if IS_VXWORKS
24812
+ {
24813
+ char *zRealname = vxrealpath(zPath, 0);
24814
+ zOut[0] = '\0';
24815
+ if( !zRealname ){
24816
+ return SQLITE_CANTOPEN;
24817
+ }
24818
+ sqlite3_snprintf(nOut, zOut, "%s", zRealname);
24819
+ sqlite3_free(zRealname);
24820
+ return SQLITE_OK;
24821
+ }
24822
+#else
2412924823
zOut[nOut-1] = '\0';
2413024824
if( zPath[0]=='/' ){
2413124825
sqlite3_snprintf(nOut, zOut, "%s", zPath);
2413224826
}else{
2413324827
int nCwd;
@@ -24162,20 +24856,22 @@
2416224856
zFull[j++] = zFull[i];
2416324857
}
2416424858
zFull[j] = 0;
2416524859
}
2416624860
#endif
24861
+#endif
2416724862
}
2416824863
2416924864
2417024865
#ifndef SQLITE_OMIT_LOAD_EXTENSION
2417124866
/*
2417224867
** Interfaces for opening a shared library, finding entry points
2417324868
** within the shared library, and closing the shared library.
2417424869
*/
2417524870
#include <dlfcn.h>
24176
-static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
24871
+static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
24872
+ UNUSED_PARAMETER(NotUsed);
2417724873
return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2417824874
}
2417924875
2418024876
/*
2418124877
** SQLite calls this function immediately after a call to unixDlSym() or
@@ -24182,23 +24878,26 @@
2418224878
** unixDlOpen() fails (returns a null pointer). If a more detailed error
2418324879
** message is available, it is written to zBufOut. If no error message
2418424880
** is available, zBufOut is left unmodified and SQLite uses a default
2418524881
** error message.
2418624882
*/
24187
-static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24883
+static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
2418824884
char *zErr;
24885
+ UNUSED_PARAMETER(NotUsed);
2418924886
enterMutex();
2419024887
zErr = dlerror();
2419124888
if( zErr ){
2419224889
sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
2419324890
}
2419424891
leaveMutex();
2419524892
}
24196
-static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
24893
+static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
24894
+ UNUSED_PARAMETER(NotUsed);
2419724895
return dlsym(pHandle, zSymbol);
2419824896
}
24199
-static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
24897
+static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
24898
+ UNUSED_PARAMETER(NotUsed);
2420024899
dlclose(pHandle);
2420124900
}
2420224901
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
2420324902
#define unixDlOpen 0
2420424903
#define unixDlError 0
@@ -24207,13 +24906,13 @@
2420724906
#endif
2420824907
2420924908
/*
2421024909
** Write nBuf bytes of random data to the supplied buffer zBuf.
2421124910
*/
24212
-static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24213
-
24214
- assert(nBuf>=(sizeof(time_t)+sizeof(int)));
24911
+static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
24912
+ UNUSED_PARAMETER(NotUsed);
24913
+ assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
2421524914
2421624915
/* We have to initialize zBuf to prevent valgrind from reporting
2421724916
** errors. The reports issued by valgrind are incorrect - we would
2421824917
** prefer that the randomness be increased by making use of the
2421924918
** uninitialized space in zBuf - but valgrind errors tend to worry
@@ -24234,11 +24933,11 @@
2423424933
time_t t;
2423524934
time(&t);
2423624935
memcpy(zBuf, &t, sizeof(t));
2423724936
pid = getpid();
2423824937
memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
24239
- assert( sizeof(t)+sizeof(pid)<=nBuf );
24938
+ assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
2424024939
nBuf = sizeof(t) + sizeof(pid);
2424124940
}else{
2424224941
nBuf = read(fd, zBuf, nBuf);
2424324942
close(fd);
2424424943
}
@@ -24254,19 +24953,27 @@
2425424953
** The return value is the number of microseconds of sleep actually
2425524954
** requested from the underlying operating system, a number which
2425624955
** might be greater than or equal to the argument, but not less
2425724956
** than the argument.
2425824957
*/
24259
-static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
24260
-#if defined(HAVE_USLEEP) && HAVE_USLEEP
24958
+static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
24959
+#if IS_VXWORKS
24960
+ struct timespec sp;
24961
+
24962
+ sp.tv_sec = microseconds / 1000000;
24963
+ sp.tv_nsec = (microseconds % 1000000) * 1000;
24964
+ nanosleep(&sp, NULL);
24965
+ return microseconds;
24966
+#elif defined(HAVE_USLEEP) && HAVE_USLEEP
2426124967
usleep(microseconds);
2426224968
return microseconds;
2426324969
#else
2426424970
int seconds = (microseconds+999999)/1000000;
2426524971
sleep(seconds);
2426624972
return seconds*1000000;
2426724973
#endif
24974
+ UNUSED_PARAMETER(NotUsed);
2426824975
}
2426924976
2427024977
/*
2427124978
** The following variable, if set to a non-zero value, becomes the result
2427224979
** returned from sqlite3OsCurrentTime(). This is used for testing.
@@ -24278,29 +24985,38 @@
2427824985
/*
2427924986
** Find the current time (in Universal Coordinated Time). Write the
2428024987
** current time and date as a Julian Day number into *prNow and
2428124988
** return 0. Return 1 if the time and date cannot be found.
2428224989
*/
24283
-static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
24284
-#ifdef NO_GETTOD
24990
+static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
24991
+#if IS_VXWORKS
24992
+ struct timespec sNow;
24993
+ clock_gettime(CLOCK_REALTIME, &sNow);
24994
+ *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
24995
+#elif defined(NO_GETTOD)
2428524996
time_t t;
2428624997
time(&t);
2428724998
*prNow = t/86400.0 + 2440587.5;
2428824999
#else
2428925000
struct timeval sNow;
2429025001
gettimeofday(&sNow, 0);
2429125002
*prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2429225003
#endif
25004
+
2429325005
#ifdef SQLITE_TEST
2429425006
if( sqlite3_current_time ){
2429525007
*prNow = sqlite3_current_time/86400.0 + 2440587.5;
2429625008
}
2429725009
#endif
25010
+ UNUSED_PARAMETER(NotUsed);
2429825011
return 0;
2429925012
}
2430025013
24301
-static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
25014
+static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
25015
+ UNUSED_PARAMETER(NotUsed);
25016
+ UNUSED_PARAMETER(NotUsed2);
25017
+ UNUSED_PARAMETER(NotUsed3);
2430225018
return 0;
2430325019
}
2430425020
2430525021
/*
2430625022
** Initialize the operating system interface.
@@ -24338,16 +25054,20 @@
2433825054
static sqlite3_vfs aVfs[] = {
2433925055
UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
2434025056
UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
2434125057
UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
2434225058
UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
24343
- UNIXVFS("unix-none", LOCKING_STYLE_NONE)
25059
+ UNIXVFS("unix-none", LOCKING_STYLE_NONE),
25060
+ UNIXVFS("unix-namedsem",LOCKING_STYLE_NAMEDSEM),
2434425061
};
2434525062
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
2434625063
sqlite3_vfs_register(&aVfs[i], 0);
2434725064
}
2434825065
#endif
25066
+#if IS_VXWORKS
25067
+ sqlite3HashInit(&nameHash, 1);
25068
+#endif
2434925069
sqlite3_vfs_register(&unixVfs, 1);
2435025070
return SQLITE_OK;
2435125071
}
2435225072
2435325073
/*
@@ -24373,11 +25093,11 @@
2437325093
**
2437425094
******************************************************************************
2437525095
**
2437625096
** This file contains code that is specific to windows.
2437725097
**
24378
-** $Id: os_win.c,v 1.137 2008/11/07 00:06:18 drh Exp $
25098
+** $Id: os_win.c,v 1.140 2008/11/19 21:35:47 shane Exp $
2437925099
*/
2438025100
#if SQLITE_OS_WIN /* This file is used for windows only */
2438125101
2438225102
2438325103
/*
@@ -24844,11 +25564,11 @@
2484425564
2484525565
/*
2484625566
** Convert multibyte character string to UTF-8. Space to hold the
2484725567
** returned string is obtained from malloc().
2484825568
*/
24849
-static char *mbcsToUtf8(const char *zFilename){
25569
+SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
2485025570
char *zFilenameUtf8;
2485125571
WCHAR *zTmpWide;
2485225572
2485325573
zTmpWide = mbcsToUnicode(zFilename);
2485425574
if( zTmpWide==0 ){
@@ -25342,15 +26062,22 @@
2534226062
if( flags & SQLITE_SYNC_FULL ){
2534326063
sqlite3_fullsync_count++;
2534426064
}
2534526065
sqlite3_sync_count++;
2534626066
#endif
26067
+ /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
26068
+ ** no-op
26069
+ */
26070
+#ifdef SQLITE_NO_SYNC
26071
+ return SQLITE_OK;
26072
+#else
2534726073
if( FlushFileBuffers(pFile->h) ){
2534826074
return SQLITE_OK;
2534926075
}else{
2535026076
return SQLITE_IOERR;
2535126077
}
26078
+#endif
2535226079
}
2535326080
2535426081
/*
2535526082
** Determine the current size of a file in bytes
2535626083
*/
@@ -25724,11 +26451,11 @@
2572426451
#if SQLITE_OS_WINCE==0
2572526452
}else{
2572626453
char *zUtf8;
2572726454
char zMbcsPath[MAX_PATH];
2572826455
GetTempPathA(MAX_PATH-30, zMbcsPath);
25729
- zUtf8 = mbcsToUtf8(zMbcsPath);
26456
+ zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
2573026457
if( zUtf8 ){
2573126458
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
2573226459
free(zUtf8);
2573326460
}else{
2573426461
return SQLITE_NOMEM;
@@ -26065,11 +26792,11 @@
2606526792
free(zConverted);
2606626793
return SQLITE_NOMEM;
2606726794
}
2606826795
GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
2606926796
free(zConverted);
26070
- zOut = mbcsToUtf8(zTemp);
26797
+ zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
2607126798
free(zTemp);
2607226799
#endif
2607326800
}
2607426801
if( zOut ){
2607526802
sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
@@ -26137,10 +26864,15 @@
2613726864
/*
2613826865
** Write up to nBuf bytes of randomness into zBuf.
2613926866
*/
2614026867
static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
2614126868
int n = 0;
26869
+ UNUSED_PARAMETER(pVfs);
26870
+#if defined(SQLITE_TEST)
26871
+ n = nBuf;
26872
+ memset(zBuf, 0, nBuf);
26873
+#else
2614226874
if( sizeof(SYSTEMTIME)<=nBuf-n ){
2614326875
SYSTEMTIME x;
2614426876
GetSystemTime(&x);
2614526877
memcpy(&zBuf[n], &x, sizeof(x));
2614626878
n += sizeof(x);
@@ -26159,10 +26891,11 @@
2615926891
LARGE_INTEGER i;
2616026892
QueryPerformanceCounter(&i);
2616126893
memcpy(&zBuf[n], &i, sizeof(i));
2616226894
n += sizeof(i);
2616326895
}
26896
+#endif
2616426897
return n;
2616526898
}
2616626899
2616726900
2616826901
/*
@@ -26316,24 +27049,45 @@
2631627049
** sometimes grow into tens of thousands or larger. The size of the
2631727050
** Bitvec object is the number of pages in the database file at the
2631827051
** start of a transaction, and is thus usually less than a few thousand,
2631927052
** but can be as large as 2 billion for a really big database.
2632027053
**
26321
-** @(#) $Id: bitvec.c,v 1.7 2008/11/03 20:55:07 drh Exp $
27054
+** @(#) $Id: bitvec.c,v 1.9 2008/11/19 18:30:35 shane Exp $
2632227055
*/
2632327056
27057
+/* Size of the Bitvec structure in bytes. */
2632427058
#define BITVEC_SZ 512
27059
+
2632527060
/* Round the union size down to the nearest pointer boundary, since that's how
2632627061
** it will be aligned within the Bitvec struct. */
26327
-#define BITVEC_USIZE (((BITVEC_SZ-12)/sizeof(Bitvec*))*sizeof(Bitvec*))
26328
-#define BITVEC_NCHAR BITVEC_USIZE
26329
-#define BITVEC_NBIT (BITVEC_NCHAR*8)
26330
-#define BITVEC_NINT (BITVEC_USIZE/4)
27062
+#define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
27063
+
27064
+/* Type of the array "element" for the bitmap representation.
27065
+** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
27066
+** Setting this to the "natural word" size of your CPU may improve
27067
+** performance. */
27068
+#define BITVEC_TELEM u8
27069
+/* Size, in bits, of the bitmap element. */
27070
+#define BITVEC_SZELEM 8
27071
+/* Number of elements in a bitmap array. */
27072
+#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
27073
+/* Number of bits in the bitmap array. */
27074
+#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
27075
+
27076
+/* Number of u32 values in hash table. */
27077
+#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
27078
+/* Maximum number of entries in hash table before
27079
+** sub-dividing and re-hashing. */
2633127080
#define BITVEC_MXHASH (BITVEC_NINT/2)
27081
+/* Hashing function for the aHash representation.
27082
+** Empirical testing showed that the *37 multiplier
27083
+** (an arbitrary prime)in the hash function provided
27084
+** no fewer collisions than the no-op *1. */
27085
+#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
27086
+
2633227087
#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
2633327088
26334
-#define BITVEC_HASH(X) (((X)*37)%BITVEC_NINT)
2633527089
2633627090
/*
2633727091
** A bitmap is an instance of the following structure.
2633827092
**
2633927093
** This bitmap records the existance of zero or more bits
@@ -26353,15 +27107,19 @@
2635327107
** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
2635427108
** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
2635527109
** to hold deal with values between 1 and iDivisor.
2635627110
*/
2635727111
struct Bitvec {
26358
- u32 iSize; /* Maximum bit index */
26359
- u32 nSet; /* Number of bits that are set */
26360
- u32 iDivisor; /* Number of bits handled by each apSub[] entry */
27112
+ u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
27113
+ u32 nSet; /* Number of bits that are set - only valid for aHash element */
27114
+ /* Max nSet is BITVEC_NINT. For BITVEC_SZ of 512, this would be 125. */
27115
+ u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
27116
+ /* Should >=0 for apSub element. */
27117
+ /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
27118
+ /* For a BITVEC_SZ of 512, this would be 34,359,739. */
2636127119
union {
26362
- u8 aBitmap[BITVEC_NCHAR]; /* Bitmap representation */
27120
+ BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
2636327121
u32 aHash[BITVEC_NINT]; /* Hash table representation */
2636427122
Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
2636527123
} u;
2636627124
};
2636727125
@@ -26386,20 +27144,23 @@
2638627144
** i is out of range, then return false.
2638727145
*/
2638827146
SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
2638927147
if( p==0 ) return 0;
2639027148
if( i>p->iSize || i==0 ) return 0;
27149
+ i--;
27150
+ while( p->iDivisor ){
27151
+ u32 bin = i/p->iDivisor;
27152
+ i = i%p->iDivisor;
27153
+ p = p->u.apSub[bin];
27154
+ if (!p) {
27155
+ return 0;
27156
+ }
27157
+ }
2639127158
if( p->iSize<=BITVEC_NBIT ){
26392
- i--;
26393
- return (p->u.aBitmap[i/8] & (1<<(i&7)))!=0;
26394
- }
26395
- if( p->iDivisor>0 ){
26396
- u32 bin = (i-1)/p->iDivisor;
26397
- i = (i-1)%p->iDivisor + 1;
26398
- return sqlite3BitvecTest(p->u.apSub[bin], i);
26399
- }else{
26400
- u32 h = BITVEC_HASH(i);
27159
+ return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
27160
+ } else{
27161
+ u32 h = BITVEC_HASH(i++);
2640127162
while( p->u.aHash[h] ){
2640227163
if( p->u.aHash[h]==i ) return 1;
2640327164
h++;
2640427165
if( h>=BITVEC_NINT ) h = 0;
2640527166
}
@@ -26422,74 +27183,99 @@
2642227183
SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
2642327184
u32 h;
2642427185
assert( p!=0 );
2642527186
assert( i>0 );
2642627187
assert( i<=p->iSize );
26427
- if( p->iSize<=BITVEC_NBIT ){
26428
- i--;
26429
- p->u.aBitmap[i/8] |= 1 << (i&7);
26430
- return SQLITE_OK;
26431
- }
26432
- if( p->iDivisor ){
26433
- u32 bin = (i-1)/p->iDivisor;
26434
- i = (i-1)%p->iDivisor + 1;
27188
+ i--;
27189
+ while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
27190
+ u32 bin = i/p->iDivisor;
27191
+ i = i%p->iDivisor;
2643527192
if( p->u.apSub[bin]==0 ){
2643627193
sqlite3BeginBenignMalloc();
2643727194
p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
2643827195
sqlite3EndBenignMalloc();
2643927196
if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
2644027197
}
26441
- return sqlite3BitvecSet(p->u.apSub[bin], i);
27198
+ p = p->u.apSub[bin];
2644227199
}
26443
- h = BITVEC_HASH(i);
26444
- while( p->u.aHash[h] ){
27200
+ if( p->iSize<=BITVEC_NBIT ){
27201
+ p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
27202
+ return SQLITE_OK;
27203
+ }
27204
+ h = BITVEC_HASH(i++);
27205
+ /* if there wasn't a hash collision, and this doesn't */
27206
+ /* completely fill the hash, then just add it without */
27207
+ /* worring about sub-dividing and re-hashing. */
27208
+ if( !p->u.aHash[h] ){
27209
+ if (p->nSet<(BITVEC_NINT-1)) {
27210
+ goto bitvec_set_end;
27211
+ } else {
27212
+ goto bitvec_set_rehash;
27213
+ }
27214
+ }
27215
+ /* there was a collision, check to see if it's already */
27216
+ /* in hash, if not, try to find a spot for it */
27217
+ do {
2644527218
if( p->u.aHash[h]==i ) return SQLITE_OK;
2644627219
h++;
26447
- if( h==BITVEC_NINT ) h = 0;
26448
- }
26449
- p->nSet++;
27220
+ if( h>=BITVEC_NINT ) h = 0;
27221
+ } while( p->u.aHash[h] );
27222
+ /* we didn't find it in the hash. h points to the first */
27223
+ /* available free spot. check to see if this is going to */
27224
+ /* make our hash too "full". */
27225
+bitvec_set_rehash:
2645027226
if( p->nSet>=BITVEC_MXHASH ){
26451
- int j, rc;
27227
+ unsigned int j;
27228
+ int rc;
2645227229
u32 aiValues[BITVEC_NINT];
2645327230
memcpy(aiValues, p->u.aHash, sizeof(aiValues));
26454
- memset(p->u.apSub, 0, sizeof(p->u.apSub[0])*BITVEC_NPTR);
27231
+ memset(p->u.apSub, 0, sizeof(aiValues));
2645527232
p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
2645627233
rc = sqlite3BitvecSet(p, i);
2645727234
for(j=0; j<BITVEC_NINT; j++){
2645827235
if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
2645927236
}
2646027237
return rc;
2646127238
}
27239
+bitvec_set_end:
27240
+ p->nSet++;
2646227241
p->u.aHash[h] = i;
2646327242
return SQLITE_OK;
2646427243
}
2646527244
2646627245
/*
26467
-** Clear the i-th bit. Return 0 on success and an error code if
26468
-** anything goes wrong.
27246
+** Clear the i-th bit.
2646927247
*/
2647027248
SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i){
2647127249
assert( p!=0 );
2647227250
assert( i>0 );
27251
+ i--;
27252
+ while( p->iDivisor ){
27253
+ u32 bin = i/p->iDivisor;
27254
+ i = i%p->iDivisor;
27255
+ p = p->u.apSub[bin];
27256
+ if (!p) {
27257
+ return;
27258
+ }
27259
+ }
2647327260
if( p->iSize<=BITVEC_NBIT ){
26474
- i--;
26475
- p->u.aBitmap[i/8] &= ~(1 << (i&7));
26476
- }else if( p->iDivisor ){
26477
- u32 bin = (i-1)/p->iDivisor;
26478
- i = (i-1)%p->iDivisor + 1;
26479
- if( p->u.apSub[bin] ){
26480
- sqlite3BitvecClear(p->u.apSub[bin], i);
26481
- }
27261
+ p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
2648227262
}else{
26483
- int j;
27263
+ unsigned int j;
2648427264
u32 aiValues[BITVEC_NINT];
2648527265
memcpy(aiValues, p->u.aHash, sizeof(aiValues));
26486
- memset(p->u.aHash, 0, sizeof(p->u.aHash[0])*BITVEC_NINT);
27266
+ memset(p->u.aHash, 0, sizeof(aiValues));
2648727267
p->nSet = 0;
2648827268
for(j=0; j<BITVEC_NINT; j++){
26489
- if( aiValues[j] && aiValues[j]!=i ){
26490
- sqlite3BitvecSet(p, aiValues[j]);
27269
+ if( aiValues[j] && aiValues[j]!=(i+1) ){
27270
+ u32 h = BITVEC_HASH(aiValues[j]-1);
27271
+ p->nSet++;
27272
+ while( p->u.aHash[h] ){
27273
+ h++;
27274
+ if( h>=BITVEC_NINT ) h = 0;
27275
+ }
27276
+ p->u.aHash[h] = aiValues[j];
2649127277
}
2649227278
}
2649327279
}
2649427280
}
2649527281
@@ -26497,11 +27283,11 @@
2649727283
** Destroy a bitmap object. Reclaim all memory used.
2649827284
*/
2649927285
SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
2650027286
if( p==0 ) return;
2650127287
if( p->iDivisor ){
26502
- int i;
27288
+ unsigned int i;
2650327289
for(i=0; i<BITVEC_NPTR; i++){
2650427290
sqlite3BitvecDestroy(p->u.apSub[i]);
2650527291
}
2650627292
}
2650727293
sqlite3_free(p);
@@ -26630,106 +27416,30 @@
2663027416
** May you share freely, never taking more than you give.
2663127417
**
2663227418
*************************************************************************
2663327419
** This file implements that page cache.
2663427420
**
26635
-** @(#) $Id: pcache.c,v 1.34 2008/10/17 18:51:53 danielk1977 Exp $
27421
+** @(#) $Id: pcache.c,v 1.38 2008/11/19 16:52:44 danielk1977 Exp $
2663627422
*/
2663727423
2663827424
/*
2663927425
** A complete page cache is an instance of this structure.
26640
-**
26641
-** A cache may only be deleted by its owner and while holding the
26642
-** SQLITE_MUTEX_STATUS_LRU mutex.
2664327426
*/
2664427427
struct PCache {
26645
- /*********************************************************************
26646
- ** The first group of elements may be read or written at any time by
26647
- ** the cache owner without holding the mutex. No thread other than the
26648
- ** cache owner is permitted to access these elements at any time.
26649
- */
2665027428
PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
2665127429
PgHdr *pSynced; /* Last synced page in dirty page list */
26652
- int nRef; /* Number of pinned pages */
26653
- int nPinned; /* Number of pinned and/or dirty pages */
27430
+ int nRef; /* Number of referenced pages */
2665427431
int nMax; /* Configured cache size */
2665527432
int nMin; /* Configured minimum cache size */
26656
- /**********************************************************************
26657
- ** The next group of elements are fixed when the cache is created and
26658
- ** may not be changed afterwards. These elements can read at any time by
26659
- ** the cache owner or by any thread holding the the mutex. Non-owner
26660
- ** threads must hold the mutex when reading these elements to prevent
26661
- ** the entire PCache object from being deleted during the read.
26662
- */
2666327433
int szPage; /* Size of every page in this cache */
2666427434
int szExtra; /* Size of extra space for each page */
2666527435
int bPurgeable; /* True if pages are on backing store */
2666627436
int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
2666727437
void *pStress; /* Argument to xStress */
26668
- /**********************************************************************
26669
- ** The final group of elements can only be accessed while holding the
26670
- ** mutex. Both the cache owner and any other thread must hold the mutex
26671
- ** to read or write any of these elements.
26672
- */
26673
- int nPage; /* Total number of pages in apHash */
26674
- int nHash; /* Number of slots in apHash[] */
26675
- PgHdr **apHash; /* Hash table for fast lookup by pgno */
26676
- PgHdr *pClean; /* List of clean pages in use */
26677
-};
26678
-
26679
-/*
26680
-** Free slots in the page block allocator
26681
-*/
26682
-typedef struct PgFreeslot PgFreeslot;
26683
-struct PgFreeslot {
26684
- PgFreeslot *pNext; /* Next free slot */
26685
-};
26686
-
26687
-/*
26688
-** Global data for the page cache.
26689
-*/
26690
-static SQLITE_WSD struct PCacheGlobal {
26691
- int isInit; /* True when initialized */
26692
- sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
26693
-
26694
- int nMaxPage; /* Sum of nMaxPage for purgeable caches */
26695
- int nMinPage; /* Sum of nMinPage for purgeable caches */
26696
- int nCurrentPage; /* Number of purgeable pages allocated */
26697
- PgHdr *pLruHead, *pLruTail; /* LRU list of unused clean pgs */
26698
-
26699
- /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
26700
- int szSlot; /* Size of each free slot */
26701
- void *pStart, *pEnd; /* Bounds of pagecache malloc range */
26702
- PgFreeslot *pFree; /* Free page blocks */
26703
-} pcache = {0};
26704
-
26705
-/*
26706
-** All code in this file should access the global pcache structure via the
26707
-** alias "pcache_g". This ensures that the WSD emulation is used when
26708
-** compiling for systems that do not support real WSD.
26709
-*/
26710
-#define pcache_g (GLOBAL(struct PCacheGlobal, pcache))
26711
-
26712
-/*
26713
-** All global variables used by this module (all of which are grouped
26714
-** together in global structure "pcache" above) are protected by the static
26715
-** SQLITE_MUTEX_STATIC_LRU mutex. A pointer to this mutex is stored in
26716
-** variable "pcache.mutex".
26717
-**
26718
-** Some elements of the PCache and PgHdr structures are protected by the
26719
-** SQLITE_MUTEX_STATUS_LRU mutex and other are not. The protected
26720
-** elements are grouped at the end of the structures and are clearly
26721
-** marked.
26722
-**
26723
-** Use the following macros must surround all access (read or write)
26724
-** of protected elements. The mutex is not recursive and may not be
26725
-** entered more than once. The pcacheMutexHeld() macro should only be
26726
-** used within an assert() to verify that the mutex is being held.
26727
-*/
26728
-#define pcacheEnterMutex() sqlite3_mutex_enter(pcache_g.mutex)
26729
-#define pcacheExitMutex() sqlite3_mutex_leave(pcache_g.mutex)
26730
-#define pcacheMutexHeld() sqlite3_mutex_held(pcache_g.mutex)
27438
+ sqlite3_pcache *pCache; /* Pluggable cache module */
27439
+ PgHdr *pPage1;
27440
+};
2673127441
2673227442
/*
2673327443
** Some of the assert() macros in this code are too expensive to run
2673427444
** even during normal debugging. Use them only rarely on long-running
2673527445
** tests. Enable the expensive asserts using the
@@ -26740,52 +27450,10 @@
2674027450
#else
2674127451
# define expensive_assert(X)
2674227452
#endif
2674327453
2674427454
/********************************** Linked List Management ********************/
26745
-
26746
-#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
26747
-/*
26748
-** This routine verifies that the number of entries in the hash table
26749
-** is pCache->nPage. This routine is used within assert() statements
26750
-** only and is therefore disabled during production builds.
26751
-*/
26752
-static int pcacheCheckHashCount(PCache *pCache){
26753
- int i;
26754
- int nPage = 0;
26755
- for(i=0; i<pCache->nHash; i++){
26756
- PgHdr *p;
26757
- for(p=pCache->apHash[i]; p; p=p->pNextHash){
26758
- nPage++;
26759
- }
26760
- }
26761
- assert( nPage==pCache->nPage );
26762
- return 1;
26763
-}
26764
-#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
26765
-
26766
-
26767
-#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
26768
-/*
26769
-** Based on the current value of PCache.nRef and the contents of the
26770
-** PCache.pDirty list, return the expected value of the PCache.nPinned
26771
-** counter. This is only used in debugging builds, as follows:
26772
-**
26773
-** expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
26774
-*/
26775
-static int pcachePinnedCount(PCache *pCache){
26776
- PgHdr *p;
26777
- int nPinned = pCache->nRef;
26778
- for(p=pCache->pDirty; p; p=p->pNext){
26779
- if( p->nRef==0 ){
26780
- nPinned++;
26781
- }
26782
- }
26783
- return nPinned;
26784
-}
26785
-#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
26786
-
2678727455
2678827456
#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
2678927457
/*
2679027458
** Check that the pCache->pSynced variable is set correctly. If it
2679127459
** is not, either fail an assert or return zero. Otherwise, return
@@ -26792,505 +27460,148 @@
2679227460
** non-zero. This is only used in debugging builds, as follows:
2679327461
**
2679427462
** expensive_assert( pcacheCheckSynced(pCache) );
2679527463
*/
2679627464
static int pcacheCheckSynced(PCache *pCache){
26797
- PgHdr *p = pCache->pDirtyTail;
26798
- for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pPrev){
27465
+ PgHdr *p;
27466
+ for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
2679927467
assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
2680027468
}
2680127469
return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
2680227470
}
2680327471
#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
2680427472
26805
-
26806
-
26807
-/*
26808
-** Remove a page from its hash table (PCache.apHash[]).
26809
-*/
26810
-static void pcacheRemoveFromHash(PgHdr *pPage){
26811
- assert( pcacheMutexHeld() );
26812
- if( pPage->pPrevHash ){
26813
- pPage->pPrevHash->pNextHash = pPage->pNextHash;
26814
- }else{
26815
- PCache *pCache = pPage->pCache;
26816
- u32 h = pPage->pgno % pCache->nHash;
26817
- assert( pCache->apHash[h]==pPage );
26818
- pCache->apHash[h] = pPage->pNextHash;
26819
- }
26820
- if( pPage->pNextHash ){
26821
- pPage->pNextHash->pPrevHash = pPage->pPrevHash;
26822
- }
26823
- pPage->pCache->nPage--;
26824
- expensive_assert( pcacheCheckHashCount(pPage->pCache) );
26825
-}
26826
-
26827
-/*
26828
-** Insert a page into the hash table
26829
-**
26830
-** The mutex must be held by the caller.
26831
-*/
26832
-static void pcacheAddToHash(PgHdr *pPage){
26833
- PCache *pCache = pPage->pCache;
26834
- u32 h = pPage->pgno % pCache->nHash;
26835
- assert( pcacheMutexHeld() );
26836
- pPage->pNextHash = pCache->apHash[h];
26837
- pPage->pPrevHash = 0;
26838
- if( pCache->apHash[h] ){
26839
- pCache->apHash[h]->pPrevHash = pPage;
26840
- }
26841
- pCache->apHash[h] = pPage;
26842
- pCache->nPage++;
26843
- expensive_assert( pcacheCheckHashCount(pCache) );
26844
-}
26845
-
26846
-/*
26847
-** Attempt to increase the size the hash table to contain
26848
-** at least nHash buckets.
26849
-*/
26850
-static int pcacheResizeHash(PCache *pCache, int nHash){
26851
- PgHdr *p;
26852
- PgHdr **pNew;
26853
- assert( pcacheMutexHeld() );
26854
-#ifdef SQLITE_MALLOC_SOFT_LIMIT
26855
- if( nHash*sizeof(PgHdr*)>SQLITE_MALLOC_SOFT_LIMIT ){
26856
- nHash = SQLITE_MALLOC_SOFT_LIMIT/sizeof(PgHdr *);
26857
- }
26858
-#endif
26859
- pcacheExitMutex();
26860
- pNew = (PgHdr **)sqlite3Malloc(sizeof(PgHdr*)*nHash);
26861
- pcacheEnterMutex();
26862
- if( !pNew ){
26863
- return SQLITE_NOMEM;
26864
- }
26865
- memset(pNew, 0, sizeof(PgHdr *)*nHash);
26866
- sqlite3_free(pCache->apHash);
26867
- pCache->apHash = pNew;
26868
- pCache->nHash = nHash;
26869
- pCache->nPage = 0;
26870
-
26871
- for(p=pCache->pClean; p; p=p->pNext){
26872
- pcacheAddToHash(p);
26873
- }
26874
- for(p=pCache->pDirty; p; p=p->pNext){
26875
- pcacheAddToHash(p);
26876
- }
26877
- return SQLITE_OK;
26878
-}
26879
-
26880
-/*
26881
-** Remove a page from a linked list that is headed by *ppHead.
26882
-** *ppHead is either PCache.pClean or PCache.pDirty.
26883
-*/
26884
-static void pcacheRemoveFromList(PgHdr **ppHead, PgHdr *pPage){
26885
- int isDirtyList = (ppHead==&pPage->pCache->pDirty);
26886
- assert( ppHead==&pPage->pCache->pClean || ppHead==&pPage->pCache->pDirty );
26887
- assert( pcacheMutexHeld() || ppHead!=&pPage->pCache->pClean );
26888
-
26889
- if( pPage->pPrev ){
26890
- pPage->pPrev->pNext = pPage->pNext;
26891
- }else{
26892
- assert( *ppHead==pPage );
26893
- *ppHead = pPage->pNext;
26894
- }
26895
- if( pPage->pNext ){
26896
- pPage->pNext->pPrev = pPage->pPrev;
26897
- }
26898
-
26899
- if( isDirtyList ){
26900
- PCache *pCache = pPage->pCache;
26901
- assert( pPage->pNext || pCache->pDirtyTail==pPage );
26902
- if( !pPage->pNext ){
26903
- pCache->pDirtyTail = pPage->pPrev;
26904
- }
26905
- if( pCache->pSynced==pPage ){
26906
- PgHdr *pSynced = pPage->pPrev;
26907
- while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
26908
- pSynced = pSynced->pPrev;
26909
- }
26910
- pCache->pSynced = pSynced;
26911
- }
26912
- }
26913
-}
26914
-
26915
-/*
26916
-** Add a page from a linked list that is headed by *ppHead.
26917
-** *ppHead is either PCache.pClean or PCache.pDirty.
26918
-*/
26919
-static void pcacheAddToList(PgHdr **ppHead, PgHdr *pPage){
26920
- int isDirtyList = (ppHead==&pPage->pCache->pDirty);
26921
- assert( ppHead==&pPage->pCache->pClean || ppHead==&pPage->pCache->pDirty );
26922
-
26923
- if( (*ppHead) ){
26924
- (*ppHead)->pPrev = pPage;
26925
- }
26926
- pPage->pNext = *ppHead;
26927
- pPage->pPrev = 0;
26928
- *ppHead = pPage;
26929
-
26930
- if( isDirtyList ){
26931
- PCache *pCache = pPage->pCache;
26932
- if( !pCache->pDirtyTail ){
26933
- assert( pPage->pNext==0 );
26934
- pCache->pDirtyTail = pPage;
26935
- }
26936
- if( !pCache->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
26937
- pCache->pSynced = pPage;
26938
- }
26939
- }
26940
-}
26941
-
26942
-/*
26943
-** Remove a page from the global LRU list
26944
-*/
26945
-static void pcacheRemoveFromLruList(PgHdr *pPage){
26946
- assert( sqlite3_mutex_held(pcache_g.mutex) );
26947
- assert( (pPage->flags&PGHDR_DIRTY)==0 );
26948
- if( pPage->pCache->bPurgeable==0 ) return;
26949
- if( pPage->pNextLru ){
26950
- assert( pcache_g.pLruTail!=pPage );
26951
- pPage->pNextLru->pPrevLru = pPage->pPrevLru;
26952
- }else{
26953
- assert( pcache_g.pLruTail==pPage );
26954
- pcache_g.pLruTail = pPage->pPrevLru;
26955
- }
26956
- if( pPage->pPrevLru ){
26957
- assert( pcache_g.pLruHead!=pPage );
26958
- pPage->pPrevLru->pNextLru = pPage->pNextLru;
26959
- }else{
26960
- assert( pcache_g.pLruHead==pPage );
26961
- pcache_g.pLruHead = pPage->pNextLru;
26962
- }
26963
-}
26964
-
26965
-/*
26966
-** Add a page to the global LRU list. The page is normally added
26967
-** to the front of the list so that it will be the last page recycled.
26968
-** However, if the PGHDR_REUSE_UNLIKELY bit is set, the page is added
26969
-** to the end of the LRU list so that it will be the next to be recycled.
26970
-*/
26971
-static void pcacheAddToLruList(PgHdr *pPage){
26972
- assert( sqlite3_mutex_held(pcache_g.mutex) );
26973
- assert( (pPage->flags&PGHDR_DIRTY)==0 );
26974
- if( pPage->pCache->bPurgeable==0 ) return;
26975
- if( pcache_g.pLruTail && (pPage->flags & PGHDR_REUSE_UNLIKELY)!=0 ){
26976
- /* If reuse is unlikely. Put the page at the end of the LRU list
26977
- ** where it will be recycled sooner rather than later.
26978
- */
26979
- assert( pcache_g.pLruHead );
26980
- pPage->pNextLru = 0;
26981
- pPage->pPrevLru = pcache_g.pLruTail;
26982
- pcache_g.pLruTail->pNextLru = pPage;
26983
- pcache_g.pLruTail = pPage;
26984
- pPage->flags &= ~PGHDR_REUSE_UNLIKELY;
26985
- }else{
26986
- /* If reuse is possible. the page goes at the beginning of the LRU
26987
- ** list so that it will be the last to be recycled.
26988
- */
26989
- if( pcache_g.pLruHead ){
26990
- pcache_g.pLruHead->pPrevLru = pPage;
26991
- }
26992
- pPage->pNextLru = pcache_g.pLruHead;
26993
- pcache_g.pLruHead = pPage;
26994
- pPage->pPrevLru = 0;
26995
- if( pcache_g.pLruTail==0 ){
26996
- pcache_g.pLruTail = pPage;
26997
- }
26998
- }
26999
-}
27000
-
27001
-/*********************************************** Memory Allocation ***********
27002
-**
27003
-** Initialize the page cache memory pool.
27004
-**
27005
-** This must be called at start-time when no page cache lines are
27006
-** checked out. This function is not threadsafe.
27007
-*/
27008
-SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
27009
- PgFreeslot *p;
27010
- sz &= ~7;
27011
- pcache_g.szSlot = sz;
27012
- pcache_g.pStart = pBuf;
27013
- pcache_g.pFree = 0;
27014
- while( n-- ){
27015
- p = (PgFreeslot*)pBuf;
27016
- p->pNext = pcache_g.pFree;
27017
- pcache_g.pFree = p;
27018
- pBuf = (void*)&((char*)pBuf)[sz];
27019
- }
27020
- pcache_g.pEnd = pBuf;
27021
-}
27022
-
27023
-/*
27024
-** Allocate a page cache line. Look in the page cache memory pool first
27025
-** and use an element from it first if available. If nothing is available
27026
-** in the page cache memory pool, go to the general purpose memory allocator.
27027
-*/
27028
-static void *pcacheMalloc(int sz, PCache *pCache){
27029
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27030
- if( sz<=pcache_g.szSlot && pcache_g.pFree ){
27031
- PgFreeslot *p = pcache_g.pFree;
27032
- pcache_g.pFree = p->pNext;
27033
- sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, sz);
27034
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
27035
- return (void*)p;
27036
- }else{
27037
- void *p;
27038
-
27039
- /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
27040
- ** global pcache mutex and unlock the pager-cache object pCache. This is
27041
- ** so that if the attempt to allocate a new buffer causes the the
27042
- ** configured soft-heap-limit to be breached, it will be possible to
27043
- ** reclaim memory from this pager-cache.
27044
- */
27045
- pcacheExitMutex();
27046
- p = sqlite3Malloc(sz);
27047
- pcacheEnterMutex();
27048
-
27049
- if( p ){
27050
- sz = sqlite3MallocSize(p);
27051
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
27052
- }
27053
- return p;
27054
- }
27055
-}
27056
-SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
27057
- void *p;
27058
- pcacheEnterMutex();
27059
- p = pcacheMalloc(sz, 0);
27060
- pcacheExitMutex();
27061
- return p;
27062
-}
27063
-
27064
-/*
27065
-** Release a pager memory allocation
27066
-*/
27067
-static void pcacheFree(void *p){
27068
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27069
- if( p==0 ) return;
27070
- if( p>=pcache_g.pStart && p<pcache_g.pEnd ){
27071
- PgFreeslot *pSlot;
27072
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
27073
- pSlot = (PgFreeslot*)p;
27074
- pSlot->pNext = pcache_g.pFree;
27075
- pcache_g.pFree = pSlot;
27076
- }else{
27077
- int iSize = sqlite3MallocSize(p);
27078
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
27079
- sqlite3_free(p);
27080
- }
27081
-}
27082
-SQLITE_PRIVATE void sqlite3PageFree(void *p){
27083
- pcacheEnterMutex();
27084
- pcacheFree(p);
27085
- pcacheExitMutex();
27086
-}
27087
-
27088
-/*
27089
-** Allocate a new page.
27090
-*/
27091
-static PgHdr *pcachePageAlloc(PCache *pCache){
27092
- PgHdr *p;
27093
- int sz = sizeof(*p) + pCache->szPage + pCache->szExtra;
27094
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27095
- p = pcacheMalloc(sz, pCache);
27096
- if( p==0 ) return 0;
27097
- memset(p, 0, sizeof(PgHdr));
27098
- p->pData = (void*)&p[1];
27099
- p->pExtra = (void*)&((char*)p->pData)[pCache->szPage];
27473
+/*
27474
+** Remove page pPage from the list of dirty pages.
27475
+*/
27476
+static void pcacheRemoveFromDirtyList(PgHdr *pPage){
27477
+ PCache *p = pPage->pCache;
27478
+
27479
+ assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
27480
+ assert( pPage->pDirtyPrev || pPage==p->pDirty );
27481
+
27482
+ /* Update the PCache1.pSynced variable if necessary. */
27483
+ if( p->pSynced==pPage ){
27484
+ PgHdr *pSynced = pPage->pDirtyPrev;
27485
+ while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
27486
+ pSynced = pSynced->pDirtyPrev;
27487
+ }
27488
+ p->pSynced = pSynced;
27489
+ }
27490
+
27491
+ if( pPage->pDirtyNext ){
27492
+ pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
27493
+ }else{
27494
+ assert( pPage==p->pDirtyTail );
27495
+ p->pDirtyTail = pPage->pDirtyPrev;
27496
+ }
27497
+ if( pPage->pDirtyPrev ){
27498
+ pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
27499
+ }else{
27500
+ assert( pPage==p->pDirty );
27501
+ p->pDirty = pPage->pDirtyNext;
27502
+ }
27503
+ pPage->pDirtyNext = 0;
27504
+ pPage->pDirtyPrev = 0;
27505
+
27506
+ expensive_assert( pcacheCheckSynced(p) );
27507
+}
27508
+
27509
+/*
27510
+** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
27511
+** pPage).
27512
+*/
27513
+static void pcacheAddToDirtyList(PgHdr *pPage){
27514
+ PCache *p = pPage->pCache;
27515
+
27516
+ assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
27517
+
27518
+ pPage->pDirtyNext = p->pDirty;
27519
+ if( pPage->pDirtyNext ){
27520
+ assert( pPage->pDirtyNext->pDirtyPrev==0 );
27521
+ pPage->pDirtyNext->pDirtyPrev = pPage;
27522
+ }
27523
+ p->pDirty = pPage;
27524
+ if( !p->pDirtyTail ){
27525
+ p->pDirtyTail = pPage;
27526
+ }
27527
+ if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
27528
+ p->pSynced = pPage;
27529
+ }
27530
+ expensive_assert( pcacheCheckSynced(p) );
27531
+}
27532
+
27533
+/*
27534
+** Wrapper around the pluggable caches xUnpin method. If the cache is
27535
+** being used for an in-memory database, this function is a no-op.
27536
+*/
27537
+static void pcacheUnpin(PgHdr *p){
27538
+ PCache *pCache = p->pCache;
2710027539
if( pCache->bPurgeable ){
27101
- pcache_g.nCurrentPage++;
27102
- }
27103
- return p;
27104
-}
27105
-
27106
-/*
27107
-** Deallocate a page
27108
-*/
27109
-static void pcachePageFree(PgHdr *p){
27110
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27111
- if( p->pCache->bPurgeable ){
27112
- pcache_g.nCurrentPage--;
27113
- }
27114
- pcacheFree(p);
27115
-}
27116
-
27117
-#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27118
-/*
27119
-** Return the number of bytes that will be returned to the heap when
27120
-** the argument is passed to pcachePageFree().
27121
-*/
27122
-static int pcachePageSize(PgHdr *p){
27123
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27124
- assert( !pcache_g.pStart );
27125
- assert( p->apSave[0]==0 );
27126
- assert( p->apSave[1]==0 );
27127
- assert( p && p->pCache );
27128
- return sqlite3MallocSize(p);
27129
-}
27130
-#endif
27131
-
27132
-/*
27133
-** Attempt to 'recycle' a page from the global LRU list. Only clean,
27134
-** unreferenced pages from purgeable caches are eligible for recycling.
27135
-**
27136
-** This function removes page pcache.pLruTail from the global LRU list,
27137
-** and from the hash-table and PCache.pClean list of the owner pcache.
27138
-** There should be no other references to the page.
27139
-**
27140
-** A pointer to the recycled page is returned, or NULL if no page is
27141
-** eligible for recycling.
27142
-*/
27143
-static PgHdr *pcacheRecyclePage(void){
27144
- PgHdr *p = 0;
27145
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27146
-
27147
- if( (p=pcache_g.pLruTail) ){
27148
- assert( (p->flags&PGHDR_DIRTY)==0 );
27149
- pcacheRemoveFromLruList(p);
27150
- pcacheRemoveFromHash(p);
27151
- pcacheRemoveFromList(&p->pCache->pClean, p);
27152
- }
27153
-
27154
- return p;
27155
-}
27156
-
27157
-/*
27158
-** Obtain space for a page. Try to recycle an old page if the limit on the
27159
-** number of pages has been reached. If the limit has not been reached or
27160
-** there are no pages eligible for recycling, allocate a new page.
27161
-**
27162
-** Return a pointer to the new page, or NULL if an OOM condition occurs.
27163
-*/
27164
-static int pcacheRecycleOrAlloc(PCache *pCache, PgHdr **ppPage){
27165
- PgHdr *p = 0;
27166
-
27167
- int szPage = pCache->szPage;
27168
- int szExtra = pCache->szExtra;
27169
-
27170
- assert( pcache_g.isInit );
27171
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27172
-
27173
- *ppPage = 0;
27174
-
27175
- /* If we have reached either the global or the local limit for
27176
- ** pinned+dirty pages, and there is at least one dirty page,
27177
- ** invoke the xStress callback to cause a page to become clean.
27178
- */
27179
- expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27180
- expensive_assert( pcacheCheckSynced(pCache) );
27181
- if( pCache->xStress
27182
- && pCache->pDirty
27183
- && (pCache->nPinned>=(pcache_g.nMaxPage+pCache->nMin-pcache_g.nMinPage)
27184
- || pCache->nPinned>=pCache->nMax)
27185
- ){
27186
- PgHdr *pPg;
27187
- assert(pCache->pDirtyTail);
27188
-
27189
- for(pPg=pCache->pSynced;
27190
- pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
27191
- pPg=pPg->pPrev
27192
- );
27193
- if( !pPg ){
27194
- for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pPrev);
27195
- }
27196
- if( pPg ){
27197
- int rc;
27198
- pcacheExitMutex();
27199
- rc = pCache->xStress(pCache->pStress, pPg);
27200
- pcacheEnterMutex();
27201
- if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
27202
- return rc;
27203
- }
27204
- }
27205
- }
27206
-
27207
- /* If either the local or the global page limit has been reached,
27208
- ** try to recycle a page.
27209
- */
27210
- if( pCache->bPurgeable && (pCache->nPage>=pCache->nMax-1 ||
27211
- pcache_g.nCurrentPage>=pcache_g.nMaxPage) ){
27212
- p = pcacheRecyclePage();
27213
- }
27214
-
27215
- /* If a page has been recycled but it is the wrong size, free it. */
27216
- if( p && (p->pCache->szPage!=szPage || p->pCache->szPage!=szExtra) ){
27217
- pcachePageFree(p);
27218
- p = 0;
27219
- }
27220
-
27221
- if( !p ){
27222
- p = pcachePageAlloc(pCache);
27223
- }
27224
-
27225
- *ppPage = p;
27226
- return (p?SQLITE_OK:SQLITE_NOMEM);
27540
+ if( p->pgno==1 ){
27541
+ pCache->pPage1 = 0;
27542
+ }
27543
+ sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
27544
+ }
2722727545
}
2722827546
2722927547
/*************************************************** General Interfaces ******
2723027548
**
2723127549
** Initialize and shutdown the page cache subsystem. Neither of these
2723227550
** functions are threadsafe.
2723327551
*/
2723427552
SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
27235
- assert( pcache_g.isInit==0 );
27236
- memset(&pcache_g, 0, sizeof(pcache));
27237
- if( sqlite3GlobalConfig.bCoreMutex ){
27238
- /* No need to check the return value of sqlite3_mutex_alloc().
27239
- ** Allocating a static mutex cannot fail.
27240
- */
27241
- pcache_g.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
27242
- }
27243
- pcache_g.isInit = 1;
27244
- return SQLITE_OK;
27553
+ if( sqlite3GlobalConfig.pcache.xInit==0 ){
27554
+ sqlite3PCacheSetDefault();
27555
+ }
27556
+ return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
2724527557
}
2724627558
SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
27247
- memset(&pcache_g, 0, sizeof(pcache));
27559
+ if( sqlite3GlobalConfig.pcache.xShutdown ){
27560
+ sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
27561
+ }
2724827562
}
2724927563
2725027564
/*
2725127565
** Return the size in bytes of a PCache object.
2725227566
*/
2725327567
SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
2725427568
2725527569
/*
27256
-** Create a new PCache object. Storage space to hold the object
27257
-** has already been allocated and is passed in as the p pointer.
27570
+** Create a new PCache object. Storage space to hold the object
27571
+** has already been allocated and is passed in as the p pointer.
27572
+** The caller discovers how much space needs to be allocated by
27573
+** calling sqlite3PcacheSize().
2725827574
*/
2725927575
SQLITE_PRIVATE void sqlite3PcacheOpen(
2726027576
int szPage, /* Size of every page */
2726127577
int szExtra, /* Extra space associated with each page */
2726227578
int bPurgeable, /* True if pages are on backing store */
2726327579
int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
2726427580
void *pStress, /* Argument to xStress */
2726527581
PCache *p /* Preallocated space for the PCache */
2726627582
){
27267
- assert( pcache_g.isInit );
2726827583
memset(p, 0, sizeof(PCache));
2726927584
p->szPage = szPage;
2727027585
p->szExtra = szExtra;
2727127586
p->bPurgeable = bPurgeable;
2727227587
p->xStress = xStress;
2727327588
p->pStress = pStress;
2727427589
p->nMax = 100;
2727527590
p->nMin = 10;
27276
-
27277
- pcacheEnterMutex();
27278
- if( bPurgeable ){
27279
- pcache_g.nMaxPage += p->nMax;
27280
- pcache_g.nMinPage += p->nMin;
27281
- }
27282
-
27283
- pcacheExitMutex();
2728427591
}
2728527592
2728627593
/*
27287
-** Change the page size for PCache object. This can only happen
27288
-** when the cache is empty.
27594
+** Change the page size for PCache object. The caller must ensure that there
27595
+** are no outstanding page references when this function is called.
2728927596
*/
2729027597
SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
27291
- assert(pCache->nPage==0);
27598
+ assert( pCache->nRef==0 && pCache->pDirty==0 );
27599
+ if( pCache->pCache ){
27600
+ sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
27601
+ pCache->pCache = 0;
27602
+ }
2729227603
pCache->szPage = szPage;
2729327604
}
2729427605
2729527606
/*
2729627607
** Try to obtain a page from the cache.
@@ -27299,96 +27610,106 @@
2729927610
PCache *pCache, /* Obtain the page from this cache */
2730027611
Pgno pgno, /* Page number to obtain */
2730127612
int createFlag, /* If true, create page if it does not exist already */
2730227613
PgHdr **ppPage /* Write the page here */
2730327614
){
27304
- int rc = SQLITE_OK;
2730527615
PgHdr *pPage = 0;
27616
+ int eCreate;
2730627617
27307
- assert( pcache_g.isInit );
2730827618
assert( pCache!=0 );
2730927619
assert( pgno>0 );
27310
- expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27311
-
27312
- pcacheEnterMutex();
27313
-
27314
- /* Search the hash table for the requested page. Exit early if it is found. */
27315
- if( pCache->apHash ){
27316
- u32 h = pgno % pCache->nHash;
27317
- for(pPage=pCache->apHash[h]; pPage; pPage=pPage->pNextHash){
27318
- if( pPage->pgno==pgno ){
27319
- if( pPage->nRef==0 ){
27320
- if( 0==(pPage->flags&PGHDR_DIRTY) ){
27321
- pcacheRemoveFromLruList(pPage);
27322
- pCache->nPinned++;
27323
- }
27324
- pCache->nRef++;
27325
- }
27326
- pPage->nRef++;
27327
- break;
27328
- }
27329
- }
27330
- }
27331
-
27332
- if( !pPage && createFlag ){
27333
- if( pCache->nHash<=pCache->nPage ){
27334
- rc = pcacheResizeHash(pCache, pCache->nHash<256 ? 256 : pCache->nHash*2);
27335
- }
27336
- if( rc==SQLITE_OK ){
27337
- rc = pcacheRecycleOrAlloc(pCache, &pPage);
27338
- }
27339
- if( rc==SQLITE_OK ){
27340
- pPage->pPager = 0;
27341
- pPage->flags = 0;
27342
- pPage->pDirty = 0;
27343
- pPage->pgno = pgno;
27344
- pPage->pCache = pCache;
27345
- pPage->nRef = 1;
27346
- pCache->nRef++;
27347
- pCache->nPinned++;
27348
- pcacheAddToList(&pCache->pClean, pPage);
27349
- pcacheAddToHash(pPage);
27350
- }
27351
- }
27352
-
27353
- pcacheExitMutex();
27354
-
27355
- *ppPage = pPage;
27356
- expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27357
- assert( pPage || !createFlag || rc!=SQLITE_OK );
27358
- return rc;
27359
-}
27360
-
27361
-/*
27362
-** Dereference a page. When the reference count reaches zero,
27363
-** move the page to the LRU list if it is clean.
27620
+
27621
+ /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
27622
+ ** allocate it now.
27623
+ */
27624
+ if( !pCache->pCache && createFlag ){
27625
+ sqlite3_pcache *p;
27626
+ int nByte;
27627
+ nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
27628
+ p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
27629
+ if( !p ){
27630
+ return SQLITE_NOMEM;
27631
+ }
27632
+ sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
27633
+ pCache->pCache = p;
27634
+ }
27635
+
27636
+ eCreate = createFlag ? 1 : 0;
27637
+ if( eCreate && (!pCache->bPurgeable || !pCache->pDirty) ){
27638
+ eCreate = 2;
27639
+ }
27640
+ if( pCache->pCache ){
27641
+ pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
27642
+ }
27643
+
27644
+ if( !pPage && eCreate==1 ){
27645
+ PgHdr *pPg;
27646
+
27647
+ /* Find a dirty page to write-out and recycle. First try to find a
27648
+ ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
27649
+ ** cleared), but if that is not possible settle for any other
27650
+ ** unreferenced dirty page.
27651
+ */
27652
+ expensive_assert( pcacheCheckSynced(pCache) );
27653
+ for(pPg=pCache->pSynced;
27654
+ pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
27655
+ pPg=pPg->pDirtyPrev
27656
+ );
27657
+ if( !pPg ){
27658
+ for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
27659
+ }
27660
+ if( pPg ){
27661
+ int rc;
27662
+ rc = pCache->xStress(pCache->pStress, pPg);
27663
+ if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
27664
+ return rc;
27665
+ }
27666
+ }
27667
+
27668
+ pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
27669
+ }
27670
+
27671
+ if( pPage ){
27672
+ if( 0==pPage->nRef ){
27673
+ pCache->nRef++;
27674
+ }
27675
+ pPage->nRef++;
27676
+ pPage->pData = (void*)&pPage[1];
27677
+ pPage->pExtra = (void*)&((char*)pPage->pData)[pCache->szPage];
27678
+ pPage->pCache = pCache;
27679
+ pPage->pgno = pgno;
27680
+ if( pgno==1 ){
27681
+ pCache->pPage1 = pPage;
27682
+ }
27683
+ }
27684
+ *ppPage = pPage;
27685
+ return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
27686
+}
27687
+
27688
+/*
27689
+** Decrement the reference count on a page. If the page is clean and the
27690
+** reference count drops to 0, then it is made elible for recycling.
2736427691
*/
2736527692
SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
2736627693
assert( p->nRef>0 );
2736727694
p->nRef--;
2736827695
if( p->nRef==0 ){
2736927696
PCache *pCache = p->pCache;
2737027697
pCache->nRef--;
2737127698
if( (p->flags&PGHDR_DIRTY)==0 ){
27372
- pCache->nPinned--;
27373
- pcacheEnterMutex();
27374
- if( pcache_g.nCurrentPage>pcache_g.nMaxPage ){
27375
- pcacheRemoveFromList(&pCache->pClean, p);
27376
- pcacheRemoveFromHash(p);
27377
- pcachePageFree(p);
27378
- }else{
27379
- pcacheAddToLruList(p);
27380
- }
27381
- pcacheExitMutex();
27382
- }else{
27383
- /* Move the page to the head of the caches dirty list. */
27384
- pcacheRemoveFromList(&pCache->pDirty, p);
27385
- pcacheAddToList(&pCache->pDirty, p);
27699
+ pcacheUnpin(p);
27700
+ }else{
27701
+ /* Move the page to the head of the dirty list. */
27702
+ pcacheRemoveFromDirtyList(p);
27703
+ pcacheAddToDirtyList(p);
2738627704
}
2738727705
}
2738827706
}
2738927707
27708
+/*
27709
+** Increase the reference count of a supplied page by 1.
27710
+*/
2739027711
SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
2739127712
assert(p->nRef>0);
2739227713
p->nRef++;
2739327714
}
2739427715
@@ -27398,224 +27719,134 @@
2739827719
** page pointed to by p is invalid.
2739927720
*/
2740027721
SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
2740127722
PCache *pCache;
2740227723
assert( p->nRef==1 );
27403
- assert( 0==(p->flags&PGHDR_DIRTY) );
27724
+ if( p->flags&PGHDR_DIRTY ){
27725
+ pcacheRemoveFromDirtyList(p);
27726
+ }
2740427727
pCache = p->pCache;
2740527728
pCache->nRef--;
27406
- pCache->nPinned--;
27407
- pcacheEnterMutex();
27408
- pcacheRemoveFromList(&pCache->pClean, p);
27409
- pcacheRemoveFromHash(p);
27410
- pcachePageFree(p);
27411
- pcacheExitMutex();
27729
+ if( p->pgno==1 ){
27730
+ pCache->pPage1 = 0;
27731
+ }
27732
+ sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
2741227733
}
2741327734
2741427735
/*
27415
-** Make sure the page is marked as dirty. If it isn't dirty already,
27736
+** Make sure the page is marked as dirty. If it isn't dirty already,
2741627737
** make it so.
2741727738
*/
2741827739
SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
2741927740
PCache *pCache;
2742027741
p->flags &= ~PGHDR_DONT_WRITE;
27421
- if( p->flags & PGHDR_DIRTY ) return;
27422
- assert( (p->flags & PGHDR_DIRTY)==0 );
2742327742
assert( p->nRef>0 );
27424
- pCache = p->pCache;
27425
- pcacheEnterMutex();
27426
- pcacheRemoveFromList(&pCache->pClean, p);
27427
- pcacheAddToList(&pCache->pDirty, p);
27428
- pcacheExitMutex();
27429
- p->flags |= PGHDR_DIRTY;
27430
-}
27431
-
27432
-static void pcacheMakeClean(PgHdr *p){
27433
- PCache *pCache = p->pCache;
27434
- assert( p->flags & PGHDR_DIRTY );
27435
- pcacheRemoveFromList(&pCache->pDirty, p);
27436
- pcacheAddToList(&pCache->pClean, p);
27437
- p->flags &= ~PGHDR_DIRTY;
27438
- if( p->nRef==0 ){
27439
- pcacheAddToLruList(p);
27440
- pCache->nPinned--;
27441
- }
27442
- expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27743
+ if( 0==(p->flags & PGHDR_DIRTY) ){
27744
+ pCache = p->pCache;
27745
+ p->flags |= PGHDR_DIRTY;
27746
+ pcacheAddToDirtyList( p);
27747
+ }
2744327748
}
2744427749
2744527750
/*
27446
-** Make sure the page is marked as clean. If it isn't clean already,
27751
+** Make sure the page is marked as clean. If it isn't clean already,
2744727752
** make it so.
2744827753
*/
2744927754
SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
2745027755
if( (p->flags & PGHDR_DIRTY) ){
27451
- pcacheEnterMutex();
27452
- pcacheMakeClean(p);
27453
- pcacheExitMutex();
27756
+ pcacheRemoveFromDirtyList(p);
27757
+ p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
27758
+ if( p->nRef==0 ){
27759
+ pcacheUnpin(p);
27760
+ }
2745427761
}
2745527762
}
2745627763
2745727764
/*
2745827765
** Make every page in the cache clean.
2745927766
*/
2746027767
SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
2746127768
PgHdr *p;
27462
- pcacheEnterMutex();
2746327769
while( (p = pCache->pDirty)!=0 ){
27464
- pcacheRemoveFromList(&pCache->pDirty, p);
27465
- p->flags &= ~PGHDR_DIRTY;
27466
- pcacheAddToList(&pCache->pClean, p);
27467
- if( p->nRef==0 ){
27468
- pcacheAddToLruList(p);
27469
- pCache->nPinned--;
27470
- }
27471
- }
27472
- sqlite3PcacheAssertFlags(pCache, 0, PGHDR_DIRTY);
27473
- expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27474
- pcacheExitMutex();
27770
+ sqlite3PcacheMakeClean(p);
27771
+ }
2747527772
}
2747627773
2747727774
/*
27478
-** Change the page number of page p to newPgno. If newPgno is 0, then the
27479
-** page object is added to the clean-list and the PGHDR_REUSE_UNLIKELY
27480
-** flag set.
27775
+** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
27776
+*/
27777
+SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
27778
+ PgHdr *p;
27779
+ for(p=pCache->pDirty; p; p=p->pDirtyNext){
27780
+ p->flags &= ~PGHDR_NEED_SYNC;
27781
+ }
27782
+ pCache->pSynced = pCache->pDirtyTail;
27783
+}
27784
+
27785
+/*
27786
+** Change the page number of page p to newPgno.
2748127787
*/
2748227788
SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
27789
+ PCache *pCache = p->pCache;
2748327790
assert( p->nRef>0 );
27484
- pcacheEnterMutex();
27485
- pcacheRemoveFromHash(p);
27791
+ assert( newPgno>0 );
27792
+ sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
2748627793
p->pgno = newPgno;
27487
- if( newPgno==0 ){
27488
- if( (p->flags & PGHDR_DIRTY) ){
27489
- pcacheMakeClean(p);
27490
- }
27491
- p->flags = PGHDR_REUSE_UNLIKELY;
27492
- }
27493
- pcacheAddToHash(p);
27494
- pcacheExitMutex();
27495
-}
27496
-
27497
-/*
27498
-** Remove all content from a page cache
27499
-*/
27500
-static void pcacheClear(PCache *pCache){
27501
- PgHdr *p, *pNext;
27502
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27503
- for(p=pCache->pClean; p; p=pNext){
27504
- pNext = p->pNext;
27505
- pcacheRemoveFromLruList(p);
27506
- pcachePageFree(p);
27507
- }
27508
- for(p=pCache->pDirty; p; p=pNext){
27509
- pNext = p->pNext;
27510
- pcachePageFree(p);
27511
- }
27512
- pCache->pClean = 0;
27513
- pCache->pDirty = 0;
27514
- pCache->pDirtyTail = 0;
27515
- pCache->nPage = 0;
27516
- pCache->nPinned = 0;
27517
- memset(pCache->apHash, 0, pCache->nHash*sizeof(pCache->apHash[0]));
27518
-}
27519
-
27520
-
27521
-/*
27522
-** Drop every cache entry whose page number is greater than "pgno".
27794
+ if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
27795
+ pcacheRemoveFromDirtyList(p);
27796
+ pcacheAddToDirtyList(p);
27797
+ }
27798
+}
27799
+
27800
+/*
27801
+** Drop every cache entry whose page number is greater than "pgno". The
27802
+** caller must ensure that there are no outstanding references to any pages
27803
+** other than page 1 with a page number greater than pgno.
27804
+**
27805
+** If there is a reference to page 1 and the pgno parameter passed to this
27806
+** function is 0, then the data area associated with page 1 is zeroed, but
27807
+** the page object is not dropped.
2752327808
*/
2752427809
SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
27525
- PgHdr *p, *pNext;
27526
- PgHdr *pDirty = pCache->pDirty;
27527
- pcacheEnterMutex();
27528
- for(p=pCache->pClean; p||pDirty; p=pNext){
27529
- if( !p ){
27530
- p = pDirty;
27531
- pDirty = 0;
27532
- }
27533
- pNext = p->pNext;
27534
- if( p->pgno>pgno ){
27535
- if( p->nRef==0 ){
27536
- pcacheRemoveFromHash(p);
27537
- if( p->flags&PGHDR_DIRTY ){
27538
- pcacheRemoveFromList(&pCache->pDirty, p);
27539
- pCache->nPinned--;
27540
- }else{
27541
- pcacheRemoveFromList(&pCache->pClean, p);
27542
- pcacheRemoveFromLruList(p);
27543
- }
27544
- pcachePageFree(p);
27545
- }else{
27546
- /* If there are references to the page, it cannot be freed. In this
27547
- ** case, zero the page content instead.
27548
- */
27549
- memset(p->pData, 0, pCache->szPage);
27550
- }
27551
- }
27552
- }
27553
- pcacheExitMutex();
27554
-}
27555
-
27556
-/*
27557
-** If there are currently more than pcache.nMaxPage pages allocated, try
27558
-** to recycle pages to reduce the number allocated to pcache.nMaxPage.
27559
-*/
27560
-static void pcacheEnforceMaxPage(void){
27561
- PgHdr *p;
27562
- assert( sqlite3_mutex_held(pcache_g.mutex) );
27563
- while( pcache_g.nCurrentPage>pcache_g.nMaxPage && (p = pcacheRecyclePage()) ){
27564
- pcachePageFree(p);
27810
+ if( pCache->pCache ){
27811
+ PgHdr *p;
27812
+ PgHdr *pNext;
27813
+ for(p=pCache->pDirty; p; p=pNext){
27814
+ pNext = p->pDirtyNext;
27815
+ if( p->pgno>pgno ){
27816
+ assert( p->flags&PGHDR_DIRTY );
27817
+ sqlite3PcacheMakeClean(p);
27818
+ }
27819
+ }
27820
+ if( pgno==0 && pCache->pPage1 ){
27821
+ memset(pCache->pPage1->pData, 0, pCache->szPage);
27822
+ pgno = 1;
27823
+ }
27824
+ sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
2756527825
}
2756627826
}
2756727827
2756827828
/*
2756927829
** Close a cache.
2757027830
*/
2757127831
SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
27572
- pcacheEnterMutex();
27573
-
27574
- /* Free all the pages used by this pager and remove them from the LRU list. */
27575
- pcacheClear(pCache);
27576
- if( pCache->bPurgeable ){
27577
- pcache_g.nMaxPage -= pCache->nMax;
27578
- pcache_g.nMinPage -= pCache->nMin;
27579
- pcacheEnforceMaxPage();
27580
- }
27581
- sqlite3_free(pCache->apHash);
27582
- pcacheExitMutex();
27583
-}
27584
-
27585
-
27586
-#ifndef NDEBUG
27587
-/*
27588
-** Assert flags settings on all pages. Debugging only.
27589
-*/
27590
-SQLITE_PRIVATE void sqlite3PcacheAssertFlags(PCache *pCache, int trueMask, int falseMask){
27591
- PgHdr *p;
27592
- for(p=pCache->pDirty; p; p=p->pNext){
27593
- assert( (p->flags&trueMask)==trueMask );
27594
- assert( (p->flags&falseMask)==0 );
27595
- }
27596
- for(p=pCache->pClean; p; p=p->pNext){
27597
- assert( (p->flags&trueMask)==trueMask );
27598
- assert( (p->flags&falseMask)==0 );
27599
- }
27600
-}
27601
-#endif
27832
+ if( pCache->pCache ){
27833
+ sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
27834
+ }
27835
+}
2760227836
2760327837
/*
2760427838
** Discard the contents of the cache.
2760527839
*/
2760627840
SQLITE_PRIVATE int sqlite3PcacheClear(PCache *pCache){
27607
- assert(pCache->nRef==0);
27608
- pcacheEnterMutex();
27609
- pcacheClear(pCache);
27610
- pcacheExitMutex();
27841
+ sqlite3PcacheTruncate(pCache, 0);
2761127842
return SQLITE_OK;
2761227843
}
2761327844
2761427845
/*
2761527846
** Merge two lists of pages connected by pDirty and in pgno order.
27616
-** Do not both fixing the pPrevDirty pointers.
27847
+** Do not both fixing the pDirtyPrev pointers.
2761727848
*/
2761827849
static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
2761927850
PgHdr result, *pTail;
2762027851
pTail = &result;
2762127852
while( pA && pB ){
@@ -27639,11 +27870,11 @@
2763927870
return result.pDirty;
2764027871
}
2764127872
2764227873
/*
2764327874
** Sort the list of pages in accending order by pgno. Pages are
27644
-** connected by pDirty pointers. The pPrevDirty pointers are
27875
+** connected by pDirty pointers. The pDirtyPrev pointers are
2764527876
** corrupted by this sort.
2764627877
*/
2764727878
#define N_SORT_BUCKET_ALLOC 25
2764827879
#define N_SORT_BUCKET 25
2764927880
#ifdef SQLITE_TEST
@@ -27688,101 +27919,762 @@
2768827919
/*
2768927920
** Return a list of all dirty pages in the cache, sorted by page number.
2769027921
*/
2769127922
SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
2769227923
PgHdr *p;
27693
- for(p=pCache->pDirty; p; p=p->pNext){
27694
- p->pDirty = p->pNext;
27924
+ for(p=pCache->pDirty; p; p=p->pDirtyNext){
27925
+ p->pDirty = p->pDirtyNext;
2769527926
}
2769627927
return pcacheSortDirtyList(pCache->pDirty);
2769727928
}
2769827929
2769927930
/*
27700
-** Return the total number of outstanding page references.
27931
+** Return the total number of referenced pages held by the cache.
2770127932
*/
2770227933
SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
2770327934
return pCache->nRef;
2770427935
}
2770527936
27937
+/*
27938
+** Return the number of references to the page supplied as an argument.
27939
+*/
2770627940
SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
2770727941
return p->nRef;
2770827942
}
2770927943
2771027944
/*
2771127945
** Return the total number of pages in the cache.
2771227946
*/
2771327947
SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
27714
- assert( pCache->nPage>=0 );
27715
- return pCache->nPage;
27716
-}
27717
-
27718
-#ifdef SQLITE_CHECK_PAGES
27719
-/*
27720
-** This function is used by the pager.c module to iterate through all
27721
-** pages in the cache. At present, this is only required if the
27722
-** SQLITE_CHECK_PAGES macro (used for debugging) is specified.
27723
-*/
27724
-SQLITE_PRIVATE void sqlite3PcacheIterate(PCache *pCache, void (*xIter)(PgHdr *)){
27725
- PgHdr *p;
27726
- for(p=pCache->pClean; p; p=p->pNext){
27727
- xIter(p);
27728
- }
27729
- for(p=pCache->pDirty; p; p=p->pNext){
27730
- xIter(p);
27731
- }
27732
-}
27733
-#endif
27734
-
27735
-/*
27736
-** Set flags on all pages in the page cache
27737
-*/
27738
-SQLITE_PRIVATE void sqlite3PcacheClearFlags(PCache *pCache, int mask){
27739
- PgHdr *p;
27740
-
27741
- /* Obtain the global mutex before modifying any PgHdr.flags variables
27742
- ** or traversing the LRU list.
27743
- */
27744
- pcacheEnterMutex();
27745
-
27746
- mask = ~mask;
27747
- for(p=pCache->pDirty; p; p=p->pNext){
27748
- p->flags &= mask;
27749
- }
27750
- for(p=pCache->pClean; p; p=p->pNext){
27751
- p->flags &= mask;
27752
- }
27753
-
27754
- if( 0==(mask&PGHDR_NEED_SYNC) ){
27755
- pCache->pSynced = pCache->pDirtyTail;
27756
- assert( !pCache->pSynced || (pCache->pSynced->flags&PGHDR_NEED_SYNC)==0 );
27757
- }
27758
-
27759
- pcacheExitMutex();
27760
-}
27761
-
27762
-/*
27763
-** Set the suggested cache-size value.
27948
+ int nPage = 0;
27949
+ if( pCache->pCache ){
27950
+ nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
27951
+ }
27952
+ return nPage;
27953
+}
27954
+
27955
+#ifdef SQLITE_TEST
27956
+/*
27957
+** Get the suggested cache-size value.
2776427958
*/
2776527959
SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
2776627960
return pCache->nMax;
2776727961
}
27962
+#endif
2776827963
2776927964
/*
2777027965
** Set the suggested cache-size value.
2777127966
*/
2777227967
SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
27773
- if( mxPage<10 ){
27774
- mxPage = 10;
27775
- }
27776
- if( pCache->bPurgeable ){
27777
- pcacheEnterMutex();
27778
- pcache_g.nMaxPage -= pCache->nMax;
27779
- pcache_g.nMaxPage += mxPage;
27780
- pcacheEnforceMaxPage();
27781
- pcacheExitMutex();
27782
- }
2778327968
pCache->nMax = mxPage;
27969
+ if( pCache->pCache ){
27970
+ sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
27971
+ }
27972
+}
27973
+
27974
+#ifdef SQLITE_CHECK_PAGES
27975
+/*
27976
+** For all dirty pages currently in the cache, invoke the specified
27977
+** callback. This is only used if the SQLITE_CHECK_PAGES macro is
27978
+** defined.
27979
+*/
27980
+SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
27981
+ PgHdr *pDirty;
27982
+ for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
27983
+ xIter(pDirty);
27984
+ }
27985
+}
27986
+#endif
27987
+
27988
+
27989
+/************** End of pcache.c **********************************************/
27990
+/************** Begin file pcache1.c *****************************************/
27991
+/*
27992
+** 2008 November 05
27993
+**
27994
+** The author disclaims copyright to this source code. In place of
27995
+** a legal notice, here is a blessing:
27996
+**
27997
+** May you do good and not evil.
27998
+** May you find forgiveness for yourself and forgive others.
27999
+** May you share freely, never taking more than you give.
28000
+**
28001
+*************************************************************************
28002
+**
28003
+** This file implements the default page cache implementation (the
28004
+** sqlite3_pcache interface). It also contains part of the implementation
28005
+** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
28006
+** If the default page cache implementation is overriden, then neither of
28007
+** these two features are available.
28008
+**
28009
+** @(#) $Id: pcache1.c,v 1.3 2008/11/19 09:05:27 danielk1977 Exp $
28010
+*/
28011
+
28012
+
28013
+typedef struct PCache1 PCache1;
28014
+typedef struct PgHdr1 PgHdr1;
28015
+typedef struct PgFreeslot PgFreeslot;
28016
+
28017
+/* Pointers to structures of this type are cast and returned as
28018
+** opaque sqlite3_pcache* handles
28019
+*/
28020
+struct PCache1 {
28021
+ /* Cache configuration parameters. Page size (szPage) and the purgeable
28022
+ ** flag (bPurgeable) are set when the cache is created. nMax may be
28023
+ ** modified at any time by a call to the pcache1CacheSize() method.
28024
+ ** The global mutex must be held when accessing nMax.
28025
+ */
28026
+ int szPage; /* Size of allocated pages in bytes */
28027
+ int bPurgeable; /* True if cache is purgeable */
28028
+ unsigned int nMin; /* Minimum number of pages reserved */
28029
+ unsigned int nMax; /* Configured "cache_size" value */
28030
+
28031
+ /* Hash table of all pages. The following variables may only be accessed
28032
+ ** when the accessor is holding the global mutex (see pcache1EnterMutex()
28033
+ ** and pcache1LeaveMutex()).
28034
+ */
28035
+ unsigned int nRecyclable; /* Number of pages in the LRU list */
28036
+ unsigned int nPage; /* Total number of pages in apHash */
28037
+ unsigned int nHash; /* Number of slots in apHash[] */
28038
+ PgHdr1 **apHash; /* Hash table for fast lookup by key */
28039
+};
28040
+
28041
+/*
28042
+** Each cache entry is represented by an instance of the following
28043
+** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
28044
+** directly after the structure in memory (see the PGHDR1_TO_PAGE()
28045
+** macro below).
28046
+*/
28047
+struct PgHdr1 {
28048
+ unsigned int iKey; /* Key value (page number) */
28049
+ PgHdr1 *pNext; /* Next in hash table chain */
28050
+ PCache1 *pCache; /* Cache that currently owns this page */
28051
+ PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
28052
+ PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
28053
+};
28054
+
28055
+/*
28056
+** Free slots in the allocator used to divide up the buffer provided using
28057
+** the SQLITE_CONFIG_PAGECACHE mechanism.
28058
+*/
28059
+struct PgFreeslot {
28060
+ PgFreeslot *pNext; /* Next free slot */
28061
+};
28062
+
28063
+/*
28064
+** Global data used by this cache.
28065
+*/
28066
+static SQLITE_WSD struct PCacheGlobal {
28067
+ sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
28068
+
28069
+ int nMaxPage; /* Sum of nMaxPage for purgeable caches */
28070
+ int nMinPage; /* Sum of nMinPage for purgeable caches */
28071
+ int nCurrentPage; /* Number of purgeable pages allocated */
28072
+ PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
28073
+
28074
+ /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
28075
+ int szSlot; /* Size of each free slot */
28076
+ void *pStart, *pEnd; /* Bounds of pagecache malloc range */
28077
+ PgFreeslot *pFree; /* Free page blocks */
28078
+} pcache1_g;
28079
+
28080
+/*
28081
+** All code in this file should access the global structure above via the
28082
+** alias "pcache1". This ensures that the WSD emulation is used when
28083
+** compiling for systems that do not support real WSD.
28084
+*/
28085
+#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
28086
+
28087
+/*
28088
+** When a PgHdr1 structure is allocated, the associated PCache1.szPage
28089
+** bytes of data are located directly after it in memory (i.e. the total
28090
+** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
28091
+** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
28092
+** an argument and returns a pointer to the associated block of szPage
28093
+** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
28094
+** a pointer to a block of szPage bytes of data and the return value is
28095
+** a pointer to the associated PgHdr1 structure.
28096
+**
28097
+** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(X))==X );
28098
+*/
28099
+#define PGHDR1_TO_PAGE(p) (void *)(&((unsigned char *)p)[sizeof(PgHdr1)])
28100
+#define PAGE_TO_PGHDR1(p) (PgHdr1 *)(&((unsigned char *)p)[-1*sizeof(PgHdr1)])
28101
+
28102
+/*
28103
+** Macros to enter and leave the global LRU mutex.
28104
+*/
28105
+#define pcache1EnterMutex() sqlite3_mutex_enter(pcache1.mutex)
28106
+#define pcache1LeaveMutex() sqlite3_mutex_leave(pcache1.mutex)
28107
+
28108
+/******************************************************************************/
28109
+/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
28110
+
28111
+/*
28112
+** This function is called during initialization if a static buffer is
28113
+** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
28114
+** verb to sqlite3_config(). Parameter pBuf points to an allocation large
28115
+** enough to contain 'n' buffers of 'sz' bytes each.
28116
+*/
28117
+SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
28118
+ PgFreeslot *p;
28119
+ sz &= ~7;
28120
+ pcache1.szSlot = sz;
28121
+ pcache1.pStart = pBuf;
28122
+ pcache1.pFree = 0;
28123
+ while( n-- ){
28124
+ p = (PgFreeslot*)pBuf;
28125
+ p->pNext = pcache1.pFree;
28126
+ pcache1.pFree = p;
28127
+ pBuf = (void*)&((char*)pBuf)[sz];
28128
+ }
28129
+ pcache1.pEnd = pBuf;
28130
+}
28131
+
28132
+/*
28133
+** Malloc function used within this file to allocate space from the buffer
28134
+** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
28135
+** such buffer exists or there is no space left in it, this function falls
28136
+** back to sqlite3Malloc().
28137
+*/
28138
+static void *pcache1Alloc(int nByte){
28139
+ void *p;
28140
+ assert( sqlite3_mutex_held(pcache1.mutex) );
28141
+ if( nByte<=pcache1.szSlot && pcache1.pFree ){
28142
+ p = (PgHdr1 *)pcache1.pFree;
28143
+ pcache1.pFree = pcache1.pFree->pNext;
28144
+ sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
28145
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
28146
+ }else{
28147
+
28148
+ /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
28149
+ ** global pcache mutex and unlock the pager-cache object pCache. This is
28150
+ ** so that if the attempt to allocate a new buffer causes the the
28151
+ ** configured soft-heap-limit to be breached, it will be possible to
28152
+ ** reclaim memory from this pager-cache.
28153
+ */
28154
+ pcache1LeaveMutex();
28155
+ p = sqlite3Malloc(nByte);
28156
+ pcache1EnterMutex();
28157
+ if( p ){
28158
+ int sz = sqlite3MallocSize(p);
28159
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
28160
+ }
28161
+ }
28162
+ return p;
28163
+}
28164
+
28165
+/*
28166
+** Free an allocated buffer obtained from pcache1Alloc().
28167
+*/
28168
+static void pcache1Free(void *p){
28169
+ assert( sqlite3_mutex_held(pcache1.mutex) );
28170
+ if( p==0 ) return;
28171
+ if( p>=pcache1.pStart && p<pcache1.pEnd ){
28172
+ PgFreeslot *pSlot;
28173
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
28174
+ pSlot = (PgFreeslot*)p;
28175
+ pSlot->pNext = pcache1.pFree;
28176
+ pcache1.pFree = pSlot;
28177
+ }else{
28178
+ int iSize = sqlite3MallocSize(p);
28179
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
28180
+ sqlite3_free(p);
28181
+ }
28182
+}
28183
+
28184
+/*
28185
+** Allocate a new page object initially associated with cache pCache.
28186
+*/
28187
+static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
28188
+ int nByte = sizeof(PgHdr1) + pCache->szPage;
28189
+ PgHdr1 *p = (PgHdr1 *)pcache1Alloc(nByte);
28190
+ if( p ){
28191
+ memset(p, 0, nByte);
28192
+ if( pCache->bPurgeable ){
28193
+ pcache1.nCurrentPage++;
28194
+ }
28195
+ }
28196
+ return p;
28197
+}
28198
+
28199
+/*
28200
+** Free a page object allocated by pcache1AllocPage().
28201
+*/
28202
+static void pcache1FreePage(PgHdr1 *p){
28203
+ if( p ){
28204
+ if( p->pCache->bPurgeable ){
28205
+ pcache1.nCurrentPage--;
28206
+ }
28207
+ pcache1Free(p);
28208
+ }
28209
+}
28210
+
28211
+/*
28212
+** Malloc function used by SQLite to obtain space from the buffer configured
28213
+** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
28214
+** exists, this function falls back to sqlite3Malloc().
28215
+*/
28216
+SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
28217
+ void *p;
28218
+ pcache1EnterMutex();
28219
+ p = pcache1Alloc(sz);
28220
+ pcache1LeaveMutex();
28221
+ return p;
28222
+}
28223
+
28224
+/*
28225
+** Free an allocated buffer obtained from sqlite3PageMalloc().
28226
+*/
28227
+SQLITE_PRIVATE void sqlite3PageFree(void *p){
28228
+ pcache1EnterMutex();
28229
+ pcache1Free(p);
28230
+ pcache1LeaveMutex();
28231
+}
28232
+
28233
+/******************************************************************************/
28234
+/******** General Implementation Functions ************************************/
28235
+
28236
+/*
28237
+** This function is used to resize the hash table used by the cache passed
28238
+** as the first argument.
28239
+**
28240
+** The global mutex must be held when this function is called.
28241
+*/
28242
+static int pcache1ResizeHash(PCache1 *p){
28243
+ PgHdr1 **apNew;
28244
+ unsigned int nNew;
28245
+ unsigned int i;
28246
+
28247
+ assert( sqlite3_mutex_held(pcache1.mutex) );
28248
+
28249
+ nNew = p->nHash*2;
28250
+ if( nNew<256 ){
28251
+ nNew = 256;
28252
+ }
28253
+
28254
+ pcache1LeaveMutex();
28255
+ apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
28256
+ pcache1EnterMutex();
28257
+ if( apNew ){
28258
+ memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
28259
+ for(i=0; i<p->nHash; i++){
28260
+ PgHdr1 *pPage;
28261
+ PgHdr1 *pNext = p->apHash[i];
28262
+ while( (pPage = pNext) ){
28263
+ unsigned int h = pPage->iKey % nNew;
28264
+ pNext = pPage->pNext;
28265
+ pPage->pNext = apNew[h];
28266
+ apNew[h] = pPage;
28267
+ }
28268
+ }
28269
+ sqlite3_free(p->apHash);
28270
+ p->apHash = apNew;
28271
+ p->nHash = nNew;
28272
+ }
28273
+
28274
+ return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
28275
+}
28276
+
28277
+/*
28278
+** This function is used internally to remove the page pPage from the
28279
+** global LRU list, if is part of it. If pPage is not part of the global
28280
+** LRU list, then this function is a no-op.
28281
+**
28282
+** The global mutex must be held when this function is called.
28283
+*/
28284
+static void pcache1PinPage(PgHdr1 *pPage){
28285
+ assert( sqlite3_mutex_held(pcache1.mutex) );
28286
+ if( pPage && (pPage->pLruNext || pPage==pcache1.pLruTail) ){
28287
+ if( pPage->pLruPrev ){
28288
+ pPage->pLruPrev->pLruNext = pPage->pLruNext;
28289
+ }
28290
+ if( pPage->pLruNext ){
28291
+ pPage->pLruNext->pLruPrev = pPage->pLruPrev;
28292
+ }
28293
+ if( pcache1.pLruHead==pPage ){
28294
+ pcache1.pLruHead = pPage->pLruNext;
28295
+ }
28296
+ if( pcache1.pLruTail==pPage ){
28297
+ pcache1.pLruTail = pPage->pLruPrev;
28298
+ }
28299
+ pPage->pLruNext = 0;
28300
+ pPage->pLruPrev = 0;
28301
+ pPage->pCache->nRecyclable--;
28302
+ }
28303
+}
28304
+
28305
+
28306
+/*
28307
+** Remove the page supplied as an argument from the hash table
28308
+** (PCache1.apHash structure) that it is currently stored in.
28309
+**
28310
+** The global mutex must be held when this function is called.
28311
+*/
28312
+static void pcache1RemoveFromHash(PgHdr1 *pPage){
28313
+ unsigned int h;
28314
+ PCache1 *pCache = pPage->pCache;
28315
+ PgHdr1 **pp;
28316
+
28317
+ h = pPage->iKey % pCache->nHash;
28318
+ for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
28319
+ *pp = (*pp)->pNext;
28320
+
28321
+ pCache->nPage--;
28322
+}
28323
+
28324
+/*
28325
+** If there are currently more than pcache.nMaxPage pages allocated, try
28326
+** to recycle pages to reduce the number allocated to pcache.nMaxPage.
28327
+*/
28328
+static void pcache1EnforceMaxPage(void){
28329
+ assert( sqlite3_mutex_held(pcache1.mutex) );
28330
+ while( pcache1.nCurrentPage>pcache1.nMaxPage && pcache1.pLruTail ){
28331
+ PgHdr1 *p = pcache1.pLruTail;
28332
+ pcache1PinPage(p);
28333
+ pcache1RemoveFromHash(p);
28334
+ pcache1FreePage(p);
28335
+ }
28336
+}
28337
+
28338
+/*
28339
+** Discard all pages from cache pCache with a page number (key value)
28340
+** greater than or equal to iLimit. Any pinned pages that meet this
28341
+** criteria are unpinned before they are discarded.
28342
+**
28343
+** The global mutex must be held when this function is called.
28344
+*/
28345
+static void pcache1TruncateUnsafe(
28346
+ PCache1 *pCache,
28347
+ unsigned int iLimit
28348
+){
28349
+ unsigned int h;
28350
+ assert( sqlite3_mutex_held(pcache1.mutex) );
28351
+ for(h=0; h<pCache->nHash; h++){
28352
+ PgHdr1 **pp = &pCache->apHash[h];
28353
+ PgHdr1 *pPage;
28354
+ while( (pPage = *pp) ){
28355
+ if( pPage->iKey>=iLimit ){
28356
+ pcache1PinPage(pPage);
28357
+ *pp = pPage->pNext;
28358
+ pcache1FreePage(pPage);
28359
+ }else{
28360
+ pp = &pPage->pNext;
28361
+ }
28362
+ }
28363
+ }
28364
+}
28365
+
28366
+/******************************************************************************/
28367
+/******** sqlite3_pcache Methods **********************************************/
28368
+
28369
+/*
28370
+** Implementation of the sqlite3_pcache.xInit method.
28371
+*/
28372
+static int pcache1Init(void *NotUsed){
28373
+ UNUSED_PARAMETER(NotUsed);
28374
+ memset(&pcache1, 0, sizeof(pcache1));
28375
+ if( sqlite3GlobalConfig.bCoreMutex ){
28376
+ pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
28377
+ }
28378
+ return SQLITE_OK;
28379
+}
28380
+
28381
+/*
28382
+** Implementation of the sqlite3_pcache.xShutdown method.
28383
+*/
28384
+static void pcache1Shutdown(void *NotUsed){
28385
+ UNUSED_PARAMETER(NotUsed);
28386
+ /* no-op */
28387
+}
28388
+
28389
+/*
28390
+** Implementation of the sqlite3_pcache.xCreate method.
28391
+**
28392
+** Allocate a new cache.
28393
+*/
28394
+static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
28395
+ PCache1 *pCache;
28396
+
28397
+ pCache = (PCache1 *)sqlite3_malloc(sizeof(PCache1));
28398
+ if( pCache ){
28399
+ memset(pCache, 0, sizeof(PCache1));
28400
+ pCache->szPage = szPage;
28401
+ pCache->bPurgeable = (bPurgeable ? 1 : 0);
28402
+ if( bPurgeable ){
28403
+ pCache->nMin = 10;
28404
+ pcache1EnterMutex();
28405
+ pcache1.nMinPage += pCache->nMin;
28406
+ pcache1LeaveMutex();
28407
+ }
28408
+ }
28409
+ return (sqlite3_pcache *)pCache;
28410
+}
28411
+
28412
+/*
28413
+** Implementation of the sqlite3_pcache.xCachesize method.
28414
+**
28415
+** Configure the cache_size limit for a cache.
28416
+*/
28417
+static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
28418
+ PCache1 *pCache = (PCache1 *)p;
28419
+ if( pCache->bPurgeable ){
28420
+ pcache1EnterMutex();
28421
+ pcache1.nMaxPage += (nMax - pCache->nMax);
28422
+ pCache->nMax = nMax;
28423
+ pcache1EnforceMaxPage();
28424
+ pcache1LeaveMutex();
28425
+ }
28426
+}
28427
+
28428
+/*
28429
+** Implementation of the sqlite3_pcache.xPagecount method.
28430
+*/
28431
+static int pcache1Pagecount(sqlite3_pcache *p){
28432
+ int n;
28433
+ pcache1EnterMutex();
28434
+ n = ((PCache1 *)p)->nPage;
28435
+ pcache1LeaveMutex();
28436
+ return n;
28437
+}
28438
+
28439
+/*
28440
+** Implementation of the sqlite3_pcache.xFetch method.
28441
+**
28442
+** Fetch a page by key value.
28443
+**
28444
+** Whether or not a new page may be allocated by this function depends on
28445
+** the value of the createFlag argument.
28446
+**
28447
+** There are three different approaches to obtaining space for a page,
28448
+** depending on the value of parameter createFlag (which may be 0, 1 or 2).
28449
+**
28450
+** 1. Regardless of the value of createFlag, the cache is searched for a
28451
+** copy of the requested page. If one is found, it is returned.
28452
+**
28453
+** 2. If createFlag==0 and the page is not already in the cache, NULL is
28454
+** returned.
28455
+**
28456
+** 3. If createFlag is 1, the cache is marked as purgeable and the page is
28457
+** not already in the cache, and if either of the following are true,
28458
+** return NULL:
28459
+**
28460
+** (a) the number of pages pinned by the cache is greater than
28461
+** PCache1.nMax, or
28462
+** (b) the number of pages pinned by the cache is greater than
28463
+** the sum of nMax for all purgeable caches, less the sum of
28464
+** nMin for all other purgeable caches.
28465
+**
28466
+** 4. If none of the first three conditions apply and the cache is marked
28467
+** as purgeable, and if one of the following is true:
28468
+**
28469
+** (a) The number of pages allocated for the cache is already
28470
+** PCache1.nMax, or
28471
+**
28472
+** (b) The number of pages allocated for all purgeable caches is
28473
+** already equal to or greater than the sum of nMax for all
28474
+** purgeable caches,
28475
+**
28476
+** then attempt to recycle a page from the LRU list. If it is the right
28477
+** size, return the recycled buffer. Otherwise, free the buffer and
28478
+** proceed to step 5.
28479
+**
28480
+** 5. Otherwise, allocate and return a new page buffer.
28481
+*/
28482
+static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
28483
+ unsigned int nPinned;
28484
+ PCache1 *pCache = (PCache1 *)p;
28485
+ PgHdr1 *pPage = 0;
28486
+
28487
+ pcache1EnterMutex();
28488
+ if( createFlag==1 ) sqlite3BeginBenignMalloc();
28489
+
28490
+ /* Search the hash table for an existing entry. */
28491
+ if( pCache->nHash>0 ){
28492
+ unsigned int h = iKey % pCache->nHash;
28493
+ for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
28494
+ }
28495
+
28496
+ if( pPage || createFlag==0 ){
28497
+ pcache1PinPage(pPage);
28498
+ goto fetch_out;
28499
+ }
28500
+
28501
+ /* Step 3 of header comment. */
28502
+ nPinned = pCache->nPage - pCache->nRecyclable;
28503
+ if( createFlag==1 && pCache->bPurgeable && (
28504
+ nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
28505
+ || nPinned>=(pCache->nMax)
28506
+ )){
28507
+ goto fetch_out;
28508
+ }
28509
+
28510
+ if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
28511
+ goto fetch_out;
28512
+ }
28513
+
28514
+ /* Step 4. Try to recycle a page buffer if appropriate. */
28515
+ if( pCache->bPurgeable && pcache1.pLruTail && (
28516
+ pCache->nPage>=pCache->nMax-1 || pcache1.nCurrentPage>=pcache1.nMaxPage
28517
+ )){
28518
+ pPage = pcache1.pLruTail;
28519
+ pcache1RemoveFromHash(pPage);
28520
+ pcache1PinPage(pPage);
28521
+ if( pPage->pCache->szPage!=pCache->szPage ){
28522
+ pcache1FreePage(pPage);
28523
+ pPage = 0;
28524
+ }else{
28525
+ pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable);
28526
+ }
28527
+ }
28528
+
28529
+ /* Step 5. If a usable page buffer has still not been found,
28530
+ ** attempt to allocate a new one.
28531
+ */
28532
+ if( !pPage ){
28533
+ pPage = pcache1AllocPage(pCache);
28534
+ }
28535
+
28536
+ if( pPage ){
28537
+ unsigned int h = iKey % pCache->nHash;
28538
+ memset(pPage, 0, pCache->szPage + sizeof(PgHdr1));
28539
+ pCache->nPage++;
28540
+ pPage->iKey = iKey;
28541
+ pPage->pNext = pCache->apHash[h];
28542
+ pPage->pCache = pCache;
28543
+ pCache->apHash[h] = pPage;
28544
+ }
28545
+
28546
+fetch_out:
28547
+ if( createFlag==1 ) sqlite3EndBenignMalloc();
28548
+ pcache1LeaveMutex();
28549
+ return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
28550
+}
28551
+
28552
+
28553
+/*
28554
+** Implementation of the sqlite3_pcache.xUnpin method.
28555
+**
28556
+** Mark a page as unpinned (eligible for asynchronous recycling).
28557
+*/
28558
+static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
28559
+ PCache1 *pCache = (PCache1 *)p;
28560
+ PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
28561
+
28562
+ pcache1EnterMutex();
28563
+
28564
+ /* It is an error to call this function if the page is already
28565
+ ** part of the global LRU list.
28566
+ */
28567
+ assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
28568
+ assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage );
28569
+
28570
+ if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){
28571
+ pcache1RemoveFromHash(pPage);
28572
+ pcache1FreePage(pPage);
28573
+ }else{
28574
+ /* Add the page to the global LRU list. Normally, the page is added to
28575
+ ** the head of the list (last page to be recycled). However, if the
28576
+ ** reuseUnlikely flag passed to this function is true, the page is added
28577
+ ** to the tail of the list (first page to be recycled).
28578
+ */
28579
+ if( pcache1.pLruHead ){
28580
+ pcache1.pLruHead->pLruPrev = pPage;
28581
+ pPage->pLruNext = pcache1.pLruHead;
28582
+ pcache1.pLruHead = pPage;
28583
+ }else{
28584
+ pcache1.pLruTail = pPage;
28585
+ pcache1.pLruHead = pPage;
28586
+ }
28587
+ pCache->nRecyclable++;
28588
+ }
28589
+
28590
+ pcache1LeaveMutex();
28591
+}
28592
+
28593
+/*
28594
+** Implementation of the sqlite3_pcache.xRekey method.
28595
+*/
28596
+static void pcache1Rekey(
28597
+ sqlite3_pcache *p,
28598
+ void *pPg,
28599
+ unsigned int iOld,
28600
+ unsigned int iNew
28601
+){
28602
+ PCache1 *pCache = (PCache1 *)p;
28603
+ PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
28604
+ PgHdr1 **pp;
28605
+ unsigned int h;
28606
+ assert( pPage->iKey==iOld );
28607
+
28608
+ pcache1EnterMutex();
28609
+
28610
+ h = iOld%pCache->nHash;
28611
+ pp = &pCache->apHash[h];
28612
+ while( (*pp)!=pPage ){
28613
+ pp = &(*pp)->pNext;
28614
+ }
28615
+ *pp = pPage->pNext;
28616
+
28617
+ h = iNew%pCache->nHash;
28618
+ pPage->iKey = iNew;
28619
+ pPage->pNext = pCache->apHash[h];
28620
+ pCache->apHash[h] = pPage;
28621
+
28622
+ pcache1LeaveMutex();
28623
+}
28624
+
28625
+/*
28626
+** Implementation of the sqlite3_pcache.xTruncate method.
28627
+**
28628
+** Discard all unpinned pages in the cache with a page number equal to
28629
+** or greater than parameter iLimit. Any pinned pages with a page number
28630
+** equal to or greater than iLimit are implicitly unpinned.
28631
+*/
28632
+static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
28633
+ PCache1 *pCache = (PCache1 *)p;
28634
+ pcache1EnterMutex();
28635
+ pcache1TruncateUnsafe(pCache, iLimit);
28636
+ pcache1LeaveMutex();
28637
+}
28638
+
28639
+/*
28640
+** Implementation of the sqlite3_pcache.xDestroy method.
28641
+**
28642
+** Destroy a cache allocated using pcache1Create().
28643
+*/
28644
+static void pcache1Destroy(sqlite3_pcache *p){
28645
+ PCache1 *pCache = (PCache1 *)p;
28646
+ pcache1EnterMutex();
28647
+ pcache1TruncateUnsafe(pCache, 0);
28648
+ pcache1.nMaxPage -= pCache->nMax;
28649
+ pcache1.nMinPage -= pCache->nMin;
28650
+ pcache1EnforceMaxPage();
28651
+ pcache1LeaveMutex();
28652
+ sqlite3_free(pCache->apHash);
28653
+ sqlite3_free(pCache);
28654
+}
28655
+
28656
+/*
28657
+** This function is called during initialization (sqlite3_initialize()) to
28658
+** install the default pluggable cache module, assuming the user has not
28659
+** already provided an alternative.
28660
+*/
28661
+SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
28662
+ static sqlite3_pcache_methods defaultMethods = {
28663
+ 0, /* pArg */
28664
+ pcache1Init, /* xInit */
28665
+ pcache1Shutdown, /* xShutdown */
28666
+ pcache1Create, /* xCreate */
28667
+ pcache1Cachesize, /* xCachesize */
28668
+ pcache1Pagecount, /* xPagecount */
28669
+ pcache1Fetch, /* xFetch */
28670
+ pcache1Unpin, /* xUnpin */
28671
+ pcache1Rekey, /* xRekey */
28672
+ pcache1Truncate, /* xTruncate */
28673
+ pcache1Destroy /* xDestroy */
28674
+ };
28675
+ sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
2778428676
}
2778528677
2778628678
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
2778728679
/*
2778828680
** This function is called to free superfluous dynamically allocated memory
@@ -27793,44 +28685,49 @@
2779328685
** been released, the function returns. The return value is the total number
2779428686
** of bytes of memory released.
2779528687
*/
2779628688
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
2779728689
int nFree = 0;
27798
- if( pcache_g.pStart==0 ){
27799
- PgHdr *p;
27800
- pcacheEnterMutex();
27801
- while( (nReq<0 || nFree<nReq) && (p=pcacheRecyclePage()) ){
27802
- nFree += pcachePageSize(p);
27803
- pcachePageFree(p);
27804
- }
27805
- pcacheExitMutex();
28690
+ if( pcache1.pStart==0 ){
28691
+ PgHdr1 *p;
28692
+ pcache1EnterMutex();
28693
+ while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){
28694
+ nFree += sqlite3MallocSize(p);
28695
+ pcache1PinPage(p);
28696
+ pcache1RemoveFromHash(p);
28697
+ pcache1FreePage(p);
28698
+ }
28699
+ pcache1LeaveMutex();
2780628700
}
2780728701
return nFree;
2780828702
}
2780928703
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
2781028704
2781128705
#ifdef SQLITE_TEST
28706
+/*
28707
+** This function is used by test procedures to inspect the internal state
28708
+** of the global cache.
28709
+*/
2781228710
SQLITE_PRIVATE void sqlite3PcacheStats(
27813
- int *pnCurrent,
27814
- int *pnMax,
27815
- int *pnMin,
27816
- int *pnRecyclable
28711
+ int *pnCurrent, /* OUT: Total number of pages cached */
28712
+ int *pnMax, /* OUT: Global maximum cache size */
28713
+ int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
28714
+ int *pnRecyclable /* OUT: Total number of pages available for recycling */
2781728715
){
27818
- PgHdr *p;
28716
+ PgHdr1 *p;
2781928717
int nRecyclable = 0;
27820
- for(p=pcache_g.pLruHead; p; p=p->pNextLru){
28718
+ for(p=pcache1.pLruHead; p; p=p->pLruNext){
2782128719
nRecyclable++;
2782228720
}
27823
-
27824
- *pnCurrent = pcache_g.nCurrentPage;
27825
- *pnMax = pcache_g.nMaxPage;
27826
- *pnMin = pcache_g.nMinPage;
28721
+ *pnCurrent = pcache1.nCurrentPage;
28722
+ *pnMax = pcache1.nMaxPage;
28723
+ *pnMin = pcache1.nMinPage;
2782728724
*pnRecyclable = nRecyclable;
2782828725
}
2782928726
#endif
2783028727
27831
-/************** End of pcache.c **********************************************/
28728
+/************** End of pcache1.c *********************************************/
2783228729
/************** Begin file pager.c *******************************************/
2783328730
/*
2783428731
** 2001 September 15
2783528732
**
2783628733
** The author disclaims copyright to this source code. In place of
@@ -27848,11 +28745,11 @@
2784828745
** is separate from the database file. The pager also implements file
2784928746
** locking to prevent two processes from writing the same database
2785028747
** file simultaneously, or one process from reading the database while
2785128748
** another is writing.
2785228749
**
27853
-** @(#) $Id: pager.c,v 1.502 2008/11/07 00:24:54 drh Exp $
28750
+** @(#) $Id: pager.c,v 1.506 2008/11/19 18:30:29 drh Exp $
2785428751
*/
2785528752
#ifndef SQLITE_OMIT_DISKIO
2785628753
2785728754
/*
2785828755
** Macros for troubleshooting. Normally turned off
@@ -28005,15 +28902,16 @@
2800528902
u8 doNotSync; /* Boolean. While true, do not spill the cache */
2800628903
u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
2800728904
u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
2800828905
u8 dbModified; /* True if there are any changes to the Db */
2800928906
u8 changeCountDone; /* Set after incrementing the change-counter */
28907
+ u8 dbSizeValid; /* Set when dbSize is correct */
2801028908
u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
2801128909
int errCode; /* One of several kinds of errors */
28012
- int dbSize; /* Number of pages in the file */
28013
- int origDbSize; /* dbSize before the current change */
28014
- int stmtSize; /* Size of database (in pages) at stmt_begin() */
28910
+ Pgno dbSize; /* Number of pages in the file */
28911
+ Pgno origDbSize; /* dbSize before the current change */
28912
+ Pgno stmtSize; /* Size of database (in pages) at stmt_begin() */
2801528913
int nRec; /* Number of pages written to the journal */
2801628914
u32 cksumInit; /* Quasi-random value added to every checksum */
2801728915
int stmtNRec; /* Number of records in stmt subjournal */
2801828916
int nExtra; /* Add this many bytes to each in-memory page */
2801928917
int pageSize; /* Number of bytes in a page */
@@ -28026,11 +28924,12 @@
2802628924
char *zFilename; /* Name of the database file */
2802728925
char *zJournal; /* Name of the journal file */
2802828926
char *zDirectory; /* Directory hold database and journal files */
2802928927
sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
2803028928
sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
28031
- BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
28929
+ int (*xBusyHandler)(void*); /* Function to call when busy */
28930
+ void *pBusyHandlerArg; /* Context argument for xBusyHandler */
2803228931
i64 journalOff; /* Current byte offset in the journal file */
2803328932
i64 journalHdr; /* Byte offset to previous journal header */
2803428933
i64 stmtHdrOff; /* First journal header written this statement */
2803528934
i64 stmtCksum; /* cksumInit when statement was started */
2803628935
i64 stmtJSize; /* Size of journal at stmt_begin() */
@@ -28124,11 +29023,11 @@
2812429023
** used in the journal to signify that the remainder of the journal file
2812529024
** is devoted to storing a master journal name - there are no more pages to
2812629025
** roll back. See comments for function writeMasterJournal() for details.
2812729026
*/
2812829027
/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
28129
-#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
29028
+#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
2813029029
2813129030
/*
2813229031
** The maximum legal page number is (2^31 - 1).
2813329032
*/
2813429033
#define PAGER_MAX_PGNO 2147483647
@@ -28140,10 +29039,14 @@
2814029039
*/
2814129040
static int pageInStatement(PgHdr *pPg){
2814229041
Pager *pPager = pPg->pPager;
2814329042
return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
2814429043
}
29044
+
29045
+static int pageInJournal(PgHdr *pPg){
29046
+ return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
29047
+}
2814529048
2814629049
/*
2814729050
** Read a 32-bit integer from the given file descriptor. Store the integer
2814829051
** that is read in *pRes. Return SQLITE_OK if everything worked, or an
2814929052
** error code is something goes wrong.
@@ -28283,11 +29186,11 @@
2828329186
return hash;
2828429187
}
2828529188
static u32 pager_pagehash(PgHdr *pPage){
2828629189
return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
2828729190
}
28288
-static u32 pager_set_pagehash(PgHdr *pPage){
29191
+static void pager_set_pagehash(PgHdr *pPage){
2828929192
pPage->pageHash = pager_pagehash(pPage);
2829029193
}
2829129194
2829229195
/*
2829329196
** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
@@ -28320,11 +29223,11 @@
2832029223
** were present in the journal.
2832129224
**
2832229225
** If no master journal file name is present zMaster[0] is set to 0 and
2832329226
** SQLITE_OK returned.
2832429227
*/
28325
-static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
29228
+static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
2832629229
int rc;
2832729230
u32 len;
2832829231
i64 szJ;
2832929232
u32 cksum;
2833029233
u32 u; /* Unsigned loop counter */
@@ -28452,12 +29355,12 @@
2845229355
** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
2845329356
*/
2845429357
static int writeJournalHdr(Pager *pPager){
2845529358
int rc = SQLITE_OK;
2845629359
char *zHeader = pPager->pTmpSpace;
28457
- int nHeader = pPager->pageSize;
28458
- int nWrite;
29360
+ u32 nHeader = pPager->pageSize;
29361
+ u32 nWrite;
2845929362
2846029363
if( nHeader>JOURNAL_HDR_SZ(pPager) ){
2846129364
nHeader = JOURNAL_HDR_SZ(pPager);
2846229365
}
2846329366
@@ -28712,11 +29615,11 @@
2871229615
*/
2871329616
static void pager_unlock(Pager *pPager){
2871429617
if( !pPager->exclusiveMode ){
2871529618
int rc = osUnlock(pPager->fd, NO_LOCK);
2871629619
if( rc ) pPager->errCode = rc;
28717
- pPager->dbSize = -1;
29620
+ pPager->dbSizeValid = 0;
2871829621
IOTRACE(("UNLOCK %p\n", pPager))
2871929622
2872029623
/* Always close the journal file when dropping the database lock.
2872129624
** Otherwise, another connection with journal_mode=delete might
2872229625
** delete the file out from under us.
@@ -28826,17 +29729,14 @@
2882629729
}
2882729730
sqlite3BitvecDestroy(pPager->pInJournal);
2882829731
pPager->pInJournal = 0;
2882929732
sqlite3BitvecDestroy(pPager->pAlwaysRollback);
2883029733
pPager->pAlwaysRollback = 0;
28831
- sqlite3PcacheCleanAll(pPager->pPCache);
2883229734
#ifdef SQLITE_CHECK_PAGES
28833
- sqlite3PcacheIterate(pPager->pPCache, pager_set_pagehash);
29735
+ sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
2883429736
#endif
28835
- sqlite3PcacheClearFlags(pPager->pPCache,
28836
- PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC
28837
- );
29737
+ sqlite3PcacheCleanAll(pPager->pPCache);
2883829738
pPager->dirtyCache = 0;
2883929739
pPager->nRec = 0;
2884029740
}else{
2884129741
assert( pPager->pInJournal==0 );
2884229742
}
@@ -28850,11 +29750,11 @@
2885029750
pPager->origDbSize = 0;
2885129751
pPager->setMaster = 0;
2885229752
pPager->needSync = 0;
2885329753
/* lruListSetFirstSynced(pPager); */
2885429754
if( !MEMDB ){
28855
- pPager->dbSize = -1;
29755
+ pPager->dbSizeValid = 0;
2885629756
}
2885729757
pPager->dbModified = 0;
2885829758
2885929759
return (rc==SQLITE_OK?rc2:rc);
2886029760
}
@@ -29138,11 +30038,11 @@
2913830038
** operating system implementations can get confused if you try to
2913930039
** truncate a file to some size that is larger than it currently is,
2914030040
** so detect this case and write a single zero byte to the end of the new
2914130041
** file instead.
2914230042
*/
29143
-static int pager_truncate(Pager *pPager, int nPage){
30043
+static int pager_truncate(Pager *pPager, Pgno nPage){
2914430044
int rc = SQLITE_OK;
2914530045
if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
2914630046
i64 currentSize, newSize;
2914730047
rc = sqlite3OsFileSize(pPager->fd, &currentSize);
2914830048
newSize = pPager->pageSize*(i64)nPage;
@@ -29647,11 +30547,11 @@
2964730547
}
2964830548
2964930549
/* Open the pager file.
2965030550
*/
2965130551
if( zFilename && zFilename[0] && !memDb ){
29652
- if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
30552
+ if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
2965330553
rc = SQLITE_CANTOPEN;
2965430554
}else{
2965530555
int fout = 0;
2965630556
rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2965730557
pPager->vfsFlags, &fout);
@@ -29738,11 +30638,11 @@
2973830638
pPager->useJournal = useJournal;
2973930639
pPager->noReadlock = noReadlock && readOnly;
2974030640
/* pPager->stmtOpen = 0; */
2974130641
/* pPager->stmtInUse = 0; */
2974230642
/* pPager->nRef = 0; */
29743
- pPager->dbSize = memDb-1;
30643
+ pPager->dbSizeValid = memDb;
2974430644
pPager->pageSize = szPageDflt;
2974530645
/* pPager->stmtSize = 0; */
2974630646
/* pPager->stmtJSize = 0; */
2974730647
/* pPager->nPage = 0; */
2974830648
pPager->mxPage = 100;
@@ -29769,21 +30669,27 @@
2976930669
assert(pPager->fd->pMethods||tempFile);
2977030670
setSectorSize(pPager);
2977130671
if( memDb ){
2977230672
pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
2977330673
}
29774
- /* pPager->pBusyHandler = 0; */
30674
+ /* pPager->xBusyHandler = 0; */
30675
+ /* pPager->pBusyHandlerArg = 0; */
2977530676
/* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
2977630677
*ppPager = pPager;
2977730678
return SQLITE_OK;
2977830679
}
2977930680
2978030681
/*
2978130682
** Set the busy handler function.
2978230683
*/
29783
-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
29784
- pPager->pBusyHandler = pBusyHandler;
30684
+SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
30685
+ Pager *pPager,
30686
+ int (*xBusyHandler)(void *),
30687
+ void *pBusyHandlerArg
30688
+){
30689
+ pPager->xBusyHandler = xBusyHandler;
30690
+ pPager->pBusyHandlerArg = pBusyHandlerArg;
2978530691
}
2978630692
2978730693
/*
2978830694
** Set the reinitializer for this pager. If not NULL, the reinitializer
2978930695
** is called when the content of a page in cache is restored to its original
@@ -29916,11 +30822,11 @@
2991630822
assert( pPager!=0 );
2991730823
if( pPager->errCode ){
2991830824
rc = pPager->errCode;
2991930825
return rc;
2992030826
}
29921
- if( pPager->dbSize>=0 ){
30827
+ if( pPager->dbSizeValid ){
2992230828
n = pPager->dbSize;
2992330829
} else {
2992430830
assert(pPager->fd->pMethods||pPager->tempFile);
2992530831
if( (pPager->fd->pMethods)
2992630832
&& (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
@@ -29932,10 +30838,11 @@
2993230838
}else{
2993330839
n /= pPager->pageSize;
2993430840
}
2993530841
if( pPager->state!=PAGER_UNLOCK ){
2993630842
pPager->dbSize = n;
30843
+ pPager->dbSizeValid = 1;
2993730844
}
2993830845
}
2993930846
if( n==(PENDING_BYTE/pPager->pageSize) ){
2994030847
n++;
2994130848
}
@@ -29984,19 +30891,18 @@
2998430891
assert( PAGER_SHARED==SHARED_LOCK );
2998530892
assert( PAGER_RESERVED==RESERVED_LOCK );
2998630893
assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
2998730894
2998830895
/* If the file is currently unlocked then the size must be unknown */
29989
- assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 );
30896
+ assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
2999030897
2999130898
if( pPager->state>=locktype ){
2999230899
rc = SQLITE_OK;
2999330900
}else{
29994
- if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
2999530901
do {
2999630902
rc = sqlite3OsLock(pPager->fd, locktype);
29997
- }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
30903
+ }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
2999830904
if( rc==SQLITE_OK ){
2999930905
pPager->state = locktype;
3000030906
IOTRACE(("LOCK %p %d\n", pPager, locktype))
3000130907
}
3000230908
}
@@ -30011,11 +30917,11 @@
3001130917
assert( pPager->state>=PAGER_SHARED );
3001230918
3001330919
sqlite3PagerPagecount(pPager, 0);
3001430920
if( pPager->errCode ){
3001530921
rc = pPager->errCode;
30016
- }else if( nPage<(unsigned)pPager->dbSize ){
30922
+ }else if( nPage<pPager->dbSize ){
3001730923
rc = syncJournal(pPager);
3001830924
if( rc==SQLITE_OK ){
3001930925
/* Get an exclusive lock on the database before truncating. */
3002030926
rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3002130927
}
@@ -30168,22 +31074,12 @@
3016831074
}
3016931075
pPager->needSync = 0;
3017031076
3017131077
/* Erase the needSync flag from every page.
3017231078
*/
30173
- sqlite3PcacheClearFlags(pPager->pPCache, PGHDR_NEED_SYNC);
30174
- }
30175
-
30176
-#ifndef NDEBUG
30177
- /* If the Pager.needSync flag is clear then the PgHdr.needSync
30178
- ** flag must also be clear for all pages. Verify that this
30179
- ** invariant is true.
30180
- */
30181
- else{
30182
- sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_NEED_SYNC);
30183
- }
30184
-#endif
31079
+ sqlite3PcacheClearSyncFlags(pPager->pPCache);
31080
+ }
3018531081
3018631082
return rc;
3018731083
}
3018831084
3018931085
/*
@@ -30531,10 +31427,11 @@
3053131427
if( pPager->errCode ){
3053231428
rc = pPager->errCode;
3053331429
goto failed;
3053431430
}
3053531431
31432
+ assert( pPager->dbSizeValid );
3053631433
if( pPager->dbSize>0 ){
3053731434
IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
3053831435
rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
3053931436
if( rc!=SQLITE_OK ){
3054031437
goto failed;
@@ -30681,13 +31578,10 @@
3068131578
** be initialized.
3068231579
*/
3068331580
int nMax;
3068431581
PAGER_INCR(pPager->nMiss);
3068531582
pPg->pPager = pPager;
30686
- if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
30687
- pPg->flags |= PGHDR_IN_JOURNAL;
30688
- }
3068931583
memset(pPg->pExtra, 0, pPager->nExtra);
3069031584
3069131585
rc = sqlite3PagerPagecount(pPager, &nMax);
3069231586
if( rc!=SQLITE_OK ){
3069331587
sqlite3PagerUnref(pPg);
@@ -30887,11 +31781,10 @@
3088731781
assert( pPg->nRef>0 );
3088831782
assert( pPager->state!=PAGER_UNLOCK );
3088931783
if( pPager->state==PAGER_SHARED ){
3089031784
assert( pPager->pInJournal==0 );
3089131785
assert( !MEMDB );
30892
- sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_IN_JOURNAL);
3089331786
rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
3089431787
if( rc==SQLITE_OK ){
3089531788
pPager->state = PAGER_RESERVED;
3089631789
if( exFlag ){
3089731790
rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
@@ -30996,13 +31889,11 @@
3099631889
3099731890
/* Mark the page as dirty. If the page has already been written
3099831891
** to the journal then we can return right away.
3099931892
*/
3100031893
makeDirty(pPg);
31001
- if( (pPg->flags&PGHDR_IN_JOURNAL)
31002
- && (pageInStatement(pPg) || pPager->stmtInUse==0)
31003
- ){
31894
+ if( pageInJournal(pPg) && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
3100431895
pPager->dirtyCache = 1;
3100531896
pPager->dbModified = 1;
3100631897
}else{
3100731898
3100831899
/* If we get this far, it means that the page needs to be
@@ -31028,12 +31919,12 @@
3102831919
3102931920
/* The transaction journal now exists and we have a RESERVED or an
3103031921
** EXCLUSIVE lock on the main database file. Write the current page to
3103131922
** the transaction journal if it is not there already.
3103231923
*/
31033
- if( !(pPg->flags&PGHDR_IN_JOURNAL) && pPager->journalOpen ){
31034
- if( (int)pPg->pgno <= pPager->origDbSize ){
31924
+ if( !pageInJournal(pPg) && pPager->journalOpen ){
31925
+ if( pPg->pgno<=pPager->origDbSize ){
3103531926
u32 cksum;
3103631927
char *pData2;
3103731928
3103831929
/* We should never write to the journal file the page that
3103931930
** contains the database locks. The following assert verifies
@@ -31083,26 +31974,24 @@
3108331974
((pPg->flags&PGHDR_NEED_SYNC)?1:0));
3108431975
}
3108531976
if( pPg->flags&PGHDR_NEED_SYNC ){
3108631977
pPager->needSync = 1;
3108731978
}
31088
- pPg->flags |= PGHDR_IN_JOURNAL;
3108931979
}
3109031980
3109131981
/* If the statement journal is open and the page is not in it,
3109231982
** then write the current page to the statement journal. Note that
3109331983
** the statement journal format differs from the standard journal format
3109431984
** in that it omits the checksums and the header.
3109531985
*/
3109631986
if( pPager->stmtInUse
3109731987
&& !pageInStatement(pPg)
31098
- && (int)pPg->pgno<=pPager->stmtSize
31988
+ && pPg->pgno<=pPager->stmtSize
3109931989
){
3110031990
i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
3110131991
char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
31102
- assert( (pPg->flags&PGHDR_IN_JOURNAL)
31103
- || (int)pPg->pgno>pPager->origDbSize );
31992
+ assert( pageInJournal(pPg) || pPg->pgno>pPager->origDbSize );
3110431993
rc = write32bits(pPager->stfd, offset, pPg->pgno);
3110531994
if( rc==SQLITE_OK ){
3110631995
rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
3110731996
}
3110831997
PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
@@ -31116,13 +32005,13 @@
3111632005
}
3111732006
3111832007
/* Update the database size and return.
3111932008
*/
3112032009
assert( pPager->state>=PAGER_SHARED );
31121
- if( pPager->dbSize<(int)pPg->pgno ){
32010
+ if( pPager->dbSize<pPg->pgno ){
3112232011
pPager->dbSize = pPg->pgno;
31123
- if( pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
32012
+ if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
3112432013
pPager->dbSize++;
3112532014
}
3112632015
}
3112732016
return rc;
3112832017
}
@@ -31276,11 +32165,11 @@
3127632165
}
3127732166
rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
3127832167
3127932168
if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){
3128032169
assert( pPager->state>=PAGER_SHARED );
31281
- if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
32170
+ if( pPager->dbSize==pPg->pgno && pPager->origDbSize<pPager->dbSize ){
3128232171
/* If this pages is the last page in the file and the file has grown
3128332172
** during the current transaction, then do NOT mark the page as clean.
3128432173
** When the database file grows, we must make sure that the last page
3128532174
** gets written at least once so that the disk file will be the correct
3128632175
** size. If you do not write this page and the size of the file
@@ -31325,11 +32214,12 @@
3132532214
){
3132632215
return;
3132732216
}
3132832217
3132932218
#ifdef SQLITE_SECURE_DELETE
31330
- if( (pPg->flags & PGHDR_IN_JOURNAL)!=0 || (int)pPg->pgno>pPager->origDbSize ){
32219
+ if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
32220
+ || pPg->pgno>pPager->origDbSize ){
3133132221
return;
3133232222
}
3133332223
#endif
3133432224
3133532225
/* If SECURE_DELETE is disabled, then there is no way that this
@@ -31344,11 +32234,10 @@
3134432234
*/
3134532235
/* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
3134632236
3134732237
assert( pPager->pInJournal!=0 );
3134832238
sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
31349
- pPg->flags |= PGHDR_IN_JOURNAL;
3135032239
pPg->flags &= ~PGHDR_NEED_READ;
3135132240
if( pPager->stmtInUse ){
3135232241
assert( pPager->stmtSize >= pPager->origDbSize );
3135332242
sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
3135432243
}
@@ -31525,11 +32414,11 @@
3152532414
/* If this transaction has made the database smaller, then all pages
3152632415
** being discarded by the truncation must be written to the journal
3152732416
** file.
3152832417
*/
3152932418
Pgno i;
31530
- int iSkip = PAGER_MJ_PGNO(pPager);
32419
+ Pgno iSkip = PAGER_MJ_PGNO(pPager);
3153132420
for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
3153232421
if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
3153332422
rc = sqlite3PagerGet(pPager, i, &pPg);
3153432423
if( rc!=SQLITE_OK ) goto sync_exit;
3153532424
rc = sqlite3PagerWrite(pPg);
@@ -31655,11 +32544,11 @@
3165532544
}else{
3165632545
rc = pager_playback(pPager, 0);
3165732546
}
3165832547
3165932548
if( !MEMDB ){
31660
- pPager->dbSize = -1;
32549
+ pPager->dbSizeValid = 0;
3166132550
}
3166232551
3166332552
/* If an error occurs during a ROLLBACK, we can no longer trust the pager
3166432553
** cache. So call pager_error() on the way out to make any error
3166532554
** persistent.
@@ -31698,11 +32587,11 @@
3169832587
SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
3169932588
static int a[11];
3170032589
a[0] = sqlite3PcacheRefCount(pPager->pPCache);
3170132590
a[1] = sqlite3PcachePagecount(pPager->pPCache);
3170232591
a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
31703
- a[3] = pPager->dbSize;
32592
+ a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
3170432593
a[4] = pPager->state;
3170532594
a[5] = pPager->errCode;
3170632595
a[6] = pPager->nHit;
3170732596
a[7] = pPager->nMiss;
3170832597
a[8] = 0; /* Used to be pPager->nOvfl */
@@ -31724,11 +32613,11 @@
3172432613
*/
3172532614
static int pagerStmtBegin(Pager *pPager){
3172632615
int rc;
3172732616
assert( !pPager->stmtInUse );
3172832617
assert( pPager->state>=PAGER_SHARED );
31729
- assert( pPager->dbSize>=0 );
32618
+ assert( pPager->dbSizeValid );
3173032619
PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
3173132620
if( !pPager->journalOpen ){
3173232621
pPager->stmtAutoopen = 1;
3173332622
return SQLITE_OK;
3173432623
}
@@ -31906,34 +32795,30 @@
3190632795
** the journal needs to be sync()ed before database page pPg->pgno
3190732796
** can be written to. The caller has already promised not to write to it.
3190832797
*/
3190932798
if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
3191032799
needSyncPgno = pPg->pgno;
31911
- assert( (pPg->flags&PGHDR_IN_JOURNAL) || (int)pgno>pPager->origDbSize );
32800
+ assert( pageInJournal(pPg) || pgno>pPager->origDbSize );
3191232801
assert( pPg->flags&PGHDR_DIRTY );
3191332802
assert( pPager->needSync );
3191432803
}
3191532804
3191632805
/* If the cache contains a page with page-number pgno, remove it
3191732806
** from its hash chain. Also, if the PgHdr.needSync was set for
3191832807
** page pgno before the 'move' operation, it needs to be retained
3191932808
** for the page moved there.
3192032809
*/
31921
- pPg->flags &= ~(PGHDR_NEED_SYNC|PGHDR_IN_JOURNAL);
32810
+ pPg->flags &= ~PGHDR_NEED_SYNC;
3192232811
pPgOld = pager_lookup(pPager, pgno);
3192332812
assert( !pPgOld || pPgOld->nRef==1 );
3192432813
if( pPgOld ){
3192532814
pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
3192632815
}
31927
- if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
31928
- pPg->flags |= PGHDR_IN_JOURNAL;
31929
- }
3193032816
3193132817
sqlite3PcacheMove(pPg, pgno);
3193232818
if( pPgOld ){
31933
- sqlite3PcacheMove(pPgOld, 0);
31934
- sqlite3PcacheRelease(pPgOld);
32819
+ sqlite3PcacheDrop(pPgOld);
3193532820
}
3193632821
3193732822
makeDirty(pPg);
3193832823
pPager->dirtyCache = 1;
3193932824
pPager->dbModified = 1;
@@ -31959,19 +32844,18 @@
3195932844
int rc;
3196032845
PgHdr *pPgHdr;
3196132846
assert( pPager->needSync );
3196232847
rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
3196332848
if( rc!=SQLITE_OK ){
31964
- if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
32849
+ if( pPager->pInJournal && needSyncPgno<=pPager->origDbSize ){
3196532850
sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
3196632851
}
3196732852
return rc;
3196832853
}
3196932854
pPager->needSync = 1;
3197032855
assert( pPager->noSync==0 && !MEMDB );
3197132856
pPgHdr->flags |= PGHDR_NEED_SYNC;
31972
- pPgHdr->flags |= PGHDR_IN_JOURNAL;
3197332857
makeDirty(pPgHdr);
3197432858
sqlite3PagerUnref(pPgHdr);
3197532859
}
3197632860
3197732861
return SQLITE_OK;
@@ -32074,11 +32958,11 @@
3207432958
** May you find forgiveness for yourself and forgive others.
3207532959
** May you share freely, never taking more than you give.
3207632960
**
3207732961
*************************************************************************
3207832962
**
32079
-** $Id: btmutex.c,v 1.11 2008/10/07 15:25:48 drh Exp $
32963
+** $Id: btmutex.c,v 1.12 2008/11/17 19:18:55 danielk1977 Exp $
3208032964
**
3208132965
** This file contains code used to implement mutexes on Btree objects.
3208232966
** This code really belongs in btree.c. But btree.c is getting too
3208332967
** big and we want to break it down some. This packaged seemed like
3208432968
** a good breakout.
@@ -32094,11 +32978,11 @@
3209432978
** May you do good and not evil.
3209532979
** May you find forgiveness for yourself and forgive others.
3209632980
** May you share freely, never taking more than you give.
3209732981
**
3209832982
*************************************************************************
32099
-** $Id: btreeInt.h,v 1.34 2008/09/30 17:18:17 drh Exp $
32983
+** $Id: btreeInt.h,v 1.36 2008/11/19 10:22:33 danielk1977 Exp $
3210032984
**
3210132985
** This file implements a external (disk-based) database using BTrees.
3210232986
** For a detailed discussion of BTrees, refer to
3210332987
**
3210432988
** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -32464,11 +33348,10 @@
3246433348
u8 inTransaction; /* Transaction state */
3246533349
int nTransaction; /* Number of open transactions (read + write) */
3246633350
void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
3246733351
void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
3246833352
sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
32469
- BusyHandler busyHdr; /* The busy handler for this btree */
3247033353
#ifndef SQLITE_OMIT_SHARED_CACHE
3247133354
int nRef; /* Number of references to this structure */
3247233355
BtShared *pNext; /* Next on a list of sharable BtShared structs */
3247333356
BtLock *pLock; /* List of locks held on this shared-btree struct */
3247433357
Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */
@@ -32582,11 +33465,11 @@
3258233465
** in memory) then there is no pending byte.
3258333466
*/
3258433467
#ifdef SQLITE_OMIT_DISKIO
3258533468
# define PENDING_BYTE_PAGE(pBt) 0x7fffffff
3258633469
#else
32587
-# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
33470
+# define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/(pBt)->pageSize)+1))
3258833471
#endif
3258933472
3259033473
/*
3259133474
** A linked list of the following structures is stored at BtShared.pLock.
3259233475
** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
@@ -32689,11 +33572,11 @@
3268933572
*/
3269033573
typedef struct IntegrityCk IntegrityCk;
3269133574
struct IntegrityCk {
3269233575
BtShared *pBt; /* The tree being checked out */
3269333576
Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
32694
- int nPage; /* Number of pages in the database */
33577
+ Pgno nPage; /* Number of pages in the database */
3269533578
int *anRef; /* Number of times each page is referenced */
3269633579
int mxErr; /* Stop accumulating errors when this reaches zero */
3269733580
int nErr; /* Number of messages written to zErrMsg so far */
3269833581
int mallocFailed; /* A memory allocation error has occurred */
3269933582
StrAccum errMsg; /* Accumulate the error message text here */
@@ -32949,11 +33832,11 @@
3294933832
assert( pArray->aBtree[i]!=pBtree );
3295033833
}
3295133834
}
3295233835
#endif
3295333836
assert( pArray->nMutex>=0 );
32954
- assert( pArray->nMutex<sizeof(pArray->aBtree)/sizeof(pArray->aBtree[0])-1 );
33837
+ assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
3295533838
pBt = pBtree->pBt;
3295633839
for(i=0; i<pArray->nMutex; i++){
3295733840
assert( pArray->aBtree[i]!=pBtree );
3295833841
if( pArray->aBtree[i]->pBt>pBt ){
3295933842
for(j=pArray->nMutex; j>i; j--){
@@ -33028,11 +33911,11 @@
3302833911
** May you do good and not evil.
3302933912
** May you find forgiveness for yourself and forgive others.
3303033913
** May you share freely, never taking more than you give.
3303133914
**
3303233915
*************************************************************************
33033
-** $Id: btree.c,v 1.527 2008/11/03 20:55:07 drh Exp $
33916
+** $Id: btree.c,v 1.539.2.1 2008/11/22 14:07:49 drh Exp $
3303433917
**
3303533918
** This file implements a external (disk-based) database using BTrees.
3303633919
** See the header comment on "btreeInt.h" for additional information.
3303733920
** Including a description of file format and an overview of operation.
3303833921
*/
@@ -33453,11 +34336,12 @@
3345334336
** Given a page number of a regular database page, return the page
3345434337
** number for the pointer-map page that contains the entry for the
3345534338
** input page number.
3345634339
*/
3345734340
static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
33458
- int nPagesPerMapPage, iPtrMap, ret;
34341
+ int nPagesPerMapPage;
34342
+ Pgno iPtrMap, ret;
3345934343
assert( sqlite3_mutex_held(pBt->mutex) );
3346034344
nPagesPerMapPage = (pBt->usableSize/5)+1;
3346134345
iPtrMap = (pgno-2)/nPagesPerMapPage;
3346234346
ret = (iPtrMap*nPagesPerMapPage) + 2;
3346334347
if( ret==PENDING_BYTE_PAGE(pBt) ){
@@ -33720,11 +34604,11 @@
3372034604
** Defragment the page given. All Cells are moved to the
3372134605
** end of the page and all free space is collected into one
3372234606
** big FreeBlk that occurs in between the header and cell
3372334607
** pointer array and the cell content area.
3372434608
*/
33725
-static void defragmentPage(MemPage *pPage){
34609
+static int defragmentPage(MemPage *pPage){
3372634610
int i; /* Loop counter */
3372734611
int pc; /* Address of a i-th cell */
3372834612
int addr; /* Offset of first byte after cell pointer array */
3372934613
int hdr; /* Offset to the page header */
3373034614
int size; /* Size of a cell */
@@ -33752,13 +34636,19 @@
3375234636
cbrk = usableSize;
3375334637
for(i=0; i<nCell; i++){
3375434638
u8 *pAddr; /* The i-th cell pointer */
3375534639
pAddr = &data[cellOffset + i*2];
3375634640
pc = get2byte(pAddr);
33757
- assert( pc<pPage->pBt->usableSize );
34641
+ if( pc>=usableSize ){
34642
+ return SQLITE_CORRUPT_BKPT;
34643
+ }
3375834644
size = cellSizePtr(pPage, &temp[pc]);
3375934645
cbrk -= size;
34646
+ if( cbrk<cellOffset+2*nCell || pc+size>usableSize ){
34647
+ return SQLITE_CORRUPT_BKPT;
34648
+ }
34649
+ assert( cbrk+size<=usableSize && cbrk>=0 );
3376034650
memcpy(&data[cbrk], &temp[pc], size);
3376134651
put2byte(pAddr, cbrk);
3376234652
}
3376334653
assert( cbrk>=cellOffset+2*nCell );
3376434654
put2byte(&data[hdr+5], cbrk);
@@ -33765,10 +34655,14 @@
3376534655
data[hdr+1] = 0;
3376634656
data[hdr+2] = 0;
3376734657
data[hdr+7] = 0;
3376834658
addr = cellOffset+2*nCell;
3376934659
memset(&data[addr], 0, cbrk-addr);
34660
+ if( cbrk-addr!=pPage->nFree ){
34661
+ return SQLITE_CORRUPT_BKPT;
34662
+ }
34663
+ return SQLITE_OK;
3377034664
}
3377134665
3377234666
/*
3377334667
** Allocate nByte bytes of space on a page.
3377434668
**
@@ -33843,11 +34737,11 @@
3384334737
** and the size of the block is "size" bytes.
3384434738
**
3384534739
** Most of the effort here is involved in coalesing adjacent
3384634740
** free blocks into a single big free block.
3384734741
*/
33848
-static void freeSpace(MemPage *pPage, int start, int size){
34742
+static int freeSpace(MemPage *pPage, int start, int size){
3384934743
int addr, pbegin, hdr;
3385034744
unsigned char *data = pPage->aData;
3385134745
3385234746
assert( pPage->pBt!=0 );
3385334747
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -33865,14 +34759,18 @@
3386534759
/* Add the space back into the linked list of freeblocks */
3386634760
hdr = pPage->hdrOffset;
3386734761
addr = hdr + 1;
3386834762
while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
3386934763
assert( pbegin<=pPage->pBt->usableSize-4 );
33870
- assert( pbegin>addr );
34764
+ if( pbegin<=addr ) {
34765
+ return SQLITE_CORRUPT_BKPT;
34766
+ }
3387134767
addr = pbegin;
3387234768
}
33873
- assert( pbegin<=pPage->pBt->usableSize-4 );
34769
+ if ( pbegin>pPage->pBt->usableSize-4 ) {
34770
+ return SQLITE_CORRUPT_BKPT;
34771
+ }
3387434772
assert( pbegin>addr || pbegin==0 );
3387534773
put2byte(&data[addr], start);
3387634774
put2byte(&data[start], pbegin);
3387734775
put2byte(&data[start+2], size);
3387834776
pPage->nFree += size;
@@ -33885,11 +34783,13 @@
3388534783
assert( pbegin<=pPage->pBt->usableSize-4 );
3388634784
pnext = get2byte(&data[pbegin]);
3388734785
psize = get2byte(&data[pbegin+2]);
3388834786
if( pbegin + psize + 3 >= pnext && pnext>0 ){
3388934787
int frag = pnext - (pbegin+psize);
33890
- assert( frag<=data[pPage->hdrOffset+7] );
34788
+ if( (frag<0) || (frag>data[pPage->hdrOffset+7]) ){
34789
+ return SQLITE_CORRUPT_BKPT;
34790
+ }
3389134791
data[pPage->hdrOffset+7] -= frag;
3389234792
put2byte(&data[pbegin], get2byte(&data[pnext]));
3389334793
put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
3389434794
}else{
3389534795
addr = pbegin;
@@ -33902,10 +34802,11 @@
3390234802
pbegin = get2byte(&data[hdr+1]);
3390334803
memcpy(&data[hdr+1], &data[pbegin], 2);
3390434804
top = get2byte(&data[hdr+5]);
3390534805
put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
3390634806
}
34807
+ return SQLITE_OK;
3390734808
}
3390834809
3390934810
/*
3391034811
** Decode the flags byte (the first byte of the header) for a page
3391134812
** and initialize fields of the MemPage structure accordingly.
@@ -34109,18 +35010,20 @@
3410935010
*ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
3411035011
return SQLITE_OK;
3411135012
}
3411235013
3411335014
/*
34114
-** Return the size of the database file in pages. Or return -1 if
34115
-** there is any kind of error.
35015
+** Return the size of the database file in pages. If there is any kind of
35016
+** error, return ((unsigned int)-1).
3411635017
*/
34117
-static int pagerPagecount(Pager *pPager){
35018
+static Pgno pagerPagecount(BtShared *pBt){
35019
+ int nPage = -1;
3411835020
int rc;
34119
- int nPage;
34120
- rc = sqlite3PagerPagecount(pPager, &nPage);
34121
- return (rc==SQLITE_OK?nPage:-1);
35021
+ assert( pBt->pPage1 );
35022
+ rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
35023
+ assert( rc==SQLITE_OK || nPage==-1 );
35024
+ return (Pgno)nPage;
3412235025
}
3412335026
3412435027
/*
3412535028
** Get a page from the pager and initialize it. This routine
3412635029
** is just a convenience wrapper around separate calls to
@@ -34150,11 +35053,11 @@
3415035053
/* Page is already in cache */
3415135054
*ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt);
3415235055
rc = SQLITE_OK;
3415335056
}else{
3415435057
/* Page not in cache. Acquire it. */
34155
- if( pgno>pagerPagecount(pBt->pPager) ){
35058
+ if( pgno>pagerPagecount(pBt) ){
3415635059
return SQLITE_CORRUPT_BKPT;
3415735060
}
3415835061
rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
3415935062
if( rc ) return rc;
3416035063
pPage = *ppPage;
@@ -34205,11 +35108,11 @@
3420535108
}
3420635109
3420735110
/*
3420835111
** Invoke the busy handler for a btree.
3420935112
*/
34210
-static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){
35113
+static int btreeInvokeBusyHandler(void *pArg){
3421135114
BtShared *pBt = (BtShared*)pArg;
3421235115
assert( pBt->db );
3421335116
assert( sqlite3_mutex_held(pBt->db->mutex) );
3421435117
return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
3421535118
}
@@ -34322,21 +35225,19 @@
3432235225
pBt = sqlite3MallocZero( sizeof(*pBt) );
3432335226
if( pBt==0 ){
3432435227
rc = SQLITE_NOMEM;
3432535228
goto btree_open_out;
3432635229
}
34327
- pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler;
34328
- pBt->busyHdr.pArg = pBt;
3432935230
rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
3433035231
EXTRA_SIZE, flags, vfsFlags);
3433135232
if( rc==SQLITE_OK ){
3433235233
rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
3433335234
}
3433435235
if( rc!=SQLITE_OK ){
3433535236
goto btree_open_out;
3433635237
}
34337
- sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr);
35238
+ sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
3433835239
p->pBt = pBt;
3433935240
3434035241
sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
3434135242
pBt->pCursor = 0;
3434235243
pBt->pPage1 = 0;
@@ -35030,11 +35931,11 @@
3503035931
if( wrflag ) pBt->inStmt = 0;
3503135932
}else{
3503235933
unlockBtreeIfUnused(pBt);
3503335934
}
3503435935
}while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
35035
- sqlite3BtreeInvokeBusyHandler(pBt, 0) );
35936
+ btreeInvokeBusyHandler(pBt) );
3503635937
3503735938
if( rc==SQLITE_OK ){
3503835939
if( p->inTrans==TRANS_NONE ){
3503935940
pBt->nTransaction++;
3504035941
}
@@ -35054,11 +35955,10 @@
3505435955
trans_begun:
3505535956
btreeIntegrity(p);
3505635957
sqlite3BtreeLeave(p);
3505735958
return rc;
3505835959
}
35059
-
3506035960
3506135961
#ifndef SQLITE_OMIT_AUTOVACUUM
3506235962
3506335963
/*
3506435964
** Set the pointer-map entries for all children of page pPage. Also, if
@@ -35269,11 +36169,11 @@
3526936169
Pgno nFreeList; /* Number of pages still on the free-list */
3527036170
3527136171
assert( sqlite3_mutex_held(pBt->mutex) );
3527236172
iLastPg = pBt->nTrunc;
3527336173
if( iLastPg==0 ){
35274
- iLastPg = pagerPagecount(pBt->pPager);
36174
+ iLastPg = pagerPagecount(pBt);
3527536175
}
3527636176
3527736177
if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
3527836178
int rc;
3527936179
u8 eType;
@@ -35400,11 +36300,11 @@
3540036300
3540136301
if( pBt->nTrunc==0 ){
3540236302
Pgno nFree;
3540336303
Pgno nPtrmap;
3540436304
const int pgsz = pBt->pageSize;
35405
- int nOrig = pagerPagecount(pBt->pPager);
36305
+ Pgno nOrig = pagerPagecount(pBt);
3540636306
3540736307
if( PTRMAP_ISPAGE(pBt, nOrig) ){
3540836308
return SQLITE_CORRUPT_BKPT;
3540936309
}
3541036310
if( nOrig==PENDING_BYTE_PAGE(pBt) ){
@@ -35445,11 +36345,11 @@
3544536345
}
3544636346
assert( nRef==sqlite3PagerRefcount(pPager) );
3544736347
return rc;
3544836348
}
3544936349
35450
-#endif
36350
+#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
3545136351
3545236352
/*
3545336353
** This routine does the first phase of a two-phase commit. This routine
3545436354
** causes a rollback journal to be created (if it does not already exist)
3545536355
** and populated with enough information so that if a power loss occurs
@@ -35612,13 +36512,18 @@
3561236512
*/
3561336513
SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
3561436514
BtCursor *p;
3561536515
sqlite3BtreeEnter(pBtree);
3561636516
for(p=pBtree->pBt->pCursor; p; p=p->pNext){
36517
+ int i;
3561736518
sqlite3BtreeClearCursor(p);
3561836519
p->eState = CURSOR_FAULT;
3561936520
p->skip = errCode;
36521
+ for(i=0; i<=p->iPage; i++){
36522
+ releasePage(p->apPage[i]);
36523
+ p->apPage[i] = 0;
36524
+ }
3562036525
}
3562136526
sqlite3BtreeLeave(pBtree);
3562236527
}
3562336528
3562436529
/*
@@ -35801,10 +36706,11 @@
3580136706
int wrFlag, /* 1 to write. 0 read-only */
3580236707
struct KeyInfo *pKeyInfo, /* First arg to comparison function */
3580336708
BtCursor *pCur /* Space for new cursor */
3580436709
){
3580536710
int rc;
36711
+ Pgno nPage;
3580636712
BtShared *pBt = p->pBt;
3580736713
3580836714
assert( sqlite3BtreeHoldsMutex(p) );
3580936715
if( wrFlag ){
3581036716
if( pBt->readOnly ){
@@ -35823,11 +36729,15 @@
3582336729
if( pBt->readOnly && wrFlag ){
3582436730
return SQLITE_READONLY;
3582536731
}
3582636732
}
3582736733
pCur->pgnoRoot = (Pgno)iTable;
35828
- if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){
36734
+ rc = sqlite3PagerPagecount(pBt->pPager, (int *)&nPage);
36735
+ if( rc!=SQLITE_OK ){
36736
+ return rc;
36737
+ }
36738
+ if( iTable==1 && nPage==0 ){
3582936739
rc = SQLITE_EMPTY;
3583036740
goto create_cursor_exception;
3583136741
}
3583236742
rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
3583336743
if( rc!=SQLITE_OK ){
@@ -36084,11 +36994,11 @@
3608436994
3608536995
while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
3608636996
iGuess++;
3608736997
}
3608836998
36089
- if( iGuess<=pagerPagecount(pBt->pPager) ){
36999
+ if( iGuess<=pagerPagecount(pBt) ){
3609037000
rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
3609137001
if( rc!=SQLITE_OK ){
3609237002
return rc;
3609337003
}
3609437004
if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
@@ -36180,37 +37090,38 @@
3618037090
** * A commit in auto_vacuum="full" mode,
3618137091
** * Creating a table (may require moving an overflow page).
3618237092
*/
3618337093
static int accessPayload(
3618437094
BtCursor *pCur, /* Cursor pointing to entry to read from */
36185
- int offset, /* Begin reading this far into payload */
36186
- int amt, /* Read this many bytes */
37095
+ u32 offset, /* Begin reading this far into payload */
37096
+ u32 amt, /* Read this many bytes */
3618737097
unsigned char *pBuf, /* Write the bytes into this buffer */
3618837098
int skipKey, /* offset begins at data if this is true */
3618937099
int eOp /* zero to read. non-zero to write. */
3619037100
){
3619137101
unsigned char *aPayload;
3619237102
int rc = SQLITE_OK;
3619337103
u32 nKey;
3619437104
int iIdx = 0;
3619537105
MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
36196
- BtShared *pBt; /* Btree this cursor belongs to */
37106
+ BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
3619737107
3619837108
assert( pPage );
3619937109
assert( pCur->eState==CURSOR_VALID );
3620037110
assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
36201
- assert( offset>=0 );
3620237111
assert( cursorHoldsMutex(pCur) );
3620337112
3620437113
getCellInfo(pCur);
3620537114
aPayload = pCur->info.pCell + pCur->info.nHeader;
3620637115
nKey = (pPage->intKey ? 0 : pCur->info.nKey);
3620737116
3620837117
if( skipKey ){
3620937118
offset += nKey;
3621037119
}
36211
- if( offset+amt > nKey+pCur->info.nData ){
37120
+ if( offset+amt > nKey+pCur->info.nData
37121
+ || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
37122
+ ){
3621237123
/* Trying to read or write past the end of the data is an error */
3621337124
return SQLITE_CORRUPT_BKPT;
3621437125
}
3621537126
3621637127
/* Check if data must be read/written to/from the btree page itself. */
@@ -36225,13 +37136,12 @@
3622537136
amt -= a;
3622637137
}else{
3622737138
offset -= pCur->info.nLocal;
3622837139
}
3622937140
36230
- pBt = pCur->pBt;
3623137141
if( rc==SQLITE_OK && amt>0 ){
36232
- const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
37142
+ const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
3623337143
Pgno nextPage;
3623437144
3623537145
nextPage = get4byte(&aPayload[pCur->info.nLocal]);
3623637146
3623737147
#ifndef SQLITE_OMIT_INCRBLOB
@@ -36394,11 +37304,11 @@
3639437304
int skipKey /* read beginning at data if this is true */
3639537305
){
3639637306
unsigned char *aPayload;
3639737307
MemPage *pPage;
3639837308
u32 nKey;
36399
- int nLocal;
37309
+ u32 nLocal;
3640037310
3640137311
assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
3640237312
assert( pCur->eState==CURSOR_VALID );
3640337313
assert( cursorHoldsMutex(pCur) );
3640437314
pPage = pCur->apPage[pCur->iPage];
@@ -37084,11 +37994,11 @@
3708437994
/* If the 'exact' parameter was true and a query of the pointer-map
3708537995
** shows that the page 'nearby' is somewhere on the free-list, then
3708637996
** the entire-list will be searched for that page.
3708737997
*/
3708837998
#ifndef SQLITE_OMIT_AUTOVACUUM
37089
- if( exact && nearby<=pagerPagecount(pBt->pPager) ){
37999
+ if( exact && nearby<=pagerPagecount(pBt) ){
3709038000
u8 eType;
3709138001
assert( nearby>0 );
3709238002
assert( pBt->autoVacuum );
3709338003
rc = ptrmapGet(pBt, nearby, &eType, 0);
3709438004
if( rc ) return rc;
@@ -37219,13 +38129,13 @@
3721938129
closest = 0;
3722038130
}
3722138131
3722238132
iPage = get4byte(&aData[8+closest*4]);
3722338133
if( !searchList || iPage==nearby ){
37224
- int nPage;
38134
+ Pgno nPage;
3722538135
*pPgno = iPage;
37226
- nPage = pagerPagecount(pBt->pPager);
38136
+ nPage = pagerPagecount(pBt);
3722738137
if( *pPgno>nPage ){
3722838138
/* Free page off the end of the file */
3722938139
rc = SQLITE_CORRUPT_BKPT;
3723038140
goto end_allocate_page;
3723138141
}
@@ -37251,11 +38161,11 @@
3725138161
pPrevTrunk = 0;
3725238162
}while( searchList );
3725338163
}else{
3725438164
/* There are no pages on the freelist, so create a new page at the
3725538165
** end of the file */
37256
- int nPage = pagerPagecount(pBt->pPager);
38166
+ int nPage = pagerPagecount(pBt);
3725738167
*pPgno = nPage + 1;
3725838168
3725938169
#ifndef SQLITE_OMIT_AUTOVACUUM
3726038170
if( pBt->nTrunc ){
3726138171
/* An incr-vacuum has already run within this transaction. So the
@@ -37295,13 +38205,16 @@
3729538205
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
3729638206
3729738207
end_allocate_page:
3729838208
releasePage(pTrunk);
3729938209
releasePage(pPrevTrunk);
37300
- if( rc==SQLITE_OK && sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
37301
- releasePage(*ppPage);
37302
- return SQLITE_CORRUPT_BKPT;
38210
+ if( rc==SQLITE_OK ){
38211
+ if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
38212
+ releasePage(*ppPage);
38213
+ return SQLITE_CORRUPT_BKPT;
38214
+ }
38215
+ (*ppPage)->isInit = 0;
3730338216
}
3730438217
return rc;
3730538218
}
3730638219
3730738220
/*
@@ -37418,11 +38331,11 @@
3741838331
ovflPageSize = pBt->usableSize - 4;
3741938332
nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
3742038333
assert( ovflPgno==0 || nOvfl>0 );
3742138334
while( nOvfl-- ){
3742238335
MemPage *pOvfl;
37423
- if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){
38336
+ if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){
3742438337
return SQLITE_CORRUPT_BKPT;
3742538338
}
3742638339
3742738340
rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
3742838341
if( rc ) return rc;
@@ -37480,11 +38393,11 @@
3748038393
}
3748138394
nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
3748238395
sqlite3BtreeParseCellPtr(pPage, pCell, &info);
3748338396
assert( info.nHeader==nHeader );
3748438397
assert( info.nKey==nKey );
37485
- assert( info.nData==nData+nZero );
38398
+ assert( info.nData==(u32)(nData+nZero) );
3748638399
3748738400
/* Fill in the payload */
3748838401
nPayload = nData + nZero;
3748938402
if( pPage->intKey ){
3749038403
pSrc = pData;
@@ -37577,32 +38490,39 @@
3757738490
** the cell content has been copied someplace else. This routine just
3757838491
** removes the reference to the cell from pPage.
3757938492
**
3758038493
** "sz" must be the number of bytes in the cell.
3758138494
*/
37582
-static void dropCell(MemPage *pPage, int idx, int sz){
38495
+static int dropCell(MemPage *pPage, int idx, int sz){
3758338496
int i; /* Loop counter */
3758438497
int pc; /* Offset to cell content of cell being deleted */
3758538498
u8 *data; /* pPage->aData */
3758638499
u8 *ptr; /* Used to move bytes around within data[] */
38500
+ int rc; /* The return code */
3758738501
3758838502
assert( idx>=0 && idx<pPage->nCell );
3758938503
assert( sz==cellSize(pPage, idx) );
3759038504
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
3759138505
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3759238506
data = pPage->aData;
3759338507
ptr = &data[pPage->cellOffset + 2*idx];
3759438508
pc = get2byte(ptr);
37595
- assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
37596
- freeSpace(pPage, pc, sz);
38509
+ if ( (pc<pPage->hdrOffset+6+(pPage->leaf?0:4)) || (pc+sz>pPage->pBt->usableSize) ) {
38510
+ return SQLITE_CORRUPT_BKPT;
38511
+ }
38512
+ rc = freeSpace(pPage, pc, sz);
38513
+ if( rc!=SQLITE_OK ){
38514
+ return rc;
38515
+ }
3759738516
for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
3759838517
ptr[0] = ptr[2];
3759938518
ptr[1] = ptr[3];
3760038519
}
3760138520
pPage->nCell--;
3760238521
put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
3760338522
pPage->nFree += 2;
38523
+ return SQLITE_OK;
3760438524
}
3760538525
3760638526
/*
3760738527
** Insert a new cell on pPage at cell index "i". pCell points to the
3760838528
** content of the cell.
@@ -37645,11 +38565,11 @@
3764538565
if( pTemp ){
3764638566
memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
3764738567
pCell = pTemp;
3764838568
}
3764938569
j = pPage->nOverflow++;
37650
- assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
38570
+ assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
3765138571
pPage->aOvfl[j].pCell = pCell;
3765238572
pPage->aOvfl[j].idx = i;
3765338573
pPage->nFree = 0;
3765438574
}else{
3765538575
int rc = sqlite3PagerWrite(pPage->pDbPage);
@@ -37662,17 +38582,23 @@
3766238582
top = get2byte(&data[hdr+5]);
3766338583
cellOffset = pPage->cellOffset;
3766438584
end = cellOffset + 2*pPage->nCell + 2;
3766538585
ins = cellOffset + 2*i;
3766638586
if( end > top - sz ){
37667
- defragmentPage(pPage);
38587
+ rc = defragmentPage(pPage);
38588
+ if( rc!=SQLITE_OK ){
38589
+ return rc;
38590
+ }
3766838591
top = get2byte(&data[hdr+5]);
3766938592
assert( end + sz <= top );
3767038593
}
3767138594
idx = allocateSpace(pPage, sz);
3767238595
assert( idx>0 );
3767338596
assert( end <= get2byte(&data[hdr+5]) );
38597
+ if (idx+sz > pPage->pBt->usableSize) {
38598
+ return SQLITE_CORRUPT_BKPT;
38599
+ }
3767438600
pPage->nCell++;
3767538601
pPage->nFree -= 2;
3767638602
memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
3767738603
for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
3767838604
ptr[0] = ptr[-2];
@@ -38526,11 +39452,11 @@
3852639452
** the virtual root of the tree.
3852739453
*/
3852839454
VVA_ONLY( pCur->pagesShuffled = 1 );
3852939455
pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
3853039456
assert( pgnoChild>0 );
38531
- assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) );
39457
+ assert( pgnoChild<=pagerPagecount(pPage->pBt) );
3853239458
rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
3853339459
if( rc ) goto end_shallow_balance;
3853439460
if( pPage->pgno==1 ){
3853539461
rc = sqlite3BtreeInitPage(pChild);
3853639462
if( rc ) goto end_shallow_balance;
@@ -38563,13 +39489,15 @@
3856339489
freePage(pChild);
3856439490
TRACE(("BALANCE: transfer child %d into root %d\n",
3856539491
pChild->pgno, pPage->pgno));
3856639492
}
3856739493
assert( pPage->nOverflow==0 );
39494
+#ifndef SQLITE_OMIT_AUTOVACUUM
3856839495
if( ISAUTOVACUUM ){
3856939496
rc = setChildPtrmaps(pPage);
3857039497
}
39498
+#endif
3857139499
releasePage(pChild);
3857239500
}
3857339501
end_shallow_balance:
3857439502
sqlite3_free(apCell);
3857539503
return rc;
@@ -38612,11 +39540,12 @@
3861239540
hdr = pPage->hdrOffset;
3861339541
cbrk = get2byte(&data[hdr+5]);
3861439542
cdata = pChild->aData;
3861539543
memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
3861639544
memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk);
38617
-
39545
+
39546
+ assert( pChild->isInit==0 );
3861839547
rc = sqlite3BtreeInitPage(pChild);
3861939548
if( rc==SQLITE_OK ){
3862039549
int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]);
3862139550
memcpy(pChild->aOvfl, pPage->aOvfl, nCopy);
3862239551
pChild->nOverflow = pPage->nOverflow;
@@ -38627,13 +39556,15 @@
3862739556
zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
3862839557
put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
3862939558
TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
3863039559
if( ISAUTOVACUUM ){
3863139560
rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
39561
+#ifndef SQLITE_OMIT_AUTOVACUUM
3863239562
if( rc==SQLITE_OK ){
3863339563
rc = setChildPtrmaps(pChild);
3863439564
}
39565
+#endif
3863539566
}
3863639567
}
3863739568
3863839569
if( rc==SQLITE_OK ){
3863939570
pCur->iPage++;
@@ -38826,11 +39757,14 @@
3882639757
memcpy(newCell, oldCell, 4);
3882739758
}
3882839759
szOld = cellSizePtr(pPage, oldCell);
3882939760
rc = clearCell(pPage, oldCell);
3883039761
if( rc ) goto end_insert;
38831
- dropCell(pPage, idx, szOld);
39762
+ rc = dropCell(pPage, idx, szOld);
39763
+ if( rc!=SQLITE_OK ) {
39764
+ goto end_insert;
39765
+ }
3883239766
}else if( loc<0 && pPage->nCell>0 ){
3883339767
assert( pPage->leaf );
3883439768
idx = ++pCur->aiIdx[pCur->iPage];
3883539769
pCur->info.nSize = 0;
3883639770
pCur->validNKey = 0;
@@ -39037,12 +39971,14 @@
3903739971
}
3903839972
sqlite3BtreeReleaseTempCursor(&leafCur);
3903939973
}else{
3904039974
TRACE(("DELETE: table=%d delete from leaf %d\n",
3904139975
pCur->pgnoRoot, pPage->pgno));
39042
- dropCell(pPage, idx, cellSizePtr(pPage, pCell));
39043
- rc = balance(pCur, 0);
39976
+ rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell));
39977
+ if( rc==SQLITE_OK ){
39978
+ rc = balance(pCur, 0);
39979
+ }
3904439980
}
3904539981
if( rc==SQLITE_OK ){
3904639982
moveToRoot(pCur);
3904739983
}
3904839984
return rc;
@@ -39204,37 +40140,36 @@
3920440140
** the page to the freelist.
3920540141
*/
3920640142
static int clearDatabasePage(
3920740143
BtShared *pBt, /* The BTree that contains the table */
3920840144
Pgno pgno, /* Page number to clear */
39209
- MemPage *pParent, /* Parent page. NULL for the root */
3921040145
int freePageFlag, /* Deallocate page if true */
3921140146
int *pnChange
3921240147
){
3921340148
MemPage *pPage = 0;
3921440149
int rc;
3921540150
unsigned char *pCell;
3921640151
int i;
3921740152
3921840153
assert( sqlite3_mutex_held(pBt->mutex) );
39219
- if( pgno>pagerPagecount(pBt->pPager) ){
40154
+ if( pgno>pagerPagecount(pBt) ){
3922040155
return SQLITE_CORRUPT_BKPT;
3922140156
}
3922240157
3922340158
rc = getAndInitPage(pBt, pgno, &pPage);
3922440159
if( rc ) goto cleardatabasepage_out;
3922540160
for(i=0; i<pPage->nCell; i++){
3922640161
pCell = findCell(pPage, i);
3922740162
if( !pPage->leaf ){
39228
- rc = clearDatabasePage(pBt, get4byte(pCell), pPage, 1, pnChange);
40163
+ rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
3922940164
if( rc ) goto cleardatabasepage_out;
3923040165
}
3923140166
rc = clearCell(pPage, pCell);
3923240167
if( rc ) goto cleardatabasepage_out;
3923340168
}
3923440169
if( !pPage->leaf ){
39235
- rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage, 1, pnChange);
40170
+ rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
3923640171
if( rc ) goto cleardatabasepage_out;
3923740172
}else if( pnChange ){
3923840173
assert( pPage->intKey );
3923940174
*pnChange += pPage->nCell;
3924040175
}
@@ -39272,11 +40207,11 @@
3927240207
}else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){
3927340208
/* nothing to do */
3927440209
}else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
3927540210
/* nothing to do */
3927640211
}else{
39277
- rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0, pnChange);
40212
+ rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
3927840213
}
3927940214
sqlite3BtreeLeave(p);
3928040215
return rc;
3928140216
}
3928240217
@@ -39298,11 +40233,11 @@
3929840233
** page number that used to be the last root page in the file before
3929940234
** the move. If no page gets moved, *piMoved is set to 0.
3930040235
** The last root page is recorded in meta[3] and the value of
3930140236
** meta[3] is updated by this procedure.
3930240237
*/
39303
-static int btreeDropTable(Btree *p, int iTable, int *piMoved){
40238
+static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
3930440239
int rc;
3930540240
MemPage *pPage = 0;
3930640241
BtShared *pBt = p->pBt;
3930740242
3930840243
assert( sqlite3BtreeHoldsMutex(p) );
@@ -39581,13 +40516,13 @@
3958140516
** Return 1 if there are 2 ore more references to the page and 0 if
3958240517
** if this is the first reference to the page.
3958340518
**
3958440519
** Also check that the page number is in bounds.
3958540520
*/
39586
-static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
40521
+static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
3958740522
if( iPage==0 ) return 1;
39588
- if( iPage>pCheck->nPage || iPage<0 ){
40523
+ if( iPage>pCheck->nPage ){
3958940524
checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
3959040525
return 1;
3959140526
}
3959240527
if( pCheck->anRef[iPage]==1 ){
3959340528
checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
@@ -39718,11 +40653,10 @@
3971840653
** the root of the tree.
3971940654
*/
3972040655
static int checkTreePage(
3972140656
IntegrityCk *pCheck, /* Context for the sanity check */
3972240657
int iPage, /* Page number of the page to check */
39723
- MemPage *pParent, /* Parent page */
3972440658
char *zParentContext /* Parent context */
3972540659
){
3972640660
MemPage *pPage;
3972740661
int i, rc, depth, d2, pgno, cnt;
3972840662
int hdr, cellStart;
@@ -39729,11 +40663,11 @@
3972940663
int nCell;
3973040664
u8 *data;
3973140665
BtShared *pBt;
3973240666
int usableSize;
3973340667
char zContext[100];
39734
- char *hit;
40668
+ char *hit = 0;
3973540669
3973640670
sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
3973740671
3973840672
/* Check that the page exists
3973940673
*/
@@ -39756,11 +40690,11 @@
3975640690
/* Check out all the cells.
3975740691
*/
3975840692
depth = 0;
3975940693
for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
3976040694
u8 *pCell;
39761
- int sz;
40695
+ u32 sz;
3976240696
CellInfo info;
3976340697
3976440698
/* Check payload overflow pages
3976540699
*/
3976640700
sqlite3_snprintf(sizeof(zContext), zContext,
@@ -39788,11 +40722,11 @@
3978840722
#ifndef SQLITE_OMIT_AUTOVACUUM
3978940723
if( pBt->autoVacuum ){
3979040724
checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
3979140725
}
3979240726
#endif
39793
- d2 = checkTreePage(pCheck,pgno,pPage,zContext);
40727
+ d2 = checkTreePage(pCheck, pgno, zContext);
3979440728
if( i>0 && d2!=depth ){
3979540729
checkAppendMsg(pCheck, zContext, "Child page depth differs");
3979640730
}
3979740731
depth = d2;
3979840732
}
@@ -39804,11 +40738,11 @@
3980440738
#ifndef SQLITE_OMIT_AUTOVACUUM
3980540739
if( pBt->autoVacuum ){
3980640740
checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
3980740741
}
3980840742
#endif
39809
- checkTreePage(pCheck, pgno, pPage, zContext);
40743
+ checkTreePage(pCheck, pgno, zContext);
3981040744
}
3981140745
3981240746
/* Check for complete coverage of the page
3981340747
*/
3981440748
data = pPage->aData;
@@ -39815,12 +40749,18 @@
3981540749
hdr = pPage->hdrOffset;
3981640750
hit = sqlite3PageMalloc( pBt->pageSize );
3981740751
if( hit==0 ){
3981840752
pCheck->mallocFailed = 1;
3981940753
}else{
39820
- memset(hit, 0, usableSize );
39821
- memset(hit, 1, get2byte(&data[hdr+5]));
40754
+ u16 contentOffset = get2byte(&data[hdr+5]);
40755
+ if (contentOffset > usableSize) {
40756
+ checkAppendMsg(pCheck, 0,
40757
+ "Corruption detected in header on page %d",iPage,0);
40758
+ goto check_page_abort;
40759
+ }
40760
+ memset(hit+contentOffset, 0, usableSize-contentOffset);
40761
+ memset(hit, 1, contentOffset);
3982240762
nCell = get2byte(&data[hdr+3]);
3982340763
cellStart = hdr + 12 - 4*pPage->leaf;
3982440764
for(i=0; i<nCell; i++){
3982540765
int pc = get2byte(&data[cellStart+i*2]);
3982640766
u16 size = 1024;
@@ -39860,11 +40800,12 @@
3986040800
checkAppendMsg(pCheck, 0,
3986140801
"Fragmented space is %d byte reported as %d on page %d",
3986240802
cnt, data[hdr+7], iPage);
3986340803
}
3986440804
}
39865
- sqlite3PageFree(hit);
40805
+check_page_abort:
40806
+ if (hit) sqlite3PageFree(hit);
3986640807
3986740808
releasePage(pPage);
3986840809
return depth+1;
3986940810
}
3987040811
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
@@ -39885,11 +40826,11 @@
3988540826
int *aRoot, /* An array of root pages numbers for individual trees */
3988640827
int nRoot, /* Number of entries in aRoot[] */
3988740828
int mxErr, /* Stop reporting errors after this many */
3988840829
int *pnErr /* Write number of errors seen to this variable */
3988940830
){
39890
- int i;
40831
+ Pgno i;
3989140832
int nRef;
3989240833
IntegrityCk sCheck;
3989340834
BtShared *pBt = p->pBt;
3989440835
char zErr[100];
3989540836
@@ -39901,11 +40842,11 @@
3990140842
sqlite3BtreeLeave(p);
3990240843
return sqlite3DbStrDup(0, "cannot acquire a read lock on the database");
3990340844
}
3990440845
sCheck.pBt = pBt;
3990540846
sCheck.pPager = pBt->pPager;
39906
- sCheck.nPage = pagerPagecount(sCheck.pPager);
40847
+ sCheck.nPage = pagerPagecount(sCheck.pBt);
3990740848
sCheck.mxErr = mxErr;
3990840849
sCheck.nErr = 0;
3990940850
sCheck.mallocFailed = 0;
3991040851
*pnErr = 0;
3991140852
#ifndef SQLITE_OMIT_AUTOVACUUM
@@ -39937,18 +40878,18 @@
3993740878
checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
3993840879
get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
3993940880
3994040881
/* Check all the tables.
3994140882
*/
39942
- for(i=0; i<nRoot && sCheck.mxErr; i++){
40883
+ for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
3994340884
if( aRoot[i]==0 ) continue;
3994440885
#ifndef SQLITE_OMIT_AUTOVACUUM
3994540886
if( pBt->autoVacuum && aRoot[i]>1 ){
3994640887
checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
3994740888
}
3994840889
#endif
39949
- checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
40890
+ checkTreePage(&sCheck, aRoot[i], "List of tree roots: ");
3995040891
}
3995140892
3995240893
/* Make sure every page in the file is referenced
3995340894
*/
3995440895
for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
@@ -40068,12 +41009,12 @@
4006841009
}
4006941010
if( pBtTo->pCursor ){
4007041011
return SQLITE_BUSY;
4007141012
}
4007241013
40073
- nToPage = pagerPagecount(pBtTo->pPager);
40074
- nFromPage = pagerPagecount(pBtFrom->pPager);
41014
+ nToPage = pagerPagecount(pBtTo);
41015
+ nFromPage = pagerPagecount(pBtFrom);
4007541016
iSkip = PENDING_BYTE_PAGE(pBtTo);
4007641017
4007741018
/* Variable nNewPage is the number of pages required to store the
4007841019
** contents of pFrom using the current page-size of pTo.
4007941020
*/
@@ -40417,23 +41358,23 @@
4041741358
**
4041841359
*************************************************************************
4041941360
** This file implements a FIFO queue of rowids used for processing
4042041361
** UPDATE and DELETE statements.
4042141362
**
40422
-** $Id: vdbefifo.c,v 1.8 2008/07/28 19:34:54 drh Exp $
41363
+** $Id: vdbefifo.c,v 1.9 2008/11/17 19:18:55 danielk1977 Exp $
4042341364
*/
4042441365
4042541366
/*
4042641367
** Constants FIFOSIZE_FIRST and FIFOSIZE_MAX are the initial
4042741368
** number of entries in a fifo page and the maximum number of
4042841369
** entries in a fifo page.
4042941370
*/
4043041371
#define FIFOSIZE_FIRST (((128-sizeof(FifoPage))/8)+1)
4043141372
#ifdef SQLITE_MALLOC_SOFT_LIMIT
40432
-# define FIFOSIZE_MAX (((SQLITE_MALLOC_SOFT_LIMIT-sizeof(FifoPage))/8)+1)
41373
+# define FIFOSIZE_MAX (int)(((SQLITE_MALLOC_SOFT_LIMIT-sizeof(FifoPage))/8)+1)
4043341374
#else
40434
-# define FIFOSIZE_MAX (((262144-sizeof(FifoPage))/8)+1)
41375
+# define FIFOSIZE_MAX (int)(((262144-sizeof(FifoPage))/8)+1)
4043541376
#endif
4043641377
4043741378
/*
4043841379
** Allocate a new FifoPage and return a pointer to it. Return NULL if
4043941380
** we run out of memory. Leave space on the page for nEntry entries.
@@ -40551,11 +41492,11 @@
4055141492
** This file contains code use to manipulate "Mem" structure. A "Mem"
4055241493
** stores a single value in the VDBE. Mem is an opaque structure visible
4055341494
** only within the VDBE. Interface routines refer to a Mem using the
4055441495
** name sqlite_value
4055541496
**
40556
-** $Id: vdbemem.c,v 1.125 2008/11/05 17:41:19 drh Exp $
41497
+** $Id: vdbemem.c,v 1.126 2008/11/11 00:21:30 drh Exp $
4055741498
*/
4055841499
4055941500
/*
4056041501
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
4056141502
** P if required.
@@ -40619,13 +41560,10 @@
4061941560
4062041561
if( n<32 ) n = 32;
4062141562
if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
4062241563
if( preserve && pMem->z==pMem->zMalloc ){
4062341564
pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
40624
- if( !pMem->z ){
40625
- pMem->flags = MEM_Null;
40626
- }
4062741565
preserve = 0;
4062841566
}else{
4062941567
sqlite3DbFree(pMem->db, pMem->zMalloc);
4063041568
pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
4063141569
}
@@ -40637,11 +41575,15 @@
4063741575
if( pMem->flags&MEM_Dyn && pMem->xDel ){
4063841576
pMem->xDel((void *)(pMem->z));
4063941577
}
4064041578
4064141579
pMem->z = pMem->zMalloc;
40642
- pMem->flags &= ~(MEM_Ephem|MEM_Static);
41580
+ if( pMem->z==0 ){
41581
+ pMem->flags = MEM_Null;
41582
+ }else{
41583
+ pMem->flags &= ~(MEM_Ephem|MEM_Static);
41584
+ }
4064341585
pMem->xDel = 0;
4064441586
return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
4064541587
}
4064641588
4064741589
/*
@@ -41596,11 +42538,11 @@
4159642538
** This file contains code used for creating, destroying, and populating
4159742539
** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
4159842540
** to version 2.8.7, all this code was combined into the vdbe.c source file.
4159942541
** But that file was getting too big so this subroutines were split out.
4160042542
**
41601
-** $Id: vdbeaux.c,v 1.418 2008/11/05 17:41:19 drh Exp $
42543
+** $Id: vdbeaux.c,v 1.420 2008/11/17 19:18:55 danielk1977 Exp $
4160242544
*/
4160342545
4160442546
4160542547
4160642548
/*
@@ -41679,25 +42621,27 @@
4167942621
p->trace = trace;
4168042622
}
4168142623
#endif
4168242624
4168342625
/*
41684
-** Resize the Vdbe.aOp array so that it contains at least N
41685
-** elements.
42626
+** Resize the Vdbe.aOp array so that it is at least one op larger than
42627
+** it was.
4168642628
**
41687
-** If an out-of-memory error occurs while resizing the array,
41688
-** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
41689
-** any opcodes already allocated can be correctly deallocated
41690
-** along with the rest of the Vdbe).
42629
+** If an out-of-memory error occurs while resizing the array, return
42630
+** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
42631
+** unchanged (this is so that any opcodes already allocated can be
42632
+** correctly deallocated along with the rest of the Vdbe).
4169142633
*/
41692
-static void resizeOpArray(Vdbe *p, int N){
42634
+static int growOpArray(Vdbe *p){
4169342635
VdbeOp *pNew;
41694
- pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op));
42636
+ int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
42637
+ pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
4169542638
if( pNew ){
41696
- p->nOpAlloc = N;
42639
+ p->nOpAlloc = nNew;
4169742640
p->aOp = pNew;
4169842641
}
42642
+ return (pNew ? SQLITE_OK : SQLITE_NOMEM);
4169942643
}
4170042644
4170142645
/*
4170242646
** Add a new instruction to the list of instructions current in the
4170342647
** VDBE. Return the address of the new instruction.
@@ -41719,12 +42663,11 @@
4171942663
VdbeOp *pOp;
4172042664
4172142665
i = p->nOp;
4172242666
assert( p->magic==VDBE_MAGIC_INIT );
4172342667
if( p->nOpAlloc<=i ){
41724
- resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op));
41725
- if( p->db->mallocFailed ){
42668
+ if( growOpArray(p) ){
4172642669
return 0;
4172742670
}
4172842671
}
4172942672
p->nOp++;
4173042673
pOp = &p->aOp[i];
@@ -41921,15 +42864,11 @@
4192142864
** address of the first operation added.
4192242865
*/
4192342866
SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
4192442867
int addr;
4192542868
assert( p->magic==VDBE_MAGIC_INIT );
41926
- if( p->nOp + nOp > p->nOpAlloc ){
41927
- resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op));
41928
- assert( p->nOp+nOp<=p->nOpAlloc || p->db->mallocFailed );
41929
- }
41930
- if( p->db->mallocFailed ){
42869
+ if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
4193142870
return 0;
4193242871
}
4193342872
addr = p->nOp;
4193442873
if( nOp>0 ){
4193542874
int i;
@@ -42310,11 +43249,11 @@
4231043249
**
4231143250
*/
4231243251
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
4231343252
int mask;
4231443253
assert( i>=0 && i<p->db->nDb );
42315
- assert( i<sizeof(p->btreeMask)*8 );
43254
+ assert( i<(int)sizeof(p->btreeMask)*8 );
4231643255
mask = 1<<i;
4231743256
if( (p->btreeMask & mask)==0 ){
4231843257
p->btreeMask |= mask;
4231943258
sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
4232043259
}
@@ -42596,15 +43535,11 @@
4259643535
4259743536
/* There should be at least one opcode.
4259843537
*/
4259943538
assert( p->nOp>0 );
4260043539
42601
- /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
42602
- * is because the call to resizeOpArray() below may shrink the
42603
- * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
42604
- * state.
42605
- */
43540
+ /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
4260643541
p->magic = VDBE_MAGIC_RUN;
4260743542
4260843543
/* For each cursor required, also allocate a memory cell. Memory
4260943544
** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
4261043545
** the vdbe program. Instead they are used to allocate space for
@@ -42620,11 +43555,10 @@
4262043555
** Allocation space for registers.
4262143556
*/
4262243557
if( p->aMem==0 ){
4262343558
int nArg; /* Maximum number of args passed to a user function. */
4262443559
resolveP2Values(p, &nArg);
42625
- /*resizeOpArray(p, p->nOp);*/
4262643560
assert( nVar>=0 );
4262743561
if( isExplain && nMem<10 ){
4262843562
nMem = 10;
4262943563
}
4263043564
p->aMem = sqlite3DbMallocZero(db,
@@ -43164,11 +44098,14 @@
4316444098
** we do either a commit or rollback of the current transaction.
4316544099
**
4316644100
** Note: This block also runs if one of the special errors handled
4316744101
** above has occurred.
4316844102
*/
43169
- if( db->autoCommit && db->writeVdbeCnt==(p->readOnly==0) ){
44103
+ if( !sqlite3VtabInSync(db)
44104
+ && db->autoCommit
44105
+ && db->writeVdbeCnt==(p->readOnly==0)
44106
+ ){
4317044107
if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
4317144108
/* The auto-commit flag is true, and the vdbe program was
4317244109
** successful or hit an 'OR FAIL' constraint. This means a commit
4317344110
** is required.
4317444111
*/
@@ -43791,12 +44728,12 @@
4379144728
UnpackedRecord *pSpace,/* Space available to hold resulting object */
4379244729
int szSpace /* Size of pSpace[] in bytes */
4379344730
){
4379444731
const unsigned char *aKey = (const unsigned char *)pKey;
4379544732
UnpackedRecord *p;
43796
- int nByte;
43797
- int idx, d;
44733
+ int nByte, d;
44734
+ u32 idx;
4379844735
u16 u; /* Unsigned loop counter */
4379944736
u32 szHdr;
4380044737
Mem *pMem;
4380144738
4380244739
assert( sizeof(Mem)>sizeof(*p) );
@@ -43816,11 +44753,11 @@
4381644753
d = szHdr;
4381744754
u = 0;
4381844755
while( idx<szHdr && u<p->nField ){
4381944756
u32 serial_type;
4382044757
43821
- idx += getVarint32( aKey+idx, serial_type);
44758
+ idx += getVarint32(&aKey[idx], serial_type);
4382244759
if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
4382344760
pMem->enc = pKeyInfo->enc;
4382444761
pMem->db = pKeyInfo->db;
4382544762
pMem->flags = 0;
4382644763
pMem->zMalloc = 0;
@@ -43881,11 +44818,11 @@
4388144818
*/
4388244819
SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
4388344820
int nKey1, const void *pKey1, /* Left key */
4388444821
UnpackedRecord *pPKey2 /* Right key */
4388544822
){
43886
- u32 d1; /* Offset into aKey[] of next data element */
44823
+ int d1; /* Offset into aKey[] of next data element */
4388744824
u32 idx1; /* Offset into aKey[] of next header element */
4388844825
u32 szHdr1; /* Number of bytes in header */
4388944826
int i = 0;
4389044827
int nField;
4389144828
int rc = 0;
@@ -44084,11 +45021,11 @@
4408445021
*************************************************************************
4408545022
**
4408645023
** This file contains code use to implement APIs that are part of the
4408745024
** VDBE.
4408845025
**
44089
-** $Id: vdbeapi.c,v 1.148 2008/11/05 16:37:35 drh Exp $
45026
+** $Id: vdbeapi.c,v 1.149 2008/11/19 09:05:27 danielk1977 Exp $
4409045027
*/
4409145028
4409245029
#if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
4409345030
/*
4409445031
** The following structure contains pointers to the end points of a
@@ -44670,15 +45607,16 @@
4467045607
** for name resolution but are actually overloaded by the xFindFunction
4467145608
** method of virtual tables.
4467245609
*/
4467345610
SQLITE_PRIVATE void sqlite3InvalidFunction(
4467445611
sqlite3_context *context, /* The function calling context */
44675
- int argc, /* Number of arguments to the function */
44676
- sqlite3_value **argv /* Value of each argument */
45612
+ int NotUsed, /* Number of arguments to the function */
45613
+ sqlite3_value **NotUsed2 /* Value of each argument */
4467745614
){
4467845615
const char *zName = context->pFunc->zName;
4467945616
char *zErr;
45617
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
4468045618
zErr = sqlite3MPrintf(0,
4468145619
"unable to use function %s in the requested context", zName);
4468245620
sqlite3_result_error(context, zErr, -1);
4468345621
sqlite3_free(zErr);
4468445622
}
@@ -45423,11 +46361,11 @@
4542346361
** documentation, headers files, or other derived files. The formatting
4542446362
** of the code in this file is, therefore, important. See other comments
4542546363
** in this file for details. If in doubt, do not deviate from existing
4542646364
** commenting and indentation practices when changing or adding code.
4542746365
**
45428
-** $Id: vdbe.c,v 1.786 2008/11/05 16:37:35 drh Exp $
46366
+** $Id: vdbe.c,v 1.788 2008/11/17 15:31:48 danielk1977 Exp $
4542946367
*/
4543046368
4543146369
/*
4543246370
** The following global variable is incremented every time a cursor
4543346371
** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test
@@ -45554,11 +46492,11 @@
4555446492
/*
4555546493
** Return true if an opcode has any of the OPFLG_xxx properties
4555646494
** specified by mask.
4555746495
*/
4555846496
SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
45559
- assert( opcode>0 && opcode<sizeof(opcodeProperty) );
46497
+ assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) );
4556046498
return (opcodeProperty[opcode]&mask)!=0;
4556146499
}
4556246500
4556346501
/*
4556446502
** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
@@ -46019,11 +46957,10 @@
4601946957
#endif
4602046958
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4602146959
int nProgressOps = 0; /* Opcodes executed since progress callback. */
4602246960
#endif
4602346961
UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */
46024
-
4602546962
4602646963
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
4602746964
assert( db->magic==SQLITE_MAGIC_BUSY );
4602846965
sqlite3BtreeMutexArrayEnter(&p->aMutex);
4602946966
if( p->rc==SQLITE_NOMEM ){
@@ -47432,19 +48369,19 @@
4743248369
** If the column contains fewer than P2 fields, then extract a NULL. Or,
4743348370
** if the P4 argument is a P4_MEM use the value of the P4 argument as
4743448371
** the result.
4743548372
*/
4743648373
case OP_Column: {
47437
- u32 payloadSize; /* Number of bytes in the record */
48374
+ int payloadSize; /* Number of bytes in the record */
4743848375
int p1 = pOp->p1; /* P1 value of the opcode */
4743948376
int p2 = pOp->p2; /* column number to retrieve */
4744048377
VdbeCursor *pC = 0;/* The VDBE cursor */
4744148378
char *zRec; /* Pointer to complete record-data */
4744248379
BtCursor *pCrsr; /* The BTree cursor */
4744348380
u32 *aType; /* aType[i] holds the numeric type of the i-th column */
4744448381
u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
47445
- u32 nField; /* number of fields in the record */
48382
+ int nField; /* number of fields in the record */
4744648383
int len; /* The length of the serialized data for the column */
4744748384
int i; /* Loop counter */
4744848385
char *zData; /* Part of the record being decoded */
4744948386
Mem *pDest; /* Where to write the extracted value */
4745048387
Mem sMem; /* For storing the record being decoded */
@@ -47488,11 +48425,11 @@
4748848425
}else if( pC->isIndex ){
4748948426
i64 payloadSize64;
4749048427
sqlite3BtreeKeySize(pCrsr, &payloadSize64);
4749148428
payloadSize = payloadSize64;
4749248429
}else{
47493
- sqlite3BtreeDataSize(pCrsr, &payloadSize);
48430
+ sqlite3BtreeDataSize(pCrsr, (u32 *)&payloadSize);
4749448431
}
4749548432
nField = pC->nField;
4749648433
}else{
4749748434
assert( pC->pseudoTable );
4749848435
/* The record is the sole entry of a pseudo-table */
@@ -47522,11 +48459,11 @@
4752248459
if( pC->cacheStatus==p->cacheCtr ){
4752348460
aOffset = pC->aOffset;
4752448461
}else{
4752548462
u8 *zIdx; /* Index into header */
4752648463
u8 *zEndHdr; /* Pointer to first byte after the header */
47527
- u32 offset; /* Offset into the data */
48464
+ int offset; /* Offset into the data */
4752848465
int szHdrSz; /* Size of the header size field at start of record */
4752948466
int avail; /* Number of bytes of available data */
4753048467
4753148468
assert(aType);
4753248469
pC->aOffset = aOffset = &aType[nField];
@@ -47723,11 +48660,11 @@
4772348660
*/
4772448661
u8 *zNewRecord; /* A buffer to hold the data for the new record */
4772548662
Mem *pRec; /* The new record */
4772648663
u64 nData = 0; /* Number of bytes of data space */
4772748664
int nHdr = 0; /* Number of bytes of header space */
47728
- u64 nByte = 0; /* Data space required for this record */
48665
+ i64 nByte = 0; /* Data space required for this record */
4772948666
int nZero = 0; /* Number of zero bytes at the end of the record */
4773048667
int nVarint; /* Number of bytes in a varint */
4773148668
u32 serial_type; /* Type field */
4773248669
Mem *pData0; /* First field to be combined into the record */
4773348670
Mem *pLast; /* Last field of the record */
@@ -48166,11 +49103,14 @@
4816649103
assert( p2>0 );
4816749104
assert( p2<=p->nMem );
4816849105
pIn2 = &p->aMem[p2];
4816949106
sqlite3VdbeMemIntegerify(pIn2);
4817049107
p2 = pIn2->u.i;
48171
- assert( p2>=2 );
49108
+ if( p2<2 ) {
49109
+ rc = SQLITE_CORRUPT_BKPT;
49110
+ goto abort_due_to_error;
49111
+ }
4817249112
}
4817349113
assert( i>=0 );
4817449114
pCur = allocateCursor(p, i, &pOp[-1], iDb, 1);
4817549115
if( pCur==0 ) goto no_mem;
4817649116
pCur->nullRow = 1;
@@ -48793,11 +49733,11 @@
4879349733
#else
4879449734
/* Some compilers complain about constants of the form 0x7fffffffffffffff.
4879549735
** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4879649736
** to provide the constant while making all compilers happy.
4879749737
*/
48798
-# define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
49738
+# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
4879949739
#endif
4880049740
4880149741
if( !pC->useRandomRowid ){
4880249742
if( pC->nextRowidValid ){
4880349743
v = pC->nextRowid;
@@ -49099,11 +50039,11 @@
4909950039
goto too_big;
4910050040
}
4910150041
n = n64;
4910250042
}else{
4910350043
sqlite3BtreeDataSize(pCrsr, &n);
49104
- if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
50044
+ if( (int)n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
4910550045
goto too_big;
4910650046
}
4910750047
}
4910850048
if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4910950049
goto no_mem;
@@ -51213,11 +52153,11 @@
5121352153
**
5121452154
** This file contains code use to implement an in-memory rollback journal.
5121552155
** The in-memory rollback journal is used to journal transactions for
5121652156
** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
5121752157
**
51218
-** @(#) $Id: memjournal.c,v 1.2 2008/10/28 18:12:36 drh Exp $
52158
+** @(#) $Id: memjournal.c,v 1.5 2008/11/19 16:52:44 danielk1977 Exp $
5121952159
*/
5122052160
5122152161
/* Forward references to internal structures */
5122252162
typedef struct MemJournal MemJournal;
5122352163
typedef struct FilePoint FilePoint;
@@ -51228,11 +52168,13 @@
5122852168
*/
5122952169
#define JOURNAL_CHUNKSIZE 1024
5123052170
5123152171
/* Macro to find the minimum of two numeric values.
5123252172
*/
51233
-#define MIN(x,y) ((x)<(y)?(x):(y))
52173
+#ifndef MIN
52174
+# define MIN(x,y) ((x)<(y)?(x):(y))
52175
+#endif
5123452176
5123552177
/*
5123652178
** The rollback journal is composed of a linked list of these structures.
5123752179
*/
5123852180
struct FileChunk {
@@ -51319,10 +52261,11 @@
5131952261
5132052262
/* An in-memory journal file should only ever be appended to. Random
5132152263
** access writes are not required by sqlite.
5132252264
*/
5132352265
assert(iOfst==p->endpoint.iOffset);
52266
+ UNUSED_PARAMETER(iOfst);
5132452267
5132552268
while( nWrite>0 ){
5132652269
FileChunk *pChunk = p->endpoint.pChunk;
5132752270
int iChunkOffset = p->endpoint.iOffset%JOURNAL_CHUNKSIZE;
5132852271
int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
@@ -51358,10 +52301,11 @@
5135852301
*/
5135952302
static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
5136052303
MemJournal *p = (MemJournal *)pJfd;
5136152304
FileChunk *pChunk;
5136252305
assert(size==0);
52306
+ UNUSED_PARAMETER(size);
5136352307
pChunk = p->pFirst;
5136452308
while( pChunk ){
5136552309
FileChunk *pTmp = pChunk;
5136652310
pChunk = pChunk->pNext;
5136752311
sqlite3_free(pTmp);
@@ -51380,11 +52324,12 @@
5138052324
5138152325
5138252326
/*
5138352327
** Sync the file.
5138452328
*/
51385
-static int memjrnlSync(sqlite3_file *pJfd, int flags){
52329
+static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
52330
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
5138652331
return SQLITE_OK;
5138752332
}
5138852333
5138952334
/*
5139052335
** Query the size of the file in bytes.
@@ -51589,11 +52534,11 @@
5158952534
**
5159052535
** This file contains routines used for walking the parser tree and
5159152536
** resolve all identifiers by associating them with a particular
5159252537
** table and column.
5159352538
**
51594
-** $Id: resolve.c,v 1.10 2008/10/19 21:03:27 drh Exp $
52539
+** $Id: resolve.c,v 1.11 2008/11/17 19:18:55 danielk1977 Exp $
5159552540
*/
5159652541
5159752542
/*
5159852543
** Turn the pExpr expression into an alias for the iCol-th column of the
5159952544
** result set in pEList.
@@ -51921,13 +52866,13 @@
5192152866
** column number is greater than the number of bits in the bitmask
5192252867
** then set the high-order bit of the bitmask.
5192352868
*/
5192452869
if( pExpr->iColumn>=0 && pMatch!=0 ){
5192552870
int n = pExpr->iColumn;
51926
- testcase( n==sizeof(Bitmask)*8-1 );
51927
- if( n>=sizeof(Bitmask)*8 ){
51928
- n = sizeof(Bitmask)*8-1;
52871
+ testcase( n==BMS-1 );
52872
+ if( n>=BMS ){
52873
+ n = BMS-1;
5192952874
}
5193052875
assert( pMatch->iCursor==pExpr->iTable );
5193152876
pMatch->colUsed |= ((Bitmask)1)<<n;
5193252877
}
5193352878
@@ -52756,11 +53701,11 @@
5275653701
**
5275753702
*************************************************************************
5275853703
** This file contains routines used for analyzing expressions and
5275953704
** for generating VDBE code that evaluates expressions in SQLite.
5276053705
**
52761
-** $Id: expr.c,v 1.401 2008/11/06 15:33:04 drh Exp $
53706
+** $Id: expr.c,v 1.404 2008/11/19 16:52:44 danielk1977 Exp $
5276253707
*/
5276353708
5276453709
/*
5276553710
** Return the 'affinity' of the expression pExpr if any.
5276653711
**
@@ -52785,11 +53730,13 @@
5278553730
#ifndef SQLITE_OMIT_CAST
5278653731
if( op==TK_CAST ){
5278753732
return sqlite3AffinityType(&pExpr->token);
5278853733
}
5278953734
#endif
52790
- if( (op==TK_COLUMN || op==TK_REGISTER) && pExpr->pTab!=0 ){
53735
+ if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
53736
+ && pExpr->pTab!=0
53737
+ ){
5279153738
/* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
5279253739
** a TK_COLUMN but was previously evaluated and cached in a register */
5279353740
int j = pExpr->iColumn;
5279453741
if( j<0 ) return SQLITE_AFF_INTEGER;
5279553742
assert( pExpr->pTab && j<pExpr->pTab->nCol );
@@ -52831,11 +53778,11 @@
5283153778
while( p ){
5283253779
int op;
5283353780
pColl = p->pColl;
5283453781
if( pColl ) break;
5283553782
op = p->op;
52836
- if( (op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){
53783
+ if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){
5283753784
/* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
5283853785
** a TK_COLUMN but was previously evaluated and cached in a register */
5283953786
const char *zColl;
5284053787
int j = p->iColumn;
5284153788
if( j>=0 ){
@@ -53679,11 +54626,12 @@
5367954626
return WRC_Abort;
5368054627
default:
5368154628
return WRC_Continue;
5368254629
}
5368354630
}
53684
-static int selectNodeIsConstant(Walker *pWalker, Select *pSelect){
54631
+static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
54632
+ UNUSED_PARAMETER(NotUsed);
5368554633
pWalker->u.i = 0;
5368654634
return WRC_Abort;
5368754635
}
5368854636
static int exprIsConst(Expr *p, int initFlag){
5368954637
Walker w;
@@ -54199,14 +55147,15 @@
5419955147
** z[n] character is guaranteed to be something that does not look
5420055148
** like the continuation of the number.
5420155149
*/
5420255150
static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
5420355151
assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
55152
+ assert( !z || !isdigit(z[n]) );
55153
+ UNUSED_PARAMETER(n);
5420455154
if( z ){
5420555155
double value;
5420655156
char *zV;
54207
- assert( !isdigit(z[n]) );
5420855157
sqlite3AtoF(z, &value);
5420955158
if( sqlite3IsNaN(value) ){
5421055159
sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
5421155160
}else{
5421255161
if( negateFlag ) value = -value;
@@ -55854,11 +56803,11 @@
5585456803
**
5585556804
*************************************************************************
5585656805
** This file contains C code routines that used to generate VDBE code
5585756806
** that implements the ALTER TABLE command.
5585856807
**
55859
-** $Id: alter.c,v 1.49 2008/10/30 17:21:13 danielk1977 Exp $
56808
+** $Id: alter.c,v 1.50 2008/11/19 09:05:27 danielk1977 Exp $
5586056809
*/
5586156810
5586256811
/*
5586356812
** The code in this file only exists if we are not omitting the
5586456813
** ALTER TABLE logic from the build.
@@ -55879,11 +56828,11 @@
5587956828
** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
5588056829
** -> 'CREATE INDEX i ON def(a, b, c)'
5588156830
*/
5588256831
static void renameTableFunc(
5588356832
sqlite3_context *context,
55884
- int argc,
56833
+ int NotUsed,
5588556834
sqlite3_value **argv
5588656835
){
5588756836
unsigned char const *zSql = sqlite3_value_text(argv[0]);
5588856837
unsigned char const *zTableName = sqlite3_value_text(argv[1]);
5588956838
@@ -55892,10 +56841,12 @@
5589256841
unsigned char const *zCsr = zSql;
5589356842
int len = 0;
5589456843
char *zRet;
5589556844
5589656845
sqlite3 *db = sqlite3_context_db_handle(context);
56846
+
56847
+ UNUSED_PARAMETER(NotUsed);
5589756848
5589856849
/* The principle used to locate the table name in the CREATE TABLE
5589956850
** statement is that the table name is the first non-space token that
5590056851
** is immediately followed by a TK_LP or TK_USING token.
5590156852
*/
@@ -55934,11 +56885,11 @@
5593456885
** returned. This is analagous to renameTableFunc() above, except for CREATE
5593556886
** TRIGGER, not CREATE INDEX and CREATE TABLE.
5593656887
*/
5593756888
static void renameTriggerFunc(
5593856889
sqlite3_context *context,
55939
- int argc,
56890
+ int NotUsed,
5594056891
sqlite3_value **argv
5594156892
){
5594256893
unsigned char const *zSql = sqlite3_value_text(argv[0]);
5594356894
unsigned char const *zTableName = sqlite3_value_text(argv[1]);
5594456895
@@ -55946,12 +56897,13 @@
5594656897
Token tname;
5594756898
int dist = 3;
5594856899
unsigned char const *zCsr = zSql;
5594956900
int len = 0;
5595056901
char *zRet;
55951
-
5595256902
sqlite3 *db = sqlite3_context_db_handle(context);
56903
+
56904
+ UNUSED_PARAMETER(NotUsed);
5595356905
5595456906
/* The principle used to locate the table name in the CREATE TRIGGER
5595556907
** statement is that the table name is the first token that is immediatedly
5595656908
** preceded by either TK_ON or TK_DOT and immediatedly followed by one
5595756909
** of TK_WHEN, TK_BEGIN or TK_FOR.
@@ -56475,11 +57427,11 @@
5647557427
** May you share freely, never taking more than you give.
5647657428
**
5647757429
*************************************************************************
5647857430
** This file contains code associated with the ANALYZE command.
5647957431
**
56480
-** @(#) $Id: analyze.c,v 1.44 2008/11/03 20:55:07 drh Exp $
57432
+** @(#) $Id: analyze.c,v 1.46 2008/11/19 16:52:44 danielk1977 Exp $
5648157433
*/
5648257434
#ifndef SQLITE_OMIT_ANALYZE
5648357435
5648457436
/*
5648557437
** This routine generates code that opens the sqlite_stat1 table on cursor
@@ -56819,18 +57771,20 @@
5681957771
** sqlite_stat1 table.
5682057772
**
5682157773
** argv[0] = name of the index
5682257774
** argv[1] = results of analysis - on integer for each column
5682357775
*/
56824
-static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){
57776
+static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
5682557777
analysisInfo *pInfo = (analysisInfo*)pData;
5682657778
Index *pIndex;
5682757779
int i, c;
5682857780
unsigned int v;
5682957781
const char *z;
5683057782
5683157783
assert( argc==2 );
57784
+ UNUSED_PARAMETER2(NotUsed, argc);
57785
+
5683257786
if( argv==0 || argv[0]==0 || argv[1]==0 ){
5683357787
return 0;
5683457788
}
5683557789
pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
5683657790
if( pIndex==0 ){
@@ -56902,11 +57856,11 @@
5690257856
** May you share freely, never taking more than you give.
5690357857
**
5690457858
*************************************************************************
5690557859
** This file contains code used to implement the ATTACH and DETACH commands.
5690657860
**
56907
-** $Id: attach.c,v 1.79 2008/10/28 17:52:39 danielk1977 Exp $
57861
+** $Id: attach.c,v 1.80 2008/11/19 09:05:27 danielk1977 Exp $
5690857862
*/
5690957863
5691057864
#ifndef SQLITE_OMIT_ATTACH
5691157865
/*
5691257866
** Resolve an expression that was part of an ATTACH or DETACH statement. This
@@ -56954,11 +57908,11 @@
5695457908
** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
5695557909
** third argument.
5695657910
*/
5695757911
static void attachFunc(
5695857912
sqlite3_context *context,
56959
- int argc,
57913
+ int NotUsed,
5696057914
sqlite3_value **argv
5696157915
){
5696257916
int i;
5696357917
int rc = 0;
5696457918
sqlite3 *db = sqlite3_context_db_handle(context);
@@ -56965,10 +57919,12 @@
5696557919
const char *zName;
5696657920
const char *zFile;
5696757921
Db *aNew;
5696857922
char *zErrDyn = 0;
5696957923
char zErr[128];
57924
+
57925
+ UNUSED_PARAMETER(NotUsed);
5697057926
5697157927
zFile = (const char *)sqlite3_value_text(argv[0]);
5697257928
zName = (const char *)sqlite3_value_text(argv[1]);
5697357929
if( zFile==0 ) zFile = "";
5697457930
if( zName==0 ) zName = "";
@@ -57122,18 +58078,20 @@
5712258078
**
5712358079
** SELECT sqlite_detach(x)
5712458080
*/
5712558081
static void detachFunc(
5712658082
sqlite3_context *context,
57127
- int argc,
58083
+ int NotUsed,
5712858084
sqlite3_value **argv
5712958085
){
5713058086
const char *zName = (const char *)sqlite3_value_text(argv[0]);
5713158087
sqlite3 *db = sqlite3_context_db_handle(context);
5713258088
int i;
5713358089
Db *pDb = 0;
5713458090
char zErr[128];
58091
+
58092
+ UNUSED_PARAMETER(NotUsed);
5713558093
5713658094
if( zName==0 ) zName = "";
5713758095
for(i=0; i<db->nDb; i++){
5713858096
pDb = &db->aDb[i];
5713958097
if( pDb->pBt==0 ) continue;
@@ -57685,11 +58643,11 @@
5768558643
** creating ID lists
5768658644
** BEGIN TRANSACTION
5768758645
** COMMIT
5768858646
** ROLLBACK
5768958647
**
57690
-** $Id: build.c,v 1.500 2008/11/03 20:55:07 drh Exp $
58648
+** $Id: build.c,v 1.503 2008/11/17 19:18:55 danielk1977 Exp $
5769158649
*/
5769258650
5769358651
/*
5769458652
** This routine is called when a new SQL statement is beginning to
5769558653
** be parsed. Initialize the pParse structure as needed.
@@ -58287,11 +59245,11 @@
5828759245
** index of the named database in db->aDb[], or -1 if the named db
5828859246
** does not exist.
5828959247
*/
5829059248
SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
5829159249
int i = -1; /* Database number */
58292
- int n; /* Number of characters in the name */
59250
+ size_t n; /* Number of characters in the name */
5829359251
Db *pDb; /* A database whose name space is being searched */
5829459252
char *zName; /* Name we are searching for */
5829559253
5829659254
zName = sqlite3NameFromToken(db, pName);
5829759255
if( zName ){
@@ -58331,11 +59289,15 @@
5833159289
){
5833259290
int iDb; /* Database holding the object */
5833359291
sqlite3 *db = pParse->db;
5833459292
5833559293
if( pName2 && pName2->n>0 ){
58336
- assert( !db->init.busy );
59294
+ if( db->init.busy ) {
59295
+ sqlite3ErrorMsg(pParse, "corrupt database");
59296
+ pParse->nErr++;
59297
+ return -1;
59298
+ }
5833759299
*pUnqual = pName2;
5833859300
iDb = sqlite3FindDb(db, pName1);
5833959301
if( iDb<0 ){
5834059302
sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
5834159303
pParse->nErr++;
@@ -59053,11 +60015,11 @@
5905360015
k += strlen(&zStmt[k]);
5905460016
zSep = zSep2;
5905560017
identPut(zStmt, &k, pCol->zName);
5905660018
if( (z = pCol->zType)!=0 ){
5905760019
zStmt[k++] = ' ';
59058
- assert( strlen(z)+k+1<=n );
60020
+ assert( (int)(strlen(z)+k+1)<=n );
5905960021
sqlite3_snprintf(n-k, &zStmt[k], "%s", z);
5906060022
k += strlen(z);
5906160023
}
5906260024
}
5906360025
sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
@@ -60651,16 +61613,86 @@
6065161613
for(i=0; i<pList->nId; i++){
6065261614
if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
6065361615
}
6065461616
return -1;
6065561617
}
61618
+
61619
+/*
61620
+** Expand the space allocated for the given SrcList object by
61621
+** creating nExtra new slots beginning at iStart. iStart is zero based.
61622
+** New slots are zeroed.
61623
+**
61624
+** For example, suppose a SrcList initially contains two entries: A,B.
61625
+** To append 3 new entries onto the end, do this:
61626
+**
61627
+** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
61628
+**
61629
+** After the call above it would contain: A, B, nil, nil, nil.
61630
+** If the iStart argument had been 1 instead of 2, then the result
61631
+** would have been: A, nil, nil, nil, B. To prepend the new slots,
61632
+** the iStart value would be 0. The result then would
61633
+** be: nil, nil, nil, A, B.
61634
+**
61635
+** If a memory allocation fails the SrcList is unchanged. The
61636
+** db->mallocFailed flag will be set to true.
61637
+*/
61638
+SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
61639
+ sqlite3 *db, /* Database connection to notify of OOM errors */
61640
+ SrcList *pSrc, /* The SrcList to be enlarged */
61641
+ int nExtra, /* Number of new slots to add to pSrc->a[] */
61642
+ int iStart /* Index in pSrc->a[] of first new slot */
61643
+){
61644
+ int i;
61645
+
61646
+ /* Sanity checking on calling parameters */
61647
+ assert( iStart>=0 );
61648
+ assert( nExtra>=1 );
61649
+ if( pSrc==0 || iStart>pSrc->nSrc ){
61650
+ assert( db->mallocFailed );
61651
+ return pSrc;
61652
+ }
61653
+
61654
+ /* Allocate additional space if needed */
61655
+ if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
61656
+ SrcList *pNew;
61657
+ int nAlloc = pSrc->nSrc+nExtra;
61658
+ pNew = sqlite3DbRealloc(db, pSrc,
61659
+ sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
61660
+ if( pNew==0 ){
61661
+ assert( db->mallocFailed );
61662
+ return pSrc;
61663
+ }
61664
+ pSrc = pNew;
61665
+ pSrc->nAlloc = nAlloc;
61666
+ }
61667
+
61668
+ /* Move existing slots that come after the newly inserted slots
61669
+ ** out of the way */
61670
+ for(i=pSrc->nSrc-1; i>=iStart; i--){
61671
+ pSrc->a[i+nExtra] = pSrc->a[i];
61672
+ }
61673
+ pSrc->nSrc += nExtra;
61674
+
61675
+ /* Zero the newly allocated slots */
61676
+ memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
61677
+ for(i=iStart; i<iStart+nExtra; i++){
61678
+ pSrc->a[i].iCursor = -1;
61679
+ }
61680
+
61681
+ /* Return a pointer to the enlarged SrcList */
61682
+ return pSrc;
61683
+}
61684
+
6065661685
6065761686
/*
6065861687
** Append a new table name to the given SrcList. Create a new SrcList if
6065961688
** need be. A new entry is created in the SrcList even if pToken is NULL.
6066061689
**
60661
-** A new SrcList is returned, or NULL if malloc() fails.
61690
+** A SrcList is returned, or NULL if there is an OOM error. The returned
61691
+** SrcList might be the same as the SrcList that was input or it might be
61692
+** a new one. If an OOM error does occurs, then the prior value of pList
61693
+** that is input to this routine is automatically freed.
6066261694
**
6066361695
** If pDatabase is not null, it means that the table has an optional
6066461696
** database name prefix. Like this: "database.table". The pDatabase
6066561697
** points to the table name and the pTable points to the database name.
6066661698
** The SrcList.a[].zName field is filled with the table name which might
@@ -60689,23 +61721,16 @@
6068961721
if( pList==0 ){
6069061722
pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
6069161723
if( pList==0 ) return 0;
6069261724
pList->nAlloc = 1;
6069361725
}
60694
- if( pList->nSrc>=pList->nAlloc ){
60695
- SrcList *pNew;
60696
- pList->nAlloc *= 2;
60697
- pNew = sqlite3DbRealloc(db, pList,
60698
- sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
60699
- if( pNew==0 ){
60700
- sqlite3SrcListDelete(db, pList);
60701
- return 0;
60702
- }
60703
- pList = pNew;
60704
- }
60705
- pItem = &pList->a[pList->nSrc];
60706
- memset(pItem, 0, sizeof(pList->a[0]));
61726
+ pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
61727
+ if( db->mallocFailed ){
61728
+ sqlite3SrcListDelete(db, pList);
61729
+ return 0;
61730
+ }
61731
+ pItem = &pList->a[pList->nSrc-1];
6070761732
if( pDatabase && pDatabase->z==0 ){
6070861733
pDatabase = 0;
6070961734
}
6071061735
if( pDatabase && pTable ){
6071161736
Token *pTemp = pDatabase;
@@ -60712,12 +61737,10 @@
6071261737
pDatabase = pTable;
6071361738
pTable = pTemp;
6071461739
}
6071561740
pItem->zName = sqlite3NameFromToken(db, pTable);
6071661741
pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
60717
- pItem->iCursor = -1;
60718
- pList->nSrc++;
6071961742
return pList;
6072061743
}
6072161744
6072261745
/*
6072361746
** Assign VdbeCursor index numbers to all tables in a SrcList
@@ -61644,11 +62667,11 @@
6164462667
**
6164562668
*************************************************************************
6164662669
** This file contains C code routines that are called by the parser
6164762670
** in order to generate code for DELETE FROM statements.
6164862671
**
61649
-** $Id: delete.c,v 1.186 2008/10/31 10:53:23 danielk1977 Exp $
62672
+** $Id: delete.c,v 1.187 2008/11/19 09:05:27 danielk1977 Exp $
6165062673
*/
6165162674
6165262675
/*
6165362676
** Look up every table that is named in pSrc. If any table is not found,
6165462677
** add an error message to pParse->zErrMsg and return NULL. If all tables
@@ -61896,11 +62919,11 @@
6189662919
6189762920
/* Figure out if we have any triggers and if the table being
6189862921
** deleted from is a view
6189962922
*/
6190062923
#ifndef SQLITE_OMIT_TRIGGER
61901
- triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0);
62924
+ triggers_exist = sqlite3TriggersExist(pTab, TK_DELETE, 0);
6190262925
isView = pTab->pSelect!=0;
6190362926
#else
6190462927
# define triggers_exist 0
6190562928
# define isView 0
6190662929
#endif
@@ -62285,11 +63308,11 @@
6228563308
**
6228663309
** There is only one exported symbol in this file - the function
6228763310
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
6228863311
** All other code has file scope.
6228963312
**
62290
-** $Id: func.c,v 1.204 2008/10/28 17:52:39 danielk1977 Exp $
63313
+** $Id: func.c,v 1.206 2008/11/19 16:52:44 danielk1977 Exp $
6229163314
*/
6229263315
6229363316
/*
6229463317
** Return the collating function associated with a function.
6229563318
*/
@@ -62329,14 +63352,15 @@
6232963352
/*
6233063353
** Return the type of the argument.
6233163354
*/
6233263355
static void typeofFunc(
6233363356
sqlite3_context *context,
62334
- int argc,
63357
+ int NotUsed,
6233563358
sqlite3_value **argv
6233663359
){
6233763360
const char *z = 0;
63361
+ UNUSED_PARAMETER(NotUsed);
6233863362
switch( sqlite3_value_type(argv[0]) ){
6233963363
case SQLITE_NULL: z = "null"; break;
6234063364
case SQLITE_INTEGER: z = "integer"; break;
6234163365
case SQLITE_TEXT: z = "text"; break;
6234263366
case SQLITE_FLOAT: z = "real"; break;
@@ -62355,10 +63379,11 @@
6235563379
sqlite3_value **argv
6235663380
){
6235763381
int len;
6235863382
6235963383
assert( argc==1 );
63384
+ UNUSED_PARAMETER(argc);
6236063385
switch( sqlite3_value_type(argv[0]) ){
6236163386
case SQLITE_BLOB:
6236263387
case SQLITE_INTEGER:
6236363388
case SQLITE_FLOAT: {
6236463389
sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
@@ -62385,10 +63410,11 @@
6238563410
/*
6238663411
** Implementation of the abs() function
6238763412
*/
6238863413
static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
6238963414
assert( argc==1 );
63415
+ UNUSED_PARAMETER(argc);
6239063416
switch( sqlite3_value_type(argv[0]) ){
6239163417
case SQLITE_INTEGER: {
6239263418
i64 iVal = sqlite3_value_int64(argv[0]);
6239363419
if( iVal<0 ){
6239463420
if( (iVal<<1)==0 ){
@@ -62588,14 +63614,15 @@
6258863614
/*
6258963615
** Implementation of random(). Return a random integer.
6259063616
*/
6259163617
static void randomFunc(
6259263618
sqlite3_context *context,
62593
- int argc,
62594
- sqlite3_value **argv
63619
+ int NotUsed,
63620
+ sqlite3_value **NotUsed2
6259563621
){
6259663622
sqlite_int64 r;
63623
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
6259763624
sqlite3_randomness(sizeof(r), &r);
6259863625
if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */
6259963626
/* can always do abs() of the result */
6260063627
sqlite3_result_int64(context, r);
6260163628
}
@@ -62610,10 +63637,11 @@
6261063637
sqlite3_value **argv
6261163638
){
6261263639
int n;
6261363640
unsigned char *p;
6261463641
assert( argc==1 );
63642
+ UNUSED_PARAMETER(argc);
6261563643
n = sqlite3_value_int(argv[0]);
6261663644
if( n<1 ){
6261763645
n = 1;
6261863646
}
6261963647
p = contextMalloc(context, n);
@@ -62627,40 +63655,43 @@
6262763655
** Implementation of the last_insert_rowid() SQL function. The return
6262863656
** value is the same as the sqlite3_last_insert_rowid() API function.
6262963657
*/
6263063658
static void last_insert_rowid(
6263163659
sqlite3_context *context,
62632
- int arg,
62633
- sqlite3_value **argv
63660
+ int NotUsed,
63661
+ sqlite3_value **NotUsed2
6263463662
){
6263563663
sqlite3 *db = sqlite3_context_db_handle(context);
63664
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
6263663665
sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
6263763666
}
6263863667
6263963668
/*
6264063669
** Implementation of the changes() SQL function. The return value is the
6264163670
** same as the sqlite3_changes() API function.
6264263671
*/
6264363672
static void changes(
6264463673
sqlite3_context *context,
62645
- int arg,
62646
- sqlite3_value **argv
63674
+ int NotUsed,
63675
+ sqlite3_value **NotUsed2
6264763676
){
6264863677
sqlite3 *db = sqlite3_context_db_handle(context);
63678
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
6264963679
sqlite3_result_int(context, sqlite3_changes(db));
6265063680
}
6265163681
6265263682
/*
6265363683
** Implementation of the total_changes() SQL function. The return value is
6265463684
** the same as the sqlite3_total_changes() API function.
6265563685
*/
6265663686
static void total_changes(
6265763687
sqlite3_context *context,
62658
- int arg,
62659
- sqlite3_value **argv
63688
+ int NotUsed,
63689
+ sqlite3_value **NotUsed2
6266063690
){
6266163691
sqlite3 *db = sqlite3_context_db_handle(context);
63692
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
6266263693
sqlite3_result_int(context, sqlite3_total_changes(db));
6266363694
}
6266463695
6266563696
/*
6266663697
** A structure defining how to do GLOB-style comparisons.
@@ -62901,14 +63932,15 @@
6290163932
** argument if the arguments are different. The result is NULL if the
6290263933
** arguments are equal to each other.
6290363934
*/
6290463935
static void nullifFunc(
6290563936
sqlite3_context *context,
62906
- int argc,
63937
+ int NotUsed,
6290763938
sqlite3_value **argv
6290863939
){
6290963940
CollSeq *pColl = sqlite3GetFuncCollSeq(context);
63941
+ UNUSED_PARAMETER(NotUsed);
6291063942
if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
6291163943
sqlite3_result_value(context, argv[0]);
6291263944
}
6291363945
}
6291463946
@@ -62916,13 +63948,14 @@
6291663948
** Implementation of the VERSION(*) function. The result is the version
6291763949
** of the SQLite library that is running.
6291863950
*/
6291963951
static void versionFunc(
6292063952
sqlite3_context *context,
62921
- int argc,
62922
- sqlite3_value **argv
63953
+ int NotUsed,
63954
+ sqlite3_value **NotUsed2
6292363955
){
63956
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
6292463957
sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
6292563958
}
6292663959
6292763960
/* Array for converting from half-bytes (nybbles) into ASCII hex
6292863961
** digits. */
@@ -63011,10 +64044,11 @@
6301164044
){
6301264045
int i, n;
6301364046
const unsigned char *pBlob;
6301464047
char *zHex, *z;
6301564048
assert( argc==1 );
64049
+ UNUSED_PARAMETER(argc);
6301664050
pBlob = sqlite3_value_blob(argv[0]);
6301764051
n = sqlite3_value_bytes(argv[0]);
6301864052
assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
6301964053
z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
6302064054
if( zHex ){
@@ -63036,10 +64070,11 @@
6303664070
int argc,
6303764071
sqlite3_value **argv
6303864072
){
6303964073
i64 n;
6304064074
assert( argc==1 );
64075
+ UNUSED_PARAMETER(argc);
6304164076
n = sqlite3_value_int64(argv[0]);
6304264077
if( n>SQLITE_MAX_LENGTH ){
6304364078
sqlite3_result_error_toobig(context);
6304464079
}else{
6304564080
sqlite3_result_zeroblob(context, n);
@@ -63067,10 +64102,11 @@
6306764102
i64 nOut; /* Maximum size of zOut */
6306864103
int loopLimit; /* Last zStr[] that might match zPattern[] */
6306964104
int i, j; /* Loop counters */
6307064105
6307164106
assert( argc==3 );
64107
+ UNUSED_PARAMETER(argc);
6307264108
zStr = sqlite3_value_text(argv[0]);
6307364109
if( zStr==0 ) return;
6307464110
nStr = sqlite3_value_bytes(argv[0]);
6307564111
assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
6307664112
zPattern = sqlite3_value_text(argv[1]);
@@ -63304,10 +64340,11 @@
6330464340
*/
6330564341
static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
6330664342
SumCtx *p;
6330764343
int type;
6330864344
assert( argc==1 );
64345
+ UNUSED_PARAMETER(argc);
6330964346
p = sqlite3_aggregate_context(context, sizeof(*p));
6331064347
type = sqlite3_value_numeric_type(argv[0]);
6331164348
if( p && type!=SQLITE_NULL ){
6331264349
p->cnt++;
6331364350
if( type==SQLITE_INTEGER ){
@@ -63379,13 +64416,18 @@
6337964416
}
6338064417
6338164418
/*
6338264419
** Routines to implement min() and max() aggregate functions.
6338364420
*/
63384
-static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
64421
+static void minmaxStep(
64422
+ sqlite3_context *context,
64423
+ int NotUsed,
64424
+ sqlite3_value **argv
64425
+){
6338564426
Mem *pArg = (Mem *)argv[0];
6338664427
Mem *pBest;
64428
+ UNUSED_PARAMETER(NotUsed);
6338764429
6338864430
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
6338964431
pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
6339064432
if( !pBest ) return;
6339164433
@@ -63661,11 +64703,11 @@
6366164703
**
6366264704
*************************************************************************
6366364705
** This file contains C code routines that are called by the parser
6366464706
** to handle INSERT statements in SQLite.
6366564707
**
63666
-** $Id: insert.c,v 1.251 2008/11/03 20:55:07 drh Exp $
64708
+** $Id: insert.c,v 1.253 2008/11/19 09:05:27 danielk1977 Exp $
6366764709
*/
6366864710
6366964711
/*
6367064712
** Set P4 of the most recently inserted opcode to a column affinity
6367164713
** string for index pIdx. A column affinity string has one character
@@ -64078,11 +65120,11 @@
6407865120
6407965121
/* Figure out if we have any triggers and if the table being
6408065122
** inserted into is a view
6408165123
*/
6408265124
#ifndef SQLITE_OMIT_TRIGGER
64083
- triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
65125
+ triggers_exist = sqlite3TriggersExist(pTab, TK_INSERT, 0);
6408465126
isView = pTab->pSelect!=0;
6408565127
#else
6408665128
# define triggers_exist 0
6408765129
# define isView 0
6408865130
#endif
@@ -64584,11 +65626,10 @@
6458465626
pTab,
6458565627
baseCur,
6458665628
regIns,
6458765629
aRegIdx,
6458865630
0,
64589
- 0,
6459065631
(triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1,
6459165632
appendFlag
6459265633
);
6459365634
}
6459465635
}
@@ -64920,30 +65961,30 @@
6492065961
case OE_Rollback:
6492165962
case OE_Abort:
6492265963
case OE_Fail: {
6492365964
int j, n1, n2;
6492465965
char zErrMsg[200];
64925
- sqlite3_snprintf(sizeof(zErrMsg), zErrMsg,
65966
+ sqlite3_snprintf(ArraySize(zErrMsg), zErrMsg,
6492665967
pIdx->nColumn>1 ? "columns " : "column ");
6492765968
n1 = strlen(zErrMsg);
64928
- for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
65969
+ for(j=0; j<pIdx->nColumn && n1<ArraySize(zErrMsg)-30; j++){
6492965970
char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
6493065971
n2 = strlen(zCol);
6493165972
if( j>0 ){
64932
- sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], ", ");
65973
+ sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], ", ");
6493365974
n1 += 2;
6493465975
}
64935
- if( n1+n2>sizeof(zErrMsg)-30 ){
64936
- sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "...");
65976
+ if( n1+n2>ArraySize(zErrMsg)-30 ){
65977
+ sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], "...");
6493765978
n1 += 3;
6493865979
break;
6493965980
}else{
64940
- sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol);
65981
+ sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol);
6494165982
n1 += n2;
6494265983
}
6494365984
}
64944
- sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1],
65985
+ sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1],
6494565986
pIdx->nColumn>1 ? " are not unique" : " is not unique");
6494665987
sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErrMsg,0);
6494765988
break;
6494865989
}
6494965990
case OE_Ignore: {
@@ -64976,11 +66017,10 @@
6497666017
Parse *pParse, /* The parser context */
6497766018
Table *pTab, /* the table into which we are inserting */
6497866019
int baseCur, /* Index of a read/write cursor pointing at pTab */
6497966020
int regRowid, /* Range of content */
6498066021
int *aRegIdx, /* Register used by each index. 0 for unused indices */
64981
- int rowidChng, /* True if the record number will change */
6498266022
int isUpdate, /* True for UPDATE, False for INSERT */
6498366023
int newIdx, /* Index of NEW table for triggers. -1 if none */
6498466024
int appendBias /* True if this is likely to be an append */
6498566025
){
6498666026
int i;
@@ -66534,11 +67574,11 @@
6653467574
** May you share freely, never taking more than you give.
6653567575
**
6653667576
*************************************************************************
6653767577
** This file contains code used to implement the PRAGMA command.
6653867578
**
66539
-** $Id: pragma.c,v 1.192 2008/10/31 10:53:23 danielk1977 Exp $
67579
+** $Id: pragma.c,v 1.194 2008/11/17 19:18:55 danielk1977 Exp $
6654067580
*/
6654167581
6654267582
/* Ignore this whole file if pragmas are disabled
6654367583
*/
6654467584
#if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
@@ -66562,11 +67602,11 @@
6656267602
int i, n;
6656367603
if( isdigit(*z) ){
6656467604
return atoi(z);
6656567605
}
6656667606
n = strlen(z);
66567
- for(i=0; i<sizeof(iLength); i++){
67607
+ for(i=0; i<ArraySize(iLength); i++){
6656867608
if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
6656967609
return iValue[i];
6657067610
}
6657167611
}
6657267612
return 1;
@@ -66712,11 +67752,11 @@
6671267752
** flag if there are any active statements. */
6671367753
{ "read_uncommitted", SQLITE_ReadUncommitted },
6671467754
};
6671567755
int i;
6671667756
const struct sPragmaType *p;
66717
- for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
67757
+ for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
6671867758
if( sqlite3StrICmp(zLeft, p->zName)==0 ){
6671967759
sqlite3 *db = pParse->db;
6672067760
Vdbe *v;
6672167761
v = sqlite3GetVdbe(pParse);
6672267762
if( v ){
@@ -66976,11 +68016,11 @@
6697668016
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
6697768017
}else
6697868018
6697968019
/*
6698068020
** PRAGMA [database.]journal_mode
66981
- ** PRAGMA [database.]journal_mode = (delete|persist|memory|off)
68021
+ ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
6698268022
*/
6698368023
if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
6698468024
int eMode;
6698568025
static char * const azModeName[] = {
6698668026
"delete", "persist", "off", "truncate", "memory"
@@ -67891,11 +68931,11 @@
6789168931
*************************************************************************
6789268932
** This file contains the implementation of the sqlite3_prepare()
6789368933
** interface, and routines that contribute to loading the database schema
6789468934
** from disk.
6789568935
**
67896
-** $Id: prepare.c,v 1.98 2008/10/31 10:53:23 danielk1977 Exp $
68936
+** $Id: prepare.c,v 1.101 2008/11/19 16:52:44 danielk1977 Exp $
6789768937
*/
6789868938
6789968939
/*
6790068940
** Fill the InitData structure with an error message that indicates
6790168941
** that the database is corrupt.
@@ -67928,23 +68968,24 @@
6792868968
** argv[0] = name of thing being created
6792968969
** argv[1] = root page number for table or index. 0 for trigger or view.
6793068970
** argv[2] = SQL text for the CREATE statement.
6793168971
**
6793268972
*/
67933
-SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
68973
+SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
6793468974
InitData *pData = (InitData*)pInit;
6793568975
sqlite3 *db = pData->db;
6793668976
int iDb = pData->iDb;
6793768977
68978
+ assert( argc==3 );
68979
+ UNUSED_PARAMETER2(NotUsed, argc);
6793868980
assert( sqlite3_mutex_held(db->mutex) );
6793968981
DbClearProperty(db, iDb, DB_Empty);
6794068982
if( db->mallocFailed ){
6794168983
corruptSchema(pData, argv[0], 0);
6794268984
return SQLITE_NOMEM;
6794368985
}
6794468986
67945
- assert( argc==3 );
6794668987
assert( iDb>=0 && iDb<db->nDb );
6794768988
if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
6794868989
if( argv[1]==0 ){
6794968990
corruptSchema(pData, argv[0], 0);
6795068991
}else if( argv[2] && argv[2][0] ){
@@ -68120,11 +69161,11 @@
6812069161
** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
6812169162
** the possible values of meta[4].
6812269163
*/
6812369164
if( rc==SQLITE_OK ){
6812469165
int i;
68125
- for(i=0; i<sizeof(meta)/sizeof(meta[0]); i++){
69166
+ for(i=0; i<ArraySize(meta); i++){
6812669167
rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
6812769168
if( rc ){
6812869169
sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
6812969170
goto initone_error_out;
6813069171
}
@@ -68702,11 +69743,11 @@
6870269743
**
6870369744
*************************************************************************
6870469745
** This file contains C code routines that are called by the parser
6870569746
** to handle SELECT statements in SQLite.
6870669747
**
68707
-** $Id: select.c,v 1.482 2008/10/31 10:53:23 danielk1977 Exp $
69748
+** $Id: select.c,v 1.486 2008/11/19 09:05:27 danielk1977 Exp $
6870869749
*/
6870969750
6871069751
6871169752
/*
6871269753
** Delete all the content of a Select structure but do not deallocate
@@ -68754,11 +69795,11 @@
6875469795
){
6875569796
Select *pNew;
6875669797
Select standin;
6875769798
sqlite3 *db = pParse->db;
6875869799
pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
68759
- assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */
69800
+ assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
6876069801
if( pNew==0 ){
6876169802
pNew = &standin;
6876269803
memset(pNew, 0, sizeof(*pNew));
6876369804
}
6876469805
if( pEList==0 ){
@@ -68833,18 +69874,18 @@
6883369874
apAll[0] = pA;
6883469875
apAll[1] = pB;
6883569876
apAll[2] = pC;
6883669877
for(i=0; i<3 && apAll[i]; i++){
6883769878
p = apAll[i];
68838
- for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
69879
+ for(j=0; j<ArraySize(keywords); j++){
6883969880
if( p->n==keywords[j].nChar
6884069881
&& sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){
6884169882
jointype |= keywords[j].code;
6884269883
break;
6884369884
}
6884469885
}
68845
- if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
69886
+ if( j>=ArraySize(keywords) ){
6884669887
jointype |= JT_ERROR;
6884769888
break;
6884869889
}
6884969890
}
6885069891
if(
@@ -71262,11 +72303,13 @@
7126272303
struct SrcList_item *pSubitem; /* The subquery */
7126372304
sqlite3 *db = pParse->db;
7126472305
7126572306
/* Check to see if flattening is permitted. Return 0 if not.
7126672307
*/
72308
+ assert( p!=0 );
7126772309
if( p==0 ) return 0;
72310
+ assert( p->pPrior==0 ); /* Unable to flatten compound queries */
7126872311
pSrc = p->pSrc;
7126972312
assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
7127072313
pSubitem = &pSrc->a[iFrom];
7127172314
iParent = pSubitem->iCursor;
7127272315
pSub = pSubitem->pSelect;
@@ -71374,96 +72417,150 @@
7137472417
** be of the form:
7137572418
**
7137672419
** SELECT <expr-list> FROM (<sub-query>) <where-clause>
7137772420
**
7137872421
** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
71379
- ** creates N copies of the parent query without any ORDER BY, LIMIT or
72422
+ ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
7138072423
** OFFSET clauses and joins them to the left-hand-side of the original
7138172424
** using UNION ALL operators. In this case N is the number of simple
7138272425
** select statements in the compound sub-query.
72426
+ **
72427
+ ** Example:
72428
+ **
72429
+ ** SELECT a+1 FROM (
72430
+ ** SELECT x FROM tab
72431
+ ** UNION ALL
72432
+ ** SELECT y FROM tab
72433
+ ** UNION ALL
72434
+ ** SELECT abs(z*2) FROM tab2
72435
+ ** ) WHERE a!=5 ORDER BY 1
72436
+ **
72437
+ ** Transformed into:
72438
+ **
72439
+ ** SELECT x+1 FROM tab WHERE x+1!=5
72440
+ ** UNION ALL
72441
+ ** SELECT y+1 FROM tab WHERE y+1!=5
72442
+ ** UNION ALL
72443
+ ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
72444
+ ** ORDER BY 1
72445
+ **
72446
+ ** We call this the "compound-subquery flattening".
7138372447
*/
7138472448
for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
7138572449
Select *pNew;
7138672450
ExprList *pOrderBy = p->pOrderBy;
7138772451
Expr *pLimit = p->pLimit;
71388
- Expr *pOffset = p->pOffset;
7138972452
Select *pPrior = p->pPrior;
7139072453
p->pOrderBy = 0;
7139172454
p->pSrc = 0;
7139272455
p->pPrior = 0;
7139372456
p->pLimit = 0;
7139472457
pNew = sqlite3SelectDup(db, p);
71395
- pNew->pPrior = pPrior;
71396
- p->pPrior = pNew;
72458
+ p->pLimit = pLimit;
7139772459
p->pOrderBy = pOrderBy;
72460
+ p->pSrc = pSrc;
7139872461
p->op = TK_ALL;
71399
- p->pSrc = pSrc;
71400
- p->pLimit = pLimit;
71401
- p->pOffset = pOffset;
7140272462
p->pRightmost = 0;
71403
- pNew->pRightmost = 0;
72463
+ if( pNew==0 ){
72464
+ pNew = pPrior;
72465
+ }else{
72466
+ pNew->pPrior = pPrior;
72467
+ pNew->pRightmost = 0;
72468
+ }
72469
+ p->pPrior = pNew;
72470
+ if( db->mallocFailed ) return 1;
7140472471
}
7140572472
7140672473
/* Begin flattening the iFrom-th entry of the FROM clause
7140772474
** in the outer query.
7140872475
*/
7140972476
pSub = pSub1 = pSubitem->pSelect;
72477
+
72478
+ /* Delete the transient table structure associated with the
72479
+ ** subquery
72480
+ */
72481
+ sqlite3DbFree(db, pSubitem->zDatabase);
72482
+ sqlite3DbFree(db, pSubitem->zName);
72483
+ sqlite3DbFree(db, pSubitem->zAlias);
72484
+ pSubitem->zDatabase = 0;
72485
+ pSubitem->zName = 0;
72486
+ pSubitem->zAlias = 0;
72487
+ pSubitem->pSelect = 0;
72488
+
72489
+ /* Defer deleting the Table object associated with the
72490
+ ** subquery until code generation is
72491
+ ** complete, since there may still exist Expr.pTab entries that
72492
+ ** refer to the subquery even after flattening. Ticket #3346.
72493
+ */
72494
+ if( pSubitem->pTab!=0 ){
72495
+ Table *pTabToDel = pSubitem->pTab;
72496
+ if( pTabToDel->nRef==1 ){
72497
+ pTabToDel->pNextZombie = pParse->pZombieTab;
72498
+ pParse->pZombieTab = pTabToDel;
72499
+ }else{
72500
+ pTabToDel->nRef--;
72501
+ }
72502
+ pSubitem->pTab = 0;
72503
+ }
72504
+
72505
+ /* The following loop runs once for each term in a compound-subquery
72506
+ ** flattening (as described above). If we are doing a different kind
72507
+ ** of flattening - a flattening other than a compound-subquery flattening -
72508
+ ** then this loop only runs once.
72509
+ **
72510
+ ** This loop moves all of the FROM elements of the subquery into the
72511
+ ** the FROM clause of the outer query. Before doing this, remember
72512
+ ** the cursor number for the original outer query FROM element in
72513
+ ** iParent. The iParent cursor will never be used. Subsequent code
72514
+ ** will scan expressions looking for iParent references and replace
72515
+ ** those references with expressions that resolve to the subquery FROM
72516
+ ** elements we are now copying in.
72517
+ */
7141072518
for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
71411
- int nSubSrc = pSubSrc->nSrc;
72519
+ int nSubSrc;
7141272520
int jointype = 0;
71413
- pSubSrc = pSub->pSrc;
71414
- pSrc = pParent->pSrc;
71415
-
71416
- /* Move all of the FROM elements of the subquery into the
71417
- ** the FROM clause of the outer query. Before doing this, remember
71418
- ** the cursor number for the original outer query FROM element in
71419
- ** iParent. The iParent cursor will never be used. Subsequent code
71420
- ** will scan expressions looking for iParent references and replace
71421
- ** those references with expressions that resolve to the subquery FROM
71422
- ** elements we are now copying in.
71423
- */
72521
+ pSubSrc = pSub->pSrc; /* FROM clause of subquery */
72522
+ nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
72523
+ pSrc = pParent->pSrc; /* FROM clause of the outer query */
72524
+
7142472525
if( pSrc ){
71425
- Table *pTabToDel;
71426
- pSubitem = &pSrc->a[iFrom];
71427
- nSubSrc = pSubSrc->nSrc;
72526
+ assert( pParent==p ); /* First time through the loop */
7142872527
jointype = pSubitem->jointype;
71429
- sqlite3DbFree(db, pSubitem->zDatabase);
71430
- sqlite3DbFree(db, pSubitem->zName);
71431
- sqlite3DbFree(db, pSubitem->zAlias);
71432
- pSubitem->zDatabase = 0;
71433
- pSubitem->zName = 0;
71434
- pSubitem->zAlias = 0;
71435
-
71436
- /* If the FROM element is a subquery, defer deleting the Table
71437
- ** object associated with that subquery until code generation is
71438
- ** complete, since there may still exist Expr.pTab entires that
71439
- ** refer to the subquery even after flattening. Ticket #3346.
71440
- */
71441
- if( (pTabToDel = pSubitem->pTab)!=0 ){
71442
- if( pTabToDel->nRef==1 ){
71443
- pTabToDel->pNextZombie = pParse->pZombieTab;
71444
- pParse->pZombieTab = pTabToDel;
71445
- }else{
71446
- pTabToDel->nRef--;
71447
- }
71448
- }
71449
- pSubitem->pTab = 0;
71450
- }
71451
- if( nSubSrc!=1 || !pSrc ){
71452
- int extra = nSubSrc - 1;
71453
- for(i=(pSrc?1:0); i<nSubSrc; i++){
71454
- pSrc = sqlite3SrcListAppend(db, pSrc, 0, 0);
71455
- if( pSrc==0 ){
71456
- pParent->pSrc = 0;
71457
- return 1;
71458
- }
71459
- }
71460
- pParent->pSrc = pSrc;
71461
- for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
71462
- pSrc->a[i] = pSrc->a[i-extra];
71463
- }
71464
- }
72528
+ }else{
72529
+ assert( pParent!=p ); /* 2nd and subsequent times through the loop */
72530
+ pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
72531
+ if( pSrc==0 ){
72532
+ assert( db->mallocFailed );
72533
+ break;
72534
+ }
72535
+ }
72536
+
72537
+ /* The subquery uses a single slot of the FROM clause of the outer
72538
+ ** query. If the subquery has more than one element in its FROM clause,
72539
+ ** then expand the outer query to make space for it to hold all elements
72540
+ ** of the subquery.
72541
+ **
72542
+ ** Example:
72543
+ **
72544
+ ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
72545
+ **
72546
+ ** The outer query has 3 slots in its FROM clause. One slot of the
72547
+ ** outer query (the middle slot) is used by the subquery. The next
72548
+ ** block of code will expand the out query to 4 slots. The middle
72549
+ ** slot is expanded to two slots in order to make space for the
72550
+ ** two elements in the FROM clause of the subquery.
72551
+ */
72552
+ if( nSubSrc>1 ){
72553
+ pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
72554
+ if( db->mallocFailed ){
72555
+ break;
72556
+ }
72557
+ }
72558
+
72559
+ /* Transfer the FROM clause terms from the subquery into the
72560
+ ** outer query.
72561
+ */
7146572562
for(i=0; i<nSubSrc; i++){
7146672563
pSrc->a[i+iFrom] = pSubSrc->a[i];
7146772564
memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
7146872565
}
7146972566
pSrc->a[iFrom].jointype = jointype;
@@ -71554,11 +72651,11 @@
7155472651
** 1. There is a single object in the FROM clause.
7155572652
**
7155672653
** 2. There is a single expression in the result set, and it is
7155772654
** either min(x) or max(x), where x is a column reference.
7155872655
*/
71559
-static int minMaxQuery(Parse *pParse, Select *p){
72656
+static int minMaxQuery(Select *p){
7156072657
Expr *pExpr;
7156172658
ExprList *pEList = p->pEList;
7156272659
7156372660
if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
7156472661
pExpr = pEList->a[0].pExpr;
@@ -71854,11 +72951,12 @@
7185472951
** are walked without any actions being taken at each node. Presumably,
7185572952
** when this routine is used for Walker.xExprCallback then
7185672953
** Walker.xSelectCallback is set to do something useful for every
7185772954
** subquery in the parser tree.
7185872955
*/
71859
-static int exprWalkNoop(Walker *pWalker, Expr *pExpr){
72956
+static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
72957
+ UNUSED_PARAMETER2(NotUsed, NotUsed2);
7186072958
return WRC_Continue;
7186172959
}
7186272960
7186372961
/*
7186472962
** This routine "expands" a SELECT statement and all of its subqueries.
@@ -72645,11 +73743,11 @@
7264573743
** + The optimizer code in where.c (the thing that decides which
7264673744
** index or indices to use) should place a different priority on
7264773745
** satisfying the 'ORDER BY' clause than it does in other cases.
7264873746
** Refer to code and comments in where.c for details.
7264973747
*/
72650
- flag = minMaxQuery(pParse, p);
73748
+ flag = minMaxQuery(p);
7265173749
if( flag ){
7265273750
pDel = pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->pList);
7265373751
if( pMinMax && !db->mallocFailed ){
7265473752
pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN;
7265573753
pMinMax->a[0].pExpr->op = TK_COLUMN;
@@ -73027,11 +74125,11 @@
7302774125
** May you share freely, never taking more than you give.
7302874126
**
7302974127
*************************************************************************
7303074128
**
7303174129
**
73032
-** $Id: trigger.c,v 1.129 2008/08/20 16:35:10 drh Exp $
74130
+** $Id: trigger.c,v 1.130 2008/11/19 09:05:27 danielk1977 Exp $
7303374131
*/
7303474132
7303574133
#ifndef SQLITE_OMIT_TRIGGER
7303674134
/*
7303774135
** Delete a linked list of TriggerStep structures.
@@ -73619,11 +74717,10 @@
7361974717
**
7362074718
** The returned bit vector is some combination of TRIGGER_BEFORE and
7362174719
** TRIGGER_AFTER.
7362274720
*/
7362374721
SQLITE_PRIVATE int sqlite3TriggersExist(
73624
- Parse *pParse, /* Used to check for recursive triggers */
7362574722
Table *pTab, /* The table the contains the triggers */
7362674723
int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
7362774724
ExprList *pChanges /* Columns that change in an UPDATE statement */
7362874725
){
7362974726
Trigger *pTrigger;
@@ -73883,11 +74980,11 @@
7388374980
**
7388474981
*************************************************************************
7388574982
** This file contains C code routines that are called by the parser
7388674983
** to handle UPDATE statements.
7388774984
**
73888
-** $Id: update.c,v 1.186 2008/10/31 10:53:23 danielk1977 Exp $
74985
+** $Id: update.c,v 1.187 2008/11/19 09:05:27 danielk1977 Exp $
7388974986
*/
7389074987
7389174988
#ifndef SQLITE_OMIT_VIRTUALTABLE
7389274989
/* Forward declaration */
7389374990
static void updateVirtualTable(
@@ -74012,11 +75109,11 @@
7401275109
7401375110
/* Figure out if we have any triggers and if the table being
7401475111
** updated is a view
7401575112
*/
7401675113
#ifndef SQLITE_OMIT_TRIGGER
74017
- triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
75114
+ triggers_exist = sqlite3TriggersExist(pTab, TK_UPDATE, pChanges);
7401875115
isView = pTab->pSelect!=0;
7401975116
#else
7402075117
# define triggers_exist 0
7402175118
# define isView 0
7402275119
#endif
@@ -74395,11 +75492,11 @@
7439575492
sqlite3VdbeJumpHere(v, j1);
7439675493
7439775494
/* Create the new index entries and the new record.
7439875495
*/
7439975496
sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid,
74400
- aRegIdx, chngRowid, 1, -1, 0);
75497
+ aRegIdx, 1, -1, 0);
7440175498
}
7440275499
7440375500
/* Increment the row counter
7440475501
*/
7440575502
if( db->flags & SQLITE_CountRows && !pParse->trigStack){
@@ -74568,11 +75665,11 @@
7456875665
** This file contains code used to implement the VACUUM command.
7456975666
**
7457075667
** Most of the code in this file may be omitted by defining the
7457175668
** SQLITE_OMIT_VACUUM macro.
7457275669
**
74573
-** $Id: vacuum.c,v 1.83 2008/08/26 21:07:27 drh Exp $
75670
+** $Id: vacuum.c,v 1.84 2008/11/17 19:18:55 danielk1977 Exp $
7457475671
*/
7457575672
7457675673
#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
7457775674
/*
7457875675
** Execute zSql on database db. Return an error code.
@@ -74803,11 +75900,11 @@
7480375900
7480475901
assert( 1==sqlite3BtreeIsInTrans(pTemp) );
7480575902
assert( 1==sqlite3BtreeIsInTrans(pMain) );
7480675903
7480775904
/* Copy Btree meta values */
74808
- for(i=0; i<sizeof(aCopy)/sizeof(aCopy[0]); i+=2){
75905
+ for(i=0; i<ArraySize(aCopy); i+=2){
7480975906
rc = sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
7481075907
if( rc!=SQLITE_OK ) goto end_of_vacuum;
7481175908
rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
7481275909
if( rc!=SQLITE_OK ) goto end_of_vacuum;
7481375910
}
@@ -74866,11 +75963,11 @@
7486675963
** May you share freely, never taking more than you give.
7486775964
**
7486875965
*************************************************************************
7486975966
** This file contains code used to help implement virtual tables.
7487075967
**
74871
-** $Id: vtab.c,v 1.76 2008/08/20 16:35:10 drh Exp $
75968
+** $Id: vtab.c,v 1.78 2008/11/13 19:12:36 danielk1977 Exp $
7487275969
*/
7487375970
#ifndef SQLITE_OMIT_VIRTUALTABLE
7487475971
7487575972
static int createModule(
7487675973
sqlite3 *db, /* Database in which module is registered */
@@ -74900,10 +75997,12 @@
7490075997
sqlite3DbFree(db, pDel);
7490175998
if( pDel==pMod ){
7490275999
db->mallocFailed = 1;
7490376000
}
7490476001
sqlite3ResetInternalSchema(db, 0);
76002
+ }else if( xDestroy ){
76003
+ xDestroy(pAux);
7490576004
}
7490676005
rc = sqlite3ApiExit(db, SQLITE_OK);
7490776006
sqlite3_mutex_leave(db->mutex);
7490876007
return rc;
7490976008
}
@@ -75564,11 +76663,11 @@
7556476663
/* Special case: If db->aVTrans is NULL and db->nVTrans is greater
7556576664
** than zero, then this function is being called from within a
7556676665
** virtual module xSync() callback. It is illegal to write to
7556776666
** virtual module tables in this case, so return SQLITE_LOCKED.
7556876667
*/
75569
- if( 0==db->aVTrans && db->nVTrans>0 ){
76668
+ if( sqlite3VtabInSync(db) ){
7557076669
return SQLITE_LOCKED;
7557176670
}
7557276671
if( !pVtab ){
7557376672
return SQLITE_OK;
7557476673
}
@@ -75712,18 +76811,13 @@
7571276811
** generating the code that loops through a table looking for applicable
7571376812
** rows. Indices are selected and used to speed the search when doing
7571476813
** so is applicable. Because this module is responsible for selecting
7571576814
** indices, you might also think of this module as the "query optimizer".
7571676815
**
75717
-** $Id: where.c,v 1.328 2008/11/03 09:06:06 danielk1977 Exp $
76816
+** $Id: where.c,v 1.330 2008/11/17 19:18:55 danielk1977 Exp $
7571876817
*/
7571976818
75720
-/*
75721
-** The number of bits in a Bitmask. "BMS" means "BitMask Size".
75722
-*/
75723
-#define BMS (sizeof(Bitmask)*8)
75724
-
7572576819
/*
7572676820
** Trace output macros
7572776821
*/
7572876822
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
7572976823
SQLITE_PRIVATE int sqlite3WhereTrace = 0;
@@ -75834,11 +76928,11 @@
7583476928
** numbers all get mapped into bit numbers that begin with 0 and contain
7583576929
** no gaps.
7583676930
*/
7583776931
struct ExprMaskSet {
7583876932
int n; /* Number of assigned cursor values */
75839
- int ix[sizeof(Bitmask)*8]; /* Cursor assigned to each bit */
76933
+ int ix[BMS]; /* Cursor assigned to each bit */
7584076934
};
7584176935
7584276936
7584376937
/*
7584476938
** Bitmasks for the operators that indices are able to exploit. An
@@ -78015,11 +79109,11 @@
7801579109
}else
7801679110
#endif
7801779111
if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
7801879112
int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
7801979113
sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
78020
- if( !pWInfo->okOnePass && pTab->nCol<(sizeof(Bitmask)*8) ){
79114
+ if( !pWInfo->okOnePass && pTab->nCol<BMS ){
7802179115
Bitmask b = pTabItem->colUsed;
7802279116
int n = 0;
7802379117
for(; b; b=b>>1, n++){}
7802479118
sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-2, n);
7802579119
assert( n<=pTab->nCol );
@@ -79896,10 +80990,12 @@
7989680990
pParser->yyidx = -1;
7989780991
#ifdef YYTRACKMAXSTACKDEPTH
7989880992
pParser->yyidxMax = 0;
7989980993
#endif
7990080994
#if YYSTACKDEPTH<=0
80995
+ pParser->yystack = NULL;
80996
+ pParser->yystksz = 0;
7990180997
yyGrowStack(pParser);
7990280998
#endif
7990380999
}
7990481000
return pParser;
7990581001
}
@@ -82745,11 +83841,11 @@
8274583841
** Main file for the SQLite library. The routines in this file
8274683842
** implement the programmer interface to the library. Routines in
8274783843
** other files are for internal use by SQLite and should not be
8274883844
** accessed by users of the library.
8274983845
**
82750
-** $Id: main.c,v 1.510 2008/11/04 13:46:28 drh Exp $
83846
+** $Id: main.c,v 1.514 2008/11/19 09:05:27 danielk1977 Exp $
8275183847
*/
8275283848
8275383849
#ifdef SQLITE_ENABLE_FTS3
8275483850
/************** Include fts3.h in the middle of main.c ***********************/
8275583851
/************** Begin file fts3.h ********************************************/
@@ -83125,10 +84221,24 @@
8312584221
sqlite3GlobalConfig.pPage = va_arg(ap, void*);
8312684222
sqlite3GlobalConfig.szPage = va_arg(ap, int);
8312784223
sqlite3GlobalConfig.nPage = va_arg(ap, int);
8312884224
break;
8312984225
}
84226
+
84227
+ case SQLITE_CONFIG_PCACHE: {
84228
+ /* Specify an alternative malloc implementation */
84229
+ sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
84230
+ break;
84231
+ }
84232
+
84233
+ case SQLITE_CONFIG_GETPCACHE: {
84234
+ if( sqlite3GlobalConfig.pcache.xInit==0 ){
84235
+ sqlite3PCacheSetDefault();
84236
+ }
84237
+ *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
84238
+ break;
84239
+ }
8313084240
8313184241
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8313284242
case SQLITE_CONFIG_HEAP: {
8313384243
/* Designate a buffer for heap memory space */
8313484244
sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
@@ -83155,18 +84265,10 @@
8315584265
sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
8315684266
#endif
8315784267
}
8315884268
break;
8315984269
}
83160
-#endif
83161
-
83162
-#if defined(SQLITE_ENABLE_MEMSYS6)
83163
- case SQLITE_CONFIG_CHUNKALLOC: {
83164
- sqlite3GlobalConfig.nSmall = va_arg(ap, int);
83165
- sqlite3GlobalConfig.m = *sqlite3MemGetMemsys6();
83166
- break;
83167
- }
8316884270
#endif
8316984271
8317084272
case SQLITE_CONFIG_LOOKASIDE: {
8317184273
sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
8317284274
sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
@@ -83326,10 +84428,11 @@
8332684428
int nKey1, const void *pKey1,
8332784429
int nKey2, const void *pKey2
8332884430
){
8332984431
int r = sqlite3StrNICmp(
8333084432
(const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
84433
+ UNUSED_PARAMETER(NotUsed);
8333184434
if( 0==r ){
8333284435
r = nKey1-nKey2;
8333384436
}
8333484437
return r;
8333584438
}
@@ -92737,10 +93840,17 @@
9273793840
elem->data = data;
9273893841
}
9273993842
return old_data;
9274093843
}
9274193844
if( data==0 ) return 0;
93845
+ if( pH->htsize==0 ){
93846
+ fts3Rehash(pH,8);
93847
+ if( pH->htsize==0 ){
93848
+ pH->count = 0;
93849
+ return data;
93850
+ }
93851
+ }
9274293852
new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) );
9274393853
if( new_elem==0 ) return data;
9274493854
if( pH->copyKey && pKey!=0 ){
9274593855
new_elem->pKey = fts3HashMalloc( nKey );
9274693856
if( new_elem->pKey==0 ){
@@ -92751,18 +93861,10 @@
9275193861
}else{
9275293862
new_elem->pKey = (void*)pKey;
9275393863
}
9275493864
new_elem->nKey = nKey;
9275593865
pH->count++;
92756
- if( pH->htsize==0 ){
92757
- fts3Rehash(pH,8);
92758
- if( pH->htsize==0 ){
92759
- pH->count = 0;
92760
- fts3HashFree(new_elem);
92761
- return data;
92762
- }
92763
- }
9276493866
if( pH->count > pH->htsize ){
9276593867
fts3Rehash(pH,pH->htsize*2);
9276693868
}
9276793869
assert( pH->htsize>0 );
9276893870
assert( (pH->htsize & (pH->htsize-1))==0 );
@@ -94022,11 +95124,11 @@
9402295124
**
9402395125
*************************************************************************
9402495126
** This file contains code for implementations of the r-tree and r*-tree
9402595127
** algorithms packaged as an SQLite virtual table module.
9402695128
**
94027
-** $Id: rtree.c,v 1.10 2008/10/25 17:10:10 danielk1977 Exp $
95129
+** $Id: rtree.c,v 1.11 2008/11/12 15:24:27 drh Exp $
9402895130
*/
9402995131
9403095132
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
9403195133
9403295134
/*
@@ -94232,12 +95334,16 @@
9423295334
struct RtreeCell {
9423395335
i64 iRowid;
9423495336
RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
9423595337
};
9423695338
94237
-#define MAX(x,y) ((x) < (y) ? (y) : (x))
94238
-#define MIN(x,y) ((x) > (y) ? (y) : (x))
95339
+#ifndef MAX
95340
+# define MAX(x,y) ((x) < (y) ? (y) : (x))
95341
+#endif
95342
+#ifndef MIN
95343
+# define MIN(x,y) ((x) > (y) ? (y) : (x))
95344
+#endif
9423995345
9424095346
/*
9424195347
** Functions to deserialize a 16 bit integer, 32 bit real number and
9424295348
** 64 bit integer. The deserialized value is returned.
9424395349
*/
9424495350
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.6.4. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% are more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -9,17 +9,17 @@
9 **
10 ** This file is all you need to compile SQLite. To use SQLite in other
11 ** programs, you need this file and the "sqlite3.h" header file that defines
12 ** the programming interface to the SQLite library. (If you do not have
13 ** the "sqlite3.h" header file at hand, you will find a copy in the first
14 ** 6569 lines past this header comment.) Additional code files may be
15 ** needed if you want a wrapper to interface SQLite with your choice of
16 ** programming language. The code for the "sqlite3" command-line shell
17 ** is also in a separate file. This file contains only code for the core
18 ** SQLite library.
19 **
20 ** This amalgamation was generated on 2008-11-10 00:14:36 UTC.
21 */
22 #define SQLITE_CORE 1
23 #define SQLITE_AMALGAMATION 1
24 #ifndef SQLITE_PRIVATE
25 # define SQLITE_PRIVATE static
@@ -39,11 +39,11 @@
39 ** May you share freely, never taking more than you give.
40 **
41 *************************************************************************
42 ** Internal interface definitions for SQLite.
43 **
44 ** @(#) $Id: sqliteInt.h,v 1.788 2008/11/05 16:37:35 drh Exp $
45 */
46 #ifndef _SQLITEINT_H_
47 #define _SQLITEINT_H_
48
49 /*
@@ -485,11 +485,11 @@
485 ** The name of this file under configuration management is "sqlite.h.in".
486 ** The makefile makes some minor changes to this file (such as inserting
487 ** the version number) and changes its name to "sqlite3.h" as
488 ** part of the build process.
489 **
490 ** @(#) $Id: sqlite.h.in,v 1.409 2008/11/07 00:06:18 drh Exp $
491 */
492 #ifndef _SQLITE3_H_
493 #define _SQLITE3_H_
494 #include <stdarg.h> /* Needed for the definition of va_list */
495
@@ -562,12 +562,12 @@
562 **
563 ** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer
564 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z
565 ** are the major version, minor version, and release number.
566 */
567 #define SQLITE_VERSION "3.6.4"
568 #define SQLITE_VERSION_NUMBER 3006004
569
570 /*
571 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
572 ** KEYWORDS: sqlite3_version
573 **
@@ -1042,11 +1042,11 @@
1042 **
1043 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1044 ** sync operation only needs to flush data to mass storage. Inode
1045 ** information need not be flushed. The SQLITE_SYNC_NORMAL flag means
1046 ** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means
1047 ** to use Mac OS-X style fullsync instead of fsync().
1048 */
1049 #define SQLITE_SYNC_NORMAL 0x00002
1050 #define SQLITE_SYNC_FULL 0x00003
1051 #define SQLITE_SYNC_DATAONLY 0x00010
1052
@@ -1074,11 +1074,11 @@
1074 ** This object defines the methods used to perform various operations
1075 ** against the open file represented by the [sqlite3_file] object.
1076 **
1077 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1078 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1079 ** The second choice is a Mac OS-X style fullsync. The [SQLITE_SYNC_DATAONLY]
1080 ** flag may be ORed in to indicate that only the data of the file
1081 ** and not its inode needs to be synced.
1082 **
1083 ** The integer values to xLock() and xUnlock() are one of
1084 ** <ul>
@@ -1729,11 +1729,14 @@
1729 ** scratch buffers or if no scratch buffer space is specified, then SQLite
1730 ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
1731 **
1732 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1733 ** <dd>This option specifies a static memory buffer that SQLite can use for
1734 ** the database page cache. There are three arguments: A pointer to the
 
 
 
1735 ** memory, the size of each page buffer (sz), and the number of pages (N).
1736 ** The sz argument must be a power of two between 512 and 32768. The first
1737 ** argument should point to an allocation of at least sz*N bytes of memory.
1738 ** SQLite will use the memory provided by the first argument to satisfy its
1739 ** memory needs for the first N pages that it adds to cache. If additional
@@ -1774,10 +1777,21 @@
1774 ** <dd>This option takes two arguments that determine the default
1775 ** memory allcation lookaside optimization. The first argument is the
1776 ** size of each lookaside buffer slot and the second is the number of
1777 ** slots allocated to each database connection.</dd>
1778 **
 
 
 
 
 
 
 
 
 
 
 
1779 ** </dl>
1780 */
1781 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1782 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1783 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1787,12 +1801,14 @@
1787 #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
1788 #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
1789 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
1790 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
1791 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1792 #define SQLITE_CONFIG_CHUNKALLOC 12 /* int threshold */
1793 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
 
 
1794
1795 /*
1796 ** CAPI3REF: Configuration Options {H10170} <S20000>
1797 ** EXPERIMENTAL
1798 **
@@ -2838,11 +2854,11 @@
2838 #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
2839 #define SQLITE_REINDEX 27 /* Index Name NULL */
2840 #define SQLITE_ANALYZE 28 /* Table Name NULL */
2841 #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
2842 #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
2843 #define SQLITE_FUNCTION 31 /* Function Name NULL */
2844 #define SQLITE_COPY 0 /* No longer used */
2845
2846 /*
2847 ** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400>
2848 ** EXPERIMENTAL
@@ -4606,11 +4622,11 @@
4606 **
4607 ** These functions are [deprecated]. In order to maintain
4608 ** backwards compatibility with older code, these functions continue
4609 ** to be supported. However, new applications should avoid
4610 ** the use of these functions. To help encourage people to avoid
4611 ** using these functions, we are not going to tell you want they do.
4612 */
4613 #ifndef SQLITE_OMIT_DEPRECATED
4614 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4615 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4616 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
@@ -7010,10 +7026,153 @@
7010 ** </dl>
7011 */
7012 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
7013 #define SQLITE_STMTSTATUS_SORT 2
7014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7015 /*
7016 ** Undo the hack that converts floating point types to integer for
7017 ** builds on processors without floating point support.
7018 */
7019 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -7479,11 +7638,11 @@
7479
7480 /*
7481 ** A convenience macro that returns the number of elements in
7482 ** an array.
7483 */
7484 #define ArraySize(X) (sizeof(X)/sizeof(X[0]))
7485
7486 /*
7487 ** The following value as a destructor means to use sqlite3DbFree().
7488 ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
7489 */
@@ -7498,11 +7657,11 @@
7498 ** macro is used for this purpose. And instead of referencing the variable
7499 ** directly, we use its constant as a key to lookup the run-time allocated
7500 ** buffer that holds real variable. The constant is also the initializer
7501 ** for the run-time allocated buffer.
7502 **
7503 ** In the usually case where WSD is supported, the SQLITE_WSD and GLOBAL
7504 ** macros become no-ops and have zero performance impact.
7505 */
7506 #ifdef SQLITE_OMIT_WSD
7507 #define SQLITE_WSD const
7508 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
@@ -7513,10 +7672,29 @@
7513 #define SQLITE_WSD
7514 #define GLOBAL(t,v) v
7515 #define sqlite3GlobalConfig sqlite3Config
7516 #endif
7517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7518 /*
7519 ** Forward references to structures
7520 */
7521 typedef struct AggInfo AggInfo;
7522 typedef struct AuthContext AuthContext;
@@ -8195,11 +8373,11 @@
8195 *************************************************************************
8196 ** This header file defines the interface that the sqlite page cache
8197 ** subsystem. The page cache subsystem reads and writes a file a page
8198 ** at a time and provides a journal for rollback.
8199 **
8200 ** @(#) $Id: pager.h,v 1.86 2008/10/17 18:51:53 danielk1977 Exp $
8201 */
8202
8203 #ifndef _PAGER_H_
8204 #define _PAGER_H_
8205
@@ -8255,11 +8433,11 @@
8255 /*
8256 ** See source code comments for a detailed description of the following
8257 ** routines:
8258 */
8259 SQLITE_PRIVATE int sqlite3PagerOpen(sqlite3_vfs *, Pager **ppPager, const char*, int,int,int);
8260 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, BusyHandler *pBusyHandler);
8261 SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*));
8262 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*);
8263 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
8264 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
8265 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
@@ -8341,11 +8519,11 @@
8341 **
8342 *************************************************************************
8343 ** This header file defines the interface that the sqlite page cache
8344 ** subsystem.
8345 **
8346 ** @(#) $Id: pcache.h,v 1.14 2008/10/17 18:51:53 danielk1977 Exp $
8347 */
8348
8349 #ifndef _PCACHE_H_
8350
8351 typedef struct PgHdr PgHdr;
@@ -8363,29 +8541,23 @@
8363 Pager *pPager; /* The pager this page is part of */
8364 #ifdef SQLITE_CHECK_PAGES
8365 u32 pageHash; /* Hash of page content */
8366 #endif
8367 u16 flags; /* PGHDR flags defined below */
 
8368 /**********************************************************************
8369 ** Elements above are public. All that follows is private to pcache.c
8370 ** and should not be accessed by other modules.
8371 */
8372 i16 nRef; /* Number of users of this page */
8373 PCache *pCache; /* Cache that owns this page */
8374
8375 /**********************************************************************
8376 ** Elements above are accessible at any time by the owner of the cache
8377 ** without the need for a mutex. The elements that follow can only be
8378 ** accessed while holding the SQLITE_MUTEX_STATIC_LRU mutex.
8379 */
8380 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
8381 PgHdr *pNext, *pPrev; /* List of clean or dirty pages */
8382 PgHdr *pNextLru, *pPrevLru; /* Part of global LRU list */
8383 };
8384
8385 /* Bit values for PgHdr.flags */
8386 #define PGHDR_IN_JOURNAL 0x001 /* Page is in rollback journal */
8387 #define PGHDR_DIRTY 0x002 /* Page has changed */
8388 #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
8389 ** writing this page to the database */
8390 #define PGHDR_NEED_READ 0x008 /* Content is unread */
8391 #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
@@ -8397,12 +8569,10 @@
8397
8398 /* Page cache buffer management:
8399 ** These routines implement SQLITE_CONFIG_PAGECACHE.
8400 */
8401 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
8402 SQLITE_PRIVATE void *sqlite3PCacheMalloc(int sz);
8403 SQLITE_PRIVATE void sqlite3PCacheFree(void*);
8404
8405 /* Create a new pager cache.
8406 ** Under memory stress, invoke xStress to try to make pages clean.
8407 ** Only clean and unpinned pages can be reclaimed.
8408 */
@@ -8445,21 +8615,11 @@
8445
8446 /* Reset and close the cache object */
8447 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
8448
8449 /* Clear flags from pages of the page cache */
8450 SQLITE_PRIVATE void sqlite3PcacheClearFlags(PCache*, int mask);
8451
8452 /* Assert flags settings on all pages. Debugging only */
8453 #ifndef NDEBUG
8454 SQLITE_PRIVATE void sqlite3PcacheAssertFlags(PCache*, int trueMask, int falseMask);
8455 #else
8456 # define sqlite3PcacheAssertFlags(A,B,C)
8457 #endif
8458
8459 /* Return true if the number of dirty pages is 0 or 1 */
8460 SQLITE_PRIVATE int sqlite3PcacheZeroOrOneDirtyPages(PCache*);
8461
8462 /* Discard the contents of the cache */
8463 SQLITE_PRIVATE int sqlite3PcacheClear(PCache*);
8464
8465 /* Return the total number of outstanding page references */
@@ -8472,34 +8632,38 @@
8472
8473 /* Return the total number of pages stored in the cache */
8474 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
8475
8476 #ifdef SQLITE_CHECK_PAGES
8477 /* Iterate through all pages currently stored in the cache. This interface
8478 ** is only available if SQLITE_CHECK_PAGES is defined when the library is
8479 ** built.
8480 */
8481 SQLITE_PRIVATE void sqlite3PcacheIterate(PCache *pCache, void (*xIter)(PgHdr *));
8482 #endif
8483
8484 /* Set and get the suggested cache-size for the specified pager-cache.
8485 **
8486 ** If no global maximum is configured, then the system attempts to limit
8487 ** the total number of pages cached by purgeable pager-caches to the sum
8488 ** of the suggested cache-sizes.
8489 */
8490 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8491 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
 
 
 
8492
8493 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
8494 /* Try to return memory used by the pcache module to the main memory heap */
8495 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
8496 #endif
8497
8498 #ifdef SQLITE_TEST
8499 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
8500 #endif
 
 
8501
8502 #endif /* _PCACHE_H_ */
8503
8504 /************** End of pcache.h **********************************************/
8505 /************** Continuing where we left off in sqliteInt.h ******************/
@@ -9017,11 +9181,11 @@
9017 int nextPagesize; /* Pagesize after VACUUM if >0 */
9018 int nTable; /* Number of tables in the database */
9019 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
9020 i64 lastRowid; /* ROWID of most recent insert (see above) */
9021 i64 priorNewRowid; /* Last randomly generated ROWID */
9022 int magic; /* Magic number for detect library misuse */
9023 int nChange; /* Value returned by sqlite3_changes() */
9024 int nTotalChange; /* Value returned by sqlite3_total_changes() */
9025 sqlite3_mutex *mutex; /* Connection mutex */
9026 int aLimit[SQLITE_N_LIMIT]; /* Limits */
9027 struct sqlite3InitInfo { /* Information used during initialization */
@@ -9180,17 +9344,17 @@
9180 ** available as the function user-data (sqlite3_user_data()). The
9181 ** FuncDef.flags variable is set to the value passed as the flags
9182 ** parameter.
9183 */
9184 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
9185 {nArg, SQLITE_UTF8, bNC*8, SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName}
9186 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
9187 {nArg, SQLITE_UTF8, bNC*8, pArg, 0, xFunc, 0, 0, #zName}
9188 #define LIKEFUNC(zName, nArg, arg, flags) \
9189 {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName}
9190 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
9191 {nArg, SQLITE_UTF8, nc*8, SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal, #zName}
9192
9193
9194 /*
9195 ** Each SQLite module (virtual table definition) is defined by an
9196 ** instance of the following structure, stored in the sqlite3.aModule
@@ -9755,10 +9919,15 @@
9755 ** tables in a join to 32 instead of 64. But it also reduces the size
9756 ** of the library by 738 bytes on ix86.
9757 */
9758 typedef u64 Bitmask;
9759
 
 
 
 
 
9760 /*
9761 ** The following structure describes the FROM clause of a SELECT statement.
9762 ** Each table or subquery in the FROM clause is a separate element of
9763 ** the SrcList.a[] array.
9764 **
@@ -10290,10 +10459,11 @@
10290 int mxStrlen; /* Maximum string length */
10291 int szLookaside; /* Default lookaside buffer size */
10292 int nLookaside; /* Default lookaside buffer count */
10293 sqlite3_mem_methods m; /* Low-level memory allocation interface */
10294 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
 
10295 void *pHeap; /* Heap storage space */
10296 int nHeap; /* Size of pHeap[] */
10297 int mnReq, mxReq; /* Min and max heap requests sizes */
10298 void *pScratch; /* Scratch memory */
10299 int szScratch; /* Size of each scratch buffer */
@@ -10304,11 +10474,10 @@
10304 int isInit; /* True after initialization has finished */
10305 int inProgress; /* True while initialization in progress */
10306 int isMallocInit; /* True after malloc is initialized */
10307 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
10308 int nRefInitMutex; /* Number of users of pInitMutex */
10309 int nSmall; /* alloc size threshold used by mem6.c */
10310 int mxParserStack; /* maximum depth of the parser stack */
10311 int sharedCacheEnabled; /* true if shared-cache mode enabled */
10312 };
10313
10314 /*
@@ -10387,14 +10556,20 @@
10387 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
10388 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
10389 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
10390 SQLITE_PRIVATE void sqlite3PageFree(void*);
10391 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
10392 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
10393 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
10394 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
10395 SQLITE_PRIVATE int sqlite3MemoryAlarm(void (*)(void*, sqlite3_int64, int), void*, sqlite3_int64);
 
 
 
 
 
 
 
 
10396
10397 #ifndef SQLITE_MUTEX_OMIT
10398 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void);
10399 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
10400 SQLITE_PRIVATE int sqlite3MutexInit(void);
@@ -10477,10 +10652,11 @@
10477 SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
10478 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
10479 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
10480 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
10481 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
 
10482 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
10483 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
10484 Token*, Select*, Expr*, IdList*);
10485 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
10486 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
@@ -10549,11 +10725,11 @@
10549 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int);
10550 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
10551 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
10552 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
10553 int*,int,int,int,int);
10554 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*,int,int,int,int);
10555 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
10556 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
10557 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*);
10558 SQLITE_PRIVATE void sqlite3TokenCopy(sqlite3*,Token*, Token*);
10559 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*);
@@ -10563,11 +10739,10 @@
10563 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
10564 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
10565 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
10566 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
10567 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
10568 SQLITE_PRIVATE int sqlite3GetBuiltinFunction(const char *, int, FuncDef **);
10569 #ifdef SQLITE_DEBUG
10570 SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*);
10571 SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3*);
10572 #else
10573 # define sqlite3SafetyOn(A) 0
@@ -10585,11 +10760,11 @@
10585 SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
10586 Expr*,int, int);
10587 SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
10588 SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
10589 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
10590 SQLITE_PRIVATE int sqlite3TriggersExist(Parse*, Table*, int, ExprList*);
10591 SQLITE_PRIVATE int sqlite3CodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int,
10592 int, int, u32*, u32*);
10593 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
10594 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
10595 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
@@ -10598,11 +10773,11 @@
10598 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, int);
10599 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
10600 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
10601 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
10602 #else
10603 # define sqlite3TriggersExist(A,B,C,D,E,F) 0
10604 # define sqlite3DeleteTrigger(A,B)
10605 # define sqlite3DropTriggerPtr(A,B)
10606 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
10607 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I,J,K) 0
10608 #endif
@@ -10666,12 +10841,12 @@
10666 **
10667 ** x = getVarint32( A, B );
10668 ** x = putVarint32( A, B );
10669 **
10670 */
10671 #define getVarint32(A,B) ((*(A)<(unsigned char)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), &(B)))
10672 #define putVarint32(A,B) (((B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
10673 #define getVarint sqlite3GetVarint
10674 #define putVarint sqlite3PutVarint
10675
10676
10677 SQLITE_PRIVATE void sqlite3IndexAffinityStr(Vdbe *, Index *);
@@ -10778,15 +10953,17 @@
10778 #ifdef SQLITE_OMIT_VIRTUALTABLE
10779 # define sqlite3VtabClear(X)
10780 # define sqlite3VtabSync(X,Y) SQLITE_OK
10781 # define sqlite3VtabRollback(X)
10782 # define sqlite3VtabCommit(X)
 
10783 #else
10784 SQLITE_PRIVATE void sqlite3VtabClear(Table*);
10785 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
10786 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
10787 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
 
10788 #endif
10789 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
10790 SQLITE_PRIVATE void sqlite3VtabLock(sqlite3_vtab*);
10791 SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3*, sqlite3_vtab*);
10792 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
@@ -11107,11 +11284,11 @@
11107 **
11108 ** There is only one exported symbol in this file - the function
11109 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
11110 ** All other code has file scope.
11111 **
11112 ** $Id: date.c,v 1.92 2008/10/13 15:35:09 drh Exp $
11113 **
11114 ** SQLite processes all times and dates as Julian Day numbers. The
11115 ** dates and times are stored as the number of days since noon
11116 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
11117 ** calendar system.
@@ -11609,11 +11786,11 @@
11609 int rc = 1;
11610 int n;
11611 double r;
11612 char *z, zBuf[30];
11613 z = zBuf;
11614 for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){
11615 z[n] = tolower(zMod[n]);
11616 }
11617 z[n] = 0;
11618 switch( z[0] ){
11619 #ifndef SQLITE_OMIT_LOCALTIME
@@ -11977,11 +12154,11 @@
11977 i++;
11978 }
11979 }
11980 if( n<sizeof(zBuf) ){
11981 z = zBuf;
11982 }else if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
11983 sqlite3_result_error_toobig(context);
11984 return;
11985 }else{
11986 z = sqlite3DbMallocRaw(db, n);
11987 if( z==0 ){
@@ -12056,13 +12233,14 @@
12056 **
12057 ** This function returns the same value as time('now').
12058 */
12059 static void ctimeFunc(
12060 sqlite3_context *context,
12061 int argc,
12062 sqlite3_value **argv
12063 ){
 
12064 timeFunc(context, 0, 0);
12065 }
12066
12067 /*
12068 ** current_date()
@@ -12069,13 +12247,14 @@
12069 **
12070 ** This function returns the same value as date('now').
12071 */
12072 static void cdateFunc(
12073 sqlite3_context *context,
12074 int argc,
12075 sqlite3_value **argv
12076 ){
 
12077 dateFunc(context, 0, 0);
12078 }
12079
12080 /*
12081 ** current_timestamp()
@@ -12082,13 +12261,14 @@
12082 **
12083 ** This function returns the same value as datetime('now').
12084 */
12085 static void ctimestampFunc(
12086 sqlite3_context *context,
12087 int argc,
12088 sqlite3_value **argv
12089 ){
 
12090 datetimeFunc(context, 0, 0);
12091 }
12092 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
12093
12094 #ifdef SQLITE_OMIT_DATETIME_FUNCS
@@ -12623,11 +12803,11 @@
12623 ** to obtain the memory it needs.
12624 **
12625 ** This file contains implementations of the low-level memory allocation
12626 ** routines specified in the sqlite3_mem_methods object.
12627 **
12628 ** $Id: mem1.c,v 1.27 2008/10/28 18:58:20 drh Exp $
12629 */
12630
12631 /*
12632 ** This version of the memory allocator is the default. It is
12633 ** used when no other memory allocator is specified using compile-time
@@ -12715,17 +12895,19 @@
12715
12716 /*
12717 ** Initialize this module.
12718 */
12719 static int sqlite3MemInit(void *NotUsed){
 
12720 return SQLITE_OK;
12721 }
12722
12723 /*
12724 ** Deinitialize this module.
12725 */
12726 static void sqlite3MemShutdown(void *NotUsed){
 
12727 return;
12728 }
12729
12730 /*
12731 ** This routine is the only routine in this file with external linkage.
@@ -13213,11 +13395,11 @@
13213 ** be changed.
13214 **
13215 ** This version of the memory allocation subsystem is included
13216 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
13217 **
13218 ** $Id: mem3.c,v 1.23 2008/09/02 17:52:52 danielk1977 Exp $
13219 */
13220
13221 /*
13222 ** This version of the memory allocator is only built into the library
13223 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
@@ -13440,11 +13622,11 @@
13440 /*
13441 ** Chunk i is a free chunk that has been unlinked. Adjust its
13442 ** size parameters for check-out and return a pointer to the
13443 ** user portion of the chunk.
13444 */
13445 static void *memsys3Checkout(u32 i, int nBlock){
13446 u32 x;
13447 assert( sqlite3_mutex_held(mem3.mutex) );
13448 assert( i>=1 );
13449 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
13450 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
@@ -13458,11 +13640,11 @@
13458 /*
13459 ** Carve a piece off of the end of the mem3.iMaster free chunk.
13460 ** Return a pointer to the new allocation. Or, if the master chunk
13461 ** is not large enough, return 0.
13462 */
13463 static void *memsys3FromMaster(int nBlock){
13464 assert( sqlite3_mutex_held(mem3.mutex) );
13465 assert( mem3.szMaster>=nBlock );
13466 if( nBlock>=mem3.szMaster-1 ){
13467 /* Use the entire master */
13468 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
@@ -13544,12 +13726,12 @@
13544 ** This function assumes that the necessary mutexes, if any, are
13545 ** already held by the caller. Hence "Unsafe".
13546 */
13547 static void *memsys3MallocUnsafe(int nByte){
13548 u32 i;
13549 int nBlock;
13550 int toFree;
13551
13552 assert( sqlite3_mutex_held(mem3.mutex) );
13553 assert( sizeof(Mem3Block)==8 );
13554 if( nByte<=12 ){
13555 nBlock = 2;
@@ -13741,10 +13923,11 @@
13741
13742 /*
13743 ** Initialize this module.
13744 */
13745 static int memsys3Init(void *NotUsed){
 
13746 if( !sqlite3GlobalConfig.pHeap ){
13747 return SQLITE_ERROR;
13748 }
13749
13750 /* Store a pointer to the memory block in global structure mem3. */
@@ -13765,10 +13948,11 @@
13765
13766 /*
13767 ** Deinitialize this module.
13768 */
13769 static void memsys3Shutdown(void *NotUsed){
 
13770 return;
13771 }
13772
13773
13774
@@ -13777,11 +13961,11 @@
13777 ** allocations into that log.
13778 */
13779 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
13780 #ifdef SQLITE_DEBUG
13781 FILE *out;
13782 int i, j;
13783 u32 size;
13784 if( zFilename==0 || zFilename[0]==0 ){
13785 out = stdout;
13786 }else{
13787 out = fopen(zFilename, "w");
@@ -13842,10 +14026,12 @@
13842 if( out==stdout ){
13843 fflush(stdout);
13844 }else{
13845 fclose(out);
13846 }
 
 
13847 #endif
13848 }
13849
13850 /*
13851 ** This routine is the only routine in this file with external
@@ -13899,11 +14085,11 @@
13899 ** be changed.
13900 **
13901 ** This version of the memory allocation subsystem is included
13902 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
13903 **
13904 ** $Id: mem5.c,v 1.15 2008/10/28 18:58:20 drh Exp $
13905 */
13906
13907 /*
13908 ** This version of the memory allocator is used only when
13909 ** SQLITE_ENABLE_MEMSYS5 is defined.
@@ -14083,16 +14269,15 @@
14083 int iFullSz; /* Size of allocation rounded up to power of 2 */
14084 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
14085
14086 /* Keep track of the maximum allocation request. Even unfulfilled
14087 ** requests are counted */
14088 if( nByte>mem5.maxRequest ){
14089 mem5.maxRequest = nByte;
14090 }
14091
14092 /* Round nByte up to the next valid power of two */
14093 if( nByte>POW2_MAX ) return 0;
14094 for(iFullSz=mem5.nAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
14095
14096 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
14097 ** block. If not, then split a block of the next larger power of
14098 ** two in order to create a new free block of size iLogsize.
@@ -14140,16 +14325,16 @@
14140 assert( ((u8 *)pOld-mem5.zPool)%mem5.nAtom==0 );
14141 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
14142
14143 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
14144 size = 1<<iLogsize;
14145 assert( iBlock+size-1<mem5.nBlock );
14146
14147 mem5.aCtrl[iBlock] |= CTRL_FREE;
14148 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
14149 assert( mem5.currentCount>0 );
14150 assert( mem5.currentOut>=0 );
14151 mem5.currentCount--;
14152 mem5.currentOut -= size*mem5.nAtom;
14153 assert( mem5.currentOut>0 || mem5.currentCount==0 );
14154 assert( mem5.currentCount>0 || mem5.currentOut==0 );
14155
@@ -14255,17 +14440,19 @@
14255 int nByte = sqlite3GlobalConfig.nHeap;
14256 u8 *zByte = (u8 *)sqlite3GlobalConfig.pHeap;
14257 int nMinLog; /* Log of minimum allocation size in bytes*/
14258 int iOffset;
14259
 
 
14260 if( !zByte ){
14261 return SQLITE_ERROR;
14262 }
14263
14264 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
14265 mem5.nAtom = (1<<nMinLog);
14266 while( sizeof(Mem5Link)>mem5.nAtom ){
14267 mem5.nAtom = mem5.nAtom << 1;
14268 }
14269
14270 mem5.nBlock = (nByte / (mem5.nAtom+sizeof(u8)));
14271 mem5.zPool = zByte;
@@ -14291,10 +14478,11 @@
14291
14292 /*
14293 ** Deinitialize this module.
14294 */
14295 static void memsys5Shutdown(void *NotUsed){
 
14296 return;
14297 }
14298
14299 /*
14300 ** Open the file indicated and write a log of all unfreed memory
@@ -14334,10 +14522,12 @@
14334 if( out==stdout ){
14335 fflush(stdout);
14336 }else{
14337 fclose(out);
14338 }
 
 
14339 #endif
14340 }
14341
14342 /*
14343 ** This routine is the only routine in this file with external
@@ -14985,11 +15175,11 @@
14985 ** May you share freely, never taking more than you give.
14986 **
14987 *************************************************************************
14988 ** This file contains the C functions that implement mutexes for pthreads
14989 **
14990 ** $Id: mutex_unix.c,v 1.13 2008/07/16 12:33:24 drh Exp $
14991 */
14992
14993 /*
14994 ** The code in this file is only used if we are compiling threadsafe
14995 ** under unix with pthreads.
@@ -15034,11 +15224,11 @@
15034 ** On those platforms where pthread_equal() is not atomic, SQLite
15035 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
15036 ** make sure no assert() statements are evaluated and hence these
15037 ** routines are never called.
15038 */
15039 #ifndef NDEBUG
15040 static int pthreadMutexHeld(sqlite3_mutex *p){
15041 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
15042 }
15043 static int pthreadMutexNotheld(sqlite3_mutex *p){
15044 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
@@ -15130,11 +15320,11 @@
15130 }
15131 break;
15132 }
15133 default: {
15134 assert( iType-2 >= 0 );
15135 assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
15136 p = &staticMutexes[iType-2];
15137 p->id = iType;
15138 break;
15139 }
15140 }
@@ -15312,11 +15502,11 @@
15312 ** May you share freely, never taking more than you give.
15313 **
15314 *************************************************************************
15315 ** This file contains the C functions that implement mutexes for win32
15316 **
15317 ** $Id: mutex_w32.c,v 1.11 2008/06/26 10:41:19 danielk1977 Exp $
15318 */
15319
15320 /*
15321 ** The code in this file is only used if we are compiling multithreaded
15322 ** on a win32 system.
@@ -15341,11 +15531,18 @@
15341 ** the LockFileEx() API. But we can still statically link against that
15342 ** API as long as we don't call it win running Win95/98/ME. A call to
15343 ** this routine is used to determine if the host is Win95/98/ME or
15344 ** WinNT/2K/XP so that we will know whether or not we can safely call
15345 ** the LockFileEx() API.
 
 
 
 
 
 
15346 */
 
15347 #if SQLITE_OS_WINCE
15348 # define mutexIsNT() (1)
15349 #else
15350 static int mutexIsNT(void){
15351 static int osType = 0;
@@ -15356,11 +15553,11 @@
15356 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
15357 }
15358 return osType==2;
15359 }
15360 #endif /* SQLITE_OS_WINCE */
15361
15362
15363 #ifdef SQLITE_DEBUG
15364 /*
15365 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
15366 ** intended for use only inside assert() statements.
@@ -15559,23 +15756,24 @@
15559 **
15560 *************************************************************************
15561 **
15562 ** Memory allocation functions used throughout sqlite.
15563 **
15564 ** $Id: malloc.c,v 1.45 2008/10/12 00:27:53 shane Exp $
15565 */
15566
15567 /*
15568 ** This routine runs when the memory allocator sees that the
15569 ** total memory allocation is about to exceed the soft heap
15570 ** limit.
15571 */
15572 static void softHeapLimitEnforcer(
15573 void *NotUsed,
15574 sqlite3_int64 inUse,
15575 int allocSize
15576 ){
 
15577 sqlite3_release_memory(allocSize);
15578 }
15579
15580 /*
15581 ** Set the soft heap-size limit for the library. Passing a zero or
@@ -15613,10 +15811,11 @@
15613 nRet += sqlite3VdbeReleaseMemory(n);
15614 #endif
15615 nRet += sqlite3PcacheReleaseMemory(n-nRet);
15616 return nRet;
15617 #else
 
15618 return SQLITE_OK;
15619 #endif
15620 }
15621
15622 /*
@@ -15646,11 +15845,11 @@
15646 ** sqlite3GlobalConfig.pPage to a block of memory that records
15647 ** which pages are available.
15648 */
15649 u32 *aScratchFree;
15650 u32 *aPageFree;
15651 } mem0 = { 62560955 };
15652
15653 #define mem0 GLOBAL(struct Mem0Global, mem0)
15654
15655 /*
15656 ** Initialize the memory allocation subsystem.
@@ -15931,11 +16130,11 @@
15931 int i;
15932 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
15933 i /= sqlite3GlobalConfig.szScratch;
15934 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
15935 sqlite3_mutex_enter(mem0.mutex);
15936 assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch );
15937 mem0.aScratchFree[mem0.nScratchFree++] = i;
15938 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
15939 sqlite3_mutex_leave(mem0.mutex);
15940 }
15941 }
@@ -16339,11 +16538,11 @@
16339 ** the public domain. The original comments are included here for
16340 ** completeness. They are very out-of-date but might be useful as
16341 ** an historical reference. Most of the "enhancements" have been backed
16342 ** out so that the functionality is now the same as standard printf().
16343 **
16344 ** $Id: printf.c,v 1.94 2008/08/22 14:08:36 drh Exp $
16345 **
16346 **************************************************************************
16347 **
16348 ** The following modules is an enhanced replacement for the "printf" subroutines
16349 ** found in the standard C library. The following enhancements are
@@ -16470,11 +16669,10 @@
16470 { 'p', 16, 0, etPOINTER, 0, 1 },
16471 { 'T', 0, 2, etTOKEN, 0, 0 },
16472 { 'S', 0, 2, etSRCLIST, 0, 0 },
16473 { 'r', 10, 3, etORDINAL, 0, 0 },
16474 };
16475 #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
16476
16477 /*
16478 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
16479 ** conversions will work.
16480 */
@@ -16507,11 +16705,11 @@
16507 /*
16508 ** Append N space characters to the given string buffer.
16509 */
16510 static void appendSpace(StrAccum *pAccum, int N){
16511 static const char zSpaces[] = " ";
16512 while( N>=sizeof(zSpaces)-1 ){
16513 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
16514 N -= sizeof(zSpaces)-1;
16515 }
16516 if( N>0 ){
16517 sqlite3StrAccumAppend(pAccum, zSpaces, N);
@@ -16670,11 +16868,11 @@
16670 }else{
16671 flag_long = flag_longlong = 0;
16672 }
16673 /* Fetch the info entry for the field */
16674 infop = 0;
16675 for(idx=0; idx<etNINFO; idx++){
16676 if( c==fmtinfo[idx].fmttype ){
16677 infop = &fmtinfo[idx];
16678 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
16679 xtype = infop->type;
16680 }else{
@@ -16979,11 +17177,14 @@
16979 }
16980 needQuote = !isnull && xtype==etSQLESCAPE2;
16981 n += i + 1 + needQuote*2;
16982 if( n>etBUFSIZE ){
16983 bufpt = zExtra = sqlite3Malloc( n );
16984 if( bufpt==0 ) return;
 
 
 
16985 }else{
16986 bufpt = buf;
16987 }
16988 j = 0;
16989 if( needQuote ) bufpt[j++] = q;
@@ -17471,11 +17672,11 @@
17471 ** VDBE. This information used to all be at the top of the single
17472 ** source code file "vdbe.c". When that file became too big (over
17473 ** 6000 lines long) it was split up into several smaller files and
17474 ** this header information was factored out.
17475 **
17476 ** $Id: vdbeInt.h,v 1.157 2008/11/05 16:37:35 drh Exp $
17477 */
17478 #ifndef _VDBEINT_H_
17479 #define _VDBEINT_H_
17480
17481 /*
@@ -17752,11 +17953,11 @@
17752 VdbeCursor **apCsr; /* One element of this array for each open cursor */
17753 int nVar; /* Number of entries in aVar[] */
17754 Mem *aVar; /* Values for the OP_Variable opcode. */
17755 char **azVar; /* Name of variables */
17756 int okVar; /* True if azVar[] has been initialized */
17757 int magic; /* Magic number for sanity checking */
17758 int nMem; /* Number of memory locations currently allocated */
17759 Mem *aMem; /* The memory locations */
17760 int nCallback; /* Number of callbacks invoked so far */
17761 int cacheCtr; /* VdbeCursor row cache generation counter */
17762 Fifo sFifo; /* A list of ROWIDs */
@@ -18392,11 +18593,11 @@
18392 ** Utility functions used throughout sqlite.
18393 **
18394 ** This file contains functions for allocating memory, comparing
18395 ** strings, and stuff like that.
18396 **
18397 ** $Id: util.c,v 1.241 2008/07/28 19:34:54 drh Exp $
18398 */
18399
18400
18401 /*
18402 ** Return true if the floating point value is Not a Number (NaN).
@@ -18432,11 +18633,11 @@
18432 ** to exceed the SQLITE_LIMIT_LENGTH setting.
18433 */
18434 SQLITE_PRIVATE int sqlite3Strlen(sqlite3 *db, const char *z){
18435 const char *z2 = z;
18436 int len;
18437 size_t x;
18438 while( *z2 ){ z2++; }
18439 x = z2 - z;
18440 len = 0x7fffffff & x;
18441 if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){
18442 return db->aLimit[SQLITE_LIMIT_LENGTH];
@@ -19309,19 +19510,19 @@
19309 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
19310 ** open properly and is not fit for general use but which can be
19311 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
19312 */
19313 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
19314 int magic;
19315 if( db==0 ) return 0;
19316 magic = db->magic;
19317 if( magic!=SQLITE_MAGIC_OPEN &&
19318 magic!=SQLITE_MAGIC_BUSY ) return 0;
19319 return 1;
19320 }
19321 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
19322 int magic;
19323 if( db==0 ) return 0;
19324 magic = db->magic;
19325 if( magic!=SQLITE_MAGIC_SICK &&
19326 magic!=SQLITE_MAGIC_OPEN &&
19327 magic!=SQLITE_MAGIC_BUSY ) return 0;
@@ -19801,11 +20002,11 @@
19801 **
19802 ******************************************************************************
19803 **
19804 ** This file contains code that is specific to OS/2.
19805 **
19806 ** $Id: os_os2.c,v 1.58 2008/11/07 00:06:18 drh Exp $
19807 */
19808
19809
19810 #if SQLITE_OS_OS2
19811
@@ -20216,11 +20417,18 @@
20216 if( flags & SQLITE_SYNC_FULL){
20217 sqlite3_fullsync_count++;
20218 }
20219 sqlite3_sync_count++;
20220 #endif
 
 
 
 
 
 
20221 return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
 
20222 }
20223
20224 /*
20225 ** Determine the current size of a file in bytes
20226 */
@@ -21149,11 +21357,11 @@
21149 **
21150 ******************************************************************************
21151 **
21152 ** This file contains code that is specific to Unix systems.
21153 **
21154 ** $Id: os_unix.c,v 1.208 2008/11/07 00:06:18 drh Exp $
21155 */
21156 #if SQLITE_OS_UNIX /* This file is used on unix only */
21157
21158 /*
21159 ** If SQLITE_ENABLE_LOCKING_STYLE is defined and is non-zero, then several
@@ -21161,11 +21369,12 @@
21161 **
21162 ** * POSIX locking (the default),
21163 ** * No locking,
21164 ** * Dot-file locking,
21165 ** * flock() locking,
21166 ** * AFP locking (OSX only).
 
21167 **
21168 ** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by
21169 ** default on a Mac and disabled on all other posix platforms.
21170 */
21171 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
@@ -21174,10 +21383,20 @@
21174 # else
21175 # define SQLITE_ENABLE_LOCKING_STYLE 0
21176 # endif
21177 #endif
21178
 
 
 
 
 
 
 
 
 
 
21179 /*
21180 ** These #defines should enable >2GB file support on Posix if the
21181 ** underlying operating system supports it. If the OS lacks
21182 ** large file support, these should be no-ops.
21183 **
@@ -21206,13 +21425,19 @@
21206 #include <unistd.h>
21207 #include <sys/time.h>
21208 #include <errno.h>
21209
21210 #if SQLITE_ENABLE_LOCKING_STYLE
21211 #include <sys/ioctl.h>
21212 #include <sys/param.h>
21213 #include <sys/mount.h>
 
 
 
 
 
 
21214 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
21215
21216 /*
21217 ** If we are to be thread-safe, include the pthreads header and define
21218 ** the SQLITE_UNIX_THREADS macro.
@@ -21257,10 +21482,14 @@
21257 int dirfd; /* File descriptor for the directory */
21258 #if SQLITE_THREADSAFE
21259 pthread_t tid; /* The thread that "owns" this unixFile */
21260 #endif
21261 int lastErrno; /* The unix errno from the last I/O error */
 
 
 
 
21262 };
21263
21264 /*
21265 ** Include code that is common to all os_*.c files
21266 */
@@ -21667,11 +21896,15 @@
21667 ** each others locks then tid is always set to zero. tid is omitted
21668 ** if we compile without threading support.
21669 */
21670 struct lockKey {
21671 dev_t dev; /* Device number */
 
 
 
21672 ino_t ino; /* Inode number */
 
21673 #if SQLITE_THREADSAFE
21674 pthread_t tid; /* Thread ID or zero if threads can override each other */
21675 #endif
21676 };
21677
@@ -21697,11 +21930,15 @@
21697 ** to locate a particular openCnt structure given its inode. This
21698 ** is the same as the lockKey except that the thread ID is omitted.
21699 */
21700 struct openKey {
21701 dev_t dev; /* Device number */
 
 
 
21702 ino_t ino; /* Inode number */
 
21703 };
21704
21705 /*
21706 ** An instance of the following structure is allocated for each open
21707 ** inode. This structure keeps track of the number of locks on that
@@ -21713,10 +21950,14 @@
21713 struct openKey key; /* The lookup key */
21714 int nRef; /* Number of pointers to this structure */
21715 int nLock; /* Number of outstanding locks */
21716 int nPending; /* Number of pending close() operations */
21717 int *aPending; /* Malloced space holding fd's awaiting a close() */
 
 
 
 
21718 struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
21719 };
21720
21721 /*
21722 ** List of all lockInfo and openCnt objects. This used to be a hash
@@ -21724,10 +21965,22 @@
21724 ** never exceeds a few thousand. And lookup is not on a critical
21725 ** path oo a simple linked list will suffice.
21726 */
21727 static struct lockInfo *lockList = 0;
21728 static struct openCnt *openList = 0;
 
 
 
 
 
 
 
 
 
 
 
 
21729
21730 /*
21731 ** The locking styles are associated with the different file locking
21732 ** capabilities supported by different file systems.
21733 **
@@ -21737,18 +21990,21 @@
21737 ** DOTLOCK isn't a true locking style, it refers to the use of a special
21738 ** file named the same as the database file with a '.lock' extension, this
21739 ** can be used on file systems that do not offer any reliable file locking
21740 ** NO locking means that no locking will be attempted, this is only used for
21741 ** read-only file systems currently
 
 
21742 ** UNSUPPORTED means that no locking will be attempted, this is only used for
21743 ** file systems that are known to be unsupported
21744 */
21745 #define LOCKING_STYLE_POSIX 1
21746 #define LOCKING_STYLE_NONE 2
21747 #define LOCKING_STYLE_DOTFILE 3
21748 #define LOCKING_STYLE_FLOCK 4
21749 #define LOCKING_STYLE_AFP 5
 
21750
21751 /*
21752 ** Only set the lastErrno if the error code is a real error and not
21753 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
21754 */
@@ -21859,20 +22115,23 @@
21859 return s;
21860 }
21861 #define fcntl lockTrace
21862 #endif /* SQLITE_LOCK_TRACE */
21863
 
21864 /*
21865 ** The testThreadLockingBehavior() routine launches two separate
21866 ** threads on this routine. This routine attempts to lock a file
21867 ** descriptor then returns. The success or failure of that attempt
21868 ** allows the testThreadLockingBehavior() procedure to determine
21869 ** whether or not threads can override each others locks.
21870 */
 
 
21871 static void *threadLockingTest(void *pArg){
21872 struct threadTestData *pData = (struct threadTestData*)pArg;
21873 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
21874 return pArg;
21875 }
21876
21877 /*
21878 ** This procedure attempts to determine whether or not threads
@@ -21879,30 +22138,43 @@
21879 ** can override each others locks then sets the
21880 ** threadsOverrideEachOthersLocks variable appropriately.
21881 */
21882 static void testThreadLockingBehavior(int fd_orig){
21883 int fd;
21884 struct threadTestData d[2];
21885 pthread_t t[2];
 
 
21886
21887 fd = dup(fd_orig);
21888 if( fd<0 ) return;
21889 memset(d, 0, sizeof(d));
21890 d[0].fd = fd;
21891 d[0].lock.l_type = F_RDLCK;
21892 d[0].lock.l_len = 1;
21893 d[0].lock.l_start = 0;
21894 d[0].lock.l_whence = SEEK_SET;
21895 d[1] = d[0];
21896 d[1].lock.l_type = F_WRLCK;
21897 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
21898 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
21899 pthread_join(t[0], 0);
21900 pthread_join(t[1], 0);
 
21901 close(fd);
21902 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
 
21903 }
 
 
 
 
 
 
 
 
 
21904 #endif /* SQLITE_THREADSAFE */
21905
21906 /*
21907 ** Release a lockInfo structure previously allocated by findLockInfo().
21908 */
@@ -21947,15 +22219,108 @@
21947 sqlite3_free(pOpen->aPending);
21948 sqlite3_free(pOpen);
21949 }
21950 }
21951 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21952
21953 #if SQLITE_ENABLE_LOCKING_STYLE
21954 /*
21955 ** Tests a byte-range locking query to see if byte range locks are
21956 ** supported, if not we fall back to dotlockLockingStyle.
 
21957 */
21958 static int testLockingStyle(int fd){
21959 struct flock lockInfo;
21960
21961 /* Test byte-range lock using fcntl(). If the call succeeds,
@@ -21968,13 +22333,14 @@
21968 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
21969 return LOCKING_STYLE_POSIX;
21970 }
21971
21972 /* Testing for flock() can give false positives. So if if the above
21973 ** test fails, then we fall back to using dot-file style locking.
21974 */
21975 return LOCKING_STYLE_DOTFILE;
 
21976 }
21977 #endif
21978
21979 /*
21980 ** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
@@ -21985,16 +22351,27 @@
21985 ** other systems.
21986 **
21987 ** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
21988 ** returns LOCKING_STYLE_POSIX.
21989 */
 
21990 static int detectLockingStyle(
21991 sqlite3_vfs *pVfs,
21992 const char *filePath,
21993 int fd
21994 ){
21995 #if SQLITE_ENABLE_LOCKING_STYLE
 
 
 
 
 
 
 
 
 
 
21996 struct Mapping {
21997 const char *zFilesystem;
21998 int eLockingStyle;
21999 } aMap[] = {
22000 { "hfs", LOCKING_STYLE_POSIX },
@@ -22030,13 +22407,16 @@
22030 }
22031 }
22032
22033 /* Default case. Handles, amongst others, "nfs". */
22034 return testLockingStyle(fd);
22035 #endif
22036 return LOCKING_STYLE_POSIX;
22037 }
 
 
 
22038
22039 /*
22040 ** Given a file descriptor, locate lockInfo and openCnt structures that
22041 ** describes that file descriptor. Create new ones if necessary. The
22042 ** return values might be uninitialized if an error occurs.
@@ -22043,10 +22423,13 @@
22043 **
22044 ** Return an appropriate error code.
22045 */
22046 static int findLockInfo(
22047 int fd, /* The file descriptor used in the key */
 
 
 
22048 struct lockInfo **ppLock, /* Return the lockInfo structure here */
22049 struct openCnt **ppOpen /* Return the openCnt structure here */
22050 ){
22051 int rc;
22052 struct lockKey key1;
@@ -22080,20 +22463,28 @@
22080 }
22081 }
22082
22083 memset(&key1, 0, sizeof(key1));
22084 key1.dev = statbuf.st_dev;
 
 
 
22085 key1.ino = statbuf.st_ino;
 
22086 #if SQLITE_THREADSAFE
22087 if( threadsOverrideEachOthersLocks<0 ){
22088 testThreadLockingBehavior(fd);
22089 }
22090 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
22091 #endif
22092 memset(&key2, 0, sizeof(key2));
22093 key2.dev = statbuf.st_dev;
 
 
 
22094 key2.ino = statbuf.st_ino;
 
22095 pLock = lockList;
22096 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
22097 pLock = pLock->pNext;
22098 }
22099 if( pLock==0 ){
@@ -22133,10 +22524,14 @@
22133 pOpen->aPending = 0;
22134 pOpen->pNext = openList;
22135 pOpen->pPrev = 0;
22136 if( openList ) openList->pPrev = pOpen;
22137 openList = pOpen;
 
 
 
 
22138 }else{
22139 pOpen->nRef++;
22140 }
22141 *ppOpen = pOpen;
22142 }
@@ -22197,11 +22592,15 @@
22197 OSTRACE4("Transfer ownership of %d from %d to %d\n",
22198 pFile->h, pFile->tid, hSelf);
22199 pFile->tid = hSelf;
22200 if (pFile->pLock != NULL) {
22201 releaseLockInfo(pFile->pLock);
 
 
 
22202 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
 
22203 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
22204 locktypeName(pFile->locktype),
22205 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
22206 return rc;
22207 } else {
@@ -22366,10 +22765,24 @@
22366 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
22367 ** or power failure will likely corrupt the database file.
22368 */
22369 static int full_fsync(int fd, int fullSync, int dataOnly){
22370 int rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22371
22372 /* Record the number of times that we do a normal fsync() and
22373 ** FULLSYNC. This is used during testing to verify that this procedure
22374 ** gets called with the correct arguments.
22375 */
@@ -22381,13 +22794,11 @@
22381 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
22382 ** no-op
22383 */
22384 #ifdef SQLITE_NO_SYNC
22385 rc = SQLITE_OK;
22386 #else
22387
22388 #if HAVE_FULLFSYNC
22389 if( fullSync ){
22390 rc = fcntl(fd, F_FULLFSYNC, 0);
22391 }else{
22392 rc = 1;
22393 }
@@ -22402,16 +22813,21 @@
22402 if( rc ) rc = fsync(fd);
22403
22404 #else
22405 if( dataOnly ){
22406 rc = fdatasync(fd);
 
 
 
22407 }else{
22408 rc = fsync(fd);
22409 }
22410 #endif /* HAVE_FULLFSYNC */
22411 #endif /* defined(SQLITE_NO_SYNC) */
22412
 
 
 
22413 return rc;
22414 }
22415
22416 /*
22417 ** Make sure all writes to a particular file are committed to disk.
@@ -22996,10 +23412,29 @@
22996 close(pFile->dirfd);
22997 }
22998 if( pFile->h>=0 ){
22999 close(pFile->h);
23000 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23001 OSTRACE2("CLOSE %-3d\n", pFile->h);
23002 OpenCounter(-1);
23003 memset(pFile, 0, sizeof(unixFile));
23004 }
23005 return SQLITE_OK;
@@ -23039,10 +23474,12 @@
23039 return SQLITE_OK;
23040 }
23041
23042
23043 #if SQLITE_ENABLE_LOCKING_STYLE
 
 
23044 #pragma mark AFP Support
23045
23046 /*
23047 ** The afpLockingContext structure contains all afp lock specific state
23048 */
@@ -23485,10 +23922,12 @@
23485 if( id ){
23486 flockUnlock(id, NO_LOCK);
23487 }
23488 return closeUnixFile(id);
23489 }
 
 
23490
23491 #pragma mark Old-School .lock file based locking
23492
23493 /* Dotlock-style reserved lock checking following the behavior of
23494 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
@@ -23514,11 +23953,11 @@
23514 if( lstat(zLockFile, &statBuf)==0 ){
23515 /* file exists, someone else has the lock */
23516 reserved = 1;
23517 }else{
23518 /* file does not exist, we could have it if we want it */
23519 int tErrno = errno;
23520 if( ENOENT != tErrno ){
23521 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
23522 pFile->lastErrno = tErrno;
23523 }
23524 }
@@ -23537,13 +23976,14 @@
23537
23538 /* if we already have a lock, it is exclusive.
23539 ** Just adjust level and punt on outta here. */
23540 if (pFile->locktype > NO_LOCK) {
23541 pFile->locktype = locktype;
23542
23543 /* Always update the timestamp on the old file */
23544 utimes(zLockFile, NULL);
 
23545 rc = SQLITE_OK;
23546 goto dotlock_end_lock;
23547 }
23548
23549 /* check to see if lock file already exists */
@@ -23614,44 +24054,179 @@
23614
23615 /*
23616 ** Close a file.
23617 */
23618 static int dotlockClose(sqlite3_file *id) {
 
23619 if( id ){
23620 unixFile *pFile = (unixFile*)id;
23621 dotlockUnlock(id, NO_LOCK);
23622 sqlite3_free(pFile->lockingContext);
23623 }
23624 return closeUnixFile(id);
 
 
 
23625 }
23626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23627
23628 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
23629
23630 /*
23631 ** The nolockLockingContext is void
23632 */
23633 typedef void nolockLockingContext;
23634
23635 static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
 
23636 *pResOut = 0;
23637 return SQLITE_OK;
23638 }
23639
23640 static int nolockLock(sqlite3_file *id, int locktype) {
 
23641 return SQLITE_OK;
23642 }
23643
23644 static int nolockUnlock(sqlite3_file *id, int locktype) {
 
23645 return SQLITE_OK;
23646 }
23647
23648 /*
23649 ** Close a file.
23650 */
23651 static int nolockClose(sqlite3_file *id) {
23652 return closeUnixFile(id);
 
 
 
 
23653 }
23654
23655
23656 /*
23657 ** Information and control of an open file handle.
@@ -23674,18 +24249,20 @@
23674 ** SQLite code assumes this function cannot fail. It also assumes that
23675 ** if two files are created in the same file-system directory (i.e.
23676 ** a database and its journal file) that the sector size will be the
23677 ** same for both.
23678 */
23679 static int unixSectorSize(sqlite3_file *id){
 
23680 return SQLITE_DEFAULT_SECTOR_SIZE;
23681 }
23682
23683 /*
23684 ** Return the device characteristics for the file. This is always 0.
23685 */
23686 static int unixDeviceCharacteristics(sqlite3_file *id){
 
23687 return 0;
23688 }
23689
23690 /*
23691 ** Initialize the contents of the unixFile structure pointed to by pId.
@@ -23699,11 +24276,12 @@
23699 sqlite3_vfs *pVfs, /* Pointer to vfs object */
23700 int h, /* Open file descriptor of file being opened */
23701 int dirfd, /* Directory file descriptor */
23702 sqlite3_file *pId, /* Write to the unixFile structure here */
23703 const char *zFilename, /* Name of the file being opened */
23704 int noLock /* Omit locking if true */
 
23705 ){
23706 int eLockingStyle;
23707 unixFile *pNew = (unixFile *)pId;
23708 int rc = SQLITE_OK;
23709
@@ -23730,12 +24308,19 @@
23730 static sqlite3_io_methods aIoMethod[] = {
23731 IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
23732 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
23733 #if SQLITE_ENABLE_LOCKING_STYLE
23734 ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
 
 
 
 
 
23735 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
23736 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
 
 
23737 #endif
23738 };
23739 /* The order of the IOMETHODS macros above is important. It must be the
23740 ** same order as the LOCKING_STYLE numbers
23741 */
@@ -23742,18 +24327,63 @@
23742 assert(LOCKING_STYLE_POSIX==1);
23743 assert(LOCKING_STYLE_NONE==2);
23744 assert(LOCKING_STYLE_DOTFILE==3);
23745 assert(LOCKING_STYLE_FLOCK==4);
23746 assert(LOCKING_STYLE_AFP==5);
 
23747
23748 assert( pNew->pLock==NULL );
23749 assert( pNew->pOpen==NULL );
23750
 
 
 
 
 
 
 
 
23751 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
23752 pNew->h = h;
23753 pNew->dirfd = dirfd;
23754 SET_THREADID(pNew);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23755
23756 if( noLock ){
23757 eLockingStyle = LOCKING_STYLE_NONE;
23758 }else{
23759 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
@@ -23761,16 +24391,22 @@
23761
23762 switch( eLockingStyle ){
23763
23764 case LOCKING_STYLE_POSIX: {
23765 enterMutex();
 
 
 
23766 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
 
23767 leaveMutex();
23768 break;
23769 }
23770
23771 #if SQLITE_ENABLE_LOCKING_STYLE
 
 
23772 case LOCKING_STYLE_AFP: {
23773 /* AFP locking uses the file path so it needs to be included in
23774 ** the afpLockingContext.
23775 */
23776 afpLockingContext *pCtx;
@@ -23784,10 +24420,11 @@
23784 pCtx->filePath = zFilename;
23785 srandomdev();
23786 }
23787 break;
23788 }
 
23789
23790 case LOCKING_STYLE_DOTFILE: {
23791 /* Dotfile locking uses the file path so it needs to be included in
23792 ** the dotlockLockingContext
23793 */
@@ -23801,18 +24438,49 @@
23801 sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
23802 }
23803 pNew->lockingContext = zLockFile;
23804 break;
23805 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23806
23807 case LOCKING_STYLE_FLOCK:
23808 case LOCKING_STYLE_NONE:
23809 break;
23810 #endif
23811 }
23812
23813 pNew->lastErrno = 0;
 
 
 
 
 
 
 
23814 if( rc!=SQLITE_OK ){
23815 if( dirfd>=0 ) close(dirfd);
23816 close(h);
23817 }else{
23818 pNew->pMethod = &aIoMethod[eLockingStyle-1];
@@ -23878,11 +24546,11 @@
23878 ** function failing.
23879 */
23880 SimulateIOError( return SQLITE_IOERR );
23881
23882 azDirs[0] = sqlite3_temp_directory;
23883 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
23884 if( azDirs[i]==0 ) continue;
23885 if( stat(azDirs[i], &buf) ) continue;
23886 if( !S_ISDIR(buf.st_mode) ) continue;
23887 if( access(azDirs[i], 07) ) continue;
23888 zDir = azDirs[i];
@@ -23890,11 +24558,11 @@
23890 }
23891
23892 /* Check that the output buffer is large enough for the temporary file
23893 ** name. If it is not, return SQLITE_ERROR.
23894 */
23895 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
23896 return SQLITE_ERROR;
23897 }
23898
23899 do{
23900 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
@@ -24007,10 +24675,11 @@
24007 if( isCreate ) oflags |= O_CREAT;
24008 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
24009 oflags |= (O_LARGEFILE|O_BINARY);
24010
24011 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
 
24012 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
24013 /* Failed to open the file for read/write access. Try read-only. */
24014 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
24015 flags |= SQLITE_OPEN_READONLY;
24016 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
@@ -24017,11 +24686,15 @@
24017 }
24018 if( fd<0 ){
24019 return SQLITE_CANTOPEN;
24020 }
24021 if( isDelete ){
 
 
 
24022 unlink(zName);
 
24023 }
24024 if( pOutFlags ){
24025 *pOutFlags = flags;
24026 }
24027
@@ -24037,27 +24710,33 @@
24037 #ifdef FD_CLOEXEC
24038 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
24039 #endif
24040
24041 noLock = eType!=SQLITE_OPEN_MAIN_DB;
24042 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock);
24043 }
24044
24045 /*
24046 ** Delete the file at zPath. If the dirSync argument is true, fsync()
24047 ** the directory after deleting the file.
24048 */
24049 static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
24050 int rc = SQLITE_OK;
 
24051 SimulateIOError(return SQLITE_IOERR_DELETE);
24052 unlink(zPath);
24053 #ifndef SQLITE_DISABLE_DIRSYNC
24054 if( dirSync ){
24055 int fd;
24056 rc = openDirectory(zPath, &fd);
24057 if( rc==SQLITE_OK ){
24058 if( fsync(fd) ){
 
 
 
 
 
24059 rc = SQLITE_IOERR_DIR_FSYNC;
24060 }
24061 close(fd);
24062 }
24063 }
@@ -24074,16 +24753,17 @@
24074 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
24075 **
24076 ** Otherwise return 0.
24077 */
24078 static int unixAccess(
24079 sqlite3_vfs *pVfs,
24080 const char *zPath,
24081 int flags,
24082 int *pResOut
24083 ){
24084 int amode = 0;
 
24085 SimulateIOError( return SQLITE_IOERR_ACCESS; );
24086 switch( flags ){
24087 case SQLITE_ACCESS_EXISTS:
24088 amode = F_OK;
24089 break;
@@ -24124,10 +24804,24 @@
24124 ** current working directly has been unlinked.
24125 */
24126 SimulateIOError( return SQLITE_ERROR );
24127
24128 assert( pVfs->mxPathname==MAX_PATHNAME );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24129 zOut[nOut-1] = '\0';
24130 if( zPath[0]=='/' ){
24131 sqlite3_snprintf(nOut, zOut, "%s", zPath);
24132 }else{
24133 int nCwd;
@@ -24162,20 +24856,22 @@
24162 zFull[j++] = zFull[i];
24163 }
24164 zFull[j] = 0;
24165 }
24166 #endif
 
24167 }
24168
24169
24170 #ifndef SQLITE_OMIT_LOAD_EXTENSION
24171 /*
24172 ** Interfaces for opening a shared library, finding entry points
24173 ** within the shared library, and closing the shared library.
24174 */
24175 #include <dlfcn.h>
24176 static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
 
24177 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
24178 }
24179
24180 /*
24181 ** SQLite calls this function immediately after a call to unixDlSym() or
@@ -24182,23 +24878,26 @@
24182 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
24183 ** message is available, it is written to zBufOut. If no error message
24184 ** is available, zBufOut is left unmodified and SQLite uses a default
24185 ** error message.
24186 */
24187 static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24188 char *zErr;
 
24189 enterMutex();
24190 zErr = dlerror();
24191 if( zErr ){
24192 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
24193 }
24194 leaveMutex();
24195 }
24196 static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
 
24197 return dlsym(pHandle, zSymbol);
24198 }
24199 static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
 
24200 dlclose(pHandle);
24201 }
24202 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24203 #define unixDlOpen 0
24204 #define unixDlError 0
@@ -24207,13 +24906,13 @@
24207 #endif
24208
24209 /*
24210 ** Write nBuf bytes of random data to the supplied buffer zBuf.
24211 */
24212 static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24213
24214 assert(nBuf>=(sizeof(time_t)+sizeof(int)));
24215
24216 /* We have to initialize zBuf to prevent valgrind from reporting
24217 ** errors. The reports issued by valgrind are incorrect - we would
24218 ** prefer that the randomness be increased by making use of the
24219 ** uninitialized space in zBuf - but valgrind errors tend to worry
@@ -24234,11 +24933,11 @@
24234 time_t t;
24235 time(&t);
24236 memcpy(zBuf, &t, sizeof(t));
24237 pid = getpid();
24238 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
24239 assert( sizeof(t)+sizeof(pid)<=nBuf );
24240 nBuf = sizeof(t) + sizeof(pid);
24241 }else{
24242 nBuf = read(fd, zBuf, nBuf);
24243 close(fd);
24244 }
@@ -24254,19 +24953,27 @@
24254 ** The return value is the number of microseconds of sleep actually
24255 ** requested from the underlying operating system, a number which
24256 ** might be greater than or equal to the argument, but not less
24257 ** than the argument.
24258 */
24259 static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
24260 #if defined(HAVE_USLEEP) && HAVE_USLEEP
 
 
 
 
 
 
 
24261 usleep(microseconds);
24262 return microseconds;
24263 #else
24264 int seconds = (microseconds+999999)/1000000;
24265 sleep(seconds);
24266 return seconds*1000000;
24267 #endif
 
24268 }
24269
24270 /*
24271 ** The following variable, if set to a non-zero value, becomes the result
24272 ** returned from sqlite3OsCurrentTime(). This is used for testing.
@@ -24278,29 +24985,38 @@
24278 /*
24279 ** Find the current time (in Universal Coordinated Time). Write the
24280 ** current time and date as a Julian Day number into *prNow and
24281 ** return 0. Return 1 if the time and date cannot be found.
24282 */
24283 static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
24284 #ifdef NO_GETTOD
 
 
 
 
24285 time_t t;
24286 time(&t);
24287 *prNow = t/86400.0 + 2440587.5;
24288 #else
24289 struct timeval sNow;
24290 gettimeofday(&sNow, 0);
24291 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
24292 #endif
 
24293 #ifdef SQLITE_TEST
24294 if( sqlite3_current_time ){
24295 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
24296 }
24297 #endif
 
24298 return 0;
24299 }
24300
24301 static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 
 
 
24302 return 0;
24303 }
24304
24305 /*
24306 ** Initialize the operating system interface.
@@ -24338,16 +25054,20 @@
24338 static sqlite3_vfs aVfs[] = {
24339 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
24340 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
24341 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
24342 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
24343 UNIXVFS("unix-none", LOCKING_STYLE_NONE)
 
24344 };
24345 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
24346 sqlite3_vfs_register(&aVfs[i], 0);
24347 }
24348 #endif
 
 
 
24349 sqlite3_vfs_register(&unixVfs, 1);
24350 return SQLITE_OK;
24351 }
24352
24353 /*
@@ -24373,11 +25093,11 @@
24373 **
24374 ******************************************************************************
24375 **
24376 ** This file contains code that is specific to windows.
24377 **
24378 ** $Id: os_win.c,v 1.137 2008/11/07 00:06:18 drh Exp $
24379 */
24380 #if SQLITE_OS_WIN /* This file is used for windows only */
24381
24382
24383 /*
@@ -24844,11 +25564,11 @@
24844
24845 /*
24846 ** Convert multibyte character string to UTF-8. Space to hold the
24847 ** returned string is obtained from malloc().
24848 */
24849 static char *mbcsToUtf8(const char *zFilename){
24850 char *zFilenameUtf8;
24851 WCHAR *zTmpWide;
24852
24853 zTmpWide = mbcsToUnicode(zFilename);
24854 if( zTmpWide==0 ){
@@ -25342,15 +26062,22 @@
25342 if( flags & SQLITE_SYNC_FULL ){
25343 sqlite3_fullsync_count++;
25344 }
25345 sqlite3_sync_count++;
25346 #endif
 
 
 
 
 
 
25347 if( FlushFileBuffers(pFile->h) ){
25348 return SQLITE_OK;
25349 }else{
25350 return SQLITE_IOERR;
25351 }
 
25352 }
25353
25354 /*
25355 ** Determine the current size of a file in bytes
25356 */
@@ -25724,11 +26451,11 @@
25724 #if SQLITE_OS_WINCE==0
25725 }else{
25726 char *zUtf8;
25727 char zMbcsPath[MAX_PATH];
25728 GetTempPathA(MAX_PATH-30, zMbcsPath);
25729 zUtf8 = mbcsToUtf8(zMbcsPath);
25730 if( zUtf8 ){
25731 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
25732 free(zUtf8);
25733 }else{
25734 return SQLITE_NOMEM;
@@ -26065,11 +26792,11 @@
26065 free(zConverted);
26066 return SQLITE_NOMEM;
26067 }
26068 GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
26069 free(zConverted);
26070 zOut = mbcsToUtf8(zTemp);
26071 free(zTemp);
26072 #endif
26073 }
26074 if( zOut ){
26075 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
@@ -26137,10 +26864,15 @@
26137 /*
26138 ** Write up to nBuf bytes of randomness into zBuf.
26139 */
26140 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
26141 int n = 0;
 
 
 
 
 
26142 if( sizeof(SYSTEMTIME)<=nBuf-n ){
26143 SYSTEMTIME x;
26144 GetSystemTime(&x);
26145 memcpy(&zBuf[n], &x, sizeof(x));
26146 n += sizeof(x);
@@ -26159,10 +26891,11 @@
26159 LARGE_INTEGER i;
26160 QueryPerformanceCounter(&i);
26161 memcpy(&zBuf[n], &i, sizeof(i));
26162 n += sizeof(i);
26163 }
 
26164 return n;
26165 }
26166
26167
26168 /*
@@ -26316,24 +27049,45 @@
26316 ** sometimes grow into tens of thousands or larger. The size of the
26317 ** Bitvec object is the number of pages in the database file at the
26318 ** start of a transaction, and is thus usually less than a few thousand,
26319 ** but can be as large as 2 billion for a really big database.
26320 **
26321 ** @(#) $Id: bitvec.c,v 1.7 2008/11/03 20:55:07 drh Exp $
26322 */
26323
 
26324 #define BITVEC_SZ 512
 
26325 /* Round the union size down to the nearest pointer boundary, since that's how
26326 ** it will be aligned within the Bitvec struct. */
26327 #define BITVEC_USIZE (((BITVEC_SZ-12)/sizeof(Bitvec*))*sizeof(Bitvec*))
26328 #define BITVEC_NCHAR BITVEC_USIZE
26329 #define BITVEC_NBIT (BITVEC_NCHAR*8)
26330 #define BITVEC_NINT (BITVEC_USIZE/4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26331 #define BITVEC_MXHASH (BITVEC_NINT/2)
 
 
 
 
 
 
26332 #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
26333
26334 #define BITVEC_HASH(X) (((X)*37)%BITVEC_NINT)
26335
26336 /*
26337 ** A bitmap is an instance of the following structure.
26338 **
26339 ** This bitmap records the existance of zero or more bits
@@ -26353,15 +27107,19 @@
26353 ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
26354 ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
26355 ** to hold deal with values between 1 and iDivisor.
26356 */
26357 struct Bitvec {
26358 u32 iSize; /* Maximum bit index */
26359 u32 nSet; /* Number of bits that are set */
26360 u32 iDivisor; /* Number of bits handled by each apSub[] entry */
 
 
 
 
26361 union {
26362 u8 aBitmap[BITVEC_NCHAR]; /* Bitmap representation */
26363 u32 aHash[BITVEC_NINT]; /* Hash table representation */
26364 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
26365 } u;
26366 };
26367
@@ -26386,20 +27144,23 @@
26386 ** i is out of range, then return false.
26387 */
26388 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
26389 if( p==0 ) return 0;
26390 if( i>p->iSize || i==0 ) return 0;
 
 
 
 
 
 
 
 
 
26391 if( p->iSize<=BITVEC_NBIT ){
26392 i--;
26393 return (p->u.aBitmap[i/8] & (1<<(i&7)))!=0;
26394 }
26395 if( p->iDivisor>0 ){
26396 u32 bin = (i-1)/p->iDivisor;
26397 i = (i-1)%p->iDivisor + 1;
26398 return sqlite3BitvecTest(p->u.apSub[bin], i);
26399 }else{
26400 u32 h = BITVEC_HASH(i);
26401 while( p->u.aHash[h] ){
26402 if( p->u.aHash[h]==i ) return 1;
26403 h++;
26404 if( h>=BITVEC_NINT ) h = 0;
26405 }
@@ -26422,74 +27183,99 @@
26422 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
26423 u32 h;
26424 assert( p!=0 );
26425 assert( i>0 );
26426 assert( i<=p->iSize );
26427 if( p->iSize<=BITVEC_NBIT ){
26428 i--;
26429 p->u.aBitmap[i/8] |= 1 << (i&7);
26430 return SQLITE_OK;
26431 }
26432 if( p->iDivisor ){
26433 u32 bin = (i-1)/p->iDivisor;
26434 i = (i-1)%p->iDivisor + 1;
26435 if( p->u.apSub[bin]==0 ){
26436 sqlite3BeginBenignMalloc();
26437 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
26438 sqlite3EndBenignMalloc();
26439 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
26440 }
26441 return sqlite3BitvecSet(p->u.apSub[bin], i);
26442 }
26443 h = BITVEC_HASH(i);
26444 while( p->u.aHash[h] ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26445 if( p->u.aHash[h]==i ) return SQLITE_OK;
26446 h++;
26447 if( h==BITVEC_NINT ) h = 0;
26448 }
26449 p->nSet++;
 
 
 
26450 if( p->nSet>=BITVEC_MXHASH ){
26451 int j, rc;
 
26452 u32 aiValues[BITVEC_NINT];
26453 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
26454 memset(p->u.apSub, 0, sizeof(p->u.apSub[0])*BITVEC_NPTR);
26455 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
26456 rc = sqlite3BitvecSet(p, i);
26457 for(j=0; j<BITVEC_NINT; j++){
26458 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
26459 }
26460 return rc;
26461 }
 
 
26462 p->u.aHash[h] = i;
26463 return SQLITE_OK;
26464 }
26465
26466 /*
26467 ** Clear the i-th bit. Return 0 on success and an error code if
26468 ** anything goes wrong.
26469 */
26470 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i){
26471 assert( p!=0 );
26472 assert( i>0 );
 
 
 
 
 
 
 
 
 
26473 if( p->iSize<=BITVEC_NBIT ){
26474 i--;
26475 p->u.aBitmap[i/8] &= ~(1 << (i&7));
26476 }else if( p->iDivisor ){
26477 u32 bin = (i-1)/p->iDivisor;
26478 i = (i-1)%p->iDivisor + 1;
26479 if( p->u.apSub[bin] ){
26480 sqlite3BitvecClear(p->u.apSub[bin], i);
26481 }
26482 }else{
26483 int j;
26484 u32 aiValues[BITVEC_NINT];
26485 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
26486 memset(p->u.aHash, 0, sizeof(p->u.aHash[0])*BITVEC_NINT);
26487 p->nSet = 0;
26488 for(j=0; j<BITVEC_NINT; j++){
26489 if( aiValues[j] && aiValues[j]!=i ){
26490 sqlite3BitvecSet(p, aiValues[j]);
 
 
 
 
 
 
26491 }
26492 }
26493 }
26494 }
26495
@@ -26497,11 +27283,11 @@
26497 ** Destroy a bitmap object. Reclaim all memory used.
26498 */
26499 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
26500 if( p==0 ) return;
26501 if( p->iDivisor ){
26502 int i;
26503 for(i=0; i<BITVEC_NPTR; i++){
26504 sqlite3BitvecDestroy(p->u.apSub[i]);
26505 }
26506 }
26507 sqlite3_free(p);
@@ -26630,106 +27416,30 @@
26630 ** May you share freely, never taking more than you give.
26631 **
26632 *************************************************************************
26633 ** This file implements that page cache.
26634 **
26635 ** @(#) $Id: pcache.c,v 1.34 2008/10/17 18:51:53 danielk1977 Exp $
26636 */
26637
26638 /*
26639 ** A complete page cache is an instance of this structure.
26640 **
26641 ** A cache may only be deleted by its owner and while holding the
26642 ** SQLITE_MUTEX_STATUS_LRU mutex.
26643 */
26644 struct PCache {
26645 /*********************************************************************
26646 ** The first group of elements may be read or written at any time by
26647 ** the cache owner without holding the mutex. No thread other than the
26648 ** cache owner is permitted to access these elements at any time.
26649 */
26650 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
26651 PgHdr *pSynced; /* Last synced page in dirty page list */
26652 int nRef; /* Number of pinned pages */
26653 int nPinned; /* Number of pinned and/or dirty pages */
26654 int nMax; /* Configured cache size */
26655 int nMin; /* Configured minimum cache size */
26656 /**********************************************************************
26657 ** The next group of elements are fixed when the cache is created and
26658 ** may not be changed afterwards. These elements can read at any time by
26659 ** the cache owner or by any thread holding the the mutex. Non-owner
26660 ** threads must hold the mutex when reading these elements to prevent
26661 ** the entire PCache object from being deleted during the read.
26662 */
26663 int szPage; /* Size of every page in this cache */
26664 int szExtra; /* Size of extra space for each page */
26665 int bPurgeable; /* True if pages are on backing store */
26666 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
26667 void *pStress; /* Argument to xStress */
26668 /**********************************************************************
26669 ** The final group of elements can only be accessed while holding the
26670 ** mutex. Both the cache owner and any other thread must hold the mutex
26671 ** to read or write any of these elements.
26672 */
26673 int nPage; /* Total number of pages in apHash */
26674 int nHash; /* Number of slots in apHash[] */
26675 PgHdr **apHash; /* Hash table for fast lookup by pgno */
26676 PgHdr *pClean; /* List of clean pages in use */
26677 };
26678
26679 /*
26680 ** Free slots in the page block allocator
26681 */
26682 typedef struct PgFreeslot PgFreeslot;
26683 struct PgFreeslot {
26684 PgFreeslot *pNext; /* Next free slot */
26685 };
26686
26687 /*
26688 ** Global data for the page cache.
26689 */
26690 static SQLITE_WSD struct PCacheGlobal {
26691 int isInit; /* True when initialized */
26692 sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
26693
26694 int nMaxPage; /* Sum of nMaxPage for purgeable caches */
26695 int nMinPage; /* Sum of nMinPage for purgeable caches */
26696 int nCurrentPage; /* Number of purgeable pages allocated */
26697 PgHdr *pLruHead, *pLruTail; /* LRU list of unused clean pgs */
26698
26699 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
26700 int szSlot; /* Size of each free slot */
26701 void *pStart, *pEnd; /* Bounds of pagecache malloc range */
26702 PgFreeslot *pFree; /* Free page blocks */
26703 } pcache = {0};
26704
26705 /*
26706 ** All code in this file should access the global pcache structure via the
26707 ** alias "pcache_g". This ensures that the WSD emulation is used when
26708 ** compiling for systems that do not support real WSD.
26709 */
26710 #define pcache_g (GLOBAL(struct PCacheGlobal, pcache))
26711
26712 /*
26713 ** All global variables used by this module (all of which are grouped
26714 ** together in global structure "pcache" above) are protected by the static
26715 ** SQLITE_MUTEX_STATIC_LRU mutex. A pointer to this mutex is stored in
26716 ** variable "pcache.mutex".
26717 **
26718 ** Some elements of the PCache and PgHdr structures are protected by the
26719 ** SQLITE_MUTEX_STATUS_LRU mutex and other are not. The protected
26720 ** elements are grouped at the end of the structures and are clearly
26721 ** marked.
26722 **
26723 ** Use the following macros must surround all access (read or write)
26724 ** of protected elements. The mutex is not recursive and may not be
26725 ** entered more than once. The pcacheMutexHeld() macro should only be
26726 ** used within an assert() to verify that the mutex is being held.
26727 */
26728 #define pcacheEnterMutex() sqlite3_mutex_enter(pcache_g.mutex)
26729 #define pcacheExitMutex() sqlite3_mutex_leave(pcache_g.mutex)
26730 #define pcacheMutexHeld() sqlite3_mutex_held(pcache_g.mutex)
26731
26732 /*
26733 ** Some of the assert() macros in this code are too expensive to run
26734 ** even during normal debugging. Use them only rarely on long-running
26735 ** tests. Enable the expensive asserts using the
@@ -26740,52 +27450,10 @@
26740 #else
26741 # define expensive_assert(X)
26742 #endif
26743
26744 /********************************** Linked List Management ********************/
26745
26746 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
26747 /*
26748 ** This routine verifies that the number of entries in the hash table
26749 ** is pCache->nPage. This routine is used within assert() statements
26750 ** only and is therefore disabled during production builds.
26751 */
26752 static int pcacheCheckHashCount(PCache *pCache){
26753 int i;
26754 int nPage = 0;
26755 for(i=0; i<pCache->nHash; i++){
26756 PgHdr *p;
26757 for(p=pCache->apHash[i]; p; p=p->pNextHash){
26758 nPage++;
26759 }
26760 }
26761 assert( nPage==pCache->nPage );
26762 return 1;
26763 }
26764 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
26765
26766
26767 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
26768 /*
26769 ** Based on the current value of PCache.nRef and the contents of the
26770 ** PCache.pDirty list, return the expected value of the PCache.nPinned
26771 ** counter. This is only used in debugging builds, as follows:
26772 **
26773 ** expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
26774 */
26775 static int pcachePinnedCount(PCache *pCache){
26776 PgHdr *p;
26777 int nPinned = pCache->nRef;
26778 for(p=pCache->pDirty; p; p=p->pNext){
26779 if( p->nRef==0 ){
26780 nPinned++;
26781 }
26782 }
26783 return nPinned;
26784 }
26785 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
26786
26787
26788 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
26789 /*
26790 ** Check that the pCache->pSynced variable is set correctly. If it
26791 ** is not, either fail an assert or return zero. Otherwise, return
@@ -26792,505 +27460,148 @@
26792 ** non-zero. This is only used in debugging builds, as follows:
26793 **
26794 ** expensive_assert( pcacheCheckSynced(pCache) );
26795 */
26796 static int pcacheCheckSynced(PCache *pCache){
26797 PgHdr *p = pCache->pDirtyTail;
26798 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pPrev){
26799 assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
26800 }
26801 return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
26802 }
26803 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
26804
26805
26806
26807 /*
26808 ** Remove a page from its hash table (PCache.apHash[]).
26809 */
26810 static void pcacheRemoveFromHash(PgHdr *pPage){
26811 assert( pcacheMutexHeld() );
26812 if( pPage->pPrevHash ){
26813 pPage->pPrevHash->pNextHash = pPage->pNextHash;
26814 }else{
26815 PCache *pCache = pPage->pCache;
26816 u32 h = pPage->pgno % pCache->nHash;
26817 assert( pCache->apHash[h]==pPage );
26818 pCache->apHash[h] = pPage->pNextHash;
26819 }
26820 if( pPage->pNextHash ){
26821 pPage->pNextHash->pPrevHash = pPage->pPrevHash;
26822 }
26823 pPage->pCache->nPage--;
26824 expensive_assert( pcacheCheckHashCount(pPage->pCache) );
26825 }
26826
26827 /*
26828 ** Insert a page into the hash table
26829 **
26830 ** The mutex must be held by the caller.
26831 */
26832 static void pcacheAddToHash(PgHdr *pPage){
26833 PCache *pCache = pPage->pCache;
26834 u32 h = pPage->pgno % pCache->nHash;
26835 assert( pcacheMutexHeld() );
26836 pPage->pNextHash = pCache->apHash[h];
26837 pPage->pPrevHash = 0;
26838 if( pCache->apHash[h] ){
26839 pCache->apHash[h]->pPrevHash = pPage;
26840 }
26841 pCache->apHash[h] = pPage;
26842 pCache->nPage++;
26843 expensive_assert( pcacheCheckHashCount(pCache) );
26844 }
26845
26846 /*
26847 ** Attempt to increase the size the hash table to contain
26848 ** at least nHash buckets.
26849 */
26850 static int pcacheResizeHash(PCache *pCache, int nHash){
26851 PgHdr *p;
26852 PgHdr **pNew;
26853 assert( pcacheMutexHeld() );
26854 #ifdef SQLITE_MALLOC_SOFT_LIMIT
26855 if( nHash*sizeof(PgHdr*)>SQLITE_MALLOC_SOFT_LIMIT ){
26856 nHash = SQLITE_MALLOC_SOFT_LIMIT/sizeof(PgHdr *);
26857 }
26858 #endif
26859 pcacheExitMutex();
26860 pNew = (PgHdr **)sqlite3Malloc(sizeof(PgHdr*)*nHash);
26861 pcacheEnterMutex();
26862 if( !pNew ){
26863 return SQLITE_NOMEM;
26864 }
26865 memset(pNew, 0, sizeof(PgHdr *)*nHash);
26866 sqlite3_free(pCache->apHash);
26867 pCache->apHash = pNew;
26868 pCache->nHash = nHash;
26869 pCache->nPage = 0;
26870
26871 for(p=pCache->pClean; p; p=p->pNext){
26872 pcacheAddToHash(p);
26873 }
26874 for(p=pCache->pDirty; p; p=p->pNext){
26875 pcacheAddToHash(p);
26876 }
26877 return SQLITE_OK;
26878 }
26879
26880 /*
26881 ** Remove a page from a linked list that is headed by *ppHead.
26882 ** *ppHead is either PCache.pClean or PCache.pDirty.
26883 */
26884 static void pcacheRemoveFromList(PgHdr **ppHead, PgHdr *pPage){
26885 int isDirtyList = (ppHead==&pPage->pCache->pDirty);
26886 assert( ppHead==&pPage->pCache->pClean || ppHead==&pPage->pCache->pDirty );
26887 assert( pcacheMutexHeld() || ppHead!=&pPage->pCache->pClean );
26888
26889 if( pPage->pPrev ){
26890 pPage->pPrev->pNext = pPage->pNext;
26891 }else{
26892 assert( *ppHead==pPage );
26893 *ppHead = pPage->pNext;
26894 }
26895 if( pPage->pNext ){
26896 pPage->pNext->pPrev = pPage->pPrev;
26897 }
26898
26899 if( isDirtyList ){
26900 PCache *pCache = pPage->pCache;
26901 assert( pPage->pNext || pCache->pDirtyTail==pPage );
26902 if( !pPage->pNext ){
26903 pCache->pDirtyTail = pPage->pPrev;
26904 }
26905 if( pCache->pSynced==pPage ){
26906 PgHdr *pSynced = pPage->pPrev;
26907 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
26908 pSynced = pSynced->pPrev;
26909 }
26910 pCache->pSynced = pSynced;
26911 }
26912 }
26913 }
26914
26915 /*
26916 ** Add a page from a linked list that is headed by *ppHead.
26917 ** *ppHead is either PCache.pClean or PCache.pDirty.
26918 */
26919 static void pcacheAddToList(PgHdr **ppHead, PgHdr *pPage){
26920 int isDirtyList = (ppHead==&pPage->pCache->pDirty);
26921 assert( ppHead==&pPage->pCache->pClean || ppHead==&pPage->pCache->pDirty );
26922
26923 if( (*ppHead) ){
26924 (*ppHead)->pPrev = pPage;
26925 }
26926 pPage->pNext = *ppHead;
26927 pPage->pPrev = 0;
26928 *ppHead = pPage;
26929
26930 if( isDirtyList ){
26931 PCache *pCache = pPage->pCache;
26932 if( !pCache->pDirtyTail ){
26933 assert( pPage->pNext==0 );
26934 pCache->pDirtyTail = pPage;
26935 }
26936 if( !pCache->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
26937 pCache->pSynced = pPage;
26938 }
26939 }
26940 }
26941
26942 /*
26943 ** Remove a page from the global LRU list
26944 */
26945 static void pcacheRemoveFromLruList(PgHdr *pPage){
26946 assert( sqlite3_mutex_held(pcache_g.mutex) );
26947 assert( (pPage->flags&PGHDR_DIRTY)==0 );
26948 if( pPage->pCache->bPurgeable==0 ) return;
26949 if( pPage->pNextLru ){
26950 assert( pcache_g.pLruTail!=pPage );
26951 pPage->pNextLru->pPrevLru = pPage->pPrevLru;
26952 }else{
26953 assert( pcache_g.pLruTail==pPage );
26954 pcache_g.pLruTail = pPage->pPrevLru;
26955 }
26956 if( pPage->pPrevLru ){
26957 assert( pcache_g.pLruHead!=pPage );
26958 pPage->pPrevLru->pNextLru = pPage->pNextLru;
26959 }else{
26960 assert( pcache_g.pLruHead==pPage );
26961 pcache_g.pLruHead = pPage->pNextLru;
26962 }
26963 }
26964
26965 /*
26966 ** Add a page to the global LRU list. The page is normally added
26967 ** to the front of the list so that it will be the last page recycled.
26968 ** However, if the PGHDR_REUSE_UNLIKELY bit is set, the page is added
26969 ** to the end of the LRU list so that it will be the next to be recycled.
26970 */
26971 static void pcacheAddToLruList(PgHdr *pPage){
26972 assert( sqlite3_mutex_held(pcache_g.mutex) );
26973 assert( (pPage->flags&PGHDR_DIRTY)==0 );
26974 if( pPage->pCache->bPurgeable==0 ) return;
26975 if( pcache_g.pLruTail && (pPage->flags & PGHDR_REUSE_UNLIKELY)!=0 ){
26976 /* If reuse is unlikely. Put the page at the end of the LRU list
26977 ** where it will be recycled sooner rather than later.
26978 */
26979 assert( pcache_g.pLruHead );
26980 pPage->pNextLru = 0;
26981 pPage->pPrevLru = pcache_g.pLruTail;
26982 pcache_g.pLruTail->pNextLru = pPage;
26983 pcache_g.pLruTail = pPage;
26984 pPage->flags &= ~PGHDR_REUSE_UNLIKELY;
26985 }else{
26986 /* If reuse is possible. the page goes at the beginning of the LRU
26987 ** list so that it will be the last to be recycled.
26988 */
26989 if( pcache_g.pLruHead ){
26990 pcache_g.pLruHead->pPrevLru = pPage;
26991 }
26992 pPage->pNextLru = pcache_g.pLruHead;
26993 pcache_g.pLruHead = pPage;
26994 pPage->pPrevLru = 0;
26995 if( pcache_g.pLruTail==0 ){
26996 pcache_g.pLruTail = pPage;
26997 }
26998 }
26999 }
27000
27001 /*********************************************** Memory Allocation ***********
27002 **
27003 ** Initialize the page cache memory pool.
27004 **
27005 ** This must be called at start-time when no page cache lines are
27006 ** checked out. This function is not threadsafe.
27007 */
27008 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
27009 PgFreeslot *p;
27010 sz &= ~7;
27011 pcache_g.szSlot = sz;
27012 pcache_g.pStart = pBuf;
27013 pcache_g.pFree = 0;
27014 while( n-- ){
27015 p = (PgFreeslot*)pBuf;
27016 p->pNext = pcache_g.pFree;
27017 pcache_g.pFree = p;
27018 pBuf = (void*)&((char*)pBuf)[sz];
27019 }
27020 pcache_g.pEnd = pBuf;
27021 }
27022
27023 /*
27024 ** Allocate a page cache line. Look in the page cache memory pool first
27025 ** and use an element from it first if available. If nothing is available
27026 ** in the page cache memory pool, go to the general purpose memory allocator.
27027 */
27028 static void *pcacheMalloc(int sz, PCache *pCache){
27029 assert( sqlite3_mutex_held(pcache_g.mutex) );
27030 if( sz<=pcache_g.szSlot && pcache_g.pFree ){
27031 PgFreeslot *p = pcache_g.pFree;
27032 pcache_g.pFree = p->pNext;
27033 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, sz);
27034 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
27035 return (void*)p;
27036 }else{
27037 void *p;
27038
27039 /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
27040 ** global pcache mutex and unlock the pager-cache object pCache. This is
27041 ** so that if the attempt to allocate a new buffer causes the the
27042 ** configured soft-heap-limit to be breached, it will be possible to
27043 ** reclaim memory from this pager-cache.
27044 */
27045 pcacheExitMutex();
27046 p = sqlite3Malloc(sz);
27047 pcacheEnterMutex();
27048
27049 if( p ){
27050 sz = sqlite3MallocSize(p);
27051 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
27052 }
27053 return p;
27054 }
27055 }
27056 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
27057 void *p;
27058 pcacheEnterMutex();
27059 p = pcacheMalloc(sz, 0);
27060 pcacheExitMutex();
27061 return p;
27062 }
27063
27064 /*
27065 ** Release a pager memory allocation
27066 */
27067 static void pcacheFree(void *p){
27068 assert( sqlite3_mutex_held(pcache_g.mutex) );
27069 if( p==0 ) return;
27070 if( p>=pcache_g.pStart && p<pcache_g.pEnd ){
27071 PgFreeslot *pSlot;
27072 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
27073 pSlot = (PgFreeslot*)p;
27074 pSlot->pNext = pcache_g.pFree;
27075 pcache_g.pFree = pSlot;
27076 }else{
27077 int iSize = sqlite3MallocSize(p);
27078 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
27079 sqlite3_free(p);
27080 }
27081 }
27082 SQLITE_PRIVATE void sqlite3PageFree(void *p){
27083 pcacheEnterMutex();
27084 pcacheFree(p);
27085 pcacheExitMutex();
27086 }
27087
27088 /*
27089 ** Allocate a new page.
27090 */
27091 static PgHdr *pcachePageAlloc(PCache *pCache){
27092 PgHdr *p;
27093 int sz = sizeof(*p) + pCache->szPage + pCache->szExtra;
27094 assert( sqlite3_mutex_held(pcache_g.mutex) );
27095 p = pcacheMalloc(sz, pCache);
27096 if( p==0 ) return 0;
27097 memset(p, 0, sizeof(PgHdr));
27098 p->pData = (void*)&p[1];
27099 p->pExtra = (void*)&((char*)p->pData)[pCache->szPage];
27100 if( pCache->bPurgeable ){
27101 pcache_g.nCurrentPage++;
27102 }
27103 return p;
27104 }
27105
27106 /*
27107 ** Deallocate a page
27108 */
27109 static void pcachePageFree(PgHdr *p){
27110 assert( sqlite3_mutex_held(pcache_g.mutex) );
27111 if( p->pCache->bPurgeable ){
27112 pcache_g.nCurrentPage--;
27113 }
27114 pcacheFree(p);
27115 }
27116
27117 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27118 /*
27119 ** Return the number of bytes that will be returned to the heap when
27120 ** the argument is passed to pcachePageFree().
27121 */
27122 static int pcachePageSize(PgHdr *p){
27123 assert( sqlite3_mutex_held(pcache_g.mutex) );
27124 assert( !pcache_g.pStart );
27125 assert( p->apSave[0]==0 );
27126 assert( p->apSave[1]==0 );
27127 assert( p && p->pCache );
27128 return sqlite3MallocSize(p);
27129 }
27130 #endif
27131
27132 /*
27133 ** Attempt to 'recycle' a page from the global LRU list. Only clean,
27134 ** unreferenced pages from purgeable caches are eligible for recycling.
27135 **
27136 ** This function removes page pcache.pLruTail from the global LRU list,
27137 ** and from the hash-table and PCache.pClean list of the owner pcache.
27138 ** There should be no other references to the page.
27139 **
27140 ** A pointer to the recycled page is returned, or NULL if no page is
27141 ** eligible for recycling.
27142 */
27143 static PgHdr *pcacheRecyclePage(void){
27144 PgHdr *p = 0;
27145 assert( sqlite3_mutex_held(pcache_g.mutex) );
27146
27147 if( (p=pcache_g.pLruTail) ){
27148 assert( (p->flags&PGHDR_DIRTY)==0 );
27149 pcacheRemoveFromLruList(p);
27150 pcacheRemoveFromHash(p);
27151 pcacheRemoveFromList(&p->pCache->pClean, p);
27152 }
27153
27154 return p;
27155 }
27156
27157 /*
27158 ** Obtain space for a page. Try to recycle an old page if the limit on the
27159 ** number of pages has been reached. If the limit has not been reached or
27160 ** there are no pages eligible for recycling, allocate a new page.
27161 **
27162 ** Return a pointer to the new page, or NULL if an OOM condition occurs.
27163 */
27164 static int pcacheRecycleOrAlloc(PCache *pCache, PgHdr **ppPage){
27165 PgHdr *p = 0;
27166
27167 int szPage = pCache->szPage;
27168 int szExtra = pCache->szExtra;
27169
27170 assert( pcache_g.isInit );
27171 assert( sqlite3_mutex_held(pcache_g.mutex) );
27172
27173 *ppPage = 0;
27174
27175 /* If we have reached either the global or the local limit for
27176 ** pinned+dirty pages, and there is at least one dirty page,
27177 ** invoke the xStress callback to cause a page to become clean.
27178 */
27179 expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27180 expensive_assert( pcacheCheckSynced(pCache) );
27181 if( pCache->xStress
27182 && pCache->pDirty
27183 && (pCache->nPinned>=(pcache_g.nMaxPage+pCache->nMin-pcache_g.nMinPage)
27184 || pCache->nPinned>=pCache->nMax)
27185 ){
27186 PgHdr *pPg;
27187 assert(pCache->pDirtyTail);
27188
27189 for(pPg=pCache->pSynced;
27190 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
27191 pPg=pPg->pPrev
27192 );
27193 if( !pPg ){
27194 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pPrev);
27195 }
27196 if( pPg ){
27197 int rc;
27198 pcacheExitMutex();
27199 rc = pCache->xStress(pCache->pStress, pPg);
27200 pcacheEnterMutex();
27201 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
27202 return rc;
27203 }
27204 }
27205 }
27206
27207 /* If either the local or the global page limit has been reached,
27208 ** try to recycle a page.
27209 */
27210 if( pCache->bPurgeable && (pCache->nPage>=pCache->nMax-1 ||
27211 pcache_g.nCurrentPage>=pcache_g.nMaxPage) ){
27212 p = pcacheRecyclePage();
27213 }
27214
27215 /* If a page has been recycled but it is the wrong size, free it. */
27216 if( p && (p->pCache->szPage!=szPage || p->pCache->szPage!=szExtra) ){
27217 pcachePageFree(p);
27218 p = 0;
27219 }
27220
27221 if( !p ){
27222 p = pcachePageAlloc(pCache);
27223 }
27224
27225 *ppPage = p;
27226 return (p?SQLITE_OK:SQLITE_NOMEM);
27227 }
27228
27229 /*************************************************** General Interfaces ******
27230 **
27231 ** Initialize and shutdown the page cache subsystem. Neither of these
27232 ** functions are threadsafe.
27233 */
27234 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
27235 assert( pcache_g.isInit==0 );
27236 memset(&pcache_g, 0, sizeof(pcache));
27237 if( sqlite3GlobalConfig.bCoreMutex ){
27238 /* No need to check the return value of sqlite3_mutex_alloc().
27239 ** Allocating a static mutex cannot fail.
27240 */
27241 pcache_g.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
27242 }
27243 pcache_g.isInit = 1;
27244 return SQLITE_OK;
27245 }
27246 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
27247 memset(&pcache_g, 0, sizeof(pcache));
 
 
27248 }
27249
27250 /*
27251 ** Return the size in bytes of a PCache object.
27252 */
27253 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
27254
27255 /*
27256 ** Create a new PCache object. Storage space to hold the object
27257 ** has already been allocated and is passed in as the p pointer.
 
 
27258 */
27259 SQLITE_PRIVATE void sqlite3PcacheOpen(
27260 int szPage, /* Size of every page */
27261 int szExtra, /* Extra space associated with each page */
27262 int bPurgeable, /* True if pages are on backing store */
27263 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
27264 void *pStress, /* Argument to xStress */
27265 PCache *p /* Preallocated space for the PCache */
27266 ){
27267 assert( pcache_g.isInit );
27268 memset(p, 0, sizeof(PCache));
27269 p->szPage = szPage;
27270 p->szExtra = szExtra;
27271 p->bPurgeable = bPurgeable;
27272 p->xStress = xStress;
27273 p->pStress = pStress;
27274 p->nMax = 100;
27275 p->nMin = 10;
27276
27277 pcacheEnterMutex();
27278 if( bPurgeable ){
27279 pcache_g.nMaxPage += p->nMax;
27280 pcache_g.nMinPage += p->nMin;
27281 }
27282
27283 pcacheExitMutex();
27284 }
27285
27286 /*
27287 ** Change the page size for PCache object. This can only happen
27288 ** when the cache is empty.
27289 */
27290 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
27291 assert(pCache->nPage==0);
 
 
 
 
27292 pCache->szPage = szPage;
27293 }
27294
27295 /*
27296 ** Try to obtain a page from the cache.
@@ -27299,96 +27610,106 @@
27299 PCache *pCache, /* Obtain the page from this cache */
27300 Pgno pgno, /* Page number to obtain */
27301 int createFlag, /* If true, create page if it does not exist already */
27302 PgHdr **ppPage /* Write the page here */
27303 ){
27304 int rc = SQLITE_OK;
27305 PgHdr *pPage = 0;
 
27306
27307 assert( pcache_g.isInit );
27308 assert( pCache!=0 );
27309 assert( pgno>0 );
27310 expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27311
27312 pcacheEnterMutex();
27313
27314 /* Search the hash table for the requested page. Exit early if it is found. */
27315 if( pCache->apHash ){
27316 u32 h = pgno % pCache->nHash;
27317 for(pPage=pCache->apHash[h]; pPage; pPage=pPage->pNextHash){
27318 if( pPage->pgno==pgno ){
27319 if( pPage->nRef==0 ){
27320 if( 0==(pPage->flags&PGHDR_DIRTY) ){
27321 pcacheRemoveFromLruList(pPage);
27322 pCache->nPinned++;
27323 }
27324 pCache->nRef++;
27325 }
27326 pPage->nRef++;
27327 break;
27328 }
27329 }
27330 }
27331
27332 if( !pPage && createFlag ){
27333 if( pCache->nHash<=pCache->nPage ){
27334 rc = pcacheResizeHash(pCache, pCache->nHash<256 ? 256 : pCache->nHash*2);
27335 }
27336 if( rc==SQLITE_OK ){
27337 rc = pcacheRecycleOrAlloc(pCache, &pPage);
27338 }
27339 if( rc==SQLITE_OK ){
27340 pPage->pPager = 0;
27341 pPage->flags = 0;
27342 pPage->pDirty = 0;
27343 pPage->pgno = pgno;
27344 pPage->pCache = pCache;
27345 pPage->nRef = 1;
27346 pCache->nRef++;
27347 pCache->nPinned++;
27348 pcacheAddToList(&pCache->pClean, pPage);
27349 pcacheAddToHash(pPage);
27350 }
27351 }
27352
27353 pcacheExitMutex();
27354
27355 *ppPage = pPage;
27356 expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27357 assert( pPage || !createFlag || rc!=SQLITE_OK );
27358 return rc;
27359 }
27360
27361 /*
27362 ** Dereference a page. When the reference count reaches zero,
27363 ** move the page to the LRU list if it is clean.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27364 */
27365 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
27366 assert( p->nRef>0 );
27367 p->nRef--;
27368 if( p->nRef==0 ){
27369 PCache *pCache = p->pCache;
27370 pCache->nRef--;
27371 if( (p->flags&PGHDR_DIRTY)==0 ){
27372 pCache->nPinned--;
27373 pcacheEnterMutex();
27374 if( pcache_g.nCurrentPage>pcache_g.nMaxPage ){
27375 pcacheRemoveFromList(&pCache->pClean, p);
27376 pcacheRemoveFromHash(p);
27377 pcachePageFree(p);
27378 }else{
27379 pcacheAddToLruList(p);
27380 }
27381 pcacheExitMutex();
27382 }else{
27383 /* Move the page to the head of the caches dirty list. */
27384 pcacheRemoveFromList(&pCache->pDirty, p);
27385 pcacheAddToList(&pCache->pDirty, p);
27386 }
27387 }
27388 }
27389
 
 
 
27390 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
27391 assert(p->nRef>0);
27392 p->nRef++;
27393 }
27394
@@ -27398,224 +27719,134 @@
27398 ** page pointed to by p is invalid.
27399 */
27400 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
27401 PCache *pCache;
27402 assert( p->nRef==1 );
27403 assert( 0==(p->flags&PGHDR_DIRTY) );
 
 
27404 pCache = p->pCache;
27405 pCache->nRef--;
27406 pCache->nPinned--;
27407 pcacheEnterMutex();
27408 pcacheRemoveFromList(&pCache->pClean, p);
27409 pcacheRemoveFromHash(p);
27410 pcachePageFree(p);
27411 pcacheExitMutex();
27412 }
27413
27414 /*
27415 ** Make sure the page is marked as dirty. If it isn't dirty already,
27416 ** make it so.
27417 */
27418 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
27419 PCache *pCache;
27420 p->flags &= ~PGHDR_DONT_WRITE;
27421 if( p->flags & PGHDR_DIRTY ) return;
27422 assert( (p->flags & PGHDR_DIRTY)==0 );
27423 assert( p->nRef>0 );
27424 pCache = p->pCache;
27425 pcacheEnterMutex();
27426 pcacheRemoveFromList(&pCache->pClean, p);
27427 pcacheAddToList(&pCache->pDirty, p);
27428 pcacheExitMutex();
27429 p->flags |= PGHDR_DIRTY;
27430 }
27431
27432 static void pcacheMakeClean(PgHdr *p){
27433 PCache *pCache = p->pCache;
27434 assert( p->flags & PGHDR_DIRTY );
27435 pcacheRemoveFromList(&pCache->pDirty, p);
27436 pcacheAddToList(&pCache->pClean, p);
27437 p->flags &= ~PGHDR_DIRTY;
27438 if( p->nRef==0 ){
27439 pcacheAddToLruList(p);
27440 pCache->nPinned--;
27441 }
27442 expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27443 }
27444
27445 /*
27446 ** Make sure the page is marked as clean. If it isn't clean already,
27447 ** make it so.
27448 */
27449 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
27450 if( (p->flags & PGHDR_DIRTY) ){
27451 pcacheEnterMutex();
27452 pcacheMakeClean(p);
27453 pcacheExitMutex();
 
 
27454 }
27455 }
27456
27457 /*
27458 ** Make every page in the cache clean.
27459 */
27460 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
27461 PgHdr *p;
27462 pcacheEnterMutex();
27463 while( (p = pCache->pDirty)!=0 ){
27464 pcacheRemoveFromList(&pCache->pDirty, p);
27465 p->flags &= ~PGHDR_DIRTY;
27466 pcacheAddToList(&pCache->pClean, p);
27467 if( p->nRef==0 ){
27468 pcacheAddToLruList(p);
27469 pCache->nPinned--;
27470 }
27471 }
27472 sqlite3PcacheAssertFlags(pCache, 0, PGHDR_DIRTY);
27473 expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
27474 pcacheExitMutex();
27475 }
27476
27477 /*
27478 ** Change the page number of page p to newPgno. If newPgno is 0, then the
27479 ** page object is added to the clean-list and the PGHDR_REUSE_UNLIKELY
27480 ** flag set.
 
 
 
 
 
 
 
 
 
27481 */
27482 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
 
27483 assert( p->nRef>0 );
27484 pcacheEnterMutex();
27485 pcacheRemoveFromHash(p);
27486 p->pgno = newPgno;
27487 if( newPgno==0 ){
27488 if( (p->flags & PGHDR_DIRTY) ){
27489 pcacheMakeClean(p);
27490 }
27491 p->flags = PGHDR_REUSE_UNLIKELY;
27492 }
27493 pcacheAddToHash(p);
27494 pcacheExitMutex();
27495 }
27496
27497 /*
27498 ** Remove all content from a page cache
27499 */
27500 static void pcacheClear(PCache *pCache){
27501 PgHdr *p, *pNext;
27502 assert( sqlite3_mutex_held(pcache_g.mutex) );
27503 for(p=pCache->pClean; p; p=pNext){
27504 pNext = p->pNext;
27505 pcacheRemoveFromLruList(p);
27506 pcachePageFree(p);
27507 }
27508 for(p=pCache->pDirty; p; p=pNext){
27509 pNext = p->pNext;
27510 pcachePageFree(p);
27511 }
27512 pCache->pClean = 0;
27513 pCache->pDirty = 0;
27514 pCache->pDirtyTail = 0;
27515 pCache->nPage = 0;
27516 pCache->nPinned = 0;
27517 memset(pCache->apHash, 0, pCache->nHash*sizeof(pCache->apHash[0]));
27518 }
27519
27520
27521 /*
27522 ** Drop every cache entry whose page number is greater than "pgno".
27523 */
27524 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
27525 PgHdr *p, *pNext;
27526 PgHdr *pDirty = pCache->pDirty;
27527 pcacheEnterMutex();
27528 for(p=pCache->pClean; p||pDirty; p=pNext){
27529 if( !p ){
27530 p = pDirty;
27531 pDirty = 0;
27532 }
27533 pNext = p->pNext;
27534 if( p->pgno>pgno ){
27535 if( p->nRef==0 ){
27536 pcacheRemoveFromHash(p);
27537 if( p->flags&PGHDR_DIRTY ){
27538 pcacheRemoveFromList(&pCache->pDirty, p);
27539 pCache->nPinned--;
27540 }else{
27541 pcacheRemoveFromList(&pCache->pClean, p);
27542 pcacheRemoveFromLruList(p);
27543 }
27544 pcachePageFree(p);
27545 }else{
27546 /* If there are references to the page, it cannot be freed. In this
27547 ** case, zero the page content instead.
27548 */
27549 memset(p->pData, 0, pCache->szPage);
27550 }
27551 }
27552 }
27553 pcacheExitMutex();
27554 }
27555
27556 /*
27557 ** If there are currently more than pcache.nMaxPage pages allocated, try
27558 ** to recycle pages to reduce the number allocated to pcache.nMaxPage.
27559 */
27560 static void pcacheEnforceMaxPage(void){
27561 PgHdr *p;
27562 assert( sqlite3_mutex_held(pcache_g.mutex) );
27563 while( pcache_g.nCurrentPage>pcache_g.nMaxPage && (p = pcacheRecyclePage()) ){
27564 pcachePageFree(p);
27565 }
27566 }
27567
27568 /*
27569 ** Close a cache.
27570 */
27571 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
27572 pcacheEnterMutex();
27573
27574 /* Free all the pages used by this pager and remove them from the LRU list. */
27575 pcacheClear(pCache);
27576 if( pCache->bPurgeable ){
27577 pcache_g.nMaxPage -= pCache->nMax;
27578 pcache_g.nMinPage -= pCache->nMin;
27579 pcacheEnforceMaxPage();
27580 }
27581 sqlite3_free(pCache->apHash);
27582 pcacheExitMutex();
27583 }
27584
27585
27586 #ifndef NDEBUG
27587 /*
27588 ** Assert flags settings on all pages. Debugging only.
27589 */
27590 SQLITE_PRIVATE void sqlite3PcacheAssertFlags(PCache *pCache, int trueMask, int falseMask){
27591 PgHdr *p;
27592 for(p=pCache->pDirty; p; p=p->pNext){
27593 assert( (p->flags&trueMask)==trueMask );
27594 assert( (p->flags&falseMask)==0 );
27595 }
27596 for(p=pCache->pClean; p; p=p->pNext){
27597 assert( (p->flags&trueMask)==trueMask );
27598 assert( (p->flags&falseMask)==0 );
27599 }
27600 }
27601 #endif
27602
27603 /*
27604 ** Discard the contents of the cache.
27605 */
27606 SQLITE_PRIVATE int sqlite3PcacheClear(PCache *pCache){
27607 assert(pCache->nRef==0);
27608 pcacheEnterMutex();
27609 pcacheClear(pCache);
27610 pcacheExitMutex();
27611 return SQLITE_OK;
27612 }
27613
27614 /*
27615 ** Merge two lists of pages connected by pDirty and in pgno order.
27616 ** Do not both fixing the pPrevDirty pointers.
27617 */
27618 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
27619 PgHdr result, *pTail;
27620 pTail = &result;
27621 while( pA && pB ){
@@ -27639,11 +27870,11 @@
27639 return result.pDirty;
27640 }
27641
27642 /*
27643 ** Sort the list of pages in accending order by pgno. Pages are
27644 ** connected by pDirty pointers. The pPrevDirty pointers are
27645 ** corrupted by this sort.
27646 */
27647 #define N_SORT_BUCKET_ALLOC 25
27648 #define N_SORT_BUCKET 25
27649 #ifdef SQLITE_TEST
@@ -27688,101 +27919,762 @@
27688 /*
27689 ** Return a list of all dirty pages in the cache, sorted by page number.
27690 */
27691 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
27692 PgHdr *p;
27693 for(p=pCache->pDirty; p; p=p->pNext){
27694 p->pDirty = p->pNext;
27695 }
27696 return pcacheSortDirtyList(pCache->pDirty);
27697 }
27698
27699 /*
27700 ** Return the total number of outstanding page references.
27701 */
27702 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
27703 return pCache->nRef;
27704 }
27705
 
 
 
27706 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
27707 return p->nRef;
27708 }
27709
27710 /*
27711 ** Return the total number of pages in the cache.
27712 */
27713 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
27714 assert( pCache->nPage>=0 );
27715 return pCache->nPage;
27716 }
27717
27718 #ifdef SQLITE_CHECK_PAGES
27719 /*
27720 ** This function is used by the pager.c module to iterate through all
27721 ** pages in the cache. At present, this is only required if the
27722 ** SQLITE_CHECK_PAGES macro (used for debugging) is specified.
27723 */
27724 SQLITE_PRIVATE void sqlite3PcacheIterate(PCache *pCache, void (*xIter)(PgHdr *)){
27725 PgHdr *p;
27726 for(p=pCache->pClean; p; p=p->pNext){
27727 xIter(p);
27728 }
27729 for(p=pCache->pDirty; p; p=p->pNext){
27730 xIter(p);
27731 }
27732 }
27733 #endif
27734
27735 /*
27736 ** Set flags on all pages in the page cache
27737 */
27738 SQLITE_PRIVATE void sqlite3PcacheClearFlags(PCache *pCache, int mask){
27739 PgHdr *p;
27740
27741 /* Obtain the global mutex before modifying any PgHdr.flags variables
27742 ** or traversing the LRU list.
27743 */
27744 pcacheEnterMutex();
27745
27746 mask = ~mask;
27747 for(p=pCache->pDirty; p; p=p->pNext){
27748 p->flags &= mask;
27749 }
27750 for(p=pCache->pClean; p; p=p->pNext){
27751 p->flags &= mask;
27752 }
27753
27754 if( 0==(mask&PGHDR_NEED_SYNC) ){
27755 pCache->pSynced = pCache->pDirtyTail;
27756 assert( !pCache->pSynced || (pCache->pSynced->flags&PGHDR_NEED_SYNC)==0 );
27757 }
27758
27759 pcacheExitMutex();
27760 }
27761
27762 /*
27763 ** Set the suggested cache-size value.
27764 */
27765 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
27766 return pCache->nMax;
27767 }
 
27768
27769 /*
27770 ** Set the suggested cache-size value.
27771 */
27772 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
27773 if( mxPage<10 ){
27774 mxPage = 10;
27775 }
27776 if( pCache->bPurgeable ){
27777 pcacheEnterMutex();
27778 pcache_g.nMaxPage -= pCache->nMax;
27779 pcache_g.nMaxPage += mxPage;
27780 pcacheEnforceMaxPage();
27781 pcacheExitMutex();
27782 }
27783 pCache->nMax = mxPage;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27784 }
27785
27786 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
27787 /*
27788 ** This function is called to free superfluous dynamically allocated memory
@@ -27793,44 +28685,49 @@
27793 ** been released, the function returns. The return value is the total number
27794 ** of bytes of memory released.
27795 */
27796 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
27797 int nFree = 0;
27798 if( pcache_g.pStart==0 ){
27799 PgHdr *p;
27800 pcacheEnterMutex();
27801 while( (nReq<0 || nFree<nReq) && (p=pcacheRecyclePage()) ){
27802 nFree += pcachePageSize(p);
27803 pcachePageFree(p);
27804 }
27805 pcacheExitMutex();
 
 
27806 }
27807 return nFree;
27808 }
27809 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
27810
27811 #ifdef SQLITE_TEST
 
 
 
 
27812 SQLITE_PRIVATE void sqlite3PcacheStats(
27813 int *pnCurrent,
27814 int *pnMax,
27815 int *pnMin,
27816 int *pnRecyclable
27817 ){
27818 PgHdr *p;
27819 int nRecyclable = 0;
27820 for(p=pcache_g.pLruHead; p; p=p->pNextLru){
27821 nRecyclable++;
27822 }
27823
27824 *pnCurrent = pcache_g.nCurrentPage;
27825 *pnMax = pcache_g.nMaxPage;
27826 *pnMin = pcache_g.nMinPage;
27827 *pnRecyclable = nRecyclable;
27828 }
27829 #endif
27830
27831 /************** End of pcache.c **********************************************/
27832 /************** Begin file pager.c *******************************************/
27833 /*
27834 ** 2001 September 15
27835 **
27836 ** The author disclaims copyright to this source code. In place of
@@ -27848,11 +28745,11 @@
27848 ** is separate from the database file. The pager also implements file
27849 ** locking to prevent two processes from writing the same database
27850 ** file simultaneously, or one process from reading the database while
27851 ** another is writing.
27852 **
27853 ** @(#) $Id: pager.c,v 1.502 2008/11/07 00:24:54 drh Exp $
27854 */
27855 #ifndef SQLITE_OMIT_DISKIO
27856
27857 /*
27858 ** Macros for troubleshooting. Normally turned off
@@ -28005,15 +28902,16 @@
28005 u8 doNotSync; /* Boolean. While true, do not spill the cache */
28006 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
28007 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
28008 u8 dbModified; /* True if there are any changes to the Db */
28009 u8 changeCountDone; /* Set after incrementing the change-counter */
 
28010 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
28011 int errCode; /* One of several kinds of errors */
28012 int dbSize; /* Number of pages in the file */
28013 int origDbSize; /* dbSize before the current change */
28014 int stmtSize; /* Size of database (in pages) at stmt_begin() */
28015 int nRec; /* Number of pages written to the journal */
28016 u32 cksumInit; /* Quasi-random value added to every checksum */
28017 int stmtNRec; /* Number of records in stmt subjournal */
28018 int nExtra; /* Add this many bytes to each in-memory page */
28019 int pageSize; /* Number of bytes in a page */
@@ -28026,11 +28924,12 @@
28026 char *zFilename; /* Name of the database file */
28027 char *zJournal; /* Name of the journal file */
28028 char *zDirectory; /* Directory hold database and journal files */
28029 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
28030 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
28031 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
 
28032 i64 journalOff; /* Current byte offset in the journal file */
28033 i64 journalHdr; /* Byte offset to previous journal header */
28034 i64 stmtHdrOff; /* First journal header written this statement */
28035 i64 stmtCksum; /* cksumInit when statement was started */
28036 i64 stmtJSize; /* Size of journal at stmt_begin() */
@@ -28124,11 +29023,11 @@
28124 ** used in the journal to signify that the remainder of the journal file
28125 ** is devoted to storing a master journal name - there are no more pages to
28126 ** roll back. See comments for function writeMasterJournal() for details.
28127 */
28128 /* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
28129 #define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
28130
28131 /*
28132 ** The maximum legal page number is (2^31 - 1).
28133 */
28134 #define PAGER_MAX_PGNO 2147483647
@@ -28140,10 +29039,14 @@
28140 */
28141 static int pageInStatement(PgHdr *pPg){
28142 Pager *pPager = pPg->pPager;
28143 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
28144 }
 
 
 
 
28145
28146 /*
28147 ** Read a 32-bit integer from the given file descriptor. Store the integer
28148 ** that is read in *pRes. Return SQLITE_OK if everything worked, or an
28149 ** error code is something goes wrong.
@@ -28283,11 +29186,11 @@
28283 return hash;
28284 }
28285 static u32 pager_pagehash(PgHdr *pPage){
28286 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
28287 }
28288 static u32 pager_set_pagehash(PgHdr *pPage){
28289 pPage->pageHash = pager_pagehash(pPage);
28290 }
28291
28292 /*
28293 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
@@ -28320,11 +29223,11 @@
28320 ** were present in the journal.
28321 **
28322 ** If no master journal file name is present zMaster[0] is set to 0 and
28323 ** SQLITE_OK returned.
28324 */
28325 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
28326 int rc;
28327 u32 len;
28328 i64 szJ;
28329 u32 cksum;
28330 u32 u; /* Unsigned loop counter */
@@ -28452,12 +29355,12 @@
28452 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
28453 */
28454 static int writeJournalHdr(Pager *pPager){
28455 int rc = SQLITE_OK;
28456 char *zHeader = pPager->pTmpSpace;
28457 int nHeader = pPager->pageSize;
28458 int nWrite;
28459
28460 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
28461 nHeader = JOURNAL_HDR_SZ(pPager);
28462 }
28463
@@ -28712,11 +29615,11 @@
28712 */
28713 static void pager_unlock(Pager *pPager){
28714 if( !pPager->exclusiveMode ){
28715 int rc = osUnlock(pPager->fd, NO_LOCK);
28716 if( rc ) pPager->errCode = rc;
28717 pPager->dbSize = -1;
28718 IOTRACE(("UNLOCK %p\n", pPager))
28719
28720 /* Always close the journal file when dropping the database lock.
28721 ** Otherwise, another connection with journal_mode=delete might
28722 ** delete the file out from under us.
@@ -28826,17 +29729,14 @@
28826 }
28827 sqlite3BitvecDestroy(pPager->pInJournal);
28828 pPager->pInJournal = 0;
28829 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
28830 pPager->pAlwaysRollback = 0;
28831 sqlite3PcacheCleanAll(pPager->pPCache);
28832 #ifdef SQLITE_CHECK_PAGES
28833 sqlite3PcacheIterate(pPager->pPCache, pager_set_pagehash);
28834 #endif
28835 sqlite3PcacheClearFlags(pPager->pPCache,
28836 PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC
28837 );
28838 pPager->dirtyCache = 0;
28839 pPager->nRec = 0;
28840 }else{
28841 assert( pPager->pInJournal==0 );
28842 }
@@ -28850,11 +29750,11 @@
28850 pPager->origDbSize = 0;
28851 pPager->setMaster = 0;
28852 pPager->needSync = 0;
28853 /* lruListSetFirstSynced(pPager); */
28854 if( !MEMDB ){
28855 pPager->dbSize = -1;
28856 }
28857 pPager->dbModified = 0;
28858
28859 return (rc==SQLITE_OK?rc2:rc);
28860 }
@@ -29138,11 +30038,11 @@
29138 ** operating system implementations can get confused if you try to
29139 ** truncate a file to some size that is larger than it currently is,
29140 ** so detect this case and write a single zero byte to the end of the new
29141 ** file instead.
29142 */
29143 static int pager_truncate(Pager *pPager, int nPage){
29144 int rc = SQLITE_OK;
29145 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
29146 i64 currentSize, newSize;
29147 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
29148 newSize = pPager->pageSize*(i64)nPage;
@@ -29647,11 +30547,11 @@
29647 }
29648
29649 /* Open the pager file.
29650 */
29651 if( zFilename && zFilename[0] && !memDb ){
29652 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
29653 rc = SQLITE_CANTOPEN;
29654 }else{
29655 int fout = 0;
29656 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
29657 pPager->vfsFlags, &fout);
@@ -29738,11 +30638,11 @@
29738 pPager->useJournal = useJournal;
29739 pPager->noReadlock = noReadlock && readOnly;
29740 /* pPager->stmtOpen = 0; */
29741 /* pPager->stmtInUse = 0; */
29742 /* pPager->nRef = 0; */
29743 pPager->dbSize = memDb-1;
29744 pPager->pageSize = szPageDflt;
29745 /* pPager->stmtSize = 0; */
29746 /* pPager->stmtJSize = 0; */
29747 /* pPager->nPage = 0; */
29748 pPager->mxPage = 100;
@@ -29769,21 +30669,27 @@
29769 assert(pPager->fd->pMethods||tempFile);
29770 setSectorSize(pPager);
29771 if( memDb ){
29772 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
29773 }
29774 /* pPager->pBusyHandler = 0; */
 
29775 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
29776 *ppPager = pPager;
29777 return SQLITE_OK;
29778 }
29779
29780 /*
29781 ** Set the busy handler function.
29782 */
29783 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
29784 pPager->pBusyHandler = pBusyHandler;
 
 
 
 
 
29785 }
29786
29787 /*
29788 ** Set the reinitializer for this pager. If not NULL, the reinitializer
29789 ** is called when the content of a page in cache is restored to its original
@@ -29916,11 +30822,11 @@
29916 assert( pPager!=0 );
29917 if( pPager->errCode ){
29918 rc = pPager->errCode;
29919 return rc;
29920 }
29921 if( pPager->dbSize>=0 ){
29922 n = pPager->dbSize;
29923 } else {
29924 assert(pPager->fd->pMethods||pPager->tempFile);
29925 if( (pPager->fd->pMethods)
29926 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
@@ -29932,10 +30838,11 @@
29932 }else{
29933 n /= pPager->pageSize;
29934 }
29935 if( pPager->state!=PAGER_UNLOCK ){
29936 pPager->dbSize = n;
 
29937 }
29938 }
29939 if( n==(PENDING_BYTE/pPager->pageSize) ){
29940 n++;
29941 }
@@ -29984,19 +30891,18 @@
29984 assert( PAGER_SHARED==SHARED_LOCK );
29985 assert( PAGER_RESERVED==RESERVED_LOCK );
29986 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
29987
29988 /* If the file is currently unlocked then the size must be unknown */
29989 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 );
29990
29991 if( pPager->state>=locktype ){
29992 rc = SQLITE_OK;
29993 }else{
29994 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
29995 do {
29996 rc = sqlite3OsLock(pPager->fd, locktype);
29997 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
29998 if( rc==SQLITE_OK ){
29999 pPager->state = locktype;
30000 IOTRACE(("LOCK %p %d\n", pPager, locktype))
30001 }
30002 }
@@ -30011,11 +30917,11 @@
30011 assert( pPager->state>=PAGER_SHARED );
30012
30013 sqlite3PagerPagecount(pPager, 0);
30014 if( pPager->errCode ){
30015 rc = pPager->errCode;
30016 }else if( nPage<(unsigned)pPager->dbSize ){
30017 rc = syncJournal(pPager);
30018 if( rc==SQLITE_OK ){
30019 /* Get an exclusive lock on the database before truncating. */
30020 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
30021 }
@@ -30168,22 +31074,12 @@
30168 }
30169 pPager->needSync = 0;
30170
30171 /* Erase the needSync flag from every page.
30172 */
30173 sqlite3PcacheClearFlags(pPager->pPCache, PGHDR_NEED_SYNC);
30174 }
30175
30176 #ifndef NDEBUG
30177 /* If the Pager.needSync flag is clear then the PgHdr.needSync
30178 ** flag must also be clear for all pages. Verify that this
30179 ** invariant is true.
30180 */
30181 else{
30182 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_NEED_SYNC);
30183 }
30184 #endif
30185
30186 return rc;
30187 }
30188
30189 /*
@@ -30531,10 +31427,11 @@
30531 if( pPager->errCode ){
30532 rc = pPager->errCode;
30533 goto failed;
30534 }
30535
 
30536 if( pPager->dbSize>0 ){
30537 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
30538 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
30539 if( rc!=SQLITE_OK ){
30540 goto failed;
@@ -30681,13 +31578,10 @@
30681 ** be initialized.
30682 */
30683 int nMax;
30684 PAGER_INCR(pPager->nMiss);
30685 pPg->pPager = pPager;
30686 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
30687 pPg->flags |= PGHDR_IN_JOURNAL;
30688 }
30689 memset(pPg->pExtra, 0, pPager->nExtra);
30690
30691 rc = sqlite3PagerPagecount(pPager, &nMax);
30692 if( rc!=SQLITE_OK ){
30693 sqlite3PagerUnref(pPg);
@@ -30887,11 +31781,10 @@
30887 assert( pPg->nRef>0 );
30888 assert( pPager->state!=PAGER_UNLOCK );
30889 if( pPager->state==PAGER_SHARED ){
30890 assert( pPager->pInJournal==0 );
30891 assert( !MEMDB );
30892 sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_IN_JOURNAL);
30893 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
30894 if( rc==SQLITE_OK ){
30895 pPager->state = PAGER_RESERVED;
30896 if( exFlag ){
30897 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
@@ -30996,13 +31889,11 @@
30996
30997 /* Mark the page as dirty. If the page has already been written
30998 ** to the journal then we can return right away.
30999 */
31000 makeDirty(pPg);
31001 if( (pPg->flags&PGHDR_IN_JOURNAL)
31002 && (pageInStatement(pPg) || pPager->stmtInUse==0)
31003 ){
31004 pPager->dirtyCache = 1;
31005 pPager->dbModified = 1;
31006 }else{
31007
31008 /* If we get this far, it means that the page needs to be
@@ -31028,12 +31919,12 @@
31028
31029 /* The transaction journal now exists and we have a RESERVED or an
31030 ** EXCLUSIVE lock on the main database file. Write the current page to
31031 ** the transaction journal if it is not there already.
31032 */
31033 if( !(pPg->flags&PGHDR_IN_JOURNAL) && pPager->journalOpen ){
31034 if( (int)pPg->pgno <= pPager->origDbSize ){
31035 u32 cksum;
31036 char *pData2;
31037
31038 /* We should never write to the journal file the page that
31039 ** contains the database locks. The following assert verifies
@@ -31083,26 +31974,24 @@
31083 ((pPg->flags&PGHDR_NEED_SYNC)?1:0));
31084 }
31085 if( pPg->flags&PGHDR_NEED_SYNC ){
31086 pPager->needSync = 1;
31087 }
31088 pPg->flags |= PGHDR_IN_JOURNAL;
31089 }
31090
31091 /* If the statement journal is open and the page is not in it,
31092 ** then write the current page to the statement journal. Note that
31093 ** the statement journal format differs from the standard journal format
31094 ** in that it omits the checksums and the header.
31095 */
31096 if( pPager->stmtInUse
31097 && !pageInStatement(pPg)
31098 && (int)pPg->pgno<=pPager->stmtSize
31099 ){
31100 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
31101 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
31102 assert( (pPg->flags&PGHDR_IN_JOURNAL)
31103 || (int)pPg->pgno>pPager->origDbSize );
31104 rc = write32bits(pPager->stfd, offset, pPg->pgno);
31105 if( rc==SQLITE_OK ){
31106 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
31107 }
31108 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
@@ -31116,13 +32005,13 @@
31116 }
31117
31118 /* Update the database size and return.
31119 */
31120 assert( pPager->state>=PAGER_SHARED );
31121 if( pPager->dbSize<(int)pPg->pgno ){
31122 pPager->dbSize = pPg->pgno;
31123 if( pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
31124 pPager->dbSize++;
31125 }
31126 }
31127 return rc;
31128 }
@@ -31276,11 +32165,11 @@
31276 }
31277 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
31278
31279 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){
31280 assert( pPager->state>=PAGER_SHARED );
31281 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
31282 /* If this pages is the last page in the file and the file has grown
31283 ** during the current transaction, then do NOT mark the page as clean.
31284 ** When the database file grows, we must make sure that the last page
31285 ** gets written at least once so that the disk file will be the correct
31286 ** size. If you do not write this page and the size of the file
@@ -31325,11 +32214,12 @@
31325 ){
31326 return;
31327 }
31328
31329 #ifdef SQLITE_SECURE_DELETE
31330 if( (pPg->flags & PGHDR_IN_JOURNAL)!=0 || (int)pPg->pgno>pPager->origDbSize ){
 
31331 return;
31332 }
31333 #endif
31334
31335 /* If SECURE_DELETE is disabled, then there is no way that this
@@ -31344,11 +32234,10 @@
31344 */
31345 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
31346
31347 assert( pPager->pInJournal!=0 );
31348 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
31349 pPg->flags |= PGHDR_IN_JOURNAL;
31350 pPg->flags &= ~PGHDR_NEED_READ;
31351 if( pPager->stmtInUse ){
31352 assert( pPager->stmtSize >= pPager->origDbSize );
31353 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
31354 }
@@ -31525,11 +32414,11 @@
31525 /* If this transaction has made the database smaller, then all pages
31526 ** being discarded by the truncation must be written to the journal
31527 ** file.
31528 */
31529 Pgno i;
31530 int iSkip = PAGER_MJ_PGNO(pPager);
31531 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
31532 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
31533 rc = sqlite3PagerGet(pPager, i, &pPg);
31534 if( rc!=SQLITE_OK ) goto sync_exit;
31535 rc = sqlite3PagerWrite(pPg);
@@ -31655,11 +32544,11 @@
31655 }else{
31656 rc = pager_playback(pPager, 0);
31657 }
31658
31659 if( !MEMDB ){
31660 pPager->dbSize = -1;
31661 }
31662
31663 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
31664 ** cache. So call pager_error() on the way out to make any error
31665 ** persistent.
@@ -31698,11 +32587,11 @@
31698 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
31699 static int a[11];
31700 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
31701 a[1] = sqlite3PcachePagecount(pPager->pPCache);
31702 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
31703 a[3] = pPager->dbSize;
31704 a[4] = pPager->state;
31705 a[5] = pPager->errCode;
31706 a[6] = pPager->nHit;
31707 a[7] = pPager->nMiss;
31708 a[8] = 0; /* Used to be pPager->nOvfl */
@@ -31724,11 +32613,11 @@
31724 */
31725 static int pagerStmtBegin(Pager *pPager){
31726 int rc;
31727 assert( !pPager->stmtInUse );
31728 assert( pPager->state>=PAGER_SHARED );
31729 assert( pPager->dbSize>=0 );
31730 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
31731 if( !pPager->journalOpen ){
31732 pPager->stmtAutoopen = 1;
31733 return SQLITE_OK;
31734 }
@@ -31906,34 +32795,30 @@
31906 ** the journal needs to be sync()ed before database page pPg->pgno
31907 ** can be written to. The caller has already promised not to write to it.
31908 */
31909 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
31910 needSyncPgno = pPg->pgno;
31911 assert( (pPg->flags&PGHDR_IN_JOURNAL) || (int)pgno>pPager->origDbSize );
31912 assert( pPg->flags&PGHDR_DIRTY );
31913 assert( pPager->needSync );
31914 }
31915
31916 /* If the cache contains a page with page-number pgno, remove it
31917 ** from its hash chain. Also, if the PgHdr.needSync was set for
31918 ** page pgno before the 'move' operation, it needs to be retained
31919 ** for the page moved there.
31920 */
31921 pPg->flags &= ~(PGHDR_NEED_SYNC|PGHDR_IN_JOURNAL);
31922 pPgOld = pager_lookup(pPager, pgno);
31923 assert( !pPgOld || pPgOld->nRef==1 );
31924 if( pPgOld ){
31925 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
31926 }
31927 if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){
31928 pPg->flags |= PGHDR_IN_JOURNAL;
31929 }
31930
31931 sqlite3PcacheMove(pPg, pgno);
31932 if( pPgOld ){
31933 sqlite3PcacheMove(pPgOld, 0);
31934 sqlite3PcacheRelease(pPgOld);
31935 }
31936
31937 makeDirty(pPg);
31938 pPager->dirtyCache = 1;
31939 pPager->dbModified = 1;
@@ -31959,19 +32844,18 @@
31959 int rc;
31960 PgHdr *pPgHdr;
31961 assert( pPager->needSync );
31962 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
31963 if( rc!=SQLITE_OK ){
31964 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
31965 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
31966 }
31967 return rc;
31968 }
31969 pPager->needSync = 1;
31970 assert( pPager->noSync==0 && !MEMDB );
31971 pPgHdr->flags |= PGHDR_NEED_SYNC;
31972 pPgHdr->flags |= PGHDR_IN_JOURNAL;
31973 makeDirty(pPgHdr);
31974 sqlite3PagerUnref(pPgHdr);
31975 }
31976
31977 return SQLITE_OK;
@@ -32074,11 +32958,11 @@
32074 ** May you find forgiveness for yourself and forgive others.
32075 ** May you share freely, never taking more than you give.
32076 **
32077 *************************************************************************
32078 **
32079 ** $Id: btmutex.c,v 1.11 2008/10/07 15:25:48 drh Exp $
32080 **
32081 ** This file contains code used to implement mutexes on Btree objects.
32082 ** This code really belongs in btree.c. But btree.c is getting too
32083 ** big and we want to break it down some. This packaged seemed like
32084 ** a good breakout.
@@ -32094,11 +32978,11 @@
32094 ** May you do good and not evil.
32095 ** May you find forgiveness for yourself and forgive others.
32096 ** May you share freely, never taking more than you give.
32097 **
32098 *************************************************************************
32099 ** $Id: btreeInt.h,v 1.34 2008/09/30 17:18:17 drh Exp $
32100 **
32101 ** This file implements a external (disk-based) database using BTrees.
32102 ** For a detailed discussion of BTrees, refer to
32103 **
32104 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -32464,11 +33348,10 @@
32464 u8 inTransaction; /* Transaction state */
32465 int nTransaction; /* Number of open transactions (read + write) */
32466 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
32467 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
32468 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
32469 BusyHandler busyHdr; /* The busy handler for this btree */
32470 #ifndef SQLITE_OMIT_SHARED_CACHE
32471 int nRef; /* Number of references to this structure */
32472 BtShared *pNext; /* Next on a list of sharable BtShared structs */
32473 BtLock *pLock; /* List of locks held on this shared-btree struct */
32474 Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */
@@ -32582,11 +33465,11 @@
32582 ** in memory) then there is no pending byte.
32583 */
32584 #ifdef SQLITE_OMIT_DISKIO
32585 # define PENDING_BYTE_PAGE(pBt) 0x7fffffff
32586 #else
32587 # define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
32588 #endif
32589
32590 /*
32591 ** A linked list of the following structures is stored at BtShared.pLock.
32592 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
@@ -32689,11 +33572,11 @@
32689 */
32690 typedef struct IntegrityCk IntegrityCk;
32691 struct IntegrityCk {
32692 BtShared *pBt; /* The tree being checked out */
32693 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
32694 int nPage; /* Number of pages in the database */
32695 int *anRef; /* Number of times each page is referenced */
32696 int mxErr; /* Stop accumulating errors when this reaches zero */
32697 int nErr; /* Number of messages written to zErrMsg so far */
32698 int mallocFailed; /* A memory allocation error has occurred */
32699 StrAccum errMsg; /* Accumulate the error message text here */
@@ -32949,11 +33832,11 @@
32949 assert( pArray->aBtree[i]!=pBtree );
32950 }
32951 }
32952 #endif
32953 assert( pArray->nMutex>=0 );
32954 assert( pArray->nMutex<sizeof(pArray->aBtree)/sizeof(pArray->aBtree[0])-1 );
32955 pBt = pBtree->pBt;
32956 for(i=0; i<pArray->nMutex; i++){
32957 assert( pArray->aBtree[i]!=pBtree );
32958 if( pArray->aBtree[i]->pBt>pBt ){
32959 for(j=pArray->nMutex; j>i; j--){
@@ -33028,11 +33911,11 @@
33028 ** May you do good and not evil.
33029 ** May you find forgiveness for yourself and forgive others.
33030 ** May you share freely, never taking more than you give.
33031 **
33032 *************************************************************************
33033 ** $Id: btree.c,v 1.527 2008/11/03 20:55:07 drh Exp $
33034 **
33035 ** This file implements a external (disk-based) database using BTrees.
33036 ** See the header comment on "btreeInt.h" for additional information.
33037 ** Including a description of file format and an overview of operation.
33038 */
@@ -33453,11 +34336,12 @@
33453 ** Given a page number of a regular database page, return the page
33454 ** number for the pointer-map page that contains the entry for the
33455 ** input page number.
33456 */
33457 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
33458 int nPagesPerMapPage, iPtrMap, ret;
 
33459 assert( sqlite3_mutex_held(pBt->mutex) );
33460 nPagesPerMapPage = (pBt->usableSize/5)+1;
33461 iPtrMap = (pgno-2)/nPagesPerMapPage;
33462 ret = (iPtrMap*nPagesPerMapPage) + 2;
33463 if( ret==PENDING_BYTE_PAGE(pBt) ){
@@ -33720,11 +34604,11 @@
33720 ** Defragment the page given. All Cells are moved to the
33721 ** end of the page and all free space is collected into one
33722 ** big FreeBlk that occurs in between the header and cell
33723 ** pointer array and the cell content area.
33724 */
33725 static void defragmentPage(MemPage *pPage){
33726 int i; /* Loop counter */
33727 int pc; /* Address of a i-th cell */
33728 int addr; /* Offset of first byte after cell pointer array */
33729 int hdr; /* Offset to the page header */
33730 int size; /* Size of a cell */
@@ -33752,13 +34636,19 @@
33752 cbrk = usableSize;
33753 for(i=0; i<nCell; i++){
33754 u8 *pAddr; /* The i-th cell pointer */
33755 pAddr = &data[cellOffset + i*2];
33756 pc = get2byte(pAddr);
33757 assert( pc<pPage->pBt->usableSize );
 
 
33758 size = cellSizePtr(pPage, &temp[pc]);
33759 cbrk -= size;
 
 
 
 
33760 memcpy(&data[cbrk], &temp[pc], size);
33761 put2byte(pAddr, cbrk);
33762 }
33763 assert( cbrk>=cellOffset+2*nCell );
33764 put2byte(&data[hdr+5], cbrk);
@@ -33765,10 +34655,14 @@
33765 data[hdr+1] = 0;
33766 data[hdr+2] = 0;
33767 data[hdr+7] = 0;
33768 addr = cellOffset+2*nCell;
33769 memset(&data[addr], 0, cbrk-addr);
 
 
 
 
33770 }
33771
33772 /*
33773 ** Allocate nByte bytes of space on a page.
33774 **
@@ -33843,11 +34737,11 @@
33843 ** and the size of the block is "size" bytes.
33844 **
33845 ** Most of the effort here is involved in coalesing adjacent
33846 ** free blocks into a single big free block.
33847 */
33848 static void freeSpace(MemPage *pPage, int start, int size){
33849 int addr, pbegin, hdr;
33850 unsigned char *data = pPage->aData;
33851
33852 assert( pPage->pBt!=0 );
33853 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -33865,14 +34759,18 @@
33865 /* Add the space back into the linked list of freeblocks */
33866 hdr = pPage->hdrOffset;
33867 addr = hdr + 1;
33868 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
33869 assert( pbegin<=pPage->pBt->usableSize-4 );
33870 assert( pbegin>addr );
 
 
33871 addr = pbegin;
33872 }
33873 assert( pbegin<=pPage->pBt->usableSize-4 );
 
 
33874 assert( pbegin>addr || pbegin==0 );
33875 put2byte(&data[addr], start);
33876 put2byte(&data[start], pbegin);
33877 put2byte(&data[start+2], size);
33878 pPage->nFree += size;
@@ -33885,11 +34783,13 @@
33885 assert( pbegin<=pPage->pBt->usableSize-4 );
33886 pnext = get2byte(&data[pbegin]);
33887 psize = get2byte(&data[pbegin+2]);
33888 if( pbegin + psize + 3 >= pnext && pnext>0 ){
33889 int frag = pnext - (pbegin+psize);
33890 assert( frag<=data[pPage->hdrOffset+7] );
 
 
33891 data[pPage->hdrOffset+7] -= frag;
33892 put2byte(&data[pbegin], get2byte(&data[pnext]));
33893 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
33894 }else{
33895 addr = pbegin;
@@ -33902,10 +34802,11 @@
33902 pbegin = get2byte(&data[hdr+1]);
33903 memcpy(&data[hdr+1], &data[pbegin], 2);
33904 top = get2byte(&data[hdr+5]);
33905 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
33906 }
 
33907 }
33908
33909 /*
33910 ** Decode the flags byte (the first byte of the header) for a page
33911 ** and initialize fields of the MemPage structure accordingly.
@@ -34109,18 +35010,20 @@
34109 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
34110 return SQLITE_OK;
34111 }
34112
34113 /*
34114 ** Return the size of the database file in pages. Or return -1 if
34115 ** there is any kind of error.
34116 */
34117 static int pagerPagecount(Pager *pPager){
 
34118 int rc;
34119 int nPage;
34120 rc = sqlite3PagerPagecount(pPager, &nPage);
34121 return (rc==SQLITE_OK?nPage:-1);
 
34122 }
34123
34124 /*
34125 ** Get a page from the pager and initialize it. This routine
34126 ** is just a convenience wrapper around separate calls to
@@ -34150,11 +35053,11 @@
34150 /* Page is already in cache */
34151 *ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt);
34152 rc = SQLITE_OK;
34153 }else{
34154 /* Page not in cache. Acquire it. */
34155 if( pgno>pagerPagecount(pBt->pPager) ){
34156 return SQLITE_CORRUPT_BKPT;
34157 }
34158 rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
34159 if( rc ) return rc;
34160 pPage = *ppPage;
@@ -34205,11 +35108,11 @@
34205 }
34206
34207 /*
34208 ** Invoke the busy handler for a btree.
34209 */
34210 static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){
34211 BtShared *pBt = (BtShared*)pArg;
34212 assert( pBt->db );
34213 assert( sqlite3_mutex_held(pBt->db->mutex) );
34214 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
34215 }
@@ -34322,21 +35225,19 @@
34322 pBt = sqlite3MallocZero( sizeof(*pBt) );
34323 if( pBt==0 ){
34324 rc = SQLITE_NOMEM;
34325 goto btree_open_out;
34326 }
34327 pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler;
34328 pBt->busyHdr.pArg = pBt;
34329 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
34330 EXTRA_SIZE, flags, vfsFlags);
34331 if( rc==SQLITE_OK ){
34332 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
34333 }
34334 if( rc!=SQLITE_OK ){
34335 goto btree_open_out;
34336 }
34337 sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr);
34338 p->pBt = pBt;
34339
34340 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
34341 pBt->pCursor = 0;
34342 pBt->pPage1 = 0;
@@ -35030,11 +35931,11 @@
35030 if( wrflag ) pBt->inStmt = 0;
35031 }else{
35032 unlockBtreeIfUnused(pBt);
35033 }
35034 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
35035 sqlite3BtreeInvokeBusyHandler(pBt, 0) );
35036
35037 if( rc==SQLITE_OK ){
35038 if( p->inTrans==TRANS_NONE ){
35039 pBt->nTransaction++;
35040 }
@@ -35054,11 +35955,10 @@
35054 trans_begun:
35055 btreeIntegrity(p);
35056 sqlite3BtreeLeave(p);
35057 return rc;
35058 }
35059
35060
35061 #ifndef SQLITE_OMIT_AUTOVACUUM
35062
35063 /*
35064 ** Set the pointer-map entries for all children of page pPage. Also, if
@@ -35269,11 +36169,11 @@
35269 Pgno nFreeList; /* Number of pages still on the free-list */
35270
35271 assert( sqlite3_mutex_held(pBt->mutex) );
35272 iLastPg = pBt->nTrunc;
35273 if( iLastPg==0 ){
35274 iLastPg = pagerPagecount(pBt->pPager);
35275 }
35276
35277 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
35278 int rc;
35279 u8 eType;
@@ -35400,11 +36300,11 @@
35400
35401 if( pBt->nTrunc==0 ){
35402 Pgno nFree;
35403 Pgno nPtrmap;
35404 const int pgsz = pBt->pageSize;
35405 int nOrig = pagerPagecount(pBt->pPager);
35406
35407 if( PTRMAP_ISPAGE(pBt, nOrig) ){
35408 return SQLITE_CORRUPT_BKPT;
35409 }
35410 if( nOrig==PENDING_BYTE_PAGE(pBt) ){
@@ -35445,11 +36345,11 @@
35445 }
35446 assert( nRef==sqlite3PagerRefcount(pPager) );
35447 return rc;
35448 }
35449
35450 #endif
35451
35452 /*
35453 ** This routine does the first phase of a two-phase commit. This routine
35454 ** causes a rollback journal to be created (if it does not already exist)
35455 ** and populated with enough information so that if a power loss occurs
@@ -35612,13 +36512,18 @@
35612 */
35613 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
35614 BtCursor *p;
35615 sqlite3BtreeEnter(pBtree);
35616 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
 
35617 sqlite3BtreeClearCursor(p);
35618 p->eState = CURSOR_FAULT;
35619 p->skip = errCode;
 
 
 
 
35620 }
35621 sqlite3BtreeLeave(pBtree);
35622 }
35623
35624 /*
@@ -35801,10 +36706,11 @@
35801 int wrFlag, /* 1 to write. 0 read-only */
35802 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
35803 BtCursor *pCur /* Space for new cursor */
35804 ){
35805 int rc;
 
35806 BtShared *pBt = p->pBt;
35807
35808 assert( sqlite3BtreeHoldsMutex(p) );
35809 if( wrFlag ){
35810 if( pBt->readOnly ){
@@ -35823,11 +36729,15 @@
35823 if( pBt->readOnly && wrFlag ){
35824 return SQLITE_READONLY;
35825 }
35826 }
35827 pCur->pgnoRoot = (Pgno)iTable;
35828 if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){
 
 
 
 
35829 rc = SQLITE_EMPTY;
35830 goto create_cursor_exception;
35831 }
35832 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
35833 if( rc!=SQLITE_OK ){
@@ -36084,11 +36994,11 @@
36084
36085 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
36086 iGuess++;
36087 }
36088
36089 if( iGuess<=pagerPagecount(pBt->pPager) ){
36090 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
36091 if( rc!=SQLITE_OK ){
36092 return rc;
36093 }
36094 if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
@@ -36180,37 +37090,38 @@
36180 ** * A commit in auto_vacuum="full" mode,
36181 ** * Creating a table (may require moving an overflow page).
36182 */
36183 static int accessPayload(
36184 BtCursor *pCur, /* Cursor pointing to entry to read from */
36185 int offset, /* Begin reading this far into payload */
36186 int amt, /* Read this many bytes */
36187 unsigned char *pBuf, /* Write the bytes into this buffer */
36188 int skipKey, /* offset begins at data if this is true */
36189 int eOp /* zero to read. non-zero to write. */
36190 ){
36191 unsigned char *aPayload;
36192 int rc = SQLITE_OK;
36193 u32 nKey;
36194 int iIdx = 0;
36195 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
36196 BtShared *pBt; /* Btree this cursor belongs to */
36197
36198 assert( pPage );
36199 assert( pCur->eState==CURSOR_VALID );
36200 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
36201 assert( offset>=0 );
36202 assert( cursorHoldsMutex(pCur) );
36203
36204 getCellInfo(pCur);
36205 aPayload = pCur->info.pCell + pCur->info.nHeader;
36206 nKey = (pPage->intKey ? 0 : pCur->info.nKey);
36207
36208 if( skipKey ){
36209 offset += nKey;
36210 }
36211 if( offset+amt > nKey+pCur->info.nData ){
 
 
36212 /* Trying to read or write past the end of the data is an error */
36213 return SQLITE_CORRUPT_BKPT;
36214 }
36215
36216 /* Check if data must be read/written to/from the btree page itself. */
@@ -36225,13 +37136,12 @@
36225 amt -= a;
36226 }else{
36227 offset -= pCur->info.nLocal;
36228 }
36229
36230 pBt = pCur->pBt;
36231 if( rc==SQLITE_OK && amt>0 ){
36232 const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
36233 Pgno nextPage;
36234
36235 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
36236
36237 #ifndef SQLITE_OMIT_INCRBLOB
@@ -36394,11 +37304,11 @@
36394 int skipKey /* read beginning at data if this is true */
36395 ){
36396 unsigned char *aPayload;
36397 MemPage *pPage;
36398 u32 nKey;
36399 int nLocal;
36400
36401 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
36402 assert( pCur->eState==CURSOR_VALID );
36403 assert( cursorHoldsMutex(pCur) );
36404 pPage = pCur->apPage[pCur->iPage];
@@ -37084,11 +37994,11 @@
37084 /* If the 'exact' parameter was true and a query of the pointer-map
37085 ** shows that the page 'nearby' is somewhere on the free-list, then
37086 ** the entire-list will be searched for that page.
37087 */
37088 #ifndef SQLITE_OMIT_AUTOVACUUM
37089 if( exact && nearby<=pagerPagecount(pBt->pPager) ){
37090 u8 eType;
37091 assert( nearby>0 );
37092 assert( pBt->autoVacuum );
37093 rc = ptrmapGet(pBt, nearby, &eType, 0);
37094 if( rc ) return rc;
@@ -37219,13 +38129,13 @@
37219 closest = 0;
37220 }
37221
37222 iPage = get4byte(&aData[8+closest*4]);
37223 if( !searchList || iPage==nearby ){
37224 int nPage;
37225 *pPgno = iPage;
37226 nPage = pagerPagecount(pBt->pPager);
37227 if( *pPgno>nPage ){
37228 /* Free page off the end of the file */
37229 rc = SQLITE_CORRUPT_BKPT;
37230 goto end_allocate_page;
37231 }
@@ -37251,11 +38161,11 @@
37251 pPrevTrunk = 0;
37252 }while( searchList );
37253 }else{
37254 /* There are no pages on the freelist, so create a new page at the
37255 ** end of the file */
37256 int nPage = pagerPagecount(pBt->pPager);
37257 *pPgno = nPage + 1;
37258
37259 #ifndef SQLITE_OMIT_AUTOVACUUM
37260 if( pBt->nTrunc ){
37261 /* An incr-vacuum has already run within this transaction. So the
@@ -37295,13 +38205,16 @@
37295 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
37296
37297 end_allocate_page:
37298 releasePage(pTrunk);
37299 releasePage(pPrevTrunk);
37300 if( rc==SQLITE_OK && sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
37301 releasePage(*ppPage);
37302 return SQLITE_CORRUPT_BKPT;
 
 
 
37303 }
37304 return rc;
37305 }
37306
37307 /*
@@ -37418,11 +38331,11 @@
37418 ovflPageSize = pBt->usableSize - 4;
37419 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
37420 assert( ovflPgno==0 || nOvfl>0 );
37421 while( nOvfl-- ){
37422 MemPage *pOvfl;
37423 if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){
37424 return SQLITE_CORRUPT_BKPT;
37425 }
37426
37427 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
37428 if( rc ) return rc;
@@ -37480,11 +38393,11 @@
37480 }
37481 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
37482 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
37483 assert( info.nHeader==nHeader );
37484 assert( info.nKey==nKey );
37485 assert( info.nData==nData+nZero );
37486
37487 /* Fill in the payload */
37488 nPayload = nData + nZero;
37489 if( pPage->intKey ){
37490 pSrc = pData;
@@ -37577,32 +38490,39 @@
37577 ** the cell content has been copied someplace else. This routine just
37578 ** removes the reference to the cell from pPage.
37579 **
37580 ** "sz" must be the number of bytes in the cell.
37581 */
37582 static void dropCell(MemPage *pPage, int idx, int sz){
37583 int i; /* Loop counter */
37584 int pc; /* Offset to cell content of cell being deleted */
37585 u8 *data; /* pPage->aData */
37586 u8 *ptr; /* Used to move bytes around within data[] */
 
37587
37588 assert( idx>=0 && idx<pPage->nCell );
37589 assert( sz==cellSize(pPage, idx) );
37590 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
37591 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
37592 data = pPage->aData;
37593 ptr = &data[pPage->cellOffset + 2*idx];
37594 pc = get2byte(ptr);
37595 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
37596 freeSpace(pPage, pc, sz);
 
 
 
 
 
37597 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
37598 ptr[0] = ptr[2];
37599 ptr[1] = ptr[3];
37600 }
37601 pPage->nCell--;
37602 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
37603 pPage->nFree += 2;
 
37604 }
37605
37606 /*
37607 ** Insert a new cell on pPage at cell index "i". pCell points to the
37608 ** content of the cell.
@@ -37645,11 +38565,11 @@
37645 if( pTemp ){
37646 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
37647 pCell = pTemp;
37648 }
37649 j = pPage->nOverflow++;
37650 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
37651 pPage->aOvfl[j].pCell = pCell;
37652 pPage->aOvfl[j].idx = i;
37653 pPage->nFree = 0;
37654 }else{
37655 int rc = sqlite3PagerWrite(pPage->pDbPage);
@@ -37662,17 +38582,23 @@
37662 top = get2byte(&data[hdr+5]);
37663 cellOffset = pPage->cellOffset;
37664 end = cellOffset + 2*pPage->nCell + 2;
37665 ins = cellOffset + 2*i;
37666 if( end > top - sz ){
37667 defragmentPage(pPage);
 
 
 
37668 top = get2byte(&data[hdr+5]);
37669 assert( end + sz <= top );
37670 }
37671 idx = allocateSpace(pPage, sz);
37672 assert( idx>0 );
37673 assert( end <= get2byte(&data[hdr+5]) );
 
 
 
37674 pPage->nCell++;
37675 pPage->nFree -= 2;
37676 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
37677 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
37678 ptr[0] = ptr[-2];
@@ -38526,11 +39452,11 @@
38526 ** the virtual root of the tree.
38527 */
38528 VVA_ONLY( pCur->pagesShuffled = 1 );
38529 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
38530 assert( pgnoChild>0 );
38531 assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) );
38532 rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
38533 if( rc ) goto end_shallow_balance;
38534 if( pPage->pgno==1 ){
38535 rc = sqlite3BtreeInitPage(pChild);
38536 if( rc ) goto end_shallow_balance;
@@ -38563,13 +39489,15 @@
38563 freePage(pChild);
38564 TRACE(("BALANCE: transfer child %d into root %d\n",
38565 pChild->pgno, pPage->pgno));
38566 }
38567 assert( pPage->nOverflow==0 );
 
38568 if( ISAUTOVACUUM ){
38569 rc = setChildPtrmaps(pPage);
38570 }
 
38571 releasePage(pChild);
38572 }
38573 end_shallow_balance:
38574 sqlite3_free(apCell);
38575 return rc;
@@ -38612,11 +39540,12 @@
38612 hdr = pPage->hdrOffset;
38613 cbrk = get2byte(&data[hdr+5]);
38614 cdata = pChild->aData;
38615 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
38616 memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk);
38617
 
38618 rc = sqlite3BtreeInitPage(pChild);
38619 if( rc==SQLITE_OK ){
38620 int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]);
38621 memcpy(pChild->aOvfl, pPage->aOvfl, nCopy);
38622 pChild->nOverflow = pPage->nOverflow;
@@ -38627,13 +39556,15 @@
38627 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
38628 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
38629 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
38630 if( ISAUTOVACUUM ){
38631 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
 
38632 if( rc==SQLITE_OK ){
38633 rc = setChildPtrmaps(pChild);
38634 }
 
38635 }
38636 }
38637
38638 if( rc==SQLITE_OK ){
38639 pCur->iPage++;
@@ -38826,11 +39757,14 @@
38826 memcpy(newCell, oldCell, 4);
38827 }
38828 szOld = cellSizePtr(pPage, oldCell);
38829 rc = clearCell(pPage, oldCell);
38830 if( rc ) goto end_insert;
38831 dropCell(pPage, idx, szOld);
 
 
 
38832 }else if( loc<0 && pPage->nCell>0 ){
38833 assert( pPage->leaf );
38834 idx = ++pCur->aiIdx[pCur->iPage];
38835 pCur->info.nSize = 0;
38836 pCur->validNKey = 0;
@@ -39037,12 +39971,14 @@
39037 }
39038 sqlite3BtreeReleaseTempCursor(&leafCur);
39039 }else{
39040 TRACE(("DELETE: table=%d delete from leaf %d\n",
39041 pCur->pgnoRoot, pPage->pgno));
39042 dropCell(pPage, idx, cellSizePtr(pPage, pCell));
39043 rc = balance(pCur, 0);
 
 
39044 }
39045 if( rc==SQLITE_OK ){
39046 moveToRoot(pCur);
39047 }
39048 return rc;
@@ -39204,37 +40140,36 @@
39204 ** the page to the freelist.
39205 */
39206 static int clearDatabasePage(
39207 BtShared *pBt, /* The BTree that contains the table */
39208 Pgno pgno, /* Page number to clear */
39209 MemPage *pParent, /* Parent page. NULL for the root */
39210 int freePageFlag, /* Deallocate page if true */
39211 int *pnChange
39212 ){
39213 MemPage *pPage = 0;
39214 int rc;
39215 unsigned char *pCell;
39216 int i;
39217
39218 assert( sqlite3_mutex_held(pBt->mutex) );
39219 if( pgno>pagerPagecount(pBt->pPager) ){
39220 return SQLITE_CORRUPT_BKPT;
39221 }
39222
39223 rc = getAndInitPage(pBt, pgno, &pPage);
39224 if( rc ) goto cleardatabasepage_out;
39225 for(i=0; i<pPage->nCell; i++){
39226 pCell = findCell(pPage, i);
39227 if( !pPage->leaf ){
39228 rc = clearDatabasePage(pBt, get4byte(pCell), pPage, 1, pnChange);
39229 if( rc ) goto cleardatabasepage_out;
39230 }
39231 rc = clearCell(pPage, pCell);
39232 if( rc ) goto cleardatabasepage_out;
39233 }
39234 if( !pPage->leaf ){
39235 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage, 1, pnChange);
39236 if( rc ) goto cleardatabasepage_out;
39237 }else if( pnChange ){
39238 assert( pPage->intKey );
39239 *pnChange += pPage->nCell;
39240 }
@@ -39272,11 +40207,11 @@
39272 }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){
39273 /* nothing to do */
39274 }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
39275 /* nothing to do */
39276 }else{
39277 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0, pnChange);
39278 }
39279 sqlite3BtreeLeave(p);
39280 return rc;
39281 }
39282
@@ -39298,11 +40233,11 @@
39298 ** page number that used to be the last root page in the file before
39299 ** the move. If no page gets moved, *piMoved is set to 0.
39300 ** The last root page is recorded in meta[3] and the value of
39301 ** meta[3] is updated by this procedure.
39302 */
39303 static int btreeDropTable(Btree *p, int iTable, int *piMoved){
39304 int rc;
39305 MemPage *pPage = 0;
39306 BtShared *pBt = p->pBt;
39307
39308 assert( sqlite3BtreeHoldsMutex(p) );
@@ -39581,13 +40516,13 @@
39581 ** Return 1 if there are 2 ore more references to the page and 0 if
39582 ** if this is the first reference to the page.
39583 **
39584 ** Also check that the page number is in bounds.
39585 */
39586 static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
39587 if( iPage==0 ) return 1;
39588 if( iPage>pCheck->nPage || iPage<0 ){
39589 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
39590 return 1;
39591 }
39592 if( pCheck->anRef[iPage]==1 ){
39593 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
@@ -39718,11 +40653,10 @@
39718 ** the root of the tree.
39719 */
39720 static int checkTreePage(
39721 IntegrityCk *pCheck, /* Context for the sanity check */
39722 int iPage, /* Page number of the page to check */
39723 MemPage *pParent, /* Parent page */
39724 char *zParentContext /* Parent context */
39725 ){
39726 MemPage *pPage;
39727 int i, rc, depth, d2, pgno, cnt;
39728 int hdr, cellStart;
@@ -39729,11 +40663,11 @@
39729 int nCell;
39730 u8 *data;
39731 BtShared *pBt;
39732 int usableSize;
39733 char zContext[100];
39734 char *hit;
39735
39736 sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
39737
39738 /* Check that the page exists
39739 */
@@ -39756,11 +40690,11 @@
39756 /* Check out all the cells.
39757 */
39758 depth = 0;
39759 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
39760 u8 *pCell;
39761 int sz;
39762 CellInfo info;
39763
39764 /* Check payload overflow pages
39765 */
39766 sqlite3_snprintf(sizeof(zContext), zContext,
@@ -39788,11 +40722,11 @@
39788 #ifndef SQLITE_OMIT_AUTOVACUUM
39789 if( pBt->autoVacuum ){
39790 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
39791 }
39792 #endif
39793 d2 = checkTreePage(pCheck,pgno,pPage,zContext);
39794 if( i>0 && d2!=depth ){
39795 checkAppendMsg(pCheck, zContext, "Child page depth differs");
39796 }
39797 depth = d2;
39798 }
@@ -39804,11 +40738,11 @@
39804 #ifndef SQLITE_OMIT_AUTOVACUUM
39805 if( pBt->autoVacuum ){
39806 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
39807 }
39808 #endif
39809 checkTreePage(pCheck, pgno, pPage, zContext);
39810 }
39811
39812 /* Check for complete coverage of the page
39813 */
39814 data = pPage->aData;
@@ -39815,12 +40749,18 @@
39815 hdr = pPage->hdrOffset;
39816 hit = sqlite3PageMalloc( pBt->pageSize );
39817 if( hit==0 ){
39818 pCheck->mallocFailed = 1;
39819 }else{
39820 memset(hit, 0, usableSize );
39821 memset(hit, 1, get2byte(&data[hdr+5]));
 
 
 
 
 
 
39822 nCell = get2byte(&data[hdr+3]);
39823 cellStart = hdr + 12 - 4*pPage->leaf;
39824 for(i=0; i<nCell; i++){
39825 int pc = get2byte(&data[cellStart+i*2]);
39826 u16 size = 1024;
@@ -39860,11 +40800,12 @@
39860 checkAppendMsg(pCheck, 0,
39861 "Fragmented space is %d byte reported as %d on page %d",
39862 cnt, data[hdr+7], iPage);
39863 }
39864 }
39865 sqlite3PageFree(hit);
 
39866
39867 releasePage(pPage);
39868 return depth+1;
39869 }
39870 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
@@ -39885,11 +40826,11 @@
39885 int *aRoot, /* An array of root pages numbers for individual trees */
39886 int nRoot, /* Number of entries in aRoot[] */
39887 int mxErr, /* Stop reporting errors after this many */
39888 int *pnErr /* Write number of errors seen to this variable */
39889 ){
39890 int i;
39891 int nRef;
39892 IntegrityCk sCheck;
39893 BtShared *pBt = p->pBt;
39894 char zErr[100];
39895
@@ -39901,11 +40842,11 @@
39901 sqlite3BtreeLeave(p);
39902 return sqlite3DbStrDup(0, "cannot acquire a read lock on the database");
39903 }
39904 sCheck.pBt = pBt;
39905 sCheck.pPager = pBt->pPager;
39906 sCheck.nPage = pagerPagecount(sCheck.pPager);
39907 sCheck.mxErr = mxErr;
39908 sCheck.nErr = 0;
39909 sCheck.mallocFailed = 0;
39910 *pnErr = 0;
39911 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -39937,18 +40878,18 @@
39937 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
39938 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
39939
39940 /* Check all the tables.
39941 */
39942 for(i=0; i<nRoot && sCheck.mxErr; i++){
39943 if( aRoot[i]==0 ) continue;
39944 #ifndef SQLITE_OMIT_AUTOVACUUM
39945 if( pBt->autoVacuum && aRoot[i]>1 ){
39946 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
39947 }
39948 #endif
39949 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
39950 }
39951
39952 /* Make sure every page in the file is referenced
39953 */
39954 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
@@ -40068,12 +41009,12 @@
40068 }
40069 if( pBtTo->pCursor ){
40070 return SQLITE_BUSY;
40071 }
40072
40073 nToPage = pagerPagecount(pBtTo->pPager);
40074 nFromPage = pagerPagecount(pBtFrom->pPager);
40075 iSkip = PENDING_BYTE_PAGE(pBtTo);
40076
40077 /* Variable nNewPage is the number of pages required to store the
40078 ** contents of pFrom using the current page-size of pTo.
40079 */
@@ -40417,23 +41358,23 @@
40417 **
40418 *************************************************************************
40419 ** This file implements a FIFO queue of rowids used for processing
40420 ** UPDATE and DELETE statements.
40421 **
40422 ** $Id: vdbefifo.c,v 1.8 2008/07/28 19:34:54 drh Exp $
40423 */
40424
40425 /*
40426 ** Constants FIFOSIZE_FIRST and FIFOSIZE_MAX are the initial
40427 ** number of entries in a fifo page and the maximum number of
40428 ** entries in a fifo page.
40429 */
40430 #define FIFOSIZE_FIRST (((128-sizeof(FifoPage))/8)+1)
40431 #ifdef SQLITE_MALLOC_SOFT_LIMIT
40432 # define FIFOSIZE_MAX (((SQLITE_MALLOC_SOFT_LIMIT-sizeof(FifoPage))/8)+1)
40433 #else
40434 # define FIFOSIZE_MAX (((262144-sizeof(FifoPage))/8)+1)
40435 #endif
40436
40437 /*
40438 ** Allocate a new FifoPage and return a pointer to it. Return NULL if
40439 ** we run out of memory. Leave space on the page for nEntry entries.
@@ -40551,11 +41492,11 @@
40551 ** This file contains code use to manipulate "Mem" structure. A "Mem"
40552 ** stores a single value in the VDBE. Mem is an opaque structure visible
40553 ** only within the VDBE. Interface routines refer to a Mem using the
40554 ** name sqlite_value
40555 **
40556 ** $Id: vdbemem.c,v 1.125 2008/11/05 17:41:19 drh Exp $
40557 */
40558
40559 /*
40560 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
40561 ** P if required.
@@ -40619,13 +41560,10 @@
40619
40620 if( n<32 ) n = 32;
40621 if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
40622 if( preserve && pMem->z==pMem->zMalloc ){
40623 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
40624 if( !pMem->z ){
40625 pMem->flags = MEM_Null;
40626 }
40627 preserve = 0;
40628 }else{
40629 sqlite3DbFree(pMem->db, pMem->zMalloc);
40630 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
40631 }
@@ -40637,11 +41575,15 @@
40637 if( pMem->flags&MEM_Dyn && pMem->xDel ){
40638 pMem->xDel((void *)(pMem->z));
40639 }
40640
40641 pMem->z = pMem->zMalloc;
40642 pMem->flags &= ~(MEM_Ephem|MEM_Static);
 
 
 
 
40643 pMem->xDel = 0;
40644 return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
40645 }
40646
40647 /*
@@ -41596,11 +42538,11 @@
41596 ** This file contains code used for creating, destroying, and populating
41597 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
41598 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
41599 ** But that file was getting too big so this subroutines were split out.
41600 **
41601 ** $Id: vdbeaux.c,v 1.418 2008/11/05 17:41:19 drh Exp $
41602 */
41603
41604
41605
41606 /*
@@ -41679,25 +42621,27 @@
41679 p->trace = trace;
41680 }
41681 #endif
41682
41683 /*
41684 ** Resize the Vdbe.aOp array so that it contains at least N
41685 ** elements.
41686 **
41687 ** If an out-of-memory error occurs while resizing the array,
41688 ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
41689 ** any opcodes already allocated can be correctly deallocated
41690 ** along with the rest of the Vdbe).
41691 */
41692 static void resizeOpArray(Vdbe *p, int N){
41693 VdbeOp *pNew;
41694 pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op));
 
41695 if( pNew ){
41696 p->nOpAlloc = N;
41697 p->aOp = pNew;
41698 }
 
41699 }
41700
41701 /*
41702 ** Add a new instruction to the list of instructions current in the
41703 ** VDBE. Return the address of the new instruction.
@@ -41719,12 +42663,11 @@
41719 VdbeOp *pOp;
41720
41721 i = p->nOp;
41722 assert( p->magic==VDBE_MAGIC_INIT );
41723 if( p->nOpAlloc<=i ){
41724 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op));
41725 if( p->db->mallocFailed ){
41726 return 0;
41727 }
41728 }
41729 p->nOp++;
41730 pOp = &p->aOp[i];
@@ -41921,15 +42864,11 @@
41921 ** address of the first operation added.
41922 */
41923 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
41924 int addr;
41925 assert( p->magic==VDBE_MAGIC_INIT );
41926 if( p->nOp + nOp > p->nOpAlloc ){
41927 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op));
41928 assert( p->nOp+nOp<=p->nOpAlloc || p->db->mallocFailed );
41929 }
41930 if( p->db->mallocFailed ){
41931 return 0;
41932 }
41933 addr = p->nOp;
41934 if( nOp>0 ){
41935 int i;
@@ -42310,11 +43249,11 @@
42310 **
42311 */
42312 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
42313 int mask;
42314 assert( i>=0 && i<p->db->nDb );
42315 assert( i<sizeof(p->btreeMask)*8 );
42316 mask = 1<<i;
42317 if( (p->btreeMask & mask)==0 ){
42318 p->btreeMask |= mask;
42319 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
42320 }
@@ -42596,15 +43535,11 @@
42596
42597 /* There should be at least one opcode.
42598 */
42599 assert( p->nOp>0 );
42600
42601 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
42602 * is because the call to resizeOpArray() below may shrink the
42603 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
42604 * state.
42605 */
42606 p->magic = VDBE_MAGIC_RUN;
42607
42608 /* For each cursor required, also allocate a memory cell. Memory
42609 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
42610 ** the vdbe program. Instead they are used to allocate space for
@@ -42620,11 +43555,10 @@
42620 ** Allocation space for registers.
42621 */
42622 if( p->aMem==0 ){
42623 int nArg; /* Maximum number of args passed to a user function. */
42624 resolveP2Values(p, &nArg);
42625 /*resizeOpArray(p, p->nOp);*/
42626 assert( nVar>=0 );
42627 if( isExplain && nMem<10 ){
42628 nMem = 10;
42629 }
42630 p->aMem = sqlite3DbMallocZero(db,
@@ -43164,11 +44098,14 @@
43164 ** we do either a commit or rollback of the current transaction.
43165 **
43166 ** Note: This block also runs if one of the special errors handled
43167 ** above has occurred.
43168 */
43169 if( db->autoCommit && db->writeVdbeCnt==(p->readOnly==0) ){
 
 
 
43170 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
43171 /* The auto-commit flag is true, and the vdbe program was
43172 ** successful or hit an 'OR FAIL' constraint. This means a commit
43173 ** is required.
43174 */
@@ -43791,12 +44728,12 @@
43791 UnpackedRecord *pSpace,/* Space available to hold resulting object */
43792 int szSpace /* Size of pSpace[] in bytes */
43793 ){
43794 const unsigned char *aKey = (const unsigned char *)pKey;
43795 UnpackedRecord *p;
43796 int nByte;
43797 int idx, d;
43798 u16 u; /* Unsigned loop counter */
43799 u32 szHdr;
43800 Mem *pMem;
43801
43802 assert( sizeof(Mem)>sizeof(*p) );
@@ -43816,11 +44753,11 @@
43816 d = szHdr;
43817 u = 0;
43818 while( idx<szHdr && u<p->nField ){
43819 u32 serial_type;
43820
43821 idx += getVarint32( aKey+idx, serial_type);
43822 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
43823 pMem->enc = pKeyInfo->enc;
43824 pMem->db = pKeyInfo->db;
43825 pMem->flags = 0;
43826 pMem->zMalloc = 0;
@@ -43881,11 +44818,11 @@
43881 */
43882 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
43883 int nKey1, const void *pKey1, /* Left key */
43884 UnpackedRecord *pPKey2 /* Right key */
43885 ){
43886 u32 d1; /* Offset into aKey[] of next data element */
43887 u32 idx1; /* Offset into aKey[] of next header element */
43888 u32 szHdr1; /* Number of bytes in header */
43889 int i = 0;
43890 int nField;
43891 int rc = 0;
@@ -44084,11 +45021,11 @@
44084 *************************************************************************
44085 **
44086 ** This file contains code use to implement APIs that are part of the
44087 ** VDBE.
44088 **
44089 ** $Id: vdbeapi.c,v 1.148 2008/11/05 16:37:35 drh Exp $
44090 */
44091
44092 #if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
44093 /*
44094 ** The following structure contains pointers to the end points of a
@@ -44670,15 +45607,16 @@
44670 ** for name resolution but are actually overloaded by the xFindFunction
44671 ** method of virtual tables.
44672 */
44673 SQLITE_PRIVATE void sqlite3InvalidFunction(
44674 sqlite3_context *context, /* The function calling context */
44675 int argc, /* Number of arguments to the function */
44676 sqlite3_value **argv /* Value of each argument */
44677 ){
44678 const char *zName = context->pFunc->zName;
44679 char *zErr;
 
44680 zErr = sqlite3MPrintf(0,
44681 "unable to use function %s in the requested context", zName);
44682 sqlite3_result_error(context, zErr, -1);
44683 sqlite3_free(zErr);
44684 }
@@ -45423,11 +46361,11 @@
45423 ** documentation, headers files, or other derived files. The formatting
45424 ** of the code in this file is, therefore, important. See other comments
45425 ** in this file for details. If in doubt, do not deviate from existing
45426 ** commenting and indentation practices when changing or adding code.
45427 **
45428 ** $Id: vdbe.c,v 1.786 2008/11/05 16:37:35 drh Exp $
45429 */
45430
45431 /*
45432 ** The following global variable is incremented every time a cursor
45433 ** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test
@@ -45554,11 +46492,11 @@
45554 /*
45555 ** Return true if an opcode has any of the OPFLG_xxx properties
45556 ** specified by mask.
45557 */
45558 SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
45559 assert( opcode>0 && opcode<sizeof(opcodeProperty) );
45560 return (opcodeProperty[opcode]&mask)!=0;
45561 }
45562
45563 /*
45564 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
@@ -46019,11 +46957,10 @@
46019 #endif
46020 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
46021 int nProgressOps = 0; /* Opcodes executed since progress callback. */
46022 #endif
46023 UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */
46024
46025
46026 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
46027 assert( db->magic==SQLITE_MAGIC_BUSY );
46028 sqlite3BtreeMutexArrayEnter(&p->aMutex);
46029 if( p->rc==SQLITE_NOMEM ){
@@ -47432,19 +48369,19 @@
47432 ** If the column contains fewer than P2 fields, then extract a NULL. Or,
47433 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
47434 ** the result.
47435 */
47436 case OP_Column: {
47437 u32 payloadSize; /* Number of bytes in the record */
47438 int p1 = pOp->p1; /* P1 value of the opcode */
47439 int p2 = pOp->p2; /* column number to retrieve */
47440 VdbeCursor *pC = 0;/* The VDBE cursor */
47441 char *zRec; /* Pointer to complete record-data */
47442 BtCursor *pCrsr; /* The BTree cursor */
47443 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
47444 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
47445 u32 nField; /* number of fields in the record */
47446 int len; /* The length of the serialized data for the column */
47447 int i; /* Loop counter */
47448 char *zData; /* Part of the record being decoded */
47449 Mem *pDest; /* Where to write the extracted value */
47450 Mem sMem; /* For storing the record being decoded */
@@ -47488,11 +48425,11 @@
47488 }else if( pC->isIndex ){
47489 i64 payloadSize64;
47490 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
47491 payloadSize = payloadSize64;
47492 }else{
47493 sqlite3BtreeDataSize(pCrsr, &payloadSize);
47494 }
47495 nField = pC->nField;
47496 }else{
47497 assert( pC->pseudoTable );
47498 /* The record is the sole entry of a pseudo-table */
@@ -47522,11 +48459,11 @@
47522 if( pC->cacheStatus==p->cacheCtr ){
47523 aOffset = pC->aOffset;
47524 }else{
47525 u8 *zIdx; /* Index into header */
47526 u8 *zEndHdr; /* Pointer to first byte after the header */
47527 u32 offset; /* Offset into the data */
47528 int szHdrSz; /* Size of the header size field at start of record */
47529 int avail; /* Number of bytes of available data */
47530
47531 assert(aType);
47532 pC->aOffset = aOffset = &aType[nField];
@@ -47723,11 +48660,11 @@
47723 */
47724 u8 *zNewRecord; /* A buffer to hold the data for the new record */
47725 Mem *pRec; /* The new record */
47726 u64 nData = 0; /* Number of bytes of data space */
47727 int nHdr = 0; /* Number of bytes of header space */
47728 u64 nByte = 0; /* Data space required for this record */
47729 int nZero = 0; /* Number of zero bytes at the end of the record */
47730 int nVarint; /* Number of bytes in a varint */
47731 u32 serial_type; /* Type field */
47732 Mem *pData0; /* First field to be combined into the record */
47733 Mem *pLast; /* Last field of the record */
@@ -48166,11 +49103,14 @@
48166 assert( p2>0 );
48167 assert( p2<=p->nMem );
48168 pIn2 = &p->aMem[p2];
48169 sqlite3VdbeMemIntegerify(pIn2);
48170 p2 = pIn2->u.i;
48171 assert( p2>=2 );
 
 
 
48172 }
48173 assert( i>=0 );
48174 pCur = allocateCursor(p, i, &pOp[-1], iDb, 1);
48175 if( pCur==0 ) goto no_mem;
48176 pCur->nullRow = 1;
@@ -48793,11 +49733,11 @@
48793 #else
48794 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
48795 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
48796 ** to provide the constant while making all compilers happy.
48797 */
48798 # define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
48799 #endif
48800
48801 if( !pC->useRandomRowid ){
48802 if( pC->nextRowidValid ){
48803 v = pC->nextRowid;
@@ -49099,11 +50039,11 @@
49099 goto too_big;
49100 }
49101 n = n64;
49102 }else{
49103 sqlite3BtreeDataSize(pCrsr, &n);
49104 if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
49105 goto too_big;
49106 }
49107 }
49108 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
49109 goto no_mem;
@@ -51213,11 +52153,11 @@
51213 **
51214 ** This file contains code use to implement an in-memory rollback journal.
51215 ** The in-memory rollback journal is used to journal transactions for
51216 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
51217 **
51218 ** @(#) $Id: memjournal.c,v 1.2 2008/10/28 18:12:36 drh Exp $
51219 */
51220
51221 /* Forward references to internal structures */
51222 typedef struct MemJournal MemJournal;
51223 typedef struct FilePoint FilePoint;
@@ -51228,11 +52168,13 @@
51228 */
51229 #define JOURNAL_CHUNKSIZE 1024
51230
51231 /* Macro to find the minimum of two numeric values.
51232 */
51233 #define MIN(x,y) ((x)<(y)?(x):(y))
 
 
51234
51235 /*
51236 ** The rollback journal is composed of a linked list of these structures.
51237 */
51238 struct FileChunk {
@@ -51319,10 +52261,11 @@
51319
51320 /* An in-memory journal file should only ever be appended to. Random
51321 ** access writes are not required by sqlite.
51322 */
51323 assert(iOfst==p->endpoint.iOffset);
 
51324
51325 while( nWrite>0 ){
51326 FileChunk *pChunk = p->endpoint.pChunk;
51327 int iChunkOffset = p->endpoint.iOffset%JOURNAL_CHUNKSIZE;
51328 int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
@@ -51358,10 +52301,11 @@
51358 */
51359 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
51360 MemJournal *p = (MemJournal *)pJfd;
51361 FileChunk *pChunk;
51362 assert(size==0);
 
51363 pChunk = p->pFirst;
51364 while( pChunk ){
51365 FileChunk *pTmp = pChunk;
51366 pChunk = pChunk->pNext;
51367 sqlite3_free(pTmp);
@@ -51380,11 +52324,12 @@
51380
51381
51382 /*
51383 ** Sync the file.
51384 */
51385 static int memjrnlSync(sqlite3_file *pJfd, int flags){
 
51386 return SQLITE_OK;
51387 }
51388
51389 /*
51390 ** Query the size of the file in bytes.
@@ -51589,11 +52534,11 @@
51589 **
51590 ** This file contains routines used for walking the parser tree and
51591 ** resolve all identifiers by associating them with a particular
51592 ** table and column.
51593 **
51594 ** $Id: resolve.c,v 1.10 2008/10/19 21:03:27 drh Exp $
51595 */
51596
51597 /*
51598 ** Turn the pExpr expression into an alias for the iCol-th column of the
51599 ** result set in pEList.
@@ -51921,13 +52866,13 @@
51921 ** column number is greater than the number of bits in the bitmask
51922 ** then set the high-order bit of the bitmask.
51923 */
51924 if( pExpr->iColumn>=0 && pMatch!=0 ){
51925 int n = pExpr->iColumn;
51926 testcase( n==sizeof(Bitmask)*8-1 );
51927 if( n>=sizeof(Bitmask)*8 ){
51928 n = sizeof(Bitmask)*8-1;
51929 }
51930 assert( pMatch->iCursor==pExpr->iTable );
51931 pMatch->colUsed |= ((Bitmask)1)<<n;
51932 }
51933
@@ -52756,11 +53701,11 @@
52756 **
52757 *************************************************************************
52758 ** This file contains routines used for analyzing expressions and
52759 ** for generating VDBE code that evaluates expressions in SQLite.
52760 **
52761 ** $Id: expr.c,v 1.401 2008/11/06 15:33:04 drh Exp $
52762 */
52763
52764 /*
52765 ** Return the 'affinity' of the expression pExpr if any.
52766 **
@@ -52785,11 +53730,13 @@
52785 #ifndef SQLITE_OMIT_CAST
52786 if( op==TK_CAST ){
52787 return sqlite3AffinityType(&pExpr->token);
52788 }
52789 #endif
52790 if( (op==TK_COLUMN || op==TK_REGISTER) && pExpr->pTab!=0 ){
 
 
52791 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
52792 ** a TK_COLUMN but was previously evaluated and cached in a register */
52793 int j = pExpr->iColumn;
52794 if( j<0 ) return SQLITE_AFF_INTEGER;
52795 assert( pExpr->pTab && j<pExpr->pTab->nCol );
@@ -52831,11 +53778,11 @@
52831 while( p ){
52832 int op;
52833 pColl = p->pColl;
52834 if( pColl ) break;
52835 op = p->op;
52836 if( (op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){
52837 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
52838 ** a TK_COLUMN but was previously evaluated and cached in a register */
52839 const char *zColl;
52840 int j = p->iColumn;
52841 if( j>=0 ){
@@ -53679,11 +54626,12 @@
53679 return WRC_Abort;
53680 default:
53681 return WRC_Continue;
53682 }
53683 }
53684 static int selectNodeIsConstant(Walker *pWalker, Select *pSelect){
 
53685 pWalker->u.i = 0;
53686 return WRC_Abort;
53687 }
53688 static int exprIsConst(Expr *p, int initFlag){
53689 Walker w;
@@ -54199,14 +55147,15 @@
54199 ** z[n] character is guaranteed to be something that does not look
54200 ** like the continuation of the number.
54201 */
54202 static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
54203 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
 
 
54204 if( z ){
54205 double value;
54206 char *zV;
54207 assert( !isdigit(z[n]) );
54208 sqlite3AtoF(z, &value);
54209 if( sqlite3IsNaN(value) ){
54210 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
54211 }else{
54212 if( negateFlag ) value = -value;
@@ -55854,11 +56803,11 @@
55854 **
55855 *************************************************************************
55856 ** This file contains C code routines that used to generate VDBE code
55857 ** that implements the ALTER TABLE command.
55858 **
55859 ** $Id: alter.c,v 1.49 2008/10/30 17:21:13 danielk1977 Exp $
55860 */
55861
55862 /*
55863 ** The code in this file only exists if we are not omitting the
55864 ** ALTER TABLE logic from the build.
@@ -55879,11 +56828,11 @@
55879 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
55880 ** -> 'CREATE INDEX i ON def(a, b, c)'
55881 */
55882 static void renameTableFunc(
55883 sqlite3_context *context,
55884 int argc,
55885 sqlite3_value **argv
55886 ){
55887 unsigned char const *zSql = sqlite3_value_text(argv[0]);
55888 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
55889
@@ -55892,10 +56841,12 @@
55892 unsigned char const *zCsr = zSql;
55893 int len = 0;
55894 char *zRet;
55895
55896 sqlite3 *db = sqlite3_context_db_handle(context);
 
 
55897
55898 /* The principle used to locate the table name in the CREATE TABLE
55899 ** statement is that the table name is the first non-space token that
55900 ** is immediately followed by a TK_LP or TK_USING token.
55901 */
@@ -55934,11 +56885,11 @@
55934 ** returned. This is analagous to renameTableFunc() above, except for CREATE
55935 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
55936 */
55937 static void renameTriggerFunc(
55938 sqlite3_context *context,
55939 int argc,
55940 sqlite3_value **argv
55941 ){
55942 unsigned char const *zSql = sqlite3_value_text(argv[0]);
55943 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
55944
@@ -55946,12 +56897,13 @@
55946 Token tname;
55947 int dist = 3;
55948 unsigned char const *zCsr = zSql;
55949 int len = 0;
55950 char *zRet;
55951
55952 sqlite3 *db = sqlite3_context_db_handle(context);
 
 
55953
55954 /* The principle used to locate the table name in the CREATE TRIGGER
55955 ** statement is that the table name is the first token that is immediatedly
55956 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
55957 ** of TK_WHEN, TK_BEGIN or TK_FOR.
@@ -56475,11 +57427,11 @@
56475 ** May you share freely, never taking more than you give.
56476 **
56477 *************************************************************************
56478 ** This file contains code associated with the ANALYZE command.
56479 **
56480 ** @(#) $Id: analyze.c,v 1.44 2008/11/03 20:55:07 drh Exp $
56481 */
56482 #ifndef SQLITE_OMIT_ANALYZE
56483
56484 /*
56485 ** This routine generates code that opens the sqlite_stat1 table on cursor
@@ -56819,18 +57771,20 @@
56819 ** sqlite_stat1 table.
56820 **
56821 ** argv[0] = name of the index
56822 ** argv[1] = results of analysis - on integer for each column
56823 */
56824 static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){
56825 analysisInfo *pInfo = (analysisInfo*)pData;
56826 Index *pIndex;
56827 int i, c;
56828 unsigned int v;
56829 const char *z;
56830
56831 assert( argc==2 );
 
 
56832 if( argv==0 || argv[0]==0 || argv[1]==0 ){
56833 return 0;
56834 }
56835 pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
56836 if( pIndex==0 ){
@@ -56902,11 +57856,11 @@
56902 ** May you share freely, never taking more than you give.
56903 **
56904 *************************************************************************
56905 ** This file contains code used to implement the ATTACH and DETACH commands.
56906 **
56907 ** $Id: attach.c,v 1.79 2008/10/28 17:52:39 danielk1977 Exp $
56908 */
56909
56910 #ifndef SQLITE_OMIT_ATTACH
56911 /*
56912 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
@@ -56954,11 +57908,11 @@
56954 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
56955 ** third argument.
56956 */
56957 static void attachFunc(
56958 sqlite3_context *context,
56959 int argc,
56960 sqlite3_value **argv
56961 ){
56962 int i;
56963 int rc = 0;
56964 sqlite3 *db = sqlite3_context_db_handle(context);
@@ -56965,10 +57919,12 @@
56965 const char *zName;
56966 const char *zFile;
56967 Db *aNew;
56968 char *zErrDyn = 0;
56969 char zErr[128];
 
 
56970
56971 zFile = (const char *)sqlite3_value_text(argv[0]);
56972 zName = (const char *)sqlite3_value_text(argv[1]);
56973 if( zFile==0 ) zFile = "";
56974 if( zName==0 ) zName = "";
@@ -57122,18 +58078,20 @@
57122 **
57123 ** SELECT sqlite_detach(x)
57124 */
57125 static void detachFunc(
57126 sqlite3_context *context,
57127 int argc,
57128 sqlite3_value **argv
57129 ){
57130 const char *zName = (const char *)sqlite3_value_text(argv[0]);
57131 sqlite3 *db = sqlite3_context_db_handle(context);
57132 int i;
57133 Db *pDb = 0;
57134 char zErr[128];
 
 
57135
57136 if( zName==0 ) zName = "";
57137 for(i=0; i<db->nDb; i++){
57138 pDb = &db->aDb[i];
57139 if( pDb->pBt==0 ) continue;
@@ -57685,11 +58643,11 @@
57685 ** creating ID lists
57686 ** BEGIN TRANSACTION
57687 ** COMMIT
57688 ** ROLLBACK
57689 **
57690 ** $Id: build.c,v 1.500 2008/11/03 20:55:07 drh Exp $
57691 */
57692
57693 /*
57694 ** This routine is called when a new SQL statement is beginning to
57695 ** be parsed. Initialize the pParse structure as needed.
@@ -58287,11 +59245,11 @@
58287 ** index of the named database in db->aDb[], or -1 if the named db
58288 ** does not exist.
58289 */
58290 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
58291 int i = -1; /* Database number */
58292 int n; /* Number of characters in the name */
58293 Db *pDb; /* A database whose name space is being searched */
58294 char *zName; /* Name we are searching for */
58295
58296 zName = sqlite3NameFromToken(db, pName);
58297 if( zName ){
@@ -58331,11 +59289,15 @@
58331 ){
58332 int iDb; /* Database holding the object */
58333 sqlite3 *db = pParse->db;
58334
58335 if( pName2 && pName2->n>0 ){
58336 assert( !db->init.busy );
 
 
 
 
58337 *pUnqual = pName2;
58338 iDb = sqlite3FindDb(db, pName1);
58339 if( iDb<0 ){
58340 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
58341 pParse->nErr++;
@@ -59053,11 +60015,11 @@
59053 k += strlen(&zStmt[k]);
59054 zSep = zSep2;
59055 identPut(zStmt, &k, pCol->zName);
59056 if( (z = pCol->zType)!=0 ){
59057 zStmt[k++] = ' ';
59058 assert( strlen(z)+k+1<=n );
59059 sqlite3_snprintf(n-k, &zStmt[k], "%s", z);
59060 k += strlen(z);
59061 }
59062 }
59063 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
@@ -60651,16 +61613,86 @@
60651 for(i=0; i<pList->nId; i++){
60652 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
60653 }
60654 return -1;
60655 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60656
60657 /*
60658 ** Append a new table name to the given SrcList. Create a new SrcList if
60659 ** need be. A new entry is created in the SrcList even if pToken is NULL.
60660 **
60661 ** A new SrcList is returned, or NULL if malloc() fails.
 
 
 
60662 **
60663 ** If pDatabase is not null, it means that the table has an optional
60664 ** database name prefix. Like this: "database.table". The pDatabase
60665 ** points to the table name and the pTable points to the database name.
60666 ** The SrcList.a[].zName field is filled with the table name which might
@@ -60689,23 +61721,16 @@
60689 if( pList==0 ){
60690 pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
60691 if( pList==0 ) return 0;
60692 pList->nAlloc = 1;
60693 }
60694 if( pList->nSrc>=pList->nAlloc ){
60695 SrcList *pNew;
60696 pList->nAlloc *= 2;
60697 pNew = sqlite3DbRealloc(db, pList,
60698 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
60699 if( pNew==0 ){
60700 sqlite3SrcListDelete(db, pList);
60701 return 0;
60702 }
60703 pList = pNew;
60704 }
60705 pItem = &pList->a[pList->nSrc];
60706 memset(pItem, 0, sizeof(pList->a[0]));
60707 if( pDatabase && pDatabase->z==0 ){
60708 pDatabase = 0;
60709 }
60710 if( pDatabase && pTable ){
60711 Token *pTemp = pDatabase;
@@ -60712,12 +61737,10 @@
60712 pDatabase = pTable;
60713 pTable = pTemp;
60714 }
60715 pItem->zName = sqlite3NameFromToken(db, pTable);
60716 pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
60717 pItem->iCursor = -1;
60718 pList->nSrc++;
60719 return pList;
60720 }
60721
60722 /*
60723 ** Assign VdbeCursor index numbers to all tables in a SrcList
@@ -61644,11 +62667,11 @@
61644 **
61645 *************************************************************************
61646 ** This file contains C code routines that are called by the parser
61647 ** in order to generate code for DELETE FROM statements.
61648 **
61649 ** $Id: delete.c,v 1.186 2008/10/31 10:53:23 danielk1977 Exp $
61650 */
61651
61652 /*
61653 ** Look up every table that is named in pSrc. If any table is not found,
61654 ** add an error message to pParse->zErrMsg and return NULL. If all tables
@@ -61896,11 +62919,11 @@
61896
61897 /* Figure out if we have any triggers and if the table being
61898 ** deleted from is a view
61899 */
61900 #ifndef SQLITE_OMIT_TRIGGER
61901 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0);
61902 isView = pTab->pSelect!=0;
61903 #else
61904 # define triggers_exist 0
61905 # define isView 0
61906 #endif
@@ -62285,11 +63308,11 @@
62285 **
62286 ** There is only one exported symbol in this file - the function
62287 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
62288 ** All other code has file scope.
62289 **
62290 ** $Id: func.c,v 1.204 2008/10/28 17:52:39 danielk1977 Exp $
62291 */
62292
62293 /*
62294 ** Return the collating function associated with a function.
62295 */
@@ -62329,14 +63352,15 @@
62329 /*
62330 ** Return the type of the argument.
62331 */
62332 static void typeofFunc(
62333 sqlite3_context *context,
62334 int argc,
62335 sqlite3_value **argv
62336 ){
62337 const char *z = 0;
 
62338 switch( sqlite3_value_type(argv[0]) ){
62339 case SQLITE_NULL: z = "null"; break;
62340 case SQLITE_INTEGER: z = "integer"; break;
62341 case SQLITE_TEXT: z = "text"; break;
62342 case SQLITE_FLOAT: z = "real"; break;
@@ -62355,10 +63379,11 @@
62355 sqlite3_value **argv
62356 ){
62357 int len;
62358
62359 assert( argc==1 );
 
62360 switch( sqlite3_value_type(argv[0]) ){
62361 case SQLITE_BLOB:
62362 case SQLITE_INTEGER:
62363 case SQLITE_FLOAT: {
62364 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
@@ -62385,10 +63410,11 @@
62385 /*
62386 ** Implementation of the abs() function
62387 */
62388 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
62389 assert( argc==1 );
 
62390 switch( sqlite3_value_type(argv[0]) ){
62391 case SQLITE_INTEGER: {
62392 i64 iVal = sqlite3_value_int64(argv[0]);
62393 if( iVal<0 ){
62394 if( (iVal<<1)==0 ){
@@ -62588,14 +63614,15 @@
62588 /*
62589 ** Implementation of random(). Return a random integer.
62590 */
62591 static void randomFunc(
62592 sqlite3_context *context,
62593 int argc,
62594 sqlite3_value **argv
62595 ){
62596 sqlite_int64 r;
 
62597 sqlite3_randomness(sizeof(r), &r);
62598 if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */
62599 /* can always do abs() of the result */
62600 sqlite3_result_int64(context, r);
62601 }
@@ -62610,10 +63637,11 @@
62610 sqlite3_value **argv
62611 ){
62612 int n;
62613 unsigned char *p;
62614 assert( argc==1 );
 
62615 n = sqlite3_value_int(argv[0]);
62616 if( n<1 ){
62617 n = 1;
62618 }
62619 p = contextMalloc(context, n);
@@ -62627,40 +63655,43 @@
62627 ** Implementation of the last_insert_rowid() SQL function. The return
62628 ** value is the same as the sqlite3_last_insert_rowid() API function.
62629 */
62630 static void last_insert_rowid(
62631 sqlite3_context *context,
62632 int arg,
62633 sqlite3_value **argv
62634 ){
62635 sqlite3 *db = sqlite3_context_db_handle(context);
 
62636 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
62637 }
62638
62639 /*
62640 ** Implementation of the changes() SQL function. The return value is the
62641 ** same as the sqlite3_changes() API function.
62642 */
62643 static void changes(
62644 sqlite3_context *context,
62645 int arg,
62646 sqlite3_value **argv
62647 ){
62648 sqlite3 *db = sqlite3_context_db_handle(context);
 
62649 sqlite3_result_int(context, sqlite3_changes(db));
62650 }
62651
62652 /*
62653 ** Implementation of the total_changes() SQL function. The return value is
62654 ** the same as the sqlite3_total_changes() API function.
62655 */
62656 static void total_changes(
62657 sqlite3_context *context,
62658 int arg,
62659 sqlite3_value **argv
62660 ){
62661 sqlite3 *db = sqlite3_context_db_handle(context);
 
62662 sqlite3_result_int(context, sqlite3_total_changes(db));
62663 }
62664
62665 /*
62666 ** A structure defining how to do GLOB-style comparisons.
@@ -62901,14 +63932,15 @@
62901 ** argument if the arguments are different. The result is NULL if the
62902 ** arguments are equal to each other.
62903 */
62904 static void nullifFunc(
62905 sqlite3_context *context,
62906 int argc,
62907 sqlite3_value **argv
62908 ){
62909 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
 
62910 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
62911 sqlite3_result_value(context, argv[0]);
62912 }
62913 }
62914
@@ -62916,13 +63948,14 @@
62916 ** Implementation of the VERSION(*) function. The result is the version
62917 ** of the SQLite library that is running.
62918 */
62919 static void versionFunc(
62920 sqlite3_context *context,
62921 int argc,
62922 sqlite3_value **argv
62923 ){
 
62924 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
62925 }
62926
62927 /* Array for converting from half-bytes (nybbles) into ASCII hex
62928 ** digits. */
@@ -63011,10 +64044,11 @@
63011 ){
63012 int i, n;
63013 const unsigned char *pBlob;
63014 char *zHex, *z;
63015 assert( argc==1 );
 
63016 pBlob = sqlite3_value_blob(argv[0]);
63017 n = sqlite3_value_bytes(argv[0]);
63018 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
63019 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
63020 if( zHex ){
@@ -63036,10 +64070,11 @@
63036 int argc,
63037 sqlite3_value **argv
63038 ){
63039 i64 n;
63040 assert( argc==1 );
 
63041 n = sqlite3_value_int64(argv[0]);
63042 if( n>SQLITE_MAX_LENGTH ){
63043 sqlite3_result_error_toobig(context);
63044 }else{
63045 sqlite3_result_zeroblob(context, n);
@@ -63067,10 +64102,11 @@
63067 i64 nOut; /* Maximum size of zOut */
63068 int loopLimit; /* Last zStr[] that might match zPattern[] */
63069 int i, j; /* Loop counters */
63070
63071 assert( argc==3 );
 
63072 zStr = sqlite3_value_text(argv[0]);
63073 if( zStr==0 ) return;
63074 nStr = sqlite3_value_bytes(argv[0]);
63075 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
63076 zPattern = sqlite3_value_text(argv[1]);
@@ -63304,10 +64340,11 @@
63304 */
63305 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
63306 SumCtx *p;
63307 int type;
63308 assert( argc==1 );
 
63309 p = sqlite3_aggregate_context(context, sizeof(*p));
63310 type = sqlite3_value_numeric_type(argv[0]);
63311 if( p && type!=SQLITE_NULL ){
63312 p->cnt++;
63313 if( type==SQLITE_INTEGER ){
@@ -63379,13 +64416,18 @@
63379 }
63380
63381 /*
63382 ** Routines to implement min() and max() aggregate functions.
63383 */
63384 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
 
 
 
 
63385 Mem *pArg = (Mem *)argv[0];
63386 Mem *pBest;
 
63387
63388 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
63389 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
63390 if( !pBest ) return;
63391
@@ -63661,11 +64703,11 @@
63661 **
63662 *************************************************************************
63663 ** This file contains C code routines that are called by the parser
63664 ** to handle INSERT statements in SQLite.
63665 **
63666 ** $Id: insert.c,v 1.251 2008/11/03 20:55:07 drh Exp $
63667 */
63668
63669 /*
63670 ** Set P4 of the most recently inserted opcode to a column affinity
63671 ** string for index pIdx. A column affinity string has one character
@@ -64078,11 +65120,11 @@
64078
64079 /* Figure out if we have any triggers and if the table being
64080 ** inserted into is a view
64081 */
64082 #ifndef SQLITE_OMIT_TRIGGER
64083 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
64084 isView = pTab->pSelect!=0;
64085 #else
64086 # define triggers_exist 0
64087 # define isView 0
64088 #endif
@@ -64584,11 +65626,10 @@
64584 pTab,
64585 baseCur,
64586 regIns,
64587 aRegIdx,
64588 0,
64589 0,
64590 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1,
64591 appendFlag
64592 );
64593 }
64594 }
@@ -64920,30 +65961,30 @@
64920 case OE_Rollback:
64921 case OE_Abort:
64922 case OE_Fail: {
64923 int j, n1, n2;
64924 char zErrMsg[200];
64925 sqlite3_snprintf(sizeof(zErrMsg), zErrMsg,
64926 pIdx->nColumn>1 ? "columns " : "column ");
64927 n1 = strlen(zErrMsg);
64928 for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
64929 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
64930 n2 = strlen(zCol);
64931 if( j>0 ){
64932 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], ", ");
64933 n1 += 2;
64934 }
64935 if( n1+n2>sizeof(zErrMsg)-30 ){
64936 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "...");
64937 n1 += 3;
64938 break;
64939 }else{
64940 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol);
64941 n1 += n2;
64942 }
64943 }
64944 sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1],
64945 pIdx->nColumn>1 ? " are not unique" : " is not unique");
64946 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErrMsg,0);
64947 break;
64948 }
64949 case OE_Ignore: {
@@ -64976,11 +66017,10 @@
64976 Parse *pParse, /* The parser context */
64977 Table *pTab, /* the table into which we are inserting */
64978 int baseCur, /* Index of a read/write cursor pointing at pTab */
64979 int regRowid, /* Range of content */
64980 int *aRegIdx, /* Register used by each index. 0 for unused indices */
64981 int rowidChng, /* True if the record number will change */
64982 int isUpdate, /* True for UPDATE, False for INSERT */
64983 int newIdx, /* Index of NEW table for triggers. -1 if none */
64984 int appendBias /* True if this is likely to be an append */
64985 ){
64986 int i;
@@ -66534,11 +67574,11 @@
66534 ** May you share freely, never taking more than you give.
66535 **
66536 *************************************************************************
66537 ** This file contains code used to implement the PRAGMA command.
66538 **
66539 ** $Id: pragma.c,v 1.192 2008/10/31 10:53:23 danielk1977 Exp $
66540 */
66541
66542 /* Ignore this whole file if pragmas are disabled
66543 */
66544 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
@@ -66562,11 +67602,11 @@
66562 int i, n;
66563 if( isdigit(*z) ){
66564 return atoi(z);
66565 }
66566 n = strlen(z);
66567 for(i=0; i<sizeof(iLength); i++){
66568 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
66569 return iValue[i];
66570 }
66571 }
66572 return 1;
@@ -66712,11 +67752,11 @@
66712 ** flag if there are any active statements. */
66713 { "read_uncommitted", SQLITE_ReadUncommitted },
66714 };
66715 int i;
66716 const struct sPragmaType *p;
66717 for(i=0, p=aPragma; i<sizeof(aPragma)/sizeof(aPragma[0]); i++, p++){
66718 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
66719 sqlite3 *db = pParse->db;
66720 Vdbe *v;
66721 v = sqlite3GetVdbe(pParse);
66722 if( v ){
@@ -66976,11 +68016,11 @@
66976 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
66977 }else
66978
66979 /*
66980 ** PRAGMA [database.]journal_mode
66981 ** PRAGMA [database.]journal_mode = (delete|persist|memory|off)
66982 */
66983 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
66984 int eMode;
66985 static char * const azModeName[] = {
66986 "delete", "persist", "off", "truncate", "memory"
@@ -67891,11 +68931,11 @@
67891 *************************************************************************
67892 ** This file contains the implementation of the sqlite3_prepare()
67893 ** interface, and routines that contribute to loading the database schema
67894 ** from disk.
67895 **
67896 ** $Id: prepare.c,v 1.98 2008/10/31 10:53:23 danielk1977 Exp $
67897 */
67898
67899 /*
67900 ** Fill the InitData structure with an error message that indicates
67901 ** that the database is corrupt.
@@ -67928,23 +68968,24 @@
67928 ** argv[0] = name of thing being created
67929 ** argv[1] = root page number for table or index. 0 for trigger or view.
67930 ** argv[2] = SQL text for the CREATE statement.
67931 **
67932 */
67933 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
67934 InitData *pData = (InitData*)pInit;
67935 sqlite3 *db = pData->db;
67936 int iDb = pData->iDb;
67937
 
 
67938 assert( sqlite3_mutex_held(db->mutex) );
67939 DbClearProperty(db, iDb, DB_Empty);
67940 if( db->mallocFailed ){
67941 corruptSchema(pData, argv[0], 0);
67942 return SQLITE_NOMEM;
67943 }
67944
67945 assert( argc==3 );
67946 assert( iDb>=0 && iDb<db->nDb );
67947 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
67948 if( argv[1]==0 ){
67949 corruptSchema(pData, argv[0], 0);
67950 }else if( argv[2] && argv[2][0] ){
@@ -68120,11 +69161,11 @@
68120 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
68121 ** the possible values of meta[4].
68122 */
68123 if( rc==SQLITE_OK ){
68124 int i;
68125 for(i=0; i<sizeof(meta)/sizeof(meta[0]); i++){
68126 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
68127 if( rc ){
68128 sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
68129 goto initone_error_out;
68130 }
@@ -68702,11 +69743,11 @@
68702 **
68703 *************************************************************************
68704 ** This file contains C code routines that are called by the parser
68705 ** to handle SELECT statements in SQLite.
68706 **
68707 ** $Id: select.c,v 1.482 2008/10/31 10:53:23 danielk1977 Exp $
68708 */
68709
68710
68711 /*
68712 ** Delete all the content of a Select structure but do not deallocate
@@ -68754,11 +69795,11 @@
68754 ){
68755 Select *pNew;
68756 Select standin;
68757 sqlite3 *db = pParse->db;
68758 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
68759 assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */
68760 if( pNew==0 ){
68761 pNew = &standin;
68762 memset(pNew, 0, sizeof(*pNew));
68763 }
68764 if( pEList==0 ){
@@ -68833,18 +69874,18 @@
68833 apAll[0] = pA;
68834 apAll[1] = pB;
68835 apAll[2] = pC;
68836 for(i=0; i<3 && apAll[i]; i++){
68837 p = apAll[i];
68838 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
68839 if( p->n==keywords[j].nChar
68840 && sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){
68841 jointype |= keywords[j].code;
68842 break;
68843 }
68844 }
68845 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
68846 jointype |= JT_ERROR;
68847 break;
68848 }
68849 }
68850 if(
@@ -71262,11 +72303,13 @@
71262 struct SrcList_item *pSubitem; /* The subquery */
71263 sqlite3 *db = pParse->db;
71264
71265 /* Check to see if flattening is permitted. Return 0 if not.
71266 */
 
71267 if( p==0 ) return 0;
 
71268 pSrc = p->pSrc;
71269 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
71270 pSubitem = &pSrc->a[iFrom];
71271 iParent = pSubitem->iCursor;
71272 pSub = pSubitem->pSelect;
@@ -71374,96 +72417,150 @@
71374 ** be of the form:
71375 **
71376 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
71377 **
71378 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
71379 ** creates N copies of the parent query without any ORDER BY, LIMIT or
71380 ** OFFSET clauses and joins them to the left-hand-side of the original
71381 ** using UNION ALL operators. In this case N is the number of simple
71382 ** select statements in the compound sub-query.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71383 */
71384 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
71385 Select *pNew;
71386 ExprList *pOrderBy = p->pOrderBy;
71387 Expr *pLimit = p->pLimit;
71388 Expr *pOffset = p->pOffset;
71389 Select *pPrior = p->pPrior;
71390 p->pOrderBy = 0;
71391 p->pSrc = 0;
71392 p->pPrior = 0;
71393 p->pLimit = 0;
71394 pNew = sqlite3SelectDup(db, p);
71395 pNew->pPrior = pPrior;
71396 p->pPrior = pNew;
71397 p->pOrderBy = pOrderBy;
 
71398 p->op = TK_ALL;
71399 p->pSrc = pSrc;
71400 p->pLimit = pLimit;
71401 p->pOffset = pOffset;
71402 p->pRightmost = 0;
71403 pNew->pRightmost = 0;
 
 
 
 
 
 
 
71404 }
71405
71406 /* Begin flattening the iFrom-th entry of the FROM clause
71407 ** in the outer query.
71408 */
71409 pSub = pSub1 = pSubitem->pSelect;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71410 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
71411 int nSubSrc = pSubSrc->nSrc;
71412 int jointype = 0;
71413 pSubSrc = pSub->pSrc;
71414 pSrc = pParent->pSrc;
71415
71416 /* Move all of the FROM elements of the subquery into the
71417 ** the FROM clause of the outer query. Before doing this, remember
71418 ** the cursor number for the original outer query FROM element in
71419 ** iParent. The iParent cursor will never be used. Subsequent code
71420 ** will scan expressions looking for iParent references and replace
71421 ** those references with expressions that resolve to the subquery FROM
71422 ** elements we are now copying in.
71423 */
71424 if( pSrc ){
71425 Table *pTabToDel;
71426 pSubitem = &pSrc->a[iFrom];
71427 nSubSrc = pSubSrc->nSrc;
71428 jointype = pSubitem->jointype;
71429 sqlite3DbFree(db, pSubitem->zDatabase);
71430 sqlite3DbFree(db, pSubitem->zName);
71431 sqlite3DbFree(db, pSubitem->zAlias);
71432 pSubitem->zDatabase = 0;
71433 pSubitem->zName = 0;
71434 pSubitem->zAlias = 0;
71435
71436 /* If the FROM element is a subquery, defer deleting the Table
71437 ** object associated with that subquery until code generation is
71438 ** complete, since there may still exist Expr.pTab entires that
71439 ** refer to the subquery even after flattening. Ticket #3346.
71440 */
71441 if( (pTabToDel = pSubitem->pTab)!=0 ){
71442 if( pTabToDel->nRef==1 ){
71443 pTabToDel->pNextZombie = pParse->pZombieTab;
71444 pParse->pZombieTab = pTabToDel;
71445 }else{
71446 pTabToDel->nRef--;
71447 }
71448 }
71449 pSubitem->pTab = 0;
71450 }
71451 if( nSubSrc!=1 || !pSrc ){
71452 int extra = nSubSrc - 1;
71453 for(i=(pSrc?1:0); i<nSubSrc; i++){
71454 pSrc = sqlite3SrcListAppend(db, pSrc, 0, 0);
71455 if( pSrc==0 ){
71456 pParent->pSrc = 0;
71457 return 1;
71458 }
71459 }
71460 pParent->pSrc = pSrc;
71461 for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
71462 pSrc->a[i] = pSrc->a[i-extra];
71463 }
71464 }
71465 for(i=0; i<nSubSrc; i++){
71466 pSrc->a[i+iFrom] = pSubSrc->a[i];
71467 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
71468 }
71469 pSrc->a[iFrom].jointype = jointype;
@@ -71554,11 +72651,11 @@
71554 ** 1. There is a single object in the FROM clause.
71555 **
71556 ** 2. There is a single expression in the result set, and it is
71557 ** either min(x) or max(x), where x is a column reference.
71558 */
71559 static int minMaxQuery(Parse *pParse, Select *p){
71560 Expr *pExpr;
71561 ExprList *pEList = p->pEList;
71562
71563 if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
71564 pExpr = pEList->a[0].pExpr;
@@ -71854,11 +72951,12 @@
71854 ** are walked without any actions being taken at each node. Presumably,
71855 ** when this routine is used for Walker.xExprCallback then
71856 ** Walker.xSelectCallback is set to do something useful for every
71857 ** subquery in the parser tree.
71858 */
71859 static int exprWalkNoop(Walker *pWalker, Expr *pExpr){
 
71860 return WRC_Continue;
71861 }
71862
71863 /*
71864 ** This routine "expands" a SELECT statement and all of its subqueries.
@@ -72645,11 +73743,11 @@
72645 ** + The optimizer code in where.c (the thing that decides which
72646 ** index or indices to use) should place a different priority on
72647 ** satisfying the 'ORDER BY' clause than it does in other cases.
72648 ** Refer to code and comments in where.c for details.
72649 */
72650 flag = minMaxQuery(pParse, p);
72651 if( flag ){
72652 pDel = pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->pList);
72653 if( pMinMax && !db->mallocFailed ){
72654 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN;
72655 pMinMax->a[0].pExpr->op = TK_COLUMN;
@@ -73027,11 +74125,11 @@
73027 ** May you share freely, never taking more than you give.
73028 **
73029 *************************************************************************
73030 **
73031 **
73032 ** $Id: trigger.c,v 1.129 2008/08/20 16:35:10 drh Exp $
73033 */
73034
73035 #ifndef SQLITE_OMIT_TRIGGER
73036 /*
73037 ** Delete a linked list of TriggerStep structures.
@@ -73619,11 +74717,10 @@
73619 **
73620 ** The returned bit vector is some combination of TRIGGER_BEFORE and
73621 ** TRIGGER_AFTER.
73622 */
73623 SQLITE_PRIVATE int sqlite3TriggersExist(
73624 Parse *pParse, /* Used to check for recursive triggers */
73625 Table *pTab, /* The table the contains the triggers */
73626 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
73627 ExprList *pChanges /* Columns that change in an UPDATE statement */
73628 ){
73629 Trigger *pTrigger;
@@ -73883,11 +74980,11 @@
73883 **
73884 *************************************************************************
73885 ** This file contains C code routines that are called by the parser
73886 ** to handle UPDATE statements.
73887 **
73888 ** $Id: update.c,v 1.186 2008/10/31 10:53:23 danielk1977 Exp $
73889 */
73890
73891 #ifndef SQLITE_OMIT_VIRTUALTABLE
73892 /* Forward declaration */
73893 static void updateVirtualTable(
@@ -74012,11 +75109,11 @@
74012
74013 /* Figure out if we have any triggers and if the table being
74014 ** updated is a view
74015 */
74016 #ifndef SQLITE_OMIT_TRIGGER
74017 triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges);
74018 isView = pTab->pSelect!=0;
74019 #else
74020 # define triggers_exist 0
74021 # define isView 0
74022 #endif
@@ -74395,11 +75492,11 @@
74395 sqlite3VdbeJumpHere(v, j1);
74396
74397 /* Create the new index entries and the new record.
74398 */
74399 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid,
74400 aRegIdx, chngRowid, 1, -1, 0);
74401 }
74402
74403 /* Increment the row counter
74404 */
74405 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
@@ -74568,11 +75665,11 @@
74568 ** This file contains code used to implement the VACUUM command.
74569 **
74570 ** Most of the code in this file may be omitted by defining the
74571 ** SQLITE_OMIT_VACUUM macro.
74572 **
74573 ** $Id: vacuum.c,v 1.83 2008/08/26 21:07:27 drh Exp $
74574 */
74575
74576 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
74577 /*
74578 ** Execute zSql on database db. Return an error code.
@@ -74803,11 +75900,11 @@
74803
74804 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
74805 assert( 1==sqlite3BtreeIsInTrans(pMain) );
74806
74807 /* Copy Btree meta values */
74808 for(i=0; i<sizeof(aCopy)/sizeof(aCopy[0]); i+=2){
74809 rc = sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
74810 if( rc!=SQLITE_OK ) goto end_of_vacuum;
74811 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
74812 if( rc!=SQLITE_OK ) goto end_of_vacuum;
74813 }
@@ -74866,11 +75963,11 @@
74866 ** May you share freely, never taking more than you give.
74867 **
74868 *************************************************************************
74869 ** This file contains code used to help implement virtual tables.
74870 **
74871 ** $Id: vtab.c,v 1.76 2008/08/20 16:35:10 drh Exp $
74872 */
74873 #ifndef SQLITE_OMIT_VIRTUALTABLE
74874
74875 static int createModule(
74876 sqlite3 *db, /* Database in which module is registered */
@@ -74900,10 +75997,12 @@
74900 sqlite3DbFree(db, pDel);
74901 if( pDel==pMod ){
74902 db->mallocFailed = 1;
74903 }
74904 sqlite3ResetInternalSchema(db, 0);
 
 
74905 }
74906 rc = sqlite3ApiExit(db, SQLITE_OK);
74907 sqlite3_mutex_leave(db->mutex);
74908 return rc;
74909 }
@@ -75564,11 +76663,11 @@
75564 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
75565 ** than zero, then this function is being called from within a
75566 ** virtual module xSync() callback. It is illegal to write to
75567 ** virtual module tables in this case, so return SQLITE_LOCKED.
75568 */
75569 if( 0==db->aVTrans && db->nVTrans>0 ){
75570 return SQLITE_LOCKED;
75571 }
75572 if( !pVtab ){
75573 return SQLITE_OK;
75574 }
@@ -75712,18 +76811,13 @@
75712 ** generating the code that loops through a table looking for applicable
75713 ** rows. Indices are selected and used to speed the search when doing
75714 ** so is applicable. Because this module is responsible for selecting
75715 ** indices, you might also think of this module as the "query optimizer".
75716 **
75717 ** $Id: where.c,v 1.328 2008/11/03 09:06:06 danielk1977 Exp $
75718 */
75719
75720 /*
75721 ** The number of bits in a Bitmask. "BMS" means "BitMask Size".
75722 */
75723 #define BMS (sizeof(Bitmask)*8)
75724
75725 /*
75726 ** Trace output macros
75727 */
75728 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
75729 SQLITE_PRIVATE int sqlite3WhereTrace = 0;
@@ -75834,11 +76928,11 @@
75834 ** numbers all get mapped into bit numbers that begin with 0 and contain
75835 ** no gaps.
75836 */
75837 struct ExprMaskSet {
75838 int n; /* Number of assigned cursor values */
75839 int ix[sizeof(Bitmask)*8]; /* Cursor assigned to each bit */
75840 };
75841
75842
75843 /*
75844 ** Bitmasks for the operators that indices are able to exploit. An
@@ -78015,11 +79109,11 @@
78015 }else
78016 #endif
78017 if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
78018 int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
78019 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
78020 if( !pWInfo->okOnePass && pTab->nCol<(sizeof(Bitmask)*8) ){
78021 Bitmask b = pTabItem->colUsed;
78022 int n = 0;
78023 for(; b; b=b>>1, n++){}
78024 sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-2, n);
78025 assert( n<=pTab->nCol );
@@ -79896,10 +80990,12 @@
79896 pParser->yyidx = -1;
79897 #ifdef YYTRACKMAXSTACKDEPTH
79898 pParser->yyidxMax = 0;
79899 #endif
79900 #if YYSTACKDEPTH<=0
 
 
79901 yyGrowStack(pParser);
79902 #endif
79903 }
79904 return pParser;
79905 }
@@ -82745,11 +83841,11 @@
82745 ** Main file for the SQLite library. The routines in this file
82746 ** implement the programmer interface to the library. Routines in
82747 ** other files are for internal use by SQLite and should not be
82748 ** accessed by users of the library.
82749 **
82750 ** $Id: main.c,v 1.510 2008/11/04 13:46:28 drh Exp $
82751 */
82752
82753 #ifdef SQLITE_ENABLE_FTS3
82754 /************** Include fts3.h in the middle of main.c ***********************/
82755 /************** Begin file fts3.h ********************************************/
@@ -83125,10 +84221,24 @@
83125 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
83126 sqlite3GlobalConfig.szPage = va_arg(ap, int);
83127 sqlite3GlobalConfig.nPage = va_arg(ap, int);
83128 break;
83129 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83130
83131 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
83132 case SQLITE_CONFIG_HEAP: {
83133 /* Designate a buffer for heap memory space */
83134 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
@@ -83155,18 +84265,10 @@
83155 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
83156 #endif
83157 }
83158 break;
83159 }
83160 #endif
83161
83162 #if defined(SQLITE_ENABLE_MEMSYS6)
83163 case SQLITE_CONFIG_CHUNKALLOC: {
83164 sqlite3GlobalConfig.nSmall = va_arg(ap, int);
83165 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys6();
83166 break;
83167 }
83168 #endif
83169
83170 case SQLITE_CONFIG_LOOKASIDE: {
83171 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
83172 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
@@ -83326,10 +84428,11 @@
83326 int nKey1, const void *pKey1,
83327 int nKey2, const void *pKey2
83328 ){
83329 int r = sqlite3StrNICmp(
83330 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
 
83331 if( 0==r ){
83332 r = nKey1-nKey2;
83333 }
83334 return r;
83335 }
@@ -92737,10 +93840,17 @@
92737 elem->data = data;
92738 }
92739 return old_data;
92740 }
92741 if( data==0 ) return 0;
 
 
 
 
 
 
 
92742 new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) );
92743 if( new_elem==0 ) return data;
92744 if( pH->copyKey && pKey!=0 ){
92745 new_elem->pKey = fts3HashMalloc( nKey );
92746 if( new_elem->pKey==0 ){
@@ -92751,18 +93861,10 @@
92751 }else{
92752 new_elem->pKey = (void*)pKey;
92753 }
92754 new_elem->nKey = nKey;
92755 pH->count++;
92756 if( pH->htsize==0 ){
92757 fts3Rehash(pH,8);
92758 if( pH->htsize==0 ){
92759 pH->count = 0;
92760 fts3HashFree(new_elem);
92761 return data;
92762 }
92763 }
92764 if( pH->count > pH->htsize ){
92765 fts3Rehash(pH,pH->htsize*2);
92766 }
92767 assert( pH->htsize>0 );
92768 assert( (pH->htsize & (pH->htsize-1))==0 );
@@ -94022,11 +95124,11 @@
94022 **
94023 *************************************************************************
94024 ** This file contains code for implementations of the r-tree and r*-tree
94025 ** algorithms packaged as an SQLite virtual table module.
94026 **
94027 ** $Id: rtree.c,v 1.10 2008/10/25 17:10:10 danielk1977 Exp $
94028 */
94029
94030 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
94031
94032 /*
@@ -94232,12 +95334,16 @@
94232 struct RtreeCell {
94233 i64 iRowid;
94234 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
94235 };
94236
94237 #define MAX(x,y) ((x) < (y) ? (y) : (x))
94238 #define MIN(x,y) ((x) > (y) ? (y) : (x))
 
 
 
 
94239
94240 /*
94241 ** Functions to deserialize a 16 bit integer, 32 bit real number and
94242 ** 64 bit integer. The deserialized value is returned.
94243 */
94244
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.6.6.1. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% are more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -9,17 +9,17 @@
9 **
10 ** This file is all you need to compile SQLite. To use SQLite in other
11 ** programs, you need this file and the "sqlite3.h" header file that defines
12 ** the programming interface to the SQLite library. (If you do not have
13 ** the "sqlite3.h" header file at hand, you will find a copy in the first
14 ** 6728 lines past this header comment.) Additional code files may be
15 ** needed if you want a wrapper to interface SQLite with your choice of
16 ** programming language. The code for the "sqlite3" command-line shell
17 ** is also in a separate file. This file contains only code for the core
18 ** SQLite library.
19 **
20 ** This amalgamation was generated on 2008-11-22 14:31:32 UTC.
21 */
22 #define SQLITE_CORE 1
23 #define SQLITE_AMALGAMATION 1
24 #ifndef SQLITE_PRIVATE
25 # define SQLITE_PRIVATE static
@@ -39,11 +39,11 @@
39 ** May you share freely, never taking more than you give.
40 **
41 *************************************************************************
42 ** Internal interface definitions for SQLite.
43 **
44 ** @(#) $Id: sqliteInt.h,v 1.798 2008/11/19 16:52:44 danielk1977 Exp $
45 */
46 #ifndef _SQLITEINT_H_
47 #define _SQLITEINT_H_
48
49 /*
@@ -485,11 +485,11 @@
485 ** The name of this file under configuration management is "sqlite.h.in".
486 ** The makefile makes some minor changes to this file (such as inserting
487 ** the version number) and changes its name to "sqlite3.h" as
488 ** part of the build process.
489 **
490 ** @(#) $Id: sqlite.h.in,v 1.415 2008/11/19 01:20:26 drh Exp $
491 */
492 #ifndef _SQLITE3_H_
493 #define _SQLITE3_H_
494 #include <stdarg.h> /* Needed for the definition of va_list */
495
@@ -562,12 +562,12 @@
562 **
563 ** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer
564 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z
565 ** are the major version, minor version, and release number.
566 */
567 #define SQLITE_VERSION "3.6.6.1"
568 #define SQLITE_VERSION_NUMBER 3006006
569
570 /*
571 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
572 ** KEYWORDS: sqlite3_version
573 **
@@ -1042,11 +1042,11 @@
1042 **
1043 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1044 ** sync operation only needs to flush data to mass storage. Inode
1045 ** information need not be flushed. The SQLITE_SYNC_NORMAL flag means
1046 ** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means
1047 ** to use Mac OS X style fullsync instead of fsync().
1048 */
1049 #define SQLITE_SYNC_NORMAL 0x00002
1050 #define SQLITE_SYNC_FULL 0x00003
1051 #define SQLITE_SYNC_DATAONLY 0x00010
1052
@@ -1074,11 +1074,11 @@
1074 ** This object defines the methods used to perform various operations
1075 ** against the open file represented by the [sqlite3_file] object.
1076 **
1077 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1078 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1079 ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1080 ** flag may be ORed in to indicate that only the data of the file
1081 ** and not its inode needs to be synced.
1082 **
1083 ** The integer values to xLock() and xUnlock() are one of
1084 ** <ul>
@@ -1729,11 +1729,14 @@
1729 ** scratch buffers or if no scratch buffer space is specified, then SQLite
1730 ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
1731 **
1732 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1733 ** <dd>This option specifies a static memory buffer that SQLite can use for
1734 ** the database page cache with the default page cache implemenation.
1735 ** This configuration should not be used if an application-define page
1736 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1737 ** There are three arguments to this option: A pointer to the
1738 ** memory, the size of each page buffer (sz), and the number of pages (N).
1739 ** The sz argument must be a power of two between 512 and 32768. The first
1740 ** argument should point to an allocation of at least sz*N bytes of memory.
1741 ** SQLite will use the memory provided by the first argument to satisfy its
1742 ** memory needs for the first N pages that it adds to cache. If additional
@@ -1774,10 +1777,21 @@
1777 ** <dd>This option takes two arguments that determine the default
1778 ** memory allcation lookaside optimization. The first argument is the
1779 ** size of each lookaside buffer slot and the second is the number of
1780 ** slots allocated to each database connection.</dd>
1781 **
1782 ** <dt>SQLITE_CONFIG_PCACHE</dt>
1783 ** <dd>This option takes a single argument which is a pointer to
1784 ** an [sqlite3_pcache_methods] object. This object specifies the interface
1785 ** to a custom page cache implementation. SQLite makes a copy of the
1786 ** object and uses it for page cache memory allocations.</dd>
1787 **
1788 ** <dt>SQLITE_CONFIG_GETPCACHE</dt>
1789 ** <dd>This option takes a single argument which is a pointer to an
1790 ** [sqlite3_pcache_methods] object. SQLite copies of the current
1791 ** page cache implementation into that object.</dd>
1792 **
1793 ** </dl>
1794 */
1795 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1796 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1797 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1787,12 +1801,14 @@
1801 #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
1802 #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
1803 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
1804 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
1805 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1806 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
1807 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
1808 #define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
1809 #define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
1810
1811 /*
1812 ** CAPI3REF: Configuration Options {H10170} <S20000>
1813 ** EXPERIMENTAL
1814 **
@@ -2838,11 +2854,11 @@
2854 #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
2855 #define SQLITE_REINDEX 27 /* Index Name NULL */
2856 #define SQLITE_ANALYZE 28 /* Table Name NULL */
2857 #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
2858 #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
2859 #define SQLITE_FUNCTION 31 /* NULL Function Name */
2860 #define SQLITE_COPY 0 /* No longer used */
2861
2862 /*
2863 ** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400>
2864 ** EXPERIMENTAL
@@ -4606,11 +4622,11 @@
4622 **
4623 ** These functions are [deprecated]. In order to maintain
4624 ** backwards compatibility with older code, these functions continue
4625 ** to be supported. However, new applications should avoid
4626 ** the use of these functions. To help encourage people to avoid
4627 ** using these functions, we are not going to tell you what they do.
4628 */
4629 #ifndef SQLITE_OMIT_DEPRECATED
4630 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4631 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4632 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
@@ -7010,10 +7026,153 @@
7026 ** </dl>
7027 */
7028 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
7029 #define SQLITE_STMTSTATUS_SORT 2
7030
7031 /*
7032 ** CAPI3REF: Custom Page Cache Object
7033 ** EXPERIMENTAL
7034 **
7035 ** The sqlite3_pcache type is opaque. It is implemented by
7036 ** the pluggable module. The SQLite core has no knowledge of
7037 ** its size or internal structure and never deals with the
7038 ** sqlite3_pcache object except by holding and passing pointers
7039 ** to the object.
7040 **
7041 ** See [sqlite3_pcache_methods] for additional information.
7042 */
7043 typedef struct sqlite3_pcache sqlite3_pcache;
7044
7045 /*
7046 ** CAPI3REF: Application Defined Page Cache.
7047 ** EXPERIMENTAL
7048 **
7049 ** The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
7050 ** register an alternative page cache implementation by passing in an
7051 ** instance of the sqlite3_pcache_methods structure. The majority of the
7052 ** heap memory used by sqlite is used by the page cache to cache data read
7053 ** from, or ready to be written to, the database file. By implementing a
7054 ** custom page cache using this API, an application can control more
7055 ** precisely the amount of memory consumed by sqlite, the way in which
7056 ** said memory is allocated and released, and the policies used to
7057 ** determine exactly which parts of a database file are cached and for
7058 ** how long.
7059 **
7060 ** The contents of the structure are copied to an internal buffer by sqlite
7061 ** within the call to [sqlite3_config].
7062 **
7063 ** The xInit() method is called once for each call to [sqlite3_initialize()]
7064 ** (usually only once during the lifetime of the process). It is passed
7065 ** a copy of the sqlite3_pcache_methods.pArg value. It can be used to set
7066 ** up global structures and mutexes required by the custom page cache
7067 ** implementation. The xShutdown() method is called from within
7068 ** [sqlite3_shutdown()], if the application invokes this API. It can be used
7069 ** to clean up any outstanding resources before process shutdown, if required.
7070 **
7071 ** The xCreate() method is used to construct a new cache instance. The
7072 ** first parameter, szPage, is the size in bytes of the pages that must
7073 ** be allocated by the cache. szPage will not be a power of two. The
7074 ** second argument, bPurgeable, is true if the cache being created will
7075 ** be used to cache database pages read from a file stored on disk, or
7076 ** false if it is used for an in-memory database. The cache implementation
7077 ** does not have to do anything special based on the value of bPurgeable,
7078 ** it is purely advisory.
7079 **
7080 ** The xCachesize() method may be called at any time by SQLite to set the
7081 ** suggested maximum cache-size (number of pages stored by) the cache
7082 ** instance passed as the first argument. This is the value configured using
7083 ** the SQLite "[PRAGMA cache_size]" command. As with the bPurgeable parameter,
7084 ** the implementation is not required to do anything special with this
7085 ** value, it is advisory only.
7086 **
7087 ** The xPagecount() method should return the number of pages currently
7088 ** stored in the cache supplied as an argument.
7089 **
7090 ** The xFetch() method is used to fetch a page and return a pointer to it.
7091 ** A 'page', in this context, is a buffer of szPage bytes aligned at an
7092 ** 8-byte boundary. The page to be fetched is determined by the key. The
7093 ** mimimum key value is 1. After it has been retrieved using xFetch, the page
7094 ** is considered to be pinned.
7095 **
7096 ** If the requested page is already in the page cache, then a pointer to
7097 ** the cached buffer should be returned with its contents intact. If the
7098 ** page is not already in the cache, then the expected behaviour of the
7099 ** cache is determined by the value of the createFlag parameter passed
7100 ** to xFetch, according to the following table:
7101 **
7102 ** <table border=1 width=85% align=center>
7103 ** <tr><th>createFlag<th>Expected Behaviour
7104 ** <tr><td>0<td>NULL should be returned. No new cache entry is created.
7105 ** <tr><td>1<td>If createFlag is set to 1, this indicates that
7106 ** SQLite is holding pinned pages that can be unpinned
7107 ** by writing their contents to the database file (a
7108 ** relatively expensive operation). In this situation the
7109 ** cache implementation has two choices: it can return NULL,
7110 ** in which case SQLite will attempt to unpin one or more
7111 ** pages before re-requesting the same page, or it can
7112 ** allocate a new page and return a pointer to it. If a new
7113 ** page is allocated, then it must be completely zeroed before
7114 ** it is returned.
7115 ** <tr><td>2<td>If createFlag is set to 2, then SQLite is not holding any
7116 ** pinned pages associated with the specific cache passed
7117 ** as the first argument to xFetch() that can be unpinned. The
7118 ** cache implementation should attempt to allocate a new
7119 ** cache entry and return a pointer to it. Again, the new
7120 ** page should be zeroed before it is returned. If the xFetch()
7121 ** method returns NULL when createFlag==2, SQLite assumes that
7122 ** a memory allocation failed and returns SQLITE_NOMEM to the
7123 ** user.
7124 ** </table>
7125 **
7126 ** xUnpin() is called by SQLite with a pointer to a currently pinned page
7127 ** as its second argument. If the third parameter, discard, is non-zero,
7128 ** then the page should be evicted from the cache. In this case SQLite
7129 ** assumes that the next time the page is retrieved from the cache using
7130 ** the xFetch() method, it will be zeroed. If the discard parameter is
7131 ** zero, then the page is considered to be unpinned. The cache implementation
7132 ** may choose to reclaim (free or recycle) unpinned pages at any time.
7133 ** SQLite assumes that next time the page is retrieved from the cache
7134 ** it will either be zeroed, or contain the same data that it did when it
7135 ** was unpinned.
7136 **
7137 ** The cache is not required to perform any reference counting. A single
7138 ** call to xUnpin() unpins the page regardless of the number of prior calls
7139 ** to xFetch().
7140 **
7141 ** The xRekey() method is used to change the key value associated with the
7142 ** page passed as the second argument from oldKey to newKey. If the cache
7143 ** previously contains an entry associated with newKey, it should be
7144 ** discarded. Any prior cache entry associated with newKey is guaranteed not
7145 ** to be pinned.
7146 **
7147 ** When SQLite calls the xTruncate() method, the cache must discard all
7148 ** existing cache entries with page numbers (keys) greater than or equal
7149 ** to the value of the iLimit parameter passed to xTruncate(). If any
7150 ** of these pages are pinned, they are implicitly unpinned, meaning that
7151 ** they can be safely discarded.
7152 **
7153 ** The xDestroy() method is used to delete a cache allocated by xCreate().
7154 ** All resources associated with the specified cache should be freed. After
7155 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
7156 ** handle invalid, and will not use it with any other sqlite3_pcache_methods
7157 ** functions.
7158 */
7159 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
7160 struct sqlite3_pcache_methods {
7161 void *pArg;
7162 int (*xInit)(void*);
7163 void (*xShutdown)(void*);
7164 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
7165 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
7166 int (*xPagecount)(sqlite3_pcache*);
7167 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
7168 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
7169 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
7170 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
7171 void (*xDestroy)(sqlite3_pcache*);
7172 };
7173
7174 /*
7175 ** Undo the hack that converts floating point types to integer for
7176 ** builds on processors without floating point support.
7177 */
7178 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -7479,11 +7638,11 @@
7638
7639 /*
7640 ** A convenience macro that returns the number of elements in
7641 ** an array.
7642 */
7643 #define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
7644
7645 /*
7646 ** The following value as a destructor means to use sqlite3DbFree().
7647 ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
7648 */
@@ -7498,11 +7657,11 @@
7657 ** macro is used for this purpose. And instead of referencing the variable
7658 ** directly, we use its constant as a key to lookup the run-time allocated
7659 ** buffer that holds real variable. The constant is also the initializer
7660 ** for the run-time allocated buffer.
7661 **
7662 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
7663 ** macros become no-ops and have zero performance impact.
7664 */
7665 #ifdef SQLITE_OMIT_WSD
7666 #define SQLITE_WSD const
7667 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
@@ -7513,10 +7672,29 @@
7672 #define SQLITE_WSD
7673 #define GLOBAL(t,v) v
7674 #define sqlite3GlobalConfig sqlite3Config
7675 #endif
7676
7677 /*
7678 ** The following macros are used to suppress compiler warnings and to
7679 ** make it clear to human readers when a function parameter is deliberately
7680 ** left unused within the body of a function. This usually happens when
7681 ** a function is called via a function pointer. For example the
7682 ** implementation of an SQL aggregate step callback may not use the
7683 ** parameter indicating the number of arguments passed to the aggregate,
7684 ** if it knows that this is enforced elsewhere.
7685 **
7686 ** When a function parameter is not used at all within the body of a function,
7687 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
7688 ** However, these macros may also be used to suppress warnings related to
7689 ** parameters that may or may not be used depending on compilation options.
7690 ** For example those parameters only used in assert() statements. In these
7691 ** cases the parameters are named as per the usual conventions.
7692 */
7693 #define UNUSED_PARAMETER(x) (void)(x)
7694 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
7695
7696 /*
7697 ** Forward references to structures
7698 */
7699 typedef struct AggInfo AggInfo;
7700 typedef struct AuthContext AuthContext;
@@ -8195,11 +8373,11 @@
8373 *************************************************************************
8374 ** This header file defines the interface that the sqlite page cache
8375 ** subsystem. The page cache subsystem reads and writes a file a page
8376 ** at a time and provides a journal for rollback.
8377 **
8378 ** @(#) $Id: pager.h,v 1.87 2008/11/19 10:22:33 danielk1977 Exp $
8379 */
8380
8381 #ifndef _PAGER_H_
8382 #define _PAGER_H_
8383
@@ -8255,11 +8433,11 @@
8433 /*
8434 ** See source code comments for a detailed description of the following
8435 ** routines:
8436 */
8437 SQLITE_PRIVATE int sqlite3PagerOpen(sqlite3_vfs *, Pager **ppPager, const char*, int,int,int);
8438 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
8439 SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*));
8440 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*);
8441 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
8442 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
8443 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
@@ -8341,11 +8519,11 @@
8519 **
8520 *************************************************************************
8521 ** This header file defines the interface that the sqlite page cache
8522 ** subsystem.
8523 **
8524 ** @(#) $Id: pcache.h,v 1.16 2008/11/19 16:52:44 danielk1977 Exp $
8525 */
8526
8527 #ifndef _PCACHE_H_
8528
8529 typedef struct PgHdr PgHdr;
@@ -8363,29 +8541,23 @@
8541 Pager *pPager; /* The pager this page is part of */
8542 #ifdef SQLITE_CHECK_PAGES
8543 u32 pageHash; /* Hash of page content */
8544 #endif
8545 u16 flags; /* PGHDR flags defined below */
8546
8547 /**********************************************************************
8548 ** Elements above are public. All that follows is private to pcache.c
8549 ** and should not be accessed by other modules.
8550 */
8551 i16 nRef; /* Number of users of this page */
8552 PCache *pCache; /* Cache that owns this page */
8553
8554 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
8555 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
 
 
 
 
 
 
8556 };
8557
8558 /* Bit values for PgHdr.flags */
 
8559 #define PGHDR_DIRTY 0x002 /* Page has changed */
8560 #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
8561 ** writing this page to the database */
8562 #define PGHDR_NEED_READ 0x008 /* Content is unread */
8563 #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
@@ -8397,12 +8569,10 @@
8569
8570 /* Page cache buffer management:
8571 ** These routines implement SQLITE_CONFIG_PAGECACHE.
8572 */
8573 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
 
 
8574
8575 /* Create a new pager cache.
8576 ** Under memory stress, invoke xStress to try to make pages clean.
8577 ** Only clean and unpinned pages can be reclaimed.
8578 */
@@ -8445,21 +8615,11 @@
8615
8616 /* Reset and close the cache object */
8617 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
8618
8619 /* Clear flags from pages of the page cache */
8620 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
 
 
 
 
 
 
 
 
 
 
8621
8622 /* Discard the contents of the cache */
8623 SQLITE_PRIVATE int sqlite3PcacheClear(PCache*);
8624
8625 /* Return the total number of outstanding page references */
@@ -8472,34 +8632,38 @@
8632
8633 /* Return the total number of pages stored in the cache */
8634 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
8635
8636 #ifdef SQLITE_CHECK_PAGES
8637 /* Iterate through all dirty pages currently stored in the cache. This
8638 ** interface is only available if SQLITE_CHECK_PAGES is defined when the
8639 ** library is built.
8640 */
8641 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
8642 #endif
8643
8644 /* Set and get the suggested cache-size for the specified pager-cache.
8645 **
8646 ** If no global maximum is configured, then the system attempts to limit
8647 ** the total number of pages cached by purgeable pager-caches to the sum
8648 ** of the suggested cache-sizes.
8649 */
 
8650 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
8651 #ifdef SQLITE_TEST
8652 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8653 #endif
8654
8655 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
8656 /* Try to return memory used by the pcache module to the main memory heap */
8657 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
8658 #endif
8659
8660 #ifdef SQLITE_TEST
8661 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
8662 #endif
8663
8664 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
8665
8666 #endif /* _PCACHE_H_ */
8667
8668 /************** End of pcache.h **********************************************/
8669 /************** Continuing where we left off in sqliteInt.h ******************/
@@ -9017,11 +9181,11 @@
9181 int nextPagesize; /* Pagesize after VACUUM if >0 */
9182 int nTable; /* Number of tables in the database */
9183 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
9184 i64 lastRowid; /* ROWID of most recent insert (see above) */
9185 i64 priorNewRowid; /* Last randomly generated ROWID */
9186 u32 magic; /* Magic number for detect library misuse */
9187 int nChange; /* Value returned by sqlite3_changes() */
9188 int nTotalChange; /* Value returned by sqlite3_total_changes() */
9189 sqlite3_mutex *mutex; /* Connection mutex */
9190 int aLimit[SQLITE_N_LIMIT]; /* Limits */
9191 struct sqlite3InitInfo { /* Information used during initialization */
@@ -9180,17 +9344,17 @@
9344 ** available as the function user-data (sqlite3_user_data()). The
9345 ** FuncDef.flags variable is set to the value passed as the flags
9346 ** parameter.
9347 */
9348 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
9349 {nArg, SQLITE_UTF8, bNC*8, SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0}
9350 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
9351 {nArg, SQLITE_UTF8, bNC*8, pArg, 0, xFunc, 0, 0, #zName, 0}
9352 #define LIKEFUNC(zName, nArg, arg, flags) \
9353 {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0}
9354 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
9355 {nArg, SQLITE_UTF8, nc*8, SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0}
9356
9357
9358 /*
9359 ** Each SQLite module (virtual table definition) is defined by an
9360 ** instance of the following structure, stored in the sqlite3.aModule
@@ -9755,10 +9919,15 @@
9919 ** tables in a join to 32 instead of 64. But it also reduces the size
9920 ** of the library by 738 bytes on ix86.
9921 */
9922 typedef u64 Bitmask;
9923
9924 /*
9925 ** The number of bits in a Bitmask. "BMS" means "BitMask Size".
9926 */
9927 #define BMS ((int)(sizeof(Bitmask)*8))
9928
9929 /*
9930 ** The following structure describes the FROM clause of a SELECT statement.
9931 ** Each table or subquery in the FROM clause is a separate element of
9932 ** the SrcList.a[] array.
9933 **
@@ -10290,10 +10459,11 @@
10459 int mxStrlen; /* Maximum string length */
10460 int szLookaside; /* Default lookaside buffer size */
10461 int nLookaside; /* Default lookaside buffer count */
10462 sqlite3_mem_methods m; /* Low-level memory allocation interface */
10463 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
10464 sqlite3_pcache_methods pcache; /* Low-level page-cache interface */
10465 void *pHeap; /* Heap storage space */
10466 int nHeap; /* Size of pHeap[] */
10467 int mnReq, mxReq; /* Min and max heap requests sizes */
10468 void *pScratch; /* Scratch memory */
10469 int szScratch; /* Size of each scratch buffer */
@@ -10304,11 +10474,10 @@
10474 int isInit; /* True after initialization has finished */
10475 int inProgress; /* True while initialization in progress */
10476 int isMallocInit; /* True after malloc is initialized */
10477 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
10478 int nRefInitMutex; /* Number of users of pInitMutex */
 
10479 int mxParserStack; /* maximum depth of the parser stack */
10480 int sharedCacheEnabled; /* true if shared-cache mode enabled */
10481 };
10482
10483 /*
@@ -10387,14 +10556,20 @@
10556 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
10557 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
10558 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
10559 SQLITE_PRIVATE void sqlite3PageFree(void*);
10560 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
 
 
10561 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
10562 SQLITE_PRIVATE int sqlite3MemoryAlarm(void (*)(void*, sqlite3_int64, int), void*, sqlite3_int64);
10563
10564 #ifdef SQLITE_ENABLE_MEMSYS3
10565 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
10566 #endif
10567 #ifdef SQLITE_ENABLE_MEMSYS5
10568 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
10569 #endif
10570
10571
10572 #ifndef SQLITE_MUTEX_OMIT
10573 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void);
10574 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
10575 SQLITE_PRIVATE int sqlite3MutexInit(void);
@@ -10477,10 +10652,11 @@
10652 SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
10653 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
10654 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
10655 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
10656 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
10657 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
10658 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
10659 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
10660 Token*, Select*, Expr*, IdList*);
10661 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
10662 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
@@ -10549,11 +10725,11 @@
10725 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int);
10726 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
10727 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
10728 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
10729 int*,int,int,int,int);
10730 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
10731 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
10732 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
10733 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*);
10734 SQLITE_PRIVATE void sqlite3TokenCopy(sqlite3*,Token*, Token*);
10735 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*);
@@ -10563,11 +10739,10 @@
10739 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
10740 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
10741 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
10742 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
10743 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
 
10744 #ifdef SQLITE_DEBUG
10745 SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*);
10746 SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3*);
10747 #else
10748 # define sqlite3SafetyOn(A) 0
@@ -10585,11 +10760,11 @@
10760 SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
10761 Expr*,int, int);
10762 SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
10763 SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
10764 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
10765 SQLITE_PRIVATE int sqlite3TriggersExist(Table*, int, ExprList*);
10766 SQLITE_PRIVATE int sqlite3CodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int,
10767 int, int, u32*, u32*);
10768 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
10769 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
10770 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
@@ -10598,11 +10773,11 @@
10773 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, int);
10774 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
10775 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
10776 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
10777 #else
10778 # define sqlite3TriggersExist(B,C,D,E,F) 0
10779 # define sqlite3DeleteTrigger(A,B)
10780 # define sqlite3DropTriggerPtr(A,B)
10781 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
10782 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I,J,K) 0
10783 #endif
@@ -10666,12 +10841,12 @@
10841 **
10842 ** x = getVarint32( A, B );
10843 ** x = putVarint32( A, B );
10844 **
10845 */
10846 #define getVarint32(A,B) ((*(A)<(unsigned char)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
10847 #define putVarint32(A,B) (((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
10848 #define getVarint sqlite3GetVarint
10849 #define putVarint sqlite3PutVarint
10850
10851
10852 SQLITE_PRIVATE void sqlite3IndexAffinityStr(Vdbe *, Index *);
@@ -10778,15 +10953,17 @@
10953 #ifdef SQLITE_OMIT_VIRTUALTABLE
10954 # define sqlite3VtabClear(X)
10955 # define sqlite3VtabSync(X,Y) SQLITE_OK
10956 # define sqlite3VtabRollback(X)
10957 # define sqlite3VtabCommit(X)
10958 # define sqlite3VtabInSync(db) 0
10959 #else
10960 SQLITE_PRIVATE void sqlite3VtabClear(Table*);
10961 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
10962 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
10963 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
10964 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
10965 #endif
10966 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
10967 SQLITE_PRIVATE void sqlite3VtabLock(sqlite3_vtab*);
10968 SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3*, sqlite3_vtab*);
10969 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
@@ -11107,11 +11284,11 @@
11284 **
11285 ** There is only one exported symbol in this file - the function
11286 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
11287 ** All other code has file scope.
11288 **
11289 ** $Id: date.c,v 1.94 2008/11/19 09:05:27 danielk1977 Exp $
11290 **
11291 ** SQLite processes all times and dates as Julian Day numbers. The
11292 ** dates and times are stored as the number of days since noon
11293 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
11294 ** calendar system.
@@ -11609,11 +11786,11 @@
11786 int rc = 1;
11787 int n;
11788 double r;
11789 char *z, zBuf[30];
11790 z = zBuf;
11791 for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
11792 z[n] = tolower(zMod[n]);
11793 }
11794 z[n] = 0;
11795 switch( z[0] ){
11796 #ifndef SQLITE_OMIT_LOCALTIME
@@ -11977,11 +12154,11 @@
12154 i++;
12155 }
12156 }
12157 if( n<sizeof(zBuf) ){
12158 z = zBuf;
12159 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
12160 sqlite3_result_error_toobig(context);
12161 return;
12162 }else{
12163 z = sqlite3DbMallocRaw(db, n);
12164 if( z==0 ){
@@ -12056,13 +12233,14 @@
12233 **
12234 ** This function returns the same value as time('now').
12235 */
12236 static void ctimeFunc(
12237 sqlite3_context *context,
12238 int NotUsed,
12239 sqlite3_value **NotUsed2
12240 ){
12241 UNUSED_PARAMETER2(NotUsed, NotUsed2);
12242 timeFunc(context, 0, 0);
12243 }
12244
12245 /*
12246 ** current_date()
@@ -12069,13 +12247,14 @@
12247 **
12248 ** This function returns the same value as date('now').
12249 */
12250 static void cdateFunc(
12251 sqlite3_context *context,
12252 int NotUsed,
12253 sqlite3_value **NotUsed2
12254 ){
12255 UNUSED_PARAMETER2(NotUsed, NotUsed2);
12256 dateFunc(context, 0, 0);
12257 }
12258
12259 /*
12260 ** current_timestamp()
@@ -12082,13 +12261,14 @@
12261 **
12262 ** This function returns the same value as datetime('now').
12263 */
12264 static void ctimestampFunc(
12265 sqlite3_context *context,
12266 int NotUsed,
12267 sqlite3_value **NotUsed2
12268 ){
12269 UNUSED_PARAMETER2(NotUsed, NotUsed2);
12270 datetimeFunc(context, 0, 0);
12271 }
12272 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
12273
12274 #ifdef SQLITE_OMIT_DATETIME_FUNCS
@@ -12623,11 +12803,11 @@
12803 ** to obtain the memory it needs.
12804 **
12805 ** This file contains implementations of the low-level memory allocation
12806 ** routines specified in the sqlite3_mem_methods object.
12807 **
12808 ** $Id: mem1.c,v 1.28 2008/11/19 09:05:27 danielk1977 Exp $
12809 */
12810
12811 /*
12812 ** This version of the memory allocator is the default. It is
12813 ** used when no other memory allocator is specified using compile-time
@@ -12715,17 +12895,19 @@
12895
12896 /*
12897 ** Initialize this module.
12898 */
12899 static int sqlite3MemInit(void *NotUsed){
12900 UNUSED_PARAMETER(NotUsed);
12901 return SQLITE_OK;
12902 }
12903
12904 /*
12905 ** Deinitialize this module.
12906 */
12907 static void sqlite3MemShutdown(void *NotUsed){
12908 UNUSED_PARAMETER(NotUsed);
12909 return;
12910 }
12911
12912 /*
12913 ** This routine is the only routine in this file with external linkage.
@@ -13213,11 +13395,11 @@
13395 ** be changed.
13396 **
13397 ** This version of the memory allocation subsystem is included
13398 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
13399 **
13400 ** $Id: mem3.c,v 1.25 2008/11/19 16:52:44 danielk1977 Exp $
13401 */
13402
13403 /*
13404 ** This version of the memory allocator is only built into the library
13405 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
@@ -13440,11 +13622,11 @@
13622 /*
13623 ** Chunk i is a free chunk that has been unlinked. Adjust its
13624 ** size parameters for check-out and return a pointer to the
13625 ** user portion of the chunk.
13626 */
13627 static void *memsys3Checkout(u32 i, u32 nBlock){
13628 u32 x;
13629 assert( sqlite3_mutex_held(mem3.mutex) );
13630 assert( i>=1 );
13631 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
13632 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
@@ -13458,11 +13640,11 @@
13640 /*
13641 ** Carve a piece off of the end of the mem3.iMaster free chunk.
13642 ** Return a pointer to the new allocation. Or, if the master chunk
13643 ** is not large enough, return 0.
13644 */
13645 static void *memsys3FromMaster(u32 nBlock){
13646 assert( sqlite3_mutex_held(mem3.mutex) );
13647 assert( mem3.szMaster>=nBlock );
13648 if( nBlock>=mem3.szMaster-1 ){
13649 /* Use the entire master */
13650 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
@@ -13544,12 +13726,12 @@
13726 ** This function assumes that the necessary mutexes, if any, are
13727 ** already held by the caller. Hence "Unsafe".
13728 */
13729 static void *memsys3MallocUnsafe(int nByte){
13730 u32 i;
13731 u32 nBlock;
13732 u32 toFree;
13733
13734 assert( sqlite3_mutex_held(mem3.mutex) );
13735 assert( sizeof(Mem3Block)==8 );
13736 if( nByte<=12 ){
13737 nBlock = 2;
@@ -13741,10 +13923,11 @@
13923
13924 /*
13925 ** Initialize this module.
13926 */
13927 static int memsys3Init(void *NotUsed){
13928 UNUSED_PARAMETER(NotUsed);
13929 if( !sqlite3GlobalConfig.pHeap ){
13930 return SQLITE_ERROR;
13931 }
13932
13933 /* Store a pointer to the memory block in global structure mem3. */
@@ -13765,10 +13948,11 @@
13948
13949 /*
13950 ** Deinitialize this module.
13951 */
13952 static void memsys3Shutdown(void *NotUsed){
13953 UNUSED_PARAMETER(NotUsed);
13954 return;
13955 }
13956
13957
13958
@@ -13777,11 +13961,11 @@
13961 ** allocations into that log.
13962 */
13963 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
13964 #ifdef SQLITE_DEBUG
13965 FILE *out;
13966 u32 i, j;
13967 u32 size;
13968 if( zFilename==0 || zFilename[0]==0 ){
13969 out = stdout;
13970 }else{
13971 out = fopen(zFilename, "w");
@@ -13842,10 +14026,12 @@
14026 if( out==stdout ){
14027 fflush(stdout);
14028 }else{
14029 fclose(out);
14030 }
14031 #else
14032 UNUSED_PARAMETER(zFilename);
14033 #endif
14034 }
14035
14036 /*
14037 ** This routine is the only routine in this file with external
@@ -13899,11 +14085,11 @@
14085 ** be changed.
14086 **
14087 ** This version of the memory allocation subsystem is included
14088 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
14089 **
14090 ** $Id: mem5.c,v 1.19 2008/11/19 16:52:44 danielk1977 Exp $
14091 */
14092
14093 /*
14094 ** This version of the memory allocator is used only when
14095 ** SQLITE_ENABLE_MEMSYS5 is defined.
@@ -14083,16 +14269,15 @@
14269 int iFullSz; /* Size of allocation rounded up to power of 2 */
14270 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
14271
14272 /* Keep track of the maximum allocation request. Even unfulfilled
14273 ** requests are counted */
14274 if( (u32)nByte>mem5.maxRequest ){
14275 mem5.maxRequest = nByte;
14276 }
14277
14278 /* Round nByte up to the next valid power of two */
 
14279 for(iFullSz=mem5.nAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
14280
14281 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
14282 ** block. If not, then split a block of the next larger power of
14283 ** two in order to create a new free block of size iLogsize.
@@ -14140,16 +14325,16 @@
14325 assert( ((u8 *)pOld-mem5.zPool)%mem5.nAtom==0 );
14326 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
14327
14328 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
14329 size = 1<<iLogsize;
14330 assert( iBlock+size-1<(u32)mem5.nBlock );
14331
14332 mem5.aCtrl[iBlock] |= CTRL_FREE;
14333 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
14334 assert( mem5.currentCount>0 );
14335 assert( mem5.currentOut>=(size*mem5.nAtom) );
14336 mem5.currentCount--;
14337 mem5.currentOut -= size*mem5.nAtom;
14338 assert( mem5.currentOut>0 || mem5.currentCount==0 );
14339 assert( mem5.currentCount>0 || mem5.currentOut==0 );
14340
@@ -14255,17 +14440,19 @@
14440 int nByte = sqlite3GlobalConfig.nHeap;
14441 u8 *zByte = (u8 *)sqlite3GlobalConfig.pHeap;
14442 int nMinLog; /* Log of minimum allocation size in bytes*/
14443 int iOffset;
14444
14445 UNUSED_PARAMETER(NotUsed);
14446
14447 if( !zByte ){
14448 return SQLITE_ERROR;
14449 }
14450
14451 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
14452 mem5.nAtom = (1<<nMinLog);
14453 while( (int)sizeof(Mem5Link)>mem5.nAtom ){
14454 mem5.nAtom = mem5.nAtom << 1;
14455 }
14456
14457 mem5.nBlock = (nByte / (mem5.nAtom+sizeof(u8)));
14458 mem5.zPool = zByte;
@@ -14291,10 +14478,11 @@
14478
14479 /*
14480 ** Deinitialize this module.
14481 */
14482 static void memsys5Shutdown(void *NotUsed){
14483 UNUSED_PARAMETER(NotUsed);
14484 return;
14485 }
14486
14487 /*
14488 ** Open the file indicated and write a log of all unfreed memory
@@ -14334,10 +14522,12 @@
14522 if( out==stdout ){
14523 fflush(stdout);
14524 }else{
14525 fclose(out);
14526 }
14527 #else
14528 UNUSED_PARAMETER(zFilename);
14529 #endif
14530 }
14531
14532 /*
14533 ** This routine is the only routine in this file with external
@@ -14985,11 +15175,11 @@
15175 ** May you share freely, never taking more than you give.
15176 **
15177 *************************************************************************
15178 ** This file contains the C functions that implement mutexes for pthreads
15179 **
15180 ** $Id: mutex_unix.c,v 1.15 2008/11/17 19:18:55 danielk1977 Exp $
15181 */
15182
15183 /*
15184 ** The code in this file is only used if we are compiling threadsafe
15185 ** under unix with pthreads.
@@ -15034,11 +15224,11 @@
15224 ** On those platforms where pthread_equal() is not atomic, SQLite
15225 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
15226 ** make sure no assert() statements are evaluated and hence these
15227 ** routines are never called.
15228 */
15229 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
15230 static int pthreadMutexHeld(sqlite3_mutex *p){
15231 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
15232 }
15233 static int pthreadMutexNotheld(sqlite3_mutex *p){
15234 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
@@ -15130,11 +15320,11 @@
15320 }
15321 break;
15322 }
15323 default: {
15324 assert( iType-2 >= 0 );
15325 assert( iType-2 < ArraySize(staticMutexes) );
15326 p = &staticMutexes[iType-2];
15327 p->id = iType;
15328 break;
15329 }
15330 }
@@ -15312,11 +15502,11 @@
15502 ** May you share freely, never taking more than you give.
15503 **
15504 *************************************************************************
15505 ** This file contains the C functions that implement mutexes for win32
15506 **
15507 ** $Id: mutex_w32.c,v 1.12 2008/11/10 20:01:41 shane Exp $
15508 */
15509
15510 /*
15511 ** The code in this file is only used if we are compiling multithreaded
15512 ** on a win32 system.
@@ -15341,11 +15531,18 @@
15531 ** the LockFileEx() API. But we can still statically link against that
15532 ** API as long as we don't call it win running Win95/98/ME. A call to
15533 ** this routine is used to determine if the host is Win95/98/ME or
15534 ** WinNT/2K/XP so that we will know whether or not we can safely call
15535 ** the LockFileEx() API.
15536 **
15537 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
15538 ** which is only available if your application was compiled with
15539 ** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
15540 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
15541 ** this out as well.
15542 */
15543 #if 0
15544 #if SQLITE_OS_WINCE
15545 # define mutexIsNT() (1)
15546 #else
15547 static int mutexIsNT(void){
15548 static int osType = 0;
@@ -15356,11 +15553,11 @@
15553 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
15554 }
15555 return osType==2;
15556 }
15557 #endif /* SQLITE_OS_WINCE */
15558 #endif
15559
15560 #ifdef SQLITE_DEBUG
15561 /*
15562 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
15563 ** intended for use only inside assert() statements.
@@ -15559,23 +15756,24 @@
15756 **
15757 *************************************************************************
15758 **
15759 ** Memory allocation functions used throughout sqlite.
15760 **
15761 ** $Id: malloc.c,v 1.48 2008/11/19 09:05:27 danielk1977 Exp $
15762 */
15763
15764 /*
15765 ** This routine runs when the memory allocator sees that the
15766 ** total memory allocation is about to exceed the soft heap
15767 ** limit.
15768 */
15769 static void softHeapLimitEnforcer(
15770 void *NotUsed,
15771 sqlite3_int64 NotUsed2,
15772 int allocSize
15773 ){
15774 UNUSED_PARAMETER2(NotUsed, NotUsed2);
15775 sqlite3_release_memory(allocSize);
15776 }
15777
15778 /*
15779 ** Set the soft heap-size limit for the library. Passing a zero or
@@ -15613,10 +15811,11 @@
15811 nRet += sqlite3VdbeReleaseMemory(n);
15812 #endif
15813 nRet += sqlite3PcacheReleaseMemory(n-nRet);
15814 return nRet;
15815 #else
15816 UNUSED_PARAMETER(n);
15817 return SQLITE_OK;
15818 #endif
15819 }
15820
15821 /*
@@ -15646,11 +15845,11 @@
15845 ** sqlite3GlobalConfig.pPage to a block of memory that records
15846 ** which pages are available.
15847 */
15848 u32 *aScratchFree;
15849 u32 *aPageFree;
15850 } mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 };
15851
15852 #define mem0 GLOBAL(struct Mem0Global, mem0)
15853
15854 /*
15855 ** Initialize the memory allocation subsystem.
@@ -15931,11 +16130,11 @@
16130 int i;
16131 i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch;
16132 i /= sqlite3GlobalConfig.szScratch;
16133 assert( i>=0 && i<sqlite3GlobalConfig.nScratch );
16134 sqlite3_mutex_enter(mem0.mutex);
16135 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
16136 mem0.aScratchFree[mem0.nScratchFree++] = i;
16137 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
16138 sqlite3_mutex_leave(mem0.mutex);
16139 }
16140 }
@@ -16339,11 +16538,11 @@
16538 ** the public domain. The original comments are included here for
16539 ** completeness. They are very out-of-date but might be useful as
16540 ** an historical reference. Most of the "enhancements" have been backed
16541 ** out so that the functionality is now the same as standard printf().
16542 **
16543 ** $Id: printf.c,v 1.96 2008/11/20 18:20:28 drh Exp $
16544 **
16545 **************************************************************************
16546 **
16547 ** The following modules is an enhanced replacement for the "printf" subroutines
16548 ** found in the standard C library. The following enhancements are
@@ -16470,11 +16669,10 @@
16669 { 'p', 16, 0, etPOINTER, 0, 1 },
16670 { 'T', 0, 2, etTOKEN, 0, 0 },
16671 { 'S', 0, 2, etSRCLIST, 0, 0 },
16672 { 'r', 10, 3, etORDINAL, 0, 0 },
16673 };
 
16674
16675 /*
16676 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
16677 ** conversions will work.
16678 */
@@ -16507,11 +16705,11 @@
16705 /*
16706 ** Append N space characters to the given string buffer.
16707 */
16708 static void appendSpace(StrAccum *pAccum, int N){
16709 static const char zSpaces[] = " ";
16710 while( N>=(int)sizeof(zSpaces)-1 ){
16711 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
16712 N -= sizeof(zSpaces)-1;
16713 }
16714 if( N>0 ){
16715 sqlite3StrAccumAppend(pAccum, zSpaces, N);
@@ -16670,11 +16868,11 @@
16868 }else{
16869 flag_long = flag_longlong = 0;
16870 }
16871 /* Fetch the info entry for the field */
16872 infop = 0;
16873 for(idx=0; idx<ArraySize(fmtinfo); idx++){
16874 if( c==fmtinfo[idx].fmttype ){
16875 infop = &fmtinfo[idx];
16876 if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
16877 xtype = infop->type;
16878 }else{
@@ -16979,11 +17177,14 @@
17177 }
17178 needQuote = !isnull && xtype==etSQLESCAPE2;
17179 n += i + 1 + needQuote*2;
17180 if( n>etBUFSIZE ){
17181 bufpt = zExtra = sqlite3Malloc( n );
17182 if( bufpt==0 ){
17183 pAccum->mallocFailed = 1;
17184 return;
17185 }
17186 }else{
17187 bufpt = buf;
17188 }
17189 j = 0;
17190 if( needQuote ) bufpt[j++] = q;
@@ -17471,11 +17672,11 @@
17672 ** VDBE. This information used to all be at the top of the single
17673 ** source code file "vdbe.c". When that file became too big (over
17674 ** 6000 lines long) it was split up into several smaller files and
17675 ** this header information was factored out.
17676 **
17677 ** $Id: vdbeInt.h,v 1.158 2008/11/17 15:31:48 danielk1977 Exp $
17678 */
17679 #ifndef _VDBEINT_H_
17680 #define _VDBEINT_H_
17681
17682 /*
@@ -17752,11 +17953,11 @@
17953 VdbeCursor **apCsr; /* One element of this array for each open cursor */
17954 int nVar; /* Number of entries in aVar[] */
17955 Mem *aVar; /* Values for the OP_Variable opcode. */
17956 char **azVar; /* Name of variables */
17957 int okVar; /* True if azVar[] has been initialized */
17958 u32 magic; /* Magic number for sanity checking */
17959 int nMem; /* Number of memory locations currently allocated */
17960 Mem *aMem; /* The memory locations */
17961 int nCallback; /* Number of callbacks invoked so far */
17962 int cacheCtr; /* VdbeCursor row cache generation counter */
17963 Fifo sFifo; /* A list of ROWIDs */
@@ -18392,11 +18593,11 @@
18593 ** Utility functions used throughout sqlite.
18594 **
18595 ** This file contains functions for allocating memory, comparing
18596 ** strings, and stuff like that.
18597 **
18598 ** $Id: util.c,v 1.242 2008/11/17 19:18:55 danielk1977 Exp $
18599 */
18600
18601
18602 /*
18603 ** Return true if the floating point value is Not a Number (NaN).
@@ -18432,11 +18633,11 @@
18633 ** to exceed the SQLITE_LIMIT_LENGTH setting.
18634 */
18635 SQLITE_PRIVATE int sqlite3Strlen(sqlite3 *db, const char *z){
18636 const char *z2 = z;
18637 int len;
18638 int x;
18639 while( *z2 ){ z2++; }
18640 x = z2 - z;
18641 len = 0x7fffffff & x;
18642 if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){
18643 return db->aLimit[SQLITE_LIMIT_LENGTH];
@@ -19309,19 +19510,19 @@
19510 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
19511 ** open properly and is not fit for general use but which can be
19512 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
19513 */
19514 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
19515 u32 magic;
19516 if( db==0 ) return 0;
19517 magic = db->magic;
19518 if( magic!=SQLITE_MAGIC_OPEN &&
19519 magic!=SQLITE_MAGIC_BUSY ) return 0;
19520 return 1;
19521 }
19522 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
19523 u32 magic;
19524 if( db==0 ) return 0;
19525 magic = db->magic;
19526 if( magic!=SQLITE_MAGIC_SICK &&
19527 magic!=SQLITE_MAGIC_OPEN &&
19528 magic!=SQLITE_MAGIC_BUSY ) return 0;
@@ -19801,11 +20002,11 @@
20002 **
20003 ******************************************************************************
20004 **
20005 ** This file contains code that is specific to OS/2.
20006 **
20007 ** $Id: os_os2.c,v 1.59 2008/11/18 23:03:40 pweilbacher Exp $
20008 */
20009
20010
20011 #if SQLITE_OS_OS2
20012
@@ -20216,11 +20417,18 @@
20417 if( flags & SQLITE_SYNC_FULL){
20418 sqlite3_fullsync_count++;
20419 }
20420 sqlite3_sync_count++;
20421 #endif
20422 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
20423 ** no-op
20424 */
20425 #ifdef SQLITE_NO_SYNC
20426 return SQLITE_OK;
20427 #else
20428 return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
20429 #endif
20430 }
20431
20432 /*
20433 ** Determine the current size of a file in bytes
20434 */
@@ -21149,11 +21357,11 @@
21357 **
21358 ******************************************************************************
21359 **
21360 ** This file contains code that is specific to Unix systems.
21361 **
21362 ** $Id: os_unix.c,v 1.216 2008/11/19 16:52:44 danielk1977 Exp $
21363 */
21364 #if SQLITE_OS_UNIX /* This file is used on unix only */
21365
21366 /*
21367 ** If SQLITE_ENABLE_LOCKING_STYLE is defined and is non-zero, then several
@@ -21161,11 +21369,12 @@
21369 **
21370 ** * POSIX locking (the default),
21371 ** * No locking,
21372 ** * Dot-file locking,
21373 ** * flock() locking,
21374 ** * AFP locking (OSX only),
21375 ** * Named POSIX semaphores (VXWorks only).
21376 **
21377 ** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by
21378 ** default on a Mac and disabled on all other posix platforms.
21379 */
21380 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
@@ -21174,10 +21383,20 @@
21383 # else
21384 # define SQLITE_ENABLE_LOCKING_STYLE 0
21385 # endif
21386 #endif
21387
21388 /*
21389 ** Define the IS_VXWORKS pre-processor macro to 1 if building on
21390 ** vxworks, or 0 otherwise.
21391 */
21392 #if defined(__RTP__) || defined(_WRS_KERNEL)
21393 # define IS_VXWORKS 1
21394 #else
21395 # define IS_VXWORKS 0
21396 #endif
21397
21398 /*
21399 ** These #defines should enable >2GB file support on Posix if the
21400 ** underlying operating system supports it. If the OS lacks
21401 ** large file support, these should be no-ops.
21402 **
@@ -21206,13 +21425,19 @@
21425 #include <unistd.h>
21426 #include <sys/time.h>
21427 #include <errno.h>
21428
21429 #if SQLITE_ENABLE_LOCKING_STYLE
21430 # include <sys/ioctl.h>
21431 # if IS_VXWORKS
21432 # define lstat stat
21433 # include <semaphore.h>
21434 # include <limits.h>
21435 # else
21436 # include <sys/param.h>
21437 # include <sys/mount.h>
21438 # endif
21439 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
21440
21441 /*
21442 ** If we are to be thread-safe, include the pthreads header and define
21443 ** the SQLITE_UNIX_THREADS macro.
@@ -21257,10 +21482,14 @@
21482 int dirfd; /* File descriptor for the directory */
21483 #if SQLITE_THREADSAFE
21484 pthread_t tid; /* The thread that "owns" this unixFile */
21485 #endif
21486 int lastErrno; /* The unix errno from the last I/O error */
21487 #if IS_VXWORKS
21488 int isDelete; /* Delete on close if true */
21489 char *zRealpath;
21490 #endif
21491 };
21492
21493 /*
21494 ** Include code that is common to all os_*.c files
21495 */
@@ -21667,11 +21896,15 @@
21896 ** each others locks then tid is always set to zero. tid is omitted
21897 ** if we compile without threading support.
21898 */
21899 struct lockKey {
21900 dev_t dev; /* Device number */
21901 #if IS_VXWORKS
21902 void *rnam; /* Realname since inode unusable */
21903 #else
21904 ino_t ino; /* Inode number */
21905 #endif
21906 #if SQLITE_THREADSAFE
21907 pthread_t tid; /* Thread ID or zero if threads can override each other */
21908 #endif
21909 };
21910
@@ -21697,11 +21930,15 @@
21930 ** to locate a particular openCnt structure given its inode. This
21931 ** is the same as the lockKey except that the thread ID is omitted.
21932 */
21933 struct openKey {
21934 dev_t dev; /* Device number */
21935 #if IS_VXWORKS
21936 void *rnam; /* Realname since inode unusable */
21937 #else
21938 ino_t ino; /* Inode number */
21939 #endif
21940 };
21941
21942 /*
21943 ** An instance of the following structure is allocated for each open
21944 ** inode. This structure keeps track of the number of locks on that
@@ -21713,10 +21950,14 @@
21950 struct openKey key; /* The lookup key */
21951 int nRef; /* Number of pointers to this structure */
21952 int nLock; /* Number of outstanding locks */
21953 int nPending; /* Number of pending close() operations */
21954 int *aPending; /* Malloced space holding fd's awaiting a close() */
21955 #if IS_VXWORKS
21956 sem_t *pSem; /* Named POSIX semaphore */
21957 char aSemName[MAX_PATHNAME+1]; /* Name of that semaphore */
21958 #endif
21959 struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
21960 };
21961
21962 /*
21963 ** List of all lockInfo and openCnt objects. This used to be a hash
@@ -21724,10 +21965,22 @@
21965 ** never exceeds a few thousand. And lookup is not on a critical
21966 ** path oo a simple linked list will suffice.
21967 */
21968 static struct lockInfo *lockList = 0;
21969 static struct openCnt *openList = 0;
21970
21971 #if IS_VXWORKS
21972 /*
21973 ** This hash table is used to bind the canonical file name to a
21974 ** unixFile structure and use the hash key (= canonical name)
21975 ** instead of the Inode number of the file to find the matching
21976 ** lockInfo and openCnt structures. It also helps to make the
21977 ** name of the semaphore when LOCKING_STYLE_NAMEDSEM is used
21978 ** for the file.
21979 */
21980 static Hash nameHash;
21981 #endif
21982
21983 /*
21984 ** The locking styles are associated with the different file locking
21985 ** capabilities supported by different file systems.
21986 **
@@ -21737,18 +21990,21 @@
21990 ** DOTLOCK isn't a true locking style, it refers to the use of a special
21991 ** file named the same as the database file with a '.lock' extension, this
21992 ** can be used on file systems that do not offer any reliable file locking
21993 ** NO locking means that no locking will be attempted, this is only used for
21994 ** read-only file systems currently
21995 ** NAMEDSEM is similar to DOTLOCK but uses a named semaphore instead of an
21996 ** indicator file.
21997 ** UNSUPPORTED means that no locking will be attempted, this is only used for
21998 ** file systems that are known to be unsupported
21999 */
22000 #define LOCKING_STYLE_POSIX 1
22001 #define LOCKING_STYLE_NONE 2
22002 #define LOCKING_STYLE_DOTFILE 3
22003 #define LOCKING_STYLE_FLOCK 4
22004 #define LOCKING_STYLE_AFP 5
22005 #define LOCKING_STYLE_NAMEDSEM 6
22006
22007 /*
22008 ** Only set the lastErrno if the error code is a real error and not
22009 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
22010 */
@@ -21859,20 +22115,23 @@
22115 return s;
22116 }
22117 #define fcntl lockTrace
22118 #endif /* SQLITE_LOCK_TRACE */
22119
22120 #ifdef __linux__
22121 /*
22122 ** This function is used as the main routine for a thread launched by
22123 ** testThreadLockingBehavior(). It tests whether the shared-lock obtained
22124 ** by the main thread in testThreadLockingBehavior() conflicts with a
22125 ** hypothetical write-lock obtained by this thread on the same file.
22126 **
22127 ** The write-lock is not actually acquired, as this is not possible if
22128 ** the file is open in read-only mode (see ticket #3472).
22129 */
22130 static void *threadLockingTest(void *pArg){
22131 struct threadTestData *pData = (struct threadTestData*)pArg;
22132 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
22133 return pArg;
22134 }
22135
22136 /*
22137 ** This procedure attempts to determine whether or not threads
@@ -21879,30 +22138,43 @@
22138 ** can override each others locks then sets the
22139 ** threadsOverrideEachOthersLocks variable appropriately.
22140 */
22141 static void testThreadLockingBehavior(int fd_orig){
22142 int fd;
22143 int rc;
22144 struct threadTestData d;
22145 struct flock l;
22146 pthread_t t;
22147
22148 fd = dup(fd_orig);
22149 if( fd<0 ) return;
22150 memset(&l, 0, sizeof(l));
22151 l.l_type = F_RDLCK;
22152 l.l_len = 1;
22153 l.l_start = 0;
22154 l.l_whence = SEEK_SET;
22155 rc = fcntl(fd_orig, F_SETLK, &l);
22156 if( rc!=0 ) return;
22157 memset(&d, 0, sizeof(d));
22158 d.fd = fd;
22159 d.lock = l;
22160 d.lock.l_type = F_WRLCK;
22161 pthread_create(&t, 0, threadLockingTest, &d);
22162 pthread_join(t, 0);
22163 close(fd);
22164 if( d.result!=0 ) return;
22165 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
22166 }
22167 #else
22168 /*
22169 ** On anything other than linux, assume threads override each others locks.
22170 */
22171 static void testThreadLockingBehavior(int fd_orig){
22172 threadsOverrideEachOthersLocks = 1;
22173 }
22174 #endif /* __linux__ */
22175
22176 #endif /* SQLITE_THREADSAFE */
22177
22178 /*
22179 ** Release a lockInfo structure previously allocated by findLockInfo().
22180 */
@@ -21947,15 +22219,108 @@
22219 sqlite3_free(pOpen->aPending);
22220 sqlite3_free(pOpen);
22221 }
22222 }
22223 }
22224
22225 #if IS_VXWORKS
22226 /*
22227 ** Implementation of a realpath() like function for vxWorks
22228 ** to determine canonical path name from given name. It does
22229 ** not support symlinks. Neither does it handle volume prefixes.
22230 */
22231 char *
22232 vxrealpath(const char *pathname, int dostat)
22233 {
22234 struct stat sbuf;
22235 int len;
22236 char *where, *ptr, *last;
22237 char *result, *curpath, *workpath, *namebuf;
22238
22239 len = pathconf(pathname, _PC_PATH_MAX);
22240 if( len<0 ){
22241 len = PATH_MAX;
22242 }
22243 result = sqlite3_malloc(len * 4);
22244 if( !result ){
22245 return 0;
22246 }
22247 curpath = result + len;
22248 workpath = curpath + len;
22249 namebuf = workpath + len;
22250 strcpy(curpath, pathname);
22251 if( *pathname!='/' ){
22252 if( !getcwd(workpath, len) ){
22253 sqlite3_free(result);
22254 return 0;
22255 }
22256 }else{
22257 *workpath = '\0';
22258 }
22259 where = curpath;
22260 while( *where ){
22261 if( !strcmp(where, ".") ){
22262 where++;
22263 continue;
22264 }
22265 if( !strncmp(where, "./", 2) ){
22266 where += 2;
22267 continue;
22268 }
22269 if( !strncmp(where, "../", 3) ){
22270 where += 3;
22271 ptr = last = workpath;
22272 while( *ptr ){
22273 if( *ptr=='/' ){
22274 last = ptr;
22275 }
22276 ptr++;
22277 }
22278 *last = '\0';
22279 continue;
22280 }
22281 ptr = strchr(where, '/');
22282 if( !ptr ){
22283 ptr = where + strlen(where) - 1;
22284 }else{
22285 *ptr = '\0';
22286 }
22287 strcpy(namebuf, workpath);
22288 for( last = namebuf; *last; last++ ){
22289 continue;
22290 }
22291 if( *--last!='/' ){
22292 strcat(namebuf, "/");
22293 }
22294 strcat(namebuf, where);
22295 where = ++ptr;
22296 if( dostat ){
22297 if( stat(namebuf, &sbuf)==-1 ){
22298 sqlite3_free(result);
22299 return 0;
22300 }
22301 if( (sbuf.st_mode & S_IFDIR)==S_IFDIR ){
22302 strcpy(workpath, namebuf);
22303 continue;
22304 }
22305 if( *where ){
22306 sqlite3_free(result);
22307 return 0;
22308 }
22309 }
22310 strcpy(workpath, namebuf);
22311 }
22312 strcpy(result, workpath);
22313 return result;
22314 }
22315 #endif
22316
22317 #if SQLITE_ENABLE_LOCKING_STYLE
22318 /*
22319 ** Tests a byte-range locking query to see if byte range locks are
22320 ** supported, if not we fall back to dotlockLockingStyle.
22321 ** On vxWorks we fall back to namedsemLockingStyle.
22322 */
22323 static int testLockingStyle(int fd){
22324 struct flock lockInfo;
22325
22326 /* Test byte-range lock using fcntl(). If the call succeeds,
@@ -21968,13 +22333,14 @@
22333 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
22334 return LOCKING_STYLE_POSIX;
22335 }
22336
22337 /* Testing for flock() can give false positives. So if if the above
22338 ** test fails, then we fall back to using dot-file style locking (or
22339 ** named-semaphore locking on vxworks).
22340 */
22341 return (IS_VXWORKS ? LOCKING_STYLE_NAMEDSEM : LOCKING_STYLE_DOTFILE);
22342 }
22343 #endif
22344
22345 /*
22346 ** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
@@ -21985,16 +22351,27 @@
22351 ** other systems.
22352 **
22353 ** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
22354 ** returns LOCKING_STYLE_POSIX.
22355 */
22356 #if SQLITE_ENABLE_LOCKING_STYLE
22357 static int detectLockingStyle(
22358 sqlite3_vfs *pVfs,
22359 const char *filePath,
22360 int fd
22361 ){
22362 #if IS_VXWORKS
22363 if( !filePath ){
22364 return LOCKING_STYLE_NONE;
22365 }
22366 if( pVfs->pAppData ){
22367 return SQLITE_PTR_TO_INT(pVfs->pAppData);
22368 }
22369 if (access(filePath, 0) != -1){
22370 return testLockingStyle(fd);
22371 }
22372 #else
22373 struct Mapping {
22374 const char *zFilesystem;
22375 int eLockingStyle;
22376 } aMap[] = {
22377 { "hfs", LOCKING_STYLE_POSIX },
@@ -22030,13 +22407,16 @@
22407 }
22408 }
22409
22410 /* Default case. Handles, amongst others, "nfs". */
22411 return testLockingStyle(fd);
22412 #endif /* if IS_VXWORKS */
22413 return LOCKING_STYLE_POSIX;
22414 }
22415 #else
22416 #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX
22417 #endif /* ifdef SQLITE_ENABLE_LOCKING_STYLE */
22418
22419 /*
22420 ** Given a file descriptor, locate lockInfo and openCnt structures that
22421 ** describes that file descriptor. Create new ones if necessary. The
22422 ** return values might be uninitialized if an error occurs.
@@ -22043,10 +22423,13 @@
22423 **
22424 ** Return an appropriate error code.
22425 */
22426 static int findLockInfo(
22427 int fd, /* The file descriptor used in the key */
22428 #if IS_VXWORKS
22429 void *rnam, /* vxWorks realname */
22430 #endif
22431 struct lockInfo **ppLock, /* Return the lockInfo structure here */
22432 struct openCnt **ppOpen /* Return the openCnt structure here */
22433 ){
22434 int rc;
22435 struct lockKey key1;
@@ -22080,20 +22463,28 @@
22463 }
22464 }
22465
22466 memset(&key1, 0, sizeof(key1));
22467 key1.dev = statbuf.st_dev;
22468 #if IS_VXWORKS
22469 key1.rnam = rnam;
22470 #else
22471 key1.ino = statbuf.st_ino;
22472 #endif
22473 #if SQLITE_THREADSAFE
22474 if( threadsOverrideEachOthersLocks<0 ){
22475 testThreadLockingBehavior(fd);
22476 }
22477 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
22478 #endif
22479 memset(&key2, 0, sizeof(key2));
22480 key2.dev = statbuf.st_dev;
22481 #if IS_VXWORKS
22482 key2.rnam = rnam;
22483 #else
22484 key2.ino = statbuf.st_ino;
22485 #endif
22486 pLock = lockList;
22487 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
22488 pLock = pLock->pNext;
22489 }
22490 if( pLock==0 ){
@@ -22133,10 +22524,14 @@
22524 pOpen->aPending = 0;
22525 pOpen->pNext = openList;
22526 pOpen->pPrev = 0;
22527 if( openList ) openList->pPrev = pOpen;
22528 openList = pOpen;
22529 #if IS_VXWORKS
22530 pOpen->pSem = NULL;
22531 pOpen->aSemName[0] = '\0';
22532 #endif
22533 }else{
22534 pOpen->nRef++;
22535 }
22536 *ppOpen = pOpen;
22537 }
@@ -22197,11 +22592,15 @@
22592 OSTRACE4("Transfer ownership of %d from %d to %d\n",
22593 pFile->h, pFile->tid, hSelf);
22594 pFile->tid = hSelf;
22595 if (pFile->pLock != NULL) {
22596 releaseLockInfo(pFile->pLock);
22597 #if IS_VXWORKS
22598 rc = findLockInfo(pFile->h, pFile->zRealpath, &pFile->pLock, 0);
22599 #else
22600 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
22601 #endif
22602 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
22603 locktypeName(pFile->locktype),
22604 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
22605 return rc;
22606 } else {
@@ -22366,10 +22765,24 @@
22765 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
22766 ** or power failure will likely corrupt the database file.
22767 */
22768 static int full_fsync(int fd, int fullSync, int dataOnly){
22769 int rc;
22770
22771 /* The following "ifdef/elif/else/" block has the same structure as
22772 ** the one below. It is replicated here solely to avoid cluttering
22773 ** up the real code with the UNUSED_PARAMETER() macros.
22774 */
22775 #ifdef SQLITE_NO_SYNC
22776 UNUSED_PARAMETER(fd);
22777 UNUSED_PARAMETER(fullSync);
22778 UNUSED_PARAMETER(dataOnly);
22779 #elif HAVE_FULLFSYNC
22780 UNUSED_PARAMETER(dataOnly);
22781 #else
22782 UNUSED_PARAMETER(fullSync);
22783 #endif
22784
22785 /* Record the number of times that we do a normal fsync() and
22786 ** FULLSYNC. This is used during testing to verify that this procedure
22787 ** gets called with the correct arguments.
22788 */
@@ -22381,13 +22794,11 @@
22794 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
22795 ** no-op
22796 */
22797 #ifdef SQLITE_NO_SYNC
22798 rc = SQLITE_OK;
22799 #elif HAVE_FULLFSYNC
 
 
22800 if( fullSync ){
22801 rc = fcntl(fd, F_FULLFSYNC, 0);
22802 }else{
22803 rc = 1;
22804 }
@@ -22402,16 +22813,21 @@
22813 if( rc ) rc = fsync(fd);
22814
22815 #else
22816 if( dataOnly ){
22817 rc = fdatasync(fd);
22818 if( IS_VXWORKS && rc==-1 && errno==ENOTSUP ){
22819 rc = fsync(fd);
22820 }
22821 }else{
22822 rc = fsync(fd);
22823 }
22824 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
 
22825
22826 if( IS_VXWORKS && rc!= -1 ){
22827 rc = 0;
22828 }
22829 return rc;
22830 }
22831
22832 /*
22833 ** Make sure all writes to a particular file are committed to disk.
@@ -22996,10 +23412,29 @@
23412 close(pFile->dirfd);
23413 }
23414 if( pFile->h>=0 ){
23415 close(pFile->h);
23416 }
23417 #if IS_VXWORKS
23418 if( pFile->isDelete && pFile->zRealpath ){
23419 unlink(pFile->zRealpath);
23420 }
23421 if( pFile->zRealpath ){
23422 HashElem *pElem;
23423 int n = strlen(pFile->zRealpath) + 1;
23424 pElem = sqlite3HashFindElem(&nameHash, pFile->zRealpath, n);
23425 if( pElem ){
23426 long cnt = (long)pElem->data;
23427 cnt--;
23428 if( cnt==0 ){
23429 sqlite3HashInsert(&nameHash, pFile->zRealpath, n, 0);
23430 }else{
23431 pElem->data = (void*)cnt;
23432 }
23433 }
23434 }
23435 #endif
23436 OSTRACE2("CLOSE %-3d\n", pFile->h);
23437 OpenCounter(-1);
23438 memset(pFile, 0, sizeof(unixFile));
23439 }
23440 return SQLITE_OK;
@@ -23039,10 +23474,12 @@
23474 return SQLITE_OK;
23475 }
23476
23477
23478 #if SQLITE_ENABLE_LOCKING_STYLE
23479
23480 #if !IS_VXWORKS
23481 #pragma mark AFP Support
23482
23483 /*
23484 ** The afpLockingContext structure contains all afp lock specific state
23485 */
@@ -23485,10 +23922,12 @@
23922 if( id ){
23923 flockUnlock(id, NO_LOCK);
23924 }
23925 return closeUnixFile(id);
23926 }
23927
23928 #endif /* !IS_VXWORKS */
23929
23930 #pragma mark Old-School .lock file based locking
23931
23932 /* Dotlock-style reserved lock checking following the behavior of
23933 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
@@ -23514,11 +23953,11 @@
23953 if( lstat(zLockFile, &statBuf)==0 ){
23954 /* file exists, someone else has the lock */
23955 reserved = 1;
23956 }else{
23957 /* file does not exist, we could have it if we want it */
23958 int tErrno = errno;
23959 if( ENOENT != tErrno ){
23960 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
23961 pFile->lastErrno = tErrno;
23962 }
23963 }
@@ -23537,13 +23976,14 @@
23976
23977 /* if we already have a lock, it is exclusive.
23978 ** Just adjust level and punt on outta here. */
23979 if (pFile->locktype > NO_LOCK) {
23980 pFile->locktype = locktype;
23981 #if !IS_VXWORKS
23982 /* Always update the timestamp on the old file */
23983 utimes(zLockFile, NULL);
23984 #endif
23985 rc = SQLITE_OK;
23986 goto dotlock_end_lock;
23987 }
23988
23989 /* check to see if lock file already exists */
@@ -23614,44 +24054,179 @@
24054
24055 /*
24056 ** Close a file.
24057 */
24058 static int dotlockClose(sqlite3_file *id) {
24059 int rc;
24060 if( id ){
24061 unixFile *pFile = (unixFile*)id;
24062 dotlockUnlock(id, NO_LOCK);
24063 sqlite3_free(pFile->lockingContext);
24064 }
24065 if( IS_VXWORKS ) enterMutex();
24066 rc = closeUnixFile(id);
24067 if( IS_VXWORKS ) leaveMutex();
24068 return rc;
24069 }
24070
24071 #if IS_VXWORKS
24072
24073 #pragma mark POSIX/vxWorks named semaphore based locking
24074
24075 /* Namedsem-style reserved lock checking following the behavior of
24076 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
24077 static int namedsemCheckReservedLock(sqlite3_file *id, int *pResOut) {
24078 int rc = SQLITE_OK;
24079 int reserved = 0;
24080 unixFile *pFile = (unixFile*)id;
24081
24082 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24083
24084 assert( pFile );
24085
24086 /* Check if a thread in this process holds such a lock */
24087 if( pFile->locktype>SHARED_LOCK ){
24088 reserved = 1;
24089 }
24090
24091 /* Otherwise see if some other process holds it. */
24092 if( !reserved ){
24093 sem_t *pSem = pFile->pOpen->pSem;
24094 struct stat statBuf;
24095
24096 if( sem_trywait(pSem)==-1 ){
24097 int tErrno = errno;
24098 if( EAGAIN != tErrno ){
24099 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
24100 pFile->lastErrno = tErrno;
24101 } else {
24102 /* someone else has the lock when we are in NO_LOCK */
24103 reserved = (pFile->locktype < SHARED_LOCK);
24104 }
24105 }else{
24106 /* we could have it if we want it */
24107 sem_post(pSem);
24108 }
24109 }
24110 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
24111
24112 *pResOut = reserved;
24113 return rc;
24114 }
24115
24116 static int namedsemLock(sqlite3_file *id, int locktype) {
24117 unixFile *pFile = (unixFile*)id;
24118 int fd;
24119 sem_t *pSem = pFile->pOpen->pSem;
24120 int rc = SQLITE_OK;
24121
24122 /* if we already have a lock, it is exclusive.
24123 ** Just adjust level and punt on outta here. */
24124 if (pFile->locktype > NO_LOCK) {
24125 pFile->locktype = locktype;
24126 rc = SQLITE_OK;
24127 goto namedsem_end_lock;
24128 }
24129
24130 /* lock semaphore now but bail out when already locked. */
24131 if( sem_trywait(pSem)==-1 ){
24132 rc = SQLITE_BUSY;
24133 goto namedsem_end_lock;
24134 }
24135
24136 /* got it, set the type and return ok */
24137 pFile->locktype = locktype;
24138
24139 namedsem_end_lock:
24140 return rc;
24141 }
24142
24143 static int namedsemUnlock(sqlite3_file *id, int locktype) {
24144 unixFile *pFile = (unixFile*)id;
24145 sem_t *pSem = pFile->pOpen->pSem;
24146
24147 assert( pFile );
24148 assert( pSem );
24149 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
24150 pFile->locktype, getpid());
24151 assert( locktype<=SHARED_LOCK );
24152
24153 /* no-op if possible */
24154 if( pFile->locktype==locktype ){
24155 return SQLITE_OK;
24156 }
24157
24158 /* shared can just be set because we always have an exclusive */
24159 if (locktype==SHARED_LOCK) {
24160 pFile->locktype = locktype;
24161 return SQLITE_OK;
24162 }
24163
24164 /* no, really unlock. */
24165 if ( sem_post(pSem)==-1 ) {
24166 int rc, tErrno = errno;
24167 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
24168 if( IS_LOCK_ERROR(rc) ){
24169 pFile->lastErrno = tErrno;
24170 }
24171 return rc;
24172 }
24173 pFile->locktype = NO_LOCK;
24174 return SQLITE_OK;
24175 }
24176
24177 /*
24178 ** Close a file.
24179 */
24180 static int namedsemClose(sqlite3_file *id) {
24181 if( id ){
24182 unixFile *pFile = (unixFile*)id;
24183 namedsemUnlock(id, NO_LOCK);
24184 assert( pFile );
24185 enterMutex();
24186 releaseLockInfo(pFile->pLock);
24187 releaseOpenCnt(pFile->pOpen);
24188 closeUnixFile(id);
24189 leaveMutex();
24190 }
24191 return SQLITE_OK;
24192 }
24193
24194 #endif /* IS_VXWORKS */
24195
24196 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
24197
24198 /*
24199 ** The nolockLockingContext is void
24200 */
24201 typedef void nolockLockingContext;
24202
24203 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
24204 UNUSED_PARAMETER(NotUsed);
24205 *pResOut = 0;
24206 return SQLITE_OK;
24207 }
24208
24209 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
24210 UNUSED_PARAMETER2(NotUsed, NotUsed2);
24211 return SQLITE_OK;
24212 }
24213
24214 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
24215 UNUSED_PARAMETER2(NotUsed, NotUsed2);
24216 return SQLITE_OK;
24217 }
24218
24219 /*
24220 ** Close a file.
24221 */
24222 static int nolockClose(sqlite3_file *id) {
24223 int rc;
24224 if( IS_VXWORKS ) enterMutex();
24225 rc = closeUnixFile(id);
24226 if( IS_VXWORKS ) leaveMutex();
24227 return rc;
24228 }
24229
24230
24231 /*
24232 ** Information and control of an open file handle.
@@ -23674,18 +24249,20 @@
24249 ** SQLite code assumes this function cannot fail. It also assumes that
24250 ** if two files are created in the same file-system directory (i.e.
24251 ** a database and its journal file) that the sector size will be the
24252 ** same for both.
24253 */
24254 static int unixSectorSize(sqlite3_file *NotUsed){
24255 UNUSED_PARAMETER(NotUsed);
24256 return SQLITE_DEFAULT_SECTOR_SIZE;
24257 }
24258
24259 /*
24260 ** Return the device characteristics for the file. This is always 0 for unix.
24261 */
24262 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
24263 UNUSED_PARAMETER(NotUsed);
24264 return 0;
24265 }
24266
24267 /*
24268 ** Initialize the contents of the unixFile structure pointed to by pId.
@@ -23699,11 +24276,12 @@
24276 sqlite3_vfs *pVfs, /* Pointer to vfs object */
24277 int h, /* Open file descriptor of file being opened */
24278 int dirfd, /* Directory file descriptor */
24279 sqlite3_file *pId, /* Write to the unixFile structure here */
24280 const char *zFilename, /* Name of the file being opened */
24281 int noLock, /* Omit locking if true */
24282 int isDelete /* Delete on close if true */
24283 ){
24284 int eLockingStyle;
24285 unixFile *pNew = (unixFile *)pId;
24286 int rc = SQLITE_OK;
24287
@@ -23730,12 +24308,19 @@
24308 static sqlite3_io_methods aIoMethod[] = {
24309 IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
24310 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
24311 #if SQLITE_ENABLE_LOCKING_STYLE
24312 ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
24313 #if IS_VXWORKS
24314 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
24315 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
24316 ,IOMETHODS(namedsemClose, namedsemLock, namedsemUnlock, namedsemCheckReservedLock)
24317 #else
24318 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
24319 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
24320 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
24321 #endif
24322 #endif
24323 };
24324 /* The order of the IOMETHODS macros above is important. It must be the
24325 ** same order as the LOCKING_STYLE numbers
24326 */
@@ -23742,18 +24327,63 @@
24327 assert(LOCKING_STYLE_POSIX==1);
24328 assert(LOCKING_STYLE_NONE==2);
24329 assert(LOCKING_STYLE_DOTFILE==3);
24330 assert(LOCKING_STYLE_FLOCK==4);
24331 assert(LOCKING_STYLE_AFP==5);
24332 assert(LOCKING_STYLE_NAMEDSEM==6);
24333
24334 assert( pNew->pLock==NULL );
24335 assert( pNew->pOpen==NULL );
24336
24337 /* Parameter isDelete is only used on vxworks. Parameter pVfs is only
24338 ** used if ENABLE_LOCKING_STYLE is defined. Express this explicitly
24339 ** here to prevent compiler warnings about unused parameters.
24340 */
24341 if( !IS_VXWORKS ) UNUSED_PARAMETER(isDelete);
24342 if( !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(pVfs);
24343 if( !IS_VXWORKS && !SQLITE_ENABLE_LOCKING_STYLE ) UNUSED_PARAMETER(zFilename);
24344
24345 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
24346 pNew->h = h;
24347 pNew->dirfd = dirfd;
24348 SET_THREADID(pNew);
24349
24350 #if IS_VXWORKS
24351 {
24352 HashElem *pElem;
24353 char *zRealname = vxrealpath(zFilename, 1);
24354 int n;
24355 pNew->zRealpath = 0;
24356 if( !zRealname ){
24357 rc = SQLITE_NOMEM;
24358 eLockingStyle = LOCKING_STYLE_NONE;
24359 }else{
24360 n = strlen(zRealname) + 1;
24361 enterMutex();
24362 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
24363 if( pElem ){
24364 long cnt = (long)pElem->data;
24365 cnt++;
24366 pNew->zRealpath = pElem->pKey;
24367 pElem->data = (void*)cnt;
24368 }else{
24369 if( sqlite3HashInsert(&nameHash, zRealname, n, (void*)1)==0 ){
24370 pElem = sqlite3HashFindElem(&nameHash, zRealname, n);
24371 if( pElem ){
24372 pNew->zRealpath = pElem->pKey;
24373 }else{
24374 sqlite3HashInsert(&nameHash, zRealname, n, 0);
24375 rc = SQLITE_NOMEM;
24376 eLockingStyle = LOCKING_STYLE_NONE;
24377 }
24378 }
24379 }
24380 leaveMutex();
24381 sqlite3_free(zRealname);
24382 }
24383 }
24384 #endif
24385
24386 if( noLock ){
24387 eLockingStyle = LOCKING_STYLE_NONE;
24388 }else{
24389 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
@@ -23761,16 +24391,22 @@
24391
24392 switch( eLockingStyle ){
24393
24394 case LOCKING_STYLE_POSIX: {
24395 enterMutex();
24396 #if IS_VXWORKS
24397 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
24398 #else
24399 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
24400 #endif
24401 leaveMutex();
24402 break;
24403 }
24404
24405 #if SQLITE_ENABLE_LOCKING_STYLE
24406
24407 #if !IS_VXWORKS
24408 case LOCKING_STYLE_AFP: {
24409 /* AFP locking uses the file path so it needs to be included in
24410 ** the afpLockingContext.
24411 */
24412 afpLockingContext *pCtx;
@@ -23784,10 +24420,11 @@
24420 pCtx->filePath = zFilename;
24421 srandomdev();
24422 }
24423 break;
24424 }
24425 #endif
24426
24427 case LOCKING_STYLE_DOTFILE: {
24428 /* Dotfile locking uses the file path so it needs to be included in
24429 ** the dotlockLockingContext
24430 */
@@ -23801,18 +24438,49 @@
24438 sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
24439 }
24440 pNew->lockingContext = zLockFile;
24441 break;
24442 }
24443
24444 #if IS_VXWORKS
24445 case LOCKING_STYLE_NAMEDSEM: {
24446 /* Named semaphore locking uses the file path so it needs to be
24447 ** included in the namedsemLockingContext
24448 */
24449 enterMutex();
24450 rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen);
24451 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
24452 char *zSemName = pNew->pOpen->aSemName;
24453 int n;
24454 sqlite3_snprintf(MAX_PATHNAME, zSemName, "%s.sem", pNew->zRealpath);
24455 for( n=0; zSemName[n]; n++ )
24456 if( zSemName[n]=='/' ) zSemName[n] = '_';
24457 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
24458 if( pNew->pOpen->pSem == SEM_FAILED ){
24459 rc = SQLITE_NOMEM;
24460 pNew->pOpen->aSemName[0] = '\0';
24461 }
24462 }
24463 leaveMutex();
24464 break;
24465 }
24466 #endif
24467
24468 case LOCKING_STYLE_FLOCK:
24469 case LOCKING_STYLE_NONE:
24470 break;
24471 #endif
24472 }
24473
24474 pNew->lastErrno = 0;
24475 #if IS_VXWORKS
24476 if( rc!=SQLITE_OK ){
24477 unlink(zFilename);
24478 isDelete = 0;
24479 }
24480 pNew->isDelete = isDelete;
24481 #endif
24482 if( rc!=SQLITE_OK ){
24483 if( dirfd>=0 ) close(dirfd);
24484 close(h);
24485 }else{
24486 pNew->pMethod = &aIoMethod[eLockingStyle-1];
@@ -23878,11 +24546,11 @@
24546 ** function failing.
24547 */
24548 SimulateIOError( return SQLITE_IOERR );
24549
24550 azDirs[0] = sqlite3_temp_directory;
24551 for(i=0; i<ArraySize(azDirs); i++){
24552 if( azDirs[i]==0 ) continue;
24553 if( stat(azDirs[i], &buf) ) continue;
24554 if( !S_ISDIR(buf.st_mode) ) continue;
24555 if( access(azDirs[i], 07) ) continue;
24556 zDir = azDirs[i];
@@ -23890,11 +24558,11 @@
24558 }
24559
24560 /* Check that the output buffer is large enough for the temporary file
24561 ** name. If it is not, return SQLITE_ERROR.
24562 */
24563 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
24564 return SQLITE_ERROR;
24565 }
24566
24567 do{
24568 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
@@ -24007,10 +24675,11 @@
24675 if( isCreate ) oflags |= O_CREAT;
24676 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
24677 oflags |= (O_LARGEFILE|O_BINARY);
24678
24679 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
24680 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags);
24681 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
24682 /* Failed to open the file for read/write access. Try read-only. */
24683 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
24684 flags |= SQLITE_OPEN_READONLY;
24685 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
@@ -24017,11 +24686,15 @@
24686 }
24687 if( fd<0 ){
24688 return SQLITE_CANTOPEN;
24689 }
24690 if( isDelete ){
24691 #if IS_VXWORKS
24692 zPath = zName;
24693 #else
24694 unlink(zName);
24695 #endif
24696 }
24697 if( pOutFlags ){
24698 *pOutFlags = flags;
24699 }
24700
@@ -24037,27 +24710,33 @@
24710 #ifdef FD_CLOEXEC
24711 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
24712 #endif
24713
24714 noLock = eType!=SQLITE_OPEN_MAIN_DB;
24715 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
24716 }
24717
24718 /*
24719 ** Delete the file at zPath. If the dirSync argument is true, fsync()
24720 ** the directory after deleting the file.
24721 */
24722 static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){
24723 int rc = SQLITE_OK;
24724 UNUSED_PARAMETER(NotUsed);
24725 SimulateIOError(return SQLITE_IOERR_DELETE);
24726 unlink(zPath);
24727 #ifndef SQLITE_DISABLE_DIRSYNC
24728 if( dirSync ){
24729 int fd;
24730 rc = openDirectory(zPath, &fd);
24731 if( rc==SQLITE_OK ){
24732 #if IS_VXWORKS
24733 if( fsync(fd)==-1 )
24734 #else
24735 if( fsync(fd) )
24736 #endif
24737 {
24738 rc = SQLITE_IOERR_DIR_FSYNC;
24739 }
24740 close(fd);
24741 }
24742 }
@@ -24074,16 +24753,17 @@
24753 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
24754 **
24755 ** Otherwise return 0.
24756 */
24757 static int unixAccess(
24758 sqlite3_vfs *NotUsed,
24759 const char *zPath,
24760 int flags,
24761 int *pResOut
24762 ){
24763 int amode = 0;
24764 UNUSED_PARAMETER(NotUsed);
24765 SimulateIOError( return SQLITE_IOERR_ACCESS; );
24766 switch( flags ){
24767 case SQLITE_ACCESS_EXISTS:
24768 amode = F_OK;
24769 break;
@@ -24124,10 +24804,24 @@
24804 ** current working directly has been unlinked.
24805 */
24806 SimulateIOError( return SQLITE_ERROR );
24807
24808 assert( pVfs->mxPathname==MAX_PATHNAME );
24809 UNUSED_PARAMETER(pVfs);
24810
24811 #if IS_VXWORKS
24812 {
24813 char *zRealname = vxrealpath(zPath, 0);
24814 zOut[0] = '\0';
24815 if( !zRealname ){
24816 return SQLITE_CANTOPEN;
24817 }
24818 sqlite3_snprintf(nOut, zOut, "%s", zRealname);
24819 sqlite3_free(zRealname);
24820 return SQLITE_OK;
24821 }
24822 #else
24823 zOut[nOut-1] = '\0';
24824 if( zPath[0]=='/' ){
24825 sqlite3_snprintf(nOut, zOut, "%s", zPath);
24826 }else{
24827 int nCwd;
@@ -24162,20 +24856,22 @@
24856 zFull[j++] = zFull[i];
24857 }
24858 zFull[j] = 0;
24859 }
24860 #endif
24861 #endif
24862 }
24863
24864
24865 #ifndef SQLITE_OMIT_LOAD_EXTENSION
24866 /*
24867 ** Interfaces for opening a shared library, finding entry points
24868 ** within the shared library, and closing the shared library.
24869 */
24870 #include <dlfcn.h>
24871 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
24872 UNUSED_PARAMETER(NotUsed);
24873 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
24874 }
24875
24876 /*
24877 ** SQLite calls this function immediately after a call to unixDlSym() or
@@ -24182,23 +24878,26 @@
24878 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
24879 ** message is available, it is written to zBufOut. If no error message
24880 ** is available, zBufOut is left unmodified and SQLite uses a default
24881 ** error message.
24882 */
24883 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
24884 char *zErr;
24885 UNUSED_PARAMETER(NotUsed);
24886 enterMutex();
24887 zErr = dlerror();
24888 if( zErr ){
24889 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
24890 }
24891 leaveMutex();
24892 }
24893 static void *unixDlSym(sqlite3_vfs *NotUsed, void *pHandle, const char*zSymbol){
24894 UNUSED_PARAMETER(NotUsed);
24895 return dlsym(pHandle, zSymbol);
24896 }
24897 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
24898 UNUSED_PARAMETER(NotUsed);
24899 dlclose(pHandle);
24900 }
24901 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24902 #define unixDlOpen 0
24903 #define unixDlError 0
@@ -24207,13 +24906,13 @@
24906 #endif
24907
24908 /*
24909 ** Write nBuf bytes of random data to the supplied buffer zBuf.
24910 */
24911 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
24912 UNUSED_PARAMETER(NotUsed);
24913 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
24914
24915 /* We have to initialize zBuf to prevent valgrind from reporting
24916 ** errors. The reports issued by valgrind are incorrect - we would
24917 ** prefer that the randomness be increased by making use of the
24918 ** uninitialized space in zBuf - but valgrind errors tend to worry
@@ -24234,11 +24933,11 @@
24933 time_t t;
24934 time(&t);
24935 memcpy(zBuf, &t, sizeof(t));
24936 pid = getpid();
24937 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
24938 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
24939 nBuf = sizeof(t) + sizeof(pid);
24940 }else{
24941 nBuf = read(fd, zBuf, nBuf);
24942 close(fd);
24943 }
@@ -24254,19 +24953,27 @@
24953 ** The return value is the number of microseconds of sleep actually
24954 ** requested from the underlying operating system, a number which
24955 ** might be greater than or equal to the argument, but not less
24956 ** than the argument.
24957 */
24958 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
24959 #if IS_VXWORKS
24960 struct timespec sp;
24961
24962 sp.tv_sec = microseconds / 1000000;
24963 sp.tv_nsec = (microseconds % 1000000) * 1000;
24964 nanosleep(&sp, NULL);
24965 return microseconds;
24966 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
24967 usleep(microseconds);
24968 return microseconds;
24969 #else
24970 int seconds = (microseconds+999999)/1000000;
24971 sleep(seconds);
24972 return seconds*1000000;
24973 #endif
24974 UNUSED_PARAMETER(NotUsed);
24975 }
24976
24977 /*
24978 ** The following variable, if set to a non-zero value, becomes the result
24979 ** returned from sqlite3OsCurrentTime(). This is used for testing.
@@ -24278,29 +24985,38 @@
24985 /*
24986 ** Find the current time (in Universal Coordinated Time). Write the
24987 ** current time and date as a Julian Day number into *prNow and
24988 ** return 0. Return 1 if the time and date cannot be found.
24989 */
24990 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
24991 #if IS_VXWORKS
24992 struct timespec sNow;
24993 clock_gettime(CLOCK_REALTIME, &sNow);
24994 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
24995 #elif defined(NO_GETTOD)
24996 time_t t;
24997 time(&t);
24998 *prNow = t/86400.0 + 2440587.5;
24999 #else
25000 struct timeval sNow;
25001 gettimeofday(&sNow, 0);
25002 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
25003 #endif
25004
25005 #ifdef SQLITE_TEST
25006 if( sqlite3_current_time ){
25007 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
25008 }
25009 #endif
25010 UNUSED_PARAMETER(NotUsed);
25011 return 0;
25012 }
25013
25014 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
25015 UNUSED_PARAMETER(NotUsed);
25016 UNUSED_PARAMETER(NotUsed2);
25017 UNUSED_PARAMETER(NotUsed3);
25018 return 0;
25019 }
25020
25021 /*
25022 ** Initialize the operating system interface.
@@ -24338,16 +25054,20 @@
25054 static sqlite3_vfs aVfs[] = {
25055 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
25056 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
25057 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
25058 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
25059 UNIXVFS("unix-none", LOCKING_STYLE_NONE),
25060 UNIXVFS("unix-namedsem",LOCKING_STYLE_NAMEDSEM),
25061 };
25062 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
25063 sqlite3_vfs_register(&aVfs[i], 0);
25064 }
25065 #endif
25066 #if IS_VXWORKS
25067 sqlite3HashInit(&nameHash, 1);
25068 #endif
25069 sqlite3_vfs_register(&unixVfs, 1);
25070 return SQLITE_OK;
25071 }
25072
25073 /*
@@ -24373,11 +25093,11 @@
25093 **
25094 ******************************************************************************
25095 **
25096 ** This file contains code that is specific to windows.
25097 **
25098 ** $Id: os_win.c,v 1.140 2008/11/19 21:35:47 shane Exp $
25099 */
25100 #if SQLITE_OS_WIN /* This file is used for windows only */
25101
25102
25103 /*
@@ -24844,11 +25564,11 @@
25564
25565 /*
25566 ** Convert multibyte character string to UTF-8. Space to hold the
25567 ** returned string is obtained from malloc().
25568 */
25569 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
25570 char *zFilenameUtf8;
25571 WCHAR *zTmpWide;
25572
25573 zTmpWide = mbcsToUnicode(zFilename);
25574 if( zTmpWide==0 ){
@@ -25342,15 +26062,22 @@
26062 if( flags & SQLITE_SYNC_FULL ){
26063 sqlite3_fullsync_count++;
26064 }
26065 sqlite3_sync_count++;
26066 #endif
26067 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
26068 ** no-op
26069 */
26070 #ifdef SQLITE_NO_SYNC
26071 return SQLITE_OK;
26072 #else
26073 if( FlushFileBuffers(pFile->h) ){
26074 return SQLITE_OK;
26075 }else{
26076 return SQLITE_IOERR;
26077 }
26078 #endif
26079 }
26080
26081 /*
26082 ** Determine the current size of a file in bytes
26083 */
@@ -25724,11 +26451,11 @@
26451 #if SQLITE_OS_WINCE==0
26452 }else{
26453 char *zUtf8;
26454 char zMbcsPath[MAX_PATH];
26455 GetTempPathA(MAX_PATH-30, zMbcsPath);
26456 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
26457 if( zUtf8 ){
26458 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
26459 free(zUtf8);
26460 }else{
26461 return SQLITE_NOMEM;
@@ -26065,11 +26792,11 @@
26792 free(zConverted);
26793 return SQLITE_NOMEM;
26794 }
26795 GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
26796 free(zConverted);
26797 zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
26798 free(zTemp);
26799 #endif
26800 }
26801 if( zOut ){
26802 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
@@ -26137,10 +26864,15 @@
26864 /*
26865 ** Write up to nBuf bytes of randomness into zBuf.
26866 */
26867 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
26868 int n = 0;
26869 UNUSED_PARAMETER(pVfs);
26870 #if defined(SQLITE_TEST)
26871 n = nBuf;
26872 memset(zBuf, 0, nBuf);
26873 #else
26874 if( sizeof(SYSTEMTIME)<=nBuf-n ){
26875 SYSTEMTIME x;
26876 GetSystemTime(&x);
26877 memcpy(&zBuf[n], &x, sizeof(x));
26878 n += sizeof(x);
@@ -26159,10 +26891,11 @@
26891 LARGE_INTEGER i;
26892 QueryPerformanceCounter(&i);
26893 memcpy(&zBuf[n], &i, sizeof(i));
26894 n += sizeof(i);
26895 }
26896 #endif
26897 return n;
26898 }
26899
26900
26901 /*
@@ -26316,24 +27049,45 @@
27049 ** sometimes grow into tens of thousands or larger. The size of the
27050 ** Bitvec object is the number of pages in the database file at the
27051 ** start of a transaction, and is thus usually less than a few thousand,
27052 ** but can be as large as 2 billion for a really big database.
27053 **
27054 ** @(#) $Id: bitvec.c,v 1.9 2008/11/19 18:30:35 shane Exp $
27055 */
27056
27057 /* Size of the Bitvec structure in bytes. */
27058 #define BITVEC_SZ 512
27059
27060 /* Round the union size down to the nearest pointer boundary, since that's how
27061 ** it will be aligned within the Bitvec struct. */
27062 #define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
27063
27064 /* Type of the array "element" for the bitmap representation.
27065 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
27066 ** Setting this to the "natural word" size of your CPU may improve
27067 ** performance. */
27068 #define BITVEC_TELEM u8
27069 /* Size, in bits, of the bitmap element. */
27070 #define BITVEC_SZELEM 8
27071 /* Number of elements in a bitmap array. */
27072 #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
27073 /* Number of bits in the bitmap array. */
27074 #define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
27075
27076 /* Number of u32 values in hash table. */
27077 #define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
27078 /* Maximum number of entries in hash table before
27079 ** sub-dividing and re-hashing. */
27080 #define BITVEC_MXHASH (BITVEC_NINT/2)
27081 /* Hashing function for the aHash representation.
27082 ** Empirical testing showed that the *37 multiplier
27083 ** (an arbitrary prime)in the hash function provided
27084 ** no fewer collisions than the no-op *1. */
27085 #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
27086
27087 #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
27088
 
27089
27090 /*
27091 ** A bitmap is an instance of the following structure.
27092 **
27093 ** This bitmap records the existance of zero or more bits
@@ -26353,15 +27107,19 @@
27107 ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
27108 ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
27109 ** to hold deal with values between 1 and iDivisor.
27110 */
27111 struct Bitvec {
27112 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
27113 u32 nSet; /* Number of bits that are set - only valid for aHash element */
27114 /* Max nSet is BITVEC_NINT. For BITVEC_SZ of 512, this would be 125. */
27115 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
27116 /* Should >=0 for apSub element. */
27117 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
27118 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
27119 union {
27120 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
27121 u32 aHash[BITVEC_NINT]; /* Hash table representation */
27122 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
27123 } u;
27124 };
27125
@@ -26386,20 +27144,23 @@
27144 ** i is out of range, then return false.
27145 */
27146 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
27147 if( p==0 ) return 0;
27148 if( i>p->iSize || i==0 ) return 0;
27149 i--;
27150 while( p->iDivisor ){
27151 u32 bin = i/p->iDivisor;
27152 i = i%p->iDivisor;
27153 p = p->u.apSub[bin];
27154 if (!p) {
27155 return 0;
27156 }
27157 }
27158 if( p->iSize<=BITVEC_NBIT ){
27159 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
27160 } else{
27161 u32 h = BITVEC_HASH(i++);
 
 
 
 
 
 
27162 while( p->u.aHash[h] ){
27163 if( p->u.aHash[h]==i ) return 1;
27164 h++;
27165 if( h>=BITVEC_NINT ) h = 0;
27166 }
@@ -26422,74 +27183,99 @@
27183 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
27184 u32 h;
27185 assert( p!=0 );
27186 assert( i>0 );
27187 assert( i<=p->iSize );
27188 i--;
27189 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
27190 u32 bin = i/p->iDivisor;
27191 i = i%p->iDivisor;
 
 
 
 
27192 if( p->u.apSub[bin]==0 ){
27193 sqlite3BeginBenignMalloc();
27194 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
27195 sqlite3EndBenignMalloc();
27196 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
27197 }
27198 p = p->u.apSub[bin];
27199 }
27200 if( p->iSize<=BITVEC_NBIT ){
27201 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
27202 return SQLITE_OK;
27203 }
27204 h = BITVEC_HASH(i++);
27205 /* if there wasn't a hash collision, and this doesn't */
27206 /* completely fill the hash, then just add it without */
27207 /* worring about sub-dividing and re-hashing. */
27208 if( !p->u.aHash[h] ){
27209 if (p->nSet<(BITVEC_NINT-1)) {
27210 goto bitvec_set_end;
27211 } else {
27212 goto bitvec_set_rehash;
27213 }
27214 }
27215 /* there was a collision, check to see if it's already */
27216 /* in hash, if not, try to find a spot for it */
27217 do {
27218 if( p->u.aHash[h]==i ) return SQLITE_OK;
27219 h++;
27220 if( h>=BITVEC_NINT ) h = 0;
27221 } while( p->u.aHash[h] );
27222 /* we didn't find it in the hash. h points to the first */
27223 /* available free spot. check to see if this is going to */
27224 /* make our hash too "full". */
27225 bitvec_set_rehash:
27226 if( p->nSet>=BITVEC_MXHASH ){
27227 unsigned int j;
27228 int rc;
27229 u32 aiValues[BITVEC_NINT];
27230 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
27231 memset(p->u.apSub, 0, sizeof(aiValues));
27232 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
27233 rc = sqlite3BitvecSet(p, i);
27234 for(j=0; j<BITVEC_NINT; j++){
27235 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
27236 }
27237 return rc;
27238 }
27239 bitvec_set_end:
27240 p->nSet++;
27241 p->u.aHash[h] = i;
27242 return SQLITE_OK;
27243 }
27244
27245 /*
27246 ** Clear the i-th bit.
 
27247 */
27248 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i){
27249 assert( p!=0 );
27250 assert( i>0 );
27251 i--;
27252 while( p->iDivisor ){
27253 u32 bin = i/p->iDivisor;
27254 i = i%p->iDivisor;
27255 p = p->u.apSub[bin];
27256 if (!p) {
27257 return;
27258 }
27259 }
27260 if( p->iSize<=BITVEC_NBIT ){
27261 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
 
 
 
 
 
 
 
27262 }else{
27263 unsigned int j;
27264 u32 aiValues[BITVEC_NINT];
27265 memcpy(aiValues, p->u.aHash, sizeof(aiValues));
27266 memset(p->u.aHash, 0, sizeof(aiValues));
27267 p->nSet = 0;
27268 for(j=0; j<BITVEC_NINT; j++){
27269 if( aiValues[j] && aiValues[j]!=(i+1) ){
27270 u32 h = BITVEC_HASH(aiValues[j]-1);
27271 p->nSet++;
27272 while( p->u.aHash[h] ){
27273 h++;
27274 if( h>=BITVEC_NINT ) h = 0;
27275 }
27276 p->u.aHash[h] = aiValues[j];
27277 }
27278 }
27279 }
27280 }
27281
@@ -26497,11 +27283,11 @@
27283 ** Destroy a bitmap object. Reclaim all memory used.
27284 */
27285 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
27286 if( p==0 ) return;
27287 if( p->iDivisor ){
27288 unsigned int i;
27289 for(i=0; i<BITVEC_NPTR; i++){
27290 sqlite3BitvecDestroy(p->u.apSub[i]);
27291 }
27292 }
27293 sqlite3_free(p);
@@ -26630,106 +27416,30 @@
27416 ** May you share freely, never taking more than you give.
27417 **
27418 *************************************************************************
27419 ** This file implements that page cache.
27420 **
27421 ** @(#) $Id: pcache.c,v 1.38 2008/11/19 16:52:44 danielk1977 Exp $
27422 */
27423
27424 /*
27425 ** A complete page cache is an instance of this structure.
 
 
 
27426 */
27427 struct PCache {
 
 
 
 
 
27428 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
27429 PgHdr *pSynced; /* Last synced page in dirty page list */
27430 int nRef; /* Number of referenced pages */
 
27431 int nMax; /* Configured cache size */
27432 int nMin; /* Configured minimum cache size */
 
 
 
 
 
 
 
27433 int szPage; /* Size of every page in this cache */
27434 int szExtra; /* Size of extra space for each page */
27435 int bPurgeable; /* True if pages are on backing store */
27436 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
27437 void *pStress; /* Argument to xStress */
27438 sqlite3_pcache *pCache; /* Pluggable cache module */
27439 PgHdr *pPage1;
27440 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27441
27442 /*
27443 ** Some of the assert() macros in this code are too expensive to run
27444 ** even during normal debugging. Use them only rarely on long-running
27445 ** tests. Enable the expensive asserts using the
@@ -26740,52 +27450,10 @@
27450 #else
27451 # define expensive_assert(X)
27452 #endif
27453
27454 /********************************** Linked List Management ********************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27455
27456 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
27457 /*
27458 ** Check that the pCache->pSynced variable is set correctly. If it
27459 ** is not, either fail an assert or return zero. Otherwise, return
@@ -26792,505 +27460,148 @@
27460 ** non-zero. This is only used in debugging builds, as follows:
27461 **
27462 ** expensive_assert( pcacheCheckSynced(pCache) );
27463 */
27464 static int pcacheCheckSynced(PCache *pCache){
27465 PgHdr *p;
27466 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
27467 assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
27468 }
27469 return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
27470 }
27471 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
27472
27473 /*
27474 ** Remove page pPage from the list of dirty pages.
27475 */
27476 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
27477 PCache *p = pPage->pCache;
27478
27479 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
27480 assert( pPage->pDirtyPrev || pPage==p->pDirty );
27481
27482 /* Update the PCache1.pSynced variable if necessary. */
27483 if( p->pSynced==pPage ){
27484 PgHdr *pSynced = pPage->pDirtyPrev;
27485 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
27486 pSynced = pSynced->pDirtyPrev;
27487 }
27488 p->pSynced = pSynced;
27489 }
27490
27491 if( pPage->pDirtyNext ){
27492 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
27493 }else{
27494 assert( pPage==p->pDirtyTail );
27495 p->pDirtyTail = pPage->pDirtyPrev;
27496 }
27497 if( pPage->pDirtyPrev ){
27498 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
27499 }else{
27500 assert( pPage==p->pDirty );
27501 p->pDirty = pPage->pDirtyNext;
27502 }
27503 pPage->pDirtyNext = 0;
27504 pPage->pDirtyPrev = 0;
27505
27506 expensive_assert( pcacheCheckSynced(p) );
27507 }
27508
27509 /*
27510 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
27511 ** pPage).
27512 */
27513 static void pcacheAddToDirtyList(PgHdr *pPage){
27514 PCache *p = pPage->pCache;
27515
27516 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
27517
27518 pPage->pDirtyNext = p->pDirty;
27519 if( pPage->pDirtyNext ){
27520 assert( pPage->pDirtyNext->pDirtyPrev==0 );
27521 pPage->pDirtyNext->pDirtyPrev = pPage;
27522 }
27523 p->pDirty = pPage;
27524 if( !p->pDirtyTail ){
27525 p->pDirtyTail = pPage;
27526 }
27527 if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
27528 p->pSynced = pPage;
27529 }
27530 expensive_assert( pcacheCheckSynced(p) );
27531 }
27532
27533 /*
27534 ** Wrapper around the pluggable caches xUnpin method. If the cache is
27535 ** being used for an in-memory database, this function is a no-op.
27536 */
27537 static void pcacheUnpin(PgHdr *p){
27538 PCache *pCache = p->pCache;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27539 if( pCache->bPurgeable ){
27540 if( p->pgno==1 ){
27541 pCache->pPage1 = 0;
27542 }
27543 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
27544 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27545 }
27546
27547 /*************************************************** General Interfaces ******
27548 **
27549 ** Initialize and shutdown the page cache subsystem. Neither of these
27550 ** functions are threadsafe.
27551 */
27552 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
27553 if( sqlite3GlobalConfig.pcache.xInit==0 ){
27554 sqlite3PCacheSetDefault();
27555 }
27556 return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
 
 
 
 
 
 
27557 }
27558 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
27559 if( sqlite3GlobalConfig.pcache.xShutdown ){
27560 sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
27561 }
27562 }
27563
27564 /*
27565 ** Return the size in bytes of a PCache object.
27566 */
27567 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
27568
27569 /*
27570 ** Create a new PCache object. Storage space to hold the object
27571 ** has already been allocated and is passed in as the p pointer.
27572 ** The caller discovers how much space needs to be allocated by
27573 ** calling sqlite3PcacheSize().
27574 */
27575 SQLITE_PRIVATE void sqlite3PcacheOpen(
27576 int szPage, /* Size of every page */
27577 int szExtra, /* Extra space associated with each page */
27578 int bPurgeable, /* True if pages are on backing store */
27579 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
27580 void *pStress, /* Argument to xStress */
27581 PCache *p /* Preallocated space for the PCache */
27582 ){
 
27583 memset(p, 0, sizeof(PCache));
27584 p->szPage = szPage;
27585 p->szExtra = szExtra;
27586 p->bPurgeable = bPurgeable;
27587 p->xStress = xStress;
27588 p->pStress = pStress;
27589 p->nMax = 100;
27590 p->nMin = 10;
 
 
 
 
 
 
 
 
27591 }
27592
27593 /*
27594 ** Change the page size for PCache object. The caller must ensure that there
27595 ** are no outstanding page references when this function is called.
27596 */
27597 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
27598 assert( pCache->nRef==0 && pCache->pDirty==0 );
27599 if( pCache->pCache ){
27600 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
27601 pCache->pCache = 0;
27602 }
27603 pCache->szPage = szPage;
27604 }
27605
27606 /*
27607 ** Try to obtain a page from the cache.
@@ -27299,96 +27610,106 @@
27610 PCache *pCache, /* Obtain the page from this cache */
27611 Pgno pgno, /* Page number to obtain */
27612 int createFlag, /* If true, create page if it does not exist already */
27613 PgHdr **ppPage /* Write the page here */
27614 ){
 
27615 PgHdr *pPage = 0;
27616 int eCreate;
27617
 
27618 assert( pCache!=0 );
27619 assert( pgno>0 );
27620
27621 /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
27622 ** allocate it now.
27623 */
27624 if( !pCache->pCache && createFlag ){
27625 sqlite3_pcache *p;
27626 int nByte;
27627 nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
27628 p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
27629 if( !p ){
27630 return SQLITE_NOMEM;
27631 }
27632 sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
27633 pCache->pCache = p;
27634 }
27635
27636 eCreate = createFlag ? 1 : 0;
27637 if( eCreate && (!pCache->bPurgeable || !pCache->pDirty) ){
27638 eCreate = 2;
27639 }
27640 if( pCache->pCache ){
27641 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
27642 }
27643
27644 if( !pPage && eCreate==1 ){
27645 PgHdr *pPg;
27646
27647 /* Find a dirty page to write-out and recycle. First try to find a
27648 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
27649 ** cleared), but if that is not possible settle for any other
27650 ** unreferenced dirty page.
27651 */
27652 expensive_assert( pcacheCheckSynced(pCache) );
27653 for(pPg=pCache->pSynced;
27654 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
27655 pPg=pPg->pDirtyPrev
27656 );
27657 if( !pPg ){
27658 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
27659 }
27660 if( pPg ){
27661 int rc;
27662 rc = pCache->xStress(pCache->pStress, pPg);
27663 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
27664 return rc;
27665 }
27666 }
27667
27668 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
27669 }
27670
27671 if( pPage ){
27672 if( 0==pPage->nRef ){
27673 pCache->nRef++;
27674 }
27675 pPage->nRef++;
27676 pPage->pData = (void*)&pPage[1];
27677 pPage->pExtra = (void*)&((char*)pPage->pData)[pCache->szPage];
27678 pPage->pCache = pCache;
27679 pPage->pgno = pgno;
27680 if( pgno==1 ){
27681 pCache->pPage1 = pPage;
27682 }
27683 }
27684 *ppPage = pPage;
27685 return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
27686 }
27687
27688 /*
27689 ** Decrement the reference count on a page. If the page is clean and the
27690 ** reference count drops to 0, then it is made elible for recycling.
27691 */
27692 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
27693 assert( p->nRef>0 );
27694 p->nRef--;
27695 if( p->nRef==0 ){
27696 PCache *pCache = p->pCache;
27697 pCache->nRef--;
27698 if( (p->flags&PGHDR_DIRTY)==0 ){
27699 pcacheUnpin(p);
27700 }else{
27701 /* Move the page to the head of the dirty list. */
27702 pcacheRemoveFromDirtyList(p);
27703 pcacheAddToDirtyList(p);
 
 
 
 
 
 
 
 
 
27704 }
27705 }
27706 }
27707
27708 /*
27709 ** Increase the reference count of a supplied page by 1.
27710 */
27711 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
27712 assert(p->nRef>0);
27713 p->nRef++;
27714 }
27715
@@ -27398,224 +27719,134 @@
27719 ** page pointed to by p is invalid.
27720 */
27721 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
27722 PCache *pCache;
27723 assert( p->nRef==1 );
27724 if( p->flags&PGHDR_DIRTY ){
27725 pcacheRemoveFromDirtyList(p);
27726 }
27727 pCache = p->pCache;
27728 pCache->nRef--;
27729 if( p->pgno==1 ){
27730 pCache->pPage1 = 0;
27731 }
27732 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
 
 
27733 }
27734
27735 /*
27736 ** Make sure the page is marked as dirty. If it isn't dirty already,
27737 ** make it so.
27738 */
27739 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
27740 PCache *pCache;
27741 p->flags &= ~PGHDR_DONT_WRITE;
 
 
27742 assert( p->nRef>0 );
27743 if( 0==(p->flags & PGHDR_DIRTY) ){
27744 pCache = p->pCache;
27745 p->flags |= PGHDR_DIRTY;
27746 pcacheAddToDirtyList( p);
27747 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27748 }
27749
27750 /*
27751 ** Make sure the page is marked as clean. If it isn't clean already,
27752 ** make it so.
27753 */
27754 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
27755 if( (p->flags & PGHDR_DIRTY) ){
27756 pcacheRemoveFromDirtyList(p);
27757 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
27758 if( p->nRef==0 ){
27759 pcacheUnpin(p);
27760 }
27761 }
27762 }
27763
27764 /*
27765 ** Make every page in the cache clean.
27766 */
27767 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
27768 PgHdr *p;
 
27769 while( (p = pCache->pDirty)!=0 ){
27770 sqlite3PcacheMakeClean(p);
27771 }
 
 
 
 
 
 
 
 
 
27772 }
27773
27774 /*
27775 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
27776 */
27777 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
27778 PgHdr *p;
27779 for(p=pCache->pDirty; p; p=p->pDirtyNext){
27780 p->flags &= ~PGHDR_NEED_SYNC;
27781 }
27782 pCache->pSynced = pCache->pDirtyTail;
27783 }
27784
27785 /*
27786 ** Change the page number of page p to newPgno.
27787 */
27788 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
27789 PCache *pCache = p->pCache;
27790 assert( p->nRef>0 );
27791 assert( newPgno>0 );
27792 sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
27793 p->pgno = newPgno;
27794 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
27795 pcacheRemoveFromDirtyList(p);
27796 pcacheAddToDirtyList(p);
27797 }
27798 }
27799
27800 /*
27801 ** Drop every cache entry whose page number is greater than "pgno". The
27802 ** caller must ensure that there are no outstanding references to any pages
27803 ** other than page 1 with a page number greater than pgno.
27804 **
27805 ** If there is a reference to page 1 and the pgno parameter passed to this
27806 ** function is 0, then the data area associated with page 1 is zeroed, but
27807 ** the page object is not dropped.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27808 */
27809 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
27810 if( pCache->pCache ){
27811 PgHdr *p;
27812 PgHdr *pNext;
27813 for(p=pCache->pDirty; p; p=pNext){
27814 pNext = p->pDirtyNext;
27815 if( p->pgno>pgno ){
27816 assert( p->flags&PGHDR_DIRTY );
27817 sqlite3PcacheMakeClean(p);
27818 }
27819 }
27820 if( pgno==0 && pCache->pPage1 ){
27821 memset(pCache->pPage1->pData, 0, pCache->szPage);
27822 pgno = 1;
27823 }
27824 sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27825 }
27826 }
27827
27828 /*
27829 ** Close a cache.
27830 */
27831 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
27832 if( pCache->pCache ){
27833 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
27834 }
27835 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27836
27837 /*
27838 ** Discard the contents of the cache.
27839 */
27840 SQLITE_PRIVATE int sqlite3PcacheClear(PCache *pCache){
27841 sqlite3PcacheTruncate(pCache, 0);
 
 
 
27842 return SQLITE_OK;
27843 }
27844
27845 /*
27846 ** Merge two lists of pages connected by pDirty and in pgno order.
27847 ** Do not both fixing the pDirtyPrev pointers.
27848 */
27849 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
27850 PgHdr result, *pTail;
27851 pTail = &result;
27852 while( pA && pB ){
@@ -27639,11 +27870,11 @@
27870 return result.pDirty;
27871 }
27872
27873 /*
27874 ** Sort the list of pages in accending order by pgno. Pages are
27875 ** connected by pDirty pointers. The pDirtyPrev pointers are
27876 ** corrupted by this sort.
27877 */
27878 #define N_SORT_BUCKET_ALLOC 25
27879 #define N_SORT_BUCKET 25
27880 #ifdef SQLITE_TEST
@@ -27688,101 +27919,762 @@
27919 /*
27920 ** Return a list of all dirty pages in the cache, sorted by page number.
27921 */
27922 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
27923 PgHdr *p;
27924 for(p=pCache->pDirty; p; p=p->pDirtyNext){
27925 p->pDirty = p->pDirtyNext;
27926 }
27927 return pcacheSortDirtyList(pCache->pDirty);
27928 }
27929
27930 /*
27931 ** Return the total number of referenced pages held by the cache.
27932 */
27933 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
27934 return pCache->nRef;
27935 }
27936
27937 /*
27938 ** Return the number of references to the page supplied as an argument.
27939 */
27940 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
27941 return p->nRef;
27942 }
27943
27944 /*
27945 ** Return the total number of pages in the cache.
27946 */
27947 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
27948 int nPage = 0;
27949 if( pCache->pCache ){
27950 nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
27951 }
27952 return nPage;
27953 }
27954
27955 #ifdef SQLITE_TEST
27956 /*
27957 ** Get the suggested cache-size value.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27958 */
27959 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
27960 return pCache->nMax;
27961 }
27962 #endif
27963
27964 /*
27965 ** Set the suggested cache-size value.
27966 */
27967 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
 
 
 
 
 
 
 
 
 
 
27968 pCache->nMax = mxPage;
27969 if( pCache->pCache ){
27970 sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
27971 }
27972 }
27973
27974 #ifdef SQLITE_CHECK_PAGES
27975 /*
27976 ** For all dirty pages currently in the cache, invoke the specified
27977 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
27978 ** defined.
27979 */
27980 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
27981 PgHdr *pDirty;
27982 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
27983 xIter(pDirty);
27984 }
27985 }
27986 #endif
27987
27988
27989 /************** End of pcache.c **********************************************/
27990 /************** Begin file pcache1.c *****************************************/
27991 /*
27992 ** 2008 November 05
27993 **
27994 ** The author disclaims copyright to this source code. In place of
27995 ** a legal notice, here is a blessing:
27996 **
27997 ** May you do good and not evil.
27998 ** May you find forgiveness for yourself and forgive others.
27999 ** May you share freely, never taking more than you give.
28000 **
28001 *************************************************************************
28002 **
28003 ** This file implements the default page cache implementation (the
28004 ** sqlite3_pcache interface). It also contains part of the implementation
28005 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
28006 ** If the default page cache implementation is overriden, then neither of
28007 ** these two features are available.
28008 **
28009 ** @(#) $Id: pcache1.c,v 1.3 2008/11/19 09:05:27 danielk1977 Exp $
28010 */
28011
28012
28013 typedef struct PCache1 PCache1;
28014 typedef struct PgHdr1 PgHdr1;
28015 typedef struct PgFreeslot PgFreeslot;
28016
28017 /* Pointers to structures of this type are cast and returned as
28018 ** opaque sqlite3_pcache* handles
28019 */
28020 struct PCache1 {
28021 /* Cache configuration parameters. Page size (szPage) and the purgeable
28022 ** flag (bPurgeable) are set when the cache is created. nMax may be
28023 ** modified at any time by a call to the pcache1CacheSize() method.
28024 ** The global mutex must be held when accessing nMax.
28025 */
28026 int szPage; /* Size of allocated pages in bytes */
28027 int bPurgeable; /* True if cache is purgeable */
28028 unsigned int nMin; /* Minimum number of pages reserved */
28029 unsigned int nMax; /* Configured "cache_size" value */
28030
28031 /* Hash table of all pages. The following variables may only be accessed
28032 ** when the accessor is holding the global mutex (see pcache1EnterMutex()
28033 ** and pcache1LeaveMutex()).
28034 */
28035 unsigned int nRecyclable; /* Number of pages in the LRU list */
28036 unsigned int nPage; /* Total number of pages in apHash */
28037 unsigned int nHash; /* Number of slots in apHash[] */
28038 PgHdr1 **apHash; /* Hash table for fast lookup by key */
28039 };
28040
28041 /*
28042 ** Each cache entry is represented by an instance of the following
28043 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
28044 ** directly after the structure in memory (see the PGHDR1_TO_PAGE()
28045 ** macro below).
28046 */
28047 struct PgHdr1 {
28048 unsigned int iKey; /* Key value (page number) */
28049 PgHdr1 *pNext; /* Next in hash table chain */
28050 PCache1 *pCache; /* Cache that currently owns this page */
28051 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
28052 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
28053 };
28054
28055 /*
28056 ** Free slots in the allocator used to divide up the buffer provided using
28057 ** the SQLITE_CONFIG_PAGECACHE mechanism.
28058 */
28059 struct PgFreeslot {
28060 PgFreeslot *pNext; /* Next free slot */
28061 };
28062
28063 /*
28064 ** Global data used by this cache.
28065 */
28066 static SQLITE_WSD struct PCacheGlobal {
28067 sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
28068
28069 int nMaxPage; /* Sum of nMaxPage for purgeable caches */
28070 int nMinPage; /* Sum of nMinPage for purgeable caches */
28071 int nCurrentPage; /* Number of purgeable pages allocated */
28072 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
28073
28074 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
28075 int szSlot; /* Size of each free slot */
28076 void *pStart, *pEnd; /* Bounds of pagecache malloc range */
28077 PgFreeslot *pFree; /* Free page blocks */
28078 } pcache1_g;
28079
28080 /*
28081 ** All code in this file should access the global structure above via the
28082 ** alias "pcache1". This ensures that the WSD emulation is used when
28083 ** compiling for systems that do not support real WSD.
28084 */
28085 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
28086
28087 /*
28088 ** When a PgHdr1 structure is allocated, the associated PCache1.szPage
28089 ** bytes of data are located directly after it in memory (i.e. the total
28090 ** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
28091 ** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
28092 ** an argument and returns a pointer to the associated block of szPage
28093 ** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
28094 ** a pointer to a block of szPage bytes of data and the return value is
28095 ** a pointer to the associated PgHdr1 structure.
28096 **
28097 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(X))==X );
28098 */
28099 #define PGHDR1_TO_PAGE(p) (void *)(&((unsigned char *)p)[sizeof(PgHdr1)])
28100 #define PAGE_TO_PGHDR1(p) (PgHdr1 *)(&((unsigned char *)p)[-1*sizeof(PgHdr1)])
28101
28102 /*
28103 ** Macros to enter and leave the global LRU mutex.
28104 */
28105 #define pcache1EnterMutex() sqlite3_mutex_enter(pcache1.mutex)
28106 #define pcache1LeaveMutex() sqlite3_mutex_leave(pcache1.mutex)
28107
28108 /******************************************************************************/
28109 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
28110
28111 /*
28112 ** This function is called during initialization if a static buffer is
28113 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
28114 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
28115 ** enough to contain 'n' buffers of 'sz' bytes each.
28116 */
28117 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
28118 PgFreeslot *p;
28119 sz &= ~7;
28120 pcache1.szSlot = sz;
28121 pcache1.pStart = pBuf;
28122 pcache1.pFree = 0;
28123 while( n-- ){
28124 p = (PgFreeslot*)pBuf;
28125 p->pNext = pcache1.pFree;
28126 pcache1.pFree = p;
28127 pBuf = (void*)&((char*)pBuf)[sz];
28128 }
28129 pcache1.pEnd = pBuf;
28130 }
28131
28132 /*
28133 ** Malloc function used within this file to allocate space from the buffer
28134 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
28135 ** such buffer exists or there is no space left in it, this function falls
28136 ** back to sqlite3Malloc().
28137 */
28138 static void *pcache1Alloc(int nByte){
28139 void *p;
28140 assert( sqlite3_mutex_held(pcache1.mutex) );
28141 if( nByte<=pcache1.szSlot && pcache1.pFree ){
28142 p = (PgHdr1 *)pcache1.pFree;
28143 pcache1.pFree = pcache1.pFree->pNext;
28144 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
28145 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
28146 }else{
28147
28148 /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
28149 ** global pcache mutex and unlock the pager-cache object pCache. This is
28150 ** so that if the attempt to allocate a new buffer causes the the
28151 ** configured soft-heap-limit to be breached, it will be possible to
28152 ** reclaim memory from this pager-cache.
28153 */
28154 pcache1LeaveMutex();
28155 p = sqlite3Malloc(nByte);
28156 pcache1EnterMutex();
28157 if( p ){
28158 int sz = sqlite3MallocSize(p);
28159 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
28160 }
28161 }
28162 return p;
28163 }
28164
28165 /*
28166 ** Free an allocated buffer obtained from pcache1Alloc().
28167 */
28168 static void pcache1Free(void *p){
28169 assert( sqlite3_mutex_held(pcache1.mutex) );
28170 if( p==0 ) return;
28171 if( p>=pcache1.pStart && p<pcache1.pEnd ){
28172 PgFreeslot *pSlot;
28173 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
28174 pSlot = (PgFreeslot*)p;
28175 pSlot->pNext = pcache1.pFree;
28176 pcache1.pFree = pSlot;
28177 }else{
28178 int iSize = sqlite3MallocSize(p);
28179 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
28180 sqlite3_free(p);
28181 }
28182 }
28183
28184 /*
28185 ** Allocate a new page object initially associated with cache pCache.
28186 */
28187 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
28188 int nByte = sizeof(PgHdr1) + pCache->szPage;
28189 PgHdr1 *p = (PgHdr1 *)pcache1Alloc(nByte);
28190 if( p ){
28191 memset(p, 0, nByte);
28192 if( pCache->bPurgeable ){
28193 pcache1.nCurrentPage++;
28194 }
28195 }
28196 return p;
28197 }
28198
28199 /*
28200 ** Free a page object allocated by pcache1AllocPage().
28201 */
28202 static void pcache1FreePage(PgHdr1 *p){
28203 if( p ){
28204 if( p->pCache->bPurgeable ){
28205 pcache1.nCurrentPage--;
28206 }
28207 pcache1Free(p);
28208 }
28209 }
28210
28211 /*
28212 ** Malloc function used by SQLite to obtain space from the buffer configured
28213 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
28214 ** exists, this function falls back to sqlite3Malloc().
28215 */
28216 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
28217 void *p;
28218 pcache1EnterMutex();
28219 p = pcache1Alloc(sz);
28220 pcache1LeaveMutex();
28221 return p;
28222 }
28223
28224 /*
28225 ** Free an allocated buffer obtained from sqlite3PageMalloc().
28226 */
28227 SQLITE_PRIVATE void sqlite3PageFree(void *p){
28228 pcache1EnterMutex();
28229 pcache1Free(p);
28230 pcache1LeaveMutex();
28231 }
28232
28233 /******************************************************************************/
28234 /******** General Implementation Functions ************************************/
28235
28236 /*
28237 ** This function is used to resize the hash table used by the cache passed
28238 ** as the first argument.
28239 **
28240 ** The global mutex must be held when this function is called.
28241 */
28242 static int pcache1ResizeHash(PCache1 *p){
28243 PgHdr1 **apNew;
28244 unsigned int nNew;
28245 unsigned int i;
28246
28247 assert( sqlite3_mutex_held(pcache1.mutex) );
28248
28249 nNew = p->nHash*2;
28250 if( nNew<256 ){
28251 nNew = 256;
28252 }
28253
28254 pcache1LeaveMutex();
28255 apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
28256 pcache1EnterMutex();
28257 if( apNew ){
28258 memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
28259 for(i=0; i<p->nHash; i++){
28260 PgHdr1 *pPage;
28261 PgHdr1 *pNext = p->apHash[i];
28262 while( (pPage = pNext) ){
28263 unsigned int h = pPage->iKey % nNew;
28264 pNext = pPage->pNext;
28265 pPage->pNext = apNew[h];
28266 apNew[h] = pPage;
28267 }
28268 }
28269 sqlite3_free(p->apHash);
28270 p->apHash = apNew;
28271 p->nHash = nNew;
28272 }
28273
28274 return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
28275 }
28276
28277 /*
28278 ** This function is used internally to remove the page pPage from the
28279 ** global LRU list, if is part of it. If pPage is not part of the global
28280 ** LRU list, then this function is a no-op.
28281 **
28282 ** The global mutex must be held when this function is called.
28283 */
28284 static void pcache1PinPage(PgHdr1 *pPage){
28285 assert( sqlite3_mutex_held(pcache1.mutex) );
28286 if( pPage && (pPage->pLruNext || pPage==pcache1.pLruTail) ){
28287 if( pPage->pLruPrev ){
28288 pPage->pLruPrev->pLruNext = pPage->pLruNext;
28289 }
28290 if( pPage->pLruNext ){
28291 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
28292 }
28293 if( pcache1.pLruHead==pPage ){
28294 pcache1.pLruHead = pPage->pLruNext;
28295 }
28296 if( pcache1.pLruTail==pPage ){
28297 pcache1.pLruTail = pPage->pLruPrev;
28298 }
28299 pPage->pLruNext = 0;
28300 pPage->pLruPrev = 0;
28301 pPage->pCache->nRecyclable--;
28302 }
28303 }
28304
28305
28306 /*
28307 ** Remove the page supplied as an argument from the hash table
28308 ** (PCache1.apHash structure) that it is currently stored in.
28309 **
28310 ** The global mutex must be held when this function is called.
28311 */
28312 static void pcache1RemoveFromHash(PgHdr1 *pPage){
28313 unsigned int h;
28314 PCache1 *pCache = pPage->pCache;
28315 PgHdr1 **pp;
28316
28317 h = pPage->iKey % pCache->nHash;
28318 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
28319 *pp = (*pp)->pNext;
28320
28321 pCache->nPage--;
28322 }
28323
28324 /*
28325 ** If there are currently more than pcache.nMaxPage pages allocated, try
28326 ** to recycle pages to reduce the number allocated to pcache.nMaxPage.
28327 */
28328 static void pcache1EnforceMaxPage(void){
28329 assert( sqlite3_mutex_held(pcache1.mutex) );
28330 while( pcache1.nCurrentPage>pcache1.nMaxPage && pcache1.pLruTail ){
28331 PgHdr1 *p = pcache1.pLruTail;
28332 pcache1PinPage(p);
28333 pcache1RemoveFromHash(p);
28334 pcache1FreePage(p);
28335 }
28336 }
28337
28338 /*
28339 ** Discard all pages from cache pCache with a page number (key value)
28340 ** greater than or equal to iLimit. Any pinned pages that meet this
28341 ** criteria are unpinned before they are discarded.
28342 **
28343 ** The global mutex must be held when this function is called.
28344 */
28345 static void pcache1TruncateUnsafe(
28346 PCache1 *pCache,
28347 unsigned int iLimit
28348 ){
28349 unsigned int h;
28350 assert( sqlite3_mutex_held(pcache1.mutex) );
28351 for(h=0; h<pCache->nHash; h++){
28352 PgHdr1 **pp = &pCache->apHash[h];
28353 PgHdr1 *pPage;
28354 while( (pPage = *pp) ){
28355 if( pPage->iKey>=iLimit ){
28356 pcache1PinPage(pPage);
28357 *pp = pPage->pNext;
28358 pcache1FreePage(pPage);
28359 }else{
28360 pp = &pPage->pNext;
28361 }
28362 }
28363 }
28364 }
28365
28366 /******************************************************************************/
28367 /******** sqlite3_pcache Methods **********************************************/
28368
28369 /*
28370 ** Implementation of the sqlite3_pcache.xInit method.
28371 */
28372 static int pcache1Init(void *NotUsed){
28373 UNUSED_PARAMETER(NotUsed);
28374 memset(&pcache1, 0, sizeof(pcache1));
28375 if( sqlite3GlobalConfig.bCoreMutex ){
28376 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
28377 }
28378 return SQLITE_OK;
28379 }
28380
28381 /*
28382 ** Implementation of the sqlite3_pcache.xShutdown method.
28383 */
28384 static void pcache1Shutdown(void *NotUsed){
28385 UNUSED_PARAMETER(NotUsed);
28386 /* no-op */
28387 }
28388
28389 /*
28390 ** Implementation of the sqlite3_pcache.xCreate method.
28391 **
28392 ** Allocate a new cache.
28393 */
28394 static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
28395 PCache1 *pCache;
28396
28397 pCache = (PCache1 *)sqlite3_malloc(sizeof(PCache1));
28398 if( pCache ){
28399 memset(pCache, 0, sizeof(PCache1));
28400 pCache->szPage = szPage;
28401 pCache->bPurgeable = (bPurgeable ? 1 : 0);
28402 if( bPurgeable ){
28403 pCache->nMin = 10;
28404 pcache1EnterMutex();
28405 pcache1.nMinPage += pCache->nMin;
28406 pcache1LeaveMutex();
28407 }
28408 }
28409 return (sqlite3_pcache *)pCache;
28410 }
28411
28412 /*
28413 ** Implementation of the sqlite3_pcache.xCachesize method.
28414 **
28415 ** Configure the cache_size limit for a cache.
28416 */
28417 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
28418 PCache1 *pCache = (PCache1 *)p;
28419 if( pCache->bPurgeable ){
28420 pcache1EnterMutex();
28421 pcache1.nMaxPage += (nMax - pCache->nMax);
28422 pCache->nMax = nMax;
28423 pcache1EnforceMaxPage();
28424 pcache1LeaveMutex();
28425 }
28426 }
28427
28428 /*
28429 ** Implementation of the sqlite3_pcache.xPagecount method.
28430 */
28431 static int pcache1Pagecount(sqlite3_pcache *p){
28432 int n;
28433 pcache1EnterMutex();
28434 n = ((PCache1 *)p)->nPage;
28435 pcache1LeaveMutex();
28436 return n;
28437 }
28438
28439 /*
28440 ** Implementation of the sqlite3_pcache.xFetch method.
28441 **
28442 ** Fetch a page by key value.
28443 **
28444 ** Whether or not a new page may be allocated by this function depends on
28445 ** the value of the createFlag argument.
28446 **
28447 ** There are three different approaches to obtaining space for a page,
28448 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
28449 **
28450 ** 1. Regardless of the value of createFlag, the cache is searched for a
28451 ** copy of the requested page. If one is found, it is returned.
28452 **
28453 ** 2. If createFlag==0 and the page is not already in the cache, NULL is
28454 ** returned.
28455 **
28456 ** 3. If createFlag is 1, the cache is marked as purgeable and the page is
28457 ** not already in the cache, and if either of the following are true,
28458 ** return NULL:
28459 **
28460 ** (a) the number of pages pinned by the cache is greater than
28461 ** PCache1.nMax, or
28462 ** (b) the number of pages pinned by the cache is greater than
28463 ** the sum of nMax for all purgeable caches, less the sum of
28464 ** nMin for all other purgeable caches.
28465 **
28466 ** 4. If none of the first three conditions apply and the cache is marked
28467 ** as purgeable, and if one of the following is true:
28468 **
28469 ** (a) The number of pages allocated for the cache is already
28470 ** PCache1.nMax, or
28471 **
28472 ** (b) The number of pages allocated for all purgeable caches is
28473 ** already equal to or greater than the sum of nMax for all
28474 ** purgeable caches,
28475 **
28476 ** then attempt to recycle a page from the LRU list. If it is the right
28477 ** size, return the recycled buffer. Otherwise, free the buffer and
28478 ** proceed to step 5.
28479 **
28480 ** 5. Otherwise, allocate and return a new page buffer.
28481 */
28482 static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
28483 unsigned int nPinned;
28484 PCache1 *pCache = (PCache1 *)p;
28485 PgHdr1 *pPage = 0;
28486
28487 pcache1EnterMutex();
28488 if( createFlag==1 ) sqlite3BeginBenignMalloc();
28489
28490 /* Search the hash table for an existing entry. */
28491 if( pCache->nHash>0 ){
28492 unsigned int h = iKey % pCache->nHash;
28493 for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
28494 }
28495
28496 if( pPage || createFlag==0 ){
28497 pcache1PinPage(pPage);
28498 goto fetch_out;
28499 }
28500
28501 /* Step 3 of header comment. */
28502 nPinned = pCache->nPage - pCache->nRecyclable;
28503 if( createFlag==1 && pCache->bPurgeable && (
28504 nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
28505 || nPinned>=(pCache->nMax)
28506 )){
28507 goto fetch_out;
28508 }
28509
28510 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
28511 goto fetch_out;
28512 }
28513
28514 /* Step 4. Try to recycle a page buffer if appropriate. */
28515 if( pCache->bPurgeable && pcache1.pLruTail && (
28516 pCache->nPage>=pCache->nMax-1 || pcache1.nCurrentPage>=pcache1.nMaxPage
28517 )){
28518 pPage = pcache1.pLruTail;
28519 pcache1RemoveFromHash(pPage);
28520 pcache1PinPage(pPage);
28521 if( pPage->pCache->szPage!=pCache->szPage ){
28522 pcache1FreePage(pPage);
28523 pPage = 0;
28524 }else{
28525 pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable);
28526 }
28527 }
28528
28529 /* Step 5. If a usable page buffer has still not been found,
28530 ** attempt to allocate a new one.
28531 */
28532 if( !pPage ){
28533 pPage = pcache1AllocPage(pCache);
28534 }
28535
28536 if( pPage ){
28537 unsigned int h = iKey % pCache->nHash;
28538 memset(pPage, 0, pCache->szPage + sizeof(PgHdr1));
28539 pCache->nPage++;
28540 pPage->iKey = iKey;
28541 pPage->pNext = pCache->apHash[h];
28542 pPage->pCache = pCache;
28543 pCache->apHash[h] = pPage;
28544 }
28545
28546 fetch_out:
28547 if( createFlag==1 ) sqlite3EndBenignMalloc();
28548 pcache1LeaveMutex();
28549 return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
28550 }
28551
28552
28553 /*
28554 ** Implementation of the sqlite3_pcache.xUnpin method.
28555 **
28556 ** Mark a page as unpinned (eligible for asynchronous recycling).
28557 */
28558 static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
28559 PCache1 *pCache = (PCache1 *)p;
28560 PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
28561
28562 pcache1EnterMutex();
28563
28564 /* It is an error to call this function if the page is already
28565 ** part of the global LRU list.
28566 */
28567 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
28568 assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage );
28569
28570 if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){
28571 pcache1RemoveFromHash(pPage);
28572 pcache1FreePage(pPage);
28573 }else{
28574 /* Add the page to the global LRU list. Normally, the page is added to
28575 ** the head of the list (last page to be recycled). However, if the
28576 ** reuseUnlikely flag passed to this function is true, the page is added
28577 ** to the tail of the list (first page to be recycled).
28578 */
28579 if( pcache1.pLruHead ){
28580 pcache1.pLruHead->pLruPrev = pPage;
28581 pPage->pLruNext = pcache1.pLruHead;
28582 pcache1.pLruHead = pPage;
28583 }else{
28584 pcache1.pLruTail = pPage;
28585 pcache1.pLruHead = pPage;
28586 }
28587 pCache->nRecyclable++;
28588 }
28589
28590 pcache1LeaveMutex();
28591 }
28592
28593 /*
28594 ** Implementation of the sqlite3_pcache.xRekey method.
28595 */
28596 static void pcache1Rekey(
28597 sqlite3_pcache *p,
28598 void *pPg,
28599 unsigned int iOld,
28600 unsigned int iNew
28601 ){
28602 PCache1 *pCache = (PCache1 *)p;
28603 PgHdr1 *pPage = PAGE_TO_PGHDR1(pPg);
28604 PgHdr1 **pp;
28605 unsigned int h;
28606 assert( pPage->iKey==iOld );
28607
28608 pcache1EnterMutex();
28609
28610 h = iOld%pCache->nHash;
28611 pp = &pCache->apHash[h];
28612 while( (*pp)!=pPage ){
28613 pp = &(*pp)->pNext;
28614 }
28615 *pp = pPage->pNext;
28616
28617 h = iNew%pCache->nHash;
28618 pPage->iKey = iNew;
28619 pPage->pNext = pCache->apHash[h];
28620 pCache->apHash[h] = pPage;
28621
28622 pcache1LeaveMutex();
28623 }
28624
28625 /*
28626 ** Implementation of the sqlite3_pcache.xTruncate method.
28627 **
28628 ** Discard all unpinned pages in the cache with a page number equal to
28629 ** or greater than parameter iLimit. Any pinned pages with a page number
28630 ** equal to or greater than iLimit are implicitly unpinned.
28631 */
28632 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
28633 PCache1 *pCache = (PCache1 *)p;
28634 pcache1EnterMutex();
28635 pcache1TruncateUnsafe(pCache, iLimit);
28636 pcache1LeaveMutex();
28637 }
28638
28639 /*
28640 ** Implementation of the sqlite3_pcache.xDestroy method.
28641 **
28642 ** Destroy a cache allocated using pcache1Create().
28643 */
28644 static void pcache1Destroy(sqlite3_pcache *p){
28645 PCache1 *pCache = (PCache1 *)p;
28646 pcache1EnterMutex();
28647 pcache1TruncateUnsafe(pCache, 0);
28648 pcache1.nMaxPage -= pCache->nMax;
28649 pcache1.nMinPage -= pCache->nMin;
28650 pcache1EnforceMaxPage();
28651 pcache1LeaveMutex();
28652 sqlite3_free(pCache->apHash);
28653 sqlite3_free(pCache);
28654 }
28655
28656 /*
28657 ** This function is called during initialization (sqlite3_initialize()) to
28658 ** install the default pluggable cache module, assuming the user has not
28659 ** already provided an alternative.
28660 */
28661 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
28662 static sqlite3_pcache_methods defaultMethods = {
28663 0, /* pArg */
28664 pcache1Init, /* xInit */
28665 pcache1Shutdown, /* xShutdown */
28666 pcache1Create, /* xCreate */
28667 pcache1Cachesize, /* xCachesize */
28668 pcache1Pagecount, /* xPagecount */
28669 pcache1Fetch, /* xFetch */
28670 pcache1Unpin, /* xUnpin */
28671 pcache1Rekey, /* xRekey */
28672 pcache1Truncate, /* xTruncate */
28673 pcache1Destroy /* xDestroy */
28674 };
28675 sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
28676 }
28677
28678 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
28679 /*
28680 ** This function is called to free superfluous dynamically allocated memory
@@ -27793,44 +28685,49 @@
28685 ** been released, the function returns. The return value is the total number
28686 ** of bytes of memory released.
28687 */
28688 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
28689 int nFree = 0;
28690 if( pcache1.pStart==0 ){
28691 PgHdr1 *p;
28692 pcache1EnterMutex();
28693 while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){
28694 nFree += sqlite3MallocSize(p);
28695 pcache1PinPage(p);
28696 pcache1RemoveFromHash(p);
28697 pcache1FreePage(p);
28698 }
28699 pcache1LeaveMutex();
28700 }
28701 return nFree;
28702 }
28703 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
28704
28705 #ifdef SQLITE_TEST
28706 /*
28707 ** This function is used by test procedures to inspect the internal state
28708 ** of the global cache.
28709 */
28710 SQLITE_PRIVATE void sqlite3PcacheStats(
28711 int *pnCurrent, /* OUT: Total number of pages cached */
28712 int *pnMax, /* OUT: Global maximum cache size */
28713 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
28714 int *pnRecyclable /* OUT: Total number of pages available for recycling */
28715 ){
28716 PgHdr1 *p;
28717 int nRecyclable = 0;
28718 for(p=pcache1.pLruHead; p; p=p->pLruNext){
28719 nRecyclable++;
28720 }
28721 *pnCurrent = pcache1.nCurrentPage;
28722 *pnMax = pcache1.nMaxPage;
28723 *pnMin = pcache1.nMinPage;
 
28724 *pnRecyclable = nRecyclable;
28725 }
28726 #endif
28727
28728 /************** End of pcache1.c *********************************************/
28729 /************** Begin file pager.c *******************************************/
28730 /*
28731 ** 2001 September 15
28732 **
28733 ** The author disclaims copyright to this source code. In place of
@@ -27848,11 +28745,11 @@
28745 ** is separate from the database file. The pager also implements file
28746 ** locking to prevent two processes from writing the same database
28747 ** file simultaneously, or one process from reading the database while
28748 ** another is writing.
28749 **
28750 ** @(#) $Id: pager.c,v 1.506 2008/11/19 18:30:29 drh Exp $
28751 */
28752 #ifndef SQLITE_OMIT_DISKIO
28753
28754 /*
28755 ** Macros for troubleshooting. Normally turned off
@@ -28005,15 +28902,16 @@
28902 u8 doNotSync; /* Boolean. While true, do not spill the cache */
28903 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
28904 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
28905 u8 dbModified; /* True if there are any changes to the Db */
28906 u8 changeCountDone; /* Set after incrementing the change-counter */
28907 u8 dbSizeValid; /* Set when dbSize is correct */
28908 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
28909 int errCode; /* One of several kinds of errors */
28910 Pgno dbSize; /* Number of pages in the file */
28911 Pgno origDbSize; /* dbSize before the current change */
28912 Pgno stmtSize; /* Size of database (in pages) at stmt_begin() */
28913 int nRec; /* Number of pages written to the journal */
28914 u32 cksumInit; /* Quasi-random value added to every checksum */
28915 int stmtNRec; /* Number of records in stmt subjournal */
28916 int nExtra; /* Add this many bytes to each in-memory page */
28917 int pageSize; /* Number of bytes in a page */
@@ -28026,11 +28924,12 @@
28924 char *zFilename; /* Name of the database file */
28925 char *zJournal; /* Name of the journal file */
28926 char *zDirectory; /* Directory hold database and journal files */
28927 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
28928 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
28929 int (*xBusyHandler)(void*); /* Function to call when busy */
28930 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
28931 i64 journalOff; /* Current byte offset in the journal file */
28932 i64 journalHdr; /* Byte offset to previous journal header */
28933 i64 stmtHdrOff; /* First journal header written this statement */
28934 i64 stmtCksum; /* cksumInit when statement was started */
28935 i64 stmtJSize; /* Size of journal at stmt_begin() */
@@ -28124,11 +29023,11 @@
29023 ** used in the journal to signify that the remainder of the journal file
29024 ** is devoted to storing a master journal name - there are no more pages to
29025 ** roll back. See comments for function writeMasterJournal() for details.
29026 */
29027 /* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
29028 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
29029
29030 /*
29031 ** The maximum legal page number is (2^31 - 1).
29032 */
29033 #define PAGER_MAX_PGNO 2147483647
@@ -28140,10 +29039,14 @@
29039 */
29040 static int pageInStatement(PgHdr *pPg){
29041 Pager *pPager = pPg->pPager;
29042 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
29043 }
29044
29045 static int pageInJournal(PgHdr *pPg){
29046 return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
29047 }
29048
29049 /*
29050 ** Read a 32-bit integer from the given file descriptor. Store the integer
29051 ** that is read in *pRes. Return SQLITE_OK if everything worked, or an
29052 ** error code is something goes wrong.
@@ -28283,11 +29186,11 @@
29186 return hash;
29187 }
29188 static u32 pager_pagehash(PgHdr *pPage){
29189 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
29190 }
29191 static void pager_set_pagehash(PgHdr *pPage){
29192 pPage->pageHash = pager_pagehash(pPage);
29193 }
29194
29195 /*
29196 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
@@ -28320,11 +29223,11 @@
29223 ** were present in the journal.
29224 **
29225 ** If no master journal file name is present zMaster[0] is set to 0 and
29226 ** SQLITE_OK returned.
29227 */
29228 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
29229 int rc;
29230 u32 len;
29231 i64 szJ;
29232 u32 cksum;
29233 u32 u; /* Unsigned loop counter */
@@ -28452,12 +29355,12 @@
29355 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
29356 */
29357 static int writeJournalHdr(Pager *pPager){
29358 int rc = SQLITE_OK;
29359 char *zHeader = pPager->pTmpSpace;
29360 u32 nHeader = pPager->pageSize;
29361 u32 nWrite;
29362
29363 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
29364 nHeader = JOURNAL_HDR_SZ(pPager);
29365 }
29366
@@ -28712,11 +29615,11 @@
29615 */
29616 static void pager_unlock(Pager *pPager){
29617 if( !pPager->exclusiveMode ){
29618 int rc = osUnlock(pPager->fd, NO_LOCK);
29619 if( rc ) pPager->errCode = rc;
29620 pPager->dbSizeValid = 0;
29621 IOTRACE(("UNLOCK %p\n", pPager))
29622
29623 /* Always close the journal file when dropping the database lock.
29624 ** Otherwise, another connection with journal_mode=delete might
29625 ** delete the file out from under us.
@@ -28826,17 +29729,14 @@
29729 }
29730 sqlite3BitvecDestroy(pPager->pInJournal);
29731 pPager->pInJournal = 0;
29732 sqlite3BitvecDestroy(pPager->pAlwaysRollback);
29733 pPager->pAlwaysRollback = 0;
 
29734 #ifdef SQLITE_CHECK_PAGES
29735 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
29736 #endif
29737 sqlite3PcacheCleanAll(pPager->pPCache);
 
 
29738 pPager->dirtyCache = 0;
29739 pPager->nRec = 0;
29740 }else{
29741 assert( pPager->pInJournal==0 );
29742 }
@@ -28850,11 +29750,11 @@
29750 pPager->origDbSize = 0;
29751 pPager->setMaster = 0;
29752 pPager->needSync = 0;
29753 /* lruListSetFirstSynced(pPager); */
29754 if( !MEMDB ){
29755 pPager->dbSizeValid = 0;
29756 }
29757 pPager->dbModified = 0;
29758
29759 return (rc==SQLITE_OK?rc2:rc);
29760 }
@@ -29138,11 +30038,11 @@
30038 ** operating system implementations can get confused if you try to
30039 ** truncate a file to some size that is larger than it currently is,
30040 ** so detect this case and write a single zero byte to the end of the new
30041 ** file instead.
30042 */
30043 static int pager_truncate(Pager *pPager, Pgno nPage){
30044 int rc = SQLITE_OK;
30045 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
30046 i64 currentSize, newSize;
30047 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
30048 newSize = pPager->pageSize*(i64)nPage;
@@ -29647,11 +30547,11 @@
30547 }
30548
30549 /* Open the pager file.
30550 */
30551 if( zFilename && zFilename[0] && !memDb ){
30552 if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){
30553 rc = SQLITE_CANTOPEN;
30554 }else{
30555 int fout = 0;
30556 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
30557 pPager->vfsFlags, &fout);
@@ -29738,11 +30638,11 @@
30638 pPager->useJournal = useJournal;
30639 pPager->noReadlock = noReadlock && readOnly;
30640 /* pPager->stmtOpen = 0; */
30641 /* pPager->stmtInUse = 0; */
30642 /* pPager->nRef = 0; */
30643 pPager->dbSizeValid = memDb;
30644 pPager->pageSize = szPageDflt;
30645 /* pPager->stmtSize = 0; */
30646 /* pPager->stmtJSize = 0; */
30647 /* pPager->nPage = 0; */
30648 pPager->mxPage = 100;
@@ -29769,21 +30669,27 @@
30669 assert(pPager->fd->pMethods||tempFile);
30670 setSectorSize(pPager);
30671 if( memDb ){
30672 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
30673 }
30674 /* pPager->xBusyHandler = 0; */
30675 /* pPager->pBusyHandlerArg = 0; */
30676 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
30677 *ppPager = pPager;
30678 return SQLITE_OK;
30679 }
30680
30681 /*
30682 ** Set the busy handler function.
30683 */
30684 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
30685 Pager *pPager,
30686 int (*xBusyHandler)(void *),
30687 void *pBusyHandlerArg
30688 ){
30689 pPager->xBusyHandler = xBusyHandler;
30690 pPager->pBusyHandlerArg = pBusyHandlerArg;
30691 }
30692
30693 /*
30694 ** Set the reinitializer for this pager. If not NULL, the reinitializer
30695 ** is called when the content of a page in cache is restored to its original
@@ -29916,11 +30822,11 @@
30822 assert( pPager!=0 );
30823 if( pPager->errCode ){
30824 rc = pPager->errCode;
30825 return rc;
30826 }
30827 if( pPager->dbSizeValid ){
30828 n = pPager->dbSize;
30829 } else {
30830 assert(pPager->fd->pMethods||pPager->tempFile);
30831 if( (pPager->fd->pMethods)
30832 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
@@ -29932,10 +30838,11 @@
30838 }else{
30839 n /= pPager->pageSize;
30840 }
30841 if( pPager->state!=PAGER_UNLOCK ){
30842 pPager->dbSize = n;
30843 pPager->dbSizeValid = 1;
30844 }
30845 }
30846 if( n==(PENDING_BYTE/pPager->pageSize) ){
30847 n++;
30848 }
@@ -29984,19 +30891,18 @@
30891 assert( PAGER_SHARED==SHARED_LOCK );
30892 assert( PAGER_RESERVED==RESERVED_LOCK );
30893 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
30894
30895 /* If the file is currently unlocked then the size must be unknown */
30896 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
30897
30898 if( pPager->state>=locktype ){
30899 rc = SQLITE_OK;
30900 }else{
 
30901 do {
30902 rc = sqlite3OsLock(pPager->fd, locktype);
30903 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
30904 if( rc==SQLITE_OK ){
30905 pPager->state = locktype;
30906 IOTRACE(("LOCK %p %d\n", pPager, locktype))
30907 }
30908 }
@@ -30011,11 +30917,11 @@
30917 assert( pPager->state>=PAGER_SHARED );
30918
30919 sqlite3PagerPagecount(pPager, 0);
30920 if( pPager->errCode ){
30921 rc = pPager->errCode;
30922 }else if( nPage<pPager->dbSize ){
30923 rc = syncJournal(pPager);
30924 if( rc==SQLITE_OK ){
30925 /* Get an exclusive lock on the database before truncating. */
30926 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
30927 }
@@ -30168,22 +31074,12 @@
31074 }
31075 pPager->needSync = 0;
31076
31077 /* Erase the needSync flag from every page.
31078 */
31079 sqlite3PcacheClearSyncFlags(pPager->pPCache);
31080 }
 
 
 
 
 
 
 
 
 
 
31081
31082 return rc;
31083 }
31084
31085 /*
@@ -30531,10 +31427,11 @@
31427 if( pPager->errCode ){
31428 rc = pPager->errCode;
31429 goto failed;
31430 }
31431
31432 assert( pPager->dbSizeValid );
31433 if( pPager->dbSize>0 ){
31434 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
31435 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
31436 if( rc!=SQLITE_OK ){
31437 goto failed;
@@ -30681,13 +31578,10 @@
31578 ** be initialized.
31579 */
31580 int nMax;
31581 PAGER_INCR(pPager->nMiss);
31582 pPg->pPager = pPager;
 
 
 
31583 memset(pPg->pExtra, 0, pPager->nExtra);
31584
31585 rc = sqlite3PagerPagecount(pPager, &nMax);
31586 if( rc!=SQLITE_OK ){
31587 sqlite3PagerUnref(pPg);
@@ -30887,11 +31781,10 @@
31781 assert( pPg->nRef>0 );
31782 assert( pPager->state!=PAGER_UNLOCK );
31783 if( pPager->state==PAGER_SHARED ){
31784 assert( pPager->pInJournal==0 );
31785 assert( !MEMDB );
 
31786 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
31787 if( rc==SQLITE_OK ){
31788 pPager->state = PAGER_RESERVED;
31789 if( exFlag ){
31790 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
@@ -30996,13 +31889,11 @@
31889
31890 /* Mark the page as dirty. If the page has already been written
31891 ** to the journal then we can return right away.
31892 */
31893 makeDirty(pPg);
31894 if( pageInJournal(pPg) && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
 
 
31895 pPager->dirtyCache = 1;
31896 pPager->dbModified = 1;
31897 }else{
31898
31899 /* If we get this far, it means that the page needs to be
@@ -31028,12 +31919,12 @@
31919
31920 /* The transaction journal now exists and we have a RESERVED or an
31921 ** EXCLUSIVE lock on the main database file. Write the current page to
31922 ** the transaction journal if it is not there already.
31923 */
31924 if( !pageInJournal(pPg) && pPager->journalOpen ){
31925 if( pPg->pgno<=pPager->origDbSize ){
31926 u32 cksum;
31927 char *pData2;
31928
31929 /* We should never write to the journal file the page that
31930 ** contains the database locks. The following assert verifies
@@ -31083,26 +31974,24 @@
31974 ((pPg->flags&PGHDR_NEED_SYNC)?1:0));
31975 }
31976 if( pPg->flags&PGHDR_NEED_SYNC ){
31977 pPager->needSync = 1;
31978 }
 
31979 }
31980
31981 /* If the statement journal is open and the page is not in it,
31982 ** then write the current page to the statement journal. Note that
31983 ** the statement journal format differs from the standard journal format
31984 ** in that it omits the checksums and the header.
31985 */
31986 if( pPager->stmtInUse
31987 && !pageInStatement(pPg)
31988 && pPg->pgno<=pPager->stmtSize
31989 ){
31990 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
31991 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
31992 assert( pageInJournal(pPg) || pPg->pgno>pPager->origDbSize );
 
31993 rc = write32bits(pPager->stfd, offset, pPg->pgno);
31994 if( rc==SQLITE_OK ){
31995 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
31996 }
31997 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
@@ -31116,13 +32005,13 @@
32005 }
32006
32007 /* Update the database size and return.
32008 */
32009 assert( pPager->state>=PAGER_SHARED );
32010 if( pPager->dbSize<pPg->pgno ){
32011 pPager->dbSize = pPg->pgno;
32012 if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){
32013 pPager->dbSize++;
32014 }
32015 }
32016 return rc;
32017 }
@@ -31276,11 +32165,11 @@
32165 }
32166 rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno);
32167
32168 if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){
32169 assert( pPager->state>=PAGER_SHARED );
32170 if( pPager->dbSize==pPg->pgno && pPager->origDbSize<pPager->dbSize ){
32171 /* If this pages is the last page in the file and the file has grown
32172 ** during the current transaction, then do NOT mark the page as clean.
32173 ** When the database file grows, we must make sure that the last page
32174 ** gets written at least once so that the disk file will be the correct
32175 ** size. If you do not write this page and the size of the file
@@ -31325,11 +32214,12 @@
32214 ){
32215 return;
32216 }
32217
32218 #ifdef SQLITE_SECURE_DELETE
32219 if( sqlite3BitvecTest(pPager->pInJournal, pPg->pgno)!=0
32220 || pPg->pgno>pPager->origDbSize ){
32221 return;
32222 }
32223 #endif
32224
32225 /* If SECURE_DELETE is disabled, then there is no way that this
@@ -31344,11 +32234,10 @@
32234 */
32235 /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */
32236
32237 assert( pPager->pInJournal!=0 );
32238 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
 
32239 pPg->flags &= ~PGHDR_NEED_READ;
32240 if( pPager->stmtInUse ){
32241 assert( pPager->stmtSize >= pPager->origDbSize );
32242 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
32243 }
@@ -31525,11 +32414,11 @@
32414 /* If this transaction has made the database smaller, then all pages
32415 ** being discarded by the truncation must be written to the journal
32416 ** file.
32417 */
32418 Pgno i;
32419 Pgno iSkip = PAGER_MJ_PGNO(pPager);
32420 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
32421 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
32422 rc = sqlite3PagerGet(pPager, i, &pPg);
32423 if( rc!=SQLITE_OK ) goto sync_exit;
32424 rc = sqlite3PagerWrite(pPg);
@@ -31655,11 +32544,11 @@
32544 }else{
32545 rc = pager_playback(pPager, 0);
32546 }
32547
32548 if( !MEMDB ){
32549 pPager->dbSizeValid = 0;
32550 }
32551
32552 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
32553 ** cache. So call pager_error() on the way out to make any error
32554 ** persistent.
@@ -31698,11 +32587,11 @@
32587 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
32588 static int a[11];
32589 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
32590 a[1] = sqlite3PcachePagecount(pPager->pPCache);
32591 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
32592 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
32593 a[4] = pPager->state;
32594 a[5] = pPager->errCode;
32595 a[6] = pPager->nHit;
32596 a[7] = pPager->nMiss;
32597 a[8] = 0; /* Used to be pPager->nOvfl */
@@ -31724,11 +32613,11 @@
32613 */
32614 static int pagerStmtBegin(Pager *pPager){
32615 int rc;
32616 assert( !pPager->stmtInUse );
32617 assert( pPager->state>=PAGER_SHARED );
32618 assert( pPager->dbSizeValid );
32619 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
32620 if( !pPager->journalOpen ){
32621 pPager->stmtAutoopen = 1;
32622 return SQLITE_OK;
32623 }
@@ -31906,34 +32795,30 @@
32795 ** the journal needs to be sync()ed before database page pPg->pgno
32796 ** can be written to. The caller has already promised not to write to it.
32797 */
32798 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
32799 needSyncPgno = pPg->pgno;
32800 assert( pageInJournal(pPg) || pgno>pPager->origDbSize );
32801 assert( pPg->flags&PGHDR_DIRTY );
32802 assert( pPager->needSync );
32803 }
32804
32805 /* If the cache contains a page with page-number pgno, remove it
32806 ** from its hash chain. Also, if the PgHdr.needSync was set for
32807 ** page pgno before the 'move' operation, it needs to be retained
32808 ** for the page moved there.
32809 */
32810 pPg->flags &= ~PGHDR_NEED_SYNC;
32811 pPgOld = pager_lookup(pPager, pgno);
32812 assert( !pPgOld || pPgOld->nRef==1 );
32813 if( pPgOld ){
32814 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
32815 }
 
 
 
32816
32817 sqlite3PcacheMove(pPg, pgno);
32818 if( pPgOld ){
32819 sqlite3PcacheDrop(pPgOld);
 
32820 }
32821
32822 makeDirty(pPg);
32823 pPager->dirtyCache = 1;
32824 pPager->dbModified = 1;
@@ -31959,19 +32844,18 @@
32844 int rc;
32845 PgHdr *pPgHdr;
32846 assert( pPager->needSync );
32847 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
32848 if( rc!=SQLITE_OK ){
32849 if( pPager->pInJournal && needSyncPgno<=pPager->origDbSize ){
32850 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
32851 }
32852 return rc;
32853 }
32854 pPager->needSync = 1;
32855 assert( pPager->noSync==0 && !MEMDB );
32856 pPgHdr->flags |= PGHDR_NEED_SYNC;
 
32857 makeDirty(pPgHdr);
32858 sqlite3PagerUnref(pPgHdr);
32859 }
32860
32861 return SQLITE_OK;
@@ -32074,11 +32958,11 @@
32958 ** May you find forgiveness for yourself and forgive others.
32959 ** May you share freely, never taking more than you give.
32960 **
32961 *************************************************************************
32962 **
32963 ** $Id: btmutex.c,v 1.12 2008/11/17 19:18:55 danielk1977 Exp $
32964 **
32965 ** This file contains code used to implement mutexes on Btree objects.
32966 ** This code really belongs in btree.c. But btree.c is getting too
32967 ** big and we want to break it down some. This packaged seemed like
32968 ** a good breakout.
@@ -32094,11 +32978,11 @@
32978 ** May you do good and not evil.
32979 ** May you find forgiveness for yourself and forgive others.
32980 ** May you share freely, never taking more than you give.
32981 **
32982 *************************************************************************
32983 ** $Id: btreeInt.h,v 1.36 2008/11/19 10:22:33 danielk1977 Exp $
32984 **
32985 ** This file implements a external (disk-based) database using BTrees.
32986 ** For a detailed discussion of BTrees, refer to
32987 **
32988 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
@@ -32464,11 +33348,10 @@
33348 u8 inTransaction; /* Transaction state */
33349 int nTransaction; /* Number of open transactions (read + write) */
33350 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
33351 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
33352 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
 
33353 #ifndef SQLITE_OMIT_SHARED_CACHE
33354 int nRef; /* Number of references to this structure */
33355 BtShared *pNext; /* Next on a list of sharable BtShared structs */
33356 BtLock *pLock; /* List of locks held on this shared-btree struct */
33357 Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */
@@ -32582,11 +33465,11 @@
33465 ** in memory) then there is no pending byte.
33466 */
33467 #ifdef SQLITE_OMIT_DISKIO
33468 # define PENDING_BYTE_PAGE(pBt) 0x7fffffff
33469 #else
33470 # define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/(pBt)->pageSize)+1))
33471 #endif
33472
33473 /*
33474 ** A linked list of the following structures is stored at BtShared.pLock.
33475 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
@@ -32689,11 +33572,11 @@
33572 */
33573 typedef struct IntegrityCk IntegrityCk;
33574 struct IntegrityCk {
33575 BtShared *pBt; /* The tree being checked out */
33576 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
33577 Pgno nPage; /* Number of pages in the database */
33578 int *anRef; /* Number of times each page is referenced */
33579 int mxErr; /* Stop accumulating errors when this reaches zero */
33580 int nErr; /* Number of messages written to zErrMsg so far */
33581 int mallocFailed; /* A memory allocation error has occurred */
33582 StrAccum errMsg; /* Accumulate the error message text here */
@@ -32949,11 +33832,11 @@
33832 assert( pArray->aBtree[i]!=pBtree );
33833 }
33834 }
33835 #endif
33836 assert( pArray->nMutex>=0 );
33837 assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
33838 pBt = pBtree->pBt;
33839 for(i=0; i<pArray->nMutex; i++){
33840 assert( pArray->aBtree[i]!=pBtree );
33841 if( pArray->aBtree[i]->pBt>pBt ){
33842 for(j=pArray->nMutex; j>i; j--){
@@ -33028,11 +33911,11 @@
33911 ** May you do good and not evil.
33912 ** May you find forgiveness for yourself and forgive others.
33913 ** May you share freely, never taking more than you give.
33914 **
33915 *************************************************************************
33916 ** $Id: btree.c,v 1.539.2.1 2008/11/22 14:07:49 drh Exp $
33917 **
33918 ** This file implements a external (disk-based) database using BTrees.
33919 ** See the header comment on "btreeInt.h" for additional information.
33920 ** Including a description of file format and an overview of operation.
33921 */
@@ -33453,11 +34336,12 @@
34336 ** Given a page number of a regular database page, return the page
34337 ** number for the pointer-map page that contains the entry for the
34338 ** input page number.
34339 */
34340 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
34341 int nPagesPerMapPage;
34342 Pgno iPtrMap, ret;
34343 assert( sqlite3_mutex_held(pBt->mutex) );
34344 nPagesPerMapPage = (pBt->usableSize/5)+1;
34345 iPtrMap = (pgno-2)/nPagesPerMapPage;
34346 ret = (iPtrMap*nPagesPerMapPage) + 2;
34347 if( ret==PENDING_BYTE_PAGE(pBt) ){
@@ -33720,11 +34604,11 @@
34604 ** Defragment the page given. All Cells are moved to the
34605 ** end of the page and all free space is collected into one
34606 ** big FreeBlk that occurs in between the header and cell
34607 ** pointer array and the cell content area.
34608 */
34609 static int defragmentPage(MemPage *pPage){
34610 int i; /* Loop counter */
34611 int pc; /* Address of a i-th cell */
34612 int addr; /* Offset of first byte after cell pointer array */
34613 int hdr; /* Offset to the page header */
34614 int size; /* Size of a cell */
@@ -33752,13 +34636,19 @@
34636 cbrk = usableSize;
34637 for(i=0; i<nCell; i++){
34638 u8 *pAddr; /* The i-th cell pointer */
34639 pAddr = &data[cellOffset + i*2];
34640 pc = get2byte(pAddr);
34641 if( pc>=usableSize ){
34642 return SQLITE_CORRUPT_BKPT;
34643 }
34644 size = cellSizePtr(pPage, &temp[pc]);
34645 cbrk -= size;
34646 if( cbrk<cellOffset+2*nCell || pc+size>usableSize ){
34647 return SQLITE_CORRUPT_BKPT;
34648 }
34649 assert( cbrk+size<=usableSize && cbrk>=0 );
34650 memcpy(&data[cbrk], &temp[pc], size);
34651 put2byte(pAddr, cbrk);
34652 }
34653 assert( cbrk>=cellOffset+2*nCell );
34654 put2byte(&data[hdr+5], cbrk);
@@ -33765,10 +34655,14 @@
34655 data[hdr+1] = 0;
34656 data[hdr+2] = 0;
34657 data[hdr+7] = 0;
34658 addr = cellOffset+2*nCell;
34659 memset(&data[addr], 0, cbrk-addr);
34660 if( cbrk-addr!=pPage->nFree ){
34661 return SQLITE_CORRUPT_BKPT;
34662 }
34663 return SQLITE_OK;
34664 }
34665
34666 /*
34667 ** Allocate nByte bytes of space on a page.
34668 **
@@ -33843,11 +34737,11 @@
34737 ** and the size of the block is "size" bytes.
34738 **
34739 ** Most of the effort here is involved in coalesing adjacent
34740 ** free blocks into a single big free block.
34741 */
34742 static int freeSpace(MemPage *pPage, int start, int size){
34743 int addr, pbegin, hdr;
34744 unsigned char *data = pPage->aData;
34745
34746 assert( pPage->pBt!=0 );
34747 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -33865,14 +34759,18 @@
34759 /* Add the space back into the linked list of freeblocks */
34760 hdr = pPage->hdrOffset;
34761 addr = hdr + 1;
34762 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
34763 assert( pbegin<=pPage->pBt->usableSize-4 );
34764 if( pbegin<=addr ) {
34765 return SQLITE_CORRUPT_BKPT;
34766 }
34767 addr = pbegin;
34768 }
34769 if ( pbegin>pPage->pBt->usableSize-4 ) {
34770 return SQLITE_CORRUPT_BKPT;
34771 }
34772 assert( pbegin>addr || pbegin==0 );
34773 put2byte(&data[addr], start);
34774 put2byte(&data[start], pbegin);
34775 put2byte(&data[start+2], size);
34776 pPage->nFree += size;
@@ -33885,11 +34783,13 @@
34783 assert( pbegin<=pPage->pBt->usableSize-4 );
34784 pnext = get2byte(&data[pbegin]);
34785 psize = get2byte(&data[pbegin+2]);
34786 if( pbegin + psize + 3 >= pnext && pnext>0 ){
34787 int frag = pnext - (pbegin+psize);
34788 if( (frag<0) || (frag>data[pPage->hdrOffset+7]) ){
34789 return SQLITE_CORRUPT_BKPT;
34790 }
34791 data[pPage->hdrOffset+7] -= frag;
34792 put2byte(&data[pbegin], get2byte(&data[pnext]));
34793 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
34794 }else{
34795 addr = pbegin;
@@ -33902,10 +34802,11 @@
34802 pbegin = get2byte(&data[hdr+1]);
34803 memcpy(&data[hdr+1], &data[pbegin], 2);
34804 top = get2byte(&data[hdr+5]);
34805 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
34806 }
34807 return SQLITE_OK;
34808 }
34809
34810 /*
34811 ** Decode the flags byte (the first byte of the header) for a page
34812 ** and initialize fields of the MemPage structure accordingly.
@@ -34109,18 +35010,20 @@
35010 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
35011 return SQLITE_OK;
35012 }
35013
35014 /*
35015 ** Return the size of the database file in pages. If there is any kind of
35016 ** error, return ((unsigned int)-1).
35017 */
35018 static Pgno pagerPagecount(BtShared *pBt){
35019 int nPage = -1;
35020 int rc;
35021 assert( pBt->pPage1 );
35022 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
35023 assert( rc==SQLITE_OK || nPage==-1 );
35024 return (Pgno)nPage;
35025 }
35026
35027 /*
35028 ** Get a page from the pager and initialize it. This routine
35029 ** is just a convenience wrapper around separate calls to
@@ -34150,11 +35053,11 @@
35053 /* Page is already in cache */
35054 *ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt);
35055 rc = SQLITE_OK;
35056 }else{
35057 /* Page not in cache. Acquire it. */
35058 if( pgno>pagerPagecount(pBt) ){
35059 return SQLITE_CORRUPT_BKPT;
35060 }
35061 rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
35062 if( rc ) return rc;
35063 pPage = *ppPage;
@@ -34205,11 +35108,11 @@
35108 }
35109
35110 /*
35111 ** Invoke the busy handler for a btree.
35112 */
35113 static int btreeInvokeBusyHandler(void *pArg){
35114 BtShared *pBt = (BtShared*)pArg;
35115 assert( pBt->db );
35116 assert( sqlite3_mutex_held(pBt->db->mutex) );
35117 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
35118 }
@@ -34322,21 +35225,19 @@
35225 pBt = sqlite3MallocZero( sizeof(*pBt) );
35226 if( pBt==0 ){
35227 rc = SQLITE_NOMEM;
35228 goto btree_open_out;
35229 }
 
 
35230 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
35231 EXTRA_SIZE, flags, vfsFlags);
35232 if( rc==SQLITE_OK ){
35233 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
35234 }
35235 if( rc!=SQLITE_OK ){
35236 goto btree_open_out;
35237 }
35238 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
35239 p->pBt = pBt;
35240
35241 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
35242 pBt->pCursor = 0;
35243 pBt->pPage1 = 0;
@@ -35030,11 +35931,11 @@
35931 if( wrflag ) pBt->inStmt = 0;
35932 }else{
35933 unlockBtreeIfUnused(pBt);
35934 }
35935 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
35936 btreeInvokeBusyHandler(pBt) );
35937
35938 if( rc==SQLITE_OK ){
35939 if( p->inTrans==TRANS_NONE ){
35940 pBt->nTransaction++;
35941 }
@@ -35054,11 +35955,10 @@
35955 trans_begun:
35956 btreeIntegrity(p);
35957 sqlite3BtreeLeave(p);
35958 return rc;
35959 }
 
35960
35961 #ifndef SQLITE_OMIT_AUTOVACUUM
35962
35963 /*
35964 ** Set the pointer-map entries for all children of page pPage. Also, if
@@ -35269,11 +36169,11 @@
36169 Pgno nFreeList; /* Number of pages still on the free-list */
36170
36171 assert( sqlite3_mutex_held(pBt->mutex) );
36172 iLastPg = pBt->nTrunc;
36173 if( iLastPg==0 ){
36174 iLastPg = pagerPagecount(pBt);
36175 }
36176
36177 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
36178 int rc;
36179 u8 eType;
@@ -35400,11 +36300,11 @@
36300
36301 if( pBt->nTrunc==0 ){
36302 Pgno nFree;
36303 Pgno nPtrmap;
36304 const int pgsz = pBt->pageSize;
36305 Pgno nOrig = pagerPagecount(pBt);
36306
36307 if( PTRMAP_ISPAGE(pBt, nOrig) ){
36308 return SQLITE_CORRUPT_BKPT;
36309 }
36310 if( nOrig==PENDING_BYTE_PAGE(pBt) ){
@@ -35445,11 +36345,11 @@
36345 }
36346 assert( nRef==sqlite3PagerRefcount(pPager) );
36347 return rc;
36348 }
36349
36350 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
36351
36352 /*
36353 ** This routine does the first phase of a two-phase commit. This routine
36354 ** causes a rollback journal to be created (if it does not already exist)
36355 ** and populated with enough information so that if a power loss occurs
@@ -35612,13 +36512,18 @@
36512 */
36513 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
36514 BtCursor *p;
36515 sqlite3BtreeEnter(pBtree);
36516 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
36517 int i;
36518 sqlite3BtreeClearCursor(p);
36519 p->eState = CURSOR_FAULT;
36520 p->skip = errCode;
36521 for(i=0; i<=p->iPage; i++){
36522 releasePage(p->apPage[i]);
36523 p->apPage[i] = 0;
36524 }
36525 }
36526 sqlite3BtreeLeave(pBtree);
36527 }
36528
36529 /*
@@ -35801,10 +36706,11 @@
36706 int wrFlag, /* 1 to write. 0 read-only */
36707 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
36708 BtCursor *pCur /* Space for new cursor */
36709 ){
36710 int rc;
36711 Pgno nPage;
36712 BtShared *pBt = p->pBt;
36713
36714 assert( sqlite3BtreeHoldsMutex(p) );
36715 if( wrFlag ){
36716 if( pBt->readOnly ){
@@ -35823,11 +36729,15 @@
36729 if( pBt->readOnly && wrFlag ){
36730 return SQLITE_READONLY;
36731 }
36732 }
36733 pCur->pgnoRoot = (Pgno)iTable;
36734 rc = sqlite3PagerPagecount(pBt->pPager, (int *)&nPage);
36735 if( rc!=SQLITE_OK ){
36736 return rc;
36737 }
36738 if( iTable==1 && nPage==0 ){
36739 rc = SQLITE_EMPTY;
36740 goto create_cursor_exception;
36741 }
36742 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
36743 if( rc!=SQLITE_OK ){
@@ -36084,11 +36994,11 @@
36994
36995 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
36996 iGuess++;
36997 }
36998
36999 if( iGuess<=pagerPagecount(pBt) ){
37000 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
37001 if( rc!=SQLITE_OK ){
37002 return rc;
37003 }
37004 if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
@@ -36180,37 +37090,38 @@
37090 ** * A commit in auto_vacuum="full" mode,
37091 ** * Creating a table (may require moving an overflow page).
37092 */
37093 static int accessPayload(
37094 BtCursor *pCur, /* Cursor pointing to entry to read from */
37095 u32 offset, /* Begin reading this far into payload */
37096 u32 amt, /* Read this many bytes */
37097 unsigned char *pBuf, /* Write the bytes into this buffer */
37098 int skipKey, /* offset begins at data if this is true */
37099 int eOp /* zero to read. non-zero to write. */
37100 ){
37101 unsigned char *aPayload;
37102 int rc = SQLITE_OK;
37103 u32 nKey;
37104 int iIdx = 0;
37105 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
37106 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
37107
37108 assert( pPage );
37109 assert( pCur->eState==CURSOR_VALID );
37110 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
 
37111 assert( cursorHoldsMutex(pCur) );
37112
37113 getCellInfo(pCur);
37114 aPayload = pCur->info.pCell + pCur->info.nHeader;
37115 nKey = (pPage->intKey ? 0 : pCur->info.nKey);
37116
37117 if( skipKey ){
37118 offset += nKey;
37119 }
37120 if( offset+amt > nKey+pCur->info.nData
37121 || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
37122 ){
37123 /* Trying to read or write past the end of the data is an error */
37124 return SQLITE_CORRUPT_BKPT;
37125 }
37126
37127 /* Check if data must be read/written to/from the btree page itself. */
@@ -36225,13 +37136,12 @@
37136 amt -= a;
37137 }else{
37138 offset -= pCur->info.nLocal;
37139 }
37140
 
37141 if( rc==SQLITE_OK && amt>0 ){
37142 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
37143 Pgno nextPage;
37144
37145 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
37146
37147 #ifndef SQLITE_OMIT_INCRBLOB
@@ -36394,11 +37304,11 @@
37304 int skipKey /* read beginning at data if this is true */
37305 ){
37306 unsigned char *aPayload;
37307 MemPage *pPage;
37308 u32 nKey;
37309 u32 nLocal;
37310
37311 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
37312 assert( pCur->eState==CURSOR_VALID );
37313 assert( cursorHoldsMutex(pCur) );
37314 pPage = pCur->apPage[pCur->iPage];
@@ -37084,11 +37994,11 @@
37994 /* If the 'exact' parameter was true and a query of the pointer-map
37995 ** shows that the page 'nearby' is somewhere on the free-list, then
37996 ** the entire-list will be searched for that page.
37997 */
37998 #ifndef SQLITE_OMIT_AUTOVACUUM
37999 if( exact && nearby<=pagerPagecount(pBt) ){
38000 u8 eType;
38001 assert( nearby>0 );
38002 assert( pBt->autoVacuum );
38003 rc = ptrmapGet(pBt, nearby, &eType, 0);
38004 if( rc ) return rc;
@@ -37219,13 +38129,13 @@
38129 closest = 0;
38130 }
38131
38132 iPage = get4byte(&aData[8+closest*4]);
38133 if( !searchList || iPage==nearby ){
38134 Pgno nPage;
38135 *pPgno = iPage;
38136 nPage = pagerPagecount(pBt);
38137 if( *pPgno>nPage ){
38138 /* Free page off the end of the file */
38139 rc = SQLITE_CORRUPT_BKPT;
38140 goto end_allocate_page;
38141 }
@@ -37251,11 +38161,11 @@
38161 pPrevTrunk = 0;
38162 }while( searchList );
38163 }else{
38164 /* There are no pages on the freelist, so create a new page at the
38165 ** end of the file */
38166 int nPage = pagerPagecount(pBt);
38167 *pPgno = nPage + 1;
38168
38169 #ifndef SQLITE_OMIT_AUTOVACUUM
38170 if( pBt->nTrunc ){
38171 /* An incr-vacuum has already run within this transaction. So the
@@ -37295,13 +38205,16 @@
38205 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
38206
38207 end_allocate_page:
38208 releasePage(pTrunk);
38209 releasePage(pPrevTrunk);
38210 if( rc==SQLITE_OK ){
38211 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
38212 releasePage(*ppPage);
38213 return SQLITE_CORRUPT_BKPT;
38214 }
38215 (*ppPage)->isInit = 0;
38216 }
38217 return rc;
38218 }
38219
38220 /*
@@ -37418,11 +38331,11 @@
38331 ovflPageSize = pBt->usableSize - 4;
38332 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
38333 assert( ovflPgno==0 || nOvfl>0 );
38334 while( nOvfl-- ){
38335 MemPage *pOvfl;
38336 if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){
38337 return SQLITE_CORRUPT_BKPT;
38338 }
38339
38340 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
38341 if( rc ) return rc;
@@ -37480,11 +38393,11 @@
38393 }
38394 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
38395 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
38396 assert( info.nHeader==nHeader );
38397 assert( info.nKey==nKey );
38398 assert( info.nData==(u32)(nData+nZero) );
38399
38400 /* Fill in the payload */
38401 nPayload = nData + nZero;
38402 if( pPage->intKey ){
38403 pSrc = pData;
@@ -37577,32 +38490,39 @@
38490 ** the cell content has been copied someplace else. This routine just
38491 ** removes the reference to the cell from pPage.
38492 **
38493 ** "sz" must be the number of bytes in the cell.
38494 */
38495 static int dropCell(MemPage *pPage, int idx, int sz){
38496 int i; /* Loop counter */
38497 int pc; /* Offset to cell content of cell being deleted */
38498 u8 *data; /* pPage->aData */
38499 u8 *ptr; /* Used to move bytes around within data[] */
38500 int rc; /* The return code */
38501
38502 assert( idx>=0 && idx<pPage->nCell );
38503 assert( sz==cellSize(pPage, idx) );
38504 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
38505 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
38506 data = pPage->aData;
38507 ptr = &data[pPage->cellOffset + 2*idx];
38508 pc = get2byte(ptr);
38509 if ( (pc<pPage->hdrOffset+6+(pPage->leaf?0:4)) || (pc+sz>pPage->pBt->usableSize) ) {
38510 return SQLITE_CORRUPT_BKPT;
38511 }
38512 rc = freeSpace(pPage, pc, sz);
38513 if( rc!=SQLITE_OK ){
38514 return rc;
38515 }
38516 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
38517 ptr[0] = ptr[2];
38518 ptr[1] = ptr[3];
38519 }
38520 pPage->nCell--;
38521 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
38522 pPage->nFree += 2;
38523 return SQLITE_OK;
38524 }
38525
38526 /*
38527 ** Insert a new cell on pPage at cell index "i". pCell points to the
38528 ** content of the cell.
@@ -37645,11 +38565,11 @@
38565 if( pTemp ){
38566 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
38567 pCell = pTemp;
38568 }
38569 j = pPage->nOverflow++;
38570 assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
38571 pPage->aOvfl[j].pCell = pCell;
38572 pPage->aOvfl[j].idx = i;
38573 pPage->nFree = 0;
38574 }else{
38575 int rc = sqlite3PagerWrite(pPage->pDbPage);
@@ -37662,17 +38582,23 @@
38582 top = get2byte(&data[hdr+5]);
38583 cellOffset = pPage->cellOffset;
38584 end = cellOffset + 2*pPage->nCell + 2;
38585 ins = cellOffset + 2*i;
38586 if( end > top - sz ){
38587 rc = defragmentPage(pPage);
38588 if( rc!=SQLITE_OK ){
38589 return rc;
38590 }
38591 top = get2byte(&data[hdr+5]);
38592 assert( end + sz <= top );
38593 }
38594 idx = allocateSpace(pPage, sz);
38595 assert( idx>0 );
38596 assert( end <= get2byte(&data[hdr+5]) );
38597 if (idx+sz > pPage->pBt->usableSize) {
38598 return SQLITE_CORRUPT_BKPT;
38599 }
38600 pPage->nCell++;
38601 pPage->nFree -= 2;
38602 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
38603 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
38604 ptr[0] = ptr[-2];
@@ -38526,11 +39452,11 @@
39452 ** the virtual root of the tree.
39453 */
39454 VVA_ONLY( pCur->pagesShuffled = 1 );
39455 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
39456 assert( pgnoChild>0 );
39457 assert( pgnoChild<=pagerPagecount(pPage->pBt) );
39458 rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
39459 if( rc ) goto end_shallow_balance;
39460 if( pPage->pgno==1 ){
39461 rc = sqlite3BtreeInitPage(pChild);
39462 if( rc ) goto end_shallow_balance;
@@ -38563,13 +39489,15 @@
39489 freePage(pChild);
39490 TRACE(("BALANCE: transfer child %d into root %d\n",
39491 pChild->pgno, pPage->pgno));
39492 }
39493 assert( pPage->nOverflow==0 );
39494 #ifndef SQLITE_OMIT_AUTOVACUUM
39495 if( ISAUTOVACUUM ){
39496 rc = setChildPtrmaps(pPage);
39497 }
39498 #endif
39499 releasePage(pChild);
39500 }
39501 end_shallow_balance:
39502 sqlite3_free(apCell);
39503 return rc;
@@ -38612,11 +39540,12 @@
39540 hdr = pPage->hdrOffset;
39541 cbrk = get2byte(&data[hdr+5]);
39542 cdata = pChild->aData;
39543 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
39544 memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk);
39545
39546 assert( pChild->isInit==0 );
39547 rc = sqlite3BtreeInitPage(pChild);
39548 if( rc==SQLITE_OK ){
39549 int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]);
39550 memcpy(pChild->aOvfl, pPage->aOvfl, nCopy);
39551 pChild->nOverflow = pPage->nOverflow;
@@ -38627,13 +39556,15 @@
39556 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
39557 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
39558 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
39559 if( ISAUTOVACUUM ){
39560 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
39561 #ifndef SQLITE_OMIT_AUTOVACUUM
39562 if( rc==SQLITE_OK ){
39563 rc = setChildPtrmaps(pChild);
39564 }
39565 #endif
39566 }
39567 }
39568
39569 if( rc==SQLITE_OK ){
39570 pCur->iPage++;
@@ -38826,11 +39757,14 @@
39757 memcpy(newCell, oldCell, 4);
39758 }
39759 szOld = cellSizePtr(pPage, oldCell);
39760 rc = clearCell(pPage, oldCell);
39761 if( rc ) goto end_insert;
39762 rc = dropCell(pPage, idx, szOld);
39763 if( rc!=SQLITE_OK ) {
39764 goto end_insert;
39765 }
39766 }else if( loc<0 && pPage->nCell>0 ){
39767 assert( pPage->leaf );
39768 idx = ++pCur->aiIdx[pCur->iPage];
39769 pCur->info.nSize = 0;
39770 pCur->validNKey = 0;
@@ -39037,12 +39971,14 @@
39971 }
39972 sqlite3BtreeReleaseTempCursor(&leafCur);
39973 }else{
39974 TRACE(("DELETE: table=%d delete from leaf %d\n",
39975 pCur->pgnoRoot, pPage->pgno));
39976 rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell));
39977 if( rc==SQLITE_OK ){
39978 rc = balance(pCur, 0);
39979 }
39980 }
39981 if( rc==SQLITE_OK ){
39982 moveToRoot(pCur);
39983 }
39984 return rc;
@@ -39204,37 +40140,36 @@
40140 ** the page to the freelist.
40141 */
40142 static int clearDatabasePage(
40143 BtShared *pBt, /* The BTree that contains the table */
40144 Pgno pgno, /* Page number to clear */
 
40145 int freePageFlag, /* Deallocate page if true */
40146 int *pnChange
40147 ){
40148 MemPage *pPage = 0;
40149 int rc;
40150 unsigned char *pCell;
40151 int i;
40152
40153 assert( sqlite3_mutex_held(pBt->mutex) );
40154 if( pgno>pagerPagecount(pBt) ){
40155 return SQLITE_CORRUPT_BKPT;
40156 }
40157
40158 rc = getAndInitPage(pBt, pgno, &pPage);
40159 if( rc ) goto cleardatabasepage_out;
40160 for(i=0; i<pPage->nCell; i++){
40161 pCell = findCell(pPage, i);
40162 if( !pPage->leaf ){
40163 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
40164 if( rc ) goto cleardatabasepage_out;
40165 }
40166 rc = clearCell(pPage, pCell);
40167 if( rc ) goto cleardatabasepage_out;
40168 }
40169 if( !pPage->leaf ){
40170 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
40171 if( rc ) goto cleardatabasepage_out;
40172 }else if( pnChange ){
40173 assert( pPage->intKey );
40174 *pnChange += pPage->nCell;
40175 }
@@ -39272,11 +40207,11 @@
40207 }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){
40208 /* nothing to do */
40209 }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
40210 /* nothing to do */
40211 }else{
40212 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
40213 }
40214 sqlite3BtreeLeave(p);
40215 return rc;
40216 }
40217
@@ -39298,11 +40233,11 @@
40233 ** page number that used to be the last root page in the file before
40234 ** the move. If no page gets moved, *piMoved is set to 0.
40235 ** The last root page is recorded in meta[3] and the value of
40236 ** meta[3] is updated by this procedure.
40237 */
40238 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
40239 int rc;
40240 MemPage *pPage = 0;
40241 BtShared *pBt = p->pBt;
40242
40243 assert( sqlite3BtreeHoldsMutex(p) );
@@ -39581,13 +40516,13 @@
40516 ** Return 1 if there are 2 ore more references to the page and 0 if
40517 ** if this is the first reference to the page.
40518 **
40519 ** Also check that the page number is in bounds.
40520 */
40521 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
40522 if( iPage==0 ) return 1;
40523 if( iPage>pCheck->nPage ){
40524 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
40525 return 1;
40526 }
40527 if( pCheck->anRef[iPage]==1 ){
40528 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
@@ -39718,11 +40653,10 @@
40653 ** the root of the tree.
40654 */
40655 static int checkTreePage(
40656 IntegrityCk *pCheck, /* Context for the sanity check */
40657 int iPage, /* Page number of the page to check */
 
40658 char *zParentContext /* Parent context */
40659 ){
40660 MemPage *pPage;
40661 int i, rc, depth, d2, pgno, cnt;
40662 int hdr, cellStart;
@@ -39729,11 +40663,11 @@
40663 int nCell;
40664 u8 *data;
40665 BtShared *pBt;
40666 int usableSize;
40667 char zContext[100];
40668 char *hit = 0;
40669
40670 sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
40671
40672 /* Check that the page exists
40673 */
@@ -39756,11 +40690,11 @@
40690 /* Check out all the cells.
40691 */
40692 depth = 0;
40693 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
40694 u8 *pCell;
40695 u32 sz;
40696 CellInfo info;
40697
40698 /* Check payload overflow pages
40699 */
40700 sqlite3_snprintf(sizeof(zContext), zContext,
@@ -39788,11 +40722,11 @@
40722 #ifndef SQLITE_OMIT_AUTOVACUUM
40723 if( pBt->autoVacuum ){
40724 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
40725 }
40726 #endif
40727 d2 = checkTreePage(pCheck, pgno, zContext);
40728 if( i>0 && d2!=depth ){
40729 checkAppendMsg(pCheck, zContext, "Child page depth differs");
40730 }
40731 depth = d2;
40732 }
@@ -39804,11 +40738,11 @@
40738 #ifndef SQLITE_OMIT_AUTOVACUUM
40739 if( pBt->autoVacuum ){
40740 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
40741 }
40742 #endif
40743 checkTreePage(pCheck, pgno, zContext);
40744 }
40745
40746 /* Check for complete coverage of the page
40747 */
40748 data = pPage->aData;
@@ -39815,12 +40749,18 @@
40749 hdr = pPage->hdrOffset;
40750 hit = sqlite3PageMalloc( pBt->pageSize );
40751 if( hit==0 ){
40752 pCheck->mallocFailed = 1;
40753 }else{
40754 u16 contentOffset = get2byte(&data[hdr+5]);
40755 if (contentOffset > usableSize) {
40756 checkAppendMsg(pCheck, 0,
40757 "Corruption detected in header on page %d",iPage,0);
40758 goto check_page_abort;
40759 }
40760 memset(hit+contentOffset, 0, usableSize-contentOffset);
40761 memset(hit, 1, contentOffset);
40762 nCell = get2byte(&data[hdr+3]);
40763 cellStart = hdr + 12 - 4*pPage->leaf;
40764 for(i=0; i<nCell; i++){
40765 int pc = get2byte(&data[cellStart+i*2]);
40766 u16 size = 1024;
@@ -39860,11 +40800,12 @@
40800 checkAppendMsg(pCheck, 0,
40801 "Fragmented space is %d byte reported as %d on page %d",
40802 cnt, data[hdr+7], iPage);
40803 }
40804 }
40805 check_page_abort:
40806 if (hit) sqlite3PageFree(hit);
40807
40808 releasePage(pPage);
40809 return depth+1;
40810 }
40811 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
@@ -39885,11 +40826,11 @@
40826 int *aRoot, /* An array of root pages numbers for individual trees */
40827 int nRoot, /* Number of entries in aRoot[] */
40828 int mxErr, /* Stop reporting errors after this many */
40829 int *pnErr /* Write number of errors seen to this variable */
40830 ){
40831 Pgno i;
40832 int nRef;
40833 IntegrityCk sCheck;
40834 BtShared *pBt = p->pBt;
40835 char zErr[100];
40836
@@ -39901,11 +40842,11 @@
40842 sqlite3BtreeLeave(p);
40843 return sqlite3DbStrDup(0, "cannot acquire a read lock on the database");
40844 }
40845 sCheck.pBt = pBt;
40846 sCheck.pPager = pBt->pPager;
40847 sCheck.nPage = pagerPagecount(sCheck.pBt);
40848 sCheck.mxErr = mxErr;
40849 sCheck.nErr = 0;
40850 sCheck.mallocFailed = 0;
40851 *pnErr = 0;
40852 #ifndef SQLITE_OMIT_AUTOVACUUM
@@ -39937,18 +40878,18 @@
40878 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
40879 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
40880
40881 /* Check all the tables.
40882 */
40883 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
40884 if( aRoot[i]==0 ) continue;
40885 #ifndef SQLITE_OMIT_AUTOVACUUM
40886 if( pBt->autoVacuum && aRoot[i]>1 ){
40887 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
40888 }
40889 #endif
40890 checkTreePage(&sCheck, aRoot[i], "List of tree roots: ");
40891 }
40892
40893 /* Make sure every page in the file is referenced
40894 */
40895 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
@@ -40068,12 +41009,12 @@
41009 }
41010 if( pBtTo->pCursor ){
41011 return SQLITE_BUSY;
41012 }
41013
41014 nToPage = pagerPagecount(pBtTo);
41015 nFromPage = pagerPagecount(pBtFrom);
41016 iSkip = PENDING_BYTE_PAGE(pBtTo);
41017
41018 /* Variable nNewPage is the number of pages required to store the
41019 ** contents of pFrom using the current page-size of pTo.
41020 */
@@ -40417,23 +41358,23 @@
41358 **
41359 *************************************************************************
41360 ** This file implements a FIFO queue of rowids used for processing
41361 ** UPDATE and DELETE statements.
41362 **
41363 ** $Id: vdbefifo.c,v 1.9 2008/11/17 19:18:55 danielk1977 Exp $
41364 */
41365
41366 /*
41367 ** Constants FIFOSIZE_FIRST and FIFOSIZE_MAX are the initial
41368 ** number of entries in a fifo page and the maximum number of
41369 ** entries in a fifo page.
41370 */
41371 #define FIFOSIZE_FIRST (((128-sizeof(FifoPage))/8)+1)
41372 #ifdef SQLITE_MALLOC_SOFT_LIMIT
41373 # define FIFOSIZE_MAX (int)(((SQLITE_MALLOC_SOFT_LIMIT-sizeof(FifoPage))/8)+1)
41374 #else
41375 # define FIFOSIZE_MAX (int)(((262144-sizeof(FifoPage))/8)+1)
41376 #endif
41377
41378 /*
41379 ** Allocate a new FifoPage and return a pointer to it. Return NULL if
41380 ** we run out of memory. Leave space on the page for nEntry entries.
@@ -40551,11 +41492,11 @@
41492 ** This file contains code use to manipulate "Mem" structure. A "Mem"
41493 ** stores a single value in the VDBE. Mem is an opaque structure visible
41494 ** only within the VDBE. Interface routines refer to a Mem using the
41495 ** name sqlite_value
41496 **
41497 ** $Id: vdbemem.c,v 1.126 2008/11/11 00:21:30 drh Exp $
41498 */
41499
41500 /*
41501 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
41502 ** P if required.
@@ -40619,13 +41560,10 @@
41560
41561 if( n<32 ) n = 32;
41562 if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
41563 if( preserve && pMem->z==pMem->zMalloc ){
41564 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 
 
 
41565 preserve = 0;
41566 }else{
41567 sqlite3DbFree(pMem->db, pMem->zMalloc);
41568 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
41569 }
@@ -40637,11 +41575,15 @@
41575 if( pMem->flags&MEM_Dyn && pMem->xDel ){
41576 pMem->xDel((void *)(pMem->z));
41577 }
41578
41579 pMem->z = pMem->zMalloc;
41580 if( pMem->z==0 ){
41581 pMem->flags = MEM_Null;
41582 }else{
41583 pMem->flags &= ~(MEM_Ephem|MEM_Static);
41584 }
41585 pMem->xDel = 0;
41586 return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
41587 }
41588
41589 /*
@@ -41596,11 +42538,11 @@
42538 ** This file contains code used for creating, destroying, and populating
42539 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
42540 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
42541 ** But that file was getting too big so this subroutines were split out.
42542 **
42543 ** $Id: vdbeaux.c,v 1.420 2008/11/17 19:18:55 danielk1977 Exp $
42544 */
42545
42546
42547
42548 /*
@@ -41679,25 +42621,27 @@
42621 p->trace = trace;
42622 }
42623 #endif
42624
42625 /*
42626 ** Resize the Vdbe.aOp array so that it is at least one op larger than
42627 ** it was.
42628 **
42629 ** If an out-of-memory error occurs while resizing the array, return
42630 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
42631 ** unchanged (this is so that any opcodes already allocated can be
42632 ** correctly deallocated along with the rest of the Vdbe).
42633 */
42634 static int growOpArray(Vdbe *p){
42635 VdbeOp *pNew;
42636 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
42637 pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
42638 if( pNew ){
42639 p->nOpAlloc = nNew;
42640 p->aOp = pNew;
42641 }
42642 return (pNew ? SQLITE_OK : SQLITE_NOMEM);
42643 }
42644
42645 /*
42646 ** Add a new instruction to the list of instructions current in the
42647 ** VDBE. Return the address of the new instruction.
@@ -41719,12 +42663,11 @@
42663 VdbeOp *pOp;
42664
42665 i = p->nOp;
42666 assert( p->magic==VDBE_MAGIC_INIT );
42667 if( p->nOpAlloc<=i ){
42668 if( growOpArray(p) ){
 
42669 return 0;
42670 }
42671 }
42672 p->nOp++;
42673 pOp = &p->aOp[i];
@@ -41921,15 +42864,11 @@
42864 ** address of the first operation added.
42865 */
42866 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
42867 int addr;
42868 assert( p->magic==VDBE_MAGIC_INIT );
42869 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
 
 
 
 
42870 return 0;
42871 }
42872 addr = p->nOp;
42873 if( nOp>0 ){
42874 int i;
@@ -42310,11 +43249,11 @@
43249 **
43250 */
43251 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
43252 int mask;
43253 assert( i>=0 && i<p->db->nDb );
43254 assert( i<(int)sizeof(p->btreeMask)*8 );
43255 mask = 1<<i;
43256 if( (p->btreeMask & mask)==0 ){
43257 p->btreeMask |= mask;
43258 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
43259 }
@@ -42596,15 +43535,11 @@
43535
43536 /* There should be at least one opcode.
43537 */
43538 assert( p->nOp>0 );
43539
43540 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
 
 
 
 
43541 p->magic = VDBE_MAGIC_RUN;
43542
43543 /* For each cursor required, also allocate a memory cell. Memory
43544 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
43545 ** the vdbe program. Instead they are used to allocate space for
@@ -42620,11 +43555,10 @@
43555 ** Allocation space for registers.
43556 */
43557 if( p->aMem==0 ){
43558 int nArg; /* Maximum number of args passed to a user function. */
43559 resolveP2Values(p, &nArg);
 
43560 assert( nVar>=0 );
43561 if( isExplain && nMem<10 ){
43562 nMem = 10;
43563 }
43564 p->aMem = sqlite3DbMallocZero(db,
@@ -43164,11 +44098,14 @@
44098 ** we do either a commit or rollback of the current transaction.
44099 **
44100 ** Note: This block also runs if one of the special errors handled
44101 ** above has occurred.
44102 */
44103 if( !sqlite3VtabInSync(db)
44104 && db->autoCommit
44105 && db->writeVdbeCnt==(p->readOnly==0)
44106 ){
44107 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
44108 /* The auto-commit flag is true, and the vdbe program was
44109 ** successful or hit an 'OR FAIL' constraint. This means a commit
44110 ** is required.
44111 */
@@ -43791,12 +44728,12 @@
44728 UnpackedRecord *pSpace,/* Space available to hold resulting object */
44729 int szSpace /* Size of pSpace[] in bytes */
44730 ){
44731 const unsigned char *aKey = (const unsigned char *)pKey;
44732 UnpackedRecord *p;
44733 int nByte, d;
44734 u32 idx;
44735 u16 u; /* Unsigned loop counter */
44736 u32 szHdr;
44737 Mem *pMem;
44738
44739 assert( sizeof(Mem)>sizeof(*p) );
@@ -43816,11 +44753,11 @@
44753 d = szHdr;
44754 u = 0;
44755 while( idx<szHdr && u<p->nField ){
44756 u32 serial_type;
44757
44758 idx += getVarint32(&aKey[idx], serial_type);
44759 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break;
44760 pMem->enc = pKeyInfo->enc;
44761 pMem->db = pKeyInfo->db;
44762 pMem->flags = 0;
44763 pMem->zMalloc = 0;
@@ -43881,11 +44818,11 @@
44818 */
44819 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
44820 int nKey1, const void *pKey1, /* Left key */
44821 UnpackedRecord *pPKey2 /* Right key */
44822 ){
44823 int d1; /* Offset into aKey[] of next data element */
44824 u32 idx1; /* Offset into aKey[] of next header element */
44825 u32 szHdr1; /* Number of bytes in header */
44826 int i = 0;
44827 int nField;
44828 int rc = 0;
@@ -44084,11 +45021,11 @@
45021 *************************************************************************
45022 **
45023 ** This file contains code use to implement APIs that are part of the
45024 ** VDBE.
45025 **
45026 ** $Id: vdbeapi.c,v 1.149 2008/11/19 09:05:27 danielk1977 Exp $
45027 */
45028
45029 #if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
45030 /*
45031 ** The following structure contains pointers to the end points of a
@@ -44670,15 +45607,16 @@
45607 ** for name resolution but are actually overloaded by the xFindFunction
45608 ** method of virtual tables.
45609 */
45610 SQLITE_PRIVATE void sqlite3InvalidFunction(
45611 sqlite3_context *context, /* The function calling context */
45612 int NotUsed, /* Number of arguments to the function */
45613 sqlite3_value **NotUsed2 /* Value of each argument */
45614 ){
45615 const char *zName = context->pFunc->zName;
45616 char *zErr;
45617 UNUSED_PARAMETER2(NotUsed, NotUsed2);
45618 zErr = sqlite3MPrintf(0,
45619 "unable to use function %s in the requested context", zName);
45620 sqlite3_result_error(context, zErr, -1);
45621 sqlite3_free(zErr);
45622 }
@@ -45423,11 +46361,11 @@
46361 ** documentation, headers files, or other derived files. The formatting
46362 ** of the code in this file is, therefore, important. See other comments
46363 ** in this file for details. If in doubt, do not deviate from existing
46364 ** commenting and indentation practices when changing or adding code.
46365 **
46366 ** $Id: vdbe.c,v 1.788 2008/11/17 15:31:48 danielk1977 Exp $
46367 */
46368
46369 /*
46370 ** The following global variable is incremented every time a cursor
46371 ** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test
@@ -45554,11 +46492,11 @@
46492 /*
46493 ** Return true if an opcode has any of the OPFLG_xxx properties
46494 ** specified by mask.
46495 */
46496 SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
46497 assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) );
46498 return (opcodeProperty[opcode]&mask)!=0;
46499 }
46500
46501 /*
46502 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
@@ -46019,11 +46957,10 @@
46957 #endif
46958 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
46959 int nProgressOps = 0; /* Opcodes executed since progress callback. */
46960 #endif
46961 UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */
 
46962
46963 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
46964 assert( db->magic==SQLITE_MAGIC_BUSY );
46965 sqlite3BtreeMutexArrayEnter(&p->aMutex);
46966 if( p->rc==SQLITE_NOMEM ){
@@ -47432,19 +48369,19 @@
48369 ** If the column contains fewer than P2 fields, then extract a NULL. Or,
48370 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
48371 ** the result.
48372 */
48373 case OP_Column: {
48374 int payloadSize; /* Number of bytes in the record */
48375 int p1 = pOp->p1; /* P1 value of the opcode */
48376 int p2 = pOp->p2; /* column number to retrieve */
48377 VdbeCursor *pC = 0;/* The VDBE cursor */
48378 char *zRec; /* Pointer to complete record-data */
48379 BtCursor *pCrsr; /* The BTree cursor */
48380 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
48381 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
48382 int nField; /* number of fields in the record */
48383 int len; /* The length of the serialized data for the column */
48384 int i; /* Loop counter */
48385 char *zData; /* Part of the record being decoded */
48386 Mem *pDest; /* Where to write the extracted value */
48387 Mem sMem; /* For storing the record being decoded */
@@ -47488,11 +48425,11 @@
48425 }else if( pC->isIndex ){
48426 i64 payloadSize64;
48427 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
48428 payloadSize = payloadSize64;
48429 }else{
48430 sqlite3BtreeDataSize(pCrsr, (u32 *)&payloadSize);
48431 }
48432 nField = pC->nField;
48433 }else{
48434 assert( pC->pseudoTable );
48435 /* The record is the sole entry of a pseudo-table */
@@ -47522,11 +48459,11 @@
48459 if( pC->cacheStatus==p->cacheCtr ){
48460 aOffset = pC->aOffset;
48461 }else{
48462 u8 *zIdx; /* Index into header */
48463 u8 *zEndHdr; /* Pointer to first byte after the header */
48464 int offset; /* Offset into the data */
48465 int szHdrSz; /* Size of the header size field at start of record */
48466 int avail; /* Number of bytes of available data */
48467
48468 assert(aType);
48469 pC->aOffset = aOffset = &aType[nField];
@@ -47723,11 +48660,11 @@
48660 */
48661 u8 *zNewRecord; /* A buffer to hold the data for the new record */
48662 Mem *pRec; /* The new record */
48663 u64 nData = 0; /* Number of bytes of data space */
48664 int nHdr = 0; /* Number of bytes of header space */
48665 i64 nByte = 0; /* Data space required for this record */
48666 int nZero = 0; /* Number of zero bytes at the end of the record */
48667 int nVarint; /* Number of bytes in a varint */
48668 u32 serial_type; /* Type field */
48669 Mem *pData0; /* First field to be combined into the record */
48670 Mem *pLast; /* Last field of the record */
@@ -48166,11 +49103,14 @@
49103 assert( p2>0 );
49104 assert( p2<=p->nMem );
49105 pIn2 = &p->aMem[p2];
49106 sqlite3VdbeMemIntegerify(pIn2);
49107 p2 = pIn2->u.i;
49108 if( p2<2 ) {
49109 rc = SQLITE_CORRUPT_BKPT;
49110 goto abort_due_to_error;
49111 }
49112 }
49113 assert( i>=0 );
49114 pCur = allocateCursor(p, i, &pOp[-1], iDb, 1);
49115 if( pCur==0 ) goto no_mem;
49116 pCur->nullRow = 1;
@@ -48793,11 +49733,11 @@
49733 #else
49734 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
49735 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
49736 ** to provide the constant while making all compilers happy.
49737 */
49738 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
49739 #endif
49740
49741 if( !pC->useRandomRowid ){
49742 if( pC->nextRowidValid ){
49743 v = pC->nextRowid;
@@ -49099,11 +50039,11 @@
50039 goto too_big;
50040 }
50041 n = n64;
50042 }else{
50043 sqlite3BtreeDataSize(pCrsr, &n);
50044 if( (int)n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
50045 goto too_big;
50046 }
50047 }
50048 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
50049 goto no_mem;
@@ -51213,11 +52153,11 @@
52153 **
52154 ** This file contains code use to implement an in-memory rollback journal.
52155 ** The in-memory rollback journal is used to journal transactions for
52156 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
52157 **
52158 ** @(#) $Id: memjournal.c,v 1.5 2008/11/19 16:52:44 danielk1977 Exp $
52159 */
52160
52161 /* Forward references to internal structures */
52162 typedef struct MemJournal MemJournal;
52163 typedef struct FilePoint FilePoint;
@@ -51228,11 +52168,13 @@
52168 */
52169 #define JOURNAL_CHUNKSIZE 1024
52170
52171 /* Macro to find the minimum of two numeric values.
52172 */
52173 #ifndef MIN
52174 # define MIN(x,y) ((x)<(y)?(x):(y))
52175 #endif
52176
52177 /*
52178 ** The rollback journal is composed of a linked list of these structures.
52179 */
52180 struct FileChunk {
@@ -51319,10 +52261,11 @@
52261
52262 /* An in-memory journal file should only ever be appended to. Random
52263 ** access writes are not required by sqlite.
52264 */
52265 assert(iOfst==p->endpoint.iOffset);
52266 UNUSED_PARAMETER(iOfst);
52267
52268 while( nWrite>0 ){
52269 FileChunk *pChunk = p->endpoint.pChunk;
52270 int iChunkOffset = p->endpoint.iOffset%JOURNAL_CHUNKSIZE;
52271 int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
@@ -51358,10 +52301,11 @@
52301 */
52302 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
52303 MemJournal *p = (MemJournal *)pJfd;
52304 FileChunk *pChunk;
52305 assert(size==0);
52306 UNUSED_PARAMETER(size);
52307 pChunk = p->pFirst;
52308 while( pChunk ){
52309 FileChunk *pTmp = pChunk;
52310 pChunk = pChunk->pNext;
52311 sqlite3_free(pTmp);
@@ -51380,11 +52324,12 @@
52324
52325
52326 /*
52327 ** Sync the file.
52328 */
52329 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
52330 UNUSED_PARAMETER2(NotUsed, NotUsed2);
52331 return SQLITE_OK;
52332 }
52333
52334 /*
52335 ** Query the size of the file in bytes.
@@ -51589,11 +52534,11 @@
52534 **
52535 ** This file contains routines used for walking the parser tree and
52536 ** resolve all identifiers by associating them with a particular
52537 ** table and column.
52538 **
52539 ** $Id: resolve.c,v 1.11 2008/11/17 19:18:55 danielk1977 Exp $
52540 */
52541
52542 /*
52543 ** Turn the pExpr expression into an alias for the iCol-th column of the
52544 ** result set in pEList.
@@ -51921,13 +52866,13 @@
52866 ** column number is greater than the number of bits in the bitmask
52867 ** then set the high-order bit of the bitmask.
52868 */
52869 if( pExpr->iColumn>=0 && pMatch!=0 ){
52870 int n = pExpr->iColumn;
52871 testcase( n==BMS-1 );
52872 if( n>=BMS ){
52873 n = BMS-1;
52874 }
52875 assert( pMatch->iCursor==pExpr->iTable );
52876 pMatch->colUsed |= ((Bitmask)1)<<n;
52877 }
52878
@@ -52756,11 +53701,11 @@
53701 **
53702 *************************************************************************
53703 ** This file contains routines used for analyzing expressions and
53704 ** for generating VDBE code that evaluates expressions in SQLite.
53705 **
53706 ** $Id: expr.c,v 1.404 2008/11/19 16:52:44 danielk1977 Exp $
53707 */
53708
53709 /*
53710 ** Return the 'affinity' of the expression pExpr if any.
53711 **
@@ -52785,11 +53730,13 @@
53730 #ifndef SQLITE_OMIT_CAST
53731 if( op==TK_CAST ){
53732 return sqlite3AffinityType(&pExpr->token);
53733 }
53734 #endif
53735 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
53736 && pExpr->pTab!=0
53737 ){
53738 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
53739 ** a TK_COLUMN but was previously evaluated and cached in a register */
53740 int j = pExpr->iColumn;
53741 if( j<0 ) return SQLITE_AFF_INTEGER;
53742 assert( pExpr->pTab && j<pExpr->pTab->nCol );
@@ -52831,11 +53778,11 @@
53778 while( p ){
53779 int op;
53780 pColl = p->pColl;
53781 if( pColl ) break;
53782 op = p->op;
53783 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){
53784 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
53785 ** a TK_COLUMN but was previously evaluated and cached in a register */
53786 const char *zColl;
53787 int j = p->iColumn;
53788 if( j>=0 ){
@@ -53679,11 +54626,12 @@
54626 return WRC_Abort;
54627 default:
54628 return WRC_Continue;
54629 }
54630 }
54631 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
54632 UNUSED_PARAMETER(NotUsed);
54633 pWalker->u.i = 0;
54634 return WRC_Abort;
54635 }
54636 static int exprIsConst(Expr *p, int initFlag){
54637 Walker w;
@@ -54199,14 +55147,15 @@
55147 ** z[n] character is guaranteed to be something that does not look
55148 ** like the continuation of the number.
55149 */
55150 static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
55151 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
55152 assert( !z || !isdigit(z[n]) );
55153 UNUSED_PARAMETER(n);
55154 if( z ){
55155 double value;
55156 char *zV;
 
55157 sqlite3AtoF(z, &value);
55158 if( sqlite3IsNaN(value) ){
55159 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
55160 }else{
55161 if( negateFlag ) value = -value;
@@ -55854,11 +56803,11 @@
56803 **
56804 *************************************************************************
56805 ** This file contains C code routines that used to generate VDBE code
56806 ** that implements the ALTER TABLE command.
56807 **
56808 ** $Id: alter.c,v 1.50 2008/11/19 09:05:27 danielk1977 Exp $
56809 */
56810
56811 /*
56812 ** The code in this file only exists if we are not omitting the
56813 ** ALTER TABLE logic from the build.
@@ -55879,11 +56828,11 @@
56828 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
56829 ** -> 'CREATE INDEX i ON def(a, b, c)'
56830 */
56831 static void renameTableFunc(
56832 sqlite3_context *context,
56833 int NotUsed,
56834 sqlite3_value **argv
56835 ){
56836 unsigned char const *zSql = sqlite3_value_text(argv[0]);
56837 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
56838
@@ -55892,10 +56841,12 @@
56841 unsigned char const *zCsr = zSql;
56842 int len = 0;
56843 char *zRet;
56844
56845 sqlite3 *db = sqlite3_context_db_handle(context);
56846
56847 UNUSED_PARAMETER(NotUsed);
56848
56849 /* The principle used to locate the table name in the CREATE TABLE
56850 ** statement is that the table name is the first non-space token that
56851 ** is immediately followed by a TK_LP or TK_USING token.
56852 */
@@ -55934,11 +56885,11 @@
56885 ** returned. This is analagous to renameTableFunc() above, except for CREATE
56886 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
56887 */
56888 static void renameTriggerFunc(
56889 sqlite3_context *context,
56890 int NotUsed,
56891 sqlite3_value **argv
56892 ){
56893 unsigned char const *zSql = sqlite3_value_text(argv[0]);
56894 unsigned char const *zTableName = sqlite3_value_text(argv[1]);
56895
@@ -55946,12 +56897,13 @@
56897 Token tname;
56898 int dist = 3;
56899 unsigned char const *zCsr = zSql;
56900 int len = 0;
56901 char *zRet;
 
56902 sqlite3 *db = sqlite3_context_db_handle(context);
56903
56904 UNUSED_PARAMETER(NotUsed);
56905
56906 /* The principle used to locate the table name in the CREATE TRIGGER
56907 ** statement is that the table name is the first token that is immediatedly
56908 ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
56909 ** of TK_WHEN, TK_BEGIN or TK_FOR.
@@ -56475,11 +57427,11 @@
57427 ** May you share freely, never taking more than you give.
57428 **
57429 *************************************************************************
57430 ** This file contains code associated with the ANALYZE command.
57431 **
57432 ** @(#) $Id: analyze.c,v 1.46 2008/11/19 16:52:44 danielk1977 Exp $
57433 */
57434 #ifndef SQLITE_OMIT_ANALYZE
57435
57436 /*
57437 ** This routine generates code that opens the sqlite_stat1 table on cursor
@@ -56819,18 +57771,20 @@
57771 ** sqlite_stat1 table.
57772 **
57773 ** argv[0] = name of the index
57774 ** argv[1] = results of analysis - on integer for each column
57775 */
57776 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
57777 analysisInfo *pInfo = (analysisInfo*)pData;
57778 Index *pIndex;
57779 int i, c;
57780 unsigned int v;
57781 const char *z;
57782
57783 assert( argc==2 );
57784 UNUSED_PARAMETER2(NotUsed, argc);
57785
57786 if( argv==0 || argv[0]==0 || argv[1]==0 ){
57787 return 0;
57788 }
57789 pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
57790 if( pIndex==0 ){
@@ -56902,11 +57856,11 @@
57856 ** May you share freely, never taking more than you give.
57857 **
57858 *************************************************************************
57859 ** This file contains code used to implement the ATTACH and DETACH commands.
57860 **
57861 ** $Id: attach.c,v 1.80 2008/11/19 09:05:27 danielk1977 Exp $
57862 */
57863
57864 #ifndef SQLITE_OMIT_ATTACH
57865 /*
57866 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
@@ -56954,11 +57908,11 @@
57908 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
57909 ** third argument.
57910 */
57911 static void attachFunc(
57912 sqlite3_context *context,
57913 int NotUsed,
57914 sqlite3_value **argv
57915 ){
57916 int i;
57917 int rc = 0;
57918 sqlite3 *db = sqlite3_context_db_handle(context);
@@ -56965,10 +57919,12 @@
57919 const char *zName;
57920 const char *zFile;
57921 Db *aNew;
57922 char *zErrDyn = 0;
57923 char zErr[128];
57924
57925 UNUSED_PARAMETER(NotUsed);
57926
57927 zFile = (const char *)sqlite3_value_text(argv[0]);
57928 zName = (const char *)sqlite3_value_text(argv[1]);
57929 if( zFile==0 ) zFile = "";
57930 if( zName==0 ) zName = "";
@@ -57122,18 +58078,20 @@
58078 **
58079 ** SELECT sqlite_detach(x)
58080 */
58081 static void detachFunc(
58082 sqlite3_context *context,
58083 int NotUsed,
58084 sqlite3_value **argv
58085 ){
58086 const char *zName = (const char *)sqlite3_value_text(argv[0]);
58087 sqlite3 *db = sqlite3_context_db_handle(context);
58088 int i;
58089 Db *pDb = 0;
58090 char zErr[128];
58091
58092 UNUSED_PARAMETER(NotUsed);
58093
58094 if( zName==0 ) zName = "";
58095 for(i=0; i<db->nDb; i++){
58096 pDb = &db->aDb[i];
58097 if( pDb->pBt==0 ) continue;
@@ -57685,11 +58643,11 @@
58643 ** creating ID lists
58644 ** BEGIN TRANSACTION
58645 ** COMMIT
58646 ** ROLLBACK
58647 **
58648 ** $Id: build.c,v 1.503 2008/11/17 19:18:55 danielk1977 Exp $
58649 */
58650
58651 /*
58652 ** This routine is called when a new SQL statement is beginning to
58653 ** be parsed. Initialize the pParse structure as needed.
@@ -58287,11 +59245,11 @@
59245 ** index of the named database in db->aDb[], or -1 if the named db
59246 ** does not exist.
59247 */
59248 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
59249 int i = -1; /* Database number */
59250 size_t n; /* Number of characters in the name */
59251 Db *pDb; /* A database whose name space is being searched */
59252 char *zName; /* Name we are searching for */
59253
59254 zName = sqlite3NameFromToken(db, pName);
59255 if( zName ){
@@ -58331,11 +59289,15 @@
59289 ){
59290 int iDb; /* Database holding the object */
59291 sqlite3 *db = pParse->db;
59292
59293 if( pName2 && pName2->n>0 ){
59294 if( db->init.busy ) {
59295 sqlite3ErrorMsg(pParse, "corrupt database");
59296 pParse->nErr++;
59297 return -1;
59298 }
59299 *pUnqual = pName2;
59300 iDb = sqlite3FindDb(db, pName1);
59301 if( iDb<0 ){
59302 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
59303 pParse->nErr++;
@@ -59053,11 +60015,11 @@
60015 k += strlen(&zStmt[k]);
60016 zSep = zSep2;
60017 identPut(zStmt, &k, pCol->zName);
60018 if( (z = pCol->zType)!=0 ){
60019 zStmt[k++] = ' ';
60020 assert( (int)(strlen(z)+k+1)<=n );
60021 sqlite3_snprintf(n-k, &zStmt[k], "%s", z);
60022 k += strlen(z);
60023 }
60024 }
60025 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
@@ -60651,16 +61613,86 @@
61613 for(i=0; i<pList->nId; i++){
61614 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
61615 }
61616 return -1;
61617 }
61618
61619 /*
61620 ** Expand the space allocated for the given SrcList object by
61621 ** creating nExtra new slots beginning at iStart. iStart is zero based.
61622 ** New slots are zeroed.
61623 **
61624 ** For example, suppose a SrcList initially contains two entries: A,B.
61625 ** To append 3 new entries onto the end, do this:
61626 **
61627 ** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
61628 **
61629 ** After the call above it would contain: A, B, nil, nil, nil.
61630 ** If the iStart argument had been 1 instead of 2, then the result
61631 ** would have been: A, nil, nil, nil, B. To prepend the new slots,
61632 ** the iStart value would be 0. The result then would
61633 ** be: nil, nil, nil, A, B.
61634 **
61635 ** If a memory allocation fails the SrcList is unchanged. The
61636 ** db->mallocFailed flag will be set to true.
61637 */
61638 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
61639 sqlite3 *db, /* Database connection to notify of OOM errors */
61640 SrcList *pSrc, /* The SrcList to be enlarged */
61641 int nExtra, /* Number of new slots to add to pSrc->a[] */
61642 int iStart /* Index in pSrc->a[] of first new slot */
61643 ){
61644 int i;
61645
61646 /* Sanity checking on calling parameters */
61647 assert( iStart>=0 );
61648 assert( nExtra>=1 );
61649 if( pSrc==0 || iStart>pSrc->nSrc ){
61650 assert( db->mallocFailed );
61651 return pSrc;
61652 }
61653
61654 /* Allocate additional space if needed */
61655 if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
61656 SrcList *pNew;
61657 int nAlloc = pSrc->nSrc+nExtra;
61658 pNew = sqlite3DbRealloc(db, pSrc,
61659 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
61660 if( pNew==0 ){
61661 assert( db->mallocFailed );
61662 return pSrc;
61663 }
61664 pSrc = pNew;
61665 pSrc->nAlloc = nAlloc;
61666 }
61667
61668 /* Move existing slots that come after the newly inserted slots
61669 ** out of the way */
61670 for(i=pSrc->nSrc-1; i>=iStart; i--){
61671 pSrc->a[i+nExtra] = pSrc->a[i];
61672 }
61673 pSrc->nSrc += nExtra;
61674
61675 /* Zero the newly allocated slots */
61676 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
61677 for(i=iStart; i<iStart+nExtra; i++){
61678 pSrc->a[i].iCursor = -1;
61679 }
61680
61681 /* Return a pointer to the enlarged SrcList */
61682 return pSrc;
61683 }
61684
61685
61686 /*
61687 ** Append a new table name to the given SrcList. Create a new SrcList if
61688 ** need be. A new entry is created in the SrcList even if pToken is NULL.
61689 **
61690 ** A SrcList is returned, or NULL if there is an OOM error. The returned
61691 ** SrcList might be the same as the SrcList that was input or it might be
61692 ** a new one. If an OOM error does occurs, then the prior value of pList
61693 ** that is input to this routine is automatically freed.
61694 **
61695 ** If pDatabase is not null, it means that the table has an optional
61696 ** database name prefix. Like this: "database.table". The pDatabase
61697 ** points to the table name and the pTable points to the database name.
61698 ** The SrcList.a[].zName field is filled with the table name which might
@@ -60689,23 +61721,16 @@
61721 if( pList==0 ){
61722 pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
61723 if( pList==0 ) return 0;
61724 pList->nAlloc = 1;
61725 }
61726 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
61727 if( db->mallocFailed ){
61728 sqlite3SrcListDelete(db, pList);
61729 return 0;
61730 }
61731 pItem = &pList->a[pList->nSrc-1];
 
 
 
 
 
 
 
61732 if( pDatabase && pDatabase->z==0 ){
61733 pDatabase = 0;
61734 }
61735 if( pDatabase && pTable ){
61736 Token *pTemp = pDatabase;
@@ -60712,12 +61737,10 @@
61737 pDatabase = pTable;
61738 pTable = pTemp;
61739 }
61740 pItem->zName = sqlite3NameFromToken(db, pTable);
61741 pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
 
 
61742 return pList;
61743 }
61744
61745 /*
61746 ** Assign VdbeCursor index numbers to all tables in a SrcList
@@ -61644,11 +62667,11 @@
62667 **
62668 *************************************************************************
62669 ** This file contains C code routines that are called by the parser
62670 ** in order to generate code for DELETE FROM statements.
62671 **
62672 ** $Id: delete.c,v 1.187 2008/11/19 09:05:27 danielk1977 Exp $
62673 */
62674
62675 /*
62676 ** Look up every table that is named in pSrc. If any table is not found,
62677 ** add an error message to pParse->zErrMsg and return NULL. If all tables
@@ -61896,11 +62919,11 @@
62919
62920 /* Figure out if we have any triggers and if the table being
62921 ** deleted from is a view
62922 */
62923 #ifndef SQLITE_OMIT_TRIGGER
62924 triggers_exist = sqlite3TriggersExist(pTab, TK_DELETE, 0);
62925 isView = pTab->pSelect!=0;
62926 #else
62927 # define triggers_exist 0
62928 # define isView 0
62929 #endif
@@ -62285,11 +63308,11 @@
63308 **
63309 ** There is only one exported symbol in this file - the function
63310 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
63311 ** All other code has file scope.
63312 **
63313 ** $Id: func.c,v 1.206 2008/11/19 16:52:44 danielk1977 Exp $
63314 */
63315
63316 /*
63317 ** Return the collating function associated with a function.
63318 */
@@ -62329,14 +63352,15 @@
63352 /*
63353 ** Return the type of the argument.
63354 */
63355 static void typeofFunc(
63356 sqlite3_context *context,
63357 int NotUsed,
63358 sqlite3_value **argv
63359 ){
63360 const char *z = 0;
63361 UNUSED_PARAMETER(NotUsed);
63362 switch( sqlite3_value_type(argv[0]) ){
63363 case SQLITE_NULL: z = "null"; break;
63364 case SQLITE_INTEGER: z = "integer"; break;
63365 case SQLITE_TEXT: z = "text"; break;
63366 case SQLITE_FLOAT: z = "real"; break;
@@ -62355,10 +63379,11 @@
63379 sqlite3_value **argv
63380 ){
63381 int len;
63382
63383 assert( argc==1 );
63384 UNUSED_PARAMETER(argc);
63385 switch( sqlite3_value_type(argv[0]) ){
63386 case SQLITE_BLOB:
63387 case SQLITE_INTEGER:
63388 case SQLITE_FLOAT: {
63389 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
@@ -62385,10 +63410,11 @@
63410 /*
63411 ** Implementation of the abs() function
63412 */
63413 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
63414 assert( argc==1 );
63415 UNUSED_PARAMETER(argc);
63416 switch( sqlite3_value_type(argv[0]) ){
63417 case SQLITE_INTEGER: {
63418 i64 iVal = sqlite3_value_int64(argv[0]);
63419 if( iVal<0 ){
63420 if( (iVal<<1)==0 ){
@@ -62588,14 +63614,15 @@
63614 /*
63615 ** Implementation of random(). Return a random integer.
63616 */
63617 static void randomFunc(
63618 sqlite3_context *context,
63619 int NotUsed,
63620 sqlite3_value **NotUsed2
63621 ){
63622 sqlite_int64 r;
63623 UNUSED_PARAMETER2(NotUsed, NotUsed2);
63624 sqlite3_randomness(sizeof(r), &r);
63625 if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */
63626 /* can always do abs() of the result */
63627 sqlite3_result_int64(context, r);
63628 }
@@ -62610,10 +63637,11 @@
63637 sqlite3_value **argv
63638 ){
63639 int n;
63640 unsigned char *p;
63641 assert( argc==1 );
63642 UNUSED_PARAMETER(argc);
63643 n = sqlite3_value_int(argv[0]);
63644 if( n<1 ){
63645 n = 1;
63646 }
63647 p = contextMalloc(context, n);
@@ -62627,40 +63655,43 @@
63655 ** Implementation of the last_insert_rowid() SQL function. The return
63656 ** value is the same as the sqlite3_last_insert_rowid() API function.
63657 */
63658 static void last_insert_rowid(
63659 sqlite3_context *context,
63660 int NotUsed,
63661 sqlite3_value **NotUsed2
63662 ){
63663 sqlite3 *db = sqlite3_context_db_handle(context);
63664 UNUSED_PARAMETER2(NotUsed, NotUsed2);
63665 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
63666 }
63667
63668 /*
63669 ** Implementation of the changes() SQL function. The return value is the
63670 ** same as the sqlite3_changes() API function.
63671 */
63672 static void changes(
63673 sqlite3_context *context,
63674 int NotUsed,
63675 sqlite3_value **NotUsed2
63676 ){
63677 sqlite3 *db = sqlite3_context_db_handle(context);
63678 UNUSED_PARAMETER2(NotUsed, NotUsed2);
63679 sqlite3_result_int(context, sqlite3_changes(db));
63680 }
63681
63682 /*
63683 ** Implementation of the total_changes() SQL function. The return value is
63684 ** the same as the sqlite3_total_changes() API function.
63685 */
63686 static void total_changes(
63687 sqlite3_context *context,
63688 int NotUsed,
63689 sqlite3_value **NotUsed2
63690 ){
63691 sqlite3 *db = sqlite3_context_db_handle(context);
63692 UNUSED_PARAMETER2(NotUsed, NotUsed2);
63693 sqlite3_result_int(context, sqlite3_total_changes(db));
63694 }
63695
63696 /*
63697 ** A structure defining how to do GLOB-style comparisons.
@@ -62901,14 +63932,15 @@
63932 ** argument if the arguments are different. The result is NULL if the
63933 ** arguments are equal to each other.
63934 */
63935 static void nullifFunc(
63936 sqlite3_context *context,
63937 int NotUsed,
63938 sqlite3_value **argv
63939 ){
63940 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
63941 UNUSED_PARAMETER(NotUsed);
63942 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
63943 sqlite3_result_value(context, argv[0]);
63944 }
63945 }
63946
@@ -62916,13 +63948,14 @@
63948 ** Implementation of the VERSION(*) function. The result is the version
63949 ** of the SQLite library that is running.
63950 */
63951 static void versionFunc(
63952 sqlite3_context *context,
63953 int NotUsed,
63954 sqlite3_value **NotUsed2
63955 ){
63956 UNUSED_PARAMETER2(NotUsed, NotUsed2);
63957 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
63958 }
63959
63960 /* Array for converting from half-bytes (nybbles) into ASCII hex
63961 ** digits. */
@@ -63011,10 +64044,11 @@
64044 ){
64045 int i, n;
64046 const unsigned char *pBlob;
64047 char *zHex, *z;
64048 assert( argc==1 );
64049 UNUSED_PARAMETER(argc);
64050 pBlob = sqlite3_value_blob(argv[0]);
64051 n = sqlite3_value_bytes(argv[0]);
64052 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
64053 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
64054 if( zHex ){
@@ -63036,10 +64070,11 @@
64070 int argc,
64071 sqlite3_value **argv
64072 ){
64073 i64 n;
64074 assert( argc==1 );
64075 UNUSED_PARAMETER(argc);
64076 n = sqlite3_value_int64(argv[0]);
64077 if( n>SQLITE_MAX_LENGTH ){
64078 sqlite3_result_error_toobig(context);
64079 }else{
64080 sqlite3_result_zeroblob(context, n);
@@ -63067,10 +64102,11 @@
64102 i64 nOut; /* Maximum size of zOut */
64103 int loopLimit; /* Last zStr[] that might match zPattern[] */
64104 int i, j; /* Loop counters */
64105
64106 assert( argc==3 );
64107 UNUSED_PARAMETER(argc);
64108 zStr = sqlite3_value_text(argv[0]);
64109 if( zStr==0 ) return;
64110 nStr = sqlite3_value_bytes(argv[0]);
64111 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
64112 zPattern = sqlite3_value_text(argv[1]);
@@ -63304,10 +64340,11 @@
64340 */
64341 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
64342 SumCtx *p;
64343 int type;
64344 assert( argc==1 );
64345 UNUSED_PARAMETER(argc);
64346 p = sqlite3_aggregate_context(context, sizeof(*p));
64347 type = sqlite3_value_numeric_type(argv[0]);
64348 if( p && type!=SQLITE_NULL ){
64349 p->cnt++;
64350 if( type==SQLITE_INTEGER ){
@@ -63379,13 +64416,18 @@
64416 }
64417
64418 /*
64419 ** Routines to implement min() and max() aggregate functions.
64420 */
64421 static void minmaxStep(
64422 sqlite3_context *context,
64423 int NotUsed,
64424 sqlite3_value **argv
64425 ){
64426 Mem *pArg = (Mem *)argv[0];
64427 Mem *pBest;
64428 UNUSED_PARAMETER(NotUsed);
64429
64430 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
64431 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
64432 if( !pBest ) return;
64433
@@ -63661,11 +64703,11 @@
64703 **
64704 *************************************************************************
64705 ** This file contains C code routines that are called by the parser
64706 ** to handle INSERT statements in SQLite.
64707 **
64708 ** $Id: insert.c,v 1.253 2008/11/19 09:05:27 danielk1977 Exp $
64709 */
64710
64711 /*
64712 ** Set P4 of the most recently inserted opcode to a column affinity
64713 ** string for index pIdx. A column affinity string has one character
@@ -64078,11 +65120,11 @@
65120
65121 /* Figure out if we have any triggers and if the table being
65122 ** inserted into is a view
65123 */
65124 #ifndef SQLITE_OMIT_TRIGGER
65125 triggers_exist = sqlite3TriggersExist(pTab, TK_INSERT, 0);
65126 isView = pTab->pSelect!=0;
65127 #else
65128 # define triggers_exist 0
65129 # define isView 0
65130 #endif
@@ -64584,11 +65626,10 @@
65626 pTab,
65627 baseCur,
65628 regIns,
65629 aRegIdx,
65630 0,
 
65631 (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1,
65632 appendFlag
65633 );
65634 }
65635 }
@@ -64920,30 +65961,30 @@
65961 case OE_Rollback:
65962 case OE_Abort:
65963 case OE_Fail: {
65964 int j, n1, n2;
65965 char zErrMsg[200];
65966 sqlite3_snprintf(ArraySize(zErrMsg), zErrMsg,
65967 pIdx->nColumn>1 ? "columns " : "column ");
65968 n1 = strlen(zErrMsg);
65969 for(j=0; j<pIdx->nColumn && n1<ArraySize(zErrMsg)-30; j++){
65970 char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
65971 n2 = strlen(zCol);
65972 if( j>0 ){
65973 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], ", ");
65974 n1 += 2;
65975 }
65976 if( n1+n2>ArraySize(zErrMsg)-30 ){
65977 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], "...");
65978 n1 += 3;
65979 break;
65980 }else{
65981 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol);
65982 n1 += n2;
65983 }
65984 }
65985 sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1],
65986 pIdx->nColumn>1 ? " are not unique" : " is not unique");
65987 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErrMsg,0);
65988 break;
65989 }
65990 case OE_Ignore: {
@@ -64976,11 +66017,10 @@
66017 Parse *pParse, /* The parser context */
66018 Table *pTab, /* the table into which we are inserting */
66019 int baseCur, /* Index of a read/write cursor pointing at pTab */
66020 int regRowid, /* Range of content */
66021 int *aRegIdx, /* Register used by each index. 0 for unused indices */
 
66022 int isUpdate, /* True for UPDATE, False for INSERT */
66023 int newIdx, /* Index of NEW table for triggers. -1 if none */
66024 int appendBias /* True if this is likely to be an append */
66025 ){
66026 int i;
@@ -66534,11 +67574,11 @@
67574 ** May you share freely, never taking more than you give.
67575 **
67576 *************************************************************************
67577 ** This file contains code used to implement the PRAGMA command.
67578 **
67579 ** $Id: pragma.c,v 1.194 2008/11/17 19:18:55 danielk1977 Exp $
67580 */
67581
67582 /* Ignore this whole file if pragmas are disabled
67583 */
67584 #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
@@ -66562,11 +67602,11 @@
67602 int i, n;
67603 if( isdigit(*z) ){
67604 return atoi(z);
67605 }
67606 n = strlen(z);
67607 for(i=0; i<ArraySize(iLength); i++){
67608 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
67609 return iValue[i];
67610 }
67611 }
67612 return 1;
@@ -66712,11 +67752,11 @@
67752 ** flag if there are any active statements. */
67753 { "read_uncommitted", SQLITE_ReadUncommitted },
67754 };
67755 int i;
67756 const struct sPragmaType *p;
67757 for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
67758 if( sqlite3StrICmp(zLeft, p->zName)==0 ){
67759 sqlite3 *db = pParse->db;
67760 Vdbe *v;
67761 v = sqlite3GetVdbe(pParse);
67762 if( v ){
@@ -66976,11 +68016,11 @@
68016 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
68017 }else
68018
68019 /*
68020 ** PRAGMA [database.]journal_mode
68021 ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
68022 */
68023 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
68024 int eMode;
68025 static char * const azModeName[] = {
68026 "delete", "persist", "off", "truncate", "memory"
@@ -67891,11 +68931,11 @@
68931 *************************************************************************
68932 ** This file contains the implementation of the sqlite3_prepare()
68933 ** interface, and routines that contribute to loading the database schema
68934 ** from disk.
68935 **
68936 ** $Id: prepare.c,v 1.101 2008/11/19 16:52:44 danielk1977 Exp $
68937 */
68938
68939 /*
68940 ** Fill the InitData structure with an error message that indicates
68941 ** that the database is corrupt.
@@ -67928,23 +68968,24 @@
68968 ** argv[0] = name of thing being created
68969 ** argv[1] = root page number for table or index. 0 for trigger or view.
68970 ** argv[2] = SQL text for the CREATE statement.
68971 **
68972 */
68973 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
68974 InitData *pData = (InitData*)pInit;
68975 sqlite3 *db = pData->db;
68976 int iDb = pData->iDb;
68977
68978 assert( argc==3 );
68979 UNUSED_PARAMETER2(NotUsed, argc);
68980 assert( sqlite3_mutex_held(db->mutex) );
68981 DbClearProperty(db, iDb, DB_Empty);
68982 if( db->mallocFailed ){
68983 corruptSchema(pData, argv[0], 0);
68984 return SQLITE_NOMEM;
68985 }
68986
 
68987 assert( iDb>=0 && iDb<db->nDb );
68988 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
68989 if( argv[1]==0 ){
68990 corruptSchema(pData, argv[0], 0);
68991 }else if( argv[2] && argv[2][0] ){
@@ -68120,11 +69161,11 @@
69161 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
69162 ** the possible values of meta[4].
69163 */
69164 if( rc==SQLITE_OK ){
69165 int i;
69166 for(i=0; i<ArraySize(meta); i++){
69167 rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
69168 if( rc ){
69169 sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
69170 goto initone_error_out;
69171 }
@@ -68702,11 +69743,11 @@
69743 **
69744 *************************************************************************
69745 ** This file contains C code routines that are called by the parser
69746 ** to handle SELECT statements in SQLite.
69747 **
69748 ** $Id: select.c,v 1.486 2008/11/19 09:05:27 danielk1977 Exp $
69749 */
69750
69751
69752 /*
69753 ** Delete all the content of a Select structure but do not deallocate
@@ -68754,11 +69795,11 @@
69795 ){
69796 Select *pNew;
69797 Select standin;
69798 sqlite3 *db = pParse->db;
69799 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
69800 assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
69801 if( pNew==0 ){
69802 pNew = &standin;
69803 memset(pNew, 0, sizeof(*pNew));
69804 }
69805 if( pEList==0 ){
@@ -68833,18 +69874,18 @@
69874 apAll[0] = pA;
69875 apAll[1] = pB;
69876 apAll[2] = pC;
69877 for(i=0; i<3 && apAll[i]; i++){
69878 p = apAll[i];
69879 for(j=0; j<ArraySize(keywords); j++){
69880 if( p->n==keywords[j].nChar
69881 && sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){
69882 jointype |= keywords[j].code;
69883 break;
69884 }
69885 }
69886 if( j>=ArraySize(keywords) ){
69887 jointype |= JT_ERROR;
69888 break;
69889 }
69890 }
69891 if(
@@ -71262,11 +72303,13 @@
72303 struct SrcList_item *pSubitem; /* The subquery */
72304 sqlite3 *db = pParse->db;
72305
72306 /* Check to see if flattening is permitted. Return 0 if not.
72307 */
72308 assert( p!=0 );
72309 if( p==0 ) return 0;
72310 assert( p->pPrior==0 ); /* Unable to flatten compound queries */
72311 pSrc = p->pSrc;
72312 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
72313 pSubitem = &pSrc->a[iFrom];
72314 iParent = pSubitem->iCursor;
72315 pSub = pSubitem->pSelect;
@@ -71374,96 +72417,150 @@
72417 ** be of the form:
72418 **
72419 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
72420 **
72421 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
72422 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
72423 ** OFFSET clauses and joins them to the left-hand-side of the original
72424 ** using UNION ALL operators. In this case N is the number of simple
72425 ** select statements in the compound sub-query.
72426 **
72427 ** Example:
72428 **
72429 ** SELECT a+1 FROM (
72430 ** SELECT x FROM tab
72431 ** UNION ALL
72432 ** SELECT y FROM tab
72433 ** UNION ALL
72434 ** SELECT abs(z*2) FROM tab2
72435 ** ) WHERE a!=5 ORDER BY 1
72436 **
72437 ** Transformed into:
72438 **
72439 ** SELECT x+1 FROM tab WHERE x+1!=5
72440 ** UNION ALL
72441 ** SELECT y+1 FROM tab WHERE y+1!=5
72442 ** UNION ALL
72443 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
72444 ** ORDER BY 1
72445 **
72446 ** We call this the "compound-subquery flattening".
72447 */
72448 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
72449 Select *pNew;
72450 ExprList *pOrderBy = p->pOrderBy;
72451 Expr *pLimit = p->pLimit;
 
72452 Select *pPrior = p->pPrior;
72453 p->pOrderBy = 0;
72454 p->pSrc = 0;
72455 p->pPrior = 0;
72456 p->pLimit = 0;
72457 pNew = sqlite3SelectDup(db, p);
72458 p->pLimit = pLimit;
 
72459 p->pOrderBy = pOrderBy;
72460 p->pSrc = pSrc;
72461 p->op = TK_ALL;
 
 
 
72462 p->pRightmost = 0;
72463 if( pNew==0 ){
72464 pNew = pPrior;
72465 }else{
72466 pNew->pPrior = pPrior;
72467 pNew->pRightmost = 0;
72468 }
72469 p->pPrior = pNew;
72470 if( db->mallocFailed ) return 1;
72471 }
72472
72473 /* Begin flattening the iFrom-th entry of the FROM clause
72474 ** in the outer query.
72475 */
72476 pSub = pSub1 = pSubitem->pSelect;
72477
72478 /* Delete the transient table structure associated with the
72479 ** subquery
72480 */
72481 sqlite3DbFree(db, pSubitem->zDatabase);
72482 sqlite3DbFree(db, pSubitem->zName);
72483 sqlite3DbFree(db, pSubitem->zAlias);
72484 pSubitem->zDatabase = 0;
72485 pSubitem->zName = 0;
72486 pSubitem->zAlias = 0;
72487 pSubitem->pSelect = 0;
72488
72489 /* Defer deleting the Table object associated with the
72490 ** subquery until code generation is
72491 ** complete, since there may still exist Expr.pTab entries that
72492 ** refer to the subquery even after flattening. Ticket #3346.
72493 */
72494 if( pSubitem->pTab!=0 ){
72495 Table *pTabToDel = pSubitem->pTab;
72496 if( pTabToDel->nRef==1 ){
72497 pTabToDel->pNextZombie = pParse->pZombieTab;
72498 pParse->pZombieTab = pTabToDel;
72499 }else{
72500 pTabToDel->nRef--;
72501 }
72502 pSubitem->pTab = 0;
72503 }
72504
72505 /* The following loop runs once for each term in a compound-subquery
72506 ** flattening (as described above). If we are doing a different kind
72507 ** of flattening - a flattening other than a compound-subquery flattening -
72508 ** then this loop only runs once.
72509 **
72510 ** This loop moves all of the FROM elements of the subquery into the
72511 ** the FROM clause of the outer query. Before doing this, remember
72512 ** the cursor number for the original outer query FROM element in
72513 ** iParent. The iParent cursor will never be used. Subsequent code
72514 ** will scan expressions looking for iParent references and replace
72515 ** those references with expressions that resolve to the subquery FROM
72516 ** elements we are now copying in.
72517 */
72518 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
72519 int nSubSrc;
72520 int jointype = 0;
72521 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
72522 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
72523 pSrc = pParent->pSrc; /* FROM clause of the outer query */
72524
 
 
 
 
 
 
 
72525 if( pSrc ){
72526 assert( pParent==p ); /* First time through the loop */
 
 
72527 jointype = pSubitem->jointype;
72528 }else{
72529 assert( pParent!=p ); /* 2nd and subsequent times through the loop */
72530 pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
72531 if( pSrc==0 ){
72532 assert( db->mallocFailed );
72533 break;
72534 }
72535 }
72536
72537 /* The subquery uses a single slot of the FROM clause of the outer
72538 ** query. If the subquery has more than one element in its FROM clause,
72539 ** then expand the outer query to make space for it to hold all elements
72540 ** of the subquery.
72541 **
72542 ** Example:
72543 **
72544 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
72545 **
72546 ** The outer query has 3 slots in its FROM clause. One slot of the
72547 ** outer query (the middle slot) is used by the subquery. The next
72548 ** block of code will expand the out query to 4 slots. The middle
72549 ** slot is expanded to two slots in order to make space for the
72550 ** two elements in the FROM clause of the subquery.
72551 */
72552 if( nSubSrc>1 ){
72553 pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
72554 if( db->mallocFailed ){
72555 break;
72556 }
72557 }
72558
72559 /* Transfer the FROM clause terms from the subquery into the
72560 ** outer query.
72561 */
 
 
72562 for(i=0; i<nSubSrc; i++){
72563 pSrc->a[i+iFrom] = pSubSrc->a[i];
72564 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
72565 }
72566 pSrc->a[iFrom].jointype = jointype;
@@ -71554,11 +72651,11 @@
72651 ** 1. There is a single object in the FROM clause.
72652 **
72653 ** 2. There is a single expression in the result set, and it is
72654 ** either min(x) or max(x), where x is a column reference.
72655 */
72656 static int minMaxQuery(Select *p){
72657 Expr *pExpr;
72658 ExprList *pEList = p->pEList;
72659
72660 if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
72661 pExpr = pEList->a[0].pExpr;
@@ -71854,11 +72951,12 @@
72951 ** are walked without any actions being taken at each node. Presumably,
72952 ** when this routine is used for Walker.xExprCallback then
72953 ** Walker.xSelectCallback is set to do something useful for every
72954 ** subquery in the parser tree.
72955 */
72956 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
72957 UNUSED_PARAMETER2(NotUsed, NotUsed2);
72958 return WRC_Continue;
72959 }
72960
72961 /*
72962 ** This routine "expands" a SELECT statement and all of its subqueries.
@@ -72645,11 +73743,11 @@
73743 ** + The optimizer code in where.c (the thing that decides which
73744 ** index or indices to use) should place a different priority on
73745 ** satisfying the 'ORDER BY' clause than it does in other cases.
73746 ** Refer to code and comments in where.c for details.
73747 */
73748 flag = minMaxQuery(p);
73749 if( flag ){
73750 pDel = pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->pList);
73751 if( pMinMax && !db->mallocFailed ){
73752 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN;
73753 pMinMax->a[0].pExpr->op = TK_COLUMN;
@@ -73027,11 +74125,11 @@
74125 ** May you share freely, never taking more than you give.
74126 **
74127 *************************************************************************
74128 **
74129 **
74130 ** $Id: trigger.c,v 1.130 2008/11/19 09:05:27 danielk1977 Exp $
74131 */
74132
74133 #ifndef SQLITE_OMIT_TRIGGER
74134 /*
74135 ** Delete a linked list of TriggerStep structures.
@@ -73619,11 +74717,10 @@
74717 **
74718 ** The returned bit vector is some combination of TRIGGER_BEFORE and
74719 ** TRIGGER_AFTER.
74720 */
74721 SQLITE_PRIVATE int sqlite3TriggersExist(
 
74722 Table *pTab, /* The table the contains the triggers */
74723 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
74724 ExprList *pChanges /* Columns that change in an UPDATE statement */
74725 ){
74726 Trigger *pTrigger;
@@ -73883,11 +74980,11 @@
74980 **
74981 *************************************************************************
74982 ** This file contains C code routines that are called by the parser
74983 ** to handle UPDATE statements.
74984 **
74985 ** $Id: update.c,v 1.187 2008/11/19 09:05:27 danielk1977 Exp $
74986 */
74987
74988 #ifndef SQLITE_OMIT_VIRTUALTABLE
74989 /* Forward declaration */
74990 static void updateVirtualTable(
@@ -74012,11 +75109,11 @@
75109
75110 /* Figure out if we have any triggers and if the table being
75111 ** updated is a view
75112 */
75113 #ifndef SQLITE_OMIT_TRIGGER
75114 triggers_exist = sqlite3TriggersExist(pTab, TK_UPDATE, pChanges);
75115 isView = pTab->pSelect!=0;
75116 #else
75117 # define triggers_exist 0
75118 # define isView 0
75119 #endif
@@ -74395,11 +75492,11 @@
75492 sqlite3VdbeJumpHere(v, j1);
75493
75494 /* Create the new index entries and the new record.
75495 */
75496 sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid,
75497 aRegIdx, 1, -1, 0);
75498 }
75499
75500 /* Increment the row counter
75501 */
75502 if( db->flags & SQLITE_CountRows && !pParse->trigStack){
@@ -74568,11 +75665,11 @@
75665 ** This file contains code used to implement the VACUUM command.
75666 **
75667 ** Most of the code in this file may be omitted by defining the
75668 ** SQLITE_OMIT_VACUUM macro.
75669 **
75670 ** $Id: vacuum.c,v 1.84 2008/11/17 19:18:55 danielk1977 Exp $
75671 */
75672
75673 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
75674 /*
75675 ** Execute zSql on database db. Return an error code.
@@ -74803,11 +75900,11 @@
75900
75901 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
75902 assert( 1==sqlite3BtreeIsInTrans(pMain) );
75903
75904 /* Copy Btree meta values */
75905 for(i=0; i<ArraySize(aCopy); i+=2){
75906 rc = sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
75907 if( rc!=SQLITE_OK ) goto end_of_vacuum;
75908 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
75909 if( rc!=SQLITE_OK ) goto end_of_vacuum;
75910 }
@@ -74866,11 +75963,11 @@
75963 ** May you share freely, never taking more than you give.
75964 **
75965 *************************************************************************
75966 ** This file contains code used to help implement virtual tables.
75967 **
75968 ** $Id: vtab.c,v 1.78 2008/11/13 19:12:36 danielk1977 Exp $
75969 */
75970 #ifndef SQLITE_OMIT_VIRTUALTABLE
75971
75972 static int createModule(
75973 sqlite3 *db, /* Database in which module is registered */
@@ -74900,10 +75997,12 @@
75997 sqlite3DbFree(db, pDel);
75998 if( pDel==pMod ){
75999 db->mallocFailed = 1;
76000 }
76001 sqlite3ResetInternalSchema(db, 0);
76002 }else if( xDestroy ){
76003 xDestroy(pAux);
76004 }
76005 rc = sqlite3ApiExit(db, SQLITE_OK);
76006 sqlite3_mutex_leave(db->mutex);
76007 return rc;
76008 }
@@ -75564,11 +76663,11 @@
76663 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
76664 ** than zero, then this function is being called from within a
76665 ** virtual module xSync() callback. It is illegal to write to
76666 ** virtual module tables in this case, so return SQLITE_LOCKED.
76667 */
76668 if( sqlite3VtabInSync(db) ){
76669 return SQLITE_LOCKED;
76670 }
76671 if( !pVtab ){
76672 return SQLITE_OK;
76673 }
@@ -75712,18 +76811,13 @@
76811 ** generating the code that loops through a table looking for applicable
76812 ** rows. Indices are selected and used to speed the search when doing
76813 ** so is applicable. Because this module is responsible for selecting
76814 ** indices, you might also think of this module as the "query optimizer".
76815 **
76816 ** $Id: where.c,v 1.330 2008/11/17 19:18:55 danielk1977 Exp $
76817 */
76818
 
 
 
 
 
76819 /*
76820 ** Trace output macros
76821 */
76822 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
76823 SQLITE_PRIVATE int sqlite3WhereTrace = 0;
@@ -75834,11 +76928,11 @@
76928 ** numbers all get mapped into bit numbers that begin with 0 and contain
76929 ** no gaps.
76930 */
76931 struct ExprMaskSet {
76932 int n; /* Number of assigned cursor values */
76933 int ix[BMS]; /* Cursor assigned to each bit */
76934 };
76935
76936
76937 /*
76938 ** Bitmasks for the operators that indices are able to exploit. An
@@ -78015,11 +79109,11 @@
79109 }else
79110 #endif
79111 if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
79112 int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
79113 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
79114 if( !pWInfo->okOnePass && pTab->nCol<BMS ){
79115 Bitmask b = pTabItem->colUsed;
79116 int n = 0;
79117 for(; b; b=b>>1, n++){}
79118 sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-2, n);
79119 assert( n<=pTab->nCol );
@@ -79896,10 +80990,12 @@
80990 pParser->yyidx = -1;
80991 #ifdef YYTRACKMAXSTACKDEPTH
80992 pParser->yyidxMax = 0;
80993 #endif
80994 #if YYSTACKDEPTH<=0
80995 pParser->yystack = NULL;
80996 pParser->yystksz = 0;
80997 yyGrowStack(pParser);
80998 #endif
80999 }
81000 return pParser;
81001 }
@@ -82745,11 +83841,11 @@
83841 ** Main file for the SQLite library. The routines in this file
83842 ** implement the programmer interface to the library. Routines in
83843 ** other files are for internal use by SQLite and should not be
83844 ** accessed by users of the library.
83845 **
83846 ** $Id: main.c,v 1.514 2008/11/19 09:05:27 danielk1977 Exp $
83847 */
83848
83849 #ifdef SQLITE_ENABLE_FTS3
83850 /************** Include fts3.h in the middle of main.c ***********************/
83851 /************** Begin file fts3.h ********************************************/
@@ -83125,10 +84221,24 @@
84221 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
84222 sqlite3GlobalConfig.szPage = va_arg(ap, int);
84223 sqlite3GlobalConfig.nPage = va_arg(ap, int);
84224 break;
84225 }
84226
84227 case SQLITE_CONFIG_PCACHE: {
84228 /* Specify an alternative malloc implementation */
84229 sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
84230 break;
84231 }
84232
84233 case SQLITE_CONFIG_GETPCACHE: {
84234 if( sqlite3GlobalConfig.pcache.xInit==0 ){
84235 sqlite3PCacheSetDefault();
84236 }
84237 *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
84238 break;
84239 }
84240
84241 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
84242 case SQLITE_CONFIG_HEAP: {
84243 /* Designate a buffer for heap memory space */
84244 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
@@ -83155,18 +84265,10 @@
84265 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
84266 #endif
84267 }
84268 break;
84269 }
 
 
 
 
 
 
 
 
84270 #endif
84271
84272 case SQLITE_CONFIG_LOOKASIDE: {
84273 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
84274 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
@@ -83326,10 +84428,11 @@
84428 int nKey1, const void *pKey1,
84429 int nKey2, const void *pKey2
84430 ){
84431 int r = sqlite3StrNICmp(
84432 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
84433 UNUSED_PARAMETER(NotUsed);
84434 if( 0==r ){
84435 r = nKey1-nKey2;
84436 }
84437 return r;
84438 }
@@ -92737,10 +93840,17 @@
93840 elem->data = data;
93841 }
93842 return old_data;
93843 }
93844 if( data==0 ) return 0;
93845 if( pH->htsize==0 ){
93846 fts3Rehash(pH,8);
93847 if( pH->htsize==0 ){
93848 pH->count = 0;
93849 return data;
93850 }
93851 }
93852 new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) );
93853 if( new_elem==0 ) return data;
93854 if( pH->copyKey && pKey!=0 ){
93855 new_elem->pKey = fts3HashMalloc( nKey );
93856 if( new_elem->pKey==0 ){
@@ -92751,18 +93861,10 @@
93861 }else{
93862 new_elem->pKey = (void*)pKey;
93863 }
93864 new_elem->nKey = nKey;
93865 pH->count++;
 
 
 
 
 
 
 
 
93866 if( pH->count > pH->htsize ){
93867 fts3Rehash(pH,pH->htsize*2);
93868 }
93869 assert( pH->htsize>0 );
93870 assert( (pH->htsize & (pH->htsize-1))==0 );
@@ -94022,11 +95124,11 @@
95124 **
95125 *************************************************************************
95126 ** This file contains code for implementations of the r-tree and r*-tree
95127 ** algorithms packaged as an SQLite virtual table module.
95128 **
95129 ** $Id: rtree.c,v 1.11 2008/11/12 15:24:27 drh Exp $
95130 */
95131
95132 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
95133
95134 /*
@@ -94232,12 +95334,16 @@
95334 struct RtreeCell {
95335 i64 iRowid;
95336 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
95337 };
95338
95339 #ifndef MAX
95340 # define MAX(x,y) ((x) < (y) ? (y) : (x))
95341 #endif
95342 #ifndef MIN
95343 # define MIN(x,y) ((x) > (y) ? (y) : (x))
95344 #endif
95345
95346 /*
95347 ** Functions to deserialize a 16 bit integer, 32 bit real number and
95348 ** 64 bit integer. The deserialized value is returned.
95349 */
95350
+168 -9
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -28,11 +28,11 @@
2828
** The name of this file under configuration management is "sqlite.h.in".
2929
** The makefile makes some minor changes to this file (such as inserting
3030
** the version number) and changes its name to "sqlite3.h" as
3131
** part of the build process.
3232
**
33
-** @(#) $Id: sqlite.h.in,v 1.409 2008/11/07 00:06:18 drh Exp $
33
+** @(#) $Id: sqlite.h.in,v 1.415 2008/11/19 01:20:26 drh Exp $
3434
*/
3535
#ifndef _SQLITE3_H_
3636
#define _SQLITE3_H_
3737
#include <stdarg.h> /* Needed for the definition of va_list */
3838
@@ -105,12 +105,12 @@
105105
**
106106
** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer
107107
** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z
108108
** are the major version, minor version, and release number.
109109
*/
110
-#define SQLITE_VERSION "3.6.4"
111
-#define SQLITE_VERSION_NUMBER 3006004
110
+#define SQLITE_VERSION "3.6.6.1"
111
+#define SQLITE_VERSION_NUMBER 3006006
112112
113113
/*
114114
** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
115115
** KEYWORDS: sqlite3_version
116116
**
@@ -585,11 +585,11 @@
585585
**
586586
** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
587587
** sync operation only needs to flush data to mass storage. Inode
588588
** information need not be flushed. The SQLITE_SYNC_NORMAL flag means
589589
** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means
590
-** to use Mac OS-X style fullsync instead of fsync().
590
+** to use Mac OS X style fullsync instead of fsync().
591591
*/
592592
#define SQLITE_SYNC_NORMAL 0x00002
593593
#define SQLITE_SYNC_FULL 0x00003
594594
#define SQLITE_SYNC_DATAONLY 0x00010
595595
@@ -617,11 +617,11 @@
617617
** This object defines the methods used to perform various operations
618618
** against the open file represented by the [sqlite3_file] object.
619619
**
620620
** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
621621
** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
622
-** The second choice is a Mac OS-X style fullsync. The [SQLITE_SYNC_DATAONLY]
622
+** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
623623
** flag may be ORed in to indicate that only the data of the file
624624
** and not its inode needs to be synced.
625625
**
626626
** The integer values to xLock() and xUnlock() are one of
627627
** <ul>
@@ -1272,11 +1272,14 @@
12721272
** scratch buffers or if no scratch buffer space is specified, then SQLite
12731273
** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
12741274
**
12751275
** <dt>SQLITE_CONFIG_PAGECACHE</dt>
12761276
** <dd>This option specifies a static memory buffer that SQLite can use for
1277
-** the database page cache. There are three arguments: A pointer to the
1277
+** the database page cache with the default page cache implemenation.
1278
+** This configuration should not be used if an application-define page
1279
+** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1280
+** There are three arguments to this option: A pointer to the
12781281
** memory, the size of each page buffer (sz), and the number of pages (N).
12791282
** The sz argument must be a power of two between 512 and 32768. The first
12801283
** argument should point to an allocation of at least sz*N bytes of memory.
12811284
** SQLite will use the memory provided by the first argument to satisfy its
12821285
** memory needs for the first N pages that it adds to cache. If additional
@@ -1317,10 +1320,21 @@
13171320
** <dd>This option takes two arguments that determine the default
13181321
** memory allcation lookaside optimization. The first argument is the
13191322
** size of each lookaside buffer slot and the second is the number of
13201323
** slots allocated to each database connection.</dd>
13211324
**
1325
+** <dt>SQLITE_CONFIG_PCACHE</dt>
1326
+** <dd>This option takes a single argument which is a pointer to
1327
+** an [sqlite3_pcache_methods] object. This object specifies the interface
1328
+** to a custom page cache implementation. SQLite makes a copy of the
1329
+** object and uses it for page cache memory allocations.</dd>
1330
+**
1331
+** <dt>SQLITE_CONFIG_GETPCACHE</dt>
1332
+** <dd>This option takes a single argument which is a pointer to an
1333
+** [sqlite3_pcache_methods] object. SQLite copies of the current
1334
+** page cache implementation into that object.</dd>
1335
+**
13221336
** </dl>
13231337
*/
13241338
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
13251339
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
13261340
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1330,12 +1344,14 @@
13301344
#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
13311345
#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
13321346
#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
13331347
#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
13341348
#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1335
-#define SQLITE_CONFIG_CHUNKALLOC 12 /* int threshold */
1349
+/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
13361350
#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
1351
+#define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
1352
+#define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
13371353
13381354
/*
13391355
** CAPI3REF: Configuration Options {H10170} <S20000>
13401356
** EXPERIMENTAL
13411357
**
@@ -2381,11 +2397,11 @@
23812397
#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
23822398
#define SQLITE_REINDEX 27 /* Index Name NULL */
23832399
#define SQLITE_ANALYZE 28 /* Table Name NULL */
23842400
#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
23852401
#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
2386
-#define SQLITE_FUNCTION 31 /* Function Name NULL */
2402
+#define SQLITE_FUNCTION 31 /* NULL Function Name */
23872403
#define SQLITE_COPY 0 /* No longer used */
23882404
23892405
/*
23902406
** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400>
23912407
** EXPERIMENTAL
@@ -4149,11 +4165,11 @@
41494165
**
41504166
** These functions are [deprecated]. In order to maintain
41514167
** backwards compatibility with older code, these functions continue
41524168
** to be supported. However, new applications should avoid
41534169
** the use of these functions. To help encourage people to avoid
4154
-** using these functions, we are not going to tell you want they do.
4170
+** using these functions, we are not going to tell you what they do.
41554171
*/
41564172
#ifndef SQLITE_OMIT_DEPRECATED
41574173
SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
41584174
SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
41594175
SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
@@ -6552,10 +6568,153 @@
65526568
**
65536569
** </dl>
65546570
*/
65556571
#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
65566572
#define SQLITE_STMTSTATUS_SORT 2
6573
+
6574
+/*
6575
+** CAPI3REF: Custom Page Cache Object
6576
+** EXPERIMENTAL
6577
+**
6578
+** The sqlite3_pcache type is opaque. It is implemented by
6579
+** the pluggable module. The SQLite core has no knowledge of
6580
+** its size or internal structure and never deals with the
6581
+** sqlite3_pcache object except by holding and passing pointers
6582
+** to the object.
6583
+**
6584
+** See [sqlite3_pcache_methods] for additional information.
6585
+*/
6586
+typedef struct sqlite3_pcache sqlite3_pcache;
6587
+
6588
+/*
6589
+** CAPI3REF: Application Defined Page Cache.
6590
+** EXPERIMENTAL
6591
+**
6592
+** The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6593
+** register an alternative page cache implementation by passing in an
6594
+** instance of the sqlite3_pcache_methods structure. The majority of the
6595
+** heap memory used by sqlite is used by the page cache to cache data read
6596
+** from, or ready to be written to, the database file. By implementing a
6597
+** custom page cache using this API, an application can control more
6598
+** precisely the amount of memory consumed by sqlite, the way in which
6599
+** said memory is allocated and released, and the policies used to
6600
+** determine exactly which parts of a database file are cached and for
6601
+** how long.
6602
+**
6603
+** The contents of the structure are copied to an internal buffer by sqlite
6604
+** within the call to [sqlite3_config].
6605
+**
6606
+** The xInit() method is called once for each call to [sqlite3_initialize()]
6607
+** (usually only once during the lifetime of the process). It is passed
6608
+** a copy of the sqlite3_pcache_methods.pArg value. It can be used to set
6609
+** up global structures and mutexes required by the custom page cache
6610
+** implementation. The xShutdown() method is called from within
6611
+** [sqlite3_shutdown()], if the application invokes this API. It can be used
6612
+** to clean up any outstanding resources before process shutdown, if required.
6613
+**
6614
+** The xCreate() method is used to construct a new cache instance. The
6615
+** first parameter, szPage, is the size in bytes of the pages that must
6616
+** be allocated by the cache. szPage will not be a power of two. The
6617
+** second argument, bPurgeable, is true if the cache being created will
6618
+** be used to cache database pages read from a file stored on disk, or
6619
+** false if it is used for an in-memory database. The cache implementation
6620
+** does not have to do anything special based on the value of bPurgeable,
6621
+** it is purely advisory.
6622
+**
6623
+** The xCachesize() method may be called at any time by SQLite to set the
6624
+** suggested maximum cache-size (number of pages stored by) the cache
6625
+** instance passed as the first argument. This is the value configured using
6626
+** the SQLite "[PRAGMA cache_size]" command. As with the bPurgeable parameter,
6627
+** the implementation is not required to do anything special with this
6628
+** value, it is advisory only.
6629
+**
6630
+** The xPagecount() method should return the number of pages currently
6631
+** stored in the cache supplied as an argument.
6632
+**
6633
+** The xFetch() method is used to fetch a page and return a pointer to it.
6634
+** A 'page', in this context, is a buffer of szPage bytes aligned at an
6635
+** 8-byte boundary. The page to be fetched is determined by the key. The
6636
+** mimimum key value is 1. After it has been retrieved using xFetch, the page
6637
+** is considered to be pinned.
6638
+**
6639
+** If the requested page is already in the page cache, then a pointer to
6640
+** the cached buffer should be returned with its contents intact. If the
6641
+** page is not already in the cache, then the expected behaviour of the
6642
+** cache is determined by the value of the createFlag parameter passed
6643
+** to xFetch, according to the following table:
6644
+**
6645
+** <table border=1 width=85% align=center>
6646
+** <tr><th>createFlag<th>Expected Behaviour
6647
+** <tr><td>0<td>NULL should be returned. No new cache entry is created.
6648
+** <tr><td>1<td>If createFlag is set to 1, this indicates that
6649
+** SQLite is holding pinned pages that can be unpinned
6650
+** by writing their contents to the database file (a
6651
+** relatively expensive operation). In this situation the
6652
+** cache implementation has two choices: it can return NULL,
6653
+** in which case SQLite will attempt to unpin one or more
6654
+** pages before re-requesting the same page, or it can
6655
+** allocate a new page and return a pointer to it. If a new
6656
+** page is allocated, then it must be completely zeroed before
6657
+** it is returned.
6658
+** <tr><td>2<td>If createFlag is set to 2, then SQLite is not holding any
6659
+** pinned pages associated with the specific cache passed
6660
+** as the first argument to xFetch() that can be unpinned. The
6661
+** cache implementation should attempt to allocate a new
6662
+** cache entry and return a pointer to it. Again, the new
6663
+** page should be zeroed before it is returned. If the xFetch()
6664
+** method returns NULL when createFlag==2, SQLite assumes that
6665
+** a memory allocation failed and returns SQLITE_NOMEM to the
6666
+** user.
6667
+** </table>
6668
+**
6669
+** xUnpin() is called by SQLite with a pointer to a currently pinned page
6670
+** as its second argument. If the third parameter, discard, is non-zero,
6671
+** then the page should be evicted from the cache. In this case SQLite
6672
+** assumes that the next time the page is retrieved from the cache using
6673
+** the xFetch() method, it will be zeroed. If the discard parameter is
6674
+** zero, then the page is considered to be unpinned. The cache implementation
6675
+** may choose to reclaim (free or recycle) unpinned pages at any time.
6676
+** SQLite assumes that next time the page is retrieved from the cache
6677
+** it will either be zeroed, or contain the same data that it did when it
6678
+** was unpinned.
6679
+**
6680
+** The cache is not required to perform any reference counting. A single
6681
+** call to xUnpin() unpins the page regardless of the number of prior calls
6682
+** to xFetch().
6683
+**
6684
+** The xRekey() method is used to change the key value associated with the
6685
+** page passed as the second argument from oldKey to newKey. If the cache
6686
+** previously contains an entry associated with newKey, it should be
6687
+** discarded. Any prior cache entry associated with newKey is guaranteed not
6688
+** to be pinned.
6689
+**
6690
+** When SQLite calls the xTruncate() method, the cache must discard all
6691
+** existing cache entries with page numbers (keys) greater than or equal
6692
+** to the value of the iLimit parameter passed to xTruncate(). If any
6693
+** of these pages are pinned, they are implicitly unpinned, meaning that
6694
+** they can be safely discarded.
6695
+**
6696
+** The xDestroy() method is used to delete a cache allocated by xCreate().
6697
+** All resources associated with the specified cache should be freed. After
6698
+** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6699
+** handle invalid, and will not use it with any other sqlite3_pcache_methods
6700
+** functions.
6701
+*/
6702
+typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6703
+struct sqlite3_pcache_methods {
6704
+ void *pArg;
6705
+ int (*xInit)(void*);
6706
+ void (*xShutdown)(void*);
6707
+ sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
6708
+ void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6709
+ int (*xPagecount)(sqlite3_pcache*);
6710
+ void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6711
+ void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6712
+ void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6713
+ void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6714
+ void (*xDestroy)(sqlite3_pcache*);
6715
+};
65576716
65586717
/*
65596718
** Undo the hack that converts floating point types to integer for
65606719
** builds on processors without floating point support.
65616720
*/
65626721
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -28,11 +28,11 @@
28 ** The name of this file under configuration management is "sqlite.h.in".
29 ** The makefile makes some minor changes to this file (such as inserting
30 ** the version number) and changes its name to "sqlite3.h" as
31 ** part of the build process.
32 **
33 ** @(#) $Id: sqlite.h.in,v 1.409 2008/11/07 00:06:18 drh Exp $
34 */
35 #ifndef _SQLITE3_H_
36 #define _SQLITE3_H_
37 #include <stdarg.h> /* Needed for the definition of va_list */
38
@@ -105,12 +105,12 @@
105 **
106 ** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer
107 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z
108 ** are the major version, minor version, and release number.
109 */
110 #define SQLITE_VERSION "3.6.4"
111 #define SQLITE_VERSION_NUMBER 3006004
112
113 /*
114 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
115 ** KEYWORDS: sqlite3_version
116 **
@@ -585,11 +585,11 @@
585 **
586 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
587 ** sync operation only needs to flush data to mass storage. Inode
588 ** information need not be flushed. The SQLITE_SYNC_NORMAL flag means
589 ** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means
590 ** to use Mac OS-X style fullsync instead of fsync().
591 */
592 #define SQLITE_SYNC_NORMAL 0x00002
593 #define SQLITE_SYNC_FULL 0x00003
594 #define SQLITE_SYNC_DATAONLY 0x00010
595
@@ -617,11 +617,11 @@
617 ** This object defines the methods used to perform various operations
618 ** against the open file represented by the [sqlite3_file] object.
619 **
620 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
621 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
622 ** The second choice is a Mac OS-X style fullsync. The [SQLITE_SYNC_DATAONLY]
623 ** flag may be ORed in to indicate that only the data of the file
624 ** and not its inode needs to be synced.
625 **
626 ** The integer values to xLock() and xUnlock() are one of
627 ** <ul>
@@ -1272,11 +1272,14 @@
1272 ** scratch buffers or if no scratch buffer space is specified, then SQLite
1273 ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
1274 **
1275 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1276 ** <dd>This option specifies a static memory buffer that SQLite can use for
1277 ** the database page cache. There are three arguments: A pointer to the
 
 
 
1278 ** memory, the size of each page buffer (sz), and the number of pages (N).
1279 ** The sz argument must be a power of two between 512 and 32768. The first
1280 ** argument should point to an allocation of at least sz*N bytes of memory.
1281 ** SQLite will use the memory provided by the first argument to satisfy its
1282 ** memory needs for the first N pages that it adds to cache. If additional
@@ -1317,10 +1320,21 @@
1317 ** <dd>This option takes two arguments that determine the default
1318 ** memory allcation lookaside optimization. The first argument is the
1319 ** size of each lookaside buffer slot and the second is the number of
1320 ** slots allocated to each database connection.</dd>
1321 **
 
 
 
 
 
 
 
 
 
 
 
1322 ** </dl>
1323 */
1324 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1325 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1326 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1330,12 +1344,14 @@
1330 #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
1331 #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
1332 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
1333 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
1334 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1335 #define SQLITE_CONFIG_CHUNKALLOC 12 /* int threshold */
1336 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
 
 
1337
1338 /*
1339 ** CAPI3REF: Configuration Options {H10170} <S20000>
1340 ** EXPERIMENTAL
1341 **
@@ -2381,11 +2397,11 @@
2381 #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
2382 #define SQLITE_REINDEX 27 /* Index Name NULL */
2383 #define SQLITE_ANALYZE 28 /* Table Name NULL */
2384 #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
2385 #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
2386 #define SQLITE_FUNCTION 31 /* Function Name NULL */
2387 #define SQLITE_COPY 0 /* No longer used */
2388
2389 /*
2390 ** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400>
2391 ** EXPERIMENTAL
@@ -4149,11 +4165,11 @@
4149 **
4150 ** These functions are [deprecated]. In order to maintain
4151 ** backwards compatibility with older code, these functions continue
4152 ** to be supported. However, new applications should avoid
4153 ** the use of these functions. To help encourage people to avoid
4154 ** using these functions, we are not going to tell you want they do.
4155 */
4156 #ifndef SQLITE_OMIT_DEPRECATED
4157 SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4158 SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4159 SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
@@ -6552,10 +6568,153 @@
6552 **
6553 ** </dl>
6554 */
6555 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
6556 #define SQLITE_STMTSTATUS_SORT 2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6557
6558 /*
6559 ** Undo the hack that converts floating point types to integer for
6560 ** builds on processors without floating point support.
6561 */
6562
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -28,11 +28,11 @@
28 ** The name of this file under configuration management is "sqlite.h.in".
29 ** The makefile makes some minor changes to this file (such as inserting
30 ** the version number) and changes its name to "sqlite3.h" as
31 ** part of the build process.
32 **
33 ** @(#) $Id: sqlite.h.in,v 1.415 2008/11/19 01:20:26 drh Exp $
34 */
35 #ifndef _SQLITE3_H_
36 #define _SQLITE3_H_
37 #include <stdarg.h> /* Needed for the definition of va_list */
38
@@ -105,12 +105,12 @@
105 **
106 ** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer
107 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z
108 ** are the major version, minor version, and release number.
109 */
110 #define SQLITE_VERSION "3.6.6.1"
111 #define SQLITE_VERSION_NUMBER 3006006
112
113 /*
114 ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100>
115 ** KEYWORDS: sqlite3_version
116 **
@@ -585,11 +585,11 @@
585 **
586 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
587 ** sync operation only needs to flush data to mass storage. Inode
588 ** information need not be flushed. The SQLITE_SYNC_NORMAL flag means
589 ** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means
590 ** to use Mac OS X style fullsync instead of fsync().
591 */
592 #define SQLITE_SYNC_NORMAL 0x00002
593 #define SQLITE_SYNC_FULL 0x00003
594 #define SQLITE_SYNC_DATAONLY 0x00010
595
@@ -617,11 +617,11 @@
617 ** This object defines the methods used to perform various operations
618 ** against the open file represented by the [sqlite3_file] object.
619 **
620 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
621 ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
622 ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
623 ** flag may be ORed in to indicate that only the data of the file
624 ** and not its inode needs to be synced.
625 **
626 ** The integer values to xLock() and xUnlock() are one of
627 ** <ul>
@@ -1272,11 +1272,14 @@
1272 ** scratch buffers or if no scratch buffer space is specified, then SQLite
1273 ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd>
1274 **
1275 ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
1276 ** <dd>This option specifies a static memory buffer that SQLite can use for
1277 ** the database page cache with the default page cache implemenation.
1278 ** This configuration should not be used if an application-define page
1279 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1280 ** There are three arguments to this option: A pointer to the
1281 ** memory, the size of each page buffer (sz), and the number of pages (N).
1282 ** The sz argument must be a power of two between 512 and 32768. The first
1283 ** argument should point to an allocation of at least sz*N bytes of memory.
1284 ** SQLite will use the memory provided by the first argument to satisfy its
1285 ** memory needs for the first N pages that it adds to cache. If additional
@@ -1317,10 +1320,21 @@
1320 ** <dd>This option takes two arguments that determine the default
1321 ** memory allcation lookaside optimization. The first argument is the
1322 ** size of each lookaside buffer slot and the second is the number of
1323 ** slots allocated to each database connection.</dd>
1324 **
1325 ** <dt>SQLITE_CONFIG_PCACHE</dt>
1326 ** <dd>This option takes a single argument which is a pointer to
1327 ** an [sqlite3_pcache_methods] object. This object specifies the interface
1328 ** to a custom page cache implementation. SQLite makes a copy of the
1329 ** object and uses it for page cache memory allocations.</dd>
1330 **
1331 ** <dt>SQLITE_CONFIG_GETPCACHE</dt>
1332 ** <dd>This option takes a single argument which is a pointer to an
1333 ** [sqlite3_pcache_methods] object. SQLite copies of the current
1334 ** page cache implementation into that object.</dd>
1335 **
1336 ** </dl>
1337 */
1338 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1339 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1340 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1330,12 +1344,14 @@
1344 #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
1345 #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
1346 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
1347 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
1348 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1349 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
1350 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
1351 #define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
1352 #define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
1353
1354 /*
1355 ** CAPI3REF: Configuration Options {H10170} <S20000>
1356 ** EXPERIMENTAL
1357 **
@@ -2381,11 +2397,11 @@
2397 #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
2398 #define SQLITE_REINDEX 27 /* Index Name NULL */
2399 #define SQLITE_ANALYZE 28 /* Table Name NULL */
2400 #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
2401 #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
2402 #define SQLITE_FUNCTION 31 /* NULL Function Name */
2403 #define SQLITE_COPY 0 /* No longer used */
2404
2405 /*
2406 ** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400>
2407 ** EXPERIMENTAL
@@ -4149,11 +4165,11 @@
4165 **
4166 ** These functions are [deprecated]. In order to maintain
4167 ** backwards compatibility with older code, these functions continue
4168 ** to be supported. However, new applications should avoid
4169 ** the use of these functions. To help encourage people to avoid
4170 ** using these functions, we are not going to tell you what they do.
4171 */
4172 #ifndef SQLITE_OMIT_DEPRECATED
4173 SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4174 SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4175 SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
@@ -6552,10 +6568,153 @@
6568 **
6569 ** </dl>
6570 */
6571 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
6572 #define SQLITE_STMTSTATUS_SORT 2
6573
6574 /*
6575 ** CAPI3REF: Custom Page Cache Object
6576 ** EXPERIMENTAL
6577 **
6578 ** The sqlite3_pcache type is opaque. It is implemented by
6579 ** the pluggable module. The SQLite core has no knowledge of
6580 ** its size or internal structure and never deals with the
6581 ** sqlite3_pcache object except by holding and passing pointers
6582 ** to the object.
6583 **
6584 ** See [sqlite3_pcache_methods] for additional information.
6585 */
6586 typedef struct sqlite3_pcache sqlite3_pcache;
6587
6588 /*
6589 ** CAPI3REF: Application Defined Page Cache.
6590 ** EXPERIMENTAL
6591 **
6592 ** The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6593 ** register an alternative page cache implementation by passing in an
6594 ** instance of the sqlite3_pcache_methods structure. The majority of the
6595 ** heap memory used by sqlite is used by the page cache to cache data read
6596 ** from, or ready to be written to, the database file. By implementing a
6597 ** custom page cache using this API, an application can control more
6598 ** precisely the amount of memory consumed by sqlite, the way in which
6599 ** said memory is allocated and released, and the policies used to
6600 ** determine exactly which parts of a database file are cached and for
6601 ** how long.
6602 **
6603 ** The contents of the structure are copied to an internal buffer by sqlite
6604 ** within the call to [sqlite3_config].
6605 **
6606 ** The xInit() method is called once for each call to [sqlite3_initialize()]
6607 ** (usually only once during the lifetime of the process). It is passed
6608 ** a copy of the sqlite3_pcache_methods.pArg value. It can be used to set
6609 ** up global structures and mutexes required by the custom page cache
6610 ** implementation. The xShutdown() method is called from within
6611 ** [sqlite3_shutdown()], if the application invokes this API. It can be used
6612 ** to clean up any outstanding resources before process shutdown, if required.
6613 **
6614 ** The xCreate() method is used to construct a new cache instance. The
6615 ** first parameter, szPage, is the size in bytes of the pages that must
6616 ** be allocated by the cache. szPage will not be a power of two. The
6617 ** second argument, bPurgeable, is true if the cache being created will
6618 ** be used to cache database pages read from a file stored on disk, or
6619 ** false if it is used for an in-memory database. The cache implementation
6620 ** does not have to do anything special based on the value of bPurgeable,
6621 ** it is purely advisory.
6622 **
6623 ** The xCachesize() method may be called at any time by SQLite to set the
6624 ** suggested maximum cache-size (number of pages stored by) the cache
6625 ** instance passed as the first argument. This is the value configured using
6626 ** the SQLite "[PRAGMA cache_size]" command. As with the bPurgeable parameter,
6627 ** the implementation is not required to do anything special with this
6628 ** value, it is advisory only.
6629 **
6630 ** The xPagecount() method should return the number of pages currently
6631 ** stored in the cache supplied as an argument.
6632 **
6633 ** The xFetch() method is used to fetch a page and return a pointer to it.
6634 ** A 'page', in this context, is a buffer of szPage bytes aligned at an
6635 ** 8-byte boundary. The page to be fetched is determined by the key. The
6636 ** mimimum key value is 1. After it has been retrieved using xFetch, the page
6637 ** is considered to be pinned.
6638 **
6639 ** If the requested page is already in the page cache, then a pointer to
6640 ** the cached buffer should be returned with its contents intact. If the
6641 ** page is not already in the cache, then the expected behaviour of the
6642 ** cache is determined by the value of the createFlag parameter passed
6643 ** to xFetch, according to the following table:
6644 **
6645 ** <table border=1 width=85% align=center>
6646 ** <tr><th>createFlag<th>Expected Behaviour
6647 ** <tr><td>0<td>NULL should be returned. No new cache entry is created.
6648 ** <tr><td>1<td>If createFlag is set to 1, this indicates that
6649 ** SQLite is holding pinned pages that can be unpinned
6650 ** by writing their contents to the database file (a
6651 ** relatively expensive operation). In this situation the
6652 ** cache implementation has two choices: it can return NULL,
6653 ** in which case SQLite will attempt to unpin one or more
6654 ** pages before re-requesting the same page, or it can
6655 ** allocate a new page and return a pointer to it. If a new
6656 ** page is allocated, then it must be completely zeroed before
6657 ** it is returned.
6658 ** <tr><td>2<td>If createFlag is set to 2, then SQLite is not holding any
6659 ** pinned pages associated with the specific cache passed
6660 ** as the first argument to xFetch() that can be unpinned. The
6661 ** cache implementation should attempt to allocate a new
6662 ** cache entry and return a pointer to it. Again, the new
6663 ** page should be zeroed before it is returned. If the xFetch()
6664 ** method returns NULL when createFlag==2, SQLite assumes that
6665 ** a memory allocation failed and returns SQLITE_NOMEM to the
6666 ** user.
6667 ** </table>
6668 **
6669 ** xUnpin() is called by SQLite with a pointer to a currently pinned page
6670 ** as its second argument. If the third parameter, discard, is non-zero,
6671 ** then the page should be evicted from the cache. In this case SQLite
6672 ** assumes that the next time the page is retrieved from the cache using
6673 ** the xFetch() method, it will be zeroed. If the discard parameter is
6674 ** zero, then the page is considered to be unpinned. The cache implementation
6675 ** may choose to reclaim (free or recycle) unpinned pages at any time.
6676 ** SQLite assumes that next time the page is retrieved from the cache
6677 ** it will either be zeroed, or contain the same data that it did when it
6678 ** was unpinned.
6679 **
6680 ** The cache is not required to perform any reference counting. A single
6681 ** call to xUnpin() unpins the page regardless of the number of prior calls
6682 ** to xFetch().
6683 **
6684 ** The xRekey() method is used to change the key value associated with the
6685 ** page passed as the second argument from oldKey to newKey. If the cache
6686 ** previously contains an entry associated with newKey, it should be
6687 ** discarded. Any prior cache entry associated with newKey is guaranteed not
6688 ** to be pinned.
6689 **
6690 ** When SQLite calls the xTruncate() method, the cache must discard all
6691 ** existing cache entries with page numbers (keys) greater than or equal
6692 ** to the value of the iLimit parameter passed to xTruncate(). If any
6693 ** of these pages are pinned, they are implicitly unpinned, meaning that
6694 ** they can be safely discarded.
6695 **
6696 ** The xDestroy() method is used to delete a cache allocated by xCreate().
6697 ** All resources associated with the specified cache should be freed. After
6698 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6699 ** handle invalid, and will not use it with any other sqlite3_pcache_methods
6700 ** functions.
6701 */
6702 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6703 struct sqlite3_pcache_methods {
6704 void *pArg;
6705 int (*xInit)(void*);
6706 void (*xShutdown)(void*);
6707 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
6708 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6709 int (*xPagecount)(sqlite3_pcache*);
6710 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6711 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6712 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6713 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6714 void (*xDestroy)(sqlite3_pcache*);
6715 };
6716
6717 /*
6718 ** Undo the hack that converts floating point types to integer for
6719 ** builds on processors without floating point support.
6720 */
6721

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button