]> git.ipfire.org Git - thirdparty/glibc.git/blob - malloc/tst-memalign-2.c
4996578e9fb39f3b894710777c0450e3dc24a0df
[thirdparty/glibc.git] / malloc / tst-memalign-2.c
1 /* Test for memalign chunk reuse.
2 Copyright (C) 2022 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 <string.h>
23 #include <unistd.h>
24 #include <array_length.h>
25 #include <libc-pointer-arith.h>
26 #include <support/check.h>
27
28 typedef struct TestCase {
29 size_t size;
30 size_t alignment;
31 void *ptr1;
32 void *ptr2;
33 } TestCase;
34
35 static TestCase tcache_allocs[] = {
36 { 24, 8, NULL, NULL },
37 { 24, 16, NULL, NULL },
38 { 128, 32, NULL, NULL }
39 };
40 #define TN array_length (tcache_allocs)
41
42 static TestCase large_allocs[] = {
43 { 23450, 64, NULL, NULL },
44 { 23450, 64, NULL, NULL },
45 { 23550, 64, NULL, NULL },
46 { 23550, 64, NULL, NULL },
47 { 23650, 64, NULL, NULL },
48 { 23650, 64, NULL, NULL },
49 { 33650, 64, NULL, NULL },
50 { 33650, 64, NULL, NULL }
51 };
52 #define LN array_length (large_allocs)
53
54 void *p;
55
56 /* Sanity checks, ancillary to the actual test. */
57 #define CHECK(p,a) \
58 if (p == NULL || !PTR_IS_ALIGNED (p, a)) \
59 FAIL_EXIT1 ("NULL or misaligned memory detected.\n");
60
61 static int
62 do_test (void)
63 {
64 int i, j;
65 int count;
66 void *ptr[10];
67 void *p;
68
69 /* TCache test. */
70
71 for (i = 0; i < TN; ++ i)
72 {
73 tcache_allocs[i].ptr1 = memalign (tcache_allocs[i].alignment, tcache_allocs[i].size);
74 CHECK (tcache_allocs[i].ptr1, tcache_allocs[i].alignment);
75 free (tcache_allocs[i].ptr1);
76 /* This should return the same chunk as was just free'd. */
77 tcache_allocs[i].ptr2 = memalign (tcache_allocs[i].alignment, tcache_allocs[i].size);
78 CHECK (tcache_allocs[i].ptr2, tcache_allocs[i].alignment);
79 free (tcache_allocs[i].ptr2);
80
81 TEST_VERIFY (tcache_allocs[i].ptr1 == tcache_allocs[i].ptr2);
82 }
83
84 /* Test for non-head tcache hits. */
85 for (i = 0; i < array_length (ptr); ++ i)
86 {
87 if (i == 4)
88 {
89 ptr[i] = memalign (64, 256);
90 CHECK (ptr[i], 64);
91 }
92 else
93 {
94 ptr[i] = malloc (256);
95 CHECK (ptr[i], 4);
96 }
97 }
98 for (i = 0; i < array_length (ptr); ++ i)
99 free (ptr[i]);
100
101 p = memalign (64, 256);
102 CHECK (p, 64);
103
104 count = 0;
105 for (i = 0; i < 10; ++ i)
106 if (ptr[i] == p)
107 ++ count;
108 free (p);
109 TEST_VERIFY (count > 0);
110
111 /* Large bins test. */
112
113 for (i = 0; i < LN; ++ i)
114 {
115 large_allocs[i].ptr1 = memalign (large_allocs[i].alignment, large_allocs[i].size);
116 CHECK (large_allocs[i].ptr1, large_allocs[i].alignment);
117 /* Keep chunks from combining by fragmenting the heap. */
118 p = malloc (512);
119 CHECK (p, 4);
120 }
121
122 for (i = 0; i < LN; ++ i)
123 free (large_allocs[i].ptr1);
124
125 /* Force the unsorted bins to be scanned and moved to small/large
126 bins. */
127 p = malloc (60000);
128
129 for (i = 0; i < LN; ++ i)
130 {
131 large_allocs[i].ptr2 = memalign (large_allocs[i].alignment, large_allocs[i].size);
132 CHECK (large_allocs[i].ptr2, large_allocs[i].alignment);
133 }
134
135 count = 0;
136 for (i = 0; i < LN; ++ i)
137 {
138 int ok = 0;
139 for (j = 0; j < LN; ++ j)
140 if (large_allocs[i].ptr1 == large_allocs[j].ptr2)
141 ok = 1;
142 if (ok == 1)
143 count ++;
144 }
145
146 /* The allocation algorithm is complicated outside of the memalign
147 logic, so just make sure it's working for most of the
148 allocations. This avoids possible boundary conditions with
149 empty/full heaps. */
150 TEST_VERIFY (count > LN / 2);
151
152 return 0;
153 }
154
155 #include <support/test-driver.c>