RTEMS CPU Kit with SuperCore  4.11.2
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/thread.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #ifdef RTEMS_DEBUG
29  #define HEAP_PROTECTION
30 #endif
31 
132 typedef struct Heap_Control Heap_Control;
133 
134 typedef struct Heap_Block Heap_Block;
135 
136 #ifndef HEAP_PROTECTION
137  #define HEAP_PROTECTION_HEADER_SIZE 0
138 #else
139  #define HEAP_PROTECTOR_COUNT 2
140 
141  #define HEAP_BEGIN_PROTECTOR_0 ((uintptr_t) 0xfd75a98f)
142  #define HEAP_BEGIN_PROTECTOR_1 ((uintptr_t) 0xbfa1f177)
143  #define HEAP_END_PROTECTOR_0 ((uintptr_t) 0xd6b8855e)
144  #define HEAP_END_PROTECTOR_1 ((uintptr_t) 0x13a44a5b)
145 
146  #define HEAP_FREE_PATTERN ((uintptr_t) 0xe7093cdf)
147 
148  #define HEAP_PROTECTION_OBOLUS ((Heap_Block *) 1)
149 
150  typedef void (*_Heap_Protection_handler)(
151  Heap_Control *heap,
152  Heap_Block *block
153  );
154 
155  typedef struct {
156  _Heap_Protection_handler block_initialize;
157  _Heap_Protection_handler block_check;
158  _Heap_Protection_handler block_error;
159  void *handler_data;
160  Heap_Block *first_delayed_free_block;
161  Heap_Block *last_delayed_free_block;
162  uintptr_t delayed_free_block_count;
163  uintptr_t delayed_free_fraction;
164  } Heap_Protection;
165 
166  typedef struct {
167  uintptr_t protector [HEAP_PROTECTOR_COUNT];
168  Heap_Block *next_delayed_free_block;
169  Thread_Control *task;
170  void *tag;
171  } Heap_Protection_block_begin;
172 
173  typedef struct {
174  uintptr_t protector [HEAP_PROTECTOR_COUNT];
175  } Heap_Protection_block_end;
176 
177  #define HEAP_PROTECTION_HEADER_SIZE \
178  (sizeof(Heap_Protection_block_begin) + sizeof(Heap_Protection_block_end))
179 #endif
180 
185 #define HEAP_BLOCK_HEADER_SIZE \
186  (2 * sizeof(uintptr_t) + HEAP_PROTECTION_HEADER_SIZE)
187 
191 struct Heap_Block {
205  uintptr_t prev_size;
206 
207  #ifdef HEAP_PROTECTION
208  Heap_Protection_block_begin Protection_begin;
209  #endif
210 
224  uintptr_t size_and_flag;
225 
226  #ifdef HEAP_PROTECTION
227  Heap_Protection_block_end Protection_end;
228  #endif
229 
240 
248 };
249 
257 typedef struct {
264 
270  uint64_t lifetime_freed;
271 
277  uintptr_t size;
278 
284  uintptr_t free_size;
285 
291  uintptr_t min_free_size;
292 
296  uint32_t free_blocks;
297 
301  uint32_t max_free_blocks;
302 
306  uint32_t used_blocks;
307 
311  uint32_t max_search;
312 
316  uint32_t searches;
317 
321  uint32_t allocs;
322 
326  uint32_t failed_allocs;
327 
331  uint32_t frees;
332 
336  uint32_t resizes;
338 
342 struct Heap_Control {
343  Heap_Block free_list;
344  uintptr_t page_size;
345  uintptr_t min_block_size;
346  uintptr_t area_begin;
347  uintptr_t area_end;
348  Heap_Block *first_block;
349  Heap_Block *last_block;
350  Heap_Statistics stats;
351  #ifdef HEAP_PROTECTION
352  Heap_Protection Protection;
353  #endif
354 };
355 
359 typedef struct {
363  uint32_t number;
364 
368  uint32_t largest;
369 
373  uint32_t total;
375 
379 typedef struct {
380  Heap_Information Free;
381  Heap_Information Used;
382  Heap_Statistics Stats;
384 
391 typedef struct {
392  void *begin;
393  uintptr_t size;
394 } Heap_Area;
395 
406  Heap_Control *heap,
407  void *area_begin,
408  uintptr_t area_size,
409  uintptr_t page_size_or_unused
410 );
411 
429 uintptr_t _Heap_Extend(
430  Heap_Control *heap,
431  void *area_begin,
432  uintptr_t area_size,
433  uintptr_t unused
434 );
435 
445 uintptr_t _Heap_No_extend(
446  Heap_Control *unused_0,
447  void *unused_1,
448  uintptr_t unused_2,
449  uintptr_t unused_3
450 );
451 
452 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Align_up(
453  uintptr_t value,
454  uintptr_t alignment
455 )
456 {
457  uintptr_t remainder = value % alignment;
458 
459  if ( remainder != 0 ) {
460  return value - remainder + alignment;
461  } else {
462  return value;
463  }
464 }
465 
466 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Min_block_size( uintptr_t page_size )
467 {
468  return _Heap_Align_up( sizeof( Heap_Block ), page_size );
469 }
470 
475  uintptr_t page_size
476 )
477 {
478  if ( page_size != 0 ) {
479  page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
480  } else {
481  page_size = CPU_ALIGNMENT;
482  }
483 
484  return 2 * (page_size - 1) + HEAP_BLOCK_HEADER_SIZE;
485 }
486 
492  uintptr_t page_size,
493  uintptr_t size,
494  uintptr_t alignment
495 )
496 {
497  if ( page_size != 0 ) {
498  page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
499  } else {
500  page_size = CPU_ALIGNMENT;
501  }
502 
503  if ( page_size < alignment ) {
504  page_size = alignment;
505  }
506 
507  return HEAP_BLOCK_HEADER_SIZE + page_size - 1 + size;
508 }
509 
512 #ifdef __cplusplus
513 }
514 #endif
515 
516 #endif
517 /* end of include file */
Run-time heap statistics.
Definition: heap.h:257
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:405
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:224
uint64_t lifetime_freed
Lifetime number of bytes freed to this heap.
Definition: heap.h:270
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.
Definition: heapnoextend.c:29
#define RTEMS_INLINE_ROUTINE
The following (in conjunction with compiler arguments) are used to choose between the use of static i...
Definition: basedefs.h:135
uint32_t frees
Total number of successful frees.
Definition: heap.h:331
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:474
uint32_t largest
Largest block of this type.
Definition: heap.h:368
Heap area structure for table based heap initialization and extension.
Definition: heap.h:391
Information block returned by _Heap_Get_information().
Definition: heap.h:379
uint32_t allocs
Total number of successful allocations.
Definition: heap.h:321
uint32_t number
Number of blocks of this type.
Definition: heap.h:363
Constants and Structures Related with the Thread Control Block.
uint64_t lifetime_allocated
Lifetime number of bytes allocated from this heap.
Definition: heap.h:263
Description for free or used blocks.
Definition: heap.h:191
uint32_t failed_allocs
Total number of failed allocations.
Definition: heap.h:326
uint32_t max_free_blocks
Maximum number of free blocks ever.
Definition: heap.h:301
#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:185
uint32_t used_blocks
Current number of used blocks.
Definition: heap.h:306
This structure defines the Thread Control Block (TCB).
Definition: thread.h:671
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:491
uint32_t resizes
Total number of successful resizes.
Definition: heap.h:336
Control block used to manage a heap.
Definition: heap.h:342
uintptr_t free_size
Current free size in bytes.
Definition: heap.h:284
uint32_t total
Total size of the blocks of this type.
Definition: heap.h:373
Information about blocks.
Definition: heap.h:359
uintptr_t size
Size of the allocatable area in bytes.
Definition: heap.h:277
uintptr_t min_free_size
Minimum free size ever in bytes.
Definition: heap.h:291
Heap_Block * next
Pointer to the next free block or part of the allocated area.
Definition: heap.h:239
uint32_t max_search
Maximum number of blocks searched ever.
Definition: heap.h:311
uintptr_t prev_size
Size of the previous block or part of the allocated area of the previous block.
Definition: heap.h:205
uint32_t free_blocks
Current number of free blocks.
Definition: heap.h:296
uint32_t searches
Total number of searches.
Definition: heap.h:316
Heap_Block * prev
Pointer to the previous free block or part of the allocated area.
Definition: heap.h:247
uintptr_t _Heap_Extend(Heap_Control *heap, void *area_begin, uintptr_t area_size, uintptr_t unused)
Extends the memory available for the heap heap using the memory area starting at area_begin of size a...
Definition: heapextend.c:120