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 | Memory_Area area; |
---|
82 | Memory_Information mem = { |
---|
83 | .count = 0, |
---|
84 | .areas = &area |
---|
85 | }; |
---|
86 | |
---|
87 | void *p; |
---|
88 | |
---|
89 | TEST_BEGIN(); |
---|
90 | |
---|
91 | /* Safe information on real heap */ |
---|
92 | real_heap = malloc_get_heap_pointer(); |
---|
93 | malloc_set_heap_pointer( &TempHeap ); |
---|
94 | |
---|
95 | rtems_heap_set_sbrk_amount( 0 ); |
---|
96 | |
---|
97 | puts( "No sbrk() amount" ); |
---|
98 | |
---|
99 | sbrk_count = 0; |
---|
100 | offset = 256; |
---|
101 | _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset ); |
---|
102 | RTEMS_Malloc_Initialize( &mem, NULL ); |
---|
103 | |
---|
104 | errno = 0; |
---|
105 | p = malloc( 256 ); |
---|
106 | rtems_test_assert( p == NULL ); |
---|
107 | rtems_test_assert( errno == ENOMEM ); |
---|
108 | rtems_test_assert( sbrk_count == 0 ); |
---|
109 | |
---|
110 | rtems_heap_set_sbrk_amount( 256 ); |
---|
111 | |
---|
112 | puts( "Misaligned extend" ); |
---|
113 | |
---|
114 | sbrk_count = 0; |
---|
115 | offset = 256; |
---|
116 | _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset ); |
---|
117 | RTEMS_Malloc_Initialize( &mem, NULL ); |
---|
118 | |
---|
119 | p = malloc(1); |
---|
120 | rtems_test_assert( p != NULL ); |
---|
121 | rtems_test_assert( sbrk_count == 0 ); |
---|
122 | |
---|
123 | p = malloc(257); |
---|
124 | rtems_test_assert( p != NULL ); |
---|
125 | rtems_test_assert( sbrk_count == 1 ); |
---|
126 | |
---|
127 | puts( "Not enough sbrk() space" ); |
---|
128 | |
---|
129 | sbrk_count = 0; |
---|
130 | offset = 256; |
---|
131 | _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset ); |
---|
132 | RTEMS_Malloc_Initialize( &mem, NULL ); |
---|
133 | |
---|
134 | errno = 0; |
---|
135 | p = malloc( sizeof( Malloc_Heap ) ); |
---|
136 | rtems_test_assert( p == NULL ); |
---|
137 | rtems_test_assert( errno == ENOMEM ); |
---|
138 | rtems_test_assert( sbrk_count == 1 ); |
---|
139 | |
---|
140 | puts( "Valid heap extend" ); |
---|
141 | |
---|
142 | sbrk_count = 0; |
---|
143 | offset = 256; |
---|
144 | _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset ); |
---|
145 | RTEMS_Malloc_Initialize( &mem, NULL ); |
---|
146 | |
---|
147 | p = malloc( 128 ); |
---|
148 | rtems_test_assert( p != NULL ); |
---|
149 | rtems_test_assert( sbrk_count == 0 ); |
---|
150 | |
---|
151 | p = malloc( 128 ); |
---|
152 | rtems_test_assert( p != NULL ); |
---|
153 | rtems_test_assert( sbrk_count == 1 ); |
---|
154 | |
---|
155 | puts( "Invalid heap extend" ); |
---|
156 | |
---|
157 | sbrk_count = -1; |
---|
158 | offset = 256; |
---|
159 | _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset ); |
---|
160 | RTEMS_Malloc_Initialize( &mem, 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 */ |
---|