/src/FreeRDP/winpr/include/winpr/cast.h
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Cast macros |
4 | | * |
5 | | * Copyright 2024 Armin Novak <anovak@thincast.com> |
6 | | * Copyright 2024 Thincast Technologies GmbH |
7 | | * |
8 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | * you may not use this file except in compliance with the License. |
10 | | * You may obtain a copy of the License at |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | #pragma once |
21 | | |
22 | | #include <stdint.h> |
23 | | |
24 | | #include <winpr/platform.h> |
25 | | #include <winpr/assert-api.h> |
26 | | |
27 | | /** |
28 | | * @brief C++ safe cast macro |
29 | | * @since version 3.10.1 |
30 | | */ |
31 | | #ifdef __cplusplus |
32 | | #define WINPR_CXX_COMPAT_CAST(t, val) static_cast<t>(val) |
33 | | #else |
34 | 22.2M | #define WINPR_CXX_COMPAT_CAST(t, val) ((t)(val)) |
35 | | #endif |
36 | | |
37 | | /**! @brief Checks alignment requirements. |
38 | | * - On supported platforms (arm, arm64) check that unaligned access is enabled or die |
39 | | * - On unsupported platforms print a compiler warning |
40 | | */ |
41 | | #if defined(DISABLE_SUPPORTED_ARCH_CHECKS) |
42 | | #elif defined(_M_ARM) || defined(_M_ARM64) |
43 | | /* MSVC is not defining the same guards as everyone else, so lets hope they always support it and |
44 | | * not only sometimes. */ |
45 | | #if !defined(__ARM_FEATURE_UNALIGNED) && !defined(_MSC_VER) |
46 | | #warning \ |
47 | | "-munaligned-access is required on arm. Use -DDISABLE_SUPPORTED_ARCH_CHECKS=ON to ignore, but there will be dragons ahead!" |
48 | | #else |
49 | | #define WINPR_ARCH_SUPPORTED 1 |
50 | | #endif |
51 | | #elif defined(_M_IX86) || defined(_M_AMD64) || defined(_M_PPC) || defined(_M_S390X) |
52 | | #define WINPR_ARCH_SUPPORTED 1 |
53 | | #elif defined(_M_RISCV32) || defined(_M_RISCV64) |
54 | | #if !defined(__riscv_misaligned_fast) |
55 | | #warning \ |
56 | | "RISCV must support __riscv_misaligned_fast. Use -DDISABLE_SUPPORTED_ARCH_CHECKS=ON to ignore, but there will be dragons ahead!" |
57 | | #else |
58 | | #define WINPR_ARCH_SUPPORTED 1 |
59 | | #endif |
60 | | #elif defined(__EMSCRIPTEN__) |
61 | | #define WINPR_ARCH_SUPPORTED 1 |
62 | | #endif |
63 | | |
64 | | #if !defined(WINPR_ARCH_SUPPORTED) |
65 | | #if defined(__GNUC__) || defined(__clang__) || \ |
66 | | (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)) || \ |
67 | | (defined(__cplusplus) && (__cplusplus >= 202302L)) |
68 | | #warning "unaligned pointer access not verified on platform, SIGBUS may happen!" |
69 | | #endif |
70 | | #endif |
71 | | |
72 | | /**! @brief Cast to \ref t and silence Wcast-align warnings. |
73 | | * |
74 | | * This just silences compiler warnings on supported architectures. |
75 | | * Support is checked at compile time. |
76 | | * |
77 | | * @since version 3.32.0 |
78 | | */ |
79 | | #if defined(_WIN32) |
80 | | #define WINPR_PACKED_ALIGN_CAST(t, val) WINPR_CXX_COMPAT_CAST(t, val) |
81 | | #elif defined(WINPR_ARCH_SUPPORTED) |
82 | | #define WINPR_PACKED_ALIGN_CAST(t, val) \ |
83 | 15.0k | __extension__({ \ |
84 | 15.0k | WINPR_PRAGMA_DIAG_PUSH; \ |
85 | 15.0k | WINPR_PRAGMA_DIAG_IGNORED_CAST_ALIGN; \ |
86 | 15.0k | typeof(t) aligntmp = WINPR_CXX_COMPAT_CAST(t, val); \ |
87 | 15.0k | WINPR_PRAGMA_DIAG_POP; \ |
88 | 15.0k | aligntmp; \ |
89 | 15.0k | }) |
90 | | #else |
91 | | #define WINPR_PACKED_ALIGN_CAST(t, val) \ |
92 | | __extension__({ \ |
93 | | typeof(t) aligntmp = WINPR_CXX_COMPAT_CAST(t, val); \ |
94 | | if (aligntmp) \ |
95 | | { \ |
96 | | const size_t fromtype = _Alignof(typeof(*aligntmp)); \ |
97 | | const size_t actual = ((uintptr_t)(val)) % fromtype; \ |
98 | | WINPR_ASSERT(actual == 0); \ |
99 | | } \ |
100 | | aligntmp; \ |
101 | | }) |
102 | | #endif |
103 | | |
104 | | #if defined(__GNUC__) || defined(__clang__) |
105 | | /** |
106 | | * @brief A macro to do dirty casts. Do not use without a good justification! |
107 | | * @param ptr The pointer to cast |
108 | | * @param dstType The data type to cast to |
109 | | * @return The casted pointer |
110 | | * @since version 3.9.0 |
111 | | */ |
112 | | #define WINPR_REINTERPRET_CAST(ptr, srcType, dstType) \ |
113 | 9.04k | __extension__({ \ |
114 | 9.04k | union \ |
115 | 9.04k | { \ |
116 | 9.04k | srcType src; \ |
117 | 9.04k | dstType dst; \ |
118 | 9.04k | } cnv; \ |
119 | 9.04k | WINPR_STATIC_ASSERT(sizeof(srcType) == sizeof(dstType)); \ |
120 | 9.04k | cnv.src = ptr; \ |
121 | 9.04k | cnv.dst; \ |
122 | 9.04k | }) |
123 | | |
124 | | /** |
125 | | * @brief A macro to do dirty casts. Do not use without a good justification! |
126 | | * @param ptr The pointer to cast |
127 | | * @param dstType The data type to cast to |
128 | | * @return The casted pointer |
129 | | * @since version 3.9.0 |
130 | | */ |
131 | | #define WINPR_CAST_CONST_PTR_AWAY(ptr, dstType) \ |
132 | 18.0k | __extension__({ \ |
133 | 18.0k | union \ |
134 | 18.0k | { \ |
135 | 18.0k | __typeof(ptr) src; \ |
136 | 18.0k | dstType dst; \ |
137 | 18.0k | } cnv; \ |
138 | 18.0k | cnv.src = ptr; \ |
139 | 18.0k | cnv.dst; \ |
140 | 18.0k | }) |
141 | | |
142 | | /** |
143 | | * @brief A macro to do function pointer casts. Do not use without a good justification! |
144 | | * @param ptr The pointer to cast |
145 | | * @param dstType The data type to cast to |
146 | | * @return The casted pointer |
147 | | * @since version 3.9.0 |
148 | | */ |
149 | | #define WINPR_FUNC_PTR_CAST(ptr, dstType) \ |
150 | 3.01k | __extension__({ \ |
151 | 3.01k | union \ |
152 | 3.01k | { \ |
153 | 3.01k | __typeof(ptr) src; \ |
154 | 3.01k | dstType dst; \ |
155 | 3.01k | } cnv; \ |
156 | 3.01k | WINPR_STATIC_ASSERT(sizeof(dstType) == sizeof(__typeof(ptr))); \ |
157 | 3.01k | cnv.src = ptr; \ |
158 | 3.01k | cnv.dst; \ |
159 | 3.01k | }) |
160 | | |
161 | | #else |
162 | | #define WINPR_REINTERPRET_CAST(ptr, srcType, dstType) (dstType) ptr |
163 | | #define WINPR_CAST_CONST_PTR_AWAY(ptr, dstType) (dstType) ptr |
164 | | #define WINPR_FUNC_PTR_CAST(ptr, dstType) (dstType)(uintptr_t) ptr |
165 | | #endif |
166 | | |
167 | | #if defined(__GNUC__) || defined(__clang__) |
168 | | |
169 | | /** |
170 | | * @brief A macro to do checked integer casts. |
171 | | * will check if the value does change by casting to and from the target type and comparing the |
172 | | * values. will also check if the sign of a value changes during conversion. |
173 | | * |
174 | | * @param type the type to cast to |
175 | | * @param var the integer of unknown type to cast |
176 | | * @return The casted integer |
177 | | * @since version 3.10.1 |
178 | | */ |
179 | | #define WINPR_ASSERTING_INT_CAST(type, ivar) \ |
180 | 20.1M | __extension__({ \ |
181 | 20.1M | __typeof(ivar) var = ivar; \ |
182 | 20.1M | WINPR_ASSERT((var) == \ |
183 | 20.1M | WINPR_CXX_COMPAT_CAST(__typeof(var), WINPR_CXX_COMPAT_CAST(type, (var)))); \ |
184 | 20.1M | WINPR_ASSERT((((var) > 0) && (WINPR_CXX_COMPAT_CAST(type, (var)) > 0)) || \ |
185 | 20.1M | (((var) <= 0) && WINPR_CXX_COMPAT_CAST(type, (var)) <= 0)); \ |
186 | 20.1M | WINPR_CXX_COMPAT_CAST(type, (var)); \ |
187 | 20.1M | }) |
188 | | |
189 | | #else |
190 | | #define WINPR_ASSERTING_INT_CAST(type, var) WINPR_CXX_COMPAT_CAST(type, var) |
191 | | #endif |