PERFORCE change 127141 for review
Fredrik Lindberg
fli at FreeBSD.org
Wed Oct 3 14:02:06 PDT 2007
http://perforce.freebsd.org/chv.cgi?CH=127141
Change 127141 by fli at 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_ */
More information about the p4-projects
mailing list