11539Srgrimes/*-
214288Spst * Copyright (c) 1991, 1993, 1994
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes *
51539Srgrimes * Redistribution and use in source and binary forms, with or without
61539Srgrimes * modification, are permitted provided that the following conditions
71539Srgrimes * are met:
81539Srgrimes * 1. Redistributions of source code must retain the above copyright
91539Srgrimes *    notice, this list of conditions and the following disclaimer.
101539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111539Srgrimes *    notice, this list of conditions and the following disclaimer in the
121539Srgrimes *    documentation and/or other materials provided with the distribution.
13203964Simp * 3. Neither the name of the University nor the names of its contributors
141539Srgrimes *    may be used to endorse or promote products derived from this software
151539Srgrimes *    without specific prior written permission.
161539Srgrimes *
171539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271539Srgrimes * SUCH DAMAGE.
281539Srgrimes *
29190498Sdelphij *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
3060833Sjake * $FreeBSD$
311539Srgrimes */
321539Srgrimes
332162Spaul#ifndef _MPOOL_H_
342162Spaul#define _MPOOL_H_
352162Spaul
3614288Spst#include <sys/queue.h>
3714288Spst
381539Srgrimes/*
3914288Spst * The memory pool scheme is a simple one.  Each in-memory page is referenced
4014288Spst * by a bucket which is threaded in up to two of three ways.  All active pages
4114288Spst * are threaded on a hash chain (hashed by page number) and an lru chain.
4214288Spst * Inactive pages are threaded on a free chain.  Each reference to a memory
4314288Spst * pool is handed an opaque MPOOL cookie which stores all of this information.
441539Srgrimes */
451539Srgrimes#define	HASHSIZE	128
46190498Sdelphij#define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
471539Srgrimes
4814288Spst/* The BKT structures are the elements of the queues. */
4914288Spsttypedef struct _bkt {
5070492Sphk	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
5170492Sphk	TAILQ_ENTRY(_bkt) q;		/* lru queue */
5214288Spst	void    *page;			/* page */
5314288Spst	pgno_t   pgno;			/* page number */
541539Srgrimes
551539Srgrimes#define	MPOOL_DIRTY	0x01		/* page needs to be written */
561539Srgrimes#define	MPOOL_PINNED	0x02		/* page is pinned into memory */
57190498Sdelphij#define	MPOOL_INUSE	0x04		/* page address is valid */
5814288Spst	u_int8_t flags;			/* flags */
591539Srgrimes} BKT;
601539Srgrimes
611539Srgrimestypedef struct MPOOL {
6270492Sphk	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
6314288Spst					/* hash queue array */
6470492Sphk	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
6514288Spst	pgno_t	curcache;		/* current number of cached pages */
6614288Spst	pgno_t	maxcache;		/* max number of cached pages */
6714288Spst	pgno_t	npages;			/* number of pages in the file */
68190498Sdelphij	unsigned long	pagesize;	/* file page size */
6914288Spst	int	fd;			/* file descriptor */
7014288Spst					/* page in conversion routine */
7193032Simp	void    (*pgin)(void *, pgno_t, void *);
7214288Spst					/* page out conversion routine */
7393032Simp	void    (*pgout)(void *, pgno_t, void *);
7414288Spst	void	*pgcookie;		/* cookie for page in/out routines */
751539Srgrimes#ifdef STATISTICS
76190498Sdelphij	unsigned long	cachehit;
77190498Sdelphij	unsigned long	cachemiss;
78190498Sdelphij	unsigned long	pagealloc;
79190498Sdelphij	unsigned long	pageflush;
80190498Sdelphij	unsigned long	pageget;
81190498Sdelphij	unsigned long	pagenew;
82190498Sdelphij	unsigned long	pageput;
83190498Sdelphij	unsigned long	pageread;
84190498Sdelphij	unsigned long	pagewrite;
851539Srgrimes#endif
861539Srgrimes} MPOOL;
871539Srgrimes
88190498Sdelphij#define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
89190498Sdelphij#define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
90190498Sdelphij					   specific page number. */
91190498Sdelphij#define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
92190498Sdelphij					  page number. */
93190498Sdelphij
941539Srgrimes__BEGIN_DECLS
9593032SimpMPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
9693032Simpvoid	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
9793032Simp	    void (*)(void *, pgno_t, void *), void *);
98190498Sdelphijvoid	*mpool_new(MPOOL *, pgno_t *, unsigned int);
99190498Sdelphijvoid	*mpool_get(MPOOL *, pgno_t, unsigned int);
100190498Sdelphijint	 mpool_delete(MPOOL *, void *);
101190498Sdelphijint	 mpool_put(MPOOL *, void *, unsigned int);
10293032Simpint	 mpool_sync(MPOOL *);
10393032Simpint	 mpool_close(MPOOL *);
1041539Srgrimes#ifdef STATISTICS
10593032Simpvoid	 mpool_stat(MPOOL *);
1061539Srgrimes#endif
1071539Srgrimes__END_DECLS
1082162Spaul
1092162Spaul#endif
110