xref: /csrg-svn/lib/libc/db/btree/lruhash.c (revision 46561)
146141Smao /*-
246141Smao  * Copyright (c) 1990 The Regents of the University of California.
346141Smao  * All rights reserved.
446141Smao  *
546141Smao  * This code is derived from software contributed to Berkeley by
646141Smao  * Mike Olson.
746141Smao  *
846141Smao  * %sccs.include.redist.c%
946141Smao  */
1046141Smao 
1146141Smao #if defined(LIBC_SCCS) && !defined(lint)
12*46561Sbostic static char sccsid[] = "@(#)lruhash.c	5.2 (Berkeley) 02/22/91";
1346141Smao #endif /* LIBC_SCCS and not lint */
1446141Smao 
15*46561Sbostic #include <stdlib.h>
1646141Smao #include "lrucache.h"
1746141Smao 
1846141Smao #define HASH(l, pgno)	(pgno % l->lru_csize)
1946141Smao 
2046141Smao /*
2146141Smao  *  LRUHASHGET -- Look up a block in the LRU cache by page number.
2246141Smao  *
2346141Smao  *	Parameters:
2446141Smao  *		l -- LRU cache
2546141Smao  *		pgno -- number of the logical page to get
2646141Smao  *
2746141Smao  *	Returns:
2846141Smao  *		(CACHE_ENT *) pointer to the associated hash table entry
2946141Smao  *		(if any), or NULL (if none).
3046141Smao  */
3146141Smao 
3246141Smao CACHE_ENT *
lruhashget(l,pgno)3346141Smao lruhashget(l, pgno)
3446141Smao 	LRUCACHE *l;
3546141Smao 	int pgno;
3646141Smao {
3746141Smao 	int hashind;
3846141Smao 	CACHE_ENT *ce;
3946141Smao 
4046141Smao 	hashind = HASH(l, pgno);
4146141Smao 
4246141Smao 	/* walk the chain */
4346141Smao 	for (ce = l->lru_cache[hashind];
4446141Smao 	     ce != (CACHE_ENT *) NULL;
4546141Smao 	     ce = ce->c_chain) {
4646141Smao 		if (ce->c_pgno == pgno)
4746141Smao 			return (ce);
4846141Smao 	}
4946141Smao 
5046141Smao 	return ((CACHE_ENT *) NULL);
5146141Smao }
5246141Smao 
5346141Smao /*
5446141Smao  *  LRUHASHPUT -- Add an entry for a given page to the cache hash table.
5546141Smao  *
5646141Smao  *	This routine assumes that the given page does not yet have an entry
5746141Smao  *	in the table.
5846141Smao  *
5946141Smao  *	Parameters:
6046141Smao  *		l -- LRU cache
6146141Smao  *		pgno -- logical page number for this entry
6246141Smao  *		lruent -- LRU list entry at which hash table entry should
6346141Smao  *			  point
6446141Smao  *
6546141Smao  *	Returns:
6646141Smao  *		(CACHE_ENT *) pointer to the new hash table entry on success,
6746141Smao  *		or NULL on failure.
6846141Smao  *
6946141Smao  *	Side Effects:
7046141Smao  *		Allocates memory which should be freed when the hash table
7146141Smao  *		entry is removed.
7246141Smao  */
7346141Smao 
7446141Smao CACHE_ENT *
lruhashput(l,pgno,lruent)7546141Smao lruhashput(l, pgno, lruent)
7646141Smao 	LRUCACHE *l;
7746141Smao 	int pgno;
7846141Smao 	LRU_ENT *lruent;
7946141Smao {
8046141Smao 	int hashind;
8146141Smao 	CACHE_ENT *ce;
8246141Smao 
8346141Smao 	if ((ce = (CACHE_ENT *) malloc((unsigned) sizeof(CACHE_ENT)))
8446141Smao 	    == (CACHE_ENT *) NULL)
8546141Smao 		return ((CACHE_ENT *) NULL);
8646141Smao 
8746141Smao 	hashind = HASH(l, pgno);
8846141Smao 
8946141Smao 	ce->c_pgno = pgno;
9046141Smao 	ce->c_lruent = lruent;
9146141Smao 	ce->c_chain = l->lru_cache[hashind];
9246141Smao 	l->lru_cache[hashind] = ce;
9346141Smao 
9446141Smao 	return (ce);
9546141Smao }
9646141Smao 
9746141Smao /*
9846141Smao  *  LRUHASHDEL -- Delete the entry for a given page from the LRU cache
9946141Smao  *		  hash table.
10046141Smao  *
10146141Smao  *	Parameters:
10246141Smao  *		l -- LRU cache
10346141Smao  *		pgno -- page number for which we should delete htable entry
10446141Smao  *
10546141Smao  *	Returns:
10646141Smao  *		Zero on success, -1 on failure.
10746141Smao  *
10846141Smao  *	Side Effects:
10946141Smao  *		Releases the memory occupied by the hash table entry.
11046141Smao  */
11146141Smao 
11246141Smao int
lruhashdel(l,pgno)11346141Smao lruhashdel(l, pgno)
11446141Smao 	LRUCACHE *l;
11546141Smao 	int pgno;
11646141Smao {
11746141Smao 	CACHE_ENT *ce;
11846141Smao 	CACHE_ENT *sce;
11946141Smao 	int hashind;
12046141Smao 
12146141Smao 	sce = (CACHE_ENT *) NULL;
12246141Smao 	hashind = HASH(l, pgno);
12346141Smao 
12446141Smao 	/* find the entry we want to delete */
12546141Smao 	for (ce = l->lru_cache[hashind];
12646141Smao 	     ce != (CACHE_ENT *) NULL;
12746141Smao 	     ce = ce->c_chain) {
12846141Smao 		if (ce->c_pgno == pgno)
12946141Smao 			break;
13046141Smao 		sce = ce;
13146141Smao 	}
13246141Smao 
13346141Smao 	if (ce == (CACHE_ENT *) NULL)
13446141Smao 		return (-1);
13546141Smao 
13646141Smao 	/* remove it from the chain */
13746141Smao 	if (sce == (CACHE_ENT *) NULL)
13846141Smao 		l->lru_cache[hashind] = ce->c_chain;
13946141Smao 	else
14046141Smao 		sce->c_chain = ce->c_chain;
14146141Smao 
14246141Smao 	/* release it */
14346141Smao 	free ((char *) ce);
14446141Smao 
14546141Smao 	/* success */
14646141Smao 	return (0);
14746141Smao }
148