xref: /original-bsd/include/mpool.h (revision 3f14a87d)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)mpool.h	5.2 (Berkeley) 02/14/93
8  */
9 
10 /*
11  * The memory pool scheme is a simple one.  Each in memory page is referenced
12  * by a bucket which is threaded in three ways.  All active pages are threaded
13  * on a hash chain (hashed by the page number) and an lru chain.  Inactive
14  * pages are threaded on a free chain.  Each reference to a memory pool is
15  * handed an MPOOL which is the opaque cookie passed to all of the memory
16  * routines.
17  */
18 #define	HASHSIZE	128
19 #define	HASHKEY(pgno)	((pgno - 1) % HASHSIZE)
20 
21 /* The BKT structures are the elements of the lists. */
22 typedef struct BKT {
23 	struct BKT	*hnext;		/* next hash bucket */
24 	struct BKT	*hprev;		/* previous hash bucket */
25 	struct BKT	*cnext;		/* next free/lru bucket */
26 	struct BKT	*cprev;		/* previous free/lru bucket */
27 	void		*page;		/* page */
28 	pgno_t		pgno;		/* page number */
29 
30 #define	MPOOL_DIRTY	0x01		/* page needs to be written */
31 #define	MPOOL_PINNED	0x02		/* page is pinned into memory */
32 	unsigned long	flags;		/* flags */
33 } BKT;
34 
35 /* The BKTHDR structures are the heads of the lists. */
36 typedef struct BKTHDR {
37 	struct BKT	*hnext;		/* next hash bucket */
38 	struct BKT	*hprev;		/* previous hash bucket */
39 	struct BKT	*cnext;		/* next free/lru bucket */
40 	struct BKT	*cprev;		/* previous free/lru bucket */
41 } BKTHDR;
42 
43 typedef struct MPOOL {
44 	BKTHDR	free;			/* The free list. */
45 	BKTHDR	lru;			/* The LRU list. */
46 	BKTHDR	hashtable[HASHSIZE];	/* Hashed list by page number. */
47 	pgno_t	curcache;		/* Current number of cached pages. */
48 	pgno_t	maxcache;		/* Max number of cached pages. */
49 	pgno_t	npages;			/* Number of pages in the file. */
50 	indx_t	pagesize;		/* File page size. */
51 	int	fd;			/* File descriptor. */
52 					/* Page in conversion routine. */
53 	void    (*pgin) __P((void *, pgno_t, void *));
54 					/* Page out conversion routine. */
55 	void    (*pgout) __P((void *, pgno_t, void *));
56 	void	*pgcookie;		/* Cookie for page in/out routines. */
57 #ifdef STATISTICS
58 	unsigned long	cachehit;
59 	unsigned long	cachemiss;
60 	unsigned long	pagealloc;
61 	unsigned long	pageflush;
62 	unsigned long	pageget;
63 	unsigned long	pagenew;
64 	unsigned long	pageput;
65 	unsigned long	pageread;
66 	unsigned long	pagewrite;
67 #endif
68 } MPOOL;
69 
70 #ifdef __MPOOLINTERFACE_PRIVATE
71 /* Macros to insert/delete into/from hash chain. */
72 #define rmhash(bp) { \
73         (bp)->hprev->hnext = (bp)->hnext; \
74         (bp)->hnext->hprev = (bp)->hprev; \
75 }
76 #define inshash(bp, pg) { \
77 	hp = &mp->hashtable[HASHKEY(pg)]; \
78         (bp)->hnext = hp->hnext; \
79         (bp)->hprev = (struct BKT *)hp; \
80         hp->hnext->hprev = (bp); \
81         hp->hnext = (bp); \
82 }
83 
84 /* Macros to insert/delete into/from lru and free chains. */
85 #define	rmchain(bp) { \
86         (bp)->cprev->cnext = (bp)->cnext; \
87         (bp)->cnext->cprev = (bp)->cprev; \
88 }
89 #define inschain(bp, dp) { \
90         (bp)->cnext = (dp)->cnext; \
91         (bp)->cprev = (struct BKT *)(dp); \
92         (dp)->cnext->cprev = (bp); \
93         (dp)->cnext = (bp); \
94 }
95 #endif
96 
97 __BEGIN_DECLS
98 MPOOL	*mpool_open __P((DBT *, int, pgno_t, pgno_t));
99 void	 mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *),
100 	    void (*)(void *, pgno_t, void *), void *));
101 void	*mpool_new __P((MPOOL *, pgno_t *));
102 void	*mpool_get __P((MPOOL *, pgno_t, u_int));
103 int	 mpool_put __P((MPOOL *, void *, u_int));
104 int	 mpool_sync __P((MPOOL *));
105 int	 mpool_close __P((MPOOL *));
106 #ifdef STATISTICS
107 void	 mpool_stat __P((MPOOL *));
108 #endif
109 __END_DECLS
110