malloc.h 3.4 KB

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