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 | |
---|
20 | const 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 |
---|
38 | void *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 | |
---|
47 | char Malloc_Heap[ 1024 ] CPU_STRUCTURE_ALIGNMENT; |
---|
48 | |
---|
49 | /* |
---|
50 | * Use volatile to prevent compiler optimizations due to the malloc() builtin. |
---|
51 | */ |
---|
52 | volatile int sbrk_count; |
---|
53 | |
---|
54 | Heap_Control TempHeap; |
---|
55 | |
---|
56 | size_t offset; |
---|
57 | |
---|
58 | void * 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 | |
---|
76 | rtems_task Init( |
---|
77 | rtems_task_argument argument |
---|
78 | ) |
---|
79 | { |
---|
80 | Heap_Control *real_heap; |
---|
81 | const Memory_Information *mem; |
---|
82 | Memory_Area *area; |
---|
83 | size_t i; |
---|
84 | |
---|
85 | void *p; |
---|
86 | |
---|
87 | TEST_BEGIN(); |
---|
88 | |
---|
89 | mem = _Memory_Get(); |
---|
90 | |
---|
91 | for ( i = 0; i < _Memory_Get_count( mem ); ++i ) { |
---|
92 | area = _Memory_Get_area( mem, i ); |
---|
93 | _Memory_Initialize( area, NULL, NULL ); |
---|
94 | } |
---|
95 | |
---|
96 | area = _Memory_Get_area( mem, 0 ); |
---|
97 | |
---|
98 | /* Safe information on real heap */ |
---|
99 | real_heap = malloc_get_heap_pointer(); |
---|
100 | malloc_set_heap_pointer( &TempHeap ); |
---|
101 | |
---|
102 | rtems_heap_set_sbrk_amount( 0 ); |
---|
103 | |
---|
104 | puts( "No sbrk() amount" ); |
---|
105 | |
---|
106 | sbrk_count = 0; |
---|
107 | offset = 256; |
---|
108 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
109 | _Malloc_Initialize(); |
---|
110 | |
---|
111 | errno = 0; |
---|
112 | p = malloc( 256 ); |
---|
113 | rtems_test_assert( p == NULL ); |
---|
114 | rtems_test_assert( errno == ENOMEM ); |
---|
115 | rtems_test_assert( sbrk_count == 0 ); |
---|
116 | |
---|
117 | rtems_heap_set_sbrk_amount( 256 ); |
---|
118 | |
---|
119 | puts( "Misaligned extend" ); |
---|
120 | |
---|
121 | sbrk_count = 0; |
---|
122 | offset = 256; |
---|
123 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
124 | _Malloc_Initialize(); |
---|
125 | |
---|
126 | p = malloc(1); |
---|
127 | rtems_test_assert( p != NULL ); |
---|
128 | rtems_test_assert( sbrk_count == 0 ); |
---|
129 | |
---|
130 | p = malloc(257); |
---|
131 | rtems_test_assert( p != NULL ); |
---|
132 | rtems_test_assert( sbrk_count == 1 ); |
---|
133 | |
---|
134 | puts( "Not enough sbrk() space" ); |
---|
135 | |
---|
136 | sbrk_count = 0; |
---|
137 | offset = 256; |
---|
138 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
139 | _Malloc_Initialize(); |
---|
140 | |
---|
141 | errno = 0; |
---|
142 | p = malloc( sizeof( Malloc_Heap ) ); |
---|
143 | rtems_test_assert( p == NULL ); |
---|
144 | rtems_test_assert( errno == ENOMEM ); |
---|
145 | rtems_test_assert( sbrk_count == 1 ); |
---|
146 | |
---|
147 | puts( "Valid heap extend" ); |
---|
148 | |
---|
149 | sbrk_count = 0; |
---|
150 | offset = 256; |
---|
151 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
152 | _Malloc_Initialize(); |
---|
153 | |
---|
154 | p = malloc( 128 ); |
---|
155 | rtems_test_assert( p != NULL ); |
---|
156 | rtems_test_assert( sbrk_count == 0 ); |
---|
157 | |
---|
158 | p = malloc( 128 ); |
---|
159 | rtems_test_assert( p != NULL ); |
---|
160 | rtems_test_assert( sbrk_count == 1 ); |
---|
161 | |
---|
162 | puts( "Invalid heap extend" ); |
---|
163 | |
---|
164 | sbrk_count = -1; |
---|
165 | offset = 256; |
---|
166 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
167 | _Malloc_Initialize(); |
---|
168 | |
---|
169 | errno = 0; |
---|
170 | p = malloc( 256 ); |
---|
171 | rtems_test_assert( p == NULL ); |
---|
172 | rtems_test_assert( errno == ENOMEM ); |
---|
173 | rtems_test_assert( sbrk_count == 2 ); |
---|
174 | |
---|
175 | /* Restore information on real heap */ |
---|
176 | malloc_set_heap_pointer( real_heap ); |
---|
177 | |
---|
178 | TEST_END(); |
---|
179 | |
---|
180 | rtems_test_exit(0); |
---|
181 | } |
---|
182 | |
---|
183 | /* end of file */ |
---|