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