d3d9: fix compilation after opengl rework
[vlc.git] / compat / test / heap.c
bloba4933295ffd4e873a2cbf7089d4df4970fdb558a
1 /*****************************************************************************
2 * aligned_alloc test case
3 *****************************************************************************
4 * Copyright © 2019 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #include "config.h"
22 #undef NDEBUG
23 #include <assert.h>
24 #include <stdalign.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <errno.h>
30 struct big_align_struct {
31 long long ll;
32 double d;
35 /* Supported alignments. Others are undefined (ISO C11 §7.22.3, §J.2). */
36 static const size_t alignv[] = {
37 alignof (char),
38 alignof (short),
39 alignof (int),
40 alignof (long),
41 alignof (long long),
42 alignof (float),
43 alignof (double),
44 alignof (struct big_align_struct),
45 alignof (void *),
46 alignof (max_align_t),
49 static const size_t alignc = sizeof (alignv) / sizeof (alignv[0]);
51 static void test_posix_memalign(size_t align, size_t size)
53 void *p;
54 int val = posix_memalign(&p, align, size);
56 if (align >= sizeof (void *)) {
57 if (val == 0) {
58 assert(((uintptr_t)p & (align - 1)) == 0);
59 free(p);
61 } else
62 assert(val != 0);
65 int main(void)
67 void *p;
69 /* aligned_alloc() */
71 for (size_t i = 0; i < alignc; i++) {
72 size_t align = alignv[i];
74 assert((align & (align - 1)) == 0); /* must be a power of two */
76 p = aligned_alloc(alignv[i], 0);
77 free(p); /* must free {aligned_,c,m,c,re}alloc() allocations */
79 for (size_t j = 0; j < alignc; j++) {
80 size_t size = alignv[j];
82 if (size < align)
83 continue; /* size must be a multiple of alignment */
85 p = aligned_alloc(align, size);
86 assert(p != NULL); /* small non-zero bytes allocation */
87 assert(((uintptr_t)p & (align - 1)) == 0);
88 free(p);
92 /* posix_memalign() */
94 for (size_t i = 0; i < 21; i++) {
95 size_t align = (size_t)1 << i;
97 test_posix_memalign(align, 0);
98 test_posix_memalign(align, 1);
99 test_posix_memalign(align, align - 1);
100 test_posix_memalign(align, align);
101 test_posix_memalign(align, align * 2);
104 return 0;