1*2b55b311Schristos /* $NetBSD: mpool.h,v 1.16 2016/09/24 21:18:09 christos Exp $ */ 24d2cbfceScgd 3e75cc16eSproven /*- 4fc271525Scgd * Copyright (c) 1991, 1993, 1994 570a04c5aScgd * The Regents of the University of California. All rights reserved. 6e75cc16eSproven * 7e75cc16eSproven * Redistribution and use in source and binary forms, with or without 8e75cc16eSproven * modification, are permitted provided that the following conditions 9e75cc16eSproven * are met: 10e75cc16eSproven * 1. Redistributions of source code must retain the above copyright 11e75cc16eSproven * notice, this list of conditions and the following disclaimer. 12e75cc16eSproven * 2. Redistributions in binary form must reproduce the above copyright 13e75cc16eSproven * notice, this list of conditions and the following disclaimer in the 14e75cc16eSproven * documentation and/or other materials provided with the distribution. 15039cc956Sagc * 3. Neither the name of the University nor the names of its contributors 16e75cc16eSproven * may be used to endorse or promote products derived from this software 17e75cc16eSproven * without specific prior written permission. 18e75cc16eSproven * 19e75cc16eSproven * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20e75cc16eSproven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21e75cc16eSproven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22e75cc16eSproven * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23e75cc16eSproven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24e75cc16eSproven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25e75cc16eSproven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26e75cc16eSproven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27e75cc16eSproven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28e75cc16eSproven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29e75cc16eSproven * SUCH DAMAGE. 30e75cc16eSproven * 31fc271525Scgd * @(#)mpool.h 8.2 (Berkeley) 7/14/94 32e75cc16eSproven */ 33e75cc16eSproven 34d39928a2Sperry #ifndef _MPOOL_H_ 35d39928a2Sperry #define _MPOOL_H_ 36d39928a2Sperry 37813b1610Sad #include <sys/cdefs.h> 38fc271525Scgd #include <sys/queue.h> 39fc271525Scgd 40e75cc16eSproven /* 41fc271525Scgd * The memory pool scheme is a simple one. Each in-memory page is referenced 42fc271525Scgd * by a bucket which is threaded in up to two of three ways. All active pages 43fc271525Scgd * are threaded on a hash chain (hashed by page number) and an lru chain. 44fc271525Scgd * Inactive pages are threaded on a free chain. Each reference to a memory 45fc271525Scgd * pool is handed an opaque MPOOL cookie which stores all of this information. 46e75cc16eSproven */ 47e75cc16eSproven #define HASHSIZE 128 48e75cc16eSproven #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE) 49e75cc16eSproven 50fc271525Scgd /* The BKT structures are the elements of the queues. */ 51fc271525Scgd typedef struct _bkt { 5296c26abaSchristos TAILQ_ENTRY(_bkt) hq; /* hash queue */ 5396c26abaSchristos TAILQ_ENTRY(_bkt) q; /* lru queue */ 54e75cc16eSproven void *page; /* page */ 55e75cc16eSproven pgno_t pgno; /* page number */ 56e75cc16eSproven 57e75cc16eSproven #define MPOOL_DIRTY 0x01 /* page needs to be written */ 58e75cc16eSproven #define MPOOL_PINNED 0x02 /* page is pinned into memory */ 590ac50079Schristos #define MPOOL_INUSE 0x04 /* page address is valid */ 605f65228bSperry uint8_t flags; /* flags */ 61e75cc16eSproven } BKT; 62e75cc16eSproven 63e75cc16eSproven typedef struct MPOOL { 6496c26abaSchristos TAILQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */ 65fc271525Scgd /* hash queue array */ 6696c26abaSchristos TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE]; 67fc271525Scgd pgno_t curcache; /* current number of cached pages */ 68fc271525Scgd pgno_t maxcache; /* max number of cached pages */ 69fc271525Scgd pgno_t npages; /* number of pages in the file */ 7040b37a3bSjoerg unsigned long pagesize; /* file page size */ 71fc271525Scgd int fd; /* file descriptor */ 72fc271525Scgd /* page in conversion routine */ 7319b7469aSperry void (*pgin)(void *, pgno_t, void *); 74fc271525Scgd /* page out conversion routine */ 7519b7469aSperry void (*pgout)(void *, pgno_t, void *); 76fc271525Scgd void *pgcookie; /* cookie for page in/out routines */ 77e75cc16eSproven #ifdef STATISTICS 7840b37a3bSjoerg unsigned long cachehit; 7940b37a3bSjoerg unsigned long cachemiss; 8040b37a3bSjoerg unsigned long pagealloc; 8140b37a3bSjoerg unsigned long pageflush; 8240b37a3bSjoerg unsigned long pageget; 8340b37a3bSjoerg unsigned long pagenew; 8440b37a3bSjoerg unsigned long pageput; 8540b37a3bSjoerg unsigned long pageread; 8640b37a3bSjoerg unsigned long pagewrite; 87e75cc16eSproven #endif 88e75cc16eSproven } MPOOL; 89e75cc16eSproven 900ac50079Schristos /* flags for get/put */ 910ac50079Schristos #define MPOOL_IGNOREPIN 0x01 /* Ignore if the page is pinned. */ 920ac50079Schristos /* flags for newf */ 930ac50079Schristos #define MPOOL_PAGE_REQUEST 0x01 /* Allocate a new page with a 940ac50079Schristos specific page number. */ 950ac50079Schristos #define MPOOL_PAGE_NEXT 0x02 /* Allocate a new page with the next 960ac50079Schristos page number. */ 970ac50079Schristos 98e75cc16eSproven __BEGIN_DECLS 9919b7469aSperry MPOOL *mpool_open(void *, int, pgno_t, pgno_t); 10019b7469aSperry void mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *), 10119b7469aSperry void (*)(void *, pgno_t, void *), void *); 10219b7469aSperry void *mpool_new(MPOOL *, pgno_t *); 1030ac50079Schristos void *mpool_newf(MPOOL *, pgno_t *, unsigned int); 1040ac50079Schristos int mpool_delete(MPOOL *, void *); 105*2b55b311Schristos void *mpool_get(MPOOL *, pgno_t, unsigned int); 10640b37a3bSjoerg int mpool_put(MPOOL *, void *, unsigned int); 10719b7469aSperry int mpool_sync(MPOOL *); 10819b7469aSperry int mpool_close(MPOOL *); 109e75cc16eSproven #ifdef STATISTICS 11019b7469aSperry void mpool_stat(MPOOL *); 111e75cc16eSproven #endif 112e75cc16eSproven __END_DECLS 113d39928a2Sperry 114d39928a2Sperry #endif /* _MPOOL_H_ */ 115