1 #include <stdio.h>
2 #include "tx_api.h"
3 #include "ux_api.h"
4 #include "ux_system.h"
5 #include "ux_utility.h"
6 #include "ux_hcd_sim_host.h"
7 #include "ux_test.h"
8 #include "ux_test_utility_sim.h"
9
10 #define UX_DEMO_STACK_SIZE 2048
11 #define UX_DEMO_MEMORY_SIZE (256*1024)
12 #define UX_DEMO_BUFFER_SIZE 2048
13
14 UCHAR usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
15
16 #ifdef CTEST
test_application_define(void * first_unused_memory)17 void test_application_define(void *first_unused_memory)
18 #else
19 void usbx_ux_utility_basic_memory_management_test_application_define(void *first_unused_memory)
20 #endif
21 {
22
23 UINT status = 0;
24 CHAR *stack_pointer;
25 CHAR *memory_pointer;
26 CHAR *pointer_1;
27 CHAR *pointer_2;
28 CHAR *pointer_3;
29 CHAR *pointer_4;
30 ULONG memory_size;
31 ULONG array[20];
32 UX_MEMORY_BYTE_POOL *pool_ptr;
33
34
35 /* Inform user. */
36 printf("Running USB Utility Basic Memory Management Converge Coverage Test . ");
37
38 /* Initialize the free memory pointer */
39 stack_pointer = (CHAR *) usbx_memory;
40 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
41
42
43 /* Initialize USBX. Memory */
44 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
45 if (status != UX_SUCCESS)
46 {
47 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
48 test_control_return(status);
49 return;
50 }
51
52 pool_ptr = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR];
53 #ifdef UX_ENFORCE_SAFE_ALIGNMENT
54 memory_size = UX_ALIGN_8 - 1;
55 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
56 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
57 {
58 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
59 test_control_return(1);
60 return;
61 }
62 ux_utility_memory_free(pointer_1);
63
64 memory_size = UX_ALIGN_8;
65 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
66 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
67 {
68 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
69 test_control_return(1);
70 return;
71 }
72 ux_utility_memory_free(pointer_1);
73
74 memory_size = UX_ALIGN_16;
75 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
76
77 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
78 {
79 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
80 test_control_return(1);
81 return;
82 }
83 ux_utility_memory_free(pointer_1);
84
85 memory_size = UX_ALIGN_32;
86 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
87
88 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
89 {
90 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
91 test_control_return(1);
92 return;
93 }
94 ux_utility_memory_free(pointer_1);
95
96 memory_size = UX_ALIGN_64;
97 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
98
99 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
100 {
101 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
102 test_control_return(1);
103 return;
104 }
105 ux_utility_memory_free(pointer_1);
106
107 memory_size = UX_ALIGN_128;
108 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
109
110 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
111 {
112 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
113 test_control_return(1);
114 return;
115 }
116 ux_utility_memory_free(pointer_1);
117
118 memory_size = UX_ALIGN_256;
119 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
120
121 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
122 {
123 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
124 test_control_return(1);
125 return;
126 }
127 ux_utility_memory_free(pointer_1);
128
129 memory_size = UX_ALIGN_512;
130 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
131
132 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
133 {
134 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
135 test_control_return(1);
136 return;
137 }
138 ux_utility_memory_free(pointer_1);
139
140 memory_size = UX_ALIGN_1024;
141 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
142
143 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
144 {
145 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
146 test_control_return(1);
147 return;
148 }
149 ux_utility_memory_free(pointer_1);
150
151 memory_size = UX_ALIGN_2048;
152 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
153
154 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
155 {
156 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
157 test_control_return(1);
158 return;
159 }
160 ux_utility_memory_free(pointer_1);
161
162 memory_size = UX_ALIGN_2048 + 1;
163 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
164
165 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & UX_ALIGN_4096))
166 {
167 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
168 test_control_return(1);
169 return;
170 }
171 ux_utility_memory_free(pointer_1);
172
173 memory_size = UX_MAX_SCATTER_GATHER_ALIGNMENT + 1;
174 pointer_1 = ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_REGULAR_MEMORY, memory_size);
175
176 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & (UX_MAX_SCATTER_GATHER_ALIGNMENT-1)))
177 {
178 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
179 test_control_return(1);
180 return;
181 }
182 ux_utility_memory_free(pointer_1);
183 #endif
184
185 /* Test NULL pointer release. */
186 ux_utility_memory_free(TX_NULL);
187
188 /* Test another bad block release... no pool pointer! */
189 array[0] = 0;
190 array[1] = 0;
191 array[2] = 0;
192 ux_utility_memory_free(&array[2]);
193
194 /* Test another bad block release.... pool pointer is not a valid pool! */
195 array[0] = 0;
196 array[1] = (ULONG) &array[3];
197 array[2] = 0;
198 array[3] = 0;
199 ux_utility_memory_free(&array[2]);
200
201 /* Re-Release the same block */
202 memory_size = UX_ALIGN_8 - 1;
203 pointer_1 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
204 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & memory_size))
205 {
206 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
207 test_control_return(1);
208 return;
209 }
210 ux_utility_memory_free(pointer_1);
211 ux_utility_memory_free(pointer_1);
212
213 /* Allocate each block again to make sure everything still
214 works. */
215
216 /* Allocate memory from the pool. */
217 memory_size = 24;
218 pointer_1 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
219
220 /* Check status. */
221 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & UX_ALIGN_MIN))
222 {
223 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
224 test_control_return(1);
225 return;
226 }
227 /* Allocate second memory area from the pool. */
228 memory_size = 24;
229 pointer_2 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
230
231 /* Check status. */
232 if ((pointer_2 == UX_NULL) || (((ULONG)pointer_2) & UX_ALIGN_MIN))
233 {
234 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
235 test_control_return(1);
236 return;
237 }
238
239 /* Allocate third memory area from the pool. */
240 memory_size = 24;
241 pointer_3 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
242
243 /* Check status. */
244 if ((pointer_3 == UX_NULL) || (((ULONG)pointer_3) & UX_ALIGN_MIN))
245 {
246 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
247 test_control_return(1);
248 return;
249 }
250
251 /* Attempt to allocate fourth memory area from the pool. This should fail because
252 there should be not enough bytes in the pool. */
253 memory_size = 24;
254 pointer_4 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
255
256 /* Check status. */
257 if ((pointer_4 == UX_NULL) || (((ULONG)pointer_4) & UX_ALIGN_MIN))
258 {
259 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
260 test_control_return(1);
261 return;
262 }
263
264 /* Now release each of the blocks. */
265 ux_utility_memory_free(pointer_1);
266 ux_utility_memory_free(pointer_2);
267 ux_utility_memory_free(pointer_3);
268
269 /* Now allocate a block that should cause all of the blocks to merge
270 together. */
271 memory_size = 88;
272 pointer_3 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
273
274 /* Check status. */
275 if ((pointer_3 == UX_NULL) || (((ULONG)pointer_3) & UX_ALIGN_MIN))
276 {
277 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
278 test_control_return(1);
279 return;
280 }
281 ux_utility_memory_free(pointer_3);
282 ux_utility_memory_free(pointer_4);
283
284 /* Check the allocated address is the same */
285 /* Allocate memory from the pool. */
286 memory_size = 24;
287 pointer_1 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
288
289 /* Check status. */
290 if ((pointer_1 == UX_NULL) || (((ULONG)pointer_1) & UX_ALIGN_MIN))
291 {
292 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
293 test_control_return(1);
294 return;
295 }
296
297 /* Allocate second memory area from the pool. */
298 memory_size = 24;
299 pointer_2 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
300
301 /* Check status. */
302 if ((pointer_2 == UX_NULL) || (((ULONG)pointer_2) & UX_ALIGN_MIN))
303 {
304 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
305 test_control_return(1);
306 return;
307 }
308
309 /* Allocate second memory area from the pool. */
310 memory_size = 24;
311 pointer_3 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
312
313 /* Check status. */
314 if ((pointer_3 == UX_NULL) || (((ULONG)pointer_3) & UX_ALIGN_MIN))
315 {
316 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
317 test_control_return(1);
318 return;
319 }
320
321 /* Release the middle block. */
322 ux_utility_memory_free(pointer_1);
323
324 /* Now allocate the block again. */
325 memory_size = 24;
326 pointer_4 = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
327
328 /* Check status. */
329 if ((pointer_4 == UX_NULL) || (((ULONG)pointer_4) & UX_ALIGN_MIN))
330 {
331 printf("Line:%s, ux_utility_memory_allocate failed!\n", __LINE__);
332 test_control_return(1);
333 return;
334 }
335
336 if (pointer_1 != pointer_4)
337 {
338 printf("Line:%s, Allocated address is not the same!\n", __LINE__);
339 test_control_return(1);
340 return;
341 }
342
343 /* Now release the blocks are test the merge with the update of the search pointer. */
344 ux_utility_memory_free(pointer_3);
345 ux_utility_memory_free(pointer_2);
346 ux_utility_memory_free(pointer_4);
347
348 printf("SUCCESS!\n");
349
350 test_control_return(0);
351 return;
352 }
353