00001 /*- 00002 * See the file LICENSE for redistribution information. 00003 * 00004 * Copyright (c) 1996, 1997, 1998, 1999, 2000 00005 * Sleepycat Software. All rights reserved. 00006 * 00007 * $Id: db__shash_8h-source.html,v 1.1 2008/06/08 10:18:18 sebdiaz Exp $ 00008 */ 00009 00010 /* Hash Headers */ 00011 typedef SH_TAILQ_HEAD(__hash_head) DB_HASHTAB; 00012 00013 /* 00014 * HASHACCESS -- 00015 * 00016 * Figure out to which bucket an item belongs and lock that bucket, 00017 * returning the bucket index. 00018 * 00019 * synch: beginning of the array of mutexes that protect the table. 00020 * elt: the item on which we're computing the hash function. 00021 * nelems: the number of buckets in the hash table. 00022 * hash: the hash function that operates on elements of the type of elt 00023 * ndx: the index into the hash/synch array that we're locking. 00024 * fh: the locking file handle. 00025 */ 00026 #define HASHACCESS(synch, elt, nelems, hash, ndx, fh) do { \ 00027 ndx = hash(elt) % (nelems); \ 00028 MUTEX_LOCK(&synch[ndx], fh); \ 00029 } while(0) 00030 00031 /* 00032 * HASHRELEASE -- 00033 * 00034 * Release a hash bucket that we have locked. 00035 * 00036 * synch: beginning of the array of mutexes that protect the table. 00037 * ndx: the index into the hash/synch array that we're locking. 00038 * fh: the locking file handle. 00039 */ 00040 #define HASHRELEASE(synch, ndx, fh) do { \ 00041 MUTEX_UNLOCK(&synch[ndx]); \ 00042 } while(0) 00043 00044 /* 00045 * HASHLOOKUP -- 00046 * 00047 * Look up something in a shared memory hash table. The "elt" argument 00048 * should be a key, and cmp_func must know how to compare a key to whatever 00049 * structure it is that appears in the hash table. The comparison function 00050 * 00051 * begin: address of the beginning of the hash table. 00052 * ndx: index into table for this item. 00053 * type: the structure type of the elements that are linked in each bucket. 00054 * field: the name of the field by which the "type" structures are linked. 00055 * elt: the item for which we are searching in the hash table. 00056 * res: the variable into which we'll store the element if we find it. 00057 * cmp: called as: cmp(lookup_elt, table_elt). 00058 * 00059 * If the element is not in the hash table, this macro exits with res set 00060 * to NULL. 00061 */ 00062 #define HASHLOOKUP(begin, ndx, type, field, elt, res, cmp) do { \ 00063 DB_HASHTAB *__bucket; \ 00064 \ 00065 __bucket = &begin[ndx]; \ 00066 for (res = SH_TAILQ_FIRST(__bucket, type); \ 00067 res != NULL; res = SH_TAILQ_NEXT(res, field, type)) \ 00068 if (cmp(elt, res)) \ 00069 break; \ 00070 } while(0) 00071 00072 /* 00073 * HASHINSERT -- 00074 * 00075 * Insert a new entry into the hash table. This assumes that you already 00076 * have the bucket locked and that lookup has failed; don't call it if you 00077 * haven't already called HASHLOOKUP. If you do, you could get duplicate 00078 * entries. 00079 * 00080 * begin: the beginning address of the hash table. 00081 * ndx: the index for this element. 00082 * type: the structure type of the elements that are linked in each bucket. 00083 * field: the name of the field by which the "type" structures are linked. 00084 * elt: the item to be inserted. 00085 */ 00086 #define HASHINSERT(begin, ndx, type, field, elt) do { \ 00087 DB_HASHTAB *__bucket; \ 00088 \ 00089 __bucket = &begin[ndx]; \ 00090 SH_TAILQ_INSERT_HEAD(__bucket, elt, field, type); \ 00091 } while(0) 00092 00093 /* 00094 * HASHREMOVE_EL -- 00095 * Given the object "obj" in the table, remove it. 00096 * 00097 * begin: address of the beginning of the hash table. 00098 * ndx: index into hash table of where this element belongs. 00099 * type: the structure type of the elements that are linked in each bucket. 00100 * field: the name of the field by which the "type" structures are linked. 00101 * obj: the object in the table that we with to delete. 00102 */ 00103 #define HASHREMOVE_EL(begin, ndx, type, field, obj) { \ 00104 DB_HASHTAB *__bucket; \ 00105 \ 00106 __bucket = &begin[ndx]; \ 00107 SH_TAILQ_REMOVE(__bucket, obj, field, type); \ 00108 }