source: rtems/testsuites/libtests/malloctest/init.c @ 435f2c0

4.115
Last change on this file since 435f2c0 was 435f2c0, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 15, 2013 at 1:11:51 PM

libtests/malloctest: Fix heap extend test case

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