ua_util.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef UA_UTIL_H_
  5. #define UA_UTIL_H_
  6. #include "ua_types.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /* BSD Queue Macros */
  11. #include "queue.h"
  12. /* Macro-Expand for MSVC workarounds */
  13. #define UA_MACRO_EXPAND(x) x
  14. #ifdef UA_ENABLE_MULTITHREADING
  15. /* Thread Local Storage */
  16. # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  17. # define UA_THREAD_LOCAL _Thread_local /* C11 */
  18. # elif defined(__GNUC__)
  19. # define UA_THREAD_LOCAL __thread /* GNU extension */
  20. # elif defined(_MSC_VER)
  21. # define UA_THREAD_LOCAL __declspec(thread) /* MSVC extension */
  22. # else
  23. # define UA_THREAD_LOCAL
  24. # warning The compiler does not allow thread-local variables. \
  25. The library can be built, but will not be thread-safe.
  26. # endif
  27. #else
  28. # define UA_THREAD_LOCAL
  29. #endif
  30. /* Integer Shortnames
  31. * ------------------
  32. * These are not exposed on the public API, since many user-applications make
  33. * the same definitions in their headers. */
  34. typedef UA_Byte u8;
  35. typedef UA_SByte i8;
  36. typedef UA_UInt16 u16;
  37. typedef UA_Int16 i16;
  38. typedef UA_UInt32 u32;
  39. typedef UA_Int32 i32;
  40. typedef UA_UInt64 u64;
  41. typedef UA_Int64 i64;
  42. typedef UA_StatusCode status;
  43. /* Atomic Operations
  44. * -----------------
  45. * Atomic operations that synchronize across processor cores (for
  46. * multithreading). Only the inline-functions defined next are used. Replace
  47. * with architecture-specific operations if necessary. */
  48. #ifndef UA_ENABLE_MULTITHREADING
  49. # define UA_atomic_sync()
  50. #else
  51. # ifdef _MSC_VER /* Visual Studio */
  52. # define UA_atomic_sync() _ReadWriteBarrier()
  53. # else /* GCC/Clang */
  54. # define UA_atomic_sync() __sync_synchronize()
  55. # endif
  56. #endif
  57. static UA_INLINE void *
  58. UA_atomic_xchg(void * volatile * addr, void *newptr) {
  59. #ifndef UA_ENABLE_MULTITHREADING
  60. void *old = *addr;
  61. *addr = newptr;
  62. return old;
  63. #else
  64. # ifdef _MSC_VER /* Visual Studio */
  65. return _InterlockedExchangePointer(addr, newptr);
  66. # else /* GCC/Clang */
  67. return __sync_lock_test_and_set(addr, newptr);
  68. # endif
  69. #endif
  70. }
  71. static UA_INLINE void *
  72. UA_atomic_cmpxchg(void * volatile * addr, void *expected, void *newptr) {
  73. #ifndef UA_ENABLE_MULTITHREADING
  74. void *old = *addr;
  75. if(old == expected) {
  76. *addr = newptr;
  77. }
  78. return old;
  79. #else
  80. # ifdef _MSC_VER /* Visual Studio */
  81. return _InterlockedCompareExchangePointer(addr, expected, newptr);
  82. # else /* GCC/Clang */
  83. return __sync_val_compare_and_swap(addr, expected, newptr);
  84. # endif
  85. #endif
  86. }
  87. static UA_INLINE uint32_t
  88. UA_atomic_add(volatile uint32_t *addr, uint32_t increase) {
  89. #ifndef UA_ENABLE_MULTITHREADING
  90. *addr += increase;
  91. return *addr;
  92. #else
  93. # ifdef _MSC_VER /* Visual Studio */
  94. return _InterlockedExchangeAdd(addr, increase) + increase;
  95. # else /* GCC/Clang */
  96. return __sync_add_and_fetch(addr, increase);
  97. # endif
  98. #endif
  99. }
  100. /* Utility Functions
  101. * ----------------- */
  102. /* Convert given byte string to a positive number. Returns the number of valid
  103. * digits. Stops if a non-digit char is found and returns the number of digits
  104. * up to that point. */
  105. size_t UA_readNumber(u8 *buf, size_t buflen, u32 *number);
  106. #define MIN(A,B) (A > B ? B : A)
  107. #define MAX(A,B) (A > B ? A : B)
  108. /* The typename string can be disabled to safe memory */
  109. #ifdef UA_ENABLE_TYPENAMES
  110. # define UA_TYPENAME(name) name,
  111. #else
  112. # define UA_TYPENAME(name)
  113. #endif
  114. #ifdef UA_DEBUG_DUMP_PKGS
  115. void UA_EXPORT UA_dump_hex_pkg(UA_Byte* buffer, size_t bufferLen);
  116. #endif
  117. #ifdef __cplusplus
  118. } // extern "C"
  119. #endif
  120. #endif /* UA_UTIL_H_ */