summary refs log tree commit diff
path: root/db2/include/db_shash.h
blob: f695a2bafad5de0bba1192cb2517dc605a2d8845 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997
 *	Sleepycat Software.  All rights reserved.
 *
 *	@(#)db_shash.h	10.1 (Sleepycat) 4/12/97
 */

/* Hash Headers */
typedef	SH_TAILQ_HEAD(hash_head) DB_HASHTAB;

/*
 * __db_hashlookup --
 *
 * Look up something in a shared memory hash table.  The "elt" argument
 * should be a key, and cmp_func must know how to compare a key to whatever
 * structure it is that appears in the hash table.  The comparison function
 * cmp_func is called as: cmp_func(lookup_elt, table_elt);
 * begin: address of the beginning of the hash table.
 * type: the structure type of the elements that are linked in each bucket.
 * field: the name of the field by which the "type" structures are linked.
 * elt: the item for which we are searching in the hash table.
 * result: the variable into which we'll store the element if we find it.
 * nelems: the number of buckets in the hash table.
 * hash_func: the hash function that operates on elements of the type of elt
 * cmp_func: compare elements of the type of elt with those in the table (of
 *	type "type").
 *
 * If the element is not in the hash table, this macro exits with result
 * set to NULL.
 */
#define	__db_hashlookup(begin, type, field, elt, r, n, hash, cmp) do {	\
	DB_HASHTAB *__bucket;						\
	u_int32_t __ndx;						\
									\
	__ndx = hash(elt) % (n);					\
	__bucket = &begin[__ndx];					\
	for (r = SH_TAILQ_FIRST(__bucket, type);			\
	    r != NULL; r = SH_TAILQ_NEXT(r, field, type))		\
		if (cmp(elt, r))					\
			break;						\
} while(0)

/*
 * __db_hashinsert --
 *
 * Insert a new entry into the hash table.  This assumes that lookup has
 * failed; don't call it if you haven't already called __db_hashlookup.
 * begin: the beginning address of the hash table.
 * type: the structure type of the elements that are linked in each bucket.
 * field: the name of the field by which the "type" structures are linked.
 * elt: the item to be inserted.
 * nelems: the number of buckets in the hash table.
 * hash_func: the hash function that operates on elements of the type of elt
 */
#define	__db_hashinsert(begin, type, field, elt, n, hash) do {		\
	u_int32_t __ndx;						\
	DB_HASHTAB *__bucket;						\
									\
	__ndx = hash(elt) % (n);					\
	__bucket = &begin[__ndx];					\
	SH_TAILQ_INSERT_HEAD(__bucket, elt, field, type);		\
} while(0)

/*
 * __db_hashremove --
 *	Remove the entry with a key == elt.
 * begin: address of the beginning of the hash table.
 * type: the structure type of the elements that are linked in each bucket.
 * field: the name of the field by which the "type" structures are linked.
 * elt: the item to be deleted.
 * nelems: the number of buckets in the hash table.
 * hash_func: the hash function that operates on elements of the type of elt
 * cmp_func: compare elements of the type of elt with those in the table (of
 *	type "type").
 */
#define	__db_hashremove(begin, type, field, elt, n, hash, cmp) {	\
	u_int32_t __ndx;						\
	DB_HASHTAB *__bucket;						\
	SH_TAILQ_ENTRY *__entp;						\
									\
	__ndx = hash(elt) % (n);					\
	__bucket = &begin[__ndx];					\
	__db_hashlookup(begin, type, field, elt, __entp, n, hash, cmp);	\
	SH_TAILQ_REMOVE(__bucket, __entp, field, type);			\
}

/*
 * __db_hashremove_el --
 *	Given the object "obj" in the table, remove it.
 * begin: address of the beginning of the hash table.
 * type: the structure type of the elements that are linked in each bucket.
 * field: the name of the field by which the "type" structures are linked.
 * obj: the object in the table that we with to delete.
 * nelems: the number of buckets in the hash table.
 * hash_func: the hash function that operates on elements of the type of elt
 */
#define	__db_hashremove_el(begin, type, field, obj, n, hash) {		\
	u_int32_t __ndx;						\
	DB_HASHTAB *__bucket;						\
									\
	__ndx = hash(obj) % (n);					\
	__bucket = &begin[__ndx];					\
	SH_TAILQ_REMOVE(__bucket, obj, field, type);			\
}