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