1 /* 2 ** 2008 August 05 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file implements that page cache. 13 */ 14 #include "sqliteInt.h" 15 16 /* 17 ** A complete page cache is an instance of this structure. 18 */ 19 struct PCache { 20 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ 21 PgHdr *pSynced; /* Last synced page in dirty page list */ 22 int nRef; /* Number of referenced pages */ 23 int szCache; /* Configured cache size */ 24 int szPage; /* Size of every page in this cache */ 25 int szExtra; /* Size of extra space for each page */ 26 u8 bPurgeable; /* True if pages are on backing store */ 27 u8 eCreate; /* eCreate value for for xFetch() */ 28 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ 29 void *pStress; /* Argument to xStress */ 30 sqlite3_pcache *pCache; /* Pluggable cache module */ 31 PgHdr *pPage1; /* Reference to page 1 */ 32 }; 33 34 /* 35 ** Some of the assert() macros in this code are too expensive to run 36 ** even during normal debugging. Use them only rarely on long-running 37 ** tests. Enable the expensive asserts using the 38 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option. 39 */ 40 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT 41 # define expensive_assert(X) assert(X) 42 #else 43 # define expensive_assert(X) 44 #endif 45 46 /********************************** Linked List Management ********************/ 47 48 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT) 49 /* 50 ** Check that the pCache->pSynced variable is set correctly. If it 51 ** is not, either fail an assert or return zero. Otherwise, return 52 ** non-zero. This is only used in debugging builds, as follows: 53 ** 54 ** expensive_assert( pcacheCheckSynced(pCache) ); 55 */ 56 static int pcacheCheckSynced(PCache *pCache){ 57 PgHdr *p; 58 for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){ 59 assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) ); 60 } 61 return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0); 62 } 63 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */ 64 65 /* Allowed values for second argument to pcacheManageDirtyList() */ 66 #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */ 67 #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */ 68 #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */ 69 70 /* 71 ** Manage pPage's participation on the dirty list. Bits of the addRemove 72 ** argument determines what operation to do. The 0x01 bit means first 73 ** remove pPage from the dirty list. The 0x02 means add pPage back to 74 ** the dirty list. Doing both moves pPage to the front of the dirty list. 75 */ 76 static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ 77 PCache *p = pPage->pCache; 78 79 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){ 80 assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); 81 assert( pPage->pDirtyPrev || pPage==p->pDirty ); 82 83 /* Update the PCache1.pSynced variable if necessary. */ 84 if( p->pSynced==pPage ){ 85 PgHdr *pSynced = pPage->pDirtyPrev; 86 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){ 87 pSynced = pSynced->pDirtyPrev; 88 } 89 p->pSynced = pSynced; 90 } 91 92 if( pPage->pDirtyNext ){ 93 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev; 94 }else{ 95 assert( pPage==p->pDirtyTail ); 96 p->pDirtyTail = pPage->pDirtyPrev; 97 } 98 if( pPage->pDirtyPrev ){ 99 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; 100 }else{ 101 assert( pPage==p->pDirty ); 102 p->pDirty = pPage->pDirtyNext; 103 if( p->pDirty==0 && p->bPurgeable ){ 104 assert( p->eCreate==1 ); 105 p->eCreate = 2; 106 } 107 } 108 pPage->pDirtyNext = 0; 109 pPage->pDirtyPrev = 0; 110 expensive_assert( pcacheCheckSynced(p) ); 111 } 112 if( addRemove & PCACHE_DIRTYLIST_ADD ){ 113 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage ); 114 115 pPage->pDirtyNext = p->pDirty; 116 if( pPage->pDirtyNext ){ 117 assert( pPage->pDirtyNext->pDirtyPrev==0 ); 118 pPage->pDirtyNext->pDirtyPrev = pPage; 119 }else if( p->bPurgeable ){ 120 assert( p->eCreate==2 ); 121 p->eCreate = 1; 122 } 123 p->pDirty = pPage; 124 if( !p->pDirtyTail ){ 125 p->pDirtyTail = pPage; 126 } 127 if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){ 128 p->pSynced = pPage; 129 } 130 expensive_assert( pcacheCheckSynced(p) ); 131 } 132 } 133 134 /* 135 ** Wrapper around the pluggable caches xUnpin method. If the cache is 136 ** being used for an in-memory database, this function is a no-op. 137 */ 138 static void pcacheUnpin(PgHdr *p){ 139 if( p->pCache->bPurgeable ){ 140 if( p->pgno==1 ){ 141 p->pCache->pPage1 = 0; 142 } 143 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); 144 } 145 } 146 147 /* 148 ** Compute the number of pages of cache requested. 149 */ 150 static int numberOfCachePages(PCache *p){ 151 if( p->szCache>=0 ){ 152 return p->szCache; 153 }else{ 154 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); 155 } 156 } 157 158 /*************************************************** General Interfaces ****** 159 ** 160 ** Initialize and shutdown the page cache subsystem. Neither of these 161 ** functions are threadsafe. 162 */ 163 int sqlite3PcacheInitialize(void){ 164 if( sqlite3GlobalConfig.pcache2.xInit==0 ){ 165 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the 166 ** built-in default page cache is used instead of the application defined 167 ** page cache. */ 168 sqlite3PCacheSetDefault(); 169 } 170 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg); 171 } 172 void sqlite3PcacheShutdown(void){ 173 if( sqlite3GlobalConfig.pcache2.xShutdown ){ 174 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ 175 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg); 176 } 177 } 178 179 /* 180 ** Return the size in bytes of a PCache object. 181 */ 182 int sqlite3PcacheSize(void){ return sizeof(PCache); } 183 184 /* 185 ** Create a new PCache object. Storage space to hold the object 186 ** has already been allocated and is passed in as the p pointer. 187 ** The caller discovers how much space needs to be allocated by 188 ** calling sqlite3PcacheSize(). 189 */ 190 int sqlite3PcacheOpen( 191 int szPage, /* Size of every page */ 192 int szExtra, /* Extra space associated with each page */ 193 int bPurgeable, /* True if pages are on backing store */ 194 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */ 195 void *pStress, /* Argument to xStress */ 196 PCache *p /* Preallocated space for the PCache */ 197 ){ 198 memset(p, 0, sizeof(PCache)); 199 p->szPage = 1; 200 p->szExtra = szExtra; 201 p->bPurgeable = bPurgeable; 202 p->eCreate = 2; 203 p->xStress = xStress; 204 p->pStress = pStress; 205 p->szCache = 100; 206 return sqlite3PcacheSetPageSize(p, szPage); 207 } 208 209 /* 210 ** Change the page size for PCache object. The caller must ensure that there 211 ** are no outstanding page references when this function is called. 212 */ 213 int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ 214 assert( pCache->nRef==0 && pCache->pDirty==0 ); 215 if( pCache->szPage ){ 216 sqlite3_pcache *pNew; 217 pNew = sqlite3GlobalConfig.pcache2.xCreate( 218 szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable 219 ); 220 if( pNew==0 ) return SQLITE_NOMEM; 221 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); 222 if( pCache->pCache ){ 223 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); 224 } 225 pCache->pCache = pNew; 226 pCache->pPage1 = 0; 227 pCache->szPage = szPage; 228 } 229 return SQLITE_OK; 230 } 231 232 /* 233 ** Try to obtain a page from the cache. 234 ** 235 ** This routine returns a pointer to an sqlite3_pcache_page object if 236 ** such an object is already in cache, or if a new one is created. 237 ** This routine returns a NULL pointer if the object was not in cache 238 ** and could not be created. 239 ** 240 ** The createFlags should be 0 to check for existing pages and should 241 ** be 3 (not 1, but 3) to try to create a new page. 242 ** 243 ** If the createFlag is 0, then NULL is always returned if the page 244 ** is not already in the cache. If createFlag is 1, then a new page 245 ** is created only if that can be done without spilling dirty pages 246 ** and without exceeding the cache size limit. 247 ** 248 ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly 249 ** initialize the sqlite3_pcache_page object and convert it into a 250 ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish() 251 ** routines are split this way for performance reasons. When separated 252 ** they can both (usually) operate without having to push values to 253 ** the stack on entry and pop them back off on exit, which saves a 254 ** lot of pushing and popping. 255 */ 256 sqlite3_pcache_page *sqlite3PcacheFetch( 257 PCache *pCache, /* Obtain the page from this cache */ 258 Pgno pgno, /* Page number to obtain */ 259 int createFlag /* If true, create page if it does not exist already */ 260 ){ 261 int eCreate; 262 263 assert( pCache!=0 ); 264 assert( pCache->pCache!=0 ); 265 assert( createFlag==3 || createFlag==0 ); 266 assert( pgno>0 ); 267 268 /* eCreate defines what to do if the page does not exist. 269 ** 0 Do not allocate a new page. (createFlag==0) 270 ** 1 Allocate a new page if doing so is inexpensive. 271 ** (createFlag==1 AND bPurgeable AND pDirty) 272 ** 2 Allocate a new page even it doing so is difficult. 273 ** (createFlag==1 AND !(bPurgeable AND pDirty) 274 */ 275 eCreate = createFlag & pCache->eCreate; 276 assert( eCreate==0 || eCreate==1 || eCreate==2 ); 277 assert( createFlag==0 || pCache->eCreate==eCreate ); 278 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); 279 return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); 280 } 281 282 /* 283 ** If the sqlite3PcacheFetch() routine is unable to allocate a new 284 ** page because new clean pages are available for reuse and the cache 285 ** size limit has been reached, then this routine can be invoked to 286 ** try harder to allocate a page. This routine might invoke the stress 287 ** callback to spill dirty pages to the journal. It will then try to 288 ** allocate the new page and will only fail to allocate a new page on 289 ** an OOM error. 290 ** 291 ** This routine should be invoked only after sqlite3PcacheFetch() fails. 292 */ 293 int sqlite3PcacheFetchStress( 294 PCache *pCache, /* Obtain the page from this cache */ 295 Pgno pgno, /* Page number to obtain */ 296 sqlite3_pcache_page **ppPage /* Write result here */ 297 ){ 298 PgHdr *pPg; 299 if( pCache->eCreate==2 ) return 0; 300 301 302 /* Find a dirty page to write-out and recycle. First try to find a 303 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC 304 ** cleared), but if that is not possible settle for any other 305 ** unreferenced dirty page. 306 */ 307 expensive_assert( pcacheCheckSynced(pCache) ); 308 for(pPg=pCache->pSynced; 309 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 310 pPg=pPg->pDirtyPrev 311 ); 312 pCache->pSynced = pPg; 313 if( !pPg ){ 314 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); 315 } 316 if( pPg ){ 317 int rc; 318 #ifdef SQLITE_LOG_CACHE_SPILL 319 sqlite3_log(SQLITE_FULL, 320 "spill page %d making room for %d - cache used: %d/%d", 321 pPg->pgno, pgno, 322 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache), 323 numberOfCachePages(pCache)); 324 #endif 325 rc = pCache->xStress(pCache->pStress, pPg); 326 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 327 return rc; 328 } 329 } 330 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); 331 return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK; 332 } 333 334 /* 335 ** This is a helper routine for sqlite3PcacheFetchFinish() 336 ** 337 ** In the uncommon case where the page being fetched has not been 338 ** initialized, this routine is invoked to do the initialization. 339 ** This routine is broken out into a separate function since it 340 ** requires extra stack manipulation that can be avoided in the common 341 ** case. 342 */ 343 static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit( 344 PCache *pCache, /* Obtain the page from this cache */ 345 Pgno pgno, /* Page number obtained */ 346 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ 347 ){ 348 PgHdr *pPgHdr; 349 assert( pPage!=0 ); 350 pPgHdr = (PgHdr*)pPage->pExtra; 351 assert( pPgHdr->pPage==0 ); 352 memset(pPgHdr, 0, sizeof(PgHdr)); 353 pPgHdr->pPage = pPage; 354 pPgHdr->pData = pPage->pBuf; 355 pPgHdr->pExtra = (void *)&pPgHdr[1]; 356 memset(pPgHdr->pExtra, 0, pCache->szExtra); 357 pPgHdr->pCache = pCache; 358 pPgHdr->pgno = pgno; 359 return sqlite3PcacheFetchFinish(pCache,pgno,pPage); 360 } 361 362 /* 363 ** This routine converts the sqlite3_pcache_page object returned by 364 ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine 365 ** must be called after sqlite3PcacheFetch() in order to get a usable 366 ** result. 367 */ 368 PgHdr *sqlite3PcacheFetchFinish( 369 PCache *pCache, /* Obtain the page from this cache */ 370 Pgno pgno, /* Page number obtained */ 371 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ 372 ){ 373 PgHdr *pPgHdr; 374 375 if( pPage==0 ) return 0; 376 pPgHdr = (PgHdr *)pPage->pExtra; 377 378 if( !pPgHdr->pPage ){ 379 return pcacheFetchFinishWithInit(pCache, pgno, pPage); 380 } 381 if( 0==pPgHdr->nRef ){ 382 pCache->nRef++; 383 } 384 pPgHdr->nRef++; 385 if( pgno==1 ){ 386 pCache->pPage1 = pPgHdr; 387 } 388 return pPgHdr; 389 } 390 391 /* 392 ** Decrement the reference count on a page. If the page is clean and the 393 ** reference count drops to 0, then it is made eligible for recycling. 394 */ 395 void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ 396 assert( p->nRef>0 ); 397 p->nRef--; 398 if( p->nRef==0 ){ 399 p->pCache->nRef--; 400 if( (p->flags&PGHDR_DIRTY)==0 ){ 401 pcacheUnpin(p); 402 }else{ 403 /* Move the page to the head of the dirty list. */ 404 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); 405 } 406 } 407 } 408 409 /* 410 ** Increase the reference count of a supplied page by 1. 411 */ 412 void sqlite3PcacheRef(PgHdr *p){ 413 assert(p->nRef>0); 414 p->nRef++; 415 } 416 417 /* 418 ** Drop a page from the cache. There must be exactly one reference to the 419 ** page. This function deletes that reference, so after it returns the 420 ** page pointed to by p is invalid. 421 */ 422 void sqlite3PcacheDrop(PgHdr *p){ 423 assert( p->nRef==1 ); 424 if( p->flags&PGHDR_DIRTY ){ 425 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); 426 } 427 p->pCache->nRef--; 428 if( p->pgno==1 ){ 429 p->pCache->pPage1 = 0; 430 } 431 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1); 432 } 433 434 /* 435 ** Make sure the page is marked as dirty. If it isn't dirty already, 436 ** make it so. 437 */ 438 void sqlite3PcacheMakeDirty(PgHdr *p){ 439 p->flags &= ~PGHDR_DONT_WRITE; 440 assert( p->nRef>0 ); 441 if( 0==(p->flags & PGHDR_DIRTY) ){ 442 p->flags |= PGHDR_DIRTY; 443 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); 444 } 445 } 446 447 /* 448 ** Make sure the page is marked as clean. If it isn't clean already, 449 ** make it so. 450 */ 451 void sqlite3PcacheMakeClean(PgHdr *p){ 452 if( (p->flags & PGHDR_DIRTY) ){ 453 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); 454 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC); 455 if( p->nRef==0 ){ 456 pcacheUnpin(p); 457 } 458 } 459 } 460 461 /* 462 ** Make every page in the cache clean. 463 */ 464 void sqlite3PcacheCleanAll(PCache *pCache){ 465 PgHdr *p; 466 while( (p = pCache->pDirty)!=0 ){ 467 sqlite3PcacheMakeClean(p); 468 } 469 } 470 471 /* 472 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. 473 */ 474 void sqlite3PcacheClearSyncFlags(PCache *pCache){ 475 PgHdr *p; 476 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 477 p->flags &= ~PGHDR_NEED_SYNC; 478 } 479 pCache->pSynced = pCache->pDirtyTail; 480 } 481 482 /* 483 ** Change the page number of page p to newPgno. 484 */ 485 void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ 486 PCache *pCache = p->pCache; 487 assert( p->nRef>0 ); 488 assert( newPgno>0 ); 489 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); 490 p->pgno = newPgno; 491 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ 492 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); 493 } 494 } 495 496 /* 497 ** Drop every cache entry whose page number is greater than "pgno". The 498 ** caller must ensure that there are no outstanding references to any pages 499 ** other than page 1 with a page number greater than pgno. 500 ** 501 ** If there is a reference to page 1 and the pgno parameter passed to this 502 ** function is 0, then the data area associated with page 1 is zeroed, but 503 ** the page object is not dropped. 504 */ 505 void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ 506 if( pCache->pCache ){ 507 PgHdr *p; 508 PgHdr *pNext; 509 for(p=pCache->pDirty; p; p=pNext){ 510 pNext = p->pDirtyNext; 511 /* This routine never gets call with a positive pgno except right 512 ** after sqlite3PcacheCleanAll(). So if there are dirty pages, 513 ** it must be that pgno==0. 514 */ 515 assert( p->pgno>0 ); 516 if( ALWAYS(p->pgno>pgno) ){ 517 assert( p->flags&PGHDR_DIRTY ); 518 sqlite3PcacheMakeClean(p); 519 } 520 } 521 if( pgno==0 && pCache->pPage1 ){ 522 memset(pCache->pPage1->pData, 0, pCache->szPage); 523 pgno = 1; 524 } 525 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); 526 } 527 } 528 529 /* 530 ** Close a cache. 531 */ 532 void sqlite3PcacheClose(PCache *pCache){ 533 assert( pCache->pCache!=0 ); 534 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); 535 } 536 537 /* 538 ** Discard the contents of the cache. 539 */ 540 void sqlite3PcacheClear(PCache *pCache){ 541 sqlite3PcacheTruncate(pCache, 0); 542 } 543 544 /* 545 ** Merge two lists of pages connected by pDirty and in pgno order. 546 ** Do not both fixing the pDirtyPrev pointers. 547 */ 548 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ 549 PgHdr result, *pTail; 550 pTail = &result; 551 while( pA && pB ){ 552 if( pA->pgno<pB->pgno ){ 553 pTail->pDirty = pA; 554 pTail = pA; 555 pA = pA->pDirty; 556 }else{ 557 pTail->pDirty = pB; 558 pTail = pB; 559 pB = pB->pDirty; 560 } 561 } 562 if( pA ){ 563 pTail->pDirty = pA; 564 }else if( pB ){ 565 pTail->pDirty = pB; 566 }else{ 567 pTail->pDirty = 0; 568 } 569 return result.pDirty; 570 } 571 572 /* 573 ** Sort the list of pages in accending order by pgno. Pages are 574 ** connected by pDirty pointers. The pDirtyPrev pointers are 575 ** corrupted by this sort. 576 ** 577 ** Since there cannot be more than 2^31 distinct pages in a database, 578 ** there cannot be more than 31 buckets required by the merge sorter. 579 ** One extra bucket is added to catch overflow in case something 580 ** ever changes to make the previous sentence incorrect. 581 */ 582 #define N_SORT_BUCKET 32 583 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ 584 PgHdr *a[N_SORT_BUCKET], *p; 585 int i; 586 memset(a, 0, sizeof(a)); 587 while( pIn ){ 588 p = pIn; 589 pIn = p->pDirty; 590 p->pDirty = 0; 591 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){ 592 if( a[i]==0 ){ 593 a[i] = p; 594 break; 595 }else{ 596 p = pcacheMergeDirtyList(a[i], p); 597 a[i] = 0; 598 } 599 } 600 if( NEVER(i==N_SORT_BUCKET-1) ){ 601 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in 602 ** the input list. But that is impossible. 603 */ 604 a[i] = pcacheMergeDirtyList(a[i], p); 605 } 606 } 607 p = a[0]; 608 for(i=1; i<N_SORT_BUCKET; i++){ 609 p = pcacheMergeDirtyList(p, a[i]); 610 } 611 return p; 612 } 613 614 /* 615 ** Return a list of all dirty pages in the cache, sorted by page number. 616 */ 617 PgHdr *sqlite3PcacheDirtyList(PCache *pCache){ 618 PgHdr *p; 619 for(p=pCache->pDirty; p; p=p->pDirtyNext){ 620 p->pDirty = p->pDirtyNext; 621 } 622 return pcacheSortDirtyList(pCache->pDirty); 623 } 624 625 /* 626 ** Return the total number of referenced pages held by the cache. 627 */ 628 int sqlite3PcacheRefCount(PCache *pCache){ 629 return pCache->nRef; 630 } 631 632 /* 633 ** Return the number of references to the page supplied as an argument. 634 */ 635 int sqlite3PcachePageRefcount(PgHdr *p){ 636 return p->nRef; 637 } 638 639 /* 640 ** Return the total number of pages in the cache. 641 */ 642 int sqlite3PcachePagecount(PCache *pCache){ 643 assert( pCache->pCache!=0 ); 644 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache); 645 } 646 647 #ifdef SQLITE_TEST 648 /* 649 ** Get the suggested cache-size value. 650 */ 651 int sqlite3PcacheGetCachesize(PCache *pCache){ 652 return numberOfCachePages(pCache); 653 } 654 #endif 655 656 /* 657 ** Set the suggested cache-size value. 658 */ 659 void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){ 660 assert( pCache->pCache!=0 ); 661 pCache->szCache = mxPage; 662 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache, 663 numberOfCachePages(pCache)); 664 } 665 666 /* 667 ** Free up as much memory as possible from the page cache. 668 */ 669 void sqlite3PcacheShrink(PCache *pCache){ 670 assert( pCache->pCache!=0 ); 671 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); 672 } 673 674 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) 675 /* 676 ** For all dirty pages currently in the cache, invoke the specified 677 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is 678 ** defined. 679 */ 680 void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){ 681 PgHdr *pDirty; 682 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){ 683 xIter(pDirty); 684 } 685 } 686 #endif 687