rtx_memory.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2013-2018 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * -----------------------------------------------------------------------------
  19. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: Memory functions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "rtx_lib.h"
  26. // Memory Pool Header structure
  27. typedef struct {
  28. uint32_t size; // Memory Pool size
  29. uint32_t used; // Used Memory
  30. } mem_head_t;
  31. // Memory Block Header structure
  32. typedef struct mem_block_s {
  33. struct mem_block_s *next; // Next Memory Block in list
  34. uint32_t info; // Block Info or max used Memory (in last block)
  35. } mem_block_t;
  36. // Memory Block Info: Length = <31:2>:'00', Type = <1:0>
  37. #define MB_INFO_LEN_MASK 0xFFFFFFFCU // Length mask
  38. #define MB_INFO_TYPE_MASK 0x00000003U // Type mask
  39. // Memory Head Pointer
  40. __STATIC_INLINE mem_head_t *MemHeadPtr (void *mem) {
  41. //lint -e{9079} -e{9087} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
  42. return ((mem_head_t *)mem);
  43. }
  44. // Memory Block Pointer
  45. __STATIC_INLINE mem_block_t *MemBlockPtr (void *mem, uint32_t offset) {
  46. uint32_t addr;
  47. mem_block_t *ptr;
  48. //lint --e{923} --e{9078} "cast between pointer and unsigned int" [MISRA Note 8]
  49. addr = (uint32_t)mem + offset;
  50. ptr = (mem_block_t *)addr;
  51. return ptr;
  52. }
  53. // ==== Library functions ====
  54. /// Initialize Memory Pool with variable block size.
  55. /// \param[in] mem pointer to memory pool.
  56. /// \param[in] size size of a memory pool in bytes.
  57. /// \return 1 - success, 0 - failure.
  58. __WEAK uint32_t osRtxMemoryInit (void *mem, uint32_t size) {
  59. mem_head_t *head;
  60. mem_block_t *ptr;
  61. // Check parameters
  62. //lint -e{923} "cast from pointer to unsigned int" [MISRA Note 7]
  63. if ((mem == NULL) || (((uint32_t)mem & 7U) != 0U) || ((size & 7U) != 0U) ||
  64. (size < (sizeof(mem_head_t) + (2U*sizeof(mem_block_t))))) {
  65. EvrRtxMemoryInit(mem, size, 0U);
  66. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  67. return 0U;
  68. }
  69. // Initialize memory pool header
  70. head = MemHeadPtr(mem);
  71. head->size = size;
  72. head->used = sizeof(mem_head_t) + sizeof(mem_block_t);
  73. // Initialize first and last block header
  74. ptr = MemBlockPtr(mem, sizeof(mem_head_t));
  75. ptr->next = MemBlockPtr(mem, size - sizeof(mem_block_t));
  76. ptr->next->next = NULL;
  77. ptr->next->info = sizeof(mem_head_t) + sizeof(mem_block_t);
  78. ptr->info = 0U;
  79. EvrRtxMemoryInit(mem, size, 1U);
  80. return 1U;
  81. }
  82. /// Allocate a memory block from a Memory Pool.
  83. /// \param[in] mem pointer to memory pool.
  84. /// \param[in] size size of a memory block in bytes.
  85. /// \param[in] type memory block type: 0 - generic, 1 - control block
  86. /// \return allocated memory block or NULL in case of no memory is available.
  87. __WEAK void *osRtxMemoryAlloc (void *mem, uint32_t size, uint32_t type) {
  88. mem_block_t *ptr;
  89. mem_block_t *p, *p_new;
  90. uint32_t block_size;
  91. uint32_t hole_size;
  92. // Check parameters
  93. if ((mem == NULL) || (size == 0U) || ((type & ~MB_INFO_TYPE_MASK) != 0U)) {
  94. EvrRtxMemoryAlloc(mem, size, type, NULL);
  95. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  96. return NULL;
  97. }
  98. // Add block header to size
  99. block_size = size + sizeof(mem_block_t);
  100. // Make sure that block is 8-byte aligned
  101. block_size = (block_size + 7U) & ~((uint32_t)7U);
  102. // Search for hole big enough
  103. p = MemBlockPtr(mem, sizeof(mem_head_t));
  104. for (;;) {
  105. //lint -e{923} -e{9078} "cast from pointer to unsigned int"
  106. hole_size = (uint32_t)p->next - (uint32_t)p;
  107. hole_size -= p->info & MB_INFO_LEN_MASK;
  108. if (hole_size >= block_size) {
  109. // Hole found
  110. break;
  111. }
  112. p = p->next;
  113. if (p->next == NULL) {
  114. // Failed (end of list)
  115. EvrRtxMemoryAlloc(mem, size, type, NULL);
  116. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  117. return NULL;
  118. }
  119. }
  120. // Update used memory
  121. (MemHeadPtr(mem))->used += block_size;
  122. // Update max used memory
  123. p_new = MemBlockPtr(mem, (MemHeadPtr(mem))->size - sizeof(mem_block_t));
  124. if (p_new->info < (MemHeadPtr(mem))->used) {
  125. p_new->info = (MemHeadPtr(mem))->used;
  126. }
  127. // Allocate block
  128. if (p->info == 0U) {
  129. // No block allocated, set info of first element
  130. p->info = block_size | type;
  131. ptr = MemBlockPtr(p, sizeof(mem_block_t));
  132. } else {
  133. // Insert new element into the list
  134. p_new = MemBlockPtr(p, p->info & MB_INFO_LEN_MASK);
  135. p_new->next = p->next;
  136. p_new->info = block_size | type;
  137. p->next = p_new;
  138. ptr = MemBlockPtr(p_new, sizeof(mem_block_t));
  139. }
  140. EvrRtxMemoryAlloc(mem, size, type, ptr);
  141. return ptr;
  142. }
  143. /// Return an allocated memory block back to a Memory Pool.
  144. /// \param[in] mem pointer to memory pool.
  145. /// \param[in] block memory block to be returned to the memory pool.
  146. /// \return 1 - success, 0 - failure.
  147. __WEAK uint32_t osRtxMemoryFree (void *mem, void *block) {
  148. const mem_block_t *ptr;
  149. mem_block_t *p, *p_prev;
  150. // Check parameters
  151. if ((mem == NULL) || (block == NULL)) {
  152. EvrRtxMemoryFree(mem, block, 0U);
  153. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  154. return 0U;
  155. }
  156. // Memory block header
  157. ptr = MemBlockPtr(block, 0U);
  158. ptr--;
  159. // Search for block header
  160. p_prev = NULL;
  161. p = MemBlockPtr(mem, sizeof(mem_head_t));
  162. while (p != ptr) {
  163. p_prev = p;
  164. p = p->next;
  165. if (p == NULL) {
  166. // Not found
  167. EvrRtxMemoryFree(mem, block, 0U);
  168. //lint -e{904} "Return statement before end of function" [MISRA Note 1]
  169. return 0U;
  170. }
  171. }
  172. // Update used memory
  173. (MemHeadPtr(mem))->used -= p->info & MB_INFO_LEN_MASK;
  174. // Free block
  175. if (p_prev == NULL) {
  176. // Release first block, only set info to 0
  177. p->info = 0U;
  178. } else {
  179. // Discard block from chained list
  180. p_prev->next = p->next;
  181. }
  182. EvrRtxMemoryFree(mem, block, 1U);
  183. return 1U;
  184. }