as: define and use container_of()

This commit is contained in:
Mikael Pettersson 2015-04-27 22:36:58 +02:00
parent a7c47b4013
commit 772d5e4061
4 changed files with 21 additions and 13 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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)

View File

@ -20,6 +20,7 @@
#ifndef TUNIT_H
#define TUNIT_H
#include <stddef.h>
#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 &section 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 */