]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_MemBox.c
Merge commit 'fdc38ef3f92af7adeeb4de49550d8838c8a39b5c'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_A / rt_MemBox.c
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
35 #include "rt_TypeDef.h"
36 #include "RTX_Config.h"
37 #include "rt_System.h"
38 #include "rt_MemBox.h"
39 #ifdef __CORTEX_A9
40 #include "rt_HAL_CA.h"
41 #else
42 #include "rt_HAL_CM.h"
43 #endif
44
45 /*----------------------------------------------------------------------------
46 * Global Functions
47 *---------------------------------------------------------------------------*/
48
49
50 /*--------------------------- _init_box -------------------------------------*/
51
52 int _init_box (void *box_mem, U32 box_size, U32 blk_size) {
53 /* Initialize memory block system, returns 0 if OK, 1 if fails. */
54 void *end;
55 void *blk;
56 void *next;
57 U32 sizeof_bm;
58
59 /* Create memory structure. */
60 if (blk_size & BOX_ALIGN_8) {
61 /* Memory blocks 8-byte aligned. */
62 blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7;
63 sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7;
64 }
65 else {
66 /* Memory blocks 4-byte aligned. */
67 blk_size = (blk_size + 3) & ~3;
68 sizeof_bm = sizeof (struct OS_BM);
69 }
70 if (blk_size == 0) {
71 return (1);
72 }
73 if ((blk_size + sizeof_bm) > box_size) {
74 return (1);
75 }
76 /* Create a Memory structure. */
77 blk = ((U8 *) box_mem) + sizeof_bm;
78 ((P_BM) box_mem)->free = blk;
79 end = ((U8 *) box_mem) + box_size;
80 ((P_BM) box_mem)->end = end;
81 ((P_BM) box_mem)->blk_size = blk_size;
82
83 /* Link all free blocks using offsets. */
84 end = ((U8 *) end) - blk_size;
85 while (1) {
86 next = ((U8 *) blk) + blk_size;
87 if (next > end) break;
88 *((void **)blk) = next;
89 blk = next;
90 }
91 /* end marker */
92 *((void **)blk) = 0;
93 return (0);
94 }
95
96 /*--------------------------- rt_alloc_box ----------------------------------*/
97
98 void *rt_alloc_box (void *box_mem) {
99 /* Allocate a memory block and return start address. */
100 void **free;
101 #ifndef __USE_EXCLUSIVE_ACCESS
102 int irq_dis;
103
104 irq_dis = __disable_irq ();
105 free = ((P_BM) box_mem)->free;
106 if (free) {
107 ((P_BM) box_mem)->free = *free;
108 }
109 if (!irq_dis) __enable_irq ();
110 #else
111 do {
112 if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) {
113 __clrex();
114 break;
115 }
116 } while (__strex((U32)*free, &((P_BM) box_mem)->free));
117 #endif
118 return (free);
119 }
120
121
122 /*--------------------------- _calloc_box -----------------------------------*/
123
124 void *_calloc_box (void *box_mem) {
125 /* Allocate a 0-initialized memory block and return start address. */
126 void *free;
127 U32 *p;
128 U32 i;
129
130 free = _alloc_box (box_mem);
131 if (free) {
132 p = free;
133 for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) {
134 *p = 0;
135 p++;
136 }
137 }
138 return (free);
139 }
140
141
142 /*--------------------------- rt_free_box -----------------------------------*/
143
144 int rt_free_box (void *box_mem, void *box) {
145 /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
146 #ifndef __USE_EXCLUSIVE_ACCESS
147 int irq_dis;
148 #endif
149
150 if (box < box_mem || box >= ((P_BM) box_mem)->end) {
151 return (1);
152 }
153
154 #ifndef __USE_EXCLUSIVE_ACCESS
155 irq_dis = __disable_irq ();
156 *((void **)box) = ((P_BM) box_mem)->free;
157 ((P_BM) box_mem)->free = box;
158 if (!irq_dis) __enable_irq ();
159 #else
160 do {
161 *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free);
162 } while (__strex ((U32)box, &((P_BM) box_mem)->free));
163 #endif
164 return (0);
165 }
166
167 /*----------------------------------------------------------------------------
168 * end of file
169 *---------------------------------------------------------------------------*/
170
Imprint / Impressum