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