[DTrace-devel] [PATCH 4/7] htab: add dt_htab_purge to purge an htab rapidly

Nick Alcock nick.alcock at oracle.com
Tue Oct 5 06:38:48 PDT 2021


Destroying large htabs right before destroying their contents is
annoying: you have to delete every entry one by one from the htab before
you destroy it, lest you leak all the buckets: this also serves to reset
the dt_hentry pointers in every entry.

This is expensive, and (if destroying the buckets anyway) useless work.
Add a new dt_htab_purge, which just deletes the buckets without
adjusting the entries.  Obviously this is dangerous unless you know
you're going to be deleting everything anyway, but in that situation it
is a useful optimization.

Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
---
 libdtrace/dt_htab.c | 19 +++++++++++++++++++
 libdtrace/dt_htab.h |  1 +
 2 files changed, 20 insertions(+)

diff --git a/libdtrace/dt_htab.c b/libdtrace/dt_htab.c
index ff80738f17e6..dc5487ede375 100644
--- a/libdtrace/dt_htab.c
+++ b/libdtrace/dt_htab.c
@@ -241,6 +241,25 @@ int dt_htab_delete(dt_htab_t *htab, void *entry)
 	return 0;
 }
 
+/*
+ * Purge all the buckets in an htab, as if it were new (but the same size as it
+ * always was).  The hentries are *not* reset!  Should be used only before
+ * wholesale deletion of everything that might contain a hentry.
+ */
+void dt_htab_purge(dt_htab_t *htab)
+{
+	int i;
+	for (i = 0; i < htab->size; i++) {
+		dt_hbucket_t *bucket, *next;
+		for (bucket = htab->tab[i]; bucket; bucket = next) {
+			next = bucket->next;
+			free(bucket);
+		}
+		htab->tab[i] = NULL;
+	}
+	htab->nbuckets = 0;
+}
+
 /*
  * Report statistics on the given hashtable.
  */
diff --git a/libdtrace/dt_htab.h b/libdtrace/dt_htab.h
index 9da72163a9dd..0c0f982fb815 100644
--- a/libdtrace/dt_htab.h
+++ b/libdtrace/dt_htab.h
@@ -89,6 +89,7 @@ extern void dt_htab_destroy(struct dtrace_hdl *dtp, dt_htab_t *htab);
 extern int dt_htab_insert(dt_htab_t *htab, void *entry);
 extern int dt_htab_insert_unique(dt_htab_t *htab, void *entry);
 extern void *dt_htab_lookup(const dt_htab_t *htab, const void *entry);
+extern void dt_htab_purge(dt_htab_t *htab);
 extern int dt_htab_delete(dt_htab_t *htab, void *entry);
 extern void dt_htab_stats(const char *name, const dt_htab_t *htab);
 
-- 
2.33.0.256.gb827f06fa9




More information about the DTrace-devel mailing list