From owner-p4-projects@FreeBSD.ORG Wed Oct 3 21:02:05 2007 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id C0D8E16A421; Wed, 3 Oct 2007 21:02:05 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1D6AA16A418 for ; Wed, 3 Oct 2007 21:02:05 +0000 (UTC) (envelope-from fli@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id ED23113C46E for ; Wed, 3 Oct 2007 21:02:04 +0000 (UTC) (envelope-from fli@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.1/8.14.1) with ESMTP id l93L24Sn094293 for ; Wed, 3 Oct 2007 21:02:04 GMT (envelope-from fli@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.1/8.14.1/Submit) id l93L24wa094290 for perforce@freebsd.org; Wed, 3 Oct 2007 21:02:04 GMT (envelope-from fli@FreeBSD.org) Date: Wed, 3 Oct 2007 21:02:04 GMT Message-Id: <200710032102.l93L24wa094290@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 127141 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: Wed, 03 Oct 2007 21:02:06 -0000 http://perforce.freebsd.org/chv.cgi?CH=127141 Change 127141 by fli@fli_nexus on 2007/10/03 21:01:46 - Add hashtbl_flush() that flushes all entries without destroying the table itself. - Add missing checks for failed memory allocations. - Plug a memory leak, some memory wasn't freed properly when the table was destroyed. Affected files ... .. //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.c#7 edit .. //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.h#7 edit Differences ... ==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.c#7 (text+ko) ==== @@ -248,6 +248,8 @@ len = 1 << ht->ht_alloc_size; hep = malloc(sizeof(struct he_pool) + (sizeof(struct hashentry) * len)); + if (hep == NULL) + return (NULL); hep->hep_size = len; hep->hep_pos = 0; SLIST_INSERT_HEAD(&ht->ht_new, hep, hep_next); @@ -291,6 +293,8 @@ ht->ht_mask = ht->ht_tblsz - 1; bkts = malloc(sizeof(struct hashbkt) * ht->ht_tblsz); + if (bkts == NULL) + return; for (i = 0; i < ht->ht_tblsz; i++) { TAILQ_INIT(&bkts[i].hb_table); bkts[i].hb_len = 0; @@ -320,6 +324,8 @@ size_t i; ht->ht_bkts = malloc(sizeof(struct hashbkt) * len); + if (ht->ht_bkts == NULL) + return (-1); ht->ht_tblsz = len; ht->ht_grow = growsz; ht->ht_col = col; @@ -342,16 +348,9 @@ void hashtbl_destroy(struct hashtbl *ht) { - struct hashentry *he; struct he_pool *hep, *hep2; - size_t i; - for (i = 0; i < ht->ht_tblsz; i++) { - TAILQ_FOREACH(he, &ht->ht_bkts[i].hb_table, he_next) { - if (he->he_flags & HASHTBL_KEYDUP) - free(he->he_key.vol); - } - } + hashtbl_flush(ht); SLIST_FOREACH_SAFE(hep, &ht->ht_new, hep_next, hep2) { free(hep); } @@ -401,6 +400,8 @@ return (-1); he = alloc_he(ht); + if (he == NULL) + return (-1); he->he_hash = hval; hval &= ht->ht_mask; @@ -408,6 +409,8 @@ if (flags & HASHTBL_KEYDUP) { he->he_key.vol = malloc(keylen); + if (he->he_key.vol == NULL) + return (-1); memcpy(he->he_key.vol, key, keylen); } else { @@ -512,3 +515,23 @@ } } } + +/* + * Flush all entries in table without destroying it + * ht - hash table + */ +void +hashtbl_flush(struct hashtbl *ht) +{ + struct hashentry *he, *he2; + size_t i; + + for (i = 0; i < ht->ht_tblsz; i++) { + TAILQ_FOREACH_SAFE(he, &ht->ht_bkts[i].hb_table, he_next, he2) { + TAILQ_REMOVE(&ht->ht_bkts[i].hb_table, he, he_next); + if (he->he_flags & HASHTBL_KEYDUP) + free(he->he_key.vol); + free_he(ht, he); + } + } +} ==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/hash.h#7 (text+ko) ==== @@ -86,5 +86,6 @@ int hashtbl_del(struct hashtbl *, const void *, size_t); void * hashtbl_find(struct hashtbl *, const void *, size_t); void hashtbl_walk(struct hashtbl *, hashtbl_cb, void *); +void hashtbl_flush(struct hashtbl *); #endif /* _HASH_H_ */