chiark / gitweb /
@@@ random mess
[mLib] / utils / macros.h
index 2ca5a06c2d316deb25d98f4baa865f2dbe98f10e..b48e6f68fb13335e31ca8cbebbe0adfc112a4125 100644 (file)
@@ -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
 
 #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@
 #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