ua_util.h 2.2 KB

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