Fossil SCM

Update the built-in SQLite to the latest 3.7.5 beta.

drh 2011-01-25 19:03 trunk
Commit 07556099ab20600be46ccdd07f1fc2c524e98d3b
3 files changed +26 -1 +403 -239 +26 -17
+26 -1
--- src/shell.c
+++ src/shell.c
@@ -982,11 +982,11 @@
982982
iHiwtr = iCur = -1;
983983
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
984984
fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
985985
iHiwtr = iCur = -1;
986986
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
987
- fprintf(pArg->out, "Number of Allocations: %d (max %d)\n", iCur, iHiwtr);
987
+ fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
988988
/*
989989
** Not currently used by the CLI.
990990
** iHiwtr = iCur = -1;
991991
** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
992992
** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
@@ -1021,10 +1021,16 @@
10211021
10221022
if( pArg && pArg->out && db ){
10231023
iHiwtr = iCur = -1;
10241024
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
10251025
fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1026
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1027
+ fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1028
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1029
+ fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1030
+ sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1031
+ fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
10261032
iHiwtr = iCur = -1;
10271033
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
10281034
fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur);
10291035
iHiwtr = iCur = -1;
10301036
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
@@ -2540,10 +2546,11 @@
25402546
signal(SIGINT, interrupt_handler);
25412547
#endif
25422548
25432549
/* Do an initial pass through the command-line argument to locate
25442550
** the name of the database file, the name of the initialization file,
2551
+ ** the size of the alternative malloc heap,
25452552
** and the first command to execute.
25462553
*/
25472554
for(i=1; i<argc-1; i++){
25482555
char *z;
25492556
if( argv[i][0]!='-' ) break;
@@ -2558,10 +2565,26 @@
25582565
** informational messages (like from process_sqliterc) before
25592566
** we do the actual processing of arguments later in a second pass.
25602567
*/
25612568
}else if( strcmp(argv[i],"-batch")==0 ){
25622569
stdin_is_interactive = 0;
2570
+ }else if( strcmp(argv[i],"-heap")==0 ){
2571
+ int j, c;
2572
+ const char *zSize;
2573
+ sqlite3_int64 szHeap;
2574
+
2575
+ zSize = argv[++i];
2576
+ szHeap = atoi(zSize);
2577
+ for(j=0; (c = zSize[j])!=0; j++){
2578
+ if( c=='M' ){ szHeap *= 1000000; break; }
2579
+ if( c=='K' ){ szHeap *= 1000; break; }
2580
+ if( c=='G' ){ szHeap *= 1000000000; break; }
2581
+ }
2582
+ if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
2583
+#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
2584
+ sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
2585
+#endif
25632586
}
25642587
}
25652588
if( i<argc ){
25662589
#if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
25672590
data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
@@ -2670,10 +2693,12 @@
26702693
return 0;
26712694
}else if( strcmp(z,"-interactive")==0 ){
26722695
stdin_is_interactive = 1;
26732696
}else if( strcmp(z,"-batch")==0 ){
26742697
stdin_is_interactive = 0;
2698
+ }else if( strcmp(z,"-heap")==0 ){
2699
+ i++;
26752700
}else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
26762701
usage(1);
26772702
}else{
26782703
fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
26792704
fprintf(stderr,"Use -help for a list of options.\n");
26802705
--- src/shell.c
+++ src/shell.c
@@ -982,11 +982,11 @@
982 iHiwtr = iCur = -1;
983 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
984 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
985 iHiwtr = iCur = -1;
986 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
987 fprintf(pArg->out, "Number of Allocations: %d (max %d)\n", iCur, iHiwtr);
988 /*
989 ** Not currently used by the CLI.
990 ** iHiwtr = iCur = -1;
991 ** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
992 ** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
@@ -1021,10 +1021,16 @@
1021
1022 if( pArg && pArg->out && db ){
1023 iHiwtr = iCur = -1;
1024 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1025 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
 
 
 
 
 
 
1026 iHiwtr = iCur = -1;
1027 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1028 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur);
1029 iHiwtr = iCur = -1;
1030 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
@@ -2540,10 +2546,11 @@
2540 signal(SIGINT, interrupt_handler);
2541 #endif
2542
2543 /* Do an initial pass through the command-line argument to locate
2544 ** the name of the database file, the name of the initialization file,
 
2545 ** and the first command to execute.
2546 */
2547 for(i=1; i<argc-1; i++){
2548 char *z;
2549 if( argv[i][0]!='-' ) break;
@@ -2558,10 +2565,26 @@
2558 ** informational messages (like from process_sqliterc) before
2559 ** we do the actual processing of arguments later in a second pass.
2560 */
2561 }else if( strcmp(argv[i],"-batch")==0 ){
2562 stdin_is_interactive = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2563 }
2564 }
2565 if( i<argc ){
2566 #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
2567 data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
@@ -2670,10 +2693,12 @@
2670 return 0;
2671 }else if( strcmp(z,"-interactive")==0 ){
2672 stdin_is_interactive = 1;
2673 }else if( strcmp(z,"-batch")==0 ){
2674 stdin_is_interactive = 0;
 
 
2675 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
2676 usage(1);
2677 }else{
2678 fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
2679 fprintf(stderr,"Use -help for a list of options.\n");
2680
--- src/shell.c
+++ src/shell.c
@@ -982,11 +982,11 @@
982 iHiwtr = iCur = -1;
983 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
984 fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
985 iHiwtr = iCur = -1;
986 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
987 fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr);
988 /*
989 ** Not currently used by the CLI.
990 ** iHiwtr = iCur = -1;
991 ** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
992 ** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
@@ -1021,10 +1021,16 @@
1021
1022 if( pArg && pArg->out && db ){
1023 iHiwtr = iCur = -1;
1024 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
1025 fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
1026 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset);
1027 fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
1028 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset);
1029 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1030 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1031 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1032 iHiwtr = iCur = -1;
1033 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1034 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur);
1035 iHiwtr = iCur = -1;
1036 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
@@ -2540,10 +2546,11 @@
2546 signal(SIGINT, interrupt_handler);
2547 #endif
2548
2549 /* Do an initial pass through the command-line argument to locate
2550 ** the name of the database file, the name of the initialization file,
2551 ** the size of the alternative malloc heap,
2552 ** and the first command to execute.
2553 */
2554 for(i=1; i<argc-1; i++){
2555 char *z;
2556 if( argv[i][0]!='-' ) break;
@@ -2558,10 +2565,26 @@
2565 ** informational messages (like from process_sqliterc) before
2566 ** we do the actual processing of arguments later in a second pass.
2567 */
2568 }else if( strcmp(argv[i],"-batch")==0 ){
2569 stdin_is_interactive = 0;
2570 }else if( strcmp(argv[i],"-heap")==0 ){
2571 int j, c;
2572 const char *zSize;
2573 sqlite3_int64 szHeap;
2574
2575 zSize = argv[++i];
2576 szHeap = atoi(zSize);
2577 for(j=0; (c = zSize[j])!=0; j++){
2578 if( c=='M' ){ szHeap *= 1000000; break; }
2579 if( c=='K' ){ szHeap *= 1000; break; }
2580 if( c=='G' ){ szHeap *= 1000000000; break; }
2581 }
2582 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
2583 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
2584 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
2585 #endif
2586 }
2587 }
2588 if( i<argc ){
2589 #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2
2590 data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] );
@@ -2670,10 +2693,12 @@
2693 return 0;
2694 }else if( strcmp(z,"-interactive")==0 ){
2695 stdin_is_interactive = 1;
2696 }else if( strcmp(z,"-batch")==0 ){
2697 stdin_is_interactive = 0;
2698 }else if( strcmp(z,"-heap")==0 ){
2699 i++;
2700 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
2701 usage(1);
2702 }else{
2703 fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
2704 fprintf(stderr,"Use -help for a list of options.\n");
2705
+403 -239
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -650,11 +650,11 @@
650650
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651651
** [sqlite_version()] and [sqlite_source_id()].
652652
*/
653653
#define SQLITE_VERSION "3.7.5"
654654
#define SQLITE_VERSION_NUMBER 3007005
655
-#define SQLITE_SOURCE_ID "2011-01-17 02:24:12 b93f6f3e679c7710f42580a8dd9ce43136376c1d"
655
+#define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
656656
657657
/*
658658
** CAPI3REF: Run-Time Library Version Numbers
659659
** KEYWORDS: sqlite3_version, sqlite3_sourceid
660660
**
@@ -1262,24 +1262,28 @@
12621262
** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
12631263
** to the [sqlite3_file] object associated with a particular database
12641264
** connection. See the [sqlite3_file_control()] documentation for
12651265
** additional information.
12661266
**
1267
-** The [SQLITE_FCNTL_SYNC] opcode is used internally. SQLite calls
1268
-** the file-control method with this opcode immediately after the database
1269
-** file is synced, or if the database is running in synchronous=off mode
1270
-** immediately after it would have been synced otherwise. This makes it
1271
-** easier to write special VFS modules that depend on the xSync call.
1267
+** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
1268
+** SQLite and sent to all VFSes in place of a call to the xSync method
1269
+** when the database connection has [PRAGMA synchronous] set to OFF.)^
1270
+** Some specialized VFSes need this signal in order to operate correctly
1271
+** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
1272
+** VFSes do not need this signal and should silently ignore this opcode.
1273
+** Applications should not call [sqlite3_file_control()] with this
1274
+** opcode as doing so may disrupt the operation of the specilized VFSes
1275
+** that do require it.
12721276
*/
12731277
#define SQLITE_FCNTL_LOCKSTATE 1
12741278
#define SQLITE_GET_LOCKPROXYFILE 2
12751279
#define SQLITE_SET_LOCKPROXYFILE 3
12761280
#define SQLITE_LAST_ERRNO 4
12771281
#define SQLITE_FCNTL_SIZE_HINT 5
12781282
#define SQLITE_FCNTL_CHUNK_SIZE 6
12791283
#define SQLITE_FCNTL_FILE_POINTER 7
1280
-#define SQLITE_FCNTL_SYNC 8
1284
+#define SQLITE_FCNTL_SYNC_OMITTED 8
12811285
12821286
12831287
/*
12841288
** CAPI3REF: Mutex Handle
12851289
**
@@ -2395,11 +2399,11 @@
23952399
** The strings returned by these two routines should be
23962400
** released by [sqlite3_free()]. ^Both routines return a
23972401
** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
23982402
** memory to hold the resulting string.
23992403
**
2400
-** ^(In sqlite3_snprintf() routine is similar to "snprintf()" from
2404
+** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
24012405
** the standard C library. The result is written into the
24022406
** buffer supplied as the second parameter whose size is given by
24032407
** the first parameter. Note that the order of the
24042408
** first two parameters is reversed from snprintf().)^ This is an
24052409
** historical accident that cannot be fixed without breaking
@@ -3207,11 +3211,11 @@
32073211
32083212
/*
32093213
** CAPI3REF: Determine If An SQL Statement Writes The Database
32103214
**
32113215
** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3212
-** and only if the [prepared statement] X is makes no direct changes to
3216
+** and only if the [prepared statement] X makes no direct changes to
32133217
** the content of the database file.
32143218
**
32153219
** Note that [application-defined SQL functions] or
32163220
** [virtual tables] might change the database indirectly as a side effect.
32173221
** ^(For example, if an application defines a function "eval()" that
@@ -3624,17 +3628,21 @@
36243628
** already been [sqlite3_finalize | finalized] or on one that had
36253629
** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
36263630
** be the case that the same database connection is being used by two or
36273631
** more threads at the same moment in time.
36283632
**
3629
-** For all versions of SQLite up to and including 3.6.23.1, it was required
3630
-** after sqlite3_step() returned anything other than [SQLITE_ROW] that
3631
-** [sqlite3_reset()] be called before any subsequent invocation of
3632
-** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
3633
-** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
3634
-** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
3635
-** automatically in this circumstance rather than returning [SQLITE_MISUSE].
3633
+** For all versions of SQLite up to and including 3.6.23.1, a call to
3634
+** [sqlite3_reset()] was required after sqlite3_step() returned anything
3635
+** other than [SQLITE_ROW] before any subsequent invocation of
3636
+** sqlite3_step(). Failure to reset the prepared statement using
3637
+** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3638
+** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began
3639
+** calling [sqlite3_reset()] automatically in this circumstance rather
3640
+** than returning [SQLITE_MISUSE]. This is not considered a compatibility
3641
+** break because any application that ever receives an SQLITE_MISUSE error
3642
+** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
3643
+** can be used to restore the legacy behavior.
36363644
**
36373645
** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
36383646
** API always returns a generic error code, [SQLITE_ERROR], following any
36393647
** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
36403648
** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
@@ -5808,11 +5816,12 @@
58085816
#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
58095817
#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
58105818
#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
58115819
#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
58125820
#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
5813
-#define SQLITE_MUTEX_STATIC_LRU2 7 /* lru page list */
5821
+#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
5822
+#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
58145823
58155824
/*
58165825
** CAPI3REF: Retrieve the mutex for a database connection
58175826
**
58185827
** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -9016,10 +9025,11 @@
90169025
int nExtension; /* Number of loaded extensions */
90179026
void **aExtension; /* Array of shared library handles */
90189027
struct Vdbe *pVdbe; /* List of active virtual machines */
90199028
int activeVdbeCnt; /* Number of VDBEs currently executing */
90209029
int writeVdbeCnt; /* Number of active VDBEs that are writing */
9030
+ int vdbeExecCnt; /* Number of nested calls to VdbeExec() */
90219031
void (*xTrace)(void*,const char*); /* Trace function */
90229032
void *pTraceArg; /* Argument to the trace function */
90239033
void (*xProfile)(void*,const char*,u64); /* Profiling function */
90249034
void *pProfileArg; /* Argument to profile function */
90259035
void *pCommitArg; /* Argument to xCommitCallback() */
@@ -11787,10 +11797,13 @@
1178711797
#ifdef SQLITE_OMIT_AUTOINIT
1178811798
"OMIT_AUTOINIT",
1178911799
#endif
1179011800
#ifdef SQLITE_OMIT_AUTOMATIC_INDEX
1179111801
"OMIT_AUTOMATIC_INDEX",
11802
+#endif
11803
+#ifdef SQLITE_OMIT_AUTORESET
11804
+ "OMIT_AUTORESET",
1179211805
#endif
1179311806
#ifdef SQLITE_OMIT_AUTOVACUUM
1179411807
"OMIT_AUTOVACUUM",
1179511808
#endif
1179611809
#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
@@ -19048,10 +19061,11 @@
1904819061
N = p->nAlloc - p->nChar - 1;
1904919062
if( N<=0 ){
1905019063
return;
1905119064
}
1905219065
}else{
19066
+ char *zOld = (p->zText==p->zBase ? 0 : p->zText);
1905319067
i64 szNew = p->nChar;
1905419068
szNew += N + 1;
1905519069
if( szNew > p->mxAlloc ){
1905619070
sqlite3StrAccumReset(p);
1905719071
p->tooBig = 1;
@@ -19058,17 +19072,16 @@
1905819072
return;
1905919073
}else{
1906019074
p->nAlloc = (int)szNew;
1906119075
}
1906219076
if( p->useMalloc==1 ){
19063
- zNew = sqlite3DbMallocRaw(p->db, p->nAlloc );
19077
+ zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
1906419078
}else{
19065
- zNew = sqlite3_malloc(p->nAlloc);
19079
+ zNew = sqlite3_realloc(zOld, p->nAlloc);
1906619080
}
1906719081
if( zNew ){
19068
- memcpy(zNew, p->zText, p->nChar);
19069
- sqlite3StrAccumReset(p);
19082
+ if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
1907019083
p->zText = zNew;
1907119084
}else{
1907219085
p->mallocFailed = 1;
1907319086
sqlite3StrAccumReset(p);
1907419087
return;
@@ -33400,10 +33413,42 @@
3340033413
3340133414
3340233415
typedef struct PCache1 PCache1;
3340333416
typedef struct PgHdr1 PgHdr1;
3340433417
typedef struct PgFreeslot PgFreeslot;
33418
+typedef struct PGroup PGroup;
33419
+
33420
+/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
33421
+** of one or more PCaches that are able to recycle each others unpinned
33422
+** pages when they are under memory pressure. A PGroup is an instance of
33423
+** the following object.
33424
+**
33425
+** This page cache implementation works in one of two modes:
33426
+**
33427
+** (1) Every PCache is the sole member of its own PGroup. There is
33428
+** one PGroup per PCache.
33429
+**
33430
+** (2) There is a single global PGroup that all PCaches are a member
33431
+** of.
33432
+**
33433
+** Mode 1 uses more memory (since PCache instances are not able to rob
33434
+** unused pages from other PCaches) but it also operates without a mutex,
33435
+** and is therefore often faster. Mode 2 requires a mutex in order to be
33436
+** threadsafe, but is able recycle pages more efficient.
33437
+**
33438
+** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
33439
+** PGroup which is the pcache1.grp global variable and its mutex is
33440
+** SQLITE_MUTEX_STATIC_LRU.
33441
+*/
33442
+struct PGroup {
33443
+ sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
33444
+ int nMaxPage; /* Sum of nMax for purgeable caches */
33445
+ int nMinPage; /* Sum of nMin for purgeable caches */
33446
+ int mxPinned; /* nMaxpage + 10 - nMinPage */
33447
+ int nCurrentPage; /* Number of purgeable pages allocated */
33448
+ PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
33449
+};
3340533450
3340633451
/* Each page cache is an instance of the following object. Every
3340733452
** open database file (including each in-memory database and each
3340833453
** temporary or transient database) has a single page cache which
3340933454
** is an instance of this object.
@@ -33413,20 +33458,21 @@
3341333458
*/
3341433459
struct PCache1 {
3341533460
/* Cache configuration parameters. Page size (szPage) and the purgeable
3341633461
** flag (bPurgeable) are set when the cache is created. nMax may be
3341733462
** modified at any time by a call to the pcache1CacheSize() method.
33418
- ** The global mutex must be held when accessing nMax.
33463
+ ** The PGroup mutex must be held when accessing nMax.
3341933464
*/
33465
+ PGroup *pGroup; /* PGroup this cache belongs to */
3342033466
int szPage; /* Size of allocated pages in bytes */
3342133467
int bPurgeable; /* True if cache is purgeable */
3342233468
unsigned int nMin; /* Minimum number of pages reserved */
3342333469
unsigned int nMax; /* Configured "cache_size" value */
33470
+ unsigned int mxPinned; /* nMax*9/10 */
3342433471
3342533472
/* Hash table of all pages. The following variables may only be accessed
33426
- ** when the accessor is holding the global mutex (see pcache1EnterMutex()
33427
- ** and pcache1LeaveMutex()).
33473
+ ** when the accessor is holding the PGroup mutex.
3342833474
*/
3342933475
unsigned int nRecyclable; /* Number of pages in the LRU list */
3343033476
unsigned int nPage; /* Total number of pages in apHash */
3343133477
unsigned int nHash; /* Number of slots in apHash[] */
3343233478
PgHdr1 **apHash; /* Hash table for fast lookup by key */
@@ -33458,25 +33504,31 @@
3345833504
3345933505
/*
3346033506
** Global data used by this cache.
3346133507
*/
3346233508
static SQLITE_WSD struct PCacheGlobal {
33463
- sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
33464
-
33465
- int nMaxPage; /* Sum of nMaxPage for purgeable caches */
33466
- int nMinPage; /* Sum of nMinPage for purgeable caches */
33467
- int nCurrentPage; /* Number of purgeable pages allocated */
33468
- PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
33469
-
33470
- /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
33471
- int szSlot; /* Size of each free slot */
33472
- int nSlot; /* The number of pcache slots */
33473
- int nFreeSlot; /* Number of unused pcache slots */
33474
- int nReserve; /* Try to keep nFreeSlot above this */
33475
- void *pStart, *pEnd; /* Bounds of pagecache malloc range */
33476
- PgFreeslot *pFree; /* Free page blocks */
33477
- int isInit; /* True if initialized */
33509
+ PGroup grp; /* The global PGroup for mode (2) */
33510
+
33511
+ /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
33512
+ ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
33513
+ ** fixed at sqlite3_initialize() time and do not require mutex protection.
33514
+ ** The nFreeSlot and pFree values do require mutex protection.
33515
+ */
33516
+ int isInit; /* True if initialized */
33517
+ int szSlot; /* Size of each free slot */
33518
+ int nSlot; /* The number of pcache slots */
33519
+ int nReserve; /* Try to keep nFreeSlot above this */
33520
+ void *pStart, *pEnd; /* Bounds of pagecache malloc range */
33521
+ /* Above requires no mutex. Use mutex below for variable that follow. */
33522
+ sqlite3_mutex *mutex; /* Mutex for accessing the following: */
33523
+ int nFreeSlot; /* Number of unused pcache slots */
33524
+ PgFreeslot *pFree; /* Free page blocks */
33525
+ /* The following value requires a mutex to change. We skip the mutex on
33526
+ ** reading because (1) most platforms read a 32-bit integer atomically and
33527
+ ** (2) even if an incorrect value is read, no great harm is done since this
33528
+ ** is really just an optimization. */
33529
+ int bUnderPressure; /* True if low on PAGECACHE memory */
3347833530
} pcache1_g;
3347933531
3348033532
/*
3348133533
** All code in this file should access the global structure above via the
3348233534
** alias "pcache1". This ensures that the WSD emulation is used when
@@ -33498,23 +33550,26 @@
3349833550
*/
3349933551
#define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
3350033552
#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
3350133553
3350233554
/*
33503
-** Macros to enter and leave the global LRU mutex.
33555
+** Macros to enter and leave the PCache LRU mutex.
3350433556
*/
33505
-#define pcache1EnterMutex() sqlite3_mutex_enter(pcache1.mutex)
33506
-#define pcache1LeaveMutex() sqlite3_mutex_leave(pcache1.mutex)
33557
+#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
33558
+#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
3350733559
3350833560
/******************************************************************************/
3350933561
/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
3351033562
3351133563
/*
3351233564
** This function is called during initialization if a static buffer is
3351333565
** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
3351433566
** verb to sqlite3_config(). Parameter pBuf points to an allocation large
3351533567
** enough to contain 'n' buffers of 'sz' bytes each.
33568
+**
33569
+** This routine is called from sqlite3_initialize() and so it is guaranteed
33570
+** to be serialized already. There is no need for further mutexing.
3351633571
*/
3351733572
SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
3351833573
if( pcache1.isInit ){
3351933574
PgFreeslot *p;
3352033575
sz = ROUNDDOWN8(sz);
@@ -33521,10 +33576,11 @@
3352133576
pcache1.szSlot = sz;
3352233577
pcache1.nSlot = pcache1.nFreeSlot = n;
3352333578
pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
3352433579
pcache1.pStart = pBuf;
3352533580
pcache1.pFree = 0;
33581
+ pcache1.bUnderPressure = 0;
3352633582
while( n-- ){
3352733583
p = (PgFreeslot*)pBuf;
3352833584
p->pNext = pcache1.pFree;
3352933585
pcache1.pFree = p;
3353033586
pBuf = (void*)&((char*)pBuf)[sz];
@@ -33536,33 +33592,35 @@
3353633592
/*
3353733593
** Malloc function used within this file to allocate space from the buffer
3353833594
** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
3353933595
** such buffer exists or there is no space left in it, this function falls
3354033596
** back to sqlite3Malloc().
33597
+**
33598
+** Multiple threads can run this routine at the same time. Global variables
33599
+** in pcache1 need to be protected via mutex.
3354133600
*/
3354233601
static void *pcache1Alloc(int nByte){
33543
- void *p;
33544
- assert( sqlite3_mutex_held(pcache1.mutex) );
33602
+ void *p = 0;
33603
+ assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
3354533604
sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
33546
- if( nByte<=pcache1.szSlot && pcache1.pFree ){
33547
- assert( pcache1.isInit );
33605
+ if( nByte<=pcache1.szSlot ){
33606
+ sqlite3_mutex_enter(pcache1.mutex);
3354833607
p = (PgHdr1 *)pcache1.pFree;
33549
- pcache1.pFree = pcache1.pFree->pNext;
33550
- pcache1.nFreeSlot--;
33551
- assert( pcache1.nFreeSlot>=0 );
33552
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
33553
- }else{
33554
-
33555
- /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
33556
- ** global pcache mutex and unlock the pager-cache object pCache. This is
33557
- ** so that if the attempt to allocate a new buffer causes the the
33558
- ** configured soft-heap-limit to be breached, it will be possible to
33559
- ** reclaim memory from this pager-cache.
33608
+ if( p ){
33609
+ pcache1.pFree = pcache1.pFree->pNext;
33610
+ pcache1.nFreeSlot--;
33611
+ pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
33612
+ assert( pcache1.nFreeSlot>=0 );
33613
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
33614
+ }
33615
+ sqlite3_mutex_leave(pcache1.mutex);
33616
+ }
33617
+ if( p==0 ){
33618
+ /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
33619
+ ** it from sqlite3Malloc instead.
3356033620
*/
33561
- pcache1LeaveMutex();
3356233621
p = sqlite3Malloc(nByte);
33563
- pcache1EnterMutex();
3356433622
if( p ){
3356533623
int sz = sqlite3MallocSize(p);
3356633624
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
3356733625
}
3356833626
sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
@@ -33572,20 +33630,22 @@
3357233630
3357333631
/*
3357433632
** Free an allocated buffer obtained from pcache1Alloc().
3357533633
*/
3357633634
static void pcache1Free(void *p){
33577
- assert( sqlite3_mutex_held(pcache1.mutex) );
3357833635
if( p==0 ) return;
3357933636
if( p>=pcache1.pStart && p<pcache1.pEnd ){
3358033637
PgFreeslot *pSlot;
33638
+ sqlite3_mutex_enter(pcache1.mutex);
3358133639
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
3358233640
pSlot = (PgFreeslot*)p;
3358333641
pSlot->pNext = pcache1.pFree;
3358433642
pcache1.pFree = pSlot;
3358533643
pcache1.nFreeSlot++;
33644
+ pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
3358633645
assert( pcache1.nFreeSlot<=pcache1.nSlot );
33646
+ sqlite3_mutex_leave(pcache1.mutex);
3358733647
}else{
3358833648
int iSize;
3358933649
assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
3359033650
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
3359133651
iSize = sqlite3MallocSize(p);
@@ -33597,11 +33657,10 @@
3359733657
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
3359833658
/*
3359933659
** Return the size of a pcache allocation
3360033660
*/
3360133661
static int pcache1MemSize(void *p){
33602
- assert( sqlite3_mutex_held(pcache1.mutex) );
3360333662
if( p>=pcache1.pStart && p<pcache1.pEnd ){
3360433663
return pcache1.szSlot;
3360533664
}else{
3360633665
int iSize;
3360733666
assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
@@ -33621,11 +33680,11 @@
3362133680
void *pPg = pcache1Alloc(nByte);
3362233681
PgHdr1 *p;
3362333682
if( pPg ){
3362433683
p = PAGE_TO_PGHDR1(pCache, pPg);
3362533684
if( pCache->bPurgeable ){
33626
- pcache1.nCurrentPage++;
33685
+ pCache->pGroup->nCurrentPage++;
3362733686
}
3362833687
}else{
3362933688
p = 0;
3363033689
}
3363133690
return p;
@@ -33638,12 +33697,13 @@
3363833697
** that the current implementation happens to never call this routine
3363933698
** with a NULL pointer, so we mark the NULL test with ALWAYS().
3364033699
*/
3364133700
static void pcache1FreePage(PgHdr1 *p){
3364233701
if( ALWAYS(p) ){
33643
- if( p->pCache->bPurgeable ){
33644
- pcache1.nCurrentPage--;
33702
+ PCache1 *pCache = p->pCache;
33703
+ if( pCache->bPurgeable ){
33704
+ pCache->pGroup->nCurrentPage--;
3364533705
}
3364633706
pcache1Free(PGHDR1_TO_PAGE(p));
3364733707
}
3364833708
}
3364933709
@@ -33651,24 +33711,18 @@
3365133711
** Malloc function used by SQLite to obtain space from the buffer configured
3365233712
** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
3365333713
** exists, this function falls back to sqlite3Malloc().
3365433714
*/
3365533715
SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
33656
- void *p;
33657
- pcache1EnterMutex();
33658
- p = pcache1Alloc(sz);
33659
- pcache1LeaveMutex();
33660
- return p;
33716
+ return pcache1Alloc(sz);
3366133717
}
3366233718
3366333719
/*
3366433720
** Free an allocated buffer obtained from sqlite3PageMalloc().
3366533721
*/
3366633722
SQLITE_PRIVATE void sqlite3PageFree(void *p){
33667
- pcache1EnterMutex();
3366833723
pcache1Free(p);
33669
- pcache1LeaveMutex();
3367033724
}
3367133725
3367233726
3367333727
/*
3367433728
** Return true if it desirable to avoid allocating a new page cache
@@ -33685,13 +33739,12 @@
3368533739
** under memory pressure, then again it is desirable to avoid
3368633740
** allocating a new page cache entry in order to avoid stressing
3368733741
** the heap even further.
3368833742
*/
3368933743
static int pcache1UnderMemoryPressure(PCache1 *pCache){
33690
- assert( sqlite3_mutex_held(pcache1.mutex) );
3369133744
if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
33692
- return pcache1.nFreeSlot<pcache1.nReserve;
33745
+ return pcache1.bUnderPressure;
3369333746
}else{
3369433747
return sqlite3HeapNearlyFull();
3369533748
}
3369633749
}
3369733750
@@ -33700,29 +33753,29 @@
3370033753
3370133754
/*
3370233755
** This function is used to resize the hash table used by the cache passed
3370333756
** as the first argument.
3370433757
**
33705
-** The global mutex must be held when this function is called.
33758
+** The PCache mutex must be held when this function is called.
3370633759
*/
3370733760
static int pcache1ResizeHash(PCache1 *p){
3370833761
PgHdr1 **apNew;
3370933762
unsigned int nNew;
3371033763
unsigned int i;
3371133764
33712
- assert( sqlite3_mutex_held(pcache1.mutex) );
33765
+ assert( sqlite3_mutex_held(p->pGroup->mutex) );
3371333766
3371433767
nNew = p->nHash*2;
3371533768
if( nNew<256 ){
3371633769
nNew = 256;
3371733770
}
3371833771
33719
- pcache1LeaveMutex();
33772
+ pcache1LeaveMutex(p->pGroup);
3372033773
if( p->nHash ){ sqlite3BeginBenignMalloc(); }
3372133774
apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
3372233775
if( p->nHash ){ sqlite3EndBenignMalloc(); }
33723
- pcache1EnterMutex();
33776
+ pcache1EnterMutex(p->pGroup);
3372433777
if( apNew ){
3372533778
memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
3372633779
for(i=0; i<p->nHash; i++){
3372733780
PgHdr1 *pPage;
3372833781
PgHdr1 *pNext = p->apHash[i];
@@ -33741,29 +33794,37 @@
3374133794
return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
3374233795
}
3374333796
3374433797
/*
3374533798
** This function is used internally to remove the page pPage from the
33746
-** global LRU list, if is part of it. If pPage is not part of the global
33799
+** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
3374733800
** LRU list, then this function is a no-op.
3374833801
**
33749
-** The global mutex must be held when this function is called.
33802
+** The PGroup mutex must be held when this function is called.
33803
+**
33804
+** If pPage is NULL then this routine is a no-op.
3375033805
*/
3375133806
static void pcache1PinPage(PgHdr1 *pPage){
33752
- assert( sqlite3_mutex_held(pcache1.mutex) );
33753
- if( pPage && (pPage->pLruNext || pPage==pcache1.pLruTail) ){
33807
+ PCache1 *pCache;
33808
+ PGroup *pGroup;
33809
+
33810
+ if( pPage==0 ) return;
33811
+ pCache = pPage->pCache;
33812
+ pGroup = pCache->pGroup;
33813
+ assert( sqlite3_mutex_held(pGroup->mutex) );
33814
+ if( pPage->pLruNext || pPage==pGroup->pLruTail ){
3375433815
if( pPage->pLruPrev ){
3375533816
pPage->pLruPrev->pLruNext = pPage->pLruNext;
3375633817
}
3375733818
if( pPage->pLruNext ){
3375833819
pPage->pLruNext->pLruPrev = pPage->pLruPrev;
3375933820
}
33760
- if( pcache1.pLruHead==pPage ){
33761
- pcache1.pLruHead = pPage->pLruNext;
33821
+ if( pGroup->pLruHead==pPage ){
33822
+ pGroup->pLruHead = pPage->pLruNext;
3376233823
}
33763
- if( pcache1.pLruTail==pPage ){
33764
- pcache1.pLruTail = pPage->pLruPrev;
33824
+ if( pGroup->pLruTail==pPage ){
33825
+ pGroup->pLruTail = pPage->pLruPrev;
3376533826
}
3376633827
pPage->pLruNext = 0;
3376733828
pPage->pLruPrev = 0;
3376833829
pPage->pCache->nRecyclable--;
3376933830
}
@@ -33772,32 +33833,34 @@
3377233833
3377333834
/*
3377433835
** Remove the page supplied as an argument from the hash table
3377533836
** (PCache1.apHash structure) that it is currently stored in.
3377633837
**
33777
-** The global mutex must be held when this function is called.
33838
+** The PGroup mutex must be held when this function is called.
3377833839
*/
3377933840
static void pcache1RemoveFromHash(PgHdr1 *pPage){
3378033841
unsigned int h;
3378133842
PCache1 *pCache = pPage->pCache;
3378233843
PgHdr1 **pp;
3378333844
33845
+ assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
3378433846
h = pPage->iKey % pCache->nHash;
3378533847
for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
3378633848
*pp = (*pp)->pNext;
3378733849
3378833850
pCache->nPage--;
3378933851
}
3379033852
3379133853
/*
33792
-** If there are currently more than pcache.nMaxPage pages allocated, try
33793
-** to recycle pages to reduce the number allocated to pcache.nMaxPage.
33854
+** If there are currently more than nMaxPage pages allocated, try
33855
+** to recycle pages to reduce the number allocated to nMaxPage.
3379433856
*/
33795
-static void pcache1EnforceMaxPage(void){
33796
- assert( sqlite3_mutex_held(pcache1.mutex) );
33797
- while( pcache1.nCurrentPage>pcache1.nMaxPage && pcache1.pLruTail ){
33798
- PgHdr1 *p = pcache1.pLruTail;
33857
+static void pcache1EnforceMaxPage(PGroup *pGroup){
33858
+ assert( sqlite3_mutex_held(pGroup->mutex) );
33859
+ while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
33860
+ PgHdr1 *p = pGroup->pLruTail;
33861
+ assert( p->pCache->pGroup==pGroup );
3379933862
pcache1PinPage(p);
3380033863
pcache1RemoveFromHash(p);
3380133864
pcache1FreePage(p);
3380233865
}
3380333866
}
@@ -33805,19 +33868,19 @@
3380533868
/*
3380633869
** Discard all pages from cache pCache with a page number (key value)
3380733870
** greater than or equal to iLimit. Any pinned pages that meet this
3380833871
** criteria are unpinned before they are discarded.
3380933872
**
33810
-** The global mutex must be held when this function is called.
33873
+** The PCache mutex must be held when this function is called.
3381133874
*/
3381233875
static void pcache1TruncateUnsafe(
33813
- PCache1 *pCache,
33814
- unsigned int iLimit
33876
+ PCache1 *pCache, /* The cache to truncate */
33877
+ unsigned int iLimit /* Drop pages with this pgno or larger */
3381533878
){
33816
- TESTONLY( unsigned int nPage = 0; ) /* Used to assert pCache->nPage is correct */
33879
+ TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
3381733880
unsigned int h;
33818
- assert( sqlite3_mutex_held(pcache1.mutex) );
33881
+ assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
3381933882
for(h=0; h<pCache->nHash; h++){
3382033883
PgHdr1 **pp = &pCache->apHash[h];
3382133884
PgHdr1 *pPage;
3382233885
while( (pPage = *pp)!=0 ){
3382333886
if( pPage->iKey>=iLimit ){
@@ -33843,12 +33906,14 @@
3384333906
static int pcache1Init(void *NotUsed){
3384433907
UNUSED_PARAMETER(NotUsed);
3384533908
assert( pcache1.isInit==0 );
3384633909
memset(&pcache1, 0, sizeof(pcache1));
3384733910
if( sqlite3GlobalConfig.bCoreMutex ){
33848
- pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
33911
+ pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
33912
+ pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
3384933913
}
33914
+ pcache1.grp.mxPinned = 10;
3385033915
pcache1.isInit = 1;
3385133916
return SQLITE_OK;
3385233917
}
3385333918
3385433919
/*
@@ -33866,22 +33931,51 @@
3386633931
** Implementation of the sqlite3_pcache.xCreate method.
3386733932
**
3386833933
** Allocate a new cache.
3386933934
*/
3387033935
static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
33871
- PCache1 *pCache;
33936
+ PCache1 *pCache; /* The newly created page cache */
33937
+ PGroup *pGroup; /* The group the new page cache will belong to */
33938
+ int sz; /* Bytes of memory required to allocate the new cache */
3387233939
33873
- pCache = (PCache1 *)sqlite3_malloc(sizeof(PCache1));
33940
+ /*
33941
+ ** The seperateCache variable is true if each PCache has its own private
33942
+ ** PGroup. In other words, separateCache is true for mode (1) where no
33943
+ ** mutexing is required.
33944
+ **
33945
+ ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
33946
+ **
33947
+ ** * Always use a unified cache in single-threaded applications
33948
+ **
33949
+ ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
33950
+ ** use separate caches (mode-1)
33951
+ */
33952
+#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
33953
+ const int separateCache = 0;
33954
+#else
33955
+ int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
33956
+#endif
33957
+
33958
+ sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
33959
+ pCache = (PCache1 *)sqlite3_malloc(sz);
3387433960
if( pCache ){
33875
- memset(pCache, 0, sizeof(PCache1));
33961
+ memset(pCache, 0, sz);
33962
+ if( separateCache ){
33963
+ pGroup = (PGroup*)&pCache[1];
33964
+ pGroup->mxPinned = 10;
33965
+ }else{
33966
+ pGroup = &pcache1_g.grp;
33967
+ }
33968
+ pCache->pGroup = pGroup;
3387633969
pCache->szPage = szPage;
3387733970
pCache->bPurgeable = (bPurgeable ? 1 : 0);
3387833971
if( bPurgeable ){
3387933972
pCache->nMin = 10;
33880
- pcache1EnterMutex();
33881
- pcache1.nMinPage += pCache->nMin;
33882
- pcache1LeaveMutex();
33973
+ pcache1EnterMutex(pGroup);
33974
+ pGroup->nMinPage += pCache->nMin;
33975
+ pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
33976
+ pcache1LeaveMutex(pGroup);
3388333977
}
3388433978
}
3388533979
return (sqlite3_pcache *)pCache;
3388633980
}
3388733981
@@ -33891,26 +33985,30 @@
3389133985
** Configure the cache_size limit for a cache.
3389233986
*/
3389333987
static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
3389433988
PCache1 *pCache = (PCache1 *)p;
3389533989
if( pCache->bPurgeable ){
33896
- pcache1EnterMutex();
33897
- pcache1.nMaxPage += (nMax - pCache->nMax);
33990
+ PGroup *pGroup = pCache->pGroup;
33991
+ pcache1EnterMutex(pGroup);
33992
+ pGroup->nMaxPage += (nMax - pCache->nMax);
33993
+ pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
3389833994
pCache->nMax = nMax;
33899
- pcache1EnforceMaxPage();
33900
- pcache1LeaveMutex();
33995
+ pCache->mxPinned = nMax*9/10;
33996
+ pcache1EnforceMaxPage(pGroup);
33997
+ pcache1LeaveMutex(pGroup);
3390133998
}
3390233999
}
3390334000
3390434001
/*
3390534002
** Implementation of the sqlite3_pcache.xPagecount method.
3390634003
*/
3390734004
static int pcache1Pagecount(sqlite3_pcache *p){
3390834005
int n;
33909
- pcache1EnterMutex();
33910
- n = ((PCache1 *)p)->nPage;
33911
- pcache1LeaveMutex();
34006
+ PCache1 *pCache = (PCache1*)p;
34007
+ pcache1EnterMutex(pCache->pGroup);
34008
+ n = pCache->nPage;
34009
+ pcache1LeaveMutex(pCache->pGroup);
3391234010
return n;
3391334011
}
3391434012
3391534013
/*
3391634014
** Implementation of the sqlite3_pcache.xFetch method.
@@ -33965,65 +34063,89 @@
3396534063
** proceed to step 5.
3396634064
**
3396734065
** 5. Otherwise, allocate and return a new page buffer.
3396834066
*/
3396934067
static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
33970
- unsigned int nPinned;
34068
+ int nPinned;
3397134069
PCache1 *pCache = (PCache1 *)p;
34070
+ PGroup *pGroup;
3397234071
PgHdr1 *pPage = 0;
3397334072
3397434073
assert( pCache->bPurgeable || createFlag!=1 );
33975
- pcache1EnterMutex();
33976
- if( createFlag==1 ) sqlite3BeginBenignMalloc();
34074
+ assert( pCache->bPurgeable || pCache->nMin==0 );
34075
+ assert( pCache->bPurgeable==0 || pCache->nMin==10 );
34076
+ assert( pCache->nMin==0 || pCache->bPurgeable );
34077
+ pcache1EnterMutex(pGroup = pCache->pGroup);
3397734078
33978
- /* Search the hash table for an existing entry. */
34079
+ /* Step 1: Search the hash table for an existing entry. */
3397934080
if( pCache->nHash>0 ){
3398034081
unsigned int h = iKey % pCache->nHash;
3398134082
for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
3398234083
}
3398334084
34085
+ /* Step 2: Abort if no existing page is found and createFlag is 0 */
3398434086
if( pPage || createFlag==0 ){
3398534087
pcache1PinPage(pPage);
3398634088
goto fetch_out;
3398734089
}
3398834090
33989
- /* Step 3 of header comment. */
34091
+ /* The pGroup local variable will normally be initialized by the
34092
+ ** pcache1EnterMutex() macro above. But if SQLITE_MUTEX_OMIT is defined,
34093
+ ** then pcache1EnterMutex() is a no-op, so we have to initialize the
34094
+ ** local variable here. Delaying the initialization of pGroup is an
34095
+ ** optimization: The common case is to exit the module before reaching
34096
+ ** this point.
34097
+ */
34098
+#ifdef SQLITE_MUTEX_OMIT
34099
+ pGroup = pCache->pGroup;
34100
+#endif
34101
+
34102
+
34103
+ /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
3399034104
nPinned = pCache->nPage - pCache->nRecyclable;
34105
+ assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
34106
+ assert( pCache->mxPinned == pCache->nMax*9/10 );
3399134107
if( createFlag==1 && (
33992
- nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
33993
- || nPinned>=(pCache->nMax * 9 / 10)
34108
+ nPinned>=pGroup->mxPinned
34109
+ || nPinned>=(int)pCache->mxPinned
3399434110
|| pcache1UnderMemoryPressure(pCache)
3399534111
)){
3399634112
goto fetch_out;
3399734113
}
3399834114
3399934115
if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
3400034116
goto fetch_out;
3400134117
}
3400234118
34003
- /* Step 4. Try to recycle a page buffer if appropriate. */
34004
- if( pCache->bPurgeable && pcache1.pLruTail && (
34119
+ /* Step 4. Try to recycle a page. */
34120
+ if( pCache->bPurgeable && pGroup->pLruTail && (
3400534121
(pCache->nPage+1>=pCache->nMax)
34006
- || pcache1.nCurrentPage>=pcache1.nMaxPage
34122
+ || pGroup->nCurrentPage>=pGroup->nMaxPage
3400734123
|| pcache1UnderMemoryPressure(pCache)
3400834124
)){
34009
- pPage = pcache1.pLruTail;
34125
+ PCache1 *pOtherCache;
34126
+ pPage = pGroup->pLruTail;
3401034127
pcache1RemoveFromHash(pPage);
3401134128
pcache1PinPage(pPage);
34012
- if( pPage->pCache->szPage!=pCache->szPage ){
34129
+ if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
3401334130
pcache1FreePage(pPage);
3401434131
pPage = 0;
3401534132
}else{
34016
- pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable);
34133
+ pGroup->nCurrentPage -=
34134
+ (pOtherCache->bPurgeable - pCache->bPurgeable);
3401734135
}
3401834136
}
3401934137
3402034138
/* Step 5. If a usable page buffer has still not been found,
3402134139
** attempt to allocate a new one.
3402234140
*/
3402334141
if( !pPage ){
34142
+ if( createFlag==1 ) sqlite3BeginBenignMalloc();
34143
+ pcache1LeaveMutex(pGroup);
3402434144
pPage = pcache1AllocPage(pCache);
34145
+ pcache1EnterMutex(pGroup);
34146
+ if( createFlag==1 ) sqlite3EndBenignMalloc();
3402534147
}
3402634148
3402734149
if( pPage ){
3402834150
unsigned int h = iKey % pCache->nHash;
3402934151
pCache->nPage++;
@@ -34038,12 +34160,11 @@
3403834160
3403934161
fetch_out:
3404034162
if( pPage && iKey>pCache->iMaxKey ){
3404134163
pCache->iMaxKey = iKey;
3404234164
}
34043
- if( createFlag==1 ) sqlite3EndBenignMalloc();
34044
- pcache1LeaveMutex();
34165
+ pcache1LeaveMutex(pGroup);
3404534166
return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
3404634167
}
3404734168
3404834169
3404934170
/*
@@ -34052,37 +34173,38 @@
3405234173
** Mark a page as unpinned (eligible for asynchronous recycling).
3405334174
*/
3405434175
static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
3405534176
PCache1 *pCache = (PCache1 *)p;
3405634177
PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
34178
+ PGroup *pGroup = pCache->pGroup;
3405734179
3405834180
assert( pPage->pCache==pCache );
34059
- pcache1EnterMutex();
34181
+ pcache1EnterMutex(pGroup);
3406034182
3406134183
/* It is an error to call this function if the page is already
34062
- ** part of the global LRU list.
34184
+ ** part of the PGroup LRU list.
3406334185
*/
3406434186
assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
34065
- assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage );
34187
+ assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
3406634188
34067
- if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){
34189
+ if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
3406834190
pcache1RemoveFromHash(pPage);
3406934191
pcache1FreePage(pPage);
3407034192
}else{
34071
- /* Add the page to the global LRU list. */
34072
- if( pcache1.pLruHead ){
34073
- pcache1.pLruHead->pLruPrev = pPage;
34074
- pPage->pLruNext = pcache1.pLruHead;
34075
- pcache1.pLruHead = pPage;
34193
+ /* Add the page to the PGroup LRU list. */
34194
+ if( pGroup->pLruHead ){
34195
+ pGroup->pLruHead->pLruPrev = pPage;
34196
+ pPage->pLruNext = pGroup->pLruHead;
34197
+ pGroup->pLruHead = pPage;
3407634198
}else{
34077
- pcache1.pLruTail = pPage;
34078
- pcache1.pLruHead = pPage;
34199
+ pGroup->pLruTail = pPage;
34200
+ pGroup->pLruHead = pPage;
3407934201
}
3408034202
pCache->nRecyclable++;
3408134203
}
3408234204
34083
- pcache1LeaveMutex();
34205
+ pcache1LeaveMutex(pCache->pGroup);
3408434206
}
3408534207
3408634208
/*
3408734209
** Implementation of the sqlite3_pcache.xRekey method.
3408834210
*/
@@ -34097,11 +34219,11 @@
3409734219
PgHdr1 **pp;
3409834220
unsigned int h;
3409934221
assert( pPage->iKey==iOld );
3410034222
assert( pPage->pCache==pCache );
3410134223
34102
- pcache1EnterMutex();
34224
+ pcache1EnterMutex(pCache->pGroup);
3410334225
3410434226
h = iOld%pCache->nHash;
3410534227
pp = &pCache->apHash[h];
3410634228
while( (*pp)!=pPage ){
3410734229
pp = &(*pp)->pNext;
@@ -34114,11 +34236,11 @@
3411434236
pCache->apHash[h] = pPage;
3411534237
if( iNew>pCache->iMaxKey ){
3411634238
pCache->iMaxKey = iNew;
3411734239
}
3411834240
34119
- pcache1LeaveMutex();
34241
+ pcache1LeaveMutex(pCache->pGroup);
3412034242
}
3412134243
3412234244
/*
3412334245
** Implementation of the sqlite3_pcache.xTruncate method.
3412434246
**
@@ -34126,32 +34248,34 @@
3412634248
** or greater than parameter iLimit. Any pinned pages with a page number
3412734249
** equal to or greater than iLimit are implicitly unpinned.
3412834250
*/
3412934251
static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
3413034252
PCache1 *pCache = (PCache1 *)p;
34131
- pcache1EnterMutex();
34253
+ pcache1EnterMutex(pCache->pGroup);
3413234254
if( iLimit<=pCache->iMaxKey ){
3413334255
pcache1TruncateUnsafe(pCache, iLimit);
3413434256
pCache->iMaxKey = iLimit-1;
3413534257
}
34136
- pcache1LeaveMutex();
34258
+ pcache1LeaveMutex(pCache->pGroup);
3413734259
}
3413834260
3413934261
/*
3414034262
** Implementation of the sqlite3_pcache.xDestroy method.
3414134263
**
3414234264
** Destroy a cache allocated using pcache1Create().
3414334265
*/
3414434266
static void pcache1Destroy(sqlite3_pcache *p){
3414534267
PCache1 *pCache = (PCache1 *)p;
34268
+ PGroup *pGroup = pCache->pGroup;
3414634269
assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
34147
- pcache1EnterMutex();
34270
+ pcache1EnterMutex(pGroup);
3414834271
pcache1TruncateUnsafe(pCache, 0);
34149
- pcache1.nMaxPage -= pCache->nMax;
34150
- pcache1.nMinPage -= pCache->nMin;
34151
- pcache1EnforceMaxPage();
34152
- pcache1LeaveMutex();
34272
+ pGroup->nMaxPage -= pCache->nMax;
34273
+ pGroup->nMinPage -= pCache->nMin;
34274
+ pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
34275
+ pcache1EnforceMaxPage(pGroup);
34276
+ pcache1LeaveMutex(pGroup);
3415334277
sqlite3_free(pCache->apHash);
3415434278
sqlite3_free(pCache);
3415534279
}
3415634280
3415734281
/*
@@ -34186,20 +34310,22 @@
3418634310
** been released, the function returns. The return value is the total number
3418734311
** of bytes of memory released.
3418834312
*/
3418934313
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
3419034314
int nFree = 0;
34315
+ assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
34316
+ assert( sqlite3_mutex_notheld(pcache1.mutex) );
3419134317
if( pcache1.pStart==0 ){
3419234318
PgHdr1 *p;
34193
- pcache1EnterMutex();
34194
- while( (nReq<0 || nFree<nReq) && ((p=pcache1.pLruTail)!=0) ){
34319
+ pcache1EnterMutex(&pcache1.grp);
34320
+ while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
3419534321
nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
3419634322
pcache1PinPage(p);
3419734323
pcache1RemoveFromHash(p);
3419834324
pcache1FreePage(p);
3419934325
}
34200
- pcache1LeaveMutex();
34326
+ pcache1LeaveMutex(&pcache1.grp);
3420134327
}
3420234328
return nFree;
3420334329
}
3420434330
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
3420534331
@@ -34214,16 +34340,16 @@
3421434340
int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
3421534341
int *pnRecyclable /* OUT: Total number of pages available for recycling */
3421634342
){
3421734343
PgHdr1 *p;
3421834344
int nRecyclable = 0;
34219
- for(p=pcache1.pLruHead; p; p=p->pLruNext){
34345
+ for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
3422034346
nRecyclable++;
3422134347
}
34222
- *pnCurrent = pcache1.nCurrentPage;
34223
- *pnMax = pcache1.nMaxPage;
34224
- *pnMin = pcache1.nMinPage;
34348
+ *pnCurrent = pcache1.grp.nCurrentPage;
34349
+ *pnMax = pcache1.grp.nMaxPage;
34350
+ *pnMin = pcache1.grp.nMinPage;
3422534351
*pnRecyclable = nRecyclable;
3422634352
}
3422734353
#endif
3422834354
3422934355
/************** End of pcache1.c *********************************************/
@@ -40390,19 +40516,17 @@
4039040516
**
4039140517
** If successful, or if called on a pager for which it is a no-op, this
4039240518
** function returns SQLITE_OK. Otherwise, an IO error code is returned.
4039340519
*/
4039440520
SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
40395
- int rc; /* Return code */
40396
- assert( !MEMDB );
40397
- if( pPager->noSync ){
40398
- rc = SQLITE_OK;
40399
- }else{
40521
+ int rc = SQLITE_OK;
40522
+ if( !pPager->noSync ){
40523
+ assert( !MEMDB );
4040040524
rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
40401
- }
40402
- if( isOpen(pPager->fd) ){
40403
- sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, (void *)&rc);
40525
+ }else if( isOpen(pPager->fd) ){
40526
+ assert( !MEMDB );
40527
+ sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
4040440528
}
4040540529
return rc;
4040640530
}
4040740531
4040840532
/*
@@ -46264,18 +46388,13 @@
4626446388
testcase( nPayload==pPage->maxLocal+1 );
4626546389
if( likely(nPayload<=pPage->maxLocal) ){
4626646390
/* This is the (easy) common case where the entire payload fits
4626746391
** on the local page. No overflow is required.
4626846392
*/
46269
- int nSize; /* Total size of cell content in bytes */
46270
- nSize = nPayload + n;
46393
+ if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
4627146394
pInfo->nLocal = (u16)nPayload;
4627246395
pInfo->iOverflow = 0;
46273
- if( (nSize & ~3)==0 ){
46274
- nSize = 4; /* Minimum cell size is 4 */
46275
- }
46276
- pInfo->nSize = (u16)nSize;
4627746396
}else{
4627846397
/* If the payload will not fit completely on the local page, we have
4627946398
** to decide how much to store locally and how much to spill onto
4628046399
** overflow pages. The strategy is to minimize the amount of unused
4628146400
** space on overflow pages while keeping the amount of local storage
@@ -53936,10 +54055,20 @@
5393654055
assert( pFile );
5393754056
assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
5393854057
nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
5393954058
&& iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
5394054059
));
54060
+
54061
+ /* This call ensures that all data required to recreate the original
54062
+ ** database has been stored in the journal for pDestPager and the
54063
+ ** journal synced to disk. So at this point we may safely modify
54064
+ ** the database file in any way, knowing that if a power failure
54065
+ ** occurs, the original database will be reconstructed from the
54066
+ ** journal file. */
54067
+ rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
54068
+
54069
+ /* Write the extra pages and truncate the database file as required. */
5394154070
iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
5394254071
for(
5394354072
iOff=PENDING_BYTE+pgszSrc;
5394454073
rc==SQLITE_OK && iOff<iEnd;
5394554074
iOff+=pgszSrc
@@ -53952,14 +54081,16 @@
5395254081
rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
5395354082
}
5395454083
sqlite3PagerUnref(pSrcPg);
5395554084
}
5395654085
if( rc==SQLITE_OK ){
53957
- rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
53958
- if( rc==SQLITE_OK ){
53959
- rc = backupTruncateFile(pFile, iSize);
53960
- }
54086
+ rc = backupTruncateFile(pFile, iSize);
54087
+ }
54088
+
54089
+ /* Sync the database file to disk. */
54090
+ if( rc==SQLITE_OK ){
54091
+ rc = sqlite3PagerSync(pDestPager);
5396154092
}
5396254093
}else{
5396354094
rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
5396454095
}
5396554096
@@ -58815,15 +58946,34 @@
5881558946
int rc;
5881658947
5881758948
assert(p);
5881858949
if( p->magic!=VDBE_MAGIC_RUN ){
5881958950
/* We used to require that sqlite3_reset() be called before retrying
58820
- ** sqlite3_step() after any error. But after 3.6.23, we changed this
58821
- ** so that sqlite3_reset() would be called automatically instead of
58822
- ** throwing the error.
58951
+ ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
58952
+ ** with version 3.7.0, we changed this so that sqlite3_reset() would
58953
+ ** be called automatically instead of throwing the SQLITE_MISUSE error.
58954
+ ** This "automatic-reset" change is not technically an incompatibility,
58955
+ ** since any application that receives an SQLITE_MISUSE is broken by
58956
+ ** definition.
58957
+ **
58958
+ ** Nevertheless, some published applications that were originally written
58959
+ ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
58960
+ ** returns, and the so were broken by the automatic-reset change. As a
58961
+ ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
58962
+ ** legacy behavior of returning SQLITE_MISUSE for cases where the
58963
+ ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
58964
+ ** or SQLITE_BUSY error.
5882358965
*/
58966
+#ifdef SQLITE_OMIT_AUTORESET
58967
+ if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
58968
+ sqlite3_reset((sqlite3_stmt*)p);
58969
+ }else{
58970
+ return SQLITE_MISUSE_BKPT;
58971
+ }
58972
+#else
5882458973
sqlite3_reset((sqlite3_stmt*)p);
58974
+#endif
5882558975
}
5882658976
5882758977
/* Check that malloc() has not failed. If it has, return early. */
5882858978
db = p->db;
5882958979
if( db->mallocFailed ){
@@ -58861,11 +59011,13 @@
5886159011
if( p->explain ){
5886259012
rc = sqlite3VdbeList(p);
5886359013
}else
5886459014
#endif /* SQLITE_OMIT_EXPLAIN */
5886559015
{
59016
+ db->vdbeExecCnt++;
5886659017
rc = sqlite3VdbeExec(p);
59018
+ db->vdbeExecCnt--;
5886759019
}
5886859020
5886959021
#ifndef SQLITE_OMIT_TRACE
5887059022
/* Invoke the profile callback if there is one
5887159023
*/
@@ -59818,13 +59970,16 @@
5981859970
}
5981959971
return nTotal;
5982059972
}
5982159973
5982259974
/*
59823
-** Return a pointer to a string in memory obtained form sqlite3DbMalloc() which
59824
-** holds a copy of zRawSql but with host parameters expanded to their
59825
-** current bindings.
59975
+** This function returns a pointer to a nul-terminated string in memory
59976
+** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
59977
+** string contains a copy of zRawSql but with host parameters expanded to
59978
+** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
59979
+** then the returned string holds a copy of zRawSql with "-- " prepended
59980
+** to each line of text.
5982659981
**
5982759982
** The calling function is responsible for making sure the memory returned
5982859983
** is eventually freed.
5982959984
**
5983059985
** ALGORITHM: Scan the input string looking for host parameters in any of
@@ -59851,67 +60006,76 @@
5985160006
5985260007
db = p->db;
5985360008
sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
5985460009
db->aLimit[SQLITE_LIMIT_LENGTH]);
5985560010
out.db = db;
59856
- while( zRawSql[0] ){
59857
- n = findNextHostParameter(zRawSql, &nToken);
59858
- assert( n>0 );
59859
- sqlite3StrAccumAppend(&out, zRawSql, n);
59860
- zRawSql += n;
59861
- assert( zRawSql[0] || nToken==0 );
59862
- if( nToken==0 ) break;
59863
- if( zRawSql[0]=='?' ){
59864
- if( nToken>1 ){
59865
- assert( sqlite3Isdigit(zRawSql[1]) );
59866
- sqlite3GetInt32(&zRawSql[1], &idx);
59867
- }else{
59868
- idx = nextIndex;
59869
- }
59870
- }else{
59871
- assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
59872
- testcase( zRawSql[0]==':' );
59873
- testcase( zRawSql[0]=='$' );
59874
- testcase( zRawSql[0]=='@' );
59875
- idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
59876
- assert( idx>0 );
59877
- }
59878
- zRawSql += nToken;
59879
- nextIndex = idx + 1;
59880
- assert( idx>0 && idx<=p->nVar );
59881
- pVar = &p->aVar[idx-1];
59882
- if( pVar->flags & MEM_Null ){
59883
- sqlite3StrAccumAppend(&out, "NULL", 4);
59884
- }else if( pVar->flags & MEM_Int ){
59885
- sqlite3XPrintf(&out, "%lld", pVar->u.i);
59886
- }else if( pVar->flags & MEM_Real ){
59887
- sqlite3XPrintf(&out, "%!.15g", pVar->r);
59888
- }else if( pVar->flags & MEM_Str ){
60011
+ if( db->vdbeExecCnt>1 ){
60012
+ while( *zRawSql ){
60013
+ const char *zStart = zRawSql;
60014
+ while( *(zRawSql++)!='\n' && *zRawSql );
60015
+ sqlite3StrAccumAppend(&out, "-- ", 3);
60016
+ sqlite3StrAccumAppend(&out, zStart, zRawSql-zStart);
60017
+ }
60018
+ }else{
60019
+ while( zRawSql[0] ){
60020
+ n = findNextHostParameter(zRawSql, &nToken);
60021
+ assert( n>0 );
60022
+ sqlite3StrAccumAppend(&out, zRawSql, n);
60023
+ zRawSql += n;
60024
+ assert( zRawSql[0] || nToken==0 );
60025
+ if( nToken==0 ) break;
60026
+ if( zRawSql[0]=='?' ){
60027
+ if( nToken>1 ){
60028
+ assert( sqlite3Isdigit(zRawSql[1]) );
60029
+ sqlite3GetInt32(&zRawSql[1], &idx);
60030
+ }else{
60031
+ idx = nextIndex;
60032
+ }
60033
+ }else{
60034
+ assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
60035
+ testcase( zRawSql[0]==':' );
60036
+ testcase( zRawSql[0]=='$' );
60037
+ testcase( zRawSql[0]=='@' );
60038
+ idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
60039
+ assert( idx>0 );
60040
+ }
60041
+ zRawSql += nToken;
60042
+ nextIndex = idx + 1;
60043
+ assert( idx>0 && idx<=p->nVar );
60044
+ pVar = &p->aVar[idx-1];
60045
+ if( pVar->flags & MEM_Null ){
60046
+ sqlite3StrAccumAppend(&out, "NULL", 4);
60047
+ }else if( pVar->flags & MEM_Int ){
60048
+ sqlite3XPrintf(&out, "%lld", pVar->u.i);
60049
+ }else if( pVar->flags & MEM_Real ){
60050
+ sqlite3XPrintf(&out, "%!.15g", pVar->r);
60051
+ }else if( pVar->flags & MEM_Str ){
5988960052
#ifndef SQLITE_OMIT_UTF16
59890
- u8 enc = ENC(db);
59891
- if( enc!=SQLITE_UTF8 ){
59892
- Mem utf8;
59893
- memset(&utf8, 0, sizeof(utf8));
59894
- utf8.db = db;
59895
- sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
59896
- sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
59897
- sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
59898
- sqlite3VdbeMemRelease(&utf8);
59899
- }else
60053
+ u8 enc = ENC(db);
60054
+ if( enc!=SQLITE_UTF8 ){
60055
+ Mem utf8;
60056
+ memset(&utf8, 0, sizeof(utf8));
60057
+ utf8.db = db;
60058
+ sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
60059
+ sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
60060
+ sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
60061
+ sqlite3VdbeMemRelease(&utf8);
60062
+ }else
5990060063
#endif
59901
- {
59902
- sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
59903
- }
59904
- }else if( pVar->flags & MEM_Zero ){
59905
- sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
59906
- }else{
59907
- assert( pVar->flags & MEM_Blob );
59908
- sqlite3StrAccumAppend(&out, "x'", 2);
59909
- for(i=0; i<pVar->n; i++){
59910
- sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
59911
- }
59912
- sqlite3StrAccumAppend(&out, "'", 1);
60064
+ {
60065
+ sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
60066
+ }
60067
+ }else if( pVar->flags & MEM_Zero ){
60068
+ sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
60069
+ }else{
60070
+ assert( pVar->flags & MEM_Blob );
60071
+ sqlite3StrAccumAppend(&out, "x'", 2);
60072
+ for(i=0; i<pVar->n; i++){
60073
+ sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
60074
+ }
60075
+ sqlite3StrAccumAppend(&out, "'", 1);
60076
+ }
5991360077
}
5991460078
}
5991560079
return sqlite3StrAccumFinish(&out);
5991660080
}
5991760081
@@ -73655,15 +73819,15 @@
7365573819
** out-of-bounds write if SQLITE_DEBUG is not defined). */
7365673820
return;
7365773821
}
7365873822
sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
7365973823
for(i=0; i<nCol; i++){
73660
- int addr = sqlite3VdbeCurrentAddr(v) - (nCol*2);
73824
+ int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
7366173825
if( i==0 ){
73662
- sqlite3VdbeJumpHere(v, addr-1); /* Set jump dest for the OP_IfNot */
73826
+ sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
7366373827
}
73664
- sqlite3VdbeJumpHere(v, addr); /* Set jump dest for the OP_Ne */
73828
+ sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
7366573829
sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
7366673830
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
7366773831
}
7366873832
7366973833
/* End of the analysis loop. */
@@ -87777,11 +87941,11 @@
8777787941
/*
8777887942
** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
8777987943
*/
8778087944
static int sqlite3Prepare16(
8778187945
sqlite3 *db, /* Database handle. */
87782
- const void *zSql, /* UTF-8 encoded SQL statement. */
87946
+ const void *zSql, /* UTF-16 encoded SQL statement. */
8778387947
int nBytes, /* Length of zSql in bytes. */
8778487948
int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
8778587949
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
8778687950
const void **pzTail /* OUT: End of parsed string */
8778787951
){
@@ -87827,11 +87991,11 @@
8782787991
** and the statement is automatically recompiled if an schema change
8782887992
** occurs.
8782987993
*/
8783087994
SQLITE_API int sqlite3_prepare16(
8783187995
sqlite3 *db, /* Database handle. */
87832
- const void *zSql, /* UTF-8 encoded SQL statement. */
87996
+ const void *zSql, /* UTF-16 encoded SQL statement. */
8783387997
int nBytes, /* Length of zSql in bytes. */
8783487998
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
8783587999
const void **pzTail /* OUT: End of parsed string */
8783688000
){
8783788001
int rc;
@@ -87839,11 +88003,11 @@
8783988003
assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
8784088004
return rc;
8784188005
}
8784288006
SQLITE_API int sqlite3_prepare16_v2(
8784388007
sqlite3 *db, /* Database handle. */
87844
- const void *zSql, /* UTF-8 encoded SQL statement. */
88008
+ const void *zSql, /* UTF-16 encoded SQL statement. */
8784588009
int nBytes, /* Length of zSql in bytes. */
8784688010
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
8784788011
const void **pzTail /* OUT: End of parsed string */
8784888012
){
8784988013
int rc;
8785088014
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -650,11 +650,11 @@
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.5"
654 #define SQLITE_VERSION_NUMBER 3007005
655 #define SQLITE_SOURCE_ID "2011-01-17 02:24:12 b93f6f3e679c7710f42580a8dd9ce43136376c1d"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -1262,24 +1262,28 @@
1262 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1263 ** to the [sqlite3_file] object associated with a particular database
1264 ** connection. See the [sqlite3_file_control()] documentation for
1265 ** additional information.
1266 **
1267 ** The [SQLITE_FCNTL_SYNC] opcode is used internally. SQLite calls
1268 ** the file-control method with this opcode immediately after the database
1269 ** file is synced, or if the database is running in synchronous=off mode
1270 ** immediately after it would have been synced otherwise. This makes it
1271 ** easier to write special VFS modules that depend on the xSync call.
 
 
 
 
1272 */
1273 #define SQLITE_FCNTL_LOCKSTATE 1
1274 #define SQLITE_GET_LOCKPROXYFILE 2
1275 #define SQLITE_SET_LOCKPROXYFILE 3
1276 #define SQLITE_LAST_ERRNO 4
1277 #define SQLITE_FCNTL_SIZE_HINT 5
1278 #define SQLITE_FCNTL_CHUNK_SIZE 6
1279 #define SQLITE_FCNTL_FILE_POINTER 7
1280 #define SQLITE_FCNTL_SYNC 8
1281
1282
1283 /*
1284 ** CAPI3REF: Mutex Handle
1285 **
@@ -2395,11 +2399,11 @@
2395 ** The strings returned by these two routines should be
2396 ** released by [sqlite3_free()]. ^Both routines return a
2397 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2398 ** memory to hold the resulting string.
2399 **
2400 ** ^(In sqlite3_snprintf() routine is similar to "snprintf()" from
2401 ** the standard C library. The result is written into the
2402 ** buffer supplied as the second parameter whose size is given by
2403 ** the first parameter. Note that the order of the
2404 ** first two parameters is reversed from snprintf().)^ This is an
2405 ** historical accident that cannot be fixed without breaking
@@ -3207,11 +3211,11 @@
3207
3208 /*
3209 ** CAPI3REF: Determine If An SQL Statement Writes The Database
3210 **
3211 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3212 ** and only if the [prepared statement] X is makes no direct changes to
3213 ** the content of the database file.
3214 **
3215 ** Note that [application-defined SQL functions] or
3216 ** [virtual tables] might change the database indirectly as a side effect.
3217 ** ^(For example, if an application defines a function "eval()" that
@@ -3624,17 +3628,21 @@
3624 ** already been [sqlite3_finalize | finalized] or on one that had
3625 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
3626 ** be the case that the same database connection is being used by two or
3627 ** more threads at the same moment in time.
3628 **
3629 ** For all versions of SQLite up to and including 3.6.23.1, it was required
3630 ** after sqlite3_step() returned anything other than [SQLITE_ROW] that
3631 ** [sqlite3_reset()] be called before any subsequent invocation of
3632 ** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
3633 ** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
3634 ** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
3635 ** automatically in this circumstance rather than returning [SQLITE_MISUSE].
 
 
 
 
3636 **
3637 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3638 ** API always returns a generic error code, [SQLITE_ERROR], following any
3639 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
3640 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
@@ -5808,11 +5816,12 @@
5808 #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
5809 #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
5810 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
5811 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
5812 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
5813 #define SQLITE_MUTEX_STATIC_LRU2 7 /* lru page list */
 
5814
5815 /*
5816 ** CAPI3REF: Retrieve the mutex for a database connection
5817 **
5818 ** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -9016,10 +9025,11 @@
9016 int nExtension; /* Number of loaded extensions */
9017 void **aExtension; /* Array of shared library handles */
9018 struct Vdbe *pVdbe; /* List of active virtual machines */
9019 int activeVdbeCnt; /* Number of VDBEs currently executing */
9020 int writeVdbeCnt; /* Number of active VDBEs that are writing */
 
9021 void (*xTrace)(void*,const char*); /* Trace function */
9022 void *pTraceArg; /* Argument to the trace function */
9023 void (*xProfile)(void*,const char*,u64); /* Profiling function */
9024 void *pProfileArg; /* Argument to profile function */
9025 void *pCommitArg; /* Argument to xCommitCallback() */
@@ -11787,10 +11797,13 @@
11787 #ifdef SQLITE_OMIT_AUTOINIT
11788 "OMIT_AUTOINIT",
11789 #endif
11790 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
11791 "OMIT_AUTOMATIC_INDEX",
 
 
 
11792 #endif
11793 #ifdef SQLITE_OMIT_AUTOVACUUM
11794 "OMIT_AUTOVACUUM",
11795 #endif
11796 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
@@ -19048,10 +19061,11 @@
19048 N = p->nAlloc - p->nChar - 1;
19049 if( N<=0 ){
19050 return;
19051 }
19052 }else{
 
19053 i64 szNew = p->nChar;
19054 szNew += N + 1;
19055 if( szNew > p->mxAlloc ){
19056 sqlite3StrAccumReset(p);
19057 p->tooBig = 1;
@@ -19058,17 +19072,16 @@
19058 return;
19059 }else{
19060 p->nAlloc = (int)szNew;
19061 }
19062 if( p->useMalloc==1 ){
19063 zNew = sqlite3DbMallocRaw(p->db, p->nAlloc );
19064 }else{
19065 zNew = sqlite3_malloc(p->nAlloc);
19066 }
19067 if( zNew ){
19068 memcpy(zNew, p->zText, p->nChar);
19069 sqlite3StrAccumReset(p);
19070 p->zText = zNew;
19071 }else{
19072 p->mallocFailed = 1;
19073 sqlite3StrAccumReset(p);
19074 return;
@@ -33400,10 +33413,42 @@
33400
33401
33402 typedef struct PCache1 PCache1;
33403 typedef struct PgHdr1 PgHdr1;
33404 typedef struct PgFreeslot PgFreeslot;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33405
33406 /* Each page cache is an instance of the following object. Every
33407 ** open database file (including each in-memory database and each
33408 ** temporary or transient database) has a single page cache which
33409 ** is an instance of this object.
@@ -33413,20 +33458,21 @@
33413 */
33414 struct PCache1 {
33415 /* Cache configuration parameters. Page size (szPage) and the purgeable
33416 ** flag (bPurgeable) are set when the cache is created. nMax may be
33417 ** modified at any time by a call to the pcache1CacheSize() method.
33418 ** The global mutex must be held when accessing nMax.
33419 */
 
33420 int szPage; /* Size of allocated pages in bytes */
33421 int bPurgeable; /* True if cache is purgeable */
33422 unsigned int nMin; /* Minimum number of pages reserved */
33423 unsigned int nMax; /* Configured "cache_size" value */
 
33424
33425 /* Hash table of all pages. The following variables may only be accessed
33426 ** when the accessor is holding the global mutex (see pcache1EnterMutex()
33427 ** and pcache1LeaveMutex()).
33428 */
33429 unsigned int nRecyclable; /* Number of pages in the LRU list */
33430 unsigned int nPage; /* Total number of pages in apHash */
33431 unsigned int nHash; /* Number of slots in apHash[] */
33432 PgHdr1 **apHash; /* Hash table for fast lookup by key */
@@ -33458,25 +33504,31 @@
33458
33459 /*
33460 ** Global data used by this cache.
33461 */
33462 static SQLITE_WSD struct PCacheGlobal {
33463 sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
33464
33465 int nMaxPage; /* Sum of nMaxPage for purgeable caches */
33466 int nMinPage; /* Sum of nMinPage for purgeable caches */
33467 int nCurrentPage; /* Number of purgeable pages allocated */
33468 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
33469
33470 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
33471 int szSlot; /* Size of each free slot */
33472 int nSlot; /* The number of pcache slots */
33473 int nFreeSlot; /* Number of unused pcache slots */
33474 int nReserve; /* Try to keep nFreeSlot above this */
33475 void *pStart, *pEnd; /* Bounds of pagecache malloc range */
33476 PgFreeslot *pFree; /* Free page blocks */
33477 int isInit; /* True if initialized */
 
 
 
 
 
 
33478 } pcache1_g;
33479
33480 /*
33481 ** All code in this file should access the global structure above via the
33482 ** alias "pcache1". This ensures that the WSD emulation is used when
@@ -33498,23 +33550,26 @@
33498 */
33499 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
33500 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
33501
33502 /*
33503 ** Macros to enter and leave the global LRU mutex.
33504 */
33505 #define pcache1EnterMutex() sqlite3_mutex_enter(pcache1.mutex)
33506 #define pcache1LeaveMutex() sqlite3_mutex_leave(pcache1.mutex)
33507
33508 /******************************************************************************/
33509 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
33510
33511 /*
33512 ** This function is called during initialization if a static buffer is
33513 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
33514 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
33515 ** enough to contain 'n' buffers of 'sz' bytes each.
 
 
 
33516 */
33517 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
33518 if( pcache1.isInit ){
33519 PgFreeslot *p;
33520 sz = ROUNDDOWN8(sz);
@@ -33521,10 +33576,11 @@
33521 pcache1.szSlot = sz;
33522 pcache1.nSlot = pcache1.nFreeSlot = n;
33523 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
33524 pcache1.pStart = pBuf;
33525 pcache1.pFree = 0;
 
33526 while( n-- ){
33527 p = (PgFreeslot*)pBuf;
33528 p->pNext = pcache1.pFree;
33529 pcache1.pFree = p;
33530 pBuf = (void*)&((char*)pBuf)[sz];
@@ -33536,33 +33592,35 @@
33536 /*
33537 ** Malloc function used within this file to allocate space from the buffer
33538 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
33539 ** such buffer exists or there is no space left in it, this function falls
33540 ** back to sqlite3Malloc().
 
 
 
33541 */
33542 static void *pcache1Alloc(int nByte){
33543 void *p;
33544 assert( sqlite3_mutex_held(pcache1.mutex) );
33545 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
33546 if( nByte<=pcache1.szSlot && pcache1.pFree ){
33547 assert( pcache1.isInit );
33548 p = (PgHdr1 *)pcache1.pFree;
33549 pcache1.pFree = pcache1.pFree->pNext;
33550 pcache1.nFreeSlot--;
33551 assert( pcache1.nFreeSlot>=0 );
33552 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
33553 }else{
33554
33555 /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
33556 ** global pcache mutex and unlock the pager-cache object pCache. This is
33557 ** so that if the attempt to allocate a new buffer causes the the
33558 ** configured soft-heap-limit to be breached, it will be possible to
33559 ** reclaim memory from this pager-cache.
 
33560 */
33561 pcache1LeaveMutex();
33562 p = sqlite3Malloc(nByte);
33563 pcache1EnterMutex();
33564 if( p ){
33565 int sz = sqlite3MallocSize(p);
33566 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
33567 }
33568 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
@@ -33572,20 +33630,22 @@
33572
33573 /*
33574 ** Free an allocated buffer obtained from pcache1Alloc().
33575 */
33576 static void pcache1Free(void *p){
33577 assert( sqlite3_mutex_held(pcache1.mutex) );
33578 if( p==0 ) return;
33579 if( p>=pcache1.pStart && p<pcache1.pEnd ){
33580 PgFreeslot *pSlot;
 
33581 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
33582 pSlot = (PgFreeslot*)p;
33583 pSlot->pNext = pcache1.pFree;
33584 pcache1.pFree = pSlot;
33585 pcache1.nFreeSlot++;
 
33586 assert( pcache1.nFreeSlot<=pcache1.nSlot );
 
33587 }else{
33588 int iSize;
33589 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
33590 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
33591 iSize = sqlite3MallocSize(p);
@@ -33597,11 +33657,10 @@
33597 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
33598 /*
33599 ** Return the size of a pcache allocation
33600 */
33601 static int pcache1MemSize(void *p){
33602 assert( sqlite3_mutex_held(pcache1.mutex) );
33603 if( p>=pcache1.pStart && p<pcache1.pEnd ){
33604 return pcache1.szSlot;
33605 }else{
33606 int iSize;
33607 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
@@ -33621,11 +33680,11 @@
33621 void *pPg = pcache1Alloc(nByte);
33622 PgHdr1 *p;
33623 if( pPg ){
33624 p = PAGE_TO_PGHDR1(pCache, pPg);
33625 if( pCache->bPurgeable ){
33626 pcache1.nCurrentPage++;
33627 }
33628 }else{
33629 p = 0;
33630 }
33631 return p;
@@ -33638,12 +33697,13 @@
33638 ** that the current implementation happens to never call this routine
33639 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
33640 */
33641 static void pcache1FreePage(PgHdr1 *p){
33642 if( ALWAYS(p) ){
33643 if( p->pCache->bPurgeable ){
33644 pcache1.nCurrentPage--;
 
33645 }
33646 pcache1Free(PGHDR1_TO_PAGE(p));
33647 }
33648 }
33649
@@ -33651,24 +33711,18 @@
33651 ** Malloc function used by SQLite to obtain space from the buffer configured
33652 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
33653 ** exists, this function falls back to sqlite3Malloc().
33654 */
33655 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
33656 void *p;
33657 pcache1EnterMutex();
33658 p = pcache1Alloc(sz);
33659 pcache1LeaveMutex();
33660 return p;
33661 }
33662
33663 /*
33664 ** Free an allocated buffer obtained from sqlite3PageMalloc().
33665 */
33666 SQLITE_PRIVATE void sqlite3PageFree(void *p){
33667 pcache1EnterMutex();
33668 pcache1Free(p);
33669 pcache1LeaveMutex();
33670 }
33671
33672
33673 /*
33674 ** Return true if it desirable to avoid allocating a new page cache
@@ -33685,13 +33739,12 @@
33685 ** under memory pressure, then again it is desirable to avoid
33686 ** allocating a new page cache entry in order to avoid stressing
33687 ** the heap even further.
33688 */
33689 static int pcache1UnderMemoryPressure(PCache1 *pCache){
33690 assert( sqlite3_mutex_held(pcache1.mutex) );
33691 if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
33692 return pcache1.nFreeSlot<pcache1.nReserve;
33693 }else{
33694 return sqlite3HeapNearlyFull();
33695 }
33696 }
33697
@@ -33700,29 +33753,29 @@
33700
33701 /*
33702 ** This function is used to resize the hash table used by the cache passed
33703 ** as the first argument.
33704 **
33705 ** The global mutex must be held when this function is called.
33706 */
33707 static int pcache1ResizeHash(PCache1 *p){
33708 PgHdr1 **apNew;
33709 unsigned int nNew;
33710 unsigned int i;
33711
33712 assert( sqlite3_mutex_held(pcache1.mutex) );
33713
33714 nNew = p->nHash*2;
33715 if( nNew<256 ){
33716 nNew = 256;
33717 }
33718
33719 pcache1LeaveMutex();
33720 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
33721 apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
33722 if( p->nHash ){ sqlite3EndBenignMalloc(); }
33723 pcache1EnterMutex();
33724 if( apNew ){
33725 memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
33726 for(i=0; i<p->nHash; i++){
33727 PgHdr1 *pPage;
33728 PgHdr1 *pNext = p->apHash[i];
@@ -33741,29 +33794,37 @@
33741 return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
33742 }
33743
33744 /*
33745 ** This function is used internally to remove the page pPage from the
33746 ** global LRU list, if is part of it. If pPage is not part of the global
33747 ** LRU list, then this function is a no-op.
33748 **
33749 ** The global mutex must be held when this function is called.
 
 
33750 */
33751 static void pcache1PinPage(PgHdr1 *pPage){
33752 assert( sqlite3_mutex_held(pcache1.mutex) );
33753 if( pPage && (pPage->pLruNext || pPage==pcache1.pLruTail) ){
 
 
 
 
 
 
33754 if( pPage->pLruPrev ){
33755 pPage->pLruPrev->pLruNext = pPage->pLruNext;
33756 }
33757 if( pPage->pLruNext ){
33758 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
33759 }
33760 if( pcache1.pLruHead==pPage ){
33761 pcache1.pLruHead = pPage->pLruNext;
33762 }
33763 if( pcache1.pLruTail==pPage ){
33764 pcache1.pLruTail = pPage->pLruPrev;
33765 }
33766 pPage->pLruNext = 0;
33767 pPage->pLruPrev = 0;
33768 pPage->pCache->nRecyclable--;
33769 }
@@ -33772,32 +33833,34 @@
33772
33773 /*
33774 ** Remove the page supplied as an argument from the hash table
33775 ** (PCache1.apHash structure) that it is currently stored in.
33776 **
33777 ** The global mutex must be held when this function is called.
33778 */
33779 static void pcache1RemoveFromHash(PgHdr1 *pPage){
33780 unsigned int h;
33781 PCache1 *pCache = pPage->pCache;
33782 PgHdr1 **pp;
33783
 
33784 h = pPage->iKey % pCache->nHash;
33785 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
33786 *pp = (*pp)->pNext;
33787
33788 pCache->nPage--;
33789 }
33790
33791 /*
33792 ** If there are currently more than pcache.nMaxPage pages allocated, try
33793 ** to recycle pages to reduce the number allocated to pcache.nMaxPage.
33794 */
33795 static void pcache1EnforceMaxPage(void){
33796 assert( sqlite3_mutex_held(pcache1.mutex) );
33797 while( pcache1.nCurrentPage>pcache1.nMaxPage && pcache1.pLruTail ){
33798 PgHdr1 *p = pcache1.pLruTail;
 
33799 pcache1PinPage(p);
33800 pcache1RemoveFromHash(p);
33801 pcache1FreePage(p);
33802 }
33803 }
@@ -33805,19 +33868,19 @@
33805 /*
33806 ** Discard all pages from cache pCache with a page number (key value)
33807 ** greater than or equal to iLimit. Any pinned pages that meet this
33808 ** criteria are unpinned before they are discarded.
33809 **
33810 ** The global mutex must be held when this function is called.
33811 */
33812 static void pcache1TruncateUnsafe(
33813 PCache1 *pCache,
33814 unsigned int iLimit
33815 ){
33816 TESTONLY( unsigned int nPage = 0; ) /* Used to assert pCache->nPage is correct */
33817 unsigned int h;
33818 assert( sqlite3_mutex_held(pcache1.mutex) );
33819 for(h=0; h<pCache->nHash; h++){
33820 PgHdr1 **pp = &pCache->apHash[h];
33821 PgHdr1 *pPage;
33822 while( (pPage = *pp)!=0 ){
33823 if( pPage->iKey>=iLimit ){
@@ -33843,12 +33906,14 @@
33843 static int pcache1Init(void *NotUsed){
33844 UNUSED_PARAMETER(NotUsed);
33845 assert( pcache1.isInit==0 );
33846 memset(&pcache1, 0, sizeof(pcache1));
33847 if( sqlite3GlobalConfig.bCoreMutex ){
33848 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
 
33849 }
 
33850 pcache1.isInit = 1;
33851 return SQLITE_OK;
33852 }
33853
33854 /*
@@ -33866,22 +33931,51 @@
33866 ** Implementation of the sqlite3_pcache.xCreate method.
33867 **
33868 ** Allocate a new cache.
33869 */
33870 static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
33871 PCache1 *pCache;
 
 
33872
33873 pCache = (PCache1 *)sqlite3_malloc(sizeof(PCache1));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33874 if( pCache ){
33875 memset(pCache, 0, sizeof(PCache1));
 
 
 
 
 
 
 
33876 pCache->szPage = szPage;
33877 pCache->bPurgeable = (bPurgeable ? 1 : 0);
33878 if( bPurgeable ){
33879 pCache->nMin = 10;
33880 pcache1EnterMutex();
33881 pcache1.nMinPage += pCache->nMin;
33882 pcache1LeaveMutex();
 
33883 }
33884 }
33885 return (sqlite3_pcache *)pCache;
33886 }
33887
@@ -33891,26 +33985,30 @@
33891 ** Configure the cache_size limit for a cache.
33892 */
33893 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
33894 PCache1 *pCache = (PCache1 *)p;
33895 if( pCache->bPurgeable ){
33896 pcache1EnterMutex();
33897 pcache1.nMaxPage += (nMax - pCache->nMax);
 
 
33898 pCache->nMax = nMax;
33899 pcache1EnforceMaxPage();
33900 pcache1LeaveMutex();
 
33901 }
33902 }
33903
33904 /*
33905 ** Implementation of the sqlite3_pcache.xPagecount method.
33906 */
33907 static int pcache1Pagecount(sqlite3_pcache *p){
33908 int n;
33909 pcache1EnterMutex();
33910 n = ((PCache1 *)p)->nPage;
33911 pcache1LeaveMutex();
 
33912 return n;
33913 }
33914
33915 /*
33916 ** Implementation of the sqlite3_pcache.xFetch method.
@@ -33965,65 +34063,89 @@
33965 ** proceed to step 5.
33966 **
33967 ** 5. Otherwise, allocate and return a new page buffer.
33968 */
33969 static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
33970 unsigned int nPinned;
33971 PCache1 *pCache = (PCache1 *)p;
 
33972 PgHdr1 *pPage = 0;
33973
33974 assert( pCache->bPurgeable || createFlag!=1 );
33975 pcache1EnterMutex();
33976 if( createFlag==1 ) sqlite3BeginBenignMalloc();
 
 
33977
33978 /* Search the hash table for an existing entry. */
33979 if( pCache->nHash>0 ){
33980 unsigned int h = iKey % pCache->nHash;
33981 for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
33982 }
33983
 
33984 if( pPage || createFlag==0 ){
33985 pcache1PinPage(pPage);
33986 goto fetch_out;
33987 }
33988
33989 /* Step 3 of header comment. */
 
 
 
 
 
 
 
 
 
 
 
 
33990 nPinned = pCache->nPage - pCache->nRecyclable;
 
 
33991 if( createFlag==1 && (
33992 nPinned>=(pcache1.nMaxPage+pCache->nMin-pcache1.nMinPage)
33993 || nPinned>=(pCache->nMax * 9 / 10)
33994 || pcache1UnderMemoryPressure(pCache)
33995 )){
33996 goto fetch_out;
33997 }
33998
33999 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
34000 goto fetch_out;
34001 }
34002
34003 /* Step 4. Try to recycle a page buffer if appropriate. */
34004 if( pCache->bPurgeable && pcache1.pLruTail && (
34005 (pCache->nPage+1>=pCache->nMax)
34006 || pcache1.nCurrentPage>=pcache1.nMaxPage
34007 || pcache1UnderMemoryPressure(pCache)
34008 )){
34009 pPage = pcache1.pLruTail;
 
34010 pcache1RemoveFromHash(pPage);
34011 pcache1PinPage(pPage);
34012 if( pPage->pCache->szPage!=pCache->szPage ){
34013 pcache1FreePage(pPage);
34014 pPage = 0;
34015 }else{
34016 pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable);
 
34017 }
34018 }
34019
34020 /* Step 5. If a usable page buffer has still not been found,
34021 ** attempt to allocate a new one.
34022 */
34023 if( !pPage ){
 
 
34024 pPage = pcache1AllocPage(pCache);
 
 
34025 }
34026
34027 if( pPage ){
34028 unsigned int h = iKey % pCache->nHash;
34029 pCache->nPage++;
@@ -34038,12 +34160,11 @@
34038
34039 fetch_out:
34040 if( pPage && iKey>pCache->iMaxKey ){
34041 pCache->iMaxKey = iKey;
34042 }
34043 if( createFlag==1 ) sqlite3EndBenignMalloc();
34044 pcache1LeaveMutex();
34045 return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
34046 }
34047
34048
34049 /*
@@ -34052,37 +34173,38 @@
34052 ** Mark a page as unpinned (eligible for asynchronous recycling).
34053 */
34054 static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
34055 PCache1 *pCache = (PCache1 *)p;
34056 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
 
34057
34058 assert( pPage->pCache==pCache );
34059 pcache1EnterMutex();
34060
34061 /* It is an error to call this function if the page is already
34062 ** part of the global LRU list.
34063 */
34064 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
34065 assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage );
34066
34067 if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){
34068 pcache1RemoveFromHash(pPage);
34069 pcache1FreePage(pPage);
34070 }else{
34071 /* Add the page to the global LRU list. */
34072 if( pcache1.pLruHead ){
34073 pcache1.pLruHead->pLruPrev = pPage;
34074 pPage->pLruNext = pcache1.pLruHead;
34075 pcache1.pLruHead = pPage;
34076 }else{
34077 pcache1.pLruTail = pPage;
34078 pcache1.pLruHead = pPage;
34079 }
34080 pCache->nRecyclable++;
34081 }
34082
34083 pcache1LeaveMutex();
34084 }
34085
34086 /*
34087 ** Implementation of the sqlite3_pcache.xRekey method.
34088 */
@@ -34097,11 +34219,11 @@
34097 PgHdr1 **pp;
34098 unsigned int h;
34099 assert( pPage->iKey==iOld );
34100 assert( pPage->pCache==pCache );
34101
34102 pcache1EnterMutex();
34103
34104 h = iOld%pCache->nHash;
34105 pp = &pCache->apHash[h];
34106 while( (*pp)!=pPage ){
34107 pp = &(*pp)->pNext;
@@ -34114,11 +34236,11 @@
34114 pCache->apHash[h] = pPage;
34115 if( iNew>pCache->iMaxKey ){
34116 pCache->iMaxKey = iNew;
34117 }
34118
34119 pcache1LeaveMutex();
34120 }
34121
34122 /*
34123 ** Implementation of the sqlite3_pcache.xTruncate method.
34124 **
@@ -34126,32 +34248,34 @@
34126 ** or greater than parameter iLimit. Any pinned pages with a page number
34127 ** equal to or greater than iLimit are implicitly unpinned.
34128 */
34129 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
34130 PCache1 *pCache = (PCache1 *)p;
34131 pcache1EnterMutex();
34132 if( iLimit<=pCache->iMaxKey ){
34133 pcache1TruncateUnsafe(pCache, iLimit);
34134 pCache->iMaxKey = iLimit-1;
34135 }
34136 pcache1LeaveMutex();
34137 }
34138
34139 /*
34140 ** Implementation of the sqlite3_pcache.xDestroy method.
34141 **
34142 ** Destroy a cache allocated using pcache1Create().
34143 */
34144 static void pcache1Destroy(sqlite3_pcache *p){
34145 PCache1 *pCache = (PCache1 *)p;
 
34146 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
34147 pcache1EnterMutex();
34148 pcache1TruncateUnsafe(pCache, 0);
34149 pcache1.nMaxPage -= pCache->nMax;
34150 pcache1.nMinPage -= pCache->nMin;
34151 pcache1EnforceMaxPage();
34152 pcache1LeaveMutex();
 
34153 sqlite3_free(pCache->apHash);
34154 sqlite3_free(pCache);
34155 }
34156
34157 /*
@@ -34186,20 +34310,22 @@
34186 ** been released, the function returns. The return value is the total number
34187 ** of bytes of memory released.
34188 */
34189 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
34190 int nFree = 0;
 
 
34191 if( pcache1.pStart==0 ){
34192 PgHdr1 *p;
34193 pcache1EnterMutex();
34194 while( (nReq<0 || nFree<nReq) && ((p=pcache1.pLruTail)!=0) ){
34195 nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
34196 pcache1PinPage(p);
34197 pcache1RemoveFromHash(p);
34198 pcache1FreePage(p);
34199 }
34200 pcache1LeaveMutex();
34201 }
34202 return nFree;
34203 }
34204 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
34205
@@ -34214,16 +34340,16 @@
34214 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
34215 int *pnRecyclable /* OUT: Total number of pages available for recycling */
34216 ){
34217 PgHdr1 *p;
34218 int nRecyclable = 0;
34219 for(p=pcache1.pLruHead; p; p=p->pLruNext){
34220 nRecyclable++;
34221 }
34222 *pnCurrent = pcache1.nCurrentPage;
34223 *pnMax = pcache1.nMaxPage;
34224 *pnMin = pcache1.nMinPage;
34225 *pnRecyclable = nRecyclable;
34226 }
34227 #endif
34228
34229 /************** End of pcache1.c *********************************************/
@@ -40390,19 +40516,17 @@
40390 **
40391 ** If successful, or if called on a pager for which it is a no-op, this
40392 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
40393 */
40394 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
40395 int rc; /* Return code */
40396 assert( !MEMDB );
40397 if( pPager->noSync ){
40398 rc = SQLITE_OK;
40399 }else{
40400 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
40401 }
40402 if( isOpen(pPager->fd) ){
40403 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, (void *)&rc);
40404 }
40405 return rc;
40406 }
40407
40408 /*
@@ -46264,18 +46388,13 @@
46264 testcase( nPayload==pPage->maxLocal+1 );
46265 if( likely(nPayload<=pPage->maxLocal) ){
46266 /* This is the (easy) common case where the entire payload fits
46267 ** on the local page. No overflow is required.
46268 */
46269 int nSize; /* Total size of cell content in bytes */
46270 nSize = nPayload + n;
46271 pInfo->nLocal = (u16)nPayload;
46272 pInfo->iOverflow = 0;
46273 if( (nSize & ~3)==0 ){
46274 nSize = 4; /* Minimum cell size is 4 */
46275 }
46276 pInfo->nSize = (u16)nSize;
46277 }else{
46278 /* If the payload will not fit completely on the local page, we have
46279 ** to decide how much to store locally and how much to spill onto
46280 ** overflow pages. The strategy is to minimize the amount of unused
46281 ** space on overflow pages while keeping the amount of local storage
@@ -53936,10 +54055,20 @@
53936 assert( pFile );
53937 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
53938 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
53939 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
53940 ));
 
 
 
 
 
 
 
 
 
 
53941 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
53942 for(
53943 iOff=PENDING_BYTE+pgszSrc;
53944 rc==SQLITE_OK && iOff<iEnd;
53945 iOff+=pgszSrc
@@ -53952,14 +54081,16 @@
53952 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
53953 }
53954 sqlite3PagerUnref(pSrcPg);
53955 }
53956 if( rc==SQLITE_OK ){
53957 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
53958 if( rc==SQLITE_OK ){
53959 rc = backupTruncateFile(pFile, iSize);
53960 }
 
 
53961 }
53962 }else{
53963 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
53964 }
53965
@@ -58815,15 +58946,34 @@
58815 int rc;
58816
58817 assert(p);
58818 if( p->magic!=VDBE_MAGIC_RUN ){
58819 /* We used to require that sqlite3_reset() be called before retrying
58820 ** sqlite3_step() after any error. But after 3.6.23, we changed this
58821 ** so that sqlite3_reset() would be called automatically instead of
58822 ** throwing the error.
 
 
 
 
 
 
 
 
 
 
 
58823 */
 
 
 
 
 
 
 
58824 sqlite3_reset((sqlite3_stmt*)p);
 
58825 }
58826
58827 /* Check that malloc() has not failed. If it has, return early. */
58828 db = p->db;
58829 if( db->mallocFailed ){
@@ -58861,11 +59011,13 @@
58861 if( p->explain ){
58862 rc = sqlite3VdbeList(p);
58863 }else
58864 #endif /* SQLITE_OMIT_EXPLAIN */
58865 {
 
58866 rc = sqlite3VdbeExec(p);
 
58867 }
58868
58869 #ifndef SQLITE_OMIT_TRACE
58870 /* Invoke the profile callback if there is one
58871 */
@@ -59818,13 +59970,16 @@
59818 }
59819 return nTotal;
59820 }
59821
59822 /*
59823 ** Return a pointer to a string in memory obtained form sqlite3DbMalloc() which
59824 ** holds a copy of zRawSql but with host parameters expanded to their
59825 ** current bindings.
 
 
 
59826 **
59827 ** The calling function is responsible for making sure the memory returned
59828 ** is eventually freed.
59829 **
59830 ** ALGORITHM: Scan the input string looking for host parameters in any of
@@ -59851,67 +60006,76 @@
59851
59852 db = p->db;
59853 sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
59854 db->aLimit[SQLITE_LIMIT_LENGTH]);
59855 out.db = db;
59856 while( zRawSql[0] ){
59857 n = findNextHostParameter(zRawSql, &nToken);
59858 assert( n>0 );
59859 sqlite3StrAccumAppend(&out, zRawSql, n);
59860 zRawSql += n;
59861 assert( zRawSql[0] || nToken==0 );
59862 if( nToken==0 ) break;
59863 if( zRawSql[0]=='?' ){
59864 if( nToken>1 ){
59865 assert( sqlite3Isdigit(zRawSql[1]) );
59866 sqlite3GetInt32(&zRawSql[1], &idx);
59867 }else{
59868 idx = nextIndex;
59869 }
59870 }else{
59871 assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
59872 testcase( zRawSql[0]==':' );
59873 testcase( zRawSql[0]=='$' );
59874 testcase( zRawSql[0]=='@' );
59875 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
59876 assert( idx>0 );
59877 }
59878 zRawSql += nToken;
59879 nextIndex = idx + 1;
59880 assert( idx>0 && idx<=p->nVar );
59881 pVar = &p->aVar[idx-1];
59882 if( pVar->flags & MEM_Null ){
59883 sqlite3StrAccumAppend(&out, "NULL", 4);
59884 }else if( pVar->flags & MEM_Int ){
59885 sqlite3XPrintf(&out, "%lld", pVar->u.i);
59886 }else if( pVar->flags & MEM_Real ){
59887 sqlite3XPrintf(&out, "%!.15g", pVar->r);
59888 }else if( pVar->flags & MEM_Str ){
 
 
 
 
 
 
 
 
59889 #ifndef SQLITE_OMIT_UTF16
59890 u8 enc = ENC(db);
59891 if( enc!=SQLITE_UTF8 ){
59892 Mem utf8;
59893 memset(&utf8, 0, sizeof(utf8));
59894 utf8.db = db;
59895 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
59896 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
59897 sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
59898 sqlite3VdbeMemRelease(&utf8);
59899 }else
59900 #endif
59901 {
59902 sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
59903 }
59904 }else if( pVar->flags & MEM_Zero ){
59905 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
59906 }else{
59907 assert( pVar->flags & MEM_Blob );
59908 sqlite3StrAccumAppend(&out, "x'", 2);
59909 for(i=0; i<pVar->n; i++){
59910 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
59911 }
59912 sqlite3StrAccumAppend(&out, "'", 1);
 
59913 }
59914 }
59915 return sqlite3StrAccumFinish(&out);
59916 }
59917
@@ -73655,15 +73819,15 @@
73655 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
73656 return;
73657 }
73658 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
73659 for(i=0; i<nCol; i++){
73660 int addr = sqlite3VdbeCurrentAddr(v) - (nCol*2);
73661 if( i==0 ){
73662 sqlite3VdbeJumpHere(v, addr-1); /* Set jump dest for the OP_IfNot */
73663 }
73664 sqlite3VdbeJumpHere(v, addr); /* Set jump dest for the OP_Ne */
73665 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
73666 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
73667 }
73668
73669 /* End of the analysis loop. */
@@ -87777,11 +87941,11 @@
87777 /*
87778 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
87779 */
87780 static int sqlite3Prepare16(
87781 sqlite3 *db, /* Database handle. */
87782 const void *zSql, /* UTF-8 encoded SQL statement. */
87783 int nBytes, /* Length of zSql in bytes. */
87784 int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
87785 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
87786 const void **pzTail /* OUT: End of parsed string */
87787 ){
@@ -87827,11 +87991,11 @@
87827 ** and the statement is automatically recompiled if an schema change
87828 ** occurs.
87829 */
87830 SQLITE_API int sqlite3_prepare16(
87831 sqlite3 *db, /* Database handle. */
87832 const void *zSql, /* UTF-8 encoded SQL statement. */
87833 int nBytes, /* Length of zSql in bytes. */
87834 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
87835 const void **pzTail /* OUT: End of parsed string */
87836 ){
87837 int rc;
@@ -87839,11 +88003,11 @@
87839 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
87840 return rc;
87841 }
87842 SQLITE_API int sqlite3_prepare16_v2(
87843 sqlite3 *db, /* Database handle. */
87844 const void *zSql, /* UTF-8 encoded SQL statement. */
87845 int nBytes, /* Length of zSql in bytes. */
87846 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
87847 const void **pzTail /* OUT: End of parsed string */
87848 ){
87849 int rc;
87850
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -650,11 +650,11 @@
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.5"
654 #define SQLITE_VERSION_NUMBER 3007005
655 #define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -1262,24 +1262,28 @@
1262 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1263 ** to the [sqlite3_file] object associated with a particular database
1264 ** connection. See the [sqlite3_file_control()] documentation for
1265 ** additional information.
1266 **
1267 ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
1268 ** SQLite and sent to all VFSes in place of a call to the xSync method
1269 ** when the database connection has [PRAGMA synchronous] set to OFF.)^
1270 ** Some specialized VFSes need this signal in order to operate correctly
1271 ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
1272 ** VFSes do not need this signal and should silently ignore this opcode.
1273 ** Applications should not call [sqlite3_file_control()] with this
1274 ** opcode as doing so may disrupt the operation of the specilized VFSes
1275 ** that do require it.
1276 */
1277 #define SQLITE_FCNTL_LOCKSTATE 1
1278 #define SQLITE_GET_LOCKPROXYFILE 2
1279 #define SQLITE_SET_LOCKPROXYFILE 3
1280 #define SQLITE_LAST_ERRNO 4
1281 #define SQLITE_FCNTL_SIZE_HINT 5
1282 #define SQLITE_FCNTL_CHUNK_SIZE 6
1283 #define SQLITE_FCNTL_FILE_POINTER 7
1284 #define SQLITE_FCNTL_SYNC_OMITTED 8
1285
1286
1287 /*
1288 ** CAPI3REF: Mutex Handle
1289 **
@@ -2395,11 +2399,11 @@
2399 ** The strings returned by these two routines should be
2400 ** released by [sqlite3_free()]. ^Both routines return a
2401 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2402 ** memory to hold the resulting string.
2403 **
2404 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2405 ** the standard C library. The result is written into the
2406 ** buffer supplied as the second parameter whose size is given by
2407 ** the first parameter. Note that the order of the
2408 ** first two parameters is reversed from snprintf().)^ This is an
2409 ** historical accident that cannot be fixed without breaking
@@ -3207,11 +3211,11 @@
3211
3212 /*
3213 ** CAPI3REF: Determine If An SQL Statement Writes The Database
3214 **
3215 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3216 ** and only if the [prepared statement] X makes no direct changes to
3217 ** the content of the database file.
3218 **
3219 ** Note that [application-defined SQL functions] or
3220 ** [virtual tables] might change the database indirectly as a side effect.
3221 ** ^(For example, if an application defines a function "eval()" that
@@ -3624,17 +3628,21 @@
3628 ** already been [sqlite3_finalize | finalized] or on one that had
3629 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
3630 ** be the case that the same database connection is being used by two or
3631 ** more threads at the same moment in time.
3632 **
3633 ** For all versions of SQLite up to and including 3.6.23.1, a call to
3634 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
3635 ** other than [SQLITE_ROW] before any subsequent invocation of
3636 ** sqlite3_step(). Failure to reset the prepared statement using
3637 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3638 ** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began
3639 ** calling [sqlite3_reset()] automatically in this circumstance rather
3640 ** than returning [SQLITE_MISUSE]. This is not considered a compatibility
3641 ** break because any application that ever receives an SQLITE_MISUSE error
3642 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
3643 ** can be used to restore the legacy behavior.
3644 **
3645 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3646 ** API always returns a generic error code, [SQLITE_ERROR], following any
3647 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
3648 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
@@ -5808,11 +5816,12 @@
5816 #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
5817 #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
5818 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
5819 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
5820 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
5821 #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
5822 #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
5823
5824 /*
5825 ** CAPI3REF: Retrieve the mutex for a database connection
5826 **
5827 ** ^This interface returns a pointer the [sqlite3_mutex] object that
@@ -9016,10 +9025,11 @@
9025 int nExtension; /* Number of loaded extensions */
9026 void **aExtension; /* Array of shared library handles */
9027 struct Vdbe *pVdbe; /* List of active virtual machines */
9028 int activeVdbeCnt; /* Number of VDBEs currently executing */
9029 int writeVdbeCnt; /* Number of active VDBEs that are writing */
9030 int vdbeExecCnt; /* Number of nested calls to VdbeExec() */
9031 void (*xTrace)(void*,const char*); /* Trace function */
9032 void *pTraceArg; /* Argument to the trace function */
9033 void (*xProfile)(void*,const char*,u64); /* Profiling function */
9034 void *pProfileArg; /* Argument to profile function */
9035 void *pCommitArg; /* Argument to xCommitCallback() */
@@ -11787,10 +11797,13 @@
11797 #ifdef SQLITE_OMIT_AUTOINIT
11798 "OMIT_AUTOINIT",
11799 #endif
11800 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
11801 "OMIT_AUTOMATIC_INDEX",
11802 #endif
11803 #ifdef SQLITE_OMIT_AUTORESET
11804 "OMIT_AUTORESET",
11805 #endif
11806 #ifdef SQLITE_OMIT_AUTOVACUUM
11807 "OMIT_AUTOVACUUM",
11808 #endif
11809 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
@@ -19048,10 +19061,11 @@
19061 N = p->nAlloc - p->nChar - 1;
19062 if( N<=0 ){
19063 return;
19064 }
19065 }else{
19066 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
19067 i64 szNew = p->nChar;
19068 szNew += N + 1;
19069 if( szNew > p->mxAlloc ){
19070 sqlite3StrAccumReset(p);
19071 p->tooBig = 1;
@@ -19058,17 +19072,16 @@
19072 return;
19073 }else{
19074 p->nAlloc = (int)szNew;
19075 }
19076 if( p->useMalloc==1 ){
19077 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
19078 }else{
19079 zNew = sqlite3_realloc(zOld, p->nAlloc);
19080 }
19081 if( zNew ){
19082 if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
 
19083 p->zText = zNew;
19084 }else{
19085 p->mallocFailed = 1;
19086 sqlite3StrAccumReset(p);
19087 return;
@@ -33400,10 +33413,42 @@
33413
33414
33415 typedef struct PCache1 PCache1;
33416 typedef struct PgHdr1 PgHdr1;
33417 typedef struct PgFreeslot PgFreeslot;
33418 typedef struct PGroup PGroup;
33419
33420 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
33421 ** of one or more PCaches that are able to recycle each others unpinned
33422 ** pages when they are under memory pressure. A PGroup is an instance of
33423 ** the following object.
33424 **
33425 ** This page cache implementation works in one of two modes:
33426 **
33427 ** (1) Every PCache is the sole member of its own PGroup. There is
33428 ** one PGroup per PCache.
33429 **
33430 ** (2) There is a single global PGroup that all PCaches are a member
33431 ** of.
33432 **
33433 ** Mode 1 uses more memory (since PCache instances are not able to rob
33434 ** unused pages from other PCaches) but it also operates without a mutex,
33435 ** and is therefore often faster. Mode 2 requires a mutex in order to be
33436 ** threadsafe, but is able recycle pages more efficient.
33437 **
33438 ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
33439 ** PGroup which is the pcache1.grp global variable and its mutex is
33440 ** SQLITE_MUTEX_STATIC_LRU.
33441 */
33442 struct PGroup {
33443 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
33444 int nMaxPage; /* Sum of nMax for purgeable caches */
33445 int nMinPage; /* Sum of nMin for purgeable caches */
33446 int mxPinned; /* nMaxpage + 10 - nMinPage */
33447 int nCurrentPage; /* Number of purgeable pages allocated */
33448 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
33449 };
33450
33451 /* Each page cache is an instance of the following object. Every
33452 ** open database file (including each in-memory database and each
33453 ** temporary or transient database) has a single page cache which
33454 ** is an instance of this object.
@@ -33413,20 +33458,21 @@
33458 */
33459 struct PCache1 {
33460 /* Cache configuration parameters. Page size (szPage) and the purgeable
33461 ** flag (bPurgeable) are set when the cache is created. nMax may be
33462 ** modified at any time by a call to the pcache1CacheSize() method.
33463 ** The PGroup mutex must be held when accessing nMax.
33464 */
33465 PGroup *pGroup; /* PGroup this cache belongs to */
33466 int szPage; /* Size of allocated pages in bytes */
33467 int bPurgeable; /* True if cache is purgeable */
33468 unsigned int nMin; /* Minimum number of pages reserved */
33469 unsigned int nMax; /* Configured "cache_size" value */
33470 unsigned int mxPinned; /* nMax*9/10 */
33471
33472 /* Hash table of all pages. The following variables may only be accessed
33473 ** when the accessor is holding the PGroup mutex.
 
33474 */
33475 unsigned int nRecyclable; /* Number of pages in the LRU list */
33476 unsigned int nPage; /* Total number of pages in apHash */
33477 unsigned int nHash; /* Number of slots in apHash[] */
33478 PgHdr1 **apHash; /* Hash table for fast lookup by key */
@@ -33458,25 +33504,31 @@
33504
33505 /*
33506 ** Global data used by this cache.
33507 */
33508 static SQLITE_WSD struct PCacheGlobal {
33509 PGroup grp; /* The global PGroup for mode (2) */
33510
33511 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
33512 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
33513 ** fixed at sqlite3_initialize() time and do not require mutex protection.
33514 ** The nFreeSlot and pFree values do require mutex protection.
33515 */
33516 int isInit; /* True if initialized */
33517 int szSlot; /* Size of each free slot */
33518 int nSlot; /* The number of pcache slots */
33519 int nReserve; /* Try to keep nFreeSlot above this */
33520 void *pStart, *pEnd; /* Bounds of pagecache malloc range */
33521 /* Above requires no mutex. Use mutex below for variable that follow. */
33522 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
33523 int nFreeSlot; /* Number of unused pcache slots */
33524 PgFreeslot *pFree; /* Free page blocks */
33525 /* The following value requires a mutex to change. We skip the mutex on
33526 ** reading because (1) most platforms read a 32-bit integer atomically and
33527 ** (2) even if an incorrect value is read, no great harm is done since this
33528 ** is really just an optimization. */
33529 int bUnderPressure; /* True if low on PAGECACHE memory */
33530 } pcache1_g;
33531
33532 /*
33533 ** All code in this file should access the global structure above via the
33534 ** alias "pcache1". This ensures that the WSD emulation is used when
@@ -33498,23 +33550,26 @@
33550 */
33551 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
33552 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
33553
33554 /*
33555 ** Macros to enter and leave the PCache LRU mutex.
33556 */
33557 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
33558 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
33559
33560 /******************************************************************************/
33561 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
33562
33563 /*
33564 ** This function is called during initialization if a static buffer is
33565 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
33566 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
33567 ** enough to contain 'n' buffers of 'sz' bytes each.
33568 **
33569 ** This routine is called from sqlite3_initialize() and so it is guaranteed
33570 ** to be serialized already. There is no need for further mutexing.
33571 */
33572 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
33573 if( pcache1.isInit ){
33574 PgFreeslot *p;
33575 sz = ROUNDDOWN8(sz);
@@ -33521,10 +33576,11 @@
33576 pcache1.szSlot = sz;
33577 pcache1.nSlot = pcache1.nFreeSlot = n;
33578 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
33579 pcache1.pStart = pBuf;
33580 pcache1.pFree = 0;
33581 pcache1.bUnderPressure = 0;
33582 while( n-- ){
33583 p = (PgFreeslot*)pBuf;
33584 p->pNext = pcache1.pFree;
33585 pcache1.pFree = p;
33586 pBuf = (void*)&((char*)pBuf)[sz];
@@ -33536,33 +33592,35 @@
33592 /*
33593 ** Malloc function used within this file to allocate space from the buffer
33594 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
33595 ** such buffer exists or there is no space left in it, this function falls
33596 ** back to sqlite3Malloc().
33597 **
33598 ** Multiple threads can run this routine at the same time. Global variables
33599 ** in pcache1 need to be protected via mutex.
33600 */
33601 static void *pcache1Alloc(int nByte){
33602 void *p = 0;
33603 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
33604 sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
33605 if( nByte<=pcache1.szSlot ){
33606 sqlite3_mutex_enter(pcache1.mutex);
33607 p = (PgHdr1 *)pcache1.pFree;
33608 if( p ){
33609 pcache1.pFree = pcache1.pFree->pNext;
33610 pcache1.nFreeSlot--;
33611 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
33612 assert( pcache1.nFreeSlot>=0 );
33613 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
33614 }
33615 sqlite3_mutex_leave(pcache1.mutex);
33616 }
33617 if( p==0 ){
33618 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
33619 ** it from sqlite3Malloc instead.
33620 */
 
33621 p = sqlite3Malloc(nByte);
 
33622 if( p ){
33623 int sz = sqlite3MallocSize(p);
33624 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
33625 }
33626 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
@@ -33572,20 +33630,22 @@
33630
33631 /*
33632 ** Free an allocated buffer obtained from pcache1Alloc().
33633 */
33634 static void pcache1Free(void *p){
 
33635 if( p==0 ) return;
33636 if( p>=pcache1.pStart && p<pcache1.pEnd ){
33637 PgFreeslot *pSlot;
33638 sqlite3_mutex_enter(pcache1.mutex);
33639 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
33640 pSlot = (PgFreeslot*)p;
33641 pSlot->pNext = pcache1.pFree;
33642 pcache1.pFree = pSlot;
33643 pcache1.nFreeSlot++;
33644 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
33645 assert( pcache1.nFreeSlot<=pcache1.nSlot );
33646 sqlite3_mutex_leave(pcache1.mutex);
33647 }else{
33648 int iSize;
33649 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
33650 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
33651 iSize = sqlite3MallocSize(p);
@@ -33597,11 +33657,10 @@
33657 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
33658 /*
33659 ** Return the size of a pcache allocation
33660 */
33661 static int pcache1MemSize(void *p){
 
33662 if( p>=pcache1.pStart && p<pcache1.pEnd ){
33663 return pcache1.szSlot;
33664 }else{
33665 int iSize;
33666 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
@@ -33621,11 +33680,11 @@
33680 void *pPg = pcache1Alloc(nByte);
33681 PgHdr1 *p;
33682 if( pPg ){
33683 p = PAGE_TO_PGHDR1(pCache, pPg);
33684 if( pCache->bPurgeable ){
33685 pCache->pGroup->nCurrentPage++;
33686 }
33687 }else{
33688 p = 0;
33689 }
33690 return p;
@@ -33638,12 +33697,13 @@
33697 ** that the current implementation happens to never call this routine
33698 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
33699 */
33700 static void pcache1FreePage(PgHdr1 *p){
33701 if( ALWAYS(p) ){
33702 PCache1 *pCache = p->pCache;
33703 if( pCache->bPurgeable ){
33704 pCache->pGroup->nCurrentPage--;
33705 }
33706 pcache1Free(PGHDR1_TO_PAGE(p));
33707 }
33708 }
33709
@@ -33651,24 +33711,18 @@
33711 ** Malloc function used by SQLite to obtain space from the buffer configured
33712 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
33713 ** exists, this function falls back to sqlite3Malloc().
33714 */
33715 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
33716 return pcache1Alloc(sz);
 
 
 
 
33717 }
33718
33719 /*
33720 ** Free an allocated buffer obtained from sqlite3PageMalloc().
33721 */
33722 SQLITE_PRIVATE void sqlite3PageFree(void *p){
 
33723 pcache1Free(p);
 
33724 }
33725
33726
33727 /*
33728 ** Return true if it desirable to avoid allocating a new page cache
@@ -33685,13 +33739,12 @@
33739 ** under memory pressure, then again it is desirable to avoid
33740 ** allocating a new page cache entry in order to avoid stressing
33741 ** the heap even further.
33742 */
33743 static int pcache1UnderMemoryPressure(PCache1 *pCache){
 
33744 if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
33745 return pcache1.bUnderPressure;
33746 }else{
33747 return sqlite3HeapNearlyFull();
33748 }
33749 }
33750
@@ -33700,29 +33753,29 @@
33753
33754 /*
33755 ** This function is used to resize the hash table used by the cache passed
33756 ** as the first argument.
33757 **
33758 ** The PCache mutex must be held when this function is called.
33759 */
33760 static int pcache1ResizeHash(PCache1 *p){
33761 PgHdr1 **apNew;
33762 unsigned int nNew;
33763 unsigned int i;
33764
33765 assert( sqlite3_mutex_held(p->pGroup->mutex) );
33766
33767 nNew = p->nHash*2;
33768 if( nNew<256 ){
33769 nNew = 256;
33770 }
33771
33772 pcache1LeaveMutex(p->pGroup);
33773 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
33774 apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
33775 if( p->nHash ){ sqlite3EndBenignMalloc(); }
33776 pcache1EnterMutex(p->pGroup);
33777 if( apNew ){
33778 memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
33779 for(i=0; i<p->nHash; i++){
33780 PgHdr1 *pPage;
33781 PgHdr1 *pNext = p->apHash[i];
@@ -33741,29 +33794,37 @@
33794 return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
33795 }
33796
33797 /*
33798 ** This function is used internally to remove the page pPage from the
33799 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
33800 ** LRU list, then this function is a no-op.
33801 **
33802 ** The PGroup mutex must be held when this function is called.
33803 **
33804 ** If pPage is NULL then this routine is a no-op.
33805 */
33806 static void pcache1PinPage(PgHdr1 *pPage){
33807 PCache1 *pCache;
33808 PGroup *pGroup;
33809
33810 if( pPage==0 ) return;
33811 pCache = pPage->pCache;
33812 pGroup = pCache->pGroup;
33813 assert( sqlite3_mutex_held(pGroup->mutex) );
33814 if( pPage->pLruNext || pPage==pGroup->pLruTail ){
33815 if( pPage->pLruPrev ){
33816 pPage->pLruPrev->pLruNext = pPage->pLruNext;
33817 }
33818 if( pPage->pLruNext ){
33819 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
33820 }
33821 if( pGroup->pLruHead==pPage ){
33822 pGroup->pLruHead = pPage->pLruNext;
33823 }
33824 if( pGroup->pLruTail==pPage ){
33825 pGroup->pLruTail = pPage->pLruPrev;
33826 }
33827 pPage->pLruNext = 0;
33828 pPage->pLruPrev = 0;
33829 pPage->pCache->nRecyclable--;
33830 }
@@ -33772,32 +33833,34 @@
33833
33834 /*
33835 ** Remove the page supplied as an argument from the hash table
33836 ** (PCache1.apHash structure) that it is currently stored in.
33837 **
33838 ** The PGroup mutex must be held when this function is called.
33839 */
33840 static void pcache1RemoveFromHash(PgHdr1 *pPage){
33841 unsigned int h;
33842 PCache1 *pCache = pPage->pCache;
33843 PgHdr1 **pp;
33844
33845 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
33846 h = pPage->iKey % pCache->nHash;
33847 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
33848 *pp = (*pp)->pNext;
33849
33850 pCache->nPage--;
33851 }
33852
33853 /*
33854 ** If there are currently more than nMaxPage pages allocated, try
33855 ** to recycle pages to reduce the number allocated to nMaxPage.
33856 */
33857 static void pcache1EnforceMaxPage(PGroup *pGroup){
33858 assert( sqlite3_mutex_held(pGroup->mutex) );
33859 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
33860 PgHdr1 *p = pGroup->pLruTail;
33861 assert( p->pCache->pGroup==pGroup );
33862 pcache1PinPage(p);
33863 pcache1RemoveFromHash(p);
33864 pcache1FreePage(p);
33865 }
33866 }
@@ -33805,19 +33868,19 @@
33868 /*
33869 ** Discard all pages from cache pCache with a page number (key value)
33870 ** greater than or equal to iLimit. Any pinned pages that meet this
33871 ** criteria are unpinned before they are discarded.
33872 **
33873 ** The PCache mutex must be held when this function is called.
33874 */
33875 static void pcache1TruncateUnsafe(
33876 PCache1 *pCache, /* The cache to truncate */
33877 unsigned int iLimit /* Drop pages with this pgno or larger */
33878 ){
33879 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */
33880 unsigned int h;
33881 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
33882 for(h=0; h<pCache->nHash; h++){
33883 PgHdr1 **pp = &pCache->apHash[h];
33884 PgHdr1 *pPage;
33885 while( (pPage = *pp)!=0 ){
33886 if( pPage->iKey>=iLimit ){
@@ -33843,12 +33906,14 @@
33906 static int pcache1Init(void *NotUsed){
33907 UNUSED_PARAMETER(NotUsed);
33908 assert( pcache1.isInit==0 );
33909 memset(&pcache1, 0, sizeof(pcache1));
33910 if( sqlite3GlobalConfig.bCoreMutex ){
33911 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
33912 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
33913 }
33914 pcache1.grp.mxPinned = 10;
33915 pcache1.isInit = 1;
33916 return SQLITE_OK;
33917 }
33918
33919 /*
@@ -33866,22 +33931,51 @@
33931 ** Implementation of the sqlite3_pcache.xCreate method.
33932 **
33933 ** Allocate a new cache.
33934 */
33935 static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
33936 PCache1 *pCache; /* The newly created page cache */
33937 PGroup *pGroup; /* The group the new page cache will belong to */
33938 int sz; /* Bytes of memory required to allocate the new cache */
33939
33940 /*
33941 ** The seperateCache variable is true if each PCache has its own private
33942 ** PGroup. In other words, separateCache is true for mode (1) where no
33943 ** mutexing is required.
33944 **
33945 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
33946 **
33947 ** * Always use a unified cache in single-threaded applications
33948 **
33949 ** * Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
33950 ** use separate caches (mode-1)
33951 */
33952 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
33953 const int separateCache = 0;
33954 #else
33955 int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
33956 #endif
33957
33958 sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
33959 pCache = (PCache1 *)sqlite3_malloc(sz);
33960 if( pCache ){
33961 memset(pCache, 0, sz);
33962 if( separateCache ){
33963 pGroup = (PGroup*)&pCache[1];
33964 pGroup->mxPinned = 10;
33965 }else{
33966 pGroup = &pcache1_g.grp;
33967 }
33968 pCache->pGroup = pGroup;
33969 pCache->szPage = szPage;
33970 pCache->bPurgeable = (bPurgeable ? 1 : 0);
33971 if( bPurgeable ){
33972 pCache->nMin = 10;
33973 pcache1EnterMutex(pGroup);
33974 pGroup->nMinPage += pCache->nMin;
33975 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
33976 pcache1LeaveMutex(pGroup);
33977 }
33978 }
33979 return (sqlite3_pcache *)pCache;
33980 }
33981
@@ -33891,26 +33985,30 @@
33985 ** Configure the cache_size limit for a cache.
33986 */
33987 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
33988 PCache1 *pCache = (PCache1 *)p;
33989 if( pCache->bPurgeable ){
33990 PGroup *pGroup = pCache->pGroup;
33991 pcache1EnterMutex(pGroup);
33992 pGroup->nMaxPage += (nMax - pCache->nMax);
33993 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
33994 pCache->nMax = nMax;
33995 pCache->mxPinned = nMax*9/10;
33996 pcache1EnforceMaxPage(pGroup);
33997 pcache1LeaveMutex(pGroup);
33998 }
33999 }
34000
34001 /*
34002 ** Implementation of the sqlite3_pcache.xPagecount method.
34003 */
34004 static int pcache1Pagecount(sqlite3_pcache *p){
34005 int n;
34006 PCache1 *pCache = (PCache1*)p;
34007 pcache1EnterMutex(pCache->pGroup);
34008 n = pCache->nPage;
34009 pcache1LeaveMutex(pCache->pGroup);
34010 return n;
34011 }
34012
34013 /*
34014 ** Implementation of the sqlite3_pcache.xFetch method.
@@ -33965,65 +34063,89 @@
34063 ** proceed to step 5.
34064 **
34065 ** 5. Otherwise, allocate and return a new page buffer.
34066 */
34067 static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
34068 int nPinned;
34069 PCache1 *pCache = (PCache1 *)p;
34070 PGroup *pGroup;
34071 PgHdr1 *pPage = 0;
34072
34073 assert( pCache->bPurgeable || createFlag!=1 );
34074 assert( pCache->bPurgeable || pCache->nMin==0 );
34075 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
34076 assert( pCache->nMin==0 || pCache->bPurgeable );
34077 pcache1EnterMutex(pGroup = pCache->pGroup);
34078
34079 /* Step 1: Search the hash table for an existing entry. */
34080 if( pCache->nHash>0 ){
34081 unsigned int h = iKey % pCache->nHash;
34082 for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
34083 }
34084
34085 /* Step 2: Abort if no existing page is found and createFlag is 0 */
34086 if( pPage || createFlag==0 ){
34087 pcache1PinPage(pPage);
34088 goto fetch_out;
34089 }
34090
34091 /* The pGroup local variable will normally be initialized by the
34092 ** pcache1EnterMutex() macro above. But if SQLITE_MUTEX_OMIT is defined,
34093 ** then pcache1EnterMutex() is a no-op, so we have to initialize the
34094 ** local variable here. Delaying the initialization of pGroup is an
34095 ** optimization: The common case is to exit the module before reaching
34096 ** this point.
34097 */
34098 #ifdef SQLITE_MUTEX_OMIT
34099 pGroup = pCache->pGroup;
34100 #endif
34101
34102
34103 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
34104 nPinned = pCache->nPage - pCache->nRecyclable;
34105 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
34106 assert( pCache->mxPinned == pCache->nMax*9/10 );
34107 if( createFlag==1 && (
34108 nPinned>=pGroup->mxPinned
34109 || nPinned>=(int)pCache->mxPinned
34110 || pcache1UnderMemoryPressure(pCache)
34111 )){
34112 goto fetch_out;
34113 }
34114
34115 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
34116 goto fetch_out;
34117 }
34118
34119 /* Step 4. Try to recycle a page. */
34120 if( pCache->bPurgeable && pGroup->pLruTail && (
34121 (pCache->nPage+1>=pCache->nMax)
34122 || pGroup->nCurrentPage>=pGroup->nMaxPage
34123 || pcache1UnderMemoryPressure(pCache)
34124 )){
34125 PCache1 *pOtherCache;
34126 pPage = pGroup->pLruTail;
34127 pcache1RemoveFromHash(pPage);
34128 pcache1PinPage(pPage);
34129 if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
34130 pcache1FreePage(pPage);
34131 pPage = 0;
34132 }else{
34133 pGroup->nCurrentPage -=
34134 (pOtherCache->bPurgeable - pCache->bPurgeable);
34135 }
34136 }
34137
34138 /* Step 5. If a usable page buffer has still not been found,
34139 ** attempt to allocate a new one.
34140 */
34141 if( !pPage ){
34142 if( createFlag==1 ) sqlite3BeginBenignMalloc();
34143 pcache1LeaveMutex(pGroup);
34144 pPage = pcache1AllocPage(pCache);
34145 pcache1EnterMutex(pGroup);
34146 if( createFlag==1 ) sqlite3EndBenignMalloc();
34147 }
34148
34149 if( pPage ){
34150 unsigned int h = iKey % pCache->nHash;
34151 pCache->nPage++;
@@ -34038,12 +34160,11 @@
34160
34161 fetch_out:
34162 if( pPage && iKey>pCache->iMaxKey ){
34163 pCache->iMaxKey = iKey;
34164 }
34165 pcache1LeaveMutex(pGroup);
 
34166 return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
34167 }
34168
34169
34170 /*
@@ -34052,37 +34173,38 @@
34173 ** Mark a page as unpinned (eligible for asynchronous recycling).
34174 */
34175 static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
34176 PCache1 *pCache = (PCache1 *)p;
34177 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
34178 PGroup *pGroup = pCache->pGroup;
34179
34180 assert( pPage->pCache==pCache );
34181 pcache1EnterMutex(pGroup);
34182
34183 /* It is an error to call this function if the page is already
34184 ** part of the PGroup LRU list.
34185 */
34186 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
34187 assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
34188
34189 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
34190 pcache1RemoveFromHash(pPage);
34191 pcache1FreePage(pPage);
34192 }else{
34193 /* Add the page to the PGroup LRU list. */
34194 if( pGroup->pLruHead ){
34195 pGroup->pLruHead->pLruPrev = pPage;
34196 pPage->pLruNext = pGroup->pLruHead;
34197 pGroup->pLruHead = pPage;
34198 }else{
34199 pGroup->pLruTail = pPage;
34200 pGroup->pLruHead = pPage;
34201 }
34202 pCache->nRecyclable++;
34203 }
34204
34205 pcache1LeaveMutex(pCache->pGroup);
34206 }
34207
34208 /*
34209 ** Implementation of the sqlite3_pcache.xRekey method.
34210 */
@@ -34097,11 +34219,11 @@
34219 PgHdr1 **pp;
34220 unsigned int h;
34221 assert( pPage->iKey==iOld );
34222 assert( pPage->pCache==pCache );
34223
34224 pcache1EnterMutex(pCache->pGroup);
34225
34226 h = iOld%pCache->nHash;
34227 pp = &pCache->apHash[h];
34228 while( (*pp)!=pPage ){
34229 pp = &(*pp)->pNext;
@@ -34114,11 +34236,11 @@
34236 pCache->apHash[h] = pPage;
34237 if( iNew>pCache->iMaxKey ){
34238 pCache->iMaxKey = iNew;
34239 }
34240
34241 pcache1LeaveMutex(pCache->pGroup);
34242 }
34243
34244 /*
34245 ** Implementation of the sqlite3_pcache.xTruncate method.
34246 **
@@ -34126,32 +34248,34 @@
34248 ** or greater than parameter iLimit. Any pinned pages with a page number
34249 ** equal to or greater than iLimit are implicitly unpinned.
34250 */
34251 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
34252 PCache1 *pCache = (PCache1 *)p;
34253 pcache1EnterMutex(pCache->pGroup);
34254 if( iLimit<=pCache->iMaxKey ){
34255 pcache1TruncateUnsafe(pCache, iLimit);
34256 pCache->iMaxKey = iLimit-1;
34257 }
34258 pcache1LeaveMutex(pCache->pGroup);
34259 }
34260
34261 /*
34262 ** Implementation of the sqlite3_pcache.xDestroy method.
34263 **
34264 ** Destroy a cache allocated using pcache1Create().
34265 */
34266 static void pcache1Destroy(sqlite3_pcache *p){
34267 PCache1 *pCache = (PCache1 *)p;
34268 PGroup *pGroup = pCache->pGroup;
34269 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
34270 pcache1EnterMutex(pGroup);
34271 pcache1TruncateUnsafe(pCache, 0);
34272 pGroup->nMaxPage -= pCache->nMax;
34273 pGroup->nMinPage -= pCache->nMin;
34274 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
34275 pcache1EnforceMaxPage(pGroup);
34276 pcache1LeaveMutex(pGroup);
34277 sqlite3_free(pCache->apHash);
34278 sqlite3_free(pCache);
34279 }
34280
34281 /*
@@ -34186,20 +34310,22 @@
34310 ** been released, the function returns. The return value is the total number
34311 ** of bytes of memory released.
34312 */
34313 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
34314 int nFree = 0;
34315 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
34316 assert( sqlite3_mutex_notheld(pcache1.mutex) );
34317 if( pcache1.pStart==0 ){
34318 PgHdr1 *p;
34319 pcache1EnterMutex(&pcache1.grp);
34320 while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
34321 nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
34322 pcache1PinPage(p);
34323 pcache1RemoveFromHash(p);
34324 pcache1FreePage(p);
34325 }
34326 pcache1LeaveMutex(&pcache1.grp);
34327 }
34328 return nFree;
34329 }
34330 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
34331
@@ -34214,16 +34340,16 @@
34340 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
34341 int *pnRecyclable /* OUT: Total number of pages available for recycling */
34342 ){
34343 PgHdr1 *p;
34344 int nRecyclable = 0;
34345 for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
34346 nRecyclable++;
34347 }
34348 *pnCurrent = pcache1.grp.nCurrentPage;
34349 *pnMax = pcache1.grp.nMaxPage;
34350 *pnMin = pcache1.grp.nMinPage;
34351 *pnRecyclable = nRecyclable;
34352 }
34353 #endif
34354
34355 /************** End of pcache1.c *********************************************/
@@ -40390,19 +40516,17 @@
40516 **
40517 ** If successful, or if called on a pager for which it is a no-op, this
40518 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
40519 */
40520 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
40521 int rc = SQLITE_OK;
40522 if( !pPager->noSync ){
40523 assert( !MEMDB );
 
 
40524 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
40525 }else if( isOpen(pPager->fd) ){
40526 assert( !MEMDB );
40527 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
40528 }
40529 return rc;
40530 }
40531
40532 /*
@@ -46264,18 +46388,13 @@
46388 testcase( nPayload==pPage->maxLocal+1 );
46389 if( likely(nPayload<=pPage->maxLocal) ){
46390 /* This is the (easy) common case where the entire payload fits
46391 ** on the local page. No overflow is required.
46392 */
46393 if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
 
46394 pInfo->nLocal = (u16)nPayload;
46395 pInfo->iOverflow = 0;
 
 
 
 
46396 }else{
46397 /* If the payload will not fit completely on the local page, we have
46398 ** to decide how much to store locally and how much to spill onto
46399 ** overflow pages. The strategy is to minimize the amount of unused
46400 ** space on overflow pages while keeping the amount of local storage
@@ -53936,10 +54055,20 @@
54055 assert( pFile );
54056 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
54057 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
54058 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
54059 ));
54060
54061 /* This call ensures that all data required to recreate the original
54062 ** database has been stored in the journal for pDestPager and the
54063 ** journal synced to disk. So at this point we may safely modify
54064 ** the database file in any way, knowing that if a power failure
54065 ** occurs, the original database will be reconstructed from the
54066 ** journal file. */
54067 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
54068
54069 /* Write the extra pages and truncate the database file as required. */
54070 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
54071 for(
54072 iOff=PENDING_BYTE+pgszSrc;
54073 rc==SQLITE_OK && iOff<iEnd;
54074 iOff+=pgszSrc
@@ -53952,14 +54081,16 @@
54081 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
54082 }
54083 sqlite3PagerUnref(pSrcPg);
54084 }
54085 if( rc==SQLITE_OK ){
54086 rc = backupTruncateFile(pFile, iSize);
54087 }
54088
54089 /* Sync the database file to disk. */
54090 if( rc==SQLITE_OK ){
54091 rc = sqlite3PagerSync(pDestPager);
54092 }
54093 }else{
54094 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
54095 }
54096
@@ -58815,15 +58946,34 @@
58946 int rc;
58947
58948 assert(p);
58949 if( p->magic!=VDBE_MAGIC_RUN ){
58950 /* We used to require that sqlite3_reset() be called before retrying
58951 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
58952 ** with version 3.7.0, we changed this so that sqlite3_reset() would
58953 ** be called automatically instead of throwing the SQLITE_MISUSE error.
58954 ** This "automatic-reset" change is not technically an incompatibility,
58955 ** since any application that receives an SQLITE_MISUSE is broken by
58956 ** definition.
58957 **
58958 ** Nevertheless, some published applications that were originally written
58959 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
58960 ** returns, and the so were broken by the automatic-reset change. As a
58961 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
58962 ** legacy behavior of returning SQLITE_MISUSE for cases where the
58963 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
58964 ** or SQLITE_BUSY error.
58965 */
58966 #ifdef SQLITE_OMIT_AUTORESET
58967 if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
58968 sqlite3_reset((sqlite3_stmt*)p);
58969 }else{
58970 return SQLITE_MISUSE_BKPT;
58971 }
58972 #else
58973 sqlite3_reset((sqlite3_stmt*)p);
58974 #endif
58975 }
58976
58977 /* Check that malloc() has not failed. If it has, return early. */
58978 db = p->db;
58979 if( db->mallocFailed ){
@@ -58861,11 +59011,13 @@
59011 if( p->explain ){
59012 rc = sqlite3VdbeList(p);
59013 }else
59014 #endif /* SQLITE_OMIT_EXPLAIN */
59015 {
59016 db->vdbeExecCnt++;
59017 rc = sqlite3VdbeExec(p);
59018 db->vdbeExecCnt--;
59019 }
59020
59021 #ifndef SQLITE_OMIT_TRACE
59022 /* Invoke the profile callback if there is one
59023 */
@@ -59818,13 +59970,16 @@
59970 }
59971 return nTotal;
59972 }
59973
59974 /*
59975 ** This function returns a pointer to a nul-terminated string in memory
59976 ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
59977 ** string contains a copy of zRawSql but with host parameters expanded to
59978 ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
59979 ** then the returned string holds a copy of zRawSql with "-- " prepended
59980 ** to each line of text.
59981 **
59982 ** The calling function is responsible for making sure the memory returned
59983 ** is eventually freed.
59984 **
59985 ** ALGORITHM: Scan the input string looking for host parameters in any of
@@ -59851,67 +60006,76 @@
60006
60007 db = p->db;
60008 sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
60009 db->aLimit[SQLITE_LIMIT_LENGTH]);
60010 out.db = db;
60011 if( db->vdbeExecCnt>1 ){
60012 while( *zRawSql ){
60013 const char *zStart = zRawSql;
60014 while( *(zRawSql++)!='\n' && *zRawSql );
60015 sqlite3StrAccumAppend(&out, "-- ", 3);
60016 sqlite3StrAccumAppend(&out, zStart, zRawSql-zStart);
60017 }
60018 }else{
60019 while( zRawSql[0] ){
60020 n = findNextHostParameter(zRawSql, &nToken);
60021 assert( n>0 );
60022 sqlite3StrAccumAppend(&out, zRawSql, n);
60023 zRawSql += n;
60024 assert( zRawSql[0] || nToken==0 );
60025 if( nToken==0 ) break;
60026 if( zRawSql[0]=='?' ){
60027 if( nToken>1 ){
60028 assert( sqlite3Isdigit(zRawSql[1]) );
60029 sqlite3GetInt32(&zRawSql[1], &idx);
60030 }else{
60031 idx = nextIndex;
60032 }
60033 }else{
60034 assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
60035 testcase( zRawSql[0]==':' );
60036 testcase( zRawSql[0]=='$' );
60037 testcase( zRawSql[0]=='@' );
60038 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
60039 assert( idx>0 );
60040 }
60041 zRawSql += nToken;
60042 nextIndex = idx + 1;
60043 assert( idx>0 && idx<=p->nVar );
60044 pVar = &p->aVar[idx-1];
60045 if( pVar->flags & MEM_Null ){
60046 sqlite3StrAccumAppend(&out, "NULL", 4);
60047 }else if( pVar->flags & MEM_Int ){
60048 sqlite3XPrintf(&out, "%lld", pVar->u.i);
60049 }else if( pVar->flags & MEM_Real ){
60050 sqlite3XPrintf(&out, "%!.15g", pVar->r);
60051 }else if( pVar->flags & MEM_Str ){
60052 #ifndef SQLITE_OMIT_UTF16
60053 u8 enc = ENC(db);
60054 if( enc!=SQLITE_UTF8 ){
60055 Mem utf8;
60056 memset(&utf8, 0, sizeof(utf8));
60057 utf8.db = db;
60058 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
60059 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
60060 sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
60061 sqlite3VdbeMemRelease(&utf8);
60062 }else
60063 #endif
60064 {
60065 sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
60066 }
60067 }else if( pVar->flags & MEM_Zero ){
60068 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
60069 }else{
60070 assert( pVar->flags & MEM_Blob );
60071 sqlite3StrAccumAppend(&out, "x'", 2);
60072 for(i=0; i<pVar->n; i++){
60073 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
60074 }
60075 sqlite3StrAccumAppend(&out, "'", 1);
60076 }
60077 }
60078 }
60079 return sqlite3StrAccumFinish(&out);
60080 }
60081
@@ -73655,15 +73819,15 @@
73819 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
73820 return;
73821 }
73822 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
73823 for(i=0; i<nCol; i++){
73824 int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
73825 if( i==0 ){
73826 sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
73827 }
73828 sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
73829 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
73830 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
73831 }
73832
73833 /* End of the analysis loop. */
@@ -87777,11 +87941,11 @@
87941 /*
87942 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
87943 */
87944 static int sqlite3Prepare16(
87945 sqlite3 *db, /* Database handle. */
87946 const void *zSql, /* UTF-16 encoded SQL statement. */
87947 int nBytes, /* Length of zSql in bytes. */
87948 int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
87949 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
87950 const void **pzTail /* OUT: End of parsed string */
87951 ){
@@ -87827,11 +87991,11 @@
87991 ** and the statement is automatically recompiled if an schema change
87992 ** occurs.
87993 */
87994 SQLITE_API int sqlite3_prepare16(
87995 sqlite3 *db, /* Database handle. */
87996 const void *zSql, /* UTF-16 encoded SQL statement. */
87997 int nBytes, /* Length of zSql in bytes. */
87998 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
87999 const void **pzTail /* OUT: End of parsed string */
88000 ){
88001 int rc;
@@ -87839,11 +88003,11 @@
88003 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
88004 return rc;
88005 }
88006 SQLITE_API int sqlite3_prepare16_v2(
88007 sqlite3 *db, /* Database handle. */
88008 const void *zSql, /* UTF-16 encoded SQL statement. */
88009 int nBytes, /* Length of zSql in bytes. */
88010 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
88011 const void **pzTail /* OUT: End of parsed string */
88012 ){
88013 int rc;
88014
+26 -17
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.5"
111111
#define SQLITE_VERSION_NUMBER 3007005
112
-#define SQLITE_SOURCE_ID "2011-01-17 02:24:12 b93f6f3e679c7710f42580a8dd9ce43136376c1d"
112
+#define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -719,24 +719,28 @@
719719
** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
720720
** to the [sqlite3_file] object associated with a particular database
721721
** connection. See the [sqlite3_file_control()] documentation for
722722
** additional information.
723723
**
724
-** The [SQLITE_FCNTL_SYNC] opcode is used internally. SQLite calls
725
-** the file-control method with this opcode immediately after the database
726
-** file is synced, or if the database is running in synchronous=off mode
727
-** immediately after it would have been synced otherwise. This makes it
728
-** easier to write special VFS modules that depend on the xSync call.
724
+** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
725
+** SQLite and sent to all VFSes in place of a call to the xSync method
726
+** when the database connection has [PRAGMA synchronous] set to OFF.)^
727
+** Some specialized VFSes need this signal in order to operate correctly
728
+** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
729
+** VFSes do not need this signal and should silently ignore this opcode.
730
+** Applications should not call [sqlite3_file_control()] with this
731
+** opcode as doing so may disrupt the operation of the specilized VFSes
732
+** that do require it.
729733
*/
730734
#define SQLITE_FCNTL_LOCKSTATE 1
731735
#define SQLITE_GET_LOCKPROXYFILE 2
732736
#define SQLITE_SET_LOCKPROXYFILE 3
733737
#define SQLITE_LAST_ERRNO 4
734738
#define SQLITE_FCNTL_SIZE_HINT 5
735739
#define SQLITE_FCNTL_CHUNK_SIZE 6
736740
#define SQLITE_FCNTL_FILE_POINTER 7
737
-#define SQLITE_FCNTL_SYNC 8
741
+#define SQLITE_FCNTL_SYNC_OMITTED 8
738742
739743
740744
/*
741745
** CAPI3REF: Mutex Handle
742746
**
@@ -1852,11 +1856,11 @@
18521856
** The strings returned by these two routines should be
18531857
** released by [sqlite3_free()]. ^Both routines return a
18541858
** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
18551859
** memory to hold the resulting string.
18561860
**
1857
-** ^(In sqlite3_snprintf() routine is similar to "snprintf()" from
1861
+** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
18581862
** the standard C library. The result is written into the
18591863
** buffer supplied as the second parameter whose size is given by
18601864
** the first parameter. Note that the order of the
18611865
** first two parameters is reversed from snprintf().)^ This is an
18621866
** historical accident that cannot be fixed without breaking
@@ -2664,11 +2668,11 @@
26642668
26652669
/*
26662670
** CAPI3REF: Determine If An SQL Statement Writes The Database
26672671
**
26682672
** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
2669
-** and only if the [prepared statement] X is makes no direct changes to
2673
+** and only if the [prepared statement] X makes no direct changes to
26702674
** the content of the database file.
26712675
**
26722676
** Note that [application-defined SQL functions] or
26732677
** [virtual tables] might change the database indirectly as a side effect.
26742678
** ^(For example, if an application defines a function "eval()" that
@@ -3081,17 +3085,21 @@
30813085
** already been [sqlite3_finalize | finalized] or on one that had
30823086
** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
30833087
** be the case that the same database connection is being used by two or
30843088
** more threads at the same moment in time.
30853089
**
3086
-** For all versions of SQLite up to and including 3.6.23.1, it was required
3087
-** after sqlite3_step() returned anything other than [SQLITE_ROW] that
3088
-** [sqlite3_reset()] be called before any subsequent invocation of
3089
-** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
3090
-** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
3091
-** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
3092
-** automatically in this circumstance rather than returning [SQLITE_MISUSE].
3090
+** For all versions of SQLite up to and including 3.6.23.1, a call to
3091
+** [sqlite3_reset()] was required after sqlite3_step() returned anything
3092
+** other than [SQLITE_ROW] before any subsequent invocation of
3093
+** sqlite3_step(). Failure to reset the prepared statement using
3094
+** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3095
+** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began
3096
+** calling [sqlite3_reset()] automatically in this circumstance rather
3097
+** than returning [SQLITE_MISUSE]. This is not considered a compatibility
3098
+** break because any application that ever receives an SQLITE_MISUSE error
3099
+** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
3100
+** can be used to restore the legacy behavior.
30933101
**
30943102
** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
30953103
** API always returns a generic error code, [SQLITE_ERROR], following any
30963104
** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
30973105
** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
@@ -5265,11 +5273,12 @@
52655273
#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
52665274
#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
52675275
#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
52685276
#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
52695277
#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
5270
-#define SQLITE_MUTEX_STATIC_LRU2 7 /* lru page list */
5278
+#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
5279
+#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
52715280
52725281
/*
52735282
** CAPI3REF: Retrieve the mutex for a database connection
52745283
**
52755284
** ^This interface returns a pointer the [sqlite3_mutex] object that
52765285
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.5"
111 #define SQLITE_VERSION_NUMBER 3007005
112 #define SQLITE_SOURCE_ID "2011-01-17 02:24:12 b93f6f3e679c7710f42580a8dd9ce43136376c1d"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -719,24 +719,28 @@
719 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
720 ** to the [sqlite3_file] object associated with a particular database
721 ** connection. See the [sqlite3_file_control()] documentation for
722 ** additional information.
723 **
724 ** The [SQLITE_FCNTL_SYNC] opcode is used internally. SQLite calls
725 ** the file-control method with this opcode immediately after the database
726 ** file is synced, or if the database is running in synchronous=off mode
727 ** immediately after it would have been synced otherwise. This makes it
728 ** easier to write special VFS modules that depend on the xSync call.
 
 
 
 
729 */
730 #define SQLITE_FCNTL_LOCKSTATE 1
731 #define SQLITE_GET_LOCKPROXYFILE 2
732 #define SQLITE_SET_LOCKPROXYFILE 3
733 #define SQLITE_LAST_ERRNO 4
734 #define SQLITE_FCNTL_SIZE_HINT 5
735 #define SQLITE_FCNTL_CHUNK_SIZE 6
736 #define SQLITE_FCNTL_FILE_POINTER 7
737 #define SQLITE_FCNTL_SYNC 8
738
739
740 /*
741 ** CAPI3REF: Mutex Handle
742 **
@@ -1852,11 +1856,11 @@
1852 ** The strings returned by these two routines should be
1853 ** released by [sqlite3_free()]. ^Both routines return a
1854 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
1855 ** memory to hold the resulting string.
1856 **
1857 ** ^(In sqlite3_snprintf() routine is similar to "snprintf()" from
1858 ** the standard C library. The result is written into the
1859 ** buffer supplied as the second parameter whose size is given by
1860 ** the first parameter. Note that the order of the
1861 ** first two parameters is reversed from snprintf().)^ This is an
1862 ** historical accident that cannot be fixed without breaking
@@ -2664,11 +2668,11 @@
2664
2665 /*
2666 ** CAPI3REF: Determine If An SQL Statement Writes The Database
2667 **
2668 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
2669 ** and only if the [prepared statement] X is makes no direct changes to
2670 ** the content of the database file.
2671 **
2672 ** Note that [application-defined SQL functions] or
2673 ** [virtual tables] might change the database indirectly as a side effect.
2674 ** ^(For example, if an application defines a function "eval()" that
@@ -3081,17 +3085,21 @@
3081 ** already been [sqlite3_finalize | finalized] or on one that had
3082 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
3083 ** be the case that the same database connection is being used by two or
3084 ** more threads at the same moment in time.
3085 **
3086 ** For all versions of SQLite up to and including 3.6.23.1, it was required
3087 ** after sqlite3_step() returned anything other than [SQLITE_ROW] that
3088 ** [sqlite3_reset()] be called before any subsequent invocation of
3089 ** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
3090 ** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
3091 ** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
3092 ** automatically in this circumstance rather than returning [SQLITE_MISUSE].
 
 
 
 
3093 **
3094 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3095 ** API always returns a generic error code, [SQLITE_ERROR], following any
3096 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
3097 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
@@ -5265,11 +5273,12 @@
5265 #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
5266 #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
5267 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
5268 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
5269 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
5270 #define SQLITE_MUTEX_STATIC_LRU2 7 /* lru page list */
 
5271
5272 /*
5273 ** CAPI3REF: Retrieve the mutex for a database connection
5274 **
5275 ** ^This interface returns a pointer the [sqlite3_mutex] object that
5276
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.5"
111 #define SQLITE_VERSION_NUMBER 3007005
112 #define SQLITE_SOURCE_ID "2011-01-25 18:30:51 c17703ec1e604934f8bd5b1f66f34b19d17a6d1f"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -719,24 +719,28 @@
719 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
720 ** to the [sqlite3_file] object associated with a particular database
721 ** connection. See the [sqlite3_file_control()] documentation for
722 ** additional information.
723 **
724 ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
725 ** SQLite and sent to all VFSes in place of a call to the xSync method
726 ** when the database connection has [PRAGMA synchronous] set to OFF.)^
727 ** Some specialized VFSes need this signal in order to operate correctly
728 ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
729 ** VFSes do not need this signal and should silently ignore this opcode.
730 ** Applications should not call [sqlite3_file_control()] with this
731 ** opcode as doing so may disrupt the operation of the specilized VFSes
732 ** that do require it.
733 */
734 #define SQLITE_FCNTL_LOCKSTATE 1
735 #define SQLITE_GET_LOCKPROXYFILE 2
736 #define SQLITE_SET_LOCKPROXYFILE 3
737 #define SQLITE_LAST_ERRNO 4
738 #define SQLITE_FCNTL_SIZE_HINT 5
739 #define SQLITE_FCNTL_CHUNK_SIZE 6
740 #define SQLITE_FCNTL_FILE_POINTER 7
741 #define SQLITE_FCNTL_SYNC_OMITTED 8
742
743
744 /*
745 ** CAPI3REF: Mutex Handle
746 **
@@ -1852,11 +1856,11 @@
1856 ** The strings returned by these two routines should be
1857 ** released by [sqlite3_free()]. ^Both routines return a
1858 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
1859 ** memory to hold the resulting string.
1860 **
1861 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
1862 ** the standard C library. The result is written into the
1863 ** buffer supplied as the second parameter whose size is given by
1864 ** the first parameter. Note that the order of the
1865 ** first two parameters is reversed from snprintf().)^ This is an
1866 ** historical accident that cannot be fixed without breaking
@@ -2664,11 +2668,11 @@
2668
2669 /*
2670 ** CAPI3REF: Determine If An SQL Statement Writes The Database
2671 **
2672 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
2673 ** and only if the [prepared statement] X makes no direct changes to
2674 ** the content of the database file.
2675 **
2676 ** Note that [application-defined SQL functions] or
2677 ** [virtual tables] might change the database indirectly as a side effect.
2678 ** ^(For example, if an application defines a function "eval()" that
@@ -3081,17 +3085,21 @@
3085 ** already been [sqlite3_finalize | finalized] or on one that had
3086 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
3087 ** be the case that the same database connection is being used by two or
3088 ** more threads at the same moment in time.
3089 **
3090 ** For all versions of SQLite up to and including 3.6.23.1, a call to
3091 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
3092 ** other than [SQLITE_ROW] before any subsequent invocation of
3093 ** sqlite3_step(). Failure to reset the prepared statement using
3094 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3095 ** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began
3096 ** calling [sqlite3_reset()] automatically in this circumstance rather
3097 ** than returning [SQLITE_MISUSE]. This is not considered a compatibility
3098 ** break because any application that ever receives an SQLITE_MISUSE error
3099 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
3100 ** can be used to restore the legacy behavior.
3101 **
3102 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3103 ** API always returns a generic error code, [SQLITE_ERROR], following any
3104 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
3105 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
@@ -5265,11 +5273,12 @@
5273 #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
5274 #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
5275 #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
5276 #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
5277 #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
5278 #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
5279 #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
5280
5281 /*
5282 ** CAPI3REF: Retrieve the mutex for a database connection
5283 **
5284 ** ^This interface returns a pointer the [sqlite3_mutex] object that
5285

Keyboard Shortcuts

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