Tor  0.4.7.0-alpha-dev
malloc.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file malloc.h
8  * \brief Headers for util_malloc.c
9  **/
10 
11 #ifndef TOR_UTIL_MALLOC_H
12 #define TOR_UTIL_MALLOC_H
13 
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include "lib/cc/compat_compiler.h"
17 
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);
32 
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.
36  *
37  * This is a macro. If you need a function pointer to release memory from
38  * tor_malloc(), use tor_free_().
39  *
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().
44  */
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__) */
57 
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)
68 
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
76 
77 /* Helper macro: free a variable of type 'typename' using freefn, and
78  * set the variable to NULL.
79  */
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)
87 
88 #ifdef UTIL_MALLOC_PRIVATE
89 STATIC int size_mul_check(const size_t x, const size_t y);
90 #endif
91 
92 #endif /* !defined(TOR_UTIL_MALLOC_H) */
Utility macros to handle different features and behavior in different compilers.
STATIC int size_mul_check(const size_t x, const size_t y)
Definition: malloc.c:86
void * tor_memdup_(const void *mem, size_t len) ATTR_MALLOC
Definition: malloc.c:200
void * tor_reallocarray_(void *ptr, size_t size1, size_t size2)
Definition: malloc.c:146
void tor_free_(void *mem)
Definition: malloc.c:227
void * tor_realloc_(void *ptr, size_t size)
Definition: malloc.c:118
void * tor_memdup_nulterm_(const void *mem, size_t len) ATTR_MALLOC
Definition: malloc.c:213
char * tor_strndup_(const char *s, size_t n) ATTR_MALLOC
Definition: malloc.c:182
char * tor_strdup_(const char *s) ATTR_MALLOC
Definition: malloc.c:160
void * tor_calloc_(size_t nmemb, size_t size) ATTR_MALLOC
Definition: malloc.c:107
void * tor_malloc_zero_(size_t size) ATTR_MALLOC
Definition: malloc.c:63
void * tor_malloc_(size_t size) ATTR_MALLOC
Definition: malloc.c:32
#define STATIC
Definition: testsupport.h:32