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 | |
---|
15 | #include <rtems/system.h> |
---|
16 | #include <rtems/score/sysstate.h> |
---|
17 | #include <rtems/score/heap.h> |
---|
18 | |
---|
19 | /* |
---|
20 | * _Heap_Resize_block |
---|
21 | * |
---|
22 | * DESCRIPTION: |
---|
23 | * |
---|
24 | * This routine tries to resize in place the block that is pointed to by the |
---|
25 | * 'starting_address' to the new 'size'. |
---|
26 | * |
---|
27 | * Input parameters: |
---|
28 | * the_heap - pointer to heap header |
---|
29 | * starting_address - starting address of the memory block |
---|
30 | * size - new size |
---|
31 | * |
---|
32 | * Output parameters: |
---|
33 | * 'old_mem_size' - the size of the user memory area of the block before |
---|
34 | * resizing. |
---|
35 | * 'avail_mem_size' - the size of the user memory area of the free block |
---|
36 | * that has been enlarged or created due to resizing, |
---|
37 | * 0 if none. |
---|
38 | * Returns |
---|
39 | * HEAP_RESIZE_SUCCESSFUL - if success |
---|
40 | * HEAP_RESIZE_UNSATISFIED - if the block can't be resized in place |
---|
41 | * HEAP_RESIZE_FATAL_ERROR - if failure |
---|
42 | */ |
---|
43 | |
---|
44 | Heap_Resize_status _Heap_Resize_block( |
---|
45 | Heap_Control *the_heap, |
---|
46 | void *starting_address, |
---|
47 | uint32_t size, |
---|
48 | uint32_t *old_mem_size, |
---|
49 | uint32_t *avail_mem_size |
---|
50 | ) |
---|
51 | { |
---|
52 | Heap_Block *the_block; |
---|
53 | Heap_Block *next_block; |
---|
54 | uint32_t next_block_size; |
---|
55 | boolean next_is_used; |
---|
56 | Heap_Block *next_next_block; |
---|
57 | uint32_t old_block_size; |
---|
58 | uint32_t old_user_size; |
---|
59 | uint32_t prev_used_flag; |
---|
60 | Heap_Statistics *const stats = &the_heap->stats; |
---|
61 | uint32_t const min_block_size = the_heap->min_block_size; |
---|
62 | uint32_t const page_size = the_heap->page_size; |
---|
63 | |
---|
64 | *old_mem_size = 0; |
---|
65 | *avail_mem_size = 0; |
---|
66 | |
---|
67 | _Heap_Start_of_block(the_heap, starting_address, &the_block); |
---|
68 | _HAssert(_Heap_Is_block_in(the_heap, the_block)); |
---|
69 | if (!_Heap_Is_block_in(the_heap, the_block)) |
---|
70 | return HEAP_RESIZE_FATAL_ERROR; |
---|
71 | |
---|
72 | prev_used_flag = the_block->size & HEAP_PREV_USED; |
---|
73 | old_block_size = _Heap_Block_size(the_block); |
---|
74 | next_block = _Heap_Block_at(the_block, old_block_size); |
---|
75 | |
---|
76 | _HAssert(_Heap_Is_block_in(the_heap, next_block)); |
---|
77 | _HAssert(_Heap_Is_prev_used(next_block)); |
---|
78 | if ( !_Heap_Is_block_in(the_heap, next_block) || |
---|
79 | !_Heap_Is_prev_used(next_block)) |
---|
80 | return HEAP_RESIZE_FATAL_ERROR; |
---|
81 | |
---|
82 | next_block_size = _Heap_Block_size(next_block); |
---|
83 | next_next_block = _Heap_Block_at(next_block, next_block_size); |
---|
84 | next_is_used = (next_block == the_heap->final) || |
---|
85 | _Heap_Is_prev_used(next_next_block); |
---|
86 | |
---|
87 | /* See _Heap_Size_of_user_area() source for explanations */ |
---|
88 | old_user_size = _Addresses_Subtract(next_block, starting_address) |
---|
89 | + HEAP_BLOCK_HEADER_OFFSET; |
---|
90 | |
---|
91 | *old_mem_size = old_user_size; |
---|
92 | |
---|
93 | if (size > old_user_size) { |
---|
94 | /* Need to extend the block: allocate part of the next block and then |
---|
95 | merge 'the_block' and allocated block together. */ |
---|
96 | if (next_is_used) /* Next block is in use, -- no way to extend */ |
---|
97 | return HEAP_RESIZE_UNSATISFIED; |
---|
98 | else { |
---|
99 | uint32_t add_block_size = size - old_user_size; |
---|
100 | _Heap_Align_up(&add_block_size, page_size); |
---|
101 | if (add_block_size < min_block_size) |
---|
102 | add_block_size = min_block_size; |
---|
103 | if (add_block_size > next_block_size) |
---|
104 | return HEAP_RESIZE_UNSATISFIED; /* Next block is too small or none. */ |
---|
105 | add_block_size = |
---|
106 | _Heap_Block_allocate(the_heap, next_block, add_block_size); |
---|
107 | /* Merge two subsequent blocks */ |
---|
108 | the_block->size = (old_block_size + add_block_size) | prev_used_flag; |
---|
109 | --stats->used_blocks; |
---|
110 | } |
---|
111 | } else { |
---|
112 | |
---|
113 | /* Calculate how much memory we could free */ |
---|
114 | uint32_t free_block_size = old_user_size - size; |
---|
115 | _Heap_Align_down(&free_block_size, page_size); |
---|
116 | |
---|
117 | if (free_block_size > 0) { |
---|
118 | |
---|
119 | /* To free some memory the block should be shortened so that it can |
---|
120 | can hold 'size' user bytes and still remain not shorter than |
---|
121 | 'min_block_size'. */ |
---|
122 | |
---|
123 | uint32_t new_block_size = old_block_size - free_block_size; |
---|
124 | |
---|
125 | if (new_block_size < min_block_size) { |
---|
126 | uint32_t delta = min_block_size - new_block_size; |
---|
127 | _HAssert(free_block_size >= delta); |
---|
128 | free_block_size -= delta; |
---|
129 | if (free_block_size == 0) { |
---|
130 | ++stats->resizes; |
---|
131 | return HEAP_RESIZE_SUCCESSFUL; |
---|
132 | } |
---|
133 | new_block_size += delta; |
---|
134 | } |
---|
135 | |
---|
136 | _HAssert(new_block_size >= min_block_size); |
---|
137 | _HAssert(new_block_size + free_block_size == old_block_size); |
---|
138 | _HAssert(_Heap_Is_aligned(new_block_size, page_size)); |
---|
139 | _HAssert(_Heap_Is_aligned(free_block_size, page_size)); |
---|
140 | |
---|
141 | if (!next_is_used) { |
---|
142 | /* Extend the next block to the low addresses by 'free_block_size' */ |
---|
143 | Heap_Block *const new_next_block = |
---|
144 | _Heap_Block_at(the_block, new_block_size); |
---|
145 | uint32_t const new_next_block_size = |
---|
146 | next_block_size + free_block_size; |
---|
147 | _HAssert(_Heap_Is_block_in(the_heap, next_next_block)); |
---|
148 | the_block->size = new_block_size | prev_used_flag; |
---|
149 | new_next_block->size = new_next_block_size | HEAP_PREV_USED; |
---|
150 | next_next_block->prev_size = new_next_block_size; |
---|
151 | _Heap_Block_replace(next_block, new_next_block); |
---|
152 | the_heap->stats.free_size += free_block_size; |
---|
153 | *avail_mem_size = new_next_block_size - HEAP_BLOCK_USED_OVERHEAD; |
---|
154 | |
---|
155 | } else if (free_block_size >= min_block_size) { |
---|
156 | /* Split the block into 2 used parts, then free the second one. */ |
---|
157 | the_block->size = new_block_size | prev_used_flag; |
---|
158 | next_block = _Heap_Block_at(the_block, new_block_size); |
---|
159 | next_block->size = free_block_size | HEAP_PREV_USED; |
---|
160 | ++stats->used_blocks; /* We have created used block */ |
---|
161 | --stats->frees; /* Don't count next call in stats */ |
---|
162 | _Heap_Free(the_heap, _Heap_User_area(next_block)); |
---|
163 | *avail_mem_size = free_block_size - HEAP_BLOCK_USED_OVERHEAD; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | ++stats->resizes; |
---|
169 | return HEAP_RESIZE_SUCCESSFUL; |
---|
170 | } |
---|