]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD: compiler: add the container_of() and container_of_safe() macros
authorWilly Tarreau <w@1wt.eu>
Wed, 6 Oct 2021 16:11:38 +0000 (18:11 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 6 Oct 2021 23:36:51 +0000 (01:36 +0200)
These ones are called from a few places in the code and are only provided
by ebtree.h, which is not normal given that some callers do not even use
ebtree.

include/haproxy/compiler.h

index 352da8ec2c3281940c78ad8d91a5c366455b0d75..f18b5fa5cd1a5425dd30f09c06c925f58b3a49e5 100644 (file)
 #endif
 #endif
 
+/* Linux-like "container_of". It returns a pointer to the structure of type
+ * <type> which has its member <name> stored at address <ptr>.
+ */
+#ifndef container_of
+#define container_of(ptr, type, name) ((type *)(((void *)(ptr)) - ((long)&((type *)0)->name)))
+#endif
+
+/* returns a pointer to the structure of type <type> which has its member <name>
+ * stored at address <ptr>, unless <ptr> is 0, in which case 0 is returned.
+ */
+#ifndef container_of_safe
+#define container_of_safe(ptr, type, name) \
+       ({ void *__p = (ptr); \
+               __p ? (type *)(__p - ((long)&((type *)0)->name)) : (type *)0; \
+       })
+#endif
+
 /* Some architectures have a double-word CAS, sometimes even dual-8 bytes.
  * Some architectures support unaligned accesses, others are fine with them
  * but only for non-atomic operations. Also mention those supporting unaligned