[Asterisk-Dev] Re: ast_flags question
Kevin P. Fleming
kpfleming at starnetworks.us
Sat Jan 8 16:15:30 MST 2005
Tom Ivar Helbekkmo wrote:
> The "int" combines with the ":1" to mean that the field "b" is a 1 bit
> integer, and only needs 1 bit of storage. As it stands, it will
> probably use 32 bits on a 32 bit machine, but if you add a field "int
> c : 1;" to the struct, the whole struct will still only use 32 bits --
> and so on, until you pass 32 such 1 bit variables.
That's true; my point was that using "int b:1" saves zero bytes over
"int", which is what we are currently using.
Now if you had used "char b:1", then yes, that could potentially save
some space (although in reality it likely wouldn't because the compiler
would choose to use a 4-byte space for the struct anyway for performance
reasons).
> A struct containing fewer than 32 of those 1 bit variables, and an int
> that's used with a matching set of bit masks to isolate the needed
> bits, can both use 32 bits of storage if the compiler chooses to do it
> that way, but there are two clear advantages:
Of course, you're preaching to the choir! I already agreed that I would
prefer to see it coded this way, but the decision was made without input
from many of us and it'd be a lot of work to change it at this point
without a clearly demonstrated benefit.
> 1) if (session.authenticated) looks better than
> if (session.flags & SESS_AUTHENTICATED)
Unfortunately, this is the only benefit that can be demonstrated, and
while I agree it would make the code much easier to read and maintain
(especially for the two-bit flags I've been adding lately), that's not
enough to rework all the existing code.
If you want to get on IRC and join #asterisk-dev and convince the other
devs that this would be a good idea, we might be able to convince Mark
to do it while there's still time before it has spread to every module
in Asterisk...
More information about the asterisk-dev
mailing list