summaryrefslogtreecommitdiff
path: root/src/target
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
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')
-rw-r--r--src/target/arm720t.h7
-rw-r--r--src/target/arm7_9_common.h7
-rw-r--r--src/target/arm920t.h7
-rw-r--r--src/target/arm926ejs.h8
-rw-r--r--src/target/arm966e.h7
-rw-r--r--src/target/armv4_5.h6
-rw-r--r--src/target/armv7a.h7
-rw-r--r--src/target/armv7m.h6
-rw-r--r--src/target/cortex_a8.h7
-rw-r--r--src/target/cortex_m3.h7
-rw-r--r--src/target/target.h15
-rw-r--r--src/target/xscale.h7
12 files changed, 91 insertions, 0 deletions
diff --git a/src/target/arm720t.h b/src/target/arm720t.h
index c7696266..0689e444 100644
--- a/src/target/arm720t.h
+++ b/src/target/arm720t.h
@@ -35,4 +35,11 @@ typedef struct arm720t_common_s
uint32_t far_reg;
} arm720t_common_t;
+static inline struct arm720t_common_s *
+target_to_arm720(struct target_s *target)
+{
+ return container_of(target->arch_info, struct arm720t_common_s,
+ arm7tdmi_common.arm7_9_common.armv4_5_common);
+}
+
#endif /* ARM720T_H */
diff --git a/src/target/arm7_9_common.h b/src/target/arm7_9_common.h
index 80f8fc74..d86ac24d 100644
--- a/src/target/arm7_9_common.h
+++ b/src/target/arm7_9_common.h
@@ -112,6 +112,13 @@ typedef struct arm7_9_common_s
} arm7_9_common_t;
+static inline struct arm7_9_common_s *
+target_to_arm7_9(struct target_s *target)
+{
+ return container_of(target->arch_info, struct arm7_9_common_s,
+ armv4_5_common);
+}
+
int arm7_9_register_commands(struct command_context_s *cmd_ctx);
int arm7_9_poll(target_t *target);
diff --git a/src/target/arm920t.h b/src/target/arm920t.h
index ed851a93..eb66eaa8 100644
--- a/src/target/arm920t.h
+++ b/src/target/arm920t.h
@@ -38,6 +38,13 @@ typedef struct arm920t_common_s
int preserve_cache;
} arm920t_common_t;
+static inline struct arm920t_common_s *
+target_to_arm920(struct target_s *target)
+{
+ return container_of(target->arch_info, struct arm920t_common_s,
+ arm9tdmi_common.arm7_9_common.armv4_5_common);
+}
+
typedef struct arm920t_cache_line_s
{
uint32_t cam;
diff --git a/src/target/arm926ejs.h b/src/target/arm926ejs.h
index 7adbf1fb..ff811e3f 100644
--- a/src/target/arm926ejs.h
+++ b/src/target/arm926ejs.h
@@ -38,6 +38,14 @@ typedef struct arm926ejs_common_s
uint32_t d_far;
} arm926ejs_common_t;
+static inline struct arm926ejs_common_s *
+target_to_arm926(struct target_s *target)
+{
+ return container_of(target->arch_info, struct arm926ejs_common_s,
+ arm9tdmi_common.arm7_9_common.armv4_5_common);
+}
+
+
extern int arm926ejs_init_arch_info(target_t *target, arm926ejs_common_t *arm926ejs, jtag_tap_t *tap);
extern int arm926ejs_register_commands(struct command_context_s *cmd_ctx);
extern int arm926ejs_arch_state(struct target_s *target);
diff --git a/src/target/arm966e.h b/src/target/arm966e.h
index 21dee1e5..710f2071 100644
--- a/src/target/arm966e.h
+++ b/src/target/arm966e.h
@@ -34,6 +34,13 @@ typedef struct arm966e_common_s
uint32_t cp15_control_reg;
} arm966e_common_t;
+static inline struct arm966e_common_s *
+target_to_arm966(struct target_s *target)
+{
+ return container_of(target->arch_info, struct arm966e_common_s,
+ arm9tdmi_common.arm7_9_common.armv4_5_common);
+}
+
extern int arm966e_init_arch_info(target_t *target, arm966e_common_t *arm966e, jtag_tap_t *tap);
extern int arm966e_register_commands(struct command_context_s *cmd_ctx);
extern int arm966e_write_cp15(target_t *target, int reg_addr, uint32_t value);
diff --git a/src/target/armv4_5.h b/src/target/armv4_5.h
index 80f28db3..fb7926b6 100644
--- a/src/target/armv4_5.h
+++ b/src/target/armv4_5.h
@@ -86,6 +86,12 @@ typedef struct armv4_5_common_s
void *arch_info;
} armv4_5_common_t;
+static inline struct armv4_5_common_s *
+target_to_armv4_5(struct target_s *target)
+{
+ return target->arch_info;
+}
+
typedef struct armv4_5_algorithm_s
{
int common_magic;
diff --git a/src/target/armv7a.h b/src/target/armv7a.h
index c5e3257b..3fc97f1f 100644
--- a/src/target/armv7a.h
+++ b/src/target/armv7a.h
@@ -127,6 +127,13 @@ typedef struct armv7a_common_s
} armv7a_common_t;
+static inline struct armv7a_common_s *
+target_to_armv7a(struct target_s *target)
+{
+ return container_of(target->arch_info, struct armv7a_common_s,
+ armv4_5_common);
+}
+
typedef struct armv7a_algorithm_s
{
int common_magic;
diff --git a/src/target/armv7m.h b/src/target/armv7m.h
index d9c62a8d..487d6fde 100644
--- a/src/target/armv7m.h
+++ b/src/target/armv7m.h
@@ -116,6 +116,12 @@ typedef struct armv7m_common_s
void *arch_info;
} armv7m_common_t;
+static inline struct armv7m_common_s *
+target_to_armv7m(struct target_s *target)
+{
+ return target->arch_info;
+}
+
typedef struct armv7m_algorithm_s
{
int common_magic;
diff --git a/src/target/cortex_a8.h b/src/target/cortex_a8.h
index 36956964..b98a7de1 100644
--- a/src/target/cortex_a8.h
+++ b/src/target/cortex_a8.h
@@ -137,6 +137,13 @@ typedef struct cortex_a8_common_s
void *arch_info;
} cortex_a8_common_t;
+static inline struct cortex_a8_common_s *
+target_to_cortex_a8(struct target_s *target)
+{
+ return container_of(target->arch_info, struct cortex_a8_common_s,
+ armv7a_common.armv4_5_common);
+}
+
extern int cortex_a8_init_arch_info(target_t *target, cortex_a8_common_t *cortex_a8, jtag_tap_t *tap);
int cortex_a8_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
int cortex_a8_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
diff --git a/src/target/cortex_m3.h b/src/target/cortex_m3.h
index 1dd724c8..629db8e1 100644
--- a/src/target/cortex_m3.h
+++ b/src/target/cortex_m3.h
@@ -164,4 +164,11 @@ typedef struct cortex_m3_common_s
void *arch_info;
} cortex_m3_common_t;
+static inline struct cortex_m3_common_s *
+target_to_cm3(struct target_s *target)
+{
+ return container_of(target->arch_info,
+ struct cortex_m3_common_s, armv7m);
+}
+
#endif /* CORTEX_M3_H */
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
diff --git a/src/target/xscale.h b/src/target/xscale.h
index 4b34cf88..a1f3d463 100644
--- a/src/target/xscale.h
+++ b/src/target/xscale.h
@@ -134,6 +134,13 @@ typedef struct xscale_common_s
int fast_memory_access;
} xscale_common_t;
+static inline struct xscale_common_s *
+target_to_xscale(struct target_s *target)
+{
+ return container_of(target->arch_info, struct xscale_common_s,
+ armv4_5_common);
+}
+
typedef struct xscale_reg_s
{
int dbg_handler_number;