[DTrace-devel] [PATCH 02/15] htab: support lookups for buckets with multiple entries

Kris Van Hees kris.van.hees at oracle.com
Thu Feb 23 07:23:47 UTC 2023


Probes are represented by a quadruple naming scheme.  For easy lookup,
hashtables are kept based on each naming component.  When performing
lookups based on one of those hashtables, the head element was always
returned which is obviously wrong.

This patch introduced dt_htab_find() which provides for lookups using
a template entry and a secondary comparison function that is used to
evaluate the different entries in the bucket that match the template.

Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libdtrace/dt_htab.c | 15 +++++++++++++++
 libdtrace/dt_htab.h |  3 +++
 2 files changed, 18 insertions(+)

diff --git a/libdtrace/dt_htab.c b/libdtrace/dt_htab.c
index 1974fe92..f651b243 100644
--- a/libdtrace/dt_htab.c
+++ b/libdtrace/dt_htab.c
@@ -214,6 +214,21 @@ void *dt_htab_lookup(const dt_htab_t *htab, const void *entry)
 	return NULL;
 }
 
+/*
+ * Find an entry in the hashtable, using the provided callback function as a
+ * secondary comparison function to differentiate between entries in a bucket.
+ */
+void *dt_htab_find(const dt_htab_t *htab, const void *entry, dt_htab_f *cmpf,
+		   void *arg)
+{
+	void	*ent = dt_htab_lookup(htab, entry);
+
+	while (ent != NULL && !cmpf(ent, arg))
+		ent = htab->ops->next(ent);
+
+	return ent;
+}
+
 /*
  * Remove an entry from the hashtable.  If we are deleting the last entry in a
  * bucket, get rid of the bucket.
diff --git a/libdtrace/dt_htab.h b/libdtrace/dt_htab.h
index 6dae0262..97bb0672 100644
--- a/libdtrace/dt_htab.h
+++ b/libdtrace/dt_htab.h
@@ -97,6 +97,9 @@ extern dt_htab_t *dt_htab_create(struct dtrace_hdl *dtp, dt_htab_ops_t *ops);
 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 void *dt_htab_lookup(const dt_htab_t *htab, const void *entry);
+typedef int dt_htab_f(const void *entry, void *arg);
+extern void *dt_htab_find(const dt_htab_t *htab, const void *entry,
+			  dt_htab_f *cmpf, void *arg);
 extern size_t dt_htab_entries(const dt_htab_t *htab);
 extern int dt_htab_delete(dt_htab_t *htab, void *entry);
 extern void *dt_htab_next(const dt_htab_t *htab, dt_htab_next_t **it);
-- 
2.39.1




More information about the DTrace-devel mailing list