X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/d04c0e00da3a27693bbf9cc4f2d5c88e56d80f20..d93e9a33ee30faed3945b2a7acfc46b84197d432:/utils/macros.h diff --git a/utils/macros.h b/utils/macros.h index 2ca5a06..b48e6f6 100644 --- a/utils/macros.h +++ b/utils/macros.h @@ -92,7 +92,7 @@ IGNORABLE extern char static_assert_failed[1 - 2*!(cond)] #endif -/* --- @CHECK_TYPE@ --- +/* --- @CHECK_TYPE@ --- * * * Arguments: @expty@ = expected type of @x@ * @expty x@ = some object @@ -105,7 +105,7 @@ #define CHECK_TYPE(expty, x) (!sizeof(*(expty *)0 = (x))) -/* --- @CONVERT_CAREFULLY@ --- +/* --- @CONVERT_CAREFULLY@ --- * * * Arguments: @newty@ = new type for the result * @expty@ = expected type of @x@ @@ -121,6 +121,19 @@ #define CONVERT_CAREFULLY(newty, expty, x) \ (CHECK_TYPE(expty, x) + (/*unconst unvolatile*/ newty)(x)) +/* --- @CONTAINER@ --- * + * + * Arguments: @type@ = the parent type, a structure or union + * @mem@ = the name of the member we have a pointer to + * @memty *p@ = pointer to @member@ within @type@ + * + * Returns: The address of the containing @ty@ object. + */ + +#define CONTAINER(type, member, p) \ + (!sizeof((p) = &((type *)0)->mem) + \ + (type *)((unsigned char *)(p) - offsetof(type, mem))) + /* --- @UNCONST@, @UNVOLATILE@, @UNQUALIFY@ --- * * * Arguments: @type@ = a type name