1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * COPYRIGHT (c) 1989-2011. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <tmacros.h> |
---|
34 | #include "test_support.h" |
---|
35 | #include <rtems/libcsupport.h> |
---|
36 | #include <rtems/malloc.h> |
---|
37 | #include <errno.h> |
---|
38 | |
---|
39 | const char rtems_test_name[] = "MALLOC 4"; |
---|
40 | |
---|
41 | /* configuration information */ |
---|
42 | /* At top of file to have access to configuration variables */ |
---|
43 | |
---|
44 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
45 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
46 | |
---|
47 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
48 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
49 | |
---|
50 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
51 | |
---|
52 | #define CONFIGURE_INIT |
---|
53 | #include <rtems/confdefs.h> |
---|
54 | /* end of configuration */ |
---|
55 | |
---|
56 | #ifndef CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK |
---|
57 | void *rtems_heap_null_extend( |
---|
58 | Heap_Control *heap, |
---|
59 | size_t alloc_size |
---|
60 | ) |
---|
61 | { |
---|
62 | return rtems_heap_extend_via_sbrk( heap, alloc_size ); |
---|
63 | } |
---|
64 | #endif |
---|
65 | |
---|
66 | char Malloc_Heap[ 1024 ] CPU_STRUCTURE_ALIGNMENT; |
---|
67 | |
---|
68 | /* |
---|
69 | * Use volatile to prevent compiler optimizations due to the malloc() builtin. |
---|
70 | */ |
---|
71 | volatile int sbrk_count; |
---|
72 | |
---|
73 | Heap_Control TempHeap; |
---|
74 | |
---|
75 | size_t offset; |
---|
76 | |
---|
77 | void * sbrk(ptrdiff_t incr) |
---|
78 | { |
---|
79 | void *p = (void *) -1; |
---|
80 | |
---|
81 | printf( "sbrk(%td)\n", incr ); |
---|
82 | if ( sbrk_count == -1 ) { |
---|
83 | p = (void *) -2; |
---|
84 | sbrk_count = 0; |
---|
85 | } else if ( offset + incr <= sizeof(Malloc_Heap) ) { |
---|
86 | p = &Malloc_Heap[ offset ]; |
---|
87 | offset += incr; |
---|
88 | } |
---|
89 | |
---|
90 | ++sbrk_count; |
---|
91 | |
---|
92 | return p; |
---|
93 | } |
---|
94 | |
---|
95 | rtems_task Init( |
---|
96 | rtems_task_argument argument |
---|
97 | ) |
---|
98 | { |
---|
99 | Heap_Control *real_heap; |
---|
100 | const Memory_Information *mem; |
---|
101 | Memory_Area *area; |
---|
102 | size_t i; |
---|
103 | |
---|
104 | void *p; |
---|
105 | |
---|
106 | TEST_BEGIN(); |
---|
107 | |
---|
108 | mem = _Memory_Get(); |
---|
109 | |
---|
110 | for ( i = 0; i < _Memory_Get_count( mem ); ++i ) { |
---|
111 | area = _Memory_Get_area( mem, i ); |
---|
112 | _Memory_Initialize( area, NULL, NULL ); |
---|
113 | } |
---|
114 | |
---|
115 | area = _Memory_Get_area( mem, 0 ); |
---|
116 | |
---|
117 | /* Safe information on real heap */ |
---|
118 | real_heap = malloc_get_heap_pointer(); |
---|
119 | malloc_set_heap_pointer( &TempHeap ); |
---|
120 | |
---|
121 | rtems_heap_set_sbrk_amount( 0 ); |
---|
122 | |
---|
123 | puts( "No sbrk() amount" ); |
---|
124 | |
---|
125 | sbrk_count = 0; |
---|
126 | offset = 256; |
---|
127 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
128 | _Malloc_Initialize(); |
---|
129 | |
---|
130 | errno = 0; |
---|
131 | p = malloc( 256 ); |
---|
132 | rtems_test_assert( p == NULL ); |
---|
133 | rtems_test_assert( errno == ENOMEM ); |
---|
134 | rtems_test_assert( sbrk_count == 0 ); |
---|
135 | |
---|
136 | rtems_heap_set_sbrk_amount( 256 ); |
---|
137 | |
---|
138 | puts( "Misaligned extend" ); |
---|
139 | |
---|
140 | sbrk_count = 0; |
---|
141 | offset = 256; |
---|
142 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
143 | _Malloc_Initialize(); |
---|
144 | |
---|
145 | p = malloc(1); |
---|
146 | rtems_test_assert( p != NULL ); |
---|
147 | rtems_test_assert( sbrk_count == 0 ); |
---|
148 | |
---|
149 | p = malloc(257); |
---|
150 | rtems_test_assert( p != NULL ); |
---|
151 | rtems_test_assert( sbrk_count == 1 ); |
---|
152 | |
---|
153 | puts( "Not enough sbrk() space" ); |
---|
154 | |
---|
155 | sbrk_count = 0; |
---|
156 | offset = 256; |
---|
157 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
158 | _Malloc_Initialize(); |
---|
159 | |
---|
160 | errno = 0; |
---|
161 | p = malloc( sizeof( Malloc_Heap ) ); |
---|
162 | rtems_test_assert( p == NULL ); |
---|
163 | rtems_test_assert( errno == ENOMEM ); |
---|
164 | rtems_test_assert( sbrk_count == 1 ); |
---|
165 | |
---|
166 | puts( "Valid heap extend" ); |
---|
167 | |
---|
168 | sbrk_count = 0; |
---|
169 | offset = 256; |
---|
170 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
171 | _Malloc_Initialize(); |
---|
172 | |
---|
173 | p = malloc( 128 ); |
---|
174 | rtems_test_assert( p != NULL ); |
---|
175 | rtems_test_assert( sbrk_count == 0 ); |
---|
176 | |
---|
177 | p = malloc( 128 ); |
---|
178 | rtems_test_assert( p != NULL ); |
---|
179 | rtems_test_assert( sbrk_count == 1 ); |
---|
180 | |
---|
181 | puts( "Invalid heap extend" ); |
---|
182 | |
---|
183 | sbrk_count = -1; |
---|
184 | offset = 256; |
---|
185 | _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset ); |
---|
186 | _Malloc_Initialize(); |
---|
187 | |
---|
188 | errno = 0; |
---|
189 | p = malloc( 256 ); |
---|
190 | rtems_test_assert( p == NULL ); |
---|
191 | rtems_test_assert( errno == ENOMEM ); |
---|
192 | rtems_test_assert( sbrk_count == 2 ); |
---|
193 | |
---|
194 | /* Restore information on real heap */ |
---|
195 | malloc_set_heap_pointer( real_heap ); |
---|
196 | |
---|
197 | TEST_END(); |
---|
198 | |
---|
199 | rtems_test_exit(0); |
---|
200 | } |
---|
201 | |
---|
202 | /* end of file */ |
---|