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