ua_util.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef UA_UTIL_H_
  2. #define UA_UTIL_H_
  3. #include <stdio.h> // printf
  4. #include <stdlib.h> // malloc, free
  5. #include <string.h> // memcpy
  6. #include <assert.h> // assert
  7. #include "ua_config.h"
  8. #include <stddef.h> /* Needed for sys/queue.h */
  9. #ifndef WIN32
  10. #include <sys/queue.h>
  11. #include <alloca.h>
  12. #else
  13. #include "queue.h"
  14. #include <malloc.h>
  15. #endif
  16. #include "ua_types.h"
  17. #define UA_NULL ((void *)0)
  18. #define UA_TRUE (42 == 42)
  19. #define UA_FALSE (!UA_TRUE)
  20. //identifier numbers are different for XML and binary, so we have to substract an offset for comparison
  21. #define UA_ENCODINGOFFSET_XML 1
  22. #define UA_ENCODINGOFFSET_BINARY 2
  23. /* Debug macros */
  24. #define DBG_VERBOSE(expression) // omit debug code
  25. #define DBG_ERR(expression) // omit debug code
  26. #define DBG(expression) // omit debug code
  27. #if defined(DEBUG) // --enable-debug=(yes|verbose)
  28. # undef DBG
  29. # define DBG(expression) expression
  30. # undef DBG_ERR
  31. # define DBG_ERR(expression) expression
  32. # if defined(VERBOSE) // --enable-debug=verbose
  33. # undef DBG_VERBOSE
  34. # define DBG_VERBOSE(expression) expression
  35. # endif
  36. #endif
  37. #ifdef DEBUG
  38. #define UA_assert(ignore) assert(ignore)
  39. #else
  40. #define UA_assert(ignore)
  41. #endif
  42. /* Heap memory functions */
  43. #ifdef DEBUG
  44. #define UA_free(ptr) _UA_free(ptr, # ptr, __FILE__, __LINE__)
  45. INLINE void _UA_free(void *ptr, char *pname, char *f, UA_Int32 l) {
  46. DBG_VERBOSE(printf("UA_free;%p;;%s;;%s;%d\n", ptr, pname, f, l); fflush(stdout));
  47. free(ptr); // checks if ptr != UA_NULL in the background
  48. }
  49. #else
  50. #define UA_free(ptr) _UA_free(ptr)
  51. INLINE void _UA_free(void *ptr) {
  52. free(ptr); // checks if ptr != UA_NULL in the background
  53. }
  54. #endif
  55. #ifdef DEBUG
  56. #define UA_alloc(size) _UA_alloc(size, __FILE__, __LINE__)
  57. INLINE void * _UA_alloc(UA_Int32 size, char *file, UA_Int32 line) {
  58. DBG_VERBOSE(printf("UA_alloc - %d;%s;%d\n", size, file, line); fflush(stdout));
  59. return malloc(size);
  60. }
  61. #else
  62. #define UA_alloc(size) _UA_alloc(size)
  63. INLINE void * _UA_alloc(UA_Int32 size) {
  64. return malloc(size);
  65. }
  66. #endif
  67. INLINE void UA_memcpy(void *dst, void const *src, UA_Int32 size) {
  68. DBG_VERBOSE(printf("UA_memcpy - %p;%p;%d\n", dst, src, size));
  69. memcpy(dst, src, size);
  70. }
  71. #ifdef WIN32
  72. #define UA_alloca(size) _alloca(size)
  73. #else
  74. #define UA_alloca(size) alloca(size)
  75. #endif
  76. #endif /* UA_UTIL_H_ */