From 5f20e31b5b3e95f6050a2cf5ed6291ebc581239a Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Tue, 23 Nov 2021 11:38:06 -0800 Subject: [PATCH] lib/os: bitarray: introduce SYS_BITARRAY_DEFINE_STATIC() This allows to declare a static bitarray struct that is local to the source file. Signed-off-by: Daniel Leung --- include/portability/cmsis_os.h | 2 +- include/sys/bitarray.h | 43 ++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/include/portability/cmsis_os.h b/include/portability/cmsis_os.h index c6ef5ccff0c..f0c3dd51805 100644 --- a/include/portability/cmsis_os.h +++ b/include/portability/cmsis_os.h @@ -291,7 +291,7 @@ static K_THREAD_STACK_ARRAY_DEFINE(stacks_##name, instances, CONFIG_CMSIS_THREAD static struct k_thread cm_thread_##name[instances]; \ static struct k_poll_signal wait_signal_##name; \ static struct k_poll_event wait_events_##name; \ -static SYS_BITARRAY_DEFINE(bitarray_##name, instances); \ +SYS_BITARRAY_DEFINE_STATIC(bitarray_##name, instances); \ static osThreadDef_t os_thread_def_##name = \ { (name), (priority), (instances), (stacksz), (void *)(stacks_##name), \ (cm_thread_##name), (&wait_signal_##name), \ diff --git a/include/sys/bitarray.h b/include/sys/bitarray.h index 265fc4d8451..12e611fe1a8 100644 --- a/include/sys/bitarray.h +++ b/include/sys/bitarray.h @@ -32,6 +32,27 @@ struct sys_bitarray { typedef struct sys_bitarray sys_bitarray_t; +/** + * @def _SYS_BITARRAY_DEFINE + * + * @brief Create a bitarray object. + * + * @param name Name of the bitarray object. + * @param total_bits Total number of bits in this bitarray object. + * @param sba_mod Modifier to the bitarray variables. + */ +#define _SYS_BITARRAY_DEFINE(name, total_bits, sba_mod) \ + sba_mod uint32_t _sys_bitarray_bundles_##name \ + [(((total_bits + 8 - 1) / 8) + sizeof(uint32_t) - 1) \ + / sizeof(uint32_t)] = {0U}; \ + sba_mod sys_bitarray_t name = { \ + .num_bits = total_bits, \ + .num_bundles = (((total_bits + 8 - 1) / 8) \ + + sizeof(uint32_t) - 1) \ + / sizeof(uint32_t), \ + .bundles = _sys_bitarray_bundles_##name, \ + } + /** * @def SYS_BITARRAY_DEFINE * @@ -41,16 +62,18 @@ typedef struct sys_bitarray sys_bitarray_t; * @param total_bits Total number of bits in this bitarray object. */ #define SYS_BITARRAY_DEFINE(name, total_bits) \ - uint32_t _sys_bitarray_bundles_##name \ - [(((total_bits + 8 - 1) / 8) + sizeof(uint32_t) - 1) \ - / sizeof(uint32_t)] = {0U}; \ - sys_bitarray_t name = { \ - .num_bits = total_bits, \ - .num_bundles = (((total_bits + 8 - 1) / 8) \ - + sizeof(uint32_t) - 1) \ - / sizeof(uint32_t), \ - .bundles = _sys_bitarray_bundles_##name, \ - } + _SYS_BITARRAY_DEFINE(name, total_bits,) + +/** + * @def SYS_BITARRAY_DEFINE_STATIC + * + * @brief Create a static bitarray object. + * + * @param name Name of the bitarray object. + * @param total_bits Total number of bits in this bitarray object. + */ +#define SYS_BITARRAY_DEFINE_STATIC(name, total_bits) \ + _SYS_BITARRAY_DEFINE(name, total_bits, static) /** * Set a bit in a bit array