summaryrefslogtreecommitdiff
path: root/src/common/util.h
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-06-21 15:17:54 -0400
committerNick Mathewson <nickm@torproject.org>2018-06-21 15:20:01 -0400
commite066966bf48b4f6ca557175cb2ab3e2dffa5b1db (patch)
tree7cd9b093bddd06349629426fe3a404cb4947b14c /src/common/util.h
parenteb784aa9eaced1adb436d752ccdbeb2ff19c9e23 (diff)
downloadtor-e066966bf48b4f6ca557175cb2ab3e2dffa5b1db.tar.gz
tor-e066966bf48b4f6ca557175cb2ab3e2dffa5b1db.zip
Extract tor_malloc and friends to a new module.
Diffstat (limited to 'src/common/util.h')
-rw-r--r--src/common/util.h71
1 files changed, 1 insertions, 70 deletions
diff --git a/src/common/util.h b/src/common/util.h
index d1528886c8..bc29ddbe2a 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -23,6 +23,7 @@
#include <sys/stat.h>
#endif
#include "lib/err/torerr.h"
+#include "lib/malloc/util_malloc.h"
#include "common/util_bug.h"
#ifndef O_BINARY
@@ -35,79 +36,11 @@
#define O_NOFOLLOW 0
#endif
-/* Memory management */
-void *tor_malloc_(size_t size) ATTR_MALLOC;
-void *tor_malloc_zero_(size_t size) ATTR_MALLOC;
-void *tor_calloc_(size_t nmemb, size_t size) ATTR_MALLOC;
-void *tor_realloc_(void *ptr, size_t size);
-void *tor_reallocarray_(void *ptr, size_t size1, size_t size2);
-char *tor_strdup_(const char *s) ATTR_MALLOC ATTR_NONNULL((1));
-char *tor_strndup_(const char *s, size_t n)
- ATTR_MALLOC ATTR_NONNULL((1));
-void *tor_memdup_(const void *mem, size_t len)
- ATTR_MALLOC ATTR_NONNULL((1));
-void *tor_memdup_nulterm_(const void *mem, size_t len)
- ATTR_MALLOC ATTR_NONNULL((1));
-void tor_free_(void *mem);
uint64_t tor_htonll(uint64_t a);
uint64_t tor_ntohll(uint64_t a);
-/** Release memory allocated by tor_malloc, tor_realloc, tor_strdup,
- * etc. Unlike the free() function, the tor_free() macro sets the
- * pointer value to NULL after freeing it.
- *
- * This is a macro. If you need a function pointer to release memory from
- * tor_malloc(), use tor_free_().
- *
- * Note that this macro takes the address of the pointer it is going to
- * free and clear. If that pointer is stored with a nonstandard
- * alignment (eg because of a "packed" pragma) it is not correct to use
- * tor_free().
- */
-#ifdef __GNUC__
-#define tor_free(p) STMT_BEGIN \
- typeof(&(p)) tor_free__tmpvar = &(p); \
- raw_free(*tor_free__tmpvar); \
- *tor_free__tmpvar=NULL; \
- STMT_END
-#else
-#define tor_free(p) STMT_BEGIN \
- raw_free(p); \
- (p)=NULL; \
- STMT_END
-#endif
-
-#define tor_malloc(size) tor_malloc_(size)
-#define tor_malloc_zero(size) tor_malloc_zero_(size)
-#define tor_calloc(nmemb,size) tor_calloc_(nmemb, size)
-#define tor_realloc(ptr, size) tor_realloc_(ptr, size)
-#define tor_reallocarray(ptr, sz1, sz2) \
- tor_reallocarray_((ptr), (sz1), (sz2))
-#define tor_strdup(s) tor_strdup_(s)
-#define tor_strndup(s, n) tor_strndup_(s, n)
-#define tor_memdup(s, n) tor_memdup_(s, n)
-#define tor_memdup_nulterm(s, n) tor_memdup_nulterm_(s, n)
-
-/* Aliases for the underlying system malloc/realloc/free. Only use
- * them to indicate "I really want the underlying system function, I know
- * what I'm doing." */
-#define raw_malloc malloc
-#define raw_realloc realloc
-#define raw_free free
-#define raw_strdup strdup
void tor_log_mallinfo(int severity);
-/* Helper macro: free a variable of type 'typename' using freefn, and
- * set the variable to NULL.
- */
-#define FREE_AND_NULL(typename, freefn, var) \
- do { \
- /* only evaluate (var) once. */ \
- typename **tmp__free__ptr ## freefn = &(var); \
- freefn(*tmp__free__ptr ## freefn); \
- (*tmp__free__ptr ## freefn) = NULL; \
- } while (0)
-
/** Macro: yield a pointer to the field at position <b>off</b> within the
* structure <b>st</b>. Example:
* <pre>
@@ -538,8 +471,6 @@ STATIC int format_helper_exit_status(unsigned char child_state,
#endif /* defined(UTIL_PRIVATE) */
-int size_mul_check(const size_t x, const size_t y);
-
#define ARRAY_LENGTH(x) ((sizeof(x)) / sizeof(x[0]))
#endif /* !defined(TOR_UTIL_H) */