nrf_section_iter.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2016 - 2020, 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. #ifndef NRF_SECTION_ITER_H__
  41. #define NRF_SECTION_ITER_H__
  42. #include <stddef.h>
  43. #include "nrf_section.h"
  44. #include "nrf_assert.h"
  45. #include "app_util.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /**
  50. * @defgroup nrf_section_iter Section variables iterator
  51. * @ingroup app_common
  52. * @{
  53. */
  54. /**@brief Single section description structure. */
  55. typedef struct
  56. {
  57. void * p_start; //!< Pointer to the start of section.
  58. void * p_end; //!< Pointer to the end of section.
  59. } nrf_section_t;
  60. /**@brief Set of the sections description structure. */
  61. typedef struct
  62. {
  63. #if defined(__GNUC__)
  64. nrf_section_t section; //!< Description of the set of sections.
  65. /**<
  66. * In case of GCC all sections in the set are sorted and
  67. * placed in contiguous area, because they are treated as
  68. * one section.
  69. */
  70. #else
  71. nrf_section_t const * p_first; //!< Pointer to the first section in the set.
  72. nrf_section_t const * p_last; //!< Pointer to the last section in the set.
  73. #endif
  74. size_t item_size; //!< Size of the single item in the section.
  75. } nrf_section_set_t;
  76. /**@brief Section iterator structure. */
  77. typedef struct
  78. {
  79. nrf_section_set_t const * p_set; //!< Pointer to the appropriate section set.
  80. #if !defined(__GNUC__)
  81. nrf_section_t const * p_section; //!< Pointer to the selected section.
  82. /**<
  83. * In case of GCC all sections in the set are sorted and
  84. * placed in contiguous area, because they are treated
  85. * as one section.
  86. */
  87. #endif
  88. void * p_item; //!< Pointer to the selected item in the section.
  89. } nrf_section_iter_t;
  90. /**@brief Create a set of sections.
  91. *
  92. * @note This macro reserves memory for the given set of sections.
  93. *
  94. * @details A set of sections, is an ordered collections of sections.
  95. *
  96. * @param[in] _name Name of the set.
  97. * @param[in] _type Type of the elements stored in the sections.
  98. * @param[in] _count Number of the sections in the set. This parameter is ignored in case of GCC.
  99. * @hideinitializer
  100. */
  101. #if defined(__GNUC__)
  102. #define NRF_SECTION_SET_DEF(_name, _type, _count) \
  103. \
  104. NRF_SECTION_DEF(_name, _type); \
  105. static nrf_section_set_t const _name = \
  106. { \
  107. .section = \
  108. { \
  109. .p_start = NRF_SECTION_START_ADDR(_name), \
  110. .p_end = NRF_SECTION_END_ADDR(_name), \
  111. }, \
  112. .item_size = sizeof(_type), \
  113. }
  114. #else
  115. #define NRF_SECTION_SET_DEF(_name, _type, _count) \
  116. /*lint -save -emacro(14, MACRO_REPEAT_FOR*) */ \
  117. MACRO_REPEAT_FOR(_count, NRF_SECTION_DEF_, _name, _type) \
  118. static nrf_section_t const CONCAT_2(_name, _array)[] = \
  119. { \
  120. MACRO_REPEAT_FOR(_count, NRF_SECTION_SET_DEF_, _name) \
  121. }; \
  122. /*lint -restore */ \
  123. static nrf_section_set_t const _name = \
  124. { \
  125. .p_first = CONCAT_2(_name, _array), \
  126. .p_last = CONCAT_2(_name, _array) + ARRAY_SIZE(CONCAT_2(_name, _array)), \
  127. .item_size = sizeof(_type), \
  128. }
  129. #ifndef DOXYGEN
  130. #define NRF_SECTION_DEF_(_priority, _name, _type) \
  131. NRF_SECTION_DEF(CONCAT_2(_name, _priority), _type);
  132. #define NRF_SECTION_SET_DEF_(_priority, _name) \
  133. { \
  134. .p_start = NRF_SECTION_START_ADDR(CONCAT_2(_name, _priority)), \
  135. .p_end = NRF_SECTION_END_ADDR(CONCAT_2(_name, _priority)), \
  136. },
  137. #endif // DOXYGEN
  138. #endif // __GNUC__
  139. /**@brief Macro to declare a variable and register it in the section set.
  140. *
  141. * @note The order of the section in the set is based on the priority. The order with which
  142. * variables are placed in a section is dependant on the order with which the linker
  143. * encouters the variables during linking.
  144. *
  145. * @param[in] _name Name of the section set.
  146. * @param[in] _priority Priority of the desired section.
  147. * @param[in] _var The variable to register in the given section.
  148. * @hideinitializer
  149. */
  150. #define NRF_SECTION_SET_ITEM_REGISTER(_name, _priority, _var) \
  151. NRF_SECTION_ITEM_REGISTER(CONCAT_2(_name, _priority), _var)
  152. /**@brief Function for initializing the section set iterator.
  153. *
  154. * @param[in] p_iter Pointer to the iterator.
  155. * @param[in] p_set Pointer to the sections set.
  156. */
  157. void nrf_section_iter_init(nrf_section_iter_t * p_iter, nrf_section_set_t const * p_set);
  158. /**@brief Function for incrementing iterator.
  159. *
  160. * @param[in] p_iter Pointer to the iterator.
  161. */
  162. void nrf_section_iter_next(nrf_section_iter_t * p_iter);
  163. /**@brief Function for getting the element pointed to by the iterator.
  164. *
  165. * @param[in] p_iter Pointer to the iterator.
  166. *
  167. * @retval Pointer to the element or NULL if iterator points end of the set.
  168. */
  169. static inline void * nrf_section_iter_get(nrf_section_iter_t const * p_iter)
  170. {
  171. ASSERT(p_iter);
  172. return p_iter->p_item;
  173. }
  174. /** @} */
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif // NRF_SECTION_ITER_H__