architecture_definitions.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. * Copyright 2018 (c) Jose Cabral, fortiss GmbH
  8. */
  9. #ifndef ARCH_UA_ARCHITECTURE_DEFINITIONS_H_
  10. #define ARCH_UA_ARCHITECTURE_DEFINITIONS_H_
  11. #include <open62541/config.h>
  12. /**
  13. * C99 Definitions
  14. * --------------- */
  15. #include <string.h>
  16. #include <stddef.h>
  17. /* Include stdint.h and stdbool.h or workaround for older Visual Studios */
  18. #ifdef UNDER_CE
  19. # include "stdint.h"
  20. #endif
  21. #if !defined(_MSC_VER) || _MSC_VER >= 1800
  22. # include <stdint.h>
  23. # include <stdbool.h> /* C99 Boolean */
  24. #else
  25. # include "ms_stdint.h"
  26. # if !defined(__bool_true_false_are_defined)
  27. # define bool unsigned char
  28. # define true 1
  29. # define false 0
  30. # define __bool_true_false_are_defined
  31. # endif
  32. #endif
  33. /**
  34. * Assertions
  35. * ----------
  36. * The assert macro is disabled by defining NDEBUG. It is often forgotten to
  37. * include -DNDEBUG in the compiler flags when using the single-file release. So
  38. * we make assertions dependent on the UA_DEBUG definition handled by CMake. */
  39. #ifdef UA_DEBUG
  40. # include <assert.h>
  41. # define UA_assert(ignore) assert(ignore)
  42. #else
  43. # define UA_assert(ignore) do {} while(0)
  44. #endif
  45. /* Outputs an error message at compile time if the assert fails.
  46. * Example usage:
  47. * UA_STATIC_ASSERT(sizeof(long)==7, use_another_compiler_luke)
  48. * See: https://stackoverflow.com/a/4815532/869402 */
  49. #if defined(__cplusplus) && __cplusplus >= 201103L /* C++11 or above */
  50. # define UA_STATIC_ASSERT(cond,msg) static_assert(cond, #msg)
  51. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L /* C11 or above */
  52. # define UA_STATIC_ASSERT(cond,msg) _Static_assert(cond, #msg)
  53. #elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) /* GCC, Clang, MSC */
  54. # define UA_CTASTR2(pre,post) pre ## post
  55. # define UA_CTASTR(pre,post) UA_CTASTR2(pre,post)
  56. # ifndef __COUNTER__ /* PPC GCC fix */
  57. # define __COUNTER__ __LINE__
  58. # endif
  59. # define UA_STATIC_ASSERT(cond,msg) \
  60. typedef struct { \
  61. int UA_CTASTR(static_assertion_failed_,msg) : !!(cond); \
  62. } UA_CTASTR(static_assertion_failed_,__COUNTER__)
  63. #else /* Everybody else */
  64. # define UA_STATIC_ASSERT(cond,msg) typedef char static_assertion_##msg[(cond)?1:-1]
  65. #endif
  66. #if defined(_WIN32) && defined(UA_DYNAMIC_LINKING)
  67. # ifdef UA_DYNAMIC_LINKING_EXPORT /* export dll */
  68. # ifdef __GNUC__
  69. # define UA_EXPORT __attribute__ ((dllexport))
  70. # else
  71. # define UA_EXPORT __declspec(dllexport)
  72. # endif
  73. # else /* import dll */
  74. # ifdef __GNUC__
  75. # define UA_EXPORT __attribute__ ((dllimport))
  76. # else
  77. # define UA_EXPORT __declspec(dllimport)
  78. # endif
  79. # endif
  80. #else /* non win32 */
  81. # if __GNUC__ || __clang__
  82. # define UA_EXPORT __attribute__ ((visibility ("default")))
  83. # endif
  84. #endif
  85. #ifndef UA_EXPORT
  86. # define UA_EXPORT /* fallback to default */
  87. #endif
  88. #define UA_THREADSAFE
  89. /**
  90. * Inline Functions
  91. * ---------------- */
  92. #ifdef _MSC_VER
  93. # define UA_INLINE __inline
  94. #else
  95. # define UA_INLINE inline
  96. #endif
  97. /**
  98. * Non-aliasing pointers
  99. * -------------------- */
  100. #ifdef _MSC_VER
  101. # define UA_RESTRICT __restrict
  102. #elif defined(__GNUC__)
  103. # define UA_RESTRICT __restrict__
  104. #else
  105. # define UA_RESTRICT restrict
  106. #endif
  107. /**
  108. * Function attributes
  109. * ------------------- */
  110. #if defined(__GNUC__) || defined(__clang__)
  111. # define UA_FUNC_ATTR_MALLOC __attribute__((malloc))
  112. # define UA_FUNC_ATTR_PURE __attribute__ ((pure))
  113. # define UA_FUNC_ATTR_CONST __attribute__((const))
  114. # define UA_FUNC_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  115. # define UA_FORMAT(X,Y) __attribute__ ((format (printf, X, Y)))
  116. #else
  117. # define UA_FUNC_ATTR_MALLOC
  118. # define UA_FUNC_ATTR_PURE
  119. # define UA_FUNC_ATTR_CONST
  120. # define UA_FUNC_ATTR_WARN_UNUSED_RESULT
  121. # define UA_FORMAT(X,Y)
  122. #endif
  123. #if defined(__GNUC__) || defined(__clang__)
  124. # define UA_DEPRECATED __attribute__((deprecated))
  125. #elif defined(_MSC_VER)
  126. # define UA_DEPRECATED __declspec(deprecated)
  127. #else
  128. # define UA_DEPRECATED
  129. #endif
  130. /**
  131. * Internal Attributes
  132. * -------------------
  133. * These attributes are only defined if the macro UA_INTERNAL is defined. That
  134. * way public methods can be annotated (e.g. to warn for unused results) but
  135. * warnings are only triggered for internal code. */
  136. #if defined(UA_INTERNAL) && (defined(__GNUC__) || defined(__clang__))
  137. # define UA_INTERNAL_DEPRECATED _Pragma ("GCC warning \"Macro is deprecated for internal use\"")
  138. #else
  139. # define UA_INTERNAL_DEPRECATED
  140. #endif
  141. #if defined(UA_INTERNAL) && (defined(__GNUC__) || defined(__clang__))
  142. # define UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  143. #else
  144. # define UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT
  145. #endif
  146. /**
  147. * Detect Endianness and IEEE 754 floating point
  148. * ---------------------------------------------
  149. * Integers and floating point numbers are transmitted in little-endian (IEEE
  150. * 754 for floating point) encoding. If the target architecture uses the same
  151. * format, numeral datatypes can be memcpy'd (overlayed) on the network buffer.
  152. * Otherwise, a slow default encoding routine is used that works for every
  153. * architecture.
  154. *
  155. * Integer Endianness
  156. * ^^^^^^^^^^^^^^^^^^
  157. * The definition ``UA_LITTLE_ENDIAN`` is true when the integer representation
  158. * of the target architecture is little-endian. */
  159. #if defined(_WIN32)
  160. # define UA_LITTLE_ENDIAN 1
  161. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
  162. # define UA_LITTLE_ENDIAN 1
  163. #elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  164. (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  165. # define UA_LITTLE_ENDIAN 1
  166. #elif defined(__linux__) /* Linux (including Android) */
  167. # include <endian.h>
  168. # if __BYTE_ORDER == __LITTLE_ENDIAN
  169. # define UA_LITTLE_ENDIAN 1
  170. # endif
  171. #elif defined(__OpenBSD__) /* OpenBSD */
  172. # include <sys/endian.h>
  173. # if BYTE_ORDER == LITTLE_ENDIAN
  174. # define UA_LITTLE_ENDIAN 1
  175. # endif
  176. #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) /* Other BSD */
  177. # include <sys/endian.h>
  178. # if _BYTE_ORDER == _LITTLE_ENDIAN
  179. # define UA_LITTLE_ENDIAN 1
  180. # endif
  181. #elif defined(__APPLE__) /* Apple (MacOS, iOS) */
  182. # include <libkern/OSByteOrder.h>
  183. # if defined(__LITTLE_ENDIAN__)
  184. # define UA_LITTLE_ENDIAN 1
  185. # endif
  186. #elif defined(__QNX__) || defined(__QNXNTO__) /* QNX */
  187. # include <gulliver.h>
  188. # if defined(__LITTLEENDIAN__)
  189. # define UA_LITTLE_ENDIAN 1
  190. # endif
  191. #elif defined(_OS9000) /* OS-9 */
  192. # if defined(_LIL_END)
  193. # define UA_LITTLE_ENDIAN 1
  194. # endif
  195. #endif
  196. #ifndef UA_LITTLE_ENDIAN
  197. # define UA_LITTLE_ENDIAN 0
  198. #endif
  199. /* Can the integers be memcpy'd onto the network buffer? Add additional checks
  200. * here. Some platforms (e.g. QNX) have sizeof(bool) > 1. Manually disable
  201. * overlayed integer encoding if that is the case. */
  202. #if (UA_LITTLE_ENDIAN == 1)
  203. UA_STATIC_ASSERT(sizeof(bool) == 1, cannot_overlay_integers_with_large_bool);
  204. # define UA_BINARY_OVERLAYABLE_INTEGER 1
  205. #else
  206. # define UA_BINARY_OVERLAYABLE_INTEGER 0
  207. #endif
  208. /**
  209. * Float Endianness
  210. * ^^^^^^^^^^^^^^^^
  211. * The definition ``UA_FLOAT_IEEE754`` is set to true when the floating point
  212. * number representation of the target architecture is IEEE 754. The definition
  213. * ``UA_FLOAT_LITTLE_ENDIAN`` is set to true when the floating point number
  214. * representation is in little-endian encoding. */
  215. #if defined(_WIN32)
  216. # define UA_FLOAT_IEEE754 1
  217. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
  218. defined(__ia64__) || defined(__powerpc__) || defined(__sparc__) || \
  219. defined(__arm__)
  220. # define UA_FLOAT_IEEE754 1
  221. #elif defined(__STDC_IEC_559__)
  222. # define UA_FLOAT_IEEE754 1
  223. #else
  224. # define UA_FLOAT_IEEE754 0
  225. #endif
  226. /* Wikipedia says (https://en.wikipedia.org/wiki/Endianness): Although the
  227. * ubiquitous x86 processors of today use little-endian storage for all types of
  228. * data (integer, floating point, BCD), there are a number of hardware
  229. * architectures where floating-point numbers are represented in big-endian form
  230. * while integers are represented in little-endian form. */
  231. #if defined(_WIN32)
  232. # define UA_FLOAT_LITTLE_ENDIAN 1
  233. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
  234. # define UA_FLOAT_LITTLE_ENDIAN 1
  235. #elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  236. (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) /* Defined only in GCC */
  237. # define UA_FLOAT_LITTLE_ENDIAN 1
  238. #elif defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && \
  239. (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) /* Defined only in GCC */
  240. # define UA_FLOAT_LITTLE_ENDIAN 1
  241. #endif
  242. #ifndef UA_FLOAT_LITTLE_ENDIAN
  243. # define UA_FLOAT_LITTLE_ENDIAN 0
  244. #endif
  245. /* Only if the floating points are litle-endian **and** in IEEE 754 format can
  246. * we memcpy directly onto the network buffer. */
  247. #if (UA_FLOAT_IEEE754 == 1) && (UA_FLOAT_LITTLE_ENDIAN == 1)
  248. # define UA_BINARY_OVERLAYABLE_FLOAT 1
  249. #else
  250. # define UA_BINARY_OVERLAYABLE_FLOAT 0
  251. #endif
  252. /* Atomic Operations
  253. * -----------------
  254. * Atomic operations that synchronize across processor cores (for
  255. * multithreading). Only the inline-functions defined next are used. Replace
  256. * with architecture-specific operations if necessary. */
  257. #if UA_MULTITHREADING >= 200
  258. #ifdef _MSC_VER /* Visual Studio */
  259. #define UA_atomic_sync() _ReadWriteBarrier()
  260. #else /* GCC/Clang */
  261. #define UA_atomic_sync() __sync_synchronize()
  262. #endif
  263. #else
  264. #define UA_atomic_sync()
  265. #endif
  266. static UA_INLINE void *
  267. UA_atomic_xchg(void * volatile * addr, void *newptr) {
  268. #if UA_MULTITHREADING >= 200
  269. #ifdef _MSC_VER /* Visual Studio */
  270. return _InterlockedExchangePointer(addr, newptr);
  271. #else /* GCC/Clang */
  272. return __sync_lock_test_and_set(addr, newptr);
  273. #endif
  274. #else
  275. void *old = *addr;
  276. *addr = newptr;
  277. return old;
  278. #endif
  279. }
  280. static UA_INLINE void *
  281. UA_atomic_cmpxchg(void * volatile * addr, void *expected, void *newptr) {
  282. #if UA_MULTITHREADING >= 200
  283. #ifdef _MSC_VER /* Visual Studio */
  284. return _InterlockedCompareExchangePointer(addr, expected, newptr);
  285. #else /* GCC/Clang */
  286. return __sync_val_compare_and_swap(addr, expected, newptr);
  287. #endif
  288. #else
  289. void *old = *addr;
  290. if(old == expected) {
  291. *addr = newptr;
  292. }
  293. return old;
  294. #endif
  295. }
  296. static UA_INLINE uint32_t
  297. UA_atomic_addUInt32(volatile uint32_t *addr, uint32_t increase) {
  298. #if UA_MULTITHREADING >= 200
  299. #ifdef _MSC_VER /* Visual Studio */
  300. return _InterlockedExchangeAdd(addr, increase) + increase;
  301. #else /* GCC/Clang */
  302. return __sync_add_and_fetch(addr, increase);
  303. #endif
  304. #else
  305. *addr += increase;
  306. return *addr;
  307. #endif
  308. }
  309. static UA_INLINE size_t
  310. UA_atomic_addSize(volatile size_t *addr, size_t increase) {
  311. #if UA_MULTITHREADING >= 200
  312. #ifdef _MSC_VER /* Visual Studio */
  313. return _InterlockedExchangeAdd(addr, increase) + increase;
  314. #else /* GCC/Clang */
  315. return __sync_add_and_fetch(addr, increase);
  316. #endif
  317. #else
  318. *addr += increase;
  319. return *addr;
  320. #endif
  321. }
  322. static UA_INLINE uint32_t
  323. UA_atomic_subUInt32(volatile uint32_t *addr, uint32_t decrease) {
  324. #if UA_MULTITHREADING >= 200
  325. #ifdef _MSC_VER /* Visual Studio */
  326. return _InterlockedExchangeSub(addr, decrease) - decrease;
  327. #else /* GCC/Clang */
  328. return __sync_sub_and_fetch(addr, decrease);
  329. #endif
  330. #else
  331. *addr -= decrease;
  332. return *addr;
  333. #endif
  334. }
  335. static UA_INLINE size_t
  336. UA_atomic_subSize(volatile size_t *addr, size_t decrease) {
  337. #if UA_MULTITHREADING >= 200
  338. #ifdef _MSC_VER /* Visual Studio */
  339. return _InterlockedExchangeSub(addr, decrease) - decrease;
  340. #else /* GCC/Clang */
  341. return __sync_sub_and_fetch(addr, decrease);
  342. #endif
  343. #else
  344. *addr -= decrease;
  345. return *addr;
  346. #endif
  347. }
  348. #endif /* ARCH_UA_ARCHITECTURE_DEFINITIONS_H_ */