forked from AFD-Illinois/igrmonty2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc.c
121 lines (89 loc) · 2.1 KB
/
malloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "decs.h"
void *malloc_rank1(int n1, int size)
{
void *A;
if((A = malloc(n1*size)) == NULL){
fprintf(stderr,"malloc failure in malloc_rank1\n");
exit(123);
}
return A;
}
void **malloc_rank2(int n1, int n2, int size)
{
void **A;
if ((A = (void **)malloc(n1*sizeof(void *))) == NULL) {
fprintf(stderr, "malloc_rank2 failure\n");
exit(-1);
}
for (int i = 0; i < N1; i++) {
A[i] = malloc_rank1(n2, size);
}
return A;
}
void ***malloc_rank3(int n1, int n2, int n3, int size)
{
void ***A;
if ((A = (void ***)malloc(n1*sizeof(void **))) == NULL) {
fprintf(stderr, "malloc_rank3 failure\n");
exit(-1);
}
for (int i = 0; i < N1; i++) {
A[i] = malloc_rank2(n2, n3, size);
}
return A;
}
void ****malloc_rank4(int n1, int n2, int n3, int n4, int size)
{
void ****A;
if ((A = (void ****)malloc(n1*sizeof(void ***))) == NULL) {
fprintf(stderr, "malloc_rank4 failure\n");
exit(-1);
}
for (int i = 0; i < N1; i++) {
A[i] = malloc_rank3(n2, n3, n4, size);
}
return A;
}
double **malloc_rank2_double(int n1, int n2)
{
double **A;
double *space;
int i;
space = malloc_rank1(n1*n2, sizeof(double));
A = malloc_rank1(n1, sizeof(double *));
for(i=0;i<n1;i++){
A[i] = &(space[n2*i]);
//A[i] = malloc_rank1(n2,sizeof(double *));
}
return A;
}
double ****malloc_rank4_double(int n1, int n2, int n3, int n4)
{
double ****A;
double *space;
int i,j,k;
space = malloc_rank1(n1*n2*n3*n4, sizeof(double));
A = malloc_rank1(n1, sizeof(double *));
for(i=0;i<n1;i++){
A[i] = malloc_rank1(n2,sizeof(double *));
for(j=0;j<n2;j++){
A[i][j] = malloc_rank1(n3,sizeof(double *));
for(k=0;k<n3;k++){
A[i][j][k] = &(space[n4*(k + n3*(j + n2*i))]);
}
}
}
return A;
}
void *****malloc_rank5(int n1, int n2, int n3, int n4, int n5, int size)
{
void *****A;
if ((A = (void *****)malloc(n1*sizeof(void ****))) == NULL) {
fprintf(stderr, "malloc_rank5 failure\n");
exit(-1);
}
for (int i = 0; i < N1; i++) {
A[i] = malloc_rank4(n2, n3, n4, n5, size);
}
return A;
}