ua_util_internal.h 4.4 KB

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