From db116b1ea3c77a3c5850fccbce9e0795faa21dda Mon Sep 17 00:00:00 2001 From: David Brownell Date: Thu, 5 Nov 2009 21:59:39 -0800 Subject: 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 --- src/target/arm926ejs.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/target/arm926ejs.h') 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); -- cgit v1.2.3