nrf_memobj.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * Copyright (c) 2017 - 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. #ifndef NRF_MEMOBJ_H
  41. #define NRF_MEMOBJ_H
  42. /**
  43. * @defgroup nrf_memobj Memory Object module
  44. * @{
  45. * @ingroup app_common
  46. * @brief Functions for controlling a memory object.
  47. */
  48. #include <stdint.h>
  49. #include <stdlib.h>
  50. #include "sdk_errors.h"
  51. #include "nrf_balloc.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**
  56. * A memory object can consist of multiple chunks with the same size. Each object has a header part
  57. * and a data part. The first element in a memory object is memory object head which has a special header.
  58. * The remaining objects have a header of the same size.
  59. *
  60. @verbatim
  61. _____________________ _____________________ _____________________
  62. | | | | | |
  63. |4 B head header | --> |4 B p_next |------->|4 B p_memobj_pool |
  64. |_____________________| | |_____________________| |_____________________|
  65. | | | | | | |
  66. |4 B p_next |--| | | | |
  67. |_____________________| | | .... | |
  68. | | | data | | data |
  69. | | | | | |
  70. | data | | | | |
  71. | | | | | |
  72. |_____________________| |_____________________| |_____________________|
  73. head mid_element last_element
  74. @endverbatim
  75. *
  76. */
  77. #define NRF_MEMOBJ_STD_HEADER_SIZE sizeof(uint32_t)
  78. /**
  79. * @brief Macro for creating an nrf_memobj pool.
  80. *
  81. * This macro declares an nrf_balloc object. The element in the pool contains a user-defined data part and
  82. * a memory object header.
  83. *
  84. * @param _name Name of the instance.
  85. * @param _chunk_size Size of a single chunk.
  86. * @param _pool_size Number of chunks in the pool.
  87. */
  88. #define NRF_MEMOBJ_POOL_DEF(_name, _chunk_size, _pool_size) \
  89. STATIC_ASSERT((_chunk_size) > sizeof(uint32_t)); \
  90. NRF_BALLOC_DEF(_name, ((_chunk_size)+NRF_MEMOBJ_STD_HEADER_SIZE), (_pool_size))
  91. /**
  92. * @brief Pool of memory objects.
  93. */
  94. typedef nrf_balloc_t nrf_memobj_pool_t;
  95. /**
  96. * @brief Memory object handle.
  97. */
  98. typedef void * nrf_memobj_t;
  99. /**
  100. * @brief Function for initializing the memobj pool instance.
  101. *
  102. * This function initializes the pool.
  103. *
  104. * @param[in] p_pool Pointer to the memobj pool instance structure.
  105. *
  106. * @return NRF_SUCCESS on success, otherwise an error code.
  107. */
  108. ret_code_t nrf_memobj_pool_init(nrf_memobj_pool_t const * p_pool);
  109. /**
  110. * @brief Function for allocating a memobj with a requested size.
  111. *
  112. * Fixed length elements in the pool are linked together to provide the amount of memory requested by
  113. * the user. If a memory object is successfully allocated, then the users can use the memory.
  114. * However, it is fragmented into multiple objects so it must be accessed through the API:
  115. * @ref nrf_memobj_write and @ref nrf_memobj_read.
  116. *
  117. * @param[in] p_pool Pointer to the memobj pool instance structure.
  118. * @param[in] size Data size of requested object.
  119. *
  120. * @return Pointer to a memory object or NULL if the requested size cannot be allocated.
  121. */
  122. nrf_memobj_t * nrf_memobj_alloc(nrf_memobj_pool_t const * p_pool,
  123. size_t size);
  124. /**
  125. * @brief Function for indicating that a memory object is used and cannot be freed.
  126. *
  127. * Memory object can be shared and reused between multiple modules and this mechanism ensures that
  128. * object is freed when no longer used by any module. Memory object has a counter which is incremented
  129. * whenever this function is called. @ref nrf_memobj_put function decrements the counter.
  130. *
  131. * @param[in] p_obj Pointer to memory object.
  132. */
  133. void nrf_memobj_get(nrf_memobj_t const * p_obj);
  134. /**
  135. * @brief Function for indicated that memory object is no longer used by the module and can be freed
  136. * if no other module is using it.
  137. *
  138. * Memory object is returned to the pool if internal counter reaches 0 after decrementing. It means
  139. * that no other module is needing it anymore.
  140. *
  141. * @note Memory object holds pointer to the pool which was used to allocate it so it does not have
  142. * to be provided explicitly to this function.
  143. *
  144. * @param[in] p_obj Pointer to memory object.
  145. */
  146. void nrf_memobj_put(nrf_memobj_t * p_obj);
  147. /**
  148. * @brief Function for forcing freeing of the memory object.
  149. *
  150. * @note This function should be use with caution because it can lead to undefined behavior of the
  151. * modules since modules using the memory object are not aware that it has been freed.
  152. *
  153. * @param[in] p_obj Pointer to memory object.
  154. */
  155. void nrf_memobj_free(nrf_memobj_t * p_obj);
  156. /**
  157. * @brief Function for writing data to the memory object.
  158. *
  159. * @param[in] p_obj Pointer to memory object.
  160. * @param[in] p_data Pointer to data to be written to the memory object.
  161. * @param[in] len Amount of data to be written to the memory object.
  162. * @param[in] offset Offset.
  163. */
  164. void nrf_memobj_write(nrf_memobj_t * p_obj,
  165. void * p_data,
  166. size_t len,
  167. size_t offset);
  168. /**
  169. * @brief Function for reading data from the memory object.
  170. *
  171. * @param[in] p_obj Pointer to memory object.
  172. * @param[in] p_data Pointer to the destination buffer.
  173. * @param[in] len Amount of data to be read from the memory object.
  174. * @param[in] offset Offset.
  175. */
  176. void nrf_memobj_read(nrf_memobj_t * p_obj,
  177. void * p_data,
  178. size_t len,
  179. size_t offset);
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif //NRF_MEMOBJ_H
  184. /** @} */