X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/b1a20beee623c83315c3ce21abc7bcce103c6efb..d93e9a33ee30faed3945b2a7acfc46b84197d432:/utils/macros.h diff --git a/utils/macros.h b/utils/macros.h index bc83629..b48e6f6 100644 --- a/utils/macros.h +++ b/utils/macros.h @@ -61,16 +61,18 @@ #define MLIB__STR(x) #x #define STR(x) MLIB__STR(x) -/* --- @GLUE@ --- * +/* --- @GLUE@, @GLUE3@ --- * * * Arguments: @x, y@ = two sequences of tokens + * @z@ = a third sequence of tokens * * Returns: A single token formed by gluing together the macro-expansions - * of @x@ and @y@. + * of @x@ and @y@, and @z@ for @GLUE3@. */ #define MLIB__GLUE(x, y) x##y #define GLUE(x, y) MLIB__GLUE(x, y) +#define GLUE3(x, y, z) GLUE(x, MLIB__GLUE(y, z)) /* --- @STATIC_ASSERT@ --- * * @@ -90,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 @@ -103,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@ @@ -119,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 @@ -144,6 +159,15 @@ #define UNQUALIFY(type, p) \ CONVERT_CAREFULLY(type *, const volatile type *, p) +/* --- @EMPTY@ --- * + * + * Arguments: --- + * + * Returns: The empty token sequence. + */ + +#define EMPTY + /* --- @COMMA@ --- * * * Arguments: --- @@ -252,10 +276,10 @@ # define LAUNDER(x) \ ({ __typeof__(x) _y; __asm__("" : "=g"(_y) : "0"(x)); _y; }) # define ADMIRE(x) \ - ({ __asm__("" : : "g"(x)); }) + ({ __asm__("" :: "g"(x)); }) # define ADMIRE_BUF(p, sz) \ - ({ __asm__("" : : "g"(p), "g"(sz)); }) -# define RELAX do __asm__ __volatile__("" ::: "memory"); while (0) + ({ __asm__("" :: "m"(*(unsigned char *)p), "g"(sz) : "memory"); }) +# define RELAX do __asm__(""); while (0) #endif #if CLANG_VERSION_P(3, 3)