xref: /freebsd/include/mpool.h (revision f2556687c6724a3c8b3685103aad511afb3068b2)
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.
13f2556687SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1459deaec5SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1559deaec5SRodney W. Grimes  *    without specific prior written permission.
1659deaec5SRodney W. Grimes  *
1759deaec5SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1859deaec5SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1959deaec5SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2059deaec5SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2159deaec5SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2259deaec5SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2359deaec5SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2459deaec5SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2559deaec5SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2659deaec5SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2759deaec5SRodney W. Grimes  * SUCH DAMAGE.
2859deaec5SRodney W. Grimes  *
299fc74a87SXin LI  *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
30740a1973SJake Burkholder  * $FreeBSD$
3159deaec5SRodney W. Grimes  */
3259deaec5SRodney W. Grimes 
3354b0ee63SPaul Richards #ifndef _MPOOL_H_
3454b0ee63SPaul Richards #define _MPOOL_H_
3554b0ee63SPaul Richards 
3690fd5bdfSPaul Traina #include <sys/queue.h>
3790fd5bdfSPaul Traina 
3859deaec5SRodney W. Grimes /*
3990fd5bdfSPaul Traina  * The memory pool scheme is a simple one.  Each in-memory page is referenced
4090fd5bdfSPaul Traina  * by a bucket which is threaded in up to two of three ways.  All active pages
4190fd5bdfSPaul Traina  * are threaded on a hash chain (hashed by page number) and an lru chain.
4290fd5bdfSPaul Traina  * Inactive pages are threaded on a free chain.  Each reference to a memory
4390fd5bdfSPaul Traina  * pool is handed an opaque MPOOL cookie which stores all of this information.
4459deaec5SRodney W. Grimes  */
4559deaec5SRodney W. Grimes #define	HASHSIZE	128
469fc74a87SXin LI #define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
4759deaec5SRodney W. Grimes 
4890fd5bdfSPaul Traina /* The BKT structures are the elements of the queues. */
4990fd5bdfSPaul Traina typedef struct _bkt {
50fabacd3aSPoul-Henning Kamp 	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
51fabacd3aSPoul-Henning Kamp 	TAILQ_ENTRY(_bkt) q;		/* lru queue */
5259deaec5SRodney W. Grimes 	void    *page;			/* page */
5359deaec5SRodney W. Grimes 	pgno_t   pgno;			/* page number */
5459deaec5SRodney W. Grimes 
5559deaec5SRodney W. Grimes #define	MPOOL_DIRTY	0x01		/* page needs to be written */
5659deaec5SRodney W. Grimes #define	MPOOL_PINNED	0x02		/* page is pinned into memory */
579fc74a87SXin LI #define	MPOOL_INUSE	0x04		/* page address is valid */
5890fd5bdfSPaul Traina 	u_int8_t flags;			/* flags */
5959deaec5SRodney W. Grimes } BKT;
6059deaec5SRodney W. Grimes 
6159deaec5SRodney W. Grimes typedef struct MPOOL {
62fabacd3aSPoul-Henning Kamp 	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
6390fd5bdfSPaul Traina 					/* hash queue array */
64fabacd3aSPoul-Henning Kamp 	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
6590fd5bdfSPaul Traina 	pgno_t	curcache;		/* current number of cached pages */
6690fd5bdfSPaul Traina 	pgno_t	maxcache;		/* max number of cached pages */
6790fd5bdfSPaul Traina 	pgno_t	npages;			/* number of pages in the file */
689fc74a87SXin LI 	unsigned long	pagesize;	/* file page size */
6990fd5bdfSPaul Traina 	int	fd;			/* file descriptor */
7090fd5bdfSPaul Traina 					/* page in conversion routine */
71bb28f3c2SWarner Losh 	void    (*pgin)(void *, pgno_t, void *);
7290fd5bdfSPaul Traina 					/* page out conversion routine */
73bb28f3c2SWarner Losh 	void    (*pgout)(void *, pgno_t, void *);
7490fd5bdfSPaul Traina 	void	*pgcookie;		/* cookie for page in/out routines */
7559deaec5SRodney W. Grimes #ifdef STATISTICS
769fc74a87SXin LI 	unsigned long	cachehit;
779fc74a87SXin LI 	unsigned long	cachemiss;
789fc74a87SXin LI 	unsigned long	pagealloc;
799fc74a87SXin LI 	unsigned long	pageflush;
809fc74a87SXin LI 	unsigned long	pageget;
819fc74a87SXin LI 	unsigned long	pagenew;
829fc74a87SXin LI 	unsigned long	pageput;
839fc74a87SXin LI 	unsigned long	pageread;
849fc74a87SXin LI 	unsigned long	pagewrite;
8559deaec5SRodney W. Grimes #endif
8659deaec5SRodney W. Grimes } MPOOL;
8759deaec5SRodney W. Grimes 
889fc74a87SXin LI #define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
899fc74a87SXin LI #define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
909fc74a87SXin LI 					   specific page number. */
919fc74a87SXin LI #define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
929fc74a87SXin LI 					  page number. */
939fc74a87SXin LI 
9459deaec5SRodney W. Grimes __BEGIN_DECLS
95bb28f3c2SWarner Losh MPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
96bb28f3c2SWarner Losh void	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
97bb28f3c2SWarner Losh 	    void (*)(void *, pgno_t, void *), void *);
989fc74a87SXin LI void	*mpool_new(MPOOL *, pgno_t *, unsigned int);
999fc74a87SXin LI void	*mpool_get(MPOOL *, pgno_t, unsigned int);
1009fc74a87SXin LI int	 mpool_delete(MPOOL *, void *);
1019fc74a87SXin LI int	 mpool_put(MPOOL *, void *, unsigned int);
102bb28f3c2SWarner Losh int	 mpool_sync(MPOOL *);
103bb28f3c2SWarner Losh int	 mpool_close(MPOOL *);
10459deaec5SRodney W. Grimes #ifdef STATISTICS
105bb28f3c2SWarner Losh void	 mpool_stat(MPOOL *);
10659deaec5SRodney W. Grimes #endif
10759deaec5SRodney W. Grimes __END_DECLS
10854b0ee63SPaul Richards 
10954b0ee63SPaul Richards #endif
110