/src/open5gs/lib/core/ogs-macros.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com> |
3 | | * |
4 | | * This file is part of Open5GS. |
5 | | * |
6 | | * This program is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Affero General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #if !defined(OGS_CORE_INSIDE) && !defined(OGS_CORE_COMPILATION) |
21 | | #error "This header cannot be included directly." |
22 | | #endif |
23 | | |
24 | | #ifndef OGS_MACROS_H |
25 | | #define OGS_MACROS_H |
26 | | |
27 | | #ifdef __cplusplus |
28 | | extern "C" { |
29 | | #endif |
30 | | |
31 | | #ifdef __GNUC__ |
32 | | #define OGS_GNUC_CHECK_VERSION(major, minor) \ |
33 | | ((__GNUC__ > (major)) || \ |
34 | | ((__GNUC__ == (major)) && (__GNUC_MINOR__ >= (minor)))) |
35 | | #else |
36 | | #define OGS_GNUC_CHECK_VERSION(major, minor) 0 |
37 | | #endif |
38 | | |
39 | | #if defined(_MSC_VER) |
40 | | #define ogs_inline __inline |
41 | | #else |
42 | | #define ogs_inline __inline__ |
43 | | #endif |
44 | | |
45 | | #if defined(_WIN32) |
46 | | #define OGS_FUNC __FUNCTION__ |
47 | | #elif defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L |
48 | | #define OGS_FUNC __FUNCTION__ |
49 | | #else |
50 | 202k | #define OGS_FUNC __func__ |
51 | | #endif |
52 | | |
53 | | #if defined(__GNUC__) |
54 | 2.32M | #define ogs_likely(x) __builtin_expect (!!(x), 1) |
55 | 324k | #define ogs_unlikely(x) __builtin_expect (!!(x), 0) |
56 | | #else |
57 | | #define ogs_likely(v) v |
58 | | #define ogs_unlikely(v) v |
59 | | #endif |
60 | | |
61 | | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) |
62 | | #if !defined (__clang__) && OGS_GNUC_CHECK_VERSION (4, 4) |
63 | | #define OGS_GNUC_PRINTF(f, v) __attribute__ ((format (gnu_printf, f, v))) |
64 | | #else |
65 | | #define OGS_GNUC_PRINTF(f, v) __attribute__ ((format (__printf__, f, v))) |
66 | | #endif |
67 | | #define OGS_GNUC_NORETURN __attribute__((__noreturn__)) |
68 | | #else |
69 | | #define OGS_GNUC_PRINTF(f, v) |
70 | | #define OGS_GNUC_NORETURN |
71 | | #endif |
72 | | |
73 | | #if __GNUC__ > 6 |
74 | | #define OGS_GNUC_FALLTHROUGH __attribute__ ((fallthrough)) |
75 | | #else |
76 | | #define OGS_GNUC_FALLTHROUGH |
77 | | #endif |
78 | | |
79 | | #if defined(_WIN32) |
80 | | #define htole16(x) (x) |
81 | | #define htole32(x) (x) |
82 | | #define htole64(x) (x) |
83 | | #define le16toh(x) (x) |
84 | | #define le32toh(x) (x) |
85 | | #define le64toh(x) (x) |
86 | | |
87 | | #define htobe16(x) htons((x)) |
88 | | #define htobe32(x) htonl((x)) |
89 | | #define htobe64(x) htonll((x)) |
90 | | #define be16toh(x) ntohs((x)) |
91 | | #define be32toh(x) ntohl((x)) |
92 | | #define be64toh(x) ntohll((x)) |
93 | | |
94 | | #elif defined(__APPLE__) |
95 | | #include <libkern/OSByteOrder.h> |
96 | | #define htole16(x) OSSwapHostToLittleInt16((x)) |
97 | | #define htole32(x) OSSwapHostToLittleInt32((x)) |
98 | | #define htole64(x) OSSwapHostToLittleInt64((x)) |
99 | | #define le16toh(x) OSSwapLittleToHostInt16((x)) |
100 | | #define le32toh(x) OSSwapLittleToHostInt32((x)) |
101 | | #define le64toh(x) OSSwapLittleToHostInt64((x)) |
102 | | |
103 | | #define htobe16(x) OSSwapHostToBigInt16((x)) |
104 | | #define htobe32(x) OSSwapHostToBigInt32((x)) |
105 | | #define htobe64(x) OSSwapHostToBigInt64((x)) |
106 | | #define be16toh(x) OSSwapBigToHostInt16((x)) |
107 | | #define be32toh(x) OSSwapBigToHostInt32((x)) |
108 | | #define be64toh(x) OSSwapBigToHostInt64((x)) |
109 | | |
110 | | #elif defined(__FreeBSD__) |
111 | | #include <sys/endian.h> |
112 | | |
113 | | #elif defined(__linux__) |
114 | | #include <endian.h> |
115 | | |
116 | | #endif |
117 | | |
118 | | #ifndef WORDS_BIGENDIAN |
119 | | #if OGS_BYTE_ORDER == OGS_BIG_ENDIAN |
120 | | #define WORDS_BIGENDIAN 1 |
121 | | #endif |
122 | | #endif |
123 | | |
124 | | typedef struct ogs_uint24_s { |
125 | | uint32_t v:24; |
126 | | } __attribute__ ((packed)) ogs_uint24_t; |
127 | | |
128 | | static ogs_inline ogs_uint24_t ogs_be24toh(ogs_uint24_t x) |
129 | 0 | { |
130 | 0 | uint32_t tmp = x.v; |
131 | 0 | tmp = be32toh(tmp); |
132 | 0 | x.v = tmp >> 8; |
133 | 0 | return x; |
134 | 0 | } Unexecuted instantiation: nas-message-fuzz.c:ogs_be24toh Unexecuted instantiation: decoder.c:ogs_be24toh Unexecuted instantiation: ies.c:ogs_be24toh Unexecuted instantiation: types.c:ogs_be24toh Unexecuted instantiation: ogs-abort.c:ogs_be24toh Unexecuted instantiation: ogs-strings.c:ogs_be24toh Unexecuted instantiation: ogs-conv.c:ogs_be24toh Unexecuted instantiation: ogs-log.c:ogs_be24toh Unexecuted instantiation: ogs-pkbuf.c:ogs_be24toh Unexecuted instantiation: ogs-memory.c:ogs_be24toh Unexecuted instantiation: ogs-sockaddr.c:ogs_be24toh Unexecuted instantiation: ogs-hash.c:ogs_be24toh Unexecuted instantiation: ogs-core.c:ogs_be24toh Unexecuted instantiation: ogs-errno.c:ogs_be24toh Unexecuted instantiation: ogs-time.c:ogs_be24toh Unexecuted instantiation: ogs-socket.c:ogs_be24toh Unexecuted instantiation: ogs-tlv.c:ogs_be24toh Unexecuted instantiation: gtp-message-fuzz.c:ogs_be24toh Unexecuted instantiation: context.c:ogs_be24toh Unexecuted instantiation: xact.c:ogs_be24toh Unexecuted instantiation: conv.c:ogs_be24toh Unexecuted instantiation: message.c:ogs_be24toh Unexecuted instantiation: path.c:ogs_be24toh Unexecuted instantiation: build.c:ogs_be24toh Unexecuted instantiation: ogs-timer.c:ogs_be24toh Unexecuted instantiation: ogs-socknode.c:ogs_be24toh Unexecuted instantiation: ogs-udp.c:ogs_be24toh Unexecuted instantiation: ogs-poll.c:ogs_be24toh Unexecuted instantiation: ogs-tlv-msg.c:ogs_be24toh Unexecuted instantiation: ogs-epoll.c:ogs_be24toh Unexecuted instantiation: ogs-rbtree.c:ogs_be24toh Unexecuted instantiation: ogs-sockopt.c:ogs_be24toh Unexecuted instantiation: ogs-notify.c:ogs_be24toh Unexecuted instantiation: ogs-yaml.c:ogs_be24toh Unexecuted instantiation: ogs-context.c:ogs_be24toh Unexecuted instantiation: ogs-config.c:ogs_be24toh Unexecuted instantiation: ogs-init.c:ogs_be24toh Unexecuted instantiation: ogs-queue.c:ogs_be24toh Unexecuted instantiation: ogs-env.c:ogs_be24toh |
135 | | |
136 | | static ogs_inline ogs_uint24_t ogs_htobe24(ogs_uint24_t x) |
137 | 0 | { |
138 | 0 | uint32_t tmp = x.v; |
139 | 0 | tmp = htobe32(tmp); |
140 | 0 | x.v = tmp >> 8; |
141 | 0 | return x; |
142 | 0 | } Unexecuted instantiation: nas-message-fuzz.c:ogs_htobe24 Unexecuted instantiation: decoder.c:ogs_htobe24 Unexecuted instantiation: ies.c:ogs_htobe24 Unexecuted instantiation: types.c:ogs_htobe24 Unexecuted instantiation: ogs-abort.c:ogs_htobe24 Unexecuted instantiation: ogs-strings.c:ogs_htobe24 Unexecuted instantiation: ogs-conv.c:ogs_htobe24 Unexecuted instantiation: ogs-log.c:ogs_htobe24 Unexecuted instantiation: ogs-pkbuf.c:ogs_htobe24 Unexecuted instantiation: ogs-memory.c:ogs_htobe24 Unexecuted instantiation: ogs-sockaddr.c:ogs_htobe24 Unexecuted instantiation: ogs-hash.c:ogs_htobe24 Unexecuted instantiation: ogs-core.c:ogs_htobe24 Unexecuted instantiation: ogs-errno.c:ogs_htobe24 Unexecuted instantiation: ogs-time.c:ogs_htobe24 Unexecuted instantiation: ogs-socket.c:ogs_htobe24 Unexecuted instantiation: ogs-tlv.c:ogs_htobe24 Unexecuted instantiation: gtp-message-fuzz.c:ogs_htobe24 Unexecuted instantiation: context.c:ogs_htobe24 Unexecuted instantiation: xact.c:ogs_htobe24 Unexecuted instantiation: conv.c:ogs_htobe24 Unexecuted instantiation: message.c:ogs_htobe24 Unexecuted instantiation: path.c:ogs_htobe24 Unexecuted instantiation: build.c:ogs_htobe24 Unexecuted instantiation: ogs-timer.c:ogs_htobe24 Unexecuted instantiation: ogs-socknode.c:ogs_htobe24 Unexecuted instantiation: ogs-udp.c:ogs_htobe24 Unexecuted instantiation: ogs-poll.c:ogs_htobe24 Unexecuted instantiation: ogs-tlv-msg.c:ogs_htobe24 Unexecuted instantiation: ogs-epoll.c:ogs_htobe24 Unexecuted instantiation: ogs-rbtree.c:ogs_htobe24 Unexecuted instantiation: ogs-sockopt.c:ogs_htobe24 Unexecuted instantiation: ogs-notify.c:ogs_htobe24 Unexecuted instantiation: ogs-yaml.c:ogs_htobe24 Unexecuted instantiation: ogs-context.c:ogs_htobe24 Unexecuted instantiation: ogs-config.c:ogs_htobe24 Unexecuted instantiation: ogs-init.c:ogs_htobe24 Unexecuted instantiation: ogs-queue.c:ogs_htobe24 Unexecuted instantiation: ogs-env.c:ogs_htobe24 |
143 | | |
144 | | #if OGS_BYTE_ORDER == OGS_BIG_ENDIAN |
145 | | #define ED2(x1, x2) x1 x2 |
146 | | #define ED3(x1, x2, x3) x1 x2 x3 |
147 | | #define ED4(x1, x2, x3, x4) x1 x2 x3 x4 |
148 | | #define ED5(x1, x2, x3, x4, x5) x1 x2 x3 x4 x5 |
149 | | #define ED6(x1, x2, x3, x4, x5, x6) x1 x2 x3 x4 x5 x6 |
150 | | #define ED7(x1, x2, x3, x4, x5, x6, x7) x1 x2 x3 x4 x5 x6 x7 |
151 | | #define ED8(x1, x2, x3, x4, x5, x6, x7, x8) x1 x2 x3 x4 x5 x6 x7 x8 |
152 | | #else |
153 | | #define ED2(x1, x2) x2 x1 |
154 | | #define ED3(x1, x2, x3) x3 x2 x1 |
155 | | #define ED4(x1, x2, x3, x4) x4 x3 x2 x1 |
156 | | #define ED5(x1, x2, x3, x4, x5) x5 x4 x3 x2 x1 |
157 | | #define ED6(x1, x2, x3, x4, x5, x6) x6 x5 x4 x3 x2 x1 |
158 | | #define ED7(x1, x2, x3, x4, x5, x6, x7) x7 x6 x5 x4 x3 x2 x1 |
159 | | #define ED8(x1, x2, x3, x4, x5, x6, x7, x8) x8 x7 x6 x5 x4 x3 x2 x1 |
160 | | #endif |
161 | | |
162 | | #define OGS_STATIC_ASSERT(expr) \ |
163 | | typedef char dummy_for_ogs_static_assert##__LINE__[(expr) ? 1 : -1] |
164 | | |
165 | 7.96k | #define OGS_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
166 | | |
167 | 11.3k | #define OGS_STRINGIFY(n) OGS_STRINGIFY_HELPER(n) |
168 | 11.3k | #define OGS_STRINGIFY_HELPER(n) #n |
169 | | |
170 | | #define OGS_PASTE(n1, n2) OGS_PASTE_HELPER(n1, n2) |
171 | | #define OGS_PASTE_HELPER(n1, n2) n1##n2 |
172 | | |
173 | | #define OGS_INET_NTOP(src, dst) \ |
174 | 0 | inet_ntop(AF_INET, (void *)(uintptr_t)(src), (dst), INET_ADDRSTRLEN) |
175 | | #define OGS_INET6_NTOP(src, dst) \ |
176 | 0 | inet_ntop(AF_INET6, (void *)(src), (dst), INET6_ADDRSTRLEN) |
177 | | |
178 | 0 | #define ogs_max(x , y) (((x) > (y)) ? (x) : (y)) |
179 | 0 | #define ogs_min(x , y) (((x) < (y)) ? (x) : (y)) |
180 | | |
181 | | #if defined(_WIN32) |
182 | | #define OGS_IS_DIR_SEPARATOR(c) ((c) == OGS_DIR_SEPARATOR || (c) == '/') |
183 | | #else |
184 | | #define OGS_IS_DIR_SEPARATOR(c) ((c) == OGS_DIR_SEPARATOR) |
185 | | #endif |
186 | | |
187 | | #define ogs_container_of(ptr, type, member) \ |
188 | 31.6k | (type *)((unsigned char *)ptr - offsetof(type, member)) |
189 | | |
190 | | #ifndef SWITCH_CASE_INIT |
191 | | #define SWITCH_CASE_INIT |
192 | | #define SWITCH(X) {char *__switch_p__, __switch_next__; \ |
193 | | for (__switch_p__ = \ |
194 | | X ? (char *)X : (char *)"OGS_SWITCH_NULL", \ |
195 | | __switch_next__ = 1; \ |
196 | | __switch_p__; \ |
197 | | __switch_p__ = 0, __switch_next__ = 1) { { |
198 | | #define CASE(X) } if (!__switch_next__ || \ |
199 | | (__switch_next__ = \ |
200 | | strcmp(__switch_p__, X)) == 0) { |
201 | | #define DEFAULT } { |
202 | | #define END }}} |
203 | | #endif |
204 | | |
205 | | #define OGS_ARG_MAX 256 |
206 | | #define OGS_MAX_FILEPATH_LEN 256 |
207 | | #define OGS_MAX_IFNAME_LEN 32 |
208 | | |
209 | | #define OGS_MAX_SDU_LEN 32768 /* Should Heap */ |
210 | 97.2k | #define OGS_HUGE_LEN 8192 /* Can Stack */ |
211 | | #define OGS_MAX_PKT_LEN 2048 |
212 | | |
213 | 11.3k | #define OGS_FILE_LINE __FILE__ ":" OGS_STRINGIFY(__LINE__) |
214 | | |
215 | | #define ogs_uint64_to_uint32(x) ((x >= 0xffffffffUL) ? 0xffffffffU : x) |
216 | | |
217 | | #define OGS_OBJECT_REF(__oBJ) \ |
218 | | ((__oBJ)->reference_count)++, \ |
219 | | ogs_debug("[REF] %d", ((__oBJ)->reference_count)) |
220 | | #define OGS_OBJECT_UNREF(__oBJ) \ |
221 | | ogs_debug("[UNREF] %d", ((__oBJ)->reference_count)), \ |
222 | | ((__oBJ)->reference_count)-- |
223 | | #define OGS_OBJECT_IS_REF(__oBJ) ((__oBJ)->reference_count > 1) |
224 | | |
225 | 0 | #define OGS_POINTER_TO_UINT(u) ((uintptr_t)(u)) |
226 | 0 | #define OGS_UINT_TO_POINTER(u) ((void *)(uintptr_t)(u)) |
227 | | |
228 | | #ifdef __cplusplus |
229 | | } |
230 | | #endif |
231 | | |
232 | | #endif /* OGS_MACROS_H */ |