util/rb-tree: Work around C++'s dislike of offsetof
authorIan Romanick <ian.d.romanick@intel.com>
Sat, 19 Aug 2023 01:57:25 +0000 (18:57 -0700)
committerMarge Bot <emma+marge@anholt.net>
Thu, 14 Sep 2023 22:31:22 +0000 (22:31 +0000)
This is the same technique used in src/compiler/glsl/list.h.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25091>

src/util/rb_tree.h

index 2ccc102..9d78987 100644 (file)
@@ -93,6 +93,14 @@ struct rb_node *rb_node_next(struct rb_node *node);
 /** Get the next previous (to the left) in the tree or NULL */
 struct rb_node *rb_node_prev(struct rb_node *node);
 
+#ifdef __cplusplus
+/* This macro will not work correctly if `t' uses virtual inheritance. */
+#define rb_tree_offsetof(t, f, p) \
+   (((char *) &((t *) p)->f) - ((char *) p))
+#else
+#define rb_tree_offsetof(t, f, p) offsetof(t, f)
+#endif
+
 /** Retrieve the data structure containing a node
  *
  * \param   type    The type of the containing data structure
@@ -102,7 +110,7 @@ struct rb_node *rb_node_prev(struct rb_node *node);
  * \param   field   The rb_node field in the containing data structure
  */
 #define rb_node_data(type, node, field) \
-    ((type *)(((char *)(node)) - offsetof(type, field)))
+    ((type *)(((char *)(node)) - rb_tree_offsetof(type, field, node)))
 
 /** Insert a node into a tree at a particular location
  *