SAF
Loading...
Searching...
No Matches
md_malloc.c
Go to the documentation of this file.
1/*
2 Copyright (c) 2019 Leo McCormack
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
45#include <stdio.h>
46#include <math.h>
47#include <stdlib.h>
48#include <string.h>
49#include <assert.h>
50#include "md_malloc.h"
51
52#ifndef MIN
53# define MIN(a,b) (( (a) < (b) ) ? (a) : (b))
54#endif
55#ifndef MAX
56# define MAX(a,b) (( (a) > (b) ) ? (a) : (b))
57#endif
58
59void* malloc1d(size_t dim1_data_size)
60{
61 void *ptr = malloc(dim1_data_size);
62#if !defined(NDEBUG)
63 if (ptr == NULL && dim1_data_size!=0)
64 fprintf(stderr, "Error: 'malloc1d' failed to allocate %zu bytes.\n", dim1_data_size);
65#endif
66 return ptr;
67}
68
69void* calloc1d(size_t dim1, size_t data_size)
70{
71 void *ptr = calloc(dim1, data_size);
72#if !defined(NDEBUG)
73 if (ptr == NULL && dim1!=0)
74 fprintf(stderr, "Error: 'calloc1d' failed to allocate %zu bytes.\n", dim1*data_size);
75#endif
76 return ptr;
77}
78
79void* realloc1d(void* ptr, size_t dim1_data_size)
80{
81 ptr = realloc(ptr, dim1_data_size);
82#if !defined(NDEBUG)
83 if (ptr == NULL && dim1_data_size!=0)
84 fprintf(stderr, "Error: 'realloc1d' failed to allocate %zu bytes.\n", dim1_data_size);
85#endif
86 return ptr;
87}
88
89void** malloc2d(size_t dim1, size_t dim2, size_t data_size)
90{
91 size_t i, stride;
92 void** ptr;
93 unsigned char* p2;
94 stride = dim2*data_size;
95 ptr = malloc1d(dim1*sizeof(void*) + dim1*stride);
96 p2 = (unsigned char*)(ptr + dim1);
97 for(i=0; i<dim1; i++)
98 ptr[i] = &p2[i*stride];
99 return ptr;
100}
101
102void** calloc2d(size_t dim1, size_t dim2, size_t data_size)
103{
104 size_t i, stride;
105 void** ptr;
106 unsigned char* p2;
107 stride = dim2*data_size;
108 ptr = calloc1d(dim1, sizeof(void*) + stride);
109 p2 = (unsigned char*)(ptr + dim1);
110 for(i=0; i<dim1; i++)
111 ptr[i] = &p2[i*stride];
112 return ptr;
113}
114
115void** realloc2d(void** ptr, size_t dim1, size_t dim2, size_t data_size)
116{
117 size_t i, stride;
118 unsigned char* p2;
119 stride = dim2*data_size;
120 ptr = realloc1d(ptr, dim1*sizeof(void*) + dim1*stride);
121 p2 = (unsigned char*)(ptr + dim1);
122 for(i=0;i<dim1;i++)
123 ptr[i] = &p2[i*stride];
124 return ptr;
125}
126
127void** realloc2d_r(void** ptr, size_t new_dim1, size_t new_dim2, size_t prev_dim1, size_t prev_dim2, size_t data_size)
128{
129 size_t i, stride;
130 void** prev_data;
131
132 /* Copy previous data */
133 prev_data = malloc2d(prev_dim1, prev_dim2, data_size);
134 memcpy(FLATTEN2D(prev_data), FLATTEN2D(ptr), prev_dim1*prev_dim2*data_size);
135
136 /* Resize */
137 unsigned char* p2;
138 stride = new_dim2*data_size;
139 ptr = realloc1d(ptr, new_dim1*sizeof(void*) + new_dim1*stride);
140 p2 = (unsigned char*)(ptr + new_dim1);
141 for(i=0;i<new_dim1;i++)
142 ptr[i] = &p2[i*stride];
143
144 /* Repopulate */
145 for(i=0; i<MIN(new_dim1,prev_dim1); i++)
146 memcpy(ptr[i], prev_data[i], MIN(new_dim2,prev_dim2)*data_size);
147 free(prev_data);
148 return ptr;
149}
150
151void*** malloc3d(size_t dim1, size_t dim2, size_t dim3, size_t data_size)
152{
153 size_t i, j, stride1, stride2;
154 void*** ptr;
155 void** p2;
156 unsigned char* p3;
157 stride1 = dim2*dim3*data_size;
158 stride2 = dim3*data_size;
159 ptr = malloc1d(dim1*sizeof(void**) + dim1*dim2*sizeof(void*) + dim1*stride1);
160 p2 = (void**)(ptr + dim1);
161 p3 = (unsigned char*)(p2 + dim1*dim2);
162 for(i=0;i<dim1;i++)
163 ptr[i] = &p2[i*dim2];
164 for(i=0;i<dim1;i++)
165 for(j=0;j<dim2;j++)
166 p2[i*dim2+j] = &p3[i*stride1 + j*stride2];
167 return ptr;
168}
169
170void*** calloc3d(size_t dim1, size_t dim2, size_t dim3, size_t data_size)
171{
172 size_t i, j, stride1, stride2;
173 void*** ptr;
174 void** p2;
175 unsigned char* p3;
176 stride1 = dim2*dim3*data_size;
177 stride2 = dim3*data_size;
178 ptr = calloc1d(dim1, sizeof(void**) + dim2*sizeof(void*) + stride1);
179 p2 = (void**)(ptr + dim1);
180 p3 = (unsigned char*)(p2 + dim1*dim2);
181 for(i=0;i<dim1;i++)
182 ptr[i] = &p2[i*dim2];
183 for(i=0;i<dim1;i++)
184 for(j=0;j<dim2;j++)
185 p2[i*dim2+j] = &p3[i*stride1 + j*stride2];
186 return ptr;
187}
188
189void*** realloc3d(void*** ptr, size_t new_dim1, size_t new_dim2, size_t new_dim3, size_t data_size)
190{
191 size_t i, j, stride1, stride2;
192 void** p2;
193 unsigned char* p3;
194 stride1 = new_dim2*new_dim3*data_size;
195 stride2 = new_dim3*data_size;
196 ptr = realloc1d(ptr, new_dim1*sizeof(void**) + new_dim1*new_dim2*sizeof(void*) + new_dim1*stride1);
197 p2 = (void**)(ptr + new_dim1);
198 p3 = (unsigned char*)(p2 + new_dim1*new_dim2);
199 for(i=0;i<new_dim1;i++)
200 ptr[i] = &p2[i*new_dim2];
201 for(i=0;i<new_dim1;i++)
202 for(j=0;j<new_dim2;j++)
203 p2[i*new_dim2+j] = &p3[i*stride1 + j*stride2];
204 return ptr;
205}
206
207void*** realloc3d_r(void*** ptr, size_t new_dim1, size_t new_dim2, size_t new_dim3, size_t prev_dim1, size_t prev_dim2, size_t prev_dim3, size_t data_size)
208{
209 size_t i, j, stride1, stride2;
210 void** p2;
211 unsigned char* p3;
212 void*** prev_data;
213
214 /* Copy previous data */
215 prev_data = malloc3d(prev_dim1, prev_dim2, prev_dim3, data_size);
216 memcpy(FLATTEN3D(prev_data), FLATTEN3D(ptr), prev_dim1*prev_dim2*prev_dim3*data_size);
217
218 /* Resize */
219 stride1 = new_dim2*new_dim3*data_size;
220 stride2 = new_dim3*data_size;
221 ptr = realloc1d(ptr, new_dim1*sizeof(void**) + new_dim1*new_dim2*sizeof(void*) + new_dim1*stride1);
222 p2 = (void**)(ptr + new_dim1);
223 p3 = (unsigned char*)(p2 + new_dim1*new_dim2);
224 for(i=0;i<new_dim1;i++)
225 ptr[i] = &p2[i*new_dim2];
226 for(i=0;i<new_dim1;i++)
227 for(j=0;j<new_dim2;j++)
228 p2[i*new_dim2+j] = &p3[i*stride1 + j*stride2];
229
230 /* Repopulate */
231 for(i=0; i<MIN(new_dim1,prev_dim1); i++)
232 for(j=0; j<MIN(new_dim2,prev_dim2); j++)
233 memcpy(ptr[i][j], prev_data[i][j], MIN(new_dim3,prev_dim3)*data_size);
234 free(prev_data);
235 return ptr;
236}
237
238void**** malloc4d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t data_size)
239{
240 size_t i, j, k, stride1, stride2, stride3;
241 void**** ptr;
242 void*** p2;
243 void** p3;
244 unsigned char* p4;
245 stride1 = dim2*dim3*dim4*data_size;
246 stride2 = dim3*dim4*data_size;
247 stride3 = dim4*data_size;
248 ptr = malloc1d(dim1*sizeof(void***) + dim1*dim2*sizeof(void**) + dim1*dim2*dim3*sizeof(void*) + dim1*stride1);
249 p2 = (void***)(ptr + dim1);
250 p3 = (void**)(p2 + dim1*dim2);
251 p4 = (unsigned char*)(p3 + dim1*dim2*dim3);
252 for(i=0;i<dim1;i++)
253 ptr[i] = &p2[i*dim2];
254 for(i=0;i<dim1;i++)
255 for(j=0;j<dim2;j++)
256 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
257 for(i=0;i<dim1;i++)
258 for(j=0;j<dim2;j++)
259 for(k=0;k<dim3;k++)
260 p3[i*dim2*dim3+j*dim3+k] = &p4[i*stride1 + j*stride2 + k*stride3];
261 return ptr;
262}
263
264void**** calloc4d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t data_size)
265{
266 size_t i, j, k, stride1, stride2, stride3;
267 void**** ptr;
268 void*** p2;
269 void** p3;
270 unsigned char* p4;
271 stride1 = dim2*dim3*dim4*data_size;
272 stride2 = dim3*dim4*data_size;
273 stride3 = dim4*data_size;
274 ptr = calloc1d(dim1, sizeof(void***) + dim2*sizeof(void**) + dim2*dim3*sizeof(void*) + stride1);
275 p2 = (void***)(ptr + dim1);
276 p3 = (void**)(p2 + dim1*dim2);
277 p4 = (unsigned char*)(p3 + dim1*dim2*dim3);
278 for(i=0;i<dim1;i++)
279 ptr[i] = &p2[i*dim2];
280 for(i=0;i<dim1;i++)
281 for(j=0;j<dim2;j++)
282 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
283 for(i=0;i<dim1;i++)
284 for(j=0;j<dim2;j++)
285 for(k=0;k<dim3;k++)
286 p3[i*dim2*dim3+j*dim3+k] = &p4[i*stride1 + j*stride2 + k*stride3];
287 return ptr;
288}
289
290void**** realloc4d(void**** ptr, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t data_size)
291{
292 size_t i, j, k, stride1, stride2, stride3;
293 void*** p2;
294 void** p3;
295 unsigned char* p4;
296 stride1 = dim2*dim3*dim4*data_size;
297 stride2 = dim3*dim4*data_size;
298 stride3 = dim4*data_size;
299 ptr = realloc1d(ptr, dim1*sizeof(void***) + dim1*dim2*sizeof(void**) + dim1*dim2*dim3*sizeof(void*) + dim1*stride1);
300 p2 = (void***)(ptr + dim1);
301 p3 = (void**)(p2 + dim1*dim2);
302 p4 = (unsigned char*)(p3 + dim1*dim2*dim3);
303 for(i=0;i<dim1;i++)
304 ptr[i] = &p2[i*dim2];
305 for(i=0;i<dim1;i++)
306 for(j=0;j<dim2;j++)
307 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
308 for(i=0;i<dim1;i++)
309 for(j=0;j<dim2;j++)
310 for(k=0;k<dim3;k++)
311 p3[i*dim2*dim3+j*dim3+k] = &p4[i*stride1 + j*stride2 + k*stride3];
312 return ptr;
313}
314
315void***** malloc5d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t data_size)
316{
317 size_t i, j, k, l, stride1, stride2, stride3, stride4;
318 void***** ptr;
319 void**** p2;
320 void*** p3;
321 void** p4;
322 unsigned char* p5;
323 stride1 = dim2*dim3*dim4*dim5*data_size;
324 stride2 = dim3*dim4*dim5*data_size;
325 stride3 = dim4*dim5*data_size;
326 stride4 = dim5*data_size;
327 ptr = malloc1d(dim1*sizeof(void****) + dim1*dim2*sizeof(void***) + dim1*dim2*dim3*sizeof(void**) + dim1*dim2*dim3*dim4*sizeof(void*) + dim1*stride1);
328 p2 = (void****)(ptr + dim1);
329 p3 = (void***)(p2 + dim1*dim2);
330 p4 = (void**)(p3 + dim1*dim2*dim3);
331 p5 = (unsigned char*)(p4 + dim1*dim2*dim3*dim4);
332 for(i=0;i<dim1;i++)
333 ptr[i] = &p2[i*dim2];
334 for(i=0;i<dim1;i++)
335 for(j=0;j<dim2;j++)
336 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
337 for(i=0;i<dim1;i++)
338 for(j=0;j<dim2;j++)
339 for(k=0;k<dim3;k++)
340 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
341 for(i=0;i<dim1;i++)
342 for(j=0;j<dim2;j++)
343 for(k=0;k<dim3;k++)
344 for(l=0;l<dim4;l++)
345 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + l] = &p5[i*stride1 + j*stride2 + k*stride3 + l*stride4];
346 return ptr;
347}
348
349void***** calloc5d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t data_size)
350{
351 size_t i, j, k, l, stride1, stride2, stride3, stride4;
352 void***** ptr;
353 void**** p2;
354 void*** p3;
355 void** p4;
356 unsigned char* p5;
357 stride1 = dim2*dim3*dim4*dim5*data_size;
358 stride2 = dim3*dim4*dim5*data_size;
359 stride3 = dim4*dim5*data_size;
360 stride4 = dim5*data_size;
361 ptr = calloc1d(dim1, sizeof(void****) + dim2*sizeof(void***) + dim2*dim3*sizeof(void**) + dim2*dim3*dim4*sizeof(void*) + stride1);
362 p2 = (void****)(ptr + dim1);
363 p3 = (void***)(p2 + dim1*dim2);
364 p4 = (void**)(p3 + dim1*dim2*dim3);
365 p5 = (unsigned char*)(p4 + dim1*dim2*dim3*dim4);
366 for(i=0;i<dim1;i++)
367 ptr[i] = &p2[i*dim2];
368 for(i=0;i<dim1;i++)
369 for(j=0;j<dim2;j++)
370 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
371 for(i=0;i<dim1;i++)
372 for(j=0;j<dim2;j++)
373 for(k=0;k<dim3;k++)
374 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
375 for(i=0;i<dim1;i++)
376 for(j=0;j<dim2;j++)
377 for(k=0;k<dim3;k++)
378 for(l=0;l<dim4;l++)
379 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + l] = &p5[i*stride1 + j*stride2 + k*stride3 + l*stride4];
380 return ptr;
381}
382
383void***** realloc5d(void***** ptr, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t data_size)
384{
385 size_t i, j, k, l, stride1, stride2, stride3, stride4;
386 void**** p2;
387 void*** p3;
388 void** p4;
389 unsigned char* p5;
390 stride1 = dim2*dim3*dim4*dim5*data_size;
391 stride2 = dim3*dim4*dim5*data_size;
392 stride3 = dim4*dim5*data_size;
393 stride4 = dim5*data_size;
394 ptr = realloc1d(ptr, dim1*sizeof(void****) + dim1*dim2*sizeof(void***) + dim1*dim2*dim3*sizeof(void**) + dim1*dim2*dim3*dim4*sizeof(void*) + dim1*stride1);
395 p2 = (void****)(ptr + dim1);
396 p3 = (void***)(p2 + dim1*dim2);
397 p4 = (void**)(p3 + dim1*dim2*dim3);
398 p5 = (unsigned char*)(p4 + dim1*dim2*dim3*dim4);
399 for(i=0;i<dim1;i++)
400 ptr[i] = &p2[i*dim2];
401 for(i=0;i<dim1;i++)
402 for(j=0;j<dim2;j++)
403 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
404 for(i=0;i<dim1;i++)
405 for(j=0;j<dim2;j++)
406 for(k=0;k<dim3;k++)
407 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
408 for(i=0;i<dim1;i++)
409 for(j=0;j<dim2;j++)
410 for(k=0;k<dim3;k++)
411 for(l=0;l<dim4;l++)
412 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + l] = &p5[i*stride1 + j*stride2 + k*stride3 + l*stride4];
413 return ptr;
414}
415
416void****** malloc6d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, size_t data_size)
417{
418 size_t i, j, k, l, p, stride1, stride2, stride3, stride4, stride5;
419 void****** ptr;
420 void***** p2;
421 void**** p3;
422 void*** p4;
423 void** p5;
424 unsigned char* p6;
425 stride1 = dim2*dim3*dim4*dim5*dim6*data_size;
426 stride2 = dim3*dim4*dim5*dim6*data_size;
427 stride3 = dim4*dim5*dim6*data_size;
428 stride4 = dim5*dim6*data_size;
429 stride5 = dim6*data_size;
430 ptr = malloc1d(dim1*sizeof(void*****) + dim1*dim2*sizeof(void****) + dim1*dim2*dim3*sizeof(void***) +
431 dim1*dim2*dim3*dim4*sizeof(void**) + dim1*dim2*dim3*dim4*dim5*sizeof(void*) + dim1*stride1);
432 p2 = (void*****)(ptr + dim1);
433 p3 = (void****)(p2 + dim1*dim2);
434 p4 = (void***)(p3 + dim1*dim2*dim3);
435 p5 = (void**)(p4 + dim1*dim2*dim3*dim4);
436 p6 = (unsigned char*)(p5 + dim1*dim2*dim3*dim4*dim5);
437 for(i=0;i<dim1;i++)
438 ptr[i] = &p2[i*dim2];
439 for(i=0;i<dim1;i++)
440 for(j=0;j<dim2;j++)
441 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
442 for(i=0;i<dim1;i++)
443 for(j=0;j<dim2;j++)
444 for(k=0;k<dim3;k++)
445 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
446 for(i=0;i<dim1;i++)
447 for(j=0;j<dim2;j++)
448 for(k=0;k<dim3;k++)
449 for(p=0;p<dim4;p++)
450 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + p] = &p5[i*dim2*dim3*dim4*dim5 + j*dim3*dim4*dim5 + k*dim4*dim5 + p*dim5];
451 for(i=0;i<dim1;i++)
452 for(j=0;j<dim2;j++)
453 for(k=0;k<dim3;k++)
454 for(l=0;l<dim4;l++)
455 for(p=0;p<dim5;p++)
456 p5[i*dim2*dim3*dim4*dim5 + j*dim3*dim4*dim5 + k*dim4*dim5 + l*dim5 + p] = &p6[i*stride1 + j*stride2 + k*stride3 + l*stride4 + p*stride5];
457 return ptr;
458}
459
460void****** calloc6d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, size_t data_size)
461{
462 size_t i, j, k, l, p, stride1, stride2, stride3, stride4, stride5;
463 void****** ptr;
464 void***** p2;
465 void**** p3;
466 void*** p4;
467 void** p5;
468 unsigned char* p6;
469 stride1 = dim2*dim3*dim4*dim5*dim6*data_size;
470 stride2 = dim3*dim4*dim5*dim6*data_size;
471 stride3 = dim4*dim5*dim6*data_size;
472 stride4 = dim5*dim6*data_size;
473 stride5 = dim6*data_size;
474 ptr = calloc1d(dim1, sizeof(void*****) + dim2*sizeof(void****) + dim2*dim3*sizeof(void***) +
475 dim2*dim3*dim4*sizeof(void**) + dim2*dim3*dim4*dim5*sizeof(void*) + stride1);
476 p2 = (void*****)(ptr + dim1);
477 p3 = (void****)(p2 + dim1*dim2);
478 p4 = (void***)(p3 + dim1*dim2*dim3);
479 p5 = (void**)(p4 + dim1*dim2*dim3*dim4);
480 p6 = (unsigned char*)(p5 + dim1*dim2*dim3*dim4*dim5);
481 for(i=0;i<dim1;i++)
482 ptr[i] = &p2[i*dim2];
483 for(i=0;i<dim1;i++)
484 for(j=0;j<dim2;j++)
485 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
486 for(i=0;i<dim1;i++)
487 for(j=0;j<dim2;j++)
488 for(k=0;k<dim3;k++)
489 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
490 for(i=0;i<dim1;i++)
491 for(j=0;j<dim2;j++)
492 for(k=0;k<dim3;k++)
493 for(p=0;p<dim4;p++)
494 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + p] = &p5[i*dim2*dim3*dim4*dim5 + j*dim3*dim4*dim5 + k*dim4*dim5 + p*dim5];
495 for(i=0;i<dim1;i++)
496 for(j=0;j<dim2;j++)
497 for(k=0;k<dim3;k++)
498 for(l=0;l<dim4;l++)
499 for(p=0;p<dim5;p++)
500 p5[i*dim2*dim3*dim4*dim5 + j*dim3*dim4*dim5 + k*dim4*dim5 + l*dim5 + p] = &p6[i*stride1 + j*stride2 + k*stride3 + l*stride4 + p*stride5];
501 return ptr;
502}
503
504void****** realloc6d(void****** ptr, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, size_t data_size)
505{
506 size_t i, j, k, l, p, stride1, stride2, stride3, stride4, stride5;
507 void***** p2;
508 void**** p3;
509 void*** p4;
510 void** p5;
511 unsigned char* p6;
512 stride1 = dim2*dim3*dim4*dim5*dim6*data_size;
513 stride2 = dim3*dim4*dim5*dim6*data_size;
514 stride3 = dim4*dim5*dim6*data_size;
515 stride4 = dim5*dim6*data_size;
516 stride5 = dim6*data_size;
517 ptr = realloc1d(ptr, dim1*sizeof(void*****) + dim1*dim2*sizeof(void****) + dim1*dim2*dim3*sizeof(void***) +
518 dim1*dim2*dim3*dim4*sizeof(void**) + dim1*dim2*dim3*dim4*dim5*sizeof(void*) + dim1*stride1);
519 p2 = (void*****)(ptr + dim1);
520 p3 = (void****)(p2 + dim1*dim2);
521 p4 = (void***)(p3 + dim1*dim2*dim3);
522 p5 = (void**)(p4 + dim1*dim2*dim3*dim4);
523 p6 = (unsigned char*)(p5 + dim1*dim2*dim3*dim4*dim5);
524 for(i=0;i<dim1;i++)
525 ptr[i] = &p2[i*dim2];
526 for(i=0;i<dim1;i++)
527 for(j=0;j<dim2;j++)
528 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
529 for(i=0;i<dim1;i++)
530 for(j=0;j<dim2;j++)
531 for(k=0;k<dim3;k++)
532 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
533 for(i=0;i<dim1;i++)
534 for(j=0;j<dim2;j++)
535 for(k=0;k<dim3;k++)
536 for(p=0;p<dim4;p++)
537 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + p] = &p5[i*dim2*dim3*dim4*dim5 + j*dim3*dim4*dim5 + k*dim4*dim5 + p*dim5];
538 for(i=0;i<dim1;i++)
539 for(j=0;j<dim2;j++)
540 for(k=0;k<dim3;k++)
541 for(l=0;l<dim4;l++)
542 for(p=0;p<dim5;p++)
543 p5[i*dim2*dim3*dim4*dim5 + j*dim3*dim4*dim5 + k*dim4*dim5 + l*dim5 + p] = &p6[i*stride1 + j*stride2 + k*stride3 + l*stride4 + p*stride5];
544 return ptr;
545}
void ** malloc2d(size_t dim1, size_t dim2, size_t data_size)
2-D malloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:89
void **** realloc4d(void ****ptr, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t data_size)
4-D realloc which does NOT retain previous data order
Definition md_malloc.c:290
void ** realloc2d(void **ptr, size_t dim1, size_t dim2, size_t data_size)
2-D realloc which does NOT retain previous data order
Definition md_malloc.c:115
void * malloc1d(size_t dim1_data_size)
1-D malloc (same as malloc, but with error checking)
Definition md_malloc.c:59
void ****** malloc6d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, size_t data_size)
6-D malloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:416
void * calloc1d(size_t dim1, size_t data_size)
1-D calloc (same as calloc, but with error checking)
Definition md_malloc.c:69
void ****** realloc6d(void ******ptr, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, size_t data_size)
6-D realloc which does NOT retain previous data order
Definition md_malloc.c:504
void * realloc1d(void *ptr, size_t dim1_data_size)
1-D realloc (same as realloc, but with error checking)
Definition md_malloc.c:79
void ***** realloc5d(void *****ptr, size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t data_size)
5-D realloc which does NOT retain previous data order
Definition md_malloc.c:383
void *** malloc3d(size_t dim1, size_t dim2, size_t dim3, size_t data_size)
3-D malloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:151
void *** realloc3d(void ***ptr, size_t new_dim1, size_t new_dim2, size_t new_dim3, size_t data_size)
3-D realloc which does NOT retain previous data order
Definition md_malloc.c:189
void *** realloc3d_r(void ***ptr, size_t new_dim1, size_t new_dim2, size_t new_dim3, size_t prev_dim1, size_t prev_dim2, size_t prev_dim3, size_t data_size)
3-D realloc which does retain previous data order
Definition md_malloc.c:207
void **** malloc4d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t data_size)
4-D malloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:238
void **** calloc4d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t data_size)
4-D calloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:264
void ***** calloc5d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t data_size)
5-D calloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:349
void ** realloc2d_r(void **ptr, size_t new_dim1, size_t new_dim2, size_t prev_dim1, size_t prev_dim2, size_t data_size)
2-D realloc which does retain previous data order
Definition md_malloc.c:127
void ** calloc2d(size_t dim1, size_t dim2, size_t data_size)
2-D calloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:102
void ***** malloc5d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t data_size)
5-D malloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:315
void *** calloc3d(size_t dim1, size_t dim2, size_t dim3, size_t data_size)
3-D calloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:170
void ****** calloc6d(size_t dim1, size_t dim2, size_t dim3, size_t dim4, size_t dim5, size_t dim6, size_t data_size)
6-D malloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:460
Contiguous memory allocation functions for multi-dimensional arrays.
#define FLATTEN2D(A)
Use this macro when passing a 2-D dynamic multi-dimensional array to memset, memcpy or any other func...
Definition md_malloc.h:65
#define FLATTEN3D(A)
Use this macro when passing a 3-D dynamic multi-dimensional array to memset, memcpy or any other func...
Definition md_malloc.h:72