xref: /freebsd/include/mpool.h (revision 5a1d14419a5b620430949a46cb6ee63148a43cb9)
159deaec5SRodney W. Grimes /*-
2*2321c474SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*2321c474SPedro F. Giffuni  *
490fd5bdfSPaul Traina  * Copyright (c) 1991, 1993, 1994
559deaec5SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
659deaec5SRodney W. Grimes  *
759deaec5SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
859deaec5SRodney W. Grimes  * modification, are permitted provided that the following conditions
959deaec5SRodney W. Grimes  * are met:
1059deaec5SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1159deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1259deaec5SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1359deaec5SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1459deaec5SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15f2556687SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1659deaec5SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1759deaec5SRodney W. Grimes  *    without specific prior written permission.
1859deaec5SRodney W. Grimes  *
1959deaec5SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2059deaec5SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2159deaec5SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2259deaec5SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2359deaec5SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2459deaec5SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2559deaec5SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2659deaec5SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2759deaec5SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2859deaec5SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2959deaec5SRodney W. Grimes  * SUCH DAMAGE.
3059deaec5SRodney W. Grimes  */
3159deaec5SRodney W. Grimes 
3254b0ee63SPaul Richards #ifndef _MPOOL_H_
3354b0ee63SPaul Richards #define _MPOOL_H_
3454b0ee63SPaul Richards 
3590fd5bdfSPaul Traina #include <sys/queue.h>
3690fd5bdfSPaul Traina 
3759deaec5SRodney W. Grimes /*
3890fd5bdfSPaul Traina  * The memory pool scheme is a simple one.  Each in-memory page is referenced
3990fd5bdfSPaul Traina  * by a bucket which is threaded in up to two of three ways.  All active pages
4090fd5bdfSPaul Traina  * are threaded on a hash chain (hashed by page number) and an lru chain.
4190fd5bdfSPaul Traina  * Inactive pages are threaded on a free chain.  Each reference to a memory
4290fd5bdfSPaul Traina  * pool is handed an opaque MPOOL cookie which stores all of this information.
4359deaec5SRodney W. Grimes  */
4459deaec5SRodney W. Grimes #define	HASHSIZE	128
459fc74a87SXin LI #define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
4659deaec5SRodney W. Grimes 
4790fd5bdfSPaul Traina /* The BKT structures are the elements of the queues. */
4890fd5bdfSPaul Traina typedef struct _bkt {
49fabacd3aSPoul-Henning Kamp 	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
50fabacd3aSPoul-Henning Kamp 	TAILQ_ENTRY(_bkt) q;		/* lru queue */
5159deaec5SRodney W. Grimes 	void    *page;			/* page */
5259deaec5SRodney W. Grimes 	pgno_t   pgno;			/* page number */
5359deaec5SRodney W. Grimes 
5459deaec5SRodney W. Grimes #define	MPOOL_DIRTY	0x01		/* page needs to be written */
5559deaec5SRodney W. Grimes #define	MPOOL_PINNED	0x02		/* page is pinned into memory */
569fc74a87SXin LI #define	MPOOL_INUSE	0x04		/* page address is valid */
5790fd5bdfSPaul Traina 	u_int8_t flags;			/* flags */
5859deaec5SRodney W. Grimes } BKT;
5959deaec5SRodney W. Grimes 
6059deaec5SRodney W. Grimes typedef struct MPOOL {
61fabacd3aSPoul-Henning Kamp 	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
6290fd5bdfSPaul Traina 					/* hash queue array */
63fabacd3aSPoul-Henning Kamp 	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
6490fd5bdfSPaul Traina 	pgno_t	curcache;		/* current number of cached pages */
6590fd5bdfSPaul Traina 	pgno_t	maxcache;		/* max number of cached pages */
6690fd5bdfSPaul Traina 	pgno_t	npages;			/* number of pages in the file */
679fc74a87SXin LI 	unsigned long	pagesize;	/* file page size */
6890fd5bdfSPaul Traina 	int	fd;			/* file descriptor */
6990fd5bdfSPaul Traina 					/* page in conversion routine */
70bb28f3c2SWarner Losh 	void    (*pgin)(void *, pgno_t, void *);
7190fd5bdfSPaul Traina 					/* page out conversion routine */
72bb28f3c2SWarner Losh 	void    (*pgout)(void *, pgno_t, void *);
7390fd5bdfSPaul Traina 	void	*pgcookie;		/* cookie for page in/out routines */
7459deaec5SRodney W. Grimes #ifdef STATISTICS
759fc74a87SXin LI 	unsigned long	cachehit;
769fc74a87SXin LI 	unsigned long	cachemiss;
779fc74a87SXin LI 	unsigned long	pagealloc;
789fc74a87SXin LI 	unsigned long	pageflush;
799fc74a87SXin LI 	unsigned long	pageget;
809fc74a87SXin LI 	unsigned long	pagenew;
819fc74a87SXin LI 	unsigned long	pageput;
829fc74a87SXin LI 	unsigned long	pageread;
839fc74a87SXin LI 	unsigned long	pagewrite;
8459deaec5SRodney W. Grimes #endif
8559deaec5SRodney W. Grimes } MPOOL;
8659deaec5SRodney W. Grimes 
879fc74a87SXin LI #define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
889fc74a87SXin LI #define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
899fc74a87SXin LI 					   specific page number. */
909fc74a87SXin LI #define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
919fc74a87SXin LI 					  page number. */
929fc74a87SXin LI 
9359deaec5SRodney W. Grimes __BEGIN_DECLS
94bb28f3c2SWarner Losh MPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
95bb28f3c2SWarner Losh void	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
96bb28f3c2SWarner Losh 	    void (*)(void *, pgno_t, void *), void *);
979fc74a87SXin LI void	*mpool_new(MPOOL *, pgno_t *, unsigned int);
989fc74a87SXin LI void	*mpool_get(MPOOL *, pgno_t, unsigned int);
999fc74a87SXin LI int	 mpool_delete(MPOOL *, void *);
1009fc74a87SXin LI int	 mpool_put(MPOOL *, void *, unsigned int);
101bb28f3c2SWarner Losh int	 mpool_sync(MPOOL *);
102bb28f3c2SWarner Losh int	 mpool_close(MPOOL *);
10359deaec5SRodney W. Grimes #ifdef STATISTICS
104bb28f3c2SWarner Losh void	 mpool_stat(MPOOL *);
10559deaec5SRodney W. Grimes #endif
10659deaec5SRodney W. Grimes __END_DECLS
10754b0ee63SPaul Richards 
10854b0ee63SPaul Richards #endif
109