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.4 (Berkeley) 11/2/95 34 */ 35 36 #include "db-queue.h" 37 38 /* 39 * The memory pool scheme is a simple one. Each in-memory page is referenced 40 * by a bucket which is threaded in up to two of three ways. All active pages 41 * are threaded on a hash chain (hashed by page number) and an lru chain. 42 * Inactive pages are threaded on a free chain. Each reference to a memory 43 * pool is handed an opaque MPOOL cookie which stores all of this information. 44 */ 45 #define HASHSIZE 128 46 #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE) 47 48 /* The BKT structures are the elements of the queues. */ 49 typedef struct _bkt { 50 TAILQ_ENTRY(_bkt) hq; /* hash queue */ 51 TAILQ_ENTRY(_bkt) q; /* lru queue */ 52 void *page; /* page */ 53 db_pgno_t pgno; /* page number */ 54 55 #define MPOOL_DIRTY 0x01 /* page needs to be written */ 56 #define MPOOL_PINNED 0x02 /* page is pinned into memory */ 57 #define MPOOL_INUSE 0x04 /* page address is valid */ 58 u_int8_t flags; /* flags */ 59 } BKT; 60 61 typedef struct MPOOL { 62 TAILQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */ 63 /* hash queue array */ 64 TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE]; 65 db_pgno_t curcache; /* current number of cached pages */ 66 db_pgno_t maxcache; /* max number of cached pages */ 67 db_pgno_t npages; /* number of pages in the file */ 68 u_long pagesize; /* file page size */ 69 int fd; /* file descriptor */ 70 /* page in conversion routine */ 71 void (*pgin) __P((void *, db_pgno_t, void *)); 72 /* page out conversion routine */ 73 void (*pgout) __P((void *, db_pgno_t, void *)); 74 void *pgcookie; /* cookie for page in/out routines */ 75 #ifdef STATISTICS 76 u_long cachehit; 77 u_long cachemiss; 78 u_long pagealloc; 79 u_long pageflush; 80 u_long pageget; 81 u_long pagenew; 82 u_long pageput; 83 u_long pageread; 84 u_long pagewrite; 85 #endif 86 } MPOOL; 87 88 #define MPOOL_IGNOREPIN 0x01 /* Ignore if the page is pinned. */ 89 #define MPOOL_PAGE_REQUEST 0x01 /* Allocate a new page with a 90 specific page number. */ 91 #define MPOOL_PAGE_NEXT 0x02 /* Allocate a new page with the next 92 page number. */ 93 94 #define mpool_open kdb2_mpool_open 95 #define mpool_filter kdb2_mpool_filter 96 #define mpool_new kdb2_mpool_new 97 #define mpool_get kdb2_mpool_get 98 #define mpool_delete kdb2_mpool_delete 99 #define mpool_put kdb2_mpool_put 100 #define mpool_sync kdb2_mpool_sync 101 #define mpool_close kdb2_mpool_close 102 #define mpool_stat kdb2_mpool_stat 103 104 __BEGIN_DECLS 105 MPOOL *mpool_open __P((void *, int, db_pgno_t, db_pgno_t)); 106 void mpool_filter __P((MPOOL *, void (*)(void *, db_pgno_t, void *), 107 void (*)(void *, db_pgno_t, void *), void *)); 108 void *mpool_new __P((MPOOL *, db_pgno_t *, u_int)); 109 void *mpool_get __P((MPOOL *, db_pgno_t, u_int)); 110 int mpool_delete __P((MPOOL *, void *)); 111 int mpool_put __P((MPOOL *, void *, u_int)); 112 int mpool_sync __P((MPOOL *)); 113 int mpool_close __P((MPOOL *)); 114 115 void mpool_stat __P((MPOOL *)); 116 117 __END_DECLS 118