Fossil SCM
Update the version of SQLite used internally to 3.6.6.1.
Commit
dec2c22d9d0e1d9a7114000f4c5599e2824298c8
Parent
5ebaac3dd40997b…
2 files changed
+2597
-1491
+168
-9
+2597
-1491
| --- src/sqlite3.c | ||
| +++ src/sqlite3.c | ||
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | /****************************************************************************** |
| 2 | 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 | |
| 3 | +** version 3.6.6.1. By combining all the individual C code files into this | |
| 4 | 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| @@ -9,17 +9,17 @@ | ||
| 9 | 9 | ** |
| 10 | 10 | ** This file is all you need to compile SQLite. To use SQLite in other |
| 11 | 11 | ** programs, you need this file and the "sqlite3.h" header file that defines |
| 12 | 12 | ** the programming interface to the SQLite library. (If you do not have |
| 13 | 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 | |
| 14 | +** 6728 lines past this header comment.) Additional code files may be | |
| 15 | 15 | ** needed if you want a wrapper to interface SQLite with your choice of |
| 16 | 16 | ** programming language. The code for the "sqlite3" command-line shell |
| 17 | 17 | ** is also in a separate file. This file contains only code for the core |
| 18 | 18 | ** SQLite library. |
| 19 | 19 | ** |
| 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. | |
| 21 | 21 | */ |
| 22 | 22 | #define SQLITE_CORE 1 |
| 23 | 23 | #define SQLITE_AMALGAMATION 1 |
| 24 | 24 | #ifndef SQLITE_PRIVATE |
| 25 | 25 | # define SQLITE_PRIVATE static |
| @@ -39,11 +39,11 @@ | ||
| 39 | 39 | ** May you share freely, never taking more than you give. |
| 40 | 40 | ** |
| 41 | 41 | ************************************************************************* |
| 42 | 42 | ** Internal interface definitions for SQLite. |
| 43 | 43 | ** |
| 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 $ | |
| 45 | 45 | */ |
| 46 | 46 | #ifndef _SQLITEINT_H_ |
| 47 | 47 | #define _SQLITEINT_H_ |
| 48 | 48 | |
| 49 | 49 | /* |
| @@ -485,11 +485,11 @@ | ||
| 485 | 485 | ** The name of this file under configuration management is "sqlite.h.in". |
| 486 | 486 | ** The makefile makes some minor changes to this file (such as inserting |
| 487 | 487 | ** the version number) and changes its name to "sqlite3.h" as |
| 488 | 488 | ** part of the build process. |
| 489 | 489 | ** |
| 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 $ | |
| 491 | 491 | */ |
| 492 | 492 | #ifndef _SQLITE3_H_ |
| 493 | 493 | #define _SQLITE3_H_ |
| 494 | 494 | #include <stdarg.h> /* Needed for the definition of va_list */ |
| 495 | 495 | |
| @@ -562,12 +562,12 @@ | ||
| 562 | 562 | ** |
| 563 | 563 | ** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer |
| 564 | 564 | ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z |
| 565 | 565 | ** are the major version, minor version, and release number. |
| 566 | 566 | */ |
| 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 | |
| 569 | 569 | |
| 570 | 570 | /* |
| 571 | 571 | ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100> |
| 572 | 572 | ** KEYWORDS: sqlite3_version |
| 573 | 573 | ** |
| @@ -1042,11 +1042,11 @@ | ||
| 1042 | 1042 | ** |
| 1043 | 1043 | ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the |
| 1044 | 1044 | ** sync operation only needs to flush data to mass storage. Inode |
| 1045 | 1045 | ** information need not be flushed. The SQLITE_SYNC_NORMAL flag means |
| 1046 | 1046 | ** 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(). | |
| 1048 | 1048 | */ |
| 1049 | 1049 | #define SQLITE_SYNC_NORMAL 0x00002 |
| 1050 | 1050 | #define SQLITE_SYNC_FULL 0x00003 |
| 1051 | 1051 | #define SQLITE_SYNC_DATAONLY 0x00010 |
| 1052 | 1052 | |
| @@ -1074,11 +1074,11 @@ | ||
| 1074 | 1074 | ** This object defines the methods used to perform various operations |
| 1075 | 1075 | ** against the open file represented by the [sqlite3_file] object. |
| 1076 | 1076 | ** |
| 1077 | 1077 | ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or |
| 1078 | 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] | |
| 1079 | +** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY] | |
| 1080 | 1080 | ** flag may be ORed in to indicate that only the data of the file |
| 1081 | 1081 | ** and not its inode needs to be synced. |
| 1082 | 1082 | ** |
| 1083 | 1083 | ** The integer values to xLock() and xUnlock() are one of |
| 1084 | 1084 | ** <ul> |
| @@ -1729,11 +1729,14 @@ | ||
| 1729 | 1729 | ** scratch buffers or if no scratch buffer space is specified, then SQLite |
| 1730 | 1730 | ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd> |
| 1731 | 1731 | ** |
| 1732 | 1732 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1733 | 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 | |
| 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 | |
| 1735 | 1738 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1736 | 1739 | ** The sz argument must be a power of two between 512 and 32768. The first |
| 1737 | 1740 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1738 | 1741 | ** SQLite will use the memory provided by the first argument to satisfy its |
| 1739 | 1742 | ** memory needs for the first N pages that it adds to cache. If additional |
| @@ -1774,10 +1777,21 @@ | ||
| 1774 | 1777 | ** <dd>This option takes two arguments that determine the default |
| 1775 | 1778 | ** memory allcation lookaside optimization. The first argument is the |
| 1776 | 1779 | ** size of each lookaside buffer slot and the second is the number of |
| 1777 | 1780 | ** slots allocated to each database connection.</dd> |
| 1778 | 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 | +** | |
| 1779 | 1793 | ** </dl> |
| 1780 | 1794 | */ |
| 1781 | 1795 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1782 | 1796 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1783 | 1797 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| @@ -1787,12 +1801,14 @@ | ||
| 1787 | 1801 | #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ |
| 1788 | 1802 | #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ |
| 1789 | 1803 | #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ |
| 1790 | 1804 | #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ |
| 1791 | 1805 | #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. */ | |
| 1793 | 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* */ | |
| 1794 | 1810 | |
| 1795 | 1811 | /* |
| 1796 | 1812 | ** CAPI3REF: Configuration Options {H10170} <S20000> |
| 1797 | 1813 | ** EXPERIMENTAL |
| 1798 | 1814 | ** |
| @@ -2838,11 +2854,11 @@ | ||
| 2838 | 2854 | #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ |
| 2839 | 2855 | #define SQLITE_REINDEX 27 /* Index Name NULL */ |
| 2840 | 2856 | #define SQLITE_ANALYZE 28 /* Table Name NULL */ |
| 2841 | 2857 | #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ |
| 2842 | 2858 | #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 */ | |
| 2844 | 2860 | #define SQLITE_COPY 0 /* No longer used */ |
| 2845 | 2861 | |
| 2846 | 2862 | /* |
| 2847 | 2863 | ** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400> |
| 2848 | 2864 | ** EXPERIMENTAL |
| @@ -4606,11 +4622,11 @@ | ||
| 4606 | 4622 | ** |
| 4607 | 4623 | ** These functions are [deprecated]. In order to maintain |
| 4608 | 4624 | ** backwards compatibility with older code, these functions continue |
| 4609 | 4625 | ** to be supported. However, new applications should avoid |
| 4610 | 4626 | ** 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. | |
| 4612 | 4628 | */ |
| 4613 | 4629 | #ifndef SQLITE_OMIT_DEPRECATED |
| 4614 | 4630 | SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*); |
| 4615 | 4631 | SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*); |
| 4616 | 4632 | SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); |
| @@ -7010,10 +7026,153 @@ | ||
| 7010 | 7026 | ** </dl> |
| 7011 | 7027 | */ |
| 7012 | 7028 | #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 |
| 7013 | 7029 | #define SQLITE_STMTSTATUS_SORT 2 |
| 7014 | 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 | + | |
| 7015 | 7174 | /* |
| 7016 | 7175 | ** Undo the hack that converts floating point types to integer for |
| 7017 | 7176 | ** builds on processors without floating point support. |
| 7018 | 7177 | */ |
| 7019 | 7178 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| @@ -7479,11 +7638,11 @@ | ||
| 7479 | 7638 | |
| 7480 | 7639 | /* |
| 7481 | 7640 | ** A convenience macro that returns the number of elements in |
| 7482 | 7641 | ** an array. |
| 7483 | 7642 | */ |
| 7484 | -#define ArraySize(X) (sizeof(X)/sizeof(X[0])) | |
| 7643 | +#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0]))) | |
| 7485 | 7644 | |
| 7486 | 7645 | /* |
| 7487 | 7646 | ** The following value as a destructor means to use sqlite3DbFree(). |
| 7488 | 7647 | ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT. |
| 7489 | 7648 | */ |
| @@ -7498,11 +7657,11 @@ | ||
| 7498 | 7657 | ** macro is used for this purpose. And instead of referencing the variable |
| 7499 | 7658 | ** directly, we use its constant as a key to lookup the run-time allocated |
| 7500 | 7659 | ** buffer that holds real variable. The constant is also the initializer |
| 7501 | 7660 | ** for the run-time allocated buffer. |
| 7502 | 7661 | ** |
| 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 | |
| 7504 | 7663 | ** macros become no-ops and have zero performance impact. |
| 7505 | 7664 | */ |
| 7506 | 7665 | #ifdef SQLITE_OMIT_WSD |
| 7507 | 7666 | #define SQLITE_WSD const |
| 7508 | 7667 | #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v))) |
| @@ -7513,10 +7672,29 @@ | ||
| 7513 | 7672 | #define SQLITE_WSD |
| 7514 | 7673 | #define GLOBAL(t,v) v |
| 7515 | 7674 | #define sqlite3GlobalConfig sqlite3Config |
| 7516 | 7675 | #endif |
| 7517 | 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 | + | |
| 7518 | 7696 | /* |
| 7519 | 7697 | ** Forward references to structures |
| 7520 | 7698 | */ |
| 7521 | 7699 | typedef struct AggInfo AggInfo; |
| 7522 | 7700 | typedef struct AuthContext AuthContext; |
| @@ -8195,11 +8373,11 @@ | ||
| 8195 | 8373 | ************************************************************************* |
| 8196 | 8374 | ** This header file defines the interface that the sqlite page cache |
| 8197 | 8375 | ** subsystem. The page cache subsystem reads and writes a file a page |
| 8198 | 8376 | ** at a time and provides a journal for rollback. |
| 8199 | 8377 | ** |
| 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 $ | |
| 8201 | 8379 | */ |
| 8202 | 8380 | |
| 8203 | 8381 | #ifndef _PAGER_H_ |
| 8204 | 8382 | #define _PAGER_H_ |
| 8205 | 8383 | |
| @@ -8255,11 +8433,11 @@ | ||
| 8255 | 8433 | /* |
| 8256 | 8434 | ** See source code comments for a detailed description of the following |
| 8257 | 8435 | ** routines: |
| 8258 | 8436 | */ |
| 8259 | 8437 | 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 *); | |
| 8261 | 8439 | SQLITE_PRIVATE void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*)); |
| 8262 | 8440 | SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*); |
| 8263 | 8441 | SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int); |
| 8264 | 8442 | SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); |
| 8265 | 8443 | SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int); |
| @@ -8341,11 +8519,11 @@ | ||
| 8341 | 8519 | ** |
| 8342 | 8520 | ************************************************************************* |
| 8343 | 8521 | ** This header file defines the interface that the sqlite page cache |
| 8344 | 8522 | ** subsystem. |
| 8345 | 8523 | ** |
| 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 $ | |
| 8347 | 8525 | */ |
| 8348 | 8526 | |
| 8349 | 8527 | #ifndef _PCACHE_H_ |
| 8350 | 8528 | |
| 8351 | 8529 | typedef struct PgHdr PgHdr; |
| @@ -8363,29 +8541,23 @@ | ||
| 8363 | 8541 | Pager *pPager; /* The pager this page is part of */ |
| 8364 | 8542 | #ifdef SQLITE_CHECK_PAGES |
| 8365 | 8543 | u32 pageHash; /* Hash of page content */ |
| 8366 | 8544 | #endif |
| 8367 | 8545 | u16 flags; /* PGHDR flags defined below */ |
| 8546 | + | |
| 8368 | 8547 | /********************************************************************** |
| 8369 | 8548 | ** Elements above are public. All that follows is private to pcache.c |
| 8370 | 8549 | ** and should not be accessed by other modules. |
| 8371 | 8550 | */ |
| 8372 | 8551 | i16 nRef; /* Number of users of this page */ |
| 8373 | 8552 | PCache *pCache; /* Cache that owns this page */ |
| 8374 | 8553 | |
| 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 */ | |
| 8383 | 8556 | }; |
| 8384 | 8557 | |
| 8385 | 8558 | /* Bit values for PgHdr.flags */ |
| 8386 | -#define PGHDR_IN_JOURNAL 0x001 /* Page is in rollback journal */ | |
| 8387 | 8559 | #define PGHDR_DIRTY 0x002 /* Page has changed */ |
| 8388 | 8560 | #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before |
| 8389 | 8561 | ** writing this page to the database */ |
| 8390 | 8562 | #define PGHDR_NEED_READ 0x008 /* Content is unread */ |
| 8391 | 8563 | #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */ |
| @@ -8397,12 +8569,10 @@ | ||
| 8397 | 8569 | |
| 8398 | 8570 | /* Page cache buffer management: |
| 8399 | 8571 | ** These routines implement SQLITE_CONFIG_PAGECACHE. |
| 8400 | 8572 | */ |
| 8401 | 8573 | SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n); |
| 8402 | -SQLITE_PRIVATE void *sqlite3PCacheMalloc(int sz); | |
| 8403 | -SQLITE_PRIVATE void sqlite3PCacheFree(void*); | |
| 8404 | 8574 | |
| 8405 | 8575 | /* Create a new pager cache. |
| 8406 | 8576 | ** Under memory stress, invoke xStress to try to make pages clean. |
| 8407 | 8577 | ** Only clean and unpinned pages can be reclaimed. |
| 8408 | 8578 | */ |
| @@ -8445,21 +8615,11 @@ | ||
| 8445 | 8615 | |
| 8446 | 8616 | /* Reset and close the cache object */ |
| 8447 | 8617 | SQLITE_PRIVATE void sqlite3PcacheClose(PCache*); |
| 8448 | 8618 | |
| 8449 | 8619 | /* 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 *); | |
| 8461 | 8621 | |
| 8462 | 8622 | /* Discard the contents of the cache */ |
| 8463 | 8623 | SQLITE_PRIVATE int sqlite3PcacheClear(PCache*); |
| 8464 | 8624 | |
| 8465 | 8625 | /* Return the total number of outstanding page references */ |
| @@ -8472,34 +8632,38 @@ | ||
| 8472 | 8632 | |
| 8473 | 8633 | /* Return the total number of pages stored in the cache */ |
| 8474 | 8634 | SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*); |
| 8475 | 8635 | |
| 8476 | 8636 | #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. | |
| 8480 | 8640 | */ |
| 8481 | -SQLITE_PRIVATE void sqlite3PcacheIterate(PCache *pCache, void (*xIter)(PgHdr *)); | |
| 8641 | +SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)); | |
| 8482 | 8642 | #endif |
| 8483 | 8643 | |
| 8484 | 8644 | /* Set and get the suggested cache-size for the specified pager-cache. |
| 8485 | 8645 | ** |
| 8486 | 8646 | ** If no global maximum is configured, then the system attempts to limit |
| 8487 | 8647 | ** the total number of pages cached by purgeable pager-caches to the sum |
| 8488 | 8648 | ** of the suggested cache-sizes. |
| 8489 | 8649 | */ |
| 8490 | -SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *); | |
| 8491 | 8650 | SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int); |
| 8651 | +#ifdef SQLITE_TEST | |
| 8652 | +SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *); | |
| 8653 | +#endif | |
| 8492 | 8654 | |
| 8493 | 8655 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 8494 | 8656 | /* Try to return memory used by the pcache module to the main memory heap */ |
| 8495 | 8657 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int); |
| 8496 | 8658 | #endif |
| 8497 | 8659 | |
| 8498 | 8660 | #ifdef SQLITE_TEST |
| 8499 | 8661 | SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); |
| 8500 | 8662 | #endif |
| 8663 | + | |
| 8664 | +SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); | |
| 8501 | 8665 | |
| 8502 | 8666 | #endif /* _PCACHE_H_ */ |
| 8503 | 8667 | |
| 8504 | 8668 | /************** End of pcache.h **********************************************/ |
| 8505 | 8669 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| @@ -9017,11 +9181,11 @@ | ||
| 9017 | 9181 | int nextPagesize; /* Pagesize after VACUUM if >0 */ |
| 9018 | 9182 | int nTable; /* Number of tables in the database */ |
| 9019 | 9183 | CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ |
| 9020 | 9184 | i64 lastRowid; /* ROWID of most recent insert (see above) */ |
| 9021 | 9185 | i64 priorNewRowid; /* Last randomly generated ROWID */ |
| 9022 | - int magic; /* Magic number for detect library misuse */ | |
| 9186 | + u32 magic; /* Magic number for detect library misuse */ | |
| 9023 | 9187 | int nChange; /* Value returned by sqlite3_changes() */ |
| 9024 | 9188 | int nTotalChange; /* Value returned by sqlite3_total_changes() */ |
| 9025 | 9189 | sqlite3_mutex *mutex; /* Connection mutex */ |
| 9026 | 9190 | int aLimit[SQLITE_N_LIMIT]; /* Limits */ |
| 9027 | 9191 | struct sqlite3InitInfo { /* Information used during initialization */ |
| @@ -9180,17 +9344,17 @@ | ||
| 9180 | 9344 | ** available as the function user-data (sqlite3_user_data()). The |
| 9181 | 9345 | ** FuncDef.flags variable is set to the value passed as the flags |
| 9182 | 9346 | ** parameter. |
| 9183 | 9347 | */ |
| 9184 | 9348 | #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} | |
| 9186 | 9350 | #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} | |
| 9188 | 9352 | #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} | |
| 9190 | 9354 | #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} | |
| 9192 | 9356 | |
| 9193 | 9357 | |
| 9194 | 9358 | /* |
| 9195 | 9359 | ** Each SQLite module (virtual table definition) is defined by an |
| 9196 | 9360 | ** instance of the following structure, stored in the sqlite3.aModule |
| @@ -9755,10 +9919,15 @@ | ||
| 9755 | 9919 | ** tables in a join to 32 instead of 64. But it also reduces the size |
| 9756 | 9920 | ** of the library by 738 bytes on ix86. |
| 9757 | 9921 | */ |
| 9758 | 9922 | typedef u64 Bitmask; |
| 9759 | 9923 | |
| 9924 | +/* | |
| 9925 | +** The number of bits in a Bitmask. "BMS" means "BitMask Size". | |
| 9926 | +*/ | |
| 9927 | +#define BMS ((int)(sizeof(Bitmask)*8)) | |
| 9928 | + | |
| 9760 | 9929 | /* |
| 9761 | 9930 | ** The following structure describes the FROM clause of a SELECT statement. |
| 9762 | 9931 | ** Each table or subquery in the FROM clause is a separate element of |
| 9763 | 9932 | ** the SrcList.a[] array. |
| 9764 | 9933 | ** |
| @@ -10290,10 +10459,11 @@ | ||
| 10290 | 10459 | int mxStrlen; /* Maximum string length */ |
| 10291 | 10460 | int szLookaside; /* Default lookaside buffer size */ |
| 10292 | 10461 | int nLookaside; /* Default lookaside buffer count */ |
| 10293 | 10462 | sqlite3_mem_methods m; /* Low-level memory allocation interface */ |
| 10294 | 10463 | sqlite3_mutex_methods mutex; /* Low-level mutex interface */ |
| 10464 | + sqlite3_pcache_methods pcache; /* Low-level page-cache interface */ | |
| 10295 | 10465 | void *pHeap; /* Heap storage space */ |
| 10296 | 10466 | int nHeap; /* Size of pHeap[] */ |
| 10297 | 10467 | int mnReq, mxReq; /* Min and max heap requests sizes */ |
| 10298 | 10468 | void *pScratch; /* Scratch memory */ |
| 10299 | 10469 | int szScratch; /* Size of each scratch buffer */ |
| @@ -10304,11 +10474,10 @@ | ||
| 10304 | 10474 | int isInit; /* True after initialization has finished */ |
| 10305 | 10475 | int inProgress; /* True while initialization in progress */ |
| 10306 | 10476 | int isMallocInit; /* True after malloc is initialized */ |
| 10307 | 10477 | sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */ |
| 10308 | 10478 | int nRefInitMutex; /* Number of users of pInitMutex */ |
| 10309 | - int nSmall; /* alloc size threshold used by mem6.c */ | |
| 10310 | 10479 | int mxParserStack; /* maximum depth of the parser stack */ |
| 10311 | 10480 | int sharedCacheEnabled; /* true if shared-cache mode enabled */ |
| 10312 | 10481 | }; |
| 10313 | 10482 | |
| 10314 | 10483 | /* |
| @@ -10387,14 +10556,20 @@ | ||
| 10387 | 10556 | SQLITE_PRIVATE void *sqlite3ScratchMalloc(int); |
| 10388 | 10557 | SQLITE_PRIVATE void sqlite3ScratchFree(void*); |
| 10389 | 10558 | SQLITE_PRIVATE void *sqlite3PageMalloc(int); |
| 10390 | 10559 | SQLITE_PRIVATE void sqlite3PageFree(void*); |
| 10391 | 10560 | SQLITE_PRIVATE void sqlite3MemSetDefault(void); |
| 10392 | -SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void); | |
| 10393 | -SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void); | |
| 10394 | 10561 | SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void)); |
| 10395 | 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 | + | |
| 10396 | 10571 | |
| 10397 | 10572 | #ifndef SQLITE_MUTEX_OMIT |
| 10398 | 10573 | SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void); |
| 10399 | 10574 | SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int); |
| 10400 | 10575 | SQLITE_PRIVATE int sqlite3MutexInit(void); |
| @@ -10477,10 +10652,11 @@ | ||
| 10477 | 10652 | SQLITE_PRIVATE void sqlite3DeleteTable(Table*); |
| 10478 | 10653 | SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int); |
| 10479 | 10654 | SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*); |
| 10480 | 10655 | SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*); |
| 10481 | 10656 | SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*); |
| 10657 | +SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int); | |
| 10482 | 10658 | SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*); |
| 10483 | 10659 | SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*, |
| 10484 | 10660 | Token*, Select*, Expr*, IdList*); |
| 10485 | 10661 | SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *); |
| 10486 | 10662 | SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *); |
| @@ -10549,11 +10725,11 @@ | ||
| 10549 | 10725 | SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int); |
| 10550 | 10726 | SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*); |
| 10551 | 10727 | SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int); |
| 10552 | 10728 | SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int, |
| 10553 | 10729 | 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); | |
| 10555 | 10731 | SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int); |
| 10556 | 10732 | SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int); |
| 10557 | 10733 | SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*); |
| 10558 | 10734 | SQLITE_PRIVATE void sqlite3TokenCopy(sqlite3*,Token*, Token*); |
| 10559 | 10735 | SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*); |
| @@ -10563,11 +10739,10 @@ | ||
| 10563 | 10739 | SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*); |
| 10564 | 10740 | SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int); |
| 10565 | 10741 | SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*); |
| 10566 | 10742 | SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void); |
| 10567 | 10743 | SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void); |
| 10568 | -SQLITE_PRIVATE int sqlite3GetBuiltinFunction(const char *, int, FuncDef **); | |
| 10569 | 10744 | #ifdef SQLITE_DEBUG |
| 10570 | 10745 | SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*); |
| 10571 | 10746 | SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3*); |
| 10572 | 10747 | #else |
| 10573 | 10748 | # define sqlite3SafetyOn(A) 0 |
| @@ -10585,11 +10760,11 @@ | ||
| 10585 | 10760 | SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*, |
| 10586 | 10761 | Expr*,int, int); |
| 10587 | 10762 | SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*); |
| 10588 | 10763 | SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int); |
| 10589 | 10764 | SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*); |
| 10590 | -SQLITE_PRIVATE int sqlite3TriggersExist(Parse*, Table*, int, ExprList*); | |
| 10765 | +SQLITE_PRIVATE int sqlite3TriggersExist(Table*, int, ExprList*); | |
| 10591 | 10766 | SQLITE_PRIVATE int sqlite3CodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int, |
| 10592 | 10767 | int, int, u32*, u32*); |
| 10593 | 10768 | void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*); |
| 10594 | 10769 | SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*); |
| 10595 | 10770 | SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*); |
| @@ -10598,11 +10773,11 @@ | ||
| 10598 | 10773 | SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, int); |
| 10599 | 10774 | SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*); |
| 10600 | 10775 | SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*); |
| 10601 | 10776 | SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*); |
| 10602 | 10777 | #else |
| 10603 | -# define sqlite3TriggersExist(A,B,C,D,E,F) 0 | |
| 10778 | +# define sqlite3TriggersExist(B,C,D,E,F) 0 | |
| 10604 | 10779 | # define sqlite3DeleteTrigger(A,B) |
| 10605 | 10780 | # define sqlite3DropTriggerPtr(A,B) |
| 10606 | 10781 | # define sqlite3UnlinkAndDeleteTrigger(A,B,C) |
| 10607 | 10782 | # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I,J,K) 0 |
| 10608 | 10783 | #endif |
| @@ -10666,12 +10841,12 @@ | ||
| 10666 | 10841 | ** |
| 10667 | 10842 | ** x = getVarint32( A, B ); |
| 10668 | 10843 | ** x = putVarint32( A, B ); |
| 10669 | 10844 | ** |
| 10670 | 10845 | */ |
| 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))) | |
| 10673 | 10848 | #define getVarint sqlite3GetVarint |
| 10674 | 10849 | #define putVarint sqlite3PutVarint |
| 10675 | 10850 | |
| 10676 | 10851 | |
| 10677 | 10852 | SQLITE_PRIVATE void sqlite3IndexAffinityStr(Vdbe *, Index *); |
| @@ -10778,15 +10953,17 @@ | ||
| 10778 | 10953 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 10779 | 10954 | # define sqlite3VtabClear(X) |
| 10780 | 10955 | # define sqlite3VtabSync(X,Y) SQLITE_OK |
| 10781 | 10956 | # define sqlite3VtabRollback(X) |
| 10782 | 10957 | # define sqlite3VtabCommit(X) |
| 10958 | +# define sqlite3VtabInSync(db) 0 | |
| 10783 | 10959 | #else |
| 10784 | 10960 | SQLITE_PRIVATE void sqlite3VtabClear(Table*); |
| 10785 | 10961 | SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **); |
| 10786 | 10962 | SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db); |
| 10787 | 10963 | SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db); |
| 10964 | +# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0) | |
| 10788 | 10965 | #endif |
| 10789 | 10966 | SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*); |
| 10790 | 10967 | SQLITE_PRIVATE void sqlite3VtabLock(sqlite3_vtab*); |
| 10791 | 10968 | SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3*, sqlite3_vtab*); |
| 10792 | 10969 | SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*); |
| @@ -11107,11 +11284,11 @@ | ||
| 11107 | 11284 | ** |
| 11108 | 11285 | ** There is only one exported symbol in this file - the function |
| 11109 | 11286 | ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. |
| 11110 | 11287 | ** All other code has file scope. |
| 11111 | 11288 | ** |
| 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 $ | |
| 11113 | 11290 | ** |
| 11114 | 11291 | ** SQLite processes all times and dates as Julian Day numbers. The |
| 11115 | 11292 | ** dates and times are stored as the number of days since noon |
| 11116 | 11293 | ** in Greenwich on November 24, 4714 B.C. according to the Gregorian |
| 11117 | 11294 | ** calendar system. |
| @@ -11609,11 +11786,11 @@ | ||
| 11609 | 11786 | int rc = 1; |
| 11610 | 11787 | int n; |
| 11611 | 11788 | double r; |
| 11612 | 11789 | char *z, zBuf[30]; |
| 11613 | 11790 | z = zBuf; |
| 11614 | - for(n=0; n<sizeof(zBuf)-1 && zMod[n]; n++){ | |
| 11791 | + for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){ | |
| 11615 | 11792 | z[n] = tolower(zMod[n]); |
| 11616 | 11793 | } |
| 11617 | 11794 | z[n] = 0; |
| 11618 | 11795 | switch( z[0] ){ |
| 11619 | 11796 | #ifndef SQLITE_OMIT_LOCALTIME |
| @@ -11977,11 +12154,11 @@ | ||
| 11977 | 12154 | i++; |
| 11978 | 12155 | } |
| 11979 | 12156 | } |
| 11980 | 12157 | if( n<sizeof(zBuf) ){ |
| 11981 | 12158 | z = zBuf; |
| 11982 | - }else if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
| 12159 | + }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
| 11983 | 12160 | sqlite3_result_error_toobig(context); |
| 11984 | 12161 | return; |
| 11985 | 12162 | }else{ |
| 11986 | 12163 | z = sqlite3DbMallocRaw(db, n); |
| 11987 | 12164 | if( z==0 ){ |
| @@ -12056,13 +12233,14 @@ | ||
| 12056 | 12233 | ** |
| 12057 | 12234 | ** This function returns the same value as time('now'). |
| 12058 | 12235 | */ |
| 12059 | 12236 | static void ctimeFunc( |
| 12060 | 12237 | sqlite3_context *context, |
| 12061 | - int argc, | |
| 12062 | - sqlite3_value **argv | |
| 12238 | + int NotUsed, | |
| 12239 | + sqlite3_value **NotUsed2 | |
| 12063 | 12240 | ){ |
| 12241 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 12064 | 12242 | timeFunc(context, 0, 0); |
| 12065 | 12243 | } |
| 12066 | 12244 | |
| 12067 | 12245 | /* |
| 12068 | 12246 | ** current_date() |
| @@ -12069,13 +12247,14 @@ | ||
| 12069 | 12247 | ** |
| 12070 | 12248 | ** This function returns the same value as date('now'). |
| 12071 | 12249 | */ |
| 12072 | 12250 | static void cdateFunc( |
| 12073 | 12251 | sqlite3_context *context, |
| 12074 | - int argc, | |
| 12075 | - sqlite3_value **argv | |
| 12252 | + int NotUsed, | |
| 12253 | + sqlite3_value **NotUsed2 | |
| 12076 | 12254 | ){ |
| 12255 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 12077 | 12256 | dateFunc(context, 0, 0); |
| 12078 | 12257 | } |
| 12079 | 12258 | |
| 12080 | 12259 | /* |
| 12081 | 12260 | ** current_timestamp() |
| @@ -12082,13 +12261,14 @@ | ||
| 12082 | 12261 | ** |
| 12083 | 12262 | ** This function returns the same value as datetime('now'). |
| 12084 | 12263 | */ |
| 12085 | 12264 | static void ctimestampFunc( |
| 12086 | 12265 | sqlite3_context *context, |
| 12087 | - int argc, | |
| 12088 | - sqlite3_value **argv | |
| 12266 | + int NotUsed, | |
| 12267 | + sqlite3_value **NotUsed2 | |
| 12089 | 12268 | ){ |
| 12269 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 12090 | 12270 | datetimeFunc(context, 0, 0); |
| 12091 | 12271 | } |
| 12092 | 12272 | #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */ |
| 12093 | 12273 | |
| 12094 | 12274 | #ifdef SQLITE_OMIT_DATETIME_FUNCS |
| @@ -12623,11 +12803,11 @@ | ||
| 12623 | 12803 | ** to obtain the memory it needs. |
| 12624 | 12804 | ** |
| 12625 | 12805 | ** This file contains implementations of the low-level memory allocation |
| 12626 | 12806 | ** routines specified in the sqlite3_mem_methods object. |
| 12627 | 12807 | ** |
| 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 $ | |
| 12629 | 12809 | */ |
| 12630 | 12810 | |
| 12631 | 12811 | /* |
| 12632 | 12812 | ** This version of the memory allocator is the default. It is |
| 12633 | 12813 | ** used when no other memory allocator is specified using compile-time |
| @@ -12715,17 +12895,19 @@ | ||
| 12715 | 12895 | |
| 12716 | 12896 | /* |
| 12717 | 12897 | ** Initialize this module. |
| 12718 | 12898 | */ |
| 12719 | 12899 | static int sqlite3MemInit(void *NotUsed){ |
| 12900 | + UNUSED_PARAMETER(NotUsed); | |
| 12720 | 12901 | return SQLITE_OK; |
| 12721 | 12902 | } |
| 12722 | 12903 | |
| 12723 | 12904 | /* |
| 12724 | 12905 | ** Deinitialize this module. |
| 12725 | 12906 | */ |
| 12726 | 12907 | static void sqlite3MemShutdown(void *NotUsed){ |
| 12908 | + UNUSED_PARAMETER(NotUsed); | |
| 12727 | 12909 | return; |
| 12728 | 12910 | } |
| 12729 | 12911 | |
| 12730 | 12912 | /* |
| 12731 | 12913 | ** This routine is the only routine in this file with external linkage. |
| @@ -13213,11 +13395,11 @@ | ||
| 13213 | 13395 | ** be changed. |
| 13214 | 13396 | ** |
| 13215 | 13397 | ** This version of the memory allocation subsystem is included |
| 13216 | 13398 | ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined. |
| 13217 | 13399 | ** |
| 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 $ | |
| 13219 | 13401 | */ |
| 13220 | 13402 | |
| 13221 | 13403 | /* |
| 13222 | 13404 | ** This version of the memory allocator is only built into the library |
| 13223 | 13405 | ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not |
| @@ -13440,11 +13622,11 @@ | ||
| 13440 | 13622 | /* |
| 13441 | 13623 | ** Chunk i is a free chunk that has been unlinked. Adjust its |
| 13442 | 13624 | ** size parameters for check-out and return a pointer to the |
| 13443 | 13625 | ** user portion of the chunk. |
| 13444 | 13626 | */ |
| 13445 | -static void *memsys3Checkout(u32 i, int nBlock){ | |
| 13627 | +static void *memsys3Checkout(u32 i, u32 nBlock){ | |
| 13446 | 13628 | u32 x; |
| 13447 | 13629 | assert( sqlite3_mutex_held(mem3.mutex) ); |
| 13448 | 13630 | assert( i>=1 ); |
| 13449 | 13631 | assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); |
| 13450 | 13632 | assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); |
| @@ -13458,11 +13640,11 @@ | ||
| 13458 | 13640 | /* |
| 13459 | 13641 | ** Carve a piece off of the end of the mem3.iMaster free chunk. |
| 13460 | 13642 | ** Return a pointer to the new allocation. Or, if the master chunk |
| 13461 | 13643 | ** is not large enough, return 0. |
| 13462 | 13644 | */ |
| 13463 | -static void *memsys3FromMaster(int nBlock){ | |
| 13645 | +static void *memsys3FromMaster(u32 nBlock){ | |
| 13464 | 13646 | assert( sqlite3_mutex_held(mem3.mutex) ); |
| 13465 | 13647 | assert( mem3.szMaster>=nBlock ); |
| 13466 | 13648 | if( nBlock>=mem3.szMaster-1 ){ |
| 13467 | 13649 | /* Use the entire master */ |
| 13468 | 13650 | void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster); |
| @@ -13544,12 +13726,12 @@ | ||
| 13544 | 13726 | ** This function assumes that the necessary mutexes, if any, are |
| 13545 | 13727 | ** already held by the caller. Hence "Unsafe". |
| 13546 | 13728 | */ |
| 13547 | 13729 | static void *memsys3MallocUnsafe(int nByte){ |
| 13548 | 13730 | u32 i; |
| 13549 | - int nBlock; | |
| 13550 | - int toFree; | |
| 13731 | + u32 nBlock; | |
| 13732 | + u32 toFree; | |
| 13551 | 13733 | |
| 13552 | 13734 | assert( sqlite3_mutex_held(mem3.mutex) ); |
| 13553 | 13735 | assert( sizeof(Mem3Block)==8 ); |
| 13554 | 13736 | if( nByte<=12 ){ |
| 13555 | 13737 | nBlock = 2; |
| @@ -13741,10 +13923,11 @@ | ||
| 13741 | 13923 | |
| 13742 | 13924 | /* |
| 13743 | 13925 | ** Initialize this module. |
| 13744 | 13926 | */ |
| 13745 | 13927 | static int memsys3Init(void *NotUsed){ |
| 13928 | + UNUSED_PARAMETER(NotUsed); | |
| 13746 | 13929 | if( !sqlite3GlobalConfig.pHeap ){ |
| 13747 | 13930 | return SQLITE_ERROR; |
| 13748 | 13931 | } |
| 13749 | 13932 | |
| 13750 | 13933 | /* Store a pointer to the memory block in global structure mem3. */ |
| @@ -13765,10 +13948,11 @@ | ||
| 13765 | 13948 | |
| 13766 | 13949 | /* |
| 13767 | 13950 | ** Deinitialize this module. |
| 13768 | 13951 | */ |
| 13769 | 13952 | static void memsys3Shutdown(void *NotUsed){ |
| 13953 | + UNUSED_PARAMETER(NotUsed); | |
| 13770 | 13954 | return; |
| 13771 | 13955 | } |
| 13772 | 13956 | |
| 13773 | 13957 | |
| 13774 | 13958 | |
| @@ -13777,11 +13961,11 @@ | ||
| 13777 | 13961 | ** allocations into that log. |
| 13778 | 13962 | */ |
| 13779 | 13963 | SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){ |
| 13780 | 13964 | #ifdef SQLITE_DEBUG |
| 13781 | 13965 | FILE *out; |
| 13782 | - int i, j; | |
| 13966 | + u32 i, j; | |
| 13783 | 13967 | u32 size; |
| 13784 | 13968 | if( zFilename==0 || zFilename[0]==0 ){ |
| 13785 | 13969 | out = stdout; |
| 13786 | 13970 | }else{ |
| 13787 | 13971 | out = fopen(zFilename, "w"); |
| @@ -13842,10 +14026,12 @@ | ||
| 13842 | 14026 | if( out==stdout ){ |
| 13843 | 14027 | fflush(stdout); |
| 13844 | 14028 | }else{ |
| 13845 | 14029 | fclose(out); |
| 13846 | 14030 | } |
| 14031 | +#else | |
| 14032 | + UNUSED_PARAMETER(zFilename); | |
| 13847 | 14033 | #endif |
| 13848 | 14034 | } |
| 13849 | 14035 | |
| 13850 | 14036 | /* |
| 13851 | 14037 | ** This routine is the only routine in this file with external |
| @@ -13899,11 +14085,11 @@ | ||
| 13899 | 14085 | ** be changed. |
| 13900 | 14086 | ** |
| 13901 | 14087 | ** This version of the memory allocation subsystem is included |
| 13902 | 14088 | ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined. |
| 13903 | 14089 | ** |
| 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 $ | |
| 13905 | 14091 | */ |
| 13906 | 14092 | |
| 13907 | 14093 | /* |
| 13908 | 14094 | ** This version of the memory allocator is used only when |
| 13909 | 14095 | ** SQLITE_ENABLE_MEMSYS5 is defined. |
| @@ -14083,16 +14269,15 @@ | ||
| 14083 | 14269 | int iFullSz; /* Size of allocation rounded up to power of 2 */ |
| 14084 | 14270 | int iLogsize; /* Log2 of iFullSz/POW2_MIN */ |
| 14085 | 14271 | |
| 14086 | 14272 | /* Keep track of the maximum allocation request. Even unfulfilled |
| 14087 | 14273 | ** requests are counted */ |
| 14088 | - if( nByte>mem5.maxRequest ){ | |
| 14274 | + if( (u32)nByte>mem5.maxRequest ){ | |
| 14089 | 14275 | mem5.maxRequest = nByte; |
| 14090 | 14276 | } |
| 14091 | 14277 | |
| 14092 | 14278 | /* Round nByte up to the next valid power of two */ |
| 14093 | - if( nByte>POW2_MAX ) return 0; | |
| 14094 | 14279 | for(iFullSz=mem5.nAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){} |
| 14095 | 14280 | |
| 14096 | 14281 | /* Make sure mem5.aiFreelist[iLogsize] contains at least one free |
| 14097 | 14282 | ** block. If not, then split a block of the next larger power of |
| 14098 | 14283 | ** two in order to create a new free block of size iLogsize. |
| @@ -14140,16 +14325,16 @@ | ||
| 14140 | 14325 | assert( ((u8 *)pOld-mem5.zPool)%mem5.nAtom==0 ); |
| 14141 | 14326 | assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 ); |
| 14142 | 14327 | |
| 14143 | 14328 | iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE; |
| 14144 | 14329 | size = 1<<iLogsize; |
| 14145 | - assert( iBlock+size-1<mem5.nBlock ); | |
| 14330 | + assert( iBlock+size-1<(u32)mem5.nBlock ); | |
| 14146 | 14331 | |
| 14147 | 14332 | mem5.aCtrl[iBlock] |= CTRL_FREE; |
| 14148 | 14333 | mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; |
| 14149 | 14334 | assert( mem5.currentCount>0 ); |
| 14150 | - assert( mem5.currentOut>=0 ); | |
| 14335 | + assert( mem5.currentOut>=(size*mem5.nAtom) ); | |
| 14151 | 14336 | mem5.currentCount--; |
| 14152 | 14337 | mem5.currentOut -= size*mem5.nAtom; |
| 14153 | 14338 | assert( mem5.currentOut>0 || mem5.currentCount==0 ); |
| 14154 | 14339 | assert( mem5.currentCount>0 || mem5.currentOut==0 ); |
| 14155 | 14340 | |
| @@ -14255,17 +14440,19 @@ | ||
| 14255 | 14440 | int nByte = sqlite3GlobalConfig.nHeap; |
| 14256 | 14441 | u8 *zByte = (u8 *)sqlite3GlobalConfig.pHeap; |
| 14257 | 14442 | int nMinLog; /* Log of minimum allocation size in bytes*/ |
| 14258 | 14443 | int iOffset; |
| 14259 | 14444 | |
| 14445 | + UNUSED_PARAMETER(NotUsed); | |
| 14446 | + | |
| 14260 | 14447 | if( !zByte ){ |
| 14261 | 14448 | return SQLITE_ERROR; |
| 14262 | 14449 | } |
| 14263 | 14450 | |
| 14264 | 14451 | nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq); |
| 14265 | 14452 | mem5.nAtom = (1<<nMinLog); |
| 14266 | - while( sizeof(Mem5Link)>mem5.nAtom ){ | |
| 14453 | + while( (int)sizeof(Mem5Link)>mem5.nAtom ){ | |
| 14267 | 14454 | mem5.nAtom = mem5.nAtom << 1; |
| 14268 | 14455 | } |
| 14269 | 14456 | |
| 14270 | 14457 | mem5.nBlock = (nByte / (mem5.nAtom+sizeof(u8))); |
| 14271 | 14458 | mem5.zPool = zByte; |
| @@ -14291,10 +14478,11 @@ | ||
| 14291 | 14478 | |
| 14292 | 14479 | /* |
| 14293 | 14480 | ** Deinitialize this module. |
| 14294 | 14481 | */ |
| 14295 | 14482 | static void memsys5Shutdown(void *NotUsed){ |
| 14483 | + UNUSED_PARAMETER(NotUsed); | |
| 14296 | 14484 | return; |
| 14297 | 14485 | } |
| 14298 | 14486 | |
| 14299 | 14487 | /* |
| 14300 | 14488 | ** Open the file indicated and write a log of all unfreed memory |
| @@ -14334,10 +14522,12 @@ | ||
| 14334 | 14522 | if( out==stdout ){ |
| 14335 | 14523 | fflush(stdout); |
| 14336 | 14524 | }else{ |
| 14337 | 14525 | fclose(out); |
| 14338 | 14526 | } |
| 14527 | +#else | |
| 14528 | + UNUSED_PARAMETER(zFilename); | |
| 14339 | 14529 | #endif |
| 14340 | 14530 | } |
| 14341 | 14531 | |
| 14342 | 14532 | /* |
| 14343 | 14533 | ** This routine is the only routine in this file with external |
| @@ -14985,11 +15175,11 @@ | ||
| 14985 | 15175 | ** May you share freely, never taking more than you give. |
| 14986 | 15176 | ** |
| 14987 | 15177 | ************************************************************************* |
| 14988 | 15178 | ** This file contains the C functions that implement mutexes for pthreads |
| 14989 | 15179 | ** |
| 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 $ | |
| 14991 | 15181 | */ |
| 14992 | 15182 | |
| 14993 | 15183 | /* |
| 14994 | 15184 | ** The code in this file is only used if we are compiling threadsafe |
| 14995 | 15185 | ** under unix with pthreads. |
| @@ -15034,11 +15224,11 @@ | ||
| 15034 | 15224 | ** On those platforms where pthread_equal() is not atomic, SQLite |
| 15035 | 15225 | ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to |
| 15036 | 15226 | ** make sure no assert() statements are evaluated and hence these |
| 15037 | 15227 | ** routines are never called. |
| 15038 | 15228 | */ |
| 15039 | -#ifndef NDEBUG | |
| 15229 | +#if !defined(NDEBUG) || defined(SQLITE_DEBUG) | |
| 15040 | 15230 | static int pthreadMutexHeld(sqlite3_mutex *p){ |
| 15041 | 15231 | return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); |
| 15042 | 15232 | } |
| 15043 | 15233 | static int pthreadMutexNotheld(sqlite3_mutex *p){ |
| 15044 | 15234 | return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; |
| @@ -15130,11 +15320,11 @@ | ||
| 15130 | 15320 | } |
| 15131 | 15321 | break; |
| 15132 | 15322 | } |
| 15133 | 15323 | default: { |
| 15134 | 15324 | assert( iType-2 >= 0 ); |
| 15135 | - assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) ); | |
| 15325 | + assert( iType-2 < ArraySize(staticMutexes) ); | |
| 15136 | 15326 | p = &staticMutexes[iType-2]; |
| 15137 | 15327 | p->id = iType; |
| 15138 | 15328 | break; |
| 15139 | 15329 | } |
| 15140 | 15330 | } |
| @@ -15312,11 +15502,11 @@ | ||
| 15312 | 15502 | ** May you share freely, never taking more than you give. |
| 15313 | 15503 | ** |
| 15314 | 15504 | ************************************************************************* |
| 15315 | 15505 | ** This file contains the C functions that implement mutexes for win32 |
| 15316 | 15506 | ** |
| 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 $ | |
| 15318 | 15508 | */ |
| 15319 | 15509 | |
| 15320 | 15510 | /* |
| 15321 | 15511 | ** The code in this file is only used if we are compiling multithreaded |
| 15322 | 15512 | ** on a win32 system. |
| @@ -15341,11 +15531,18 @@ | ||
| 15341 | 15531 | ** the LockFileEx() API. But we can still statically link against that |
| 15342 | 15532 | ** API as long as we don't call it win running Win95/98/ME. A call to |
| 15343 | 15533 | ** this routine is used to determine if the host is Win95/98/ME or |
| 15344 | 15534 | ** WinNT/2K/XP so that we will know whether or not we can safely call |
| 15345 | 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. | |
| 15346 | 15542 | */ |
| 15543 | +#if 0 | |
| 15347 | 15544 | #if SQLITE_OS_WINCE |
| 15348 | 15545 | # define mutexIsNT() (1) |
| 15349 | 15546 | #else |
| 15350 | 15547 | static int mutexIsNT(void){ |
| 15351 | 15548 | static int osType = 0; |
| @@ -15356,11 +15553,11 @@ | ||
| 15356 | 15553 | osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; |
| 15357 | 15554 | } |
| 15358 | 15555 | return osType==2; |
| 15359 | 15556 | } |
| 15360 | 15557 | #endif /* SQLITE_OS_WINCE */ |
| 15361 | - | |
| 15558 | +#endif | |
| 15362 | 15559 | |
| 15363 | 15560 | #ifdef SQLITE_DEBUG |
| 15364 | 15561 | /* |
| 15365 | 15562 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
| 15366 | 15563 | ** intended for use only inside assert() statements. |
| @@ -15559,23 +15756,24 @@ | ||
| 15559 | 15756 | ** |
| 15560 | 15757 | ************************************************************************* |
| 15561 | 15758 | ** |
| 15562 | 15759 | ** Memory allocation functions used throughout sqlite. |
| 15563 | 15760 | ** |
| 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 $ | |
| 15565 | 15762 | */ |
| 15566 | 15763 | |
| 15567 | 15764 | /* |
| 15568 | 15765 | ** This routine runs when the memory allocator sees that the |
| 15569 | 15766 | ** total memory allocation is about to exceed the soft heap |
| 15570 | 15767 | ** limit. |
| 15571 | 15768 | */ |
| 15572 | 15769 | static void softHeapLimitEnforcer( |
| 15573 | 15770 | void *NotUsed, |
| 15574 | - sqlite3_int64 inUse, | |
| 15771 | + sqlite3_int64 NotUsed2, | |
| 15575 | 15772 | int allocSize |
| 15576 | 15773 | ){ |
| 15774 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 15577 | 15775 | sqlite3_release_memory(allocSize); |
| 15578 | 15776 | } |
| 15579 | 15777 | |
| 15580 | 15778 | /* |
| 15581 | 15779 | ** Set the soft heap-size limit for the library. Passing a zero or |
| @@ -15613,10 +15811,11 @@ | ||
| 15613 | 15811 | nRet += sqlite3VdbeReleaseMemory(n); |
| 15614 | 15812 | #endif |
| 15615 | 15813 | nRet += sqlite3PcacheReleaseMemory(n-nRet); |
| 15616 | 15814 | return nRet; |
| 15617 | 15815 | #else |
| 15816 | + UNUSED_PARAMETER(n); | |
| 15618 | 15817 | return SQLITE_OK; |
| 15619 | 15818 | #endif |
| 15620 | 15819 | } |
| 15621 | 15820 | |
| 15622 | 15821 | /* |
| @@ -15646,11 +15845,11 @@ | ||
| 15646 | 15845 | ** sqlite3GlobalConfig.pPage to a block of memory that records |
| 15647 | 15846 | ** which pages are available. |
| 15648 | 15847 | */ |
| 15649 | 15848 | u32 *aScratchFree; |
| 15650 | 15849 | u32 *aPageFree; |
| 15651 | -} mem0 = { 62560955 }; | |
| 15850 | +} mem0 = { 62560955, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
| 15652 | 15851 | |
| 15653 | 15852 | #define mem0 GLOBAL(struct Mem0Global, mem0) |
| 15654 | 15853 | |
| 15655 | 15854 | /* |
| 15656 | 15855 | ** Initialize the memory allocation subsystem. |
| @@ -15931,11 +16130,11 @@ | ||
| 15931 | 16130 | int i; |
| 15932 | 16131 | i = (u8 *)p - (u8 *)sqlite3GlobalConfig.pScratch; |
| 15933 | 16132 | i /= sqlite3GlobalConfig.szScratch; |
| 15934 | 16133 | assert( i>=0 && i<sqlite3GlobalConfig.nScratch ); |
| 15935 | 16134 | sqlite3_mutex_enter(mem0.mutex); |
| 15936 | - assert( mem0.nScratchFree<sqlite3GlobalConfig.nScratch ); | |
| 16135 | + assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); | |
| 15937 | 16136 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
| 15938 | 16137 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
| 15939 | 16138 | sqlite3_mutex_leave(mem0.mutex); |
| 15940 | 16139 | } |
| 15941 | 16140 | } |
| @@ -16339,11 +16538,11 @@ | ||
| 16339 | 16538 | ** the public domain. The original comments are included here for |
| 16340 | 16539 | ** completeness. They are very out-of-date but might be useful as |
| 16341 | 16540 | ** an historical reference. Most of the "enhancements" have been backed |
| 16342 | 16541 | ** out so that the functionality is now the same as standard printf(). |
| 16343 | 16542 | ** |
| 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 $ | |
| 16345 | 16544 | ** |
| 16346 | 16545 | ************************************************************************** |
| 16347 | 16546 | ** |
| 16348 | 16547 | ** The following modules is an enhanced replacement for the "printf" subroutines |
| 16349 | 16548 | ** found in the standard C library. The following enhancements are |
| @@ -16470,11 +16669,10 @@ | ||
| 16470 | 16669 | { 'p', 16, 0, etPOINTER, 0, 1 }, |
| 16471 | 16670 | { 'T', 0, 2, etTOKEN, 0, 0 }, |
| 16472 | 16671 | { 'S', 0, 2, etSRCLIST, 0, 0 }, |
| 16473 | 16672 | { 'r', 10, 3, etORDINAL, 0, 0 }, |
| 16474 | 16673 | }; |
| 16475 | -#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) | |
| 16476 | 16674 | |
| 16477 | 16675 | /* |
| 16478 | 16676 | ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point |
| 16479 | 16677 | ** conversions will work. |
| 16480 | 16678 | */ |
| @@ -16507,11 +16705,11 @@ | ||
| 16507 | 16705 | /* |
| 16508 | 16706 | ** Append N space characters to the given string buffer. |
| 16509 | 16707 | */ |
| 16510 | 16708 | static void appendSpace(StrAccum *pAccum, int N){ |
| 16511 | 16709 | static const char zSpaces[] = " "; |
| 16512 | - while( N>=sizeof(zSpaces)-1 ){ | |
| 16710 | + while( N>=(int)sizeof(zSpaces)-1 ){ | |
| 16513 | 16711 | sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1); |
| 16514 | 16712 | N -= sizeof(zSpaces)-1; |
| 16515 | 16713 | } |
| 16516 | 16714 | if( N>0 ){ |
| 16517 | 16715 | sqlite3StrAccumAppend(pAccum, zSpaces, N); |
| @@ -16670,11 +16868,11 @@ | ||
| 16670 | 16868 | }else{ |
| 16671 | 16869 | flag_long = flag_longlong = 0; |
| 16672 | 16870 | } |
| 16673 | 16871 | /* Fetch the info entry for the field */ |
| 16674 | 16872 | infop = 0; |
| 16675 | - for(idx=0; idx<etNINFO; idx++){ | |
| 16873 | + for(idx=0; idx<ArraySize(fmtinfo); idx++){ | |
| 16676 | 16874 | if( c==fmtinfo[idx].fmttype ){ |
| 16677 | 16875 | infop = &fmtinfo[idx]; |
| 16678 | 16876 | if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ |
| 16679 | 16877 | xtype = infop->type; |
| 16680 | 16878 | }else{ |
| @@ -16979,11 +17177,14 @@ | ||
| 16979 | 17177 | } |
| 16980 | 17178 | needQuote = !isnull && xtype==etSQLESCAPE2; |
| 16981 | 17179 | n += i + 1 + needQuote*2; |
| 16982 | 17180 | if( n>etBUFSIZE ){ |
| 16983 | 17181 | bufpt = zExtra = sqlite3Malloc( n ); |
| 16984 | - if( bufpt==0 ) return; | |
| 17182 | + if( bufpt==0 ){ | |
| 17183 | + pAccum->mallocFailed = 1; | |
| 17184 | + return; | |
| 17185 | + } | |
| 16985 | 17186 | }else{ |
| 16986 | 17187 | bufpt = buf; |
| 16987 | 17188 | } |
| 16988 | 17189 | j = 0; |
| 16989 | 17190 | if( needQuote ) bufpt[j++] = q; |
| @@ -17471,11 +17672,11 @@ | ||
| 17471 | 17672 | ** VDBE. This information used to all be at the top of the single |
| 17472 | 17673 | ** source code file "vdbe.c". When that file became too big (over |
| 17473 | 17674 | ** 6000 lines long) it was split up into several smaller files and |
| 17474 | 17675 | ** this header information was factored out. |
| 17475 | 17676 | ** |
| 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 $ | |
| 17477 | 17678 | */ |
| 17478 | 17679 | #ifndef _VDBEINT_H_ |
| 17479 | 17680 | #define _VDBEINT_H_ |
| 17480 | 17681 | |
| 17481 | 17682 | /* |
| @@ -17752,11 +17953,11 @@ | ||
| 17752 | 17953 | VdbeCursor **apCsr; /* One element of this array for each open cursor */ |
| 17753 | 17954 | int nVar; /* Number of entries in aVar[] */ |
| 17754 | 17955 | Mem *aVar; /* Values for the OP_Variable opcode. */ |
| 17755 | 17956 | char **azVar; /* Name of variables */ |
| 17756 | 17957 | int okVar; /* True if azVar[] has been initialized */ |
| 17757 | - int magic; /* Magic number for sanity checking */ | |
| 17958 | + u32 magic; /* Magic number for sanity checking */ | |
| 17758 | 17959 | int nMem; /* Number of memory locations currently allocated */ |
| 17759 | 17960 | Mem *aMem; /* The memory locations */ |
| 17760 | 17961 | int nCallback; /* Number of callbacks invoked so far */ |
| 17761 | 17962 | int cacheCtr; /* VdbeCursor row cache generation counter */ |
| 17762 | 17963 | Fifo sFifo; /* A list of ROWIDs */ |
| @@ -18392,11 +18593,11 @@ | ||
| 18392 | 18593 | ** Utility functions used throughout sqlite. |
| 18393 | 18594 | ** |
| 18394 | 18595 | ** This file contains functions for allocating memory, comparing |
| 18395 | 18596 | ** strings, and stuff like that. |
| 18396 | 18597 | ** |
| 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 $ | |
| 18398 | 18599 | */ |
| 18399 | 18600 | |
| 18400 | 18601 | |
| 18401 | 18602 | /* |
| 18402 | 18603 | ** Return true if the floating point value is Not a Number (NaN). |
| @@ -18432,11 +18633,11 @@ | ||
| 18432 | 18633 | ** to exceed the SQLITE_LIMIT_LENGTH setting. |
| 18433 | 18634 | */ |
| 18434 | 18635 | SQLITE_PRIVATE int sqlite3Strlen(sqlite3 *db, const char *z){ |
| 18435 | 18636 | const char *z2 = z; |
| 18436 | 18637 | int len; |
| 18437 | - size_t x; | |
| 18638 | + int x; | |
| 18438 | 18639 | while( *z2 ){ z2++; } |
| 18439 | 18640 | x = z2 - z; |
| 18440 | 18641 | len = 0x7fffffff & x; |
| 18441 | 18642 | if( len!=x || len > db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 18442 | 18643 | return db->aLimit[SQLITE_LIMIT_LENGTH]; |
| @@ -19309,19 +19510,19 @@ | ||
| 19309 | 19510 | ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to |
| 19310 | 19511 | ** open properly and is not fit for general use but which can be |
| 19311 | 19512 | ** used as an argument to sqlite3_errmsg() or sqlite3_close(). |
| 19312 | 19513 | */ |
| 19313 | 19514 | SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){ |
| 19314 | - int magic; | |
| 19515 | + u32 magic; | |
| 19315 | 19516 | if( db==0 ) return 0; |
| 19316 | 19517 | magic = db->magic; |
| 19317 | 19518 | if( magic!=SQLITE_MAGIC_OPEN && |
| 19318 | 19519 | magic!=SQLITE_MAGIC_BUSY ) return 0; |
| 19319 | 19520 | return 1; |
| 19320 | 19521 | } |
| 19321 | 19522 | SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ |
| 19322 | - int magic; | |
| 19523 | + u32 magic; | |
| 19323 | 19524 | if( db==0 ) return 0; |
| 19324 | 19525 | magic = db->magic; |
| 19325 | 19526 | if( magic!=SQLITE_MAGIC_SICK && |
| 19326 | 19527 | magic!=SQLITE_MAGIC_OPEN && |
| 19327 | 19528 | magic!=SQLITE_MAGIC_BUSY ) return 0; |
| @@ -19801,11 +20002,11 @@ | ||
| 19801 | 20002 | ** |
| 19802 | 20003 | ****************************************************************************** |
| 19803 | 20004 | ** |
| 19804 | 20005 | ** This file contains code that is specific to OS/2. |
| 19805 | 20006 | ** |
| 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 $ | |
| 19807 | 20008 | */ |
| 19808 | 20009 | |
| 19809 | 20010 | |
| 19810 | 20011 | #if SQLITE_OS_OS2 |
| 19811 | 20012 | |
| @@ -20216,11 +20417,18 @@ | ||
| 20216 | 20417 | if( flags & SQLITE_SYNC_FULL){ |
| 20217 | 20418 | sqlite3_fullsync_count++; |
| 20218 | 20419 | } |
| 20219 | 20420 | sqlite3_sync_count++; |
| 20220 | 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 | |
| 20221 | 20428 | return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR; |
| 20429 | +#endif | |
| 20222 | 20430 | } |
| 20223 | 20431 | |
| 20224 | 20432 | /* |
| 20225 | 20433 | ** Determine the current size of a file in bytes |
| 20226 | 20434 | */ |
| @@ -21149,11 +21357,11 @@ | ||
| 21149 | 21357 | ** |
| 21150 | 21358 | ****************************************************************************** |
| 21151 | 21359 | ** |
| 21152 | 21360 | ** This file contains code that is specific to Unix systems. |
| 21153 | 21361 | ** |
| 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 $ | |
| 21155 | 21363 | */ |
| 21156 | 21364 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
| 21157 | 21365 | |
| 21158 | 21366 | /* |
| 21159 | 21367 | ** If SQLITE_ENABLE_LOCKING_STYLE is defined and is non-zero, then several |
| @@ -21161,11 +21369,12 @@ | ||
| 21161 | 21369 | ** |
| 21162 | 21370 | ** * POSIX locking (the default), |
| 21163 | 21371 | ** * No locking, |
| 21164 | 21372 | ** * Dot-file locking, |
| 21165 | 21373 | ** * flock() locking, |
| 21166 | -** * AFP locking (OSX only). | |
| 21374 | +** * AFP locking (OSX only), | |
| 21375 | +** * Named POSIX semaphores (VXWorks only). | |
| 21167 | 21376 | ** |
| 21168 | 21377 | ** SQLITE_ENABLE_LOCKING_STYLE only works on a Mac. It is turned on by |
| 21169 | 21378 | ** default on a Mac and disabled on all other posix platforms. |
| 21170 | 21379 | */ |
| 21171 | 21380 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
| @@ -21174,10 +21383,20 @@ | ||
| 21174 | 21383 | # else |
| 21175 | 21384 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 21176 | 21385 | # endif |
| 21177 | 21386 | #endif |
| 21178 | 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 | + | |
| 21179 | 21398 | /* |
| 21180 | 21399 | ** These #defines should enable >2GB file support on Posix if the |
| 21181 | 21400 | ** underlying operating system supports it. If the OS lacks |
| 21182 | 21401 | ** large file support, these should be no-ops. |
| 21183 | 21402 | ** |
| @@ -21206,13 +21425,19 @@ | ||
| 21206 | 21425 | #include <unistd.h> |
| 21207 | 21426 | #include <sys/time.h> |
| 21208 | 21427 | #include <errno.h> |
| 21209 | 21428 | |
| 21210 | 21429 | #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 | |
| 21214 | 21439 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 21215 | 21440 | |
| 21216 | 21441 | /* |
| 21217 | 21442 | ** If we are to be thread-safe, include the pthreads header and define |
| 21218 | 21443 | ** the SQLITE_UNIX_THREADS macro. |
| @@ -21257,10 +21482,14 @@ | ||
| 21257 | 21482 | int dirfd; /* File descriptor for the directory */ |
| 21258 | 21483 | #if SQLITE_THREADSAFE |
| 21259 | 21484 | pthread_t tid; /* The thread that "owns" this unixFile */ |
| 21260 | 21485 | #endif |
| 21261 | 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 | |
| 21262 | 21491 | }; |
| 21263 | 21492 | |
| 21264 | 21493 | /* |
| 21265 | 21494 | ** Include code that is common to all os_*.c files |
| 21266 | 21495 | */ |
| @@ -21667,11 +21896,15 @@ | ||
| 21667 | 21896 | ** each others locks then tid is always set to zero. tid is omitted |
| 21668 | 21897 | ** if we compile without threading support. |
| 21669 | 21898 | */ |
| 21670 | 21899 | struct lockKey { |
| 21671 | 21900 | dev_t dev; /* Device number */ |
| 21901 | +#if IS_VXWORKS | |
| 21902 | + void *rnam; /* Realname since inode unusable */ | |
| 21903 | +#else | |
| 21672 | 21904 | ino_t ino; /* Inode number */ |
| 21905 | +#endif | |
| 21673 | 21906 | #if SQLITE_THREADSAFE |
| 21674 | 21907 | pthread_t tid; /* Thread ID or zero if threads can override each other */ |
| 21675 | 21908 | #endif |
| 21676 | 21909 | }; |
| 21677 | 21910 | |
| @@ -21697,11 +21930,15 @@ | ||
| 21697 | 21930 | ** to locate a particular openCnt structure given its inode. This |
| 21698 | 21931 | ** is the same as the lockKey except that the thread ID is omitted. |
| 21699 | 21932 | */ |
| 21700 | 21933 | struct openKey { |
| 21701 | 21934 | dev_t dev; /* Device number */ |
| 21935 | +#if IS_VXWORKS | |
| 21936 | + void *rnam; /* Realname since inode unusable */ | |
| 21937 | +#else | |
| 21702 | 21938 | ino_t ino; /* Inode number */ |
| 21939 | +#endif | |
| 21703 | 21940 | }; |
| 21704 | 21941 | |
| 21705 | 21942 | /* |
| 21706 | 21943 | ** An instance of the following structure is allocated for each open |
| 21707 | 21944 | ** inode. This structure keeps track of the number of locks on that |
| @@ -21713,10 +21950,14 @@ | ||
| 21713 | 21950 | struct openKey key; /* The lookup key */ |
| 21714 | 21951 | int nRef; /* Number of pointers to this structure */ |
| 21715 | 21952 | int nLock; /* Number of outstanding locks */ |
| 21716 | 21953 | int nPending; /* Number of pending close() operations */ |
| 21717 | 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 | |
| 21718 | 21959 | struct openCnt *pNext, *pPrev; /* List of all openCnt objects */ |
| 21719 | 21960 | }; |
| 21720 | 21961 | |
| 21721 | 21962 | /* |
| 21722 | 21963 | ** List of all lockInfo and openCnt objects. This used to be a hash |
| @@ -21724,10 +21965,22 @@ | ||
| 21724 | 21965 | ** never exceeds a few thousand. And lookup is not on a critical |
| 21725 | 21966 | ** path oo a simple linked list will suffice. |
| 21726 | 21967 | */ |
| 21727 | 21968 | static struct lockInfo *lockList = 0; |
| 21728 | 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 | |
| 21729 | 21982 | |
| 21730 | 21983 | /* |
| 21731 | 21984 | ** The locking styles are associated with the different file locking |
| 21732 | 21985 | ** capabilities supported by different file systems. |
| 21733 | 21986 | ** |
| @@ -21737,18 +21990,21 @@ | ||
| 21737 | 21990 | ** DOTLOCK isn't a true locking style, it refers to the use of a special |
| 21738 | 21991 | ** file named the same as the database file with a '.lock' extension, this |
| 21739 | 21992 | ** can be used on file systems that do not offer any reliable file locking |
| 21740 | 21993 | ** NO locking means that no locking will be attempted, this is only used for |
| 21741 | 21994 | ** read-only file systems currently |
| 21995 | +** NAMEDSEM is similar to DOTLOCK but uses a named semaphore instead of an | |
| 21996 | +** indicator file. | |
| 21742 | 21997 | ** UNSUPPORTED means that no locking will be attempted, this is only used for |
| 21743 | 21998 | ** file systems that are known to be unsupported |
| 21744 | 21999 | */ |
| 21745 | 22000 | #define LOCKING_STYLE_POSIX 1 |
| 21746 | 22001 | #define LOCKING_STYLE_NONE 2 |
| 21747 | 22002 | #define LOCKING_STYLE_DOTFILE 3 |
| 21748 | 22003 | #define LOCKING_STYLE_FLOCK 4 |
| 21749 | 22004 | #define LOCKING_STYLE_AFP 5 |
| 22005 | +#define LOCKING_STYLE_NAMEDSEM 6 | |
| 21750 | 22006 | |
| 21751 | 22007 | /* |
| 21752 | 22008 | ** Only set the lastErrno if the error code is a real error and not |
| 21753 | 22009 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 21754 | 22010 | */ |
| @@ -21859,20 +22115,23 @@ | ||
| 21859 | 22115 | return s; |
| 21860 | 22116 | } |
| 21861 | 22117 | #define fcntl lockTrace |
| 21862 | 22118 | #endif /* SQLITE_LOCK_TRACE */ |
| 21863 | 22119 | |
| 22120 | +#ifdef __linux__ | |
| 21864 | 22121 | /* |
| 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 | +*/ | |
| 21871 | 22130 | static void *threadLockingTest(void *pArg){ |
| 21872 | 22131 | 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); | |
| 21874 | 22133 | return pArg; |
| 21875 | 22134 | } |
| 21876 | 22135 | |
| 21877 | 22136 | /* |
| 21878 | 22137 | ** This procedure attempts to determine whether or not threads |
| @@ -21879,30 +22138,43 @@ | ||
| 21879 | 22138 | ** can override each others locks then sets the |
| 21880 | 22139 | ** threadsOverrideEachOthersLocks variable appropriately. |
| 21881 | 22140 | */ |
| 21882 | 22141 | static void testThreadLockingBehavior(int fd_orig){ |
| 21883 | 22142 | 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; | |
| 21886 | 22147 | |
| 21887 | 22148 | fd = dup(fd_orig); |
| 21888 | 22149 | 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); | |
| 21901 | 22163 | 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); | |
| 21903 | 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 | + | |
| 21904 | 22176 | #endif /* SQLITE_THREADSAFE */ |
| 21905 | 22177 | |
| 21906 | 22178 | /* |
| 21907 | 22179 | ** Release a lockInfo structure previously allocated by findLockInfo(). |
| 21908 | 22180 | */ |
| @@ -21947,15 +22219,108 @@ | ||
| 21947 | 22219 | sqlite3_free(pOpen->aPending); |
| 21948 | 22220 | sqlite3_free(pOpen); |
| 21949 | 22221 | } |
| 21950 | 22222 | } |
| 21951 | 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 | |
| 21952 | 22316 | |
| 21953 | 22317 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 21954 | 22318 | /* |
| 21955 | 22319 | ** Tests a byte-range locking query to see if byte range locks are |
| 21956 | 22320 | ** supported, if not we fall back to dotlockLockingStyle. |
| 22321 | +** On vxWorks we fall back to namedsemLockingStyle. | |
| 21957 | 22322 | */ |
| 21958 | 22323 | static int testLockingStyle(int fd){ |
| 21959 | 22324 | struct flock lockInfo; |
| 21960 | 22325 | |
| 21961 | 22326 | /* Test byte-range lock using fcntl(). If the call succeeds, |
| @@ -21968,13 +22333,14 @@ | ||
| 21968 | 22333 | if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) { |
| 21969 | 22334 | return LOCKING_STYLE_POSIX; |
| 21970 | 22335 | } |
| 21971 | 22336 | |
| 21972 | 22337 | /* 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); | |
| 21976 | 22342 | } |
| 21977 | 22343 | #endif |
| 21978 | 22344 | |
| 21979 | 22345 | /* |
| 21980 | 22346 | ** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the |
| @@ -21985,16 +22351,27 @@ | ||
| 21985 | 22351 | ** other systems. |
| 21986 | 22352 | ** |
| 21987 | 22353 | ** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always |
| 21988 | 22354 | ** returns LOCKING_STYLE_POSIX. |
| 21989 | 22355 | */ |
| 22356 | +#if SQLITE_ENABLE_LOCKING_STYLE | |
| 21990 | 22357 | static int detectLockingStyle( |
| 21991 | 22358 | sqlite3_vfs *pVfs, |
| 21992 | 22359 | const char *filePath, |
| 21993 | 22360 | int fd |
| 21994 | 22361 | ){ |
| 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 | |
| 21996 | 22373 | struct Mapping { |
| 21997 | 22374 | const char *zFilesystem; |
| 21998 | 22375 | int eLockingStyle; |
| 21999 | 22376 | } aMap[] = { |
| 22000 | 22377 | { "hfs", LOCKING_STYLE_POSIX }, |
| @@ -22030,13 +22407,16 @@ | ||
| 22030 | 22407 | } |
| 22031 | 22408 | } |
| 22032 | 22409 | |
| 22033 | 22410 | /* Default case. Handles, amongst others, "nfs". */ |
| 22034 | 22411 | return testLockingStyle(fd); |
| 22035 | -#endif | |
| 22412 | +#endif /* if IS_VXWORKS */ | |
| 22036 | 22413 | return LOCKING_STYLE_POSIX; |
| 22037 | 22414 | } |
| 22415 | +#else | |
| 22416 | + #define detectLockingStyle(x,y,z) LOCKING_STYLE_POSIX | |
| 22417 | +#endif /* ifdef SQLITE_ENABLE_LOCKING_STYLE */ | |
| 22038 | 22418 | |
| 22039 | 22419 | /* |
| 22040 | 22420 | ** Given a file descriptor, locate lockInfo and openCnt structures that |
| 22041 | 22421 | ** describes that file descriptor. Create new ones if necessary. The |
| 22042 | 22422 | ** return values might be uninitialized if an error occurs. |
| @@ -22043,10 +22423,13 @@ | ||
| 22043 | 22423 | ** |
| 22044 | 22424 | ** Return an appropriate error code. |
| 22045 | 22425 | */ |
| 22046 | 22426 | static int findLockInfo( |
| 22047 | 22427 | int fd, /* The file descriptor used in the key */ |
| 22428 | +#if IS_VXWORKS | |
| 22429 | + void *rnam, /* vxWorks realname */ | |
| 22430 | +#endif | |
| 22048 | 22431 | struct lockInfo **ppLock, /* Return the lockInfo structure here */ |
| 22049 | 22432 | struct openCnt **ppOpen /* Return the openCnt structure here */ |
| 22050 | 22433 | ){ |
| 22051 | 22434 | int rc; |
| 22052 | 22435 | struct lockKey key1; |
| @@ -22080,20 +22463,28 @@ | ||
| 22080 | 22463 | } |
| 22081 | 22464 | } |
| 22082 | 22465 | |
| 22083 | 22466 | memset(&key1, 0, sizeof(key1)); |
| 22084 | 22467 | key1.dev = statbuf.st_dev; |
| 22468 | +#if IS_VXWORKS | |
| 22469 | + key1.rnam = rnam; | |
| 22470 | +#else | |
| 22085 | 22471 | key1.ino = statbuf.st_ino; |
| 22472 | +#endif | |
| 22086 | 22473 | #if SQLITE_THREADSAFE |
| 22087 | 22474 | if( threadsOverrideEachOthersLocks<0 ){ |
| 22088 | 22475 | testThreadLockingBehavior(fd); |
| 22089 | 22476 | } |
| 22090 | 22477 | key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |
| 22091 | 22478 | #endif |
| 22092 | 22479 | memset(&key2, 0, sizeof(key2)); |
| 22093 | 22480 | key2.dev = statbuf.st_dev; |
| 22481 | +#if IS_VXWORKS | |
| 22482 | + key2.rnam = rnam; | |
| 22483 | +#else | |
| 22094 | 22484 | key2.ino = statbuf.st_ino; |
| 22485 | +#endif | |
| 22095 | 22486 | pLock = lockList; |
| 22096 | 22487 | while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){ |
| 22097 | 22488 | pLock = pLock->pNext; |
| 22098 | 22489 | } |
| 22099 | 22490 | if( pLock==0 ){ |
| @@ -22133,10 +22524,14 @@ | ||
| 22133 | 22524 | pOpen->aPending = 0; |
| 22134 | 22525 | pOpen->pNext = openList; |
| 22135 | 22526 | pOpen->pPrev = 0; |
| 22136 | 22527 | if( openList ) openList->pPrev = pOpen; |
| 22137 | 22528 | openList = pOpen; |
| 22529 | +#if IS_VXWORKS | |
| 22530 | + pOpen->pSem = NULL; | |
| 22531 | + pOpen->aSemName[0] = '\0'; | |
| 22532 | +#endif | |
| 22138 | 22533 | }else{ |
| 22139 | 22534 | pOpen->nRef++; |
| 22140 | 22535 | } |
| 22141 | 22536 | *ppOpen = pOpen; |
| 22142 | 22537 | } |
| @@ -22197,11 +22592,15 @@ | ||
| 22197 | 22592 | OSTRACE4("Transfer ownership of %d from %d to %d\n", |
| 22198 | 22593 | pFile->h, pFile->tid, hSelf); |
| 22199 | 22594 | pFile->tid = hSelf; |
| 22200 | 22595 | if (pFile->pLock != NULL) { |
| 22201 | 22596 | releaseLockInfo(pFile->pLock); |
| 22597 | +#if IS_VXWORKS | |
| 22598 | + rc = findLockInfo(pFile->h, pFile->zRealpath, &pFile->pLock, 0); | |
| 22599 | +#else | |
| 22202 | 22600 | rc = findLockInfo(pFile->h, &pFile->pLock, 0); |
| 22601 | +#endif | |
| 22203 | 22602 | OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h, |
| 22204 | 22603 | locktypeName(pFile->locktype), |
| 22205 | 22604 | locktypeName(pFile->pLock->locktype), pFile->pLock->cnt); |
| 22206 | 22605 | return rc; |
| 22207 | 22606 | } else { |
| @@ -22366,10 +22765,24 @@ | ||
| 22366 | 22765 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 22367 | 22766 | ** or power failure will likely corrupt the database file. |
| 22368 | 22767 | */ |
| 22369 | 22768 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
| 22370 | 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 | |
| 22371 | 22784 | |
| 22372 | 22785 | /* Record the number of times that we do a normal fsync() and |
| 22373 | 22786 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 22374 | 22787 | ** gets called with the correct arguments. |
| 22375 | 22788 | */ |
| @@ -22381,13 +22794,11 @@ | ||
| 22381 | 22794 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 22382 | 22795 | ** no-op |
| 22383 | 22796 | */ |
| 22384 | 22797 | #ifdef SQLITE_NO_SYNC |
| 22385 | 22798 | rc = SQLITE_OK; |
| 22386 | -#else | |
| 22387 | - | |
| 22388 | -#if HAVE_FULLFSYNC | |
| 22799 | +#elif HAVE_FULLFSYNC | |
| 22389 | 22800 | if( fullSync ){ |
| 22390 | 22801 | rc = fcntl(fd, F_FULLFSYNC, 0); |
| 22391 | 22802 | }else{ |
| 22392 | 22803 | rc = 1; |
| 22393 | 22804 | } |
| @@ -22402,16 +22813,21 @@ | ||
| 22402 | 22813 | if( rc ) rc = fsync(fd); |
| 22403 | 22814 | |
| 22404 | 22815 | #else |
| 22405 | 22816 | if( dataOnly ){ |
| 22406 | 22817 | rc = fdatasync(fd); |
| 22818 | + if( IS_VXWORKS && rc==-1 && errno==ENOTSUP ){ | |
| 22819 | + rc = fsync(fd); | |
| 22820 | + } | |
| 22407 | 22821 | }else{ |
| 22408 | 22822 | rc = fsync(fd); |
| 22409 | 22823 | } |
| 22410 | -#endif /* HAVE_FULLFSYNC */ | |
| 22411 | -#endif /* defined(SQLITE_NO_SYNC) */ | |
| 22824 | +#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ | |
| 22412 | 22825 | |
| 22826 | + if( IS_VXWORKS && rc!= -1 ){ | |
| 22827 | + rc = 0; | |
| 22828 | + } | |
| 22413 | 22829 | return rc; |
| 22414 | 22830 | } |
| 22415 | 22831 | |
| 22416 | 22832 | /* |
| 22417 | 22833 | ** Make sure all writes to a particular file are committed to disk. |
| @@ -22996,10 +23412,29 @@ | ||
| 22996 | 23412 | close(pFile->dirfd); |
| 22997 | 23413 | } |
| 22998 | 23414 | if( pFile->h>=0 ){ |
| 22999 | 23415 | close(pFile->h); |
| 23000 | 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 | |
| 23001 | 23436 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
| 23002 | 23437 | OpenCounter(-1); |
| 23003 | 23438 | memset(pFile, 0, sizeof(unixFile)); |
| 23004 | 23439 | } |
| 23005 | 23440 | return SQLITE_OK; |
| @@ -23039,10 +23474,12 @@ | ||
| 23039 | 23474 | return SQLITE_OK; |
| 23040 | 23475 | } |
| 23041 | 23476 | |
| 23042 | 23477 | |
| 23043 | 23478 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 23479 | + | |
| 23480 | +#if !IS_VXWORKS | |
| 23044 | 23481 | #pragma mark AFP Support |
| 23045 | 23482 | |
| 23046 | 23483 | /* |
| 23047 | 23484 | ** The afpLockingContext structure contains all afp lock specific state |
| 23048 | 23485 | */ |
| @@ -23485,10 +23922,12 @@ | ||
| 23485 | 23922 | if( id ){ |
| 23486 | 23923 | flockUnlock(id, NO_LOCK); |
| 23487 | 23924 | } |
| 23488 | 23925 | return closeUnixFile(id); |
| 23489 | 23926 | } |
| 23927 | + | |
| 23928 | +#endif /* !IS_VXWORKS */ | |
| 23490 | 23929 | |
| 23491 | 23930 | #pragma mark Old-School .lock file based locking |
| 23492 | 23931 | |
| 23493 | 23932 | /* Dotlock-style reserved lock checking following the behavior of |
| 23494 | 23933 | ** unixCheckReservedLock, see the unixCheckReservedLock function comments */ |
| @@ -23514,11 +23953,11 @@ | ||
| 23514 | 23953 | if( lstat(zLockFile, &statBuf)==0 ){ |
| 23515 | 23954 | /* file exists, someone else has the lock */ |
| 23516 | 23955 | reserved = 1; |
| 23517 | 23956 | }else{ |
| 23518 | 23957 | /* file does not exist, we could have it if we want it */ |
| 23519 | - int tErrno = errno; | |
| 23958 | + int tErrno = errno; | |
| 23520 | 23959 | if( ENOENT != tErrno ){ |
| 23521 | 23960 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 23522 | 23961 | pFile->lastErrno = tErrno; |
| 23523 | 23962 | } |
| 23524 | 23963 | } |
| @@ -23537,13 +23976,14 @@ | ||
| 23537 | 23976 | |
| 23538 | 23977 | /* if we already have a lock, it is exclusive. |
| 23539 | 23978 | ** Just adjust level and punt on outta here. */ |
| 23540 | 23979 | if (pFile->locktype > NO_LOCK) { |
| 23541 | 23980 | pFile->locktype = locktype; |
| 23542 | - | |
| 23981 | +#if !IS_VXWORKS | |
| 23543 | 23982 | /* Always update the timestamp on the old file */ |
| 23544 | 23983 | utimes(zLockFile, NULL); |
| 23984 | +#endif | |
| 23545 | 23985 | rc = SQLITE_OK; |
| 23546 | 23986 | goto dotlock_end_lock; |
| 23547 | 23987 | } |
| 23548 | 23988 | |
| 23549 | 23989 | /* check to see if lock file already exists */ |
| @@ -23614,44 +24054,179 @@ | ||
| 23614 | 24054 | |
| 23615 | 24055 | /* |
| 23616 | 24056 | ** Close a file. |
| 23617 | 24057 | */ |
| 23618 | 24058 | static int dotlockClose(sqlite3_file *id) { |
| 24059 | + int rc; | |
| 23619 | 24060 | if( id ){ |
| 23620 | 24061 | unixFile *pFile = (unixFile*)id; |
| 23621 | 24062 | dotlockUnlock(id, NO_LOCK); |
| 23622 | 24063 | sqlite3_free(pFile->lockingContext); |
| 23623 | 24064 | } |
| 23624 | - return closeUnixFile(id); | |
| 24065 | + if( IS_VXWORKS ) enterMutex(); | |
| 24066 | + rc = closeUnixFile(id); | |
| 24067 | + if( IS_VXWORKS ) leaveMutex(); | |
| 24068 | + return rc; | |
| 23625 | 24069 | } |
| 23626 | 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 */ | |
| 23627 | 24195 | |
| 23628 | 24196 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 23629 | 24197 | |
| 23630 | 24198 | /* |
| 23631 | 24199 | ** The nolockLockingContext is void |
| 23632 | 24200 | */ |
| 23633 | 24201 | typedef void nolockLockingContext; |
| 23634 | 24202 | |
| 23635 | -static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) { | |
| 24203 | +static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ | |
| 24204 | + UNUSED_PARAMETER(NotUsed); | |
| 23636 | 24205 | *pResOut = 0; |
| 23637 | 24206 | return SQLITE_OK; |
| 23638 | 24207 | } |
| 23639 | 24208 | |
| 23640 | -static int nolockLock(sqlite3_file *id, int locktype) { | |
| 24209 | +static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ | |
| 24210 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 23641 | 24211 | return SQLITE_OK; |
| 23642 | 24212 | } |
| 23643 | 24213 | |
| 23644 | -static int nolockUnlock(sqlite3_file *id, int locktype) { | |
| 24214 | +static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ | |
| 24215 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 23645 | 24216 | return SQLITE_OK; |
| 23646 | 24217 | } |
| 23647 | 24218 | |
| 23648 | 24219 | /* |
| 23649 | 24220 | ** Close a file. |
| 23650 | 24221 | */ |
| 23651 | 24222 | 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; | |
| 23653 | 24228 | } |
| 23654 | 24229 | |
| 23655 | 24230 | |
| 23656 | 24231 | /* |
| 23657 | 24232 | ** Information and control of an open file handle. |
| @@ -23674,18 +24249,20 @@ | ||
| 23674 | 24249 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 23675 | 24250 | ** if two files are created in the same file-system directory (i.e. |
| 23676 | 24251 | ** a database and its journal file) that the sector size will be the |
| 23677 | 24252 | ** same for both. |
| 23678 | 24253 | */ |
| 23679 | -static int unixSectorSize(sqlite3_file *id){ | |
| 24254 | +static int unixSectorSize(sqlite3_file *NotUsed){ | |
| 24255 | + UNUSED_PARAMETER(NotUsed); | |
| 23680 | 24256 | return SQLITE_DEFAULT_SECTOR_SIZE; |
| 23681 | 24257 | } |
| 23682 | 24258 | |
| 23683 | 24259 | /* |
| 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. | |
| 23685 | 24261 | */ |
| 23686 | -static int unixDeviceCharacteristics(sqlite3_file *id){ | |
| 24262 | +static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ | |
| 24263 | + UNUSED_PARAMETER(NotUsed); | |
| 23687 | 24264 | return 0; |
| 23688 | 24265 | } |
| 23689 | 24266 | |
| 23690 | 24267 | /* |
| 23691 | 24268 | ** Initialize the contents of the unixFile structure pointed to by pId. |
| @@ -23699,11 +24276,12 @@ | ||
| 23699 | 24276 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 23700 | 24277 | int h, /* Open file descriptor of file being opened */ |
| 23701 | 24278 | int dirfd, /* Directory file descriptor */ |
| 23702 | 24279 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
| 23703 | 24280 | 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 */ | |
| 23705 | 24283 | ){ |
| 23706 | 24284 | int eLockingStyle; |
| 23707 | 24285 | unixFile *pNew = (unixFile *)pId; |
| 23708 | 24286 | int rc = SQLITE_OK; |
| 23709 | 24287 | |
| @@ -23730,12 +24308,19 @@ | ||
| 23730 | 24308 | static sqlite3_io_methods aIoMethod[] = { |
| 23731 | 24309 | IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock) |
| 23732 | 24310 | ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock) |
| 23733 | 24311 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 23734 | 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 | |
| 23735 | 24318 | ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock) |
| 23736 | 24319 | ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock) |
| 24320 | + ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock) | |
| 24321 | +#endif | |
| 23737 | 24322 | #endif |
| 23738 | 24323 | }; |
| 23739 | 24324 | /* The order of the IOMETHODS macros above is important. It must be the |
| 23740 | 24325 | ** same order as the LOCKING_STYLE numbers |
| 23741 | 24326 | */ |
| @@ -23742,18 +24327,63 @@ | ||
| 23742 | 24327 | assert(LOCKING_STYLE_POSIX==1); |
| 23743 | 24328 | assert(LOCKING_STYLE_NONE==2); |
| 23744 | 24329 | assert(LOCKING_STYLE_DOTFILE==3); |
| 23745 | 24330 | assert(LOCKING_STYLE_FLOCK==4); |
| 23746 | 24331 | assert(LOCKING_STYLE_AFP==5); |
| 24332 | + assert(LOCKING_STYLE_NAMEDSEM==6); | |
| 23747 | 24333 | |
| 23748 | 24334 | assert( pNew->pLock==NULL ); |
| 23749 | 24335 | assert( pNew->pOpen==NULL ); |
| 23750 | 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 | + | |
| 23751 | 24345 | OSTRACE3("OPEN %-3d %s\n", h, zFilename); |
| 23752 | 24346 | pNew->h = h; |
| 23753 | 24347 | pNew->dirfd = dirfd; |
| 23754 | 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 | |
| 23755 | 24385 | |
| 23756 | 24386 | if( noLock ){ |
| 23757 | 24387 | eLockingStyle = LOCKING_STYLE_NONE; |
| 23758 | 24388 | }else{ |
| 23759 | 24389 | eLockingStyle = detectLockingStyle(pVfs, zFilename, h); |
| @@ -23761,16 +24391,22 @@ | ||
| 23761 | 24391 | |
| 23762 | 24392 | switch( eLockingStyle ){ |
| 23763 | 24393 | |
| 23764 | 24394 | case LOCKING_STYLE_POSIX: { |
| 23765 | 24395 | enterMutex(); |
| 24396 | +#if IS_VXWORKS | |
| 24397 | + rc = findLockInfo(h, pNew->zRealpath, &pNew->pLock, &pNew->pOpen); | |
| 24398 | +#else | |
| 23766 | 24399 | rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen); |
| 24400 | +#endif | |
| 23767 | 24401 | leaveMutex(); |
| 23768 | 24402 | break; |
| 23769 | 24403 | } |
| 23770 | 24404 | |
| 23771 | 24405 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 24406 | + | |
| 24407 | +#if !IS_VXWORKS | |
| 23772 | 24408 | case LOCKING_STYLE_AFP: { |
| 23773 | 24409 | /* AFP locking uses the file path so it needs to be included in |
| 23774 | 24410 | ** the afpLockingContext. |
| 23775 | 24411 | */ |
| 23776 | 24412 | afpLockingContext *pCtx; |
| @@ -23784,10 +24420,11 @@ | ||
| 23784 | 24420 | pCtx->filePath = zFilename; |
| 23785 | 24421 | srandomdev(); |
| 23786 | 24422 | } |
| 23787 | 24423 | break; |
| 23788 | 24424 | } |
| 24425 | +#endif | |
| 23789 | 24426 | |
| 23790 | 24427 | case LOCKING_STYLE_DOTFILE: { |
| 23791 | 24428 | /* Dotfile locking uses the file path so it needs to be included in |
| 23792 | 24429 | ** the dotlockLockingContext |
| 23793 | 24430 | */ |
| @@ -23801,18 +24438,49 @@ | ||
| 23801 | 24438 | sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename); |
| 23802 | 24439 | } |
| 23803 | 24440 | pNew->lockingContext = zLockFile; |
| 23804 | 24441 | break; |
| 23805 | 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 | |
| 23806 | 24467 | |
| 23807 | 24468 | case LOCKING_STYLE_FLOCK: |
| 23808 | 24469 | case LOCKING_STYLE_NONE: |
| 23809 | 24470 | break; |
| 23810 | 24471 | #endif |
| 23811 | 24472 | } |
| 23812 | 24473 | |
| 23813 | 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 | |
| 23814 | 24482 | if( rc!=SQLITE_OK ){ |
| 23815 | 24483 | if( dirfd>=0 ) close(dirfd); |
| 23816 | 24484 | close(h); |
| 23817 | 24485 | }else{ |
| 23818 | 24486 | pNew->pMethod = &aIoMethod[eLockingStyle-1]; |
| @@ -23878,11 +24546,11 @@ | ||
| 23878 | 24546 | ** function failing. |
| 23879 | 24547 | */ |
| 23880 | 24548 | SimulateIOError( return SQLITE_IOERR ); |
| 23881 | 24549 | |
| 23882 | 24550 | azDirs[0] = sqlite3_temp_directory; |
| 23883 | - for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ | |
| 24551 | + for(i=0; i<ArraySize(azDirs); i++){ | |
| 23884 | 24552 | if( azDirs[i]==0 ) continue; |
| 23885 | 24553 | if( stat(azDirs[i], &buf) ) continue; |
| 23886 | 24554 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 23887 | 24555 | if( access(azDirs[i], 07) ) continue; |
| 23888 | 24556 | zDir = azDirs[i]; |
| @@ -23890,11 +24558,11 @@ | ||
| 23890 | 24558 | } |
| 23891 | 24559 | |
| 23892 | 24560 | /* Check that the output buffer is large enough for the temporary file |
| 23893 | 24561 | ** name. If it is not, return SQLITE_ERROR. |
| 23894 | 24562 | */ |
| 23895 | - if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){ | |
| 24563 | + if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ | |
| 23896 | 24564 | return SQLITE_ERROR; |
| 23897 | 24565 | } |
| 23898 | 24566 | |
| 23899 | 24567 | do{ |
| 23900 | 24568 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
| @@ -24007,10 +24675,11 @@ | ||
| 24007 | 24675 | if( isCreate ) oflags |= O_CREAT; |
| 24008 | 24676 | if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW); |
| 24009 | 24677 | oflags |= (O_LARGEFILE|O_BINARY); |
| 24010 | 24678 | |
| 24011 | 24679 | fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 24680 | + OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, oflags); | |
| 24012 | 24681 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 24013 | 24682 | /* Failed to open the file for read/write access. Try read-only. */ |
| 24014 | 24683 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 24015 | 24684 | flags |= SQLITE_OPEN_READONLY; |
| 24016 | 24685 | return unixOpen(pVfs, zPath, pFile, flags, pOutFlags); |
| @@ -24017,11 +24686,15 @@ | ||
| 24017 | 24686 | } |
| 24018 | 24687 | if( fd<0 ){ |
| 24019 | 24688 | return SQLITE_CANTOPEN; |
| 24020 | 24689 | } |
| 24021 | 24690 | if( isDelete ){ |
| 24691 | +#if IS_VXWORKS | |
| 24692 | + zPath = zName; | |
| 24693 | +#else | |
| 24022 | 24694 | unlink(zName); |
| 24695 | +#endif | |
| 24023 | 24696 | } |
| 24024 | 24697 | if( pOutFlags ){ |
| 24025 | 24698 | *pOutFlags = flags; |
| 24026 | 24699 | } |
| 24027 | 24700 | |
| @@ -24037,27 +24710,33 @@ | ||
| 24037 | 24710 | #ifdef FD_CLOEXEC |
| 24038 | 24711 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 24039 | 24712 | #endif |
| 24040 | 24713 | |
| 24041 | 24714 | 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); | |
| 24043 | 24716 | } |
| 24044 | 24717 | |
| 24045 | 24718 | /* |
| 24046 | 24719 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 24047 | 24720 | ** the directory after deleting the file. |
| 24048 | 24721 | */ |
| 24049 | -static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ | |
| 24722 | +static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync){ | |
| 24050 | 24723 | int rc = SQLITE_OK; |
| 24724 | + UNUSED_PARAMETER(NotUsed); | |
| 24051 | 24725 | SimulateIOError(return SQLITE_IOERR_DELETE); |
| 24052 | 24726 | unlink(zPath); |
| 24053 | 24727 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 24054 | 24728 | if( dirSync ){ |
| 24055 | 24729 | int fd; |
| 24056 | 24730 | rc = openDirectory(zPath, &fd); |
| 24057 | 24731 | 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 | + { | |
| 24059 | 24738 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 24060 | 24739 | } |
| 24061 | 24740 | close(fd); |
| 24062 | 24741 | } |
| 24063 | 24742 | } |
| @@ -24074,16 +24753,17 @@ | ||
| 24074 | 24753 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 24075 | 24754 | ** |
| 24076 | 24755 | ** Otherwise return 0. |
| 24077 | 24756 | */ |
| 24078 | 24757 | static int unixAccess( |
| 24079 | - sqlite3_vfs *pVfs, | |
| 24758 | + sqlite3_vfs *NotUsed, | |
| 24080 | 24759 | const char *zPath, |
| 24081 | 24760 | int flags, |
| 24082 | 24761 | int *pResOut |
| 24083 | 24762 | ){ |
| 24084 | 24763 | int amode = 0; |
| 24764 | + UNUSED_PARAMETER(NotUsed); | |
| 24085 | 24765 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
| 24086 | 24766 | switch( flags ){ |
| 24087 | 24767 | case SQLITE_ACCESS_EXISTS: |
| 24088 | 24768 | amode = F_OK; |
| 24089 | 24769 | break; |
| @@ -24124,10 +24804,24 @@ | ||
| 24124 | 24804 | ** current working directly has been unlinked. |
| 24125 | 24805 | */ |
| 24126 | 24806 | SimulateIOError( return SQLITE_ERROR ); |
| 24127 | 24807 | |
| 24128 | 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 | |
| 24129 | 24823 | zOut[nOut-1] = '\0'; |
| 24130 | 24824 | if( zPath[0]=='/' ){ |
| 24131 | 24825 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
| 24132 | 24826 | }else{ |
| 24133 | 24827 | int nCwd; |
| @@ -24162,20 +24856,22 @@ | ||
| 24162 | 24856 | zFull[j++] = zFull[i]; |
| 24163 | 24857 | } |
| 24164 | 24858 | zFull[j] = 0; |
| 24165 | 24859 | } |
| 24166 | 24860 | #endif |
| 24861 | +#endif | |
| 24167 | 24862 | } |
| 24168 | 24863 | |
| 24169 | 24864 | |
| 24170 | 24865 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 24171 | 24866 | /* |
| 24172 | 24867 | ** Interfaces for opening a shared library, finding entry points |
| 24173 | 24868 | ** within the shared library, and closing the shared library. |
| 24174 | 24869 | */ |
| 24175 | 24870 | #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); | |
| 24177 | 24873 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 24178 | 24874 | } |
| 24179 | 24875 | |
| 24180 | 24876 | /* |
| 24181 | 24877 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| @@ -24182,23 +24878,26 @@ | ||
| 24182 | 24878 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 24183 | 24879 | ** message is available, it is written to zBufOut. If no error message |
| 24184 | 24880 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 24185 | 24881 | ** error message. |
| 24186 | 24882 | */ |
| 24187 | -static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ | |
| 24883 | +static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ | |
| 24188 | 24884 | char *zErr; |
| 24885 | + UNUSED_PARAMETER(NotUsed); | |
| 24189 | 24886 | enterMutex(); |
| 24190 | 24887 | zErr = dlerror(); |
| 24191 | 24888 | if( zErr ){ |
| 24192 | 24889 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
| 24193 | 24890 | } |
| 24194 | 24891 | leaveMutex(); |
| 24195 | 24892 | } |
| 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); | |
| 24197 | 24895 | return dlsym(pHandle, zSymbol); |
| 24198 | 24896 | } |
| 24199 | -static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){ | |
| 24897 | +static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ | |
| 24898 | + UNUSED_PARAMETER(NotUsed); | |
| 24200 | 24899 | dlclose(pHandle); |
| 24201 | 24900 | } |
| 24202 | 24901 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 24203 | 24902 | #define unixDlOpen 0 |
| 24204 | 24903 | #define unixDlError 0 |
| @@ -24207,13 +24906,13 @@ | ||
| 24207 | 24906 | #endif |
| 24208 | 24907 | |
| 24209 | 24908 | /* |
| 24210 | 24909 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
| 24211 | 24910 | */ |
| 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))); | |
| 24215 | 24914 | |
| 24216 | 24915 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 24217 | 24916 | ** errors. The reports issued by valgrind are incorrect - we would |
| 24218 | 24917 | ** prefer that the randomness be increased by making use of the |
| 24219 | 24918 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| @@ -24234,11 +24933,11 @@ | ||
| 24234 | 24933 | time_t t; |
| 24235 | 24934 | time(&t); |
| 24236 | 24935 | memcpy(zBuf, &t, sizeof(t)); |
| 24237 | 24936 | pid = getpid(); |
| 24238 | 24937 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
| 24239 | - assert( sizeof(t)+sizeof(pid)<=nBuf ); | |
| 24938 | + assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); | |
| 24240 | 24939 | nBuf = sizeof(t) + sizeof(pid); |
| 24241 | 24940 | }else{ |
| 24242 | 24941 | nBuf = read(fd, zBuf, nBuf); |
| 24243 | 24942 | close(fd); |
| 24244 | 24943 | } |
| @@ -24254,19 +24953,27 @@ | ||
| 24254 | 24953 | ** The return value is the number of microseconds of sleep actually |
| 24255 | 24954 | ** requested from the underlying operating system, a number which |
| 24256 | 24955 | ** might be greater than or equal to the argument, but not less |
| 24257 | 24956 | ** than the argument. |
| 24258 | 24957 | */ |
| 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 | |
| 24261 | 24967 | usleep(microseconds); |
| 24262 | 24968 | return microseconds; |
| 24263 | 24969 | #else |
| 24264 | 24970 | int seconds = (microseconds+999999)/1000000; |
| 24265 | 24971 | sleep(seconds); |
| 24266 | 24972 | return seconds*1000000; |
| 24267 | 24973 | #endif |
| 24974 | + UNUSED_PARAMETER(NotUsed); | |
| 24268 | 24975 | } |
| 24269 | 24976 | |
| 24270 | 24977 | /* |
| 24271 | 24978 | ** The following variable, if set to a non-zero value, becomes the result |
| 24272 | 24979 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
| @@ -24278,29 +24985,38 @@ | ||
| 24278 | 24985 | /* |
| 24279 | 24986 | ** Find the current time (in Universal Coordinated Time). Write the |
| 24280 | 24987 | ** current time and date as a Julian Day number into *prNow and |
| 24281 | 24988 | ** return 0. Return 1 if the time and date cannot be found. |
| 24282 | 24989 | */ |
| 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) | |
| 24285 | 24996 | time_t t; |
| 24286 | 24997 | time(&t); |
| 24287 | 24998 | *prNow = t/86400.0 + 2440587.5; |
| 24288 | 24999 | #else |
| 24289 | 25000 | struct timeval sNow; |
| 24290 | 25001 | gettimeofday(&sNow, 0); |
| 24291 | 25002 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; |
| 24292 | 25003 | #endif |
| 25004 | + | |
| 24293 | 25005 | #ifdef SQLITE_TEST |
| 24294 | 25006 | if( sqlite3_current_time ){ |
| 24295 | 25007 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 24296 | 25008 | } |
| 24297 | 25009 | #endif |
| 25010 | + UNUSED_PARAMETER(NotUsed); | |
| 24298 | 25011 | return 0; |
| 24299 | 25012 | } |
| 24300 | 25013 | |
| 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); | |
| 24302 | 25018 | return 0; |
| 24303 | 25019 | } |
| 24304 | 25020 | |
| 24305 | 25021 | /* |
| 24306 | 25022 | ** Initialize the operating system interface. |
| @@ -24338,16 +25054,20 @@ | ||
| 24338 | 25054 | static sqlite3_vfs aVfs[] = { |
| 24339 | 25055 | UNIXVFS("unix-posix", LOCKING_STYLE_POSIX), |
| 24340 | 25056 | UNIXVFS("unix-afp", LOCKING_STYLE_AFP), |
| 24341 | 25057 | UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK), |
| 24342 | 25058 | 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), | |
| 24344 | 25061 | }; |
| 24345 | 25062 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
| 24346 | 25063 | sqlite3_vfs_register(&aVfs[i], 0); |
| 24347 | 25064 | } |
| 24348 | 25065 | #endif |
| 25066 | +#if IS_VXWORKS | |
| 25067 | + sqlite3HashInit(&nameHash, 1); | |
| 25068 | +#endif | |
| 24349 | 25069 | sqlite3_vfs_register(&unixVfs, 1); |
| 24350 | 25070 | return SQLITE_OK; |
| 24351 | 25071 | } |
| 24352 | 25072 | |
| 24353 | 25073 | /* |
| @@ -24373,11 +25093,11 @@ | ||
| 24373 | 25093 | ** |
| 24374 | 25094 | ****************************************************************************** |
| 24375 | 25095 | ** |
| 24376 | 25096 | ** This file contains code that is specific to windows. |
| 24377 | 25097 | ** |
| 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 $ | |
| 24379 | 25099 | */ |
| 24380 | 25100 | #if SQLITE_OS_WIN /* This file is used for windows only */ |
| 24381 | 25101 | |
| 24382 | 25102 | |
| 24383 | 25103 | /* |
| @@ -24844,11 +25564,11 @@ | ||
| 24844 | 25564 | |
| 24845 | 25565 | /* |
| 24846 | 25566 | ** Convert multibyte character string to UTF-8. Space to hold the |
| 24847 | 25567 | ** returned string is obtained from malloc(). |
| 24848 | 25568 | */ |
| 24849 | -static char *mbcsToUtf8(const char *zFilename){ | |
| 25569 | +SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){ | |
| 24850 | 25570 | char *zFilenameUtf8; |
| 24851 | 25571 | WCHAR *zTmpWide; |
| 24852 | 25572 | |
| 24853 | 25573 | zTmpWide = mbcsToUnicode(zFilename); |
| 24854 | 25574 | if( zTmpWide==0 ){ |
| @@ -25342,15 +26062,22 @@ | ||
| 25342 | 26062 | if( flags & SQLITE_SYNC_FULL ){ |
| 25343 | 26063 | sqlite3_fullsync_count++; |
| 25344 | 26064 | } |
| 25345 | 26065 | sqlite3_sync_count++; |
| 25346 | 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 | |
| 25347 | 26073 | if( FlushFileBuffers(pFile->h) ){ |
| 25348 | 26074 | return SQLITE_OK; |
| 25349 | 26075 | }else{ |
| 25350 | 26076 | return SQLITE_IOERR; |
| 25351 | 26077 | } |
| 26078 | +#endif | |
| 25352 | 26079 | } |
| 25353 | 26080 | |
| 25354 | 26081 | /* |
| 25355 | 26082 | ** Determine the current size of a file in bytes |
| 25356 | 26083 | */ |
| @@ -25724,11 +26451,11 @@ | ||
| 25724 | 26451 | #if SQLITE_OS_WINCE==0 |
| 25725 | 26452 | }else{ |
| 25726 | 26453 | char *zUtf8; |
| 25727 | 26454 | char zMbcsPath[MAX_PATH]; |
| 25728 | 26455 | GetTempPathA(MAX_PATH-30, zMbcsPath); |
| 25729 | - zUtf8 = mbcsToUtf8(zMbcsPath); | |
| 26456 | + zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath); | |
| 25730 | 26457 | if( zUtf8 ){ |
| 25731 | 26458 | sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8); |
| 25732 | 26459 | free(zUtf8); |
| 25733 | 26460 | }else{ |
| 25734 | 26461 | return SQLITE_NOMEM; |
| @@ -26065,11 +26792,11 @@ | ||
| 26065 | 26792 | free(zConverted); |
| 26066 | 26793 | return SQLITE_NOMEM; |
| 26067 | 26794 | } |
| 26068 | 26795 | GetFullPathNameA((char*)zConverted, nByte, zTemp, 0); |
| 26069 | 26796 | free(zConverted); |
| 26070 | - zOut = mbcsToUtf8(zTemp); | |
| 26797 | + zOut = sqlite3_win32_mbcs_to_utf8(zTemp); | |
| 26071 | 26798 | free(zTemp); |
| 26072 | 26799 | #endif |
| 26073 | 26800 | } |
| 26074 | 26801 | if( zOut ){ |
| 26075 | 26802 | sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut); |
| @@ -26137,10 +26864,15 @@ | ||
| 26137 | 26864 | /* |
| 26138 | 26865 | ** Write up to nBuf bytes of randomness into zBuf. |
| 26139 | 26866 | */ |
| 26140 | 26867 | static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 26141 | 26868 | int n = 0; |
| 26869 | + UNUSED_PARAMETER(pVfs); | |
| 26870 | +#if defined(SQLITE_TEST) | |
| 26871 | + n = nBuf; | |
| 26872 | + memset(zBuf, 0, nBuf); | |
| 26873 | +#else | |
| 26142 | 26874 | if( sizeof(SYSTEMTIME)<=nBuf-n ){ |
| 26143 | 26875 | SYSTEMTIME x; |
| 26144 | 26876 | GetSystemTime(&x); |
| 26145 | 26877 | memcpy(&zBuf[n], &x, sizeof(x)); |
| 26146 | 26878 | n += sizeof(x); |
| @@ -26159,10 +26891,11 @@ | ||
| 26159 | 26891 | LARGE_INTEGER i; |
| 26160 | 26892 | QueryPerformanceCounter(&i); |
| 26161 | 26893 | memcpy(&zBuf[n], &i, sizeof(i)); |
| 26162 | 26894 | n += sizeof(i); |
| 26163 | 26895 | } |
| 26896 | +#endif | |
| 26164 | 26897 | return n; |
| 26165 | 26898 | } |
| 26166 | 26899 | |
| 26167 | 26900 | |
| 26168 | 26901 | /* |
| @@ -26316,24 +27049,45 @@ | ||
| 26316 | 27049 | ** sometimes grow into tens of thousands or larger. The size of the |
| 26317 | 27050 | ** Bitvec object is the number of pages in the database file at the |
| 26318 | 27051 | ** start of a transaction, and is thus usually less than a few thousand, |
| 26319 | 27052 | ** but can be as large as 2 billion for a really big database. |
| 26320 | 27053 | ** |
| 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 $ | |
| 26322 | 27055 | */ |
| 26323 | 27056 | |
| 27057 | +/* Size of the Bitvec structure in bytes. */ | |
| 26324 | 27058 | #define BITVEC_SZ 512 |
| 27059 | + | |
| 26325 | 27060 | /* Round the union size down to the nearest pointer boundary, since that's how |
| 26326 | 27061 | ** 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. */ | |
| 26331 | 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 | + | |
| 26332 | 27087 | #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *)) |
| 26333 | 27088 | |
| 26334 | -#define BITVEC_HASH(X) (((X)*37)%BITVEC_NINT) | |
| 26335 | 27089 | |
| 26336 | 27090 | /* |
| 26337 | 27091 | ** A bitmap is an instance of the following structure. |
| 26338 | 27092 | ** |
| 26339 | 27093 | ** This bitmap records the existance of zero or more bits |
| @@ -26353,15 +27107,19 @@ | ||
| 26353 | 27107 | ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between |
| 26354 | 27108 | ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized |
| 26355 | 27109 | ** to hold deal with values between 1 and iDivisor. |
| 26356 | 27110 | */ |
| 26357 | 27111 | 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. */ | |
| 26361 | 27119 | union { |
| 26362 | - u8 aBitmap[BITVEC_NCHAR]; /* Bitmap representation */ | |
| 27120 | + BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */ | |
| 26363 | 27121 | u32 aHash[BITVEC_NINT]; /* Hash table representation */ |
| 26364 | 27122 | Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */ |
| 26365 | 27123 | } u; |
| 26366 | 27124 | }; |
| 26367 | 27125 | |
| @@ -26386,20 +27144,23 @@ | ||
| 26386 | 27144 | ** i is out of range, then return false. |
| 26387 | 27145 | */ |
| 26388 | 27146 | SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ |
| 26389 | 27147 | if( p==0 ) return 0; |
| 26390 | 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 | + } | |
| 26391 | 27158 | 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++); | |
| 26401 | 27162 | while( p->u.aHash[h] ){ |
| 26402 | 27163 | if( p->u.aHash[h]==i ) return 1; |
| 26403 | 27164 | h++; |
| 26404 | 27165 | if( h>=BITVEC_NINT ) h = 0; |
| 26405 | 27166 | } |
| @@ -26422,74 +27183,99 @@ | ||
| 26422 | 27183 | SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){ |
| 26423 | 27184 | u32 h; |
| 26424 | 27185 | assert( p!=0 ); |
| 26425 | 27186 | assert( i>0 ); |
| 26426 | 27187 | 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; | |
| 26435 | 27192 | if( p->u.apSub[bin]==0 ){ |
| 26436 | 27193 | sqlite3BeginBenignMalloc(); |
| 26437 | 27194 | p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor ); |
| 26438 | 27195 | sqlite3EndBenignMalloc(); |
| 26439 | 27196 | if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM; |
| 26440 | 27197 | } |
| 26441 | - return sqlite3BitvecSet(p->u.apSub[bin], i); | |
| 27198 | + p = p->u.apSub[bin]; | |
| 26442 | 27199 | } |
| 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 { | |
| 26445 | 27218 | if( p->u.aHash[h]==i ) return SQLITE_OK; |
| 26446 | 27219 | 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: | |
| 26450 | 27226 | if( p->nSet>=BITVEC_MXHASH ){ |
| 26451 | - int j, rc; | |
| 27227 | + unsigned int j; | |
| 27228 | + int rc; | |
| 26452 | 27229 | u32 aiValues[BITVEC_NINT]; |
| 26453 | 27230 | 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)); | |
| 26455 | 27232 | p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR; |
| 26456 | 27233 | rc = sqlite3BitvecSet(p, i); |
| 26457 | 27234 | for(j=0; j<BITVEC_NINT; j++){ |
| 26458 | 27235 | if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]); |
| 26459 | 27236 | } |
| 26460 | 27237 | return rc; |
| 26461 | 27238 | } |
| 27239 | +bitvec_set_end: | |
| 27240 | + p->nSet++; | |
| 26462 | 27241 | p->u.aHash[h] = i; |
| 26463 | 27242 | return SQLITE_OK; |
| 26464 | 27243 | } |
| 26465 | 27244 | |
| 26466 | 27245 | /* |
| 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. | |
| 26469 | 27247 | */ |
| 26470 | 27248 | SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i){ |
| 26471 | 27249 | assert( p!=0 ); |
| 26472 | 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 | + } | |
| 26473 | 27260 | 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))); | |
| 26482 | 27262 | }else{ |
| 26483 | - int j; | |
| 27263 | + unsigned int j; | |
| 26484 | 27264 | u32 aiValues[BITVEC_NINT]; |
| 26485 | 27265 | 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)); | |
| 26487 | 27267 | p->nSet = 0; |
| 26488 | 27268 | 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]; | |
| 26491 | 27277 | } |
| 26492 | 27278 | } |
| 26493 | 27279 | } |
| 26494 | 27280 | } |
| 26495 | 27281 | |
| @@ -26497,11 +27283,11 @@ | ||
| 26497 | 27283 | ** Destroy a bitmap object. Reclaim all memory used. |
| 26498 | 27284 | */ |
| 26499 | 27285 | SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){ |
| 26500 | 27286 | if( p==0 ) return; |
| 26501 | 27287 | if( p->iDivisor ){ |
| 26502 | - int i; | |
| 27288 | + unsigned int i; | |
| 26503 | 27289 | for(i=0; i<BITVEC_NPTR; i++){ |
| 26504 | 27290 | sqlite3BitvecDestroy(p->u.apSub[i]); |
| 26505 | 27291 | } |
| 26506 | 27292 | } |
| 26507 | 27293 | sqlite3_free(p); |
| @@ -26630,106 +27416,30 @@ | ||
| 26630 | 27416 | ** May you share freely, never taking more than you give. |
| 26631 | 27417 | ** |
| 26632 | 27418 | ************************************************************************* |
| 26633 | 27419 | ** This file implements that page cache. |
| 26634 | 27420 | ** |
| 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 $ | |
| 26636 | 27422 | */ |
| 26637 | 27423 | |
| 26638 | 27424 | /* |
| 26639 | 27425 | ** 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 | 27426 | */ |
| 26644 | 27427 | 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 | 27428 | PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ |
| 26651 | 27429 | 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 */ | |
| 26654 | 27431 | int nMax; /* Configured cache size */ |
| 26655 | 27432 | 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 | 27433 | int szPage; /* Size of every page in this cache */ |
| 26664 | 27434 | int szExtra; /* Size of extra space for each page */ |
| 26665 | 27435 | int bPurgeable; /* True if pages are on backing store */ |
| 26666 | 27436 | int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ |
| 26667 | 27437 | 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 | +}; | |
| 26731 | 27441 | |
| 26732 | 27442 | /* |
| 26733 | 27443 | ** Some of the assert() macros in this code are too expensive to run |
| 26734 | 27444 | ** even during normal debugging. Use them only rarely on long-running |
| 26735 | 27445 | ** tests. Enable the expensive asserts using the |
| @@ -26740,52 +27450,10 @@ | ||
| 26740 | 27450 | #else |
| 26741 | 27451 | # define expensive_assert(X) |
| 26742 | 27452 | #endif |
| 26743 | 27453 | |
| 26744 | 27454 | /********************************** 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 | 27455 | |
| 26788 | 27456 | #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT) |
| 26789 | 27457 | /* |
| 26790 | 27458 | ** Check that the pCache->pSynced variable is set correctly. If it |
| 26791 | 27459 | ** is not, either fail an assert or return zero. Otherwise, return |
| @@ -26792,505 +27460,148 @@ | ||
| 26792 | 27460 | ** non-zero. This is only used in debugging builds, as follows: |
| 26793 | 27461 | ** |
| 26794 | 27462 | ** expensive_assert( pcacheCheckSynced(pCache) ); |
| 26795 | 27463 | */ |
| 26796 | 27464 | 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){ | |
| 26799 | 27467 | assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) ); |
| 26800 | 27468 | } |
| 26801 | 27469 | return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0); |
| 26802 | 27470 | } |
| 26803 | 27471 | #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */ |
| 26804 | 27472 | |
| 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; | |
| 27100 | 27539 | 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 | + } | |
| 27227 | 27545 | } |
| 27228 | 27546 | |
| 27229 | 27547 | /*************************************************** General Interfaces ****** |
| 27230 | 27548 | ** |
| 27231 | 27549 | ** Initialize and shutdown the page cache subsystem. Neither of these |
| 27232 | 27550 | ** functions are threadsafe. |
| 27233 | 27551 | */ |
| 27234 | 27552 | 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); | |
| 27245 | 27557 | } |
| 27246 | 27558 | 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 | + } | |
| 27248 | 27562 | } |
| 27249 | 27563 | |
| 27250 | 27564 | /* |
| 27251 | 27565 | ** Return the size in bytes of a PCache object. |
| 27252 | 27566 | */ |
| 27253 | 27567 | SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); } |
| 27254 | 27568 | |
| 27255 | 27569 | /* |
| 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(). | |
| 27258 | 27574 | */ |
| 27259 | 27575 | SQLITE_PRIVATE void sqlite3PcacheOpen( |
| 27260 | 27576 | int szPage, /* Size of every page */ |
| 27261 | 27577 | int szExtra, /* Extra space associated with each page */ |
| 27262 | 27578 | int bPurgeable, /* True if pages are on backing store */ |
| 27263 | 27579 | int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */ |
| 27264 | 27580 | void *pStress, /* Argument to xStress */ |
| 27265 | 27581 | PCache *p /* Preallocated space for the PCache */ |
| 27266 | 27582 | ){ |
| 27267 | - assert( pcache_g.isInit ); | |
| 27268 | 27583 | memset(p, 0, sizeof(PCache)); |
| 27269 | 27584 | p->szPage = szPage; |
| 27270 | 27585 | p->szExtra = szExtra; |
| 27271 | 27586 | p->bPurgeable = bPurgeable; |
| 27272 | 27587 | p->xStress = xStress; |
| 27273 | 27588 | p->pStress = pStress; |
| 27274 | 27589 | p->nMax = 100; |
| 27275 | 27590 | 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 | 27591 | } |
| 27285 | 27592 | |
| 27286 | 27593 | /* |
| 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. | |
| 27289 | 27596 | */ |
| 27290 | 27597 | 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 | + } | |
| 27292 | 27603 | pCache->szPage = szPage; |
| 27293 | 27604 | } |
| 27294 | 27605 | |
| 27295 | 27606 | /* |
| 27296 | 27607 | ** Try to obtain a page from the cache. |
| @@ -27299,96 +27610,106 @@ | ||
| 27299 | 27610 | PCache *pCache, /* Obtain the page from this cache */ |
| 27300 | 27611 | Pgno pgno, /* Page number to obtain */ |
| 27301 | 27612 | int createFlag, /* If true, create page if it does not exist already */ |
| 27302 | 27613 | PgHdr **ppPage /* Write the page here */ |
| 27303 | 27614 | ){ |
| 27304 | - int rc = SQLITE_OK; | |
| 27305 | 27615 | PgHdr *pPage = 0; |
| 27616 | + int eCreate; | |
| 27306 | 27617 | |
| 27307 | - assert( pcache_g.isInit ); | |
| 27308 | 27618 | assert( pCache!=0 ); |
| 27309 | 27619 | 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. | |
| 27364 | 27691 | */ |
| 27365 | 27692 | SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){ |
| 27366 | 27693 | assert( p->nRef>0 ); |
| 27367 | 27694 | p->nRef--; |
| 27368 | 27695 | if( p->nRef==0 ){ |
| 27369 | 27696 | PCache *pCache = p->pCache; |
| 27370 | 27697 | pCache->nRef--; |
| 27371 | 27698 | 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); | |
| 27386 | 27704 | } |
| 27387 | 27705 | } |
| 27388 | 27706 | } |
| 27389 | 27707 | |
| 27708 | +/* | |
| 27709 | +** Increase the reference count of a supplied page by 1. | |
| 27710 | +*/ | |
| 27390 | 27711 | SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){ |
| 27391 | 27712 | assert(p->nRef>0); |
| 27392 | 27713 | p->nRef++; |
| 27393 | 27714 | } |
| 27394 | 27715 | |
| @@ -27398,224 +27719,134 @@ | ||
| 27398 | 27719 | ** page pointed to by p is invalid. |
| 27399 | 27720 | */ |
| 27400 | 27721 | SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){ |
| 27401 | 27722 | PCache *pCache; |
| 27402 | 27723 | assert( p->nRef==1 ); |
| 27403 | - assert( 0==(p->flags&PGHDR_DIRTY) ); | |
| 27724 | + if( p->flags&PGHDR_DIRTY ){ | |
| 27725 | + pcacheRemoveFromDirtyList(p); | |
| 27726 | + } | |
| 27404 | 27727 | pCache = p->pCache; |
| 27405 | 27728 | 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); | |
| 27412 | 27733 | } |
| 27413 | 27734 | |
| 27414 | 27735 | /* |
| 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, | |
| 27416 | 27737 | ** make it so. |
| 27417 | 27738 | */ |
| 27418 | 27739 | SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){ |
| 27419 | 27740 | PCache *pCache; |
| 27420 | 27741 | p->flags &= ~PGHDR_DONT_WRITE; |
| 27421 | - if( p->flags & PGHDR_DIRTY ) return; | |
| 27422 | - assert( (p->flags & PGHDR_DIRTY)==0 ); | |
| 27423 | 27742 | 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 | + } | |
| 27443 | 27748 | } |
| 27444 | 27749 | |
| 27445 | 27750 | /* |
| 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, | |
| 27447 | 27752 | ** make it so. |
| 27448 | 27753 | */ |
| 27449 | 27754 | SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){ |
| 27450 | 27755 | 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 | + } | |
| 27454 | 27761 | } |
| 27455 | 27762 | } |
| 27456 | 27763 | |
| 27457 | 27764 | /* |
| 27458 | 27765 | ** Make every page in the cache clean. |
| 27459 | 27766 | */ |
| 27460 | 27767 | SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){ |
| 27461 | 27768 | PgHdr *p; |
| 27462 | - pcacheEnterMutex(); | |
| 27463 | 27769 | 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 | + } | |
| 27475 | 27772 | } |
| 27476 | 27773 | |
| 27477 | 27774 | /* |
| 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. | |
| 27481 | 27787 | */ |
| 27482 | 27788 | SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ |
| 27789 | + PCache *pCache = p->pCache; | |
| 27483 | 27790 | assert( p->nRef>0 ); |
| 27484 | - pcacheEnterMutex(); | |
| 27485 | - pcacheRemoveFromHash(p); | |
| 27791 | + assert( newPgno>0 ); | |
| 27792 | + sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno); | |
| 27486 | 27793 | 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. | |
| 27523 | 27808 | */ |
| 27524 | 27809 | 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); | |
| 27565 | 27825 | } |
| 27566 | 27826 | } |
| 27567 | 27827 | |
| 27568 | 27828 | /* |
| 27569 | 27829 | ** Close a cache. |
| 27570 | 27830 | */ |
| 27571 | 27831 | 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 | +} | |
| 27602 | 27836 | |
| 27603 | 27837 | /* |
| 27604 | 27838 | ** Discard the contents of the cache. |
| 27605 | 27839 | */ |
| 27606 | 27840 | SQLITE_PRIVATE int sqlite3PcacheClear(PCache *pCache){ |
| 27607 | - assert(pCache->nRef==0); | |
| 27608 | - pcacheEnterMutex(); | |
| 27609 | - pcacheClear(pCache); | |
| 27610 | - pcacheExitMutex(); | |
| 27841 | + sqlite3PcacheTruncate(pCache, 0); | |
| 27611 | 27842 | return SQLITE_OK; |
| 27612 | 27843 | } |
| 27613 | 27844 | |
| 27614 | 27845 | /* |
| 27615 | 27846 | ** 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. | |
| 27617 | 27848 | */ |
| 27618 | 27849 | static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ |
| 27619 | 27850 | PgHdr result, *pTail; |
| 27620 | 27851 | pTail = &result; |
| 27621 | 27852 | while( pA && pB ){ |
| @@ -27639,11 +27870,11 @@ | ||
| 27639 | 27870 | return result.pDirty; |
| 27640 | 27871 | } |
| 27641 | 27872 | |
| 27642 | 27873 | /* |
| 27643 | 27874 | ** 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 | |
| 27645 | 27876 | ** corrupted by this sort. |
| 27646 | 27877 | */ |
| 27647 | 27878 | #define N_SORT_BUCKET_ALLOC 25 |
| 27648 | 27879 | #define N_SORT_BUCKET 25 |
| 27649 | 27880 | #ifdef SQLITE_TEST |
| @@ -27688,101 +27919,762 @@ | ||
| 27688 | 27919 | /* |
| 27689 | 27920 | ** Return a list of all dirty pages in the cache, sorted by page number. |
| 27690 | 27921 | */ |
| 27691 | 27922 | SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){ |
| 27692 | 27923 | 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; | |
| 27695 | 27926 | } |
| 27696 | 27927 | return pcacheSortDirtyList(pCache->pDirty); |
| 27697 | 27928 | } |
| 27698 | 27929 | |
| 27699 | 27930 | /* |
| 27700 | -** Return the total number of outstanding page references. | |
| 27931 | +** Return the total number of referenced pages held by the cache. | |
| 27701 | 27932 | */ |
| 27702 | 27933 | SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){ |
| 27703 | 27934 | return pCache->nRef; |
| 27704 | 27935 | } |
| 27705 | 27936 | |
| 27937 | +/* | |
| 27938 | +** Return the number of references to the page supplied as an argument. | |
| 27939 | +*/ | |
| 27706 | 27940 | SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){ |
| 27707 | 27941 | return p->nRef; |
| 27708 | 27942 | } |
| 27709 | 27943 | |
| 27710 | 27944 | /* |
| 27711 | 27945 | ** Return the total number of pages in the cache. |
| 27712 | 27946 | */ |
| 27713 | 27947 | 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. | |
| 27764 | 27958 | */ |
| 27765 | 27959 | SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){ |
| 27766 | 27960 | return pCache->nMax; |
| 27767 | 27961 | } |
| 27962 | +#endif | |
| 27768 | 27963 | |
| 27769 | 27964 | /* |
| 27770 | 27965 | ** Set the suggested cache-size value. |
| 27771 | 27966 | */ |
| 27772 | 27967 | 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 | 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); | |
| 27784 | 28676 | } |
| 27785 | 28677 | |
| 27786 | 28678 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 27787 | 28679 | /* |
| 27788 | 28680 | ** This function is called to free superfluous dynamically allocated memory |
| @@ -27793,44 +28685,49 @@ | ||
| 27793 | 28685 | ** been released, the function returns. The return value is the total number |
| 27794 | 28686 | ** of bytes of memory released. |
| 27795 | 28687 | */ |
| 27796 | 28688 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 27797 | 28689 | 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(); | |
| 27806 | 28700 | } |
| 27807 | 28701 | return nFree; |
| 27808 | 28702 | } |
| 27809 | 28703 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 27810 | 28704 | |
| 27811 | 28705 | #ifdef SQLITE_TEST |
| 28706 | +/* | |
| 28707 | +** This function is used by test procedures to inspect the internal state | |
| 28708 | +** of the global cache. | |
| 28709 | +*/ | |
| 27812 | 28710 | 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 */ | |
| 27817 | 28715 | ){ |
| 27818 | - PgHdr *p; | |
| 28716 | + PgHdr1 *p; | |
| 27819 | 28717 | int nRecyclable = 0; |
| 27820 | - for(p=pcache_g.pLruHead; p; p=p->pNextLru){ | |
| 28718 | + for(p=pcache1.pLruHead; p; p=p->pLruNext){ | |
| 27821 | 28719 | nRecyclable++; |
| 27822 | 28720 | } |
| 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; | |
| 27827 | 28724 | *pnRecyclable = nRecyclable; |
| 27828 | 28725 | } |
| 27829 | 28726 | #endif |
| 27830 | 28727 | |
| 27831 | -/************** End of pcache.c **********************************************/ | |
| 28728 | +/************** End of pcache1.c *********************************************/ | |
| 27832 | 28729 | /************** Begin file pager.c *******************************************/ |
| 27833 | 28730 | /* |
| 27834 | 28731 | ** 2001 September 15 |
| 27835 | 28732 | ** |
| 27836 | 28733 | ** The author disclaims copyright to this source code. In place of |
| @@ -27848,11 +28745,11 @@ | ||
| 27848 | 28745 | ** is separate from the database file. The pager also implements file |
| 27849 | 28746 | ** locking to prevent two processes from writing the same database |
| 27850 | 28747 | ** file simultaneously, or one process from reading the database while |
| 27851 | 28748 | ** another is writing. |
| 27852 | 28749 | ** |
| 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 $ | |
| 27854 | 28751 | */ |
| 27855 | 28752 | #ifndef SQLITE_OMIT_DISKIO |
| 27856 | 28753 | |
| 27857 | 28754 | /* |
| 27858 | 28755 | ** Macros for troubleshooting. Normally turned off |
| @@ -28005,15 +28902,16 @@ | ||
| 28005 | 28902 | u8 doNotSync; /* Boolean. While true, do not spill the cache */ |
| 28006 | 28903 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
| 28007 | 28904 | u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */ |
| 28008 | 28905 | u8 dbModified; /* True if there are any changes to the Db */ |
| 28009 | 28906 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
| 28907 | + u8 dbSizeValid; /* Set when dbSize is correct */ | |
| 28010 | 28908 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
| 28011 | 28909 | 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() */ | |
| 28015 | 28913 | int nRec; /* Number of pages written to the journal */ |
| 28016 | 28914 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 28017 | 28915 | int stmtNRec; /* Number of records in stmt subjournal */ |
| 28018 | 28916 | int nExtra; /* Add this many bytes to each in-memory page */ |
| 28019 | 28917 | int pageSize; /* Number of bytes in a page */ |
| @@ -28026,11 +28924,12 @@ | ||
| 28026 | 28924 | char *zFilename; /* Name of the database file */ |
| 28027 | 28925 | char *zJournal; /* Name of the journal file */ |
| 28028 | 28926 | char *zDirectory; /* Directory hold database and journal files */ |
| 28029 | 28927 | sqlite3_file *fd, *jfd; /* File descriptors for database and journal */ |
| 28030 | 28928 | 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 */ | |
| 28032 | 28931 | i64 journalOff; /* Current byte offset in the journal file */ |
| 28033 | 28932 | i64 journalHdr; /* Byte offset to previous journal header */ |
| 28034 | 28933 | i64 stmtHdrOff; /* First journal header written this statement */ |
| 28035 | 28934 | i64 stmtCksum; /* cksumInit when statement was started */ |
| 28036 | 28935 | i64 stmtJSize; /* Size of journal at stmt_begin() */ |
| @@ -28124,11 +29023,11 @@ | ||
| 28124 | 29023 | ** used in the journal to signify that the remainder of the journal file |
| 28125 | 29024 | ** is devoted to storing a master journal name - there are no more pages to |
| 28126 | 29025 | ** roll back. See comments for function writeMasterJournal() for details. |
| 28127 | 29026 | */ |
| 28128 | 29027 | /* #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)) | |
| 28130 | 29029 | |
| 28131 | 29030 | /* |
| 28132 | 29031 | ** The maximum legal page number is (2^31 - 1). |
| 28133 | 29032 | */ |
| 28134 | 29033 | #define PAGER_MAX_PGNO 2147483647 |
| @@ -28140,10 +29039,14 @@ | ||
| 28140 | 29039 | */ |
| 28141 | 29040 | static int pageInStatement(PgHdr *pPg){ |
| 28142 | 29041 | Pager *pPager = pPg->pPager; |
| 28143 | 29042 | return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno); |
| 28144 | 29043 | } |
| 29044 | + | |
| 29045 | +static int pageInJournal(PgHdr *pPg){ | |
| 29046 | + return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno); | |
| 29047 | +} | |
| 28145 | 29048 | |
| 28146 | 29049 | /* |
| 28147 | 29050 | ** Read a 32-bit integer from the given file descriptor. Store the integer |
| 28148 | 29051 | ** that is read in *pRes. Return SQLITE_OK if everything worked, or an |
| 28149 | 29052 | ** error code is something goes wrong. |
| @@ -28283,11 +29186,11 @@ | ||
| 28283 | 29186 | return hash; |
| 28284 | 29187 | } |
| 28285 | 29188 | static u32 pager_pagehash(PgHdr *pPage){ |
| 28286 | 29189 | return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData); |
| 28287 | 29190 | } |
| 28288 | -static u32 pager_set_pagehash(PgHdr *pPage){ | |
| 29191 | +static void pager_set_pagehash(PgHdr *pPage){ | |
| 28289 | 29192 | pPage->pageHash = pager_pagehash(pPage); |
| 28290 | 29193 | } |
| 28291 | 29194 | |
| 28292 | 29195 | /* |
| 28293 | 29196 | ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES |
| @@ -28320,11 +29223,11 @@ | ||
| 28320 | 29223 | ** were present in the journal. |
| 28321 | 29224 | ** |
| 28322 | 29225 | ** If no master journal file name is present zMaster[0] is set to 0 and |
| 28323 | 29226 | ** SQLITE_OK returned. |
| 28324 | 29227 | */ |
| 28325 | -static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){ | |
| 29228 | +static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){ | |
| 28326 | 29229 | int rc; |
| 28327 | 29230 | u32 len; |
| 28328 | 29231 | i64 szJ; |
| 28329 | 29232 | u32 cksum; |
| 28330 | 29233 | u32 u; /* Unsigned loop counter */ |
| @@ -28452,12 +29355,12 @@ | ||
| 28452 | 29355 | ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. |
| 28453 | 29356 | */ |
| 28454 | 29357 | static int writeJournalHdr(Pager *pPager){ |
| 28455 | 29358 | int rc = SQLITE_OK; |
| 28456 | 29359 | char *zHeader = pPager->pTmpSpace; |
| 28457 | - int nHeader = pPager->pageSize; | |
| 28458 | - int nWrite; | |
| 29360 | + u32 nHeader = pPager->pageSize; | |
| 29361 | + u32 nWrite; | |
| 28459 | 29362 | |
| 28460 | 29363 | if( nHeader>JOURNAL_HDR_SZ(pPager) ){ |
| 28461 | 29364 | nHeader = JOURNAL_HDR_SZ(pPager); |
| 28462 | 29365 | } |
| 28463 | 29366 | |
| @@ -28712,11 +29615,11 @@ | ||
| 28712 | 29615 | */ |
| 28713 | 29616 | static void pager_unlock(Pager *pPager){ |
| 28714 | 29617 | if( !pPager->exclusiveMode ){ |
| 28715 | 29618 | int rc = osUnlock(pPager->fd, NO_LOCK); |
| 28716 | 29619 | if( rc ) pPager->errCode = rc; |
| 28717 | - pPager->dbSize = -1; | |
| 29620 | + pPager->dbSizeValid = 0; | |
| 28718 | 29621 | IOTRACE(("UNLOCK %p\n", pPager)) |
| 28719 | 29622 | |
| 28720 | 29623 | /* Always close the journal file when dropping the database lock. |
| 28721 | 29624 | ** Otherwise, another connection with journal_mode=delete might |
| 28722 | 29625 | ** delete the file out from under us. |
| @@ -28826,17 +29729,14 @@ | ||
| 28826 | 29729 | } |
| 28827 | 29730 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 28828 | 29731 | pPager->pInJournal = 0; |
| 28829 | 29732 | sqlite3BitvecDestroy(pPager->pAlwaysRollback); |
| 28830 | 29733 | pPager->pAlwaysRollback = 0; |
| 28831 | - sqlite3PcacheCleanAll(pPager->pPCache); | |
| 28832 | 29734 | #ifdef SQLITE_CHECK_PAGES |
| 28833 | - sqlite3PcacheIterate(pPager->pPCache, pager_set_pagehash); | |
| 29735 | + sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); | |
| 28834 | 29736 | #endif |
| 28835 | - sqlite3PcacheClearFlags(pPager->pPCache, | |
| 28836 | - PGHDR_IN_JOURNAL | PGHDR_NEED_SYNC | |
| 28837 | - ); | |
| 29737 | + sqlite3PcacheCleanAll(pPager->pPCache); | |
| 28838 | 29738 | pPager->dirtyCache = 0; |
| 28839 | 29739 | pPager->nRec = 0; |
| 28840 | 29740 | }else{ |
| 28841 | 29741 | assert( pPager->pInJournal==0 ); |
| 28842 | 29742 | } |
| @@ -28850,11 +29750,11 @@ | ||
| 28850 | 29750 | pPager->origDbSize = 0; |
| 28851 | 29751 | pPager->setMaster = 0; |
| 28852 | 29752 | pPager->needSync = 0; |
| 28853 | 29753 | /* lruListSetFirstSynced(pPager); */ |
| 28854 | 29754 | if( !MEMDB ){ |
| 28855 | - pPager->dbSize = -1; | |
| 29755 | + pPager->dbSizeValid = 0; | |
| 28856 | 29756 | } |
| 28857 | 29757 | pPager->dbModified = 0; |
| 28858 | 29758 | |
| 28859 | 29759 | return (rc==SQLITE_OK?rc2:rc); |
| 28860 | 29760 | } |
| @@ -29138,11 +30038,11 @@ | ||
| 29138 | 30038 | ** operating system implementations can get confused if you try to |
| 29139 | 30039 | ** truncate a file to some size that is larger than it currently is, |
| 29140 | 30040 | ** so detect this case and write a single zero byte to the end of the new |
| 29141 | 30041 | ** file instead. |
| 29142 | 30042 | */ |
| 29143 | -static int pager_truncate(Pager *pPager, int nPage){ | |
| 30043 | +static int pager_truncate(Pager *pPager, Pgno nPage){ | |
| 29144 | 30044 | int rc = SQLITE_OK; |
| 29145 | 30045 | if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){ |
| 29146 | 30046 | i64 currentSize, newSize; |
| 29147 | 30047 | rc = sqlite3OsFileSize(pPager->fd, ¤tSize); |
| 29148 | 30048 | newSize = pPager->pageSize*(i64)nPage; |
| @@ -29647,11 +30547,11 @@ | ||
| 29647 | 30547 | } |
| 29648 | 30548 | |
| 29649 | 30549 | /* Open the pager file. |
| 29650 | 30550 | */ |
| 29651 | 30551 | if( zFilename && zFilename[0] && !memDb ){ |
| 29652 | - if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){ | |
| 30552 | + if( nPathname>(pVfs->mxPathname - (int)sizeof("-journal")) ){ | |
| 29653 | 30553 | rc = SQLITE_CANTOPEN; |
| 29654 | 30554 | }else{ |
| 29655 | 30555 | int fout = 0; |
| 29656 | 30556 | rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, |
| 29657 | 30557 | pPager->vfsFlags, &fout); |
| @@ -29738,11 +30638,11 @@ | ||
| 29738 | 30638 | pPager->useJournal = useJournal; |
| 29739 | 30639 | pPager->noReadlock = noReadlock && readOnly; |
| 29740 | 30640 | /* pPager->stmtOpen = 0; */ |
| 29741 | 30641 | /* pPager->stmtInUse = 0; */ |
| 29742 | 30642 | /* pPager->nRef = 0; */ |
| 29743 | - pPager->dbSize = memDb-1; | |
| 30643 | + pPager->dbSizeValid = memDb; | |
| 29744 | 30644 | pPager->pageSize = szPageDflt; |
| 29745 | 30645 | /* pPager->stmtSize = 0; */ |
| 29746 | 30646 | /* pPager->stmtJSize = 0; */ |
| 29747 | 30647 | /* pPager->nPage = 0; */ |
| 29748 | 30648 | pPager->mxPage = 100; |
| @@ -29769,21 +30669,27 @@ | ||
| 29769 | 30669 | assert(pPager->fd->pMethods||tempFile); |
| 29770 | 30670 | setSectorSize(pPager); |
| 29771 | 30671 | if( memDb ){ |
| 29772 | 30672 | pPager->journalMode = PAGER_JOURNALMODE_MEMORY; |
| 29773 | 30673 | } |
| 29774 | - /* pPager->pBusyHandler = 0; */ | |
| 30674 | + /* pPager->xBusyHandler = 0; */ | |
| 30675 | + /* pPager->pBusyHandlerArg = 0; */ | |
| 29775 | 30676 | /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
| 29776 | 30677 | *ppPager = pPager; |
| 29777 | 30678 | return SQLITE_OK; |
| 29778 | 30679 | } |
| 29779 | 30680 | |
| 29780 | 30681 | /* |
| 29781 | 30682 | ** Set the busy handler function. |
| 29782 | 30683 | */ |
| 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; | |
| 29785 | 30691 | } |
| 29786 | 30692 | |
| 29787 | 30693 | /* |
| 29788 | 30694 | ** Set the reinitializer for this pager. If not NULL, the reinitializer |
| 29789 | 30695 | ** is called when the content of a page in cache is restored to its original |
| @@ -29916,11 +30822,11 @@ | ||
| 29916 | 30822 | assert( pPager!=0 ); |
| 29917 | 30823 | if( pPager->errCode ){ |
| 29918 | 30824 | rc = pPager->errCode; |
| 29919 | 30825 | return rc; |
| 29920 | 30826 | } |
| 29921 | - if( pPager->dbSize>=0 ){ | |
| 30827 | + if( pPager->dbSizeValid ){ | |
| 29922 | 30828 | n = pPager->dbSize; |
| 29923 | 30829 | } else { |
| 29924 | 30830 | assert(pPager->fd->pMethods||pPager->tempFile); |
| 29925 | 30831 | if( (pPager->fd->pMethods) |
| 29926 | 30832 | && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){ |
| @@ -29932,10 +30838,11 @@ | ||
| 29932 | 30838 | }else{ |
| 29933 | 30839 | n /= pPager->pageSize; |
| 29934 | 30840 | } |
| 29935 | 30841 | if( pPager->state!=PAGER_UNLOCK ){ |
| 29936 | 30842 | pPager->dbSize = n; |
| 30843 | + pPager->dbSizeValid = 1; | |
| 29937 | 30844 | } |
| 29938 | 30845 | } |
| 29939 | 30846 | if( n==(PENDING_BYTE/pPager->pageSize) ){ |
| 29940 | 30847 | n++; |
| 29941 | 30848 | } |
| @@ -29984,19 +30891,18 @@ | ||
| 29984 | 30891 | assert( PAGER_SHARED==SHARED_LOCK ); |
| 29985 | 30892 | assert( PAGER_RESERVED==RESERVED_LOCK ); |
| 29986 | 30893 | assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK ); |
| 29987 | 30894 | |
| 29988 | 30895 | /* 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 ); | |
| 29990 | 30897 | |
| 29991 | 30898 | if( pPager->state>=locktype ){ |
| 29992 | 30899 | rc = SQLITE_OK; |
| 29993 | 30900 | }else{ |
| 29994 | - if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0; | |
| 29995 | 30901 | do { |
| 29996 | 30902 | rc = sqlite3OsLock(pPager->fd, locktype); |
| 29997 | - }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) ); | |
| 30903 | + }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) ); | |
| 29998 | 30904 | if( rc==SQLITE_OK ){ |
| 29999 | 30905 | pPager->state = locktype; |
| 30000 | 30906 | IOTRACE(("LOCK %p %d\n", pPager, locktype)) |
| 30001 | 30907 | } |
| 30002 | 30908 | } |
| @@ -30011,11 +30917,11 @@ | ||
| 30011 | 30917 | assert( pPager->state>=PAGER_SHARED ); |
| 30012 | 30918 | |
| 30013 | 30919 | sqlite3PagerPagecount(pPager, 0); |
| 30014 | 30920 | if( pPager->errCode ){ |
| 30015 | 30921 | rc = pPager->errCode; |
| 30016 | - }else if( nPage<(unsigned)pPager->dbSize ){ | |
| 30922 | + }else if( nPage<pPager->dbSize ){ | |
| 30017 | 30923 | rc = syncJournal(pPager); |
| 30018 | 30924 | if( rc==SQLITE_OK ){ |
| 30019 | 30925 | /* Get an exclusive lock on the database before truncating. */ |
| 30020 | 30926 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 30021 | 30927 | } |
| @@ -30168,22 +31074,12 @@ | ||
| 30168 | 31074 | } |
| 30169 | 31075 | pPager->needSync = 0; |
| 30170 | 31076 | |
| 30171 | 31077 | /* Erase the needSync flag from every page. |
| 30172 | 31078 | */ |
| 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 | + } | |
| 30185 | 31081 | |
| 30186 | 31082 | return rc; |
| 30187 | 31083 | } |
| 30188 | 31084 | |
| 30189 | 31085 | /* |
| @@ -30531,10 +31427,11 @@ | ||
| 30531 | 31427 | if( pPager->errCode ){ |
| 30532 | 31428 | rc = pPager->errCode; |
| 30533 | 31429 | goto failed; |
| 30534 | 31430 | } |
| 30535 | 31431 | |
| 31432 | + assert( pPager->dbSizeValid ); | |
| 30536 | 31433 | if( pPager->dbSize>0 ){ |
| 30537 | 31434 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
| 30538 | 31435 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
| 30539 | 31436 | if( rc!=SQLITE_OK ){ |
| 30540 | 31437 | goto failed; |
| @@ -30681,13 +31578,10 @@ | ||
| 30681 | 31578 | ** be initialized. |
| 30682 | 31579 | */ |
| 30683 | 31580 | int nMax; |
| 30684 | 31581 | PAGER_INCR(pPager->nMiss); |
| 30685 | 31582 | pPg->pPager = pPager; |
| 30686 | - if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){ | |
| 30687 | - pPg->flags |= PGHDR_IN_JOURNAL; | |
| 30688 | - } | |
| 30689 | 31583 | memset(pPg->pExtra, 0, pPager->nExtra); |
| 30690 | 31584 | |
| 30691 | 31585 | rc = sqlite3PagerPagecount(pPager, &nMax); |
| 30692 | 31586 | if( rc!=SQLITE_OK ){ |
| 30693 | 31587 | sqlite3PagerUnref(pPg); |
| @@ -30887,11 +31781,10 @@ | ||
| 30887 | 31781 | assert( pPg->nRef>0 ); |
| 30888 | 31782 | assert( pPager->state!=PAGER_UNLOCK ); |
| 30889 | 31783 | if( pPager->state==PAGER_SHARED ){ |
| 30890 | 31784 | assert( pPager->pInJournal==0 ); |
| 30891 | 31785 | assert( !MEMDB ); |
| 30892 | - sqlite3PcacheAssertFlags(pPager->pPCache, 0, PGHDR_IN_JOURNAL); | |
| 30893 | 31786 | rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK); |
| 30894 | 31787 | if( rc==SQLITE_OK ){ |
| 30895 | 31788 | pPager->state = PAGER_RESERVED; |
| 30896 | 31789 | if( exFlag ){ |
| 30897 | 31790 | rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| @@ -30996,13 +31889,11 @@ | ||
| 30996 | 31889 | |
| 30997 | 31890 | /* Mark the page as dirty. If the page has already been written |
| 30998 | 31891 | ** to the journal then we can return right away. |
| 30999 | 31892 | */ |
| 31000 | 31893 | 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) ){ | |
| 31004 | 31895 | pPager->dirtyCache = 1; |
| 31005 | 31896 | pPager->dbModified = 1; |
| 31006 | 31897 | }else{ |
| 31007 | 31898 | |
| 31008 | 31899 | /* If we get this far, it means that the page needs to be |
| @@ -31028,12 +31919,12 @@ | ||
| 31028 | 31919 | |
| 31029 | 31920 | /* The transaction journal now exists and we have a RESERVED or an |
| 31030 | 31921 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 31031 | 31922 | ** the transaction journal if it is not there already. |
| 31032 | 31923 | */ |
| 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 ){ | |
| 31035 | 31926 | u32 cksum; |
| 31036 | 31927 | char *pData2; |
| 31037 | 31928 | |
| 31038 | 31929 | /* We should never write to the journal file the page that |
| 31039 | 31930 | ** contains the database locks. The following assert verifies |
| @@ -31083,26 +31974,24 @@ | ||
| 31083 | 31974 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0)); |
| 31084 | 31975 | } |
| 31085 | 31976 | if( pPg->flags&PGHDR_NEED_SYNC ){ |
| 31086 | 31977 | pPager->needSync = 1; |
| 31087 | 31978 | } |
| 31088 | - pPg->flags |= PGHDR_IN_JOURNAL; | |
| 31089 | 31979 | } |
| 31090 | 31980 | |
| 31091 | 31981 | /* If the statement journal is open and the page is not in it, |
| 31092 | 31982 | ** then write the current page to the statement journal. Note that |
| 31093 | 31983 | ** the statement journal format differs from the standard journal format |
| 31094 | 31984 | ** in that it omits the checksums and the header. |
| 31095 | 31985 | */ |
| 31096 | 31986 | if( pPager->stmtInUse |
| 31097 | 31987 | && !pageInStatement(pPg) |
| 31098 | - && (int)pPg->pgno<=pPager->stmtSize | |
| 31988 | + && pPg->pgno<=pPager->stmtSize | |
| 31099 | 31989 | ){ |
| 31100 | 31990 | i64 offset = pPager->stmtNRec*(4+pPager->pageSize); |
| 31101 | 31991 | 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 ); | |
| 31104 | 31993 | rc = write32bits(pPager->stfd, offset, pPg->pgno); |
| 31105 | 31994 | if( rc==SQLITE_OK ){ |
| 31106 | 31995 | rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4); |
| 31107 | 31996 | } |
| 31108 | 31997 | PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno); |
| @@ -31116,13 +32005,13 @@ | ||
| 31116 | 32005 | } |
| 31117 | 32006 | |
| 31118 | 32007 | /* Update the database size and return. |
| 31119 | 32008 | */ |
| 31120 | 32009 | assert( pPager->state>=PAGER_SHARED ); |
| 31121 | - if( pPager->dbSize<(int)pPg->pgno ){ | |
| 32010 | + if( pPager->dbSize<pPg->pgno ){ | |
| 31122 | 32011 | pPager->dbSize = pPg->pgno; |
| 31123 | - if( pPager->dbSize==PENDING_BYTE/pPager->pageSize ){ | |
| 32012 | + if( pPager->dbSize==(PAGER_MJ_PGNO(pPager)-1) ){ | |
| 31124 | 32013 | pPager->dbSize++; |
| 31125 | 32014 | } |
| 31126 | 32015 | } |
| 31127 | 32016 | return rc; |
| 31128 | 32017 | } |
| @@ -31276,11 +32165,11 @@ | ||
| 31276 | 32165 | } |
| 31277 | 32166 | rc = sqlite3BitvecSet(pPager->pAlwaysRollback, pPg->pgno); |
| 31278 | 32167 | |
| 31279 | 32168 | if( rc==SQLITE_OK && (pPg->flags&PGHDR_DIRTY) && !pPager->stmtInUse ){ |
| 31280 | 32169 | 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 ){ | |
| 31282 | 32171 | /* If this pages is the last page in the file and the file has grown |
| 31283 | 32172 | ** during the current transaction, then do NOT mark the page as clean. |
| 31284 | 32173 | ** When the database file grows, we must make sure that the last page |
| 31285 | 32174 | ** gets written at least once so that the disk file will be the correct |
| 31286 | 32175 | ** size. If you do not write this page and the size of the file |
| @@ -31325,11 +32214,12 @@ | ||
| 31325 | 32214 | ){ |
| 31326 | 32215 | return; |
| 31327 | 32216 | } |
| 31328 | 32217 | |
| 31329 | 32218 | #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 ){ | |
| 31331 | 32221 | return; |
| 31332 | 32222 | } |
| 31333 | 32223 | #endif |
| 31334 | 32224 | |
| 31335 | 32225 | /* If SECURE_DELETE is disabled, then there is no way that this |
| @@ -31344,11 +32234,10 @@ | ||
| 31344 | 32234 | */ |
| 31345 | 32235 | /* assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); */ |
| 31346 | 32236 | |
| 31347 | 32237 | assert( pPager->pInJournal!=0 ); |
| 31348 | 32238 | sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
| 31349 | - pPg->flags |= PGHDR_IN_JOURNAL; | |
| 31350 | 32239 | pPg->flags &= ~PGHDR_NEED_READ; |
| 31351 | 32240 | if( pPager->stmtInUse ){ |
| 31352 | 32241 | assert( pPager->stmtSize >= pPager->origDbSize ); |
| 31353 | 32242 | sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); |
| 31354 | 32243 | } |
| @@ -31525,11 +32414,11 @@ | ||
| 31525 | 32414 | /* If this transaction has made the database smaller, then all pages |
| 31526 | 32415 | ** being discarded by the truncation must be written to the journal |
| 31527 | 32416 | ** file. |
| 31528 | 32417 | */ |
| 31529 | 32418 | Pgno i; |
| 31530 | - int iSkip = PAGER_MJ_PGNO(pPager); | |
| 32419 | + Pgno iSkip = PAGER_MJ_PGNO(pPager); | |
| 31531 | 32420 | for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){ |
| 31532 | 32421 | if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){ |
| 31533 | 32422 | rc = sqlite3PagerGet(pPager, i, &pPg); |
| 31534 | 32423 | if( rc!=SQLITE_OK ) goto sync_exit; |
| 31535 | 32424 | rc = sqlite3PagerWrite(pPg); |
| @@ -31655,11 +32544,11 @@ | ||
| 31655 | 32544 | }else{ |
| 31656 | 32545 | rc = pager_playback(pPager, 0); |
| 31657 | 32546 | } |
| 31658 | 32547 | |
| 31659 | 32548 | if( !MEMDB ){ |
| 31660 | - pPager->dbSize = -1; | |
| 32549 | + pPager->dbSizeValid = 0; | |
| 31661 | 32550 | } |
| 31662 | 32551 | |
| 31663 | 32552 | /* If an error occurs during a ROLLBACK, we can no longer trust the pager |
| 31664 | 32553 | ** cache. So call pager_error() on the way out to make any error |
| 31665 | 32554 | ** persistent. |
| @@ -31698,11 +32587,11 @@ | ||
| 31698 | 32587 | SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){ |
| 31699 | 32588 | static int a[11]; |
| 31700 | 32589 | a[0] = sqlite3PcacheRefCount(pPager->pPCache); |
| 31701 | 32590 | a[1] = sqlite3PcachePagecount(pPager->pPCache); |
| 31702 | 32591 | a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); |
| 31703 | - a[3] = pPager->dbSize; | |
| 32592 | + a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1; | |
| 31704 | 32593 | a[4] = pPager->state; |
| 31705 | 32594 | a[5] = pPager->errCode; |
| 31706 | 32595 | a[6] = pPager->nHit; |
| 31707 | 32596 | a[7] = pPager->nMiss; |
| 31708 | 32597 | a[8] = 0; /* Used to be pPager->nOvfl */ |
| @@ -31724,11 +32613,11 @@ | ||
| 31724 | 32613 | */ |
| 31725 | 32614 | static int pagerStmtBegin(Pager *pPager){ |
| 31726 | 32615 | int rc; |
| 31727 | 32616 | assert( !pPager->stmtInUse ); |
| 31728 | 32617 | assert( pPager->state>=PAGER_SHARED ); |
| 31729 | - assert( pPager->dbSize>=0 ); | |
| 32618 | + assert( pPager->dbSizeValid ); | |
| 31730 | 32619 | PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager)); |
| 31731 | 32620 | if( !pPager->journalOpen ){ |
| 31732 | 32621 | pPager->stmtAutoopen = 1; |
| 31733 | 32622 | return SQLITE_OK; |
| 31734 | 32623 | } |
| @@ -31906,34 +32795,30 @@ | ||
| 31906 | 32795 | ** the journal needs to be sync()ed before database page pPg->pgno |
| 31907 | 32796 | ** can be written to. The caller has already promised not to write to it. |
| 31908 | 32797 | */ |
| 31909 | 32798 | if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ |
| 31910 | 32799 | needSyncPgno = pPg->pgno; |
| 31911 | - assert( (pPg->flags&PGHDR_IN_JOURNAL) || (int)pgno>pPager->origDbSize ); | |
| 32800 | + assert( pageInJournal(pPg) || pgno>pPager->origDbSize ); | |
| 31912 | 32801 | assert( pPg->flags&PGHDR_DIRTY ); |
| 31913 | 32802 | assert( pPager->needSync ); |
| 31914 | 32803 | } |
| 31915 | 32804 | |
| 31916 | 32805 | /* If the cache contains a page with page-number pgno, remove it |
| 31917 | 32806 | ** from its hash chain. Also, if the PgHdr.needSync was set for |
| 31918 | 32807 | ** page pgno before the 'move' operation, it needs to be retained |
| 31919 | 32808 | ** for the page moved there. |
| 31920 | 32809 | */ |
| 31921 | - pPg->flags &= ~(PGHDR_NEED_SYNC|PGHDR_IN_JOURNAL); | |
| 32810 | + pPg->flags &= ~PGHDR_NEED_SYNC; | |
| 31922 | 32811 | pPgOld = pager_lookup(pPager, pgno); |
| 31923 | 32812 | assert( !pPgOld || pPgOld->nRef==1 ); |
| 31924 | 32813 | if( pPgOld ){ |
| 31925 | 32814 | pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); |
| 31926 | 32815 | } |
| 31927 | - if( sqlite3BitvecTest(pPager->pInJournal, pgno) ){ | |
| 31928 | - pPg->flags |= PGHDR_IN_JOURNAL; | |
| 31929 | - } | |
| 31930 | 32816 | |
| 31931 | 32817 | sqlite3PcacheMove(pPg, pgno); |
| 31932 | 32818 | if( pPgOld ){ |
| 31933 | - sqlite3PcacheMove(pPgOld, 0); | |
| 31934 | - sqlite3PcacheRelease(pPgOld); | |
| 32819 | + sqlite3PcacheDrop(pPgOld); | |
| 31935 | 32820 | } |
| 31936 | 32821 | |
| 31937 | 32822 | makeDirty(pPg); |
| 31938 | 32823 | pPager->dirtyCache = 1; |
| 31939 | 32824 | pPager->dbModified = 1; |
| @@ -31959,19 +32844,18 @@ | ||
| 31959 | 32844 | int rc; |
| 31960 | 32845 | PgHdr *pPgHdr; |
| 31961 | 32846 | assert( pPager->needSync ); |
| 31962 | 32847 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
| 31963 | 32848 | if( rc!=SQLITE_OK ){ |
| 31964 | - if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){ | |
| 32849 | + if( pPager->pInJournal && needSyncPgno<=pPager->origDbSize ){ | |
| 31965 | 32850 | sqlite3BitvecClear(pPager->pInJournal, needSyncPgno); |
| 31966 | 32851 | } |
| 31967 | 32852 | return rc; |
| 31968 | 32853 | } |
| 31969 | 32854 | pPager->needSync = 1; |
| 31970 | 32855 | assert( pPager->noSync==0 && !MEMDB ); |
| 31971 | 32856 | pPgHdr->flags |= PGHDR_NEED_SYNC; |
| 31972 | - pPgHdr->flags |= PGHDR_IN_JOURNAL; | |
| 31973 | 32857 | makeDirty(pPgHdr); |
| 31974 | 32858 | sqlite3PagerUnref(pPgHdr); |
| 31975 | 32859 | } |
| 31976 | 32860 | |
| 31977 | 32861 | return SQLITE_OK; |
| @@ -32074,11 +32958,11 @@ | ||
| 32074 | 32958 | ** May you find forgiveness for yourself and forgive others. |
| 32075 | 32959 | ** May you share freely, never taking more than you give. |
| 32076 | 32960 | ** |
| 32077 | 32961 | ************************************************************************* |
| 32078 | 32962 | ** |
| 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 $ | |
| 32080 | 32964 | ** |
| 32081 | 32965 | ** This file contains code used to implement mutexes on Btree objects. |
| 32082 | 32966 | ** This code really belongs in btree.c. But btree.c is getting too |
| 32083 | 32967 | ** big and we want to break it down some. This packaged seemed like |
| 32084 | 32968 | ** a good breakout. |
| @@ -32094,11 +32978,11 @@ | ||
| 32094 | 32978 | ** May you do good and not evil. |
| 32095 | 32979 | ** May you find forgiveness for yourself and forgive others. |
| 32096 | 32980 | ** May you share freely, never taking more than you give. |
| 32097 | 32981 | ** |
| 32098 | 32982 | ************************************************************************* |
| 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 $ | |
| 32100 | 32984 | ** |
| 32101 | 32985 | ** This file implements a external (disk-based) database using BTrees. |
| 32102 | 32986 | ** For a detailed discussion of BTrees, refer to |
| 32103 | 32987 | ** |
| 32104 | 32988 | ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
| @@ -32464,11 +33348,10 @@ | ||
| 32464 | 33348 | u8 inTransaction; /* Transaction state */ |
| 32465 | 33349 | int nTransaction; /* Number of open transactions (read + write) */ |
| 32466 | 33350 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 32467 | 33351 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 32468 | 33352 | sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */ |
| 32469 | - BusyHandler busyHdr; /* The busy handler for this btree */ | |
| 32470 | 33353 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 32471 | 33354 | int nRef; /* Number of references to this structure */ |
| 32472 | 33355 | BtShared *pNext; /* Next on a list of sharable BtShared structs */ |
| 32473 | 33356 | BtLock *pLock; /* List of locks held on this shared-btree struct */ |
| 32474 | 33357 | Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */ |
| @@ -32582,11 +33465,11 @@ | ||
| 32582 | 33465 | ** in memory) then there is no pending byte. |
| 32583 | 33466 | */ |
| 32584 | 33467 | #ifdef SQLITE_OMIT_DISKIO |
| 32585 | 33468 | # define PENDING_BYTE_PAGE(pBt) 0x7fffffff |
| 32586 | 33469 | #else |
| 32587 | -# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) | |
| 33470 | +# define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/(pBt)->pageSize)+1)) | |
| 32588 | 33471 | #endif |
| 32589 | 33472 | |
| 32590 | 33473 | /* |
| 32591 | 33474 | ** A linked list of the following structures is stored at BtShared.pLock. |
| 32592 | 33475 | ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor |
| @@ -32689,11 +33572,11 @@ | ||
| 32689 | 33572 | */ |
| 32690 | 33573 | typedef struct IntegrityCk IntegrityCk; |
| 32691 | 33574 | struct IntegrityCk { |
| 32692 | 33575 | BtShared *pBt; /* The tree being checked out */ |
| 32693 | 33576 | 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 */ | |
| 32695 | 33578 | int *anRef; /* Number of times each page is referenced */ |
| 32696 | 33579 | int mxErr; /* Stop accumulating errors when this reaches zero */ |
| 32697 | 33580 | int nErr; /* Number of messages written to zErrMsg so far */ |
| 32698 | 33581 | int mallocFailed; /* A memory allocation error has occurred */ |
| 32699 | 33582 | StrAccum errMsg; /* Accumulate the error message text here */ |
| @@ -32949,11 +33832,11 @@ | ||
| 32949 | 33832 | assert( pArray->aBtree[i]!=pBtree ); |
| 32950 | 33833 | } |
| 32951 | 33834 | } |
| 32952 | 33835 | #endif |
| 32953 | 33836 | assert( pArray->nMutex>=0 ); |
| 32954 | - assert( pArray->nMutex<sizeof(pArray->aBtree)/sizeof(pArray->aBtree[0])-1 ); | |
| 33837 | + assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 ); | |
| 32955 | 33838 | pBt = pBtree->pBt; |
| 32956 | 33839 | for(i=0; i<pArray->nMutex; i++){ |
| 32957 | 33840 | assert( pArray->aBtree[i]!=pBtree ); |
| 32958 | 33841 | if( pArray->aBtree[i]->pBt>pBt ){ |
| 32959 | 33842 | for(j=pArray->nMutex; j>i; j--){ |
| @@ -33028,11 +33911,11 @@ | ||
| 33028 | 33911 | ** May you do good and not evil. |
| 33029 | 33912 | ** May you find forgiveness for yourself and forgive others. |
| 33030 | 33913 | ** May you share freely, never taking more than you give. |
| 33031 | 33914 | ** |
| 33032 | 33915 | ************************************************************************* |
| 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 $ | |
| 33034 | 33917 | ** |
| 33035 | 33918 | ** This file implements a external (disk-based) database using BTrees. |
| 33036 | 33919 | ** See the header comment on "btreeInt.h" for additional information. |
| 33037 | 33920 | ** Including a description of file format and an overview of operation. |
| 33038 | 33921 | */ |
| @@ -33453,11 +34336,12 @@ | ||
| 33453 | 34336 | ** Given a page number of a regular database page, return the page |
| 33454 | 34337 | ** number for the pointer-map page that contains the entry for the |
| 33455 | 34338 | ** input page number. |
| 33456 | 34339 | */ |
| 33457 | 34340 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
| 33458 | - int nPagesPerMapPage, iPtrMap, ret; | |
| 34341 | + int nPagesPerMapPage; | |
| 34342 | + Pgno iPtrMap, ret; | |
| 33459 | 34343 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 33460 | 34344 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 33461 | 34345 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 33462 | 34346 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
| 33463 | 34347 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| @@ -33720,11 +34604,11 @@ | ||
| 33720 | 34604 | ** Defragment the page given. All Cells are moved to the |
| 33721 | 34605 | ** end of the page and all free space is collected into one |
| 33722 | 34606 | ** big FreeBlk that occurs in between the header and cell |
| 33723 | 34607 | ** pointer array and the cell content area. |
| 33724 | 34608 | */ |
| 33725 | -static void defragmentPage(MemPage *pPage){ | |
| 34609 | +static int defragmentPage(MemPage *pPage){ | |
| 33726 | 34610 | int i; /* Loop counter */ |
| 33727 | 34611 | int pc; /* Address of a i-th cell */ |
| 33728 | 34612 | int addr; /* Offset of first byte after cell pointer array */ |
| 33729 | 34613 | int hdr; /* Offset to the page header */ |
| 33730 | 34614 | int size; /* Size of a cell */ |
| @@ -33752,13 +34636,19 @@ | ||
| 33752 | 34636 | cbrk = usableSize; |
| 33753 | 34637 | for(i=0; i<nCell; i++){ |
| 33754 | 34638 | u8 *pAddr; /* The i-th cell pointer */ |
| 33755 | 34639 | pAddr = &data[cellOffset + i*2]; |
| 33756 | 34640 | pc = get2byte(pAddr); |
| 33757 | - assert( pc<pPage->pBt->usableSize ); | |
| 34641 | + if( pc>=usableSize ){ | |
| 34642 | + return SQLITE_CORRUPT_BKPT; | |
| 34643 | + } | |
| 33758 | 34644 | size = cellSizePtr(pPage, &temp[pc]); |
| 33759 | 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 ); | |
| 33760 | 34650 | memcpy(&data[cbrk], &temp[pc], size); |
| 33761 | 34651 | put2byte(pAddr, cbrk); |
| 33762 | 34652 | } |
| 33763 | 34653 | assert( cbrk>=cellOffset+2*nCell ); |
| 33764 | 34654 | put2byte(&data[hdr+5], cbrk); |
| @@ -33765,10 +34655,14 @@ | ||
| 33765 | 34655 | data[hdr+1] = 0; |
| 33766 | 34656 | data[hdr+2] = 0; |
| 33767 | 34657 | data[hdr+7] = 0; |
| 33768 | 34658 | addr = cellOffset+2*nCell; |
| 33769 | 34659 | memset(&data[addr], 0, cbrk-addr); |
| 34660 | + if( cbrk-addr!=pPage->nFree ){ | |
| 34661 | + return SQLITE_CORRUPT_BKPT; | |
| 34662 | + } | |
| 34663 | + return SQLITE_OK; | |
| 33770 | 34664 | } |
| 33771 | 34665 | |
| 33772 | 34666 | /* |
| 33773 | 34667 | ** Allocate nByte bytes of space on a page. |
| 33774 | 34668 | ** |
| @@ -33843,11 +34737,11 @@ | ||
| 33843 | 34737 | ** and the size of the block is "size" bytes. |
| 33844 | 34738 | ** |
| 33845 | 34739 | ** Most of the effort here is involved in coalesing adjacent |
| 33846 | 34740 | ** free blocks into a single big free block. |
| 33847 | 34741 | */ |
| 33848 | -static void freeSpace(MemPage *pPage, int start, int size){ | |
| 34742 | +static int freeSpace(MemPage *pPage, int start, int size){ | |
| 33849 | 34743 | int addr, pbegin, hdr; |
| 33850 | 34744 | unsigned char *data = pPage->aData; |
| 33851 | 34745 | |
| 33852 | 34746 | assert( pPage->pBt!=0 ); |
| 33853 | 34747 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| @@ -33865,14 +34759,18 @@ | ||
| 33865 | 34759 | /* Add the space back into the linked list of freeblocks */ |
| 33866 | 34760 | hdr = pPage->hdrOffset; |
| 33867 | 34761 | addr = hdr + 1; |
| 33868 | 34762 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
| 33869 | 34763 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
| 33870 | - assert( pbegin>addr ); | |
| 34764 | + if( pbegin<=addr ) { | |
| 34765 | + return SQLITE_CORRUPT_BKPT; | |
| 34766 | + } | |
| 33871 | 34767 | addr = pbegin; |
| 33872 | 34768 | } |
| 33873 | - assert( pbegin<=pPage->pBt->usableSize-4 ); | |
| 34769 | + if ( pbegin>pPage->pBt->usableSize-4 ) { | |
| 34770 | + return SQLITE_CORRUPT_BKPT; | |
| 34771 | + } | |
| 33874 | 34772 | assert( pbegin>addr || pbegin==0 ); |
| 33875 | 34773 | put2byte(&data[addr], start); |
| 33876 | 34774 | put2byte(&data[start], pbegin); |
| 33877 | 34775 | put2byte(&data[start+2], size); |
| 33878 | 34776 | pPage->nFree += size; |
| @@ -33885,11 +34783,13 @@ | ||
| 33885 | 34783 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
| 33886 | 34784 | pnext = get2byte(&data[pbegin]); |
| 33887 | 34785 | psize = get2byte(&data[pbegin+2]); |
| 33888 | 34786 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 33889 | 34787 | 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 | + } | |
| 33891 | 34791 | data[pPage->hdrOffset+7] -= frag; |
| 33892 | 34792 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 33893 | 34793 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 33894 | 34794 | }else{ |
| 33895 | 34795 | addr = pbegin; |
| @@ -33902,10 +34802,11 @@ | ||
| 33902 | 34802 | pbegin = get2byte(&data[hdr+1]); |
| 33903 | 34803 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 33904 | 34804 | top = get2byte(&data[hdr+5]); |
| 33905 | 34805 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
| 33906 | 34806 | } |
| 34807 | + return SQLITE_OK; | |
| 33907 | 34808 | } |
| 33908 | 34809 | |
| 33909 | 34810 | /* |
| 33910 | 34811 | ** Decode the flags byte (the first byte of the header) for a page |
| 33911 | 34812 | ** and initialize fields of the MemPage structure accordingly. |
| @@ -34109,18 +35010,20 @@ | ||
| 34109 | 35010 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
| 34110 | 35011 | return SQLITE_OK; |
| 34111 | 35012 | } |
| 34112 | 35013 | |
| 34113 | 35014 | /* |
| 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). | |
| 34116 | 35017 | */ |
| 34117 | -static int pagerPagecount(Pager *pPager){ | |
| 35018 | +static Pgno pagerPagecount(BtShared *pBt){ | |
| 35019 | + int nPage = -1; | |
| 34118 | 35020 | 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; | |
| 34122 | 35025 | } |
| 34123 | 35026 | |
| 34124 | 35027 | /* |
| 34125 | 35028 | ** Get a page from the pager and initialize it. This routine |
| 34126 | 35029 | ** is just a convenience wrapper around separate calls to |
| @@ -34150,11 +35053,11 @@ | ||
| 34150 | 35053 | /* Page is already in cache */ |
| 34151 | 35054 | *ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
| 34152 | 35055 | rc = SQLITE_OK; |
| 34153 | 35056 | }else{ |
| 34154 | 35057 | /* Page not in cache. Acquire it. */ |
| 34155 | - if( pgno>pagerPagecount(pBt->pPager) ){ | |
| 35058 | + if( pgno>pagerPagecount(pBt) ){ | |
| 34156 | 35059 | return SQLITE_CORRUPT_BKPT; |
| 34157 | 35060 | } |
| 34158 | 35061 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
| 34159 | 35062 | if( rc ) return rc; |
| 34160 | 35063 | pPage = *ppPage; |
| @@ -34205,11 +35108,11 @@ | ||
| 34205 | 35108 | } |
| 34206 | 35109 | |
| 34207 | 35110 | /* |
| 34208 | 35111 | ** Invoke the busy handler for a btree. |
| 34209 | 35112 | */ |
| 34210 | -static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){ | |
| 35113 | +static int btreeInvokeBusyHandler(void *pArg){ | |
| 34211 | 35114 | BtShared *pBt = (BtShared*)pArg; |
| 34212 | 35115 | assert( pBt->db ); |
| 34213 | 35116 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 34214 | 35117 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 34215 | 35118 | } |
| @@ -34322,21 +35225,19 @@ | ||
| 34322 | 35225 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 34323 | 35226 | if( pBt==0 ){ |
| 34324 | 35227 | rc = SQLITE_NOMEM; |
| 34325 | 35228 | goto btree_open_out; |
| 34326 | 35229 | } |
| 34327 | - pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler; | |
| 34328 | - pBt->busyHdr.pArg = pBt; | |
| 34329 | 35230 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
| 34330 | 35231 | EXTRA_SIZE, flags, vfsFlags); |
| 34331 | 35232 | if( rc==SQLITE_OK ){ |
| 34332 | 35233 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 34333 | 35234 | } |
| 34334 | 35235 | if( rc!=SQLITE_OK ){ |
| 34335 | 35236 | goto btree_open_out; |
| 34336 | 35237 | } |
| 34337 | - sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr); | |
| 35238 | + sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); | |
| 34338 | 35239 | p->pBt = pBt; |
| 34339 | 35240 | |
| 34340 | 35241 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 34341 | 35242 | pBt->pCursor = 0; |
| 34342 | 35243 | pBt->pPage1 = 0; |
| @@ -35030,11 +35931,11 @@ | ||
| 35030 | 35931 | if( wrflag ) pBt->inStmt = 0; |
| 35031 | 35932 | }else{ |
| 35032 | 35933 | unlockBtreeIfUnused(pBt); |
| 35033 | 35934 | } |
| 35034 | 35935 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
| 35035 | - sqlite3BtreeInvokeBusyHandler(pBt, 0) ); | |
| 35936 | + btreeInvokeBusyHandler(pBt) ); | |
| 35036 | 35937 | |
| 35037 | 35938 | if( rc==SQLITE_OK ){ |
| 35038 | 35939 | if( p->inTrans==TRANS_NONE ){ |
| 35039 | 35940 | pBt->nTransaction++; |
| 35040 | 35941 | } |
| @@ -35054,11 +35955,10 @@ | ||
| 35054 | 35955 | trans_begun: |
| 35055 | 35956 | btreeIntegrity(p); |
| 35056 | 35957 | sqlite3BtreeLeave(p); |
| 35057 | 35958 | return rc; |
| 35058 | 35959 | } |
| 35059 | - | |
| 35060 | 35960 | |
| 35061 | 35961 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 35062 | 35962 | |
| 35063 | 35963 | /* |
| 35064 | 35964 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| @@ -35269,11 +36169,11 @@ | ||
| 35269 | 36169 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 35270 | 36170 | |
| 35271 | 36171 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 35272 | 36172 | iLastPg = pBt->nTrunc; |
| 35273 | 36173 | if( iLastPg==0 ){ |
| 35274 | - iLastPg = pagerPagecount(pBt->pPager); | |
| 36174 | + iLastPg = pagerPagecount(pBt); | |
| 35275 | 36175 | } |
| 35276 | 36176 | |
| 35277 | 36177 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 35278 | 36178 | int rc; |
| 35279 | 36179 | u8 eType; |
| @@ -35400,11 +36300,11 @@ | ||
| 35400 | 36300 | |
| 35401 | 36301 | if( pBt->nTrunc==0 ){ |
| 35402 | 36302 | Pgno nFree; |
| 35403 | 36303 | Pgno nPtrmap; |
| 35404 | 36304 | const int pgsz = pBt->pageSize; |
| 35405 | - int nOrig = pagerPagecount(pBt->pPager); | |
| 36305 | + Pgno nOrig = pagerPagecount(pBt); | |
| 35406 | 36306 | |
| 35407 | 36307 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 35408 | 36308 | return SQLITE_CORRUPT_BKPT; |
| 35409 | 36309 | } |
| 35410 | 36310 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| @@ -35445,11 +36345,11 @@ | ||
| 35445 | 36345 | } |
| 35446 | 36346 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
| 35447 | 36347 | return rc; |
| 35448 | 36348 | } |
| 35449 | 36349 | |
| 35450 | -#endif | |
| 36350 | +#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ | |
| 35451 | 36351 | |
| 35452 | 36352 | /* |
| 35453 | 36353 | ** This routine does the first phase of a two-phase commit. This routine |
| 35454 | 36354 | ** causes a rollback journal to be created (if it does not already exist) |
| 35455 | 36355 | ** and populated with enough information so that if a power loss occurs |
| @@ -35612,13 +36512,18 @@ | ||
| 35612 | 36512 | */ |
| 35613 | 36513 | SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 35614 | 36514 | BtCursor *p; |
| 35615 | 36515 | sqlite3BtreeEnter(pBtree); |
| 35616 | 36516 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 36517 | + int i; | |
| 35617 | 36518 | sqlite3BtreeClearCursor(p); |
| 35618 | 36519 | p->eState = CURSOR_FAULT; |
| 35619 | 36520 | p->skip = errCode; |
| 36521 | + for(i=0; i<=p->iPage; i++){ | |
| 36522 | + releasePage(p->apPage[i]); | |
| 36523 | + p->apPage[i] = 0; | |
| 36524 | + } | |
| 35620 | 36525 | } |
| 35621 | 36526 | sqlite3BtreeLeave(pBtree); |
| 35622 | 36527 | } |
| 35623 | 36528 | |
| 35624 | 36529 | /* |
| @@ -35801,10 +36706,11 @@ | ||
| 35801 | 36706 | int wrFlag, /* 1 to write. 0 read-only */ |
| 35802 | 36707 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 35803 | 36708 | BtCursor *pCur /* Space for new cursor */ |
| 35804 | 36709 | ){ |
| 35805 | 36710 | int rc; |
| 36711 | + Pgno nPage; | |
| 35806 | 36712 | BtShared *pBt = p->pBt; |
| 35807 | 36713 | |
| 35808 | 36714 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 35809 | 36715 | if( wrFlag ){ |
| 35810 | 36716 | if( pBt->readOnly ){ |
| @@ -35823,11 +36729,15 @@ | ||
| 35823 | 36729 | if( pBt->readOnly && wrFlag ){ |
| 35824 | 36730 | return SQLITE_READONLY; |
| 35825 | 36731 | } |
| 35826 | 36732 | } |
| 35827 | 36733 | 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 ){ | |
| 35829 | 36739 | rc = SQLITE_EMPTY; |
| 35830 | 36740 | goto create_cursor_exception; |
| 35831 | 36741 | } |
| 35832 | 36742 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
| 35833 | 36743 | if( rc!=SQLITE_OK ){ |
| @@ -36084,11 +36994,11 @@ | ||
| 36084 | 36994 | |
| 36085 | 36995 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 36086 | 36996 | iGuess++; |
| 36087 | 36997 | } |
| 36088 | 36998 | |
| 36089 | - if( iGuess<=pagerPagecount(pBt->pPager) ){ | |
| 36999 | + if( iGuess<=pagerPagecount(pBt) ){ | |
| 36090 | 37000 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 36091 | 37001 | if( rc!=SQLITE_OK ){ |
| 36092 | 37002 | return rc; |
| 36093 | 37003 | } |
| 36094 | 37004 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| @@ -36180,37 +37090,38 @@ | ||
| 36180 | 37090 | ** * A commit in auto_vacuum="full" mode, |
| 36181 | 37091 | ** * Creating a table (may require moving an overflow page). |
| 36182 | 37092 | */ |
| 36183 | 37093 | static int accessPayload( |
| 36184 | 37094 | 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 */ | |
| 36187 | 37097 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 36188 | 37098 | int skipKey, /* offset begins at data if this is true */ |
| 36189 | 37099 | int eOp /* zero to read. non-zero to write. */ |
| 36190 | 37100 | ){ |
| 36191 | 37101 | unsigned char *aPayload; |
| 36192 | 37102 | int rc = SQLITE_OK; |
| 36193 | 37103 | u32 nKey; |
| 36194 | 37104 | int iIdx = 0; |
| 36195 | 37105 | 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 */ | |
| 36197 | 37107 | |
| 36198 | 37108 | assert( pPage ); |
| 36199 | 37109 | assert( pCur->eState==CURSOR_VALID ); |
| 36200 | 37110 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 36201 | - assert( offset>=0 ); | |
| 36202 | 37111 | assert( cursorHoldsMutex(pCur) ); |
| 36203 | 37112 | |
| 36204 | 37113 | getCellInfo(pCur); |
| 36205 | 37114 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
| 36206 | 37115 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 36207 | 37116 | |
| 36208 | 37117 | if( skipKey ){ |
| 36209 | 37118 | offset += nKey; |
| 36210 | 37119 | } |
| 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 | + ){ | |
| 36212 | 37123 | /* Trying to read or write past the end of the data is an error */ |
| 36213 | 37124 | return SQLITE_CORRUPT_BKPT; |
| 36214 | 37125 | } |
| 36215 | 37126 | |
| 36216 | 37127 | /* Check if data must be read/written to/from the btree page itself. */ |
| @@ -36225,13 +37136,12 @@ | ||
| 36225 | 37136 | amt -= a; |
| 36226 | 37137 | }else{ |
| 36227 | 37138 | offset -= pCur->info.nLocal; |
| 36228 | 37139 | } |
| 36229 | 37140 | |
| 36230 | - pBt = pCur->pBt; | |
| 36231 | 37141 | 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 */ | |
| 36233 | 37143 | Pgno nextPage; |
| 36234 | 37144 | |
| 36235 | 37145 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
| 36236 | 37146 | |
| 36237 | 37147 | #ifndef SQLITE_OMIT_INCRBLOB |
| @@ -36394,11 +37304,11 @@ | ||
| 36394 | 37304 | int skipKey /* read beginning at data if this is true */ |
| 36395 | 37305 | ){ |
| 36396 | 37306 | unsigned char *aPayload; |
| 36397 | 37307 | MemPage *pPage; |
| 36398 | 37308 | u32 nKey; |
| 36399 | - int nLocal; | |
| 37309 | + u32 nLocal; | |
| 36400 | 37310 | |
| 36401 | 37311 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
| 36402 | 37312 | assert( pCur->eState==CURSOR_VALID ); |
| 36403 | 37313 | assert( cursorHoldsMutex(pCur) ); |
| 36404 | 37314 | pPage = pCur->apPage[pCur->iPage]; |
| @@ -37084,11 +37994,11 @@ | ||
| 37084 | 37994 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 37085 | 37995 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 37086 | 37996 | ** the entire-list will be searched for that page. |
| 37087 | 37997 | */ |
| 37088 | 37998 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 37089 | - if( exact && nearby<=pagerPagecount(pBt->pPager) ){ | |
| 37999 | + if( exact && nearby<=pagerPagecount(pBt) ){ | |
| 37090 | 38000 | u8 eType; |
| 37091 | 38001 | assert( nearby>0 ); |
| 37092 | 38002 | assert( pBt->autoVacuum ); |
| 37093 | 38003 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 37094 | 38004 | if( rc ) return rc; |
| @@ -37219,13 +38129,13 @@ | ||
| 37219 | 38129 | closest = 0; |
| 37220 | 38130 | } |
| 37221 | 38131 | |
| 37222 | 38132 | iPage = get4byte(&aData[8+closest*4]); |
| 37223 | 38133 | if( !searchList || iPage==nearby ){ |
| 37224 | - int nPage; | |
| 38134 | + Pgno nPage; | |
| 37225 | 38135 | *pPgno = iPage; |
| 37226 | - nPage = pagerPagecount(pBt->pPager); | |
| 38136 | + nPage = pagerPagecount(pBt); | |
| 37227 | 38137 | if( *pPgno>nPage ){ |
| 37228 | 38138 | /* Free page off the end of the file */ |
| 37229 | 38139 | rc = SQLITE_CORRUPT_BKPT; |
| 37230 | 38140 | goto end_allocate_page; |
| 37231 | 38141 | } |
| @@ -37251,11 +38161,11 @@ | ||
| 37251 | 38161 | pPrevTrunk = 0; |
| 37252 | 38162 | }while( searchList ); |
| 37253 | 38163 | }else{ |
| 37254 | 38164 | /* There are no pages on the freelist, so create a new page at the |
| 37255 | 38165 | ** end of the file */ |
| 37256 | - int nPage = pagerPagecount(pBt->pPager); | |
| 38166 | + int nPage = pagerPagecount(pBt); | |
| 37257 | 38167 | *pPgno = nPage + 1; |
| 37258 | 38168 | |
| 37259 | 38169 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 37260 | 38170 | if( pBt->nTrunc ){ |
| 37261 | 38171 | /* An incr-vacuum has already run within this transaction. So the |
| @@ -37295,13 +38205,16 @@ | ||
| 37295 | 38205 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 37296 | 38206 | |
| 37297 | 38207 | end_allocate_page: |
| 37298 | 38208 | releasePage(pTrunk); |
| 37299 | 38209 | 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; | |
| 37303 | 38216 | } |
| 37304 | 38217 | return rc; |
| 37305 | 38218 | } |
| 37306 | 38219 | |
| 37307 | 38220 | /* |
| @@ -37418,11 +38331,11 @@ | ||
| 37418 | 38331 | ovflPageSize = pBt->usableSize - 4; |
| 37419 | 38332 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 37420 | 38333 | assert( ovflPgno==0 || nOvfl>0 ); |
| 37421 | 38334 | while( nOvfl-- ){ |
| 37422 | 38335 | MemPage *pOvfl; |
| 37423 | - if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){ | |
| 38336 | + if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){ | |
| 37424 | 38337 | return SQLITE_CORRUPT_BKPT; |
| 37425 | 38338 | } |
| 37426 | 38339 | |
| 37427 | 38340 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
| 37428 | 38341 | if( rc ) return rc; |
| @@ -37480,11 +38393,11 @@ | ||
| 37480 | 38393 | } |
| 37481 | 38394 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
| 37482 | 38395 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 37483 | 38396 | assert( info.nHeader==nHeader ); |
| 37484 | 38397 | assert( info.nKey==nKey ); |
| 37485 | - assert( info.nData==nData+nZero ); | |
| 38398 | + assert( info.nData==(u32)(nData+nZero) ); | |
| 37486 | 38399 | |
| 37487 | 38400 | /* Fill in the payload */ |
| 37488 | 38401 | nPayload = nData + nZero; |
| 37489 | 38402 | if( pPage->intKey ){ |
| 37490 | 38403 | pSrc = pData; |
| @@ -37577,32 +38490,39 @@ | ||
| 37577 | 38490 | ** the cell content has been copied someplace else. This routine just |
| 37578 | 38491 | ** removes the reference to the cell from pPage. |
| 37579 | 38492 | ** |
| 37580 | 38493 | ** "sz" must be the number of bytes in the cell. |
| 37581 | 38494 | */ |
| 37582 | -static void dropCell(MemPage *pPage, int idx, int sz){ | |
| 38495 | +static int dropCell(MemPage *pPage, int idx, int sz){ | |
| 37583 | 38496 | int i; /* Loop counter */ |
| 37584 | 38497 | int pc; /* Offset to cell content of cell being deleted */ |
| 37585 | 38498 | u8 *data; /* pPage->aData */ |
| 37586 | 38499 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 38500 | + int rc; /* The return code */ | |
| 37587 | 38501 | |
| 37588 | 38502 | assert( idx>=0 && idx<pPage->nCell ); |
| 37589 | 38503 | assert( sz==cellSize(pPage, idx) ); |
| 37590 | 38504 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 37591 | 38505 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 37592 | 38506 | data = pPage->aData; |
| 37593 | 38507 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 37594 | 38508 | 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 | + } | |
| 37597 | 38516 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 37598 | 38517 | ptr[0] = ptr[2]; |
| 37599 | 38518 | ptr[1] = ptr[3]; |
| 37600 | 38519 | } |
| 37601 | 38520 | pPage->nCell--; |
| 37602 | 38521 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 37603 | 38522 | pPage->nFree += 2; |
| 38523 | + return SQLITE_OK; | |
| 37604 | 38524 | } |
| 37605 | 38525 | |
| 37606 | 38526 | /* |
| 37607 | 38527 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 37608 | 38528 | ** content of the cell. |
| @@ -37645,11 +38565,11 @@ | ||
| 37645 | 38565 | if( pTemp ){ |
| 37646 | 38566 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
| 37647 | 38567 | pCell = pTemp; |
| 37648 | 38568 | } |
| 37649 | 38569 | j = pPage->nOverflow++; |
| 37650 | - assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); | |
| 38570 | + assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) ); | |
| 37651 | 38571 | pPage->aOvfl[j].pCell = pCell; |
| 37652 | 38572 | pPage->aOvfl[j].idx = i; |
| 37653 | 38573 | pPage->nFree = 0; |
| 37654 | 38574 | }else{ |
| 37655 | 38575 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| @@ -37662,17 +38582,23 @@ | ||
| 37662 | 38582 | top = get2byte(&data[hdr+5]); |
| 37663 | 38583 | cellOffset = pPage->cellOffset; |
| 37664 | 38584 | end = cellOffset + 2*pPage->nCell + 2; |
| 37665 | 38585 | ins = cellOffset + 2*i; |
| 37666 | 38586 | if( end > top - sz ){ |
| 37667 | - defragmentPage(pPage); | |
| 38587 | + rc = defragmentPage(pPage); | |
| 38588 | + if( rc!=SQLITE_OK ){ | |
| 38589 | + return rc; | |
| 38590 | + } | |
| 37668 | 38591 | top = get2byte(&data[hdr+5]); |
| 37669 | 38592 | assert( end + sz <= top ); |
| 37670 | 38593 | } |
| 37671 | 38594 | idx = allocateSpace(pPage, sz); |
| 37672 | 38595 | assert( idx>0 ); |
| 37673 | 38596 | assert( end <= get2byte(&data[hdr+5]) ); |
| 38597 | + if (idx+sz > pPage->pBt->usableSize) { | |
| 38598 | + return SQLITE_CORRUPT_BKPT; | |
| 38599 | + } | |
| 37674 | 38600 | pPage->nCell++; |
| 37675 | 38601 | pPage->nFree -= 2; |
| 37676 | 38602 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
| 37677 | 38603 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 37678 | 38604 | ptr[0] = ptr[-2]; |
| @@ -38526,11 +39452,11 @@ | ||
| 38526 | 39452 | ** the virtual root of the tree. |
| 38527 | 39453 | */ |
| 38528 | 39454 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
| 38529 | 39455 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 38530 | 39456 | assert( pgnoChild>0 ); |
| 38531 | - assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) ); | |
| 39457 | + assert( pgnoChild<=pagerPagecount(pPage->pBt) ); | |
| 38532 | 39458 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
| 38533 | 39459 | if( rc ) goto end_shallow_balance; |
| 38534 | 39460 | if( pPage->pgno==1 ){ |
| 38535 | 39461 | rc = sqlite3BtreeInitPage(pChild); |
| 38536 | 39462 | if( rc ) goto end_shallow_balance; |
| @@ -38563,13 +39489,15 @@ | ||
| 38563 | 39489 | freePage(pChild); |
| 38564 | 39490 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 38565 | 39491 | pChild->pgno, pPage->pgno)); |
| 38566 | 39492 | } |
| 38567 | 39493 | assert( pPage->nOverflow==0 ); |
| 39494 | +#ifndef SQLITE_OMIT_AUTOVACUUM | |
| 38568 | 39495 | if( ISAUTOVACUUM ){ |
| 38569 | 39496 | rc = setChildPtrmaps(pPage); |
| 38570 | 39497 | } |
| 39498 | +#endif | |
| 38571 | 39499 | releasePage(pChild); |
| 38572 | 39500 | } |
| 38573 | 39501 | end_shallow_balance: |
| 38574 | 39502 | sqlite3_free(apCell); |
| 38575 | 39503 | return rc; |
| @@ -38612,11 +39540,12 @@ | ||
| 38612 | 39540 | hdr = pPage->hdrOffset; |
| 38613 | 39541 | cbrk = get2byte(&data[hdr+5]); |
| 38614 | 39542 | cdata = pChild->aData; |
| 38615 | 39543 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 38616 | 39544 | memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk); |
| 38617 | - | |
| 39545 | + | |
| 39546 | + assert( pChild->isInit==0 ); | |
| 38618 | 39547 | rc = sqlite3BtreeInitPage(pChild); |
| 38619 | 39548 | if( rc==SQLITE_OK ){ |
| 38620 | 39549 | int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]); |
| 38621 | 39550 | memcpy(pChild->aOvfl, pPage->aOvfl, nCopy); |
| 38622 | 39551 | pChild->nOverflow = pPage->nOverflow; |
| @@ -38627,13 +39556,15 @@ | ||
| 38627 | 39556 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 38628 | 39557 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 38629 | 39558 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
| 38630 | 39559 | if( ISAUTOVACUUM ){ |
| 38631 | 39560 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
| 39561 | +#ifndef SQLITE_OMIT_AUTOVACUUM | |
| 38632 | 39562 | if( rc==SQLITE_OK ){ |
| 38633 | 39563 | rc = setChildPtrmaps(pChild); |
| 38634 | 39564 | } |
| 39565 | +#endif | |
| 38635 | 39566 | } |
| 38636 | 39567 | } |
| 38637 | 39568 | |
| 38638 | 39569 | if( rc==SQLITE_OK ){ |
| 38639 | 39570 | pCur->iPage++; |
| @@ -38826,11 +39757,14 @@ | ||
| 38826 | 39757 | memcpy(newCell, oldCell, 4); |
| 38827 | 39758 | } |
| 38828 | 39759 | szOld = cellSizePtr(pPage, oldCell); |
| 38829 | 39760 | rc = clearCell(pPage, oldCell); |
| 38830 | 39761 | 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 | + } | |
| 38832 | 39766 | }else if( loc<0 && pPage->nCell>0 ){ |
| 38833 | 39767 | assert( pPage->leaf ); |
| 38834 | 39768 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 38835 | 39769 | pCur->info.nSize = 0; |
| 38836 | 39770 | pCur->validNKey = 0; |
| @@ -39037,12 +39971,14 @@ | ||
| 39037 | 39971 | } |
| 39038 | 39972 | sqlite3BtreeReleaseTempCursor(&leafCur); |
| 39039 | 39973 | }else{ |
| 39040 | 39974 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 39041 | 39975 | 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 | + } | |
| 39044 | 39980 | } |
| 39045 | 39981 | if( rc==SQLITE_OK ){ |
| 39046 | 39982 | moveToRoot(pCur); |
| 39047 | 39983 | } |
| 39048 | 39984 | return rc; |
| @@ -39204,37 +40140,36 @@ | ||
| 39204 | 40140 | ** the page to the freelist. |
| 39205 | 40141 | */ |
| 39206 | 40142 | static int clearDatabasePage( |
| 39207 | 40143 | BtShared *pBt, /* The BTree that contains the table */ |
| 39208 | 40144 | Pgno pgno, /* Page number to clear */ |
| 39209 | - MemPage *pParent, /* Parent page. NULL for the root */ | |
| 39210 | 40145 | int freePageFlag, /* Deallocate page if true */ |
| 39211 | 40146 | int *pnChange |
| 39212 | 40147 | ){ |
| 39213 | 40148 | MemPage *pPage = 0; |
| 39214 | 40149 | int rc; |
| 39215 | 40150 | unsigned char *pCell; |
| 39216 | 40151 | int i; |
| 39217 | 40152 | |
| 39218 | 40153 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 39219 | - if( pgno>pagerPagecount(pBt->pPager) ){ | |
| 40154 | + if( pgno>pagerPagecount(pBt) ){ | |
| 39220 | 40155 | return SQLITE_CORRUPT_BKPT; |
| 39221 | 40156 | } |
| 39222 | 40157 | |
| 39223 | 40158 | rc = getAndInitPage(pBt, pgno, &pPage); |
| 39224 | 40159 | if( rc ) goto cleardatabasepage_out; |
| 39225 | 40160 | for(i=0; i<pPage->nCell; i++){ |
| 39226 | 40161 | pCell = findCell(pPage, i); |
| 39227 | 40162 | if( !pPage->leaf ){ |
| 39228 | - rc = clearDatabasePage(pBt, get4byte(pCell), pPage, 1, pnChange); | |
| 40163 | + rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); | |
| 39229 | 40164 | if( rc ) goto cleardatabasepage_out; |
| 39230 | 40165 | } |
| 39231 | 40166 | rc = clearCell(pPage, pCell); |
| 39232 | 40167 | if( rc ) goto cleardatabasepage_out; |
| 39233 | 40168 | } |
| 39234 | 40169 | if( !pPage->leaf ){ |
| 39235 | - rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage, 1, pnChange); | |
| 40170 | + rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange); | |
| 39236 | 40171 | if( rc ) goto cleardatabasepage_out; |
| 39237 | 40172 | }else if( pnChange ){ |
| 39238 | 40173 | assert( pPage->intKey ); |
| 39239 | 40174 | *pnChange += pPage->nCell; |
| 39240 | 40175 | } |
| @@ -39272,11 +40207,11 @@ | ||
| 39272 | 40207 | }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){ |
| 39273 | 40208 | /* nothing to do */ |
| 39274 | 40209 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 39275 | 40210 | /* nothing to do */ |
| 39276 | 40211 | }else{ |
| 39277 | - rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0, pnChange); | |
| 40212 | + rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); | |
| 39278 | 40213 | } |
| 39279 | 40214 | sqlite3BtreeLeave(p); |
| 39280 | 40215 | return rc; |
| 39281 | 40216 | } |
| 39282 | 40217 | |
| @@ -39298,11 +40233,11 @@ | ||
| 39298 | 40233 | ** page number that used to be the last root page in the file before |
| 39299 | 40234 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 39300 | 40235 | ** The last root page is recorded in meta[3] and the value of |
| 39301 | 40236 | ** meta[3] is updated by this procedure. |
| 39302 | 40237 | */ |
| 39303 | -static int btreeDropTable(Btree *p, int iTable, int *piMoved){ | |
| 40238 | +static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ | |
| 39304 | 40239 | int rc; |
| 39305 | 40240 | MemPage *pPage = 0; |
| 39306 | 40241 | BtShared *pBt = p->pBt; |
| 39307 | 40242 | |
| 39308 | 40243 | assert( sqlite3BtreeHoldsMutex(p) ); |
| @@ -39581,13 +40516,13 @@ | ||
| 39581 | 40516 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 39582 | 40517 | ** if this is the first reference to the page. |
| 39583 | 40518 | ** |
| 39584 | 40519 | ** Also check that the page number is in bounds. |
| 39585 | 40520 | */ |
| 39586 | -static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ | |
| 40521 | +static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ | |
| 39587 | 40522 | if( iPage==0 ) return 1; |
| 39588 | - if( iPage>pCheck->nPage || iPage<0 ){ | |
| 40523 | + if( iPage>pCheck->nPage ){ | |
| 39589 | 40524 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
| 39590 | 40525 | return 1; |
| 39591 | 40526 | } |
| 39592 | 40527 | if( pCheck->anRef[iPage]==1 ){ |
| 39593 | 40528 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
| @@ -39718,11 +40653,10 @@ | ||
| 39718 | 40653 | ** the root of the tree. |
| 39719 | 40654 | */ |
| 39720 | 40655 | static int checkTreePage( |
| 39721 | 40656 | IntegrityCk *pCheck, /* Context for the sanity check */ |
| 39722 | 40657 | int iPage, /* Page number of the page to check */ |
| 39723 | - MemPage *pParent, /* Parent page */ | |
| 39724 | 40658 | char *zParentContext /* Parent context */ |
| 39725 | 40659 | ){ |
| 39726 | 40660 | MemPage *pPage; |
| 39727 | 40661 | int i, rc, depth, d2, pgno, cnt; |
| 39728 | 40662 | int hdr, cellStart; |
| @@ -39729,11 +40663,11 @@ | ||
| 39729 | 40663 | int nCell; |
| 39730 | 40664 | u8 *data; |
| 39731 | 40665 | BtShared *pBt; |
| 39732 | 40666 | int usableSize; |
| 39733 | 40667 | char zContext[100]; |
| 39734 | - char *hit; | |
| 40668 | + char *hit = 0; | |
| 39735 | 40669 | |
| 39736 | 40670 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
| 39737 | 40671 | |
| 39738 | 40672 | /* Check that the page exists |
| 39739 | 40673 | */ |
| @@ -39756,11 +40690,11 @@ | ||
| 39756 | 40690 | /* Check out all the cells. |
| 39757 | 40691 | */ |
| 39758 | 40692 | depth = 0; |
| 39759 | 40693 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
| 39760 | 40694 | u8 *pCell; |
| 39761 | - int sz; | |
| 40695 | + u32 sz; | |
| 39762 | 40696 | CellInfo info; |
| 39763 | 40697 | |
| 39764 | 40698 | /* Check payload overflow pages |
| 39765 | 40699 | */ |
| 39766 | 40700 | sqlite3_snprintf(sizeof(zContext), zContext, |
| @@ -39788,11 +40722,11 @@ | ||
| 39788 | 40722 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 39789 | 40723 | if( pBt->autoVacuum ){ |
| 39790 | 40724 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 39791 | 40725 | } |
| 39792 | 40726 | #endif |
| 39793 | - d2 = checkTreePage(pCheck,pgno,pPage,zContext); | |
| 40727 | + d2 = checkTreePage(pCheck, pgno, zContext); | |
| 39794 | 40728 | if( i>0 && d2!=depth ){ |
| 39795 | 40729 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 39796 | 40730 | } |
| 39797 | 40731 | depth = d2; |
| 39798 | 40732 | } |
| @@ -39804,11 +40738,11 @@ | ||
| 39804 | 40738 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 39805 | 40739 | if( pBt->autoVacuum ){ |
| 39806 | 40740 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
| 39807 | 40741 | } |
| 39808 | 40742 | #endif |
| 39809 | - checkTreePage(pCheck, pgno, pPage, zContext); | |
| 40743 | + checkTreePage(pCheck, pgno, zContext); | |
| 39810 | 40744 | } |
| 39811 | 40745 | |
| 39812 | 40746 | /* Check for complete coverage of the page |
| 39813 | 40747 | */ |
| 39814 | 40748 | data = pPage->aData; |
| @@ -39815,12 +40749,18 @@ | ||
| 39815 | 40749 | hdr = pPage->hdrOffset; |
| 39816 | 40750 | hit = sqlite3PageMalloc( pBt->pageSize ); |
| 39817 | 40751 | if( hit==0 ){ |
| 39818 | 40752 | pCheck->mallocFailed = 1; |
| 39819 | 40753 | }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); | |
| 39822 | 40762 | nCell = get2byte(&data[hdr+3]); |
| 39823 | 40763 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 39824 | 40764 | for(i=0; i<nCell; i++){ |
| 39825 | 40765 | int pc = get2byte(&data[cellStart+i*2]); |
| 39826 | 40766 | u16 size = 1024; |
| @@ -39860,11 +40800,12 @@ | ||
| 39860 | 40800 | checkAppendMsg(pCheck, 0, |
| 39861 | 40801 | "Fragmented space is %d byte reported as %d on page %d", |
| 39862 | 40802 | cnt, data[hdr+7], iPage); |
| 39863 | 40803 | } |
| 39864 | 40804 | } |
| 39865 | - sqlite3PageFree(hit); | |
| 40805 | +check_page_abort: | |
| 40806 | + if (hit) sqlite3PageFree(hit); | |
| 39866 | 40807 | |
| 39867 | 40808 | releasePage(pPage); |
| 39868 | 40809 | return depth+1; |
| 39869 | 40810 | } |
| 39870 | 40811 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| @@ -39885,11 +40826,11 @@ | ||
| 39885 | 40826 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 39886 | 40827 | int nRoot, /* Number of entries in aRoot[] */ |
| 39887 | 40828 | int mxErr, /* Stop reporting errors after this many */ |
| 39888 | 40829 | int *pnErr /* Write number of errors seen to this variable */ |
| 39889 | 40830 | ){ |
| 39890 | - int i; | |
| 40831 | + Pgno i; | |
| 39891 | 40832 | int nRef; |
| 39892 | 40833 | IntegrityCk sCheck; |
| 39893 | 40834 | BtShared *pBt = p->pBt; |
| 39894 | 40835 | char zErr[100]; |
| 39895 | 40836 | |
| @@ -39901,11 +40842,11 @@ | ||
| 39901 | 40842 | sqlite3BtreeLeave(p); |
| 39902 | 40843 | return sqlite3DbStrDup(0, "cannot acquire a read lock on the database"); |
| 39903 | 40844 | } |
| 39904 | 40845 | sCheck.pBt = pBt; |
| 39905 | 40846 | sCheck.pPager = pBt->pPager; |
| 39906 | - sCheck.nPage = pagerPagecount(sCheck.pPager); | |
| 40847 | + sCheck.nPage = pagerPagecount(sCheck.pBt); | |
| 39907 | 40848 | sCheck.mxErr = mxErr; |
| 39908 | 40849 | sCheck.nErr = 0; |
| 39909 | 40850 | sCheck.mallocFailed = 0; |
| 39910 | 40851 | *pnErr = 0; |
| 39911 | 40852 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| @@ -39937,18 +40878,18 @@ | ||
| 39937 | 40878 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 39938 | 40879 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
| 39939 | 40880 | |
| 39940 | 40881 | /* Check all the tables. |
| 39941 | 40882 | */ |
| 39942 | - for(i=0; i<nRoot && sCheck.mxErr; i++){ | |
| 40883 | + for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ | |
| 39943 | 40884 | if( aRoot[i]==0 ) continue; |
| 39944 | 40885 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 39945 | 40886 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 39946 | 40887 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 39947 | 40888 | } |
| 39948 | 40889 | #endif |
| 39949 | - checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); | |
| 40890 | + checkTreePage(&sCheck, aRoot[i], "List of tree roots: "); | |
| 39950 | 40891 | } |
| 39951 | 40892 | |
| 39952 | 40893 | /* Make sure every page in the file is referenced |
| 39953 | 40894 | */ |
| 39954 | 40895 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
| @@ -40068,12 +41009,12 @@ | ||
| 40068 | 41009 | } |
| 40069 | 41010 | if( pBtTo->pCursor ){ |
| 40070 | 41011 | return SQLITE_BUSY; |
| 40071 | 41012 | } |
| 40072 | 41013 | |
| 40073 | - nToPage = pagerPagecount(pBtTo->pPager); | |
| 40074 | - nFromPage = pagerPagecount(pBtFrom->pPager); | |
| 41014 | + nToPage = pagerPagecount(pBtTo); | |
| 41015 | + nFromPage = pagerPagecount(pBtFrom); | |
| 40075 | 41016 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
| 40076 | 41017 | |
| 40077 | 41018 | /* Variable nNewPage is the number of pages required to store the |
| 40078 | 41019 | ** contents of pFrom using the current page-size of pTo. |
| 40079 | 41020 | */ |
| @@ -40417,23 +41358,23 @@ | ||
| 40417 | 41358 | ** |
| 40418 | 41359 | ************************************************************************* |
| 40419 | 41360 | ** This file implements a FIFO queue of rowids used for processing |
| 40420 | 41361 | ** UPDATE and DELETE statements. |
| 40421 | 41362 | ** |
| 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 $ | |
| 40423 | 41364 | */ |
| 40424 | 41365 | |
| 40425 | 41366 | /* |
| 40426 | 41367 | ** Constants FIFOSIZE_FIRST and FIFOSIZE_MAX are the initial |
| 40427 | 41368 | ** number of entries in a fifo page and the maximum number of |
| 40428 | 41369 | ** entries in a fifo page. |
| 40429 | 41370 | */ |
| 40430 | 41371 | #define FIFOSIZE_FIRST (((128-sizeof(FifoPage))/8)+1) |
| 40431 | 41372 | #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) | |
| 40433 | 41374 | #else |
| 40434 | -# define FIFOSIZE_MAX (((262144-sizeof(FifoPage))/8)+1) | |
| 41375 | +# define FIFOSIZE_MAX (int)(((262144-sizeof(FifoPage))/8)+1) | |
| 40435 | 41376 | #endif |
| 40436 | 41377 | |
| 40437 | 41378 | /* |
| 40438 | 41379 | ** Allocate a new FifoPage and return a pointer to it. Return NULL if |
| 40439 | 41380 | ** we run out of memory. Leave space on the page for nEntry entries. |
| @@ -40551,11 +41492,11 @@ | ||
| 40551 | 41492 | ** This file contains code use to manipulate "Mem" structure. A "Mem" |
| 40552 | 41493 | ** stores a single value in the VDBE. Mem is an opaque structure visible |
| 40553 | 41494 | ** only within the VDBE. Interface routines refer to a Mem using the |
| 40554 | 41495 | ** name sqlite_value |
| 40555 | 41496 | ** |
| 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 $ | |
| 40557 | 41498 | */ |
| 40558 | 41499 | |
| 40559 | 41500 | /* |
| 40560 | 41501 | ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) |
| 40561 | 41502 | ** P if required. |
| @@ -40619,13 +41560,10 @@ | ||
| 40619 | 41560 | |
| 40620 | 41561 | if( n<32 ) n = 32; |
| 40621 | 41562 | if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){ |
| 40622 | 41563 | if( preserve && pMem->z==pMem->zMalloc ){ |
| 40623 | 41564 | pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); |
| 40624 | - if( !pMem->z ){ | |
| 40625 | - pMem->flags = MEM_Null; | |
| 40626 | - } | |
| 40627 | 41565 | preserve = 0; |
| 40628 | 41566 | }else{ |
| 40629 | 41567 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
| 40630 | 41568 | pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); |
| 40631 | 41569 | } |
| @@ -40637,11 +41575,15 @@ | ||
| 40637 | 41575 | if( pMem->flags&MEM_Dyn && pMem->xDel ){ |
| 40638 | 41576 | pMem->xDel((void *)(pMem->z)); |
| 40639 | 41577 | } |
| 40640 | 41578 | |
| 40641 | 41579 | 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 | + } | |
| 40643 | 41585 | pMem->xDel = 0; |
| 40644 | 41586 | return (pMem->z ? SQLITE_OK : SQLITE_NOMEM); |
| 40645 | 41587 | } |
| 40646 | 41588 | |
| 40647 | 41589 | /* |
| @@ -41596,11 +42538,11 @@ | ||
| 41596 | 42538 | ** This file contains code used for creating, destroying, and populating |
| 41597 | 42539 | ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior |
| 41598 | 42540 | ** to version 2.8.7, all this code was combined into the vdbe.c source file. |
| 41599 | 42541 | ** But that file was getting too big so this subroutines were split out. |
| 41600 | 42542 | ** |
| 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 $ | |
| 41602 | 42544 | */ |
| 41603 | 42545 | |
| 41604 | 42546 | |
| 41605 | 42547 | |
| 41606 | 42548 | /* |
| @@ -41679,25 +42621,27 @@ | ||
| 41679 | 42621 | p->trace = trace; |
| 41680 | 42622 | } |
| 41681 | 42623 | #endif |
| 41682 | 42624 | |
| 41683 | 42625 | /* |
| 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. | |
| 41686 | 42628 | ** |
| 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). | |
| 41691 | 42633 | */ |
| 41692 | -static void resizeOpArray(Vdbe *p, int N){ | |
| 42634 | +static int growOpArray(Vdbe *p){ | |
| 41693 | 42635 | 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)); | |
| 41695 | 42638 | if( pNew ){ |
| 41696 | - p->nOpAlloc = N; | |
| 42639 | + p->nOpAlloc = nNew; | |
| 41697 | 42640 | p->aOp = pNew; |
| 41698 | 42641 | } |
| 42642 | + return (pNew ? SQLITE_OK : SQLITE_NOMEM); | |
| 41699 | 42643 | } |
| 41700 | 42644 | |
| 41701 | 42645 | /* |
| 41702 | 42646 | ** Add a new instruction to the list of instructions current in the |
| 41703 | 42647 | ** VDBE. Return the address of the new instruction. |
| @@ -41719,12 +42663,11 @@ | ||
| 41719 | 42663 | VdbeOp *pOp; |
| 41720 | 42664 | |
| 41721 | 42665 | i = p->nOp; |
| 41722 | 42666 | assert( p->magic==VDBE_MAGIC_INIT ); |
| 41723 | 42667 | if( p->nOpAlloc<=i ){ |
| 41724 | - resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); | |
| 41725 | - if( p->db->mallocFailed ){ | |
| 42668 | + if( growOpArray(p) ){ | |
| 41726 | 42669 | return 0; |
| 41727 | 42670 | } |
| 41728 | 42671 | } |
| 41729 | 42672 | p->nOp++; |
| 41730 | 42673 | pOp = &p->aOp[i]; |
| @@ -41921,15 +42864,11 @@ | ||
| 41921 | 42864 | ** address of the first operation added. |
| 41922 | 42865 | */ |
| 41923 | 42866 | SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
| 41924 | 42867 | int addr; |
| 41925 | 42868 | 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) ){ | |
| 41931 | 42870 | return 0; |
| 41932 | 42871 | } |
| 41933 | 42872 | addr = p->nOp; |
| 41934 | 42873 | if( nOp>0 ){ |
| 41935 | 42874 | int i; |
| @@ -42310,11 +43249,11 @@ | ||
| 42310 | 43249 | ** |
| 42311 | 43250 | */ |
| 42312 | 43251 | SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
| 42313 | 43252 | int mask; |
| 42314 | 43253 | assert( i>=0 && i<p->db->nDb ); |
| 42315 | - assert( i<sizeof(p->btreeMask)*8 ); | |
| 43254 | + assert( i<(int)sizeof(p->btreeMask)*8 ); | |
| 42316 | 43255 | mask = 1<<i; |
| 42317 | 43256 | if( (p->btreeMask & mask)==0 ){ |
| 42318 | 43257 | p->btreeMask |= mask; |
| 42319 | 43258 | sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt); |
| 42320 | 43259 | } |
| @@ -42596,15 +43535,11 @@ | ||
| 42596 | 43535 | |
| 42597 | 43536 | /* There should be at least one opcode. |
| 42598 | 43537 | */ |
| 42599 | 43538 | assert( p->nOp>0 ); |
| 42600 | 43539 | |
| 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. */ | |
| 42606 | 43541 | p->magic = VDBE_MAGIC_RUN; |
| 42607 | 43542 | |
| 42608 | 43543 | /* For each cursor required, also allocate a memory cell. Memory |
| 42609 | 43544 | ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by |
| 42610 | 43545 | ** the vdbe program. Instead they are used to allocate space for |
| @@ -42620,11 +43555,10 @@ | ||
| 42620 | 43555 | ** Allocation space for registers. |
| 42621 | 43556 | */ |
| 42622 | 43557 | if( p->aMem==0 ){ |
| 42623 | 43558 | int nArg; /* Maximum number of args passed to a user function. */ |
| 42624 | 43559 | resolveP2Values(p, &nArg); |
| 42625 | - /*resizeOpArray(p, p->nOp);*/ | |
| 42626 | 43560 | assert( nVar>=0 ); |
| 42627 | 43561 | if( isExplain && nMem<10 ){ |
| 42628 | 43562 | nMem = 10; |
| 42629 | 43563 | } |
| 42630 | 43564 | p->aMem = sqlite3DbMallocZero(db, |
| @@ -43164,11 +44098,14 @@ | ||
| 43164 | 44098 | ** we do either a commit or rollback of the current transaction. |
| 43165 | 44099 | ** |
| 43166 | 44100 | ** Note: This block also runs if one of the special errors handled |
| 43167 | 44101 | ** above has occurred. |
| 43168 | 44102 | */ |
| 43169 | - if( db->autoCommit && db->writeVdbeCnt==(p->readOnly==0) ){ | |
| 44103 | + if( !sqlite3VtabInSync(db) | |
| 44104 | + && db->autoCommit | |
| 44105 | + && db->writeVdbeCnt==(p->readOnly==0) | |
| 44106 | + ){ | |
| 43170 | 44107 | if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
| 43171 | 44108 | /* The auto-commit flag is true, and the vdbe program was |
| 43172 | 44109 | ** successful or hit an 'OR FAIL' constraint. This means a commit |
| 43173 | 44110 | ** is required. |
| 43174 | 44111 | */ |
| @@ -43791,12 +44728,12 @@ | ||
| 43791 | 44728 | UnpackedRecord *pSpace,/* Space available to hold resulting object */ |
| 43792 | 44729 | int szSpace /* Size of pSpace[] in bytes */ |
| 43793 | 44730 | ){ |
| 43794 | 44731 | const unsigned char *aKey = (const unsigned char *)pKey; |
| 43795 | 44732 | UnpackedRecord *p; |
| 43796 | - int nByte; | |
| 43797 | - int idx, d; | |
| 44733 | + int nByte, d; | |
| 44734 | + u32 idx; | |
| 43798 | 44735 | u16 u; /* Unsigned loop counter */ |
| 43799 | 44736 | u32 szHdr; |
| 43800 | 44737 | Mem *pMem; |
| 43801 | 44738 | |
| 43802 | 44739 | assert( sizeof(Mem)>sizeof(*p) ); |
| @@ -43816,11 +44753,11 @@ | ||
| 43816 | 44753 | d = szHdr; |
| 43817 | 44754 | u = 0; |
| 43818 | 44755 | while( idx<szHdr && u<p->nField ){ |
| 43819 | 44756 | u32 serial_type; |
| 43820 | 44757 | |
| 43821 | - idx += getVarint32( aKey+idx, serial_type); | |
| 44758 | + idx += getVarint32(&aKey[idx], serial_type); | |
| 43822 | 44759 | if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break; |
| 43823 | 44760 | pMem->enc = pKeyInfo->enc; |
| 43824 | 44761 | pMem->db = pKeyInfo->db; |
| 43825 | 44762 | pMem->flags = 0; |
| 43826 | 44763 | pMem->zMalloc = 0; |
| @@ -43881,11 +44818,11 @@ | ||
| 43881 | 44818 | */ |
| 43882 | 44819 | SQLITE_PRIVATE int sqlite3VdbeRecordCompare( |
| 43883 | 44820 | int nKey1, const void *pKey1, /* Left key */ |
| 43884 | 44821 | UnpackedRecord *pPKey2 /* Right key */ |
| 43885 | 44822 | ){ |
| 43886 | - u32 d1; /* Offset into aKey[] of next data element */ | |
| 44823 | + int d1; /* Offset into aKey[] of next data element */ | |
| 43887 | 44824 | u32 idx1; /* Offset into aKey[] of next header element */ |
| 43888 | 44825 | u32 szHdr1; /* Number of bytes in header */ |
| 43889 | 44826 | int i = 0; |
| 43890 | 44827 | int nField; |
| 43891 | 44828 | int rc = 0; |
| @@ -44084,11 +45021,11 @@ | ||
| 44084 | 45021 | ************************************************************************* |
| 44085 | 45022 | ** |
| 44086 | 45023 | ** This file contains code use to implement APIs that are part of the |
| 44087 | 45024 | ** VDBE. |
| 44088 | 45025 | ** |
| 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 $ | |
| 44090 | 45027 | */ |
| 44091 | 45028 | |
| 44092 | 45029 | #if 0 && defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) |
| 44093 | 45030 | /* |
| 44094 | 45031 | ** The following structure contains pointers to the end points of a |
| @@ -44670,15 +45607,16 @@ | ||
| 44670 | 45607 | ** for name resolution but are actually overloaded by the xFindFunction |
| 44671 | 45608 | ** method of virtual tables. |
| 44672 | 45609 | */ |
| 44673 | 45610 | SQLITE_PRIVATE void sqlite3InvalidFunction( |
| 44674 | 45611 | 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 */ | |
| 44677 | 45614 | ){ |
| 44678 | 45615 | const char *zName = context->pFunc->zName; |
| 44679 | 45616 | char *zErr; |
| 45617 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 44680 | 45618 | zErr = sqlite3MPrintf(0, |
| 44681 | 45619 | "unable to use function %s in the requested context", zName); |
| 44682 | 45620 | sqlite3_result_error(context, zErr, -1); |
| 44683 | 45621 | sqlite3_free(zErr); |
| 44684 | 45622 | } |
| @@ -45423,11 +46361,11 @@ | ||
| 45423 | 46361 | ** documentation, headers files, or other derived files. The formatting |
| 45424 | 46362 | ** of the code in this file is, therefore, important. See other comments |
| 45425 | 46363 | ** in this file for details. If in doubt, do not deviate from existing |
| 45426 | 46364 | ** commenting and indentation practices when changing or adding code. |
| 45427 | 46365 | ** |
| 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 $ | |
| 45429 | 46367 | */ |
| 45430 | 46368 | |
| 45431 | 46369 | /* |
| 45432 | 46370 | ** The following global variable is incremented every time a cursor |
| 45433 | 46371 | ** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test |
| @@ -45554,11 +46492,11 @@ | ||
| 45554 | 46492 | /* |
| 45555 | 46493 | ** Return true if an opcode has any of the OPFLG_xxx properties |
| 45556 | 46494 | ** specified by mask. |
| 45557 | 46495 | */ |
| 45558 | 46496 | SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){ |
| 45559 | - assert( opcode>0 && opcode<sizeof(opcodeProperty) ); | |
| 46497 | + assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) ); | |
| 45560 | 46498 | return (opcodeProperty[opcode]&mask)!=0; |
| 45561 | 46499 | } |
| 45562 | 46500 | |
| 45563 | 46501 | /* |
| 45564 | 46502 | ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL |
| @@ -46019,11 +46957,10 @@ | ||
| 46019 | 46957 | #endif |
| 46020 | 46958 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 46021 | 46959 | int nProgressOps = 0; /* Opcodes executed since progress callback. */ |
| 46022 | 46960 | #endif |
| 46023 | 46961 | UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */ |
| 46024 | - | |
| 46025 | 46962 | |
| 46026 | 46963 | assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ |
| 46027 | 46964 | assert( db->magic==SQLITE_MAGIC_BUSY ); |
| 46028 | 46965 | sqlite3BtreeMutexArrayEnter(&p->aMutex); |
| 46029 | 46966 | if( p->rc==SQLITE_NOMEM ){ |
| @@ -47432,19 +48369,19 @@ | ||
| 47432 | 48369 | ** If the column contains fewer than P2 fields, then extract a NULL. Or, |
| 47433 | 48370 | ** if the P4 argument is a P4_MEM use the value of the P4 argument as |
| 47434 | 48371 | ** the result. |
| 47435 | 48372 | */ |
| 47436 | 48373 | case OP_Column: { |
| 47437 | - u32 payloadSize; /* Number of bytes in the record */ | |
| 48374 | + int payloadSize; /* Number of bytes in the record */ | |
| 47438 | 48375 | int p1 = pOp->p1; /* P1 value of the opcode */ |
| 47439 | 48376 | int p2 = pOp->p2; /* column number to retrieve */ |
| 47440 | 48377 | VdbeCursor *pC = 0;/* The VDBE cursor */ |
| 47441 | 48378 | char *zRec; /* Pointer to complete record-data */ |
| 47442 | 48379 | BtCursor *pCrsr; /* The BTree cursor */ |
| 47443 | 48380 | u32 *aType; /* aType[i] holds the numeric type of the i-th column */ |
| 47444 | 48381 | 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 */ | |
| 47446 | 48383 | int len; /* The length of the serialized data for the column */ |
| 47447 | 48384 | int i; /* Loop counter */ |
| 47448 | 48385 | char *zData; /* Part of the record being decoded */ |
| 47449 | 48386 | Mem *pDest; /* Where to write the extracted value */ |
| 47450 | 48387 | Mem sMem; /* For storing the record being decoded */ |
| @@ -47488,11 +48425,11 @@ | ||
| 47488 | 48425 | }else if( pC->isIndex ){ |
| 47489 | 48426 | i64 payloadSize64; |
| 47490 | 48427 | sqlite3BtreeKeySize(pCrsr, &payloadSize64); |
| 47491 | 48428 | payloadSize = payloadSize64; |
| 47492 | 48429 | }else{ |
| 47493 | - sqlite3BtreeDataSize(pCrsr, &payloadSize); | |
| 48430 | + sqlite3BtreeDataSize(pCrsr, (u32 *)&payloadSize); | |
| 47494 | 48431 | } |
| 47495 | 48432 | nField = pC->nField; |
| 47496 | 48433 | }else{ |
| 47497 | 48434 | assert( pC->pseudoTable ); |
| 47498 | 48435 | /* The record is the sole entry of a pseudo-table */ |
| @@ -47522,11 +48459,11 @@ | ||
| 47522 | 48459 | if( pC->cacheStatus==p->cacheCtr ){ |
| 47523 | 48460 | aOffset = pC->aOffset; |
| 47524 | 48461 | }else{ |
| 47525 | 48462 | u8 *zIdx; /* Index into header */ |
| 47526 | 48463 | u8 *zEndHdr; /* Pointer to first byte after the header */ |
| 47527 | - u32 offset; /* Offset into the data */ | |
| 48464 | + int offset; /* Offset into the data */ | |
| 47528 | 48465 | int szHdrSz; /* Size of the header size field at start of record */ |
| 47529 | 48466 | int avail; /* Number of bytes of available data */ |
| 47530 | 48467 | |
| 47531 | 48468 | assert(aType); |
| 47532 | 48469 | pC->aOffset = aOffset = &aType[nField]; |
| @@ -47723,11 +48660,11 @@ | ||
| 47723 | 48660 | */ |
| 47724 | 48661 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 47725 | 48662 | Mem *pRec; /* The new record */ |
| 47726 | 48663 | u64 nData = 0; /* Number of bytes of data space */ |
| 47727 | 48664 | 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 */ | |
| 47729 | 48666 | int nZero = 0; /* Number of zero bytes at the end of the record */ |
| 47730 | 48667 | int nVarint; /* Number of bytes in a varint */ |
| 47731 | 48668 | u32 serial_type; /* Type field */ |
| 47732 | 48669 | Mem *pData0; /* First field to be combined into the record */ |
| 47733 | 48670 | Mem *pLast; /* Last field of the record */ |
| @@ -48166,11 +49103,14 @@ | ||
| 48166 | 49103 | assert( p2>0 ); |
| 48167 | 49104 | assert( p2<=p->nMem ); |
| 48168 | 49105 | pIn2 = &p->aMem[p2]; |
| 48169 | 49106 | sqlite3VdbeMemIntegerify(pIn2); |
| 48170 | 49107 | p2 = pIn2->u.i; |
| 48171 | - assert( p2>=2 ); | |
| 49108 | + if( p2<2 ) { | |
| 49109 | + rc = SQLITE_CORRUPT_BKPT; | |
| 49110 | + goto abort_due_to_error; | |
| 49111 | + } | |
| 48172 | 49112 | } |
| 48173 | 49113 | assert( i>=0 ); |
| 48174 | 49114 | pCur = allocateCursor(p, i, &pOp[-1], iDb, 1); |
| 48175 | 49115 | if( pCur==0 ) goto no_mem; |
| 48176 | 49116 | pCur->nullRow = 1; |
| @@ -48793,11 +49733,11 @@ | ||
| 48793 | 49733 | #else |
| 48794 | 49734 | /* Some compilers complain about constants of the form 0x7fffffffffffffff. |
| 48795 | 49735 | ** Others complain about 0x7ffffffffffffffffLL. The following macro seems |
| 48796 | 49736 | ** to provide the constant while making all compilers happy. |
| 48797 | 49737 | */ |
| 48798 | -# define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) | |
| 49738 | +# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) | |
| 48799 | 49739 | #endif |
| 48800 | 49740 | |
| 48801 | 49741 | if( !pC->useRandomRowid ){ |
| 48802 | 49742 | if( pC->nextRowidValid ){ |
| 48803 | 49743 | v = pC->nextRowid; |
| @@ -49099,11 +50039,11 @@ | ||
| 49099 | 50039 | goto too_big; |
| 49100 | 50040 | } |
| 49101 | 50041 | n = n64; |
| 49102 | 50042 | }else{ |
| 49103 | 50043 | sqlite3BtreeDataSize(pCrsr, &n); |
| 49104 | - if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
| 50044 | + if( (int)n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
| 49105 | 50045 | goto too_big; |
| 49106 | 50046 | } |
| 49107 | 50047 | } |
| 49108 | 50048 | if( sqlite3VdbeMemGrow(pOut, n, 0) ){ |
| 49109 | 50049 | goto no_mem; |
| @@ -51213,11 +52153,11 @@ | ||
| 51213 | 52153 | ** |
| 51214 | 52154 | ** This file contains code use to implement an in-memory rollback journal. |
| 51215 | 52155 | ** The in-memory rollback journal is used to journal transactions for |
| 51216 | 52156 | ** ":memory:" databases and when the journal_mode=MEMORY pragma is used. |
| 51217 | 52157 | ** |
| 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 $ | |
| 51219 | 52159 | */ |
| 51220 | 52160 | |
| 51221 | 52161 | /* Forward references to internal structures */ |
| 51222 | 52162 | typedef struct MemJournal MemJournal; |
| 51223 | 52163 | typedef struct FilePoint FilePoint; |
| @@ -51228,11 +52168,13 @@ | ||
| 51228 | 52168 | */ |
| 51229 | 52169 | #define JOURNAL_CHUNKSIZE 1024 |
| 51230 | 52170 | |
| 51231 | 52171 | /* Macro to find the minimum of two numeric values. |
| 51232 | 52172 | */ |
| 51233 | -#define MIN(x,y) ((x)<(y)?(x):(y)) | |
| 52173 | +#ifndef MIN | |
| 52174 | +# define MIN(x,y) ((x)<(y)?(x):(y)) | |
| 52175 | +#endif | |
| 51234 | 52176 | |
| 51235 | 52177 | /* |
| 51236 | 52178 | ** The rollback journal is composed of a linked list of these structures. |
| 51237 | 52179 | */ |
| 51238 | 52180 | struct FileChunk { |
| @@ -51319,10 +52261,11 @@ | ||
| 51319 | 52261 | |
| 51320 | 52262 | /* An in-memory journal file should only ever be appended to. Random |
| 51321 | 52263 | ** access writes are not required by sqlite. |
| 51322 | 52264 | */ |
| 51323 | 52265 | assert(iOfst==p->endpoint.iOffset); |
| 52266 | + UNUSED_PARAMETER(iOfst); | |
| 51324 | 52267 | |
| 51325 | 52268 | while( nWrite>0 ){ |
| 51326 | 52269 | FileChunk *pChunk = p->endpoint.pChunk; |
| 51327 | 52270 | int iChunkOffset = p->endpoint.iOffset%JOURNAL_CHUNKSIZE; |
| 51328 | 52271 | int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset); |
| @@ -51358,10 +52301,11 @@ | ||
| 51358 | 52301 | */ |
| 51359 | 52302 | static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ |
| 51360 | 52303 | MemJournal *p = (MemJournal *)pJfd; |
| 51361 | 52304 | FileChunk *pChunk; |
| 51362 | 52305 | assert(size==0); |
| 52306 | + UNUSED_PARAMETER(size); | |
| 51363 | 52307 | pChunk = p->pFirst; |
| 51364 | 52308 | while( pChunk ){ |
| 51365 | 52309 | FileChunk *pTmp = pChunk; |
| 51366 | 52310 | pChunk = pChunk->pNext; |
| 51367 | 52311 | sqlite3_free(pTmp); |
| @@ -51380,11 +52324,12 @@ | ||
| 51380 | 52324 | |
| 51381 | 52325 | |
| 51382 | 52326 | /* |
| 51383 | 52327 | ** Sync the file. |
| 51384 | 52328 | */ |
| 51385 | -static int memjrnlSync(sqlite3_file *pJfd, int flags){ | |
| 52329 | +static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){ | |
| 52330 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 51386 | 52331 | return SQLITE_OK; |
| 51387 | 52332 | } |
| 51388 | 52333 | |
| 51389 | 52334 | /* |
| 51390 | 52335 | ** Query the size of the file in bytes. |
| @@ -51589,11 +52534,11 @@ | ||
| 51589 | 52534 | ** |
| 51590 | 52535 | ** This file contains routines used for walking the parser tree and |
| 51591 | 52536 | ** resolve all identifiers by associating them with a particular |
| 51592 | 52537 | ** table and column. |
| 51593 | 52538 | ** |
| 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 $ | |
| 51595 | 52540 | */ |
| 51596 | 52541 | |
| 51597 | 52542 | /* |
| 51598 | 52543 | ** Turn the pExpr expression into an alias for the iCol-th column of the |
| 51599 | 52544 | ** result set in pEList. |
| @@ -51921,13 +52866,13 @@ | ||
| 51921 | 52866 | ** column number is greater than the number of bits in the bitmask |
| 51922 | 52867 | ** then set the high-order bit of the bitmask. |
| 51923 | 52868 | */ |
| 51924 | 52869 | if( pExpr->iColumn>=0 && pMatch!=0 ){ |
| 51925 | 52870 | 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; | |
| 51929 | 52874 | } |
| 51930 | 52875 | assert( pMatch->iCursor==pExpr->iTable ); |
| 51931 | 52876 | pMatch->colUsed |= ((Bitmask)1)<<n; |
| 51932 | 52877 | } |
| 51933 | 52878 | |
| @@ -52756,11 +53701,11 @@ | ||
| 52756 | 53701 | ** |
| 52757 | 53702 | ************************************************************************* |
| 52758 | 53703 | ** This file contains routines used for analyzing expressions and |
| 52759 | 53704 | ** for generating VDBE code that evaluates expressions in SQLite. |
| 52760 | 53705 | ** |
| 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 $ | |
| 52762 | 53707 | */ |
| 52763 | 53708 | |
| 52764 | 53709 | /* |
| 52765 | 53710 | ** Return the 'affinity' of the expression pExpr if any. |
| 52766 | 53711 | ** |
| @@ -52785,11 +53730,13 @@ | ||
| 52785 | 53730 | #ifndef SQLITE_OMIT_CAST |
| 52786 | 53731 | if( op==TK_CAST ){ |
| 52787 | 53732 | return sqlite3AffinityType(&pExpr->token); |
| 52788 | 53733 | } |
| 52789 | 53734 | #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 | + ){ | |
| 52791 | 53738 | /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally |
| 52792 | 53739 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 52793 | 53740 | int j = pExpr->iColumn; |
| 52794 | 53741 | if( j<0 ) return SQLITE_AFF_INTEGER; |
| 52795 | 53742 | assert( pExpr->pTab && j<pExpr->pTab->nCol ); |
| @@ -52831,11 +53778,11 @@ | ||
| 52831 | 53778 | while( p ){ |
| 52832 | 53779 | int op; |
| 52833 | 53780 | pColl = p->pColl; |
| 52834 | 53781 | if( pColl ) break; |
| 52835 | 53782 | 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 ){ | |
| 52837 | 53784 | /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally |
| 52838 | 53785 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 52839 | 53786 | const char *zColl; |
| 52840 | 53787 | int j = p->iColumn; |
| 52841 | 53788 | if( j>=0 ){ |
| @@ -53679,11 +54626,12 @@ | ||
| 53679 | 54626 | return WRC_Abort; |
| 53680 | 54627 | default: |
| 53681 | 54628 | return WRC_Continue; |
| 53682 | 54629 | } |
| 53683 | 54630 | } |
| 53684 | -static int selectNodeIsConstant(Walker *pWalker, Select *pSelect){ | |
| 54631 | +static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ | |
| 54632 | + UNUSED_PARAMETER(NotUsed); | |
| 53685 | 54633 | pWalker->u.i = 0; |
| 53686 | 54634 | return WRC_Abort; |
| 53687 | 54635 | } |
| 53688 | 54636 | static int exprIsConst(Expr *p, int initFlag){ |
| 53689 | 54637 | Walker w; |
| @@ -54199,14 +55147,15 @@ | ||
| 54199 | 55147 | ** z[n] character is guaranteed to be something that does not look |
| 54200 | 55148 | ** like the continuation of the number. |
| 54201 | 55149 | */ |
| 54202 | 55150 | static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ |
| 54203 | 55151 | assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); |
| 55152 | + assert( !z || !isdigit(z[n]) ); | |
| 55153 | + UNUSED_PARAMETER(n); | |
| 54204 | 55154 | if( z ){ |
| 54205 | 55155 | double value; |
| 54206 | 55156 | char *zV; |
| 54207 | - assert( !isdigit(z[n]) ); | |
| 54208 | 55157 | sqlite3AtoF(z, &value); |
| 54209 | 55158 | if( sqlite3IsNaN(value) ){ |
| 54210 | 55159 | sqlite3VdbeAddOp2(v, OP_Null, 0, iMem); |
| 54211 | 55160 | }else{ |
| 54212 | 55161 | if( negateFlag ) value = -value; |
| @@ -55854,11 +56803,11 @@ | ||
| 55854 | 56803 | ** |
| 55855 | 56804 | ************************************************************************* |
| 55856 | 56805 | ** This file contains C code routines that used to generate VDBE code |
| 55857 | 56806 | ** that implements the ALTER TABLE command. |
| 55858 | 56807 | ** |
| 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 $ | |
| 55860 | 56809 | */ |
| 55861 | 56810 | |
| 55862 | 56811 | /* |
| 55863 | 56812 | ** The code in this file only exists if we are not omitting the |
| 55864 | 56813 | ** ALTER TABLE logic from the build. |
| @@ -55879,11 +56828,11 @@ | ||
| 55879 | 56828 | ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def') |
| 55880 | 56829 | ** -> 'CREATE INDEX i ON def(a, b, c)' |
| 55881 | 56830 | */ |
| 55882 | 56831 | static void renameTableFunc( |
| 55883 | 56832 | sqlite3_context *context, |
| 55884 | - int argc, | |
| 56833 | + int NotUsed, | |
| 55885 | 56834 | sqlite3_value **argv |
| 55886 | 56835 | ){ |
| 55887 | 56836 | unsigned char const *zSql = sqlite3_value_text(argv[0]); |
| 55888 | 56837 | unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
| 55889 | 56838 | |
| @@ -55892,10 +56841,12 @@ | ||
| 55892 | 56841 | unsigned char const *zCsr = zSql; |
| 55893 | 56842 | int len = 0; |
| 55894 | 56843 | char *zRet; |
| 55895 | 56844 | |
| 55896 | 56845 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 56846 | + | |
| 56847 | + UNUSED_PARAMETER(NotUsed); | |
| 55897 | 56848 | |
| 55898 | 56849 | /* The principle used to locate the table name in the CREATE TABLE |
| 55899 | 56850 | ** statement is that the table name is the first non-space token that |
| 55900 | 56851 | ** is immediately followed by a TK_LP or TK_USING token. |
| 55901 | 56852 | */ |
| @@ -55934,11 +56885,11 @@ | ||
| 55934 | 56885 | ** returned. This is analagous to renameTableFunc() above, except for CREATE |
| 55935 | 56886 | ** TRIGGER, not CREATE INDEX and CREATE TABLE. |
| 55936 | 56887 | */ |
| 55937 | 56888 | static void renameTriggerFunc( |
| 55938 | 56889 | sqlite3_context *context, |
| 55939 | - int argc, | |
| 56890 | + int NotUsed, | |
| 55940 | 56891 | sqlite3_value **argv |
| 55941 | 56892 | ){ |
| 55942 | 56893 | unsigned char const *zSql = sqlite3_value_text(argv[0]); |
| 55943 | 56894 | unsigned char const *zTableName = sqlite3_value_text(argv[1]); |
| 55944 | 56895 | |
| @@ -55946,12 +56897,13 @@ | ||
| 55946 | 56897 | Token tname; |
| 55947 | 56898 | int dist = 3; |
| 55948 | 56899 | unsigned char const *zCsr = zSql; |
| 55949 | 56900 | int len = 0; |
| 55950 | 56901 | char *zRet; |
| 55951 | - | |
| 55952 | 56902 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 56903 | + | |
| 56904 | + UNUSED_PARAMETER(NotUsed); | |
| 55953 | 56905 | |
| 55954 | 56906 | /* The principle used to locate the table name in the CREATE TRIGGER |
| 55955 | 56907 | ** statement is that the table name is the first token that is immediatedly |
| 55956 | 56908 | ** preceded by either TK_ON or TK_DOT and immediatedly followed by one |
| 55957 | 56909 | ** of TK_WHEN, TK_BEGIN or TK_FOR. |
| @@ -56475,11 +57427,11 @@ | ||
| 56475 | 57427 | ** May you share freely, never taking more than you give. |
| 56476 | 57428 | ** |
| 56477 | 57429 | ************************************************************************* |
| 56478 | 57430 | ** This file contains code associated with the ANALYZE command. |
| 56479 | 57431 | ** |
| 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 $ | |
| 56481 | 57433 | */ |
| 56482 | 57434 | #ifndef SQLITE_OMIT_ANALYZE |
| 56483 | 57435 | |
| 56484 | 57436 | /* |
| 56485 | 57437 | ** This routine generates code that opens the sqlite_stat1 table on cursor |
| @@ -56819,18 +57771,20 @@ | ||
| 56819 | 57771 | ** sqlite_stat1 table. |
| 56820 | 57772 | ** |
| 56821 | 57773 | ** argv[0] = name of the index |
| 56822 | 57774 | ** argv[1] = results of analysis - on integer for each column |
| 56823 | 57775 | */ |
| 56824 | -static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){ | |
| 57776 | +static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ | |
| 56825 | 57777 | analysisInfo *pInfo = (analysisInfo*)pData; |
| 56826 | 57778 | Index *pIndex; |
| 56827 | 57779 | int i, c; |
| 56828 | 57780 | unsigned int v; |
| 56829 | 57781 | const char *z; |
| 56830 | 57782 | |
| 56831 | 57783 | assert( argc==2 ); |
| 57784 | + UNUSED_PARAMETER2(NotUsed, argc); | |
| 57785 | + | |
| 56832 | 57786 | if( argv==0 || argv[0]==0 || argv[1]==0 ){ |
| 56833 | 57787 | return 0; |
| 56834 | 57788 | } |
| 56835 | 57789 | pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase); |
| 56836 | 57790 | if( pIndex==0 ){ |
| @@ -56902,11 +57856,11 @@ | ||
| 56902 | 57856 | ** May you share freely, never taking more than you give. |
| 56903 | 57857 | ** |
| 56904 | 57858 | ************************************************************************* |
| 56905 | 57859 | ** This file contains code used to implement the ATTACH and DETACH commands. |
| 56906 | 57860 | ** |
| 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 $ | |
| 56908 | 57862 | */ |
| 56909 | 57863 | |
| 56910 | 57864 | #ifndef SQLITE_OMIT_ATTACH |
| 56911 | 57865 | /* |
| 56912 | 57866 | ** Resolve an expression that was part of an ATTACH or DETACH statement. This |
| @@ -56954,11 +57908,11 @@ | ||
| 56954 | 57908 | ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the |
| 56955 | 57909 | ** third argument. |
| 56956 | 57910 | */ |
| 56957 | 57911 | static void attachFunc( |
| 56958 | 57912 | sqlite3_context *context, |
| 56959 | - int argc, | |
| 57913 | + int NotUsed, | |
| 56960 | 57914 | sqlite3_value **argv |
| 56961 | 57915 | ){ |
| 56962 | 57916 | int i; |
| 56963 | 57917 | int rc = 0; |
| 56964 | 57918 | sqlite3 *db = sqlite3_context_db_handle(context); |
| @@ -56965,10 +57919,12 @@ | ||
| 56965 | 57919 | const char *zName; |
| 56966 | 57920 | const char *zFile; |
| 56967 | 57921 | Db *aNew; |
| 56968 | 57922 | char *zErrDyn = 0; |
| 56969 | 57923 | char zErr[128]; |
| 57924 | + | |
| 57925 | + UNUSED_PARAMETER(NotUsed); | |
| 56970 | 57926 | |
| 56971 | 57927 | zFile = (const char *)sqlite3_value_text(argv[0]); |
| 56972 | 57928 | zName = (const char *)sqlite3_value_text(argv[1]); |
| 56973 | 57929 | if( zFile==0 ) zFile = ""; |
| 56974 | 57930 | if( zName==0 ) zName = ""; |
| @@ -57122,18 +58078,20 @@ | ||
| 57122 | 58078 | ** |
| 57123 | 58079 | ** SELECT sqlite_detach(x) |
| 57124 | 58080 | */ |
| 57125 | 58081 | static void detachFunc( |
| 57126 | 58082 | sqlite3_context *context, |
| 57127 | - int argc, | |
| 58083 | + int NotUsed, | |
| 57128 | 58084 | sqlite3_value **argv |
| 57129 | 58085 | ){ |
| 57130 | 58086 | const char *zName = (const char *)sqlite3_value_text(argv[0]); |
| 57131 | 58087 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 57132 | 58088 | int i; |
| 57133 | 58089 | Db *pDb = 0; |
| 57134 | 58090 | char zErr[128]; |
| 58091 | + | |
| 58092 | + UNUSED_PARAMETER(NotUsed); | |
| 57135 | 58093 | |
| 57136 | 58094 | if( zName==0 ) zName = ""; |
| 57137 | 58095 | for(i=0; i<db->nDb; i++){ |
| 57138 | 58096 | pDb = &db->aDb[i]; |
| 57139 | 58097 | if( pDb->pBt==0 ) continue; |
| @@ -57685,11 +58643,11 @@ | ||
| 57685 | 58643 | ** creating ID lists |
| 57686 | 58644 | ** BEGIN TRANSACTION |
| 57687 | 58645 | ** COMMIT |
| 57688 | 58646 | ** ROLLBACK |
| 57689 | 58647 | ** |
| 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 $ | |
| 57691 | 58649 | */ |
| 57692 | 58650 | |
| 57693 | 58651 | /* |
| 57694 | 58652 | ** This routine is called when a new SQL statement is beginning to |
| 57695 | 58653 | ** be parsed. Initialize the pParse structure as needed. |
| @@ -58287,11 +59245,11 @@ | ||
| 58287 | 59245 | ** index of the named database in db->aDb[], or -1 if the named db |
| 58288 | 59246 | ** does not exist. |
| 58289 | 59247 | */ |
| 58290 | 59248 | SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){ |
| 58291 | 59249 | int i = -1; /* Database number */ |
| 58292 | - int n; /* Number of characters in the name */ | |
| 59250 | + size_t n; /* Number of characters in the name */ | |
| 58293 | 59251 | Db *pDb; /* A database whose name space is being searched */ |
| 58294 | 59252 | char *zName; /* Name we are searching for */ |
| 58295 | 59253 | |
| 58296 | 59254 | zName = sqlite3NameFromToken(db, pName); |
| 58297 | 59255 | if( zName ){ |
| @@ -58331,11 +59289,15 @@ | ||
| 58331 | 59289 | ){ |
| 58332 | 59290 | int iDb; /* Database holding the object */ |
| 58333 | 59291 | sqlite3 *db = pParse->db; |
| 58334 | 59292 | |
| 58335 | 59293 | 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 | + } | |
| 58337 | 59299 | *pUnqual = pName2; |
| 58338 | 59300 | iDb = sqlite3FindDb(db, pName1); |
| 58339 | 59301 | if( iDb<0 ){ |
| 58340 | 59302 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
| 58341 | 59303 | pParse->nErr++; |
| @@ -59053,11 +60015,11 @@ | ||
| 59053 | 60015 | k += strlen(&zStmt[k]); |
| 59054 | 60016 | zSep = zSep2; |
| 59055 | 60017 | identPut(zStmt, &k, pCol->zName); |
| 59056 | 60018 | if( (z = pCol->zType)!=0 ){ |
| 59057 | 60019 | zStmt[k++] = ' '; |
| 59058 | - assert( strlen(z)+k+1<=n ); | |
| 60020 | + assert( (int)(strlen(z)+k+1)<=n ); | |
| 59059 | 60021 | sqlite3_snprintf(n-k, &zStmt[k], "%s", z); |
| 59060 | 60022 | k += strlen(z); |
| 59061 | 60023 | } |
| 59062 | 60024 | } |
| 59063 | 60025 | sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); |
| @@ -60651,16 +61613,86 @@ | ||
| 60651 | 61613 | for(i=0; i<pList->nId; i++){ |
| 60652 | 61614 | if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
| 60653 | 61615 | } |
| 60654 | 61616 | return -1; |
| 60655 | 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 | + | |
| 60656 | 61685 | |
| 60657 | 61686 | /* |
| 60658 | 61687 | ** Append a new table name to the given SrcList. Create a new SrcList if |
| 60659 | 61688 | ** need be. A new entry is created in the SrcList even if pToken is NULL. |
| 60660 | 61689 | ** |
| 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. | |
| 60662 | 61694 | ** |
| 60663 | 61695 | ** If pDatabase is not null, it means that the table has an optional |
| 60664 | 61696 | ** database name prefix. Like this: "database.table". The pDatabase |
| 60665 | 61697 | ** points to the table name and the pTable points to the database name. |
| 60666 | 61698 | ** The SrcList.a[].zName field is filled with the table name which might |
| @@ -60689,23 +61721,16 @@ | ||
| 60689 | 61721 | if( pList==0 ){ |
| 60690 | 61722 | pList = sqlite3DbMallocZero(db, sizeof(SrcList) ); |
| 60691 | 61723 | if( pList==0 ) return 0; |
| 60692 | 61724 | pList->nAlloc = 1; |
| 60693 | 61725 | } |
| 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]; | |
| 60707 | 61732 | if( pDatabase && pDatabase->z==0 ){ |
| 60708 | 61733 | pDatabase = 0; |
| 60709 | 61734 | } |
| 60710 | 61735 | if( pDatabase && pTable ){ |
| 60711 | 61736 | Token *pTemp = pDatabase; |
| @@ -60712,12 +61737,10 @@ | ||
| 60712 | 61737 | pDatabase = pTable; |
| 60713 | 61738 | pTable = pTemp; |
| 60714 | 61739 | } |
| 60715 | 61740 | pItem->zName = sqlite3NameFromToken(db, pTable); |
| 60716 | 61741 | pItem->zDatabase = sqlite3NameFromToken(db, pDatabase); |
| 60717 | - pItem->iCursor = -1; | |
| 60718 | - pList->nSrc++; | |
| 60719 | 61742 | return pList; |
| 60720 | 61743 | } |
| 60721 | 61744 | |
| 60722 | 61745 | /* |
| 60723 | 61746 | ** Assign VdbeCursor index numbers to all tables in a SrcList |
| @@ -61644,11 +62667,11 @@ | ||
| 61644 | 62667 | ** |
| 61645 | 62668 | ************************************************************************* |
| 61646 | 62669 | ** This file contains C code routines that are called by the parser |
| 61647 | 62670 | ** in order to generate code for DELETE FROM statements. |
| 61648 | 62671 | ** |
| 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 $ | |
| 61650 | 62673 | */ |
| 61651 | 62674 | |
| 61652 | 62675 | /* |
| 61653 | 62676 | ** Look up every table that is named in pSrc. If any table is not found, |
| 61654 | 62677 | ** add an error message to pParse->zErrMsg and return NULL. If all tables |
| @@ -61896,11 +62919,11 @@ | ||
| 61896 | 62919 | |
| 61897 | 62920 | /* Figure out if we have any triggers and if the table being |
| 61898 | 62921 | ** deleted from is a view |
| 61899 | 62922 | */ |
| 61900 | 62923 | #ifndef SQLITE_OMIT_TRIGGER |
| 61901 | - triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0); | |
| 62924 | + triggers_exist = sqlite3TriggersExist(pTab, TK_DELETE, 0); | |
| 61902 | 62925 | isView = pTab->pSelect!=0; |
| 61903 | 62926 | #else |
| 61904 | 62927 | # define triggers_exist 0 |
| 61905 | 62928 | # define isView 0 |
| 61906 | 62929 | #endif |
| @@ -62285,11 +63308,11 @@ | ||
| 62285 | 63308 | ** |
| 62286 | 63309 | ** There is only one exported symbol in this file - the function |
| 62287 | 63310 | ** sqliteRegisterBuildinFunctions() found at the bottom of the file. |
| 62288 | 63311 | ** All other code has file scope. |
| 62289 | 63312 | ** |
| 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 $ | |
| 62291 | 63314 | */ |
| 62292 | 63315 | |
| 62293 | 63316 | /* |
| 62294 | 63317 | ** Return the collating function associated with a function. |
| 62295 | 63318 | */ |
| @@ -62329,14 +63352,15 @@ | ||
| 62329 | 63352 | /* |
| 62330 | 63353 | ** Return the type of the argument. |
| 62331 | 63354 | */ |
| 62332 | 63355 | static void typeofFunc( |
| 62333 | 63356 | sqlite3_context *context, |
| 62334 | - int argc, | |
| 63357 | + int NotUsed, | |
| 62335 | 63358 | sqlite3_value **argv |
| 62336 | 63359 | ){ |
| 62337 | 63360 | const char *z = 0; |
| 63361 | + UNUSED_PARAMETER(NotUsed); | |
| 62338 | 63362 | switch( sqlite3_value_type(argv[0]) ){ |
| 62339 | 63363 | case SQLITE_NULL: z = "null"; break; |
| 62340 | 63364 | case SQLITE_INTEGER: z = "integer"; break; |
| 62341 | 63365 | case SQLITE_TEXT: z = "text"; break; |
| 62342 | 63366 | case SQLITE_FLOAT: z = "real"; break; |
| @@ -62355,10 +63379,11 @@ | ||
| 62355 | 63379 | sqlite3_value **argv |
| 62356 | 63380 | ){ |
| 62357 | 63381 | int len; |
| 62358 | 63382 | |
| 62359 | 63383 | assert( argc==1 ); |
| 63384 | + UNUSED_PARAMETER(argc); | |
| 62360 | 63385 | switch( sqlite3_value_type(argv[0]) ){ |
| 62361 | 63386 | case SQLITE_BLOB: |
| 62362 | 63387 | case SQLITE_INTEGER: |
| 62363 | 63388 | case SQLITE_FLOAT: { |
| 62364 | 63389 | sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); |
| @@ -62385,10 +63410,11 @@ | ||
| 62385 | 63410 | /* |
| 62386 | 63411 | ** Implementation of the abs() function |
| 62387 | 63412 | */ |
| 62388 | 63413 | static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 62389 | 63414 | assert( argc==1 ); |
| 63415 | + UNUSED_PARAMETER(argc); | |
| 62390 | 63416 | switch( sqlite3_value_type(argv[0]) ){ |
| 62391 | 63417 | case SQLITE_INTEGER: { |
| 62392 | 63418 | i64 iVal = sqlite3_value_int64(argv[0]); |
| 62393 | 63419 | if( iVal<0 ){ |
| 62394 | 63420 | if( (iVal<<1)==0 ){ |
| @@ -62588,14 +63614,15 @@ | ||
| 62588 | 63614 | /* |
| 62589 | 63615 | ** Implementation of random(). Return a random integer. |
| 62590 | 63616 | */ |
| 62591 | 63617 | static void randomFunc( |
| 62592 | 63618 | sqlite3_context *context, |
| 62593 | - int argc, | |
| 62594 | - sqlite3_value **argv | |
| 63619 | + int NotUsed, | |
| 63620 | + sqlite3_value **NotUsed2 | |
| 62595 | 63621 | ){ |
| 62596 | 63622 | sqlite_int64 r; |
| 63623 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 62597 | 63624 | sqlite3_randomness(sizeof(r), &r); |
| 62598 | 63625 | if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ |
| 62599 | 63626 | /* can always do abs() of the result */ |
| 62600 | 63627 | sqlite3_result_int64(context, r); |
| 62601 | 63628 | } |
| @@ -62610,10 +63637,11 @@ | ||
| 62610 | 63637 | sqlite3_value **argv |
| 62611 | 63638 | ){ |
| 62612 | 63639 | int n; |
| 62613 | 63640 | unsigned char *p; |
| 62614 | 63641 | assert( argc==1 ); |
| 63642 | + UNUSED_PARAMETER(argc); | |
| 62615 | 63643 | n = sqlite3_value_int(argv[0]); |
| 62616 | 63644 | if( n<1 ){ |
| 62617 | 63645 | n = 1; |
| 62618 | 63646 | } |
| 62619 | 63647 | p = contextMalloc(context, n); |
| @@ -62627,40 +63655,43 @@ | ||
| 62627 | 63655 | ** Implementation of the last_insert_rowid() SQL function. The return |
| 62628 | 63656 | ** value is the same as the sqlite3_last_insert_rowid() API function. |
| 62629 | 63657 | */ |
| 62630 | 63658 | static void last_insert_rowid( |
| 62631 | 63659 | sqlite3_context *context, |
| 62632 | - int arg, | |
| 62633 | - sqlite3_value **argv | |
| 63660 | + int NotUsed, | |
| 63661 | + sqlite3_value **NotUsed2 | |
| 62634 | 63662 | ){ |
| 62635 | 63663 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 63664 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 62636 | 63665 | sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); |
| 62637 | 63666 | } |
| 62638 | 63667 | |
| 62639 | 63668 | /* |
| 62640 | 63669 | ** Implementation of the changes() SQL function. The return value is the |
| 62641 | 63670 | ** same as the sqlite3_changes() API function. |
| 62642 | 63671 | */ |
| 62643 | 63672 | static void changes( |
| 62644 | 63673 | sqlite3_context *context, |
| 62645 | - int arg, | |
| 62646 | - sqlite3_value **argv | |
| 63674 | + int NotUsed, | |
| 63675 | + sqlite3_value **NotUsed2 | |
| 62647 | 63676 | ){ |
| 62648 | 63677 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 63678 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 62649 | 63679 | sqlite3_result_int(context, sqlite3_changes(db)); |
| 62650 | 63680 | } |
| 62651 | 63681 | |
| 62652 | 63682 | /* |
| 62653 | 63683 | ** Implementation of the total_changes() SQL function. The return value is |
| 62654 | 63684 | ** the same as the sqlite3_total_changes() API function. |
| 62655 | 63685 | */ |
| 62656 | 63686 | static void total_changes( |
| 62657 | 63687 | sqlite3_context *context, |
| 62658 | - int arg, | |
| 62659 | - sqlite3_value **argv | |
| 63688 | + int NotUsed, | |
| 63689 | + sqlite3_value **NotUsed2 | |
| 62660 | 63690 | ){ |
| 62661 | 63691 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 63692 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 62662 | 63693 | sqlite3_result_int(context, sqlite3_total_changes(db)); |
| 62663 | 63694 | } |
| 62664 | 63695 | |
| 62665 | 63696 | /* |
| 62666 | 63697 | ** A structure defining how to do GLOB-style comparisons. |
| @@ -62901,14 +63932,15 @@ | ||
| 62901 | 63932 | ** argument if the arguments are different. The result is NULL if the |
| 62902 | 63933 | ** arguments are equal to each other. |
| 62903 | 63934 | */ |
| 62904 | 63935 | static void nullifFunc( |
| 62905 | 63936 | sqlite3_context *context, |
| 62906 | - int argc, | |
| 63937 | + int NotUsed, | |
| 62907 | 63938 | sqlite3_value **argv |
| 62908 | 63939 | ){ |
| 62909 | 63940 | CollSeq *pColl = sqlite3GetFuncCollSeq(context); |
| 63941 | + UNUSED_PARAMETER(NotUsed); | |
| 62910 | 63942 | if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ |
| 62911 | 63943 | sqlite3_result_value(context, argv[0]); |
| 62912 | 63944 | } |
| 62913 | 63945 | } |
| 62914 | 63946 | |
| @@ -62916,13 +63948,14 @@ | ||
| 62916 | 63948 | ** Implementation of the VERSION(*) function. The result is the version |
| 62917 | 63949 | ** of the SQLite library that is running. |
| 62918 | 63950 | */ |
| 62919 | 63951 | static void versionFunc( |
| 62920 | 63952 | sqlite3_context *context, |
| 62921 | - int argc, | |
| 62922 | - sqlite3_value **argv | |
| 63953 | + int NotUsed, | |
| 63954 | + sqlite3_value **NotUsed2 | |
| 62923 | 63955 | ){ |
| 63956 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 62924 | 63957 | sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); |
| 62925 | 63958 | } |
| 62926 | 63959 | |
| 62927 | 63960 | /* Array for converting from half-bytes (nybbles) into ASCII hex |
| 62928 | 63961 | ** digits. */ |
| @@ -63011,10 +64044,11 @@ | ||
| 63011 | 64044 | ){ |
| 63012 | 64045 | int i, n; |
| 63013 | 64046 | const unsigned char *pBlob; |
| 63014 | 64047 | char *zHex, *z; |
| 63015 | 64048 | assert( argc==1 ); |
| 64049 | + UNUSED_PARAMETER(argc); | |
| 63016 | 64050 | pBlob = sqlite3_value_blob(argv[0]); |
| 63017 | 64051 | n = sqlite3_value_bytes(argv[0]); |
| 63018 | 64052 | assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ |
| 63019 | 64053 | z = zHex = contextMalloc(context, ((i64)n)*2 + 1); |
| 63020 | 64054 | if( zHex ){ |
| @@ -63036,10 +64070,11 @@ | ||
| 63036 | 64070 | int argc, |
| 63037 | 64071 | sqlite3_value **argv |
| 63038 | 64072 | ){ |
| 63039 | 64073 | i64 n; |
| 63040 | 64074 | assert( argc==1 ); |
| 64075 | + UNUSED_PARAMETER(argc); | |
| 63041 | 64076 | n = sqlite3_value_int64(argv[0]); |
| 63042 | 64077 | if( n>SQLITE_MAX_LENGTH ){ |
| 63043 | 64078 | sqlite3_result_error_toobig(context); |
| 63044 | 64079 | }else{ |
| 63045 | 64080 | sqlite3_result_zeroblob(context, n); |
| @@ -63067,10 +64102,11 @@ | ||
| 63067 | 64102 | i64 nOut; /* Maximum size of zOut */ |
| 63068 | 64103 | int loopLimit; /* Last zStr[] that might match zPattern[] */ |
| 63069 | 64104 | int i, j; /* Loop counters */ |
| 63070 | 64105 | |
| 63071 | 64106 | assert( argc==3 ); |
| 64107 | + UNUSED_PARAMETER(argc); | |
| 63072 | 64108 | zStr = sqlite3_value_text(argv[0]); |
| 63073 | 64109 | if( zStr==0 ) return; |
| 63074 | 64110 | nStr = sqlite3_value_bytes(argv[0]); |
| 63075 | 64111 | assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ |
| 63076 | 64112 | zPattern = sqlite3_value_text(argv[1]); |
| @@ -63304,10 +64340,11 @@ | ||
| 63304 | 64340 | */ |
| 63305 | 64341 | static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ |
| 63306 | 64342 | SumCtx *p; |
| 63307 | 64343 | int type; |
| 63308 | 64344 | assert( argc==1 ); |
| 64345 | + UNUSED_PARAMETER(argc); | |
| 63309 | 64346 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
| 63310 | 64347 | type = sqlite3_value_numeric_type(argv[0]); |
| 63311 | 64348 | if( p && type!=SQLITE_NULL ){ |
| 63312 | 64349 | p->cnt++; |
| 63313 | 64350 | if( type==SQLITE_INTEGER ){ |
| @@ -63379,13 +64416,18 @@ | ||
| 63379 | 64416 | } |
| 63380 | 64417 | |
| 63381 | 64418 | /* |
| 63382 | 64419 | ** Routines to implement min() and max() aggregate functions. |
| 63383 | 64420 | */ |
| 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 | +){ | |
| 63385 | 64426 | Mem *pArg = (Mem *)argv[0]; |
| 63386 | 64427 | Mem *pBest; |
| 64428 | + UNUSED_PARAMETER(NotUsed); | |
| 63387 | 64429 | |
| 63388 | 64430 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 63389 | 64431 | pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); |
| 63390 | 64432 | if( !pBest ) return; |
| 63391 | 64433 | |
| @@ -63661,11 +64703,11 @@ | ||
| 63661 | 64703 | ** |
| 63662 | 64704 | ************************************************************************* |
| 63663 | 64705 | ** This file contains C code routines that are called by the parser |
| 63664 | 64706 | ** to handle INSERT statements in SQLite. |
| 63665 | 64707 | ** |
| 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 $ | |
| 63667 | 64709 | */ |
| 63668 | 64710 | |
| 63669 | 64711 | /* |
| 63670 | 64712 | ** Set P4 of the most recently inserted opcode to a column affinity |
| 63671 | 64713 | ** string for index pIdx. A column affinity string has one character |
| @@ -64078,11 +65120,11 @@ | ||
| 64078 | 65120 | |
| 64079 | 65121 | /* Figure out if we have any triggers and if the table being |
| 64080 | 65122 | ** inserted into is a view |
| 64081 | 65123 | */ |
| 64082 | 65124 | #ifndef SQLITE_OMIT_TRIGGER |
| 64083 | - triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0); | |
| 65125 | + triggers_exist = sqlite3TriggersExist(pTab, TK_INSERT, 0); | |
| 64084 | 65126 | isView = pTab->pSelect!=0; |
| 64085 | 65127 | #else |
| 64086 | 65128 | # define triggers_exist 0 |
| 64087 | 65129 | # define isView 0 |
| 64088 | 65130 | #endif |
| @@ -64584,11 +65626,10 @@ | ||
| 64584 | 65626 | pTab, |
| 64585 | 65627 | baseCur, |
| 64586 | 65628 | regIns, |
| 64587 | 65629 | aRegIdx, |
| 64588 | 65630 | 0, |
| 64589 | - 0, | |
| 64590 | 65631 | (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1, |
| 64591 | 65632 | appendFlag |
| 64592 | 65633 | ); |
| 64593 | 65634 | } |
| 64594 | 65635 | } |
| @@ -64920,30 +65961,30 @@ | ||
| 64920 | 65961 | case OE_Rollback: |
| 64921 | 65962 | case OE_Abort: |
| 64922 | 65963 | case OE_Fail: { |
| 64923 | 65964 | int j, n1, n2; |
| 64924 | 65965 | char zErrMsg[200]; |
| 64925 | - sqlite3_snprintf(sizeof(zErrMsg), zErrMsg, | |
| 65966 | + sqlite3_snprintf(ArraySize(zErrMsg), zErrMsg, | |
| 64926 | 65967 | pIdx->nColumn>1 ? "columns " : "column "); |
| 64927 | 65968 | 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++){ | |
| 64929 | 65970 | char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
| 64930 | 65971 | n2 = strlen(zCol); |
| 64931 | 65972 | if( j>0 ){ |
| 64932 | - sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], ", "); | |
| 65973 | + sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], ", "); | |
| 64933 | 65974 | n1 += 2; |
| 64934 | 65975 | } |
| 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], "..."); | |
| 64937 | 65978 | n1 += 3; |
| 64938 | 65979 | break; |
| 64939 | 65980 | }else{ |
| 64940 | - sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol); | |
| 65981 | + sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol); | |
| 64941 | 65982 | n1 += n2; |
| 64942 | 65983 | } |
| 64943 | 65984 | } |
| 64944 | - sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], | |
| 65985 | + sqlite3_snprintf(ArraySize(zErrMsg)-n1, &zErrMsg[n1], | |
| 64945 | 65986 | pIdx->nColumn>1 ? " are not unique" : " is not unique"); |
| 64946 | 65987 | sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErrMsg,0); |
| 64947 | 65988 | break; |
| 64948 | 65989 | } |
| 64949 | 65990 | case OE_Ignore: { |
| @@ -64976,11 +66017,10 @@ | ||
| 64976 | 66017 | Parse *pParse, /* The parser context */ |
| 64977 | 66018 | Table *pTab, /* the table into which we are inserting */ |
| 64978 | 66019 | int baseCur, /* Index of a read/write cursor pointing at pTab */ |
| 64979 | 66020 | int regRowid, /* Range of content */ |
| 64980 | 66021 | int *aRegIdx, /* Register used by each index. 0 for unused indices */ |
| 64981 | - int rowidChng, /* True if the record number will change */ | |
| 64982 | 66022 | int isUpdate, /* True for UPDATE, False for INSERT */ |
| 64983 | 66023 | int newIdx, /* Index of NEW table for triggers. -1 if none */ |
| 64984 | 66024 | int appendBias /* True if this is likely to be an append */ |
| 64985 | 66025 | ){ |
| 64986 | 66026 | int i; |
| @@ -66534,11 +67574,11 @@ | ||
| 66534 | 67574 | ** May you share freely, never taking more than you give. |
| 66535 | 67575 | ** |
| 66536 | 67576 | ************************************************************************* |
| 66537 | 67577 | ** This file contains code used to implement the PRAGMA command. |
| 66538 | 67578 | ** |
| 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 $ | |
| 66540 | 67580 | */ |
| 66541 | 67581 | |
| 66542 | 67582 | /* Ignore this whole file if pragmas are disabled |
| 66543 | 67583 | */ |
| 66544 | 67584 | #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER) |
| @@ -66562,11 +67602,11 @@ | ||
| 66562 | 67602 | int i, n; |
| 66563 | 67603 | if( isdigit(*z) ){ |
| 66564 | 67604 | return atoi(z); |
| 66565 | 67605 | } |
| 66566 | 67606 | n = strlen(z); |
| 66567 | - for(i=0; i<sizeof(iLength); i++){ | |
| 67607 | + for(i=0; i<ArraySize(iLength); i++){ | |
| 66568 | 67608 | if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){ |
| 66569 | 67609 | return iValue[i]; |
| 66570 | 67610 | } |
| 66571 | 67611 | } |
| 66572 | 67612 | return 1; |
| @@ -66712,11 +67752,11 @@ | ||
| 66712 | 67752 | ** flag if there are any active statements. */ |
| 66713 | 67753 | { "read_uncommitted", SQLITE_ReadUncommitted }, |
| 66714 | 67754 | }; |
| 66715 | 67755 | int i; |
| 66716 | 67756 | 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++){ | |
| 66718 | 67758 | if( sqlite3StrICmp(zLeft, p->zName)==0 ){ |
| 66719 | 67759 | sqlite3 *db = pParse->db; |
| 66720 | 67760 | Vdbe *v; |
| 66721 | 67761 | v = sqlite3GetVdbe(pParse); |
| 66722 | 67762 | if( v ){ |
| @@ -66976,11 +68016,11 @@ | ||
| 66976 | 68016 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); |
| 66977 | 68017 | }else |
| 66978 | 68018 | |
| 66979 | 68019 | /* |
| 66980 | 68020 | ** PRAGMA [database.]journal_mode |
| 66981 | - ** PRAGMA [database.]journal_mode = (delete|persist|memory|off) | |
| 68021 | + ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory) | |
| 66982 | 68022 | */ |
| 66983 | 68023 | if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){ |
| 66984 | 68024 | int eMode; |
| 66985 | 68025 | static char * const azModeName[] = { |
| 66986 | 68026 | "delete", "persist", "off", "truncate", "memory" |
| @@ -67891,11 +68931,11 @@ | ||
| 67891 | 68931 | ************************************************************************* |
| 67892 | 68932 | ** This file contains the implementation of the sqlite3_prepare() |
| 67893 | 68933 | ** interface, and routines that contribute to loading the database schema |
| 67894 | 68934 | ** from disk. |
| 67895 | 68935 | ** |
| 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 $ | |
| 67897 | 68937 | */ |
| 67898 | 68938 | |
| 67899 | 68939 | /* |
| 67900 | 68940 | ** Fill the InitData structure with an error message that indicates |
| 67901 | 68941 | ** that the database is corrupt. |
| @@ -67928,23 +68968,24 @@ | ||
| 67928 | 68968 | ** argv[0] = name of thing being created |
| 67929 | 68969 | ** argv[1] = root page number for table or index. 0 for trigger or view. |
| 67930 | 68970 | ** argv[2] = SQL text for the CREATE statement. |
| 67931 | 68971 | ** |
| 67932 | 68972 | */ |
| 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){ | |
| 67934 | 68974 | InitData *pData = (InitData*)pInit; |
| 67935 | 68975 | sqlite3 *db = pData->db; |
| 67936 | 68976 | int iDb = pData->iDb; |
| 67937 | 68977 | |
| 68978 | + assert( argc==3 ); | |
| 68979 | + UNUSED_PARAMETER2(NotUsed, argc); | |
| 67938 | 68980 | assert( sqlite3_mutex_held(db->mutex) ); |
| 67939 | 68981 | DbClearProperty(db, iDb, DB_Empty); |
| 67940 | 68982 | if( db->mallocFailed ){ |
| 67941 | 68983 | corruptSchema(pData, argv[0], 0); |
| 67942 | 68984 | return SQLITE_NOMEM; |
| 67943 | 68985 | } |
| 67944 | 68986 | |
| 67945 | - assert( argc==3 ); | |
| 67946 | 68987 | assert( iDb>=0 && iDb<db->nDb ); |
| 67947 | 68988 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
| 67948 | 68989 | if( argv[1]==0 ){ |
| 67949 | 68990 | corruptSchema(pData, argv[0], 0); |
| 67950 | 68991 | }else if( argv[2] && argv[2][0] ){ |
| @@ -68120,11 +69161,11 @@ | ||
| 68120 | 69161 | ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
| 68121 | 69162 | ** the possible values of meta[4]. |
| 68122 | 69163 | */ |
| 68123 | 69164 | if( rc==SQLITE_OK ){ |
| 68124 | 69165 | int i; |
| 68125 | - for(i=0; i<sizeof(meta)/sizeof(meta[0]); i++){ | |
| 69166 | + for(i=0; i<ArraySize(meta); i++){ | |
| 68126 | 69167 | rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]); |
| 68127 | 69168 | if( rc ){ |
| 68128 | 69169 | sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc)); |
| 68129 | 69170 | goto initone_error_out; |
| 68130 | 69171 | } |
| @@ -68702,11 +69743,11 @@ | ||
| 68702 | 69743 | ** |
| 68703 | 69744 | ************************************************************************* |
| 68704 | 69745 | ** This file contains C code routines that are called by the parser |
| 68705 | 69746 | ** to handle SELECT statements in SQLite. |
| 68706 | 69747 | ** |
| 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 $ | |
| 68708 | 69749 | */ |
| 68709 | 69750 | |
| 68710 | 69751 | |
| 68711 | 69752 | /* |
| 68712 | 69753 | ** Delete all the content of a Select structure but do not deallocate |
| @@ -68754,11 +69795,11 @@ | ||
| 68754 | 69795 | ){ |
| 68755 | 69796 | Select *pNew; |
| 68756 | 69797 | Select standin; |
| 68757 | 69798 | sqlite3 *db = pParse->db; |
| 68758 | 69799 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); |
| 68759 | - assert( !pOffset || pLimit ); /* Can't have OFFSET without LIMIT. */ | |
| 69800 | + assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */ | |
| 68760 | 69801 | if( pNew==0 ){ |
| 68761 | 69802 | pNew = &standin; |
| 68762 | 69803 | memset(pNew, 0, sizeof(*pNew)); |
| 68763 | 69804 | } |
| 68764 | 69805 | if( pEList==0 ){ |
| @@ -68833,18 +69874,18 @@ | ||
| 68833 | 69874 | apAll[0] = pA; |
| 68834 | 69875 | apAll[1] = pB; |
| 68835 | 69876 | apAll[2] = pC; |
| 68836 | 69877 | for(i=0; i<3 && apAll[i]; i++){ |
| 68837 | 69878 | p = apAll[i]; |
| 68838 | - for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){ | |
| 69879 | + for(j=0; j<ArraySize(keywords); j++){ | |
| 68839 | 69880 | if( p->n==keywords[j].nChar |
| 68840 | 69881 | && sqlite3StrNICmp((char*)p->z, keywords[j].zKeyword, p->n)==0 ){ |
| 68841 | 69882 | jointype |= keywords[j].code; |
| 68842 | 69883 | break; |
| 68843 | 69884 | } |
| 68844 | 69885 | } |
| 68845 | - if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ | |
| 69886 | + if( j>=ArraySize(keywords) ){ | |
| 68846 | 69887 | jointype |= JT_ERROR; |
| 68847 | 69888 | break; |
| 68848 | 69889 | } |
| 68849 | 69890 | } |
| 68850 | 69891 | if( |
| @@ -71262,11 +72303,13 @@ | ||
| 71262 | 72303 | struct SrcList_item *pSubitem; /* The subquery */ |
| 71263 | 72304 | sqlite3 *db = pParse->db; |
| 71264 | 72305 | |
| 71265 | 72306 | /* Check to see if flattening is permitted. Return 0 if not. |
| 71266 | 72307 | */ |
| 72308 | + assert( p!=0 ); | |
| 71267 | 72309 | if( p==0 ) return 0; |
| 72310 | + assert( p->pPrior==0 ); /* Unable to flatten compound queries */ | |
| 71268 | 72311 | pSrc = p->pSrc; |
| 71269 | 72312 | assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); |
| 71270 | 72313 | pSubitem = &pSrc->a[iFrom]; |
| 71271 | 72314 | iParent = pSubitem->iCursor; |
| 71272 | 72315 | pSub = pSubitem->pSelect; |
| @@ -71374,96 +72417,150 @@ | ||
| 71374 | 72417 | ** be of the form: |
| 71375 | 72418 | ** |
| 71376 | 72419 | ** SELECT <expr-list> FROM (<sub-query>) <where-clause> |
| 71377 | 72420 | ** |
| 71378 | 72421 | ** 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 | |
| 71380 | 72423 | ** OFFSET clauses and joins them to the left-hand-side of the original |
| 71381 | 72424 | ** using UNION ALL operators. In this case N is the number of simple |
| 71382 | 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". | |
| 71383 | 72447 | */ |
| 71384 | 72448 | for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){ |
| 71385 | 72449 | Select *pNew; |
| 71386 | 72450 | ExprList *pOrderBy = p->pOrderBy; |
| 71387 | 72451 | Expr *pLimit = p->pLimit; |
| 71388 | - Expr *pOffset = p->pOffset; | |
| 71389 | 72452 | Select *pPrior = p->pPrior; |
| 71390 | 72453 | p->pOrderBy = 0; |
| 71391 | 72454 | p->pSrc = 0; |
| 71392 | 72455 | p->pPrior = 0; |
| 71393 | 72456 | p->pLimit = 0; |
| 71394 | 72457 | pNew = sqlite3SelectDup(db, p); |
| 71395 | - pNew->pPrior = pPrior; | |
| 71396 | - p->pPrior = pNew; | |
| 72458 | + p->pLimit = pLimit; | |
| 71397 | 72459 | p->pOrderBy = pOrderBy; |
| 72460 | + p->pSrc = pSrc; | |
| 71398 | 72461 | p->op = TK_ALL; |
| 71399 | - p->pSrc = pSrc; | |
| 71400 | - p->pLimit = pLimit; | |
| 71401 | - p->pOffset = pOffset; | |
| 71402 | 72462 | 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; | |
| 71404 | 72471 | } |
| 71405 | 72472 | |
| 71406 | 72473 | /* Begin flattening the iFrom-th entry of the FROM clause |
| 71407 | 72474 | ** in the outer query. |
| 71408 | 72475 | */ |
| 71409 | 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 | + */ | |
| 71410 | 72518 | for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){ |
| 71411 | - int nSubSrc = pSubSrc->nSrc; | |
| 72519 | + int nSubSrc; | |
| 71412 | 72520 | 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 | + | |
| 71424 | 72525 | if( pSrc ){ |
| 71425 | - Table *pTabToDel; | |
| 71426 | - pSubitem = &pSrc->a[iFrom]; | |
| 71427 | - nSubSrc = pSubSrc->nSrc; | |
| 72526 | + assert( pParent==p ); /* First time through the loop */ | |
| 71428 | 72527 | 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 | + */ | |
| 71465 | 72562 | for(i=0; i<nSubSrc; i++){ |
| 71466 | 72563 | pSrc->a[i+iFrom] = pSubSrc->a[i]; |
| 71467 | 72564 | memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); |
| 71468 | 72565 | } |
| 71469 | 72566 | pSrc->a[iFrom].jointype = jointype; |
| @@ -71554,11 +72651,11 @@ | ||
| 71554 | 72651 | ** 1. There is a single object in the FROM clause. |
| 71555 | 72652 | ** |
| 71556 | 72653 | ** 2. There is a single expression in the result set, and it is |
| 71557 | 72654 | ** either min(x) or max(x), where x is a column reference. |
| 71558 | 72655 | */ |
| 71559 | -static int minMaxQuery(Parse *pParse, Select *p){ | |
| 72656 | +static int minMaxQuery(Select *p){ | |
| 71560 | 72657 | Expr *pExpr; |
| 71561 | 72658 | ExprList *pEList = p->pEList; |
| 71562 | 72659 | |
| 71563 | 72660 | if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL; |
| 71564 | 72661 | pExpr = pEList->a[0].pExpr; |
| @@ -71854,11 +72951,12 @@ | ||
| 71854 | 72951 | ** are walked without any actions being taken at each node. Presumably, |
| 71855 | 72952 | ** when this routine is used for Walker.xExprCallback then |
| 71856 | 72953 | ** Walker.xSelectCallback is set to do something useful for every |
| 71857 | 72954 | ** subquery in the parser tree. |
| 71858 | 72955 | */ |
| 71859 | -static int exprWalkNoop(Walker *pWalker, Expr *pExpr){ | |
| 72956 | +static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ | |
| 72957 | + UNUSED_PARAMETER2(NotUsed, NotUsed2); | |
| 71860 | 72958 | return WRC_Continue; |
| 71861 | 72959 | } |
| 71862 | 72960 | |
| 71863 | 72961 | /* |
| 71864 | 72962 | ** This routine "expands" a SELECT statement and all of its subqueries. |
| @@ -72645,11 +73743,11 @@ | ||
| 72645 | 73743 | ** + The optimizer code in where.c (the thing that decides which |
| 72646 | 73744 | ** index or indices to use) should place a different priority on |
| 72647 | 73745 | ** satisfying the 'ORDER BY' clause than it does in other cases. |
| 72648 | 73746 | ** Refer to code and comments in where.c for details. |
| 72649 | 73747 | */ |
| 72650 | - flag = minMaxQuery(pParse, p); | |
| 73748 | + flag = minMaxQuery(p); | |
| 72651 | 73749 | if( flag ){ |
| 72652 | 73750 | pDel = pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->pList); |
| 72653 | 73751 | if( pMinMax && !db->mallocFailed ){ |
| 72654 | 73752 | pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN; |
| 72655 | 73753 | pMinMax->a[0].pExpr->op = TK_COLUMN; |
| @@ -73027,11 +74125,11 @@ | ||
| 73027 | 74125 | ** May you share freely, never taking more than you give. |
| 73028 | 74126 | ** |
| 73029 | 74127 | ************************************************************************* |
| 73030 | 74128 | ** |
| 73031 | 74129 | ** |
| 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 $ | |
| 73033 | 74131 | */ |
| 73034 | 74132 | |
| 73035 | 74133 | #ifndef SQLITE_OMIT_TRIGGER |
| 73036 | 74134 | /* |
| 73037 | 74135 | ** Delete a linked list of TriggerStep structures. |
| @@ -73619,11 +74717,10 @@ | ||
| 73619 | 74717 | ** |
| 73620 | 74718 | ** The returned bit vector is some combination of TRIGGER_BEFORE and |
| 73621 | 74719 | ** TRIGGER_AFTER. |
| 73622 | 74720 | */ |
| 73623 | 74721 | SQLITE_PRIVATE int sqlite3TriggersExist( |
| 73624 | - Parse *pParse, /* Used to check for recursive triggers */ | |
| 73625 | 74722 | Table *pTab, /* The table the contains the triggers */ |
| 73626 | 74723 | int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ |
| 73627 | 74724 | ExprList *pChanges /* Columns that change in an UPDATE statement */ |
| 73628 | 74725 | ){ |
| 73629 | 74726 | Trigger *pTrigger; |
| @@ -73883,11 +74980,11 @@ | ||
| 73883 | 74980 | ** |
| 73884 | 74981 | ************************************************************************* |
| 73885 | 74982 | ** This file contains C code routines that are called by the parser |
| 73886 | 74983 | ** to handle UPDATE statements. |
| 73887 | 74984 | ** |
| 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 $ | |
| 73889 | 74986 | */ |
| 73890 | 74987 | |
| 73891 | 74988 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 73892 | 74989 | /* Forward declaration */ |
| 73893 | 74990 | static void updateVirtualTable( |
| @@ -74012,11 +75109,11 @@ | ||
| 74012 | 75109 | |
| 74013 | 75110 | /* Figure out if we have any triggers and if the table being |
| 74014 | 75111 | ** updated is a view |
| 74015 | 75112 | */ |
| 74016 | 75113 | #ifndef SQLITE_OMIT_TRIGGER |
| 74017 | - triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges); | |
| 75114 | + triggers_exist = sqlite3TriggersExist(pTab, TK_UPDATE, pChanges); | |
| 74018 | 75115 | isView = pTab->pSelect!=0; |
| 74019 | 75116 | #else |
| 74020 | 75117 | # define triggers_exist 0 |
| 74021 | 75118 | # define isView 0 |
| 74022 | 75119 | #endif |
| @@ -74395,11 +75492,11 @@ | ||
| 74395 | 75492 | sqlite3VdbeJumpHere(v, j1); |
| 74396 | 75493 | |
| 74397 | 75494 | /* Create the new index entries and the new record. |
| 74398 | 75495 | */ |
| 74399 | 75496 | sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, |
| 74400 | - aRegIdx, chngRowid, 1, -1, 0); | |
| 75497 | + aRegIdx, 1, -1, 0); | |
| 74401 | 75498 | } |
| 74402 | 75499 | |
| 74403 | 75500 | /* Increment the row counter |
| 74404 | 75501 | */ |
| 74405 | 75502 | if( db->flags & SQLITE_CountRows && !pParse->trigStack){ |
| @@ -74568,11 +75665,11 @@ | ||
| 74568 | 75665 | ** This file contains code used to implement the VACUUM command. |
| 74569 | 75666 | ** |
| 74570 | 75667 | ** Most of the code in this file may be omitted by defining the |
| 74571 | 75668 | ** SQLITE_OMIT_VACUUM macro. |
| 74572 | 75669 | ** |
| 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 $ | |
| 74574 | 75671 | */ |
| 74575 | 75672 | |
| 74576 | 75673 | #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) |
| 74577 | 75674 | /* |
| 74578 | 75675 | ** Execute zSql on database db. Return an error code. |
| @@ -74803,11 +75900,11 @@ | ||
| 74803 | 75900 | |
| 74804 | 75901 | assert( 1==sqlite3BtreeIsInTrans(pTemp) ); |
| 74805 | 75902 | assert( 1==sqlite3BtreeIsInTrans(pMain) ); |
| 74806 | 75903 | |
| 74807 | 75904 | /* 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){ | |
| 74809 | 75906 | rc = sqlite3BtreeGetMeta(pMain, aCopy[i], &meta); |
| 74810 | 75907 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 74811 | 75908 | rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]); |
| 74812 | 75909 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 74813 | 75910 | } |
| @@ -74866,11 +75963,11 @@ | ||
| 74866 | 75963 | ** May you share freely, never taking more than you give. |
| 74867 | 75964 | ** |
| 74868 | 75965 | ************************************************************************* |
| 74869 | 75966 | ** This file contains code used to help implement virtual tables. |
| 74870 | 75967 | ** |
| 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 $ | |
| 74872 | 75969 | */ |
| 74873 | 75970 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 74874 | 75971 | |
| 74875 | 75972 | static int createModule( |
| 74876 | 75973 | sqlite3 *db, /* Database in which module is registered */ |
| @@ -74900,10 +75997,12 @@ | ||
| 74900 | 75997 | sqlite3DbFree(db, pDel); |
| 74901 | 75998 | if( pDel==pMod ){ |
| 74902 | 75999 | db->mallocFailed = 1; |
| 74903 | 76000 | } |
| 74904 | 76001 | sqlite3ResetInternalSchema(db, 0); |
| 76002 | + }else if( xDestroy ){ | |
| 76003 | + xDestroy(pAux); | |
| 74905 | 76004 | } |
| 74906 | 76005 | rc = sqlite3ApiExit(db, SQLITE_OK); |
| 74907 | 76006 | sqlite3_mutex_leave(db->mutex); |
| 74908 | 76007 | return rc; |
| 74909 | 76008 | } |
| @@ -75564,11 +76663,11 @@ | ||
| 75564 | 76663 | /* Special case: If db->aVTrans is NULL and db->nVTrans is greater |
| 75565 | 76664 | ** than zero, then this function is being called from within a |
| 75566 | 76665 | ** virtual module xSync() callback. It is illegal to write to |
| 75567 | 76666 | ** virtual module tables in this case, so return SQLITE_LOCKED. |
| 75568 | 76667 | */ |
| 75569 | - if( 0==db->aVTrans && db->nVTrans>0 ){ | |
| 76668 | + if( sqlite3VtabInSync(db) ){ | |
| 75570 | 76669 | return SQLITE_LOCKED; |
| 75571 | 76670 | } |
| 75572 | 76671 | if( !pVtab ){ |
| 75573 | 76672 | return SQLITE_OK; |
| 75574 | 76673 | } |
| @@ -75712,18 +76811,13 @@ | ||
| 75712 | 76811 | ** generating the code that loops through a table looking for applicable |
| 75713 | 76812 | ** rows. Indices are selected and used to speed the search when doing |
| 75714 | 76813 | ** so is applicable. Because this module is responsible for selecting |
| 75715 | 76814 | ** indices, you might also think of this module as the "query optimizer". |
| 75716 | 76815 | ** |
| 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 $ | |
| 75718 | 76817 | */ |
| 75719 | 76818 | |
| 75720 | -/* | |
| 75721 | -** The number of bits in a Bitmask. "BMS" means "BitMask Size". | |
| 75722 | -*/ | |
| 75723 | -#define BMS (sizeof(Bitmask)*8) | |
| 75724 | - | |
| 75725 | 76819 | /* |
| 75726 | 76820 | ** Trace output macros |
| 75727 | 76821 | */ |
| 75728 | 76822 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 75729 | 76823 | SQLITE_PRIVATE int sqlite3WhereTrace = 0; |
| @@ -75834,11 +76928,11 @@ | ||
| 75834 | 76928 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 75835 | 76929 | ** no gaps. |
| 75836 | 76930 | */ |
| 75837 | 76931 | struct ExprMaskSet { |
| 75838 | 76932 | 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 */ | |
| 75840 | 76934 | }; |
| 75841 | 76935 | |
| 75842 | 76936 | |
| 75843 | 76937 | /* |
| 75844 | 76938 | ** Bitmasks for the operators that indices are able to exploit. An |
| @@ -78015,11 +79109,11 @@ | ||
| 78015 | 79109 | }else |
| 78016 | 79110 | #endif |
| 78017 | 79111 | if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){ |
| 78018 | 79112 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 78019 | 79113 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 78020 | - if( !pWInfo->okOnePass && pTab->nCol<(sizeof(Bitmask)*8) ){ | |
| 79114 | + if( !pWInfo->okOnePass && pTab->nCol<BMS ){ | |
| 78021 | 79115 | Bitmask b = pTabItem->colUsed; |
| 78022 | 79116 | int n = 0; |
| 78023 | 79117 | for(; b; b=b>>1, n++){} |
| 78024 | 79118 | sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-2, n); |
| 78025 | 79119 | assert( n<=pTab->nCol ); |
| @@ -79896,10 +80990,12 @@ | ||
| 79896 | 80990 | pParser->yyidx = -1; |
| 79897 | 80991 | #ifdef YYTRACKMAXSTACKDEPTH |
| 79898 | 80992 | pParser->yyidxMax = 0; |
| 79899 | 80993 | #endif |
| 79900 | 80994 | #if YYSTACKDEPTH<=0 |
| 80995 | + pParser->yystack = NULL; | |
| 80996 | + pParser->yystksz = 0; | |
| 79901 | 80997 | yyGrowStack(pParser); |
| 79902 | 80998 | #endif |
| 79903 | 80999 | } |
| 79904 | 81000 | return pParser; |
| 79905 | 81001 | } |
| @@ -82745,11 +83841,11 @@ | ||
| 82745 | 83841 | ** Main file for the SQLite library. The routines in this file |
| 82746 | 83842 | ** implement the programmer interface to the library. Routines in |
| 82747 | 83843 | ** other files are for internal use by SQLite and should not be |
| 82748 | 83844 | ** accessed by users of the library. |
| 82749 | 83845 | ** |
| 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 $ | |
| 82751 | 83847 | */ |
| 82752 | 83848 | |
| 82753 | 83849 | #ifdef SQLITE_ENABLE_FTS3 |
| 82754 | 83850 | /************** Include fts3.h in the middle of main.c ***********************/ |
| 82755 | 83851 | /************** Begin file fts3.h ********************************************/ |
| @@ -83125,10 +84221,24 @@ | ||
| 83125 | 84221 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 83126 | 84222 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 83127 | 84223 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
| 83128 | 84224 | break; |
| 83129 | 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 | + } | |
| 83130 | 84240 | |
| 83131 | 84241 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 83132 | 84242 | case SQLITE_CONFIG_HEAP: { |
| 83133 | 84243 | /* Designate a buffer for heap memory space */ |
| 83134 | 84244 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| @@ -83155,18 +84265,10 @@ | ||
| 83155 | 84265 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
| 83156 | 84266 | #endif |
| 83157 | 84267 | } |
| 83158 | 84268 | break; |
| 83159 | 84269 | } |
| 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 | 84270 | #endif |
| 83169 | 84271 | |
| 83170 | 84272 | case SQLITE_CONFIG_LOOKASIDE: { |
| 83171 | 84273 | sqlite3GlobalConfig.szLookaside = va_arg(ap, int); |
| 83172 | 84274 | sqlite3GlobalConfig.nLookaside = va_arg(ap, int); |
| @@ -83326,10 +84428,11 @@ | ||
| 83326 | 84428 | int nKey1, const void *pKey1, |
| 83327 | 84429 | int nKey2, const void *pKey2 |
| 83328 | 84430 | ){ |
| 83329 | 84431 | int r = sqlite3StrNICmp( |
| 83330 | 84432 | (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); |
| 84433 | + UNUSED_PARAMETER(NotUsed); | |
| 83331 | 84434 | if( 0==r ){ |
| 83332 | 84435 | r = nKey1-nKey2; |
| 83333 | 84436 | } |
| 83334 | 84437 | return r; |
| 83335 | 84438 | } |
| @@ -92737,10 +93840,17 @@ | ||
| 92737 | 93840 | elem->data = data; |
| 92738 | 93841 | } |
| 92739 | 93842 | return old_data; |
| 92740 | 93843 | } |
| 92741 | 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 | + } | |
| 92742 | 93852 | new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) ); |
| 92743 | 93853 | if( new_elem==0 ) return data; |
| 92744 | 93854 | if( pH->copyKey && pKey!=0 ){ |
| 92745 | 93855 | new_elem->pKey = fts3HashMalloc( nKey ); |
| 92746 | 93856 | if( new_elem->pKey==0 ){ |
| @@ -92751,18 +93861,10 @@ | ||
| 92751 | 93861 | }else{ |
| 92752 | 93862 | new_elem->pKey = (void*)pKey; |
| 92753 | 93863 | } |
| 92754 | 93864 | new_elem->nKey = nKey; |
| 92755 | 93865 | 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 | 93866 | if( pH->count > pH->htsize ){ |
| 92765 | 93867 | fts3Rehash(pH,pH->htsize*2); |
| 92766 | 93868 | } |
| 92767 | 93869 | assert( pH->htsize>0 ); |
| 92768 | 93870 | assert( (pH->htsize & (pH->htsize-1))==0 ); |
| @@ -94022,11 +95124,11 @@ | ||
| 94022 | 95124 | ** |
| 94023 | 95125 | ************************************************************************* |
| 94024 | 95126 | ** This file contains code for implementations of the r-tree and r*-tree |
| 94025 | 95127 | ** algorithms packaged as an SQLite virtual table module. |
| 94026 | 95128 | ** |
| 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 $ | |
| 94028 | 95130 | */ |
| 94029 | 95131 | |
| 94030 | 95132 | #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE) |
| 94031 | 95133 | |
| 94032 | 95134 | /* |
| @@ -94232,12 +95334,16 @@ | ||
| 94232 | 95334 | struct RtreeCell { |
| 94233 | 95335 | i64 iRowid; |
| 94234 | 95336 | RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; |
| 94235 | 95337 | }; |
| 94236 | 95338 | |
| 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 | |
| 94239 | 95345 | |
| 94240 | 95346 | /* |
| 94241 | 95347 | ** Functions to deserialize a 16 bit integer, 32 bit real number and |
| 94242 | 95348 | ** 64 bit integer. The deserialized value is returned. |
| 94243 | 95349 | */ |
| 94244 | 95350 |
| --- 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, ¤tSize); |
| 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, ¤tSize); |
| 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 @@ | ||
| 28 | 28 | ** The name of this file under configuration management is "sqlite.h.in". |
| 29 | 29 | ** The makefile makes some minor changes to this file (such as inserting |
| 30 | 30 | ** the version number) and changes its name to "sqlite3.h" as |
| 31 | 31 | ** part of the build process. |
| 32 | 32 | ** |
| 33 | -** @(#) $Id: sqlite.h.in,v 1.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 $ | |
| 34 | 34 | */ |
| 35 | 35 | #ifndef _SQLITE3_H_ |
| 36 | 36 | #define _SQLITE3_H_ |
| 37 | 37 | #include <stdarg.h> /* Needed for the definition of va_list */ |
| 38 | 38 | |
| @@ -105,12 +105,12 @@ | ||
| 105 | 105 | ** |
| 106 | 106 | ** {H10014} The SQLITE_VERSION_NUMBER #define shall resolve to an integer |
| 107 | 107 | ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z |
| 108 | 108 | ** are the major version, minor version, and release number. |
| 109 | 109 | */ |
| 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 | |
| 112 | 112 | |
| 113 | 113 | /* |
| 114 | 114 | ** CAPI3REF: Run-Time Library Version Numbers {H10020} <S60100> |
| 115 | 115 | ** KEYWORDS: sqlite3_version |
| 116 | 116 | ** |
| @@ -585,11 +585,11 @@ | ||
| 585 | 585 | ** |
| 586 | 586 | ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the |
| 587 | 587 | ** sync operation only needs to flush data to mass storage. Inode |
| 588 | 588 | ** information need not be flushed. The SQLITE_SYNC_NORMAL flag means |
| 589 | 589 | ** 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(). | |
| 591 | 591 | */ |
| 592 | 592 | #define SQLITE_SYNC_NORMAL 0x00002 |
| 593 | 593 | #define SQLITE_SYNC_FULL 0x00003 |
| 594 | 594 | #define SQLITE_SYNC_DATAONLY 0x00010 |
| 595 | 595 | |
| @@ -617,11 +617,11 @@ | ||
| 617 | 617 | ** This object defines the methods used to perform various operations |
| 618 | 618 | ** against the open file represented by the [sqlite3_file] object. |
| 619 | 619 | ** |
| 620 | 620 | ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or |
| 621 | 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] | |
| 622 | +** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY] | |
| 623 | 623 | ** flag may be ORed in to indicate that only the data of the file |
| 624 | 624 | ** and not its inode needs to be synced. |
| 625 | 625 | ** |
| 626 | 626 | ** The integer values to xLock() and xUnlock() are one of |
| 627 | 627 | ** <ul> |
| @@ -1272,11 +1272,14 @@ | ||
| 1272 | 1272 | ** scratch buffers or if no scratch buffer space is specified, then SQLite |
| 1273 | 1273 | ** goes to [sqlite3_malloc()] to obtain the memory it needs.</dd> |
| 1274 | 1274 | ** |
| 1275 | 1275 | ** <dt>SQLITE_CONFIG_PAGECACHE</dt> |
| 1276 | 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 | |
| 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 | |
| 1278 | 1281 | ** memory, the size of each page buffer (sz), and the number of pages (N). |
| 1279 | 1282 | ** The sz argument must be a power of two between 512 and 32768. The first |
| 1280 | 1283 | ** argument should point to an allocation of at least sz*N bytes of memory. |
| 1281 | 1284 | ** SQLite will use the memory provided by the first argument to satisfy its |
| 1282 | 1285 | ** memory needs for the first N pages that it adds to cache. If additional |
| @@ -1317,10 +1320,21 @@ | ||
| 1317 | 1320 | ** <dd>This option takes two arguments that determine the default |
| 1318 | 1321 | ** memory allcation lookaside optimization. The first argument is the |
| 1319 | 1322 | ** size of each lookaside buffer slot and the second is the number of |
| 1320 | 1323 | ** slots allocated to each database connection.</dd> |
| 1321 | 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 | +** | |
| 1322 | 1336 | ** </dl> |
| 1323 | 1337 | */ |
| 1324 | 1338 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ |
| 1325 | 1339 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ |
| 1326 | 1340 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ |
| @@ -1330,12 +1344,14 @@ | ||
| 1330 | 1344 | #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ |
| 1331 | 1345 | #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ |
| 1332 | 1346 | #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ |
| 1333 | 1347 | #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ |
| 1334 | 1348 | #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. */ | |
| 1336 | 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* */ | |
| 1337 | 1353 | |
| 1338 | 1354 | /* |
| 1339 | 1355 | ** CAPI3REF: Configuration Options {H10170} <S20000> |
| 1340 | 1356 | ** EXPERIMENTAL |
| 1341 | 1357 | ** |
| @@ -2381,11 +2397,11 @@ | ||
| 2381 | 2397 | #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ |
| 2382 | 2398 | #define SQLITE_REINDEX 27 /* Index Name NULL */ |
| 2383 | 2399 | #define SQLITE_ANALYZE 28 /* Table Name NULL */ |
| 2384 | 2400 | #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ |
| 2385 | 2401 | #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 */ | |
| 2387 | 2403 | #define SQLITE_COPY 0 /* No longer used */ |
| 2388 | 2404 | |
| 2389 | 2405 | /* |
| 2390 | 2406 | ** CAPI3REF: Tracing And Profiling Functions {H12280} <S60400> |
| 2391 | 2407 | ** EXPERIMENTAL |
| @@ -4149,11 +4165,11 @@ | ||
| 4149 | 4165 | ** |
| 4150 | 4166 | ** These functions are [deprecated]. In order to maintain |
| 4151 | 4167 | ** backwards compatibility with older code, these functions continue |
| 4152 | 4168 | ** to be supported. However, new applications should avoid |
| 4153 | 4169 | ** 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. | |
| 4155 | 4171 | */ |
| 4156 | 4172 | #ifndef SQLITE_OMIT_DEPRECATED |
| 4157 | 4173 | SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*); |
| 4158 | 4174 | SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*); |
| 4159 | 4175 | SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); |
| @@ -6552,10 +6568,153 @@ | ||
| 6552 | 6568 | ** |
| 6553 | 6569 | ** </dl> |
| 6554 | 6570 | */ |
| 6555 | 6571 | #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 |
| 6556 | 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 | +}; | |
| 6557 | 6716 | |
| 6558 | 6717 | /* |
| 6559 | 6718 | ** Undo the hack that converts floating point types to integer for |
| 6560 | 6719 | ** builds on processors without floating point support. |
| 6561 | 6720 | */ |
| 6562 | 6721 |
| --- 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 |