andre@3: /*- andre@3: * Copyright (c) 1991, 1993, 1994 andre@3: * The Regents of the University of California. All rights reserved. andre@3: * andre@3: * Redistribution and use in source and binary forms, with or without andre@3: * modification, are permitted provided that the following conditions andre@3: * are met: andre@3: * 1. Redistributions of source code must retain the above copyright andre@3: * notice, this list of conditions and the following disclaimer. andre@3: * 2. Redistributions in binary form must reproduce the above copyright andre@3: * notice, this list of conditions and the following disclaimer in the andre@3: * documentation and/or other materials provided with the distribution. andre@3: * 3. ***REMOVED*** - see andre@3: * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change andre@3: * 4. Neither the name of the University nor the names of its contributors andre@3: * may be used to endorse or promote products derived from this software andre@3: * without specific prior written permission. andre@3: * andre@3: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND andre@3: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE andre@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE andre@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE andre@3: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL andre@3: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS andre@3: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) andre@3: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT andre@3: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY andre@3: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF andre@3: * SUCH DAMAGE. andre@3: * andre@3: * @(#)mpool.h 8.2 (Berkeley) 7/14/94 andre@3: */ andre@3: andre@3: #include andre@3: andre@3: /* andre@3: * The memory pool scheme is a simple one. Each in-memory page is referenced andre@3: * by a bucket which is threaded in up to two of three ways. All active pages andre@3: * are threaded on a hash chain (hashed by page number) and an lru chain. andre@3: * Inactive pages are threaded on a free chain. Each reference to a memory andre@3: * pool is handed an opaque MPOOL cookie which stores all of this information. andre@3: */ andre@3: #define HASHSIZE 128 andre@3: #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE) andre@3: andre@3: /* The BKT structures are the elements of the queues. */ andre@3: typedef struct _bkt { andre@3: CIRCLEQ_ENTRY(_bkt) hq; /* hash queue */ andre@3: CIRCLEQ_ENTRY(_bkt) q; /* lru queue */ andre@3: void *page; /* page */ andre@3: pgno_t pgno; /* page number */ andre@3: andre@3: #define MPOOL_DIRTY 0x01 /* page needs to be written */ andre@3: #define MPOOL_PINNED 0x02 /* page is pinned into memory */ andre@3: uint8 flags; /* flags */ andre@3: } BKT; andre@3: andre@3: typedef struct MPOOL { andre@3: CIRCLEQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */ andre@3: /* hash queue array */ andre@3: CIRCLEQ_HEAD(_hqh, _bkt) hqh[HASHSIZE]; andre@3: pgno_t curcache; /* current number of cached pages */ andre@3: pgno_t maxcache; /* max number of cached pages */ andre@3: pgno_t npages; /* number of pages in the file */ andre@3: uint32 pagesize; /* file page size */ andre@3: int fd; /* file descriptor */ andre@3: /* page in conversion routine */ andre@3: void (*pgin) (void *, pgno_t, void *); andre@3: /* page out conversion routine */ andre@3: void (*pgout) (void *, pgno_t, void *); andre@3: void *pgcookie; /* cookie for page in/out routines */ andre@3: #ifdef STATISTICS andre@3: uint32 cachehit; andre@3: uint32 cachemiss; andre@3: uint32 pagealloc; andre@3: uint32 pageflush; andre@3: uint32 pageget; andre@3: uint32 pagenew; andre@3: uint32 pageput; andre@3: uint32 pageread; andre@3: uint32 pagewrite; andre@3: #endif andre@3: } MPOOL; andre@3: andre@3: __BEGIN_DECLS andre@3: MPOOL *mpool_open (void *, int, pgno_t, pgno_t); andre@3: void mpool_filter (MPOOL *, void (*)(void *, pgno_t, void *), andre@3: void (*)(void *, pgno_t, void *), void *); andre@3: void *mpool_new (MPOOL *, pgno_t *); andre@3: void *mpool_get (MPOOL *, pgno_t, uint); andre@3: int mpool_put (MPOOL *, void *, uint); andre@3: int mpool_sync (MPOOL *); andre@3: int mpool_close (MPOOL *); andre@3: #ifdef STATISTICS andre@3: void mpool_stat (MPOOL *); andre@3: #endif andre@3: __END_DECLS