ua_config.h.in 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2013-2015 the contributors as stated in the AUTHORS file
  3. *
  4. * This file is part of open62541. open62541 is free software: you can
  5. * redistribute it and/or modify it under the terms of the GNU Lesser General
  6. * Public License, version 3 (as published by the Free Software Foundation) with
  7. * a static linking exception as stated in the LICENSE file provided with
  8. * open62541.
  9. *
  10. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. * details.
  14. */
  15. #ifndef UA_CONFIG_H_
  16. #define UA_CONFIG_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #define UA_LOGLEVEL ${UA_LOGLEVEL}
  21. #define UA_GIT_COMMIT_ID "${GIT_COMMIT_ID}"
  22. #cmakedefine UA_ENABLE_MULTITHREADING
  23. #cmakedefine UA_ENABLE_METHODCALLS
  24. #cmakedefine UA_ENABLE_SUBSCRIPTIONS
  25. #cmakedefine UA_ENABLE_TYPENAMES
  26. #cmakedefine UA_ENABLE_GENERATE_NAMESPACE0
  27. #cmakedefine UA_ENABLE_EXTERNAL_NAMESPACES
  28. #cmakedefine UA_ENABLE_NODEMANAGEMENT
  29. #cmakedefine UA_ENABLE_EMBEDDED_LIBC
  30. #cmakedefine UA_ENABLE_NONSTANDARD_UDP
  31. #cmakedefine UA_ENABLE_NONSTANDARD_STATELESS
  32. /**
  33. * Standard Includes
  34. * ----------------- */
  35. #ifndef _XOPEN_SOURCE
  36. # define _XOPEN_SOURCE 500
  37. #endif
  38. #ifndef _DEFAULT_SOURCE
  39. # define _DEFAULT_SOURCE
  40. #endif
  41. #include <stddef.h>
  42. #include <stdint.h>
  43. /**
  44. * Function Export
  45. * --------------- */
  46. /* On Win32: Define UA_DYNAMIC_LINKING and UA_DYNAMIC_LINKING_EXPORT in order to
  47. export symbols for a DLL. Define UA_DYNAMIC_LINKING only to import symbols
  48. from a DLL.*/
  49. #cmakedefine UA_DYNAMIC_LINKING
  50. #if defined(_WIN32) && defined(UA_DYNAMIC_LINKING)
  51. # ifdef UA_DYNAMIC_LINKING_EXPORT /* export dll */
  52. # ifdef __GNUC__
  53. # define UA_EXPORT __attribute__ ((dllexport))
  54. # else
  55. # define UA_EXPORT __declspec(dllexport)
  56. # endif
  57. # else /* import dll */
  58. # ifdef __GNUC__
  59. # define UA_EXPORT __attribute__ ((dllimport))
  60. # else
  61. # define UA_EXPORT __declspec(dllimport)
  62. # endif
  63. # endif
  64. #else /* non win32 */
  65. # if __GNUC__ || __clang__
  66. # define UA_EXPORT __attribute__ ((visibility ("default")))
  67. # endif
  68. #endif
  69. #ifndef UA_EXPORT
  70. # define UA_EXPORT /* fallback to default */
  71. #endif
  72. /**
  73. * Inline Functions
  74. * ---------------- */
  75. #ifdef _MSC_VER
  76. # define UA_INLINE __inline
  77. #else
  78. # define UA_INLINE inline
  79. #endif
  80. /**
  81. * Non-aliasing pointers
  82. * -------------------- */
  83. #ifdef _MSC_VER
  84. # define UA_RESTRICT __restrict
  85. #elif defined(__GNUC__)
  86. # define UA_RESTRICT __restrict__
  87. #else
  88. # define UA_RESTRICT restrict
  89. #endif
  90. /**
  91. * Function attributes
  92. * ------------------- */
  93. #ifdef __GNUC__
  94. # define UA_FUNC_ATTR_MALLOC __attribute__((malloc))
  95. # define UA_FUNC_ATTR_PURE __attribute__ ((pure))
  96. # define UA_FUNC_ATTR_CONST __attribute__((const))
  97. # define UA_FUNC_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  98. #else
  99. # define UA_FUNC_ATTR_MALLOC
  100. # define UA_FUNC_ATTR_PURE
  101. # define UA_FUNC_ATTR_CONST
  102. # define UA_FUNC_ATTR_WARN_UNUSED_RESULT
  103. #endif
  104. /**
  105. * Memory Management
  106. * -----------------
  107. * Replace the macros for custom memory allocators if necessary */
  108. #include <stdlib.h>
  109. #ifdef _WIN32
  110. # include <malloc.h>
  111. #endif
  112. #define UA_free(ptr) free(ptr)
  113. #define UA_malloc(size) malloc(size)
  114. #define UA_calloc(num, size) calloc(num, size)
  115. #define UA_realloc(ptr, size) realloc(ptr, size)
  116. #ifndef NO_ALLOCA
  117. # if defined(__GNUC__) || defined(__clang__)
  118. # define UA_alloca(size) __builtin_alloca (size)
  119. # elif defined(_WIN32)
  120. # define UA_alloca(SIZE) _alloca(SIZE)
  121. # else
  122. # include <alloca.h>
  123. # define UA_alloca(SIZE) alloca(SIZE)
  124. # endif
  125. #endif
  126. /**
  127. * Atomic Operations
  128. * -----------------
  129. * Atomic operations that synchronize across processor cores (for
  130. * multithreading). Only the inline-functions defined next are used. Replace
  131. * with architecture-specific operations if necessary. */
  132. #ifndef UA_ENABLE_MULTITHREADING
  133. # define UA_atomic_sync()
  134. #else
  135. # ifdef _MSC_VER /* Visual Studio */
  136. # define UA_atomic_sync() _ReadWriteBarrier()
  137. # else /* GCC/Clang */
  138. # define UA_atomic_sync() __sync_synchronize()
  139. # endif
  140. #endif
  141. static UA_INLINE void *
  142. UA_atomic_xchg(void * volatile * addr, void *new) {
  143. #ifndef UA_ENABLE_MULTITHREADING
  144. void *old = *addr;
  145. *addr = new;
  146. return old;
  147. #else
  148. # ifdef _MSC_VER /* Visual Studio */
  149. return _InterlockedExchangePointer(addr, new);
  150. # else /* GCC/Clang */
  151. return __sync_lock_test_and_set(addr, new);
  152. # endif
  153. #endif
  154. }
  155. static UA_INLINE void *
  156. UA_atomic_cmpxchg(void * volatile * addr, void *expected, void *new) {
  157. #ifndef UA_ENABLE_MULTITHREADING
  158. void *old = *addr;
  159. if(old == expected) {
  160. *addr = new;
  161. }
  162. return old;
  163. #else
  164. # ifdef _MSC_VER /* Visual Studio */
  165. return _InterlockedCompareExchangePointer(addr, expected, new);
  166. # else /* GCC/Clang */
  167. return __sync_val_compare_and_swap(addr, expected, new);
  168. # endif
  169. #endif
  170. }
  171. static UA_INLINE uint32_t
  172. UA_atomic_add(volatile uint32_t *addr, uint32_t increase) {
  173. #ifndef UA_ENABLE_MULTITHREADING
  174. *addr += increase;
  175. return *addr;
  176. #else
  177. # ifdef _MSC_VER /* Visual Studio */
  178. return _InterlockedExchangeAdd(addr, increase) + increase;
  179. # else /* GCC/Clang */
  180. return __sync_add_and_fetch(addr, increase);
  181. # endif
  182. #endif
  183. }
  184. /**
  185. * Binary Encoding Overlays
  186. * ------------------------
  187. * Integers and floating point numbers are transmitted in little-endian (IEE 754
  188. * for floating point) encoding. If the target architecture uses the same
  189. * format, numeral datatypes can be memcpy'd (overlayed) on the binary stream.
  190. * This speeds up encoding.
  191. *
  192. * Integer Endianness
  193. * ^^^^^^^^^^^^^^^^^^ */
  194. #if defined(_WIN32) || (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  195. (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) /* little endian detected */
  196. # define UA_BINARY_OVERLAYABLE_INTEGER true
  197. #elif defined(__ANDROID__) /* Andoid */
  198. # include <endian.h>
  199. # if __BYTE_ORDER == __LITTLE_ENDIAN
  200. # define UA_BINARY_OVERLAYABLE_INTEGER true
  201. # endif
  202. #elif defined(__linux__) /* Linux */
  203. # include <endian.h>
  204. # if __BYTE_ORDER == __LITTLE_ENDIAN
  205. # define UA_BINARY_OVERLAYABLE_INTEGER true
  206. # endif
  207. # if __FLOAT_BYTE_ORDER == __LITTLE_ENDIAN
  208. # define UA_BINARY_OVERLAYABLE_FLOAT true
  209. # endif
  210. #elif defined(__OpenBSD__) /* OpenBSD */
  211. # include <sys/endian.h>
  212. # if BYTE_ORDER == LITTLE_ENDIAN
  213. # define UA_BINARY_OVERLAYABLE_INTEGER true
  214. # endif
  215. #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) /* Other BSD */
  216. # include <sys/endian.h>
  217. # if _BYTE_ORDER == _LITTLE_ENDIAN
  218. # define UA_BINARY_OVERLAYABLE_INTEGER true
  219. # endif
  220. #elif defined(__APPLE__) /* Apple (MacOS, iOS) */
  221. # include <libkern/OSByteOrder.h>
  222. # if defined(__LITTLE_ENDIAN__)
  223. # define UA_BINARY_OVERLAYABLE_INTEGER true
  224. # endif
  225. #elif defined(__QNX__) || defined(__QNXNTO__) /* QNX */
  226. # include <gulliver.h>
  227. # if defined(__LITTLEENDIAN__)
  228. # define UA_BINARY_OVERLAYABLE_INTEGER true
  229. # endif
  230. #endif
  231. #ifndef UA_BINARY_OVERLAYABLE_INTEGER
  232. # define UA_BINARY_OVERLAYABLE_INTEGER false
  233. #endif
  234. /**
  235. * Float Endianness
  236. * ^^^^^^^^^^^^^^^^ */
  237. #if defined(_WIN32)
  238. # define UA_BINARY_OVERLAYABLE_FLOAT true
  239. #elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  240. (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) /* Defined only in GCC */
  241. # define UA_BINARY_OVERLAYABLE_FLOAT true
  242. #elif defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && \
  243. (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) /* Defined only in GCC */
  244. # define UA_BINARY_OVERLAYABLE_FLOAT true
  245. #endif
  246. #ifndef UA_BINARY_OVERLAYABLE_FLOAT
  247. # define UA_BINARY_OVERLAYABLE_FLOAT false
  248. #endif
  249. /**
  250. * Embed unavailable libc functions
  251. * -------------------------------- */
  252. #ifdef UA_ENABLE_EMBEDDED_LIBC
  253. void *memcpy(void *UA_RESTRICT dest, const void *UA_RESTRICT src, size_t n);
  254. void *memset(void *dest, int c, size_t n);
  255. size_t strlen(const char *s);
  256. int memcmp(const void *vl, const void *vr, size_t n);
  257. #else
  258. # include <string.h>
  259. #endif
  260. #ifdef __cplusplus
  261. } // extern "C"
  262. #endif
  263. #endif /* UA_CONFIG_H_ */