/* */
1 /*-
2 * Copyright (c) 1991, 1993, 1994
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
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 *
29 * @(#)mpool.h 8.2 (Berkeley) 7/14/94
30 */
31
32 #include "queue.h"
33
34 /**
35 * The memory pool scheme is a simple one. Each in-memory page is referenced
36 * by a bucket which is threaded in up to two of three ways. All active pages
37 * are threaded on a hash chain (hashed by page number) and an lru chain.
38 * Inactive pages are threaded on a free chain. Each reference to a memory
39 * pool is handed an opaque #MPOOL cookie which stores all of this information.
40 */
41 #define HASHSIZE 128
42 #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE)
43
44 /** The BKT structures are the elements of the queues. */
45 typedef struct _bkt {
46
47 #ifndef IS__DOXYGEN_
48 CIRCLEQ_ENTRY(_bkt) hq; /**< hash queue */
49 CIRCLEQ_ENTRY(_bkt) q; /**< lru queue */
50 #else
51 struct {
52 struct _bkt *cqe_next;
53 struct _bkt *cqe_prev;
54 } hq; /**< hash queue */
55 struct {
56 struct _bkt *cqe_next;
57 struct _bkt *cqe_prev;
58 } q; /**< lru queue */
59 #endif
60 void *page; /**< page */
61 pgno_t pgno; /**< page number */
62
63 /** page needs to be written */
64 #define MPOOL_DIRTY 0x01
65 /** page is pinned into memory */
66 #define MPOOL_PINNED 0x02
67 u_int8_t flags; /**< flags */
68 } BKT;
69
70 typedef struct MPOOL {
71
72 #ifndef IS__DOXYGEN_
73 CIRCLEQ_HEAD(_lqh, _bkt) lqh; /**< lru queue head */
74 /** hash queue array */
75 CIRCLEQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
76 #else
77 struct _lqh {
78 struct _bkt *cqh_first;
79 struct _bkt *cqh_last;
80 } lqh; /**< lru queue head */
81 struct _hqh {
82 struct _bkt *cqh_first;
83 struct _bkt *cqh_last;
84 } hqh[HASHSIZE]; /**< hash queue array */
85 #endif
86 pgno_t curcache; /**< current number of cached pages */
87 pgno_t maxcache; /**< max number of cached pages */
88 pgno_t npages; /**< number of pages in the file */
89 u_long pagesize; /**< file page size */
90 int fd; /**< file descriptor */
91 /** page in conversion routine */
92 void (*pgin)(void *, pgno_t, void *);
93 /** page out conversion routine */
94 void (*pgout)(void *, pgno_t, void *);
95 void *pgcookie; /**< cookie for page in/out routines */
96 #ifdef STATISTICS
97 u_long cachehit;
98 u_long cachemiss;
99 u_long pagealloc;
100 u_long pageflush;
101 u_long pageget;
102 u_long pagenew;
103 u_long pageput;
104 u_long pageread;
105 u_long pagewrite;
106 #endif
107 } MPOOL;
108
109 MPOOL *mpool_open(void *, int, pgno_t, pgno_t);
110 void mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
111 void (*)(void *, pgno_t, void *), void *);
112 void *mpool_new(MPOOL *, pgno_t *);
113 void *mpool_get(MPOOL *, pgno_t, u_int);
114 int mpool_put(MPOOL *, void *, u_int);
115 int mpool_sync(MPOOL *);
116 int mpool_close(MPOOL *);
117 #ifdef STATISTICS
118 void mpool_stat(MPOOL *);
119 #endif
/* */