|
20 | 20 | #include <ztest.h>
|
21 | 21 | #include <stdlib.h>
|
22 | 22 | #include <errno.h>
|
| 23 | +#include <time.h> |
23 | 24 |
|
24 | 25 | #define BUF_LEN 10
|
25 | 26 |
|
| 27 | + |
| 28 | +/* The access test allocates objects of this type and dereferences members. */ |
| 29 | +union aligntest { |
| 30 | + long long thelonglong; |
| 31 | + double thedouble; |
| 32 | + uintmax_t theuintmax_t; |
| 33 | + void (*thepfunc)(void); |
| 34 | + time_t thetime_t; |
| 35 | +}; |
| 36 | + |
| 37 | +/* Make sure we can access some built-in types. */ |
| 38 | +static void do_the_access(volatile union aligntest *aptr) |
| 39 | +{ |
| 40 | + aptr->thelonglong = 2; |
| 41 | + aptr->thelonglong; |
| 42 | + |
| 43 | + if (IS_ENABLED(CONFIG_FPU)) { |
| 44 | + aptr->thedouble = 3.0; |
| 45 | + aptr->thedouble; |
| 46 | + } |
| 47 | + |
| 48 | + aptr->theuintmax_t = 4; |
| 49 | + aptr->theuintmax_t; |
| 50 | + |
| 51 | + aptr->thepfunc = test_main; |
| 52 | + aptr->thepfunc; |
| 53 | + |
| 54 | + aptr->thetime_t = 3; |
| 55 | + aptr->thetime_t; |
| 56 | +} |
| 57 | + |
| 58 | +#define PRINT_TYPE_INFO(_t) \ |
| 59 | + TC_PRINT(" %-14s %4zu %5zu\n", #_t, sizeof(_t), __alignof__(_t)) |
| 60 | + |
| 61 | +void test_malloc_align(void) |
| 62 | +{ |
| 63 | + char *ptr[64] = { NULL }; |
| 64 | + |
| 65 | + TC_PRINT(" Compiler type info for " CONFIG_ARCH " " CONFIG_BOARD "\n"); |
| 66 | + TC_PRINT(" TYPE SIZE ALIGN\n"); |
| 67 | + PRINT_TYPE_INFO(int); |
| 68 | + PRINT_TYPE_INFO(long); |
| 69 | + PRINT_TYPE_INFO(long long); |
| 70 | + PRINT_TYPE_INFO(double); |
| 71 | + PRINT_TYPE_INFO(size_t); |
| 72 | + PRINT_TYPE_INFO(void *); |
| 73 | + PRINT_TYPE_INFO(void (*)(void)); |
| 74 | + PRINT_TYPE_INFO(time_t); |
| 75 | + |
| 76 | + /* Tries to use the malloc() implementation when in different states. */ |
| 77 | + for (size_t i = 0; i < ARRAY_SIZE(ptr); i++) { |
| 78 | + union aligntest *aptr = NULL; |
| 79 | + |
| 80 | + ptr[i] = malloc(sizeof *(ptr[i])); |
| 81 | + aptr = malloc(sizeof(*aptr)); |
| 82 | + if (aptr) { |
| 83 | + do_the_access(aptr); |
| 84 | + } |
| 85 | + free(aptr); |
| 86 | + } |
| 87 | + for (size_t i = 0; i < ARRAY_SIZE(ptr); i++) { |
| 88 | + free(ptr[i]); |
| 89 | + ptr[i] = NULL; |
| 90 | + } |
| 91 | + |
| 92 | + for (size_t n = 0; n < ARRAY_SIZE(ptr); n++) { |
| 93 | + union aligntest *aptr = NULL; |
| 94 | + |
| 95 | + for (size_t i = 0; i < n; i++) { |
| 96 | + ptr[i] = malloc(sizeof *(ptr[i])); |
| 97 | + } |
| 98 | + aptr = malloc(sizeof(*aptr)); |
| 99 | + if (aptr) { |
| 100 | + do_the_access(aptr); |
| 101 | + } |
| 102 | + free(aptr); |
| 103 | + for (size_t i = 0; i < n; i++) { |
| 104 | + free(ptr[i]); |
| 105 | + ptr[i] = NULL; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + for (size_t n = 0; n < ARRAY_SIZE(ptr); n++) { |
| 110 | + union aligntest *aptr = NULL; |
| 111 | + |
| 112 | + ptr[n] = malloc(n); |
| 113 | + aptr = malloc(sizeof(*aptr)); |
| 114 | + if (aptr) { |
| 115 | + do_the_access(aptr); |
| 116 | + } |
| 117 | + free(aptr); |
| 118 | + free(ptr[n]); |
| 119 | + ptr[n] = NULL; |
| 120 | + } |
| 121 | +} |
| 122 | + |
26 | 123 | /**
|
27 | 124 | * @brief Test dynamic memory allocation using malloc
|
28 | 125 | *
|
@@ -97,7 +194,6 @@ void test_realloc(void)
|
97 | 194 | reloc_ptr = realloc(ptr, new_size);
|
98 | 195 |
|
99 | 196 | zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno);
|
100 |
| - zassert_not_null((ptr), "malloc/realloc failed, errno: %d", errno); |
101 | 197 | ptr = reloc_ptr;
|
102 | 198 |
|
103 | 199 | (void)memset(filled_buf, 'p', BUF_LEN);
|
@@ -169,7 +265,6 @@ void test_memalloc_all(void)
|
169 | 265 |
|
170 | 266 | reloc_ptr = realloc(mlc_ptr, new_size);
|
171 | 267 | zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno);
|
172 |
| - zassert_not_null((mlc_ptr), "malloc/realloc failed, errno: %d", errno); |
173 | 268 | mlc_ptr = reloc_ptr;
|
174 | 269 |
|
175 | 270 | free(mlc_ptr);
|
@@ -199,6 +294,7 @@ __no_optimization void test_memalloc_max(void)
|
199 | 294 | void test_main(void)
|
200 | 295 | {
|
201 | 296 | ztest_test_suite(test_c_lib_dynamic_memalloc,
|
| 297 | + ztest_user_unit_test(test_malloc_align), |
202 | 298 | ztest_user_unit_test(test_malloc),
|
203 | 299 | ztest_user_unit_test(test_free),
|
204 | 300 | ztest_user_unit_test(test_calloc),
|
|
0 commit comments