[master] ccbb958d6 binary heap: add a destructor and test it

Nils Goroll nils.goroll at uplex.de
Tue Aug 4 05:12:03 UTC 2020


commit ccbb958d6f057315233e84a420d15155988bab81
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Tue Jul 28 15:00:13 2020 +0200

    binary heap: add a destructor and test it

diff --git a/include/binary_heap.h b/include/binary_heap.h
index da2b27133..c9e39593c 100644
--- a/include/binary_heap.h
+++ b/include/binary_heap.h
@@ -58,6 +58,11 @@ struct binheap *binheap_new(void *priv, binheap_cmp_t, binheap_update_t);
 	 * 'priv' is passed to cmp and update functions.
 	 */
 
+void binheap_destroy(struct binheap **);
+	/*
+	 * Destroy an empty Binary tree
+	 */
+
 void binheap_insert(struct binheap *, void *);
 	/*
 	 * Insert an item
diff --git a/lib/libvarnish/binary_heap.c b/lib/libvarnish/binary_heap.c
index 7d1f0e50b..f31093f98 100644
--- a/lib/libvarnish/binary_heap.c
+++ b/lib/libvarnish/binary_heap.c
@@ -244,6 +244,21 @@ binheap_new(void *priv, binheap_cmp_t *cmp_f, binheap_update_t *update_f)
 	return (bh);
 }
 
+void
+binheap_destroy(struct binheap **bhp)
+{
+	struct binheap *bh;
+	unsigned u;
+
+	TAKE_OBJ_NOTNULL(bh, bhp, BINHEAP_MAGIC);
+	AZ(binheap_root(bh));
+
+	for (u = 0; u < bh->length; u += ROW_WIDTH)
+		free(ROW(bh, u));
+	free(bh->array);
+	free(bh);
+}
+
 static void
 binheap_update(const struct binheap *bh, unsigned u)
 {
@@ -634,6 +649,10 @@ main(void)
 		}
 		fprintf(stderr, "%d updates OK\n", M);
 	}
+	while ((fp = binheap_root(bh)) != NULL)
+		binheap_delete(bh, fp->idx);
+	binheap_destroy(&bh);
+	AZ(bh);
 	return (0);
 }
 #endif


More information about the varnish-commit mailing list