diff options
author | H. Peter Anvin <hpa@zytor.com> | 2008-10-30 10:52:08 -0700 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2008-10-30 10:54:55 -0700 |
commit | 9656a581cdb6f505c9adc6b5158b395c21b5c43a (patch) | |
tree | d1eb6cbb5659b1752ed1c2df17830fc38e227cc5 /compiler.h | |
parent | 3e364fe27482c09b33c85b401f601ba7ba5c48bb (diff) | |
download | nasm-9656a581cdb6f505c9adc6b5158b395c21b5c43a.tar.gz nasm-9656a581cdb6f505c9adc6b5158b395c21b5c43a.tar.bz2 nasm-9656a581cdb6f505c9adc6b5158b395c21b5c43a.zip |
compiler.h: add offsetof() and container_of()
offsetof() is a C99 construct; provided here as an ersatz for older
systems.
container_of() is a nonstandard but highly useful construct, which
allows data structure control items like tree structures to be
embedded in larger data structures without the penalty of extra
pointers and allocations.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Diffstat (limited to 'compiler.h')
-rw-r--r-- | compiler.h | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -1,6 +1,6 @@ /* ----------------------------------------------------------------------- * * - * Copyright 2007 The NASM Authors - All Rights Reserved + * Copyright 2007-2008 The NASM Authors - All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the license given in the file "LICENSE" @@ -87,6 +87,19 @@ typedef enum bool { false, true } bool; # endif #endif +/* Provide a substitute for offsetof() if we don't have one. This + variant works on most (but not *all*) systems... */ +#ifndef offsetof +# define offsetof(t,m) ((size_t)&(((t *)0)->m)) +#endif + +/* The container_of construct: if p is a pointer to member m of + container class c, then return a pointer to the container of which + *p is a member. */ +#ifndef container_of +# define container_of(p, c, m) ((c *)((char *)(p) - offsetof(c,m))) +#endif + /* Some misguided platforms hide the defs for these */ #if defined(HAVE_STRCASECMP) && !HAVE_DECL_STRCASECMP int strcasecmp(const char *, const char *); |