From: Willy Tarreau Date: Wed, 6 Oct 2021 16:11:38 +0000 (+0200) Subject: BUILD: compiler: add the container_of() and container_of_safe() macros X-Git-Tag: v2.5-dev9~95 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=337f23a11202294948527d020a98967122d870fd;p=thirdparty%2Fhaproxy.git BUILD: compiler: add the container_of() and container_of_safe() macros 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. --- diff --git a/include/haproxy/compiler.h b/include/haproxy/compiler.h index 352da8ec2c..f18b5fa5cd 100644 --- a/include/haproxy/compiler.h +++ b/include/haproxy/compiler.h @@ -171,6 +171,23 @@ #endif #endif +/* Linux-like "container_of". It returns a pointer to the structure of type + * which has its member stored at address . + */ +#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 which has its member + * stored at address , unless 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