Skip to content

Commit 34fece8

Browse files
aescolardanieldegrasse
authored andcommitted
toolchain/gcc.h: Add macro to get address of unaligned member
In principle getting the address of an unaligned structure member is undefined behaviour, and clang warns as much. Let's provide a macro which can return such an address. Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
1 parent 1fb2c56 commit 34fece8

File tree

1 file changed

+10
-0
lines changed
  • include/zephyr/toolchain

1 file changed

+10
-0
lines changed

include/zephyr/toolchain/gcc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ do { \
174174

175175
#endif
176176

177+
/*
178+
* Get the address of a structure member even if the member may not be properly
179+
* aligned. Note that accessing such an address must be done with care (for
180+
* example with UNALIGNED_GET/PUT) and cannot be in general de-referenced to
181+
* access the member directly, as that would cause a fault in architectures
182+
* which have alignment requirements.
183+
*/
184+
#define UNALIGNED_MEMBER_ADDR(_p, _member) ((__typeof__(_p->_member) *) \
185+
(((intptr_t)(_p)) + offsetof(__typeof__(*_p), _member)))
186+
177187
/* Double indirection to ensure section names are expanded before
178188
* stringification
179189
*/

0 commit comments

Comments
 (0)