hmap: Add extra build-time iteration checks for types derived from hmap.
authorBen Pfaff <blp@ovn.org>
Tue, 9 Feb 2016 00:52:45 +0000 (16:52 -0800)
committerBen Pfaff <blp@ovn.org>
Sat, 20 Feb 2016 00:43:24 +0000 (16:43 -0800)
commitbc8d7dfabdf3ca10ce6f31426d7710ffca7dbc0e
tree53e074d7291f83f45037829752f080f378a42833
parent9193d14524c4408c0d3d5aab4752f44852285e6f
hmap: Add extra build-time iteration checks for types derived from hmap.

Some of our data structures derived from hmap use the same member names.
This means it's possible to confuse them in iteration, e.g. to iterate a
shash with SIMAP_FOR_EACH.  Of course this will crash at runtime, but it
seems even better to catch it at compile time.

An alternative would be to use unique member names, e.g. shash_map and
simap_map instead of just map.  I like short names, though.

It's kind of nasty that we need support from the hmap code to do this.
An alternative would be to insert the build assertions as statements before
the for loop.  But that would cause nasty surprises if someone forgets the
{} around a block of statements; even though the OVS coding style requires
them in all cases, I suspect that programmers doing debugging, etc. tend
to omit them sometimes.

It's not actually necessary to have multiple variants of these macros,
e.g. one can write a C99-compliant HMAP_FOR_EACH that accepts 3 or 4 or
more arguments.  But such a macro is harder to read, so I don't know
whether this is a good tradeoff.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Andy Zhou <azhou@ovn.org>
lib/hmap.h
lib/hmapx.h
lib/shash.h
lib/simap.h
lib/smap.h