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