[master] adb0b3778 import __containerof() from freebsd
Nils Goroll
nils.goroll at uplex.de
Fri Jun 13 09:34:05 UTC 2025
commit adb0b3778858405c4d504026cd2d9845a92fad64
Author: Nils Goroll <nils.goroll at uplex.de>
Date: Fri Jun 13 09:28:22 2025 +0200
import __containerof() from freebsd
it is already needed by VLIST_PREV and VSTAILQ_LAST from vqueue.h
diff --git a/flint.lnt b/flint.lnt
index 6607b23ad..0a29357b9 100644
--- a/flint.lnt
+++ b/flint.lnt
@@ -261,7 +261,7 @@
///////////////////////////////////////////////////////////////////////
// <vdef.h>
-emacro(527, NEEDLESS) // unreachable code
--emacro(160, _vtake) // The sequence '( {' is non standard
+-emacro(160, _vtake, __containerof) // The sequence '( {' is non standard
+rw( __typeof__ )
///////////////////////////////////////////////////////////////////////
diff --git a/include/vdef.h b/include/vdef.h
index 381a4f799..d71716c47 100644
--- a/include/vdef.h
+++ b/include/vdef.h
@@ -38,6 +38,7 @@
*/
#include <stddef.h>
+#include <stdint.h>
#ifdef VDEF_H_INCLUDED
# error "vdef.h included multiple times"
@@ -282,3 +283,31 @@ typedef struct {
/* #3020 dummy definitions until PR is merged*/
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
+
+#ifndef __DEQUALIFY
+#define __DEQUALIFY(type, var) ((type)(uintptr_t)(const volatile void *)(var))
+#endif
+
+/**********************************************************************
+ * vqueue.h presupposes the __containerof() macro for VSTAILQ_LAST(),
+ * which is in FreeBSD cdefs.h.
+ *
+ * This differs from cdefs.h in that uintptr_t is taken from stdint.h, and
+ * offsetof() is taken from stddef.h.
+ */
+
+/*
+ * Given the pointer x to the member m of the struct s, return
+ * a pointer to the containing structure. When using GCC, we first
+ * assign pointer x to a local variable, to check that its type is
+ * compatible with member m.
+ */
+#if __GNUC_PREREQ__(3, 1)
+#define __containerof(x, s, m) ({ \
+ const volatile __typeof(((s *)0)->m) *__x = (x); \
+ __DEQUALIFY(s *, (const volatile char *)__x - offsetof(s, m)); \
+})
+#else
+#define __containerof(x, s, m) \
+ __DEQUALIFY(s *, (const volatile char *)(x) - offsetof(s, m))
+#endif
More information about the varnish-commit
mailing list