ua_util.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #define UA_assert(ignore) assert(ignore)
  37. /* Heap memory functions */
  38. INLINE UA_Int32 _UA_free(void *ptr, char *pname, char *f, UA_Int32 l) {
  39. DBG_VERBOSE(printf("UA_free;%p;;%s;;%s;%d\n", ptr, pname, f, l); fflush(stdout));
  40. free(ptr); // checks if ptr != NULL in the background
  41. return UA_SUCCESS;
  42. }
  43. INLINE UA_Int32 _UA_alloc(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
  44. if(ptr == UA_NULL)
  45. return UA_ERR_INVALID_VALUE;
  46. *ptr = malloc(size);
  47. DBG_VERBOSE(printf("UA_alloc - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
  48. if(*ptr == UA_NULL)
  49. return UA_ERR_NO_MEMORY;
  50. return UA_SUCCESS;
  51. }
  52. INLINE UA_Int32 _UA_alloca(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
  53. if(ptr == UA_NULL)
  54. return UA_ERR_INVALID_VALUE;
  55. #ifdef MSVC
  56. *ptr = _alloca(size);
  57. #else
  58. *ptr = alloca(size);
  59. #endif
  60. DBG_VERBOSE(printf("UA_alloca - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
  61. return UA_SUCCESS;
  62. }
  63. UA_Int32 UA_memcpy(void *dst, void const *src, UA_Int32 size);
  64. #define UA_free(ptr) _UA_free(ptr, # ptr, __FILE__, __LINE__)
  65. #define UA_alloc(ptr, size) _UA_alloc(ptr, size, # ptr, # size, __FILE__, __LINE__)
  66. /** @brief UA_alloca assigns memory on the stack instead of the heap. It is a
  67. very fast alternative to standard malloc. The memory is automatically
  68. returned ('freed') when the current function returns. Use this only for
  69. small temporary values. For more than 100Byte, it is more reasonable to use
  70. proper UA_alloc. */
  71. #define UA_alloca(ptr, size) _UA_alloca(ptr, size, # ptr, # size, __FILE__, __LINE__)
  72. #endif /* UA_UTIL_H_ */