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