1 /* ---- memory allocation ---- */
2 #include "alloc.h"
3
4
5 #define alloc_error_check(p) { \
6 if ((p) == NULL) { \
7 fprintf(stderr, "Allocation Failure!\n"); \
8 exit(1); \
9 } \
10 }
11
12
alloc_1d_int(int n1)13 int *alloc_1d_int(int n1)
14 {
15 int *i;
16
17 i = (int *) malloc(sizeof(int) * n1);
18 alloc_error_check(i);
19 return i;
20 }
21
22
free_1d_int(int * i)23 void free_1d_int(int *i)
24 {
25 free(i);
26 }
27
28
alloc_1d_double(int n1)29 double *alloc_1d_double(int n1)
30 {
31 double *d;
32
33 d = (double *) malloc(sizeof(double) * n1);
34 alloc_error_check(d);
35 return d;
36 }
37
38
free_1d_double(double * d)39 void free_1d_double(double *d)
40 {
41 free(d);
42 }
43
44
alloc_2d_int(int n1,int n2)45 int **alloc_2d_int(int n1, int n2)
46 {
47 int **ii, *i;
48 int j;
49
50 ii = (int **) malloc(sizeof(int *) * n1);
51 alloc_error_check(ii);
52 i = (int *) malloc(sizeof(int) * n1 * n2);
53 alloc_error_check(i);
54 ii[0] = i;
55 for (j = 1; j < n1; j++) {
56 ii[j] = ii[j - 1] + n2;
57 }
58 return ii;
59 }
60
61
free_2d_int(int ** ii)62 void free_2d_int(int **ii)
63 {
64 free(ii[0]);
65 free(ii);
66 }
67
68
alloc_2d_double(int n1,int n2)69 double **alloc_2d_double(int n1, int n2)
70 {
71 double **dd, *d;
72 int j;
73
74 dd = (double **) malloc(sizeof(double *) * n1);
75 alloc_error_check(dd);
76 d = (double *) malloc(sizeof(double) * n1 * n2);
77 alloc_error_check(d);
78 dd[0] = d;
79 for (j = 1; j < n1; j++) {
80 dd[j] = dd[j - 1] + n2;
81 }
82 return dd;
83 }
84
85
free_2d_double(double ** dd)86 void free_2d_double(double **dd)
87 {
88 free(dd[0]);
89 free(dd);
90 }
91
92
alloc_3d_int(int n1,int n2,int n3)93 int ***alloc_3d_int(int n1, int n2, int n3)
94 {
95 int ***iii, **ii, *i;
96 int j;
97
98 iii = (int ***) malloc(sizeof(int **) * n1);
99 alloc_error_check(iii);
100 ii = (int **) malloc(sizeof(int *) * n1 * n2);
101 alloc_error_check(ii);
102 iii[0] = ii;
103 for (j = 1; j < n1; j++) {
104 iii[j] = iii[j - 1] + n2;
105 }
106 i = (int *) malloc(sizeof(int) * n1 * n2 * n3);
107 alloc_error_check(i);
108 ii[0] = i;
109 for (j = 1; j < n1 * n2; j++) {
110 ii[j] = ii[j - 1] + n3;
111 }
112 return iii;
113 }
114
115
free_3d_int(int *** iii)116 void free_3d_int(int ***iii)
117 {
118 free(iii[0][0]);
119 free(iii[0]);
120 free(iii);
121 }
122
123
alloc_3d_double(int n1,int n2,int n3)124 double ***alloc_3d_double(int n1, int n2, int n3)
125 {
126 double ***ddd, **dd, *d;
127 int j;
128
129 ddd = (double ***) malloc(sizeof(double **) * n1);
130 alloc_error_check(ddd);
131 dd = (double **) malloc(sizeof(double *) * n1 * n2);
132 alloc_error_check(dd);
133 ddd[0] = dd;
134 for (j = 1; j < n1; j++) {
135 ddd[j] = ddd[j - 1] + n2;
136 }
137 d = (double *) malloc(sizeof(double) * n1 * n2 * n3);
138 alloc_error_check(d);
139 dd[0] = d;
140 for (j = 1; j < n1 * n2; j++) {
141 dd[j] = dd[j - 1] + n3;
142 }
143 return ddd;
144 }
145
146
free_3d_double(double *** ddd)147 void free_3d_double(double ***ddd)
148 {
149 free(ddd[0][0]);
150 free(ddd[0]);
151 free(ddd);
152 }
153
154