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, *p4; |
---|
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 | * Allocate with default alignment coverage |
---|
86 | */ |
---|
87 | sc = rtems_memalign( &p4, 0, 8 ); |
---|
88 | if ( !sc && p4 ) |
---|
89 | free( p4 ); |
---|
90 | |
---|
91 | /* |
---|
92 | * Walk the C Program Heap |
---|
93 | */ |
---|
94 | malloc_walk( 1234, 0 ); |
---|
95 | |
---|
96 | /* |
---|
97 | * Realloc with a bad pointer to force a point |
---|
98 | */ |
---|
99 | p4 = realloc( test_realloc, 32 ); |
---|
100 | |
---|
101 | /* |
---|
102 | * Another odd case. What we are trying to do from Sergei |
---|
103 | * |
---|
104 | * 32-bit CPU when CPU_ALIGNMENT = 4 (most targets have 8) with the |
---|
105 | * code like this: |
---|
106 | */ |
---|
107 | p1 = malloc(12); |
---|
108 | p2 = malloc(32); |
---|
109 | p3 = malloc(32); |
---|
110 | free(p2); |
---|
111 | sc = rtems_memalign(&p2, 8, 28); |
---|
112 | free(p1); |
---|
113 | free(p2); |
---|
114 | free(p3); |
---|
115 | |
---|
116 | /* |
---|
117 | * Odd case in resizing a block. Again test case outline per Sergei |
---|
118 | */ |
---|
119 | p1 = malloc(32); |
---|
120 | p2 = malloc(8); |
---|
121 | p3 = malloc(32); |
---|
122 | free(p2); |
---|
123 | p4 = realloc(p1, 42); |
---|
124 | free(p3); |
---|
125 | free(p4); |
---|
126 | |
---|
127 | } |
---|
128 | |
---|
129 | /* |
---|
130 | * A simple test of posix_memalign |
---|
131 | */ |
---|
132 | void test_posix_memalign(void) |
---|
133 | { |
---|
134 | void *p1, *p2; |
---|
135 | int i; |
---|
136 | int sc; |
---|
137 | |
---|
138 | puts( "posix_memalign - NULL return pointer -- EINVAL" ); |
---|
139 | sc = posix_memalign( NULL, 32, 8 ); |
---|
140 | fatal_posix_service_status( sc, EINVAL, "posix_memalign NULL pointer" ); |
---|
141 | |
---|
142 | puts( "posix_memalign - alignment of 0 -- EINVAL" ); |
---|
143 | sc = posix_memalign( &p1, 0, 8 ); |
---|
144 | fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 0" ); |
---|
145 | |
---|
146 | puts( "posix_memalign - alignment of 2-- EINVAL" ); |
---|
147 | sc = posix_memalign( &p1, 2, 8 ); |
---|
148 | fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 2" ); |
---|
149 | |
---|
150 | for ( i=2 ; i<32 ; i++ ) { |
---|
151 | printf( "posix_memalign - alignment of %d -- OK\n", 1 << i ); |
---|
152 | sc = posix_memalign( &p1, 1 << i, 8 ); |
---|
153 | if ( sc == ENOMEM ) { |
---|
154 | printf( "posix_memalign - ran out of memory trying %d\n", 1<<i ); |
---|
155 | break; |
---|
156 | } |
---|
157 | posix_service_failed( sc, "posix_memalign alignment OK" ); |
---|
158 | |
---|
159 | free( p1 ); |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | rtems_task Init( |
---|
165 | rtems_task_argument argument |
---|
166 | ) |
---|
167 | { |
---|
168 | rtems_time_of_day time; |
---|
169 | rtems_status_code status; |
---|
170 | |
---|
171 | puts( "\n\n*** MALLOC TEST ***" ); |
---|
172 | |
---|
173 | build_time( &time, 12, 31, 1988, 9, 0, 0, 0 ); |
---|
174 | status = rtems_clock_set( &time ); |
---|
175 | directive_failed( status, "rtems_clock_set" ); |
---|
176 | |
---|
177 | test_realloc(); |
---|
178 | |
---|
179 | test_posix_memalign(); |
---|
180 | |
---|
181 | Task_name[ 1 ] = rtems_build_name( 'T', 'A', '1', ' ' ); |
---|
182 | Task_name[ 2 ] = rtems_build_name( 'T', 'A', '2', ' ' ); |
---|
183 | Task_name[ 3 ] = rtems_build_name( 'T', 'A', '3', ' ' ); |
---|
184 | Task_name[ 4 ] = rtems_build_name( 'T', 'A', '4', ' ' ); |
---|
185 | Task_name[ 5 ] = rtems_build_name( 'T', 'A', '5', ' ' ); |
---|
186 | |
---|
187 | status = rtems_task_create( |
---|
188 | Task_name[ 1 ], |
---|
189 | 1, |
---|
190 | TASK_STACK_SIZE, |
---|
191 | RTEMS_DEFAULT_MODES, |
---|
192 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
193 | &Task_id[ 1 ] |
---|
194 | ); |
---|
195 | directive_failed( status, "rtems_task_create of TA1" ); |
---|
196 | |
---|
197 | status = rtems_task_create( |
---|
198 | Task_name[ 2 ], |
---|
199 | 1, |
---|
200 | TASK_STACK_SIZE, |
---|
201 | RTEMS_DEFAULT_MODES, |
---|
202 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
203 | &Task_id[ 2 ] |
---|
204 | ); |
---|
205 | directive_failed( status, "rtems_task_create of TA2" ); |
---|
206 | |
---|
207 | status = rtems_task_create( |
---|
208 | Task_name[ 3 ], |
---|
209 | 1, |
---|
210 | TASK_STACK_SIZE, |
---|
211 | RTEMS_DEFAULT_MODES, |
---|
212 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
213 | &Task_id[ 3 ] |
---|
214 | ); |
---|
215 | directive_failed( status, "rtems_task_create of TA3" ); |
---|
216 | |
---|
217 | status = rtems_task_create( |
---|
218 | Task_name[ 4 ], |
---|
219 | 1, |
---|
220 | TASK_STACK_SIZE, |
---|
221 | RTEMS_DEFAULT_MODES, |
---|
222 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
223 | &Task_id[ 4 ] |
---|
224 | ); |
---|
225 | directive_failed( status, "rtems_task_create of TA4" ); |
---|
226 | |
---|
227 | status = rtems_task_create( |
---|
228 | Task_name[ 5 ], |
---|
229 | 1, |
---|
230 | TASK_STACK_SIZE, |
---|
231 | RTEMS_DEFAULT_MODES, |
---|
232 | RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT, |
---|
233 | &Task_id[ 5 ] |
---|
234 | ); |
---|
235 | directive_failed( status, "rtems_task_create of TA5" ); |
---|
236 | |
---|
237 | status = rtems_task_start( Task_id[ 1 ], Task_1_through_5, 0 ); |
---|
238 | directive_failed( status, "rtems_task_start of TA1" ); |
---|
239 | |
---|
240 | status = rtems_task_start( Task_id[ 2 ], Task_1_through_5, 0 ); |
---|
241 | directive_failed( status, "rtems_task_start of TA2" ); |
---|
242 | |
---|
243 | status = rtems_task_start( Task_id[ 3 ], Task_1_through_5, 0 ); |
---|
244 | directive_failed( status, "rtems_task_start of TA3" ); |
---|
245 | |
---|
246 | status = rtems_task_start( Task_id[ 4 ], Task_1_through_5, 0 ); |
---|
247 | directive_failed( status, "rtems_task_start of TA4" ); |
---|
248 | |
---|
249 | status = rtems_task_start( Task_id[ 5 ], Task_1_through_5, 0 ); |
---|
250 | directive_failed( status, "rtems_task_start of TA5" ); |
---|
251 | |
---|
252 | status = rtems_task_delete( RTEMS_SELF ); |
---|
253 | directive_failed( status, "rtems_task_delete of RTEMS_SELF" ); |
---|
254 | } |
---|