rt_MemBox.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_MEMBOX.C
  5. * Purpose: Interface functions for fixed memory block 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 "RTX_Config.h"
  27. #include "rt_System.h"
  28. #include "rt_MemBox.h"
  29. #include "rt_HAL_CM.h"
  30. /*----------------------------------------------------------------------------
  31. * Global Functions
  32. *---------------------------------------------------------------------------*/
  33. /*--------------------------- _init_box -------------------------------------*/
  34. U32 _init_box (void *box_mem, U32 box_size, U32 blk_size) {
  35. /* Initialize memory block system, returns 0 if OK, 1 if fails. */
  36. void *end;
  37. void *blk;
  38. void *next;
  39. U32 sizeof_bm;
  40. /* Create memory structure. */
  41. if (blk_size & BOX_ALIGN_8) {
  42. /* Memory blocks 8-byte aligned. */
  43. blk_size = ((blk_size & ~BOX_ALIGN_8) + 7U) & ~(U32)7U;
  44. sizeof_bm = (sizeof (struct OS_BM) + 7U) & ~(U32)7U;
  45. }
  46. else {
  47. /* Memory blocks 4-byte aligned. */
  48. blk_size = (blk_size + 3U) & ~(U32)3U;
  49. sizeof_bm = sizeof (struct OS_BM);
  50. }
  51. if (blk_size == 0U) {
  52. return (1U);
  53. }
  54. if ((blk_size + sizeof_bm) > box_size) {
  55. return (1U);
  56. }
  57. /* Create a Memory structure. */
  58. blk = ((U8 *) box_mem) + sizeof_bm;
  59. ((P_BM) box_mem)->free = blk;
  60. end = ((U8 *) box_mem) + box_size;
  61. ((P_BM) box_mem)->end = end;
  62. ((P_BM) box_mem)->blk_size = blk_size;
  63. /* Link all free blocks using offsets. */
  64. end = ((U8 *) end) - blk_size;
  65. while (1) {
  66. next = ((U8 *) blk) + blk_size;
  67. if (next > end) { break; }
  68. *((void **)blk) = next;
  69. blk = next;
  70. }
  71. /* end marker */
  72. *((void **)blk) = 0U;
  73. return (0U);
  74. }
  75. /*--------------------------- rt_alloc_box ----------------------------------*/
  76. void *rt_alloc_box (void *box_mem) {
  77. /* Allocate a memory block and return start address. */
  78. void **free;
  79. #ifndef __USE_EXCLUSIVE_ACCESS
  80. U32 irq_mask;
  81. irq_mask = (U32)__disable_irq ();
  82. free = ((P_BM) box_mem)->free;
  83. if (free) {
  84. ((P_BM) box_mem)->free = *free;
  85. }
  86. if (irq_mask == 0U) { __enable_irq (); }
  87. #else
  88. do {
  89. if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0U) {
  90. __clrex();
  91. break;
  92. }
  93. } while (__strex((U32)*free, &((P_BM) box_mem)->free));
  94. #endif
  95. return (free);
  96. }
  97. /*--------------------------- _calloc_box -----------------------------------*/
  98. void *_calloc_box (void *box_mem) {
  99. /* Allocate a 0-initialized memory block and return start address. */
  100. void *free;
  101. U32 *p;
  102. U32 i;
  103. free = _alloc_box (box_mem);
  104. if (free) {
  105. p = free;
  106. for (i = ((P_BM) box_mem)->blk_size; i; i -= 4U) {
  107. *p = 0U;
  108. p++;
  109. }
  110. }
  111. return (free);
  112. }
  113. /*--------------------------- rt_free_box -----------------------------------*/
  114. U32 rt_free_box (void *box_mem, void *box) {
  115. /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
  116. #ifndef __USE_EXCLUSIVE_ACCESS
  117. U32 irq_mask;
  118. #endif
  119. if ((box < box_mem) || (box >= ((P_BM) box_mem)->end)) {
  120. return (1U);
  121. }
  122. #ifndef __USE_EXCLUSIVE_ACCESS
  123. irq_mask = (U32)__disable_irq ();
  124. *((void **)box) = ((P_BM) box_mem)->free;
  125. ((P_BM) box_mem)->free = box;
  126. if (irq_mask == 0U) { __enable_irq (); }
  127. #else
  128. do {
  129. do {
  130. *((void **)box) = ((P_BM) box_mem)->free;
  131. __DMB();
  132. } while (*(void**)box != (void *)__ldrex(&((P_BM) box_mem)->free));
  133. } while (__strex ((U32)box, &((P_BM) box_mem)->free));
  134. #endif
  135. return (0U);
  136. }
  137. /*----------------------------------------------------------------------------
  138. * end of file
  139. *---------------------------------------------------------------------------*/