Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rt_MemBox.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_MEMBOX.C
  5. * Purpose: Interface functions for fixed memory block management system
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "RTX_Config.h"
  36. #include "rt_System.h"
  37. #include "rt_MemBox.h"
  38. #ifdef __CORTEX_A9
  39. #include "rt_HAL_CA.h"
  40. #else
  41. #include "rt_HAL_CM.h"
  42. #endif
  43. /*----------------------------------------------------------------------------
  44. * Global Functions
  45. *---------------------------------------------------------------------------*/
  46. /*--------------------------- _init_box -------------------------------------*/
  47. int _init_box (void *box_mem, U32 box_size, U32 blk_size) {
  48. /* Initialize memory block system, returns 0 if OK, 1 if fails. */
  49. void *end;
  50. void *blk;
  51. void *next;
  52. U32 sizeof_bm;
  53. /* Create memory structure. */
  54. if (blk_size & BOX_ALIGN_8) {
  55. /* Memory blocks 8-byte aligned. */
  56. blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7;
  57. sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7;
  58. }
  59. else {
  60. /* Memory blocks 4-byte aligned. */
  61. blk_size = (blk_size + 3) & ~3;
  62. sizeof_bm = sizeof (struct OS_BM);
  63. }
  64. if (blk_size == 0) {
  65. return (1);
  66. }
  67. if ((blk_size + sizeof_bm) > box_size) {
  68. return (1);
  69. }
  70. /* Create a Memory structure. */
  71. blk = ((U8 *) box_mem) + sizeof_bm;
  72. ((P_BM) box_mem)->free = blk;
  73. end = ((U8 *) box_mem) + box_size;
  74. ((P_BM) box_mem)->end = end;
  75. ((P_BM) box_mem)->blk_size = blk_size;
  76. /* Link all free blocks using offsets. */
  77. end = ((U8 *) end) - blk_size;
  78. while (1) {
  79. next = ((U8 *) blk) + blk_size;
  80. if (next > end) break;
  81. *((void **)blk) = next;
  82. blk = next;
  83. }
  84. /* end marker */
  85. *((void **)blk) = 0;
  86. return (0);
  87. }
  88. /*--------------------------- rt_alloc_box ----------------------------------*/
  89. void *rt_alloc_box (void *box_mem) {
  90. /* Allocate a memory block and return start address. */
  91. void **free;
  92. #ifndef __USE_EXCLUSIVE_ACCESS
  93. int irq_dis;
  94. irq_dis = __disable_irq ();
  95. free = ((P_BM) box_mem)->free;
  96. if (free) {
  97. ((P_BM) box_mem)->free = *free;
  98. }
  99. if (!irq_dis) __enable_irq ();
  100. #else
  101. do {
  102. if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) {
  103. __clrex();
  104. break;
  105. }
  106. } while (__strex((U32)*free, &((P_BM) box_mem)->free));
  107. #endif
  108. return (free);
  109. }
  110. /*--------------------------- _calloc_box -----------------------------------*/
  111. void *_calloc_box (void *box_mem) {
  112. /* Allocate a 0-initialized memory block and return start address. */
  113. void *free;
  114. U32 *p;
  115. U32 i;
  116. free = _alloc_box (box_mem);
  117. if (free) {
  118. p = free;
  119. for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) {
  120. *p = 0;
  121. p++;
  122. }
  123. }
  124. return (free);
  125. }
  126. /*--------------------------- rt_free_box -----------------------------------*/
  127. int rt_free_box (void *box_mem, void *box) {
  128. /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
  129. #ifndef __USE_EXCLUSIVE_ACCESS
  130. int irq_dis;
  131. #endif
  132. if (box < box_mem || box >= ((P_BM) box_mem)->end) {
  133. return (1);
  134. }
  135. #ifndef __USE_EXCLUSIVE_ACCESS
  136. irq_dis = __disable_irq ();
  137. *((void **)box) = ((P_BM) box_mem)->free;
  138. ((P_BM) box_mem)->free = box;
  139. if (!irq_dis) __enable_irq ();
  140. #else
  141. do {
  142. *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free);
  143. } while (__strex ((U32)box, &((P_BM) box_mem)->free));
  144. #endif
  145. return (0);
  146. }
  147. /*----------------------------------------------------------------------------
  148. * end of file
  149. *---------------------------------------------------------------------------*/