rt_Memory.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_MEMORY.C
  5. * Purpose: Interface functions for Dynamic Memory Management System
  6. * Rev.: V4.79
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2017 ARM Germany GmbH. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *---------------------------------------------------------------------------*/
  25. #include "rt_TypeDef.h"
  26. #include "rt_Memory.h"
  27. /* Functions */
  28. // Initialize Dynamic Memory pool
  29. // Parameters:
  30. // pool: Pointer to memory pool
  31. // size: Size of memory pool in bytes
  32. // Return: 0 - OK, 1 - Error
  33. U32 rt_init_mem (void *pool, U32 size) {
  34. MEMP *ptr;
  35. if ((pool == NULL) || (size < sizeof(MEMP))) { return (1U); }
  36. ptr = (MEMP *)pool;
  37. ptr->next = (MEMP *)((U32)pool + size - sizeof(MEMP *));
  38. ptr->next->next = NULL;
  39. ptr->len = 0U;
  40. return (0U);
  41. }
  42. // Allocate Memory from Memory pool
  43. // Parameters:
  44. // pool: Pointer to memory pool
  45. // size: Size of memory in bytes to allocate
  46. // Return: Pointer to allocated memory
  47. void *rt_alloc_mem (void *pool, U32 size) {
  48. MEMP *p, *p_search, *p_new;
  49. U32 hole_size;
  50. if ((pool == NULL) || (size == 0U)) { return NULL; }
  51. /* Add header offset to 'size' */
  52. size += sizeof(MEMP);
  53. /* Make sure that block is 4-byte aligned */
  54. size = (size + 3U) & ~(U32)3U;
  55. p_search = (MEMP *)pool;
  56. while (1) {
  57. hole_size = (U32)p_search->next - (U32)p_search;
  58. hole_size -= p_search->len;
  59. /* Check if hole size is big enough */
  60. if (hole_size >= size) { break; }
  61. p_search = p_search->next;
  62. if (p_search->next == NULL) {
  63. /* Failed, we are at the end of the list */
  64. return NULL;
  65. }
  66. }
  67. if (p_search->len == 0U) {
  68. /* No block is allocated, set the Length of the first element */
  69. p_search->len = size;
  70. p = (MEMP *)(((U32)p_search) + sizeof(MEMP));
  71. } else {
  72. /* Insert new list element into the memory list */
  73. p_new = (MEMP *)((U32)p_search + p_search->len);
  74. p_new->next = p_search->next;
  75. p_new->len = size;
  76. p_search->next = p_new;
  77. p = (MEMP *)(((U32)p_new) + sizeof(MEMP));
  78. }
  79. return (p);
  80. }
  81. // Free Memory and return it to Memory pool
  82. // Parameters:
  83. // pool: Pointer to memory pool
  84. // mem: Pointer to memory to free
  85. // Return: 0 - OK, 1 - Error
  86. U32 rt_free_mem (void *pool, void *mem) {
  87. MEMP *p_search, *p_prev, *p_return;
  88. if ((pool == NULL) || (mem == NULL)) { return (1U); }
  89. p_return = (MEMP *)((U32)mem - sizeof(MEMP));
  90. /* Set list header */
  91. p_prev = NULL;
  92. p_search = (MEMP *)pool;
  93. while (p_search != p_return) {
  94. p_prev = p_search;
  95. p_search = p_search->next;
  96. if (p_search == NULL) {
  97. /* Valid Memory block not found */
  98. return (1U);
  99. }
  100. }
  101. if (p_prev == NULL) {
  102. /* First block to be released, only set length to 0 */
  103. p_search->len = 0U;
  104. } else {
  105. /* Discard block from chain list */
  106. p_prev->next = p_search->next;
  107. }
  108. return (0U);
  109. }