diff --git a/as/assemble.c b/as/assemble.c index 5d575ec..35812a3 100644 --- a/as/assemble.c +++ b/as/assemble.c @@ -28,7 +28,7 @@ static int assemble_section(struct hashnode *hashnode, void *data) { - struct section *section = (struct section*)hashnode; /*XXX*/ + struct section *section = container_of(hashnode, struct section, hashnode); struct tunit *tunit = data; unsigned long dot; struct stmt *stmt; diff --git a/as/output.c b/as/output.c index 2eb6c0b..26880a2 100644 --- a/as/output.c +++ b/as/output.c @@ -67,7 +67,7 @@ static int append_section(struct context *context, struct section *section) static int process_section(struct hashnode *hashnode, void *data) { - struct section *section = (struct section*)hashnode; + struct section *section = container_of(hashnode, struct section, hashnode); struct context *context = data; return append_section(context, section); @@ -95,7 +95,7 @@ static void output_section_epilogue(struct context *context, struct section *sec static int output_section(struct hashnode *hashnode, void *data) { - struct section *section = (struct section*)hashnode; + struct section *section = container_of(hashnode, struct section, hashnode); struct context *context = data; if (section->dot == 0 || (section->image == NULL && section->output == NULL)) @@ -140,7 +140,7 @@ static int output_section_header(struct context *context, const struct section * static int output_shdr(struct hashnode *hashnode, void *data) { - struct section *section = (struct section*)hashnode; + struct section *section = container_of(hashnode, struct section, hashnode); struct context *context = data; if (section->dot == 0) @@ -156,7 +156,7 @@ static int output_strtab(struct context *context, struct strtab *strtab) static int process_symbol(struct hashnode *hashnode, void *data) { - struct symbol *symbol = (struct symbol*)hashnode; + struct symbol *symbol = container_of(hashnode, struct symbol, hashnode); struct context *context = data; ++context->symnum; @@ -175,7 +175,7 @@ struct finalize_symbol_context { static int finalize_symbol(struct hashnode *hashnode, void *data) { - struct symbol *symbol = (struct symbol*)hashnode; + struct symbol *symbol = container_of(hashnode, struct symbol, hashnode); struct finalize_symbol_context *fsctx = data; Elf36_Word st_shndx; diff --git a/as/tunit.c b/as/tunit.c index a150801..9e24f0a 100644 --- a/as/tunit.c +++ b/as/tunit.c @@ -48,8 +48,7 @@ static uintptr_t string_hash(const char *string) */ static struct section *section_from_hashnode(const struct hashnode *hashnode) { - /* hashnode is first in section, so no need to mess with offsetof */ - return (struct section*)hashnode; + return container_of(hashnode, struct section, hashnode); } static int section_eq(const struct hashnode *hashnode, const void *data) @@ -167,8 +166,7 @@ pdp10_uint36_t strtab_enter(struct tunit *tunit, struct strtab *strtab, const ch static int strtab_section_output(PDP10_FILE *pdp10fp, const struct section *section) { - /* section is first in strtab, so no need to mess with offsetof */ - const struct strtab *strtab = (const struct strtab*)section; + const struct strtab *strtab = container_of(section, const struct strtab, section); struct strtab_entry *here; unsigned int i; @@ -197,8 +195,7 @@ void strtab_init(struct strtab *strtab, const char *name) */ static struct symbol *symbol_from_hashnode(const struct hashnode *hashnode) { - /* hashnode is first in symbol, so no need to mess with offsetof */ - return (struct symbol*)hashnode; + return container_of(hashnode, struct symbol, hashnode); } static int symbol_eq(const struct hashnode *hashnode, const void *data) diff --git a/as/tunit.h b/as/tunit.h index 01c7995..ad5737b 100644 --- a/as/tunit.h +++ b/as/tunit.h @@ -20,6 +20,7 @@ #ifndef TUNIT_H #define TUNIT_H +#include #include "pdp10-elf36.h" #include "pdp10-stdio.h" #include "hashtab.h" @@ -88,7 +89,7 @@ struct strtab_entry { }; struct strtab { - struct section section; /* first to simplify mapping §ion to &strtab */ + struct section section; struct strtab_entry *head; }; @@ -125,4 +126,14 @@ struct symbol *tunit_symbol_enter(struct tunit *tunit, const char *name); pdp10_uint36_t strtab_enter(struct tunit *tunit, struct strtab *strtab, const char *name); void strtab_init(struct strtab *strtab, const char *name); +/** + * container_of - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + */ +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + #endif /* TUNIT_H */