summaryrefslogtreecommitdiff
path: root/src/target/target.h
diff options
context:
space:
mode:
authorDavid Brownell <dbrownell@users.sourceforge.net>2009-11-05 21:59:39 -0800
committerDavid Brownell <dbrownell@users.sourceforge.net>2009-11-05 21:59:39 -0800
commitdb116b1ea3c77a3c5850fccbce9e0795faa21dda (patch)
tree9c9f456f726fe99b098324750b6942bf6c2bd497 /src/target/target.h
parentb7e4c26b9bb10e6e0ebfb07e5d43f0d62526cde2 (diff)
downloadopenocd+libswd-db116b1ea3c77a3c5850fccbce9e0795faa21dda.tar.gz
openocd+libswd-db116b1ea3c77a3c5850fccbce9e0795faa21dda.tar.bz2
openocd+libswd-db116b1ea3c77a3c5850fccbce9e0795faa21dda.tar.xz
openocd+libswd-db116b1ea3c77a3c5850fccbce9e0795faa21dda.zip
target: provide container_of()
Provide a cleaner way to handle single inheritance of targets in C, using the same model Linux does: structs containing other structs, un-nested via calls to a "container_of()" macro that are packaged in typesafe inline functions. Targets already use this containment idiom, but make it much more complicated because they un-nest using embedded "void *" pointers ... in chains of up to five per target, which is all pure needless complication. (Example: arm92x core, arm9tdmi, arm7_9, armv4_5 ... on top of the base "target" class.) Applying this scheme consistently simplifies things, and gets rid of many error-prone untyped pointers. It won't change any part of the type model though -- it just simplifies things. (And facilitates more cleanup later on.) Rule of thumb: where there's an X->arch_info void* pointer, access to that pointer can and should be removed. It may be convenient to set up pointers to some of the embedded structs; and shrink their current "*_common" names (annoyingly long). Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Diffstat (limited to 'src/target/target.h')
-rw-r--r--src/target/target.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/target/target.h b/src/target/target.h
index c971f18b..1bbf40f0 100644
--- a/src/target/target.h
+++ b/src/target/target.h
@@ -26,6 +26,8 @@
#ifndef TARGET_H
#define TARGET_H
+#include <stddef.h>
+
#include "breakpoints.h"
#include "algorithm.h"
#include "command.h"
@@ -34,6 +36,19 @@ struct reg_s;
struct trace_s;
struct command_context_s;
+
+/**
+ * Cast a member of a structure out to the containing structure.
+ * @param ptr The pointer to the member.
+ * @param type The type of the container struct this is embedded in.
+ * @param member The name of the member within the struct.
+ *
+ * This is a mechanism which is used throughout the Linux kernel.
+ */
+#define container_of(ptr, type, member) ({ \
+ const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+ (type *)( (char *)__mptr - offsetof(type,member) );})
+
/*
* TARGET_UNKNOWN = 0: we don't know anything about the target yet
* TARGET_RUNNING = 1: the target is executing user code