1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2010. |
---|
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.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #include <tmacros.h> |
---|
13 | #include "test_support.h" |
---|
14 | #include <rtems/libcsupport.h> |
---|
15 | #include <rtems/malloc.h> |
---|
16 | |
---|
17 | char Malloc_Heap[ 128 ] CPU_STRUCTURE_ALIGNMENT; |
---|
18 | int sbrk_count; |
---|
19 | Heap_Control Heap_Holder; |
---|
20 | |
---|
21 | /* Heap variables we need to peek and poke at */ |
---|
22 | extern Heap_Control *RTEMS_Malloc_Heap; |
---|
23 | extern size_t RTEMS_Malloc_Sbrk_amount; |
---|
24 | extern rtems_malloc_sbrk_functions_t *rtems_malloc_sbrk_helpers; |
---|
25 | extern rtems_malloc_sbrk_functions_t rtems_malloc_sbrk_helpers_table; |
---|
26 | |
---|
27 | size_t offset; |
---|
28 | void * sbrk(ptrdiff_t incr) |
---|
29 | { |
---|
30 | void *p = (void *) -1; |
---|
31 | |
---|
32 | printf( "sbrk(%td)\n", incr ); |
---|
33 | if ( offset + incr < sizeof(Malloc_Heap) ) { |
---|
34 | p = &Malloc_Heap[ offset ]; |
---|
35 | offset += incr; |
---|
36 | } else { |
---|
37 | if ( sbrk_count == 0 ) |
---|
38 | p = (void *) rtems_task_create; |
---|
39 | sbrk_count++; |
---|
40 | } |
---|
41 | |
---|
42 | sbrk_count++; |
---|
43 | return p; |
---|
44 | } |
---|
45 | |
---|
46 | rtems_task Init( |
---|
47 | rtems_task_argument argument |
---|
48 | ) |
---|
49 | { |
---|
50 | void *p1, *p2, *p3, *p4; |
---|
51 | |
---|
52 | sbrk_count = 0; |
---|
53 | offset = 0; |
---|
54 | |
---|
55 | puts( "\n\n*** TEST MALLOC 04 ***" ); |
---|
56 | |
---|
57 | /* Safe information on real heap */ |
---|
58 | Heap_Holder = *RTEMS_Malloc_Heap; |
---|
59 | rtems_malloc_sbrk_helpers = &rtems_malloc_sbrk_helpers_table; |
---|
60 | |
---|
61 | puts( "Initialize heap with some memory" ); |
---|
62 | offset = 64; |
---|
63 | sbrk_count = 0; |
---|
64 | RTEMS_Malloc_Initialize( Malloc_Heap, 64, 64 ); |
---|
65 | p1 = malloc(64); |
---|
66 | p2 = malloc(64); |
---|
67 | p3 = malloc(48); |
---|
68 | p4 = malloc(48); |
---|
69 | |
---|
70 | puts( "Initialize heap with some unaligned memory" ); |
---|
71 | offset = 65; |
---|
72 | sbrk_count = 0; |
---|
73 | RTEMS_Malloc_Initialize( &Malloc_Heap[1], 64, 64 ); |
---|
74 | p1 = malloc(64); |
---|
75 | p2 = malloc(64); |
---|
76 | p3 = malloc(48); |
---|
77 | |
---|
78 | puts( "Initialize heap with no memory (sbrk aligned)" ); |
---|
79 | offset = 7; |
---|
80 | sbrk_count = 0; |
---|
81 | RTEMS_Malloc_Initialize( NULL, 0, 64 ); |
---|
82 | p1 = malloc(64); |
---|
83 | p2 = malloc(64); |
---|
84 | p3 = malloc(48); |
---|
85 | p4 = malloc(48); |
---|
86 | |
---|
87 | puts( "Initialize heap with no memory (sbrk aligned)" ); |
---|
88 | offset = 0; |
---|
89 | sbrk_count = 0; |
---|
90 | RTEMS_Malloc_Initialize( NULL, 0, 64 ); |
---|
91 | p1 = malloc(64); |
---|
92 | |
---|
93 | puts( "Set sbrk amount in heap to 0" ); |
---|
94 | offset = 0; |
---|
95 | sbrk_count = 0; |
---|
96 | RTEMS_Malloc_Initialize( NULL, 0, 64 ); |
---|
97 | RTEMS_Malloc_Sbrk_amount = 0; |
---|
98 | p4 = malloc(48); |
---|
99 | |
---|
100 | /* Restore information on real heap */ |
---|
101 | *RTEMS_Malloc_Heap = Heap_Holder; |
---|
102 | rtems_malloc_sbrk_helpers = NULL; |
---|
103 | |
---|
104 | |
---|
105 | puts( "*** END OF TEST MALLOC 04 ***" ); |
---|
106 | |
---|
107 | rtems_test_exit(0); |
---|
108 | } |
---|
109 | |
---|
110 | /* configuration information */ |
---|
111 | |
---|
112 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
113 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
114 | |
---|
115 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
116 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
117 | |
---|
118 | #define CONFIGURE_INIT |
---|
119 | |
---|
120 | #include <rtems/confdefs.h> |
---|
121 | /* end of file */ |
---|