rtx_memory.c 4.3 KB

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