ua_util.h 3.7 KB

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