150982Sbostic /*- 2*61068Sbostic * Copyright (c) 1991, 1993 3*61068Sbostic * The Regents of the University of California. All rights reserved. 450982Sbostic * 550982Sbostic * %sccs.include.redist.c% 650982Sbostic * 7*61068Sbostic * @(#)mpool.h 8.1 (Berkeley) 06/02/93 850982Sbostic */ 950982Sbostic 1050982Sbostic /* 1150982Sbostic * The memory pool scheme is a simple one. Each in memory page is referenced 1250982Sbostic * by a bucket which is threaded in three ways. All active pages are threaded 1350982Sbostic * on a hash chain (hashed by the page number) and an lru chain. Inactive 1450982Sbostic * pages are threaded on a free chain. Each reference to a memory pool is 1550982Sbostic * handed an MPOOL which is the opaque cookie passed to all of the memory 1650982Sbostic * routines. 1750982Sbostic */ 1850982Sbostic #define HASHSIZE 128 1950982Sbostic #define HASHKEY(pgno) ((pgno - 1) % HASHSIZE) 2050982Sbostic 2150982Sbostic /* The BKT structures are the elements of the lists. */ 2250982Sbostic typedef struct BKT { 2350982Sbostic struct BKT *hnext; /* next hash bucket */ 2450982Sbostic struct BKT *hprev; /* previous hash bucket */ 2550982Sbostic struct BKT *cnext; /* next free/lru bucket */ 2650982Sbostic struct BKT *cprev; /* previous free/lru bucket */ 2750982Sbostic void *page; /* page */ 2850982Sbostic pgno_t pgno; /* page number */ 2950982Sbostic 3050982Sbostic #define MPOOL_DIRTY 0x01 /* page needs to be written */ 3150982Sbostic #define MPOOL_PINNED 0x02 /* page is pinned into memory */ 3250982Sbostic unsigned long flags; /* flags */ 3350982Sbostic } BKT; 3450982Sbostic 3550982Sbostic /* The BKTHDR structures are the heads of the lists. */ 3650982Sbostic typedef struct BKTHDR { 3750982Sbostic struct BKT *hnext; /* next hash bucket */ 3850982Sbostic struct BKT *hprev; /* previous hash bucket */ 3950982Sbostic struct BKT *cnext; /* next free/lru bucket */ 4050982Sbostic struct BKT *cprev; /* previous free/lru bucket */ 4150982Sbostic } BKTHDR; 4250982Sbostic 4350982Sbostic typedef struct MPOOL { 4450982Sbostic BKTHDR free; /* The free list. */ 4550982Sbostic BKTHDR lru; /* The LRU list. */ 4650982Sbostic BKTHDR hashtable[HASHSIZE]; /* Hashed list by page number. */ 4750982Sbostic pgno_t curcache; /* Current number of cached pages. */ 4850982Sbostic pgno_t maxcache; /* Max number of cached pages. */ 4950982Sbostic pgno_t npages; /* Number of pages in the file. */ 5060168Sbostic u_long pagesize; /* File page size. */ 5150982Sbostic int fd; /* File descriptor. */ 5250982Sbostic /* Page in conversion routine. */ 5350982Sbostic void (*pgin) __P((void *, pgno_t, void *)); 5450982Sbostic /* Page out conversion routine. */ 5550982Sbostic void (*pgout) __P((void *, pgno_t, void *)); 5650982Sbostic void *pgcookie; /* Cookie for page in/out routines. */ 5750982Sbostic #ifdef STATISTICS 5850982Sbostic unsigned long cachehit; 5950982Sbostic unsigned long cachemiss; 6050982Sbostic unsigned long pagealloc; 6150982Sbostic unsigned long pageflush; 6250982Sbostic unsigned long pageget; 6350982Sbostic unsigned long pagenew; 6450982Sbostic unsigned long pageput; 6550982Sbostic unsigned long pageread; 6650982Sbostic unsigned long pagewrite; 6750982Sbostic #endif 6850982Sbostic } MPOOL; 6950982Sbostic 7050982Sbostic #ifdef __MPOOLINTERFACE_PRIVATE 7150982Sbostic /* Macros to insert/delete into/from hash chain. */ 7250982Sbostic #define rmhash(bp) { \ 7350982Sbostic (bp)->hprev->hnext = (bp)->hnext; \ 7450982Sbostic (bp)->hnext->hprev = (bp)->hprev; \ 7550982Sbostic } 7650982Sbostic #define inshash(bp, pg) { \ 7750982Sbostic hp = &mp->hashtable[HASHKEY(pg)]; \ 7850982Sbostic (bp)->hnext = hp->hnext; \ 7950982Sbostic (bp)->hprev = (struct BKT *)hp; \ 8050982Sbostic hp->hnext->hprev = (bp); \ 8150982Sbostic hp->hnext = (bp); \ 8250982Sbostic } 8350982Sbostic 8450982Sbostic /* Macros to insert/delete into/from lru and free chains. */ 8550982Sbostic #define rmchain(bp) { \ 8650982Sbostic (bp)->cprev->cnext = (bp)->cnext; \ 8750982Sbostic (bp)->cnext->cprev = (bp)->cprev; \ 8850982Sbostic } 8950982Sbostic #define inschain(bp, dp) { \ 9050982Sbostic (bp)->cnext = (dp)->cnext; \ 9150982Sbostic (bp)->cprev = (struct BKT *)(dp); \ 9250982Sbostic (dp)->cnext->cprev = (bp); \ 9350982Sbostic (dp)->cnext = (bp); \ 9450982Sbostic } 9550982Sbostic #endif 9650982Sbostic 9750982Sbostic __BEGIN_DECLS 9850982Sbostic MPOOL *mpool_open __P((DBT *, int, pgno_t, pgno_t)); 9950982Sbostic void mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *), 10050982Sbostic void (*)(void *, pgno_t, void *), void *)); 10150982Sbostic void *mpool_new __P((MPOOL *, pgno_t *)); 10250982Sbostic void *mpool_get __P((MPOOL *, pgno_t, u_int)); 10350982Sbostic int mpool_put __P((MPOOL *, void *, u_int)); 10450982Sbostic int mpool_sync __P((MPOOL *)); 10550982Sbostic int mpool_close __P((MPOOL *)); 10650982Sbostic #ifdef STATISTICS 10750982Sbostic void mpool_stat __P((MPOOL *)); 10850982Sbostic #endif 10950982Sbostic __END_DECLS 110