1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

core/include/kernel_defines.h: Fix container_of()

container_of() is safe to use in regard to alignment requirements, when used
correctly. Using `uintptr_t` instead of `char *` for applying the offset results
in -Wcast-align not complaining.
This commit is contained in:
Marian Buschsieweke 2020-09-22 14:35:12 +02:00
parent f3a5ee69e0
commit 85e506e893
No known key found for this signature in database
GPG Key ID: 61F64C6599B1539F

View File

@ -22,6 +22,7 @@
#define KERNEL_DEFINES_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@ -47,15 +48,15 @@ extern "C" {
# define container_of(PTR, TYPE, MEMBER) \
(_Generic((PTR), \
const __typeof__ (((TYPE *) 0)->MEMBER) *: \
((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))), \
((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))), \
__typeof__ (((TYPE *) 0)->MEMBER) *: \
((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))) \
((TYPE *) ((uintptr_t) (PTR) - offsetof(TYPE, MEMBER))) \
))
#elif defined __GNUC__
# define container_of(PTR, TYPE, MEMBER) \
(__extension__ ({ \
__extension__ const __typeof__ (((TYPE *) 0)->MEMBER) *__m____ = (PTR); \
((TYPE *) ((char *) __m____ - offsetof(TYPE, MEMBER))); \
((TYPE *) ((uintptr_t) __m____ - offsetof(TYPE, MEMBER))); \
}))
#else
# define container_of(PTR, TYPE, MEMBER) \