1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation */
/* Copyright (c) 2026 Eclipse ThreadX contributors */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the MIT License which is available at */
/* https://opensource.org/licenses/MIT. */
/* */
/* SPDX-License-Identifier: MIT */
/***************************************************************************/
/* This test simulator is designed to simulate ux_utility_ APIs for test. */
#include <stdio.h>
#include "tx_api.h"
#include "ux_test.h"
#include "ux_api.h"
#include "ux_system.h"
#include "ux_utility.h"
#include "ux_test_utility_sim.h"
#define FAIL_DISABLE (~0x00ul)
extern void test_control_return(UINT status);
static UX_MEMORY_BLOCK fail_memory_block_first =
{
.ux_memory_block_next = UX_NULL,
.ux_memory_byte_pool = UX_NULL,
};
static UX_MEMORY_BLOCK *original_regular_memory_block;
static UX_MEMORY_BLOCK *original_cache_safe_memory_block;
#define MAX_FLAGGED_MEMORY_ALLOCATION_POINTERS 8*1024
static VOID *flagged_memory_allocation_pointers[2][MAX_FLAGGED_MEMORY_ALLOCATION_POINTERS];
static ULONG num_flagged_memory_allocation_pointers[2];
VOID ux_test_utility_sim_no_overriding_cleanup(VOID)
{
num_flagged_memory_allocation_pointers[0] = 0;
num_flagged_memory_allocation_pointers[1] = 0;
}
/* Memory allocation simulator*/
VOID ux_test_utility_sim_mem_alloc_fail_all_start(VOID)
{
original_regular_memory_block = (UX_MEMORY_BLOCK *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_start;
original_cache_safe_memory_block = (UX_MEMORY_BLOCK *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_start;
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_start = (UCHAR*)&fail_memory_block_first;
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_start = (UCHAR*)&fail_memory_block_first;
}
VOID ux_test_utility_sim_mem_alloc_fail_all_stop(VOID)
{
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_start = (UCHAR*)original_regular_memory_block;
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_start = (UCHAR*)original_cache_safe_memory_block;
}
/* When the memory allocator tries to find a block of memory for the requested size, it will add extra stuff to
accomodate things like having to align the memory. See the allocator for what it adds. We don't take the requested
size alignment into account - we leave it up to the caller. */
static ULONG get_amount_added_by_memory_allocator(ULONG memory_alignment, ULONG memory_size_requested)
{
ULONG added;
if (memory_alignment == UX_SAFE_ALIGN)
memory_alignment = UX_NO_ALIGN;
if (memory_alignment <= UX_ALIGN_MIN)
{
added = sizeof(UX_MEMORY_BLOCK) +
/* This is the amount of memory required to ensure the next memory block buffer is properly align. */
(((sizeof(UX_MEMORY_BLOCK) + UX_ALIGN_MIN) & (~(ULONG)UX_ALIGN_MIN)) - sizeof(UX_MEMORY_BLOCK));
}
else
{
added = memory_alignment + sizeof(UX_MEMORY_BLOCK) +
/* This is the amount of memory required to ensure the next memory block buffer is properly align. */
(((sizeof(UX_MEMORY_BLOCK) + UX_ALIGN_MIN) & (~(ULONG)UX_ALIGN_MIN)) - sizeof(UX_MEMORY_BLOCK));
}
return added;
}
/* Returns the amount of memory that should be passed to ux_utility_memory_allocate
to allocate a specific memory block of the given size. */
static ULONG memory_requested_size_for_block(ULONG memory_alignment, ULONG block_size)
{
ULONG result;
ULONG added;
added = get_amount_added_by_memory_allocator(memory_alignment, block_size);
if (added > block_size)
{
return 0;
}
/* Now subtract everything we added. */
result = block_size - added;
/* The allocator will align the requested size to 16 bytes, so if we don't do any alignment now, the allocator
might add to the size. We can avoid this by just aligning to 16 bytes now. */
result &= ~(UX_ALIGN_MIN);
// /* _ux_utility_memory_free_block_best_get does a '>' check, not '>='. */
// result--;
return(result);
}
static void add_memory_allocation_pointer(ULONG memory_cache_flag, VOID *allocation_ptr)
{
UX_TEST_ASSERT(num_flagged_memory_allocation_pointers[memory_cache_flag] < MAX_FLAGGED_MEMORY_ALLOCATION_POINTERS);
flagged_memory_allocation_pointers[memory_cache_flag][num_flagged_memory_allocation_pointers[memory_cache_flag]++] = allocation_ptr;
}
/* This function simply allocates everything. */
VOID allocate_everything(ULONG memory_cache_flag)
{
// UX_MEMORY_BLOCK *first_memory_block;
UX_MEMORY_BLOCK *memory_block;
ULONG memory_block_size;
ULONG memory_block_requested_size;
VOID *tmp_allocation_ptr;
UX_MEMORY_BYTE_POOL *pool_ptr;
if (memory_cache_flag == UX_REGULAR_MEMORY)
{
pool_ptr = (UX_MEMORY_BYTE_POOL *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR];
}
else
{
pool_ptr = (UX_MEMORY_BYTE_POOL *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE];
}
/* Try to allocate all the blocks according to their size - this is faster than repeatedly allocating 1 byte. */
memory_block = (UX_MEMORY_BLOCK *)pool_ptr -> ux_byte_pool_start;
while (memory_block -> ux_memory_block_next > memory_block)
{
if ((ULONG)memory_block->ux_memory_byte_pool == UX_BYTE_BLOCK_FREE)
{
memory_block_size = (UCHAR *)memory_block->ux_memory_block_next - (UCHAR*)memory_block;
memory_block_requested_size = memory_requested_size_for_block(UX_NO_ALIGN, memory_block_size);
if (memory_block_requested_size > 0)
{
tmp_allocation_ptr = ux_utility_memory_allocate(UX_NO_ALIGN, memory_cache_flag, memory_block_requested_size);
UX_TEST_ASSERT(tmp_allocation_ptr != UX_NULL);
add_memory_allocation_pointer(memory_cache_flag, tmp_allocation_ptr);
/* memory_block is now the block we just allocated. */
memory_block = (UX_MEMORY_BLOCK *)UX_UCHAR_POINTER_SUB(tmp_allocation_ptr, sizeof(UX_MEMORY_BLOCK));
}
}
memory_block = memory_block->ux_memory_block_next;
}
/* Now allocate everything else. */
while (1)
{
tmp_allocation_ptr = ux_utility_memory_allocate(UX_NO_ALIGN, memory_cache_flag, 1);
if (tmp_allocation_ptr == NULL)
{
break;
}
add_memory_allocation_pointer(memory_cache_flag, tmp_allocation_ptr);
}
}
/* The goal of this function is to ensure that allocating 'target_fail_level' amount of memory fails, and to ensure
any allocations below 'target_fail_level' succeeds. An example usage is trying to get some allocation to fail in USBX
where the allocation follows other allocations i.e. you want allocations X and Y to succeed, but Z to fail; by passing
the sum of the sizes of X, Y, and Z to this function, it ensures X and Y will success and Z to fail.
The gist of how we do this is to allocate memory for 'target_fail_level - 1', then allocate all other memory, then free
the 'target_fail_level - 1' allocation. While that's the gist, it's quite oversimplified and there are nuances in the
allocator that makes this a little more difficult, as is explained throughout. */
VOID ux_test_utility_sim_mem_allocate_until_align_flagged(ULONG target_fail_level, ULONG memory_alignment, ULONG memory_cache_flag)
{
ULONG amount_added_by_allocator;
ULONG amount_added_by_alignment;
ULONG total_allocation_size;
ULONG memory_used;
UX_MEMORY_BLOCK *cur_memory_block;
UX_MEMORY_BLOCK *next_memory_block;
VOID *next_memory_block_buffer;
VOID *main_allocation_ptr;
UX_MEMORY_BYTE_POOL *pool_ptr;
// printf("alloc_until(%ld, %ld, %ld)\n", target_fail_level, memory_alignment, memory_cache_flag);
if (memory_cache_flag == UX_REGULAR_MEMORY)
{
pool_ptr = (UX_MEMORY_BYTE_POOL *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR];
}
else
{
pool_ptr = (UX_MEMORY_BYTE_POOL *)_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE];
}
/* Since we're going to generate at least one error, we need to ignore it. */
ux_test_ignore_all_errors();
/* Adjust level if invalid. */
if (target_fail_level == 0)
target_fail_level++;
/* The allocator will align the requested size to a 16-byte boundary. This means that, instead of allocating 'target_fail_level - 1',
we instead allocate 'target_fail_level - 16'. If the target_fail_level is less than 16, then we simply allocate everything. */
if (target_fail_level <= (UX_ALIGN_MIN + 1))
{
allocate_everything(memory_cache_flag);
}
else
{
/* As previously said, we subtract 16 since the allocator aligns sizes to 16-bytes. */
target_fail_level -= (UX_ALIGN_MIN + 1);
/* As The header and the alignment is considered in ux_utility_memory_allocate function, we just need to allocate the request size here */
#if 0
/* We have to figure out the size we need to allocate for the 'target_fail_level - 1' block. This is tricky because the allocator will
find a memory block that fits the requested size plus some extra stuff. If the extra stuff isn't used, the allocator will create another
memory block for it, so when we try to allocate the 'target_fail_level - 1 ' again, the memory block won't be big enough (remember, we allocate
everything after doing this allocate, so the block of extra stuff will be allocated). So, we need to allocate 'target_fail_level - 1' plus
the extra stuff so that when it's freed, the memory block will be big enough for 'target_fail_level - 1' to be allocated. */
amount_added_by_allocator = get_amount_added_by_memory_allocator(memory_alignment, target_fail_level);
amount_added_by_alignment = ((target_fail_level + UX_ALIGN_MIN) & (~(ULONG)UX_ALIGN_MIN)) - target_fail_level; /* The allocator aligns the requested size i.e. it adds some stuff. Take that into account now. */
total_allocation_size = (target_fail_level) + amount_added_by_allocator + amount_added_by_alignment;
#else
total_allocation_size = target_fail_level;
#endif
main_allocation_ptr = ux_utility_memory_allocate(memory_alignment, memory_cache_flag, total_allocation_size);
if (main_allocation_ptr != NULL)
{
allocate_everything(memory_cache_flag);
/* The next memory block _must_ be USED so that we don't merge with it, since it will cause the 'target_fail_level - 1' block to become
larger. The next memory block can be UNUSED if it's too small to be allocated by even a requested size of 1. */
cur_memory_block = (UX_MEMORY_BLOCK *) main_allocation_ptr - 1;
next_memory_block = cur_memory_block->ux_memory_block_next;
if ((ULONG)next_memory_block->ux_memory_byte_pool == UX_BYTE_BLOCK_FREE)
{
next_memory_block->ux_memory_byte_pool = pool_ptr;
next_memory_block_buffer = (VOID *) (next_memory_block + 1);
add_memory_allocation_pointer(memory_cache_flag, next_memory_block_buffer);
memory_used = (UCHAR *)next_memory_block->ux_memory_block_next - (UCHAR*)next_memory_block;
if (_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_start == _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_start)
{
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available -= memory_used;
}
else
{
switch (memory_cache_flag)
{
case UX_CACHE_SAFE_MEMORY:
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_available -= memory_used;
break;
default:
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available -= memory_used;
break;
}
}
}
/* Now free the 'target_fail_level - 1' block. */
ux_utility_memory_free(main_allocation_ptr);
}
}
ux_test_unignore_all_errors();
}
VOID ux_test_utility_sim_mem_free_all_flagged(ULONG memory_cache_flag)
{
ULONG i;
for (i = 0; i < num_flagged_memory_allocation_pointers[memory_cache_flag]; i++)
{
ux_utility_memory_free(flagged_memory_allocation_pointers[memory_cache_flag][i]);
}
num_flagged_memory_allocation_pointers[memory_cache_flag] = 0;
}
VOID ux_test_utility_sim_mem_test(VOID)
{
ULONG mem_level = 400;
ULONG alloc_size;
VOID* tmp_alloc;
printf("Test memory level: %ld\n", mem_level);
ux_test_utility_sim_mem_allocate_until(mem_level);
alloc_size = mem_level;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size %ld: %p\n", alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level >> 1;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size %ld: %p\n", alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level - 4;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size -%ld(%ld): %p\n", mem_level - alloc_size, alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level - 8;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size -%ld(%ld): %p\n", mem_level - alloc_size, alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level - 16;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size -%ld(%ld): %p\n", mem_level - alloc_size, alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level - 32;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size -%ld(%ld): %p\n", mem_level - alloc_size, alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level - 64;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size -%ld(%ld): %p\n", mem_level - alloc_size, alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level - 128;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size -%ld(%ld): %p\n", mem_level - alloc_size, alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
alloc_size = mem_level - 256;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, alloc_size);
printf("Allocate size -%ld(%ld): %p\n", mem_level - alloc_size, alloc_size, tmp_alloc);
if (tmp_alloc) ux_utility_memory_free(tmp_alloc);
ux_test_utility_sim_mem_free_all();
printf("alloc resolution:\n");
mem_level = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, 4);
printf("alloc 4 bytes: %ld -> %ld (%ld)\n", mem_level, _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available, mem_level - _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available);
ux_utility_memory_free(tmp_alloc);
mem_level = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, 16);
printf("alloc 16 bytes: %ld -> %ld (%ld)\n", mem_level, _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available, mem_level - _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available);
ux_utility_memory_free(tmp_alloc);
mem_level = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, 64);
printf("alloc 64 bytes: %ld -> %ld (%ld)\n", mem_level, _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available, mem_level - _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available);
ux_utility_memory_free(tmp_alloc);
mem_level = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, 100);
printf("alloc 100 bytes: %ld -> %ld (%ld)\n", mem_level, _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available, mem_level - _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available);
ux_utility_memory_free(tmp_alloc);
mem_level = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
tmp_alloc = ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, 200);
printf("alloc 200 bytes: %ld -> %ld (%ld)\n", mem_level, _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available, mem_level - _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available);
ux_utility_memory_free(tmp_alloc);
printf("Halt this thread!\n");
while(1)
{
tx_thread_sleep(10);
}
}
|