Bug 466762 - Add redirs for C23 free_sized() and free_aligned_sized()
[valgrind.git] / memcheck / tests / linux / memalign.c
blobd8ef080e1a96c8f6529cffa159ed683f13ead361
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <assert.h>
4 #include "tests/malloc.h"
5 #include <errno.h>
6 #include "../../../config.h"
8 int main ( void )
10 // Nb: assuming VG_MIN_MALLOC_SZB is 8 or more...
11 int* p;
12 int* piece;
13 assert(sizeof(long int) == sizeof(void*));
15 #if !defined(MUSL_LIBC)
16 // Check behaviour of memalign/free for big alignment.
17 // In particular, the below aims at checking that a
18 // superblock with a big size is not marked as reclaimable
19 // if the superblock is used to provide a big aligned block
20 // (see bug 250101, comment #14).
21 // Valgrind m_mallocfree.c will allocate a big superblock for the memalign
22 // call and will split it in two. This split superblock was
23 // wrongly marked as reclaimable, which was then causing
24 // assert failures (as reclaimable blocks cannot be split).
25 p = memalign(1024 * 1024, 4 * 1024 * 1024 + 1);
26 assert(p && (0 == (long)p % (1024 * 1024)));
27 // We allocate (and then free) a piece of memory smaller than
28 // the hole created in the big superblock.
29 // If the superblock is marked as reclaimable, the below free(s) will cause
30 // an assert. Note that the test has to be run with a --free-list-vol
31 // parameter smaller than the released blocks size to ensure the free is directly
32 // executed (otherwise memcheck does not really release the memory and so
33 // the bug is not properly tested).
34 piece = malloc(1024 * 1000);
35 assert (piece);
36 free (piece);
37 free (p);
39 // Same as above but do the free in the reverse order.
40 p = memalign(1024 * 1024, 4 * 1024 * 1024 + 1);
41 assert(p && (0 == (long)p % (1024 * 1024)));
42 piece = malloc(1024 * 100);
43 assert (piece);
44 free (p);
45 free (piece);
47 p = memalign(0, 100);
48 assert(p && (0 == (long)p % 8));
49 p = memalign(1, 100);
50 assert(p && (0 == (long)p % 8));
51 p = memalign(2, 100);
52 assert(p && (0 == (long)p % 8));
53 p = memalign(3, 100);
54 assert(p && (0 == (long)p % 8));
55 p = memalign(4, 100);
56 assert(p && (0 == (long)p % 8));
57 p = memalign(5, 100);
58 assert(p && (0 == (long)p % 8));
60 p = memalign(7, 100);
61 assert(p && (0 == (long)p % 8));
62 p = memalign(8, 100);
63 assert(p && (0 == (long)p % 8));
64 p = memalign(9, 100);
65 assert(p && (0 == (long)p % 16));
67 p = memalign(31, 100);
68 assert(p && (0 == (long)p % 32));
69 p = memalign(32, 100);
70 assert(p && (0 == (long)p % 32));
71 p = memalign(33, 100);
72 assert(p && (0 == (long)p % 64));
74 p = memalign(4095, 100);
75 assert(p && (0 == (long)p % 4096));
76 p = memalign(4096, 100);
77 assert(p && (0 == (long)p % 4096));
78 p = memalign(4097, 100);
79 assert(p && (0 == (long)p % 8192));
81 p = memalign(4 * 1024 * 1024, 100);
82 assert(p && (0 == (long)p % (4 * 1024 * 1024)));
83 p = memalign(16 * 1024 * 1024, 100);
84 assert(p && (0 == (long)p % (16 * 1024 * 1024)));
86 // size 0
87 p = memalign(256, 0);
88 assert(p && (0 == (long)p % 256));
89 #else
90 p = memalign(1024 * 1024, 4 * 1024 * 1024 + 1);
91 assert(p && (0 == (long)p % (1024 * 1024)));
92 piece = malloc(1024 * 1000); assert (piece);
93 free (piece);
94 free (p);
95 p = memalign(1024 * 1024, 4 * 1024 * 1024 + 1);
96 assert(p && (0 == (long)p % (1024 * 1024)));
97 piece = malloc(1024 * 100);
98 assert (piece);
99 free (p);
100 free (piece);
102 errno = 0;
103 p = memalign(0, 100);
104 assert(p && (0 == (long)p % 8));
105 p = memalign(1, 100);
106 assert(p && (0 == (long)p % 8));
107 p = memalign(2, 100);
108 assert(p && (0 == (long)p % 8));
109 p = memalign(3, 100);
110 assert(!p);
111 //assert(errno == EINVAL);
112 errno = 0;
113 p = memalign(4, 100);
114 assert(p && 0 == (long)p % 8);
115 p = memalign(5, 100);
116 assert(!p);
117 //assert(errno == EINVAL);
118 errno = 0;
119 p = memalign(7, 100);
120 assert(!p);
121 //assert(errno == EINVAL);
122 errno = 0;
123 p = memalign(8, 100);
124 assert(p && (0 == (long)p % 8));
125 p = memalign(9, 100);
126 assert(!p);
127 //assert(errno == EINVAL);
128 errno = 0;
129 p = memalign(31, 100);
130 assert(!p);
131 //assert(errno == EINVAL);
132 p = memalign(32, 100);
133 assert(p && (0 == (long)p % 32));
134 errno = 0;
135 p = memalign(33, 100);
136 assert(!p);
137 //assert(errno == EINVAL);
138 errno = 0;
139 p = memalign(4095, 100);
140 assert(!p);
141 //assert(errno == EINVAL);
142 p = memalign(4096, 100);
143 assert(p && (0 == (long)p % 4096));
144 errno = 0;
145 p = memalign(4097, 100);
146 assert(!p);
147 //assert(errno == EINVAL);
149 p = memalign(4 * 1024 * 1024, 100);
150 assert(p && (0 == (long)p % (4 * 1024 * 1024)));
151 p = memalign(16 * 1024 * 1024, 100);
152 assert(p && (0 == (long)p % (16 * 1024 * 1024)));
154 // size 0
155 p = memalign(256, 0);
156 assert(p && (0 == (long)p % 256));
157 #endif