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

4.104.114.9
Last change on this file since f2a7fa02 was f2a7fa02, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 31, 2008 at 4:06:21 PM

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

  • malloctest/init.c: Add new test cases per Sergei. Make some of the stranger cases operate directly on a freshly initialized heap. This should make them more reproducible.
  • Property mode set to 100644
File size: 6.8 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 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 */
34void 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#define TEST_HEAP_SIZE 1024
103uint8_t TestHeapMemory[TEST_HEAP_SIZE];
104Heap_Control TestHeap;
105
106void test_heap_init()
107{
108  _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, 0 );
109}
110void test_heap_cases_1()
111{
112  void     *p1, *p2, *p3, *p4;
113  uint32_t  u1, u2;
114  Heap_Resize_status rsc;
115
116  /*
117   * Another odd case.  What we are trying to do from Sergei
118   *
119   * 32-bit CPU when CPU_ALIGNMENT = 4 (most targets have 8) with the
120   * code like this:
121   */
122  test_heap_init();
123  p1 = _Heap_Allocate( &TestHeap, 12 );
124  p2 = _Heap_Allocate( &TestHeap, 32 );
125  p3 = _Heap_Allocate( &TestHeap, 32 );
126  _Heap_Free( &TestHeap, p2 );
127  p2 = _Heap_Allocate_aligned( &TestHeap, 8, 28 );
128  _Heap_Free( &TestHeap, p1 );
129  _Heap_Free( &TestHeap, p2 );
130  _Heap_Free( &TestHeap, p3 );
131
132  /*
133   *  Odd case in resizing a block.  Again test case outline per Sergei
134   */
135  test_heap_init();
136  p1 = _Heap_Allocate( &TestHeap, 32 );
137  p2 = _Heap_Allocate( &TestHeap, 8 );
138  p3 = _Heap_Allocate( &TestHeap, 32 );
139  _Heap_Free( &TestHeap, p2 );
140  rsc = _Heap_Resize_block( &TestHeap, p1, 41, &u1, &u2 );
141  /* XXX what should we expect */
142  _Heap_Free( &TestHeap, p3 );
143  _Heap_Free( &TestHeap, p4 );
144}
145
146/*
147 *  A simple test of posix_memalign
148 */
149void test_posix_memalign(void)
150{
151  void *p1, *p2;
152  int i;
153  int sc;
154
155  puts( "posix_memalign - NULL return pointer -- EINVAL" );
156  sc = posix_memalign( NULL, 32, 8 );
157  fatal_posix_service_status( sc, EINVAL, "posix_memalign NULL pointer" );
158
159  puts( "posix_memalign - alignment of 0 -- EINVAL" );
160  sc = posix_memalign( &p1, 0, 8 );
161  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 0" );
162
163  puts( "posix_memalign - alignment  of 2-- EINVAL" );
164  sc = posix_memalign( &p1, 2, 8 );
165  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 2" );
166
167  for ( i=2 ; i<32 ; i++ ) {
168    printf( "posix_memalign - alignment of %d -- OK\n", 1 << i );
169    sc = posix_memalign( &p1, 1 << i, 8 );
170    if ( sc == ENOMEM ) {
171      printf( "posix_memalign - ran out of memory trying %d\n", 1<<i );
172      break;
173    }
174    posix_service_failed( sc, "posix_memalign alignment OK" );
175
176    free( p1 );
177  }
178
179}
180
181rtems_task Init(
182  rtems_task_argument argument
183)
184{
185  rtems_time_of_day time;
186  rtems_status_code status;
187
188  puts( "\n\n*** MALLOC TEST ***" );
189
190  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
191  status = rtems_clock_set( &time );
192  directive_failed( status, "rtems_clock_set" );
193
194  test_realloc();
195  test_heap_cases_1();
196
197  test_posix_memalign();
198
199  Task_name[ 1 ] = rtems_build_name( 'T', 'A', '1', ' ' );
200  Task_name[ 2 ] = rtems_build_name( 'T', 'A', '2', ' ' );
201  Task_name[ 3 ] = rtems_build_name( 'T', 'A', '3', ' ' );
202  Task_name[ 4 ] = rtems_build_name( 'T', 'A', '4', ' ' );
203  Task_name[ 5 ] = rtems_build_name( 'T', 'A', '5', ' ' );
204
205  status = rtems_task_create(
206     Task_name[ 1 ],
207     1,
208     TASK_STACK_SIZE,
209     RTEMS_DEFAULT_MODES,
210     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
211     &Task_id[ 1 ]
212  );
213  directive_failed( status, "rtems_task_create of TA1" );
214
215  status = rtems_task_create(
216     Task_name[ 2 ],
217     1,
218     TASK_STACK_SIZE,
219     RTEMS_DEFAULT_MODES,
220     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
221     &Task_id[ 2 ]
222  );
223  directive_failed( status, "rtems_task_create of TA2" );
224
225  status = rtems_task_create(
226     Task_name[ 3 ],
227     1,
228     TASK_STACK_SIZE,
229     RTEMS_DEFAULT_MODES,
230     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
231     &Task_id[ 3 ]
232  );
233  directive_failed( status, "rtems_task_create of TA3" );
234
235  status = rtems_task_create(
236     Task_name[ 4 ],
237     1,
238     TASK_STACK_SIZE,
239     RTEMS_DEFAULT_MODES,
240     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
241     &Task_id[ 4 ]
242  );
243  directive_failed( status, "rtems_task_create of TA4" );
244
245  status = rtems_task_create(
246     Task_name[ 5 ],
247     1,
248     TASK_STACK_SIZE,
249     RTEMS_DEFAULT_MODES,
250     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
251     &Task_id[ 5 ]
252  );
253  directive_failed( status, "rtems_task_create of TA5" );
254
255  status = rtems_task_start( Task_id[ 1 ], Task_1_through_5, 0 );
256  directive_failed( status, "rtems_task_start of TA1" );
257
258  status = rtems_task_start( Task_id[ 2 ], Task_1_through_5, 0 );
259  directive_failed( status, "rtems_task_start of TA2" );
260
261  status = rtems_task_start( Task_id[ 3 ], Task_1_through_5, 0 );
262  directive_failed( status, "rtems_task_start of TA3" );
263
264  status = rtems_task_start( Task_id[ 4 ], Task_1_through_5, 0 );
265  directive_failed( status, "rtems_task_start of TA4" );
266
267  status = rtems_task_start( Task_id[ 5 ], Task_1_through_5, 0 );
268  directive_failed( status, "rtems_task_start of TA5" );
269
270  status = rtems_task_delete( RTEMS_SELF );
271  directive_failed( status, "rtems_task_delete of RTEMS_SELF" );
272}
Note: See TracBrowser for help on using the repository browser.