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