Skip to contentSkip to conversation

stable-2.02 - container_of: use offsetof from stddef

PATCH Zdenek Kabelac 1 participant 1 message Apr 8, 2020

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=5f766b32cf0d88644232a43a59247476a16fbc9c
Commit:        5f766b32cf0d88644232a43a59247476a16fbc9c
Parent:        5d93892d4ae67b355ab6b418c0c404e1536d055f
Author:        Zdenek Kabelac <[email protected]>
AuthorDate:    Thu Mar 5 12:58:34 2020 +0100
Committer:     Zdenek Kabelac <[email protected]>
CommitterDate: Wed Apr 8 15:22:54 2020 +0200

container_of: use offsetof from stddef

Use standardized offsetof() macro from stddef. Helps to build valid code with latest gcc10 with -O2.

---
 base/memory/container_of.h | 6 ++++--
 lib/device/bcache.h        | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
base/memory/container_of.h +4 -2
index 4e4c662bf..ddb795935 100644
@@ -1,4 +1,4 @@
-// Copyright (C) 2018 Red Hat, Inc. All rights reserved.
+// Copyright (C) 2018 - 2020 Red Hat, Inc. All rights reserved.
 // 
 // This file is part of LVM2.
 //
@@ -13,10 +13,12 @@
 #ifndef BASE_MEMORY_CONTAINER_OF_H
 #define BASE_MEMORY_CONTAINER_OF_H
 
+#include <stddef.h>  // offsetof
+
 //----------------------------------------------------------------
 
 #define container_of(v, t, head) \
-    ((t *)((const char *)(v) - (const char *)&((t *) 0)->head))
+    ((t *)((const char *)(v) - offsetof(t, head)))
 
 //----------------------------------------------------------------
 
lib/device/bcache.h +1 -1
index f9067f70c..c5e6da1ad 100644
@@ -25,7 +25,7 @@
 
 // FIXME: move somewhere more sensible
 #define container_of(v, t, head) \
-    ((t *)((const char *)(v) - (const char *)&((t *) 0)->head))
+    ((t *)((const char *)(v) - offsetof(t, head)))
 
 /*----------------------------------------------------------------*/