1 | /* |
---|
2 | * Heap Handler |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2007. |
---|
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 <stdlib.h> /* abort */ |
---|
19 | |
---|
20 | #include <rtems/system.h> |
---|
21 | #include <rtems/score/sysstate.h> |
---|
22 | #include <rtems/score/heap.h> |
---|
23 | #include <rtems/score/interr.h> |
---|
24 | #include <rtems/bspIo.h> |
---|
25 | |
---|
26 | /*PAGE |
---|
27 | * |
---|
28 | * _Heap_Walk |
---|
29 | * |
---|
30 | * This kernel routine walks the heap and verifies its correctness. |
---|
31 | * |
---|
32 | * Input parameters: |
---|
33 | * the_heap - pointer to heap header |
---|
34 | * source - a numeric indicator of the invoker of this routine |
---|
35 | * do_dump - when TRUE print the information |
---|
36 | * |
---|
37 | * Output parameters: NONE |
---|
38 | */ |
---|
39 | |
---|
40 | #include <stdio.h> |
---|
41 | |
---|
42 | bool _Heap_Walk( |
---|
43 | Heap_Control *the_heap, |
---|
44 | int source, |
---|
45 | bool do_dump |
---|
46 | ) |
---|
47 | { |
---|
48 | Heap_Block *the_block = the_heap->start; |
---|
49 | Heap_Block *const end = the_heap->final; |
---|
50 | Heap_Block *const tail = _Heap_Tail(the_heap); |
---|
51 | int error = 0; |
---|
52 | int passes = 0; |
---|
53 | |
---|
54 | do_dump = FALSE; |
---|
55 | /* |
---|
56 | * We don't want to allow walking the heap until we have |
---|
57 | * transferred control to the user task so we watch the |
---|
58 | * system state. |
---|
59 | */ |
---|
60 | |
---|
61 | /* |
---|
62 | if ( !_System_state_Is_up( _System_state_Get() ) ) |
---|
63 | return TRUE; |
---|
64 | */ |
---|
65 | |
---|
66 | if (source < 0) |
---|
67 | source = the_heap->stats.instance; |
---|
68 | |
---|
69 | if (do_dump == TRUE) |
---|
70 | printk("\nPASS: %d start %p final %p first %p last %p begin %p end %p\n", |
---|
71 | source, the_block, end, |
---|
72 | _Heap_First(the_heap), _Heap_Last(the_heap), |
---|
73 | the_heap->begin, the_heap->end); |
---|
74 | |
---|
75 | /* |
---|
76 | * Handle the 1st block |
---|
77 | */ |
---|
78 | |
---|
79 | if (!_Heap_Is_prev_used(the_block)) { |
---|
80 | printk("PASS: %d !HEAP_PREV_USED flag of 1st block isn't set\n", source); |
---|
81 | error = 1; |
---|
82 | } |
---|
83 | |
---|
84 | if (the_block->prev_size != the_heap->page_size) { |
---|
85 | printk("PASS: %d !prev_size of 1st block isn't page_size\n", source); |
---|
86 | error = 1; |
---|
87 | } |
---|
88 | |
---|
89 | while ( the_block != end ) { |
---|
90 | uint32_t const the_size = _Heap_Block_size(the_block); |
---|
91 | Heap_Block *const next_block = _Heap_Block_at(the_block, the_size); |
---|
92 | bool prev_used = _Heap_Is_prev_used(the_block); |
---|
93 | |
---|
94 | if (do_dump) { |
---|
95 | printk("PASS: %d block %p size %d(%c)", |
---|
96 | source, the_block, the_size, (prev_used ? 'U' : 'F')); |
---|
97 | if (prev_used) |
---|
98 | printk(" prev_size %d", the_block->prev_size); |
---|
99 | else |
---|
100 | printk(" (prev_size) %d", the_block->prev_size); |
---|
101 | } |
---|
102 | |
---|
103 | if (!_Heap_Is_block_in(the_heap, next_block)) { |
---|
104 | if (do_dump) printk("\n"); |
---|
105 | printk("PASS: %d !block %p is out of heap\n", source, next_block); |
---|
106 | error = 1; |
---|
107 | break; |
---|
108 | } |
---|
109 | |
---|
110 | if (!_Heap_Is_prev_used(next_block)) { |
---|
111 | if (do_dump) |
---|
112 | printk( " prev %p next %p", the_block->prev, the_block->next); |
---|
113 | if (_Heap_Block_size(the_block) != next_block->prev_size) { |
---|
114 | if (do_dump) printk("\n"); |
---|
115 | printk("PASS: %d !front and back sizes don't match", source); |
---|
116 | error = 1; |
---|
117 | } |
---|
118 | if (!prev_used) { |
---|
119 | if (do_dump || error) printk("\n"); |
---|
120 | printk("PASS: %d !two consecutive blocks are free", source); |
---|
121 | error = 1; |
---|
122 | } |
---|
123 | |
---|
124 | { /* Check if 'the_block' is in the free block list */ |
---|
125 | Heap_Block* block = _Heap_First(the_heap); |
---|
126 | while(block != the_block && block != tail) |
---|
127 | block = block->next; |
---|
128 | if(block != the_block) { |
---|
129 | if (do_dump || error) printk("\n"); |
---|
130 | printk("PASS: %d !the_block not in the free list", source); |
---|
131 | error = 1; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | } |
---|
136 | if (do_dump || error) printk("\n"); |
---|
137 | |
---|
138 | if (the_size < the_heap->min_block_size) { |
---|
139 | printk("PASS: %d !block size is too small\n", source); |
---|
140 | error = 1; |
---|
141 | break; |
---|
142 | } |
---|
143 | if (!_Heap_Is_aligned( the_size, the_heap->page_size)) { |
---|
144 | printk("PASS: %d !block size is misaligned\n", source); |
---|
145 | error = 1; |
---|
146 | } |
---|
147 | |
---|
148 | if (++passes > (do_dump ? 10 : 0) && error) |
---|
149 | break; |
---|
150 | |
---|
151 | the_block = next_block; |
---|
152 | } |
---|
153 | |
---|
154 | if (the_block != end) { |
---|
155 | printk("PASS: %d !last block address isn't equal to 'final' %p %p\n", |
---|
156 | source, the_block, end); |
---|
157 | error = 1; |
---|
158 | } |
---|
159 | |
---|
160 | if (_Heap_Block_size(the_block) != the_heap->page_size) { |
---|
161 | printk("PASS: %d !last block's size isn't page_size (%d != %d)\n", source, |
---|
162 | _Heap_Block_size(the_block), the_heap->page_size); |
---|
163 | error = 1; |
---|
164 | } |
---|
165 | |
---|
166 | if(do_dump && error) |
---|
167 | _Internal_error_Occurred( INTERNAL_ERROR_CORE, TRUE, 0xffff0000 ); |
---|
168 | |
---|
169 | return error; |
---|
170 | |
---|
171 | } |
---|