bool type

Havoc Pennington hp@redhat.com
Mon Jan 3 20:11:48 PST 2005


Hi,

I don't know what to do with bool types. On the wire they are bytes,
because it seems really wasteful to use 32 bits.

However, then dbus_bool_t is currently an int.

What do you expect here:
 dbus_bool_t v_bool;
 dbus_message_iter_get_value (&iter, &v_bool);
or
 unsigned char v_bool;
 dbus_message_iter_get_value (&iter, &v_bool);

Or the following:

 const unsigned char *array;
 int n_elements;
 dbus_message_iter_get_fixed_array (&iter, &array, &n_elements);

Which returns a const pointer into the message, and thus must return an
array of bytes if the wire representation is bytes.

And here's another problem:

 dbus_message_iter_append_args (&iter, DBUS_TYPE_BOOLEAN, TRUE, 
                                DBUS_TYPE_INVALID);

Where TRUE will be 32 bits normally. I believe this is why gboolean in
glib was changed from char to int, though I'm not sure I remember that
far back.

My inclination is to change the wire representation to be 32 bits,
define dbus_bool_t as int32, and always use dbus_bool_t as the type that
is returned or passed in for bools.

In the old D-BUS type system, 32-bit bools would double their usual size
from
   1 typecode, 1 value, 2 padding = 4
to
   1 typecode, 3 padding, 4 value = 8
However, with the new type system, it's more like:
   1 value, 3 padding = 4
to
   1 value = 4
(this is all assuming the bool is followed by a 4-aligned type, which is
the most common case; there's no padding if it's followed by a 1-aligned
type).

We probably don't care about the size of single bool args anyway, since
there won't be more than a small number of them per message. But if we
did care, it's not really a meaningful difference anymore.

The real reason I originally used byte and not int32 for bool was for
arrays, where using an int32 means 32x overhead. However, I'm now
thinking:
 - 8x overhead is still pretty bad if you have enough bools to 
   start caring about overhead
 - people who are worried about overhead can use a bit vector
   class from their favorite programming language and stuff
   it in a dbus byte array
 - when was the last time you used a bool array anyhow?
   I don't think I do this very often.

If this doesn't make sense and someone feels we should keep bool == char
on the wire (which will unavoidably leak into the C API a little bit),
speak now.

Havoc




More information about the dbus mailing list