Subject: Re: Bitfields and kernel
To: None <tech-kern@netbsd.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 10/04/1999 13:37:58
>> The problem with this is that ANSI C does not define the behavior
>> for enums as bit masks.  [...]

> I thought enums were treated as integral types?
> If I have "enum { SOMEFLAG = (1<<2) };" how else could SOMEFLAG be
> interpreted but with 4?

There are two uses for enums here, and I think people are getting them
confused.

One is replacing
	#define F_FOO 0x0020
with
	enum foo_flag_t { F_FOO = 0x0020 };

The other is replacing
	int foo_flags;
with
	enum foo_flag_t foo_flags;

The former is reasonably safe, though personally I don't like it; I
prefer enums to be opaque, rather than the "funny ints" they actually
ended up being.

The latter is what is not safe; there is no guarantee that an object of
type "enum foo_flag_t" can store any values other than those given in
the definition of enum foo_flag_t.  (When such a value is converted
back to an int, it must regain the value given at declaration.  But
that's all; the compiler is allowed to do mapping between the one and
the other when loading and storing enums.)

Fortunately or unfortunately, it's possible to replace the #defines and
still leave the variables as having non-enum type.  Indeed, I know of
one shop whose house coding style calls for this.

					der Mouse

			       mouse@rodents.montreal.qc.ca
		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B