source: rtems/testsuites/libtests/malloctest/init.c @ dfaa1a7

4.104.114.9
Last change on this file since dfaa1a7 was dfaa1a7, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 31, 2008 at 10:14:04 PM

2008-01-31 Joel Sherrill <joel.sherrill@…>

  • malloctest/init.c: Add more test cases to cover heap better.
  • Property mode set to 100644
File size: 8.7 KB
Line 
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-2008.
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 __RTEMS_VIOLATE_KERNEL_VISIBILITY__
25#define TEST_INIT
26#include "system.h"
27
28#include <stdlib.h>
29#include <errno.h>
30#include <rtems/score/protectedheap.h>
31
32/*
33 *  A simple test of realloc
34 */
35void test_realloc(void)
36{
37  void *p1, *p2, *p3, *p4;
38  int i;
39  int sc;
40
41  /* Test growing reallocation "in place" */
42  p1 = malloc(1);
43  for (i=2 ; i<2048 ; i++) {
44    p2 = realloc(p1, i);
45    if (p2 != p1)
46      printf( "realloc - failed grow in place: "
47              "%p != realloc(%p,%d)\n", p1, p2, i );
48    p1 = p2;
49  }
50  free(p1);
51
52  /* Test shrinking reallocation "in place" */
53  p1 = malloc(2048);
54  for (i=2047 ; i>=1; i--)  {
55    p2 = realloc(p1, i);
56    if (p2 != p1)
57      printf( "realloc - failed shrink in place: "
58              "%p != realloc(%p,%d)\n", p1, p2, i );
59    p1 = p2;
60  }
61  free(p1);
62
63  /* Test realloc that should fail "in place", i.e.,
64   * fallback to free()--malloc()
65   */
66  p1 = malloc(32);
67  p2 = malloc(32);
68  p3 = realloc(p1, 64);
69  if (p3 == p1 || p3 == NULL)
70    printf(
71      "realloc - failed non-in place: realloc(%p,%d) = %p\n", p1, 64, p3 );
72  free(p3);
73  free(p2);
74
75  /*
76   *  Yet another case
77   */
78  p1 = malloc(8);
79  p2 = malloc(8);
80  free(p1);
81  sc = posix_memalign(&p1, 16, 32);
82  if (!sc)
83    free(p1);
84
85  /*
86   *  Allocate with default alignment coverage
87   */
88  sc = rtems_memalign( &p4, 0, 8 );
89  if ( !sc && p4 )
90    free( p4 );
91
92  /*
93   * Walk the C Program Heap
94   */
95  puts( "malloc_walk - normal path" );
96  malloc_walk( 1234, 0 );
97
98  puts( "malloc_walk - in critical section path" );
99  _Thread_Disable_dispatch();
100  malloc_walk( 1234, 0 );
101  _Thread_Enable_dispatch();
102
103  /*
104   *  Realloc with a bad pointer to force a point
105   */
106  p4 = realloc( test_realloc, 32 );
107}
108
109#define TEST_HEAP_SIZE 1024
110uint8_t TestHeapMemory[TEST_HEAP_SIZE];
111Heap_Control TestHeap;
112
113void test_heap_init()
114{
115  _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, 0 );
116}
117
118void test_heap_cases_1()
119{
120  void     *p1, *p2, *p3, *p4;
121  uint32_t  u1, u2;
122  Heap_Resize_status rsc;
123
124  /*
125   * Another odd case.  What we are trying to do from Sergei
126   *
127   * 32-bit CPU when CPU_ALIGNMENT = 4 (most targets have 8) with the
128   * code like this:
129   */
130  test_heap_init();
131  p1 = _Heap_Allocate( &TestHeap, 12 );
132  p2 = _Heap_Allocate( &TestHeap, 32 );
133  p3 = _Heap_Allocate( &TestHeap, 32 );
134  _Heap_Free( &TestHeap, p2 );
135  p2 = _Heap_Allocate_aligned( &TestHeap, 8, 28 );
136  _Heap_Free( &TestHeap, p1 );
137  _Heap_Free( &TestHeap, p2 );
138  _Heap_Free( &TestHeap, p3 );
139
140  /*
141   *  Odd case in resizing a block.  Again test case outline per Sergei
142   */
143  test_heap_init();
144  p1 = _Heap_Allocate( &TestHeap, 32 );
145  p2 = _Heap_Allocate( &TestHeap, 8 );
146  p3 = _Heap_Allocate( &TestHeap, 32 );
147  _Heap_Free( &TestHeap, p2 );
148  rsc = _Heap_Resize_block( &TestHeap, p1, 41, &u1, &u2 );
149  /* XXX what should we expect */
150  _Heap_Free( &TestHeap, p3 );
151  _Heap_Free( &TestHeap, p4 );
152}
153
154void test_heap_extend()
155{
156  void     *p1, *p2, *p3, *p4;
157  uint32_t  u1, u2;
158  boolean   ret;
159
160  /*
161   * Easier to hit extend with a dedicated heap.
162   *
163   */
164  _Heap_Initialize( &TestHeap, TestHeapMemory, 512, 0 );
165
166  puts( "heap extend - bad address" );
167  ret = _Protected_heap_Extend( &TestHeap, TestHeapMemory - 512, 512 );
168  rtems_test_assert( ret == FALSE );
169
170  puts( "heap extend - OK" );
171  ret = _Protected_heap_Extend( &TestHeap, &TestHeapMemory[ 512 ], 512 );
172  rtems_test_assert( ret == TRUE );
173}
174
175void test_heap_info(void)
176{
177  size_t                  s1, s2;
178  void                   *p1;
179  int                     sc;
180  Heap_Information_block  the_info;
181
182  s1 = malloc_free_space();
183  p1 = malloc( 512 );
184  s2 = malloc_free_space();
185  puts( "malloc_free_space - check malloc space drops after malloc" ); 
186  rtems_test_assert( s1 );
187  rtems_test_assert( s2 );
188  rtems_test_assert( s2 <= s1 );
189  free( p1 );
190
191  puts( "malloc_free_space - verify free space returns to previous value" );
192  s2 = malloc_free_space();
193  rtems_test_assert( s1 == s2 );
194
195  puts( "malloc_info - called with NULL\n" );
196  sc = malloc_info( NULL );
197  rtems_test_assert( sc == -1 );
198
199  puts( "malloc_info - check free space drops after malloc" );
200  sc = malloc_info( &the_info );
201  rtems_test_assert( sc == -1 );
202  s1 = the_info.Free.largest;
203
204  p1 = malloc( 512 );
205
206  sc = malloc_info( &the_info );
207  rtems_test_assert( sc == 0 );
208  s2 = the_info.Free.largest;
209
210  rtems_test_assert( s1 );
211  rtems_test_assert( s2 );
212  rtems_test_assert( s2 <= s1 );
213  free( p1 );
214
215  puts( "malloc_info - verify free space returns to previous value" );
216  sc = malloc_info( &the_info );
217  rtems_test_assert( sc == 0 );
218  rtems_test_assert( s1 == the_info.Free.largest );
219
220}
221
222/*
223 *  A simple test of posix_memalign
224 */
225void test_posix_memalign(void)
226{
227  void *p1, *p2;
228  int i;
229  int sc;
230
231  puts( "posix_memalign - NULL return pointer -- EINVAL" );
232  sc = posix_memalign( NULL, 32, 8 );
233  fatal_posix_service_status( sc, EINVAL, "posix_memalign NULL pointer" );
234
235  puts( "posix_memalign - alignment of 0 -- EINVAL" );
236  sc = posix_memalign( &p1, 0, 8 );
237  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 0" );
238
239  puts( "posix_memalign - alignment  of 2-- EINVAL" );
240  sc = posix_memalign( &p1, 2, 8 );
241  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 2" );
242
243  for ( i=2 ; i<32 ; i++ ) {
244    printf( "posix_memalign - alignment of %d -- OK\n", 1 << i );
245    sc = posix_memalign( &p1, 1 << i, 8 );
246    if ( sc == ENOMEM ) {
247      printf( "posix_memalign - ran out of memory trying %d\n", 1<<i );
248      break;
249    }
250    posix_service_failed( sc, "posix_memalign alignment OK" );
251
252    free( p1 );
253  }
254
255}
256
257rtems_task Init(
258  rtems_task_argument argument
259)
260{
261  rtems_time_of_day time;
262  rtems_status_code status;
263
264  puts( "\n\n*** MALLOC TEST ***" );
265
266  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
267  status = rtems_clock_set( &time );
268  directive_failed( status, "rtems_clock_set" );
269
270  test_realloc();
271  test_heap_cases_1();
272  test_heap_extend();
273  test_heap_info();
274
275  test_posix_memalign();
276
277  Task_name[ 1 ] = rtems_build_name( 'T', 'A', '1', ' ' );
278  Task_name[ 2 ] = rtems_build_name( 'T', 'A', '2', ' ' );
279  Task_name[ 3 ] = rtems_build_name( 'T', 'A', '3', ' ' );
280  Task_name[ 4 ] = rtems_build_name( 'T', 'A', '4', ' ' );
281  Task_name[ 5 ] = rtems_build_name( 'T', 'A', '5', ' ' );
282
283  status = rtems_task_create(
284     Task_name[ 1 ],
285     1,
286     TASK_STACK_SIZE,
287     RTEMS_DEFAULT_MODES,
288     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
289     &Task_id[ 1 ]
290  );
291  directive_failed( status, "rtems_task_create of TA1" );
292
293  status = rtems_task_create(
294     Task_name[ 2 ],
295     1,
296     TASK_STACK_SIZE,
297     RTEMS_DEFAULT_MODES,
298     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
299     &Task_id[ 2 ]
300  );
301  directive_failed( status, "rtems_task_create of TA2" );
302
303  status = rtems_task_create(
304     Task_name[ 3 ],
305     1,
306     TASK_STACK_SIZE,
307     RTEMS_DEFAULT_MODES,
308     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
309     &Task_id[ 3 ]
310  );
311  directive_failed( status, "rtems_task_create of TA3" );
312
313  status = rtems_task_create(
314     Task_name[ 4 ],
315     1,
316     TASK_STACK_SIZE,
317     RTEMS_DEFAULT_MODES,
318     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
319     &Task_id[ 4 ]
320  );
321  directive_failed( status, "rtems_task_create of TA4" );
322
323  status = rtems_task_create(
324     Task_name[ 5 ],
325     1,
326     TASK_STACK_SIZE,
327     RTEMS_DEFAULT_MODES,
328     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
329     &Task_id[ 5 ]
330  );
331  directive_failed( status, "rtems_task_create of TA5" );
332
333  status = rtems_task_start( Task_id[ 1 ], Task_1_through_5, 0 );
334  directive_failed( status, "rtems_task_start of TA1" );
335
336  status = rtems_task_start( Task_id[ 2 ], Task_1_through_5, 0 );
337  directive_failed( status, "rtems_task_start of TA2" );
338
339  status = rtems_task_start( Task_id[ 3 ], Task_1_through_5, 0 );
340  directive_failed( status, "rtems_task_start of TA3" );
341
342  status = rtems_task_start( Task_id[ 4 ], Task_1_through_5, 0 );
343  directive_failed( status, "rtems_task_start of TA4" );
344
345  status = rtems_task_start( Task_id[ 5 ], Task_1_through_5, 0 );
346  directive_failed( status, "rtems_task_start of TA5" );
347
348  status = rtems_task_delete( RTEMS_SELF );
349  directive_failed( status, "rtems_task_delete of RTEMS_SELF" );
350}
Note: See TracBrowser for help on using the repository browser.