xref: /openbsd/lib/libc/include/mpool.h (revision 061188c8)
1*061188c8Sguenther /*	$OpenBSD: mpool.h,v 1.1 2015/09/09 15:35:24 guenther Exp $	*/
2*061188c8Sguenther /*	$NetBSD: mpool.h,v 1.7 1996/05/03 21:13:41 cgd Exp $	*/
3*061188c8Sguenther 
4*061188c8Sguenther /*-
5*061188c8Sguenther  * Copyright (c) 1991, 1993, 1994
6*061188c8Sguenther  *	The Regents of the University of California.  All rights reserved.
7*061188c8Sguenther  *
8*061188c8Sguenther  * Redistribution and use in source and binary forms, with or without
9*061188c8Sguenther  * modification, are permitted provided that the following conditions
10*061188c8Sguenther  * are met:
11*061188c8Sguenther  * 1. Redistributions of source code must retain the above copyright
12*061188c8Sguenther  *    notice, this list of conditions and the following disclaimer.
13*061188c8Sguenther  * 2. Redistributions in binary form must reproduce the above copyright
14*061188c8Sguenther  *    notice, this list of conditions and the following disclaimer in the
15*061188c8Sguenther  *    documentation and/or other materials provided with the distribution.
16*061188c8Sguenther  * 3. Neither the name of the University nor the names of its contributors
17*061188c8Sguenther  *    may be used to endorse or promote products derived from this software
18*061188c8Sguenther  *    without specific prior written permission.
19*061188c8Sguenther  *
20*061188c8Sguenther  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*061188c8Sguenther  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*061188c8Sguenther  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*061188c8Sguenther  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*061188c8Sguenther  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*061188c8Sguenther  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*061188c8Sguenther  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*061188c8Sguenther  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*061188c8Sguenther  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*061188c8Sguenther  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*061188c8Sguenther  * SUCH DAMAGE.
31*061188c8Sguenther  *
32*061188c8Sguenther  *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
33*061188c8Sguenther  */
34*061188c8Sguenther 
35*061188c8Sguenther #ifndef _MPOOL_H_
36*061188c8Sguenther #define _MPOOL_H_
37*061188c8Sguenther 
38*061188c8Sguenther #include <sys/queue.h>
39*061188c8Sguenther 
40*061188c8Sguenther /*
41*061188c8Sguenther  * The memory pool scheme is a simple one.  Each in-memory page is referenced
42*061188c8Sguenther  * by a bucket which is threaded in up to two of three ways.  All active pages
43*061188c8Sguenther  * are threaded on a hash chain (hashed by page number) and an lru chain.
44*061188c8Sguenther  * Inactive pages are threaded on a free chain.  Each reference to a memory
45*061188c8Sguenther  * pool is handed an opaque MPOOL cookie which stores all of this information.
46*061188c8Sguenther  */
47*061188c8Sguenther #define	HASHSIZE	128
48*061188c8Sguenther #define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
49*061188c8Sguenther 
50*061188c8Sguenther /* The BKT structures are the elements of the queues. */
51*061188c8Sguenther typedef struct _bkt {
52*061188c8Sguenther 	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
53*061188c8Sguenther 	TAILQ_ENTRY(_bkt) q;		/* lru queue */
54*061188c8Sguenther 	void    *page;			/* page */
55*061188c8Sguenther 	pgno_t   pgno;			/* page number */
56*061188c8Sguenther 
57*061188c8Sguenther #define	MPOOL_DIRTY	0x01		/* page needs to be written */
58*061188c8Sguenther #define	MPOOL_PINNED	0x02		/* page is pinned into memory */
59*061188c8Sguenther #define	MPOOL_INUSE	0x04		/* page address is valid */
60*061188c8Sguenther 	u_int8_t flags;			/* flags */
61*061188c8Sguenther } BKT;
62*061188c8Sguenther 
63*061188c8Sguenther typedef struct MPOOL {
64*061188c8Sguenther 	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
65*061188c8Sguenther 					/* hash queue array */
66*061188c8Sguenther 	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
67*061188c8Sguenther 	pgno_t	curcache;		/* current number of cached pages */
68*061188c8Sguenther 	pgno_t	maxcache;		/* max number of cached pages */
69*061188c8Sguenther 	pgno_t	npages;			/* number of pages in the file */
70*061188c8Sguenther 	unsigned long	pagesize;	/* file page size */
71*061188c8Sguenther 	int	fd;			/* file descriptor */
72*061188c8Sguenther 					/* page in conversion routine */
73*061188c8Sguenther 	void    (*pgin)(void *, pgno_t, void *);
74*061188c8Sguenther 					/* page out conversion routine */
75*061188c8Sguenther 	void    (*pgout)(void *, pgno_t, void *);
76*061188c8Sguenther 	void	*pgcookie;		/* cookie for page in/out routines */
77*061188c8Sguenther #ifdef STATISTICS
78*061188c8Sguenther 	unsigned long	cachehit;
79*061188c8Sguenther 	unsigned long	cachemiss;
80*061188c8Sguenther 	unsigned long	pagealloc;
81*061188c8Sguenther 	unsigned long	pageflush;
82*061188c8Sguenther 	unsigned long	pageget;
83*061188c8Sguenther 	unsigned long	pagenew;
84*061188c8Sguenther 	unsigned long	pageput;
85*061188c8Sguenther 	unsigned long	pageread;
86*061188c8Sguenther 	unsigned long	pagewrite;
87*061188c8Sguenther #endif
88*061188c8Sguenther } MPOOL;
89*061188c8Sguenther 
90*061188c8Sguenther #define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
91*061188c8Sguenther #define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
92*061188c8Sguenther 					   specific page number. */
93*061188c8Sguenther #define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
94*061188c8Sguenther 					  page number. */
95*061188c8Sguenther 
96*061188c8Sguenther __BEGIN_HIDDEN_DECLS
97*061188c8Sguenther MPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
98*061188c8Sguenther void	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
99*061188c8Sguenther 	    void (*)(void *, pgno_t, void *), void *);
100*061188c8Sguenther void	*mpool_new(MPOOL *, pgno_t *, unsigned int);
101*061188c8Sguenther void	*mpool_get(MPOOL *, pgno_t, unsigned int);
102*061188c8Sguenther int	 mpool_delete(MPOOL *, void *);
103*061188c8Sguenther int	 mpool_put(MPOOL *, void *, unsigned int);
104*061188c8Sguenther int	 mpool_sync(MPOOL *);
105*061188c8Sguenther int	 mpool_close(MPOOL *);
106*061188c8Sguenther 
107*061188c8Sguenther PROTO_NORMAL(mpool_open);
108*061188c8Sguenther PROTO_NORMAL(mpool_filter);
109*061188c8Sguenther PROTO_NORMAL(mpool_new);
110*061188c8Sguenther PROTO_NORMAL(mpool_get);
111*061188c8Sguenther PROTO_NORMAL(mpool_delete);
112*061188c8Sguenther PROTO_NORMAL(mpool_put);
113*061188c8Sguenther PROTO_NORMAL(mpool_sync);
114*061188c8Sguenther PROTO_NORMAL(mpool_close);
115*061188c8Sguenther 
116*061188c8Sguenther #ifdef STATISTICS
117*061188c8Sguenther void	 mpool_stat(MPOOL *);
118*061188c8Sguenther PROTO_NORMAL(mpool_stat);
119*061188c8Sguenther #endif
120*061188c8Sguenther __END_HIDDEN_DECLS
121*061188c8Sguenther 
122*061188c8Sguenther #endif
123