1 | /* |
---|
2 | * Heap Handler |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-1999. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <rtems/system.h> |
---|
19 | #include <rtems/score/sysstate.h> |
---|
20 | #include <rtems/score/heap.h> |
---|
21 | |
---|
22 | /*PAGE |
---|
23 | * |
---|
24 | * _Heap_Size_of_user_area |
---|
25 | * |
---|
26 | * This kernel routine sets '*size' to the size of the block of memory |
---|
27 | * which begins at 'starting_address'. |
---|
28 | * It returns TRUE if the 'starting_address' is in the heap, and FALSE |
---|
29 | * otherwise. |
---|
30 | * |
---|
31 | * Input parameters: |
---|
32 | * the_heap - pointer to heap header |
---|
33 | * starting_address - starting address of the memory block |
---|
34 | * size - pointer to size of area |
---|
35 | * |
---|
36 | * Output parameters: |
---|
37 | * size - size of area filled in |
---|
38 | * TRUE - if starting_address is valid heap address |
---|
39 | * FALSE - if starting_address is invalid heap address |
---|
40 | */ |
---|
41 | |
---|
42 | boolean _Heap_Size_of_user_area( |
---|
43 | Heap_Control *the_heap, |
---|
44 | void *starting_address, |
---|
45 | size_t *size |
---|
46 | ) |
---|
47 | { |
---|
48 | Heap_Block *the_block; |
---|
49 | Heap_Block *next_block; |
---|
50 | uint32_t the_size; |
---|
51 | |
---|
52 | if ( !_Addresses_Is_in_range( |
---|
53 | starting_address, (void *)the_heap->start, (void *)the_heap->final ) ) |
---|
54 | return( FALSE ); |
---|
55 | |
---|
56 | _Heap_Start_of_block( the_heap, starting_address, &the_block ); |
---|
57 | |
---|
58 | _HAssert(_Heap_Is_block_in( the_heap, the_block )); |
---|
59 | if ( !_Heap_Is_block_in( the_heap, the_block ) ) |
---|
60 | return( FALSE ); |
---|
61 | |
---|
62 | the_size = _Heap_Block_size( the_block ); |
---|
63 | next_block = _Heap_Block_at( the_block, the_size ); |
---|
64 | |
---|
65 | _HAssert(_Heap_Is_block_in( the_heap, next_block )); |
---|
66 | _HAssert(_Heap_Is_prev_used( next_block )); |
---|
67 | if ( |
---|
68 | !_Heap_Is_block_in( the_heap, next_block ) || |
---|
69 | !_Heap_Is_prev_used( next_block ) |
---|
70 | ) |
---|
71 | return( FALSE ); |
---|
72 | |
---|
73 | /* 'starting_address' could be greater than 'the_block' address plus |
---|
74 | HEAP_BLOCK_USER_OFFSET as _Heap_Allocate_aligned() may produce such user |
---|
75 | pointers. To get rid of this offset we calculate user size as difference |
---|
76 | between the end of 'the_block' (='next_block') and 'starting_address' |
---|
77 | and then add correction equal to the offset of the 'size' field of the |
---|
78 | 'Heap_Block' structure. The correction is due to the fact that |
---|
79 | 'prev_size' field of the next block is actually used as user accessible |
---|
80 | area of 'the_block'. */ |
---|
81 | |
---|
82 | *size = _Addresses_Subtract ( next_block, starting_address ) |
---|
83 | + HEAP_BLOCK_HEADER_OFFSET; |
---|
84 | |
---|
85 | return( TRUE ); |
---|
86 | } |
---|
87 | |
---|