123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- #ifndef NRF_BALLOC_H__
- #define NRF_BALLOC_H__
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "sdk_errors.h"
- #include "sdk_config.h"
- #include "app_util_platform.h"
- #include "app_util.h"
- #include "nrf_log_instance.h"
- #include "nrf_section.h"
- #define NRF_BALLOC_LOG_NAME balloc
- #if NRF_BALLOC_CONFIG_DEBUG_ENABLED || NRF_BALLOC_CLI_CMDS
- #define NRF_BALLOC_HAS_NAME 1
- #else
- #define NRF_BALLOC_HAS_NAME 0
- #endif
- #define NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_SET(words) (((words) & 0xFF) << 0)
- #define NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_GET(flags) (((flags) >> 0) & 0xFF)
- #define NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_SET(words) (((words) & 0xFF) << 8)
- #define NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_GET(flags) (((flags) >> 8) & 0xFF)
- #define NRF_BALLOC_DEBUG_BASIC_CHECKS_SET(enable) (!!(enable) << 16)
- #define NRF_BALLOC_DEBUG_BASIC_CHECKS_GET(flags) (flags & (1 << 16))
- #define NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_SET(enable) (!!(enable) << 17)
- #define NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_GET(flags) (flags & (1 << 17))
- #define NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_SET(enable) (!!(enable) << 18)
- #define NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_GET(flags) (flags & (1 << 18))
- #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
- #define NRF_BALLOC_DEFAULT_DEBUG_FLAGS \
- ( \
- NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_SET(NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS) | \
- NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_SET(NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS) | \
- NRF_BALLOC_DEBUG_BASIC_CHECKS_SET(NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED) | \
- NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_SET(NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED) | \
- NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_SET(NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED) \
- )
- #else
- #define NRF_BALLOC_DEFAULT_DEBUG_FLAGS 0
- #endif
- typedef struct
- {
- uint8_t * p_stack_pointer;
- uint8_t max_utilization;
- } nrf_balloc_cb_t;
- typedef struct
- {
- nrf_balloc_cb_t * p_cb;
- uint8_t * p_stack_base;
-
- uint8_t * p_stack_limit;
- void * p_memory_begin;
-
- NRF_LOG_INSTANCE_PTR_DECLARE(p_log)
- #if NRF_BALLOC_HAS_NAME
- const char * p_name;
- #endif
- #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
- uint32_t debug_flags;
-
- #endif
- uint16_t block_size;
-
- } nrf_balloc_t;
- #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
- #define NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags) \
- ( \
- (sizeof(uint32_t) * NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_GET(_debug_flags)) + \
- ALIGN_NUM(sizeof(uint32_t), (_element_size)) + \
- (sizeof(uint32_t) * NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_GET(_debug_flags)) \
- )
- #else
- #define NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags) \
- ALIGN_NUM(sizeof(uint32_t), (_element_size))
- #endif
- #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
- #define NRF_BALLOC_ELEMENT_SIZE(_p_balloc) \
- (ALIGN_NUM(sizeof(uint32_t), (_p_balloc)->block_size) - \
- ((sizeof(uint32_t) * NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_GET((_p_balloc)->debug_flags)) + \
- (sizeof(uint32_t) * NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_GET((_p_balloc)->debug_flags))))
- #else
- #define NRF_BALLOC_ELEMENT_SIZE(_p_balloc) \
- (_p_balloc)->block_size
- #endif
- #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
- #define __NRF_BALLOC_ASSIGN_DEBUG_FLAGS(_debug_flags) .debug_flags = (_debug_flags),
- #else
- #define __NRF_BALLOC_ASSIGN_DEBUG_FLAGS(_debug_flags)
- #endif
- #if NRF_BALLOC_HAS_NAME
- #define __NRF_BALLOC_ASSIGN_POOL_NAME(_name) .p_name = STRINGIFY(_name),
- #else
- #define __NRF_BALLOC_ASSIGN_POOL_NAME(_name)
- #endif
- #define NRF_BALLOC_DBG_DEF(_name, _element_size, _pool_size, _debug_flags) \
- STATIC_ASSERT((_pool_size) <= UINT8_MAX); \
- static uint8_t CONCAT_2(_name, _nrf_balloc_pool_stack)[(_pool_size)]; \
- static uint32_t CONCAT_2(_name,_nrf_balloc_pool_mem) \
- [NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags) * (_pool_size) / sizeof(uint32_t)]; \
- static nrf_balloc_cb_t CONCAT_2(_name,_nrf_balloc_cb); \
- NRF_LOG_INSTANCE_REGISTER(NRF_BALLOC_LOG_NAME, _name, \
- NRF_BALLOC_CONFIG_INFO_COLOR, \
- NRF_BALLOC_CONFIG_DEBUG_COLOR, \
- NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL, \
- NRF_BALLOC_CONFIG_LOG_ENABLED ? \
- NRF_BALLOC_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
- NRF_SECTION_ITEM_REGISTER(nrf_balloc, const nrf_balloc_t _name) = \
- { \
- .p_cb = &CONCAT_2(_name,_nrf_balloc_cb), \
- .p_stack_base = CONCAT_2(_name,_nrf_balloc_pool_stack), \
- .p_stack_limit = CONCAT_2(_name,_nrf_balloc_pool_stack) + (_pool_size), \
- .p_memory_begin = CONCAT_2(_name,_nrf_balloc_pool_mem), \
- .block_size = NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags), \
- \
- NRF_LOG_INSTANCE_PTR_INIT(p_log, NRF_BALLOC_LOG_NAME, _name) \
- __NRF_BALLOC_ASSIGN_POOL_NAME(_name) \
- __NRF_BALLOC_ASSIGN_DEBUG_FLAGS(_debug_flags) \
- }
- #define NRF_BALLOC_DEF(_name, _element_size, _pool_size) \
- NRF_BALLOC_DBG_DEF(_name, _element_size, _pool_size, NRF_BALLOC_DEFAULT_DEBUG_FLAGS)
- #define NRF_BALLOC_INTERFACE_DEC(_type, _name) \
- _type * CONCAT_2(_name,_alloc)(void); \
- void CONCAT_2(_name,_free)(_type * p_element)
- #define NRF_BALLOC_INTERFACE_CUSTOM_DEF(_attr, _type, _name, _p_pool) \
- _attr _type * CONCAT_2(_name,_alloc)(void) \
- { \
- GCC_PRAGMA("GCC diagnostic push") \
- GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
- ASSERT((_p_pool) != NULL); \
- ASSERT((_p_pool)->block_size >= \
- NRF_BALLOC_BLOCK_SIZE(sizeof(_type), (_p_pool)->debug_flags)); \
- GCC_PRAGMA("GCC diagnostic pop") \
- return (_type *)(nrf_balloc_alloc(_p_pool)); \
- } \
- \
- _attr void CONCAT_2(_name,_free)(_type * p_element) \
- { \
- GCC_PRAGMA("GCC diagnostic push") \
- GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
- ASSERT((_p_pool) != NULL); \
- ASSERT((_p_pool)->block_size >= \
- NRF_BALLOC_BLOCK_SIZE(sizeof(_type), (_p_pool)->debug_flags)); \
- GCC_PRAGMA("GCC diagnostic pop") \
- nrf_balloc_free((_p_pool), p_element); \
- }
- #define NRF_BALLOC_INTERFACE_DEF(_type, _name, _p_pool) \
- NRF_BALLOC_INTERFACE_CUSTOM_DEF(, _type, _name, _p_pool)
- #define NRF_BALLOC_INTERFACE_LOCAL_DEF(_type, _name, _p_pool) \
- NRF_BALLOC_INTERFACE_CUSTOM_DEF(static, _type, _name, _p_pool)
- ret_code_t nrf_balloc_init(nrf_balloc_t const * p_pool);
- void * nrf_balloc_alloc(nrf_balloc_t const * p_pool);
- void nrf_balloc_free(nrf_balloc_t const * p_pool, void * p_element);
- __STATIC_INLINE uint8_t nrf_balloc_max_utilization_get(nrf_balloc_t const * p_pool);
- #ifndef SUPPRESS_INLINE_IMPLEMENTATION
- __STATIC_INLINE uint8_t nrf_balloc_max_utilization_get(nrf_balloc_t const * p_pool)
- {
- ASSERT(p_pool != NULL);
- return p_pool->p_cb->max_utilization;
- }
- #endif
- __STATIC_INLINE uint8_t nrf_balloc_utilization_get(nrf_balloc_t const * p_pool);
- #ifndef SUPPRESS_INLINE_IMPLEMENTATION
- __STATIC_INLINE uint8_t nrf_balloc_utilization_get(nrf_balloc_t const * p_pool)
- {
- ASSERT(p_pool != NULL);
- return (p_pool->p_stack_limit - p_pool->p_cb->p_stack_pointer);
- }
- #endif
- #ifdef __cplusplus
- }
- #endif
- #endif
|