nrf_balloc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**
  41. * @defgroup nrf_balloc Block memory allocator
  42. * @{
  43. * @ingroup app_common
  44. * @brief This module handles block memory allocator features.
  45. */
  46. #ifndef NRF_BALLOC_H__
  47. #define NRF_BALLOC_H__
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. #include "sdk_errors.h"
  52. #include "sdk_config.h"
  53. #include "app_util_platform.h"
  54. #include "app_util.h"
  55. #include "nrf_log_instance.h"
  56. #include "nrf_section.h"
  57. /** @brief Name of the module used for logger messaging.
  58. */
  59. #define NRF_BALLOC_LOG_NAME balloc
  60. #if NRF_BALLOC_CONFIG_DEBUG_ENABLED || NRF_BALLOC_CLI_CMDS
  61. #define NRF_BALLOC_HAS_NAME 1
  62. #else
  63. #define NRF_BALLOC_HAS_NAME 0
  64. #endif
  65. /**@defgroup NRF_BALLOC_DEBUG Macros for preparing debug flags for block allocator module.
  66. * @{ */
  67. #define NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_SET(words) (((words) & 0xFF) << 0)
  68. #define NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_GET(flags) (((flags) >> 0) & 0xFF)
  69. #define NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_SET(words) (((words) & 0xFF) << 8)
  70. #define NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_GET(flags) (((flags) >> 8) & 0xFF)
  71. #define NRF_BALLOC_DEBUG_BASIC_CHECKS_SET(enable) (!!(enable) << 16)
  72. #define NRF_BALLOC_DEBUG_BASIC_CHECKS_GET(flags) (flags & (1 << 16))
  73. #define NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_SET(enable) (!!(enable) << 17)
  74. #define NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_GET(flags) (flags & (1 << 17))
  75. #define NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_SET(enable) (!!(enable) << 18)
  76. #define NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_GET(flags) (flags & (1 << 18))
  77. /**@} */
  78. /**@brief Default debug flags for @ref nrf_balloc. This is used by the @ref NRF_BALLOC_DEF macro.
  79. * Flags can be changed in @ref sdk_config.
  80. */
  81. #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
  82. #define NRF_BALLOC_DEFAULT_DEBUG_FLAGS \
  83. ( \
  84. NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_SET(NRF_BALLOC_CONFIG_HEAD_GUARD_WORDS) | \
  85. NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_SET(NRF_BALLOC_CONFIG_TAIL_GUARD_WORDS) | \
  86. NRF_BALLOC_DEBUG_BASIC_CHECKS_SET(NRF_BALLOC_CONFIG_BASIC_CHECKS_ENABLED) | \
  87. NRF_BALLOC_DEBUG_DOUBLE_FREE_CHECK_SET(NRF_BALLOC_CONFIG_DOUBLE_FREE_CHECK_ENABLED) | \
  88. NRF_BALLOC_DEBUG_DATA_TRASHING_CHECK_SET(NRF_BALLOC_CONFIG_DATA_TRASHING_CHECK_ENABLED) \
  89. )
  90. #else
  91. #define NRF_BALLOC_DEFAULT_DEBUG_FLAGS 0
  92. #endif // NRF_BALLOC_CONFIG_DEBUG_ENABLED
  93. /**@brief Block memory allocator control block.*/
  94. typedef struct
  95. {
  96. uint8_t * p_stack_pointer; //!< Current allocation stack pointer.
  97. uint8_t max_utilization; //!< Maximum utilization of the memory pool.
  98. } nrf_balloc_cb_t;
  99. /**@brief Block memory allocator pool instance. The pool is made of elements of the same size. */
  100. typedef struct
  101. {
  102. nrf_balloc_cb_t * p_cb; //!< Pointer to the instance control block.
  103. uint8_t * p_stack_base; //!< Base of the allocation stack.
  104. /**<
  105. * Stack is used to store handlers to not allocated elements.
  106. */
  107. uint8_t * p_stack_limit; //!< Maximum possible value of the allocation stack pointer.
  108. void * p_memory_begin; //!< Pointer to the start of the memory pool.
  109. /**<
  110. * Memory is used as a heap for blocks.
  111. */
  112. NRF_LOG_INSTANCE_PTR_DECLARE(p_log) //!< Pointer to instance of the logger object (Conditionally compiled).
  113. #if NRF_BALLOC_HAS_NAME
  114. const char * p_name; //!< Pointer to string with pool name.
  115. #endif
  116. #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
  117. uint32_t debug_flags; //!< Debugging settings.
  118. /**<
  119. * Debug flag should be created by @ref NRF_BALLOC_DEBUG.
  120. */
  121. #endif // NRF_BALLOC_CONFIG_DEBUG_ENABLED
  122. uint16_t block_size; //!< Size of the allocated block (including debug overhead).
  123. /**<
  124. * Single block contains user element with header and tail
  125. * words.
  126. */
  127. } nrf_balloc_t;
  128. /**@brief Get total memory consumed by single block (element size with overhead caused by debug
  129. * flags).
  130. *
  131. * @param[in] _element_size Size of an element.
  132. * @param[in] _debug_flags Debug flags.
  133. */
  134. #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
  135. #define NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags) \
  136. ( \
  137. (sizeof(uint32_t) * NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_GET(_debug_flags)) + \
  138. ALIGN_NUM(sizeof(uint32_t), (_element_size)) + \
  139. (sizeof(uint32_t) * NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_GET(_debug_flags)) \
  140. )
  141. #else
  142. #define NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags) \
  143. ALIGN_NUM(sizeof(uint32_t), (_element_size))
  144. #endif // NRF_BALLOC_CONFIG_DEBUG_ENABLED
  145. /**@brief Get element size ( excluding debugging overhead is present)
  146. * flags).
  147. *
  148. * @param[in] _p_balloc Pointer to balloc instance.
  149. */
  150. #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
  151. #define NRF_BALLOC_ELEMENT_SIZE(_p_balloc) \
  152. (ALIGN_NUM(sizeof(uint32_t), (_p_balloc)->block_size) - \
  153. ((sizeof(uint32_t) * NRF_BALLOC_DEBUG_HEAD_GUARD_WORDS_GET((_p_balloc)->debug_flags)) + \
  154. (sizeof(uint32_t) * NRF_BALLOC_DEBUG_TAIL_GUARD_WORDS_GET((_p_balloc)->debug_flags))))
  155. #else
  156. #define NRF_BALLOC_ELEMENT_SIZE(_p_balloc) \
  157. (_p_balloc)->block_size
  158. #endif // NRF_BALLOC_CONFIG_DEBUG_ENABLED
  159. #if NRF_BALLOC_CONFIG_DEBUG_ENABLED
  160. #define __NRF_BALLOC_ASSIGN_DEBUG_FLAGS(_debug_flags) .debug_flags = (_debug_flags),
  161. #else
  162. #define __NRF_BALLOC_ASSIGN_DEBUG_FLAGS(_debug_flags)
  163. #endif
  164. #if NRF_BALLOC_HAS_NAME
  165. #define __NRF_BALLOC_ASSIGN_POOL_NAME(_name) .p_name = STRINGIFY(_name),
  166. #else
  167. #define __NRF_BALLOC_ASSIGN_POOL_NAME(_name)
  168. #endif
  169. /**@brief Create a block allocator instance with custom debug flags.
  170. *
  171. * @note This macro reserves memory for the given block allocator instance.
  172. *
  173. * @param[in] _name Name of the allocator.
  174. * @param[in] _element_size Size of one element.
  175. * @param[in] _pool_size Size of the pool.
  176. * @param[in] _debug_flags Debug flags (@ref NRF_BALLOC_DEBUG).
  177. */
  178. #define NRF_BALLOC_DBG_DEF(_name, _element_size, _pool_size, _debug_flags) \
  179. STATIC_ASSERT((_pool_size) <= UINT8_MAX); \
  180. static uint8_t CONCAT_2(_name, _nrf_balloc_pool_stack)[(_pool_size)]; \
  181. static uint32_t CONCAT_2(_name,_nrf_balloc_pool_mem) \
  182. [NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags) * (_pool_size) / sizeof(uint32_t)]; \
  183. static nrf_balloc_cb_t CONCAT_2(_name,_nrf_balloc_cb); \
  184. NRF_LOG_INSTANCE_REGISTER(NRF_BALLOC_LOG_NAME, _name, \
  185. NRF_BALLOC_CONFIG_INFO_COLOR, \
  186. NRF_BALLOC_CONFIG_DEBUG_COLOR, \
  187. NRF_BALLOC_CONFIG_INITIAL_LOG_LEVEL, \
  188. NRF_BALLOC_CONFIG_LOG_ENABLED ? \
  189. NRF_BALLOC_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
  190. NRF_SECTION_ITEM_REGISTER(nrf_balloc, const nrf_balloc_t _name) = \
  191. { \
  192. .p_cb = &CONCAT_2(_name,_nrf_balloc_cb), \
  193. .p_stack_base = CONCAT_2(_name,_nrf_balloc_pool_stack), \
  194. .p_stack_limit = CONCAT_2(_name,_nrf_balloc_pool_stack) + (_pool_size), \
  195. .p_memory_begin = CONCAT_2(_name,_nrf_balloc_pool_mem), \
  196. .block_size = NRF_BALLOC_BLOCK_SIZE(_element_size, _debug_flags), \
  197. \
  198. NRF_LOG_INSTANCE_PTR_INIT(p_log, NRF_BALLOC_LOG_NAME, _name) \
  199. __NRF_BALLOC_ASSIGN_POOL_NAME(_name) \
  200. __NRF_BALLOC_ASSIGN_DEBUG_FLAGS(_debug_flags) \
  201. }
  202. /**@brief Create a block allocator instance.
  203. *
  204. * @note This macro reserves memory for the given block allocator instance.
  205. *
  206. * @param[in] _name Name of the allocator.
  207. * @param[in] _element_size Size of one element.
  208. * @param[in] _pool_size Size of the pool.
  209. */
  210. #define NRF_BALLOC_DEF(_name, _element_size, _pool_size) \
  211. NRF_BALLOC_DBG_DEF(_name, _element_size, _pool_size, NRF_BALLOC_DEFAULT_DEBUG_FLAGS)
  212. /**@brief Create a block allocator interface.
  213. *
  214. * @param[in] _type Type which is allocated.
  215. * @param[in] _name Name of the allocator.
  216. */
  217. #define NRF_BALLOC_INTERFACE_DEC(_type, _name) \
  218. _type * CONCAT_2(_name,_alloc)(void); \
  219. void CONCAT_2(_name,_free)(_type * p_element)
  220. /**@brief Define a custom block allocator interface.
  221. *
  222. * @param[in] _attr Function attribute that will be added to allocator function definition.
  223. * @param[in] _type Type which is allocated.
  224. * @param[in] _name Name of the allocator.
  225. * @param[in] _p_pool Pool from which data will be allocated.
  226. */
  227. #define NRF_BALLOC_INTERFACE_CUSTOM_DEF(_attr, _type, _name, _p_pool) \
  228. _attr _type * CONCAT_2(_name,_alloc)(void) \
  229. { \
  230. GCC_PRAGMA("GCC diagnostic push") \
  231. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  232. ASSERT((_p_pool) != NULL); \
  233. ASSERT((_p_pool)->block_size >= \
  234. NRF_BALLOC_BLOCK_SIZE(sizeof(_type), (_p_pool)->debug_flags)); \
  235. GCC_PRAGMA("GCC diagnostic pop") \
  236. return (_type *)(nrf_balloc_alloc(_p_pool)); \
  237. } \
  238. \
  239. _attr void CONCAT_2(_name,_free)(_type * p_element) \
  240. { \
  241. GCC_PRAGMA("GCC diagnostic push") \
  242. GCC_PRAGMA("GCC diagnostic ignored \"-Waddress\"") \
  243. ASSERT((_p_pool) != NULL); \
  244. ASSERT((_p_pool)->block_size >= \
  245. NRF_BALLOC_BLOCK_SIZE(sizeof(_type), (_p_pool)->debug_flags)); \
  246. GCC_PRAGMA("GCC diagnostic pop") \
  247. nrf_balloc_free((_p_pool), p_element); \
  248. }
  249. /**@brief Define block allocator interface.
  250. *
  251. * @param[in] _type Type which is allocated.
  252. * @param[in] _name Name of the allocator.
  253. * @param[in] _p_pool Pool from which data will be allocated.
  254. */
  255. #define NRF_BALLOC_INTERFACE_DEF(_type, _name, _p_pool) \
  256. NRF_BALLOC_INTERFACE_CUSTOM_DEF(/* empty */, _type, _name, _p_pool)
  257. /**@brief Define a local block allocator interface.
  258. *
  259. * @param[in] _type Type which is allocated.
  260. * @param[in] _name Name of the allocator.
  261. * @param[in] _p_pool Pool from which data will be allocated.
  262. */
  263. #define NRF_BALLOC_INTERFACE_LOCAL_DEF(_type, _name, _p_pool) \
  264. NRF_BALLOC_INTERFACE_CUSTOM_DEF(static, _type, _name, _p_pool)
  265. /**@brief Function for initializing a block memory allocator pool.
  266. *
  267. * @param[out] p_pool Pointer to the pool that is to be initialized.
  268. *
  269. * @return NRF_SUCCESS on success, otherwise error code.
  270. */
  271. ret_code_t nrf_balloc_init(nrf_balloc_t const * p_pool);
  272. /**@brief Function for allocating an element from the pool.
  273. *
  274. * @note This module guarantees that the returned memory is aligned to 4.
  275. *
  276. * @param[in] p_pool Pointer to the memory pool from which the element will be allocated.
  277. *
  278. * @return Allocated element or NULL if the specified pool is empty.
  279. */
  280. void * nrf_balloc_alloc(nrf_balloc_t const * p_pool);
  281. /**@brief Function for freeing an element back to the pool.
  282. *
  283. * @param[in] p_pool Pointer to the memory pool.
  284. * @param[in] p_element Element to be freed.
  285. */
  286. void nrf_balloc_free(nrf_balloc_t const * p_pool, void * p_element);
  287. /**@brief Function for getting maximum memory pool utilization.
  288. *
  289. * @param[in] p_pool Pointer to the memory pool instance.
  290. *
  291. * @return Maximum number of elements allocated from the pool.
  292. */
  293. __STATIC_INLINE uint8_t nrf_balloc_max_utilization_get(nrf_balloc_t const * p_pool);
  294. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  295. __STATIC_INLINE uint8_t nrf_balloc_max_utilization_get(nrf_balloc_t const * p_pool)
  296. {
  297. ASSERT(p_pool != NULL);
  298. return p_pool->p_cb->max_utilization;
  299. }
  300. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  301. /**@brief Function for getting current memory pool utilization.
  302. *
  303. * @param[in] p_pool Pointer to the memory pool instance.
  304. *
  305. * @return Maximum number of elements allocated from the pool.
  306. */
  307. __STATIC_INLINE uint8_t nrf_balloc_utilization_get(nrf_balloc_t const * p_pool);
  308. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  309. __STATIC_INLINE uint8_t nrf_balloc_utilization_get(nrf_balloc_t const * p_pool)
  310. {
  311. ASSERT(p_pool != NULL);
  312. return (p_pool->p_stack_limit - p_pool->p_cb->p_stack_pointer);
  313. }
  314. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  315. #ifdef __cplusplus
  316. }
  317. #endif
  318. #endif // NRF_BALLOC_H__
  319. /** @} */