* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#if defined(LIBC_SCCS) && !defined(lint)
char sccsid
[] = "@(#)lrucache.c 5.3 (Berkeley) 2/22/91";
#endif /* LIBC_SCCS and not lint */
* lrucache.c -- LRU cache for disk-based btree pages.
* This file implements an LRU cache in user space for disk-based
* LRUINIT -- Initialize a new LRU cache.
* There's a separate LRU cache for every open file descriptor on which
* the user wants caching. The desired cache size and logical page
* size are passed in. We try to avoid growing the cache beyond the
* limit specified by the user, but if we cannot satisfy a block request
* without growing the cache, we do so.
* Note that the page size passed in is the logical page size for
* use with this file descriptor, and doesn't necessarily have anything
* to do with the underlying hardware page size.
* fd -- file descriptor for this cache
* cachesz -- number of buffers in cache (suggested)
* pagesz -- logical page size inside this file
* inproc -- routine to call when a buffer is read
* outproc -- routine to call when a buffer is written
* Opaque pointer to the LRU cache on success, NULL on failure.
* Allocates memory for the hash table and LRU cache. Buffers
* are allocated on demand, later.
lruinit(fd
, cachesz
, pagesz
, opaque
, inproc
, outproc
)
/* allocate the LRU cache struct */
if ((l
= (LRUCACHE
*) malloc((unsigned) sizeof(LRUCACHE
)))
/* allocate the hash table */
nbytes
= cachesz
* sizeof(CACHE_ENT
*);
if ((l
->lru_cache
= (CACHE_ENT
**) malloc((unsigned) nbytes
))
== (CACHE_ENT
**) NULL
) {
bzero((char *) (l
->lru_cache
), (size_t) nbytes
);
l
->lru_head
= l
->lru_tail
= (LRU_ENT
*) NULL
;
l
->lru_outproc
= outproc
;
* LRUGET -- Get a buffer from an LRU cache.
* If the buffer is not in the cache at present, this routine will
* instantiate it from the file. This REQUIRES that the desired
* block actually be on disk; we don't do non-blocking reads, so
* if it's not actually out there, this routine won't return for
* a very long time. In order to instantiate a new buffer, use
* lru -- the LRU cache to use.
* pgno -- the logical block number to get.
* nread -- pointer to an int, in which the number of bytes
* (char *) pointer to the buffer for the desired block. The
* number of bytes actually read is returned in *nread.
* The requested buffer is locked down until the user does
* an explicit lrurelease() on it.
LRUCACHE
*l
= (LRUCACHE
*) lru
;
/* is it already in the cache? */
if ((ce
= lruhashget(l
, pgno
)) != (CACHE_ENT
*) NULL
) {
/* yes, move it to the head and return it */
lruent
->l_flags
&= ~F_FREE
;
lruhead(l
, ce
->c_lruent
);
return (ce
->c_lruent
->l_buffer
);
/* not there, get a free page */
if ((buffer
= lrugetpg(l
, pgno
, nread
, lruget
)) == (char *) NULL
)
/* okay, got a buffer -- fill it */
pos
= (long) (l
->lru_psize
* pgno
);
if (lseek(l
->lru_fd
, pos
, L_SET
) != pos
)
*nread
= read(l
->lru_fd
, buffer
, l
->lru_psize
);
(*(l
->lru_inproc
))(buffer
, l
->lru_opaque
);
* LRUGETNEW -- Get a page for a new block in an LRU cache.
* This routine allows users to instantiate pages for a file if they
* don't exist on disk yet. It's used to make a file bigger.
* lru -- the LRU cache to use
* pgno -- the (new) logical page number to instantiate
* nread -- ptr to int to get number of bytes read (this is
* guaranteed to be zero, since the page isn't on disk)
* Pointer to a buffer for the associated page, or NULL on
* The associated buffer is locked down until the user
* explicitly does an lrurelease() on it.
lrugetnew(lru
, pgno
, nread
)
LRUCACHE
*l
= (LRUCACHE
*) lru
;
return (lrugetpg(l
, pgno
, nread
, lrugetnew
));
* LRUFLUSH -- flush an LRU page to disk.
* This routine forces a page to disk. Users should use lruwrite(),
* which simply marks a page dirty and does late writing.
* lruent -- the LRU cache entry whose page we should flush
* Zero on success, -1 on failure.
(*(l
->lru_outproc
))(lruent
->l_buffer
, l
->lru_opaque
);
pos
= (long) (l
->lru_psize
* lruent
->l_pgno
);
if (lseek(l
->lru_fd
, pos
, L_SET
) != pos
)
if (write(l
->lru_fd
, lruent
->l_buffer
, l
->lru_psize
) != l
->lru_psize
)
(*(l
->lru_inproc
))(lruent
->l_buffer
, l
->lru_opaque
);
lruent
->l_flags
&= ~F_DIRTY
;
* LRUWRITE -- Mark an LRU cache buffer dirty
* This routine is how users should move their changes to disk. The
* cache code marks the associated buffer dirty, and flushes it to
* disk if we need to reuse the buffer for some other page.
* pgno -- page number to flush
* Zero on success, -1 on failure.
LRUCACHE
*l
= (LRUCACHE
*) lru
;
if ((ce
= lruhashget(l
, pgno
)) == (CACHE_ENT
*) NULL
)
/* mark the entry dirty */
ce
->c_lruent
->l_flags
|= F_DIRTY
;
* LRUSYNC -- Flush all changes to disk
* This routine allows users to force all changes to buffers currently
* in memory to disk. This is expensive.
* lru -- LRU cache to flush
* Zero on success, -1 on failure
* After this call, all buffers are clean.
LRUCACHE
*l
= (LRUCACHE
*) lru
;
for (le
= l
->lru_head
; le
!= (LRU_ENT
*) NULL
; le
= le
->l_next
)
if (le
->l_flags
& F_DIRTY
)
* LRURELEASE -- Release a buffer in the LRU cache for reuse
* When the user does an lruget() or lrugetnew(), the buffer we return
* is locked down, to guarantee that it's not reused while the user
* still needs it. Once a buffer is no longer needed, it should be
* released (via this routine) so that we can use it for other pages
* pgno -- page number of buffer to release
* Zero on success, -1 on failure
LRUCACHE
*l
= (LRUCACHE
*) lru
;
if ((ce
= lruhashget(l
, pgno
)) == (CACHE_ENT
*) NULL
)
ce
->c_lruent
->l_flags
|= F_FREE
;
* LRUFREE -- Free an entire LRU cache
* This routine releases an LRU cache. The cache should not be
* lru -- LRU cache to free
LRUCACHE
*l
= (LRUCACHE
*) lru
;
for (le
= l
->lru_head
; le
!= (LRU_ENT
*) NULL
; ) {
free((char *) (le
->l_buffer
));