util_malloc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_malloc.h
  7. * \brief Headers for util_malloc.c
  8. **/
  9. #ifndef TOR_UTIL_MALLOC_H
  10. #define TOR_UTIL_MALLOC_H
  11. #include "lib/cc/compat_compiler.h"
  12. /* Memory management */
  13. void *tor_malloc_(size_t size) ATTR_MALLOC;
  14. void *tor_malloc_zero_(size_t size) ATTR_MALLOC;
  15. void *tor_calloc_(size_t nmemb, size_t size) ATTR_MALLOC;
  16. void *tor_realloc_(void *ptr, size_t size);
  17. void *tor_reallocarray_(void *ptr, size_t size1, size_t size2);
  18. char *tor_strdup_(const char *s) ATTR_MALLOC ATTR_NONNULL((1));
  19. char *tor_strndup_(const char *s, size_t n)
  20. ATTR_MALLOC ATTR_NONNULL((1));
  21. void *tor_memdup_(const void *mem, size_t len)
  22. ATTR_MALLOC ATTR_NONNULL((1));
  23. void *tor_memdup_nulterm_(const void *mem, size_t len)
  24. ATTR_MALLOC ATTR_NONNULL((1));
  25. void tor_free_(void *mem);
  26. /** Release memory allocated by tor_malloc, tor_realloc, tor_strdup,
  27. * etc. Unlike the free() function, the tor_free() macro sets the
  28. * pointer value to NULL after freeing it.
  29. *
  30. * This is a macro. If you need a function pointer to release memory from
  31. * tor_malloc(), use tor_free_().
  32. *
  33. * Note that this macro takes the address of the pointer it is going to
  34. * free and clear. If that pointer is stored with a nonstandard
  35. * alignment (eg because of a "packed" pragma) it is not correct to use
  36. * tor_free().
  37. */
  38. #ifdef __GNUC__
  39. #define tor_free(p) STMT_BEGIN \
  40. typeof(&(p)) tor_free__tmpvar = &(p); \
  41. raw_free(*tor_free__tmpvar); \
  42. *tor_free__tmpvar=NULL; \
  43. STMT_END
  44. #else
  45. #define tor_free(p) STMT_BEGIN \
  46. raw_free(p); \
  47. (p)=NULL; \
  48. STMT_END
  49. #endif
  50. #define tor_malloc(size) tor_malloc_(size)
  51. #define tor_malloc_zero(size) tor_malloc_zero_(size)
  52. #define tor_calloc(nmemb,size) tor_calloc_(nmemb, size)
  53. #define tor_realloc(ptr, size) tor_realloc_(ptr, size)
  54. #define tor_reallocarray(ptr, sz1, sz2) \
  55. tor_reallocarray_((ptr), (sz1), (sz2))
  56. #define tor_strdup(s) tor_strdup_(s)
  57. #define tor_strndup(s, n) tor_strndup_(s, n)
  58. #define tor_memdup(s, n) tor_memdup_(s, n)
  59. #define tor_memdup_nulterm(s, n) tor_memdup_nulterm_(s, n)
  60. /* Aliases for the underlying system malloc/realloc/free. Only use
  61. * them to indicate "I really want the underlying system function, I know
  62. * what I'm doing." */
  63. #define raw_malloc malloc
  64. #define raw_realloc realloc
  65. #define raw_free free
  66. #define raw_strdup strdup
  67. /* Helper macro: free a variable of type 'typename' using freefn, and
  68. * set the variable to NULL.
  69. */
  70. #define FREE_AND_NULL(typename, freefn, var) \
  71. do { \
  72. /* only evaluate (var) once. */ \
  73. typename **tmp__free__ptr ## freefn = &(var); \
  74. freefn(*tmp__free__ptr ## freefn); \
  75. (*tmp__free__ptr ## freefn) = NULL; \
  76. } while (0)
  77. #ifdef UTIL_MALLOC_PRIVATE
  78. STATIC int size_mul_check(const size_t x, const size_t y);
  79. #endif
  80. #endif /* !defined(TOR_UTIL_MALLOC_H) */