From: Ian Romanick Date: Sat, 19 Aug 2023 01:57:25 +0000 (-0700) Subject: util/rb-tree: Work around C++'s dislike of offsetof X-Git-Tag: upstream/23.3.3~1966 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0f917d93bf1a6664b65e1b62d97c9b46189a6c90;p=platform%2Fupstream%2Fmesa.git util/rb-tree: Work around C++'s dislike of offsetof This is the same technique used in src/compiler/glsl/list.h. Reviewed-by: Kenneth Graunke Part-of: --- diff --git a/src/util/rb_tree.h b/src/util/rb_tree.h index 2ccc102..9d78987 100644 --- a/src/util/rb_tree.h +++ b/src/util/rb_tree.h @@ -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 *