mpool.h revision 190498
137535Sdes/*-
2226537Sdes * Copyright (c) 1991, 1993, 1994
337535Sdes *	The Regents of the University of California.  All rights reserved.
437535Sdes *
537535Sdes * Redistribution and use in source and binary forms, with or without
637535Sdes * modification, are permitted provided that the following conditions
737535Sdes * are met:
837535Sdes * 1. Redistributions of source code must retain the above copyright
937535Sdes *    notice, this list of conditions and the following disclaimer.
1037535Sdes * 2. Redistributions in binary form must reproduce the above copyright
1137535Sdes *    notice, this list of conditions and the following disclaimer in the
1237535Sdes *    documentation and/or other materials provided with the distribution.
1337535Sdes * 3. All advertising materials mentioning features or use of this software
1437535Sdes *    must display the following acknowledgement:
1563012Sdes *	This product includes software developed by the University of
1637535Sdes *	California, Berkeley and its contributors.
1737535Sdes * 4. Neither the name of the University nor the names of its contributors
1837535Sdes *    may be used to endorse or promote products derived from this software
1937535Sdes *    without specific prior written permission.
2037535Sdes *
2137535Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2237535Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2337535Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2437535Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2537535Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2637535Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2737535Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2837535Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2984203Sdillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3084203Sdillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3184203Sdillon * SUCH DAMAGE.
3263236Sdes *
3363236Sdes *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
3463236Sdes * $FreeBSD: head/include/mpool.h 190498 2009-03-28 07:31:02Z delphij $
3563236Sdes */
3663236Sdes
3763236Sdes#ifndef _MPOOL_H_
3863236Sdes#define _MPOOL_H_
3963236Sdes
4063236Sdes#include <sys/queue.h>
4163236Sdes
4263236Sdes/*
4363236Sdes * The memory pool scheme is a simple one.  Each in-memory page is referenced
4463236Sdes * by a bucket which is threaded in up to two of three ways.  All active pages
4563236Sdes * are threaded on a hash chain (hashed by page number) and an lru chain.
4663236Sdes * Inactive pages are threaded on a free chain.  Each reference to a memory
4763236Sdes * pool is handed an opaque MPOOL cookie which stores all of this information.
4863236Sdes */
4990267Sdes#define	HASHSIZE	128
5063236Sdes#define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
5163236Sdes
5263236Sdes/* The BKT structures are the elements of the queues. */
5363236Sdestypedef struct _bkt {
5463236Sdes	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
5563236Sdes	TAILQ_ENTRY(_bkt) q;		/* lru queue */
5663236Sdes	void    *page;			/* page */
5763236Sdes	pgno_t   pgno;			/* page number */
5863236Sdes
5963236Sdes#define	MPOOL_DIRTY	0x01		/* page needs to be written */
6063236Sdes#define	MPOOL_PINNED	0x02		/* page is pinned into memory */
6163236Sdes#define	MPOOL_INUSE	0x04		/* page address is valid */
6263236Sdes	u_int8_t flags;			/* flags */
6363236Sdes} BKT;
6437535Sdes
6560737Sumetypedef struct MPOOL {
66186124Smurray	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
6737535Sdes					/* hash queue array */
6863012Sdes	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
6937535Sdes	pgno_t	curcache;		/* current number of cached pages */
7063012Sdes	pgno_t	maxcache;		/* max number of cached pages */
7160376Sdes	pgno_t	npages;			/* number of pages in the file */
7260189Sdes	unsigned long	pagesize;	/* file page size */
7337608Sdes	int	fd;			/* file descriptor */
7437535Sdes					/* page in conversion routine */
7537535Sdes	void    (*pgin)(void *, pgno_t, void *);
7637535Sdes					/* page out conversion routine */
7760376Sdes	void    (*pgout)(void *, pgno_t, void *);
7837535Sdes	void	*pgcookie;		/* cookie for page in/out routines */
79240496Sdes#ifdef STATISTICS
80240496Sdes	unsigned long	cachehit;
81240496Sdes	unsigned long	cachemiss;
82240496Sdes	unsigned long	pagealloc;
83240496Sdes	unsigned long	pageflush;
84240496Sdes	unsigned long	pageget;
85240496Sdes	unsigned long	pagenew;
86202613Sdes	unsigned long	pageput;
87240496Sdes	unsigned long	pageread;
8837535Sdes	unsigned long	pagewrite;
89141958Skbyanc#endif
90141958Skbyanc} MPOOL;
91141958Skbyanc
9237535Sdes#define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
9340939Sdes#define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
9441862Sdes					   specific page number. */
9537535Sdes#define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
9663012Sdes					  page number. */
9763012Sdes
9837535Sdes__BEGIN_DECLS
9963012SdesMPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
10063012Sdesvoid	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
10163012Sdes	    void (*)(void *, pgno_t, void *), void *);
10263012Sdesvoid	*mpool_new(MPOOL *, pgno_t *, unsigned int);
10363012Sdesvoid	*mpool_get(MPOOL *, pgno_t, unsigned int);
10463012Sdesint	 mpool_delete(MPOOL *, void *);
105186124Smurrayint	 mpool_put(MPOOL *, void *, unsigned int);
106169386Sdesint	 mpool_sync(MPOOL *);
10763012Sdesint	 mpool_close(MPOOL *);
10887317Sdes#ifdef STATISTICS
109125696Sdesvoid	 mpool_stat(MPOOL *);
11063012Sdes#endif
11160196Sdes__END_DECLS
11263012Sdes
11390267Sdes#endif
114169386Sdes