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.OARcorp.com/rtems/license.html. |
---|
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 | /*PAGE |
---|
20 | * |
---|
21 | * _Heap_Free |
---|
22 | * |
---|
23 | * This kernel routine returns the memory designated by the |
---|
24 | * given heap and given starting address to the memory pool. |
---|
25 | * |
---|
26 | * Input parameters: |
---|
27 | * the_heap - pointer to heap header |
---|
28 | * starting_address - starting address of the memory block to free. |
---|
29 | * |
---|
30 | * Output parameters: |
---|
31 | * TRUE - if starting_address is valid heap address |
---|
32 | * FALSE - if starting_address is invalid heap address |
---|
33 | */ |
---|
34 | |
---|
35 | boolean _Heap_Free( |
---|
36 | Heap_Control *the_heap, |
---|
37 | void *starting_address |
---|
38 | ) |
---|
39 | { |
---|
40 | Heap_Block *the_block; |
---|
41 | Heap_Block *next_block; |
---|
42 | Heap_Block *new_next_block; |
---|
43 | Heap_Block *previous_block; |
---|
44 | Heap_Block *temporary_block; |
---|
45 | unsigned32 the_size; |
---|
46 | |
---|
47 | the_block = _Heap_User_block_at( starting_address ); |
---|
48 | |
---|
49 | if ( !_Heap_Is_block_in( the_heap, the_block ) || |
---|
50 | _Heap_Is_block_free( the_block ) ) { |
---|
51 | return( FALSE ); |
---|
52 | } |
---|
53 | |
---|
54 | the_size = _Heap_Block_size( the_block ); |
---|
55 | next_block = _Heap_Block_at( the_block, the_size ); |
---|
56 | |
---|
57 | if ( !_Heap_Is_block_in( the_heap, next_block ) || |
---|
58 | (the_block->front_flag != next_block->back_flag) ) { |
---|
59 | return( FALSE ); |
---|
60 | } |
---|
61 | |
---|
62 | if ( _Heap_Is_previous_block_free( the_block ) ) { |
---|
63 | previous_block = _Heap_Previous_block( the_block ); |
---|
64 | |
---|
65 | if ( !_Heap_Is_block_in( the_heap, previous_block ) ) { |
---|
66 | return( FALSE ); |
---|
67 | } |
---|
68 | |
---|
69 | if ( _Heap_Is_block_free( next_block ) ) { /* coalesce both */ |
---|
70 | previous_block->front_flag += next_block->front_flag + the_size; |
---|
71 | temporary_block = _Heap_Next_block( previous_block ); |
---|
72 | temporary_block->back_flag = previous_block->front_flag; |
---|
73 | next_block->next->previous = next_block->previous; |
---|
74 | next_block->previous->next = next_block->next; |
---|
75 | } |
---|
76 | else { /* coalesce prev */ |
---|
77 | previous_block->front_flag = |
---|
78 | next_block->back_flag = previous_block->front_flag + the_size; |
---|
79 | } |
---|
80 | } |
---|
81 | else if ( _Heap_Is_block_free( next_block ) ) { /* coalesce next */ |
---|
82 | the_block->front_flag = the_size + next_block->front_flag; |
---|
83 | new_next_block = _Heap_Next_block( the_block ); |
---|
84 | new_next_block->back_flag = the_block->front_flag; |
---|
85 | the_block->next = next_block->next; |
---|
86 | the_block->previous = next_block->previous; |
---|
87 | next_block->previous->next = the_block; |
---|
88 | next_block->next->previous = the_block; |
---|
89 | |
---|
90 | if (the_heap->first == next_block) |
---|
91 | the_heap->first = the_block; |
---|
92 | } |
---|
93 | else { /* no coalesce */ |
---|
94 | next_block->back_flag = |
---|
95 | the_block->front_flag = the_size; |
---|
96 | the_block->previous = _Heap_Head( the_heap ); |
---|
97 | the_block->next = the_heap->first; |
---|
98 | the_heap->first = the_block; |
---|
99 | the_block->next->previous = the_block; |
---|
100 | } |
---|
101 | |
---|
102 | return( TRUE ); |
---|
103 | } |
---|
104 | |
---|