1/*
2 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6#ifndef _KRB5_DB2_MPOOL_MPOOL_H
7#define	_KRB5_DB2_MPOOL_MPOOL_H
8
9#pragma ident	"%Z%%M%	%I%	%E% SMI"
10
11#ifdef	__cplusplus
12extern "C" {
13#endif
14
15/*
16 * Copyright (c) 1991, 1993, 1994
17 *	The Regents of the University of California.  All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 *    notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 *    notice, this list of conditions and the following disclaimer in the
26 *    documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 *    must display the following acknowledgement:
29 *	This product includes software developed by the University of
30 *	California, Berkeley and its contributors.
31 * 4. Neither the name of the University nor the names of its contributors
32 *    may be used to endorse or promote products derived from this software
33 *    without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 *	@(#)mpool.h	8.4 (Berkeley) 11/2/95
48 */
49
50#include "db-queue.h"
51
52/*
53 * The memory pool scheme is a simple one.  Each in-memory page is referenced
54 * by a bucket which is threaded in up to two of three ways.  All active pages
55 * are threaded on a hash chain (hashed by page number) and an lru chain.
56 * Inactive pages are threaded on a free chain.  Each reference to a memory
57 * pool is handed an opaque MPOOL cookie which stores all of this information.
58 */
59#define	HASHSIZE	128
60#define	HASHKEY(pgno)	((pgno - 1) % HASHSIZE)
61
62/* The BKT structures are the elements of the queues. */
63typedef struct _bkt {
64	CIRCLEQ_ENTRY(_bkt) hq;		/* hash queue */
65	CIRCLEQ_ENTRY(_bkt) q;		/* lru queue */
66	void    *page;			/* page */
67	db_pgno_t   pgno;			/* page number */
68
69#define	MPOOL_DIRTY	0x01		/* page needs to be written */
70#define	MPOOL_PINNED	0x02		/* page is pinned into memory */
71#define	MPOOL_INUSE	0x04		/* page address is valid */
72	u_int8_t flags;			/* flags */
73} BKT;
74
75typedef struct MPOOL {
76	CIRCLEQ_HEAD(_lqh, _bkt) lqh;	/* lru queue head */
77					/* hash queue array */
78	CIRCLEQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
79	db_pgno_t	curcache;		/* current number of cached pages */
80	db_pgno_t	maxcache;		/* max number of cached pages */
81	db_pgno_t	npages;			/* number of pages in the file */
82	u_long	pagesize;		/* file page size */
83	int	fd;			/* file descriptor */
84					/* page in conversion routine */
85	void    (*pgin) __P((void *, db_pgno_t, void *));
86					/* page out conversion routine */
87	void    (*pgout) __P((void *, db_pgno_t, void *));
88	void	*pgcookie;		/* cookie for page in/out routines */
89#ifdef STATISTICS
90	u_long	cachehit;
91	u_long	cachemiss;
92	u_long	pagealloc;
93	u_long	pageflush;
94	u_long	pageget;
95	u_long	pagenew;
96	u_long	pageput;
97	u_long	pageread;
98	u_long	pagewrite;
99#endif
100} MPOOL;
101
102#define	MPOOL_IGNOREPIN	0x01		/* Ignore if the page is pinned. */
103#define	MPOOL_PAGE_REQUEST	0x01	/* Allocate a new page with a
104					   specific page number. */
105#define	MPOOL_PAGE_NEXT		0x02	/* Allocate a new page with the next
106					  page number. */
107
108#define mpool_open	kdb2_mpool_open
109#define mpool_filter	kdb2_mpool_filter
110#define mpool_new	kdb2_mpool_new
111#define mpool_get	kdb2_mpool_get
112#define mpool_delete	kdb2_mpool_delete
113#define mpool_put	kdb2_mpool_put
114#define mpool_sync	kdb2_mpool_sync
115#define mpool_close	kdb2_mpool_close
116#define mpool_stat	kdb2_mpool_stat
117
118__BEGIN_DECLS
119MPOOL	*mpool_open __P((void *, int, db_pgno_t, db_pgno_t));
120void	 mpool_filter __P((MPOOL *, void (*)(void *, db_pgno_t, void *),
121	    void (*)(void *, db_pgno_t, void *), void *));
122void	*mpool_new __P((MPOOL *, db_pgno_t *, u_int));
123void	*mpool_get __P((MPOOL *, db_pgno_t, u_int));
124int	 mpool_delete __P((MPOOL *, void *));
125int	 mpool_put __P((MPOOL *, void *, u_int));
126int	 mpool_sync __P((MPOOL *));
127int	 mpool_close __P((MPOOL *));
128#ifdef STATISTICS
129void	 mpool_stat __P((MPOOL *));
130#endif
131__END_DECLS
132
133#ifdef	__cplusplus
134}
135#endif
136
137#endif	/* !_KRB5_DB2_MPOOL_MPOOL_H */
138