sysdeps: sem_open: Clear O_CREAT when semaphore file is expected to exist [BZ #30789]
[glibc.git] / malloc / tst-memalign-2.c
blobecd6fa249eb38df7f8f741de67b8291f014020fb
1 /* Test for memalign chunk reuse.
2 Copyright (C) 2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
28 typedef struct TestCase {
29 size_t size;
30 size_t alignment;
31 void *ptr1;
32 void *ptr2;
33 } TestCase;
35 static TestCase tcache_allocs[] = {
36 { 24, 32, NULL, NULL },
37 { 24, 64, NULL, NULL },
38 { 128, 128, NULL, NULL },
39 { 500, 128, NULL, NULL }
41 #define TN array_length (tcache_allocs)
43 static TestCase large_allocs[] = {
44 { 23450, 64, NULL, NULL },
45 { 23450, 64, NULL, NULL },
46 { 23550, 64, NULL, NULL },
47 { 23550, 64, NULL, NULL },
48 { 23650, 64, NULL, NULL },
49 { 23650, 64, NULL, NULL },
50 { 33650, 64, NULL, NULL },
51 { 33650, 64, NULL, NULL }
53 #define LN array_length (large_allocs)
55 void *p;
57 /* Sanity checks, ancillary to the actual test. */
58 #define CHECK(p,a) \
59 if (p == NULL || !PTR_IS_ALIGNED (p, a)) \
60 FAIL_EXIT1 ("NULL or misaligned memory detected.\n");
62 static int
63 do_test (void)
65 int i, j;
66 int count;
67 void *ptr[10];
68 void *p;
70 /* TCache test. */
72 for (i = 0; i < TN; ++ i)
74 size_t sz2;
76 tcache_allocs[i].ptr1 = memalign (tcache_allocs[i].alignment, tcache_allocs[i].size);
77 CHECK (tcache_allocs[i].ptr1, tcache_allocs[i].alignment);
78 sz2 = malloc_usable_size (tcache_allocs[i].ptr1);
79 free (tcache_allocs[i].ptr1);
81 /* This should return the same chunk as was just free'd. */
82 tcache_allocs[i].ptr2 = memalign (tcache_allocs[i].alignment, sz2);
83 CHECK (tcache_allocs[i].ptr2, tcache_allocs[i].alignment);
84 free (tcache_allocs[i].ptr2);
86 TEST_VERIFY (tcache_allocs[i].ptr1 == tcache_allocs[i].ptr2);
89 /* Test for non-head tcache hits. This exercises the memalign
90 scanning code to find matching allocations. */
91 for (i = 0; i < array_length (ptr); ++ i)
93 if (i == 4)
95 ptr[i] = memalign (64, 256);
96 CHECK (ptr[i], 64);
98 else
100 ptr[i] = malloc (256);
101 CHECK (ptr[i], 4);
104 for (i = 0; i < array_length (ptr); ++ i)
105 free (ptr[i]);
107 p = memalign (64, 256);
108 CHECK (p, 64);
110 count = 0;
111 for (i = 0; i < 10; ++ i)
112 if (ptr[i] == p)
113 ++ count;
114 free (p);
115 TEST_VERIFY (count > 0);
117 /* Large bins test. This verifies that the over-allocated parts
118 that memalign releases for future allocations can be reused by
119 memalign itself at least in some cases. */
121 for (i = 0; i < LN; ++ i)
123 large_allocs[i].ptr1 = memalign (large_allocs[i].alignment, 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 (512);
127 CHECK (p, 4);
130 for (i = 0; i < LN; ++ i)
131 free (large_allocs[i].ptr1);
133 /* Force the unsorted bins to be scanned and moved to small/large
134 bins. */
135 p = malloc (60000);
137 for (i = 0; i < LN; ++ i)
139 large_allocs[i].ptr2 = memalign (large_allocs[i].alignment, large_allocs[i].size);
140 CHECK (large_allocs[i].ptr2, large_allocs[i].alignment);
143 count = 0;
144 for (i = 0; i < LN; ++ i)
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 ++;
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);
160 return 0;
163 #include <support/test-driver.c>