summaryrefslogtreecommitdiff
path: root/src/common
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
parent44010c6fc11608b13924372a179825946672cb23 (diff)
parent7ca5f4bf0365b853cdb0bab5cc9cb0ce6deaec26 (diff)
downloadtor-5ee0cccd49e57fad8c810e817d912d4a61dbc96c.tar.gz
tor-5ee0cccd49e57fad8c810e817d912d4a61dbc96c.zip
Merge branch 'macro_free_v2_squashed'
Diffstat (limited to 'src/common')
-rw-r--r--src/common/address.c6
-rw-r--r--src/common/address.h13
-rw-r--r--src/common/aes.c4
-rw-r--r--src/common/aes.h4
-rw-r--r--src/common/buffers.c2
-rw-r--r--src/common/buffers.h3
-rw-r--r--src/common/compat.c5
-rw-r--r--src/common/compat_libevent.c4
-rw-r--r--src/common/compat_libevent.h8
-rw-r--r--src/common/compat_threads.c4
-rw-r--r--src/common/compat_threads.h6
-rw-r--r--src/common/compress.c2
-rw-r--r--src/common/compress.h4
-rw-r--r--src/common/compress_lzma.c2
-rw-r--r--src/common/compress_lzma.h5
-rw-r--r--src/common/compress_zlib.c2
-rw-r--r--src/common/compress_zlib.h5
-rw-r--r--src/common/compress_zstd.c2
-rw-r--r--src/common/compress_zstd.h5
-rw-r--r--src/common/confline.c2
-rw-r--r--src/common/confline.h7
-rw-r--r--src/common/container.c19
-rw-r--r--src/common/container.h27
-rw-r--r--src/common/crypto.c10
-rw-r--r--src/common/crypto.h19
-rw-r--r--src/common/crypto_ed25519.c2
-rw-r--r--src/common/crypto_ed25519.h5
-rw-r--r--src/common/di_ops.c2
-rw-r--r--src/common/di_ops.h7
-rw-r--r--src/common/handles.h4
-rw-r--r--src/common/log.c11
-rw-r--r--src/common/memarea.c2
-rw-r--r--src/common/memarea.h7
-rw-r--r--src/common/procmon.c2
-rw-r--r--src/common/procmon.h4
-rw-r--r--src/common/sandbox.c6
-rw-r--r--src/common/storagedir.c2
-rw-r--r--src/common/storagedir.h5
-rw-r--r--src/common/timers.c2
-rw-r--r--src/common/timers.h3
-rw-r--r--src/common/tortls.c4
-rw-r--r--src/common/tortls.h7
-rw-r--r--src/common/util.c2
-rw-r--r--src/common/util.h25
-rw-r--r--src/common/workqueue.c5
45 files changed, 195 insertions, 82 deletions
diff --git a/src/common/address.c b/src/common/address.c
index dbe129be59..0c0ba782ae 100644
--- a/src/common/address.c
+++ b/src/common/address.c
@@ -1759,14 +1759,14 @@ get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr))
break;
} SMARTLIST_FOREACH_END(a);
- free_interface_address6_list(addrs);
+ interface_address6_list_free(addrs);
return rv;
}
/** Free a smartlist of IP addresses returned by get_interface_address6_list.
*/
void
-free_interface_address6_list(smartlist_t *addrs)
+interface_address6_list_free_(smartlist_t *addrs)
{
if (addrs != NULL) {
SMARTLIST_FOREACH(addrs, tor_addr_t *, a, tor_free(a));
@@ -1781,7 +1781,7 @@ free_interface_address6_list(smartlist_t *addrs)
* An empty smartlist means that there are no addresses of the selected type
* matching these criteria.
* Returns NULL on failure.
- * Use free_interface_address6_list to free the returned list.
+ * Use interface_address6_list_free to free the returned list.
*/
MOCK_IMPL(smartlist_t *,
get_interface_address6_list,(int severity,
diff --git a/src/common/address.h b/src/common/address.h
index c7f6935754..7607c76bae 100644
--- a/src/common/address.h
+++ b/src/common/address.h
@@ -206,7 +206,9 @@ const char * fmt_addr32(uint32_t addr);
MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family,
tor_addr_t *addr));
-void free_interface_address6_list(smartlist_t * addrs);
+void interface_address6_list_free_(smartlist_t * addrs);// XXXX
+#define interface_address6_list_free(addrs) \
+ FREE_AND_NULL(smartlist_t, interface_address6_list_free_, (addrs))
MOCK_DECL(smartlist_t *,get_interface_address6_list,(int severity,
sa_family_t family,
int include_internal));
@@ -321,13 +323,8 @@ int addr_mask_get_bits(uint32_t mask);
int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len);
char *tor_dup_ip(uint32_t addr) ATTR_MALLOC;
MOCK_DECL(int,get_interface_address,(int severity, uint32_t *addr));
-/** Free a smartlist of IP addresses returned by get_interface_address_list.
- */
-static inline void
-free_interface_address_list(smartlist_t *addrs)
-{
- free_interface_address6_list(addrs);
-}
+#define interface_address_list_free(lst)\
+ interface_address6_list_free(lst)
/** Return a smartlist of the IPv4 addresses of all interfaces on the server.
* Excludes loopback and multicast addresses. Only includes internal addresses
* if include_internal is true. (Note that a relay behind NAT may use an
diff --git a/src/common/aes.c b/src/common/aes.c
index df4368fdba..9b5b0197ea 100644
--- a/src/common/aes.c
+++ b/src/common/aes.c
@@ -110,7 +110,7 @@ aes_new_cipher(const uint8_t *key, const uint8_t *iv, int key_bits)
return (aes_cnt_cipher_t *) cipher;
}
void
-aes_cipher_free(aes_cnt_cipher_t *cipher_)
+aes_cipher_free_(aes_cnt_cipher_t *cipher_)
{
if (!cipher_)
return;
@@ -324,7 +324,7 @@ aes_set_key(aes_cnt_cipher_t *cipher, const uint8_t *key, int key_bits)
/** Release storage held by <b>cipher</b>
*/
void
-aes_cipher_free(aes_cnt_cipher_t *cipher)
+aes_cipher_free_(aes_cnt_cipher_t *cipher)
{
if (!cipher)
return;
diff --git a/src/common/aes.h b/src/common/aes.h
index 1e400a56e0..0b17cd55a4 100644
--- a/src/common/aes.h
+++ b/src/common/aes.h
@@ -17,7 +17,9 @@ typedef struct aes_cnt_cipher aes_cnt_cipher_t;
aes_cnt_cipher_t* aes_new_cipher(const uint8_t *key, const uint8_t *iv,
int key_bits);
-void aes_cipher_free(aes_cnt_cipher_t *cipher);
+void aes_cipher_free_(aes_cnt_cipher_t *cipher);
+#define aes_cipher_free(cipher) \
+ FREE_AND_NULL(aes_cnt_cipher_t, aes_cipher_free_, (cipher))
void aes_crypt_inplace(aes_cnt_cipher_t *cipher, char *data, size_t len);
int evaluate_evp_for_aes(int force_value);
diff --git a/src/common/buffers.c b/src/common/buffers.c
index e5d3664638..a01add9bef 100644
--- a/src/common/buffers.c
+++ b/src/common/buffers.c
@@ -409,7 +409,7 @@ buf_slack(const buf_t *buf)
/** Release storage held by <b>buf</b>. */
void
-buf_free(buf_t *buf)
+buf_free_(buf_t *buf)
{
if (!buf)
return;
diff --git a/src/common/buffers.h b/src/common/buffers.h
index 8e938eec38..22a5f7bfa3 100644
--- a/src/common/buffers.h
+++ b/src/common/buffers.h
@@ -24,7 +24,8 @@ struct tor_compress_state_t;
buf_t *buf_new(void);
buf_t *buf_new_with_capacity(size_t size);
size_t buf_get_default_chunk_size(const buf_t *buf);
-void buf_free(buf_t *buf);
+void buf_free_(buf_t *buf);
+#define buf_free(b) FREE_AND_NULL(buf_t, buf_free_, (b))
void buf_clear(buf_t *buf);
buf_t *buf_copy(const buf_t *buf);
diff --git a/src/common/compat.c b/src/common/compat.c
index 38693b21fe..b4bd6eba06 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1905,9 +1905,12 @@ tor_passwd_dup(const struct passwd *pw)
return new_pw;
}
+#define tor_passwd_free(pw) \
+ FREE_AND_NULL(struct passwd, tor_passwd_free_, (pw))
+
/** Helper: free one of our cached 'struct passwd' values. */
static void
-tor_passwd_free(struct passwd *pw)
+tor_passwd_free_(struct passwd *pw)
{
if (!pw)
return;
diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c
index 1c3a1b9f37..10489bf296 100644
--- a/src/common/compat_libevent.c
+++ b/src/common/compat_libevent.c
@@ -69,7 +69,7 @@ suppress_libevent_log_msg(const char *msg)
/* Wrapper for event_free() that tolerates tor_event_free(NULL) */
void
-tor_event_free(struct event *ev)
+tor_event_free_(struct event *ev)
{
if (ev == NULL)
return;
@@ -213,7 +213,7 @@ periodic_timer_new(struct event_base *base,
/** Stop and free a periodic timer */
void
-periodic_timer_free(periodic_timer_t *timer)
+periodic_timer_free_(periodic_timer_t *timer)
{
if (!timer)
return;
diff --git a/src/common/compat_libevent.h b/src/common/compat_libevent.h
index 3d8672fc63..0cdb73fbb9 100644
--- a/src/common/compat_libevent.h
+++ b/src/common/compat_libevent.h
@@ -19,7 +19,9 @@ void suppress_libevent_log_msg(const char *msg);
evdns_add_server_port_with_base(tor_libevent_get_base(), \
(sock),(tcp),(cb),(data));
-void tor_event_free(struct event *ev);
+void tor_event_free_(struct event *ev);
+#define tor_event_free(ev) \
+ FREE_AND_NULL(struct event, tor_event_free_, (ev))
typedef struct periodic_timer_t periodic_timer_t;
@@ -27,7 +29,9 @@ periodic_timer_t *periodic_timer_new(struct event_base *base,
const struct timeval *tv,
void (*cb)(periodic_timer_t *timer, void *data),
void *data);
-void periodic_timer_free(periodic_timer_t *);
+void periodic_timer_free_(periodic_timer_t *);
+#define periodic_timer_free(t) \
+ FREE_AND_NULL(periodic_timer_t, periodic_timer_free_, (t))
#define tor_event_base_loopexit event_base_loopexit
#define tor_event_base_loopbreak event_base_loopbreak
diff --git a/src/common/compat_threads.c b/src/common/compat_threads.c
index 6adcaab956..3171c4b2f2 100644
--- a/src/common/compat_threads.c
+++ b/src/common/compat_threads.c
@@ -48,7 +48,7 @@ tor_mutex_new_nonrecursive(void)
}
/** Release all storage and system resources held by <b>m</b>. */
void
-tor_mutex_free(tor_mutex_t *m)
+tor_mutex_free_(tor_mutex_t *m)
{
if (!m)
return;
@@ -68,7 +68,7 @@ tor_cond_new(void)
/** Free all storage held in <b>c</b>. */
void
-tor_cond_free(tor_cond_t *c)
+tor_cond_free_(tor_cond_t *c)
{
if (!c)
return;
diff --git a/src/common/compat_threads.h b/src/common/compat_threads.h
index ce86b15e9d..c93e601ec5 100644
--- a/src/common/compat_threads.h
+++ b/src/common/compat_threads.h
@@ -54,7 +54,8 @@ void tor_mutex_init(tor_mutex_t *m);
void tor_mutex_init_nonrecursive(tor_mutex_t *m);
void tor_mutex_acquire(tor_mutex_t *m);
void tor_mutex_release(tor_mutex_t *m);
-void tor_mutex_free(tor_mutex_t *m);
+void tor_mutex_free_(tor_mutex_t *m);
+#define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m))
void tor_mutex_uninit(tor_mutex_t *m);
unsigned long tor_get_thread_id(void);
void tor_threads_init(void);
@@ -81,7 +82,8 @@ typedef struct tor_cond_t {
} tor_cond_t;
tor_cond_t *tor_cond_new(void);
-void tor_cond_free(tor_cond_t *cond);
+void tor_cond_free_(tor_cond_t *cond);
+#define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c))
int tor_cond_init(tor_cond_t *cond);
void tor_cond_uninit(tor_cond_t *cond);
int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex,
diff --git a/src/common/compress.c b/src/common/compress.c
index bc12a58ad6..47c93cf6a9 100644
--- a/src/common/compress.c
+++ b/src/common/compress.c
@@ -598,7 +598,7 @@ tor_compress_process(tor_compress_state_t *state,
/** Deallocate <b>state</b>. */
void
-tor_compress_free(tor_compress_state_t *state)
+tor_compress_free_(tor_compress_state_t *state)
{
if (state == NULL)
return;
diff --git a/src/common/compress.h b/src/common/compress.h
index 23a9817479..952102bf97 100644
--- a/src/common/compress.h
+++ b/src/common/compress.h
@@ -80,7 +80,9 @@ tor_compress_output_t tor_compress_process(tor_compress_state_t *state,
char **out, size_t *out_len,
const char **in, size_t *in_len,
int finish);
-void tor_compress_free(tor_compress_state_t *state);
+void tor_compress_free_(tor_compress_state_t *state);
+#define tor_compress_free(st) \
+ FREE_AND_NULL(tor_compress_state_t, tor_compress_free_, (st))
size_t tor_compress_state_size(const tor_compress_state_t *state);
diff --git a/src/common/compress_lzma.c b/src/common/compress_lzma.c
index 6426ede4fd..051c59ba2d 100644
--- a/src/common/compress_lzma.c
+++ b/src/common/compress_lzma.c
@@ -323,7 +323,7 @@ tor_lzma_compress_process(tor_lzma_compress_state_t *state,
/** Deallocate <b>state</b>. */
void
-tor_lzma_compress_free(tor_lzma_compress_state_t *state)
+tor_lzma_compress_free_(tor_lzma_compress_state_t *state)
{
if (state == NULL)
return;
diff --git a/src/common/compress_lzma.h b/src/common/compress_lzma.h
index 7639d98a70..38a447c1f3 100644
--- a/src/common/compress_lzma.h
+++ b/src/common/compress_lzma.h
@@ -31,7 +31,10 @@ tor_lzma_compress_process(tor_lzma_compress_state_t *state,
const char **in, size_t *in_len,
int finish);
-void tor_lzma_compress_free(tor_lzma_compress_state_t *state);
+void tor_lzma_compress_free_(tor_lzma_compress_state_t *state);
+#define tor_lzma_compress_free(st) \
+ FREE_AND_NULL(tor_lzma_compress_state_t, \
+ tor_lzma_compress_free_, (st))
size_t tor_lzma_compress_state_size(const tor_lzma_compress_state_t *state);
diff --git a/src/common/compress_zlib.c b/src/common/compress_zlib.c
index 284542e885..23d71d27be 100644
--- a/src/common/compress_zlib.c
+++ b/src/common/compress_zlib.c
@@ -265,7 +265,7 @@ tor_zlib_compress_process(tor_zlib_compress_state_t *state,
/** Deallocate <b>state</b>. */
void
-tor_zlib_compress_free(tor_zlib_compress_state_t *state)
+tor_zlib_compress_free_(tor_zlib_compress_state_t *state)
{
if (state == NULL)
return;
diff --git a/src/common/compress_zlib.h b/src/common/compress_zlib.h
index 8ace467bf0..e3c1a2b339 100644
--- a/src/common/compress_zlib.h
+++ b/src/common/compress_zlib.h
@@ -31,7 +31,10 @@ tor_zlib_compress_process(tor_zlib_compress_state_t *state,
const char **in, size_t *in_len,
int finish);
-void tor_zlib_compress_free(tor_zlib_compress_state_t *state);
+void tor_zlib_compress_free_(tor_zlib_compress_state_t *state);
+#define tor_zlib_compress_free(st) \
+ FREE_AND_NULL(tor_zlib_compress_state_t, \
+ tor_zlib_compress_free_, (st))
size_t tor_zlib_compress_state_size(const tor_zlib_compress_state_t *state);
diff --git a/src/common/compress_zstd.c b/src/common/compress_zstd.c
index c1cdaf17ad..0db87d61b7 100644
--- a/src/common/compress_zstd.c
+++ b/src/common/compress_zstd.c
@@ -399,7 +399,7 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state,
/** Deallocate <b>state</b>. */
void
-tor_zstd_compress_free(tor_zstd_compress_state_t *state)
+tor_zstd_compress_free_(tor_zstd_compress_state_t *state)
{
if (state == NULL)
return;
diff --git a/src/common/compress_zstd.h b/src/common/compress_zstd.h
index 02466010ff..9bca24ded7 100644
--- a/src/common/compress_zstd.h
+++ b/src/common/compress_zstd.h
@@ -31,7 +31,10 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state,
const char **in, size_t *in_len,
int finish);
-void tor_zstd_compress_free(tor_zstd_compress_state_t *state);
+void tor_zstd_compress_free_(tor_zstd_compress_state_t *state);
+#define tor_zstd_compress_free(st) \
+ FREE_AND_NULL(tor_zstd_compress_state_t, \
+ tor_zstd_compress_free_, (st))
size_t tor_zstd_compress_state_size(const tor_zstd_compress_state_t *state);
diff --git a/src/common/confline.c b/src/common/confline.c
index 781ad2a12b..bf613ab742 100644
--- a/src/common/confline.c
+++ b/src/common/confline.c
@@ -330,7 +330,7 @@ config_process_include(const char *path, int recursion_level, int extended,
* Free all the configuration lines on the linked list <b>front</b>.
*/
void
-config_free_lines(config_line_t *front)
+config_free_lines_(config_line_t *front)
{
config_line_t *tmp;
diff --git a/src/common/confline.h b/src/common/confline.h
index feeb9f249d..772a9bbbdc 100644
--- a/src/common/confline.h
+++ b/src/common/confline.h
@@ -48,7 +48,12 @@ int config_get_lines(const char *string, config_line_t **result, int extended);
int config_get_lines_include(const char *string, config_line_t **result,
int extended, int *has_include,
smartlist_t *opened_lst);
-void config_free_lines(config_line_t *front);
+void config_free_lines_(config_line_t *front);
+#define config_free_lines(front) \
+ do { \
+ config_free_lines_(front); \
+ (front) = NULL; \
+ } while (0)
const char *parse_config_line_from_str_verbose(const char *line,
char **key_out, char **value_out,
const char **err_out);
diff --git a/src/common/container.c b/src/common/container.c
index 8645cb4826..54b0b2028f 100644
--- a/src/common/container.c
+++ b/src/common/container.c
@@ -42,7 +42,7 @@ smartlist_new,(void))
* list's elements.
*/
MOCK_IMPL(void,
-smartlist_free,(smartlist_t *sl))
+smartlist_free_,(smartlist_t *sl))
{
if (!sl)
return;
@@ -1163,19 +1163,26 @@ HT_GENERATE2(digest256map_impl, digest256map_entry_t, node,
digest256map_entry_hash,
digest256map_entries_eq, 0.6, tor_reallocarray_, tor_free_)
+#define strmap_entry_free(ent) \
+ FREE_AND_NULL(strmap_entry_t, strmap_entry_free_, (ent))
+#define digestmap_entry_free(ent) \
+ FREE_AND_NULL(digestmap_entry_t, digestmap_entry_free_, (ent))
+#define digest256map_entry_free(ent) \
+ FREE_AND_NULL(digest256map_entry_t, digest256map_entry_free_, (ent))
+
static inline void
-strmap_entry_free(strmap_entry_t *ent)
+strmap_entry_free_(strmap_entry_t *ent)
{
tor_free(ent->key);
tor_free(ent);
}
static inline void
-digestmap_entry_free(digestmap_entry_t *ent)
+digestmap_entry_free_(digestmap_entry_t *ent)
{
tor_free(ent);
}
static inline void
-digest256map_entry_free(digest256map_entry_t *ent)
+digest256map_entry_free_(digest256map_entry_t *ent)
{
tor_free(ent);
}
@@ -1335,7 +1342,7 @@ digest256map_assign_key(digest256map_entry_t *ent, const uint8_t *key)
* those entries. If free_val is provided, invoked it every value in \
* <b>map</b>. */ \
MOCK_IMPL(void, \
- prefix##_free, (maptype *map, void (*free_val)(void*))) \
+ prefix##_free_, (maptype *map, void (*free_val)(void*))) \
{ \
prefix##_entry_t **ent, **next, *this; \
if (!map) \
@@ -1525,7 +1532,7 @@ digestset_new(int max_elements)
/** Free all storage held in <b>set</b>. */
void
-digestset_free(digestset_t *set)
+digestset_free_(digestset_t *set)
{
if (!set)
return;
diff --git a/src/common/container.h b/src/common/container.h
index f6affd3bc6..5d2dce5416 100644
--- a/src/common/container.h
+++ b/src/common/container.h
@@ -28,7 +28,9 @@ typedef struct smartlist_t {
} smartlist_t;
MOCK_DECL(smartlist_t *, smartlist_new, (void));
-MOCK_DECL(void, smartlist_free, (smartlist_t *sl));
+MOCK_DECL(void, smartlist_free_, (smartlist_t *sl));
+#define smartlist_free(sl) FREE_AND_NULL(smartlist_t, smartlist_free_, (sl))
+
void smartlist_clear(smartlist_t *sl);
void smartlist_add(smartlist_t *sl, void *element);
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
@@ -350,7 +352,7 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join,
void* prefix##set(maptype *map, keytype key, void *val); \
void* prefix##get(const maptype *map, keytype key); \
void* prefix##remove(maptype *map, keytype key); \
- MOCK_DECL(void, prefix##free, (maptype *map, void (*free_val)(void*))); \
+ MOCK_DECL(void, prefix##free_, (maptype *map, void (*free_val)(void*))); \
int prefix##isempty(const maptype *map); \
int prefix##size(const maptype *map); \
prefix##iter_t *prefix##iter_init(maptype *map); \
@@ -368,6 +370,16 @@ DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
* table. */
DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
+#define MAP_FREE_AND_NULL(maptype, map, fn) \
+ do { \
+ maptype ## _free_((map), (fn)); \
+ (map) = NULL; \
+ } while (0)
+
+#define strmap_free(map, fn) MAP_FREE_AND_NULL(strmap, (map), (fn))
+#define digestmap_free(map, fn) MAP_FREE_AND_NULL(digestmap, (map), (fn))
+#define digest256map_free(map, fn) MAP_FREE_AND_NULL(digest256map, (map), (fn))
+
#undef DECLARE_MAP_FNS
/** Iterates over the key-value pairs in a map <b>map</b> in order.
@@ -528,9 +540,9 @@ void* strmap_remove_lc(strmap_t *map, const char *key);
return (valtype*)digestmap_remove((digestmap_t*)map, key); \
} \
ATTR_UNUSED static inline void \
- prefix##f##ree(maptype *map, void (*free_val)(void*)) \
+ prefix##f##ree_(maptype *map, void (*free_val)(void*)) \
{ \
- digestmap_free((digestmap_t*)map, free_val); \
+ digestmap_free_((digestmap_t*)map, free_val); \
} \
ATTR_UNUSED static inline int \
prefix##isempty(maptype *map) \
@@ -614,10 +626,12 @@ bitarray_expand(bitarray_t *ba,
}
/** Free the bit array <b>ba</b>. */
static inline void
-bitarray_free(bitarray_t *ba)
+bitarray_free_(bitarray_t *ba)
{
tor_free(ba);
}
+#define bitarray_free(ba) FREE_AND_NULL(bitarray_t, bitarray_free_, (ba))
+
/** Set the <b>bit</b>th bit in <b>b</b> to 1. */
static inline void
bitarray_set(bitarray_t *b, int bit)
@@ -679,7 +693,8 @@ digestset_contains(const digestset_t *set, const char *digest)
#undef BIT
digestset_t *digestset_new(int max_elements);
-void digestset_free(digestset_t* set);
+void digestset_free_(digestset_t* set);
+#define digestset_free(set) FREE_AND_NULL(digestset_t, digestset_free_, (set))
/* These functions, given an <b>array</b> of <b>n_elements</b>, return the
* <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
diff --git a/src/common/crypto.c b/src/common/crypto.c
index 380f038f42..575bfd0c06 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -529,7 +529,7 @@ crypto_pk_new,(void))
* are released, free the key.
*/
void
-crypto_pk_free(crypto_pk_t *env)
+crypto_pk_free_(crypto_pk_t *env)
{
if (!env)
return;
@@ -592,7 +592,7 @@ crypto_cipher_new(const char *key)
/** Free a symmetric cipher.
*/
void
-crypto_cipher_free(crypto_cipher_t *env)
+crypto_cipher_free_(crypto_cipher_t *env)
{
if (!env)
return;
@@ -1977,7 +1977,7 @@ crypto_digest512_new(digest_algorithm_t algorithm)
/** Deallocate a digest object.
*/
void
-crypto_digest_free(crypto_digest_t *digest)
+crypto_digest_free_(crypto_digest_t *digest)
{
if (!digest)
return;
@@ -2224,7 +2224,7 @@ crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len)
/** Cleanse and deallocate a XOF object. */
void
-crypto_xof_free(crypto_xof_t *xof)
+crypto_xof_free_(crypto_xof_t *xof)
{
if (!xof)
return;
@@ -2777,7 +2777,7 @@ crypto_expand_key_material_rfc5869_sha256(
/** Free a DH key exchange object.
*/
void
-crypto_dh_free(crypto_dh_t *dh)
+crypto_dh_free_(crypto_dh_t *dh)
{
if (!dh)
return;
diff --git a/src/common/crypto.h b/src/common/crypto.h
index f9aeeee2c0..eca115fa79 100644
--- a/src/common/crypto.h
+++ b/src/common/crypto.h
@@ -19,6 +19,7 @@
#include "torint.h"
#include "testsupport.h"
#include "compat.h"
+#include "util.h"
#include <openssl/engine.h>
#include "keccak-tiny/keccak-tiny.h"
@@ -146,7 +147,8 @@ int crypto_global_cleanup(void);
/* environment setup */
MOCK_DECL(crypto_pk_t *,crypto_pk_new,(void));
-void crypto_pk_free(crypto_pk_t *env);
+void crypto_pk_free_(crypto_pk_t *env);
+#define crypto_pk_free(pk) FREE_AND_NULL(crypto_pk_t, crypto_pk_free_, (pk))
void crypto_set_tls_dh_prime(void);
crypto_cipher_t *crypto_cipher_new(const char *key);
@@ -155,7 +157,9 @@ crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv);
crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
const uint8_t *iv,
int bits);
-void crypto_cipher_free(crypto_cipher_t *env);
+void crypto_cipher_free_(crypto_cipher_t *env);
+#define crypto_cipher_free(c) \
+ FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c))
/* public key crypto */
MOCK_DECL(int, crypto_pk_generate_key_with_bits,(crypto_pk_t *env, int bits));
@@ -258,7 +262,9 @@ int crypto_digest_algorithm_parse_name(const char *name);
crypto_digest_t *crypto_digest_new(void);
crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm);
crypto_digest_t *crypto_digest512_new(digest_algorithm_t algorithm);
-void crypto_digest_free(crypto_digest_t *digest);
+void crypto_digest_free_(crypto_digest_t *digest);
+#define crypto_digest_free(d) \
+ FREE_AND_NULL(crypto_digest_t, crypto_digest_free_, (d))
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data,
size_t len);
void crypto_digest_get_digest(crypto_digest_t *digest,
@@ -276,7 +282,9 @@ void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out,
crypto_xof_t *crypto_xof_new(void);
void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len);
void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len);
-void crypto_xof_free(crypto_xof_t *xof);
+void crypto_xof_free_(crypto_xof_t *xof);
+#define crypto_xof_free(xof) \
+ FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof))
/* Key negotiation */
#define DH_TYPE_CIRCUIT 1
@@ -291,7 +299,8 @@ int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out,
ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
const char *pubkey, size_t pubkey_len,
char *secret_out, size_t secret_out_len);
-void crypto_dh_free(crypto_dh_t *dh);
+void crypto_dh_free_(crypto_dh_t *dh);
+#define crypto_dh_free(dh) FREE_AND_NULL(crypto_dh_t, crypto_dh_free_, (dh))
int crypto_expand_key_material_TAP(const uint8_t *key_in,
size_t key_in_len,
diff --git a/src/common/crypto_ed25519.c b/src/common/crypto_ed25519.c
index 94b23e31b9..26523e3126 100644
--- a/src/common/crypto_ed25519.c
+++ b/src/common/crypto_ed25519.c
@@ -622,7 +622,7 @@ ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
/** Release all storage held for <b>kp</b>. */
void
-ed25519_keypair_free(ed25519_keypair_t *kp)
+ed25519_keypair_free_(ed25519_keypair_t *kp)
{
if (! kp)
return;
diff --git a/src/common/crypto_ed25519.h b/src/common/crypto_ed25519.h
index 8d13a487d6..74269ccffd 100644
--- a/src/common/crypto_ed25519.h
+++ b/src/common/crypto_ed25519.h
@@ -7,6 +7,7 @@
#include "testsupport.h"
#include "torint.h"
#include "crypto_curve25519.h"
+#include "util.h"
#define ED25519_PUBKEY_LEN 32
#define ED25519_SECKEY_LEN 64
@@ -117,7 +118,9 @@ int ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
char **tag_out,
const char *filename);
-void ed25519_keypair_free(ed25519_keypair_t *kp);
+void ed25519_keypair_free_(ed25519_keypair_t *kp);
+#define ed25519_keypair_free(kp) \
+ FREE_AND_NULL(ed25519_keypair_t, ed25519_keypair_free_, (kp))
int ed25519_pubkey_eq(const ed25519_public_key_t *key1,
const ed25519_public_key_t *key2);
diff --git a/src/common/di_ops.c b/src/common/di_ops.c
index 7c0b4e7630..90e9357c8e 100644
--- a/src/common/di_ops.c
+++ b/src/common/di_ops.c
@@ -148,7 +148,7 @@ struct di_digest256_map_t {
/** Release all storage held in <b>map</b>, calling free_fn on each value
* as we go. */
void
-dimap_free(di_digest256_map_t *map, dimap_free_fn free_fn)
+dimap_free_(di_digest256_map_t *map, dimap_free_fn free_fn)
{
while (map) {
di_digest256_map_t *victim = map;
diff --git a/src/common/di_ops.h b/src/common/di_ops.h
index e79973ba52..67d9c9f0df 100644
--- a/src/common/di_ops.h
+++ b/src/common/di_ops.h
@@ -37,7 +37,12 @@ int safe_mem_is_zero(const void *mem, size_t sz);
typedef struct di_digest256_map_t di_digest256_map_t;
typedef void (*dimap_free_fn)(void *);
-void dimap_free(di_digest256_map_t *map, dimap_free_fn free_fn);
+void dimap_free_(di_digest256_map_t *map, dimap_free_fn free_fn);
+#define dimap_free(map, free_fn) \
+ do { \
+ dimap_free_((map), (free_fn)); \
+ (map) = NULL; \
+ } while (0)
void dimap_add_entry(di_digest256_map_t **map,
const uint8_t *key, void *val);
void *dimap_search(const di_digest256_map_t *map, const uint8_t *key,
diff --git a/src/common/handles.h b/src/common/handles.h
index a610753a1c..aef8cd89ef 100644
--- a/src/common/handles.h
+++ b/src/common/handles.h
@@ -59,7 +59,7 @@
#define HANDLE_DECL(name, structname, linkage) \
typedef struct name ## _handle_t name ## _handle_t; \
linkage name ## _handle_t *name ## _handle_new(struct structname *object); \
- linkage void name ## _handle_free(name ## _handle_t *); \
+ linkage void name ## _handle_free_(name ## _handle_t *); \
linkage struct structname *name ## _handle_get(name ## _handle_t *); \
linkage void name ## _handles_clear(struct structname *object);
@@ -113,7 +113,7 @@
} \
\
linkage void \
- name ## _handle_free(struct name ## _handle_t *ref) \
+ name ## _handle_free_(struct name ## _handle_t *ref) \
{ \
if (! ref) return; \
name ## _handle_head_t *head = ref->head; \
diff --git a/src/common/log.c b/src/common/log.c
index 0becb5ce03..80055fda04 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -63,7 +63,9 @@ typedef struct logfile_t {
* log for each log domain? */
} logfile_t;
-static void log_free(logfile_t *victim);
+static void log_free_(logfile_t *victim);
+#define log_free(lg) \
+ FREE_AND_NULL(logfile_t, log_free_, (lg))
/** Helper: map a log severity to descriptive string. */
static inline const char *
@@ -385,9 +387,12 @@ pending_log_message_new(int severity, log_domain_mask_t domain,
return m;
}
+#define pending_log_message_free(msg) \
+ FREE_AND_NULL(pending_log_message_t, pending_log_message_free_, (msg))
+
/** Release all storage held by <b>msg</b>. */
static void
-pending_log_message_free(pending_log_message_t *msg)
+pending_log_message_free_(pending_log_message_t *msg)
{
if (!msg)
return;
@@ -721,7 +726,7 @@ log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain,
/** Free all storage held by <b>victim</b>. */
static void
-log_free(logfile_t *victim)
+log_free_(logfile_t *victim)
{
if (!victim)
return;
diff --git a/src/common/memarea.c b/src/common/memarea.c
index b059987e0e..fa7b69a43c 100644
--- a/src/common/memarea.c
+++ b/src/common/memarea.c
@@ -153,7 +153,7 @@ memarea_new(void)
/** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
* and friends for this area */
void
-memarea_drop_all(memarea_t *area)
+memarea_drop_all_(memarea_t *area)
{
memarea_chunk_t *chunk, *next;
for (chunk = area->first; chunk; chunk = next) {
diff --git a/src/common/memarea.h b/src/common/memarea.h
index c3d954e1ce..5207e8a5bd 100644
--- a/src/common/memarea.h
+++ b/src/common/memarea.h
@@ -8,7 +8,12 @@
typedef struct memarea_t memarea_t;
memarea_t *memarea_new(void);
-void memarea_drop_all(memarea_t *area);
+void memarea_drop_all_(memarea_t *area);
+#define memarea_drop_all(area) \
+ do { \
+ memarea_drop_all_(area); \
+ (area) = NULL; \
+ } while (0)
void memarea_clear(memarea_t *area);
int memarea_owns_ptr(const memarea_t *area, const void *ptr);
void *memarea_alloc(memarea_t *area, size_t sz);
diff --git a/src/common/procmon.c b/src/common/procmon.c
index 26c11823e8..abcbbeaa21 100644
--- a/src/common/procmon.c
+++ b/src/common/procmon.c
@@ -325,7 +325,7 @@ tor_process_monitor_poll_cb(evutil_socket_t unused1, short unused2,
/** Free the process-termination monitor <b>procmon</b>. */
void
-tor_process_monitor_free(tor_process_monitor_t *procmon)
+tor_process_monitor_free_(tor_process_monitor_t *procmon)
{
if (procmon == NULL)
return;
diff --git a/src/common/procmon.h b/src/common/procmon.h
index 10ead11ba8..63777e4111 100644
--- a/src/common/procmon.h
+++ b/src/common/procmon.h
@@ -27,7 +27,9 @@ tor_process_monitor_t *tor_process_monitor_new(struct event_base *base,
tor_procmon_callback_t cb,
void *cb_arg,
const char **msg);
-void tor_process_monitor_free(tor_process_monitor_t *procmon);
+void tor_process_monitor_free_(tor_process_monitor_t *procmon);
+#define tor_process_monitor_free(procmon) \
+ FREE_AND_NULL(tor_process_monitor_t, tor_process_monitor_free_, (procmon))
#endif /* !defined(TOR_PROCMON_H) */
diff --git a/src/common/sandbox.c b/src/common/sandbox.c
index aec9857e94..c5936f4030 100644
--- a/src/common/sandbox.c
+++ b/src/common/sandbox.c
@@ -1521,8 +1521,12 @@ cached_getaddrinfo_items_eq(const cached_getaddrinfo_item_t *a,
return (a->family == b->family) && 0 == strcmp(a->name, b->name);
}
+#define cached_getaddrinfo_item_free(item) \
+ FREE_AND_NULL(cached_getaddrinfo_item_t, \
+ cached_getaddrinfo_item_free_, (item))
+
static void
-cached_getaddrinfo_item_free(cached_getaddrinfo_item_t *item)
+cached_getaddrinfo_item_free_(cached_getaddrinfo_item_t *item)
{
if (item == NULL)
return;
diff --git a/src/common/storagedir.c b/src/common/storagedir.c
index c471ea911f..e2c7b4bb87 100644
--- a/src/common/storagedir.c
+++ b/src/common/storagedir.c
@@ -59,7 +59,7 @@ storage_dir_new(const char *dirname, int max_files)
* Drop all in-RAM storage for <b>d</b>. Does not delete any files.
*/
void
-storage_dir_free(storage_dir_t *d)
+storage_dir_free_(storage_dir_t *d)
{
if (d == NULL)
return;
diff --git a/src/common/storagedir.h b/src/common/storagedir.h
index 3de0afc361..d99bd7ec52 100644
--- a/src/common/storagedir.h
+++ b/src/common/storagedir.h
@@ -9,7 +9,10 @@ struct config_line_t;
struct sandbox_cfg_elem;
storage_dir_t * storage_dir_new(const char *dirname, int n_files);
-void storage_dir_free(storage_dir_t *d);
+void storage_dir_free_(storage_dir_t *d);
+#define storage_dir_free(d) \
+ FREE_AND_NULL(storage_dir_t, storage_dir_free_, (d))
+
int storage_dir_register_with_sandbox(storage_dir_t *d,
struct sandbox_cfg_elem **cfg);
const smartlist_t *storage_dir_list(storage_dir_t *d);
diff --git a/src/common/timers.c b/src/common/timers.c
index c8e09414f4..93cde7de5f 100644
--- a/src/common/timers.c
+++ b/src/common/timers.c
@@ -245,7 +245,7 @@ timer_new(timer_cb_fn_t cb, void *arg)
* scheduled.
*/
void
-timer_free(tor_timer_t *t)
+timer_free_(tor_timer_t *t)
{
if (! t)
return;
diff --git a/src/common/timers.h b/src/common/timers.h
index d4d4fb00a9..6d27f3e01e 100644
--- a/src/common/timers.h
+++ b/src/common/timers.h
@@ -17,7 +17,8 @@ void timer_get_cb(const tor_timer_t *t,
timer_cb_fn_t *cb_out, void **arg_out);
void timer_schedule(tor_timer_t *t, const struct timeval *delay);
void timer_disable(tor_timer_t *t);
-void timer_free(tor_timer_t *t);
+void timer_free_(tor_timer_t *t);
+#define timer_free(t) FREE_AND_NULL(tor_timer_t, timer_free_, (t))
void timers_initialize(void);
void timers_shutdown(void);
diff --git a/src/common/tortls.c b/src/common/tortls.c
index 197c5e8d3b..407603248f 100644
--- a/src/common/tortls.c
+++ b/src/common/tortls.c
@@ -644,7 +644,7 @@ static const char CLIENT_CIPHER_LIST[] =
/** Free all storage held in <b>cert</b> */
void
-tor_x509_cert_free(tor_x509_cert_t *cert)
+tor_x509_cert_free_(tor_x509_cert_t *cert)
{
if (! cert)
return;
@@ -1792,7 +1792,7 @@ tor_tls_is_server(tor_tls_t *tls)
* underlying file descriptor.
*/
void
-tor_tls_free(tor_tls_t *tls)
+tor_tls_free_(tor_tls_t *tls)
{
if (!tls)
return;
diff --git a/src/common/tortls.h b/src/common/tortls.h
index 6145f7dbc9..1dbf0b332f 100644
--- a/src/common/tortls.h
+++ b/src/common/tortls.h
@@ -216,7 +216,8 @@ void tor_tls_set_renegotiate_callback(tor_tls_t *tls,
void (*cb)(tor_tls_t *, void *arg),
void *arg);
int tor_tls_is_server(tor_tls_t *tls);
-void tor_tls_free(tor_tls_t *tls);
+void tor_tls_free_(tor_tls_t *tls);
+#define tor_tls_free(tls) FREE_AND_NULL(tor_tls_t, tor_tls_free_, (tls))
int tor_tls_peer_has_cert(tor_tls_t *tls);
MOCK_DECL(tor_x509_cert_t *,tor_tls_get_peer_cert,(tor_tls_t *tls));
MOCK_DECL(tor_x509_cert_t *,tor_tls_get_own_cert,(tor_tls_t *tls));
@@ -263,7 +264,9 @@ void check_no_tls_errors_(const char *fname, int line);
void tor_tls_log_one_error(tor_tls_t *tls, unsigned long err,
int severity, int domain, const char *doing);
-void tor_x509_cert_free(tor_x509_cert_t *cert);
+void tor_x509_cert_free_(tor_x509_cert_t *cert);
+#define tor_x509_cert_free(c) \
+ FREE_AND_NULL(tor_x509_cert_t, tor_x509_cert_free_, (c))
tor_x509_cert_t *tor_x509_cert_decode(const uint8_t *certificate,
size_t certificate_len);
void tor_x509_cert_get_der(const tor_x509_cert_t *cert,
diff --git a/src/common/util.c b/src/common/util.c
index 7dc5e8144d..67e97811c6 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -4713,7 +4713,7 @@ environment_variable_names_equal(const char *s1, const char *s2)
/** Free <b>env</b> (assuming it was produced by
* process_environment_make). */
void
-process_environment_free(process_environment_t *env)
+process_environment_free_(process_environment_t *env)
{
if (env == NULL) return;
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);
diff --git a/src/common/workqueue.c b/src/common/workqueue.c
index 42723224d3..ec96959b7d 100644
--- a/src/common/workqueue.c
+++ b/src/common/workqueue.c
@@ -148,12 +148,15 @@ workqueue_entry_new(workqueue_reply_t (*fn)(void*, void*),
return ent;
}
+#define workqueue_entry_free(ent) \
+ FREE_AND_NULL(workqueue_entry_t, workqueue_entry_free_, (ent))
+
/**
* Release all storage held in <b>ent</b>. Call only when <b>ent</b> is not on
* any queue.
*/
static void
-workqueue_entry_free(workqueue_entry_t *ent)
+workqueue_entry_free_(workqueue_entry_t *ent)
{
if (!ent)
return;