[ofa-general] [PATCH] Allow paths to the device specific library to be absolute

Jason Gunthorpe jgunthorpe at obsidianresearch.com
Wed Jul 22 14:25:05 PDT 2009


On Wed, Jul 22, 2009 at 11:55:53AM -0700, Roland Dreier wrote:

> Didn't reply to this before, sorry.  But yes I am interested.  As far as
> using enum for bit flags, what is the C++ idiom for that?

Well, it isn't just C++, it applies to C too - but gcc isn't as sticky
with the errors.

Basically, something like this:

enum bar {FOO=1, BAR=2};

Creates the distinct type 'enum bar'. The only things in the entire
compilation that are of type 'enum bar' is FOO, BAR, and explicit
casts to 'enum bar'. The expression (FOO|BAR) implicitly casts FOO and
BAR to the enums integer compatible type, does the OR and the
resulting type is the enums integer compatible type.

So this:

enum bar x = FOO|BAR;

Is not 'right' at all. G++ gives an error, some C compilers could
warn too, but gcc does not.. g++ says:

t.cc:3: error: invalid conversion from 'int' to 'bar1'

In this platform the integer compatible type for 'enum bar' is 'int',
but sadly that can vary..

You can see how this can become a big pain in C++, you have to add
enum casts to all sorts of strange places.. :(

Basically, when using bit flags in C or C++ there is no advantage
to using the enum's type as the flag parameter/member. Static analysis
won't catch any errors, ie:

enum bar {FOO=1, BAR=2};
enum bar1 {FOO1=1, BAR1=2};
enum bar1 x = FOO|BAR;

Results in no warnings as gcc silently casts the integer compatible
type back to the enum bar1.

So, basically, the message is, don't use the flag enums type as a
storage decl, only to manage the namespace.

To fix this.. Well..

int ibv_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
                  enum ibv_qp_attr_mask attr_mask);

becomes:

int ibv_modify_qp(struct ibv_qp *qp, struct ibv_qp_attr *attr,
                  ENUM_TYPE attr_mask);

What I've been mulling over is exactly what ENUM_TYPE should be to
have guarenteed ABI compatibility on ppc 32/64, i386 and x86-64.

'int' is the correct answer on x86, x86-64, and ppc32.

The basic test program is:

# cat t.cc
enum bar {FOO=1, BAR=2};
enum bar1 {FOO1=1, BAR1=2};
enum bar1 x = FOO|BAR;
# g++ -c -Wall t.cc
t.cc:3: error: invalid conversion from 'int' to 'bar1'

I'd like to see a result for ia64 and ppc64.. Roland do you have a
Debian machine logon? Could you check this on merulo.debian.org (or
merkel)?  Unfortunately I don't think pescetti is a ppc64 :|

I'm not sure where to find a ppc64? (Shirley can you help?)

The affected types look to me to be:
 ibv_device_cap_flags
 ibv_wc_flags
 ibv_access_flags
 ibv_rereg_mr_flags
 ibv_srq_attr_mask
 ibv_qp_attr_mask
 ibv_send_flags

What do you think?

Regards,
Jason



More information about the general mailing list