source: rtems/testsuites/libtests/malloctest/init.c @ 57f96b9

5
Last change on this file since 57f96b9 was 57f96b9, checked in by Sebastian Huber <sebastian.huber@…>, on 11/24/17 at 14:26:38

libtests/malloctest: Fix 64-bit targets

Update #3082.

  • Property mode set to 100644
File size: 36.6 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2011, 2014.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  Copyright (c) 2009, 2016 embedded brains GmbH.
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.org/license/LICENSE.
10 */
11
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
16#define CONFIGURE_INIT
17#include "system.h"
18
19#include <stdlib.h>
20#include <string.h>
21#include <inttypes.h>
22#include <errno.h>
23#include <rtems/score/protectedheap.h>
24#include <rtems/malloc.h>
25#include <rtems/sysinit.h>
26
27const char rtems_test_name[] = "MALLOCTEST";
28
29/*
30 *  A simple test of realloc
31 */
32static void test_realloc(void)
33{
34  void *p1, *p2, *p3, *p4;
35  size_t i;
36  int sc;
37  bool malloc_walk_ok;
38
39  /* Test growing reallocation "in place" */
40  p1 = malloc(1);
41  for (i=2 ; i<2048 ; i++) {
42    p2 = realloc(p1, i);
43    if (p2 != p1)
44      printf( "realloc - failed grow in place: "
45              "%p != realloc(%p,%zu)\n", p1, p2, i);
46    p1 = p2;
47  }
48  free(p1);
49
50  /* Test shrinking reallocation "in place" */
51  p1 = malloc(2048);
52  for (i=2047 ; i>=1; i--)  {
53    p2 = realloc(p1, i);
54    if (p2 != p1)
55      printf( "realloc - failed shrink in place: "
56              "%p != realloc(%p,%zu)\n", p1, p2, i);
57    p1 = p2;
58  }
59  free(p1);
60
61  /* Test realloc that should fail "in place", i.e.,
62   * fallback to free()-- malloc()
63   */
64  p1 = malloc(32);
65  p2 = malloc(32);
66  p3 = realloc(p1, 64);
67  if (p3 == p1 || p3 == NULL)
68    printf(
69      "realloc - failed non-in place: realloc(%p,%d) = %p\n", p1, 64, p3);
70  free(p3);
71  free(p2);
72
73  /*
74   *  Yet another case
75   */
76  p1 = malloc(8);
77  p2 = malloc(8);
78  free(p1);
79  sc = posix_memalign(&p1, 16, 32);
80  if (!sc)
81    free(p1);
82
83  /*
84   *  Allocate with default alignment coverage
85   */
86  sc = rtems_memalign( &p4, 0, 8 );
87  if ( !sc && p4 )
88    free( p4 );
89
90  /*
91   * Walk the C Program Heap
92   */
93  puts( "malloc_walk - normal path" );
94  malloc_walk_ok = malloc_walk( 1234, false );
95  rtems_test_assert( malloc_walk_ok );
96
97  puts( "malloc_walk - in critical section path" );
98  _Thread_Dispatch_disable();
99  malloc_walk_ok = malloc_walk( 1234, false );
100  rtems_test_assert( malloc_walk_ok );
101  _Thread_Dispatch_enable( _Per_CPU_Get() );
102
103  /*
104   *  Realloc with a bad pointer to force a point
105   */
106  p4 = realloc( test_realloc, 32 );
107
108  p4 = _realloc_r( NULL, NULL, 1 );
109}
110
111#define TEST_HEAP_SIZE 2048
112
113uint8_t TestHeapMemory[TEST_HEAP_SIZE];
114
115Heap_Control TestHeap;
116
117static void test_heap_default_init(void)
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  _Heap_Protection_free_all_delayed_blocks( &TestHeap );
128}
129
130static void test_heap_cases_1(void)
131{
132  void     *p1, *p2, *p3;
133  uintptr_t  u1, u2;
134  Heap_Resize_status rsc;
135
136  /*
137   * Another odd case.  What we are trying to do from Sergei
138   *
139   * 32-bit CPU when CPU_ALIGNMENT = 4 (most targets have 8) with the
140   * code like this:
141   */
142  test_heap_default_init();
143  p1 = _Heap_Allocate( &TestHeap, 12 );
144  p2 = _Heap_Allocate( &TestHeap, 32 );
145  p3 = _Heap_Allocate( &TestHeap, 32 );
146  test_free( p2 );
147  p2 = _Heap_Allocate_aligned( &TestHeap, 8, 28 );
148  test_free( p1 );
149  test_free( p2 );
150  test_free( p3 );
151
152  /*
153   *  Odd case in resizing a block.  Again test case outline per Sergei
154   */
155  test_heap_default_init();
156  p1 = _Heap_Allocate( &TestHeap, 32 );
157  p2 = _Heap_Allocate( &TestHeap, 8 );
158  p3 = _Heap_Allocate( &TestHeap, 32 );
159  test_free( p2 );
160  rsc = _Heap_Resize_block( &TestHeap, p1, 41, &u1, &u2 );
161  /* XXX what should we expect */
162  test_free( p3 );
163  test_free( p1 );
164
165  /*
166   *  To tackle a special case of resizing a block in order to cover the
167   *  code in heapresizeblock.c
168   *
169   *  Re-initialise the heap, so that the blocks created from now on
170   *  are contiguous.
171   */
172  test_heap_default_init();
173  puts( "Heap Initialized" );
174  p1 = _Heap_Allocate( &TestHeap, 400 );
175  rtems_test_assert( p1 != NULL );
176  p2 = _Heap_Allocate( &TestHeap, 496 );
177  rtems_test_assert( p2 != NULL );
178  rsc = _Heap_Resize_block( &TestHeap, p1, 256, &u1, &u2 );
179  rtems_test_assert( rsc == HEAP_RESIZE_SUCCESSFUL );
180  test_free( p1 );
181  test_free( p2 );
182}
183
184#define TEST_DEFAULT_PAGE_SIZE 128
185
186static void test_heap_init(uintptr_t page_size )
187{
188  uintptr_t rv = 0;
189
190  memset( &TestHeapMemory, 0x7f, TEST_HEAP_SIZE );
191
192  rv = _Heap_Initialize( &TestHeap, TestHeapMemory, TEST_HEAP_SIZE, page_size );
193  rtems_test_assert( rv > 0 );
194}
195
196static void test_check_alloc(
197  void *alloc_begin_ptr,
198  void *expected_alloc_begin_ptr,
199  uintptr_t alloc_size,
200  uintptr_t alignment,
201  uintptr_t boundary
202)
203{
204  uintptr_t const min_block_size = TestHeap.min_block_size;
205  uintptr_t const page_size = TestHeap.page_size;
206
207  rtems_test_assert( alloc_begin_ptr == expected_alloc_begin_ptr );
208
209  if( expected_alloc_begin_ptr != NULL ) {
210    uintptr_t const alloc_begin = (uintptr_t ) alloc_begin_ptr;
211    uintptr_t const alloc_end = alloc_begin + alloc_size;
212
213    uintptr_t const alloc_area_begin = _Heap_Align_down( alloc_begin, page_size );
214    uintptr_t const alloc_area_offset = alloc_begin - alloc_area_begin;
215#if UNUSED
216    uintptr_t const alloc_area_size = alloc_area_offset + alloc_size;
217#endif
218    Heap_Block *block = _Heap_Block_of_alloc_area( alloc_area_begin, page_size );
219    uintptr_t const block_begin = (uintptr_t ) block;
220    uintptr_t const block_size = _Heap_Block_size( block );
221    uintptr_t const block_end = block_begin + block_size;
222
223    rtems_test_assert( block_size >= min_block_size );
224    rtems_test_assert( block_begin < block_end );
225    rtems_test_assert(
226      _Heap_Is_aligned( block_begin + HEAP_BLOCK_HEADER_SIZE, page_size )
227    );
228    rtems_test_assert(
229      _Heap_Is_aligned( block_size, page_size )
230    );
231
232    rtems_test_assert( alloc_end <= block_end + HEAP_ALLOC_BONUS );
233    rtems_test_assert( alloc_area_begin > block_begin );
234    rtems_test_assert( alloc_area_offset < page_size );
235
236    rtems_test_assert( _Heap_Is_aligned( alloc_area_begin, page_size ) );
237    if ( alignment == 0 ) {
238      rtems_test_assert( alloc_begin == alloc_area_begin );
239    } else {
240      rtems_test_assert( _Heap_Is_aligned( alloc_begin, alignment ) );
241    }
242
243    if ( boundary != 0 ) {
244      uintptr_t boundary_line = _Heap_Align_down( alloc_end, boundary );
245
246      rtems_test_assert( alloc_size <= boundary );
247      rtems_test_assert(
248        boundary_line <= alloc_begin
249          || alloc_end <= boundary_line
250      );
251    }
252  }
253
254  rtems_test_assert(
255    page_size < CPU_ALIGNMENT
256      || _Heap_Walk( &TestHeap, 0, false )
257  );
258}
259
260static void test_check_alloc_simple(
261  void *alloc_begin_ptr,
262  uintptr_t alloc_size,
263  uintptr_t alignment,
264  uintptr_t boundary
265)
266{
267  test_check_alloc(
268    alloc_begin_ptr,
269    alloc_begin_ptr,
270    alloc_size,
271    alignment,
272    boundary
273  );
274}
275
276static void *test_alloc(
277  uintptr_t alloc_size,
278  uintptr_t alignment,
279  uintptr_t boundary,
280  void *expected_alloc_begin_ptr
281)
282{
283  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
284    &TestHeap,
285    alloc_size,
286    alignment,
287    boundary
288  );
289
290  test_check_alloc(
291    alloc_begin_ptr,
292    expected_alloc_begin_ptr,
293    alloc_size,
294    alignment,
295    boundary
296  );
297
298  return alloc_begin_ptr;
299}
300
301static void *test_alloc_simple(
302  uintptr_t alloc_size,
303  uintptr_t alignment,
304  uintptr_t boundary
305)
306{
307  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
308    &TestHeap,
309    alloc_size,
310    alignment,
311    boundary
312  );
313
314  test_check_alloc_simple(
315    alloc_begin_ptr,
316    alloc_size,
317    alignment,
318    boundary
319  );
320
321  rtems_test_assert( alloc_begin_ptr != NULL );
322
323  return alloc_begin_ptr;
324}
325
326static void *test_init_and_alloc(
327  uintptr_t alloc_size,
328  uintptr_t alignment,
329  uintptr_t boundary,
330  void *expected_alloc_begin_ptr
331)
332{
333  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
334
335  return test_alloc(
336    alloc_size,
337    alignment,
338    boundary,
339    expected_alloc_begin_ptr
340  );
341}
342
343static void *test_init_and_alloc_simple(
344  uintptr_t alloc_size,
345  uintptr_t alignment,
346  uintptr_t boundary
347)
348{
349  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
350
351  return test_alloc_simple(
352    alloc_size,
353    alignment,
354    boundary
355  );
356}
357
358static uintptr_t test_page_size(void)
359{
360  return TestHeap.page_size;
361}
362
363static void test_heap_do_initialize(
364  uintptr_t area_size,
365  uintptr_t page_size,
366  uintptr_t success_expected
367)
368{
369  uintptr_t rv =
370    _Heap_Initialize( &TestHeap, TestHeapMemory, area_size, page_size );
371
372  if ( success_expected ) {
373    rtems_test_assert( rv > 0 && _Heap_Walk( &TestHeap, 0, false ) );
374  } else {
375    rtems_test_assert( rv == 0 );
376  }
377}
378
379static void test_heap_initialize(void)
380{
381  puts( "run tests for _Heap_Initialize()" );
382
383  test_heap_do_initialize( TEST_HEAP_SIZE, 0, true );
384
385  test_heap_do_initialize( TEST_HEAP_SIZE, TEST_DEFAULT_PAGE_SIZE, true );
386
387  test_heap_do_initialize( 0, 0, false );
388
389  test_heap_do_initialize( (uintptr_t) -1, 0, false );
390
391  test_heap_do_initialize( TEST_HEAP_SIZE, (uintptr_t) -1, false );
392
393  test_heap_do_initialize(
394    TEST_HEAP_SIZE,
395    (uintptr_t) (-2 * CPU_ALIGNMENT),
396    false
397  );
398}
399
400static void test_heap_allocate(void)
401{
402  void *p1 = NULL;
403  void *p2 = NULL;
404  void *p3 = NULL;
405  uintptr_t alloc_size = 0;
406  uintptr_t alignment = 0;
407  uintptr_t boundary = 0;
408  uintptr_t page_size = 0;
409  uintptr_t first_page_begin = 0;
410  uintptr_t previous_last_block_begin = 0;
411  uintptr_t previous_last_page_begin = 0;
412
413  uintptr_t last_block_begin = 0;
414  uintptr_t last_alloc_begin = 0;
415
416  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
417
418  last_block_begin = (uintptr_t) TestHeap.last_block;
419  last_alloc_begin = _Heap_Alloc_area_of_block( TestHeap.last_block );
420
421  puts( "run tests for _Heap_Allocate_aligned_with_boundary()");
422
423  puts( "\tcheck if NULL will be returned if size causes integer overflow" );
424
425  alloc_size = (uintptr_t ) -1;
426  alignment = 0;
427  boundary = 0;
428  test_init_and_alloc( alloc_size, alignment, boundary, NULL );
429
430  puts( "\ttry to allocate more space than the one which fits in the boundary" );
431
432  alloc_size = 2;
433  alignment = 0;
434  boundary = alloc_size - 1;
435  test_init_and_alloc( alloc_size, alignment, boundary, NULL );
436
437  puts( "\tcheck if alignment will be set to page size if only a boundary is given" );
438
439  alloc_size = 1;
440  boundary = 1;
441
442  alignment = 0;
443  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
444
445  alignment = test_page_size();
446  test_init_and_alloc( alloc_size, alignment, boundary, p1 );
447
448  puts( "\tcreate a block which is bigger then the first free space" );
449
450  alignment = 0;
451  boundary = 0;
452
453  alloc_size = test_page_size();
454  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
455  p2 = test_alloc_simple( alloc_size, alignment, boundary );
456  rtems_test_assert( p2 );
457
458  test_free( p1 );
459
460  alloc_size = 2 * alloc_size;
461  p3 = test_alloc_simple( alloc_size, alignment, boundary );
462  rtems_test_assert( p1 != p3 );
463
464  puts( "\tset boundary before allocation begin" );
465
466  alloc_size = 1;
467  alignment = 0;
468  boundary = last_alloc_begin - test_page_size();
469  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
470  rtems_test_assert( (uintptr_t ) p1 >= boundary );
471
472  puts( "\tset boundary between allocation begin and end" );
473  alloc_size = test_page_size();
474  alignment = 0;
475  boundary = last_alloc_begin - alloc_size / 2;
476  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
477  rtems_test_assert( (uintptr_t ) p1 + alloc_size <= boundary );
478
479  puts( "\tset boundary after allocation end" );
480  alloc_size = 1;
481  alignment = 0;
482  boundary = last_alloc_begin;
483  p1 = test_init_and_alloc_simple( alloc_size, alignment, boundary );
484  rtems_test_assert( (uintptr_t ) p1 + alloc_size < boundary );
485
486  puts( "\tset boundary on allocation end" );
487  alloc_size = TEST_DEFAULT_PAGE_SIZE - HEAP_BLOCK_HEADER_SIZE;
488  alignment = 0;
489  boundary = last_block_begin;
490  p1 = (void *) (last_alloc_begin - TEST_DEFAULT_PAGE_SIZE);
491  test_init_and_alloc( alloc_size, alignment, boundary, p1);
492
493  puts( "\talign the allocation to different positions in the block header" );
494
495  page_size = sizeof(uintptr_t);
496  alloc_size = 1;
497  boundary = 0;
498
499  test_heap_init( page_size );
500
501  /* Force the page size to a small enough value */
502  TestHeap.page_size = page_size;
503
504  alignment = first_page_begin - sizeof(uintptr_t);
505  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
506
507  first_page_begin = ((uintptr_t) TestHeap.first_block ) + HEAP_BLOCK_HEADER_SIZE;
508  alignment = first_page_begin + sizeof(uintptr_t);
509  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
510
511  first_page_begin = ((uintptr_t) TestHeap.first_block )
512          + HEAP_BLOCK_HEADER_SIZE;
513  alignment = first_page_begin;
514  p1 = test_alloc_simple( alloc_size, alignment, boundary );
515
516  puts( "\tallocate last block with different boundarys" );
517  page_size = TEST_DEFAULT_PAGE_SIZE;
518  test_heap_init( page_size );
519  previous_last_block_begin = ((uintptr_t) TestHeap.last_block )
520          - TestHeap.min_block_size;
521  previous_last_page_begin = previous_last_block_begin
522          + HEAP_BLOCK_HEADER_SIZE;
523  alloc_size = TestHeap.page_size - HEAP_BLOCK_HEADER_SIZE;
524  alignment = sizeof(uintptr_t);
525  boundary = 0;
526  p1 = test_alloc( alloc_size, alignment, boundary, (void *) (previous_last_page_begin + sizeof(uintptr_t)));
527
528  test_heap_init( page_size );
529  boundary = ((uintptr_t) TestHeap.last_block );
530  p1 = test_alloc( alloc_size, alignment, boundary, (void *) previous_last_page_begin );
531
532  puts( "\tbreak the boundaries and aligns more than one time" );
533
534  page_size = CPU_ALIGNMENT * 20;
535  alloc_size = page_size / 4;
536  alignment = page_size / 5;
537  boundary = page_size / 4;
538  test_heap_init( page_size );
539  p1 = (void *) (_Heap_Alloc_area_of_block( TestHeap.last_block ) - page_size );
540  test_alloc( alloc_size, alignment, boundary, p1);
541
542  puts( "\tdifferent combinations, so that there is no valid block at the end" );
543
544  page_size = sizeof(uintptr_t);
545
546  test_heap_init( 0 );
547
548  /* Force the page size to a small enough value */
549  TestHeap.page_size = page_size;
550
551  alloc_size = 1;
552  alignment = (uintptr_t) TestHeap.last_block;
553  boundary = 0;
554  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
555
556  boundary = (uintptr_t) TestHeap.last_block;
557  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
558
559  alloc_size = 0;
560  p1 = test_alloc( alloc_size, alignment, boundary, NULL );
561
562  alloc_size = 1;
563  alignment = sizeof(uintptr_t);
564  boundary = 0;
565  p1 = test_alloc_simple( alloc_size, alignment, boundary );
566
567  puts( "\ttry to create a block, which is not possible because of the alignment and boundary" );
568
569  alloc_size = 2;
570  boundary = _Heap_Alloc_area_of_block( TestHeap.first_block )
571          + _Heap_Block_size( TestHeap.first_block ) / 2;
572  alignment = boundary - 1;
573  p1 = test_init_and_alloc( alloc_size, alignment, boundary, NULL );
574
575  alloc_size = 2;
576  alignment = _Heap_Alloc_area_of_block( TestHeap.first_block );
577  boundary = alignment + 1;
578  p1 = test_init_and_alloc( alloc_size, alignment, boundary, NULL );
579}
580
581static void test_heap_free(void)
582{
583  Heap_Control *heap = &TestHeap;
584  void *p;
585  Heap_Block *block;
586  bool ok;
587
588  _Heap_Initialize( heap, &TestHeapMemory[0], sizeof(TestHeapMemory), 0 );
589
590  p = _Heap_Allocate( heap, 1 );
591  rtems_test_assert( p != NULL );
592
593  block = _Heap_Block_of_alloc_area( (uintptr_t) p, heap->page_size );
594
595  /*
596   * This will kick the next block outside of the heap area and the next
597   * _Heap_Free() will detect this.
598   */
599  block->size_and_flag += sizeof(TestHeapMemory);
600
601  ok = _Heap_Free( heap, p );
602  rtems_test_assert( !ok );
603}
604
605static void *test_create_used_block( void )
606{
607  uintptr_t const alloc_size = 3 * TEST_DEFAULT_PAGE_SIZE;
608  uintptr_t const alignment = 0;
609  uintptr_t const boundary = 0;
610
611  return test_alloc_simple( alloc_size, alignment, boundary );
612}
613
614static void test_block_alloc(
615  int free_variant,
616  int alloc_variant,
617  uintptr_t alloc_begin,
618  uintptr_t alloc_size
619)
620{
621  void *p1 = NULL;
622  void *p2 = NULL;
623  void *p3 = NULL;
624
625  uintptr_t size_fresh_heap = 0;
626  uintptr_t pages_per_default_block = 0;
627  uint32_t exp_free_pages = 0;
628  uint32_t exp_free_blocks = 0;
629  uint32_t exp_used_blocks = 0;
630
631  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
632
633  size_fresh_heap = _Heap_Get_size( &TestHeap );
634  exp_free_pages = size_fresh_heap / TestHeap.page_size;
635
636  p1 = test_create_used_block();
637  p2 = test_create_used_block();
638  p3 = test_create_used_block();
639
640  pages_per_default_block = _Heap_Block_size(
641    _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size )
642  ) / TestHeap.page_size;
643
644  if (free_variant == 1) {
645    test_free( p1 );
646  } else if (free_variant == 2) {
647    test_free( p3 );
648  } else if (free_variant == 3) {
649    test_free( p2 );
650    test_free( p3 );
651  }
652
653  _Heap_Block_allocate(
654    &TestHeap,
655    _Heap_Block_of_alloc_area( (uintptr_t) p2, test_page_size()),
656    alloc_begin,
657    alloc_size
658  );
659
660  test_check_alloc_simple( (void *) alloc_begin, alloc_size, 0, 0 );
661
662  /* check statistics */
663  switch( free_variant ) {
664    case 1:
665      exp_free_pages = exp_free_pages - 2 * pages_per_default_block;
666      exp_used_blocks = 2;
667
668      switch( alloc_variant ) {
669        case 1:
670          /* allocate block full space */
671          exp_free_blocks = 2;
672          break;
673        case 2:
674          /* allocate block in the middle */
675          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
676          exp_free_blocks = 3;
677          break;
678        case 3:
679          /* allocate block at the end */
680          exp_free_pages = exp_free_pages + pages_per_default_block - 2;
681          exp_free_blocks = 2;
682          break;
683        default:
684          /* allocate block at the beginning */
685          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
686          exp_free_blocks = 3;
687          break;
688      }
689      break;
690    case 2:
691      exp_free_pages = exp_free_pages - 2 * pages_per_default_block;
692      exp_used_blocks = 2;
693
694      switch( alloc_variant ) {
695        case 1:
696          /* allocate block full space */
697          exp_free_blocks = 1;
698          break;
699        case 2:
700          /* allocate block in the middle */
701          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
702          exp_free_blocks = 2;
703          break;
704        case 3:
705          /* allocate block at the end */
706          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
707          exp_free_blocks = 2;
708          break;
709        default:
710          /* allocate block at the beginning */
711          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
712          exp_free_blocks = 1;
713          break;
714      }
715      break;
716    case 3:
717      exp_free_pages = exp_free_pages - pages_per_default_block;
718      exp_used_blocks = 2;
719
720      switch( alloc_variant ) {
721        case 1:
722          /* allocate block full space */
723          exp_free_pages = exp_free_pages - pages_per_default_block;
724          exp_free_blocks = 1;
725          break;
726        case 2:
727          /* allocate block in the middle */
728          exp_free_pages = exp_free_pages - 1;
729          exp_free_blocks = 2;
730          break;
731        case 3:
732          /* allocate block at the end */
733          exp_free_pages = exp_free_pages - 2;
734          exp_free_blocks = 2;
735          break;
736        default:
737          /* allocate block at the beginning */
738          exp_free_pages = exp_free_pages - 1;
739          exp_free_blocks = 1;
740          break;
741      }
742      break;
743    default:
744      exp_free_pages = exp_free_pages - 3 * pages_per_default_block;
745      exp_used_blocks = 3;
746
747      switch( alloc_variant ) {
748        case 1:
749          /* allocate block full space */
750          exp_free_blocks = 1;
751          break;
752        case 2:
753          /* allocate block in the middle */
754          exp_free_blocks = 3;
755          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
756          break;
757        case 3:
758          /* allocate block at the end */
759          exp_free_blocks = 2;
760          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
761          break;
762        default:
763          /* allocate block at the beginning */
764          exp_free_blocks = 2;
765          exp_free_pages = exp_free_pages + pages_per_default_block - 1;
766      }
767  }
768
769  rtems_test_assert( TestHeap.stats.free_size == exp_free_pages * TestHeap.page_size );
770  rtems_test_assert( TestHeap.stats.free_blocks == exp_free_blocks );
771  rtems_test_assert( TestHeap.stats.used_blocks == exp_used_blocks );
772}
773
774static void test_heap_do_block_allocate( int variant, void *p2 )
775{
776  Heap_Block *const block =
777    _Heap_Block_of_alloc_area( (uintptr_t) p2, test_page_size());
778  uintptr_t const alloc_box_begin = _Heap_Alloc_area_of_block( block );
779  uintptr_t const alloc_box_size = _Heap_Block_size( block );
780  uintptr_t const alloc_box_end = alloc_box_begin + alloc_box_size;
781  uintptr_t alloc_begin = 0;
782  uintptr_t alloc_size = 0;
783
784  puts( "\tallocate block at the beginning");
785  alloc_begin = alloc_box_begin;
786  alloc_size = 0;
787  test_block_alloc( variant, 0, alloc_begin, alloc_size );
788
789  puts( "\tallocate block full space");
790  alloc_begin = alloc_box_begin;
791  alloc_size = alloc_box_size + HEAP_ALLOC_BONUS
792    - HEAP_BLOCK_HEADER_SIZE;
793  test_block_alloc( variant, 1, alloc_begin, alloc_size );
794
795  puts( "\tallocate block in the middle");
796  alloc_begin = alloc_box_begin + TEST_DEFAULT_PAGE_SIZE;
797  alloc_size = 0;
798  test_block_alloc( variant, 2, alloc_begin, alloc_size );
799
800  puts( "\tallocate block at the end");
801  alloc_begin = alloc_box_end - TEST_DEFAULT_PAGE_SIZE;
802  alloc_size = TEST_DEFAULT_PAGE_SIZE + HEAP_ALLOC_BONUS
803    - HEAP_BLOCK_HEADER_SIZE;
804  test_block_alloc( variant, 3, alloc_begin, alloc_size );
805}
806
807static void test_heap_block_allocate( void )
808{
809  void *p2 = NULL;
810
811  puts( "run tests for _Heap_Block_allocate()" );
812
813  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
814
815  test_create_used_block();
816  p2 = test_create_used_block();
817
818  test_heap_do_block_allocate( 0, p2 );
819  test_heap_do_block_allocate( 1, p2 );
820  test_heap_do_block_allocate( 2, p2 );
821  test_heap_do_block_allocate( 3, p2 );
822}
823
824static void *test_alloc_one_page(void)
825{
826  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
827    &TestHeap,
828    1,
829    0,
830    0
831  );
832
833  test_check_alloc_simple(
834    alloc_begin_ptr,
835    1,
836    0,
837    0
838  );
839
840  rtems_test_assert( alloc_begin_ptr != NULL );
841
842  return alloc_begin_ptr;
843}
844
845static void *test_alloc_two_pages(void)
846{
847  void *alloc_begin_ptr = _Heap_Allocate_aligned_with_boundary(
848    &TestHeap,
849    3 * TestHeap.page_size / 2,
850    0,
851    0
852  );
853
854  test_check_alloc_simple(
855    alloc_begin_ptr,
856    3 * TestHeap.page_size / 2,
857    0,
858    0
859  );
860
861  rtems_test_assert( alloc_begin_ptr != NULL );
862
863  return alloc_begin_ptr;
864}
865
866static void test_simple_resize_block(
867  void *alloc_pointer,
868  uintptr_t new_alloc_size,
869  Heap_Resize_status expected_status
870)
871{
872  uintptr_t old_size = 0;
873  uintptr_t new_size = 0;
874
875  Heap_Resize_status status = _Heap_Resize_block(
876    &TestHeap,
877    alloc_pointer,
878    new_alloc_size,
879    &old_size,
880    &new_size
881  );
882
883  rtems_test_assert( status == expected_status );
884}
885
886static void test_heap_resize_block(void)
887{
888  void *p1, *p2, *p3;
889  uintptr_t new_alloc_size = 0;
890  Heap_Block *block = NULL;
891
892  puts( "run tests for _Heap_Resize_Block()" );
893
894  puts( "\tgive a block outside the heap to the function" );
895  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
896  p1 = TestHeap.first_block - TEST_DEFAULT_PAGE_SIZE;
897  new_alloc_size = 1;
898  test_simple_resize_block( p1, new_alloc_size, HEAP_RESIZE_FATAL_ERROR );
899
900  puts( "\tincrease size");
901
902  puts( "\t\tlet the next block be used alredy and try to get a size bigger than the actual block" );
903  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
904  p1 = test_alloc_one_page();
905  rtems_test_assert( p1 );
906
907  p2 = test_alloc_one_page();
908  rtems_test_assert( p2 );
909
910  new_alloc_size = 3 * TEST_DEFAULT_PAGE_SIZE / 2;
911  test_simple_resize_block( p1, new_alloc_size, HEAP_RESIZE_UNSATISFIED );
912
913  puts( "\t\tnext block not used and try to set the new allocation size between the page-alignments" );
914  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
915  p1 = test_alloc_one_page();
916  new_alloc_size = 3 * TEST_DEFAULT_PAGE_SIZE / 2;
917  test_simple_resize_block( p1, new_alloc_size, HEAP_RESIZE_SUCCESSFUL );
918
919  puts( "\t\tlet the block after the next be used and try to allocate more then one pagesize more" );
920  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
921  p1 = test_alloc_one_page();
922  rtems_test_assert( p1 );
923
924  p2 = test_alloc_one_page();
925  rtems_test_assert( p2 );
926
927  p3 = test_alloc_one_page();
928  rtems_test_assert( p3 );
929
930  test_free( p2 );
931  new_alloc_size = 5 * TEST_DEFAULT_PAGE_SIZE / 2;
932  test_simple_resize_block( p1, new_alloc_size, HEAP_RESIZE_UNSATISFIED );
933
934  puts( "\ttry to resize to the same size" );
935  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
936  p1 = test_alloc_one_page();
937  block = _Heap_Block_of_alloc_area( (uintptr_t) p1, TestHeap.page_size );
938  new_alloc_size = _Heap_Block_size( block );
939  test_simple_resize_block( p1, new_alloc_size, HEAP_RESIZE_SUCCESSFUL );
940
941  puts( "\tdecrease size");
942
943  puts( "\t\tdecrease a block with two pages to one page" );
944  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
945  p1 = test_alloc_two_pages();
946  new_alloc_size = 1;
947  test_simple_resize_block( p1, new_alloc_size, HEAP_RESIZE_SUCCESSFUL );
948
949  puts( "\t\tresize the block to the size 0" );
950  test_heap_init( TEST_DEFAULT_PAGE_SIZE );
951  p1 = test_alloc_one_page();
952  new_alloc_size = 0;
953  test_simple_resize_block( p1, new_alloc_size, HEAP_RESIZE_SUCCESSFUL );
954}
955
956static void test_heap_assert(bool ret, bool expected)
957{
958  rtems_test_assert( ret == expected );
959  rtems_test_assert( _Heap_Walk( &TestHeap, 0, false ) );
960}
961
962static void test_heap_extend(void)
963{
964  bool ret = false;
965  Heap_Control *heap = &TestHeap;
966  uint8_t *area_begin = TestHeapMemory;
967  uint8_t *sub_area_begin;
968  uint8_t *sub_area_end;
969
970  _Heap_Initialize( heap, area_begin + 768, 256, 0 );
971  sub_area_begin = (uint8_t *) heap->first_block;
972  sub_area_end = (uint8_t *) heap->first_block->prev_size;
973
974  puts( "heap extend - link below" );
975  ret = _Protected_heap_Extend( heap, area_begin + 0, 256 );
976  test_heap_assert( ret, true );
977
978  puts( "heap extend - merge below overlap" );
979  ret = _Protected_heap_Extend( heap, sub_area_begin - 128, 256 );
980  test_heap_assert( ret, false );
981
982  puts( "heap extend - merge below" );
983  ret = _Protected_heap_Extend( heap, sub_area_begin - 256, 256 );
984  test_heap_assert( ret, true );
985
986  puts( "heap extend - merge above overlap" );
987  ret = _Protected_heap_Extend( heap, sub_area_end - 128, 256 );
988  test_heap_assert( ret, false );
989
990  puts( "heap extend - merge above" );
991  ret = _Protected_heap_Extend( heap, sub_area_end, 256 );
992  test_heap_assert( ret, true );
993
994  puts( "heap extend - link above" );
995  ret = _Protected_heap_Extend( heap, area_begin + 1536, 256 );
996  test_heap_assert( ret, true );
997
998  puts( "heap extend - area too small" );
999  ret = _Protected_heap_Extend( heap, area_begin + 2048, 0 );
1000  test_heap_assert( ret, false );
1001
1002  puts( "heap extend - invalid area" );
1003  ret = _Protected_heap_Extend( heap, (void *) -1, 2 );
1004  test_heap_assert( ret, false );
1005
1006  area_begin = (uint8_t *) (((uintptr_t) area_begin) | 1);
1007
1008  _Heap_Initialize( heap, area_begin + 768, 256, 0 );
1009
1010  puts( "heap extend - merge below with align up" );
1011  ret = _Protected_heap_Extend( heap, area_begin + 512, 256 );
1012  test_heap_assert( ret, true );
1013}
1014
1015static void test_heap_extend_allocation_order(void)
1016{
1017  Heap_Control *heap = &TestHeap;
1018  uintptr_t size = 256;
1019  uintptr_t gap = 256;
1020  uint8_t *init_area_begin = TestHeapMemory;
1021  uint8_t *extend_area_begin = init_area_begin + size + gap;
1022  bool ret;
1023  uint8_t *p;
1024
1025  _Heap_Initialize( heap, init_area_begin, size, 0 );
1026
1027  ret = _Protected_heap_Extend( heap, extend_area_begin, size );
1028  test_heap_assert( ret, true );
1029
1030  p = _Heap_Allocate( heap, 1 );
1031  rtems_test_assert( (uintptr_t) (p - init_area_begin) < size );
1032}
1033
1034static void test_heap_extend_allocation_order_with_empty_heap(void)
1035{
1036  Heap_Control *heap = &TestHeap;
1037  uintptr_t size = 256;
1038  uintptr_t gap = 256;
1039  uint8_t *init_area_begin = TestHeapMemory;
1040  uint8_t *extend_area_begin = init_area_begin + size + gap;
1041  bool ret;
1042  uint8_t *p;
1043
1044  _Heap_Initialize( heap, init_area_begin, size, 0 );
1045
1046  _Heap_Greedy_allocate( heap, NULL, 0 );
1047
1048  ret = _Protected_heap_Extend( heap, extend_area_begin, size );
1049  test_heap_assert( ret, true );
1050
1051  p = _Heap_Allocate( heap, 1 );
1052  rtems_test_assert( (uintptr_t) (p - extend_area_begin) < size );
1053}
1054
1055static void test_heap_no_extend(void)
1056{
1057  uintptr_t extended_space = _Heap_No_extend( NULL, 0, 0, 0 );
1058  rtems_test_assert( extended_space == 0 );
1059}
1060
1061static void free_all_delayed_blocks( void )
1062{
1063  rtems_resource_snapshot unused;
1064
1065  rtems_resource_snapshot_take( &unused );
1066}
1067
1068static void do_free( void *p )
1069{
1070  free( p );
1071  free_all_delayed_blocks();
1072}
1073
1074static void test_heap_info(void)
1075{
1076  size_t                  s1, s2;
1077  void                   *p1;
1078  int                     sc;
1079  Heap_Information_block  the_info;
1080
1081  free_all_delayed_blocks();
1082
1083  s1 = malloc_free_space();
1084  p1 = malloc( 512 );
1085  s2 = malloc_free_space();
1086  puts( "malloc_free_space - check malloc space drops after malloc" );
1087  rtems_test_assert( s1 );
1088  rtems_test_assert( s2 );
1089  rtems_test_assert( s2 <= s1 );
1090  do_free( p1 );
1091
1092  puts( "malloc_free_space - verify free space returns to previous value" );
1093  s2 = malloc_free_space();
1094  rtems_test_assert( s1 == s2 );
1095
1096  puts( "malloc_info - called with NULL\n" );
1097  sc = malloc_info( NULL );
1098  rtems_test_assert( sc == -1 );
1099
1100  puts( "malloc_info - check free space drops after malloc" );
1101  sc = malloc_info( &the_info );
1102  rtems_test_assert( sc == 0 );
1103  s1 = the_info.Free.largest;
1104
1105  p1 = malloc( 512 );
1106
1107  sc = malloc_info( &the_info );
1108  rtems_test_assert( sc == 0 );
1109  s2 = the_info.Free.largest;
1110
1111  rtems_test_assert( s1 );
1112  rtems_test_assert( s2 );
1113  rtems_test_assert( s2 <= s1 );
1114  do_free( p1 );
1115
1116  puts( "malloc_info - verify free space returns to previous value" );
1117  sc = malloc_info( &the_info );
1118  rtems_test_assert( sc == 0 );
1119  rtems_test_assert( s1 == the_info.Free.largest );
1120}
1121
1122static void test_protected_heap_info(void)
1123{
1124  Heap_Control           heap;
1125  Heap_Information_block info;
1126  bool                   rc;
1127
1128  puts( "_Protected_heap_Get_information - NULL heap" );
1129  rc = _Protected_heap_Get_information( NULL, &info );
1130  rtems_test_assert( rc == false );
1131
1132  puts( "_Protected_heap_Get_information - NULL info" );
1133  rc = _Protected_heap_Get_information( &heap, NULL );
1134  rtems_test_assert( rc == false );
1135}
1136
1137static void test_rtems_heap_allocate_aligned_with_boundary(void)
1138{
1139  void *p = NULL;
1140
1141  p = rtems_heap_allocate_aligned_with_boundary(1, 1, 1);
1142  rtems_test_assert( p != NULL );
1143  free(p);
1144
1145  _Thread_Dispatch_disable();
1146  p = rtems_heap_allocate_aligned_with_boundary(1, 1, 1);
1147  _Thread_Dispatch_enable( _Per_CPU_Get() );
1148  rtems_test_assert( p == NULL );
1149}
1150
1151static void test_heap_size_with_overhead(void)
1152{
1153  uintptr_t s;
1154
1155  puts( "_Heap_Size_with_overhead" );
1156
1157  s = _Heap_Size_with_overhead(0, 0, 0);
1158  rtems_test_assert(s == HEAP_BLOCK_HEADER_SIZE + CPU_ALIGNMENT - 1);
1159
1160  s = _Heap_Size_with_overhead(CPU_ALIGNMENT, 0, 0);
1161  rtems_test_assert(s == HEAP_BLOCK_HEADER_SIZE + CPU_ALIGNMENT - 1);
1162
1163  s = _Heap_Size_with_overhead(CPU_ALIGNMENT, 0, 2 * CPU_ALIGNMENT);
1164  rtems_test_assert(s == HEAP_BLOCK_HEADER_SIZE + 2 * CPU_ALIGNMENT - 1);
1165
1166  s = _Heap_Size_with_overhead(CPU_ALIGNMENT, 123, 0);
1167  rtems_test_assert(s == HEAP_BLOCK_HEADER_SIZE + CPU_ALIGNMENT - 1 + 123);
1168}
1169
1170/*
1171 *  A simple test of posix_memalign
1172 */
1173static void test_posix_memalign(void)
1174{
1175  void *p1;
1176  size_t i;
1177  int sc;
1178  int maximumShift;
1179
1180  /*
1181   * posix_memalign() is declared as never having a NULL first parameter.
1182   * We need to explicitly disable this compiler warning to make this code
1183   * warning free.
1184   */
1185  COMPILER_DIAGNOSTIC_SETTINGS_PUSH
1186  COMPILER_DIAGNOSTIC_SETTINGS_DISABLE_NONNULL
1187  puts( "posix_memalign - NULL return pointer -- EINVAL" );
1188  sc = posix_memalign( NULL, 32, 8 );
1189  fatal_posix_service_status( sc, EINVAL, "posix_memalign NULL pointer" );
1190  COMPILER_DIAGNOSTIC_SETTINGS_POP
1191
1192  puts( "posix_memalign - alignment of 0 -- EINVAL" );
1193  sc = posix_memalign( &p1, 0, 8 );
1194  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 0" );
1195
1196  puts( "posix_memalign - alignment  of 2-- EINVAL" );
1197  sc = posix_memalign( &p1, 2, 8 );
1198  fatal_posix_service_status( sc, EINVAL, "posix_memalign alignment of 2" );
1199
1200  maximumShift = (sizeof(size_t) * CHAR_BIT) - 1;
1201  for ( i=sizeof(void *) ; i<maximumShift ; i++ ) {
1202    size_t alignment = 1;
1203
1204    alignment <<= i;
1205
1206    p1 = NULL; /* Initialize p1 to aovid used uninitialized */
1207
1208    printf( "posix_memalign - alignment of %zu -- OK\n", alignment);
1209    sc = posix_memalign( &p1, alignment, 8 );
1210    if ( sc == ENOMEM ) {
1211      printf( "posix_memalign - ran out of memory trying %zu\n", alignment );
1212      break;
1213    }
1214    posix_service_failed( sc, "posix_memalign alignment OK" );
1215
1216    free( p1 );
1217  }
1218  for ( ; i<maximumShift ; i++ ) {
1219    size_t alignment = 1 << i;
1220    printf( "posix_memalign - alignment of %zd -- SKIPPED\n", alignment);
1221  }
1222
1223}
1224
1225static void test_greedy_allocate(void)
1226{
1227  Heap_Control *heap = &TestHeap;
1228  uintptr_t block_size = 1;
1229  void *p;
1230
1231  _Heap_Initialize( heap, &TestHeapMemory[0], sizeof(TestHeapMemory), 0 );
1232
1233  _Heap_Greedy_allocate( heap, &block_size, 1 );
1234
1235  p = _Heap_Allocate( heap, 1 );
1236  rtems_test_assert( p != NULL );
1237
1238  p = _Heap_Allocate( heap, 1 );
1239  rtems_test_assert( p == NULL );
1240
1241  /* The internal allocation fails */
1242  _Heap_Greedy_allocate( heap, &block_size, 1 );
1243
1244  p = _Heap_Allocate( heap, 1 );
1245  rtems_test_assert( p == NULL );
1246}
1247
1248rtems_task Init(
1249  rtems_task_argument argument
1250)
1251{
1252  void             *p1;
1253  rtems_time_of_day time;
1254  rtems_status_code status;
1255
1256  TEST_BEGIN();
1257
1258  build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
1259  status = rtems_clock_set( &time );
1260  directive_failed( status, "rtems_clock_set" );
1261
1262  /*
1263   * Verify case where block is too large to calloc.
1264   */
1265#pragma GCC diagnostic push
1266#pragma GCC diagnostic ignored "-Walloc-size-larger-than=N"
1267  p1 = calloc( 1, SIZE_MAX );
1268#pragma GCC diagnostic pop
1269  if (p1) {
1270    printf("ERROR on attempt to calloc SIZE_MAX block expected failure.");
1271    free( p1 );
1272  }
1273
1274  /*
1275   * Verify error case where malloc of size 0.
1276   */
1277  p1 = malloc( 0 );
1278  if (p1) {
1279    printf("ERROR on attempt to malloc size 0 block expected failure.");
1280    free( p1 );
1281  }
1282
1283
1284
1285  test_heap_initialize();
1286  test_heap_block_allocate();
1287  test_heap_allocate();
1288  test_heap_free();
1289  test_heap_resize_block();
1290  test_realloc();
1291  test_heap_cases_1();
1292  test_heap_extend();
1293  test_heap_extend_allocation_order();
1294  test_heap_extend_allocation_order_with_empty_heap();
1295  test_heap_no_extend();
1296  test_heap_info();
1297  test_heap_size_with_overhead();
1298  test_protected_heap_info();
1299  test_rtems_heap_allocate_aligned_with_boundary();
1300  test_greedy_allocate();
1301
1302  test_posix_memalign();
1303
1304  Task_name[ 1 ] = rtems_build_name( 'T', 'A', '1', ' ' );
1305  Task_name[ 2 ] = rtems_build_name( 'T', 'A', '2', ' ' );
1306  Task_name[ 3 ] = rtems_build_name( 'T', 'A', '3', ' ' );
1307  Task_name[ 4 ] = rtems_build_name( 'T', 'A', '4', ' ' );
1308  Task_name[ 5 ] = rtems_build_name( 'T', 'A', '5', ' ' );
1309
1310  status = rtems_task_create(
1311     Task_name[ 1 ],
1312     1,
1313     TASK_STACK_SIZE,
1314     RTEMS_DEFAULT_MODES,
1315     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
1316     &Task_id[ 1 ]
1317  );
1318  directive_failed( status, "rtems_task_create of TA1" );
1319
1320  status = rtems_task_create(
1321     Task_name[ 2 ],
1322     1,
1323     TASK_STACK_SIZE,
1324     RTEMS_DEFAULT_MODES,
1325     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
1326     &Task_id[ 2 ]
1327  );
1328  directive_failed( status, "rtems_task_create of TA2" );
1329
1330  status = rtems_task_create(
1331     Task_name[ 3 ],
1332     1,
1333     TASK_STACK_SIZE,
1334     RTEMS_DEFAULT_MODES,
1335     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
1336     &Task_id[ 3 ]
1337  );
1338  directive_failed( status, "rtems_task_create of TA3" );
1339
1340  status = rtems_task_create(
1341     Task_name[ 4 ],
1342     1,
1343     TASK_STACK_SIZE,
1344     RTEMS_DEFAULT_MODES,
1345     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
1346     &Task_id[ 4 ]
1347  );
1348  directive_failed( status, "rtems_task_create of TA4" );
1349
1350  status = rtems_task_create(
1351     Task_name[ 5 ],
1352     1,
1353     TASK_STACK_SIZE,
1354     RTEMS_DEFAULT_MODES,
1355     RTEMS_DEFAULT_ATTRIBUTES | RTEMS_FLOATING_POINT,
1356     &Task_id[ 5 ]
1357  );
1358  directive_failed( status, "rtems_task_create of TA5" );
1359
1360  status = rtems_task_start( Task_id[ 1 ], Task_1_through_5, 0 );
1361  directive_failed( status, "rtems_task_start of TA1" );
1362
1363  status = rtems_task_start( Task_id[ 2 ], Task_1_through_5, 0 );
1364  directive_failed( status, "rtems_task_start of TA2" );
1365
1366  status = rtems_task_start( Task_id[ 3 ], Task_1_through_5, 0 );
1367  directive_failed( status, "rtems_task_start of TA3" );
1368
1369  status = rtems_task_start( Task_id[ 4 ], Task_1_through_5, 0 );
1370  directive_failed( status, "rtems_task_start of TA4" );
1371
1372  status = rtems_task_start( Task_id[ 5 ], Task_1_through_5, 0 );
1373  directive_failed( status, "rtems_task_start of TA5" );
1374
1375  status = rtems_task_delete( RTEMS_SELF );
1376  directive_failed( status, "rtems_task_delete of RTEMS_SELF" );
1377}
1378
1379static void test_early_malloc( void )
1380{
1381  void *p;
1382  char *q;
1383  void *r;
1384  void *s;
1385  void *t;
1386
1387  p = malloc( 1 );
1388  rtems_test_assert( p != NULL );
1389
1390  free( p );
1391
1392  q = calloc( 1, 1 );
1393  rtems_test_assert( q != NULL );
1394  rtems_test_assert( p != q );
1395  rtems_test_assert( q[0] == 0 );
1396
1397  free( q );
1398
1399  r = realloc( q, 128 );
1400  rtems_test_assert( r == q );
1401
1402  s = malloc( 1 );
1403  rtems_test_assert( s != NULL );
1404
1405  free( s );
1406
1407  t = realloc( r, 256 );
1408  rtems_test_assert( t != NULL );
1409  rtems_test_assert( t != r );
1410
1411  free( t );
1412}
1413
1414RTEMS_SYSINIT_ITEM(
1415  test_early_malloc,
1416  RTEMS_SYSINIT_INITIAL_EXTENSIONS,
1417  RTEMS_SYSINIT_ORDER_FIRST
1418);
Note: See TracBrowser for help on using the repository browser.