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 #ifndef UCX_COMMON_H
80 #define UCX_COMMON_H
81
82
83 #define UCX_VERSION_MAJOR 3
84
85
86 #define UCX_VERSION_MINOR 1
87
88
89 #define UCX_VERSION (((
UCX_VERSION_MAJOR)<<
16)|
UCX_VERSION_MINOR)
90
91
92
93
94
95 #include <stdlib.h>
96 #include <stddef.h>
97 #include <stdbool.h>
98 #include <stdint.h>
99 #include <sys/types.h>
100
101
102
103
104
105 #ifndef INTPTR_MAX
106 #error Missing
INTPTR_MAX definition
107 #endif
108 #if INTPTR_MAX ==
INT64_MAX
109
110
111
112 #define CX_WORDSIZE 64
113 #elif INTPTR_MAX ==
INT32_MAX
114
115
116
117 #define CX_WORDSIZE 32
118 #else
119 #error Unknown pointer size or missing size macros!
120 #endif
121
122
123
124
125
126 #ifndef SSIZE_MAX
127 #if CX_WORDSIZE ==
64
128
129
130
131 #define SSIZE_MAX 0x7fffffffffffffffll
132 #else
133 #define SSIZE_MAX 0x7fffffffl
134 #endif
135 #endif
136
137
138
139
140
141
142 #ifndef __GNUC__
143
144
145
146 #define __attribute__(x)
147 #endif
148
149
150
151
152 #define cx_attr_nonnull __attribute__((__nonnull__))
153
154
155
156
157 #define cx_attr_nonnull_arg(...) __attribute__((__nonnull__(
__VA_ARGS__)))
158
159
160
161
162 #define cx_attr_returns_nonnull __attribute__((__returns_nonnull__))
163
164
165
166
167 #define cx_attr_malloc __attribute__((__malloc__))
168
169 #ifndef __clang__
170
171
172
173
174
175
176
177 #define cx_attr_dealloc(freefunc, freefunc_arg) \
178 __attribute__((__malloc__(freefunc, freefunc_arg)))
179 #else
180
181
182
183 #define cx_attr_dealloc(...)
184 #endif
185
186
187
188
189 #define cx_attr_dealloc_ucx cx_attr_dealloc(cxFree,
2)
190
191
192
193
194 #define cx_attr_allocsize(...) __attribute__((__alloc_size__(
__VA_ARGS__)))
195
196
197 #ifdef __clang__
198
199
200
201 #define cx_attr_cstr_arg(idx)
202
203
204
205 #define cx_attr_access(mode, ...)
206 #else
207 #if __GNUC__ <
10
208
209
210
211 #define cx_attr_access(mode, ...)
212 #else
213
214
215
216 #define cx_attr_access(mode, ...) __attribute__((__access__(mode,
__VA_ARGS__)))
217 #endif
218 #if __GNUC__ <
14
219
220
221
222 #define cx_attr_cstr_arg(idx)
223 #else
224
225
226
227
228
229 #define cx_attr_cstr_arg(idx) \
230 __attribute__((__null_terminated_string_arg__(idx)))
231 #endif
232 #endif
233
234
235
236
237
238
239
240
241 #define cx_attr_access_r(...) cx_attr_access(__read_only__,
__VA_ARGS__)
242
243
244
245
246
247
248
249 #define cx_attr_access_rw(...) cx_attr_access(__read_write__,
__VA_ARGS__)
250
251
252
253
254
255
256
257 #define cx_attr_access_w(...) cx_attr_access(__write_only__,
__VA_ARGS__)
258
259 #if __STDC_VERSION__ >=
202300L
260
261
262
263
264 #define cx_attr_unused [[maybe_unused]]
265
266
267
268
269 #define cx_attr_nodiscard [[nodiscard]]
270
271 #else
272
273
274
275
276 #define cx_attr_unused __attribute__((__unused__))
277
278
279
280
281 #define cx_attr_nodiscard __attribute__((__warn_unused_result__))
282
283 #endif
284
285
286
287
288
289
290
291
292 typedef size_t (*cx_write_func)(
293 const void *,
294 size_t,
295 size_t,
296 void *
297 );
298
299
300
301
302 typedef size_t (*cx_read_func)(
303 void *,
304 size_t,
305 size_t,
306 void *
307 );
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322 #define cx_nmemb(arr) (
sizeof(arr)/
sizeof((arr)[
0]))
323
324
325
326
327
328 #if (
__GNUC__ >=
5 || defined(__clang__)) && !defined(
CX_NO_SZMUL_BUILTIN)
329 #define CX_SZMUL_BUILTIN
330 #define cx_szmul(a, b, result) __builtin_mul_overflow(a, b, result)
331 #else
332
333
334
335
336
337
338
339
340
341
342 #define cx_szmul(a, b, result) cx_szmul_impl(a, b, result)
343
344
345
346
347
348
349
350
351
352
353
354
355
356 #if __cplusplus
357 extern "C"
358 #endif
359 int cx_szmul_impl(
size_t a,
size_t b,
size_t *result);
360 #endif
361
362
363
364
365
366
367 #ifdef _MSC_VER
368
369 #include <BaseTsd.h>
370 typedef SSIZE_T ssize_t;
371
372
373 #define _Thread_local __declspec(thread)
374 #endif
375
376 #endif
377