source: rtems/testsuites/libtests/malloc04/init.c @ c4b8b147

5
Last change on this file since c4b8b147 was c4b8b147, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 3, 2017 at 7:35:38 AM

tests: Use simple console driver

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2011.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <tmacros.h>
15#include "test_support.h"
16#include <rtems/libcsupport.h>
17#include <rtems/malloc.h>
18#include <errno.h>
19
20const char rtems_test_name[] = "MALLOC 4";
21
22/* configuration information */
23/* At top of file to have access to configuration variables */
24
25#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
26#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
27
28#define CONFIGURE_MAXIMUM_TASKS             1
29#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
30
31#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
32
33#define CONFIGURE_INIT
34#include <rtems/confdefs.h>
35/* end of configuration */
36
37#ifndef CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK
38void *rtems_heap_null_extend(
39  Heap_Control *heap,
40  size_t alloc_size
41)
42{
43  return rtems_heap_extend_via_sbrk( heap, alloc_size );
44}
45#endif
46
47char Malloc_Heap[ 1024 ] CPU_STRUCTURE_ALIGNMENT;
48
49/*
50 * Use volatile to prevent compiler optimizations due to the malloc() builtin.
51 */
52volatile int sbrk_count;
53
54Heap_Control TempHeap;
55
56size_t offset;
57
58void * sbrk(ptrdiff_t incr)
59{
60  void *p = (void *) -1;
61
62  printf( "sbrk(%td)\n", incr );
63  if ( sbrk_count == -1 ) {
64    p = (void *) -2;
65    sbrk_count = 0;
66  } else if ( offset + incr <= sizeof(Malloc_Heap) ) {
67    p = &Malloc_Heap[ offset ];
68    offset += incr;
69  }
70
71  ++sbrk_count;
72
73  return p;
74}
75
76rtems_task Init(
77  rtems_task_argument argument
78)
79{
80  Heap_Control *real_heap;
81  Heap_Area area;
82  void *p;
83
84  TEST_BEGIN();
85
86  /* Safe information on real heap */
87  real_heap = malloc_get_heap_pointer();
88  malloc_set_heap_pointer( &TempHeap );
89
90  rtems_heap_set_sbrk_amount( 0 );
91
92  puts( "No sbrk() amount" );
93
94  sbrk_count = 0;
95  offset     = 256;
96  area.begin = &Malloc_Heap [0];
97  area.size  = offset;
98  RTEMS_Malloc_Initialize( &area, 1, NULL );
99
100  errno = 0;
101  p = malloc( 256 );
102  rtems_test_assert( p == NULL );
103  rtems_test_assert( errno == ENOMEM );
104  rtems_test_assert( sbrk_count == 0 );
105
106  rtems_heap_set_sbrk_amount( 256 );
107
108  puts( "Misaligned extend" );
109
110  sbrk_count = 0;
111  offset     = 256;
112  area.begin = &Malloc_Heap [0];
113  area.size  = offset;
114  RTEMS_Malloc_Initialize( &area, 1, NULL );
115
116  p = malloc(1);
117  rtems_test_assert( p != NULL );
118  rtems_test_assert( sbrk_count == 0 );
119
120  p = malloc(257);
121  rtems_test_assert( p != NULL );
122  rtems_test_assert( sbrk_count == 1 );
123
124  puts( "Not enough sbrk() space" );
125
126  sbrk_count = 0;
127  offset     = 256;
128  area.begin = &Malloc_Heap [0];
129  area.size  = offset;
130  RTEMS_Malloc_Initialize( &area, 1, NULL );
131
132  errno = 0;
133  p = malloc( sizeof( Malloc_Heap ) );
134  rtems_test_assert( p == NULL );
135  rtems_test_assert( errno == ENOMEM );
136  rtems_test_assert( sbrk_count == 1 );
137
138  puts( "Valid heap extend" );
139
140  sbrk_count = 0;
141  offset     = 256;
142  area.begin = &Malloc_Heap [0];
143  area.size  = offset;
144  RTEMS_Malloc_Initialize( &area, 1, NULL );
145
146  p = malloc( 128 );
147  rtems_test_assert( p != NULL );
148  rtems_test_assert( sbrk_count == 0 );
149
150  p = malloc( 128 );
151  rtems_test_assert( p != NULL );
152  rtems_test_assert( sbrk_count == 1 );
153
154  puts( "Invalid heap extend" );
155
156  sbrk_count = -1;
157  offset     = 256;
158  area.begin = &Malloc_Heap [0];
159  area.size  = offset;
160  RTEMS_Malloc_Initialize( &area, 1, NULL );
161
162  errno = 0;
163  p = malloc( 256 );
164  rtems_test_assert( p == NULL );
165  rtems_test_assert( errno == ENOMEM );
166  rtems_test_assert( sbrk_count == 2 );
167
168  /* Restore information on real heap */
169  malloc_set_heap_pointer( real_heap );
170
171  TEST_END();
172
173  rtems_test_exit(0);
174}
175
176/* end of file */
Note: See TracBrowser for help on using the repository browser.