1*7f2fe78bSCy Schubert /*- 2*7f2fe78bSCy Schubert * Copyright (c) 1991, 1993, 1994 3*7f2fe78bSCy Schubert * The Regents of the University of California. All rights reserved. 4*7f2fe78bSCy Schubert * 5*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without 6*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions 7*7f2fe78bSCy Schubert * are met: 8*7f2fe78bSCy Schubert * 1. Redistributions of source code must retain the above copyright 9*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer. 10*7f2fe78bSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 11*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in the 12*7f2fe78bSCy Schubert * documentation and/or other materials provided with the distribution. 13*7f2fe78bSCy Schubert * 3. All advertising materials mentioning features or use of this software 14*7f2fe78bSCy Schubert * must display the following acknowledgement: 15*7f2fe78bSCy Schubert * This product includes software developed by the University of 16*7f2fe78bSCy Schubert * California, Berkeley and its contributors. 17*7f2fe78bSCy Schubert * 4. Neither the name of the University nor the names of its contributors 18*7f2fe78bSCy Schubert * may be used to endorse or promote products derived from this software 19*7f2fe78bSCy Schubert * without specific prior written permission. 20*7f2fe78bSCy Schubert * 21*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22*7f2fe78bSCy Schubert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23*7f2fe78bSCy Schubert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24*7f2fe78bSCy Schubert * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25*7f2fe78bSCy Schubert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26*7f2fe78bSCy Schubert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27*7f2fe78bSCy Schubert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29*7f2fe78bSCy Schubert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30*7f2fe78bSCy Schubert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*7f2fe78bSCy Schubert * SUCH DAMAGE. 32*7f2fe78bSCy Schubert * 33*7f2fe78bSCy Schubert * @(#)mpool.h 8.4 (Berkeley) 11/2/95 34*7f2fe78bSCy Schubert */ 35*7f2fe78bSCy Schubert 36*7f2fe78bSCy Schubert #include "db-queue.h" 37*7f2fe78bSCy Schubert 38*7f2fe78bSCy Schubert /* 39*7f2fe78bSCy Schubert * The memory pool scheme is a simple one. Each in-memory page is referenced 40*7f2fe78bSCy Schubert * by a bucket which is threaded in up to two of three ways. All active pages 41*7f2fe78bSCy Schubert * are threaded on a hash chain (hashed by page number) and an lru chain. 42*7f2fe78bSCy Schubert * Inactive pages are threaded on a free chain. Each reference to a memory 43*7f2fe78bSCy Schubert * pool is handed an opaque MPOOL cookie which stores all of this information. 44*7f2fe78bSCy Schubert */ 45*7f2fe78bSCy Schubert #define HASHSIZE 128 46*7f2fe78bSCy Schubert #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE) 47*7f2fe78bSCy Schubert 48*7f2fe78bSCy Schubert /* The BKT structures are the elements of the queues. */ 49*7f2fe78bSCy Schubert typedef struct _bkt { 50*7f2fe78bSCy Schubert TAILQ_ENTRY(_bkt) hq; /* hash queue */ 51*7f2fe78bSCy Schubert TAILQ_ENTRY(_bkt) q; /* lru queue */ 52*7f2fe78bSCy Schubert void *page; /* page */ 53*7f2fe78bSCy Schubert db_pgno_t pgno; /* page number */ 54*7f2fe78bSCy Schubert 55*7f2fe78bSCy Schubert #define MPOOL_DIRTY 0x01 /* page needs to be written */ 56*7f2fe78bSCy Schubert #define MPOOL_PINNED 0x02 /* page is pinned into memory */ 57*7f2fe78bSCy Schubert #define MPOOL_INUSE 0x04 /* page address is valid */ 58*7f2fe78bSCy Schubert u_int8_t flags; /* flags */ 59*7f2fe78bSCy Schubert } BKT; 60*7f2fe78bSCy Schubert 61*7f2fe78bSCy Schubert typedef struct MPOOL { 62*7f2fe78bSCy Schubert TAILQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */ 63*7f2fe78bSCy Schubert /* hash queue array */ 64*7f2fe78bSCy Schubert TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE]; 65*7f2fe78bSCy Schubert db_pgno_t curcache; /* current number of cached pages */ 66*7f2fe78bSCy Schubert db_pgno_t maxcache; /* max number of cached pages */ 67*7f2fe78bSCy Schubert db_pgno_t npages; /* number of pages in the file */ 68*7f2fe78bSCy Schubert u_long pagesize; /* file page size */ 69*7f2fe78bSCy Schubert int fd; /* file descriptor */ 70*7f2fe78bSCy Schubert /* page in conversion routine */ 71*7f2fe78bSCy Schubert void (*pgin) __P((void *, db_pgno_t, void *)); 72*7f2fe78bSCy Schubert /* page out conversion routine */ 73*7f2fe78bSCy Schubert void (*pgout) __P((void *, db_pgno_t, void *)); 74*7f2fe78bSCy Schubert void *pgcookie; /* cookie for page in/out routines */ 75*7f2fe78bSCy Schubert #ifdef STATISTICS 76*7f2fe78bSCy Schubert u_long cachehit; 77*7f2fe78bSCy Schubert u_long cachemiss; 78*7f2fe78bSCy Schubert u_long pagealloc; 79*7f2fe78bSCy Schubert u_long pageflush; 80*7f2fe78bSCy Schubert u_long pageget; 81*7f2fe78bSCy Schubert u_long pagenew; 82*7f2fe78bSCy Schubert u_long pageput; 83*7f2fe78bSCy Schubert u_long pageread; 84*7f2fe78bSCy Schubert u_long pagewrite; 85*7f2fe78bSCy Schubert #endif 86*7f2fe78bSCy Schubert } MPOOL; 87*7f2fe78bSCy Schubert 88*7f2fe78bSCy Schubert #define MPOOL_IGNOREPIN 0x01 /* Ignore if the page is pinned. */ 89*7f2fe78bSCy Schubert #define MPOOL_PAGE_REQUEST 0x01 /* Allocate a new page with a 90*7f2fe78bSCy Schubert specific page number. */ 91*7f2fe78bSCy Schubert #define MPOOL_PAGE_NEXT 0x02 /* Allocate a new page with the next 92*7f2fe78bSCy Schubert page number. */ 93*7f2fe78bSCy Schubert 94*7f2fe78bSCy Schubert #define mpool_open kdb2_mpool_open 95*7f2fe78bSCy Schubert #define mpool_filter kdb2_mpool_filter 96*7f2fe78bSCy Schubert #define mpool_new kdb2_mpool_new 97*7f2fe78bSCy Schubert #define mpool_get kdb2_mpool_get 98*7f2fe78bSCy Schubert #define mpool_delete kdb2_mpool_delete 99*7f2fe78bSCy Schubert #define mpool_put kdb2_mpool_put 100*7f2fe78bSCy Schubert #define mpool_sync kdb2_mpool_sync 101*7f2fe78bSCy Schubert #define mpool_close kdb2_mpool_close 102*7f2fe78bSCy Schubert #define mpool_stat kdb2_mpool_stat 103*7f2fe78bSCy Schubert 104*7f2fe78bSCy Schubert __BEGIN_DECLS 105*7f2fe78bSCy Schubert MPOOL *mpool_open __P((void *, int, db_pgno_t, db_pgno_t)); 106*7f2fe78bSCy Schubert void mpool_filter __P((MPOOL *, void (*)(void *, db_pgno_t, void *), 107*7f2fe78bSCy Schubert void (*)(void *, db_pgno_t, void *), void *)); 108*7f2fe78bSCy Schubert void *mpool_new __P((MPOOL *, db_pgno_t *, u_int)); 109*7f2fe78bSCy Schubert void *mpool_get __P((MPOOL *, db_pgno_t, u_int)); 110*7f2fe78bSCy Schubert int mpool_delete __P((MPOOL *, void *)); 111*7f2fe78bSCy Schubert int mpool_put __P((MPOOL *, void *, u_int)); 112*7f2fe78bSCy Schubert int mpool_sync __P((MPOOL *)); 113*7f2fe78bSCy Schubert int mpool_close __P((MPOOL *)); 114*7f2fe78bSCy Schubert 115*7f2fe78bSCy Schubert void mpool_stat __P((MPOOL *)); 116*7f2fe78bSCy Schubert 117*7f2fe78bSCy Schubert __END_DECLS 118