source: rtems/testsuites/libtests/malloctest/init.c @ 4b6c378

4.104.115
Last change on this file since 4b6c378 was 4b6c378, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 8, 2009 at 7:15:04 PM

2009-07-08 Joel Sherrill <joel.sherrill@…>

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