Line data Source code
1 : #ifndef AWS_COMMON_MACROS_H
2 : #define AWS_COMMON_MACROS_H
3 : /**
4 : * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 : * SPDX-License-Identifier: Apache-2.0.
6 : */
7 :
8 : #ifdef __cplusplus
9 : # define AWS_EXTERN_C_BEGIN extern "C" {
10 : # define AWS_EXTERN_C_END }
11 : #else
12 : # define AWS_EXTERN_C_BEGIN
13 : # define AWS_EXTERN_C_END
14 : #endif /* __cplusplus */
15 :
16 : #define AWS_CONCAT(A, B) A##B
17 : #define AWS_STATIC_ASSERT0(cond, msg) typedef char AWS_CONCAT(static_assertion_, msg)[(!!(cond)) * 2 - 1]
18 : #define AWS_STATIC_ASSERT1(cond, line) AWS_STATIC_ASSERT0(cond, AWS_CONCAT(at_line_, line))
19 : #define AWS_STATIC_ASSERT(cond) AWS_STATIC_ASSERT1(cond, __LINE__)
20 :
21 : /* https://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion */
22 29047223 : #define GLUE(x, y) x y
23 :
24 : #define RETURN_ARG_COUNT(_1_, _2_, _3_, _4_, _5_, count, ...) count
25 : #define EXPAND_ARGS(args) RETURN_ARG_COUNT args
26 : #define COUNT_ARGS_MAX5(...) EXPAND_ARGS((__VA_ARGS__, 5, 4, 3, 2, 1, 0))
27 :
28 : #define OVERLOAD_MACRO2(name, count) name##count
29 : #define OVERLOAD_MACRO1(name, count) OVERLOAD_MACRO2(name, count)
30 : #define OVERLOAD_MACRO(name, count) OVERLOAD_MACRO1(name, count)
31 :
32 28844699 : #define CALL_OVERLOAD(name, ...) GLUE(OVERLOAD_MACRO(name, COUNT_ARGS_MAX5(__VA_ARGS__)), (__VA_ARGS__))
33 :
34 : #define CALL_OVERLOAD_TEST1(x) x
35 : #define CALL_OVERLOAD_TEST2(x, y) y
36 : #define CALL_OVERLOAD_TEST3(x, y, z) z
37 : #define CALL_OVERLOAD_TEST(...) CALL_OVERLOAD(CALL_OVERLOAD_TEST, __VA_ARGS__)
38 : AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1) == 1);
39 : AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2) == 2);
40 : AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2, 3) == 3);
41 :
42 : #define AWS_CACHE_LINE 64
43 : /**
44 : * Format macro for strings of a specified length.
45 : * Allows non null-terminated strings to be used with the printf family of functions.
46 : * Ex: printf("scheme is " PRInSTR, 4, "http://example.org"); // ouputs: "scheme is http"
47 : */
48 : #define PRInSTR "%.*s"
49 :
50 : #if defined(_MSC_VER)
51 : # include <malloc.h>
52 : # define AWS_ALIGNED_TYPEDEF(from, to, alignment) typedef __declspec(align(alignment)) from to
53 : # define AWS_LIKELY(x) x
54 : # define AWS_UNLIKELY(x) x
55 : # define AWS_FORCE_INLINE __forceinline
56 : # define AWS_NO_INLINE __declspec(noinline)
57 : # define AWS_VARIABLE_LENGTH_ARRAY(type, name, length) type *name = _alloca(sizeof(type) * (length))
58 : # define AWS_DECLSPEC_NORETURN __declspec(noreturn)
59 : # define AWS_ATTRIBUTE_NORETURN
60 : #else
61 : # if defined(__GNUC__) || defined(__clang__)
62 : # define AWS_ALIGNED_TYPEDEF(from, to, alignment) typedef from to __attribute__((aligned(alignment)))
63 : # define AWS_TYPE_OF(a) __typeof__(a)
64 102064 : # define AWS_LIKELY(x) __builtin_expect(!!(x), 1)
65 119455 : # define AWS_UNLIKELY(x) __builtin_expect(!!(x), 0)
66 : # define AWS_FORCE_INLINE __attribute__((always_inline))
67 : # define AWS_NO_INLINE __attribute__((noinline))
68 : # define AWS_DECLSPEC_NORETURN
69 : # define AWS_ATTRIBUTE_NORETURN __attribute__((noreturn))
70 : # if defined(__cplusplus)
71 : # define AWS_VARIABLE_LENGTH_ARRAY(type, name, length) type *name = alloca(sizeof(type) * (length))
72 : # else
73 : # define AWS_VARIABLE_LENGTH_ARRAY(type, name, length) type name[length];
74 : # endif /* defined(__cplusplus) */
75 : # endif /* defined(__GNUC__) || defined(__clang__) */
76 : #endif /* defined(_MSC_VER) */
77 :
78 : /* If this is C++, restrict isn't supported. If this is not at least C99 on gcc and clang, it isn't supported.
79 : * If visual C++ building in C mode, the restrict definition is __restrict.
80 : * This just figures all of that out based on who's including this header file. */
81 : #if defined(__cplusplus)
82 : # define AWS_RESTRICT
83 : #else
84 : # if defined(_MSC_VER)
85 : # define AWS_RESTRICT __restrict
86 : # else
87 : # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
88 : # define AWS_RESTRICT restrict
89 : # else
90 : # define AWS_RESTRICT
91 : # endif /* defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L */
92 : # endif /* defined(_MSC_VER) */
93 : #endif /* defined(__cplusplus) */
94 :
95 : #if defined(_MSC_VER)
96 : # define AWS_THREAD_LOCAL __declspec(thread)
97 : #else
98 : # define AWS_THREAD_LOCAL __thread
99 : #endif
100 :
101 : #define AWS_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
102 : /**
103 : * from a pointer and a type of the struct containing the node
104 : * this will get you back to the pointer of the object. member is the name of
105 : * the instance of struct aws_linked_list_node in your struct.
106 : */
107 0 : #define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr)-offsetof(type, member)))
108 :
109 : #endif /* AWS_COMMON_MACROS_H */
|