[56b7951] | 1 | /* |
---|
| 2 | * Test of Heap Walker |
---|
| 3 | * |
---|
[5f6a4a2] | 4 | * COPYRIGHT (c) 1989-2009. |
---|
[56b7951] | 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 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ |
---|
| 15 | #define CONFIGURE_INIT |
---|
| 16 | #include "system.h" |
---|
| 17 | |
---|
| 18 | #include <stdlib.h> |
---|
| 19 | #include <inttypes.h> |
---|
| 20 | #include <errno.h> |
---|
[f4812a03] | 21 | #include <string.h> |
---|
[56b7951] | 22 | #include <rtems/score/heap.h> |
---|
[93f4ac26] | 23 | #include <rtems/dumpbuf.h> |
---|
[56b7951] | 24 | |
---|
| 25 | #define TEST_HEAP_SIZE 1024 |
---|
[f4812a03] | 26 | uint32_t TestHeapMemory[TEST_HEAP_SIZE]; |
---|
[56b7951] | 27 | Heap_Control TestHeap; |
---|
| 28 | |
---|
| 29 | void test_heap_init(void) |
---|
| 30 | { |
---|
[f4812a03] | 31 | memset( TestHeapMemory, '\0', sizeof(TestHeapMemory) ); |
---|
| 32 | _Heap_Initialize( &TestHeap, TestHeapMemory, sizeof(TestHeapMemory), 0 ); |
---|
| 33 | } |
---|
| 34 | |
---|
[5f6a4a2] | 35 | void test_heap_walk_body( int source, bool do_dump ); |
---|
| 36 | |
---|
| 37 | void test_heap_walk( int source ) |
---|
| 38 | { |
---|
| 39 | test_heap_walk_body( source, true ); |
---|
| 40 | test_heap_walk_body( source, false ); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void test_heap_walk_body( int source, bool do_dump ) |
---|
[f4812a03] | 44 | { |
---|
| 45 | int i, j, original; |
---|
| 46 | |
---|
| 47 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
[5f6a4a2] | 48 | |
---|
[f4812a03] | 49 | /* |
---|
| 50 | * Now corrupt all non-zero values |
---|
| 51 | */ |
---|
| 52 | for (i=0 ; i<TEST_HEAP_SIZE ; i++) { |
---|
| 53 | original = TestHeapMemory[i]; |
---|
| 54 | if ( original == 0 ) |
---|
| 55 | continue; |
---|
| 56 | |
---|
| 57 | /* mark it free -- may or may not have already been */ |
---|
[5f6a4a2] | 58 | TestHeapMemory[i] &= ~0x01; |
---|
[f4812a03] | 59 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
| 60 | |
---|
| 61 | /* mark it used -- may or may not have already been */ |
---|
[5f6a4a2] | 62 | TestHeapMemory[i] |= 0x01; |
---|
[f4812a03] | 63 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
| 64 | |
---|
[5f6a4a2] | 65 | /* try values of 2-7 in the last three bits -- push alignment issues */ |
---|
| 66 | for (j=2 ; j<=7 ; j++) { |
---|
| 67 | TestHeapMemory[i] = (TestHeapMemory[i] & ~0x7) | j; |
---|
| 68 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
[f4812a03] | 72 | /* try 0 and see what that does */ |
---|
[5f6a4a2] | 73 | TestHeapMemory[i] = 0x00; |
---|
[f4812a03] | 74 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
| 75 | |
---|
| 76 | /* try 0xffffffff and see what that does */ |
---|
[5f6a4a2] | 77 | TestHeapMemory[i] = 0xffffffff; |
---|
[f4812a03] | 78 | _Heap_Walk( &TestHeap, source, do_dump ); |
---|
| 79 | |
---|
| 80 | TestHeapMemory[i] = original; |
---|
| 81 | } |
---|
[56b7951] | 82 | } |
---|
| 83 | |
---|
| 84 | void test_walk_freshly_initialized(void) |
---|
| 85 | { |
---|
| 86 | puts( "Walk freshly initialized heap" ); |
---|
| 87 | test_heap_init(); |
---|
[5f6a4a2] | 88 | test_heap_walk(1); |
---|
[c30fcf52] | 89 | } |
---|
[56b7951] | 90 | |
---|
[c30fcf52] | 91 | void test_negative_source_value(void) |
---|
| 92 | { |
---|
[f4812a03] | 93 | /* |
---|
| 94 | * Passing a negative value for source so that |
---|
| 95 | * the control enters the if block on line 67 |
---|
| 96 | */ |
---|
[c30fcf52] | 97 | puts( "Passing negative value for source" ); |
---|
[93f4ac26] | 98 | test_heap_init(); |
---|
[5f6a4a2] | 99 | test_heap_walk( -1 ); |
---|
[c30fcf52] | 100 | } |
---|
| 101 | |
---|
| 102 | void test_prev_block_flag_check(void) |
---|
| 103 | { |
---|
| 104 | /* Calling heapwalk without initialising the heap. |
---|
| 105 | * Covers line 80 and 85 on heapwalk. |
---|
| 106 | * Actually covers more than that. |
---|
| 107 | */ |
---|
| 108 | puts( "Calling Heap Walk without initialising" ); |
---|
[5f6a4a2] | 109 | test_heap_walk( 1 ); |
---|
[93f4ac26] | 110 | } |
---|
| 111 | |
---|
| 112 | void test_not_aligned(void) |
---|
| 113 | { |
---|
| 114 | /* |
---|
| 115 | * Hack to get to the error case where the blocks are |
---|
| 116 | * not on the page size. We initialize a heap with |
---|
| 117 | * default settings and change the page size to something |
---|
| 118 | * large. |
---|
| 119 | */ |
---|
| 120 | puts( "Testing case of blocks not on page size" ); |
---|
| 121 | test_heap_init(); |
---|
| 122 | TestHeap.page_size = 128; |
---|
[5f6a4a2] | 123 | test_heap_walk( -1 ); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void test_first_block_not_aligned(void) |
---|
| 127 | { |
---|
| 128 | /* |
---|
| 129 | * Hack to get to the error case where the blocks are |
---|
| 130 | * not on the page size. We initialize a heap with |
---|
| 131 | * default settings and change the page size to something |
---|
| 132 | * large. |
---|
| 133 | */ |
---|
| 134 | puts( "Testing case of blocks not on page size" ); |
---|
| 135 | test_heap_init(); |
---|
| 136 | _Heap_Head(&TestHeap)->next = (void *)1; |
---|
| 137 | test_heap_walk( -1 ); |
---|
[93f4ac26] | 138 | } |
---|
| 139 | |
---|
| 140 | void test_not_in_free_list(void) |
---|
| 141 | { |
---|
| 142 | void *p1, *p2, *p3; |
---|
| 143 | |
---|
| 144 | /* |
---|
| 145 | * Hack to get to the error case where the blocks are |
---|
| 146 | * not on the page size. We initialize a heap with |
---|
| 147 | * default settings and change the page size to something |
---|
| 148 | * large. |
---|
| 149 | */ |
---|
| 150 | puts( "Testing case of blocks not on page size" ); |
---|
| 151 | test_heap_init(); |
---|
| 152 | p1 =_Heap_Allocate( &TestHeap, 32 ); |
---|
| 153 | p2 =_Heap_Allocate( &TestHeap, 32 ); |
---|
| 154 | p3 =_Heap_Allocate( &TestHeap, 32 ); |
---|
| 155 | _Heap_Free( &TestHeap, p2 ); |
---|
[5f6a4a2] | 156 | test_heap_walk( -1 ); |
---|
[56b7951] | 157 | } |
---|
| 158 | |
---|
| 159 | rtems_task Init( |
---|
| 160 | rtems_task_argument argument |
---|
| 161 | ) |
---|
| 162 | { |
---|
| 163 | puts( "\n\n*** HEAP WALK TEST ***" ); |
---|
| 164 | |
---|
[c30fcf52] | 165 | test_prev_block_flag_check(); |
---|
[56b7951] | 166 | test_walk_freshly_initialized(); |
---|
[c30fcf52] | 167 | test_negative_source_value(); |
---|
[93f4ac26] | 168 | test_not_aligned(); |
---|
| 169 | test_not_in_free_list(); |
---|
[5f6a4a2] | 170 | test_first_block_not_aligned(); |
---|
[56b7951] | 171 | |
---|
| 172 | puts( "*** END OF HEAP WALK TEST ***" ); |
---|
| 173 | rtems_test_exit(0); |
---|
| 174 | } |
---|