1*84d9c625SLionel Sambuc /* $NetBSD: mpool.h,v 1.14 2013/11/22 16:25:01 christos Exp $ */ 24fcd3413SBen Gras 34fcd3413SBen Gras /*- 44fcd3413SBen Gras * Copyright (c) 1991, 1993, 1994 54fcd3413SBen Gras * The Regents of the University of California. All rights reserved. 64fcd3413SBen Gras * 74fcd3413SBen Gras * Redistribution and use in source and binary forms, with or without 84fcd3413SBen Gras * modification, are permitted provided that the following conditions 94fcd3413SBen Gras * are met: 104fcd3413SBen Gras * 1. Redistributions of source code must retain the above copyright 114fcd3413SBen Gras * notice, this list of conditions and the following disclaimer. 124fcd3413SBen Gras * 2. Redistributions in binary form must reproduce the above copyright 134fcd3413SBen Gras * notice, this list of conditions and the following disclaimer in the 144fcd3413SBen Gras * documentation and/or other materials provided with the distribution. 154fcd3413SBen Gras * 3. Neither the name of the University nor the names of its contributors 164fcd3413SBen Gras * may be used to endorse or promote products derived from this software 174fcd3413SBen Gras * without specific prior written permission. 184fcd3413SBen Gras * 194fcd3413SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 204fcd3413SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 214fcd3413SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 224fcd3413SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 234fcd3413SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 244fcd3413SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 254fcd3413SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 264fcd3413SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 274fcd3413SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 284fcd3413SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 294fcd3413SBen Gras * SUCH DAMAGE. 304fcd3413SBen Gras * 314fcd3413SBen Gras * @(#)mpool.h 8.2 (Berkeley) 7/14/94 324fcd3413SBen Gras */ 334fcd3413SBen Gras 344fcd3413SBen Gras #ifndef _MPOOL_H_ 354fcd3413SBen Gras #define _MPOOL_H_ 364fcd3413SBen Gras 374fcd3413SBen Gras #include <sys/cdefs.h> 384fcd3413SBen Gras #include <sys/queue.h> 394fcd3413SBen Gras 404fcd3413SBen Gras /* 414fcd3413SBen Gras * The memory pool scheme is a simple one. Each in-memory page is referenced 424fcd3413SBen Gras * by a bucket which is threaded in up to two of three ways. All active pages 434fcd3413SBen Gras * are threaded on a hash chain (hashed by page number) and an lru chain. 444fcd3413SBen Gras * Inactive pages are threaded on a free chain. Each reference to a memory 454fcd3413SBen Gras * pool is handed an opaque MPOOL cookie which stores all of this information. 464fcd3413SBen Gras */ 474fcd3413SBen Gras #define HASHSIZE 128 484fcd3413SBen Gras #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE) 494fcd3413SBen Gras 504fcd3413SBen Gras /* The BKT structures are the elements of the queues. */ 514fcd3413SBen Gras typedef struct _bkt { 52*84d9c625SLionel Sambuc TAILQ_ENTRY(_bkt) hq; /* hash queue */ 53*84d9c625SLionel Sambuc TAILQ_ENTRY(_bkt) q; /* lru queue */ 544fcd3413SBen Gras void *page; /* page */ 554fcd3413SBen Gras pgno_t pgno; /* page number */ 564fcd3413SBen Gras 574fcd3413SBen Gras #define MPOOL_DIRTY 0x01 /* page needs to be written */ 584fcd3413SBen Gras #define MPOOL_PINNED 0x02 /* page is pinned into memory */ 594fcd3413SBen Gras uint8_t flags; /* flags */ 604fcd3413SBen Gras } BKT; 614fcd3413SBen Gras 624fcd3413SBen Gras typedef struct MPOOL { 63*84d9c625SLionel Sambuc TAILQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */ 644fcd3413SBen Gras /* hash queue array */ 65*84d9c625SLionel Sambuc TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE]; 664fcd3413SBen Gras pgno_t curcache; /* current number of cached pages */ 674fcd3413SBen Gras pgno_t maxcache; /* max number of cached pages */ 684fcd3413SBen Gras pgno_t npages; /* number of pages in the file */ 694fcd3413SBen Gras unsigned long pagesize; /* file page size */ 704fcd3413SBen Gras int fd; /* file descriptor */ 714fcd3413SBen Gras /* page in conversion routine */ 724fcd3413SBen Gras void (*pgin)(void *, pgno_t, void *); 734fcd3413SBen Gras /* page out conversion routine */ 744fcd3413SBen Gras void (*pgout)(void *, pgno_t, void *); 754fcd3413SBen Gras void *pgcookie; /* cookie for page in/out routines */ 764fcd3413SBen Gras #ifdef STATISTICS 774fcd3413SBen Gras unsigned long cachehit; 784fcd3413SBen Gras unsigned long cachemiss; 794fcd3413SBen Gras unsigned long pagealloc; 804fcd3413SBen Gras unsigned long pageflush; 814fcd3413SBen Gras unsigned long pageget; 824fcd3413SBen Gras unsigned long pagenew; 834fcd3413SBen Gras unsigned long pageput; 844fcd3413SBen Gras unsigned long pageread; 854fcd3413SBen Gras unsigned long pagewrite; 864fcd3413SBen Gras #endif 874fcd3413SBen Gras } MPOOL; 884fcd3413SBen Gras 894fcd3413SBen Gras __BEGIN_DECLS 904fcd3413SBen Gras MPOOL *mpool_open(void *, int, pgno_t, pgno_t); 914fcd3413SBen Gras void mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *), 924fcd3413SBen Gras void (*)(void *, pgno_t, void *), void *); 934fcd3413SBen Gras void *mpool_new(MPOOL *, pgno_t *); 944fcd3413SBen Gras void *mpool_get(MPOOL *, pgno_t, unsigned int); 954fcd3413SBen Gras int mpool_put(MPOOL *, void *, unsigned int); 964fcd3413SBen Gras int mpool_sync(MPOOL *); 974fcd3413SBen Gras int mpool_close(MPOOL *); 984fcd3413SBen Gras #ifdef STATISTICS 994fcd3413SBen Gras void mpool_stat(MPOOL *); 1004fcd3413SBen Gras #endif 1014fcd3413SBen Gras __END_DECLS 1024fcd3413SBen Gras 1034fcd3413SBen Gras #endif /* _MPOOL_H_ */ 104