MISR Toolkit  1.5.1
mcache.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. Neither the name of the University nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /*****************************************************************************
32  * File: mcache.h
33  *
34  * This is a modfied version of the original Berkley code for
35  * manipulating a memory pool. This version however is not
36  * compatible with the original Berkley version.
37  *
38  * This version uses HDF number types.
39  *
40  * AUTHOR - George V.- 1996/08/22
41  *****************************************************************************/
42 
43 /* $Id$ */
44 
45 /*
46  * NOTE:
47  * Here pagesize is the same thing as chunk size and pages refer to chunks.
48  * I just didn't bother to change all references from pages to chunks.
49  *
50  * -georgev
51  */
52 
53 #ifndef _MCACHE_H
54 #define _MCACHE_H
55 
56 /* Required include */
57 #include "hqueue.h" /* Circluar queue functions(Macros) */
58 
59 /* Set return/succeed values */
60 #ifdef SUCCEED
61 #define RET_SUCCESS SUCCEED
62 #define RET_ERROR FAIL
63 #else
64 #define RET_SUCCESS 0
65 #define RET_ERROR -1
66 #endif
67 
68 /*
69  * The memory pool scheme is a simple one. Each in-memory page is referenced
70  * by a bucket which is threaded in up to two (three?) ways. All active pages
71  * are threaded on a hash chain (hashed by page number) and an lru chain.
72  * (Inactive pages are threaded on a free chain?). Each reference to a memory
73  * pool is handed an opaque MPOOL cookie which stores all of this information.
74  */
75 
76 /* Current Hash table size. Page numbers start with 1
77 * (i.e 0 will denote invalid page number) */
78 #define HASHSIZE 128
79 #define HASHKEY(pgno) ((pgno -1) % HASHSIZE)
80 
81 /* Default pagesize and max # of pages to cache */
82 #define DEF_PAGESIZE 8192
83 #define DEF_MAXCACHE 1
84 
85 #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a object */
86 
87 /* The BKT structures are the elements of the queues. */
88 typedef struct _bkt
89 {
90  CIRCLEQ_ENTRY(_bkt) hq; /* hash queue */
91  CIRCLEQ_ENTRY(_bkt) q; /* lru queue */
92  VOID *page; /* page */
93  int32 pgno; /* page number */
94 #define MCACHE_DIRTY 0x01 /* page needs to be written */
95 #define MCACHE_PINNED 0x02 /* page is pinned into memory */
96  uint8 flags; /* flags */
97 } BKT;
98 
99 /* The element structure for every page referenced(read/written) in object */
100 typedef struct _lelem
101 {
102  CIRCLEQ_ENTRY(_lelem) hl; /* hash list */
103  int32 pgno; /* page number */
104 #ifdef STATISTICS
105  int32 elemhit; /* # of hits on page */
106 #endif
107 #define ELEM_READ 0x01
108 #define ELEM_WRITTEN 0x02
109 #define ELEM_SYNC 0x03
110  uint8 eflags; /* 1= read, 2=written, 3=synced */
111 } L_ELEM;
112 
113 #define MCACHE_EXTEND 0x10 /* increase number of pages
114  i.e extend object */
115 
116 /* Memory pool cache */
117 typedef struct MCACHE
118 {
119  CIRCLEQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */
120  CIRCLEQ_HEAD(_hqh, _bkt) hqh[HASHSIZE]; /* hash queue array */
121  CIRCLEQ_HEAD(_lhqh, _lelem) lhqh[HASHSIZE]; /* hash of all elements */
122  int32 curcache; /* current num of cached pages */
123  int32 maxcache; /* max number of cached pages */
124  int32 npages; /* number of pages in the object */
125  int32 pagesize; /* cache page size */
126  int32 object_id; /* access ID of object this cache is for */
127  int32 object_size; /* size of object to cache
128  must be multiple of pagesize for now */
129  int32 (*pgin) (VOID *cookie, int32 pgno, VOID *page); /* page in conversion routine */
130  int32 (*pgout) (VOID *cookie, int32 pgno, const VOID *page);/* page out conversion routine*/
131  VOID *pgcookie; /* cookie for page in/out routines */
132 #ifdef STATISTICS
133  int32 listhit; /* # of list hits */
134  int32 listalloc; /* # of list elems allocated */
135  int32 cachehit; /* # of cache hits */
136  int32 cachemiss; /* # of cache misses */
137  int32 pagealloc; /* # of pages allocated */
138  int32 pageflush; /* # of pages flushed */
139  int32 pageget; /* # of pages requested from pool */
140  int32 pagenew; /* # of new pages */
141  int32 pageput; /* # of pages put back into pool */
142  int32 pageread; /* # of pages read from object */
143  int32 pagewrite; /* # of pages written to object */
144 #endif /* STATISTICS */
145 } MCACHE;
146 
147 #if defined c_plusplus || defined __cplusplus
148 extern "C"
149 {
150 #endif /* c_plusplus || __cplusplus */
151 
152 extern MCACHE *mcache_open (
153  VOID *key, /* IN:byte string used as handle to share buffers */
154  int32 object_id, /* IN: object handle */
155  int32 pagesize, /* IN: chunk size in bytes */
156  int32 maxcache, /* IN: maximum number of pages to cache at any time */
157  int32 npages, /* IN: number of chunks currently in object */
158  int32 flags /* IN: 0= object exists, 1= does not exist */);
159 
160 extern VOID mcache_filter (
161  MCACHE *mp, /* IN: MCACHE cookie */
162  int32 (*pgin)(VOID *cookie, int32 pgno, VOID *page) ,/* IN: page in filter */
163  int32 (*pgout)(VOID *cookie, int32 pgno, const VOID *page) , /* IN: page out filter */
164  VOID *pgcookie /* IN: filter cookie */);
165 
166 extern VOID *mcache_new (
167  MCACHE *mp, /* IN: MCACHE cookie */
168  int32 *pgnoaddr, /* IN/OUT: address of newly create page */
169  int32 flags /* IN:MCACHE_EXTEND or 0 */);
170 
171 
172 extern VOID *mcache_get (
173  MCACHE *mp, /* IN: MCACHE cookie */
174  int32 pgno, /* IN: page number */
175  int32 flags /* IN: XXX not used? */);
176 
177 extern intn mcache_put (
178  MCACHE *mp, /* IN: MCACHE cookie */
179  VOID *page, /* IN: page to put */
180  int32 flags /* IN: flags = 0, MCACHE_DIRTY */);
181 
182 extern intn mcache_sync (
183  MCACHE *mp /* IN: MCACHE cookie */);
184 
185 extern intn mcache_close (
186  MCACHE *mp /* IN: MCACHE cookie */);
187 
188 extern int32 mcache_get_pagesize (
189  MCACHE *mp /* IN: MCACHE cookie */);
190 
191 extern int32 mcache_get_maxcache (
192  MCACHE *mp /* IN: MCACHE cookie */);
193 
194 extern int32 mcache_set_maxcache (
195  MCACHE *mp, /* IN: MCACHE cookie */
196  int32 maxcache /* IN: max pages to cache */);
197 
198 extern int32 mcache_get_npages (
199  MCACHE *mp /* IN: MCACHE cookie */);
200 
201 #ifdef STATISTICS
202 extern VOID mcache_stat(
203  MCACHE *mp /* IN: MCACHE cookie */);
204 #endif /* STATISTICS */
205 #if 0 /* NOT USED */
206 extern intn mcache_page_sync (
207  MCACHE *mp, /* IN: MCACHE cookie */
208  int32 pgno, /* IN: page to sync */
209  int32 flags /* IN: flags */);
210 #endif
211 
212 #if defined c_plusplus || defined __cplusplus
213 }
214 #endif /* c_plusplus || __cplusplus */
215 
216 #endif /* _MCACHE_H */
Definition: mcache.h:117
int32 curcache
Definition: mcache.h:122
VOID * pgcookie
Definition: mcache.h:131
Definition: mcache.h:100
uint8 flags
Definition: mcache.h:96
int32 mcache_get_npages(MCACHE *mp)
VOID mcache_filter(MCACHE *mp, int32(*pgin)(VOID *cookie, int32 pgno, VOID *page), int32(*pgout)(VOID *cookie, int32 pgno, const VOID *page), VOID *pgcookie)
#define HASHSIZE
Definition: mcache.h:78
intn mcache_sync(MCACHE *mp)
VOID * mcache_get(MCACHE *mp, int32 pgno, int32 flags)
int32 pagesize
Definition: mcache.h:125
HDFFCLIBAPI intf * maxcache
int32 mcache_get_maxcache(MCACHE *mp)
int32 object_size
Definition: mcache.h:127
VOID * mcache_new(MCACHE *mp, int32 *pgnoaddr, int32 flags)
int32 object_id
Definition: mcache.h:126
MCACHE * mcache_open(VOID *key, int32 object_id, int32 pagesize, int32 maxcache, int32 npages, int32 flags)
int32 pgno
Definition: mcache.h:103
struct _bkt BKT
int32 mcache_set_maxcache(MCACHE *mp, int32 maxcache)
int32 mcache_get_pagesize(MCACHE *mp)
VOID * page
Definition: mcache.h:92
struct _lelem L_ELEM
int32 maxcache
Definition: mcache.h:123
int32 npages
Definition: mcache.h:124
Definition: mcache.h:88
struct MCACHE MCACHE
#define CIRCLEQ_HEAD(name, type)
Definition: hqueue.h:185
uint8 eflags
Definition: mcache.h:110
intn mcache_put(MCACHE *mp, VOID *page, int32 flags)
CIRCLEQ_ENTRY(_bkt) hq
intn mcache_close(MCACHE *mp)
int32 pgno
Definition: mcache.h:93

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:53