ua_util_internal.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. *
  5. * Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014, 2017 (c) Florian Palm
  7. * Copyright 2015 (c) LEvertz
  8. * Copyright 2015-2016 (c) Sten Grüner
  9. * Copyright 2015 (c) Chris Iatrou
  10. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  11. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  12. */
  13. #ifndef UA_UTIL_H_
  14. #define UA_UTIL_H_
  15. #include "ua_types.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* BSD Queue Macros */
  20. #include "../deps/queue.h"
  21. /* Macro-Expand for MSVC workarounds */
  22. #define UA_MACRO_EXPAND(x) x
  23. /* Integer Shortnames
  24. * ------------------
  25. * These are not exposed on the public API, since many user-applications make
  26. * the same definitions in their headers. */
  27. typedef UA_Byte u8;
  28. typedef UA_SByte i8;
  29. typedef UA_UInt16 u16;
  30. typedef UA_Int16 i16;
  31. typedef UA_UInt32 u32;
  32. typedef UA_Int32 i32;
  33. typedef UA_UInt64 u64;
  34. typedef UA_Int64 i64;
  35. typedef UA_StatusCode status;
  36. /* Atomic Operations
  37. * -----------------
  38. * Atomic operations that synchronize across processor cores (for
  39. * multithreading). Only the inline-functions defined next are used. Replace
  40. * with architecture-specific operations if necessary. */
  41. #ifndef UA_ENABLE_MULTITHREADING
  42. # define UA_atomic_sync()
  43. #else
  44. # ifdef _MSC_VER /* Visual Studio */
  45. # define UA_atomic_sync() _ReadWriteBarrier()
  46. # else /* GCC/Clang */
  47. # define UA_atomic_sync() __sync_synchronize()
  48. # endif
  49. #endif
  50. static UA_INLINE void *
  51. UA_atomic_xchg(void * volatile * addr, void *newptr) {
  52. #ifndef UA_ENABLE_MULTITHREADING
  53. void *old = *addr;
  54. *addr = newptr;
  55. return old;
  56. #else
  57. # ifdef _MSC_VER /* Visual Studio */
  58. return _InterlockedExchangePointer(addr, newptr);
  59. # else /* GCC/Clang */
  60. return __sync_lock_test_and_set(addr, newptr);
  61. # endif
  62. #endif
  63. }
  64. static UA_INLINE void *
  65. UA_atomic_cmpxchg(void * volatile * addr, void *expected, void *newptr) {
  66. #ifndef UA_ENABLE_MULTITHREADING
  67. void *old = *addr;
  68. if(old == expected) {
  69. *addr = newptr;
  70. }
  71. return old;
  72. #else
  73. # ifdef _MSC_VER /* Visual Studio */
  74. return _InterlockedCompareExchangePointer(addr, expected, newptr);
  75. # else /* GCC/Clang */
  76. return __sync_val_compare_and_swap(addr, expected, newptr);
  77. # endif
  78. #endif
  79. }
  80. static UA_INLINE uint32_t
  81. UA_atomic_addUInt32(volatile uint32_t *addr, uint32_t increase) {
  82. #ifndef UA_ENABLE_MULTITHREADING
  83. *addr += increase;
  84. return *addr;
  85. #else
  86. # ifdef _MSC_VER /* Visual Studio */
  87. return _InterlockedExchangeAdd(addr, increase) + increase;
  88. # else /* GCC/Clang */
  89. return __sync_add_and_fetch(addr, increase);
  90. # endif
  91. #endif
  92. }
  93. static UA_INLINE size_t
  94. UA_atomic_addSize(volatile size_t *addr, size_t increase) {
  95. #ifndef UA_ENABLE_MULTITHREADING
  96. *addr += increase;
  97. return *addr;
  98. #else
  99. # ifdef _MSC_VER /* Visual Studio */
  100. return _InterlockedExchangeAdd(addr, increase) + increase;
  101. # else /* GCC/Clang */
  102. return __sync_add_and_fetch(addr, increase);
  103. # endif
  104. #endif
  105. }
  106. static UA_INLINE uint32_t
  107. UA_atomic_subUInt32(volatile uint32_t *addr, uint32_t decrease) {
  108. #ifndef UA_ENABLE_MULTITHREADING
  109. *addr -= decrease;
  110. return *addr;
  111. #else
  112. # ifdef _MSC_VER /* Visual Studio */
  113. return _InterlockedExchangeSub(addr, decrease) - decrease;
  114. # else /* GCC/Clang */
  115. return __sync_sub_and_fetch(addr, decrease);
  116. # endif
  117. #endif
  118. }
  119. static UA_INLINE size_t
  120. UA_atomic_subSize(volatile size_t *addr, size_t decrease) {
  121. #ifndef UA_ENABLE_MULTITHREADING
  122. *addr -= decrease;
  123. return *addr;
  124. #else
  125. # ifdef _MSC_VER /* Visual Studio */
  126. return _InterlockedExchangeSub(addr, decrease) - decrease;
  127. # else /* GCC/Clang */
  128. return __sync_sub_and_fetch(addr, decrease);
  129. # endif
  130. #endif
  131. }
  132. /* Utility Functions
  133. * ----------------- */
  134. /* Convert given byte string to a positive number. Returns the number of valid
  135. * digits. Stops if a non-digit char is found and returns the number of digits
  136. * up to that point. */
  137. size_t UA_readNumber(u8 *buf, size_t buflen, u32 *number);
  138. #define UA_MIN(A,B) (A > B ? B : A)
  139. #define UA_MAX(A,B) (A > B ? A : B)
  140. #ifdef UA_DEBUG_DUMP_PKGS
  141. void UA_EXPORT UA_dump_hex_pkg(UA_Byte* buffer, size_t bufferLen);
  142. #endif
  143. #ifdef __cplusplus
  144. } // extern "C"
  145. #endif
  146. #endif /* UA_UTIL_H_ */