From 50a5d17f50bb768d90e38a41a5427845ab6f24fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikolai=20G=C3=BCtschow?= Date: Thu, 17 Jul 2025 15:26:13 +0200 Subject: [PATCH] sys/net/unicoap: add UNICOAP_OPTIONS_ALLOC_STATIC --- sys/include/net/unicoap/options.h | 52 ++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/sys/include/net/unicoap/options.h b/sys/include/net/unicoap/options.h index 7f8881a0f1..316b18926d 100644 --- a/sys/include/net/unicoap/options.h +++ b/sys/include/net/unicoap/options.h @@ -177,10 +177,12 @@ static inline void unicoap_options_clear(unicoap_options_t* options) * @{ */ #ifndef DOXYGEN -# define _UNICOAP_OPTIONS_ALLOC(_buf, _name, capacity) \ - uint8_t _buf[capacity]; \ - unicoap_options_t _name; \ - unicoap_options_init(&_name, _buf, capacity); +# define _UNICOAP_OPTIONS_ALLOC(_buf, _name, capacity, _static) \ + _static uint8_t _buf[capacity]; \ + _static unicoap_options_t _name = { \ + .entries = { { .data = _buf } }, \ + .storage_capacity = capacity, \ + }; #endif /** @@ -190,10 +192,28 @@ static inline void unicoap_options_clear(unicoap_options_t* options) * @param capacity Storage buffer capacity in bytes * * Allocates a new @ref unicoap_options_t container and a storage buffer with - * the given capacity, then calls @ref unicoap_options_t::unicoap_options_init. + * the given capacity, and initializes it. No need to call + * @ref unicoap_options_t::unicoap_options_init afterwards. + * + * See @ref UNICOAP_OPTIONS_ALLOC_STATIC for static allocation */ #define UNICOAP_OPTIONS_ALLOC(name, capacity) \ - _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity) + _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity,) + +/** + * @brief Statically allocates options with buffer capacity + * + * @param name Name of the variable storing the options structure + * @param capacity Static storage buffer capacity in bytes + * + * Statically allocates a new @ref unicoap_options_t container and a storage + * buffer with the given capacity, and initializes it. No need to call + * @ref unicoap_options_t::unicoap_options_init afterwards. + * + * See @ref UNICOAP_OPTIONS_ALLOC for non-static allocation + */ +#define UNICOAP_OPTIONS_ALLOC_STATIC(name, capacity) \ + _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity, static) /** * @brief Allocates options with default capacity @@ -201,11 +221,27 @@ static inline void unicoap_options_clear(unicoap_options_t* options) * @param name Name of the variable storing the options structure * * Allocates a new @ref unicoap_options_t container and a storage buffer with - * @ref CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY, - * then calls @ref unicoap_options_t::unicoap_options_init. + * @ref CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY, and initializes it. + * No need to call @ref unicoap_options_t::unicoap_options_init afterwards. + * + * See @ref UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT for static allocation */ #define UNICOAP_OPTIONS_ALLOC_DEFAULT(name) \ UNICOAP_OPTIONS_ALLOC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY) + +/** + * @brief Statically allocates options with default capacity + * + * @param name Name of the variable storing the options structure + * + * Statically allocates a new @ref unicoap_options_t container and a storage buffer + * with @ref CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY, and initializes it. + * No need to call @ref unicoap_options_t::unicoap_options_init afterwards. + * + * See @ref UNICOAP_OPTIONS_ALLOC_DEFAULT for non-static allocation + */ +#define UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT(name) \ + UNICOAP_OPTIONS_ALLOC_STATIC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY) /** @} */ /* MARK: - Option characteristics */