1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup RTEMSScoreMemory |
---|
5 | * |
---|
6 | * @brief Memory Handler API |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * SPDX-License-Identifier: BSD-2-Clause |
---|
11 | * |
---|
12 | * Copyright (C) 2019 embedded brains GmbH |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifndef _RTEMS_SCORE_MEMORY_H |
---|
37 | #define _RTEMS_SCORE_MEMORY_H |
---|
38 | |
---|
39 | #include <rtems/score/basedefs.h> |
---|
40 | #include <rtems/score/assert.h> |
---|
41 | |
---|
42 | #ifdef __cplusplus |
---|
43 | extern "C" { |
---|
44 | #endif /* __cplusplus */ |
---|
45 | |
---|
46 | /** |
---|
47 | * @defgroup RTEMSScoreMemory Memory Handler |
---|
48 | * |
---|
49 | * @ingroup RTEMSScore |
---|
50 | * |
---|
51 | * @brief Low level handler to provide memory areas for higher level memory |
---|
52 | * handlers such as the Workspace Handler. |
---|
53 | * |
---|
54 | * @{ |
---|
55 | */ |
---|
56 | |
---|
57 | /** |
---|
58 | * @brief The memory area description. |
---|
59 | */ |
---|
60 | typedef struct { |
---|
61 | /** |
---|
62 | * @brief A pointer to the begin of the memory area. |
---|
63 | */ |
---|
64 | const void *begin; |
---|
65 | |
---|
66 | /** |
---|
67 | * @brief A pointer to the begin of the free area of the memory area. |
---|
68 | */ |
---|
69 | void *free; |
---|
70 | |
---|
71 | /** |
---|
72 | * @brief A pointer to the end of the memory area. |
---|
73 | */ |
---|
74 | const void *end; |
---|
75 | } Memory_Area; |
---|
76 | |
---|
77 | /** |
---|
78 | * @brief The memory information. |
---|
79 | */ |
---|
80 | typedef struct { |
---|
81 | /** |
---|
82 | * @brief The count of memory areas. |
---|
83 | */ |
---|
84 | size_t count; |
---|
85 | |
---|
86 | /** |
---|
87 | * @brief The memory area table. |
---|
88 | */ |
---|
89 | Memory_Area *areas; |
---|
90 | } Memory_Information; |
---|
91 | |
---|
92 | /** |
---|
93 | * @brief Statically initialize a memory information. |
---|
94 | * |
---|
95 | * @param areas The designator of an array of the memory areas. |
---|
96 | */ |
---|
97 | #define MEMORY_INFORMATION_INITIALIZER( areas ) \ |
---|
98 | { RTEMS_ARRAY_SIZE( areas ), ( areas ) } |
---|
99 | |
---|
100 | /** |
---|
101 | * @brief Statically initialize a memory area. |
---|
102 | * |
---|
103 | * @param begin The begin of the memory area. |
---|
104 | * @param end The end of the memory area. |
---|
105 | */ |
---|
106 | #define MEMORY_INITIALIZER( begin, end ) { ( begin ), ( begin ), ( end ) } |
---|
107 | |
---|
108 | /** |
---|
109 | * @brief Get the memory area count. |
---|
110 | * |
---|
111 | * @param information The memory information. |
---|
112 | * |
---|
113 | * @return The memory area count. |
---|
114 | */ |
---|
115 | RTEMS_INLINE_ROUTINE size_t _Memory_Get_count( |
---|
116 | const Memory_Information *information |
---|
117 | ) |
---|
118 | { |
---|
119 | return information->count; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * @brief Get a memory area by index. |
---|
124 | * |
---|
125 | * @param information The memory information. |
---|
126 | * @param index The index of the memory area to return. |
---|
127 | * |
---|
128 | * @return The memory area of the specified index. |
---|
129 | */ |
---|
130 | RTEMS_INLINE_ROUTINE Memory_Area *_Memory_Get_area( |
---|
131 | const Memory_Information *information, |
---|
132 | size_t index |
---|
133 | ) |
---|
134 | { |
---|
135 | _Assert( index < _Memory_Get_count( information ) ); |
---|
136 | return &information->areas[ index ]; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * @brief Initialize the memory area. |
---|
141 | * |
---|
142 | * @param area The memory area. |
---|
143 | * @param begin The begin of the memory area. |
---|
144 | * @param end The end of the memory area. |
---|
145 | */ |
---|
146 | RTEMS_INLINE_ROUTINE void _Memory_Initialize( |
---|
147 | Memory_Area *area, |
---|
148 | void *begin, |
---|
149 | void *end |
---|
150 | ) |
---|
151 | { |
---|
152 | area->begin = begin; |
---|
153 | area->free = begin; |
---|
154 | area->end = end; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * @brief Initialize the memory area by size. |
---|
159 | * |
---|
160 | * @param area The memory area. |
---|
161 | * @param begin The begin of the memory area. |
---|
162 | * @param size The size of the memory area in bytes. |
---|
163 | */ |
---|
164 | RTEMS_INLINE_ROUTINE void _Memory_Initialize_by_size( |
---|
165 | Memory_Area *area, |
---|
166 | void *begin, |
---|
167 | uintptr_t size |
---|
168 | ) |
---|
169 | { |
---|
170 | area->begin = begin; |
---|
171 | area->free = begin; |
---|
172 | area->end = (char *) begin + size; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * @brief Get the memory area begin. |
---|
177 | * |
---|
178 | * @param area The memory area. |
---|
179 | * |
---|
180 | * @return The memory area begin. |
---|
181 | */ |
---|
182 | RTEMS_INLINE_ROUTINE const void *_Memory_Get_begin( const Memory_Area *area ) |
---|
183 | { |
---|
184 | return area->begin; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * @brief Set the memory area begin. |
---|
189 | * |
---|
190 | * @param area The memory area. |
---|
191 | * @param begin The memory area begin. |
---|
192 | */ |
---|
193 | RTEMS_INLINE_ROUTINE void _Memory_Set_begin( |
---|
194 | Memory_Area *area, |
---|
195 | const void *begin |
---|
196 | ) |
---|
197 | { |
---|
198 | area->begin = begin; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * @brief Get the memory area end. |
---|
203 | * |
---|
204 | * @param area The memory area. |
---|
205 | * |
---|
206 | * @return The memory area end. |
---|
207 | */ |
---|
208 | RTEMS_INLINE_ROUTINE const void *_Memory_Get_end( const Memory_Area *area ) |
---|
209 | { |
---|
210 | return area->end; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * @brief Set the memory area end. |
---|
215 | * |
---|
216 | * @param area The memory area. |
---|
217 | * @param end The memory area end. |
---|
218 | */ |
---|
219 | RTEMS_INLINE_ROUTINE void _Memory_Set_end( |
---|
220 | Memory_Area *area, |
---|
221 | const void *end |
---|
222 | ) |
---|
223 | { |
---|
224 | area->end = end; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * @brief Get the memory area size. |
---|
229 | * |
---|
230 | * @param area The memory area. |
---|
231 | * |
---|
232 | * @return The memory area size in bytes. |
---|
233 | */ |
---|
234 | RTEMS_INLINE_ROUTINE uintptr_t _Memory_Get_size( const Memory_Area *area ) |
---|
235 | { |
---|
236 | return (uintptr_t) area->end - (uintptr_t) area->begin; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * @brief Get the begin of the free area of the memory area. |
---|
241 | * |
---|
242 | * @param area The memory area. |
---|
243 | * |
---|
244 | * @return The free memory area begin the memory area. |
---|
245 | */ |
---|
246 | RTEMS_INLINE_ROUTINE void *_Memory_Get_free_begin( const Memory_Area *area ) |
---|
247 | { |
---|
248 | return area->free; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * @brief Set the begin of the free area of the memory area. |
---|
253 | * |
---|
254 | * @param area The memory area. |
---|
255 | * @param begin The free memory area begin the memory area. |
---|
256 | */ |
---|
257 | RTEMS_INLINE_ROUTINE void _Memory_Set_free_begin( |
---|
258 | Memory_Area *area, |
---|
259 | void *begin |
---|
260 | ) |
---|
261 | { |
---|
262 | area->free = begin; |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * @brief Get the size of the free memory area of the memory area. |
---|
267 | * |
---|
268 | * @param area The memory area. |
---|
269 | * |
---|
270 | * @return The free memory area size in bytes of the memory area. |
---|
271 | */ |
---|
272 | RTEMS_INLINE_ROUTINE uintptr_t _Memory_Get_free_size( const Memory_Area *area ) |
---|
273 | { |
---|
274 | return (uintptr_t) area->end - (uintptr_t) area->free; |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * @brief Consume the specified size from the free memory area of the memory |
---|
279 | * area. |
---|
280 | * |
---|
281 | * @param area The memory area. |
---|
282 | * @param consume The bytes to consume from the free memory area of the memory |
---|
283 | * area. |
---|
284 | */ |
---|
285 | RTEMS_INLINE_ROUTINE void _Memory_Consume( |
---|
286 | Memory_Area *area, |
---|
287 | uintptr_t consume |
---|
288 | ) |
---|
289 | { |
---|
290 | area->free = (char *) area->free + consume; |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | * @brief Return the memory information of this platform. |
---|
295 | * |
---|
296 | * This function is provided by the Board Support Package (BSP). Using a |
---|
297 | * function gives the BSPs a bit more freedom with respect to the |
---|
298 | * implementation. Calling this function shall not have side-effects. |
---|
299 | * Initialization steps to set up the memory information shall be done in a |
---|
300 | * system initialization handler (RTEMS_SYSINIT_MEMORY). |
---|
301 | * |
---|
302 | * @return The memory information. |
---|
303 | */ |
---|
304 | const Memory_Information *_Memory_Get( void ); |
---|
305 | |
---|
306 | /** |
---|
307 | * @brief Allocate a memory area from the memory information. |
---|
308 | * |
---|
309 | * It is not possible to free the memory area allocated by this function. |
---|
310 | * |
---|
311 | * @param information The memory information. |
---|
312 | * @param size The size in bytes of the memory area to allocate. |
---|
313 | * @param alignment The alignment in bytes of the memory area to allocate. It |
---|
314 | * must be a power of two. |
---|
315 | * |
---|
316 | * @retval NULL No such memory area available. |
---|
317 | * @retval begin The begin of the allocated memory area. |
---|
318 | */ |
---|
319 | void *_Memory_Allocate( |
---|
320 | const Memory_Information *information, |
---|
321 | uintptr_t size, |
---|
322 | uintptr_t alignment |
---|
323 | ); |
---|
324 | |
---|
325 | /** |
---|
326 | * @brief Fill all free memory areas of the memory information with a constant |
---|
327 | * byte. |
---|
328 | * |
---|
329 | * @param information The memory information. |
---|
330 | * @param c The constant byte to fill the free memory areas. |
---|
331 | */ |
---|
332 | void _Memory_Fill( const Memory_Information *information, int c ); |
---|
333 | |
---|
334 | /** @} */ |
---|
335 | |
---|
336 | #ifdef __cplusplus |
---|
337 | } |
---|
338 | #endif /* __cplusplus */ |
---|
339 | |
---|
340 | #endif /* _RTEMS_SCORE_MEMORY_H */ |
---|