diff mbox series

[1/2] libpdbg: Add pdbg_target_parent() function

Message ID 20180813050014.29774-1-alistair@popple.id.au
State Accepted
Headers show
Series [1/2] libpdbg: Add pdbg_target_parent() function | expand

Checks

Context Check Description
snowpatch_ozlabs/apply_patch success master/apply_patch Successfully applied
snowpatch_ozlabs/build-multiarch success Test build-multiarch on branch master

Commit Message

Alistair Popple Aug. 13, 2018, 5 a.m. UTC
This function returns a target of a particular class that is the parent of
a given target or NULL if no such target exists.

Users of the targetting system should use this instead of making
assumptions about target topology by using target->parent for example.

Signed-off-by: Alistair Popple <alistair@popple.id.au>
---
 libpdbg/libpdbg.c | 34 +++++++++++++++++++++++++++-------
 libpdbg/libpdbg.h |  7 +++++++
 2 files changed, 34 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
index 5b4ffdb..b801e5a 100644
--- a/libpdbg/libpdbg.c
+++ b/libpdbg/libpdbg.c
@@ -84,17 +84,37 @@  uint32_t pdbg_target_index(struct pdbg_target *target)
 		return dn->index;
 }
 
-/* Searched up the tree for the first target of the right class and returns its index */
-uint32_t pdbg_parent_index(struct pdbg_target *target, char *class)
+/* Find a target parent from the given class */
+struct pdbg_target *pdbg_target_parent(const char *class, struct pdbg_target *target)
 {
-	struct pdbg_target *tmp;
+	struct pdbg_target *parent;
 
-	for (tmp = target; tmp && tmp->parent; tmp = tmp->parent) {
-		if (!strcmp(class, pdbg_target_class_name(tmp)))
-			return pdbg_target_index(tmp);
+	for (parent = target->parent; parent && parent->parent; parent = parent->parent) {
+		if (!strcmp(class, pdbg_target_class_name(parent)))
+			return parent;
 	}
 
-	return -1;
+	return NULL;
+}
+
+struct pdbg_target *pdbg_target_require_parent(const char *class, struct pdbg_target *target)
+{
+	struct pdbg_target *parent = pdbg_target_parent(class, target);
+
+	assert(parent);
+	return parent;
+}
+
+/* Searched up the tree for the first target of the right class and returns its index */
+uint32_t pdbg_parent_index(struct pdbg_target *target, char *class)
+{
+	struct pdbg_target *parent;
+
+	parent = pdbg_target_parent(class, target);
+	if (parent)
+		return pdbg_target_index(parent);
+	else
+		return -1;
 }
 
 char *pdbg_target_class_name(struct pdbg_target *target)
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index 1c345cb..694085c 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -66,6 +66,13 @@  enum pdbg_target_status {PDBG_TARGET_UNKNOWN = 0, PDBG_TARGET_ENABLED,
 	     target;					      \
 	     target = __pdbg_next_child_target(parent, target))
 
+/* Return the first parent target of the given class, or NULL if the given
+ * target does not have a parent of the given class. */
+struct pdbg_target *pdbg_target_parent(const char *klass, struct pdbg_target *target);
+
+/* Same as above but instead of returning NULL causes an assert failure. */
+struct pdbg_target *pdbg_target_require_parent(const char *klass, struct pdbg_target *target);
+
 /* Set the given property. Will automatically add one if one doesn't exist */
 void pdbg_set_target_property(struct pdbg_target *target, const char *name, const void *val, size_t size);