1 | /* Test for memalign chunk reuse. |
2 | Copyright (C) 2022-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <malloc.h> |
21 | #include <stdio.h> |
22 | #include <pthread.h> |
23 | #include <string.h> |
24 | #include <unistd.h> |
25 | #include <array_length.h> |
26 | #include <libc-pointer-arith.h> |
27 | #include <support/check.h> |
28 | #include <support/xthread.h> |
29 | |
30 | |
31 | typedef struct TestCase { |
32 | size_t size; |
33 | size_t alignment; |
34 | void *ptr1; |
35 | void *ptr2; |
36 | } TestCase; |
37 | |
38 | static TestCase tcache_allocs[] = { |
39 | { 24, 32, NULL, NULL }, |
40 | { 24, 64, NULL, NULL }, |
41 | { 128, 128, NULL, NULL }, |
42 | { 500, 128, NULL, NULL } |
43 | }; |
44 | #define TN array_length (tcache_allocs) |
45 | |
46 | static TestCase large_allocs[] = { |
47 | { 23450, 64, NULL, NULL }, |
48 | { 23450, 64, NULL, NULL }, |
49 | { 23550, 64, NULL, NULL }, |
50 | { 23550, 64, NULL, NULL }, |
51 | { 23650, 64, NULL, NULL }, |
52 | { 23650, 64, NULL, NULL }, |
53 | { 33650, 64, NULL, NULL }, |
54 | { 33650, 64, NULL, NULL } |
55 | }; |
56 | #define LN array_length (large_allocs) |
57 | |
58 | void *p; |
59 | |
60 | /* Sanity checks, ancillary to the actual test. */ |
61 | #define CHECK(p,a) \ |
62 | if (p == NULL || !PTR_IS_ALIGNED (p, a)) \ |
63 | FAIL_EXIT1 ("NULL or misaligned memory detected.\n"); |
64 | |
65 | static void * |
66 | mem_test (void *closure) |
67 | { |
68 | int i; |
69 | int j; |
70 | int count; |
71 | void *ptr[10]; |
72 | void *p; |
73 | |
74 | /* TCache test. */ |
75 | for (i = 0; i < TN; ++ i) |
76 | { |
77 | size_t sz2; |
78 | |
79 | tcache_allocs[i].ptr1 = memalign (alignment: tcache_allocs[i].alignment, size: tcache_allocs[i].size); |
80 | CHECK (tcache_allocs[i].ptr1, tcache_allocs[i].alignment); |
81 | sz2 = malloc_usable_size (ptr: tcache_allocs[i].ptr1); |
82 | free (ptr: tcache_allocs[i].ptr1); |
83 | |
84 | /* This should return the same chunk as was just free'd. */ |
85 | tcache_allocs[i].ptr2 = memalign (alignment: tcache_allocs[i].alignment, size: sz2); |
86 | CHECK (tcache_allocs[i].ptr2, tcache_allocs[i].alignment); |
87 | free (ptr: tcache_allocs[i].ptr2); |
88 | |
89 | TEST_VERIFY (tcache_allocs[i].ptr1 == tcache_allocs[i].ptr2); |
90 | } |
91 | |
92 | /* Test for non-head tcache hits. */ |
93 | for (i = 0; i < array_length (ptr); ++ i) |
94 | { |
95 | if (i == 4) |
96 | { |
97 | ptr[i] = memalign (alignment: 64, size: 256); |
98 | CHECK (ptr[i], 64); |
99 | } |
100 | else |
101 | { |
102 | ptr[i] = malloc (size: 256); |
103 | CHECK (ptr[i], 4); |
104 | } |
105 | } |
106 | for (i = 0; i < array_length (ptr); ++ i) |
107 | free (ptr: ptr[i]); |
108 | |
109 | p = memalign (alignment: 64, size: 256); |
110 | CHECK (p, 64); |
111 | |
112 | count = 0; |
113 | for (i = 0; i < 10; ++ i) |
114 | if (ptr[i] == p) |
115 | ++ count; |
116 | free (ptr: p); |
117 | TEST_VERIFY (count > 0); |
118 | |
119 | /* Large bins test. */ |
120 | |
121 | for (i = 0; i < LN; ++ i) |
122 | { |
123 | large_allocs[i].ptr1 = memalign (alignment: large_allocs[i].alignment, size: large_allocs[i].size); |
124 | CHECK (large_allocs[i].ptr1, large_allocs[i].alignment); |
125 | /* Keep chunks from combining by fragmenting the heap. */ |
126 | p = malloc (size: 512); |
127 | CHECK (p, 4); |
128 | } |
129 | |
130 | for (i = 0; i < LN; ++ i) |
131 | free (ptr: large_allocs[i].ptr1); |
132 | |
133 | /* Force the unsorted bins to be scanned and moved to small/large |
134 | bins. */ |
135 | p = malloc (size: 60000); |
136 | |
137 | for (i = 0; i < LN; ++ i) |
138 | { |
139 | large_allocs[i].ptr2 = memalign (alignment: large_allocs[i].alignment, size: large_allocs[i].size); |
140 | CHECK (large_allocs[i].ptr2, large_allocs[i].alignment); |
141 | } |
142 | |
143 | count = 0; |
144 | for (i = 0; i < LN; ++ i) |
145 | { |
146 | int ok = 0; |
147 | for (j = 0; j < LN; ++ j) |
148 | if (large_allocs[i].ptr1 == large_allocs[j].ptr2) |
149 | ok = 1; |
150 | if (ok == 1) |
151 | count ++; |
152 | } |
153 | |
154 | /* The allocation algorithm is complicated outside of the memalign |
155 | logic, so just make sure it's working for most of the |
156 | allocations. This avoids possible boundary conditions with |
157 | empty/full heaps. */ |
158 | TEST_VERIFY (count > LN / 2); |
159 | |
160 | return 0; |
161 | } |
162 | |
163 | static int |
164 | do_test (void) |
165 | { |
166 | pthread_t p; |
167 | |
168 | p = xpthread_create (NULL, thread_func: mem_test, NULL); |
169 | xpthread_join (thr: p); |
170 | return 0; |
171 | } |
172 | |
173 | #include <support/test-driver.c> |
174 | |