-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.h
56 lines (47 loc) · 1.4 KB
/
macros.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
// SPDX-License-Identifier: MIT
#ifndef IOUCONTEXT_MACROS_H
#define IOUCONTEXT_MACROS_H
#include <errno.h>
#include <error.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
#define __VA_NUM_ARGS__(...) ({ \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
((sizeof((const void*[]){ NULL, ##__VA_ARGS__ })/sizeof(void*))-1); \
_Pragma("GCC diagnostic pop") \
})
#define TRY(f, ...) ({ \
typeof (f(__VA_ARGS__)) _result = f(__VA_ARGS__); \
if (__builtin_expect(_result < 0, 0)) \
error_at_line(EXIT_FAILURE, \
((uintptr_t)_result == -1) ? errno : -(uintptr_t)_result, \
__FILE_NAME__, __LINE__, \
"%s(%s)", #f, #__VA_ARGS__ \
); \
_result; \
})
#define EXPECT(v, f, ...) ({ \
typeof (f(__VA_ARGS__)) _result = f(__VA_ARGS__); \
if (__builtin_expect(_result != v, 0)) \
error_at_line(EXIT_FAILURE, \
0, \
__FILE_NAME__, __LINE__, \
"%s(%s) expected %s", #f, #__VA_ARGS__, #v \
); \
_result; \
})
#define ERRNO(f, ...) ({ \
typeof (f (__VA_ARGS__)) _result = f(__VA_ARGS__); \
_result < 0 ? ( errno = -_result), -1 : _result; \
})
#define LIKELY(e) __builtin_expect(!!(e), true)
#define UNLIKELY(e) __builtin_expect(!!(e), false)
#ifdef __cplusplus
}
#endif
#endif//IOUCONTEXT_MACROS_H