ua_util.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef MSVC
  9. #ifndef WIN32
  10. #include <alloca.h> // alloca
  11. #else
  12. #include <malloc.h> // MinGW alloca
  13. #endif
  14. #endif
  15. #include <stddef.h> /* Needed for sys/queue.h */
  16. #ifndef MSVC
  17. #include <sys/queue.h>
  18. #else
  19. #include "queue.h"
  20. #endif
  21. #include "ua_types.h"
  22. /* Debug macros */
  23. #define DBG_VERBOSE(expression) // omit debug code
  24. #define DBG_ERR(expression) // omit debug code
  25. #define DBG(expression) // omit debug code
  26. #if defined(DEBUG) // --enable-debug=(yes|verbose)
  27. # undef DBG
  28. # define DBG(expression) expression
  29. # undef DBG_ERR
  30. # define DBG_ERR(expression) expression
  31. # if defined(VERBOSE) // --enable-debug=verbose
  32. # undef DBG_VERBOSE
  33. # define DBG_VERBOSE(expression) expression
  34. # endif
  35. #endif
  36. #ifdef DEBUG
  37. #define UA_assert(ignore) assert(ignore)
  38. #else
  39. #define UA_assert(ignore)
  40. #endif
  41. /* Heap memory functions */
  42. INLINE UA_Int32 _UA_free(void *ptr, char *pname, char *f, UA_Int32 l) {
  43. DBG_VERBOSE(printf("UA_free;%p;;%s;;%s;%d\n", ptr, pname, f, l); fflush(stdout));
  44. free(ptr); // checks if ptr != NULL in the background
  45. return UA_SUCCESS;
  46. }
  47. INLINE UA_Int32 _UA_alloc(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
  48. if(ptr == UA_NULL)
  49. return UA_ERR_INVALID_VALUE;
  50. *ptr = malloc(size);
  51. DBG_VERBOSE(printf("UA_alloc - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
  52. if(*ptr == UA_NULL)
  53. return UA_ERR_NO_MEMORY;
  54. return UA_SUCCESS;
  55. }
  56. INLINE UA_Int32 _UA_alloca(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
  57. if(ptr == UA_NULL)
  58. return UA_ERR_INVALID_VALUE;
  59. #ifdef MSVC
  60. *ptr = _alloca(size);
  61. #else
  62. *ptr = alloca(size);
  63. #endif
  64. DBG_VERBOSE(printf("UA_alloca - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
  65. return UA_SUCCESS;
  66. }
  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. #define UA_free(ptr) _UA_free(ptr, # ptr, __FILE__, __LINE__)
  72. #define UA_alloc(ptr, size) _UA_alloc(ptr, size, # ptr, # size, __FILE__, __LINE__)
  73. /** @brief UA_alloca assigns memory on the stack instead of the heap. It is a
  74. very fast alternative to standard malloc. The memory is automatically
  75. returned ('freed') when the current function returns. Use this only for
  76. small temporary values. For more than 100Byte, it is more reasonable to use
  77. proper UA_alloc. */
  78. #define UA_alloca(ptr, size) _UA_alloca(ptr, size, # ptr, # size, __FILE__, __LINE__)
  79. #endif /* UA_UTIL_H_ */