xref: /freebsd/include/mpool.h (revision 9fc74a871c838bb10c1ba9b01a22e9df4aa2e55f)
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  *
339fc74a87SXin LI  *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
34740a1973SJake Burkholder  * $FreeBSD$
3559deaec5SRodney W. Grimes  */
3659deaec5SRodney W. Grimes 
3754b0ee63SPaul Richards #ifndef _MPOOL_H_
3854b0ee63SPaul Richards #define _MPOOL_H_
3954b0ee63SPaul Richards 
4090fd5bdfSPaul Traina #include <sys/queue.h>
4190fd5bdfSPaul Traina 
4259deaec5SRodney W. Grimes /*
4390fd5bdfSPaul Traina  * The memory pool scheme is a simple one.  Each in-memory page is referenced
4490fd5bdfSPaul Traina  * by a bucket which is threaded in up to two of three ways.  All active pages
4590fd5bdfSPaul Traina  * are threaded on a hash chain (hashed by page number) and an lru chain.
4690fd5bdfSPaul Traina  * Inactive pages are threaded on a free chain.  Each reference to a memory
4790fd5bdfSPaul Traina  * pool is handed an opaque MPOOL cookie which stores all of this information.
4859deaec5SRodney W. Grimes  */
4959deaec5SRodney W. Grimes #define	HASHSIZE	128
509fc74a87SXin LI #define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
5159deaec5SRodney W. Grimes 
5290fd5bdfSPaul Traina /* The BKT structures are the elements of the queues. */
5390fd5bdfSPaul Traina typedef struct _bkt {
54fabacd3aSPoul-Henning Kamp 	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
55fabacd3aSPoul-Henning Kamp 	TAILQ_ENTRY(_bkt) q;		/* lru queue */
5659deaec5SRodney W. Grimes 	void    *page;			/* page */
5759deaec5SRodney W. Grimes 	pgno_t   pgno;			/* page number */
5859deaec5SRodney W. Grimes 
5959deaec5SRodney W. Grimes #define	MPOOL_DIRTY	0x01		/* page needs to be written */
6059deaec5SRodney W. Grimes #define	MPOOL_PINNED	0x02		/* page is pinned into memory */
619fc74a87SXin LI #define	MPOOL_INUSE	0x04		/* page address is valid */
6290fd5bdfSPaul Traina 	u_int8_t flags;			/* flags */
6359deaec5SRodney W. Grimes } BKT;
6459deaec5SRodney W. Grimes 
6559deaec5SRodney W. Grimes typedef struct MPOOL {
66fabacd3aSPoul-Henning Kamp 	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
6790fd5bdfSPaul Traina 					/* hash queue array */
68fabacd3aSPoul-Henning Kamp 	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
6990fd5bdfSPaul Traina 	pgno_t	curcache;		/* current number of cached pages */
7090fd5bdfSPaul Traina 	pgno_t	maxcache;		/* max number of cached pages */
7190fd5bdfSPaul Traina 	pgno_t	npages;			/* number of pages in the file */
729fc74a87SXin LI 	unsigned long	pagesize;	/* file page size */
7390fd5bdfSPaul Traina 	int	fd;			/* file descriptor */
7490fd5bdfSPaul Traina 					/* page in conversion routine */
75bb28f3c2SWarner Losh 	void    (*pgin)(void *, pgno_t, void *);
7690fd5bdfSPaul Traina 					/* page out conversion routine */
77bb28f3c2SWarner Losh 	void    (*pgout)(void *, pgno_t, void *);
7890fd5bdfSPaul Traina 	void	*pgcookie;		/* cookie for page in/out routines */
7959deaec5SRodney W. Grimes #ifdef STATISTICS
809fc74a87SXin LI 	unsigned long	cachehit;
819fc74a87SXin LI 	unsigned long	cachemiss;
829fc74a87SXin LI 	unsigned long	pagealloc;
839fc74a87SXin LI 	unsigned long	pageflush;
849fc74a87SXin LI 	unsigned long	pageget;
859fc74a87SXin LI 	unsigned long	pagenew;
869fc74a87SXin LI 	unsigned long	pageput;
879fc74a87SXin LI 	unsigned long	pageread;
889fc74a87SXin LI 	unsigned long	pagewrite;
8959deaec5SRodney W. Grimes #endif
9059deaec5SRodney W. Grimes } MPOOL;
9159deaec5SRodney W. Grimes 
929fc74a87SXin LI #define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
939fc74a87SXin LI #define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
949fc74a87SXin LI 					   specific page number. */
959fc74a87SXin LI #define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
969fc74a87SXin LI 					  page number. */
979fc74a87SXin LI 
9859deaec5SRodney W. Grimes __BEGIN_DECLS
99bb28f3c2SWarner Losh MPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
100bb28f3c2SWarner Losh void	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
101bb28f3c2SWarner Losh 	    void (*)(void *, pgno_t, void *), void *);
1029fc74a87SXin LI void	*mpool_new(MPOOL *, pgno_t *, unsigned int);
1039fc74a87SXin LI void	*mpool_get(MPOOL *, pgno_t, unsigned int);
1049fc74a87SXin LI int	 mpool_delete(MPOOL *, void *);
1059fc74a87SXin LI int	 mpool_put(MPOOL *, void *, unsigned int);
106bb28f3c2SWarner Losh int	 mpool_sync(MPOOL *);
107bb28f3c2SWarner Losh int	 mpool_close(MPOOL *);
10859deaec5SRodney W. Grimes #ifdef STATISTICS
109bb28f3c2SWarner Losh void	 mpool_stat(MPOOL *);
11059deaec5SRodney W. Grimes #endif
11159deaec5SRodney W. Grimes __END_DECLS
11254b0ee63SPaul Richards 
11354b0ee63SPaul Richards #endif
114