From 32bb9743e4bc5d7dc120ce7e8f66d1185f79db47 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Wed, 20 Jul 2022 13:51:15 +0200 Subject: [PATCH] sys/net/gnrc/pktbuf_static: make use of alignas() Since we are now using C11, we can make use of `alignas()` provided by `` to make the alignment code easier to read. --- sys/net/gnrc/pktbuf/include/pktbuf_internal.h | 13 ++++--------- .../gnrc/pktbuf_static/gnrc_pktbuf_static.c | 19 +++++++++---------- .../pktbuf_static/include/pktbuf_static.h | 12 ++++++++++++ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/sys/net/gnrc/pktbuf/include/pktbuf_internal.h b/sys/net/gnrc/pktbuf/include/pktbuf_internal.h index c48db7b82d..ddc3462311 100644 --- a/sys/net/gnrc/pktbuf/include/pktbuf_internal.h +++ b/sys/net/gnrc/pktbuf/include/pktbuf_internal.h @@ -28,6 +28,10 @@ #include "mutex.h" +#if IS_USED(MODULE_GNRC_PKTBUF_STATIC) +#include "pktbuf_static.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -41,15 +45,6 @@ extern "C" { */ extern mutex_t gnrc_pktbuf_mutex; -#if IS_USED(MODULE_GNRC_PKTBUF_STATIC) || DOXYGEN -/** - * @brief The actual static buffer used when module gnrc_pktbuf_static is used - * - * @warning This is an internal buffer and should not be touched by external code - */ -extern uint8_t *gnrc_pktbuf_static_buf; -#endif - /** * @brief Check if the given pointer is indeed part of the packet buffer * diff --git a/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c b/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c index 041d2e9a12..6e981e740b 100644 --- a/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c +++ b/sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c @@ -44,12 +44,9 @@ #endif #define CANARY 0x55 - -/* The static buffer needs to be aligned to word size, so that its start - * address can be casted to `_unused_t *` safely. Just allocating an array of - * (word sized) uintptr_t is a trivial way to do this */ -static uintptr_t _pktbuf_buf[CONFIG_GNRC_PKTBUF_SIZE / sizeof(uintptr_t)]; -uint8_t *gnrc_pktbuf_static_buf = (uint8_t *)_pktbuf_buf; +alignas(sizeof(_unused_t)) uint8_t gnrc_pktbuf_static_buf[CONFIG_GNRC_PKTBUF_SIZE]; +static_assert((CONFIG_GNRC_PKTBUF_SIZE % sizeof(_unused_t)) == 0, + "CONFIG_GNRC_PKTBUF_SIZE has to be a multiple of 8"); static _unused_t *_first_unused; #ifdef DEVELHELP @@ -91,11 +88,13 @@ void gnrc_pktbuf_init(void) { mutex_lock(&gnrc_pktbuf_mutex); if (CONFIG_GNRC_PKTBUF_CHECK_USE_AFTER_FREE) { - memset(_pktbuf_buf, CANARY, sizeof(_pktbuf_buf)); + memset(gnrc_pktbuf_static_buf, CANARY, sizeof(gnrc_pktbuf_static_buf)); } - _first_unused = (_unused_t *)_pktbuf_buf; + /* Silence false -Wcast-align: gnrc_pktbuf_static_buf has qualifier + * `alignas(_unused_t)`, so it is guaranteed to be safe */ + _first_unused = (_unused_t *)(uintptr_t)gnrc_pktbuf_static_buf; _first_unused->next = NULL; - _first_unused->size = sizeof(_pktbuf_buf); + _first_unused->size = sizeof(gnrc_pktbuf_static_buf); mutex_unlock(&gnrc_pktbuf_mutex); } @@ -326,7 +325,7 @@ void gnrc_pktbuf_stats(void) bool gnrc_pktbuf_is_empty(void) { return ((uintptr_t)_first_unused == (uintptr_t)gnrc_pktbuf_static_buf) && - (_first_unused->size == sizeof(_pktbuf_buf)); + (_first_unused->size == sizeof(gnrc_pktbuf_static_buf)); } bool gnrc_pktbuf_is_sane(void) diff --git a/sys/net/gnrc/pktbuf_static/include/pktbuf_static.h b/sys/net/gnrc/pktbuf_static/include/pktbuf_static.h index dce6f8736e..7f55b040d5 100644 --- a/sys/net/gnrc/pktbuf_static/include/pktbuf_static.h +++ b/sys/net/gnrc/pktbuf_static/include/pktbuf_static.h @@ -22,6 +22,7 @@ #define PKTBUF_STATIC_H #include +#include #ifdef __cplusplus extern "C" { @@ -40,6 +41,17 @@ typedef struct _unused { unsigned int size; /**< the size of the unused section */ } _unused_t; + +/** + * @brief The actual static buffer used when module gnrc_pktbuf_static is used + * + * @warning This is an internal buffer and should not be touched by external + * code + * + * @details This buffer is aligned to boundaries of `sizeof(_unused_t)` + */ +extern alignas(sizeof(_unused_t)) uint8_t gnrc_pktbuf_static_buf[CONFIG_GNRC_PKTBUF_SIZE]; + /** * @brief Calculates the required space of a number of bytes including * alignment to the size of @ref _unused_t