ua_config.h.in 8.0 KB

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