ua_util.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef UA_UTIL_H_
  2. #define UA_UTIL_H_
  3. #ifndef UA_AMALGAMATE
  4. # include "ua_config.h"
  5. #endif
  6. #ifndef __USE_POSIX
  7. # define __USE_POSIX
  8. #endif
  9. #ifndef _POSIX_SOURCE
  10. # define _POSIX_SOURCE
  11. #endif
  12. #ifndef _POSIX_C_SOURCE
  13. # define _POSIX_C_SOURCE 199309L
  14. #endif
  15. #ifndef _BSD_SOURCE
  16. # define _BSD_SOURCE
  17. #endif
  18. #ifndef _DEFAULT_SOURCE
  19. # define _DEFAULT_SOURCE
  20. #endif
  21. /*********************/
  22. /* Memory Management */
  23. /*********************/
  24. #include <stdlib.h> // malloc, free
  25. #include <string.h> // memcpy
  26. #include <assert.h> // assert
  27. #ifdef _WIN32
  28. # include <malloc.h>
  29. #endif
  30. #define UA_NULL ((void *)0)
  31. // subtract from nodeids to get from the encoding to the content
  32. #define UA_ENCODINGOFFSET_XML 1
  33. #define UA_ENCODINGOFFSET_BINARY 2
  34. #define UA_assert(ignore) assert(ignore)
  35. /* Replace the macros with functions for custom allocators if necessary */
  36. #define UA_free(ptr) free(ptr)
  37. #define UA_malloc(size) malloc(size)
  38. #define UA_calloc(num, size) calloc(num, size)
  39. #define UA_realloc(ptr, size) realloc(ptr, size)
  40. #define UA_memcpy(dst, src, size) memcpy(dst, src, size)
  41. #define UA_memset(ptr, value, size) memset(ptr, value, size)
  42. #ifdef _WIN32
  43. # define UA_alloca(SIZE) _alloca(SIZE)
  44. #else
  45. #ifdef __GNUC__
  46. # define UA_alloca(size) __builtin_alloca (size)
  47. #else
  48. # include <alloca.h>
  49. # define UA_alloca(SIZE) alloca(SIZE)
  50. #endif
  51. #endif
  52. /********************/
  53. /* System Libraries */
  54. /********************/
  55. #include <stdarg.h> // va_start, va_end
  56. #include <time.h>
  57. #include <stdio.h> // printf
  58. #include <inttypes.h>
  59. #ifdef _WIN32
  60. # include <winsock2.h> //needed for amalgation
  61. # include <windows.h>
  62. # undef SLIST_ENTRY
  63. # define RAND(SEED) (UA_UInt32)rand()
  64. #else
  65. # if !(defined htole16 && defined htole32 && defined htole64 && defined le16toh && defined le32toh && defined le64toh)
  66. # include <endian.h>
  67. # if !(defined htole16 && defined htole32 && defined htole64 && defined le16toh && defined le32toh && defined le64toh)
  68. # if ( __BYTE_ORDER != __LITTLE_ENDIAN )
  69. # error "Host byte order is not little-endian and no appropriate conversion functions are defined. (Have a look at ua_config.h)"
  70. # else
  71. # define htole16(x) x
  72. # define htole32(x) x
  73. # define htole64(x) x
  74. # define le16toh(x) x
  75. # define le32toh(x) x
  76. # define le64toh(x) x
  77. # endif
  78. # endif
  79. # endif
  80. # include <sys/time.h>
  81. # define RAND(SEED) (UA_UInt32)rand_r(SEED)
  82. #endif
  83. /*************************/
  84. /* External Dependencies */
  85. /*************************/
  86. #ifndef UA_AMALGAMATE
  87. # include "queue.h"
  88. #endif
  89. #ifdef UA_MULTITHREADING
  90. # define _LGPL_SOURCE
  91. # include <urcu.h>
  92. # include <urcu/wfcqueue.h>
  93. # include <urcu/compiler.h> // for caa_container_of
  94. # include <urcu/uatomic.h>
  95. # include <urcu/rculfhash.h>
  96. #endif
  97. #endif /* UA_UTIL_H_ */