From: David Brownell Date: Fri, 6 Nov 2009 05:59:39 +0000 (-0800) Subject: target: provide container_of() X-Git-Tag: v0.4.0-rc1~900 X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=commitdiff_plain;h=db116b1ea3c77a3c5850fccbce9e0795faa21dda 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 --- diff --git a/src/target/arm720t.h b/src/target/arm720t.h index c7696266c8..0689e44421 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 80f8fc7480..d86ac24dc9 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 ed851a9385..eb66eaa811 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 7adbf1fbd3..ff811e3f26 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 21dee1e545..710f207166 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 80f28db395..fb7926b664 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 c5e3257be2..3fc97f1fe3 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 d9c62a8d44..487d6fde59 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 369569642b..b98a7de1ce 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 1dd724c8c8..629db8e114 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 c971f18bd6..1bbf40f0cd 100644 --- a/src/target/target.h +++ b/src/target/target.h @@ -26,6 +26,8 @@ #ifndef TARGET_H #define TARGET_H +#include + #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 4b34cf88b7..a1f3d46342 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;