source: rtems/cpukit/include/rtems/score/heap.h @ 805f9c26

5
Last change on this file since 805f9c26 was 805f9c26, checked in by Sebastian Huber <sebastian.huber@…>, on 11/08/18 at 10:55:11

score: Avoid complex include in heap.h

Update #3598.

  • Property mode set to 100644
File size: 13.7 KB
Line 
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.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
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#ifdef RTEMS_DEBUG
28  #define HEAP_PROTECTION
29#endif
30
31/**
32 * @defgroup ScoreHeap Heap Handler
33 *
34 * @ingroup Score
35 *
36 * @brief The Heap Handler provides a heap.
37 *
38 * A heap is a doubly linked list of variable size blocks which are allocated
39 * using the first fit method.  Garbage collection is performed each time a
40 * block is returned to the heap by coalescing neighbor blocks.  Control
41 * information for both allocated and free blocks is contained in the heap
42 * area.  A heap control structure contains control information for the heap.
43 *
44 * The alignment routines could be made faster should we require only powers of
45 * two to be supported for page size, alignment and boundary arguments.  The
46 * minimum alignment requirement for pages is currently CPU_ALIGNMENT and this
47 * value is only required to be multiple of two and explicitly not required to
48 * be a power of two.
49 *
50 * There are two kinds of blocks.  One sort describes a free block from which
51 * we can allocate memory.  The other blocks are used and provide an allocated
52 * memory area.  The free blocks are accessible via a list of free blocks.
53 *
54 * Blocks or areas cover a continuous set of memory addresses. They have a
55 * begin and end address.  The end address is not part of the set.  The size of
56 * a block or area equals the distance between the begin and end address in
57 * units of bytes.
58 *
59 * Free blocks look like:
60 * <table>
61 *   <tr>
62 *     <td rowspan=4>@ref Heap_Block</td><td>previous block size in case the
63 *       previous block is free, <br> otherwise it may contain data used by
64 *       the previous block</td>
65 *   </tr>
66 *   <tr>
67 *     <td>block size and a flag which indicates if the previous block is free
68 *       or used, <br> this field contains always valid data regardless of the
69 *       block usage</td>
70 *   </tr>
71 *   <tr><td>pointer to next block (this field is page size aligned)</td></tr>
72 *   <tr><td>pointer to previous block</td></tr>
73 *   <tr><td colspan=2>free space</td></tr>
74 * </table>
75 *
76 * Used blocks look like:
77 * <table>
78 *   <tr>
79 *     <td rowspan=4>@ref Heap_Block</td><td>previous block size in case the
80 *       previous block is free,<br>otherwise it may contain data used by
81 *       the previous block</td>
82 *   </tr>
83 *   <tr>
84 *     <td>block size and a flag which indicates if the previous block is free
85 *       or used, <br> this field contains always valid data regardless of the
86 *       block usage</td>
87 *   </tr>
88 *   <tr><td>begin of allocated area (this field is page size aligned)</td></tr>
89 *   <tr><td>allocated space</td></tr>
90 *   <tr><td colspan=2>allocated space</td></tr>
91 * </table>
92 *
93 * The heap area after initialization contains two blocks and looks like:
94 * <table>
95 *   <tr><th>Label</th><th colspan=2>Content</th></tr>
96 *   <tr><td>heap->area_begin</td><td colspan=2>heap area begin address</td></tr>
97 *   <tr>
98 *     <td>first_block->prev_size</td>
99 *     <td colspan=2>
100 *       subordinate heap area end address (this will be used to maintain a
101 *       linked list of scattered heap areas)
102 *     </td>
103 *   </tr>
104 *   <tr>
105 *     <td>first_block->size</td>
106 *     <td colspan=2>size available for allocation
107 *       | @c HEAP_PREV_BLOCK_USED</td>
108 *   </tr>
109 *   <tr>
110 *     <td>first_block->next</td><td>_Heap_Free_list_tail(heap)</td>
111 *     <td rowspan=3>memory area available for allocation</td>
112 *   </tr>
113 *   <tr><td>first_block->prev</td><td>_Heap_Free_list_head(heap)</td></tr>
114 *   <tr><td>...</td></tr>
115 *   <tr>
116 *     <td>last_block->prev_size</td><td colspan=2>size of first block</td>
117 *   </tr>
118 *   <tr>
119 *     <td>last_block->size</td>
120 *     <td colspan=2>first block begin address - last block begin address</td>
121 *   </tr>
122 *   <tr><td>heap->area_end</td><td colspan=2>heap area end address</td></tr>
123 * </table>
124 * The next block of the last block is the first block.  Since the first
125 * block indicates that the previous block is used, this ensures that the
126 * last block appears as used for the _Heap_Is_used() and _Heap_Is_free()
127 * functions.
128 */
129/**@{**/
130
131typedef struct Heap_Control Heap_Control;
132
133typedef struct Heap_Block Heap_Block;
134
135#ifndef HEAP_PROTECTION
136  #define HEAP_PROTECTION_HEADER_SIZE 0
137#else
138  #define HEAP_PROTECTOR_COUNT 2
139
140  #define HEAP_BEGIN_PROTECTOR_0 ((uintptr_t) 0xfd75a98f)
141  #define HEAP_BEGIN_PROTECTOR_1 ((uintptr_t) 0xbfa1f177)
142  #define HEAP_END_PROTECTOR_0 ((uintptr_t) 0xd6b8855e)
143  #define HEAP_END_PROTECTOR_1 ((uintptr_t) 0x13a44a5b)
144
145  #define HEAP_FREE_PATTERN ((uintptr_t) 0xe7093cdf)
146
147  #define HEAP_PROTECTION_OBOLUS ((Heap_Block *) 1)
148
149  typedef void (*_Heap_Protection_handler)(
150     Heap_Control *heap,
151     Heap_Block *block
152  );
153
154  typedef struct {
155    _Heap_Protection_handler block_initialize;
156    _Heap_Protection_handler block_check;
157    _Heap_Protection_handler block_error;
158    void *handler_data;
159    Heap_Block *first_delayed_free_block;
160    Heap_Block *last_delayed_free_block;
161    uintptr_t delayed_free_block_count;
162    uintptr_t delayed_free_fraction;
163  } Heap_Protection;
164
165  struct _Thread_Control;
166
167  typedef struct {
168    uintptr_t protector [HEAP_PROTECTOR_COUNT];
169    Heap_Block *next_delayed_free_block;
170    struct _Thread_Control *task;
171    void *tag;
172  } Heap_Protection_block_begin;
173
174  typedef struct {
175    uintptr_t protector [HEAP_PROTECTOR_COUNT];
176  } Heap_Protection_block_end;
177
178  #define HEAP_PROTECTION_HEADER_SIZE \
179    (sizeof(Heap_Protection_block_begin) + sizeof(Heap_Protection_block_end))
180#endif
181
182/**
183 * @brief The block header consists of the two size fields
184 * (@ref Heap_Block.prev_size and @ref Heap_Block.size_and_flag).
185 */
186#define HEAP_BLOCK_HEADER_SIZE \
187  (2 * sizeof(uintptr_t) + HEAP_PROTECTION_HEADER_SIZE)
188
189/**
190 * @brief Description for free or used blocks.
191 */
192struct Heap_Block {
193  /**
194   * @brief Size of the previous block or part of the allocated area of the
195   * previous block.
196   *
197   * This field is only valid if the previous block is free.  This case is
198   * indicated by a cleared @c HEAP_PREV_BLOCK_USED flag in the
199   * @a size_and_flag field of the current block.
200   *
201   * In a used block only the @a size_and_flag field needs to be valid.  The
202   * @a prev_size field of the current block is maintained by the previous
203   * block.  The current block can use the @a prev_size field in the next block
204   * for allocation.
205   */
206  uintptr_t prev_size;
207
208  #ifdef HEAP_PROTECTION
209    Heap_Protection_block_begin Protection_begin;
210  #endif
211
212  /**
213   * @brief Contains the size of the current block and a flag which indicates
214   * if the previous block is free or used.
215   *
216   * If the flag @c HEAP_PREV_BLOCK_USED is set, then the previous block is
217   * used, otherwise the previous block is free.  A used previous block may
218   * claim the @a prev_size field for allocation.  This trick allows to
219   * decrease the overhead in the used blocks by the size of the @a prev_size
220   * field.  As sizes are required to be multiples of two, the least
221   * significant bits would be always zero. We use this bit to store the flag.
222   *
223   * This field is always valid.
224   */
225  uintptr_t size_and_flag;
226
227  #ifdef HEAP_PROTECTION
228    Heap_Protection_block_end Protection_end;
229  #endif
230
231  /**
232   * @brief Pointer to the next free block or part of the allocated area.
233   *
234   * This field is page size aligned and begins of the allocated area in case
235   * the block is used.
236   *
237   * This field is only valid if the block is free and thus part of the free
238   * block list.
239   */
240  Heap_Block *next;
241
242  /**
243   * @brief Pointer to the previous free block or part of the allocated area.
244   *
245   * This field is only valid if the block is free and thus part of the free
246   * block list.
247   */
248  Heap_Block *prev;
249};
250
251/**
252 * @brief Run-time heap statistics.
253 *
254 * The value @a searches / @a allocs gives the mean number of searches per
255 * allocation, while @a max_search gives maximum number of searches ever
256 * performed on a single allocation call.
257 */
258typedef struct {
259  /**
260   * @brief Lifetime number of bytes allocated from this heap.
261   *
262   * This value is an integral multiple of the page size.
263   */
264  uint64_t lifetime_allocated;
265
266  /**
267   * @brief Lifetime number of bytes freed to this heap.
268   *
269   * This value is an integral multiple of the page size.
270   */
271  uint64_t lifetime_freed;
272
273  /**
274   * @brief Size of the allocatable area in bytes.
275   *
276   * This value is an integral multiple of the page size.
277   */
278  uintptr_t size;
279
280  /**
281   * @brief Current free size in bytes.
282   *
283   * This value is an integral multiple of the page size.
284   */
285  uintptr_t free_size;
286
287  /**
288   * @brief Minimum free size ever in bytes.
289   *
290   * This value is an integral multiple of the page size.
291   */
292  uintptr_t min_free_size;
293
294  /**
295   * @brief Current number of free blocks.
296   */
297  uint32_t free_blocks;
298
299  /**
300   * @brief Maximum number of free blocks ever.
301   */
302  uint32_t max_free_blocks;
303
304  /**
305   * @brief Current number of used blocks.
306   */
307  uint32_t used_blocks;
308
309  /**
310   * @brief Maximum number of blocks searched ever.
311   */
312  uint32_t max_search;
313
314  /**
315   * @brief Total number of searches.
316   */
317  uint32_t searches;
318
319  /**
320   * @brief Total number of successful allocations.
321   */
322  uint32_t allocs;
323
324  /**
325   * @brief Total number of failed allocations.
326   */
327  uint32_t failed_allocs;
328
329  /**
330   * @brief Total number of successful frees.
331   */
332  uint32_t frees;
333
334  /**
335   * @brief Total number of successful resizes.
336   */
337  uint32_t resizes;
338} Heap_Statistics;
339
340/**
341 * @brief Control block used to manage a heap.
342 */
343struct Heap_Control {
344  Heap_Block free_list;
345  uintptr_t page_size;
346  uintptr_t min_block_size;
347  uintptr_t area_begin;
348  uintptr_t area_end;
349  Heap_Block *first_block;
350  Heap_Block *last_block;
351  Heap_Statistics stats;
352  #ifdef HEAP_PROTECTION
353    Heap_Protection Protection;
354  #endif
355};
356
357/**
358 * @brief Information about blocks.
359 */
360typedef struct {
361  /**
362   * @brief Number of blocks of this type.
363   */
364  uintptr_t number;
365
366  /**
367   * @brief Largest block of this type.
368   */
369  uintptr_t largest;
370
371  /**
372   * @brief Total size of the blocks of this type.
373   */
374  uintptr_t total;
375} Heap_Information;
376
377/**
378 * @brief Information block returned by _Heap_Get_information().
379 */
380typedef struct {
381  Heap_Information Free;
382  Heap_Information Used;
383  Heap_Statistics Stats;
384} Heap_Information_block;
385
386/**
387 * @brief Heap area structure for table based heap initialization and
388 * extension.
389 *
390 * @see Heap_Initialization_or_extend_handler.
391 */
392typedef struct {
393  void *begin;
394  uintptr_t size;
395} Heap_Area;
396
397/**
398 * @brief Heap initialization and extend handler type.
399 *
400 * This helps to do a table based heap initialization and extension.  Create a
401 * table of Heap_Area elements and iterate through it.  Set the handler to
402 * _Heap_Initialize() in the first iteration and then to _Heap_Extend().
403 *
404 * @see Heap_Area, _Heap_Initialize(), _Heap_Extend(), or _Heap_No_extend().
405 */
406typedef uintptr_t (*Heap_Initialization_or_extend_handler)(
407  Heap_Control *heap,
408  void *area_begin,
409  uintptr_t area_size,
410  uintptr_t page_size_or_unused
411);
412
413/**
414 * @brief Extends the memory available for the heap @a heap using the memory
415 * area starting at @a area_begin of size @a area_size bytes.
416 *
417 * There are no alignment requirements for the memory area.  The memory area
418 * must be big enough to contain some maintenance blocks.  It must not overlap
419 * parts of the current heap memory areas.  Disconnected memory areas added to
420 * the heap will lead to used blocks which cover the gaps.  Extending with an
421 * inappropriate memory area will corrupt the heap resulting in undefined
422 * behaviour.
423 *
424 * The unused fourth parameter is provided to have the same signature as
425 * _Heap_Initialize().
426 *
427 * Returns the extended space available for allocation, or zero in case of failure.
428 *
429 * @see Heap_Initialization_or_extend_handler.
430 */
431uintptr_t _Heap_Extend(
432  Heap_Control *heap,
433  void *area_begin,
434  uintptr_t area_size,
435  uintptr_t unused
436);
437
438/**
439 * @brief This function returns always zero.
440 *
441 * This function only returns zero and does nothing else.
442 *
443 * Returns always zero.
444 *
445 * @see Heap_Initialization_or_extend_handler.
446 */
447uintptr_t _Heap_No_extend(
448  Heap_Control *unused_0,
449  void *unused_1,
450  uintptr_t unused_2,
451  uintptr_t unused_3
452);
453
454RTEMS_INLINE_ROUTINE uintptr_t _Heap_Align_up(
455  uintptr_t value,
456  uintptr_t alignment
457)
458{
459  uintptr_t remainder = value % alignment;
460
461  if ( remainder != 0 ) {
462    return value - remainder + alignment;
463  } else {
464    return value;
465  }
466}
467
468RTEMS_INLINE_ROUTINE uintptr_t _Heap_Min_block_size( uintptr_t page_size )
469{
470  return _Heap_Align_up( sizeof( Heap_Block ), page_size );
471}
472
473/**
474 * @brief Returns the worst case overhead to manage a memory area.
475 */
476RTEMS_INLINE_ROUTINE uintptr_t _Heap_Area_overhead(
477  uintptr_t page_size
478)
479{
480  if ( page_size != 0 ) {
481    page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
482  } else {
483    page_size = CPU_ALIGNMENT;
484  }
485
486  return 2 * (page_size - 1) + HEAP_BLOCK_HEADER_SIZE;
487}
488
489/**
490 * @brief Returns the size with administration and alignment overhead for one
491 * allocation.
492 */
493RTEMS_INLINE_ROUTINE uintptr_t _Heap_Size_with_overhead(
494  uintptr_t page_size,
495  uintptr_t size,
496  uintptr_t alignment
497)
498{
499  if ( page_size != 0 ) {
500    page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
501  } else {
502    page_size = CPU_ALIGNMENT;
503  }
504
505  if ( page_size < alignment ) {
506    page_size = alignment;
507  }
508
509  return HEAP_BLOCK_HEADER_SIZE + page_size - 1 + size;
510}
511
512/** @} */
513
514#ifdef __cplusplus
515}
516#endif
517
518#endif
519/* end of include file */
Note: See TracBrowser for help on using the repository browser.