pcache.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 header file defines the interface that the sqlite page cache
  13. ** subsystem.
  14. */
  15. #ifndef _PCACHE_H_
  16. typedef struct PgHdr PgHdr;
  17. typedef struct PCache PCache;
  18. /*
  19. ** Every page in the cache is controlled by an instance of the following
  20. ** structure.
  21. */
  22. struct PgHdr {
  23. sqlite3_pcache_page *pPage; /* Pcache object page handle */
  24. void *pData; /* Page data */
  25. void *pExtra; /* Extra content */
  26. PgHdr *pDirty; /* Transient list of dirty pages */
  27. Pager *pPager; /* The pager this page is part of */
  28. Pgno pgno; /* Page number for this page */
  29. #ifdef SQLITE_CHECK_PAGES
  30. u32 pageHash; /* Hash of page content */
  31. #endif
  32. u16 flags; /* PGHDR flags defined below */
  33. /**********************************************************************
  34. ** Elements above are public. All that follows is private to pcache.c
  35. ** and should not be accessed by other modules.
  36. */
  37. i16 nRef; /* Number of users of this page */
  38. PCache *pCache; /* Cache that owns this page */
  39. PgHdr *pDirtyNext; /* Next element in list of dirty pages */
  40. PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
  41. };
  42. /* Bit values for PgHdr.flags */
  43. #define PGHDR_DIRTY 0x002 /* Page has changed */
  44. #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
  45. ** writing this page to the database */
  46. #define PGHDR_NEED_READ 0x008 /* Content is unread */
  47. #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
  48. #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
  49. #define PGHDR_MMAP 0x040 /* This is an mmap page object */
  50. /* Initialize and shutdown the page cache subsystem */
  51. int sqlite3PcacheInitialize(void);
  52. void sqlite3PcacheShutdown(void);
  53. /* Page cache buffer management:
  54. ** These routines implement SQLITE_CONFIG_PAGECACHE.
  55. */
  56. void sqlite3PCacheBufferSetup(void *, int sz, int n);
  57. /* Create a new pager cache.
  58. ** Under memory stress, invoke xStress to try to make pages clean.
  59. ** Only clean and unpinned pages can be reclaimed.
  60. */
  61. void sqlite3PcacheOpen(
  62. int szPage, /* Size of every page */
  63. int szExtra, /* Extra space associated with each page */
  64. int bPurgeable, /* True if pages are on backing store */
  65. int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
  66. void *pStress, /* Argument to xStress */
  67. PCache *pToInit /* Preallocated space for the PCache */
  68. );
  69. /* Modify the page-size after the cache has been created. */
  70. void sqlite3PcacheSetPageSize(PCache *, int);
  71. /* Return the size in bytes of a PCache object. Used to preallocate
  72. ** storage space.
  73. */
  74. int sqlite3PcacheSize(void);
  75. /* One release per successful fetch. Page is pinned until released.
  76. ** Reference counted.
  77. */
  78. int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
  79. void sqlite3PcacheRelease(PgHdr*);
  80. void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
  81. void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
  82. void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
  83. void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
  84. /* Change a page number. Used by incr-vacuum. */
  85. void sqlite3PcacheMove(PgHdr*, Pgno);
  86. /* Remove all pages with pgno>x. Reset the cache if x==0 */
  87. void sqlite3PcacheTruncate(PCache*, Pgno x);
  88. /* Get a list of all dirty pages in the cache, sorted by page number */
  89. PgHdr *sqlite3PcacheDirtyList(PCache*);
  90. /* Reset and close the cache object */
  91. void sqlite3PcacheClose(PCache*);
  92. /* Clear flags from pages of the page cache */
  93. void sqlite3PcacheClearSyncFlags(PCache *);
  94. /* Discard the contents of the cache */
  95. void sqlite3PcacheClear(PCache*);
  96. /* Return the total number of outstanding page references */
  97. int sqlite3PcacheRefCount(PCache*);
  98. /* Increment the reference count of an existing page */
  99. void sqlite3PcacheRef(PgHdr*);
  100. int sqlite3PcachePageRefcount(PgHdr*);
  101. /* Return the total number of pages stored in the cache */
  102. int sqlite3PcachePagecount(PCache*);
  103. #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  104. /* Iterate through all dirty pages currently stored in the cache. This
  105. ** interface is only available if SQLITE_CHECK_PAGES is defined when the
  106. ** library is built.
  107. */
  108. void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  109. #endif
  110. /* Set and get the suggested cache-size for the specified pager-cache.
  111. **
  112. ** If no global maximum is configured, then the system attempts to limit
  113. ** the total number of pages cached by purgeable pager-caches to the sum
  114. ** of the suggested cache-sizes.
  115. */
  116. void sqlite3PcacheSetCachesize(PCache *, int);
  117. #ifdef SQLITE_TEST
  118. int sqlite3PcacheGetCachesize(PCache *);
  119. #endif
  120. /* Free up as much memory as possible from the page cache */
  121. void sqlite3PcacheShrink(PCache*);
  122. #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  123. /* Try to return memory used by the pcache module to the main memory heap */
  124. int sqlite3PcacheReleaseMemory(int);
  125. #endif
  126. #ifdef SQLITE_TEST
  127. void sqlite3PcacheStats(int*,int*,int*,int*);
  128. #endif
  129. void sqlite3PCacheSetDefault(void);
  130. #endif /* _PCACHE_H_ */