xref: /freebsd/include/mpool.h (revision 90fd5bdfbfde6fb7ce02f1ba673b79ddccbae947)
159deaec5SRodney W. Grimes /*-
290fd5bdfSPaul Traina  * Copyright (c) 1991, 1993, 1994
359deaec5SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
459deaec5SRodney W. Grimes  *
559deaec5SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
659deaec5SRodney W. Grimes  * modification, are permitted provided that the following conditions
759deaec5SRodney W. Grimes  * are met:
859deaec5SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
959deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1059deaec5SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1159deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1259deaec5SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1359deaec5SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1459deaec5SRodney W. Grimes  *    must display the following acknowledgement:
1559deaec5SRodney W. Grimes  *	This product includes software developed by the University of
1659deaec5SRodney W. Grimes  *	California, Berkeley and its contributors.
1759deaec5SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1859deaec5SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1959deaec5SRodney W. Grimes  *    without specific prior written permission.
2059deaec5SRodney W. Grimes  *
2159deaec5SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2259deaec5SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359deaec5SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459deaec5SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2559deaec5SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659deaec5SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759deaec5SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859deaec5SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959deaec5SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059deaec5SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159deaec5SRodney W. Grimes  * SUCH DAMAGE.
3259deaec5SRodney W. Grimes  *
3390fd5bdfSPaul Traina  *	@(#)mpool.h	8.2 (Berkeley) 7/14/94
3459deaec5SRodney W. Grimes  */
3559deaec5SRodney W. Grimes 
3654b0ee63SPaul Richards #ifndef _MPOOL_H_
3754b0ee63SPaul Richards #define _MPOOL_H_
3854b0ee63SPaul Richards 
3990fd5bdfSPaul Traina #include <sys/queue.h>
4090fd5bdfSPaul Traina 
4159deaec5SRodney W. Grimes /*
4290fd5bdfSPaul Traina  * The memory pool scheme is a simple one.  Each in-memory page is referenced
4390fd5bdfSPaul Traina  * by a bucket which is threaded in up to two of three ways.  All active pages
4490fd5bdfSPaul Traina  * are threaded on a hash chain (hashed by page number) and an lru chain.
4590fd5bdfSPaul Traina  * Inactive pages are threaded on a free chain.  Each reference to a memory
4690fd5bdfSPaul Traina  * pool is handed an opaque MPOOL cookie which stores all of this information.
4759deaec5SRodney W. Grimes  */
4859deaec5SRodney W. Grimes #define	HASHSIZE	128
4959deaec5SRodney W. Grimes #define	HASHKEY(pgno)	((pgno - 1) % HASHSIZE)
5059deaec5SRodney W. Grimes 
5190fd5bdfSPaul Traina /* The BKT structures are the elements of the queues. */
5290fd5bdfSPaul Traina typedef struct _bkt {
5390fd5bdfSPaul Traina 	CIRCLEQ_ENTRY(_bkt) hq;		/* hash queue */
5490fd5bdfSPaul Traina 	CIRCLEQ_ENTRY(_bkt) q;		/* lru queue */
5559deaec5SRodney W. Grimes 	void    *page;			/* page */
5659deaec5SRodney W. Grimes 	pgno_t   pgno;			/* page number */
5759deaec5SRodney W. Grimes 
5859deaec5SRodney W. Grimes #define	MPOOL_DIRTY	0x01		/* page needs to be written */
5959deaec5SRodney W. Grimes #define	MPOOL_PINNED	0x02		/* page is pinned into memory */
6090fd5bdfSPaul Traina 	u_int8_t flags;			/* flags */
6159deaec5SRodney W. Grimes } BKT;
6259deaec5SRodney W. Grimes 
6359deaec5SRodney W. Grimes typedef struct MPOOL {
6490fd5bdfSPaul Traina 	CIRCLEQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
6590fd5bdfSPaul Traina 					/* hash queue array */
6690fd5bdfSPaul Traina 	CIRCLEQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
6790fd5bdfSPaul Traina 	pgno_t	curcache;		/* current number of cached pages */
6890fd5bdfSPaul Traina 	pgno_t	maxcache;		/* max number of cached pages */
6990fd5bdfSPaul Traina 	pgno_t	npages;			/* number of pages in the file */
7090fd5bdfSPaul Traina 	u_long	pagesize;		/* file page size */
7190fd5bdfSPaul Traina 	int	fd;			/* file descriptor */
7290fd5bdfSPaul Traina 					/* page in conversion routine */
7359deaec5SRodney W. Grimes 	void    (*pgin) __P((void *, pgno_t, void *));
7490fd5bdfSPaul Traina 					/* page out conversion routine */
7559deaec5SRodney W. Grimes 	void    (*pgout) __P((void *, pgno_t, void *));
7690fd5bdfSPaul Traina 	void	*pgcookie;		/* cookie for page in/out routines */
7759deaec5SRodney W. Grimes #ifdef STATISTICS
7890fd5bdfSPaul Traina 	u_long	cachehit;
7990fd5bdfSPaul Traina 	u_long	cachemiss;
8090fd5bdfSPaul Traina 	u_long	pagealloc;
8190fd5bdfSPaul Traina 	u_long	pageflush;
8290fd5bdfSPaul Traina 	u_long	pageget;
8390fd5bdfSPaul Traina 	u_long	pagenew;
8490fd5bdfSPaul Traina 	u_long	pageput;
8590fd5bdfSPaul Traina 	u_long	pageread;
8690fd5bdfSPaul Traina 	u_long	pagewrite;
8759deaec5SRodney W. Grimes #endif
8859deaec5SRodney W. Grimes } MPOOL;
8959deaec5SRodney W. Grimes 
9059deaec5SRodney W. Grimes __BEGIN_DECLS
9190fd5bdfSPaul Traina MPOOL	*mpool_open __P((void *, int, pgno_t, pgno_t));
9259deaec5SRodney W. Grimes void	 mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *),
9359deaec5SRodney W. Grimes 	    void (*)(void *, pgno_t, void *), void *));
9459deaec5SRodney W. Grimes void	*mpool_new __P((MPOOL *, pgno_t *));
9559deaec5SRodney W. Grimes void	*mpool_get __P((MPOOL *, pgno_t, u_int));
9659deaec5SRodney W. Grimes int	 mpool_put __P((MPOOL *, void *, u_int));
9759deaec5SRodney W. Grimes int	 mpool_sync __P((MPOOL *));
9859deaec5SRodney W. Grimes int	 mpool_close __P((MPOOL *));
9959deaec5SRodney W. Grimes #ifdef STATISTICS
10059deaec5SRodney W. Grimes void	 mpool_stat __P((MPOOL *));
10159deaec5SRodney W. Grimes #endif
10259deaec5SRodney W. Grimes __END_DECLS
10354b0ee63SPaul Richards 
10454b0ee63SPaul Richards #endif
105