1 | /* Init |
---|
2 | * |
---|
3 | * This routine is the initialization task for this test program. |
---|
4 | * It is a user initialization task and has the responsibility for creating |
---|
5 | * and starting the tasks that make up the test. If the time of day |
---|
6 | * clock is required for the test, it should also be set to a known |
---|
7 | * value by this function. |
---|
8 | * |
---|
9 | * Input parameters: |
---|
10 | * argument - task argument |
---|
11 | * |
---|
12 | * Output parameters: NONE |
---|
13 | * |
---|
14 | * COPYRIGHT (c) 1989-2007. |
---|
15 | * On-Line Applications Research Corporation (OAR). |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.com/license/LICENSE. |
---|
20 | * |
---|
21 | * $Id$ |
---|
22 | */ |
---|
23 | |
---|
24 | #define TEST_INIT |
---|
25 | #include "system.h" |
---|
26 | |
---|
27 | #include <stdlib.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <rtems/score/protectedheap.h> |
---|
30 | |
---|
31 | /* |
---|
32 | * A simple test of realloc |
---|
33 | */ |
---|
34 | void test_realloc(void) |
---|
35 | { |
---|
36 | void *p1, *p2, *p3; |
---|
37 | int i; |
---|
38 | int sc; |
---|
39 | |
---|
40 | /* Test growing reallocation "in place" */ |
---|
41 | p1 = malloc(1); |
---|
42 | for (i=2 ; i<2048 ; i++) { |
---|
43 | p2 = realloc(p1, i); |
---|
44 | if (p2 != p1) |
---|
45 | printf( "realloc - failed grow in place: " |
---|
46 | "%p != realloc(%p,%d)\n", p1, p2, i ); |
---|
47 | p1 = p2; |
---|
48 | } |
---|
49 | free(p1); |
---|
50 | |
---|
51 | /* Test shrinking reallocation "in place" */ |
---|
52 | p1 = malloc(2048); |
---|
53 | for (i=2047 ; i>=1; i--) { |
---|
54 | p2 = realloc(p1, i); |
---|
55 | if (p2 != p1) |
---|
56 | printf( "realloc - failed shrink in place: " |
---|
57 | "%p != realloc(%p,%d)\n", p1, p2, i ); |
---|
58 | p1 = p2; |
---|
59 | } |
---|
60 | free(p1); |
---|
61 | |
---|
62 | /* Test realloc that should fail "in place", i.e., |
---|
63 | * fallback to free()--malloc() |
---|
64 | */ |
---|
65 | p1 = malloc(32); |
---|
66 | p2 = malloc(32); |
---|
67 | p3 = realloc(p1, 64); |
---|
68 | if (p3 == p1 || p3 == NULL) |
---|
69 | printf( |
---|
70 | "realloc - failed non-in place: realloc(%p,%d) = %p\n", p1, 64, p3 ); |
---|
71 | free(p3); |
---|
72 | free(p2); |
---|
73 | |
---|
74 | /* |
---|
75 | * Yet another case |
---|
76 | */ |
---|
77 | p1 = malloc(8); |
---|
78 | p2 = malloc(8); |
---|
79 | free(p1); |
---|
80 | sc = posix_memalign(&p1, 16, 32); |
---|
81 | if (!sc) |
---|
82 | free(p1); |
---|
83 | |
---|
84 | /* |
---|
85 | * Bad hack to get coverage |
---|
86 | */ |
---|
87 | |
---|
88 | { |
---|
89 | void *p5; |
---|
90 | extern Heap_Control RTEMS_Malloc_Heap; |
---|
91 | p5 = _Protected_heap_Allocate_aligned( &RTEMS_Malloc_Heap, 8, 0 ); |
---|
92 | if ( p5 ) |
---|
93 | free( p5 ); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /* |
---|
98 | * A simple test of posix_memalign |
---|
99 | */ |
---|
100 | void test_posix_memalign(void) |
---|
101 | { |
---|
102 | void *p1, *p2; |
---|
103 | int i; |
---|
104 | int sc; |
---|
105 | |
---|
106 | puts( "posix_memalign - NULL return pointer -- EINVAL" ); |
---|
107 | sc = posix_memalign( NULL, 32, 8 ); |
---|
108 | fatal_posix_service_status( sc, EINVAL, "posix_memalign NULL pointer" ); |
---|
109 | |
---|
110 | puts( "posix_memalign - alignment of 0 -- EINVAL" ); |
---|
111 | sc = posix_memalign( &p1, 0, 8 ); |
---|
112 | fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 0" ); |
---|
113 | |
---|
114 | puts( "posix_memalign - alignment of 2-- EINVAL" ); |
---|
115 | sc = posix_memalign( &p1, 2, 8 ); |
---|
116 | fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 2" ); |
---|
117 | |
---|
118 | for ( i=2 ; i<32 ; i++ ) { |
---|
119 | printf( "posix_memalign - alignment of %d -- OK\n", 1 << i ); |
---|
120 | sc = posix_memalign( &p1, 1 << i, 8 ); |
---|
121 | if ( sc == ENOMEM ) { |
---|
122 | printf( "posix_memalign - ran out of memory trying %d\n", 1<<i ); |
---|
123 | break; |
---|
124 | } |
---|
125 | posix_service_failed( sc, "posix_memalign alignment OK" ); |
---|
126 | |
---|
127 | free( p1 ); |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | rtems_task Init( |
---|
133 | rtems_task_argument argument |
---|
134 | ) |
---|
135 | { |
---|
136 | rtems_time_of_day time; |
---|
137 | rtems_status_code status; |
---|
138 | |
---|
139 | puts( "\n\n*** MALLOC TEST ***" ); |
---|
140 | |
---|
141 | build_time( &time, 12, 31, 1988, 9, 0, 0, 0 ); |
---|
142 | status = rtems_clock_set( &time ); |
---|
143 | directive_failed( status, "rtems_clock_set" ); |
---|
144 | |
---|
145 | test_realloc(); |
---|
146 | |
---|
147 | test_posix_memalign(); |
---|
148 | |
---|
149 | Task_name[ 1 ] = rtems_build_name( 'T', 'A', '1', ' ' ); |
---|
150 | Task_name[ 2 ] = rtems_build_name( 'T', 'A', '2', ' ' ); |
---|
151 | Task_name[ 3 ] = rtems_build_name( 'T', 'A', '3', ' ' ); |
---|
152 | Task_name[ 4 ] = rtems_build_name( 'T', 'A', '4', ' ' ); |
---|
153 | Task_name[ 5 ] = rtems_build_name( 'T', 'A', '5', ' ' ); |
---|
154 | |
---|
155 | status = rtems_task_create( |
---|
156 | Task_name[ 1 ], |
---|
157 | 1, |
---|
158 | TASK_STACK_SIZE, |
---|
159 | RTEMS_DEFAULT_MODES, |
---|
160 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
161 | &Task_id[ 1 ] |
---|
162 | ); |
---|
163 | directive_failed( status, "rtems_task_create of TA1" ); |
---|
164 | |
---|
165 | status = rtems_task_create( |
---|
166 | Task_name[ 2 ], |
---|
167 | 1, |
---|
168 | TASK_STACK_SIZE, |
---|
169 | RTEMS_DEFAULT_MODES, |
---|
170 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
171 | &Task_id[ 2 ] |
---|
172 | ); |
---|
173 | directive_failed( status, "rtems_task_create of TA2" ); |
---|
174 | |
---|
175 | status = rtems_task_create( |
---|
176 | Task_name[ 3 ], |
---|
177 | 1, |
---|
178 | TASK_STACK_SIZE, |
---|
179 | RTEMS_DEFAULT_MODES, |
---|
180 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
181 | &Task_id[ 3 ] |
---|
182 | ); |
---|
183 | directive_failed( status, "rtems_task_create of TA3" ); |
---|
184 | |
---|
185 | status = rtems_task_create( |
---|
186 | Task_name[ 4 ], |
---|
187 | 1, |
---|
188 | TASK_STACK_SIZE, |
---|
189 | RTEMS_DEFAULT_MODES, |
---|
190 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
191 | &Task_id[ 4 ] |
---|
192 | ); |
---|
193 | directive_failed( status, "rtems_task_create of TA4" ); |
---|
194 | |
---|
195 | status = rtems_task_create( |
---|
196 | Task_name[ 5 ], |
---|
197 | 1, |
---|
198 | TASK_STACK_SIZE, |
---|
199 | RTEMS_DEFAULT_MODES, |
---|
200 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
201 | &Task_id[ 5 ] |
---|
202 | ); |
---|
203 | directive_failed( status, "rtems_task_create of TA5" ); |
---|
204 | |
---|
205 | status = rtems_task_start( Task_id[ 1 ], Task_1_through_5, 0 ); |
---|
206 | directive_failed( status, "rtems_task_start of TA1" ); |
---|
207 | |
---|
208 | status = rtems_task_start( Task_id[ 2 ], Task_1_through_5, 0 ); |
---|
209 | directive_failed( status, "rtems_task_start of TA2" ); |
---|
210 | |
---|
211 | status = rtems_task_start( Task_id[ 3 ], Task_1_through_5, 0 ); |
---|
212 | directive_failed( status, "rtems_task_start of TA3" ); |
---|
213 | |
---|
214 | status = rtems_task_start( Task_id[ 4 ], Task_1_through_5, 0 ); |
---|
215 | directive_failed( status, "rtems_task_start of TA4" ); |
---|
216 | |
---|
217 | status = rtems_task_start( Task_id[ 5 ], Task_1_through_5, 0 ); |
---|
218 | directive_failed( status, "rtems_task_start of TA5" ); |
---|
219 | |
---|
220 | status = rtems_task_delete( RTEMS_SELF ); |
---|
221 | directive_failed( status, "rtems_task_delete of RTEMS_SELF" ); |
---|
222 | } |
---|