From 94db8f32e46925c3a64def751e5d2efbddbdf417 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 17 Nov 2017 11:46:25 -0500 Subject: Make all the free() functions from container.h clear their targets --- src/common/container.c | 6 +++--- src/common/container.h | 27 +++++++++++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) (limited to 'src/common') diff --git a/src/common/container.c b/src/common/container.c index 8645cb4826..a6e3c11f35 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; @@ -1335,7 +1335,7 @@ digest256map_assign_key(digest256map_entry_t *ent, const uint8_t *key) * those entries. If free_val is provided, invoked it every value in \ * map. */ \ 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 +1525,7 @@ digestset_new(int max_elements) /** Free all storage held in set. */ 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..95dfa4c90f 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, (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 map 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 ba. */ static inline void -bitarray_free(bitarray_t *ba) +bitarray_free_(bitarray_t *ba) { tor_free(ba); } +#define bitarray_free(ba) FREE_AND_NULL(bitarray, (ba)) + /** Set the bitth bit in 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, (set)) /* These functions, given an array of n_elements, return the * nth lowest element. nth=0 gives the lowest element; -- cgit v1.2.3-54-g00ecf