summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Rheinsberg <david.rheinsberg@gmail.com>2020-10-21 10:08:12 +0200
committerThomas Haller <thaller@redhat.com>2020-10-21 10:38:34 +0200
commit1250fcc2b13b1486020fe50827c8c84b773e5f74 (patch)
treed56a5cc70fcc23cd40f6f3b2e36b5b37e973e5f6
parent143130066be214280a3c6d4abc4cfe97c10a5f6f (diff)
c-rbtree: align CRBTree with CRBNode
We sometimes store pointers to `CRBTree` in `CRBNode*` variables, so we must make sure CRBTree has matching alignment guarantees. We already check for that with static-assertions. This commit aligns CRBTree with CRBNode for 2-byte aligned machines. While at it, add a short comment explaining what the unions are for. Signed-off-by: David Rheinsberg <david.rheinsberg@gmail.com> https://github.com/c-util/c-rbtree/commit/c795b7657f4fbe7285b846f9aa63b2b9fc2b34b0
-rw-r--r--shared/c-rbtree/src/c-rbtree.h9
1 files changed, 7 insertions, 2 deletions
diff --git a/shared/c-rbtree/src/c-rbtree.h b/shared/c-rbtree/src/c-rbtree.h
index d1941185f..d4d0fe45c 100644
--- a/shared/c-rbtree/src/c-rbtree.h
+++ b/shared/c-rbtree/src/c-rbtree.h
@@ -61,7 +61,8 @@ typedef struct CRBTree CRBTree;
struct CRBNode {
union {
unsigned long __parent_and_flags;
- alignas(4) char __dmmy_for_struct_alignment;
+ /* enforce >=4-byte alignment for @__parent_and_flags */
+ alignas(4) unsigned char __align_dummy;
};
CRBNode *left;
CRBNode *right;
@@ -92,7 +93,11 @@ void c_rbnode_unlink_stale(CRBNode *n);
* To initialize an RB-Tree, set it to NULL / all zero.
*/
struct CRBTree {
- CRBNode *root;
+ union {
+ CRBNode *root;
+ /* enforce >=4-byte alignment for @root */
+ alignas(4) unsigned char __align_dummy;
+ };
};
#define C_RBTREE_INIT {}