]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-alloc-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
8 #include "memory-util.h"
9 #include "random-util.h"
12 static void test_alloca(void) {
13 static const uint8_t zero
[997] = { };
16 t
= alloca_align(17, 512);
17 assert_se(!((uintptr_t)t
& 0xff));
20 t
= alloca0_align(997, 1024);
21 assert_se(!((uintptr_t)t
& 0x1ff));
22 assert_se(!memcmp(t
, zero
, 997));
25 static void test_GREEDY_REALLOC(void) {
26 _cleanup_free_
int *a
= NULL
, *b
= NULL
;
27 size_t n_allocated
= 0, i
, j
;
29 /* Give valgrind a chance to verify our realloc() operations */
31 for (i
= 0; i
< 20480; i
++) {
32 assert_se(GREEDY_REALLOC(a
, n_allocated
, i
+ 1));
33 assert_se(n_allocated
>= i
+ 1);
34 assert_se(malloc_usable_size(a
) >= (i
+ 1) * sizeof(int));
36 assert_se(GREEDY_REALLOC(a
, n_allocated
, i
/ 2));
37 assert_se(n_allocated
>= i
/ 2);
38 assert_se(malloc_usable_size(a
) >= (i
/ 2) * sizeof(int));
41 for (j
= 0; j
< i
/ 2; j
++)
42 assert_se(a
[j
] == (int) j
);
44 for (i
= 30, n_allocated
= 0; i
< 20480; i
+= 7) {
45 assert_se(GREEDY_REALLOC(b
, n_allocated
, i
+ 1));
46 assert_se(n_allocated
>= i
+ 1);
47 assert_se(malloc_usable_size(b
) >= (i
+ 1) * sizeof(int));
49 assert_se(GREEDY_REALLOC(b
, n_allocated
, i
/ 2));
50 assert_se(n_allocated
>= i
/ 2);
51 assert_se(malloc_usable_size(b
) >= (i
/ 2) * sizeof(int));
54 for (j
= 30; j
< i
/ 2; j
+= 7)
55 assert_se(b
[j
] == (int) j
);
58 static void test_memdup_multiply_and_greedy_realloc(void) {
59 static const int org
[] = { 1, 2, 3 };
60 _cleanup_free_
int *dup
;
62 size_t i
, allocated
= 3;
64 dup
= memdup_suffix0_multiply(org
, sizeof(int), 3);
66 assert_se(dup
[0] == 1);
67 assert_se(dup
[1] == 2);
68 assert_se(dup
[2] == 3);
69 assert_se(((uint8_t*) dup
)[sizeof(int) * 3] == 0);
72 dup
= memdup_multiply(org
, sizeof(int), 3);
74 assert_se(dup
[0] == 1);
75 assert_se(dup
[1] == 2);
76 assert_se(dup
[2] == 3);
79 assert_se(greedy_realloc0((void**) &dup
, &allocated
, 2, sizeof(int)) == p
);
81 p
= (int *) greedy_realloc0((void**) &dup
, &allocated
, 10, sizeof(int));
83 assert_se(allocated
>= 10);
87 for (i
= 3; i
< allocated
; i
++)
91 static void test_bool_assign(void) {
92 bool b
, c
, *cp
= &c
, d
, e
, f
, g
, h
;
99 g
= cp
; /* cast from pointer */
100 h
= NULL
; /* cast from pointer */
111 static int cleanup_counter
= 0;
113 static void cleanup1(void *a
) {
114 log_info("%s(%p)", __func__
, a
);
115 assert_se(++cleanup_counter
== *(int*) a
);
117 static void cleanup2(void *a
) {
118 log_info("%s(%p)", __func__
, a
);
119 assert_se(++cleanup_counter
== *(int*) a
);
121 static void cleanup3(void *a
) {
122 log_info("%s(%p)", __func__
, a
);
123 assert_se(++cleanup_counter
== *(int*) a
);
126 static void test_cleanup_order(void) {
127 _cleanup_(cleanup1
) int x1
= 4, x2
= 3;
128 _cleanup_(cleanup3
) int z
= 2;
129 _cleanup_(cleanup2
) int y
= 1;
130 log_debug("x1: %p", &x1
);
131 log_debug("x2: %p", &x2
);
132 log_debug("y: %p", &y
);
133 log_debug("z: %p", &z
);
136 static void test_auto_erase_memory(void) {
137 _cleanup_(erase_and_freep
) uint8_t *p1
, *p2
;
139 assert_se(p1
= new(uint8_t, 1024));
140 assert_se(p2
= new(uint8_t, 1024));
142 assert_se(genuine_random_bytes(p1
, 1024, RANDOM_BLOCK
) == 0);
144 /* before we exit the scope, do something with this data, so that the compiler won't optimize this away */
145 memcpy(p2
, p1
, 1024);
146 for (size_t i
= 0; i
< 1024; i
++)
147 assert_se(p1
[i
] == p2
[i
]);
150 int main(int argc
, char *argv
[]) {
151 test_setup_logging(LOG_DEBUG
);
154 test_GREEDY_REALLOC();
155 test_memdup_multiply_and_greedy_realloc();
157 test_cleanup_order();
158 test_auto_erase_memory();