diff options
Diffstat (limited to 'db2/include/db_shash.h')
-rw-r--r-- | db2/include/db_shash.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/db2/include/db_shash.h b/db2/include/db_shash.h new file mode 100644 index 0000000000..f695a2bafa --- /dev/null +++ b/db2/include/db_shash.h @@ -0,0 +1,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); \ +} |