offsetof() in array sizes


Carles Cufi
 

Hi there,

If one includes a offsetof() statement inside a macro that is then used to size an array, the following warning is issued by GCC:

warning: variably modified <VAR_NAME> at file scope

The fix for this, at least for GCC, is to use:

#if defined(__GNUC__)
#define offsetof(s, m) __builtin_offsetof(s, m)
#else
#define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
#endif

Any thoughts about this particular matter? We'd like to use offsetof() when sizing arrays in the BLE Controller but we don't want to duplicate the offsetof() macro.

Thanks,

Carles


Boie, Andrew P
 


If one includes a offsetof() statement inside a macro that is then used to size
an array, the following warning is issued by GCC:

warning: variably modified <VAR_NAME> at file scope

The fix for this, at least for GCC, is to use:

#if defined(__GNUC__)
#define offsetof(s, m) __builtin_offsetof(s, m) #else #define offsetof(s, m)
((size_t)(&(((s *)0)->m))) #endif

Any thoughts about this particular matter? We'd like to use offsetof() when
sizing arrays in the BLE Controller but we don't want to duplicate the
offsetof() macro.
I don't see a problem with replacing the existing definition in the minimal libc with the corrected version above.
Can you send a patch against lib/libc/minimal/include/stddef.h?

Andrew


Carles Cufi
 

Hi Andrew,

-----Original Message-----
From: Boie, Andrew P [mailto:andrew.p.boie(a)intel.com]
Sent: Tuesday, August 23, 2016 16:54
To: Cufi, Carles <Carles.Cufi(a)nordicsemi.no>;
devel(a)lists.zephyrproject.org
Subject: RE: offsetof() in array sizes



If one includes a offsetof() statement inside a macro that is then
used to size an array, the following warning is issued by GCC:

warning: variably modified <VAR_NAME> at file scope

The fix for this, at least for GCC, is to use:

#if defined(__GNUC__)
#define offsetof(s, m) __builtin_offsetof(s, m) #else #define
offsetof(s, m) ((size_t)(&(((s *)0)->m))) #endif

Any thoughts about this particular matter? We'd like to use offsetof()
when sizing arrays in the BLE Controller but we don't want to
duplicate the
offsetof() macro.
I don't see a problem with replacing the existing definition in the
minimal libc with the corrected version above.
Can you send a patch against lib/libc/minimal/include/stddef.h?

Andrew
Thanks for the reply, here's a patch below.

Thanks,

Carles

From ddd980e2418053ae346f25deb8ae029f50cb20b3 Mon Sep 17 00:00:00 2001
From: Carles Cufi <carles.cufi(a)nordicsemi.no>
Date:
Subject: [PATCH 1/1] lib: Use offsetof() builtin with GCC

The default offsetof() implementation generates a warning
(variably modified <variable> at file scope) when used in
to define the size of an array. By using this builtin with
GCC we avoid the warning and make sure no variable-size
arrays are used.

Change-Id: Iae215f777241f7daaa061067230086dffaa8311d
Signed-off-by: Carles Cufi <carles.cufi(a)nordicsemi.no>
---
lib/libc/minimal/include/stddef.h | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/lib/libc/minimal/include/stddef.h b/lib/libc/minimal/include/stddef.h
index 2e1ef66..bcc0a6e 100644
--- a/lib/libc/minimal/include/stddef.h
+++ b/lib/libc/minimal/include/stddef.h
@@ -27,6 +27,10 @@
typedef int ptrdiff_t;
#endif

+#if defined(__GNUC__)
+#define offsetof(type, member) __builtin_offsetof(type, member)
+#else
#define offsetof(type, member) ((size_t) (&((type *) NULL)->member))
+#endif

#endif /* __INC_stddef_h__ */
--
1.9.1


Boie, Andrew P
 

Thanks for the reply, here's a patch below.
Hi Carles,

Can you submit your contribution to our Gerrit server?

https://nexus.zephyrproject.org/content/sites/site/org.zephyrproject.zephyr/latest/contribute/gerrit.html

Regards,
Andrew