RTEMS: cpukit/include/rtems/score/heap.h Source File

| RTEMS 5.4-rc2

Loading...
Searching...
No Matches
heap.h
Go to the documentation of this file.
1
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.org/license/LICENSE.
16 */
17
18#ifndef _RTEMS_SCORE_HEAP_H
19#define _RTEMS_SCORE_HEAP_H
20
21#include <rtems/score/cpu.h>
22#include <rtems/score/heapinfo.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#ifdef RTEMS_DEBUG
29 #define HEAP_PROTECTION
30#endif
31
133typedef struct Heap_Control Heap_Control;
134
135typedef struct Heap_Block Heap_Block;
136
142 typedef enum {
146 HEAP_ERROR_BROKEN_PROTECTOR,
147
152 HEAP_ERROR_FREE_PATTERN,
153
157 HEAP_ERROR_DOUBLE_FREE,
158
163 HEAP_ERROR_BAD_USED_BLOCK,
164
168 HEAP_ERROR_BAD_FREE_BLOCK
169} Heap_Error_reason;
170
176 typedef struct {
180 Heap_Control *heap;
181
185 Heap_Block *block;
186
190 Heap_Error_reason reason;
191} Heap_Error_context;
192
193#ifndef HEAP_PROTECTION
194 #define HEAP_PROTECTION_HEADER_SIZE 0
195#else
196 #define HEAP_PROTECTOR_COUNT 2
197
198 #define HEAP_BEGIN_PROTECTOR_0 ((uintptr_t) 0xfd75a98f)
199 #define HEAP_BEGIN_PROTECTOR_1 ((uintptr_t) 0xbfa1f177)
200 #define HEAP_END_PROTECTOR_0 ((uintptr_t) 0xd6b8855e)
201 #define HEAP_END_PROTECTOR_1 ((uintptr_t) 0x13a44a5b)
202
203 #define HEAP_FREE_PATTERN ((uintptr_t) 0xe7093cdf)
204
205 #define HEAP_PROTECTION_OBOLUS ((Heap_Block *) 1)
206
207 typedef void (*_Heap_Protection_handler)(
208 Heap_Control *heap,
209 Heap_Block *block
210 );
211
212 typedef void (*_Heap_Protection_error_handler)(
213 Heap_Control *heap,
214 Heap_Block *block,
215 Heap_Error_reason reason
216 );
217
218 typedef struct {
219 _Heap_Protection_handler block_initialize;
220 _Heap_Protection_handler block_check;
221 _Heap_Protection_error_handler block_error;
222 void *handler_data;
223 Heap_Block *first_delayed_free_block;
224 Heap_Block *last_delayed_free_block;
225 uintptr_t delayed_free_block_count;
226 uintptr_t delayed_free_fraction;
227 } Heap_Protection;
228
229 struct _Thread_Control;
230
231 typedef struct {
232 uintptr_t protector [HEAP_PROTECTOR_COUNT];
233 Heap_Block *next_delayed_free_block;
234 struct _Thread_Control *task;
235 void *tag;
236 } Heap_Protection_block_begin;
237
238 typedef struct {
239 uintptr_t protector [HEAP_PROTECTOR_COUNT];
240 } Heap_Protection_block_end;
241
242 #define HEAP_PROTECTION_HEADER_SIZE \
243 (sizeof(Heap_Protection_block_begin) + sizeof(Heap_Protection_block_end))
244#endif
245
250#define HEAP_BLOCK_HEADER_SIZE \
251 (2 * sizeof(uintptr_t) + HEAP_PROTECTION_HEADER_SIZE)
252
256 struct Heap_Block {
270 uintptr_t prev_size;
271
272 #ifdef HEAP_PROTECTION
273 Heap_Protection_block_begin Protection_begin;
274 #endif
275
289 uintptr_t size_and_flag;
290
291 #ifdef HEAP_PROTECTION
292 Heap_Protection_block_end Protection_end;
293 #endif
294
304 Heap_Block *next;
305
312 Heap_Block *prev;
313};
314
318 struct Heap_Control {
319 Heap_Block free_list;
320 uintptr_t page_size;
321 uintptr_t min_block_size;
322 uintptr_t area_begin;
323 uintptr_t area_end;
324 Heap_Block *first_block;
325 Heap_Block *last_block;
326 Heap_Statistics stats;
327 #ifdef HEAP_PROTECTION
328 Heap_Protection Protection;
329 #endif
330};
331
338 typedef struct {
339 void *begin;
340 uintptr_t size;
341} Heap_Area;
342
352 typedef uintptr_t (*Heap_Initialization_or_extend_handler)(
353 Heap_Control *heap,
354 void *area_begin,
355 uintptr_t area_size,
356 uintptr_t page_size_or_unused
357);
358
379uintptr_t _Heap_Extend(
380 Heap_Control *heap,
381 void *area_begin,
382 uintptr_t area_size,
383 uintptr_t unused
384);
385
398uintptr_t _Heap_No_extend(
399 Heap_Control *unused_0,
400 void *unused_1,
401 uintptr_t unused_2,
402 uintptr_t unused_3
403);
404
413 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Align_up(
414 uintptr_t value,
415 uintptr_t alignment
416)
417{
418 uintptr_t remainder = value % alignment;
419
420 if ( remainder != 0 ) {
421 return value - remainder + alignment;
422 } else {
423 return value;
424 }
425}
426
434 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Min_block_size( uintptr_t page_size )
435{
436 return _Heap_Align_up( sizeof( Heap_Block ), page_size );
437}
438
446 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Area_overhead(
447 uintptr_t page_size
448)
449{
450 if ( page_size != 0 ) {
451 page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
452 } else {
453 page_size = CPU_ALIGNMENT;
454 }
455
456 return 2 * (page_size - 1) + HEAP_BLOCK_HEADER_SIZE;
457}
458
469 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Size_with_overhead(
470 uintptr_t page_size,
471 uintptr_t size,
472 uintptr_t alignment
473)
474{
475 if ( page_size != 0 ) {
476 page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
477 } else {
478 page_size = CPU_ALIGNMENT;
479 }
480
481 if ( page_size < alignment ) {
482 page_size = alignment;
483 }
484
485 return HEAP_BLOCK_HEADER_SIZE + page_size - 1 + size;
486}
487
490#ifdef __cplusplus
491}
492#endif
493
494#endif
495/* end of include file */
RTEMS_INLINE_ROUTINE
#define RTEMS_INLINE_ROUTINE
Definition: basedefs.h:66
_Heap_Size_with_overhead
RTEMS_INLINE_ROUTINE uintptr_t _Heap_Size_with_overhead(uintptr_t page_size, uintptr_t size, uintptr_t alignment)
Returns the size with administration and alignment overhead for one allocation.
Definition: heap.h:469
_Heap_No_extend
uintptr_t _Heap_No_extend(Heap_Control *unused_0, void *unused_1, uintptr_t unused_2, uintptr_t unused_3)
This function returns always zero.
HEAP_BLOCK_HEADER_SIZE
#define HEAP_BLOCK_HEADER_SIZE
The block header consists of the two size fields (Heap_Block::prev_size and Heap_Block::size_and_flag...
Definition: heap.h:250
_Heap_Min_block_size
RTEMS_INLINE_ROUTINE uintptr_t _Heap_Min_block_size(uintptr_t page_size)
Returns the minimal Heap Block size for the given page_size.
Definition: heap.h:434
_Heap_Extend
uintptr_t _Heap_Extend(Heap_Control *heap, void *area_begin, uintptr_t area_size, uintptr_t unused)
Extends the memory available for the heap.
Definition: heapextend.c:119
Heap_Initialization_or_extend_handler
uintptr_t(* Heap_Initialization_or_extend_handler)(Heap_Control *heap, void *area_begin, uintptr_t area_size, uintptr_t page_size_or_unused)
Heap initialization and extend handler type.
Definition: heap.h:352
Heap_Error_reason
Heap_Error_reason
The heap error reason.
Definition: heap.h:142
_Heap_Area_overhead
RTEMS_INLINE_ROUTINE uintptr_t _Heap_Area_overhead(uintptr_t page_size)
Returns the worst case overhead to manage a memory area.
Definition: heap.h:446
_Heap_Align_up
RTEMS_INLINE_ROUTINE uintptr_t _Heap_Align_up(uintptr_t value, uintptr_t alignment)
Aligns the value to a given alignment, rounding up.
Definition: heap.h:413
HEAP_ERROR_BAD_USED_BLOCK
@ HEAP_ERROR_BAD_USED_BLOCK
The next block of a supposed to be used block does not indicate that the block is used.
Definition: heap.h:163
HEAP_ERROR_BROKEN_PROTECTOR
@ HEAP_ERROR_BROKEN_PROTECTOR
There is an unexpected value in the heap block protector area.
Definition: heap.h:146
HEAP_ERROR_DOUBLE_FREE
@ HEAP_ERROR_DOUBLE_FREE
There is was an attempt to free the same block twice.
Definition: heap.h:157
HEAP_ERROR_BAD_FREE_BLOCK
@ HEAP_ERROR_BAD_FREE_BLOCK
A supposed to be free block is not inside the heap memory area.
Definition: heap.h:168
HEAP_ERROR_FREE_PATTERN
@ HEAP_ERROR_FREE_PATTERN
There is an unexpected value in the free pattern of a free heap block.
Definition: heap.h:152
heapinfo.h
Heap Handler Information API.
Heap_Area
Heap area structure for table based heap initialization and extension.
Definition: heap.h:338
Heap_Block
Description for free or used blocks.
Definition: heap.h:256
Heap_Block::prev
Heap_Block * prev
Pointer to the previous free block or part of the allocated area.
Definition: heap.h:312
Heap_Block::prev_size
uintptr_t prev_size
Size of the previous block or part of the allocated area of the previous block.
Definition: heap.h:270
Heap_Block::size_and_flag
uintptr_t size_and_flag
Contains the size of the current block and a flag which indicates if the previous block is free or us...
Definition: heap.h:289
Heap_Block::next
Heap_Block * next
Pointer to the next free block or part of the allocated area.
Definition: heap.h:304
Heap_Control
Control block used to manage a heap.
Definition: heap.h:318
Heap_Error_context
Context of a heap error.
Definition: heap.h:176
Heap_Error_context::reason
Heap_Error_reason reason
The heap error reason.
Definition: heap.h:190
Heap_Error_context::block
Heap_Block * block
The heap block causing the error.
Definition: heap.h:185
Heap_Error_context::heap
Heap_Control * heap
The heap of the block.
Definition: heap.h:180
Heap_Statistics
Run-time heap statistics.
Definition: heapinfo.h:40
_Thread_Control
Definition: thread.h:732
free_list
Definition: intercom.c:58