1 | /** |
---|
2 | * @file rtems/malloc.h |
---|
3 | * |
---|
4 | * This file defines the interface to RTEMS extensions to the Malloc Family. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2011. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may in |
---|
12 | * the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef _RTEMS_MALLOC_H |
---|
17 | #define _RTEMS_MALLOC_H |
---|
18 | |
---|
19 | #include <rtems.h> |
---|
20 | #include <rtems/bspIo.h> |
---|
21 | |
---|
22 | #include <stdint.h> |
---|
23 | |
---|
24 | #ifdef __cplusplus |
---|
25 | extern "C" { |
---|
26 | #endif |
---|
27 | |
---|
28 | /* |
---|
29 | * Malloc Statistics Structure |
---|
30 | */ |
---|
31 | typedef struct { |
---|
32 | uint32_t space_available; /* current size of malloc area */ |
---|
33 | uint32_t malloc_calls; /* # calls to malloc */ |
---|
34 | uint32_t memalign_calls; /* # calls to memalign */ |
---|
35 | uint32_t free_calls; |
---|
36 | uint32_t realloc_calls; |
---|
37 | uint32_t calloc_calls; |
---|
38 | uint32_t max_depth; /* most ever malloc'd at 1 time */ |
---|
39 | uintmax_t lifetime_allocated; |
---|
40 | uintmax_t lifetime_freed; |
---|
41 | } rtems_malloc_statistics_t; |
---|
42 | |
---|
43 | /* |
---|
44 | * Malloc statistics plugin |
---|
45 | */ |
---|
46 | typedef struct { |
---|
47 | void (*initialize)(void); |
---|
48 | void (*at_malloc)(void *); |
---|
49 | void (*at_free)(void *); |
---|
50 | } rtems_malloc_statistics_functions_t; |
---|
51 | |
---|
52 | extern rtems_malloc_statistics_functions_t |
---|
53 | rtems_malloc_statistics_helpers_table; |
---|
54 | extern rtems_malloc_statistics_functions_t *rtems_malloc_statistics_helpers; |
---|
55 | |
---|
56 | /* |
---|
57 | * Malloc Heap Extension (sbrk) plugin |
---|
58 | */ |
---|
59 | typedef struct { |
---|
60 | void *(*initialize)(void *, size_t); |
---|
61 | void *(*extend)(size_t); |
---|
62 | } rtems_malloc_sbrk_functions_t; |
---|
63 | |
---|
64 | extern rtems_malloc_sbrk_functions_t rtems_malloc_sbrk_helpers_table; |
---|
65 | extern rtems_malloc_sbrk_functions_t *rtems_malloc_sbrk_helpers; |
---|
66 | |
---|
67 | /* |
---|
68 | * Malloc Plugin to Dirty Memory at Allocation Time |
---|
69 | */ |
---|
70 | typedef void (*rtems_malloc_dirtier_t)(void *, size_t); |
---|
71 | extern rtems_malloc_dirtier_t rtems_malloc_dirty_helper; |
---|
72 | |
---|
73 | /** |
---|
74 | * @brief Dirty memory function |
---|
75 | * |
---|
76 | * This method fills the specified area with a non-zero pattern |
---|
77 | * to aid in debugging programs which do not initialize their |
---|
78 | * memory allocated from the heap. |
---|
79 | */ |
---|
80 | void rtems_malloc_dirty_memory( |
---|
81 | void *start, |
---|
82 | size_t size |
---|
83 | ); |
---|
84 | |
---|
85 | /** |
---|
86 | * @brief Print Malloc Statistic Usage Report |
---|
87 | * |
---|
88 | * This method fills in the called provided malloc statistics area. |
---|
89 | * |
---|
90 | * @return This method returns 0 if successful and -1 on error. |
---|
91 | */ |
---|
92 | int malloc_get_statistics( |
---|
93 | rtems_malloc_statistics_t *stats |
---|
94 | ); |
---|
95 | |
---|
96 | /** |
---|
97 | * @brief Print Malloc Statistic Usage Report |
---|
98 | * |
---|
99 | * This method prints a malloc statistics report. |
---|
100 | * |
---|
101 | * @note It uses printk to print the report. |
---|
102 | */ |
---|
103 | void malloc_report_statistics(void); |
---|
104 | |
---|
105 | /** |
---|
106 | * @brief Print Malloc Statistic Usage Report |
---|
107 | * |
---|
108 | * This method prints a malloc statistics report. |
---|
109 | * |
---|
110 | * @param[in] context is the context to pass to the print handler |
---|
111 | * @param[in] print is the print handler |
---|
112 | * |
---|
113 | * @note It uses the CALLER's routine to print the report. |
---|
114 | */ |
---|
115 | void malloc_report_statistics_with_plugin( |
---|
116 | void *context, |
---|
117 | rtems_printk_plugin_t print |
---|
118 | ); |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief RTEMS variation on Aligned Memory Allocation |
---|
122 | * |
---|
123 | * This method is a help memalign implementation which does all |
---|
124 | * error checking done by posix_memalign() EXCEPT it does NOT |
---|
125 | * place numeric restrictions on the alignment value. |
---|
126 | * |
---|
127 | * @param[in] pointer points to the user pointer |
---|
128 | * @param[in] alignment is the desired alignment |
---|
129 | * @param[in] size is the allocation request size in bytes |
---|
130 | * |
---|
131 | * @return This methods returns zero on success and a POSIX errno |
---|
132 | * value to indicate the failure condition. On success |
---|
133 | * *pointer will contain the address of the allocated memory. |
---|
134 | */ |
---|
135 | int rtems_memalign( |
---|
136 | void **pointer, |
---|
137 | size_t alignment, |
---|
138 | size_t size |
---|
139 | ); |
---|
140 | |
---|
141 | /** |
---|
142 | * @brief Allocates a memory area of size @a size bytes from the heap. |
---|
143 | * |
---|
144 | * If the alignment parameter @a alignment is not equal to zero, the allocated |
---|
145 | * memory area will begin at an address aligned by this value. |
---|
146 | * |
---|
147 | * If the boundary parameter @a boundary is not equal to zero, the allocated |
---|
148 | * memory area will fulfill a boundary constraint. The boundary value |
---|
149 | * specifies the set of addresses which are aligned by the boundary value. The |
---|
150 | * interior of the allocated memory area will not contain an element of this |
---|
151 | * set. The begin or end address of the area may be a member of the set. |
---|
152 | * |
---|
153 | * A size value of zero will return a unique address which may be freed with |
---|
154 | * free(). |
---|
155 | * |
---|
156 | * The memory allocated by this function can be released with a call to free(). |
---|
157 | * |
---|
158 | * @return A pointer to the begin of the allocated memory area, or @c NULL if |
---|
159 | * no memory is available or the parameters are inconsistent. |
---|
160 | */ |
---|
161 | void *rtems_heap_allocate_aligned_with_boundary( |
---|
162 | size_t size, |
---|
163 | uintptr_t alignment, |
---|
164 | uintptr_t boundary |
---|
165 | ); |
---|
166 | |
---|
167 | /** |
---|
168 | * @brief Extends the memory available for the heap using the memory area |
---|
169 | * starting at @a area_begin of size @a area_size bytes. |
---|
170 | * |
---|
171 | * There are no alignment requirements. The memory area must be big enough to |
---|
172 | * contain some maintainance blocks. It must not overlap parts of the current |
---|
173 | * heap areas. Disconnected subordinate heap areas will lead to used blocks |
---|
174 | * which cover the gaps. Extending with an inappropriate memory area will |
---|
175 | * corrupt the heap. |
---|
176 | * |
---|
177 | * @retval RTEMS_SUCCESSFUL Successful operation. |
---|
178 | * @retval RTEMS_INVALID_ADDRESS Invalid memory area. |
---|
179 | */ |
---|
180 | rtems_status_code rtems_heap_extend( |
---|
181 | void *area_begin, |
---|
182 | uintptr_t area_size |
---|
183 | ); |
---|
184 | |
---|
185 | /** |
---|
186 | * @brief Greedy allocate that empties the heap. |
---|
187 | * |
---|
188 | * Afterward the heap has at most @a remaining_free_space free space left in |
---|
189 | * one free block. All other blocks are used. |
---|
190 | * |
---|
191 | * @see rtems_heap_greedy_free(). |
---|
192 | */ |
---|
193 | void *rtems_heap_greedy_allocate( size_t remaining_free_space ); |
---|
194 | |
---|
195 | /** |
---|
196 | * @brief Frees space of a greedy allocation. |
---|
197 | * |
---|
198 | * The @a opaque argument must be the return value of |
---|
199 | * rtems_heap_greedy_allocate(). |
---|
200 | */ |
---|
201 | void rtems_heap_greedy_free( void *opaque ); |
---|
202 | |
---|
203 | #ifdef __cplusplus |
---|
204 | } |
---|
205 | #endif |
---|
206 | |
---|
207 | #endif |
---|