-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.h
210 lines (179 loc) · 8.02 KB
/
helper.h
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/******************************************************************************
* helper.h
*
*
* defines certain useful macros for source code.
*
* The intent of these macros is provide cleaner, safer code that tests for the
* correct functioning of various file and memory operations. While the program
* will in all likelihood crash anyway as result of one of these operations
* failing and this fact being untested, by testing when the operation occurs
* we can eliminate or confirm it as the point of failure.
*/
#ifndef HELPER_H
#define HELPER_H
#include <stdio.h>
#include <stdlib.h>
// File opening macro
// takes
// * file pointer, file
// * file mode, mode
// * parameters to construct the file name, string
// The string _must_ start with a string constant, which can optionally
// include any number of format specifiers and the associated variables
// as you would for a normal printf statement.
//
// Once opened, the file is checked and if it is NULL (i.e. the opening
// failed), the program exits, with a message informing you why.
#define open(file,mode,...) { \
char _str[64]; \
sprintf(_str,__VA_ARGS__); \
file = fopen(_str,mode); \
if (NULL == file) { \
oops(_str); \
} \
}
// Ooops macro
// takes
// * string that this prepended before the system error message, string
// e.g. file_name.dat: No such file or directory when string = file_name.dat
#define oops(string) { perror((string)); exit(EXIT_FAILURE); }
/* ----------- malloc macros ---------- */
// malloc macro
// takes
// * pointer to be allocated, p
// * size to be allocated, s
// * message should there be an error, m
#define MALLOC(p,s,m) {(p) = malloc((s));if(NULL==(p)){oops((m));}}
// double malloc
// takes
// * pointer to be allocated, p
// * number of doubles to be allocated, s
// * message should there be an error, m
#define MALLOC_D(p,s,m) MALLOC(p,(s)*sizeof(double),m)
// float malloc
// takes
// * pointer to be allocated, p
// * number of floats to be allocated, s
// * message should there be an error, m
#define MALLOC_F(p,s,m) MALLOC(p,(s)*sizeof(float),m)
// char malloc
// takes
// * pointer to be allocated, p
// * number of chars to be allocated, s
// * message should there be an error, m
#define MALLOC_C(p,s,m) MALLOC(p,(s)*sizeof(char),m)
// int malloc
// takes
// * pointer to be allocated, p
// * number of ints to be allocated, s
// * message should there be an error, m
#define MALLOC_I(p,s,m) MALLOC(p,(s)*sizeof(int),m)
// double pointer malloc
// takes
// * pointer to be allocated, p
// * number of doubles to be allocated, s
// * message should there be an error, m
#define MALLOC_DP(p,s,m) MALLOC(p,(s)*sizeof(double*),m)
// float pointer malloc
// takes
// * pointer to be allocated, p
// * number of float pointers to be allocated, s
// * message should there be an error, m
#define MALLOC_FP(p,s,m) MALLOC(p,(s)*sizeof(float*),m)
// char pointer malloc
// takes
// * pointer to be allocated, p
// * number of chars to be allocated, s
// * message should there be an error, m
#define MALLOC_CP(p,s,m) MALLOC(p,(s)*sizeof(char*),m)
// int pointer malloc
// takes
// * pointer to be allocated, p
// * number of ints to be allocated, s
// * message should there be an error, m
#define MALLOC_IP(p,s,m) MALLOC(p,(s)*sizeof(int*),m)
// int array 2d
// Allocates to give 2d array accessible through array[j][i] notation but with
// contiguous storage of the variables, allowing for easy output to files and
// transmission via mpi etc.
#define INT_ARRAY_2D(p,rows,cols) { \
int _i_; \
MALLOC_IP(p,(rows),"row alloc failure"); \
MALLOC_I(p[0],(rows)*(cols),"array alloc failure"); \
for (_i_=1;_i_<(rows);++_i_) p[_i_] = p[0] + (_i_*(cols)); \
}
// double array 2d
// Allocates to give 2d array accessible through array[j][i] notation but with
// contiguous storage of the variables, allowing for easy output to files and
// transmission via mpi etc.
#define D_ARRAY_2D(p,rows,cols) { \
int _i_; \
MALLOC_DP(p,(rows),"row alloc failure"); \
MALLOC_D(p[0],(rows)*(cols),"array alloc failure"); \
for (_i_=1;_i_<(rows);++_i_) p[_i_] = p[0] + (_i_*(cols)); \
}
// float array 2d
// Allocates to give 2d array accessible through array[j][i] notation but with
// contiguous storage of the variables, allowing for easy output to files and
// transmission via mpi etc.
#define F_ARRAY_2D(p,rows,cols) { \
int _i_; \
MALLOC_FP(p,(rows),"row alloc failure"); \
MALLOC_F(p[0],(rows)*(cols),"array alloc failure"); \
for (_i_=1;_i_<(rows);++_i_) p[_i_] = p[0] + (_i_*(cols)); \
}
// char array 2d
// Allocates to give 2d array accessible through array[j][i] notation but with
// contiguous storage of the variables, allowing for easy output to files and
// transmission via mpi etc.
#define UC_ARRAY_2D(p,rows,cols) { \
int _i_; \
MALLOC(p,(rows)*sizeof(unsigned char*),"row alloc failure"); \
MALLOC(p[0],(rows)*(cols)*sizeof(unsigned char),"array alloc failure"); \
for (_i_=1;_i_<(rows);++_i_) p[_i_] = p[0] + (_i_*(cols)); \
}
// double pointer array 2d
// Allocates to give 2d array accessible through array[j][i] notation but with
// contiguous storage of the variables, allowing for easy output to files and
// transmission via mpi etc.
#define DP_ARRAY_2D(p,rows,cols) { \
int _i_; \
MALLOC(p,(rows)*sizeof(double**),"row alloc failure"); \
MALLOC(p[0],(rows)*(cols)*sizeof(double*),"array alloc failure"); \
for (_i_=1;_i_<(rows);++_i_) p[_i_] = p[0] + (_i_*(cols)); \
}
// free 2D
// frees data allocated in the above pattern, first closing the array[0]
// pointer, then the array pointer
#define FREE_2D(array) { \
free( ## array ## [0]); \
free( ## array ## ); \
}
/* ---------- fread macros ---------- */
// general READ macro
// takes
// * pointer to read into, pointer
// * size of variable to read, size
// * number to read, count
// * file to read from, file
// * message for failure, message.
// attempts to read in the total number specified. If it fails to read in
// this many, displays error message to stderr and exits program.
#define READ(pointer,size,count,file,message) { \
int _read_ = fread((pointer),(size),(count),(file)); \
if ((count) != _read_) { \
fprintf(stderr,"%d: %s (%d of %d read)\n",__LINE__,(message),_read_,(count)); \
fflush(stderr); \
exit(EXIT_FAILURE); \
} \
}
// reads in doubles from a file, using the above macro
#define READ_D(pointer,count,file,message) READ(pointer,sizeof(double),count,file,message)
// reads in ints from a file, using the above macro
#define READ_I(pointer,count,file,message) READ(pointer,sizeof(int),count,file,message)
// reads in doubles from a file, using the above macro
#define READ_C(pointer,count,file,message) READ(pointer,sizeof(char),count,file,message)
// reads in unsigned chars
#define READ_UC(pointer,count,file,message) READ(pointer,sizeof(unsigned char),count,file,message)
#endif //end inclusion guard