1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ScoreHeap |
---|
5 | * |
---|
6 | * @brief Heap Handler API. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2006. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | * |
---|
17 | * $Id$ |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef _RTEMS_SCORE_HEAP_H |
---|
21 | #define _RTEMS_SCORE_HEAP_H |
---|
22 | |
---|
23 | #ifdef __cplusplus |
---|
24 | extern "C" { |
---|
25 | #endif |
---|
26 | |
---|
27 | /** |
---|
28 | * @defgroup ScoreHeap Heap Handler |
---|
29 | * |
---|
30 | * @ingroup Score |
---|
31 | * |
---|
32 | * @brief The Heap Handler provides a heap. |
---|
33 | * |
---|
34 | * A heap is a doubly linked list of variable size blocks which are allocated |
---|
35 | * using the first fit method. Garbage collection is performed each time a |
---|
36 | * block is returned to the heap by coalescing neighbor blocks. Control |
---|
37 | * information for both allocated and free blocks is contained in the heap |
---|
38 | * area. A heap control structure contains control information for the heap. |
---|
39 | * |
---|
40 | * The alignment routines could be made faster should we require only powers of |
---|
41 | * two to be supported both for page size, alignment and boundary arguments. |
---|
42 | * However, both workspace and malloc heaps are initialized with |
---|
43 | * CPU_HEAP_ALIGNMENT as page size, and while all the BSPs seem to use |
---|
44 | * CPU_ALIGNMENT (that is power of two) as CPU_HEAP_ALIGNMENT, for whatever |
---|
45 | * reason CPU_HEAP_ALIGNMENT is only required to be multiple of CPU_ALIGNMENT |
---|
46 | * and explicitly not required to be a power of two. |
---|
47 | * |
---|
48 | * There are two kinds of blocks. One sort describes a free block from which |
---|
49 | * we can allocate memory. The other blocks are used and provide an allocated |
---|
50 | * memory area. The free blocks are accessible via a list of free blocks. |
---|
51 | * |
---|
52 | * Blocks or areas cover a continuous set of memory addresses. They have a |
---|
53 | * begin and end address. The end address is not part of the set. The size of |
---|
54 | * a block or area equals the distance between the begin and end address in |
---|
55 | * units of bytes. |
---|
56 | * |
---|
57 | * Free blocks look like: |
---|
58 | * <table> |
---|
59 | * <tr> |
---|
60 | * <td rowspan=4>@ref Heap_Block</td><td>previous block size in case the |
---|
61 | * previous block is free, <br> otherwise it may contain data used by |
---|
62 | * the previous block</td> |
---|
63 | * </tr> |
---|
64 | * <tr> |
---|
65 | * <td>block size and a flag which indicates if the previous block is free |
---|
66 | * or used, <br> this field contains always valid data regardless of the |
---|
67 | * block usage</td> |
---|
68 | * </tr> |
---|
69 | * <tr><td>pointer to next block (this field is page size aligned)</td></tr> |
---|
70 | * <tr><td>pointer to previous block</td></tr> |
---|
71 | * <tr><td colspan=2>free space</td></tr> |
---|
72 | * </table> |
---|
73 | * |
---|
74 | * Used blocks look like: |
---|
75 | * <table> |
---|
76 | * <tr> |
---|
77 | * <td rowspan=4>@ref Heap_Block</td><td>previous block size in case the |
---|
78 | * previous block is free,<br>otherwise it may contain data used by |
---|
79 | * the previous block</td> |
---|
80 | * </tr> |
---|
81 | * <tr> |
---|
82 | * <td>block size and a flag which indicates if the previous block is free |
---|
83 | * or used, <br> this field contains always valid data regardless of the |
---|
84 | * block usage</td> |
---|
85 | * </tr> |
---|
86 | * <tr><td>begin of allocated area (this field is page size aligned)</td></tr> |
---|
87 | * <tr><td>allocated space</td></tr> |
---|
88 | * <tr><td colspan=2>allocated space</td></tr> |
---|
89 | * </table> |
---|
90 | * |
---|
91 | * The heap area after initialization contains two blocks and looks like: |
---|
92 | * <table> |
---|
93 | * <tr><th>Label</th><th colspan=2>Content</th></tr> |
---|
94 | * <tr><td>heap->begin</td><td colspan=2>heap area begin address</td></tr> |
---|
95 | * <tr> |
---|
96 | * <td>first_block->prev_size</td> |
---|
97 | * <td colspan=2>page size (the value is arbitrary)</td> |
---|
98 | * </tr> |
---|
99 | * <tr> |
---|
100 | * <td>first_block->size</td> |
---|
101 | * <td colspan=2>size available for allocation |
---|
102 | * | @c HEAP_PREV_BLOCK_USED</td> |
---|
103 | * </tr> |
---|
104 | * <tr> |
---|
105 | * <td>first_block->next</td><td>_Heap_Free_list_tail(heap)</td> |
---|
106 | * <td rowspan=3>memory area available for allocation</td> |
---|
107 | * </tr> |
---|
108 | * <tr><td>first_block->prev</td><td>_Heap_Free_list_head(heap)</td></tr> |
---|
109 | * <tr><td>...</td></tr> |
---|
110 | * <tr> |
---|
111 | * <td>second_block->prev_size</td><td colspan=2>size of first block</td> |
---|
112 | * </tr> |
---|
113 | * <tr> |
---|
114 | * <td>second_block->size</td> |
---|
115 | * <td colspan=2>page size (the value is arbitrary)</td> |
---|
116 | * </tr> |
---|
117 | * <tr><td>heap->end</td><td colspan=2>heap area end address</td></tr> |
---|
118 | * </table> |
---|
119 | * |
---|
120 | * @{ |
---|
121 | */ |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief See also @ref Heap_Block.size_and_flag. |
---|
125 | */ |
---|
126 | #define HEAP_PREV_BLOCK_USED ((uintptr_t) 1) |
---|
127 | |
---|
128 | /** |
---|
129 | * @brief Offset from the block begin up to the block size field |
---|
130 | * (@ref Heap_Block.size_and_flag). |
---|
131 | */ |
---|
132 | #define HEAP_BLOCK_SIZE_OFFSET sizeof(uintptr_t) |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief The block header consists of the two size fields |
---|
136 | * (@ref Heap_Block.prev_size and @ref Heap_Block.size_and_flag). |
---|
137 | */ |
---|
138 | #define HEAP_BLOCK_HEADER_SIZE (sizeof(uintptr_t) * 2) |
---|
139 | |
---|
140 | /** |
---|
141 | * @brief Description for free or used blocks. |
---|
142 | */ |
---|
143 | typedef struct Heap_Block { |
---|
144 | /** |
---|
145 | * @brief Size of the previous block or part of the allocated area of the |
---|
146 | * previous block. |
---|
147 | * |
---|
148 | * This field is only valid if the previous block is free. This case is |
---|
149 | * indicated by a cleared @c HEAP_PREV_BLOCK_USED flag in the |
---|
150 | * @a size_and_flag field of the current block. |
---|
151 | * |
---|
152 | * In a used block only the @a size_and_flag field needs to be valid. The |
---|
153 | * @a prev_size field of the current block is maintained by the previous |
---|
154 | * block. The current block can use the @a prev_size field in the next block |
---|
155 | * for allocation. |
---|
156 | */ |
---|
157 | uintptr_t prev_size; |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief Contains the size of the current block and a flag which indicates |
---|
161 | * if the previous block is free or used. |
---|
162 | * |
---|
163 | * If the flag @c HEAP_PREV_BLOCK_USED is set, then the previous block is |
---|
164 | * used, otherwise the previous block is free. A used previous block may |
---|
165 | * claim the @a prev_size field for allocation. This trick allows to |
---|
166 | * decrease the overhead in the used blocks by the size of the |
---|
167 | * @a prev_size field. As sizes are always multiples of four, the two least |
---|
168 | * significant bits are always zero. We use one of them to store the flag. |
---|
169 | * |
---|
170 | * This field is always valid. |
---|
171 | */ |
---|
172 | uintptr_t size_and_flag; |
---|
173 | |
---|
174 | /** |
---|
175 | * @brief Pointer to the next free block or part of the allocated area. |
---|
176 | * |
---|
177 | * This field is page size aligned and begins of the allocated area in case |
---|
178 | * the block is used. |
---|
179 | * |
---|
180 | * This field is only valid if the block is free and thus part of the free |
---|
181 | * block list. |
---|
182 | */ |
---|
183 | struct Heap_Block *next; |
---|
184 | |
---|
185 | /** |
---|
186 | * @brief Pointer to the previous free block or part of the allocated area. |
---|
187 | * |
---|
188 | * This field is only valid if the block is free and thus part of the free |
---|
189 | * block list. |
---|
190 | */ |
---|
191 | struct Heap_Block *prev; |
---|
192 | } Heap_Block; |
---|
193 | |
---|
194 | /** |
---|
195 | * @brief Run-time heap statistics. |
---|
196 | * |
---|
197 | * The value @a searches / @a allocs gives the mean number of searches per |
---|
198 | * allocation, while @a max_search gives maximum number of searches ever |
---|
199 | * performed on a single allocation call. |
---|
200 | */ |
---|
201 | typedef struct { |
---|
202 | /** |
---|
203 | * @brief Instance number of this heap. |
---|
204 | */ |
---|
205 | uint32_t instance; |
---|
206 | |
---|
207 | /** |
---|
208 | * @brief The size of the memory for heap. |
---|
209 | */ |
---|
210 | uintptr_t size; |
---|
211 | |
---|
212 | /** |
---|
213 | * @brief Current free size. |
---|
214 | */ |
---|
215 | uintptr_t free_size; |
---|
216 | |
---|
217 | /** |
---|
218 | * @brief Minimum free size ever. |
---|
219 | */ |
---|
220 | uintptr_t min_free_size; |
---|
221 | |
---|
222 | /** |
---|
223 | * @brief Current number of free blocks. |
---|
224 | */ |
---|
225 | uint32_t free_blocks; |
---|
226 | |
---|
227 | /** |
---|
228 | * @brief Maximum number of free blocks ever. |
---|
229 | */ |
---|
230 | uint32_t max_free_blocks; |
---|
231 | |
---|
232 | /** |
---|
233 | * @brief Current number of used blocks. |
---|
234 | */ |
---|
235 | uint32_t used_blocks; |
---|
236 | |
---|
237 | /** |
---|
238 | * @brief Maximum number of blocks searched ever. |
---|
239 | */ |
---|
240 | uint32_t max_search; |
---|
241 | |
---|
242 | /** |
---|
243 | * @brief Total number of successful calls to alloc. |
---|
244 | */ |
---|
245 | uint32_t allocs; |
---|
246 | |
---|
247 | /** |
---|
248 | * @brief Total number of searches ever. |
---|
249 | */ |
---|
250 | uint32_t searches; |
---|
251 | |
---|
252 | /** |
---|
253 | * @brief Total number of suceessful calls to free. |
---|
254 | */ |
---|
255 | uint32_t frees; |
---|
256 | |
---|
257 | /** |
---|
258 | * @brief Total number of successful resizes. |
---|
259 | */ |
---|
260 | uint32_t resizes; |
---|
261 | } Heap_Statistics; |
---|
262 | |
---|
263 | /** |
---|
264 | * @brief Control block used to manage a heap. |
---|
265 | */ |
---|
266 | typedef struct { |
---|
267 | Heap_Block free_list; |
---|
268 | uintptr_t page_size; |
---|
269 | uintptr_t min_block_size; |
---|
270 | uintptr_t area_begin; |
---|
271 | uintptr_t area_end; |
---|
272 | Heap_Block *first_block; |
---|
273 | Heap_Block *last_block; |
---|
274 | Heap_Statistics stats; |
---|
275 | } Heap_Control; |
---|
276 | |
---|
277 | /** |
---|
278 | * @brief Information about blocks. |
---|
279 | */ |
---|
280 | typedef struct { |
---|
281 | /** |
---|
282 | * @brief Number of blocks of this type. |
---|
283 | */ |
---|
284 | uint32_t number; |
---|
285 | |
---|
286 | /** |
---|
287 | * @brief Largest block of this type. |
---|
288 | */ |
---|
289 | uint32_t largest; |
---|
290 | |
---|
291 | /** |
---|
292 | * @brief Total size of the blocks of this type. |
---|
293 | */ |
---|
294 | uint32_t total; |
---|
295 | } Heap_Information; |
---|
296 | |
---|
297 | /** |
---|
298 | * @brief Information block returned by _Heap_Get_information(). |
---|
299 | */ |
---|
300 | typedef struct { |
---|
301 | Heap_Information Free; |
---|
302 | Heap_Information Used; |
---|
303 | } Heap_Information_block; |
---|
304 | |
---|
305 | /** |
---|
306 | * @brief See _Heap_Extend(). |
---|
307 | */ |
---|
308 | typedef enum { |
---|
309 | HEAP_EXTEND_SUCCESSFUL, |
---|
310 | HEAP_EXTEND_ERROR, |
---|
311 | HEAP_EXTEND_NOT_IMPLEMENTED |
---|
312 | } Heap_Extend_status; |
---|
313 | |
---|
314 | /** |
---|
315 | * @brief See _Heap_Resize_block(). |
---|
316 | */ |
---|
317 | typedef enum { |
---|
318 | HEAP_RESIZE_SUCCESSFUL, |
---|
319 | HEAP_RESIZE_UNSATISFIED, |
---|
320 | HEAP_RESIZE_FATAL_ERROR |
---|
321 | } Heap_Resize_status; |
---|
322 | |
---|
323 | /** |
---|
324 | * @brief Initializes the heap control block @a heap to manage the area |
---|
325 | * starting at @a area_begin of size @a area_size bytes. |
---|
326 | * |
---|
327 | * Blocks of memory are allocated from the heap in multiples of @a page_size |
---|
328 | * byte units. If the @a page_size is equal to zero or is not multiple of |
---|
329 | * @c CPU_ALIGNMENT, it is aligned up to the nearest @c CPU_ALIGNMENT boundary. |
---|
330 | * |
---|
331 | * Returns the maximum memory available, or zero in case of failure. |
---|
332 | */ |
---|
333 | uintptr_t _Heap_Initialize( |
---|
334 | Heap_Control *heap, |
---|
335 | void *area_begin, |
---|
336 | uintptr_t area_size, |
---|
337 | uintptr_t page_size |
---|
338 | ); |
---|
339 | |
---|
340 | /** |
---|
341 | * @brief Extends the memory area of the heap @a heap using the memory area |
---|
342 | * starting at @a area_begin of size @a area_size bytes. |
---|
343 | * |
---|
344 | * The extended space available for allocation will be returned in |
---|
345 | * @a amount_extended. |
---|
346 | * |
---|
347 | * The memory area must start at the end of the currently used memory area. |
---|
348 | */ |
---|
349 | Heap_Extend_status _Heap_Extend( |
---|
350 | Heap_Control *heap, |
---|
351 | void *area_begin, |
---|
352 | uintptr_t area_size, |
---|
353 | uintptr_t *amount_extended |
---|
354 | ); |
---|
355 | |
---|
356 | /** |
---|
357 | * @brief Allocates a memory area of size @a size bytes. |
---|
358 | * |
---|
359 | * If the alignment parameter @a alignment is not equal to zero, the allocated |
---|
360 | * memory area will begin at an address aligned by this value. |
---|
361 | * |
---|
362 | * If the boundary parameter @a boundary is not equal to zero, the allocated |
---|
363 | * memory area will fulfill a boundary constraint. The boudnary value |
---|
364 | * specifies the set of addresses which are aligned by the boundary value. The |
---|
365 | * interior of the allocated memory area will not contain an element of this |
---|
366 | * set. The begin or end address of the area may be a member of the set. |
---|
367 | * |
---|
368 | * A size value of zero will return a unique address which may be freed with |
---|
369 | * _Heap_Free(). |
---|
370 | * |
---|
371 | * Returns a pointer to the begin of the allocated memory area, or @c NULL if |
---|
372 | * no memory is available or the parameters are inconsistent. |
---|
373 | */ |
---|
374 | void *_Heap_Allocate_aligned_with_boundary( |
---|
375 | Heap_Control *heap, |
---|
376 | uintptr_t size, |
---|
377 | uintptr_t alignment, |
---|
378 | uintptr_t boundary |
---|
379 | ); |
---|
380 | |
---|
381 | #define _Heap_Allocate_aligned( heap, size, alignment ) \ |
---|
382 | _Heap_Allocate_aligned_with_boundary( heap, size, alignment, 0 ) |
---|
383 | |
---|
384 | #define _Heap_Allocate( heap, size ) \ |
---|
385 | _Heap_Allocate_aligned_with_boundary( heap, size, 0, 0 ) |
---|
386 | |
---|
387 | /** |
---|
388 | * @brief Frees the allocated memory area starting at @a addr in the heap |
---|
389 | * @a heap. |
---|
390 | * |
---|
391 | * Inappropriate values for @a addr may corrupt the heap. |
---|
392 | * |
---|
393 | * Returns @c true in case of success, and @c false otherwise. |
---|
394 | */ |
---|
395 | bool _Heap_Free( Heap_Control *heap, void *addr ); |
---|
396 | |
---|
397 | /** |
---|
398 | * @brief Walks the heap @a heap to verify its integrity. |
---|
399 | * |
---|
400 | * If @a dump is @c true, then diagnostic messages will be printed to standard |
---|
401 | * output. In this case @a source is used to mark the output lines. |
---|
402 | * |
---|
403 | * Returns @c true if no errors occured, and @c false if the heap is corrupt. |
---|
404 | */ |
---|
405 | bool _Heap_Walk( |
---|
406 | Heap_Control *heap, |
---|
407 | int source, |
---|
408 | bool dump |
---|
409 | ); |
---|
410 | |
---|
411 | /** |
---|
412 | * @brief Returns information about used and free blocks for the heap @a heap |
---|
413 | * in @a info. |
---|
414 | */ |
---|
415 | void _Heap_Get_information( |
---|
416 | Heap_Control *heap, |
---|
417 | Heap_Information_block *info |
---|
418 | ); |
---|
419 | |
---|
420 | /** |
---|
421 | * @brief Returns information about free blocks for the heap @a heap in |
---|
422 | * @a info. |
---|
423 | */ |
---|
424 | void _Heap_Get_free_information( |
---|
425 | Heap_Control *heap, |
---|
426 | Heap_Information *info |
---|
427 | ); |
---|
428 | |
---|
429 | /** |
---|
430 | * @brief Returns the size of the allocatable memory area starting at @a addr |
---|
431 | * in @a size. |
---|
432 | * |
---|
433 | * The size value may be greater than the initially requested size in |
---|
434 | * _Heap_Allocate_aligned_with_boundary(). |
---|
435 | * |
---|
436 | * Inappropriate values for @a addr will not corrupt the heap, but may yield |
---|
437 | * invalid size values. |
---|
438 | * |
---|
439 | * Returns @a true if successful, and @c false otherwise. |
---|
440 | */ |
---|
441 | bool _Heap_Size_of_alloc_area( |
---|
442 | Heap_Control *heap, |
---|
443 | void *addr, |
---|
444 | uintptr_t *size |
---|
445 | ); |
---|
446 | |
---|
447 | /** |
---|
448 | * @brief Resizes the block of the allocated memory area starting at @a addr. |
---|
449 | * |
---|
450 | * The new memory area will have a size of at least @a size bytes. A resize |
---|
451 | * may be impossible and depends on the current heap usage. |
---|
452 | * |
---|
453 | * The size available for allocation in the current block before the resize |
---|
454 | * will be returned in @a old_size. The size available for allocation in |
---|
455 | * the resized block will be returned in @a new_size. If the resize was not |
---|
456 | * successful, then a value of zero will be returned in @a new_size. |
---|
457 | * |
---|
458 | * Inappropriate values for @a addr may corrupt the heap. |
---|
459 | */ |
---|
460 | Heap_Resize_status _Heap_Resize_block( |
---|
461 | Heap_Control *heap, |
---|
462 | void *addr, |
---|
463 | uintptr_t size, |
---|
464 | uintptr_t *old_size, |
---|
465 | uintptr_t *new_size |
---|
466 | ); |
---|
467 | |
---|
468 | #if !defined(__RTEMS_APPLICATION__) |
---|
469 | |
---|
470 | #include <rtems/score/heap.inl> |
---|
471 | |
---|
472 | /** |
---|
473 | * @brief Allocates the memory area starting at @a alloc_begin of size |
---|
474 | * @a alloc_size bytes in the block @a block. |
---|
475 | * |
---|
476 | * The block may be split up into multiple blocks. |
---|
477 | * |
---|
478 | * Inappropriate values for @a alloc_begin or @a alloc_size may corrupt the |
---|
479 | * heap. |
---|
480 | * |
---|
481 | * Returns the block containing the allocated memory area. |
---|
482 | */ |
---|
483 | Heap_Block *_Heap_Block_allocate( |
---|
484 | Heap_Control *heap, |
---|
485 | Heap_Block *block, |
---|
486 | uintptr_t alloc_begin, |
---|
487 | uintptr_t alloc_size |
---|
488 | ); |
---|
489 | |
---|
490 | /** @} */ |
---|
491 | |
---|
492 | #ifdef RTEMS_DEBUG |
---|
493 | #define RTEMS_HEAP_DEBUG |
---|
494 | #define RTEMS_MALLOC_BOUNDARY_HELPERS |
---|
495 | #endif |
---|
496 | |
---|
497 | #ifdef RTEMS_HEAP_DEBUG |
---|
498 | #include <assert.h> |
---|
499 | #define _HAssert( cond ) \ |
---|
500 | do { \ |
---|
501 | if ( !(cond) ) { \ |
---|
502 | __assert( __FILE__, __LINE__, #cond ); \ |
---|
503 | } \ |
---|
504 | } while (0) |
---|
505 | #else |
---|
506 | #define _HAssert( cond ) ((void) 0) |
---|
507 | #endif |
---|
508 | |
---|
509 | #endif /* !defined(__RTEMS_APPLICATION__) */ |
---|
510 | |
---|
511 | #ifdef __cplusplus |
---|
512 | } |
---|
513 | #endif |
---|
514 | |
---|
515 | #endif |
---|
516 | /* end of include file */ |
---|