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

4.104.115
Last change on this file since d81bfed2 was 384f0225, checked in by Joel Sherrill <joel.sherrill@…>, on 08/09/09 at 17:31:48

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

  • malloctest/init.c: Adjust allocation in test so it passes now that heap overhead constant has been increased.
  • Property mode set to 100644
File size: 10.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-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, 496 );
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
255void test_heap_resize(void)
256{
257  Heap_Resize_status  rc;
258  void               *p1;
259  intptr_t            oldsize;
260  intptr_t            avail;
261
262  puts( "Initialize Test Heap" );
263  test_heap_init();
264 
265  puts( "Allocate most of heap" );
266  p1 = _Heap_Allocate( &TestHeap, TEST_HEAP_SIZE - 32 );
267  rtems_test_assert( p1 != NULL );
268
269  puts( "Resize (shrink) the area to 8 bytes to ensure remainder gets freed" );
270  rc = _Heap_Resize_block( &TestHeap, p1, 8, &oldsize, &avail );
271  rtems_test_assert( rc == HEAP_RESIZE_SUCCESSFUL );
272}
273
274/*
275 *  A simple test of posix_memalign
276 */
277void test_posix_memalign(void)
278{
279  void *p1, *p2;
280  int i;
281  int sc;
282  int maximumShift;
283
284  puts( "posix_memalign - NULL return pointer -- EINVAL" );
285  sc = posix_memalign( NULL, 32, 8 );
286  fatal_posix_service_status( sc, EINVAL, "posix_memalign NULL pointer" );
287
288  puts( "posix_memalign - alignment of 0 -- EINVAL" );
289  sc = posix_memalign( &p1, 0, 8 );
290  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 0" );
291
292  puts( "posix_memalign - alignment  of 2-- EINVAL" );
293  sc = posix_memalign( &p1, 2, 8 );
294  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 2" );
295
296  if ( sizeof(int) == 4 )
297    maximumShift = 31;
298  else if ( sizeof(int) == 2 )
299    maximumShift = 15;
300  else {
301    printf( "Unsupported int size == %d\n", sizeof(int) );
302    rtems_test_exit(0);
303  }
304  for ( i=2 ; i<maximumShift ; i++ ) {
305    printf( "posix_memalign - alignment of %" PRId32 " -- OK\n",
306      (int32_t) 1 << i );
307    sc = posix_memalign( &p1, 1 << i, 8 );
308    if ( sc == ENOMEM ) {
309      printf( "posix_memalign - ran out of memory trying %d\n", 1<<i );
310      break;
311    }
312    posix_service_failed( sc, "posix_memalign alignment OK" );
313
314    free( p1 );
315  }
316  for ( ; i<maximumShift ; i++ ) {
317    printf( "posix_memalign - alignment of %" PRId32 " -- SKIPPED\n",
318      (int32_t) 1 << i );
319  }
320
321}
322
323rtems_task Init(
324  rtems_task_argument argument
325)
326{
327  rtems_time_of_day time;
328  rtems_status_code status;
329
330  puts( "\n\n*** MALLOC TEST ***" );
331
332  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
333  status = rtems_clock_set( &time );
334  directive_failed( status, "rtems_clock_set" );
335
336  test_realloc();
337  test_heap_cases_1();
338  test_heap_extend();
339  test_heap_info();
340  test_protected_heap_info();
341  test_heap_resize();
342
343  test_posix_memalign();
344
345  Task_name[ 1 ] = rtems_build_name( 'T', 'A', '1', ' ' );
346  Task_name[ 2 ] = rtems_build_name( 'T', 'A', '2', ' ' );
347  Task_name[ 3 ] = rtems_build_name( 'T', 'A', '3', ' ' );
348  Task_name[ 4 ] = rtems_build_name( 'T', 'A', '4', ' ' );
349  Task_name[ 5 ] = rtems_build_name( 'T', 'A', '5', ' ' );
350
351  status = rtems_task_create(
352     Task_name[ 1 ],
353     1,
354     TASK_STACK_SIZE,
355     RTEMS_DEFAULT_MODES,
356     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
357     &Task_id[ 1 ]
358  );
359  directive_failed( status, "rtems_task_create of TA1" );
360
361  status = rtems_task_create(
362     Task_name[ 2 ],
363     1,
364     TASK_STACK_SIZE,
365     RTEMS_DEFAULT_MODES,
366     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
367     &Task_id[ 2 ]
368  );
369  directive_failed( status, "rtems_task_create of TA2" );
370
371  status = rtems_task_create(
372     Task_name[ 3 ],
373     1,
374     TASK_STACK_SIZE,
375     RTEMS_DEFAULT_MODES,
376     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
377     &Task_id[ 3 ]
378  );
379  directive_failed( status, "rtems_task_create of TA3" );
380
381  status = rtems_task_create(
382     Task_name[ 4 ],
383     1,
384     TASK_STACK_SIZE,
385     RTEMS_DEFAULT_MODES,
386     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
387     &Task_id[ 4 ]
388  );
389  directive_failed( status, "rtems_task_create of TA4" );
390
391  status = rtems_task_create(
392     Task_name[ 5 ],
393     1,
394     TASK_STACK_SIZE,
395     RTEMS_DEFAULT_MODES,
396     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
397     &Task_id[ 5 ]
398  );
399  directive_failed( status, "rtems_task_create of TA5" );
400
401  status = rtems_task_start( Task_id[ 1 ], Task_1_through_5, 0 );
402  directive_failed( status, "rtems_task_start of TA1" );
403
404  status = rtems_task_start( Task_id[ 2 ], Task_1_through_5, 0 );
405  directive_failed( status, "rtems_task_start of TA2" );
406
407  status = rtems_task_start( Task_id[ 3 ], Task_1_through_5, 0 );
408  directive_failed( status, "rtems_task_start of TA3" );
409
410  status = rtems_task_start( Task_id[ 4 ], Task_1_through_5, 0 );
411  directive_failed( status, "rtems_task_start of TA4" );
412
413  status = rtems_task_start( Task_id[ 5 ], Task_1_through_5, 0 );
414  directive_failed( status, "rtems_task_start of TA5" );
415
416  status = rtems_task_delete( RTEMS_SELF );
417  directive_failed( status, "rtems_task_delete of RTEMS_SELF" );
418}
Note: See TracBrowser for help on using the repository browser.