nrf_memobj.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Copyright (c) 2017 - 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. #include "nrf_memobj.h"
  41. #include "nrf_atomic.h"
  42. #include "nrf_assert.h"
  43. typedef struct memobj_elem_s memobj_elem_t;
  44. /** @brief Standard chunk header. */
  45. typedef struct
  46. {
  47. memobj_elem_t * p_next; ///< Pointer to the next element.
  48. } memobj_header_t;
  49. /** @brief Head header extension fields. */
  50. typedef struct
  51. {
  52. uint8_t user_cnt; ///< User counter (see @ref nrf_memobj_get and @ref nrf_memobj_put).
  53. uint8_t chunk_cnt; ///< Number of chunks in the object.
  54. uint16_t chunk_size; ///< Single chunk size
  55. } memobj_head_header_fields_t;
  56. /** @brief Head header extension. */
  57. typedef struct
  58. {
  59. union
  60. {
  61. nrf_atomic_u32_t atomic_user_cnt;
  62. memobj_head_header_fields_t fields;
  63. } data;
  64. } memobj_head_header_t;
  65. /** @brief Head chunk structure. */
  66. typedef struct
  67. {
  68. memobj_header_t header; ///< Standard header.
  69. memobj_head_header_t head_header; ///< Head-specific header part.
  70. uint8_t data[1]; ///< Data.
  71. } memobj_head_t;
  72. STATIC_ASSERT(sizeof(memobj_header_t) == NRF_MEMOBJ_STD_HEADER_SIZE);
  73. /** @brief Standard chunk structure. */
  74. struct memobj_elem_s
  75. {
  76. memobj_header_t header; ///< Standard header.
  77. uint8_t data[1]; ///< Data.
  78. };
  79. ret_code_t nrf_memobj_pool_init(nrf_memobj_pool_t const * p_pool)
  80. {
  81. return nrf_balloc_init((nrf_balloc_t const *)p_pool);
  82. }
  83. nrf_memobj_t * nrf_memobj_alloc(nrf_memobj_pool_t const * p_pool,
  84. size_t size)
  85. {
  86. uint32_t bsize = (uint32_t)NRF_BALLOC_ELEMENT_SIZE((nrf_balloc_t const *)p_pool) - sizeof(memobj_header_t);
  87. uint8_t num_of_chunks = (uint8_t)CEIL_DIV(size + sizeof(memobj_head_header_t), bsize);
  88. memobj_head_t * p_head = nrf_balloc_alloc((nrf_balloc_t const *)p_pool);
  89. if (p_head == NULL)
  90. {
  91. return NULL;
  92. }
  93. p_head->head_header.data.fields.user_cnt = 0;
  94. p_head->head_header.data.fields.chunk_cnt = 1;
  95. p_head->head_header.data.fields.chunk_size = bsize;
  96. memobj_header_t * p_prev = (memobj_header_t *)p_head;
  97. memobj_header_t * p_curr;
  98. uint32_t i;
  99. uint32_t chunk_less1 = (uint32_t)num_of_chunks - 1;
  100. p_prev->p_next = (memobj_elem_t *)p_pool;
  101. for (i = 0; i < chunk_less1; i++)
  102. {
  103. p_curr = (memobj_header_t *)nrf_balloc_alloc((nrf_balloc_t const *)p_pool);
  104. if (p_curr)
  105. {
  106. (p_head->head_header.data.fields.chunk_cnt)++;
  107. p_prev->p_next = (memobj_elem_t *)p_curr;
  108. p_curr->p_next = (memobj_elem_t *)p_pool;
  109. p_prev = p_curr;
  110. }
  111. else
  112. {
  113. //Could not allocate all requested buffers
  114. nrf_memobj_free((nrf_memobj_t *)p_head);
  115. return NULL;
  116. }
  117. }
  118. return (nrf_memobj_t *)p_head;
  119. }
  120. void nrf_memobj_free(nrf_memobj_t * p_obj)
  121. {
  122. memobj_head_t * p_head = (memobj_head_t *)p_obj;
  123. uint8_t chunk_cnt = p_head->head_header.data.fields.chunk_cnt;
  124. uint32_t i;
  125. memobj_header_t * p_curr = (memobj_header_t *)p_obj;
  126. memobj_header_t * p_next;
  127. uint32_t chunk_less1 = (uint32_t)chunk_cnt - 1;
  128. for (i = 0; i < chunk_less1; i++)
  129. {
  130. p_curr = (memobj_header_t *)p_curr->p_next;
  131. }
  132. nrf_balloc_t const * p_pool2 = (nrf_balloc_t const *)p_curr->p_next;
  133. p_curr = (memobj_header_t *)p_obj;
  134. for (i = 0; i < chunk_cnt; i++)
  135. {
  136. p_next = (memobj_header_t *)p_curr->p_next;
  137. nrf_balloc_free(p_pool2, p_curr);
  138. p_curr = p_next;
  139. }
  140. }
  141. void nrf_memobj_get(nrf_memobj_t const * p_obj)
  142. {
  143. memobj_head_t * p_head = (memobj_head_t *)p_obj;
  144. (void)nrf_atomic_u32_add(&p_head->head_header.data.atomic_user_cnt, 1);
  145. }
  146. void nrf_memobj_put(nrf_memobj_t * p_obj)
  147. {
  148. memobj_head_t * p_head = (memobj_head_t *)p_obj;
  149. uint32_t user_cnt = nrf_atomic_u32_sub(&p_head->head_header.data.atomic_user_cnt, 1);
  150. memobj_head_header_fields_t * p_fields = (memobj_head_header_fields_t *)&user_cnt;
  151. if (p_fields->user_cnt == 0)
  152. {
  153. nrf_memobj_free(p_obj);
  154. }
  155. }
  156. static void memobj_op(nrf_memobj_t * p_obj,
  157. void * p_data,
  158. size_t * p_len,
  159. size_t offset,
  160. bool read)
  161. {
  162. ASSERT(p_obj);
  163. memobj_head_t * p_head = (memobj_head_t *)p_obj;
  164. memobj_elem_t * p_curr_chunk = (memobj_elem_t *)p_obj;
  165. size_t obj_capacity;
  166. size_t chunk_size;
  167. size_t chunk_idx;
  168. size_t chunk_offset;
  169. size_t len;
  170. obj_capacity = (p_head->head_header.data.fields.chunk_size *
  171. p_head->head_header.data.fields.chunk_cnt) -
  172. sizeof(memobj_head_header_fields_t);
  173. ASSERT(offset < obj_capacity);
  174. chunk_size = p_head->head_header.data.fields.chunk_size;
  175. chunk_idx = (offset + sizeof(memobj_head_header_fields_t)) / chunk_size;
  176. chunk_offset = (offset + sizeof(memobj_head_header_fields_t)) % chunk_size;
  177. len = ((*p_len + offset) > obj_capacity) ? obj_capacity - offset : *p_len;
  178. //Return number of available bytes
  179. *p_len = len;
  180. //Move to the first chunk to be used
  181. while (chunk_idx > 0)
  182. {
  183. p_curr_chunk = p_curr_chunk->header.p_next;
  184. chunk_idx--;
  185. }
  186. size_t user_mem_offset = 0;
  187. size_t curr_cpy_size = chunk_size - chunk_offset;
  188. curr_cpy_size = curr_cpy_size > len ? len : curr_cpy_size;
  189. while (len)
  190. {
  191. void * p_user_mem = &((uint8_t *)p_data)[user_mem_offset];
  192. void * p_obj_mem = &p_curr_chunk->data[chunk_offset];
  193. if (read)
  194. {
  195. memcpy(p_user_mem, p_obj_mem, curr_cpy_size);
  196. }
  197. else
  198. {
  199. memcpy(p_obj_mem, p_user_mem, curr_cpy_size);
  200. }
  201. chunk_offset = 0;
  202. p_curr_chunk = p_curr_chunk->header.p_next;
  203. len -= curr_cpy_size;
  204. user_mem_offset += curr_cpy_size;
  205. curr_cpy_size = (chunk_size > len) ? len : chunk_size;
  206. }
  207. }
  208. void nrf_memobj_write(nrf_memobj_t * p_obj,
  209. void * p_data,
  210. size_t len,
  211. size_t offset)
  212. {
  213. size_t op_len = len;
  214. memobj_op(p_obj, p_data, &op_len, offset, false);
  215. ASSERT(op_len == len);
  216. }
  217. void nrf_memobj_read(nrf_memobj_t * p_obj,
  218. void * p_data,
  219. size_t len,
  220. size_t offset)
  221. {
  222. size_t op_len = len;
  223. memobj_op(p_obj, p_data, &op_len, offset, true);
  224. ASSERT(op_len == len);
  225. }