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