mpool.h revision 1.16 1 1.16 christos /* $NetBSD: mpool.h,v 1.16 2016/09/24 21:18:09 christos Exp $ */
2 1.6 cgd
3 1.1 proven /*-
4 1.7 cgd * Copyright (c) 1991, 1993, 1994
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 proven *
7 1.1 proven * Redistribution and use in source and binary forms, with or without
8 1.1 proven * modification, are permitted provided that the following conditions
9 1.1 proven * are met:
10 1.1 proven * 1. Redistributions of source code must retain the above copyright
11 1.1 proven * notice, this list of conditions and the following disclaimer.
12 1.1 proven * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 proven * notice, this list of conditions and the following disclaimer in the
14 1.1 proven * documentation and/or other materials provided with the distribution.
15 1.10 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 proven * may be used to endorse or promote products derived from this software
17 1.1 proven * without specific prior written permission.
18 1.1 proven *
19 1.1 proven * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 proven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 proven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 proven * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 proven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 proven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 proven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 proven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 proven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 proven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 proven * SUCH DAMAGE.
30 1.1 proven *
31 1.7 cgd * @(#)mpool.h 8.2 (Berkeley) 7/14/94
32 1.1 proven */
33 1.1 proven
34 1.8 perry #ifndef _MPOOL_H_
35 1.8 perry #define _MPOOL_H_
36 1.8 perry
37 1.9 ad #include <sys/cdefs.h>
38 1.7 cgd #include <sys/queue.h>
39 1.7 cgd
40 1.1 proven /*
41 1.7 cgd * The memory pool scheme is a simple one. Each in-memory page is referenced
42 1.7 cgd * by a bucket which is threaded in up to two of three ways. All active pages
43 1.7 cgd * are threaded on a hash chain (hashed by page number) and an lru chain.
44 1.7 cgd * Inactive pages are threaded on a free chain. Each reference to a memory
45 1.7 cgd * pool is handed an opaque MPOOL cookie which stores all of this information.
46 1.1 proven */
47 1.1 proven #define HASHSIZE 128
48 1.1 proven #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE)
49 1.1 proven
50 1.7 cgd /* The BKT structures are the elements of the queues. */
51 1.7 cgd typedef struct _bkt {
52 1.14 christos TAILQ_ENTRY(_bkt) hq; /* hash queue */
53 1.14 christos TAILQ_ENTRY(_bkt) q; /* lru queue */
54 1.7 cgd void *page; /* page */
55 1.7 cgd pgno_t pgno; /* page number */
56 1.1 proven
57 1.1 proven #define MPOOL_DIRTY 0x01 /* page needs to be written */
58 1.1 proven #define MPOOL_PINNED 0x02 /* page is pinned into memory */
59 1.15 christos #define MPOOL_INUSE 0x04 /* page address is valid */
60 1.12 perry uint8_t flags; /* flags */
61 1.1 proven } BKT;
62 1.1 proven
63 1.1 proven typedef struct MPOOL {
64 1.14 christos TAILQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */
65 1.7 cgd /* hash queue array */
66 1.14 christos TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
67 1.7 cgd pgno_t curcache; /* current number of cached pages */
68 1.7 cgd pgno_t maxcache; /* max number of cached pages */
69 1.7 cgd pgno_t npages; /* number of pages in the file */
70 1.13 joerg unsigned long pagesize; /* file page size */
71 1.7 cgd int fd; /* file descriptor */
72 1.7 cgd /* page in conversion routine */
73 1.11 perry void (*pgin)(void *, pgno_t, void *);
74 1.7 cgd /* page out conversion routine */
75 1.11 perry void (*pgout)(void *, pgno_t, void *);
76 1.7 cgd void *pgcookie; /* cookie for page in/out routines */
77 1.1 proven #ifdef STATISTICS
78 1.13 joerg unsigned long cachehit;
79 1.13 joerg unsigned long cachemiss;
80 1.13 joerg unsigned long pagealloc;
81 1.13 joerg unsigned long pageflush;
82 1.13 joerg unsigned long pageget;
83 1.13 joerg unsigned long pagenew;
84 1.13 joerg unsigned long pageput;
85 1.13 joerg unsigned long pageread;
86 1.13 joerg unsigned long pagewrite;
87 1.1 proven #endif
88 1.1 proven } MPOOL;
89 1.1 proven
90 1.15 christos /* flags for get/put */
91 1.15 christos #define MPOOL_IGNOREPIN 0x01 /* Ignore if the page is pinned. */
92 1.15 christos /* flags for newf */
93 1.15 christos #define MPOOL_PAGE_REQUEST 0x01 /* Allocate a new page with a
94 1.15 christos specific page number. */
95 1.15 christos #define MPOOL_PAGE_NEXT 0x02 /* Allocate a new page with the next
96 1.15 christos page number. */
97 1.15 christos
98 1.1 proven __BEGIN_DECLS
99 1.11 perry MPOOL *mpool_open(void *, int, pgno_t, pgno_t);
100 1.11 perry void mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
101 1.11 perry void (*)(void *, pgno_t, void *), void *);
102 1.11 perry void *mpool_new(MPOOL *, pgno_t *);
103 1.15 christos void *mpool_newf(MPOOL *, pgno_t *, unsigned int);
104 1.15 christos int mpool_delete(MPOOL *, void *);
105 1.16 christos void *mpool_get(MPOOL *, pgno_t, unsigned int);
106 1.13 joerg int mpool_put(MPOOL *, void *, unsigned int);
107 1.11 perry int mpool_sync(MPOOL *);
108 1.11 perry int mpool_close(MPOOL *);
109 1.1 proven #ifdef STATISTICS
110 1.11 perry void mpool_stat(MPOOL *);
111 1.1 proven #endif
112 1.1 proven __END_DECLS
113 1.8 perry
114 1.8 perry #endif /* _MPOOL_H_ */
115