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