1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup MallocSupport |
---|
7 | * |
---|
8 | * @ingroup RTEMSAPIMalloc |
---|
9 | * |
---|
10 | * @brief This header file defines interfaces to support and use dynamic memory |
---|
11 | * allocation. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * COPYRIGHT (c) 1989-2011. |
---|
16 | * On-Line Applications Research Corporation (OAR). |
---|
17 | * |
---|
18 | * Redistribution and use in source and binary forms, with or without |
---|
19 | * modification, are permitted provided that the following conditions |
---|
20 | * are met: |
---|
21 | * 1. Redistributions of source code must retain the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer. |
---|
23 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
24 | * notice, this list of conditions and the following disclaimer in the |
---|
25 | * documentation and/or other materials provided with the distribution. |
---|
26 | * |
---|
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
31 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
32 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
33 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
34 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
35 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
36 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
37 | * POSSIBILITY OF SUCH DAMAGE. |
---|
38 | */ |
---|
39 | |
---|
40 | #ifndef _RTEMS_MALLOC_H |
---|
41 | #define _RTEMS_MALLOC_H |
---|
42 | |
---|
43 | #include <rtems.h> |
---|
44 | #include <rtems/bspIo.h> |
---|
45 | #include <rtems/libcsupport.h> /* for malloc_walk() */ |
---|
46 | |
---|
47 | #include <stdint.h> |
---|
48 | |
---|
49 | #ifdef __cplusplus |
---|
50 | extern "C" { |
---|
51 | #endif |
---|
52 | |
---|
53 | /** |
---|
54 | * @defgroup MallocSupport Malloc Support |
---|
55 | * |
---|
56 | * @ingroup libcsupport |
---|
57 | * |
---|
58 | * @brief This group contains interfaces to support dynamic memory allocation. |
---|
59 | * |
---|
60 | * @{ |
---|
61 | */ |
---|
62 | |
---|
63 | /** |
---|
64 | * @brief C program heap control. |
---|
65 | * |
---|
66 | * This is the pointer to the heap control structure used to manage the C |
---|
67 | * program heap. |
---|
68 | */ |
---|
69 | extern Heap_Control *RTEMS_Malloc_Heap; |
---|
70 | |
---|
71 | void _Malloc_Initialize( void ); |
---|
72 | |
---|
73 | typedef void *(*rtems_heap_extend_handler)( |
---|
74 | Heap_Control *heap, |
---|
75 | size_t alloc_size |
---|
76 | ); |
---|
77 | |
---|
78 | /** |
---|
79 | * @brief RTEMS Extend Heap via Sbrk |
---|
80 | */ |
---|
81 | void *rtems_heap_extend_via_sbrk( |
---|
82 | Heap_Control *heap, |
---|
83 | size_t alloc_size |
---|
84 | ); |
---|
85 | |
---|
86 | void *rtems_heap_null_extend( |
---|
87 | Heap_Control *heap, |
---|
88 | size_t alloc_size |
---|
89 | ); |
---|
90 | |
---|
91 | extern const rtems_heap_extend_handler rtems_malloc_extend_handler; |
---|
92 | |
---|
93 | /* |
---|
94 | * Malloc Plugin to Dirty Memory at Allocation Time |
---|
95 | */ |
---|
96 | typedef void (*rtems_malloc_dirtier_t)(void *, size_t); |
---|
97 | extern rtems_malloc_dirtier_t rtems_malloc_dirty_helper; |
---|
98 | |
---|
99 | /** @} */ |
---|
100 | |
---|
101 | /** |
---|
102 | * @defgroup RTEMSAPIMalloc Dynamic Memory Allocation |
---|
103 | * |
---|
104 | * @ingroup RTEMSAPI |
---|
105 | * |
---|
106 | * @brief This group contains non-standard interfaces to use dynamic memory |
---|
107 | * allocation. |
---|
108 | * |
---|
109 | * @{ |
---|
110 | */ |
---|
111 | |
---|
112 | void rtems_heap_set_sbrk_amount( ptrdiff_t sbrk_amount ); |
---|
113 | |
---|
114 | /** |
---|
115 | * @brief Greedy allocate that empties the sbrk memory |
---|
116 | * |
---|
117 | * Afterwards all the sbrk avialable memory will have been allocated |
---|
118 | * to the provided heap. |
---|
119 | * |
---|
120 | * @see rtems_heap_extend_via_sbrk(). |
---|
121 | */ |
---|
122 | void rtems_heap_sbrk_greedy_allocate( |
---|
123 | Heap_Control *heap, |
---|
124 | size_t alloc_size |
---|
125 | ); |
---|
126 | |
---|
127 | /** |
---|
128 | * @brief Dirty Memory Function |
---|
129 | * |
---|
130 | * This method fills the specified area with a non-zero pattern |
---|
131 | * to aid in debugging programs which do not initialize their |
---|
132 | * memory allocated from the heap. |
---|
133 | */ |
---|
134 | void rtems_malloc_dirty_memory( |
---|
135 | void *start, |
---|
136 | size_t size |
---|
137 | ); |
---|
138 | |
---|
139 | /** |
---|
140 | * @brief RTEMS Variation on Aligned Memory Allocation |
---|
141 | * |
---|
142 | * This method is a help memalign implementation which does all |
---|
143 | * error checking done by posix_memalign() EXCEPT it does NOT |
---|
144 | * place numeric restrictions on the alignment value. |
---|
145 | * |
---|
146 | * @param[in] pointer points to the user pointer |
---|
147 | * @param[in] alignment is the desired alignment |
---|
148 | * @param[in] size is the allocation request size in bytes |
---|
149 | * |
---|
150 | * @return This methods returns zero on success and a POSIX errno |
---|
151 | * value to indicate the failure condition. On success |
---|
152 | * *pointer will contain the address of the allocated memory. |
---|
153 | */ |
---|
154 | int rtems_memalign( |
---|
155 | void **pointer, |
---|
156 | size_t alignment, |
---|
157 | size_t size |
---|
158 | ); |
---|
159 | |
---|
160 | /** |
---|
161 | * @brief Allocates a memory area of size @a size bytes from the heap. |
---|
162 | * |
---|
163 | * If the alignment parameter @a alignment is not equal to zero, the allocated |
---|
164 | * memory area will begin at an address aligned by this value. |
---|
165 | * |
---|
166 | * If the boundary parameter @a boundary is not equal to zero, the allocated |
---|
167 | * memory area will comply with a boundary constraint. The boundary value |
---|
168 | * specifies the set of addresses which are aligned by the boundary value. The |
---|
169 | * interior of the allocated memory area will not contain an element of this |
---|
170 | * set. The begin or end address of the area may be a member of the set. |
---|
171 | * |
---|
172 | * A size value of zero will return a unique address which may be freed with |
---|
173 | * free(). |
---|
174 | * |
---|
175 | * The memory allocated by this function can be released with a call to free(). |
---|
176 | * |
---|
177 | * @return A pointer to the begin of the allocated memory area, or @c NULL if |
---|
178 | * no memory is available or the parameters are inconsistent. |
---|
179 | */ |
---|
180 | void *rtems_heap_allocate_aligned_with_boundary( |
---|
181 | size_t size, |
---|
182 | uintptr_t alignment, |
---|
183 | uintptr_t boundary |
---|
184 | ) RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_ALLOC_ALIGN(2) |
---|
185 | RTEMS_WARN_UNUSED_RESULT; |
---|
186 | |
---|
187 | /** |
---|
188 | * @brief Allocates a memory area of the specified size from the heap. |
---|
189 | * |
---|
190 | * This function is almost identical to malloc(). The only exception is that |
---|
191 | * errno is not set in case of a memory allocation failure. |
---|
192 | * |
---|
193 | * @param[in] size The memory area size in bytes. |
---|
194 | * |
---|
195 | * @retval NULL The memory allocation failed or @a size is zero. |
---|
196 | * @retval otherwise The begin address of the allocated memory area. |
---|
197 | */ |
---|
198 | void *rtems_malloc(size_t size) |
---|
199 | RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE(1) RTEMS_WARN_UNUSED_RESULT; |
---|
200 | |
---|
201 | /** |
---|
202 | * @brief Allocates a memory area for the specified count of elements from the |
---|
203 | * heap. |
---|
204 | * |
---|
205 | * The allocated memory area is fully filled with zero bits. |
---|
206 | * |
---|
207 | * This function is almost identical to calloc(). The only exception is that |
---|
208 | * errno is not set in case of a memory allocation failure. |
---|
209 | * |
---|
210 | * @param[in] nelem The count of elements. |
---|
211 | * @param[in] elsize The size of each elements. |
---|
212 | * |
---|
213 | * @retval NULL The memory allocation failed or @a nelem is zero or @a elsize |
---|
214 | * is zero. |
---|
215 | * @retval otherwise The begin address of the allocated memory area. |
---|
216 | */ |
---|
217 | void *rtems_calloc(size_t nelem, size_t elsize) |
---|
218 | RTEMS_MALLOCLIKE RTEMS_ALLOC_SIZE_2(1, 2) RTEMS_WARN_UNUSED_RESULT; |
---|
219 | |
---|
220 | /** |
---|
221 | * @brief Extends the memory available for the heap using the memory area |
---|
222 | * starting at @a area_begin of size @a area_size bytes. |
---|
223 | * |
---|
224 | * There are no alignment requirements. The memory area must be big enough to |
---|
225 | * contain some maintenance blocks. It must not overlap parts of the current |
---|
226 | * heap areas. Disconnected subordinate heap areas will lead to used blocks |
---|
227 | * which cover the gaps. Extending with an inappropriate memory area will |
---|
228 | * corrupt the heap. |
---|
229 | * |
---|
230 | * @retval RTEMS_SUCCESSFUL Successful operation. |
---|
231 | * @retval RTEMS_INVALID_ADDRESS Invalid memory area. |
---|
232 | */ |
---|
233 | rtems_status_code rtems_heap_extend( |
---|
234 | void *area_begin, |
---|
235 | uintptr_t area_size |
---|
236 | ); |
---|
237 | |
---|
238 | /** |
---|
239 | * @brief Greedy allocate that empties the heap. |
---|
240 | * |
---|
241 | * Afterwards the heap has at most @a block_count allocatable blocks of sizes |
---|
242 | * specified by @a block_sizes. The @a block_sizes must point to an array with |
---|
243 | * @a block_count members. All other blocks are used. |
---|
244 | * |
---|
245 | * @see rtems_heap_greedy_free(). |
---|
246 | */ |
---|
247 | void *rtems_heap_greedy_allocate( |
---|
248 | const uintptr_t *block_sizes, |
---|
249 | size_t block_count |
---|
250 | ); |
---|
251 | |
---|
252 | /** |
---|
253 | * @brief Greedy allocate all blocks except the largest free block. |
---|
254 | * |
---|
255 | * Afterwards the heap has at most one allocatable block. This block is the |
---|
256 | * largest free block if it exists. The allocatable size of this block is |
---|
257 | * stored in @a allocatable_size. All other blocks are used. |
---|
258 | * |
---|
259 | * @see rtems_heap_greedy_free(). |
---|
260 | */ |
---|
261 | void *rtems_heap_greedy_allocate_all_except_largest( |
---|
262 | uintptr_t *allocatable_size |
---|
263 | ); |
---|
264 | |
---|
265 | /** |
---|
266 | * @brief Frees space of a greedy allocation. |
---|
267 | * |
---|
268 | * The @a opaque argument must be the return value of |
---|
269 | * rtems_heap_greedy_allocate() or |
---|
270 | * rtems_heap_greedy_allocate_all_except_largest(). |
---|
271 | */ |
---|
272 | void rtems_heap_greedy_free( void *opaque ); |
---|
273 | |
---|
274 | /** @} */ |
---|
275 | |
---|
276 | #ifdef __cplusplus |
---|
277 | } |
---|
278 | #endif |
---|
279 | |
---|
280 | #endif |
---|