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