tor  0.4.1.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-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
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 
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
52 #define tor_free(p) STMT_BEGIN \
53  raw_free(p); \
54  (p)=NULL; \
55  STMT_END
56 #endif
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) */
void * tor_reallocarray_(void *ptr, size_t size1, size_t size2)
Definition: malloc.c:146
void * tor_memdup_(const void *mem, size_t len) ATTR_MALLOC
Definition: malloc.c:200
void * tor_malloc_(size_t size) ATTR_MALLOC
Definition: malloc.c:32
void * tor_realloc_(void *ptr, size_t size)
Definition: malloc.c:118
void tor_free_(void *mem)
Definition: malloc.c:227
STATIC int size_mul_check(const size_t x, const size_t y)
Definition: malloc.c:86
char * tor_strdup_(const char *s) ATTR_MALLOC
Definition: malloc.c:160
Utility macros to handle different features and behavior in different compilers.
char * tor_strndup_(const char *s, size_t n) ATTR_MALLOC
Definition: malloc.c:182
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_memdup_nulterm_(const void *mem, size_t len) ATTR_MALLOC
Definition: malloc.c:213