0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-09-22 10:43:39 -04:00

offsetin(): use typeof() if the compiler supports it

If the C compiler has typeof(), we can use a safer implementation of
the offsetin() macro by using typeof(). Since typeof() isn't standard
(yet?) autoconf has to test for it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin (Intel)
2020-07-06 12:39:14 -07:00
parent 70f0d04ee5
commit 30844a4c62
3 changed files with 46 additions and 4 deletions

View File

@@ -189,11 +189,19 @@ typedef enum bool { false, true } bool;
# define offsetof(t,m) ((size_t)&(((t *)0)->m))
#endif
/* This is like offsetof(), but takes an object rather than a type.
Ironically enough this is actually guaranteed to be portable,
as far as I know... */
/* If typeof is defined as a macro, assume we have typeof even if
HAVE_TYPEOF is not declared (e.g. due to not using autoconf.) */
#ifdef typeof
# define HAVE_TYPEOF 1
#endif
/* This is like offsetof(), but takes an object rather than a type. */
#ifndef offsetin
# define offsetin(p,m) ((const char *)&((p).m) - (const char *)&(p))
# ifdef HAVE_TYPEOF
# define offsetin(p,m) offsetof(typeof(p),m)
# else
/* Fallback, technically non-portable if p is uninitialized. */
# define offsetin(p,m) ((const char *)&((p).m) - (const char *)&(p))
#endif
/* The container_of construct: if p is a pointer to member m of