PERFORCE change 120765 for review
Fredrik Lindberg
fli at FreeBSD.org
Sat Jun 2 11:47:30 UTC 2007
http://perforce.freebsd.org/chv.cgi?CH=120765
Change 120765 by fli at fli_genesis on 2007/06/02 11:47:05
- Only duplicate the key if explicitly instructed to do so, most of the
times the key is a part of the data object and it's simply unnecessary
to duplicate it.
- Add hashtbl_walk() which will traverse the hash table.
- Some minor adjustments to comments.
Affected files ...
.. //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.c#4 edit
.. //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.h#4 edit
Differences ...
==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.c#4 (text+ko) ====
@@ -341,7 +341,8 @@
for (i = 0; i < ht->ht_tblsz; i++) {
TAILQ_FOREACH(he, &ht->ht_buckets[i].hb_table, he_next) {
- free(he->he_key);
+ if (he->he_flags & HASHTBL_KEYDUP)
+ free(he->he_key);
}
}
SLIST_FOREACH_SAFE(hep, &ht->ht_new, hep_next, hep2) {
@@ -381,7 +382,7 @@
* Returns 0 on success, on failure -1 is returned
*/
int
-hashtbl_add(struct hashtbl *ht, void *key, size_t keylen, void *data)
+hashtbl_add(struct hashtbl *ht, void *key, size_t keylen, void *data, int flags)
{
uint32_t hval;
struct hashentry *he;
@@ -395,9 +396,15 @@
he->he_hash = hval;
hval &= ht->ht_mask;
+ he->he_flags = flags;
- he->he_key = malloc(keylen);
- memcpy(he->he_key, key, keylen);
+ if (flags & HASHTBL_KEYDUP) {
+ he->he_key = malloc(keylen);
+ memcpy(he->he_key, key, keylen);
+ }
+ else {
+ he->he_key = key;
+ }
he->he_keylen = keylen;
he->he_data = data;
TAILQ_INSERT_TAIL(&ht->ht_buckets[hval].hb_table, he, he_next);
@@ -431,7 +438,8 @@
he = find(ht, hval, key, keylen);
if (he != NULL) {
TAILQ_REMOVE(&ht->ht_buckets[hval].hb_table, he, he_next);
- free(he->he_key);
+ if (he->he_flags & HASHTBL_KEYDUP)
+ free(he->he_key);
free_he(ht, he);
return (0);
}
@@ -459,3 +467,21 @@
he = find(ht, hval, key, keylen);
return (he != NULL) ? he->he_data : NULL;
}
+
+/*
+ * Walk the hash table, cb will be called for every entry
+ * ht - hash table
+ * cb - Callback function
+ */
+void
+hashtbl_walk(struct hashtbl *ht, hashtbl_cb cb)
+{
+ struct hashentry *he, *he2;
+ size_t i;
+
+ for (i = 0; i < ht->ht_tblsz; i++) {
+ TAILQ_FOREACH_SAFE(he, &ht->ht_buckets[i].hb_table, he_next, he2) {
+ cb(ht, he->he_key, he->he_keylen, he->he_data);
+ }
+ }
+}
==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.h#4 (text+ko) ====
@@ -34,9 +34,10 @@
*/
struct hashentry {
TAILQ_ENTRY(hashentry) he_next; /* Next entry */
- uint32_t he_hash; /* Hash key */
- void *he_key; /* Key */
- size_t he_keylen; /* Key length */
+ uint32_t he_hash; /* Computed hash */
+ int he_flags;
+ void *he_key; /* Byte key */
+ size_t he_keylen; /* Byte Key length */
void *he_data; /* Data object pointer */
};
@@ -72,10 +73,14 @@
SLIST_HEAD(, he_pool) ht_free; /* free hashentry objs */
};
+#define HASHTBL_KEYDUP 0x01
+
+typedef void (hashtbl_cb)(struct hashtbl *, void *, size_t, void *);
int hashtbl_init(struct hashtbl *, size_t, size_t, size_t);
void hashtbl_destroy(struct hashtbl *);
-int hashtbl_add(struct hashtbl *, void *, size_t, void *);
+int hashtbl_add(struct hashtbl *, void *, size_t, void *, int);
int hashtbl_del(struct hashtbl *, void *, size_t);
void * hashtbl_find(struct hashtbl *, void *, size_t);
+void hashtbl_walk(struct hashtbl *, hashtbl_cb);
#endif /* _HASH_H_ */
More information about the p4-projects
mailing list