Commit db116b1e authored by David Brownell's avatar David Brownell
Browse files

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: default avatarDavid Brownell <dbrownell@users.sourceforge.net>
parent b7e4c26b
......@@ -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 */
......@@ -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);
......
......@@ -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;
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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 */
......@@ -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
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment