RTEMS
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
25 extern "C" {
26 #endif
27 
28 #ifdef RTEMS_DEBUG
29  #define HEAP_PROTECTION
30 #endif
31 
133 typedef struct Heap_Control Heap_Control;
134 
135 typedef struct Heap_Block Heap_Block;
136 
142 typedef enum {
147 
153 
158 
164 
170 
176 typedef struct {
181 
186 
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 
305 
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 
353  Heap_Control *heap,
354  void *area_begin,
355  uintptr_t area_size,
356  uintptr_t page_size_or_unused
357 );
358 
379 uintptr_t _Heap_Extend(
380  Heap_Control *heap,
381  void *area_begin,
382  uintptr_t area_size,
383  uintptr_t unused
384 );
385 
398 uintptr_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 
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 
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 
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 */
Run-time heap statistics.
Definition: heapinfo.h:40
There is was an attempt to free the same block twice.
Definition: heap.h:157
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
uintptr_t _Heap_Extend(Heap_Control *heap, void *area_begin, uintptr_t area_size, uintptr_t unused)
Extends the memory available for the heap.
The next block of a supposed to be used block does not indicate that the block is used...
Definition: heap.h:163
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
There is an unexpected value in the heap block protector area.
Definition: heap.h:146
Heap area structure for table based heap initialization and extension.
Definition: heap.h:338
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
Description for free or used blocks.
Definition: heap.h:256
A supposed to be free block is not inside the heap memory area.
Definition: heap.h:168
Heap Handler Information API.
#define CPU_ALIGNMENT
Definition: cpu.h:742
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.
Control block used to manage a heap.
Definition: heap.h:318
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
SPARC CPU Department Source.
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_Error_reason reason
The heap error reason.
Definition: heap.h:190
Heap_Error_reason
The heap error reason.
Definition: heap.h:142
Heap_Block * next
Pointer to the next free block or part of the allocated area.
Definition: heap.h:304
Context of a heap error.
Definition: heap.h:176
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 * block
The heap block causing the error.
Definition: heap.h:185
Heap_Control * heap
The heap of the block.
Definition: heap.h:180
#define RTEMS_INLINE_ROUTINE
Gives a hint to the compiler in a function declaration to inline this function.
Definition: basedefs.h:683
Heap_Block * prev
Pointer to the previous free block or part of the allocated area.
Definition: heap.h:312
#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
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
There is an unexpected value in the free pattern of a free heap block.
Definition: heap.h:152