mpool.h revision 203964
150276Speter/*-
250276Speter * Copyright (c) 1991, 1993, 1994
350276Speter *	The Regents of the University of California.  All rights reserved.
450276Speter *
550276Speter * Redistribution and use in source and binary forms, with or without
650276Speter * modification, are permitted provided that the following conditions
750276Speter * are met:
850276Speter * 1. Redistributions of source code must retain the above copyright
950276Speter *    notice, this list of conditions and the following disclaimer.
1050276Speter * 2. Redistributions in binary form must reproduce the above copyright
1150276Speter *    notice, this list of conditions and the following disclaimer in the
1250276Speter *    documentation and/or other materials provided with the distribution.
1350276Speter * 3. Neither the name of the University nor the names of its contributors
1450276Speter *    may be used to endorse or promote products derived from this software
1550276Speter *    without specific prior written permission.
1650276Speter *
1750276Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1850276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1950276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2050276Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2150276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2250276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2350276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2450276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2550276Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2650276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2750276Speter * SUCH DAMAGE.
2850276Speter *
2950276Speter *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
3050276Speter * $FreeBSD: head/include/mpool.h 203964 2010-02-16 19:39:50Z imp $
3150276Speter */
3250276Speter
3350276Speter#ifndef _MPOOL_H_
3450276Speter#define _MPOOL_H_
3550276Speter
3650276Speter#include <sys/queue.h>
3750276Speter
3850276Speter/*
3950276Speter * The memory pool scheme is a simple one.  Each in-memory page is referenced
4050276Speter * by a bucket which is threaded in up to two of three ways.  All active pages
4150276Speter * are threaded on a hash chain (hashed by page number) and an lru chain.
4250276Speter * Inactive pages are threaded on a free chain.  Each reference to a memory
4350276Speter * pool is handed an opaque MPOOL cookie which stores all of this information.
4450276Speter */
4550276Speter#define	HASHSIZE	128
4650276Speter#define	HASHKEY(pgno)	((pgno - 1 + HASHSIZE) % HASHSIZE)
4750276Speter
4850276Speter/* The BKT structures are the elements of the queues. */
4950276Spetertypedef struct _bkt {
5050276Speter	TAILQ_ENTRY(_bkt) hq;		/* hash queue */
5150276Speter	TAILQ_ENTRY(_bkt) q;		/* lru queue */
5250276Speter	void    *page;			/* page */
5350276Speter	pgno_t   pgno;			/* page number */
5450276Speter
5550276Speter#define	MPOOL_DIRTY	0x01		/* page needs to be written */
5650276Speter#define	MPOOL_PINNED	0x02		/* page is pinned into memory */
5750276Speter#define	MPOOL_INUSE	0x04		/* page address is valid */
5850276Speter	u_int8_t flags;			/* flags */
5950276Speter} BKT;
6050276Speter
6150276Spetertypedef struct MPOOL {
6250276Speter	TAILQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
6350276Speter					/* hash queue array */
64	TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
65	pgno_t	curcache;		/* current number of cached pages */
66	pgno_t	maxcache;		/* max number of cached pages */
67	pgno_t	npages;			/* number of pages in the file */
68	unsigned long	pagesize;	/* file page size */
69	int	fd;			/* file descriptor */
70					/* page in conversion routine */
71	void    (*pgin)(void *, pgno_t, void *);
72					/* page out conversion routine */
73	void    (*pgout)(void *, pgno_t, void *);
74	void	*pgcookie;		/* cookie for page in/out routines */
75#ifdef STATISTICS
76	unsigned long	cachehit;
77	unsigned long	cachemiss;
78	unsigned long	pagealloc;
79	unsigned long	pageflush;
80	unsigned long	pageget;
81	unsigned long	pagenew;
82	unsigned long	pageput;
83	unsigned long	pageread;
84	unsigned long	pagewrite;
85#endif
86} MPOOL;
87
88#define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
89#define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
90					   specific page number. */
91#define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
92					  page number. */
93
94__BEGIN_DECLS
95MPOOL	*mpool_open(void *, int, pgno_t, pgno_t);
96void	 mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
97	    void (*)(void *, pgno_t, void *), void *);
98void	*mpool_new(MPOOL *, pgno_t *, unsigned int);
99void	*mpool_get(MPOOL *, pgno_t, unsigned int);
100int	 mpool_delete(MPOOL *, void *);
101int	 mpool_put(MPOOL *, void *, unsigned int);
102int	 mpool_sync(MPOOL *);
103int	 mpool_close(MPOOL *);
104#ifdef STATISTICS
105void	 mpool_stat(MPOOL *);
106#endif
107__END_DECLS
108
109#endif
110