rtx_memory.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2013-2017 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 mem_head_s {
  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; // Info: length = <31:2>:'00', type = <1:0>
  35. } mem_block_t;
  36. #define MB_INFO_LEN_MASK 0xFFFFFFFCU
  37. #define MB_INFO_TYPE_MASK 0x00000003U
  38. // ==== Library functions ====
  39. /// Initialize Memory Pool with variable block size.
  40. /// \param[in] mem pointer to memory pool.
  41. /// \param[in] size size of a memory pool in bytes.
  42. /// \return 1 - success, 0 - failure.
  43. __WEAK uint32_t osRtxMemoryInit (void *mem, uint32_t size) {
  44. mem_head_t *head;
  45. mem_block_t *ptr;
  46. if ((mem == NULL) || ((uint32_t)mem & 7U) || (size & 7U) ||
  47. (size < (sizeof(mem_head_t) + 2*sizeof(mem_block_t)))) {
  48. EvrRtxMemoryInit(mem, size, 0U);
  49. return 0U;
  50. }
  51. head = (mem_head_t *)mem;
  52. head->size = size;
  53. head->used = sizeof(mem_head_t) + sizeof(mem_block_t);
  54. ptr = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
  55. ptr->next = (mem_block_t *)((uint32_t)mem + size - sizeof(mem_block_t));
  56. ptr->next->next = NULL;
  57. ptr->info = 0U;
  58. EvrRtxMemoryInit(mem, size, 1U);
  59. return 1U;
  60. }
  61. /// Allocate a memory block from a Memory Pool.
  62. /// \param[in] mem pointer to memory pool.
  63. /// \param[in] size size of a memory block in bytes.
  64. /// \param[in] type memory block type: 0 - generic, 1 - control block
  65. /// \return allocated memory block or NULL in case of no memory is available.
  66. __WEAK void *osRtxMemoryAlloc (void *mem, uint32_t size, uint32_t type) {
  67. mem_block_t *p, *p_new, *ptr;
  68. uint32_t hole_size;
  69. if ((mem == NULL) || (size == 0U) || (type & ~MB_INFO_TYPE_MASK)) {
  70. EvrRtxMemoryAlloc(mem, size, type, NULL);
  71. return NULL;
  72. }
  73. // Add header to size
  74. size += sizeof(mem_block_t);
  75. // Make sure that block is 8-byte aligned
  76. size = (size + 7U) & ~((uint32_t)7U);
  77. // Search for hole big enough
  78. p = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
  79. for (;;) {
  80. hole_size = (uint32_t)p->next - (uint32_t)p;
  81. hole_size -= p->info & MB_INFO_LEN_MASK;
  82. if (hole_size >= size) {
  83. // Hole found
  84. break;
  85. }
  86. p = p->next;
  87. if (p->next == NULL) {
  88. // Failed (end of list)
  89. EvrRtxMemoryAlloc(mem, size, type, NULL);
  90. return NULL;
  91. }
  92. }
  93. ((mem_head_t *)mem)->used += size;
  94. if (p->info == 0U) {
  95. // No block allocated, set info of first element
  96. p->info = size | type;
  97. ptr = (mem_block_t *)((uint32_t)p + sizeof(mem_block_t));
  98. } else {
  99. // Insert new element into the list
  100. p_new = (mem_block_t *)((uint32_t)p + (p->info & MB_INFO_LEN_MASK));
  101. p_new->next = p->next;
  102. p_new->info = size | type;
  103. p->next = p_new;
  104. ptr = (mem_block_t *)((uint32_t)p_new + sizeof(mem_block_t));
  105. }
  106. EvrRtxMemoryAlloc(mem, size, type, ptr);
  107. return ptr;
  108. }
  109. /// Return an allocated memory block back to a Memory Pool.
  110. /// \param[in] mem pointer to memory pool.
  111. /// \param[in] block memory block to be returned to the memory pool.
  112. /// \return 1 - success, 0 - failure.
  113. __WEAK uint32_t osRtxMemoryFree (void *mem, void *block) {
  114. mem_block_t *p, *p_prev, *ptr;
  115. if ((mem == NULL) || (block == NULL)) {
  116. EvrRtxMemoryFree(mem, block, 0U);
  117. return 0U;
  118. }
  119. ptr = (mem_block_t *)((uint32_t)block - sizeof(mem_block_t));
  120. // Search for header
  121. p_prev = NULL;
  122. p = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
  123. while (p != ptr) {
  124. p_prev = p;
  125. p = p->next;
  126. if (p == NULL) {
  127. // Not found
  128. EvrRtxMemoryFree(mem, block, 0U);
  129. return 0U;
  130. }
  131. }
  132. ((mem_head_t *)mem)->used -= p->info & MB_INFO_LEN_MASK;
  133. if (p_prev == NULL) {
  134. // Release first block, only set info to 0
  135. p->info = 0U;
  136. } else {
  137. // Discard block from chained list
  138. p_prev->next = p->next;
  139. }
  140. EvrRtxMemoryFree(mem, block, 1U);
  141. return 1U;
  142. }