mpool.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: mpool.h /main/3 1996/06/11 17:14:12 cde-hal $ */
  24. /*-
  25. * Copyright (c) 1991, 1993
  26. * The Regents of the University of California. All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. * 1. Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. * 3. All advertising materials mentioning features or use of this software
  37. * must display the following acknowledgement:
  38. * This product includes software developed by the University of
  39. * California, Berkeley and its contributors.
  40. * 4. Neither the name of the University nor the names of its contributors
  41. * may be used to endorse or promote products derived from this software
  42. * without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  45. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  48. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  53. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  54. * SUCH DAMAGE.
  55. *
  56. * @(#)mpool.h 8.1 (Berkeley) 6/2/93
  57. */
  58. /*
  59. * The memory pool scheme is a simple one. Each in memory page is referenced
  60. * by a bucket which is threaded in three ways. All active pages are threaded
  61. * on a hash chain (hashed by the page number) and an lru chain. Inactive
  62. * pages are threaded on a free chain. Each reference to a memory pool is
  63. * handed an MPOOL which is the opaque cookie passed to all of the memory
  64. * routines.
  65. */
  66. #define HASHSIZE 128
  67. #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE)
  68. /* The BKT structures are the elements of the lists. */
  69. typedef struct BKT {
  70. struct BKT *hnext; /* next hash bucket */
  71. struct BKT *hprev; /* previous hash bucket */
  72. struct BKT *cnext; /* next free/lru bucket */
  73. struct BKT *cprev; /* previous free/lru bucket */
  74. void *page; /* page */
  75. pgno_t pgno; /* page number */
  76. #define MPOOL_DIRTY 0x01 /* page needs to be written */
  77. #define MPOOL_PINNED 0x02 /* page is pinned into memory */
  78. unsigned long flags; /* flags */
  79. } BKT;
  80. /* The BKTHDR structures are the heads of the lists. */
  81. typedef struct BKTHDR {
  82. struct BKT *hnext; /* next hash bucket */
  83. struct BKT *hprev; /* previous hash bucket */
  84. struct BKT *cnext; /* next free/lru bucket */
  85. struct BKT *cprev; /* previous free/lru bucket */
  86. } BKTHDR;
  87. typedef struct MPOOL {
  88. BKTHDR free; /* The free list. */
  89. BKTHDR lru; /* The LRU list. */
  90. BKTHDR hashtable[HASHSIZE]; /* Hashed list by page number. */
  91. pgno_t curcache; /* Current number of cached pages. */
  92. pgno_t maxcache; /* Max number of cached pages. */
  93. pgno_t npages; /* Number of pages in the file. */
  94. u_long pagesize; /* File page size. */
  95. int fd; /* File descriptor. */
  96. /* Page in conversion routine. */
  97. void (*pgin) __P((void *, pgno_t, void *));
  98. /* Page out conversion routine. */
  99. void (*pgout) __P((void *, pgno_t, void *));
  100. void *pgcookie; /* Cookie for page in/out routines. */
  101. #ifdef STATISTICS
  102. unsigned long cachehit;
  103. unsigned long cachemiss;
  104. unsigned long pagealloc;
  105. unsigned long pageflush;
  106. unsigned long pageget;
  107. unsigned long pagenew;
  108. unsigned long pageput;
  109. unsigned long pageread;
  110. unsigned long pagewrite;
  111. #endif
  112. } MPOOL;
  113. #ifdef __MPOOLINTERFACE_PRIVATE
  114. /* Macros to insert/delete into/from hash chain. */
  115. #define rmhash(bp) { \
  116. (bp)->hprev->hnext = (bp)->hnext; \
  117. (bp)->hnext->hprev = (bp)->hprev; \
  118. }
  119. #define inshash(bp, pg) { \
  120. hp = &mp->hashtable[HASHKEY(pg)]; \
  121. (bp)->hnext = hp->hnext; \
  122. (bp)->hprev = (struct BKT *)hp; \
  123. hp->hnext->hprev = (bp); \
  124. hp->hnext = (bp); \
  125. }
  126. /* Macros to insert/delete into/from lru and free chains. */
  127. #define rmchain(bp) { \
  128. (bp)->cprev->cnext = (bp)->cnext; \
  129. (bp)->cnext->cprev = (bp)->cprev; \
  130. }
  131. #define inschain(bp, dp) { \
  132. (bp)->cnext = (dp)->cnext; \
  133. (bp)->cprev = (struct BKT *)(void *)(dp); \
  134. (dp)->cnext->cprev = (bp); \
  135. (dp)->cnext = (bp); \
  136. }
  137. #endif
  138. __BEGIN_DECLS
  139. MPOOL *mpool_open __P((DBT *, int, pgno_t, pgno_t));
  140. void mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *),
  141. void (*)(void *, pgno_t, void *), void *));
  142. void *mpool_new __P((MPOOL *, pgno_t *));
  143. void *mpool_get __P((MPOOL *, pgno_t, u_int));
  144. int mpool_put __P((MPOOL *, void *, u_int));
  145. int mpool_sync __P((MPOOL *));
  146. int mpool_close __P((MPOOL *));
  147. #ifdef STATISTICS
  148. void mpool_stat __P((MPOOL *));
  149. #endif
  150. __END_DECLS