Home | History | Annotate | Line # | Download | only in include
mpool.h revision 1.1
      1  1.1  proven /*-
      2  1.1  proven  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1  proven  * All rights reserved.
      4  1.1  proven  *
      5  1.1  proven  * Redistribution and use in source and binary forms, with or without
      6  1.1  proven  * modification, are permitted provided that the following conditions
      7  1.1  proven  * are met:
      8  1.1  proven  * 1. Redistributions of source code must retain the above copyright
      9  1.1  proven  *    notice, this list of conditions and the following disclaimer.
     10  1.1  proven  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  proven  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  proven  *    documentation and/or other materials provided with the distribution.
     13  1.1  proven  * 3. All advertising materials mentioning features or use of this software
     14  1.1  proven  *    must display the following acknowledgement:
     15  1.1  proven  *	This product includes software developed by the University of
     16  1.1  proven  *	California, Berkeley and its contributors.
     17  1.1  proven  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  proven  *    may be used to endorse or promote products derived from this software
     19  1.1  proven  *    without specific prior written permission.
     20  1.1  proven  *
     21  1.1  proven  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  proven  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  proven  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  proven  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  proven  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  proven  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  proven  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  proven  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  proven  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  proven  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  proven  * SUCH DAMAGE.
     32  1.1  proven  *
     33  1.1  proven  *	@(#)mpool.h	5.2 (Berkeley) 2/14/93
     34  1.1  proven  */
     35  1.1  proven 
     36  1.1  proven /*
     37  1.1  proven  * The memory pool scheme is a simple one.  Each in memory page is referenced
     38  1.1  proven  * by a bucket which is threaded in three ways.  All active pages are threaded
     39  1.1  proven  * on a hash chain (hashed by the page number) and an lru chain.  Inactive
     40  1.1  proven  * pages are threaded on a free chain.  Each reference to a memory pool is
     41  1.1  proven  * handed an MPOOL which is the opaque cookie passed to all of the memory
     42  1.1  proven  * routines.
     43  1.1  proven  */
     44  1.1  proven #define	HASHSIZE	128
     45  1.1  proven #define	HASHKEY(pgno)	((pgno - 1) % HASHSIZE)
     46  1.1  proven 
     47  1.1  proven /* The BKT structures are the elements of the lists. */
     48  1.1  proven typedef struct BKT {
     49  1.1  proven 	struct BKT	*hnext;		/* next hash bucket */
     50  1.1  proven 	struct BKT	*hprev;		/* previous hash bucket */
     51  1.1  proven 	struct BKT	*cnext;		/* next free/lru bucket */
     52  1.1  proven 	struct BKT	*cprev;		/* previous free/lru bucket */
     53  1.1  proven 	void		*page;		/* page */
     54  1.1  proven 	pgno_t		pgno;		/* page number */
     55  1.1  proven 
     56  1.1  proven #define	MPOOL_DIRTY	0x01		/* page needs to be written */
     57  1.1  proven #define	MPOOL_PINNED	0x02		/* page is pinned into memory */
     58  1.1  proven 	unsigned long	flags;		/* flags */
     59  1.1  proven } BKT;
     60  1.1  proven 
     61  1.1  proven /* The BKTHDR structures are the heads of the lists. */
     62  1.1  proven typedef struct BKTHDR {
     63  1.1  proven 	struct BKT	*hnext;		/* next hash bucket */
     64  1.1  proven 	struct BKT	*hprev;		/* previous hash bucket */
     65  1.1  proven 	struct BKT	*cnext;		/* next free/lru bucket */
     66  1.1  proven 	struct BKT	*cprev;		/* previous free/lru bucket */
     67  1.1  proven } BKTHDR;
     68  1.1  proven 
     69  1.1  proven typedef struct MPOOL {
     70  1.1  proven 	BKTHDR	free;			/* The free list. */
     71  1.1  proven 	BKTHDR	lru;			/* The LRU list. */
     72  1.1  proven 	BKTHDR	hashtable[HASHSIZE];	/* Hashed list by page number. */
     73  1.1  proven 	pgno_t	curcache;		/* Current number of cached pages. */
     74  1.1  proven 	pgno_t	maxcache;		/* Max number of cached pages. */
     75  1.1  proven 	pgno_t	npages;			/* Number of pages in the file. */
     76  1.1  proven 	indx_t	pagesize;		/* File page size. */
     77  1.1  proven 	int	fd;			/* File descriptor. */
     78  1.1  proven 					/* Page in conversion routine. */
     79  1.1  proven 	void    (*pgin) __P((void *, pgno_t, void *));
     80  1.1  proven 					/* Page out conversion routine. */
     81  1.1  proven 	void    (*pgout) __P((void *, pgno_t, void *));
     82  1.1  proven 	void	*pgcookie;		/* Cookie for page in/out routines. */
     83  1.1  proven #ifdef STATISTICS
     84  1.1  proven 	unsigned long	cachehit;
     85  1.1  proven 	unsigned long	cachemiss;
     86  1.1  proven 	unsigned long	pagealloc;
     87  1.1  proven 	unsigned long	pageflush;
     88  1.1  proven 	unsigned long	pageget;
     89  1.1  proven 	unsigned long	pagenew;
     90  1.1  proven 	unsigned long	pageput;
     91  1.1  proven 	unsigned long	pageread;
     92  1.1  proven 	unsigned long	pagewrite;
     93  1.1  proven #endif
     94  1.1  proven } MPOOL;
     95  1.1  proven 
     96  1.1  proven #ifdef __MPOOLINTERFACE_PRIVATE
     97  1.1  proven /* Macros to insert/delete into/from hash chain. */
     98  1.1  proven #define rmhash(bp) { \
     99  1.1  proven         (bp)->hprev->hnext = (bp)->hnext; \
    100  1.1  proven         (bp)->hnext->hprev = (bp)->hprev; \
    101  1.1  proven }
    102  1.1  proven #define inshash(bp, pg) { \
    103  1.1  proven 	hp = &mp->hashtable[HASHKEY(pg)]; \
    104  1.1  proven         (bp)->hnext = hp->hnext; \
    105  1.1  proven         (bp)->hprev = (struct BKT *)hp; \
    106  1.1  proven         hp->hnext->hprev = (bp); \
    107  1.1  proven         hp->hnext = (bp); \
    108  1.1  proven }
    109  1.1  proven 
    110  1.1  proven /* Macros to insert/delete into/from lru and free chains. */
    111  1.1  proven #define	rmchain(bp) { \
    112  1.1  proven         (bp)->cprev->cnext = (bp)->cnext; \
    113  1.1  proven         (bp)->cnext->cprev = (bp)->cprev; \
    114  1.1  proven }
    115  1.1  proven #define inschain(bp, dp) { \
    116  1.1  proven         (bp)->cnext = (dp)->cnext; \
    117  1.1  proven         (bp)->cprev = (struct BKT *)(dp); \
    118  1.1  proven         (dp)->cnext->cprev = (bp); \
    119  1.1  proven         (dp)->cnext = (bp); \
    120  1.1  proven }
    121  1.1  proven #endif
    122  1.1  proven 
    123  1.1  proven __BEGIN_DECLS
    124  1.1  proven MPOOL	*mpool_open __P((DBT *, int, pgno_t, pgno_t));
    125  1.1  proven void	 mpool_filter __P((MPOOL *, void (*)(void *, pgno_t, void *),
    126  1.1  proven 	    void (*)(void *, pgno_t, void *), void *));
    127  1.1  proven void	*mpool_new __P((MPOOL *, pgno_t *));
    128  1.1  proven void	*mpool_get __P((MPOOL *, pgno_t, u_int));
    129  1.1  proven int	 mpool_put __P((MPOOL *, void *, u_int));
    130  1.1  proven int	 mpool_sync __P((MPOOL *));
    131  1.1  proven int	 mpool_close __P((MPOOL *));
    132  1.1  proven #ifdef STATISTICS
    133  1.1  proven void	 mpool_stat __P((MPOOL *));
    134  1.1  proven #endif
    135  1.1  proven __END_DECLS
    136