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