From owner-p4-projects@FreeBSD.ORG Sat Jun 2 11:47:30 2007 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 33ECD16A469; Sat, 2 Jun 2007 11:47:30 +0000 (UTC) X-Original-To: perforce@FreeBSD.org Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id BF93A16A421 for ; Sat, 2 Jun 2007 11:47:29 +0000 (UTC) (envelope-from fli@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [69.147.83.41]) by mx1.freebsd.org (Postfix) with ESMTP id A473313C455 for ; Sat, 2 Jun 2007 11:47:29 +0000 (UTC) (envelope-from fli@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.8/8.13.8) with ESMTP id l52BlTxP073039 for ; Sat, 2 Jun 2007 11:47:29 GMT (envelope-from fli@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.8/8.13.8/Submit) id l52BlTCp073036 for perforce@freebsd.org; Sat, 2 Jun 2007 11:47:29 GMT (envelope-from fli@FreeBSD.org) Date: Sat, 2 Jun 2007 11:47:29 GMT Message-Id: <200706021147.l52BlTCp073036@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to fli@FreeBSD.org using -f From: Fredrik Lindberg To: Perforce Change Reviews Cc: Subject: PERFORCE change 120765 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Jun 2007 11:47:30 -0000 http://perforce.freebsd.org/chv.cgi?CH=120765 Change 120765 by fli@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_ */