rtx_memory.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2013-2016 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. * http://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. uint32_t os_MemoryInit (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. return 0U;
  49. }
  50. head = (mem_head_t *)mem;
  51. head->size = size;
  52. head->used = sizeof(mem_head_t) + sizeof(mem_block_t);
  53. ptr = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
  54. ptr->next = (mem_block_t *)((uint32_t)mem + size - sizeof(mem_block_t));
  55. ptr->next->next = NULL;
  56. ptr->info = 0U;
  57. return 1U;
  58. }
  59. /// Allocate a memory block from a Memory Pool.
  60. /// \param[in] mem pointer to memory pool.
  61. /// \param[in] size size of a memory block in bytes.
  62. /// \param[in] type memory block type: 0 - generic, 1 - control block
  63. /// \return allocated memory block or NULL in case of no memory is available.
  64. void *os_MemoryAlloc (void *mem, uint32_t size, uint32_t type) {
  65. mem_block_t *p, *p_new, *ptr;
  66. uint32_t hole_size;
  67. if ((mem == NULL) || (size == 0U) || (type & ~MB_INFO_TYPE_MASK)) {
  68. return NULL;
  69. }
  70. // Add header to size
  71. size += sizeof(mem_block_t);
  72. // Make sure that block is 8-byte aligned
  73. size = (size + 7U) & ~((uint32_t)7U);
  74. // Search for hole big enough
  75. p = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
  76. for (;;) {
  77. hole_size = (uint32_t)p->next - (uint32_t)p;
  78. hole_size -= p->info & MB_INFO_LEN_MASK;
  79. if (hole_size >= size) {
  80. // Hole found
  81. break;
  82. }
  83. p = p->next;
  84. if (p->next == NULL) {
  85. // Failed (end of list)
  86. return NULL;
  87. }
  88. }
  89. ((mem_head_t *)mem)->used += size;
  90. if (p->info == 0U) {
  91. // No block allocated, set info of first element
  92. p->info = size | type;
  93. ptr = (mem_block_t *)((uint32_t)p + sizeof(mem_block_t));
  94. } else {
  95. // Insert new element into the list
  96. p_new = (mem_block_t *)((uint32_t)p + (p->info & MB_INFO_LEN_MASK));
  97. p_new->next = p->next;
  98. p_new->info = size | type;
  99. p->next = p_new;
  100. ptr = (mem_block_t *)((uint32_t)p_new + sizeof(mem_block_t));
  101. }
  102. return ptr;
  103. }
  104. /// Return an allocated memory block back to a Memory Pool.
  105. /// \param[in] mem pointer to memory pool.
  106. /// \param[in] block memory block to be returned to the memory pool.
  107. /// \return 1 - success, 0 - failure.
  108. uint32_t os_MemoryFree (void *mem, void *block) {
  109. mem_block_t *p, *p_prev, *ptr;
  110. if ((mem == NULL) || (block == NULL)) {
  111. return 0U;
  112. }
  113. ptr = (mem_block_t *)((uint32_t)block - sizeof(mem_block_t));
  114. // Search for header
  115. p_prev = NULL;
  116. p = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
  117. while (p != ptr) {
  118. p_prev = p;
  119. p = p->next;
  120. if (p == NULL) {
  121. // Not found
  122. return 0U;
  123. }
  124. }
  125. ((mem_head_t *)mem)->used -= p->info & MB_INFO_LEN_MASK;
  126. if (p_prev == NULL) {
  127. // Release first block, only set info to 0
  128. p->info = 0U;
  129. } else {
  130. // Discard block from chained list
  131. p_prev->next = p->next;
  132. }
  133. return 1U;
  134. }