maps: do not include _ as part of expected prefix in macros.

Doing this makes our macro usage a little clear IMO, and also avoids
having to use an unadorned "new" within a macro.  (Clang-format
seems to think that "new" means we're doing C++, and so it generates
some output that checkSpace.pl doesn't care for.)
This commit is contained in:
Nick Mathewson 2020-01-10 14:07:11 -05:00
parent 87b71a692a
commit 6104c407e0
3 changed files with 60 additions and 60 deletions

View File

@ -46,7 +46,7 @@
#include "feature/nodelist/networkstatus_voter_info_st.h" #include "feature/nodelist/networkstatus_voter_info_st.h"
#include "feature/nodelist/node_st.h" #include "feature/nodelist/node_st.h"
DECLARE_TYPED_DIGESTMAP_FNS(dsmap_, digest_ds_map_t, download_status_t) DECLARE_TYPED_DIGESTMAP_FNS(dsmap, digest_ds_map_t, download_status_t)
#define DSMAP_FOREACH(map, keyvar, valvar) \ #define DSMAP_FOREACH(map, keyvar, valvar) \
DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \ DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \
valvar) valvar)

View File

@ -117,9 +117,9 @@
/* Typed wrappers for different digestmap types; used to avoid type /* Typed wrappers for different digestmap types; used to avoid type
* confusion. */ * confusion. */
DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t) DECLARE_TYPED_DIGESTMAP_FNS(sdmap, digest_sd_map_t, signed_descriptor_t)
DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t) DECLARE_TYPED_DIGESTMAP_FNS(rimap, digest_ri_map_t, routerinfo_t)
DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t) DECLARE_TYPED_DIGESTMAP_FNS(eimap, digest_ei_map_t, extrainfo_t)
#define SDMAP_FOREACH(map, keyvar, valvar) \ #define SDMAP_FOREACH(map, keyvar, valvar) \
DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \ DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
valvar) valvar)

View File

@ -19,29 +19,29 @@
#define DECLARE_MAP_FNS(mapname_t, keytype, prefix) \ #define DECLARE_MAP_FNS(mapname_t, keytype, prefix) \
typedef struct mapname_t mapname_t; \ typedef struct mapname_t mapname_t; \
typedef struct prefix##entry_t *prefix##iter_t; \ typedef struct prefix##_entry_t *prefix##_iter_t; \
MOCK_DECL(mapname_t*, prefix##new, (void)); \ MOCK_DECL(mapname_t*, prefix##_new, (void)); \
void* prefix##set(mapname_t *map, keytype key, void *val); \ void* prefix##_set(mapname_t *map, keytype key, void *val); \
void* prefix##get(const mapname_t *map, keytype key); \ void* prefix##_get(const mapname_t *map, keytype key); \
void* prefix##remove(mapname_t *map, keytype key); \ void* prefix##_remove(mapname_t *map, keytype key); \
MOCK_DECL(void, prefix##free_, (mapname_t *map, void (*free_val)(void*))); \ MOCK_DECL(void, prefix##_free_, (mapname_t *map, void (*free_val)(void*))); \
int prefix##isempty(const mapname_t *map); \ int prefix##_isempty(const mapname_t *map); \
int prefix##size(const mapname_t *map); \ int prefix##_size(const mapname_t *map); \
prefix##iter_t *prefix##iter_init(mapname_t *map); \ prefix##_iter_t *prefix##_iter_init(mapname_t *map); \
prefix##iter_t *prefix##iter_next(mapname_t *map, prefix##iter_t *iter); \ prefix##_iter_t *prefix##_iter_next(mapname_t *map, prefix##_iter_t *iter); \
prefix##iter_t *prefix##iter_next_rmv(mapname_t *map, \ prefix##_iter_t *prefix##_iter_next_rmv(mapname_t *map, \
prefix##iter_t *iter); \ prefix##_iter_t *iter); \
void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \ void prefix##_iter_get(prefix##_iter_t *iter, keytype *keyp, void **valp); \
int prefix##iter_done(prefix##iter_t *iter); \ int prefix##_iter_done(prefix##_iter_t *iter); \
void prefix##assert_ok(const mapname_t *map) void prefix##_assert_ok(const mapname_t *map)
/* Map from const char * to void *. Implemented with a hash table. */ /* Map from const char * to void *. Implemented with a hash table. */
DECLARE_MAP_FNS(strmap_t, const char *, strmap_); DECLARE_MAP_FNS(strmap_t, const char *, strmap);
/* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */ /* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_); DECLARE_MAP_FNS(digestmap_t, const char *, digestmap);
/* Map from const uint8_t[DIGEST256_LEN] to void *. Implemented with a hash /* Map from const uint8_t[DIGEST256_LEN] to void *. Implemented with a hash
* table. */ * table. */
DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_); DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map);
#define MAP_FREE_AND_NULL(mapname_t, map, fn) \ #define MAP_FREE_AND_NULL(mapname_t, map, fn) \
do { \ do { \
@ -56,12 +56,12 @@ DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
#undef DECLARE_MAP_FNS #undef DECLARE_MAP_FNS
/** Iterates over the key-value pairs in a map <b>map</b> in order. /** Iterates over the key-value pairs in a map <b>map</b> in order.
* <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap_ or digestmap_). * <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap or digestmap).
* The map's keys and values are of type keytype and valtype respectively; * The map's keys and values are of type keytype and valtype respectively;
* each iteration assigns them to keyvar and valvar. * each iteration assigns them to keyvar and valvar.
* *
* Example use: * Example use:
* MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) { * MAP_FOREACH(digestmap, m, const char *, k, routerinfo_t *, r) {
* // use k and r * // use k and r
* } MAP_FOREACH_END. * } MAP_FOREACH_END.
*/ */
@ -81,21 +81,21 @@ DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
*/ */
#define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \ #define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
STMT_BEGIN \ STMT_BEGIN \
prefix##iter_t *keyvar##_iter; \ prefix##_iter_t *keyvar##_iter; \
for (keyvar##_iter = prefix##iter_init(map); \ for (keyvar##_iter = prefix##_iter_init(map); \
!prefix##iter_done(keyvar##_iter); \ !prefix##_iter_done(keyvar##_iter); \
keyvar##_iter = prefix##iter_next(map, keyvar##_iter)) { \ keyvar##_iter = prefix##_iter_next(map, keyvar##_iter)) { \
keytype keyvar; \ keytype keyvar; \
void *valvar##_voidp; \ void *valvar##_voidp; \
valtype valvar; \ valtype valvar; \
prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \ prefix##_iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
valvar = valvar##_voidp; valvar = valvar##_voidp;
/** As MAP_FOREACH, except allows members to be removed from the map /** As MAP_FOREACH, except allows members to be removed from the map
* during the iteration via MAP_DEL_CURRENT. Example use: * during the iteration via MAP_DEL_CURRENT. Example use:
* *
* Example use: * Example use:
* MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) { * MAP_FOREACH(digestmap, m, const char *, k, routerinfo_t *, r) {
* if (is_very_old(r)) * if (is_very_old(r))
* MAP_DEL_CURRENT(k); * MAP_DEL_CURRENT(k);
* } MAP_FOREACH_END. * } MAP_FOREACH_END.
@ -121,18 +121,18 @@ DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
*/ */
#define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \ #define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
STMT_BEGIN \ STMT_BEGIN \
prefix##iter_t *keyvar##_iter; \ prefix##_iter_t *keyvar##_iter; \
int keyvar##_del=0; \ int keyvar##_del=0; \
for (keyvar##_iter = prefix##iter_init(map); \ for (keyvar##_iter = prefix##_iter_init(map); \
!prefix##iter_done(keyvar##_iter); \ !prefix##_iter_done(keyvar##_iter); \
keyvar##_iter = keyvar##_del ? \ keyvar##_iter = keyvar##_del ? \
prefix##iter_next_rmv(map, keyvar##_iter) : \ prefix##_iter_next_rmv(map, keyvar##_iter) : \
prefix##iter_next(map, keyvar##_iter)) { \ prefix##_iter_next(map, keyvar##_iter)) { \
keytype keyvar; \ keytype keyvar; \
void *valvar##_voidp; \ void *valvar##_voidp; \
valtype valvar; \ valtype valvar; \
keyvar##_del=0; \ keyvar##_del=0; \
prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \ prefix##_iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
valvar = valvar##_voidp; valvar = valvar##_voidp;
/** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon /** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon
@ -152,7 +152,7 @@ DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
* } DIGESTMAP_FOREACH_END. * } DIGESTMAP_FOREACH_END.
*/ */
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \ #define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar) MAP_FOREACH(digestmap, map, const char *, keyvar, valtype, valvar)
/** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or /** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or
* keytype. * keytype.
@ -163,21 +163,21 @@ DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
* } DIGESTMAP_FOREACH_END. * } DIGESTMAP_FOREACH_END.
*/ */
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \ #define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar) MAP_FOREACH_MODIFY(digestmap, map, const char *, keyvar, valtype, valvar)
/** Used to end a DIGESTMAP_FOREACH() block. */ /** Used to end a DIGESTMAP_FOREACH() block. */
#define DIGESTMAP_FOREACH_END MAP_FOREACH_END #define DIGESTMAP_FOREACH_END MAP_FOREACH_END
#define DIGEST256MAP_FOREACH(map, keyvar, valtype, valvar) \ #define DIGEST256MAP_FOREACH(map, keyvar, valtype, valvar) \
MAP_FOREACH(digest256map_, map, const uint8_t *, keyvar, valtype, valvar) MAP_FOREACH(digest256map, map, const uint8_t *, keyvar, valtype, valvar)
#define DIGEST256MAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \ #define DIGEST256MAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
MAP_FOREACH_MODIFY(digest256map_, map, const uint8_t *, \ MAP_FOREACH_MODIFY(digest256map, map, const uint8_t *, \
keyvar, valtype, valvar) keyvar, valtype, valvar)
#define DIGEST256MAP_FOREACH_END MAP_FOREACH_END #define DIGEST256MAP_FOREACH_END MAP_FOREACH_END
#define STRMAP_FOREACH(map, keyvar, valtype, valvar) \ #define STRMAP_FOREACH(map, keyvar, valtype, valvar) \
MAP_FOREACH(strmap_, map, const char *, keyvar, valtype, valvar) MAP_FOREACH(strmap, map, const char *, keyvar, valtype, valvar)
#define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \ #define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
MAP_FOREACH_MODIFY(strmap_, map, const char *, keyvar, valtype, valvar) MAP_FOREACH_MODIFY(strmap, map, const char *, keyvar, valtype, valvar)
#define STRMAP_FOREACH_END MAP_FOREACH_END #define STRMAP_FOREACH_END MAP_FOREACH_END
void* strmap_set_lc(strmap_t *map, const char *key, void *val); void* strmap_set_lc(strmap_t *map, const char *key, void *val);
@ -186,66 +186,66 @@ void* strmap_remove_lc(strmap_t *map, const char *key);
#define DECLARE_TYPED_DIGESTMAP_FNS(prefix, mapname_t, valtype) \ #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, mapname_t, valtype) \
typedef struct mapname_t mapname_t; \ typedef struct mapname_t mapname_t; \
typedef struct prefix##iter_t *prefix##iter_t; \ typedef struct prefix##_iter_t *prefix##_iter_t; \
ATTR_UNUSED static inline mapname_t* \ ATTR_UNUSED static inline mapname_t* \
prefix##new(void) \ prefix##_new(void) \
{ \ { \
return (mapname_t*)digestmap_new(); \ return (mapname_t*)digestmap_new(); \
} \ } \
ATTR_UNUSED static inline digestmap_t* \ ATTR_UNUSED static inline digestmap_t* \
prefix##to_digestmap(mapname_t *map) \ prefix##_to_digestmap(mapname_t *map) \
{ \ { \
return (digestmap_t*)map; \ return (digestmap_t*)map; \
} \ } \
ATTR_UNUSED static inline valtype* \ ATTR_UNUSED static inline valtype* \
prefix##get(mapname_t *map, const char *key) \ prefix##_get(mapname_t *map, const char *key) \
{ \ { \
return (valtype*)digestmap_get((digestmap_t*)map, key); \ return (valtype*)digestmap_get((digestmap_t*)map, key); \
} \ } \
ATTR_UNUSED static inline valtype* \ ATTR_UNUSED static inline valtype* \
prefix##set(mapname_t *map, const char *key, valtype *val) \ prefix##_set(mapname_t *map, const char *key, valtype *val) \
{ \ { \
return (valtype*)digestmap_set((digestmap_t*)map, key, val); \ return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
} \ } \
ATTR_UNUSED static inline valtype* \ ATTR_UNUSED static inline valtype* \
prefix##remove(mapname_t *map, const char *key) \ prefix##_remove(mapname_t *map, const char *key) \
{ \ { \
return (valtype*)digestmap_remove((digestmap_t*)map, key); \ return (valtype*)digestmap_remove((digestmap_t*)map, key); \
} \ } \
ATTR_UNUSED static inline void \ ATTR_UNUSED static inline void \
prefix##f##ree_(mapname_t *map, void (*free_val)(void*)) \ prefix##_f##ree_(mapname_t *map, void (*free_val)(void*)) \
{ \ { \
digestmap_free_((digestmap_t*)map, free_val); \ digestmap_free_((digestmap_t*)map, free_val); \
} \ } \
ATTR_UNUSED static inline int \ ATTR_UNUSED static inline int \
prefix##isempty(mapname_t *map) \ prefix##_isempty(mapname_t *map) \
{ \ { \
return digestmap_isempty((digestmap_t*)map); \ return digestmap_isempty((digestmap_t*)map); \
} \ } \
ATTR_UNUSED static inline int \ ATTR_UNUSED static inline int \
prefix##size(mapname_t *map) \ prefix##_size(mapname_t *map) \
{ \ { \
return digestmap_size((digestmap_t*)map); \ return digestmap_size((digestmap_t*)map); \
} \ } \
ATTR_UNUSED static inline \ ATTR_UNUSED static inline \
prefix##iter_t *prefix##iter_init(mapname_t *map) \ prefix##_iter_t *prefix##_iter_init(mapname_t *map) \
{ \ { \
return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \ return (prefix##_iter_t*) digestmap_iter_init((digestmap_t*)map); \
} \ } \
ATTR_UNUSED static inline \ ATTR_UNUSED static inline \
prefix##iter_t *prefix##iter_next(mapname_t *map, prefix##iter_t *iter) \ prefix##_iter_t *prefix##_iter_next(mapname_t *map, prefix##_iter_t *iter) \
{ \ { \
return (prefix##iter_t*) digestmap_iter_next( \ return (prefix##_iter_t*) digestmap_iter_next( \
(digestmap_t*)map, (digestmap_iter_t*)iter); \ (digestmap_t*)map, (digestmap_iter_t*)iter); \
} \ } \
ATTR_UNUSED static inline prefix##iter_t* \ ATTR_UNUSED static inline prefix##_iter_t* \
prefix##iter_next_rmv(mapname_t *map, prefix##iter_t *iter) \ prefix##_iter_next_rmv(mapname_t *map, prefix##_iter_t *iter) \
{ \ { \
return (prefix##iter_t*) digestmap_iter_next_rmv( \ return (prefix##_iter_t*) digestmap_iter_next_rmv( \
(digestmap_t*)map, (digestmap_iter_t*)iter); \ (digestmap_t*)map, (digestmap_iter_t*)iter); \
} \ } \
ATTR_UNUSED static inline void \ ATTR_UNUSED static inline void \
prefix##iter_get(prefix##iter_t *iter, \ prefix##_iter_get(prefix##_iter_t *iter, \
const char **keyp, \ const char **keyp, \
valtype **valp) \ valtype **valp) \
{ \ { \
@ -254,7 +254,7 @@ void* strmap_remove_lc(strmap_t *map, const char *key);
*valp = v; \ *valp = v; \
} \ } \
ATTR_UNUSED static inline int \ ATTR_UNUSED static inline int \
prefix##iter_done(prefix##iter_t *iter) \ prefix##_iter_done(prefix##_iter_t *iter) \
{ \ { \
return digestmap_iter_done((digestmap_iter_t*)iter); \ return digestmap_iter_done((digestmap_iter_t*)iter); \
} }