1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // heap.cfa -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Tue Nov 6 17:54:56 2018 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Dec 11 21:52:40 2018 |
---|
13 | // Update Count : 18 |
---|
14 | // |
---|
15 | |
---|
16 | #include <thread.hfa> |
---|
17 | #include <kernel.hfa> // processor |
---|
18 | #include <stdlib.hfa> // *allocs |
---|
19 | #include <malloc.h> // malloc_* |
---|
20 | |
---|
21 | // #include <time.hfa> |
---|
22 | // #define __CFA_DEFAULT_PREEMPTION__ 1000`us |
---|
23 | // //#define __CFA_DEFAULT_PREEMPTION__ 0 |
---|
24 | |
---|
25 | // Duration default_preemption() { |
---|
26 | // return __CFA_DEFAULT_PREEMPTION__; |
---|
27 | // } |
---|
28 | |
---|
29 | #define __U_DEFAULT_MMAP_START__ (512 * 1024 + 1) |
---|
30 | size_t default_mmap_start() __attribute__(( weak )) { |
---|
31 | return __U_DEFAULT_MMAP_START__; |
---|
32 | } // default_mmap_start |
---|
33 | |
---|
34 | thread Worker { |
---|
35 | }; // Worker |
---|
36 | |
---|
37 | void main( Worker & ) { |
---|
38 | enum { NoOfAllocs = 5000, NoOfMmaps = 10 }; |
---|
39 | char * locns[NoOfAllocs]; |
---|
40 | int i; |
---|
41 | |
---|
42 | // check alloc/free |
---|
43 | |
---|
44 | for ( j; 40 ) { |
---|
45 | for ( i; NoOfAllocs ) { |
---|
46 | locns[i] = alloc( i ); |
---|
47 | //sout | (void *)locns[i]; |
---|
48 | for ( k; i ) locns[i][k] = '\345'; |
---|
49 | } // for |
---|
50 | //sout | (char *)sbrk(0) - start | " bytes"; |
---|
51 | |
---|
52 | for ( i; NoOfAllocs ) { |
---|
53 | //sout | (void *)locns[i]; |
---|
54 | for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage1" ); |
---|
55 | free( locns[i] ); |
---|
56 | } // for |
---|
57 | //sout | (char *)sbrk(0) - start | " bytes"; |
---|
58 | |
---|
59 | for ( i; NoOfAllocs ) { |
---|
60 | locns[i] = alloc( i ); |
---|
61 | //sout | (void *)locns[i]; |
---|
62 | for ( k; i ) locns[i][k] = '\345'; |
---|
63 | } // for |
---|
64 | for ( i; NoOfAllocs - 1 -~= 0 ) { |
---|
65 | //sout | (void *)locns[i]; |
---|
66 | for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage2" ); |
---|
67 | free( locns[i] ); |
---|
68 | } // for |
---|
69 | } // for |
---|
70 | |
---|
71 | // check malloc/free (sbrk) |
---|
72 | |
---|
73 | for ( i; NoOfAllocs ) { |
---|
74 | size_t s = (i + 1) * 20; |
---|
75 | char * area = (char *)malloc( s ); |
---|
76 | if ( area == 0 ) abort( "malloc/free out of memory" ); |
---|
77 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
78 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
79 | free( area ); |
---|
80 | } // for |
---|
81 | |
---|
82 | for ( i; NoOfAllocs ) { |
---|
83 | size_t s = i + 1; // +1 to make initialization simpler |
---|
84 | locns[i] = (char *)malloc( s ); |
---|
85 | if ( locns[i] == 0 ) abort( "malloc/free out of memory" ); |
---|
86 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
87 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
88 | } // for |
---|
89 | for ( i; NoOfAllocs ) { |
---|
90 | size_t s = i + 1; |
---|
91 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
92 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" ); |
---|
93 | free( locns[i] ); |
---|
94 | } // for |
---|
95 | |
---|
96 | // check malloc/free (mmap) |
---|
97 | |
---|
98 | for ( i; NoOfMmaps ) { |
---|
99 | size_t s = i + default_mmap_start(); // cross over point |
---|
100 | char * area = (char *)malloc( s ); |
---|
101 | if ( area == 0 ) abort( "malloc/free out of memory" ); |
---|
102 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
103 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
104 | free( area ); |
---|
105 | } // for |
---|
106 | |
---|
107 | for ( i; NoOfMmaps ) { |
---|
108 | size_t s = i + default_mmap_start(); // cross over point |
---|
109 | locns[i] = (char *)malloc( s ); |
---|
110 | if ( locns[i] == 0 ) abort( "malloc/free out of memory" ); |
---|
111 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
112 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
113 | } // for |
---|
114 | for ( i; NoOfMmaps ) { |
---|
115 | size_t s = i + default_mmap_start(); // cross over point |
---|
116 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
117 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" ); |
---|
118 | free( locns[i] ); |
---|
119 | } // for |
---|
120 | |
---|
121 | // check calloc/free (sbrk) |
---|
122 | |
---|
123 | for ( i; NoOfAllocs ) { |
---|
124 | size_t s = (i + 1) * 20; |
---|
125 | char * area = (char *)calloc( 5, s ); |
---|
126 | if ( area == 0 ) abort( "calloc/free out of memory" ); |
---|
127 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
128 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
129 | ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage1" ); |
---|
130 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
131 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
132 | free( area ); |
---|
133 | } // for |
---|
134 | |
---|
135 | for ( i; NoOfAllocs ) { |
---|
136 | size_t s = i + 1; |
---|
137 | locns[i] = (char *)calloc( 5, s ); |
---|
138 | if ( locns[i] == 0 ) abort( "calloc/free out of memory" ); |
---|
139 | if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' || |
---|
140 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\0' || |
---|
141 | ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage2" ); |
---|
142 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
143 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
144 | } // for |
---|
145 | for ( i; NoOfAllocs ) { |
---|
146 | size_t s = i + 1; |
---|
147 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
148 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage3" ); |
---|
149 | free( locns[i] ); |
---|
150 | } // for |
---|
151 | |
---|
152 | // check calloc/free (mmap) |
---|
153 | |
---|
154 | for ( i; NoOfMmaps ) { |
---|
155 | size_t s = i + default_mmap_start(); // cross over point |
---|
156 | char * area = (char *)calloc( 1, s ); |
---|
157 | if ( area == 0 ) abort( "calloc/free out of memory" ); |
---|
158 | if ( area[0] != '\0' || area[s - 1] != '\0' ) abort( "calloc/free corrupt storage4.1" ); |
---|
159 | if ( area[malloc_usable_size( area ) - 1] != '\0' ) abort( "calloc/free corrupt storage4.2" ); |
---|
160 | if ( ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage4.3" ); |
---|
161 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
162 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
163 | free( area ); |
---|
164 | } // for |
---|
165 | |
---|
166 | for ( i; NoOfMmaps ) { |
---|
167 | size_t s = i + default_mmap_start(); // cross over point |
---|
168 | locns[i] = (char *)calloc( 1, s ); |
---|
169 | if ( locns[i] == 0 ) abort( "calloc/free out of memory" ); |
---|
170 | if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' || |
---|
171 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\0' || |
---|
172 | ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage5" ); |
---|
173 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
174 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
175 | } // for |
---|
176 | for ( i; NoOfMmaps ) { |
---|
177 | size_t s = i + default_mmap_start(); // cross over point |
---|
178 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
179 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage6" ); |
---|
180 | free( locns[i] ); |
---|
181 | } // for |
---|
182 | |
---|
183 | // check memalign/free (sbrk) |
---|
184 | |
---|
185 | enum { limit = 64 * 1024 }; // check alignments up to here |
---|
186 | |
---|
187 | for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2 |
---|
188 | //sout | alignments[a]; |
---|
189 | for ( s; 1 ~ NoOfAllocs ) { // allocation of size 0 can return null |
---|
190 | char * area = (char *)memalign( a, s ); |
---|
191 | if ( area == 0 ) abort( "memalign/free out of memory" ); |
---|
192 | //sout | i | " " | area; |
---|
193 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
194 | abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, s, area ); |
---|
195 | } // if |
---|
196 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte |
---|
197 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
198 | free( area ); |
---|
199 | } // for |
---|
200 | } // for |
---|
201 | |
---|
202 | // check memalign/free (mmap) |
---|
203 | |
---|
204 | for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2 |
---|
205 | //sout | alignments[a]; |
---|
206 | for ( i; 1 ~ NoOfMmaps ) { |
---|
207 | size_t s = i + default_mmap_start(); // cross over point |
---|
208 | char * area = (char *)memalign( a, s ); |
---|
209 | if ( area == 0 ) abort( "memalign/free out of memory" ); |
---|
210 | //sout | i | " " | area; |
---|
211 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
212 | abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)s, area ); |
---|
213 | } // if |
---|
214 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte |
---|
215 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
216 | free( area ); |
---|
217 | } // for |
---|
218 | } // for |
---|
219 | |
---|
220 | // check calloc/realloc/free (sbrk) |
---|
221 | |
---|
222 | for ( i; 1 ~ 10_000 ~ 12 ) { |
---|
223 | // initial N byte allocation |
---|
224 | char * area = (char *)calloc( 5, i ); |
---|
225 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
226 | if ( area[0] != '\0' || area[i - 1] != '\0' || |
---|
227 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
228 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" ); |
---|
229 | |
---|
230 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
231 | for ( s; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request |
---|
232 | area = (char *)realloc( area, s ); // attempt to reuse storage |
---|
233 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
234 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
235 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
236 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" ); |
---|
237 | } // for |
---|
238 | free( area ); |
---|
239 | } // for |
---|
240 | |
---|
241 | // check calloc/realloc/free (mmap) |
---|
242 | |
---|
243 | for ( i; 1 ~ 10_000 ~ 12 ) { |
---|
244 | // initial N byte allocation |
---|
245 | size_t s = i + default_mmap_start(); // cross over point |
---|
246 | char * area = (char *)calloc( 1, s ); |
---|
247 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
248 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
249 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
250 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" ); |
---|
251 | |
---|
252 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
253 | for ( r; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request |
---|
254 | area = (char *)realloc( area, r ); // attempt to reuse storage |
---|
255 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
256 | if ( area[0] != '\0' || area[r - 1] != '\0' || |
---|
257 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
258 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" ); |
---|
259 | } // for |
---|
260 | free( area ); |
---|
261 | } // for |
---|
262 | |
---|
263 | // check memalign/realloc/free |
---|
264 | |
---|
265 | size_t amount = 2; |
---|
266 | for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2 |
---|
267 | // initial N byte allocation |
---|
268 | char * area = (char *)memalign( a, amount ); // aligned N-byte allocation |
---|
269 | if ( area == 0 ) abort( "memalign/realloc/free out of memory" ); // no storage ? |
---|
270 | //sout | alignments[a] | " " | area; |
---|
271 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
272 | abort( "memalign/realloc/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area ); |
---|
273 | } // if |
---|
274 | area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte |
---|
275 | |
---|
276 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
277 | for ( s; amount ~ 256 * 1024 ) { // start at initial memory request |
---|
278 | if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" ); |
---|
279 | area = (char *)realloc( area, s ); // attempt to reuse storage |
---|
280 | if ( area == 0 ) abort( "memalign/realloc/free out of memory" ); // no storage ? |
---|
281 | //sout | i | " " | area; |
---|
282 | if ( (size_t)area % a != 0 ) { // check for initial alignment |
---|
283 | abort( "memalign/realloc/free bad alignment %p", area ); |
---|
284 | } // if |
---|
285 | area[s - 1] = '\345'; // fill last byte |
---|
286 | } // for |
---|
287 | free( area ); |
---|
288 | } // for |
---|
289 | |
---|
290 | // check cmemalign/free |
---|
291 | |
---|
292 | for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2 |
---|
293 | //sout | alignments[a]; |
---|
294 | for ( s; 1 ~ limit ) { // allocation of size 0 can return null |
---|
295 | char * area = (char *)cmemalign( a, 1, s ); |
---|
296 | if ( area == 0 ) abort( "cmemalign/free out of memory" ); |
---|
297 | //sout | i | " " | area; |
---|
298 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
299 | abort( "cmemalign/free bad alignment : cmemalign(%d,%d) = %p", (int)a, s, area ); |
---|
300 | } // if |
---|
301 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
302 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
303 | ! malloc_zero_fill( area ) ) abort( "cmemalign/free corrupt storage" ); |
---|
304 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte |
---|
305 | free( area ); |
---|
306 | } // for |
---|
307 | } // for |
---|
308 | |
---|
309 | // check cmemalign/realloc/free |
---|
310 | |
---|
311 | amount = 2; |
---|
312 | for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2 |
---|
313 | // initial N byte allocation |
---|
314 | char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation |
---|
315 | if ( area == 0 ) abort( "cmemalign/realloc/free out of memory" ); // no storage ? |
---|
316 | //sout | alignments[a] | " " | area; |
---|
317 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
318 | abort( "cmemalign/realloc/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area ); |
---|
319 | } // if |
---|
320 | if ( area[0] != '\0' || area[amount - 1] != '\0' || |
---|
321 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
322 | ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage1" ); |
---|
323 | area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte |
---|
324 | |
---|
325 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
326 | for ( s; amount ~ 256 * 1024 ) { // start at initial memory request |
---|
327 | if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc/free corrupt storage2" ); |
---|
328 | area = (char *)realloc( area, s ); // attempt to reuse storage |
---|
329 | if ( area == 0 ) abort( "cmemalign/realloc/free out of memory" ); // no storage ? |
---|
330 | //sout | i | " " | area; |
---|
331 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
332 | abort( "cmemalign/realloc/free bad alignment %p", area ); |
---|
333 | } // if |
---|
334 | if ( area[s - 1] != '\0' || area[s - 1] != '\0' || |
---|
335 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
336 | ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" ); |
---|
337 | area[s - 1] = '\345'; // fill last byte |
---|
338 | } // for |
---|
339 | free( area ); |
---|
340 | } // for |
---|
341 | //sout | "worker" | thisTask() | "successful completion"; |
---|
342 | } // Worker main |
---|
343 | |
---|
344 | int main() { |
---|
345 | const unsigned int NoOfWorkers = 4; |
---|
346 | { |
---|
347 | processor processors[NoOfWorkers - 1] __attribute__(( unused )); // more than one processor |
---|
348 | Worker workers[NoOfWorkers] __attribute__(( unused )); |
---|
349 | } |
---|
350 | // checkFreeOn(); |
---|
351 | // malloc_stats(); |
---|
352 | } |
---|
353 | |
---|
354 | // Local Variables: // |
---|
355 | // tab-width: 4 // |
---|
356 | // compile-command: "cfa -nodebug -O2 heap.cfa" // |
---|
357 | // End: // |
---|