-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
46 lines (40 loc) · 1.56 KB
/
common.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
#ifndef COMMON_H
#define COMMON_H
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define ERROR(cleanup, ret, show_errstr, msgs...) \
({ \
const char* _errstr = (show_errstr) ? strerror(errno) : ""; \
(cleanup); \
fprintf(stderr, "[<%s> @ %s: %d]: ", __FUNCTION__, __FILE__, __LINE__); \
fprintf(stderr, ##msgs); \
fprintf(stderr, "%s\n", _errstr); \
return (ret); \
})
#ifdef NDEBUG
#define DO_WITH_ASSERT(statement, expection) \
(statement)
#else
#define DO_WITH_ASSERT(statement, expection) \
({ \
auto _ret_ = (statement); \
assert(expection); \
})
#endif
#define DIV_CEIL(n, p) \
({ \
assert((n) >= 0); \
assert((p) > 0); \
((n) == 0 ? 0 : ((n) - 1) / (p) + 1); \
})
#define DIV_FLOOR(n, p) \
({ \
assert((n) >= 0); \
assert((p) > 0); \
(n) / (p); \
})
#endif