mpool.h revision 190498
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1991, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2950476Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178245Skib * SUCH DAMAGE.
321573Srgrimes *
3379531Sru *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
341573Srgrimes * $FreeBSD: head/include/mpool.h 190498 2009-03-28 07:31:02Z delphij $
351573Srgrimes */
3624443Speter
37178245Skib#ifndef _MPOOL_H_
38178245Skib#define _MPOOL_H_
391573Srgrimes
4059460Sphantom#include <sys/queue.h>
4159460Sphantom
421573Srgrimes/*
4384306Sru * The memory pool scheme is a simple one.  Each in-memory page is referenced
441573Srgrimes * by a bucket which is threaded in up to two of three ways.  All active pages
451573Srgrimes * are threaded on a hash chain (hashed by page number) and an lru chain.
461573Srgrimes * Inactive pages are threaded on a free chain.  Each reference to a memory
476511Sats * pool is handed an opaque MPOOL cookie which stores all of this information.
4824443Speter */
4924443Speter#define	HASHSIZE	128
50178245Skib#define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
51178245Skib
521573Srgrimes/* The BKT structures are the elements of the queues. */
531573Srgrimestypedef struct _bkt {
541573Srgrimes	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
551573Srgrimes	TAILQ_ENTRY(_bkt) q;		/* lru queue */
561573Srgrimes	void    *page;			/* page */
571573Srgrimes	pgno_t   pgno;			/* page number */
581573Srgrimes
591573Srgrimes#define	MPOOL_DIRTY	0x01		/* page needs to be written */
6079754Sdd#define	MPOOL_PINNED	0x02		/* page is pinned into memory */
611573Srgrimes#define	MPOOL_INUSE	0x04		/* page address is valid */
621573Srgrimes	u_int8_t flags;			/* flags */
631573Srgrimes} BKT;
641573Srgrimes
651573Srgrimestypedef struct MPOOL {
661573Srgrimes	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
671573Srgrimes					/* hash queue array */
681573Srgrimes	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
691573Srgrimes	pgno_t	curcache;		/* current number of cached pages */
70100144Skeramida	pgno_t	maxcache;		/* max number of cached pages */
71100144Skeramida	pgno_t	npages;			/* number of pages in the file */
72108028Sru	unsigned long	pagesize;	/* file page size */
731573Srgrimes	int	fd;			/* file descriptor */
741573Srgrimes					/* page in conversion routine */
751573Srgrimes	void    (*pgin)(void *, pgno_t, void *);
7613974Spst					/* page out conversion routine */
7713974Spst	void    (*pgout)(void *, pgno_t, void *);
78108028Sru	void	*pgcookie;		/* cookie for page in/out routines */
7924443Speter#ifdef STATISTICS
80108028Sru	unsigned long	cachehit;
8124443Speter	unsigned long	cachemiss;
8224443Speter	unsigned long	pagealloc;
831573Srgrimes	unsigned long	pageflush;
84100144Skeramida	unsigned long	pageget;
85100144Skeramida	unsigned long	pagenew;
86108028Sru	unsigned long	pageput;
871573Srgrimes	unsigned long	pageread;
881573Srgrimes	unsigned long	pagewrite;
891573Srgrimes#endif
901573Srgrimes} MPOOL;
91100144Skeramida
92100144Skeramida#define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
93108028Sru#define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
9424443Speter					   specific page number. */
9524443Speter#define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
9624443Speter					  page number. */
97178245Skib
98178245Skib__BEGIN_DECLS
99178245SkibMPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
100178245Skibvoid	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
101178245Skib	    void (*)(void *, pgno_t, void *), void *);
102178245Skibvoid	*mpool_new(MPOOL *, pgno_t *, unsigned int);
103178245Skibvoid	*mpool_get(MPOOL *, pgno_t, unsigned int);
104178245Skibint	 mpool_delete(MPOOL *, void *);
105178245Skibint	 mpool_put(MPOOL *, void *, unsigned int);
106178245Skibint	 mpool_sync(MPOOL *);
107178245Skibint	 mpool_close(MPOOL *);
108178245Skib#ifdef STATISTICS
109178245Skibvoid	 mpool_stat(MPOOL *);
110178245Skib#endif
111178245Skib__END_DECLS
112178245Skib
113178245Skib#endif
114178245Skib