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