1 | /* |
---|
2 | * Heap Handler |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-1998. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * Copyright assigned to U.S. Government, 1994. |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.OARcorp.com/rtems/license.html. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/score/sysstate.h> |
---|
18 | #include <rtems/score/heap.h> |
---|
19 | |
---|
20 | /*PAGE |
---|
21 | * |
---|
22 | * _Heap_Extend |
---|
23 | * |
---|
24 | * This routine grows the_heap memory area using the size bytes which |
---|
25 | * begin at starting_address. |
---|
26 | * |
---|
27 | * Input parameters: |
---|
28 | * the_heap - pointer to heap header. |
---|
29 | * starting_address - pointer to the memory area. |
---|
30 | * size - size in bytes of the memory block to allocate. |
---|
31 | * |
---|
32 | * Output parameters: |
---|
33 | * *amount_extended - amount of memory added to the_heap |
---|
34 | */ |
---|
35 | |
---|
36 | Heap_Extend_status _Heap_Extend( |
---|
37 | Heap_Control *the_heap, |
---|
38 | void *starting_address, |
---|
39 | unsigned32 size, |
---|
40 | unsigned32 *amount_extended |
---|
41 | ) |
---|
42 | { |
---|
43 | Heap_Block *the_block; |
---|
44 | unsigned32 *p; |
---|
45 | |
---|
46 | /* |
---|
47 | * The overhead was taken from the original heap memory. |
---|
48 | */ |
---|
49 | |
---|
50 | Heap_Block *old_final; |
---|
51 | Heap_Block *new_final; |
---|
52 | |
---|
53 | /* |
---|
54 | * There are five possibilities for the location of starting |
---|
55 | * address: |
---|
56 | * |
---|
57 | * 1. non-contiguous lower address (NOT SUPPORTED) |
---|
58 | * 2. contiguous lower address (NOT SUPPORTED) |
---|
59 | * 3. in the heap (ERROR) |
---|
60 | * 4. contiguous higher address (SUPPORTED) |
---|
61 | * 5. non-contiguous higher address (NOT SUPPORTED) |
---|
62 | * |
---|
63 | * As noted, this code only supports (4). |
---|
64 | */ |
---|
65 | |
---|
66 | if ( starting_address >= (void *) the_heap->start && /* case 3 */ |
---|
67 | starting_address <= (void *) the_heap->final |
---|
68 | ) |
---|
69 | return HEAP_EXTEND_ERROR; |
---|
70 | |
---|
71 | if ( starting_address < (void *) the_heap->start ) { /* cases 1 and 2 */ |
---|
72 | |
---|
73 | return HEAP_EXTEND_NOT_IMPLEMENTED; /* cases 1 and 2 */ |
---|
74 | |
---|
75 | } else { /* cases 4 and 5 */ |
---|
76 | |
---|
77 | the_block = (Heap_Block *) |
---|
78 | _Addresses_Subtract_offset( starting_address, HEAP_OVERHEAD ); |
---|
79 | if ( the_block != the_heap->final ) |
---|
80 | return HEAP_EXTEND_NOT_IMPLEMENTED; /* case 5 */ |
---|
81 | } |
---|
82 | |
---|
83 | /* |
---|
84 | * Currently only case 4 should make it to this point. |
---|
85 | * The basic trick is to make the extend area look like a used |
---|
86 | * block and free it. |
---|
87 | */ |
---|
88 | |
---|
89 | *amount_extended = size; |
---|
90 | |
---|
91 | old_final = the_heap->final; |
---|
92 | new_final = _Addresses_Add_offset( old_final, size ); |
---|
93 | /* SAME AS: _Addresses_Add_offset( starting_address, size-HEAP_OVERHEAD ); */ |
---|
94 | |
---|
95 | the_heap->final = new_final; |
---|
96 | |
---|
97 | old_final->front_flag = |
---|
98 | new_final->back_flag = _Heap_Build_flag( size, HEAP_BLOCK_USED ); |
---|
99 | new_final->front_flag = HEAP_DUMMY_FLAG; |
---|
100 | |
---|
101 | /* |
---|
102 | * Must pass in address of "user" area |
---|
103 | * So add in the offset field. |
---|
104 | */ |
---|
105 | |
---|
106 | p = (unsigned32 *) &old_final->next; |
---|
107 | *p = sizeof(unsigned32); |
---|
108 | p++; |
---|
109 | _Heap_Free( the_heap, p ); |
---|
110 | |
---|
111 | return HEAP_EXTEND_SUCCESSFUL; |
---|
112 | } |
---|
113 | |
---|