ua_util.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef UA_UTIL_H_
  2. #define UA_UTIL_H_
  3. #include "ua_config.h"
  4. /* Subtract from nodeids to get from the encoding to the content */
  5. #define UA_ENCODINGOFFSET_XML 1
  6. #define UA_ENCODINGOFFSET_BINARY 2
  7. #include <assert.h> // assert
  8. #define UA_assert(ignore) assert(ignore)
  9. /*********************/
  10. /* Memory Management */
  11. /*********************/
  12. /* Replace the macros with functions for custom allocators if necessary */
  13. #include <stdlib.h> // malloc, free
  14. #ifdef _WIN32
  15. # include <malloc.h>
  16. #endif
  17. #ifndef UA_free
  18. # define UA_free(ptr) free(ptr)
  19. #endif
  20. #ifndef UA_malloc
  21. # define UA_malloc(size) malloc(size)
  22. #endif
  23. #ifndef UA_calloc
  24. # define UA_calloc(num, size) calloc(num, size)
  25. #endif
  26. #ifndef UA_realloc
  27. # define UA_realloc(ptr, size) realloc(ptr, size)
  28. #endif
  29. #ifndef NO_ALLOCA
  30. # ifdef __GNUC__
  31. # define UA_alloca(size) __builtin_alloca (size)
  32. # elif defined(_WIN32)
  33. # define UA_alloca(SIZE) _alloca(SIZE)
  34. # else
  35. # include <alloca.h>
  36. # define UA_alloca(SIZE) alloca(SIZE)
  37. # endif
  38. #endif
  39. #define container_of(ptr, type, member) \
  40. (type *)((uintptr_t)ptr - offsetof(type,member))
  41. /************************/
  42. /* Thread Local Storage */
  43. /************************/
  44. #ifdef UA_ENABLE_MULTITHREADING
  45. # ifdef __GNUC__
  46. # define UA_THREAD_LOCAL __thread
  47. # elif defined(_MSC_VER)
  48. # define UA_THREAD_LOCAL __declspec(thread)
  49. # else
  50. # error No thread local storage keyword defined for this compiler
  51. # endif
  52. #else
  53. # define UA_THREAD_LOCAL
  54. #endif
  55. /********************/
  56. /* System Libraries */
  57. /********************/
  58. #ifdef _WIN32
  59. # include <winsock2.h> //needed for amalgation
  60. # include <windows.h>
  61. # undef SLIST_ENTRY
  62. #endif
  63. #include <time.h>
  64. #if defined(_WIN32) && !defined(__MINGW32__)
  65. int gettimeofday(struct timeval *tp, struct timezone *tzp);
  66. #else
  67. # include <sys/time.h>
  68. #endif
  69. #if defined(__APPLE__) || defined(__MACH__)
  70. #include <mach/clock.h>
  71. #include <mach/mach.h>
  72. #endif
  73. /*************************/
  74. /* External Dependencies */
  75. /*************************/
  76. #include "queue.h"
  77. #ifdef UA_ENABLE_MULTITHREADING
  78. # define _LGPL_SOURCE
  79. # include <urcu.h>
  80. # include <urcu/wfcqueue.h>
  81. # include <urcu/uatomic.h>
  82. # include <urcu/rculfhash.h>
  83. # include <urcu/lfstack.h>
  84. # ifdef NDEBUG
  85. # define UA_RCU_LOCK() rcu_read_lock()
  86. # define UA_RCU_UNLOCK() rcu_read_unlock()
  87. # define UA_ASSERT_RCU_LOCKED()
  88. # define UA_ASSERT_RCU_UNLOCKED()
  89. # else
  90. extern UA_THREAD_LOCAL bool rcu_locked;
  91. # define UA_ASSERT_RCU_LOCKED() assert(rcu_locked)
  92. # define UA_ASSERT_RCU_UNLOCKED() assert(!rcu_locked)
  93. # define UA_RCU_LOCK() do { \
  94. UA_ASSERT_RCU_UNLOCKED(); \
  95. rcu_locked = true; \
  96. rcu_read_lock(); } while(0)
  97. # define UA_RCU_UNLOCK() do { \
  98. UA_ASSERT_RCU_LOCKED(); \
  99. rcu_locked = false; \
  100. rcu_read_lock(); } while(0)
  101. # endif
  102. #else
  103. # define UA_RCU_LOCK()
  104. # define UA_RCU_UNLOCK()
  105. # define UA_ASSERT_RCU_LOCKED()
  106. # define UA_ASSERT_RCU_UNLOCKED()
  107. #endif
  108. #endif /* UA_UTIL_H_ */