1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

sys/net/gnrc/pktbuf_static: make use of alignas()

Since we are now using C11, we can make use of `alignas()` provided by
`<stdalign.h>` to make the alignment code easier to read.
This commit is contained in:
Marian Buschsieweke 2022-07-20 13:51:15 +02:00
parent 055d960d5e
commit 32bb9743e4
No known key found for this signature in database
GPG Key ID: CB8E3238CE715A94
3 changed files with 25 additions and 19 deletions

View File

@ -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
*

View File

@ -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)

View File

@ -22,6 +22,7 @@
#define PKTBUF_STATIC_H
#include <sys/types.h>
#include <stdalign.h>
#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