ua_util.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /************************/
  40. /* Thread Local Storage */
  41. /************************/
  42. #ifdef UA_ENABLE_MULTITHREADING
  43. # ifdef __GNUC__
  44. # define UA_THREAD_LOCAL __thread
  45. # elif defined(_MSC_VER)
  46. # define UA_THREAD_LOCAL __declspec(thread)
  47. # else
  48. # error No thread local storage keyword defined for this compiler
  49. # endif
  50. #else
  51. # define UA_THREAD_LOCAL
  52. #endif
  53. /********************/
  54. /* System Libraries */
  55. /********************/
  56. #include <time.h>
  57. #ifdef _WIN32
  58. # include <winsock2.h> //needed for amalgation
  59. # include <windows.h>
  60. # undef SLIST_ENTRY
  61. #else
  62. # include <sys/time.h>
  63. #endif
  64. /*************************/
  65. /* External Dependencies */
  66. /*************************/
  67. #include "queue.h"
  68. #ifdef UA_ENABLE_MULTITHREADING
  69. # define _LGPL_SOURCE
  70. # include <urcu.h>
  71. # include <urcu/wfcqueue.h>
  72. # include <urcu/uatomic.h>
  73. # include <urcu/rculfhash.h>
  74. # include <urcu/lfstack.h>
  75. # ifdef NDEBUG
  76. # define UA_RCU_LOCK() rcu_read_lock()
  77. # define UA_RCU_UNLOCK() rcu_read_unlock()
  78. # else
  79. extern UA_THREAD_LOCAL bool rcu_locked;
  80. # define UA_RCU_LOCK() do { \
  81. assert(!rcu_locked); \
  82. rcu_locked = UA_TRUE; \
  83. rcu_read_lock(); } while(0)
  84. # define UA_RCU_UNLOCK() do { \
  85. assert(rcu_locked); \
  86. rcu_locked = UA_FALSE; \
  87. rcu_read_lock(); } while(0)
  88. # endif
  89. #else
  90. # define UA_RCU_LOCK()
  91. # define UA_RCU_UNLOCK()
  92. #endif
  93. #endif /* UA_UTIL_H_ */