summaryrefslogtreecommitdiff
path: root/src/common/util.h
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-12-08 14:58:43 -0500
committerNick Mathewson <nickm@torproject.org>2017-12-08 14:58:43 -0500
commit5ee0cccd49e57fad8c810e817d912d4a61dbc96c (patch)
tree2aa4d71a332805bcb1bf7746887e7435ffdc51f4 /src/common/util.h
parent44010c6fc11608b13924372a179825946672cb23 (diff)
parent7ca5f4bf0365b853cdb0bab5cc9cb0ce6deaec26 (diff)
downloadtor-5ee0cccd49e57fad8c810e817d912d4a61dbc96c.tar.gz
tor-5ee0cccd49e57fad8c810e817d912d4a61dbc96c.zip
Merge branch 'macro_free_v2_squashed'
Diffstat (limited to 'src/common/util.h')
-rw-r--r--src/common/util.h25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/common/util.h b/src/common/util.h
index 6bc853da26..8dc64ce9fa 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -80,12 +80,22 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
* This is a macro. If you need a function pointer to release memory from
* tor_malloc(), use tor_free_().
*/
+#ifdef __GNUC__
+#define tor_free(p) STMT_BEGIN \
+ typeof(&(p)) tor_free__tmpvar = &(p); \
+ if (PREDICT_LIKELY((*tor_free__tmpvar)!=NULL)) { \
+ raw_free(*tor_free__tmpvar); \
+ *tor_free__tmpvar=NULL; \
+ } \
+ STMT_END
+#else
#define tor_free(p) STMT_BEGIN \
if (PREDICT_LIKELY((p)!=NULL)) { \
raw_free(p); \
(p)=NULL; \
} \
STMT_END
+#endif
#endif /* defined(USE_DMALLOC) */
#define tor_malloc(size) tor_malloc_(size DMALLOC_ARGS)
@@ -109,6 +119,17 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
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>
@@ -423,7 +444,9 @@ struct process_environment_t {
};
process_environment_t *process_environment_make(struct smartlist_t *env_vars);
-void process_environment_free(process_environment_t *env);
+void process_environment_free_(process_environment_t *env);
+#define process_environment_free(env) \
+ FREE_AND_NULL(process_environment_t, process_environment_free_, (env))
struct smartlist_t *get_current_process_environment_variables(void);