RTEMS 6.1-rc1
heap.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
13/*
14 * COPYRIGHT (c) 1989-2006.
15 * On-Line Applications Research Corporation (OAR).
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifndef _RTEMS_SCORE_HEAP_H
40#define _RTEMS_SCORE_HEAP_H
41
42#include <rtems/score/cpu.h>
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#ifdef RTEMS_DEBUG
50 #define HEAP_PROTECTION
51#endif
52
154typedef struct Heap_Control Heap_Control;
155
156typedef struct Heap_Block Heap_Block;
157
163typedef enum {
168
174
179
185
191
197typedef struct {
202
207
213
214#ifndef HEAP_PROTECTION
215 #define HEAP_PROTECTION_HEADER_SIZE 0
216#else
217 #define HEAP_PROTECTOR_COUNT 2
218
219 #define HEAP_BEGIN_PROTECTOR_0 ((uintptr_t) 0xfd75a98f)
220 #define HEAP_BEGIN_PROTECTOR_1 ((uintptr_t) 0xbfa1f177)
221 #define HEAP_END_PROTECTOR_0 ((uintptr_t) 0xd6b8855e)
222 #define HEAP_END_PROTECTOR_1 ((uintptr_t) 0x13a44a5b)
223
224 #define HEAP_FREE_PATTERN ((uintptr_t) 0xe7093cdf)
225
226 #define HEAP_PROTECTION_OBOLUS ((Heap_Block *) 1)
227
228 typedef void (*_Heap_Protection_handler)(
229 Heap_Control *heap,
230 Heap_Block *block
231 );
232
233 typedef void (*_Heap_Protection_error_handler)(
234 Heap_Control *heap,
235 Heap_Block *block,
236 Heap_Error_reason reason
237 );
238
239 typedef struct {
240 _Heap_Protection_handler block_initialize;
241 _Heap_Protection_handler block_check;
242 _Heap_Protection_error_handler block_error;
243 void *handler_data;
244 Heap_Block *first_delayed_free_block;
245 Heap_Block *last_delayed_free_block;
246 uintptr_t delayed_free_block_count;
247 uintptr_t delayed_free_fraction;
248 } Heap_Protection;
249
250 struct _Thread_Control;
251
252 typedef struct {
253 uintptr_t protector [HEAP_PROTECTOR_COUNT];
254 Heap_Block *next_delayed_free_block;
255 struct _Thread_Control *task;
256 void *tag;
257 } Heap_Protection_block_begin;
258
259 typedef struct {
260 uintptr_t protector [HEAP_PROTECTOR_COUNT];
261 } Heap_Protection_block_end;
262
263 #define HEAP_PROTECTION_HEADER_SIZE \
264 (sizeof(Heap_Protection_block_begin) + sizeof(Heap_Protection_block_end))
265#endif
266
271#define HEAP_BLOCK_HEADER_SIZE \
272 (2 * sizeof(uintptr_t) + HEAP_PROTECTION_HEADER_SIZE)
273
291 uintptr_t prev_size;
292
293 #ifdef HEAP_PROTECTION
294 Heap_Protection_block_begin Protection_begin;
295 #endif
296
310 uintptr_t size_and_flag;
311
312 #ifdef HEAP_PROTECTION
313 Heap_Protection_block_end Protection_end;
314 #endif
315
326
334};
335
341 uintptr_t page_size;
342 uintptr_t min_block_size;
343 uintptr_t area_begin;
344 uintptr_t area_end;
345 Heap_Block *first_block;
346 Heap_Block *last_block;
347 Heap_Statistics stats;
348 #ifdef HEAP_PROTECTION
349 Heap_Protection Protection;
350 #endif
351};
352
359typedef struct {
360 void *begin;
361 uintptr_t size;
362} Heap_Area;
363
374 Heap_Control *heap,
375 void *area_begin,
376 uintptr_t area_size,
377 uintptr_t page_size_or_unused
378);
379
400uintptr_t _Heap_Extend(
401 Heap_Control *heap,
402 void *area_begin,
403 uintptr_t area_size,
404 uintptr_t unused
405);
406
420 Heap_Control *unused_0,
421 void *unused_1,
422 uintptr_t unused_2,
423 uintptr_t unused_3
424);
425
434static inline uintptr_t _Heap_Align_up(
435 uintptr_t value,
436 uintptr_t alignment
437)
438{
439 uintptr_t remainder = value % alignment;
440
441 if ( remainder != 0 ) {
442 return value - remainder + alignment;
443 } else {
444 return value;
445 }
446}
447
455static inline uintptr_t _Heap_Min_block_size( uintptr_t page_size )
456{
457 return _Heap_Align_up( sizeof( Heap_Block ), page_size );
458}
459
467static inline uintptr_t _Heap_Area_overhead(
468 uintptr_t page_size
469)
470{
471 if ( page_size != 0 ) {
472 page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
473 } else {
474 page_size = CPU_ALIGNMENT;
475 }
476
477 /*
478 * Account for a potential alignment of the area begin address to a page
479 * boundary, the first block, and the last block. The last block consists
480 * only of a block header.
481 */
482 return page_size - 1 + _Heap_Min_block_size( page_size ) +
484}
485
496static inline uintptr_t _Heap_Size_with_overhead(
497 uintptr_t page_size,
498 uintptr_t size,
499 uintptr_t alignment
500)
501{
502 if ( page_size != 0 ) {
503 page_size = _Heap_Align_up( page_size, CPU_ALIGNMENT );
504 } else {
505 page_size = CPU_ALIGNMENT;
506 }
507
508 if ( page_size < alignment ) {
509 page_size = alignment;
510 }
511
512 return HEAP_BLOCK_HEADER_SIZE + page_size - 1 + size;
513}
514
517#ifdef __cplusplus
518}
519#endif
520
521#endif
522/* end of include file */
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 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:271
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:139
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:373
Heap_Error_reason
The heap error reason.
Definition: heap.h:163
@ 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:184
@ HEAP_ERROR_BROKEN_PROTECTOR
There is an unexpected value in the heap block protector area.
Definition: heap.h:167
@ HEAP_ERROR_DOUBLE_FREE
There is was an attempt to free the same block twice.
Definition: heap.h:178
@ HEAP_ERROR_BAD_FREE_BLOCK
A supposed to be free block is not inside the heap memory area.
Definition: heap.h:189
@ HEAP_ERROR_FREE_PATTERN
There is an unexpected value in the free pattern of a free heap block.
Definition: heap.h:173
This header file provides interfaces of the Barrier Handler which are used by the implementation and ...
Heap area structure for table based heap initialization and extension.
Definition: heap.h:359
Description for free or used blocks.
Definition: heap.h:277
Heap_Block * prev
Pointer to the previous free block or part of the allocated area.
Definition: heap.h:333
uintptr_t prev_size
Size of the previous block or part of the allocated area of the previous block.
Definition: heap.h:291
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:310
Heap_Block * next
Pointer to the next free block or part of the allocated area.
Definition: heap.h:325
Control block used to manage a heap.
Definition: heap.h:339
Context of a heap error.
Definition: heap.h:197
Heap_Error_reason reason
The heap error reason.
Definition: heap.h:211
Heap_Block * block
The heap block causing the error.
Definition: heap.h:206
Heap_Control * heap
The heap of the block.
Definition: heap.h:201
Run-time heap statistics.
Definition: heapinfo.h:60
Definition: thread.h:812
Definition: intercom.c:71
unsigned size
Definition: tte.h:1