| | @@ -650,11 +650,11 @@ |
| 650 | 650 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 651 | 651 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 652 | 652 | */ |
| 653 | 653 | #define SQLITE_VERSION "3.7.5" |
| 654 | 654 | #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" |
| 656 | 656 | |
| 657 | 657 | /* |
| 658 | 658 | ** CAPI3REF: Run-Time Library Version Numbers |
| 659 | 659 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 660 | 660 | ** |
| | @@ -1262,24 +1262,28 @@ |
| 1262 | 1262 | ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer |
| 1263 | 1263 | ** to the [sqlite3_file] object associated with a particular database |
| 1264 | 1264 | ** connection. See the [sqlite3_file_control()] documentation for |
| 1265 | 1265 | ** additional information. |
| 1266 | 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. |
| 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. |
| 1272 | 1276 | */ |
| 1273 | 1277 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 1274 | 1278 | #define SQLITE_GET_LOCKPROXYFILE 2 |
| 1275 | 1279 | #define SQLITE_SET_LOCKPROXYFILE 3 |
| 1276 | 1280 | #define SQLITE_LAST_ERRNO 4 |
| 1277 | 1281 | #define SQLITE_FCNTL_SIZE_HINT 5 |
| 1278 | 1282 | #define SQLITE_FCNTL_CHUNK_SIZE 6 |
| 1279 | 1283 | #define SQLITE_FCNTL_FILE_POINTER 7 |
| 1280 | | -#define SQLITE_FCNTL_SYNC 8 |
| 1284 | +#define SQLITE_FCNTL_SYNC_OMITTED 8 |
| 1281 | 1285 | |
| 1282 | 1286 | |
| 1283 | 1287 | /* |
| 1284 | 1288 | ** CAPI3REF: Mutex Handle |
| 1285 | 1289 | ** |
| | @@ -2395,11 +2399,11 @@ |
| 2395 | 2399 | ** The strings returned by these two routines should be |
| 2396 | 2400 | ** released by [sqlite3_free()]. ^Both routines return a |
| 2397 | 2401 | ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough |
| 2398 | 2402 | ** memory to hold the resulting string. |
| 2399 | 2403 | ** |
| 2400 | | -** ^(In sqlite3_snprintf() routine is similar to "snprintf()" from |
| 2404 | +** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from |
| 2401 | 2405 | ** the standard C library. The result is written into the |
| 2402 | 2406 | ** buffer supplied as the second parameter whose size is given by |
| 2403 | 2407 | ** the first parameter. Note that the order of the |
| 2404 | 2408 | ** first two parameters is reversed from snprintf().)^ This is an |
| 2405 | 2409 | ** historical accident that cannot be fixed without breaking |
| | @@ -3207,11 +3211,11 @@ |
| 3207 | 3211 | |
| 3208 | 3212 | /* |
| 3209 | 3213 | ** CAPI3REF: Determine If An SQL Statement Writes The Database |
| 3210 | 3214 | ** |
| 3211 | 3215 | ** ^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 |
| 3213 | 3217 | ** the content of the database file. |
| 3214 | 3218 | ** |
| 3215 | 3219 | ** Note that [application-defined SQL functions] or |
| 3216 | 3220 | ** [virtual tables] might change the database indirectly as a side effect. |
| 3217 | 3221 | ** ^(For example, if an application defines a function "eval()" that |
| | @@ -3624,17 +3628,21 @@ |
| 3624 | 3628 | ** already been [sqlite3_finalize | finalized] or on one that had |
| 3625 | 3629 | ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could |
| 3626 | 3630 | ** be the case that the same database connection is being used by two or |
| 3627 | 3631 | ** more threads at the same moment in time. |
| 3628 | 3632 | ** |
| 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. |
| 3636 | 3644 | ** |
| 3637 | 3645 | ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step() |
| 3638 | 3646 | ** API always returns a generic error code, [SQLITE_ERROR], following any |
| 3639 | 3647 | ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call |
| 3640 | 3648 | ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the |
| | @@ -5808,11 +5816,12 @@ |
| 5808 | 5816 | #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ |
| 5809 | 5817 | #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */ |
| 5810 | 5818 | #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */ |
| 5811 | 5819 | #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ |
| 5812 | 5820 | #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() */ |
| 5814 | 5823 | |
| 5815 | 5824 | /* |
| 5816 | 5825 | ** CAPI3REF: Retrieve the mutex for a database connection |
| 5817 | 5826 | ** |
| 5818 | 5827 | ** ^This interface returns a pointer the [sqlite3_mutex] object that |
| | @@ -9016,10 +9025,11 @@ |
| 9016 | 9025 | int nExtension; /* Number of loaded extensions */ |
| 9017 | 9026 | void **aExtension; /* Array of shared library handles */ |
| 9018 | 9027 | struct Vdbe *pVdbe; /* List of active virtual machines */ |
| 9019 | 9028 | int activeVdbeCnt; /* Number of VDBEs currently executing */ |
| 9020 | 9029 | int writeVdbeCnt; /* Number of active VDBEs that are writing */ |
| 9030 | + int vdbeExecCnt; /* Number of nested calls to VdbeExec() */ |
| 9021 | 9031 | void (*xTrace)(void*,const char*); /* Trace function */ |
| 9022 | 9032 | void *pTraceArg; /* Argument to the trace function */ |
| 9023 | 9033 | void (*xProfile)(void*,const char*,u64); /* Profiling function */ |
| 9024 | 9034 | void *pProfileArg; /* Argument to profile function */ |
| 9025 | 9035 | void *pCommitArg; /* Argument to xCommitCallback() */ |
| | @@ -11787,10 +11797,13 @@ |
| 11787 | 11797 | #ifdef SQLITE_OMIT_AUTOINIT |
| 11788 | 11798 | "OMIT_AUTOINIT", |
| 11789 | 11799 | #endif |
| 11790 | 11800 | #ifdef SQLITE_OMIT_AUTOMATIC_INDEX |
| 11791 | 11801 | "OMIT_AUTOMATIC_INDEX", |
| 11802 | +#endif |
| 11803 | +#ifdef SQLITE_OMIT_AUTORESET |
| 11804 | + "OMIT_AUTORESET", |
| 11792 | 11805 | #endif |
| 11793 | 11806 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 11794 | 11807 | "OMIT_AUTOVACUUM", |
| 11795 | 11808 | #endif |
| 11796 | 11809 | #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
| | @@ -19048,10 +19061,11 @@ |
| 19048 | 19061 | N = p->nAlloc - p->nChar - 1; |
| 19049 | 19062 | if( N<=0 ){ |
| 19050 | 19063 | return; |
| 19051 | 19064 | } |
| 19052 | 19065 | }else{ |
| 19066 | + char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| 19053 | 19067 | i64 szNew = p->nChar; |
| 19054 | 19068 | szNew += N + 1; |
| 19055 | 19069 | if( szNew > p->mxAlloc ){ |
| 19056 | 19070 | sqlite3StrAccumReset(p); |
| 19057 | 19071 | p->tooBig = 1; |
| | @@ -19058,17 +19072,16 @@ |
| 19058 | 19072 | return; |
| 19059 | 19073 | }else{ |
| 19060 | 19074 | p->nAlloc = (int)szNew; |
| 19061 | 19075 | } |
| 19062 | 19076 | if( p->useMalloc==1 ){ |
| 19063 | | - zNew = sqlite3DbMallocRaw(p->db, p->nAlloc ); |
| 19077 | + zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 19064 | 19078 | }else{ |
| 19065 | | - zNew = sqlite3_malloc(p->nAlloc); |
| 19079 | + zNew = sqlite3_realloc(zOld, p->nAlloc); |
| 19066 | 19080 | } |
| 19067 | 19081 | if( zNew ){ |
| 19068 | | - memcpy(zNew, p->zText, p->nChar); |
| 19069 | | - sqlite3StrAccumReset(p); |
| 19082 | + if( zOld==0 ) memcpy(zNew, p->zText, p->nChar); |
| 19070 | 19083 | p->zText = zNew; |
| 19071 | 19084 | }else{ |
| 19072 | 19085 | p->mallocFailed = 1; |
| 19073 | 19086 | sqlite3StrAccumReset(p); |
| 19074 | 19087 | return; |
| | @@ -33400,10 +33413,42 @@ |
| 33400 | 33413 | |
| 33401 | 33414 | |
| 33402 | 33415 | typedef struct PCache1 PCache1; |
| 33403 | 33416 | typedef struct PgHdr1 PgHdr1; |
| 33404 | 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 | +}; |
| 33405 | 33450 | |
| 33406 | 33451 | /* Each page cache is an instance of the following object. Every |
| 33407 | 33452 | ** open database file (including each in-memory database and each |
| 33408 | 33453 | ** temporary or transient database) has a single page cache which |
| 33409 | 33454 | ** is an instance of this object. |
| | @@ -33413,20 +33458,21 @@ |
| 33413 | 33458 | */ |
| 33414 | 33459 | struct PCache1 { |
| 33415 | 33460 | /* Cache configuration parameters. Page size (szPage) and the purgeable |
| 33416 | 33461 | ** flag (bPurgeable) are set when the cache is created. nMax may be |
| 33417 | 33462 | ** 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. |
| 33419 | 33464 | */ |
| 33465 | + PGroup *pGroup; /* PGroup this cache belongs to */ |
| 33420 | 33466 | int szPage; /* Size of allocated pages in bytes */ |
| 33421 | 33467 | int bPurgeable; /* True if cache is purgeable */ |
| 33422 | 33468 | unsigned int nMin; /* Minimum number of pages reserved */ |
| 33423 | 33469 | unsigned int nMax; /* Configured "cache_size" value */ |
| 33470 | + unsigned int mxPinned; /* nMax*9/10 */ |
| 33424 | 33471 | |
| 33425 | 33472 | /* 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. |
| 33428 | 33474 | */ |
| 33429 | 33475 | unsigned int nRecyclable; /* Number of pages in the LRU list */ |
| 33430 | 33476 | unsigned int nPage; /* Total number of pages in apHash */ |
| 33431 | 33477 | unsigned int nHash; /* Number of slots in apHash[] */ |
| 33432 | 33478 | PgHdr1 **apHash; /* Hash table for fast lookup by key */ |
| | @@ -33458,25 +33504,31 @@ |
| 33458 | 33504 | |
| 33459 | 33505 | /* |
| 33460 | 33506 | ** Global data used by this cache. |
| 33461 | 33507 | */ |
| 33462 | 33508 | 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 */ |
| 33478 | 33530 | } pcache1_g; |
| 33479 | 33531 | |
| 33480 | 33532 | /* |
| 33481 | 33533 | ** All code in this file should access the global structure above via the |
| 33482 | 33534 | ** alias "pcache1". This ensures that the WSD emulation is used when |
| | @@ -33498,23 +33550,26 @@ |
| 33498 | 33550 | */ |
| 33499 | 33551 | #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage) |
| 33500 | 33552 | #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage) |
| 33501 | 33553 | |
| 33502 | 33554 | /* |
| 33503 | | -** Macros to enter and leave the global LRU mutex. |
| 33555 | +** Macros to enter and leave the PCache LRU mutex. |
| 33504 | 33556 | */ |
| 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) |
| 33507 | 33559 | |
| 33508 | 33560 | /******************************************************************************/ |
| 33509 | 33561 | /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ |
| 33510 | 33562 | |
| 33511 | 33563 | /* |
| 33512 | 33564 | ** This function is called during initialization if a static buffer is |
| 33513 | 33565 | ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE |
| 33514 | 33566 | ** verb to sqlite3_config(). Parameter pBuf points to an allocation large |
| 33515 | 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. |
| 33516 | 33571 | */ |
| 33517 | 33572 | SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ |
| 33518 | 33573 | if( pcache1.isInit ){ |
| 33519 | 33574 | PgFreeslot *p; |
| 33520 | 33575 | sz = ROUNDDOWN8(sz); |
| | @@ -33521,10 +33576,11 @@ |
| 33521 | 33576 | pcache1.szSlot = sz; |
| 33522 | 33577 | pcache1.nSlot = pcache1.nFreeSlot = n; |
| 33523 | 33578 | pcache1.nReserve = n>90 ? 10 : (n/10 + 1); |
| 33524 | 33579 | pcache1.pStart = pBuf; |
| 33525 | 33580 | pcache1.pFree = 0; |
| 33581 | + pcache1.bUnderPressure = 0; |
| 33526 | 33582 | while( n-- ){ |
| 33527 | 33583 | p = (PgFreeslot*)pBuf; |
| 33528 | 33584 | p->pNext = pcache1.pFree; |
| 33529 | 33585 | pcache1.pFree = p; |
| 33530 | 33586 | pBuf = (void*)&((char*)pBuf)[sz]; |
| | @@ -33536,33 +33592,35 @@ |
| 33536 | 33592 | /* |
| 33537 | 33593 | ** Malloc function used within this file to allocate space from the buffer |
| 33538 | 33594 | ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no |
| 33539 | 33595 | ** such buffer exists or there is no space left in it, this function falls |
| 33540 | 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. |
| 33541 | 33600 | */ |
| 33542 | 33601 | 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) ); |
| 33545 | 33604 | 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); |
| 33548 | 33607 | 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. |
| 33560 | 33620 | */ |
| 33561 | | - pcache1LeaveMutex(); |
| 33562 | 33621 | p = sqlite3Malloc(nByte); |
| 33563 | | - pcache1EnterMutex(); |
| 33564 | 33622 | if( p ){ |
| 33565 | 33623 | int sz = sqlite3MallocSize(p); |
| 33566 | 33624 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 33567 | 33625 | } |
| 33568 | 33626 | sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| | @@ -33572,20 +33630,22 @@ |
| 33572 | 33630 | |
| 33573 | 33631 | /* |
| 33574 | 33632 | ** Free an allocated buffer obtained from pcache1Alloc(). |
| 33575 | 33633 | */ |
| 33576 | 33634 | static void pcache1Free(void *p){ |
| 33577 | | - assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33578 | 33635 | if( p==0 ) return; |
| 33579 | 33636 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 33580 | 33637 | PgFreeslot *pSlot; |
| 33638 | + sqlite3_mutex_enter(pcache1.mutex); |
| 33581 | 33639 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1); |
| 33582 | 33640 | pSlot = (PgFreeslot*)p; |
| 33583 | 33641 | pSlot->pNext = pcache1.pFree; |
| 33584 | 33642 | pcache1.pFree = pSlot; |
| 33585 | 33643 | pcache1.nFreeSlot++; |
| 33644 | + pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| 33586 | 33645 | assert( pcache1.nFreeSlot<=pcache1.nSlot ); |
| 33646 | + sqlite3_mutex_leave(pcache1.mutex); |
| 33587 | 33647 | }else{ |
| 33588 | 33648 | int iSize; |
| 33589 | 33649 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 33590 | 33650 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 33591 | 33651 | iSize = sqlite3MallocSize(p); |
| | @@ -33597,11 +33657,10 @@ |
| 33597 | 33657 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 33598 | 33658 | /* |
| 33599 | 33659 | ** Return the size of a pcache allocation |
| 33600 | 33660 | */ |
| 33601 | 33661 | static int pcache1MemSize(void *p){ |
| 33602 | | - assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33603 | 33662 | if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 33604 | 33663 | return pcache1.szSlot; |
| 33605 | 33664 | }else{ |
| 33606 | 33665 | int iSize; |
| 33607 | 33666 | assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| | @@ -33621,11 +33680,11 @@ |
| 33621 | 33680 | void *pPg = pcache1Alloc(nByte); |
| 33622 | 33681 | PgHdr1 *p; |
| 33623 | 33682 | if( pPg ){ |
| 33624 | 33683 | p = PAGE_TO_PGHDR1(pCache, pPg); |
| 33625 | 33684 | if( pCache->bPurgeable ){ |
| 33626 | | - pcache1.nCurrentPage++; |
| 33685 | + pCache->pGroup->nCurrentPage++; |
| 33627 | 33686 | } |
| 33628 | 33687 | }else{ |
| 33629 | 33688 | p = 0; |
| 33630 | 33689 | } |
| 33631 | 33690 | return p; |
| | @@ -33638,12 +33697,13 @@ |
| 33638 | 33697 | ** that the current implementation happens to never call this routine |
| 33639 | 33698 | ** with a NULL pointer, so we mark the NULL test with ALWAYS(). |
| 33640 | 33699 | */ |
| 33641 | 33700 | static void pcache1FreePage(PgHdr1 *p){ |
| 33642 | 33701 | if( ALWAYS(p) ){ |
| 33643 | | - if( p->pCache->bPurgeable ){ |
| 33644 | | - pcache1.nCurrentPage--; |
| 33702 | + PCache1 *pCache = p->pCache; |
| 33703 | + if( pCache->bPurgeable ){ |
| 33704 | + pCache->pGroup->nCurrentPage--; |
| 33645 | 33705 | } |
| 33646 | 33706 | pcache1Free(PGHDR1_TO_PAGE(p)); |
| 33647 | 33707 | } |
| 33648 | 33708 | } |
| 33649 | 33709 | |
| | @@ -33651,24 +33711,18 @@ |
| 33651 | 33711 | ** Malloc function used by SQLite to obtain space from the buffer configured |
| 33652 | 33712 | ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer |
| 33653 | 33713 | ** exists, this function falls back to sqlite3Malloc(). |
| 33654 | 33714 | */ |
| 33655 | 33715 | SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){ |
| 33656 | | - void *p; |
| 33657 | | - pcache1EnterMutex(); |
| 33658 | | - p = pcache1Alloc(sz); |
| 33659 | | - pcache1LeaveMutex(); |
| 33660 | | - return p; |
| 33716 | + return pcache1Alloc(sz); |
| 33661 | 33717 | } |
| 33662 | 33718 | |
| 33663 | 33719 | /* |
| 33664 | 33720 | ** Free an allocated buffer obtained from sqlite3PageMalloc(). |
| 33665 | 33721 | */ |
| 33666 | 33722 | SQLITE_PRIVATE void sqlite3PageFree(void *p){ |
| 33667 | | - pcache1EnterMutex(); |
| 33668 | 33723 | pcache1Free(p); |
| 33669 | | - pcache1LeaveMutex(); |
| 33670 | 33724 | } |
| 33671 | 33725 | |
| 33672 | 33726 | |
| 33673 | 33727 | /* |
| 33674 | 33728 | ** Return true if it desirable to avoid allocating a new page cache |
| | @@ -33685,13 +33739,12 @@ |
| 33685 | 33739 | ** under memory pressure, then again it is desirable to avoid |
| 33686 | 33740 | ** allocating a new page cache entry in order to avoid stressing |
| 33687 | 33741 | ** the heap even further. |
| 33688 | 33742 | */ |
| 33689 | 33743 | static int pcache1UnderMemoryPressure(PCache1 *pCache){ |
| 33690 | | - assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33691 | 33744 | if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){ |
| 33692 | | - return pcache1.nFreeSlot<pcache1.nReserve; |
| 33745 | + return pcache1.bUnderPressure; |
| 33693 | 33746 | }else{ |
| 33694 | 33747 | return sqlite3HeapNearlyFull(); |
| 33695 | 33748 | } |
| 33696 | 33749 | } |
| 33697 | 33750 | |
| | @@ -33700,29 +33753,29 @@ |
| 33700 | 33753 | |
| 33701 | 33754 | /* |
| 33702 | 33755 | ** This function is used to resize the hash table used by the cache passed |
| 33703 | 33756 | ** as the first argument. |
| 33704 | 33757 | ** |
| 33705 | | -** The global mutex must be held when this function is called. |
| 33758 | +** The PCache mutex must be held when this function is called. |
| 33706 | 33759 | */ |
| 33707 | 33760 | static int pcache1ResizeHash(PCache1 *p){ |
| 33708 | 33761 | PgHdr1 **apNew; |
| 33709 | 33762 | unsigned int nNew; |
| 33710 | 33763 | unsigned int i; |
| 33711 | 33764 | |
| 33712 | | - assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33765 | + assert( sqlite3_mutex_held(p->pGroup->mutex) ); |
| 33713 | 33766 | |
| 33714 | 33767 | nNew = p->nHash*2; |
| 33715 | 33768 | if( nNew<256 ){ |
| 33716 | 33769 | nNew = 256; |
| 33717 | 33770 | } |
| 33718 | 33771 | |
| 33719 | | - pcache1LeaveMutex(); |
| 33772 | + pcache1LeaveMutex(p->pGroup); |
| 33720 | 33773 | if( p->nHash ){ sqlite3BeginBenignMalloc(); } |
| 33721 | 33774 | apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew); |
| 33722 | 33775 | if( p->nHash ){ sqlite3EndBenignMalloc(); } |
| 33723 | | - pcache1EnterMutex(); |
| 33776 | + pcache1EnterMutex(p->pGroup); |
| 33724 | 33777 | if( apNew ){ |
| 33725 | 33778 | memset(apNew, 0, sizeof(PgHdr1 *)*nNew); |
| 33726 | 33779 | for(i=0; i<p->nHash; i++){ |
| 33727 | 33780 | PgHdr1 *pPage; |
| 33728 | 33781 | PgHdr1 *pNext = p->apHash[i]; |
| | @@ -33741,29 +33794,37 @@ |
| 33741 | 33794 | return (p->apHash ? SQLITE_OK : SQLITE_NOMEM); |
| 33742 | 33795 | } |
| 33743 | 33796 | |
| 33744 | 33797 | /* |
| 33745 | 33798 | ** 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 |
| 33747 | 33800 | ** LRU list, then this function is a no-op. |
| 33748 | 33801 | ** |
| 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. |
| 33750 | 33805 | */ |
| 33751 | 33806 | 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 ){ |
| 33754 | 33815 | if( pPage->pLruPrev ){ |
| 33755 | 33816 | pPage->pLruPrev->pLruNext = pPage->pLruNext; |
| 33756 | 33817 | } |
| 33757 | 33818 | if( pPage->pLruNext ){ |
| 33758 | 33819 | pPage->pLruNext->pLruPrev = pPage->pLruPrev; |
| 33759 | 33820 | } |
| 33760 | | - if( pcache1.pLruHead==pPage ){ |
| 33761 | | - pcache1.pLruHead = pPage->pLruNext; |
| 33821 | + if( pGroup->pLruHead==pPage ){ |
| 33822 | + pGroup->pLruHead = pPage->pLruNext; |
| 33762 | 33823 | } |
| 33763 | | - if( pcache1.pLruTail==pPage ){ |
| 33764 | | - pcache1.pLruTail = pPage->pLruPrev; |
| 33824 | + if( pGroup->pLruTail==pPage ){ |
| 33825 | + pGroup->pLruTail = pPage->pLruPrev; |
| 33765 | 33826 | } |
| 33766 | 33827 | pPage->pLruNext = 0; |
| 33767 | 33828 | pPage->pLruPrev = 0; |
| 33768 | 33829 | pPage->pCache->nRecyclable--; |
| 33769 | 33830 | } |
| | @@ -33772,32 +33833,34 @@ |
| 33772 | 33833 | |
| 33773 | 33834 | /* |
| 33774 | 33835 | ** Remove the page supplied as an argument from the hash table |
| 33775 | 33836 | ** (PCache1.apHash structure) that it is currently stored in. |
| 33776 | 33837 | ** |
| 33777 | | -** The global mutex must be held when this function is called. |
| 33838 | +** The PGroup mutex must be held when this function is called. |
| 33778 | 33839 | */ |
| 33779 | 33840 | static void pcache1RemoveFromHash(PgHdr1 *pPage){ |
| 33780 | 33841 | unsigned int h; |
| 33781 | 33842 | PCache1 *pCache = pPage->pCache; |
| 33782 | 33843 | PgHdr1 **pp; |
| 33783 | 33844 | |
| 33845 | + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 33784 | 33846 | h = pPage->iKey % pCache->nHash; |
| 33785 | 33847 | for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext); |
| 33786 | 33848 | *pp = (*pp)->pNext; |
| 33787 | 33849 | |
| 33788 | 33850 | pCache->nPage--; |
| 33789 | 33851 | } |
| 33790 | 33852 | |
| 33791 | 33853 | /* |
| 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. |
| 33794 | 33856 | */ |
| 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 ); |
| 33799 | 33862 | pcache1PinPage(p); |
| 33800 | 33863 | pcache1RemoveFromHash(p); |
| 33801 | 33864 | pcache1FreePage(p); |
| 33802 | 33865 | } |
| 33803 | 33866 | } |
| | @@ -33805,19 +33868,19 @@ |
| 33805 | 33868 | /* |
| 33806 | 33869 | ** Discard all pages from cache pCache with a page number (key value) |
| 33807 | 33870 | ** greater than or equal to iLimit. Any pinned pages that meet this |
| 33808 | 33871 | ** criteria are unpinned before they are discarded. |
| 33809 | 33872 | ** |
| 33810 | | -** The global mutex must be held when this function is called. |
| 33873 | +** The PCache mutex must be held when this function is called. |
| 33811 | 33874 | */ |
| 33812 | 33875 | 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 */ |
| 33815 | 33878 | ){ |
| 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 */ |
| 33817 | 33880 | unsigned int h; |
| 33818 | | - assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33881 | + assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 33819 | 33882 | for(h=0; h<pCache->nHash; h++){ |
| 33820 | 33883 | PgHdr1 **pp = &pCache->apHash[h]; |
| 33821 | 33884 | PgHdr1 *pPage; |
| 33822 | 33885 | while( (pPage = *pp)!=0 ){ |
| 33823 | 33886 | if( pPage->iKey>=iLimit ){ |
| | @@ -33843,12 +33906,14 @@ |
| 33843 | 33906 | static int pcache1Init(void *NotUsed){ |
| 33844 | 33907 | UNUSED_PARAMETER(NotUsed); |
| 33845 | 33908 | assert( pcache1.isInit==0 ); |
| 33846 | 33909 | memset(&pcache1, 0, sizeof(pcache1)); |
| 33847 | 33910 | 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); |
| 33849 | 33913 | } |
| 33914 | + pcache1.grp.mxPinned = 10; |
| 33850 | 33915 | pcache1.isInit = 1; |
| 33851 | 33916 | return SQLITE_OK; |
| 33852 | 33917 | } |
| 33853 | 33918 | |
| 33854 | 33919 | /* |
| | @@ -33866,22 +33931,51 @@ |
| 33866 | 33931 | ** Implementation of the sqlite3_pcache.xCreate method. |
| 33867 | 33932 | ** |
| 33868 | 33933 | ** Allocate a new cache. |
| 33869 | 33934 | */ |
| 33870 | 33935 | 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 */ |
| 33872 | 33939 | |
| 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); |
| 33874 | 33960 | 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; |
| 33876 | 33969 | pCache->szPage = szPage; |
| 33877 | 33970 | pCache->bPurgeable = (bPurgeable ? 1 : 0); |
| 33878 | 33971 | if( bPurgeable ){ |
| 33879 | 33972 | 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); |
| 33883 | 33977 | } |
| 33884 | 33978 | } |
| 33885 | 33979 | return (sqlite3_pcache *)pCache; |
| 33886 | 33980 | } |
| 33887 | 33981 | |
| | @@ -33891,26 +33985,30 @@ |
| 33891 | 33985 | ** Configure the cache_size limit for a cache. |
| 33892 | 33986 | */ |
| 33893 | 33987 | static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ |
| 33894 | 33988 | PCache1 *pCache = (PCache1 *)p; |
| 33895 | 33989 | 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; |
| 33898 | 33994 | pCache->nMax = nMax; |
| 33899 | | - pcache1EnforceMaxPage(); |
| 33900 | | - pcache1LeaveMutex(); |
| 33995 | + pCache->mxPinned = nMax*9/10; |
| 33996 | + pcache1EnforceMaxPage(pGroup); |
| 33997 | + pcache1LeaveMutex(pGroup); |
| 33901 | 33998 | } |
| 33902 | 33999 | } |
| 33903 | 34000 | |
| 33904 | 34001 | /* |
| 33905 | 34002 | ** Implementation of the sqlite3_pcache.xPagecount method. |
| 33906 | 34003 | */ |
| 33907 | 34004 | static int pcache1Pagecount(sqlite3_pcache *p){ |
| 33908 | 34005 | 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); |
| 33912 | 34010 | return n; |
| 33913 | 34011 | } |
| 33914 | 34012 | |
| 33915 | 34013 | /* |
| 33916 | 34014 | ** Implementation of the sqlite3_pcache.xFetch method. |
| | @@ -33965,65 +34063,89 @@ |
| 33965 | 34063 | ** proceed to step 5. |
| 33966 | 34064 | ** |
| 33967 | 34065 | ** 5. Otherwise, allocate and return a new page buffer. |
| 33968 | 34066 | */ |
| 33969 | 34067 | static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){ |
| 33970 | | - unsigned int nPinned; |
| 34068 | + int nPinned; |
| 33971 | 34069 | PCache1 *pCache = (PCache1 *)p; |
| 34070 | + PGroup *pGroup; |
| 33972 | 34071 | PgHdr1 *pPage = 0; |
| 33973 | 34072 | |
| 33974 | 34073 | 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); |
| 33977 | 34078 | |
| 33978 | | - /* Search the hash table for an existing entry. */ |
| 34079 | + /* Step 1: Search the hash table for an existing entry. */ |
| 33979 | 34080 | if( pCache->nHash>0 ){ |
| 33980 | 34081 | unsigned int h = iKey % pCache->nHash; |
| 33981 | 34082 | for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext); |
| 33982 | 34083 | } |
| 33983 | 34084 | |
| 34085 | + /* Step 2: Abort if no existing page is found and createFlag is 0 */ |
| 33984 | 34086 | if( pPage || createFlag==0 ){ |
| 33985 | 34087 | pcache1PinPage(pPage); |
| 33986 | 34088 | goto fetch_out; |
| 33987 | 34089 | } |
| 33988 | 34090 | |
| 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 */ |
| 33990 | 34104 | nPinned = pCache->nPage - pCache->nRecyclable; |
| 34105 | + assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage ); |
| 34106 | + assert( pCache->mxPinned == pCache->nMax*9/10 ); |
| 33991 | 34107 | 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 |
| 33994 | 34110 | || pcache1UnderMemoryPressure(pCache) |
| 33995 | 34111 | )){ |
| 33996 | 34112 | goto fetch_out; |
| 33997 | 34113 | } |
| 33998 | 34114 | |
| 33999 | 34115 | if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){ |
| 34000 | 34116 | goto fetch_out; |
| 34001 | 34117 | } |
| 34002 | 34118 | |
| 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 && ( |
| 34005 | 34121 | (pCache->nPage+1>=pCache->nMax) |
| 34006 | | - || pcache1.nCurrentPage>=pcache1.nMaxPage |
| 34122 | + || pGroup->nCurrentPage>=pGroup->nMaxPage |
| 34007 | 34123 | || pcache1UnderMemoryPressure(pCache) |
| 34008 | 34124 | )){ |
| 34009 | | - pPage = pcache1.pLruTail; |
| 34125 | + PCache1 *pOtherCache; |
| 34126 | + pPage = pGroup->pLruTail; |
| 34010 | 34127 | pcache1RemoveFromHash(pPage); |
| 34011 | 34128 | pcache1PinPage(pPage); |
| 34012 | | - if( pPage->pCache->szPage!=pCache->szPage ){ |
| 34129 | + if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){ |
| 34013 | 34130 | pcache1FreePage(pPage); |
| 34014 | 34131 | pPage = 0; |
| 34015 | 34132 | }else{ |
| 34016 | | - pcache1.nCurrentPage -= (pPage->pCache->bPurgeable - pCache->bPurgeable); |
| 34133 | + pGroup->nCurrentPage -= |
| 34134 | + (pOtherCache->bPurgeable - pCache->bPurgeable); |
| 34017 | 34135 | } |
| 34018 | 34136 | } |
| 34019 | 34137 | |
| 34020 | 34138 | /* Step 5. If a usable page buffer has still not been found, |
| 34021 | 34139 | ** attempt to allocate a new one. |
| 34022 | 34140 | */ |
| 34023 | 34141 | if( !pPage ){ |
| 34142 | + if( createFlag==1 ) sqlite3BeginBenignMalloc(); |
| 34143 | + pcache1LeaveMutex(pGroup); |
| 34024 | 34144 | pPage = pcache1AllocPage(pCache); |
| 34145 | + pcache1EnterMutex(pGroup); |
| 34146 | + if( createFlag==1 ) sqlite3EndBenignMalloc(); |
| 34025 | 34147 | } |
| 34026 | 34148 | |
| 34027 | 34149 | if( pPage ){ |
| 34028 | 34150 | unsigned int h = iKey % pCache->nHash; |
| 34029 | 34151 | pCache->nPage++; |
| | @@ -34038,12 +34160,11 @@ |
| 34038 | 34160 | |
| 34039 | 34161 | fetch_out: |
| 34040 | 34162 | if( pPage && iKey>pCache->iMaxKey ){ |
| 34041 | 34163 | pCache->iMaxKey = iKey; |
| 34042 | 34164 | } |
| 34043 | | - if( createFlag==1 ) sqlite3EndBenignMalloc(); |
| 34044 | | - pcache1LeaveMutex(); |
| 34165 | + pcache1LeaveMutex(pGroup); |
| 34045 | 34166 | return (pPage ? PGHDR1_TO_PAGE(pPage) : 0); |
| 34046 | 34167 | } |
| 34047 | 34168 | |
| 34048 | 34169 | |
| 34049 | 34170 | /* |
| | @@ -34052,37 +34173,38 @@ |
| 34052 | 34173 | ** Mark a page as unpinned (eligible for asynchronous recycling). |
| 34053 | 34174 | */ |
| 34054 | 34175 | static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){ |
| 34055 | 34176 | PCache1 *pCache = (PCache1 *)p; |
| 34056 | 34177 | PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg); |
| 34178 | + PGroup *pGroup = pCache->pGroup; |
| 34057 | 34179 | |
| 34058 | 34180 | assert( pPage->pCache==pCache ); |
| 34059 | | - pcache1EnterMutex(); |
| 34181 | + pcache1EnterMutex(pGroup); |
| 34060 | 34182 | |
| 34061 | 34183 | /* 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. |
| 34063 | 34185 | */ |
| 34064 | 34186 | assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); |
| 34065 | | - assert( pcache1.pLruHead!=pPage && pcache1.pLruTail!=pPage ); |
| 34187 | + assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage ); |
| 34066 | 34188 | |
| 34067 | | - if( reuseUnlikely || pcache1.nCurrentPage>pcache1.nMaxPage ){ |
| 34189 | + if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ |
| 34068 | 34190 | pcache1RemoveFromHash(pPage); |
| 34069 | 34191 | pcache1FreePage(pPage); |
| 34070 | 34192 | }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; |
| 34076 | 34198 | }else{ |
| 34077 | | - pcache1.pLruTail = pPage; |
| 34078 | | - pcache1.pLruHead = pPage; |
| 34199 | + pGroup->pLruTail = pPage; |
| 34200 | + pGroup->pLruHead = pPage; |
| 34079 | 34201 | } |
| 34080 | 34202 | pCache->nRecyclable++; |
| 34081 | 34203 | } |
| 34082 | 34204 | |
| 34083 | | - pcache1LeaveMutex(); |
| 34205 | + pcache1LeaveMutex(pCache->pGroup); |
| 34084 | 34206 | } |
| 34085 | 34207 | |
| 34086 | 34208 | /* |
| 34087 | 34209 | ** Implementation of the sqlite3_pcache.xRekey method. |
| 34088 | 34210 | */ |
| | @@ -34097,11 +34219,11 @@ |
| 34097 | 34219 | PgHdr1 **pp; |
| 34098 | 34220 | unsigned int h; |
| 34099 | 34221 | assert( pPage->iKey==iOld ); |
| 34100 | 34222 | assert( pPage->pCache==pCache ); |
| 34101 | 34223 | |
| 34102 | | - pcache1EnterMutex(); |
| 34224 | + pcache1EnterMutex(pCache->pGroup); |
| 34103 | 34225 | |
| 34104 | 34226 | h = iOld%pCache->nHash; |
| 34105 | 34227 | pp = &pCache->apHash[h]; |
| 34106 | 34228 | while( (*pp)!=pPage ){ |
| 34107 | 34229 | pp = &(*pp)->pNext; |
| | @@ -34114,11 +34236,11 @@ |
| 34114 | 34236 | pCache->apHash[h] = pPage; |
| 34115 | 34237 | if( iNew>pCache->iMaxKey ){ |
| 34116 | 34238 | pCache->iMaxKey = iNew; |
| 34117 | 34239 | } |
| 34118 | 34240 | |
| 34119 | | - pcache1LeaveMutex(); |
| 34241 | + pcache1LeaveMutex(pCache->pGroup); |
| 34120 | 34242 | } |
| 34121 | 34243 | |
| 34122 | 34244 | /* |
| 34123 | 34245 | ** Implementation of the sqlite3_pcache.xTruncate method. |
| 34124 | 34246 | ** |
| | @@ -34126,32 +34248,34 @@ |
| 34126 | 34248 | ** or greater than parameter iLimit. Any pinned pages with a page number |
| 34127 | 34249 | ** equal to or greater than iLimit are implicitly unpinned. |
| 34128 | 34250 | */ |
| 34129 | 34251 | static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){ |
| 34130 | 34252 | PCache1 *pCache = (PCache1 *)p; |
| 34131 | | - pcache1EnterMutex(); |
| 34253 | + pcache1EnterMutex(pCache->pGroup); |
| 34132 | 34254 | if( iLimit<=pCache->iMaxKey ){ |
| 34133 | 34255 | pcache1TruncateUnsafe(pCache, iLimit); |
| 34134 | 34256 | pCache->iMaxKey = iLimit-1; |
| 34135 | 34257 | } |
| 34136 | | - pcache1LeaveMutex(); |
| 34258 | + pcache1LeaveMutex(pCache->pGroup); |
| 34137 | 34259 | } |
| 34138 | 34260 | |
| 34139 | 34261 | /* |
| 34140 | 34262 | ** Implementation of the sqlite3_pcache.xDestroy method. |
| 34141 | 34263 | ** |
| 34142 | 34264 | ** Destroy a cache allocated using pcache1Create(). |
| 34143 | 34265 | */ |
| 34144 | 34266 | static void pcache1Destroy(sqlite3_pcache *p){ |
| 34145 | 34267 | PCache1 *pCache = (PCache1 *)p; |
| 34268 | + PGroup *pGroup = pCache->pGroup; |
| 34146 | 34269 | assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); |
| 34147 | | - pcache1EnterMutex(); |
| 34270 | + pcache1EnterMutex(pGroup); |
| 34148 | 34271 | 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); |
| 34153 | 34277 | sqlite3_free(pCache->apHash); |
| 34154 | 34278 | sqlite3_free(pCache); |
| 34155 | 34279 | } |
| 34156 | 34280 | |
| 34157 | 34281 | /* |
| | @@ -34186,20 +34310,22 @@ |
| 34186 | 34310 | ** been released, the function returns. The return value is the total number |
| 34187 | 34311 | ** of bytes of memory released. |
| 34188 | 34312 | */ |
| 34189 | 34313 | SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 34190 | 34314 | int nFree = 0; |
| 34315 | + assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 34316 | + assert( sqlite3_mutex_notheld(pcache1.mutex) ); |
| 34191 | 34317 | if( pcache1.pStart==0 ){ |
| 34192 | 34318 | 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) ){ |
| 34195 | 34321 | nFree += pcache1MemSize(PGHDR1_TO_PAGE(p)); |
| 34196 | 34322 | pcache1PinPage(p); |
| 34197 | 34323 | pcache1RemoveFromHash(p); |
| 34198 | 34324 | pcache1FreePage(p); |
| 34199 | 34325 | } |
| 34200 | | - pcache1LeaveMutex(); |
| 34326 | + pcache1LeaveMutex(&pcache1.grp); |
| 34201 | 34327 | } |
| 34202 | 34328 | return nFree; |
| 34203 | 34329 | } |
| 34204 | 34330 | #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 34205 | 34331 | |
| | @@ -34214,16 +34340,16 @@ |
| 34214 | 34340 | int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */ |
| 34215 | 34341 | int *pnRecyclable /* OUT: Total number of pages available for recycling */ |
| 34216 | 34342 | ){ |
| 34217 | 34343 | PgHdr1 *p; |
| 34218 | 34344 | int nRecyclable = 0; |
| 34219 | | - for(p=pcache1.pLruHead; p; p=p->pLruNext){ |
| 34345 | + for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){ |
| 34220 | 34346 | nRecyclable++; |
| 34221 | 34347 | } |
| 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; |
| 34225 | 34351 | *pnRecyclable = nRecyclable; |
| 34226 | 34352 | } |
| 34227 | 34353 | #endif |
| 34228 | 34354 | |
| 34229 | 34355 | /************** End of pcache1.c *********************************************/ |
| | @@ -40390,19 +40516,17 @@ |
| 40390 | 40516 | ** |
| 40391 | 40517 | ** If successful, or if called on a pager for which it is a no-op, this |
| 40392 | 40518 | ** function returns SQLITE_OK. Otherwise, an IO error code is returned. |
| 40393 | 40519 | */ |
| 40394 | 40520 | 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 ); |
| 40400 | 40524 | 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); |
| 40404 | 40528 | } |
| 40405 | 40529 | return rc; |
| 40406 | 40530 | } |
| 40407 | 40531 | |
| 40408 | 40532 | /* |
| | @@ -46264,18 +46388,13 @@ |
| 46264 | 46388 | testcase( nPayload==pPage->maxLocal+1 ); |
| 46265 | 46389 | if( likely(nPayload<=pPage->maxLocal) ){ |
| 46266 | 46390 | /* This is the (easy) common case where the entire payload fits |
| 46267 | 46391 | ** on the local page. No overflow is required. |
| 46268 | 46392 | */ |
| 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; |
| 46271 | 46394 | pInfo->nLocal = (u16)nPayload; |
| 46272 | 46395 | pInfo->iOverflow = 0; |
| 46273 | | - if( (nSize & ~3)==0 ){ |
| 46274 | | - nSize = 4; /* Minimum cell size is 4 */ |
| 46275 | | - } |
| 46276 | | - pInfo->nSize = (u16)nSize; |
| 46277 | 46396 | }else{ |
| 46278 | 46397 | /* If the payload will not fit completely on the local page, we have |
| 46279 | 46398 | ** to decide how much to store locally and how much to spill onto |
| 46280 | 46399 | ** overflow pages. The strategy is to minimize the amount of unused |
| 46281 | 46400 | ** space on overflow pages while keeping the amount of local storage |
| | @@ -53936,10 +54055,20 @@ |
| 53936 | 54055 | assert( pFile ); |
| 53937 | 54056 | assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || ( |
| 53938 | 54057 | nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1) |
| 53939 | 54058 | && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest |
| 53940 | 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. */ |
| 53941 | 54070 | iEnd = MIN(PENDING_BYTE + pgszDest, iSize); |
| 53942 | 54071 | for( |
| 53943 | 54072 | iOff=PENDING_BYTE+pgszSrc; |
| 53944 | 54073 | rc==SQLITE_OK && iOff<iEnd; |
| 53945 | 54074 | iOff+=pgszSrc |
| | @@ -53952,14 +54081,16 @@ |
| 53952 | 54081 | rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff); |
| 53953 | 54082 | } |
| 53954 | 54083 | sqlite3PagerUnref(pSrcPg); |
| 53955 | 54084 | } |
| 53956 | 54085 | 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); |
| 53961 | 54092 | } |
| 53962 | 54093 | }else{ |
| 53963 | 54094 | rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0); |
| 53964 | 54095 | } |
| 53965 | 54096 | |
| | @@ -58815,15 +58946,34 @@ |
| 58815 | 58946 | int rc; |
| 58816 | 58947 | |
| 58817 | 58948 | assert(p); |
| 58818 | 58949 | if( p->magic!=VDBE_MAGIC_RUN ){ |
| 58819 | 58950 | /* 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. |
| 58823 | 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 |
| 58824 | 58973 | sqlite3_reset((sqlite3_stmt*)p); |
| 58974 | +#endif |
| 58825 | 58975 | } |
| 58826 | 58976 | |
| 58827 | 58977 | /* Check that malloc() has not failed. If it has, return early. */ |
| 58828 | 58978 | db = p->db; |
| 58829 | 58979 | if( db->mallocFailed ){ |
| | @@ -58861,11 +59011,13 @@ |
| 58861 | 59011 | if( p->explain ){ |
| 58862 | 59012 | rc = sqlite3VdbeList(p); |
| 58863 | 59013 | }else |
| 58864 | 59014 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 58865 | 59015 | { |
| 59016 | + db->vdbeExecCnt++; |
| 58866 | 59017 | rc = sqlite3VdbeExec(p); |
| 59018 | + db->vdbeExecCnt--; |
| 58867 | 59019 | } |
| 58868 | 59020 | |
| 58869 | 59021 | #ifndef SQLITE_OMIT_TRACE |
| 58870 | 59022 | /* Invoke the profile callback if there is one |
| 58871 | 59023 | */ |
| | @@ -59818,13 +59970,16 @@ |
| 59818 | 59970 | } |
| 59819 | 59971 | return nTotal; |
| 59820 | 59972 | } |
| 59821 | 59973 | |
| 59822 | 59974 | /* |
| 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. |
| 59826 | 59981 | ** |
| 59827 | 59982 | ** The calling function is responsible for making sure the memory returned |
| 59828 | 59983 | ** is eventually freed. |
| 59829 | 59984 | ** |
| 59830 | 59985 | ** ALGORITHM: Scan the input string looking for host parameters in any of |
| | @@ -59851,67 +60006,76 @@ |
| 59851 | 60006 | |
| 59852 | 60007 | db = p->db; |
| 59853 | 60008 | sqlite3StrAccumInit(&out, zBase, sizeof(zBase), |
| 59854 | 60009 | db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 59855 | 60010 | 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 ){ |
| 59889 | 60052 | #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 |
| 59900 | 60063 | #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 | + } |
| 59913 | 60077 | } |
| 59914 | 60078 | } |
| 59915 | 60079 | return sqlite3StrAccumFinish(&out); |
| 59916 | 60080 | } |
| 59917 | 60081 | |
| | @@ -73655,15 +73819,15 @@ |
| 73655 | 73819 | ** out-of-bounds write if SQLITE_DEBUG is not defined). */ |
| 73656 | 73820 | return; |
| 73657 | 73821 | } |
| 73658 | 73822 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); |
| 73659 | 73823 | for(i=0; i<nCol; i++){ |
| 73660 | | - int addr = sqlite3VdbeCurrentAddr(v) - (nCol*2); |
| 73824 | + int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2); |
| 73661 | 73825 | 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 */ |
| 73663 | 73827 | } |
| 73664 | | - sqlite3VdbeJumpHere(v, addr); /* Set jump dest for the OP_Ne */ |
| 73828 | + sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */ |
| 73665 | 73829 | sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); |
| 73666 | 73830 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); |
| 73667 | 73831 | } |
| 73668 | 73832 | |
| 73669 | 73833 | /* End of the analysis loop. */ |
| | @@ -87777,11 +87941,11 @@ |
| 87777 | 87941 | /* |
| 87778 | 87942 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 87779 | 87943 | */ |
| 87780 | 87944 | static int sqlite3Prepare16( |
| 87781 | 87945 | sqlite3 *db, /* Database handle. */ |
| 87782 | | - const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 87946 | + const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 87783 | 87947 | int nBytes, /* Length of zSql in bytes. */ |
| 87784 | 87948 | int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ |
| 87785 | 87949 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 87786 | 87950 | const void **pzTail /* OUT: End of parsed string */ |
| 87787 | 87951 | ){ |
| | @@ -87827,11 +87991,11 @@ |
| 87827 | 87991 | ** and the statement is automatically recompiled if an schema change |
| 87828 | 87992 | ** occurs. |
| 87829 | 87993 | */ |
| 87830 | 87994 | SQLITE_API int sqlite3_prepare16( |
| 87831 | 87995 | sqlite3 *db, /* Database handle. */ |
| 87832 | | - const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 87996 | + const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 87833 | 87997 | int nBytes, /* Length of zSql in bytes. */ |
| 87834 | 87998 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 87835 | 87999 | const void **pzTail /* OUT: End of parsed string */ |
| 87836 | 88000 | ){ |
| 87837 | 88001 | int rc; |
| | @@ -87839,11 +88003,11 @@ |
| 87839 | 88003 | assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ |
| 87840 | 88004 | return rc; |
| 87841 | 88005 | } |
| 87842 | 88006 | SQLITE_API int sqlite3_prepare16_v2( |
| 87843 | 88007 | sqlite3 *db, /* Database handle. */ |
| 87844 | | - const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 88008 | + const void *zSql, /* UTF-16 encoded SQL statement. */ |
| 87845 | 88009 | int nBytes, /* Length of zSql in bytes. */ |
| 87846 | 88010 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 87847 | 88011 | const void **pzTail /* OUT: End of parsed string */ |
| 87848 | 88012 | ){ |
| 87849 | 88013 | int rc; |
| 87850 | 88014 | |