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

4.104.11
Last change on this file since f6a41d2 was f6a41d2, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 6, 2009 at 3:24:56 PM

2009-09-03 Christian Mauderer <christian.mauderer@…>

  • malloctest/init.c: New test cases. Update for heap API changes.
  • stackchk/blow.c: Update for heap API changes.
  • Property mode set to 100644
File size: 22.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 <string.h>
30#include <inttypes.h>
31#include <errno.h>
32#include <rtems/score/protectedheap.h>
33
34/*
35 *  A simple test of realloc
36 */
37static void test_realloc(void)
38{
39  void *p1, *p2, *p3, *p4;
40  size_t i;
41  int sc;
42
43  /* Test growing reallocation "in place" */
44  p1 = malloc(1);
45  for (i=2 ; i<2048 ; i++) {
46    p2 = realloc(p1, i);
47    if (p2 != p1)
48      printf( "realloc - failed grow in place: "
49              "%p != realloc(%p,%d)\n", p1, p2, i);
50    p1 = p2;
51  }
52  free(p1);
53
54  /* Test shrinking reallocation "in place" */
55  p1 = malloc(2048);
56  for (i=2047 ; i>=1; i--)  {
57    p2 = realloc(p1, i);
58    if (p2 != p1)
59      printf( "realloc - failed shrink in place: "
60              "%p != realloc(%p,%d)\n", p1, p2, i);
61    p1 = p2;
62  }
63  free(p1);
64
65  /* Test realloc that should fail "in place", i.e.,
66   * fallback to free()-- malloc()
67   */
68  p1 = malloc(32);
69  p2 = malloc(32);
70  p3 = realloc(p1, 64);
71  if (p3 == p1 || p3 == NULL)
72    printf(
73      "realloc - failed non-in place: realloc(%p,%d) = %p\n", p1, 64, p3);
74  free(p3);
75  free(p2);
76
77  /*
78   *  Yet another case
79   */
80  p1 = malloc(8);
81  p2 = malloc(8);
82  free(p1);
83  sc = posix_memalign(&p1, 16, 32);
84  if (!sc)
85    free(p1);
86
87  /*
88   *  Allocate with default alignment coverage
89   */
90  sc = rtems_memalign( &p4, 0, 8 );
91  if ( !sc && p4 )
92    free( p4 );
93
94  /*
95   * Walk the C Program Heap
96   */
97  puts( "malloc_walk - normal path" );
98  malloc_walk( 1234, 0 );
99
100  puts( "malloc_walk - in critical section path" );
101  _Thread_Disable_dispatch();
102  malloc_walk( 1234, 0 );
103  _Thread_Enable_dispatch();
104
105  /*
106   *  Realloc with a bad pointer to force a point
107   */
108  p4 = realloc( test_realloc, 32 );
109}
110
111#define TEST_HEAP_SIZE 1024
112
113uint8_t TestHeapMemory[TEST_HEAP_SIZE];
114
115Heap_Control TestHeap;
116
117static void test_heap_default_init()
118{
119  memset( &TestHeapMemory, 0x7f, TEST_HEAP_SIZE );
120  _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, 0 );
121}
122
123static void test_free( void *addr )
124{
125  rtems_test_assert( _Heap_Free( &TestHeap, addr ) );
126}
127
128static void test_heap_cases_1()
129{
130  void     *p1, *p2, *p3, *p4;
131  intptr_t  u1, u2;
132  Heap_Resize_status rsc;
133
134  /*
135   * Another odd case.  What we are trying to do from Sergei
136   *
137   * 32-bit CPU when CPU_ALIGNMENT = 4 (most targets have 8) with the
138   * code like this:
139   */
140  test_heap_default_init();
141  p1 = _Heap_Allocate( &TestHeap, 12 );
142  p2 = _Heap_Allocate( &TestHeap, 32 );
143  p3 = _Heap_Allocate( &TestHeap, 32 );
144  test_free( p2 );
145  p2 = _Heap_Allocate_aligned( &TestHeap, 8, 28 );
146  test_free( p1 );
147  test_free( p2 );
148  test_free( p3 );
149
150  /*
151   *  Odd case in resizing a block.  Again test case outline per Sergei
152   */
153  test_heap_default_init();
154  p1 = _Heap_Allocate( &TestHeap, 32 );
155  p2 = _Heap_Allocate( &TestHeap, 8 );
156  p3 = _Heap_Allocate( &TestHeap, 32 );
157  test_free( p2 );
158  rsc = _Heap_Resize_block( &TestHeap, p1, 41, &u1, &u2 );
159  /* XXX what should we expect */
160  test_free( p3 );
161  test_free( p1 );
162 
163  /*
164   *  To tackle a special case of resizing a block in order to cover the
165   *  code in heapresizeblock.c
166   *
167   *  Re-initialise the heap, so that the blocks created from now on
168   *  are contiguous.
169   */
170  test_heap_default_init(); 
171  puts( "Heap Initialized" );
172  p1 = _Heap_Allocate( &TestHeap, 500 );
173  rtems_test_assert( p1 != NULL );
174  p2 = _Heap_Allocate( &TestHeap, 496 );
175  rtems_test_assert( p2 != NULL );
176  rsc = _Heap_Resize_block( &TestHeap, p1, 256, &u1, &u2 );
177  rtems_test_assert( rsc == HEAP_RESIZE_SUCCESSFUL );
178  test_free( p1 );
179  test_free( p2 ); 
180}
181
182#define TEST_DEFAULT_PAGE_SIZE 128
183
184static void test_heap_init(uintptr_t page_size )
185{
186  uintptr_t rv = 0;
187
188  memset( &TestHeapMemory, 0x7f, TEST_HEAP_SIZE );
189
190  rv = _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, page_size );
191  rtems_test_assert( rv > 0 );
192}
193
194static void test_check_alloc(
195  void *alloc_begin_ptr,
196  void *expected_alloc_begin_ptr,
197  uintptr_t alloc_size,
198  uintptr_t alignment,
199  uintptr_t boundary
200)
201{
202  uintptr_t const min_block_size = TestHeap.min_block_size;
203  uintptr_t const page_size = TestHeap.page_size;
204
205  rtems_test_assert( alloc_begin_ptr == expected_alloc_begin_ptr );
206
207  if( expected_alloc_begin_ptr != NULL ) {
208    uintptr_t const alloc_begin = (uintptr_t ) alloc_begin_ptr;
209    uintptr_t const alloc_end = alloc_begin + alloc_size;
210
211    uintptr_t const alloc_area_begin = _Heap_Align_down( alloc_begin, page_size );
212    uintptr_t const alloc_area_offset = alloc_begin - alloc_area_begin;
213    uintptr_t const alloc_area_size = alloc_area_offset + alloc_size;
214
215    Heap_Block *block = _Heap_Block_of_alloc_area( alloc_area_begin, page_size );
216    uintptr_t const block_begin = (uintptr_t ) block;
217    uintptr_t const block_size = _Heap_Block_size( block );
218    uintptr_t const block_end = block_begin + block_size;
219
220    rtems_test_assert( block_size >= min_block_size );
221    rtems_test_assert( block_begin < block_end );
222    rtems_test_assert(
223      _Heap_Is_aligned( block_begin + HEAP_BLOCK_HEADER_SIZE, page_size )
224    );
225    rtems_test_assert(
226      _Heap_Is_aligned( block_size, page_size )
227    );
228
229    rtems_test_assert( alloc_end <= block_end + HEAP_BLOCK_SIZE_OFFSET );
230    rtems_test_assert( alloc_area_begin > block_begin );
231    rtems_test_assert( alloc_area_offset < page_size );
232
233    rtems_test_assert( _Heap_Is_aligned( alloc_area_begin, page_size ) );
234    if ( alignment == 0 ) {
235      rtems_test_assert( alloc_begin == alloc_area_begin );
236    } else {
237      rtems_test_assert( _Heap_Is_aligned( alloc_begin, alignment ) );
238    }
239
240    if ( boundary != 0 ) {
241      uintptr_t boundary_line = _Heap_Align_down( alloc_end, boundary );
242
243      rtems_test_assert( alloc_size <= boundary );
244      rtems_test_assert(
245        boundary_line <= alloc_begin
246          || alloc_end <= boundary_line
247      );
248    }
249  }
250
251  rtems_test_assert(
252    page_size < CPU_ALIGNMENT
253      || _Heap_Walk( &TestHeap, 0, false )
254  );
255}
256
257static void test_check_alloc_simple(
258  void *alloc_begin_ptr,
259  uintptr_t alloc_size,
260  uintptr_t alignment,
261  uintptr_t boundary
262)
263{
264  test_check_alloc(
265    alloc_begin_ptr,
266    alloc_begin_ptr,
267    alloc_size,
268    alignment,
269    boundary
270  );
271}
272
273static void *test_alloc(
274  uintptr_t alloc_size,
275  uintptr_t alignment,
276  uintptr_t boundary,
277  void *expected_alloc_begin_ptr
278)
279{
280  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
281    &TestHeap,
282    alloc_size,
283    alignment,
284    boundary
285  );
286
287  test_check_alloc(
288    alloc_begin_ptr,
289    expected_alloc_begin_ptr,
290    alloc_size,
291    alignment,
292    boundary
293  );
294
295  return alloc_begin_ptr;
296}
297
298static void *test_alloc_simple(
299  uintptr_t alloc_size,
300  uintptr_t alignment,
301  uintptr_t boundary
302)
303{
304  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
305    &TestHeap,
306    alloc_size,
307    alignment,
308    boundary
309  );
310
311  test_check_alloc_simple(
312    alloc_begin_ptr,
313    alloc_size,
314    alignment,
315    boundary
316  );
317
318  rtems_test_assert( alloc_begin_ptr != NULL );
319
320  return alloc_begin_ptr;
321}
322
323static void *test_init_and_alloc(
324  uintptr_t alloc_size,
325  uintptr_t alignment,
326  uintptr_t boundary,
327  void *expected_alloc_begin_ptr
328)
329{
330  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
331
332  return test_alloc(
333    alloc_size,
334    alignment,
335    boundary,
336    expected_alloc_begin_ptr
337  );
338}
339
340static void *test_init_and_alloc_simple(
341  uintptr_t alloc_size,
342  uintptr_t alignment,
343  uintptr_t boundary
344)
345{
346  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
347
348  return test_alloc_simple(
349    alloc_size,
350    alignment,
351    boundary
352  );
353}
354
355static uintptr_t test_page_size(void)
356{
357  return TestHeap.page_size;
358}
359
360static void test_heap_cases_2()
361{
362  void *p1 = NULL;
363  void *p2 = NULL;
364  void *p3 = NULL;
365  uintptr_t alloc_size = 0;
366  uintptr_t alignment = 0;
367  uintptr_t boundary = 0;
368  uintptr_t page_size = 0;
369  uintptr_t first_page_begin = 0;
370  uintptr_t previous_last_block_begin = 0;
371  uintptr_t previous_last_page_begin = 0;
372
373  uintptr_t first_block_begin = 0;
374
375  uintptr_t last_block_begin = 0;
376  uintptr_t last_alloc_begin = 0;
377
378  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
379
380  first_block_begin = (uintptr_t) TestHeap.first_block;
381
382  last_block_begin = (uintptr_t) TestHeap.last_block;
383  last_alloc_begin = _Heap_Alloc_area_of_block( TestHeap.last_block );
384
385  puts( "run tests for _Heap_Allocate_aligned_with_boundary()");
386
387  puts( "\tcheck if NULL will be returned if size causes integer overflow" );
388
389  alloc_size = (uintptr_t ) -1;
390  alignment = 0;
391  boundary = 0;
392  test_init_and_alloc( alloc_size, alignment, boundary, NULL );
393
394  puts( "\ttry to allocate more space than the one which fits in the boundary" );
395
396  alloc_size = 2;
397  alignment = 0;
398  boundary = alloc_size - 1;
399  test_init_and_alloc( alloc_size, alignment, boundary, NULL );
400
401  puts( "\tcheck if alignment will be set to page size if only a boundary is given" );
402
403  alloc_size = 1;
404  boundary = 1;
405
406  alignment = 0;
407  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
408
409  alignment = test_page_size();
410  test_init_and_alloc( alloc_size, alignment, boundary, p1 );
411
412  puts( "\tcreate a block which is bigger then the first free space" );
413
414  alignment = 0;
415  boundary = 0;
416
417  alloc_size = test_page_size();
418  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
419  p2 = test_alloc_simple( alloc_size, alignment, boundary );
420  test_free( p1 );
421
422  alloc_size = 2 * alloc_size;
423  p3 = test_alloc_simple( alloc_size, alignment, boundary );
424  rtems_test_assert( p1 != p3 );
425
426  puts( "\tset boundary before allocation begin" );
427
428  alloc_size = 1;
429  alignment = 0;
430  boundary = last_alloc_begin - test_page_size();
431  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
432  rtems_test_assert( (uintptr_t ) p1 >= boundary );
433
434  puts( "\tset boundary between allocation begin and end" );
435  alloc_size = test_page_size();
436  alignment = 0;
437  boundary = last_alloc_begin - alloc_size / 2;
438  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
439  rtems_test_assert( (uintptr_t ) p1 + alloc_size <= boundary );
440
441  puts( "\tset boundary after allocation end" );
442  alloc_size = 1;
443  alignment = 0;
444  boundary = last_alloc_begin;
445  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
446  rtems_test_assert( (uintptr_t ) p1 + alloc_size < boundary );
447
448  puts( "\tset boundary on allocation end" );
449  alloc_size = TEST_DEFAULT_PAGE_SIZE - HEAP_BLOCK_HEADER_SIZE;
450  alignment = 0;
451  boundary = last_block_begin;
452  p1 = (void *) (last_alloc_begin - TEST_DEFAULT_PAGE_SIZE);
453  test_init_and_alloc( alloc_size, alignment, boundary, p1);
454
455  puts( "\talign the allocation to different positions in the block header" );
456
457  page_size = sizeof(uintptr_t);
458  alloc_size = 1;
459  boundary = 0;
460
461  test_heap_init( page_size );
462
463  /* Force the page size to a small enough value */
464  TestHeap.page_size = page_size;
465 
466  alignment = first_page_begin - sizeof(uintptr_t);
467  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
468 
469  first_page_begin = ((uintptr_t) TestHeap.first_block ) + HEAP_BLOCK_HEADER_SIZE;
470  alignment = first_page_begin + sizeof(uintptr_t);
471  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
472
473  first_page_begin = ((uintptr_t) TestHeap.first_block ) 
474          + HEAP_BLOCK_HEADER_SIZE;
475  alignment = first_page_begin;
476  p1 = test_alloc_simple( alloc_size, alignment, boundary );
477 
478  puts( "\tallocate last block with different boundarys" );
479  page_size = TEST_DEFAULT_PAGE_SIZE;
480  test_heap_init( page_size );
481  previous_last_block_begin = ((uintptr_t) TestHeap.last_block ) 
482          - TestHeap.min_block_size;
483  previous_last_page_begin = previous_last_block_begin
484          + HEAP_BLOCK_HEADER_SIZE;
485  alloc_size = TestHeap.page_size - HEAP_BLOCK_HEADER_SIZE;
486  alignment = sizeof(uintptr_t);
487  boundary = 0;
488  p1 = test_alloc( alloc_size, alignment, boundary, (void *) (previous_last_page_begin + sizeof(uintptr_t)));
489 
490  test_heap_init( page_size );
491  boundary = ((uintptr_t) TestHeap.last_block );
492  p1 = test_alloc( alloc_size, alignment, boundary, (void *) previous_last_page_begin );
493
494  puts( "\tbreak the boundaries and aligns more than one time" );
495
496  page_size = CPU_ALIGNMENT * 20;
497  alloc_size = page_size / 4;
498  alignment = page_size / 5;
499  boundary = page_size / 4;
500  test_heap_init( page_size );
501  p1 = (void *) (_Heap_Alloc_area_of_block( TestHeap.last_block ) - page_size );
502  test_alloc( alloc_size, alignment, boundary, p1);
503
504  puts( "\tdifferent combinations, so that there is no valid block at the end" );
505
506  page_size = sizeof(uintptr_t);
507
508  test_heap_init( 0 );
509
510  /* Force the page size to a small enough value */
511  TestHeap.page_size = page_size;
512
513  alloc_size = 1;
514  alignment = (uintptr_t) TestHeap.last_block;
515  boundary = 0;
516  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
517
518  boundary = (uintptr_t) TestHeap.last_block;
519  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
520 
521  alloc_size = 0;
522  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
523 
524  alloc_size = 1;
525  alignment = sizeof(uintptr_t);
526  boundary = 0;
527  p1 = test_alloc_simple( alloc_size, alignment, boundary );
528   
529  puts( "\ttry to create a block, which is not possible because of the alignment and boundary" );
530
531  alloc_size = 2;
532  boundary = _Heap_Alloc_area_of_block( TestHeap.first_block )
533          + _Heap_Block_size( TestHeap.first_block ) / 2;
534  alignment = boundary - 1;
535  p1 = test_init_and_alloc( alloc_size, alignment, boundary, NULL );
536
537  alloc_size = 2;
538  alignment = _Heap_Alloc_area_of_block( TestHeap.first_block );
539  boundary = alignment + 1;
540  p1 = test_init_and_alloc( alloc_size, alignment, boundary, NULL );
541}
542
543static void test_block_alloc( uintptr_t alloc_begin, uintptr_t alloc_size )
544{
545  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
546
547  _Heap_Block_allocate(
548    &TestHeap,
549    TestHeap.first_block,
550    alloc_begin,
551    alloc_size
552  );
553
554  test_check_alloc_simple( (void *) alloc_begin, alloc_size, 0, 0 );
555}
556
557static void test_heap_cases_block_allocate()
558{
559  uintptr_t alloc_begin = 0;
560  uintptr_t alloc_size = 0;
561  uintptr_t alloc_box_begin = 0;
562  uintptr_t alloc_box_end = 0;
563  uintptr_t alloc_box_size = 0;
564
565  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
566
567  alloc_box_begin = _Heap_Alloc_area_of_block( TestHeap.first_block );
568  alloc_box_size = _Heap_Block_size( TestHeap.first_block );
569  alloc_box_end = alloc_box_begin + alloc_box_size;
570
571  puts( "run tests for _Heap_Block_allocate()" );
572
573  puts( "\tallocate block at the beginning");
574  alloc_begin = alloc_box_begin;
575  alloc_size = 0;
576  test_block_alloc( alloc_begin, alloc_size );
577 
578  puts( "\tallocate block full space");
579  alloc_begin = alloc_box_begin;
580  alloc_size = alloc_box_size + HEAP_BLOCK_SIZE_OFFSET
581    - HEAP_BLOCK_HEADER_SIZE;
582  test_block_alloc( alloc_begin, alloc_size );
583
584  puts( "\tallocate block in the middle");
585  alloc_begin = alloc_box_begin + TEST_DEFAULT_PAGE_SIZE;
586  alloc_size = 0;
587  test_block_alloc( alloc_begin, alloc_size );
588 
589  puts( "\tallocate block at the end");
590  alloc_begin = alloc_box_end - TEST_DEFAULT_PAGE_SIZE;
591  alloc_size = TEST_DEFAULT_PAGE_SIZE + HEAP_BLOCK_SIZE_OFFSET
592    - HEAP_BLOCK_HEADER_SIZE;
593  test_block_alloc( alloc_begin, alloc_size );
594}
595
596static void test_heap_extend()
597{
598  void     *p1, *p2, *p3, *p4;
599  uint32_t  u1, u2;
600  bool      ret;
601
602  /*
603   * Easier to hit extend with a dedicated heap.
604   *
605   */
606  _Heap_Initialize( &TestHeap, TestHeapMemory, 512, 0 );
607
608  puts( "heap extend - bad address" );
609  ret = _Protected_heap_Extend( &TestHeap, TestHeapMemory - 512, 512 );
610  rtems_test_assert( ret == false );
611
612  puts( "heap extend - OK" );
613  ret = _Protected_heap_Extend( &TestHeap, &TestHeapMemory[ 512 ], 512 );
614  rtems_test_assert( ret == true );
615}
616
617static void test_heap_info(void)
618{
619  size_t                  s1, s2;
620  void                   *p1;
621  int                     sc;
622  Heap_Information_block  the_info;
623
624  s1 = malloc_free_space();
625  p1 = malloc( 512 );
626  s2 = malloc_free_space();
627  puts( "malloc_free_space - check malloc space drops after malloc" ); 
628  rtems_test_assert( s1 );
629  rtems_test_assert( s2 );
630  rtems_test_assert( s2 <= s1 );
631  free( p1 );
632
633  puts( "malloc_free_space - verify free space returns to previous value" );
634  s2 = malloc_free_space();
635  rtems_test_assert( s1 == s2 );
636
637  puts( "malloc_info - called with NULL\n" );
638  sc = malloc_info( NULL );
639  rtems_test_assert( sc == -1 );
640
641  puts( "malloc_info - check free space drops after malloc" );
642  sc = malloc_info( &the_info );
643  rtems_test_assert( sc == 0 );
644  s1 = the_info.Free.largest;
645
646  p1 = malloc( 512 );
647
648  sc = malloc_info( &the_info );
649  rtems_test_assert( sc == 0 );
650  s2 = the_info.Free.largest;
651
652  rtems_test_assert( s1 );
653  rtems_test_assert( s2 );
654  rtems_test_assert( s2 <= s1 );
655  free( p1 );
656
657  puts( "malloc_info - verify free space returns to previous value" );
658  sc = malloc_info( &the_info );
659  rtems_test_assert( sc == 0 );
660  rtems_test_assert( s1 == the_info.Free.largest );
661}
662
663static void test_protected_heap_info(void)
664{
665  Heap_Control           heap;
666  Heap_Information_block info;
667  bool                   rc;
668
669  puts( "_Protected_heap_Get_information - NULL heap" );
670  rc = _Protected_heap_Get_information( NULL, &info );
671  rtems_test_assert( rc == false );
672
673  puts( "_Protected_heap_Get_information - NULL info" );
674  rc = _Protected_heap_Get_information( &heap, NULL );
675  rtems_test_assert( rc == false );
676}
677
678static void test_heap_resize(void)
679{
680  Heap_Resize_status  rc;
681  void               *p1;
682  intptr_t            oldsize;
683  intptr_t            avail;
684
685  puts( "Initialize Test Heap" );
686  test_heap_default_init();
687 
688  puts( "Allocate most of heap" );
689  p1 = _Heap_Allocate( &TestHeap, TEST_HEAP_SIZE - 32 );
690  rtems_test_assert( p1 != NULL );
691
692  puts( "Resize (shrink) the area to 8 bytes to ensure remainder gets freed" );
693  rc = _Heap_Resize_block( &TestHeap, p1, 8, &oldsize, &avail );
694  rtems_test_assert( rc == HEAP_RESIZE_SUCCESSFUL );
695}
696
697/*
698 *  A simple test of posix_memalign
699 */
700static void test_posix_memalign(void)
701{
702  void *p1, *p2;
703  int i;
704  int sc;
705  int maximumShift;
706
707  puts( "posix_memalign - NULL return pointer -- EINVAL" );
708  sc = posix_memalign( NULL, 32, 8 );
709  fatal_posix_service_status( sc, EINVAL, "posix_memalign NULL pointer" );
710
711  puts( "posix_memalign - alignment of 0 -- EINVAL" );
712  sc = posix_memalign( &p1, 0, 8 );
713  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 0" );
714
715  puts( "posix_memalign - alignment  of 2-- EINVAL" );
716  sc = posix_memalign( &p1, 2, 8 );
717  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 2" );
718
719  if ( sizeof(int) == 4 )
720    maximumShift = 31;
721  else if ( sizeof(int) == 2 )
722    maximumShift = 15;
723  else {
724    printf( "Unsupported int size == %d\n", sizeof(int) );
725    rtems_test_exit(0);
726  }
727  for ( i=2 ; i<maximumShift ; i++ ) {
728    printf( "posix_memalign - alignment of %" PRId32 " -- OK\n",
729      (int32_t) 1 << i );
730    sc = posix_memalign( &p1, 1 << i, 8 );
731    if ( sc == ENOMEM ) {
732      printf( "posix_memalign - ran out of memory trying %d\n", 1<<i );
733      break;
734    }
735    posix_service_failed( sc, "posix_memalign alignment OK" );
736
737    free( p1 );
738  }
739  for ( ; i<maximumShift ; i++ ) {
740    printf( "posix_memalign - alignment of %" PRId32 " -- SKIPPED\n",
741      (int32_t) 1 << i );
742  }
743
744}
745
746rtems_task Init(
747  rtems_task_argument argument
748)
749{
750  rtems_time_of_day time;
751  rtems_status_code status;
752
753  puts( "\n\n*** MALLOC TEST ***" );
754
755  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
756  status = rtems_clock_set( &time );
757  directive_failed( status, "rtems_clock_set" );
758
759  test_heap_cases_2();
760  test_heap_cases_block_allocate();
761  test_realloc();
762  test_heap_cases_1();
763  test_heap_extend();
764  test_heap_info();
765  test_protected_heap_info();
766  test_heap_resize();
767
768  test_posix_memalign();
769
770  Task_name[ 1 ] = rtems_build_name( 'T', 'A', '1', ' ' );
771  Task_name[ 2 ] = rtems_build_name( 'T', 'A', '2', ' ' );
772  Task_name[ 3 ] = rtems_build_name( 'T', 'A', '3', ' ' );
773  Task_name[ 4 ] = rtems_build_name( 'T', 'A', '4', ' ' );
774  Task_name[ 5 ] = rtems_build_name( 'T', 'A', '5', ' ' );
775
776  status = rtems_task_create(
777     Task_name[ 1 ],
778     1,
779     TASK_STACK_SIZE,
780     RTEMS_DEFAULT_MODES,
781     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
782     &Task_id[ 1 ]
783  );
784  directive_failed( status, "rtems_task_create of TA1" );
785
786  status = rtems_task_create(
787     Task_name[ 2 ],
788     1,
789     TASK_STACK_SIZE,
790     RTEMS_DEFAULT_MODES,
791     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
792     &Task_id[ 2 ]
793  );
794  directive_failed( status, "rtems_task_create of TA2" );
795
796  status = rtems_task_create(
797     Task_name[ 3 ],
798     1,
799     TASK_STACK_SIZE,
800     RTEMS_DEFAULT_MODES,
801     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
802     &Task_id[ 3 ]
803  );
804  directive_failed( status, "rtems_task_create of TA3" );
805
806  status = rtems_task_create(
807     Task_name[ 4 ],
808     1,
809     TASK_STACK_SIZE,
810     RTEMS_DEFAULT_MODES,
811     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
812     &Task_id[ 4 ]
813  );
814  directive_failed( status, "rtems_task_create of TA4" );
815
816  status = rtems_task_create(
817     Task_name[ 5 ],
818     1,
819     TASK_STACK_SIZE,
820     RTEMS_DEFAULT_MODES,
821     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
822     &Task_id[ 5 ]
823  );
824  directive_failed( status, "rtems_task_create of TA5" );
825
826  status = rtems_task_start( Task_id[ 1 ], Task_1_through_5, 0 );
827  directive_failed( status, "rtems_task_start of TA1" );
828
829  status = rtems_task_start( Task_id[ 2 ], Task_1_through_5, 0 );
830  directive_failed( status, "rtems_task_start of TA2" );
831
832  status = rtems_task_start( Task_id[ 3 ], Task_1_through_5, 0 );
833  directive_failed( status, "rtems_task_start of TA3" );
834
835  status = rtems_task_start( Task_id[ 4 ], Task_1_through_5, 0 );
836  directive_failed( status, "rtems_task_start of TA4" );
837
838  status = rtems_task_start( Task_id[ 5 ], Task_1_through_5, 0 );
839  directive_failed( status, "rtems_task_start of TA5" );
840
841  status = rtems_task_delete( RTEMS_SELF );
842  directive_failed( status, "rtems_task_delete of RTEMS_SELF" );
843}
Note: See TracBrowser for help on using the repository browser.