ua_util.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef UA_UTILITY_H_
  2. #define UA_UTILITY_H_
  3. #include <stdlib.h> // malloc, free
  4. #include <string.h> // memcpy
  5. #include "ua_types.h"
  6. /* Debug macros */
  7. #define DBG_VERBOSE(expression) // omit debug code
  8. #define DBG_ERR(expression) // omit debug code
  9. #define DBG(expression) // omit debug code
  10. #if defined(DEBUG) // --enable-debug=(yes|verbose)
  11. # undef DBG
  12. # define DBG(expression) expression
  13. # undef DBG_ERR
  14. # define DBG_ERR(expression) expression
  15. # if defined(VERBOSE) // --enable-debug=verbose
  16. # undef DBG_VERBOSE
  17. # define DBG_VERBOSE(expression) expression
  18. # endif
  19. #endif
  20. /* Global Variables */
  21. extern UA_ByteString UA_ByteString_securityPoliceNone;
  22. /* Heap memory functions */
  23. #define UA_NULL ((void *)0)
  24. extern void const *UA_alloc_lastptr;
  25. #define UA_free(ptr) _UA_free(ptr, # ptr, __FILE__, __LINE__)
  26. #define UA_alloc(ptr, size) _UA_alloc(ptr, size, # ptr, # size, __FILE__, __LINE__)
  27. inline UA_Int32 _UA_free(void *ptr, char *pname, char *f, UA_Int32 l) {
  28. DBG_VERBOSE(printf("UA_free;%p;;%s;;%s;%d\n", ptr, pname, f, l); fflush(stdout));
  29. free(ptr); // checks if ptr != NULL in the background
  30. return UA_SUCCESS;
  31. }
  32. inline UA_Int32 _UA_alloc(void **ptr, UA_Int32 size, char *pname, char *sname, char *f, UA_Int32 l) {
  33. if(ptr == UA_NULL) return UA_ERR_INVALID_VALUE;
  34. UA_alloc_lastptr = *ptr = malloc(size);
  35. DBG_VERBOSE(printf("UA_alloc - %p;%d;%s;%s;%s;%d\n", *ptr, size, pname, sname, f, l); fflush(stdout));
  36. if(*ptr == UA_NULL) return UA_ERR_NO_MEMORY;
  37. return UA_SUCCESS;
  38. }
  39. inline UA_Int32 UA_memcpy(void *dst, void const *src, UA_Int32 size) {
  40. if(dst == UA_NULL) return UA_ERR_INVALID_VALUE;
  41. DBG_VERBOSE(printf("UA_memcpy - %p;%p;%d\n", dst, src, size));
  42. memcpy(dst, src, size);
  43. return UA_SUCCESS;
  44. }
  45. static inline UA_Int32 UA_VTable_isValidType(UA_Int32 type) {
  46. if(type < 0 /* UA_BOOLEAN */ || type > 271 /* UA_INVALID */)
  47. return UA_ERR_INVALID_VALUE;
  48. return UA_SUCCESS;
  49. }
  50. #endif /* UA_UTILITY_H_ */