53# define MIN(a,b) (( (a) < (b) ) ? (a) : (b))
56# define MAX(a,b) (( (a) > (b) ) ? (a) : (b))
61 void *ptr = malloc(dim1_data_size);
63 if (ptr == NULL && dim1_data_size!=0)
64 fprintf(stderr,
"Error: 'malloc1d' failed to allocate %zu bytes.\n", dim1_data_size);
71 void *ptr = calloc(dim1, data_size);
73 if (ptr == NULL && dim1!=0)
74 fprintf(stderr,
"Error: 'calloc1d' failed to allocate %zu bytes.\n", dim1*data_size);
81 ptr = realloc(ptr, dim1_data_size);
83 if (ptr == NULL && dim1_data_size!=0)
84 fprintf(stderr,
"Error: 'realloc1d' failed to allocate %zu bytes.\n", dim1_data_size);
89void**
malloc2d(
size_t dim1,
size_t dim2,
size_t data_size)
94 stride = dim2*data_size;
95 ptr =
malloc1d(dim1*
sizeof(
void*) + dim1*stride);
96 p2 = (
unsigned char*)(ptr + dim1);
98 ptr[i] = &p2[i*stride];
102void**
calloc2d(
size_t dim1,
size_t dim2,
size_t data_size)
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];
115void**
realloc2d(
void** ptr,
size_t dim1,
size_t dim2,
size_t data_size)
119 stride = dim2*data_size;
120 ptr =
realloc1d(ptr, dim1*
sizeof(
void*) + dim1*stride);
121 p2 = (
unsigned char*)(ptr + dim1);
123 ptr[i] = &p2[i*stride];
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)
133 prev_data =
malloc2d(prev_dim1, prev_dim2, data_size);
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];
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);
151void***
malloc3d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t data_size)
153 size_t i, j, stride1, stride2;
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);
163 ptr[i] = &p2[i*dim2];
166 p2[i*dim2+j] = &p3[i*stride1 + j*stride2];
170void***
calloc3d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t data_size)
172 size_t i, j, stride1, stride2;
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);
182 ptr[i] = &p2[i*dim2];
185 p2[i*dim2+j] = &p3[i*stride1 + j*stride2];
189void***
realloc3d(
void*** ptr,
size_t new_dim1,
size_t new_dim2,
size_t new_dim3,
size_t data_size)
191 size_t i, j, stride1, stride2;
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];
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)
209 size_t i, j, stride1, stride2;
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);
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];
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);
238void****
malloc4d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t data_size)
240 size_t i, j, k, stride1, stride2, stride3;
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);
253 ptr[i] = &p2[i*dim2];
256 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
260 p3[i*dim2*dim3+j*dim3+k] = &p4[i*stride1 + j*stride2 + k*stride3];
264void****
calloc4d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t data_size)
266 size_t i, j, k, stride1, stride2, stride3;
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);
279 ptr[i] = &p2[i*dim2];
282 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
286 p3[i*dim2*dim3+j*dim3+k] = &p4[i*stride1 + j*stride2 + k*stride3];
290void****
realloc4d(
void**** ptr,
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t data_size)
292 size_t i, j, k, stride1, stride2, stride3;
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);
304 ptr[i] = &p2[i*dim2];
307 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
311 p3[i*dim2*dim3+j*dim3+k] = &p4[i*stride1 + j*stride2 + k*stride3];
315void*****
malloc5d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t dim5,
size_t data_size)
317 size_t i, j, k, l, stride1, stride2, stride3, stride4;
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);
333 ptr[i] = &p2[i*dim2];
336 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
340 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
345 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + l] = &p5[i*stride1 + j*stride2 + k*stride3 + l*stride4];
349void*****
calloc5d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t dim5,
size_t data_size)
351 size_t i, j, k, l, stride1, stride2, stride3, stride4;
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);
367 ptr[i] = &p2[i*dim2];
370 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
374 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
379 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + l] = &p5[i*stride1 + j*stride2 + k*stride3 + l*stride4];
383void*****
realloc5d(
void***** ptr,
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t dim5,
size_t data_size)
385 size_t i, j, k, l, stride1, stride2, stride3, stride4;
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);
400 ptr[i] = &p2[i*dim2];
403 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
407 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
412 p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4 + l] = &p5[i*stride1 + j*stride2 + k*stride3 + l*stride4];
416void******
malloc6d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t dim5,
size_t dim6,
size_t data_size)
418 size_t i, j, k, l, p, stride1, stride2, stride3, stride4, stride5;
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);
438 ptr[i] = &p2[i*dim2];
441 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
445 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
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];
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];
460void******
calloc6d(
size_t dim1,
size_t dim2,
size_t dim3,
size_t dim4,
size_t dim5,
size_t dim6,
size_t data_size)
462 size_t i, j, k, l, p, stride1, stride2, stride3, stride4, stride5;
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);
482 ptr[i] = &p2[i*dim2];
485 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
489 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
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];
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];
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)
506 size_t i, j, k, l, p, stride1, stride2, stride3, stride4, stride5;
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);
525 ptr[i] = &p2[i*dim2];
528 p2[i*dim2+j] = &p3[i*dim2*dim3 + j*dim3];
532 p3[i*dim2*dim3 + j*dim3 + k] = &p4[i*dim2*dim3*dim4 + j*dim3*dim4 + k*dim4];
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];
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];
void ** malloc2d(size_t dim1, size_t dim2, size_t data_size)
2-D malloc (contiguously allocated, so use free() as usual to deallocate)
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
void ** realloc2d(void **ptr, size_t dim1, size_t dim2, size_t data_size)
2-D realloc which does NOT retain previous data order
void * malloc1d(size_t dim1_data_size)
1-D malloc (same as malloc, but with error checking)
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)
void * calloc1d(size_t dim1, size_t data_size)
1-D calloc (same as calloc, but with error checking)
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
void * realloc1d(void *ptr, size_t dim1_data_size)
1-D realloc (same as realloc, but with error checking)
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
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)
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
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
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)
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)
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)
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
void ** calloc2d(size_t dim1, size_t dim2, size_t data_size)
2-D calloc (contiguously allocated, so use free() as usual to deallocate)
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)
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)
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)
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...
#define FLATTEN3D(A)
Use this macro when passing a 3-D dynamic multi-dimensional array to memset, memcpy or any other func...