RTEMS  5.0.0
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 
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  struct _Thread_Control;
167 
168  typedef struct {
169  uintptr_t protector [HEAP_PROTECTOR_COUNT];
170  Heap_Block *next_delayed_free_block;
171  struct _Thread_Control *task;
172  void *tag;
173  } Heap_Protection_block_begin;
174 
175  typedef struct {
176  uintptr_t protector [HEAP_PROTECTOR_COUNT];
177  } Heap_Protection_block_end;
178 
179  #define HEAP_PROTECTION_HEADER_SIZE \
180  (sizeof(Heap_Protection_block_begin) + sizeof(Heap_Protection_block_end))
181 #endif
182 
187 #define HEAP_BLOCK_HEADER_SIZE \
188  (2 * sizeof(uintptr_t) + HEAP_PROTECTION_HEADER_SIZE)
189 
193 struct Heap_Block {
207  uintptr_t prev_size;
208 
209  #ifdef HEAP_PROTECTION
210  Heap_Protection_block_begin Protection_begin;
211  #endif
212 
226  uintptr_t size_and_flag;
227 
228  #ifdef HEAP_PROTECTION
229  Heap_Protection_block_end Protection_end;
230  #endif
231 
242 
250 };
251 
255 struct Heap_Control {
257  uintptr_t page_size;
258  uintptr_t min_block_size;
259  uintptr_t area_begin;
260  uintptr_t area_end;
261  Heap_Block *first_block;
262  Heap_Block *last_block;
263  Heap_Statistics stats;
264  #ifdef HEAP_PROTECTION
265  Heap_Protection Protection;
266  #endif
267 };
268 
275 typedef struct {
276  void *begin;
277  uintptr_t size;
278 } Heap_Area;
279 
290  Heap_Control *heap,
291  void *area_begin,
292  uintptr_t area_size,
293  uintptr_t page_size_or_unused
294 );
295 
314 uintptr_t _Heap_Extend(
315  Heap_Control *heap,
316  void *area_begin,
317  uintptr_t area_size,
318  uintptr_t unused
319 );
320 
330 uintptr_t _Heap_No_extend(
331  Heap_Control *unused_0,
332  void *unused_1,
333  uintptr_t unused_2,
334  uintptr_t unused_3
335 );
336 
337 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Align_up(
338  uintptr_t value,
339  uintptr_t alignment
340 )
341 {
342  uintptr_t remainder = value % alignment;
343 
344  if ( remainder != 0 ) {
345  return value - remainder + alignment;
346  } else {
347  return value;
348  }
349 }
350 
351 RTEMS_INLINE_ROUTINE uintptr_t _Heap_Min_block_size( uintptr_t page_size )
352 {
353  return _Heap_Align_up( sizeof( Heap_Block ), page_size );
354 }
355 
360  uintptr_t page_size
361 )
362 {
363  if ( page_size != 0 ) {
364  page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
365  } else {
366  page_size = CPU_ALIGNMENT;
367  }
368 
369  return 2 * (page_size - 1) + HEAP_BLOCK_HEADER_SIZE;
370 }
371 
377  uintptr_t page_size,
378  uintptr_t size,
379  uintptr_t alignment
380 )
381 {
382  if ( page_size != 0 ) {
383  page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
384  } else {
385  page_size = CPU_ALIGNMENT;
386  }
387 
388  if ( page_size < alignment ) {
389  page_size = alignment;
390  }
391 
392  return HEAP_BLOCK_HEADER_SIZE + page_size - 1 + size;
393 }
394 
397 #ifdef __cplusplus
398 }
399 #endif
400 
401 #endif
402 /* end of include file */
Run-time heap statistics.
Definition: heapinfo.h:40
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:289
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:226
Definition: intercom.c:58
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.
#define RTEMS_INLINE_ROUTINE
Definition: basedefs.h:65
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:359
Heap area structure for table based heap initialization and extension.
Definition: heap.h:275
Description for free or used blocks.
Definition: heap.h:193
#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:187
Definition: thread.h:728
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:376
Heap Handler Information API.
Control block used to manage a heap.
Definition: heap.h:255
Heap_Block * next
Pointer to the next free block or part of the allocated area.
Definition: heap.h:241
uintptr_t prev_size
Size of the previous block or part of the allocated area of the previous block.
Definition: heap.h:207
unsigned size
Definition: tte.h:74
Heap_Block * prev
Pointer to the previous free block or part of the allocated area.
Definition: heap.h:249
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:119