lib: use powers of two for object type enumerators
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 10 Oct 2019 16:03:36 +0000 (12:03 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 15 Oct 2019 19:59:10 +0000 (15:59 -0400)
commit9c3869a9d005111c48715720e4c9a100cb2d5ec7
tree1335f24535d456024e5364b30b2aaa204fd94adc
parentde821fe507f035622c474f097b226f30cd3c7491
lib: use powers of two for object type enumerators

For all object type enumerators, that is, enumerators containing
`_TYPE`, use powers of two instead of sequential values.

This makes it possible to test that a given object has one or more
given types with a single bitwise operation, for example:

    bt_field_class_type fc_type = bt_field_class_get_type(my_fc);

    if (fc_type &
            (BT_FIELD_CLASS_TYPE_STATIC_ARRAY |
             BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD |
             BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD)) {
        /* `my_fc` is an array */
    }

To make things easier, I'm also introducing one type enumerator per
"abstract" field class type. For example:

* `BT_FIELD_CLASS_TYPE_SIGNED_INTEGER`
* `BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION`
* `BT_FIELD_CLASS_TYPE_REAL`
* `BT_FIELD_CLASS_TYPE_ARRAY`
* `BT_FIELD_CLASS_TYPE_OPTION_WITH_SELECTOR_FIELD`

No field class object has those exact types, but their type can have
those bits set. For example, all the integer field class types have
bit 2 set. Then, if you need to know if a given type is conceptually
a given type, use bt_field_class_type_is(), which
returns:

    (type & type_to_check) == type_to_check

Each `BT_FIELD_CLASS_TYPE_*` enumerator conceptually matches, more or
less, a field class `bt2` Python class. For example:

* `BT_FIELD_CLASS_TYPE_ARRAY` → `bt2._ArrayFieldClass`
* `BT_FIELD_CLASS_TYPE_REAL` → `bt2._RealFieldClass`

The C to Python equivalent expressions are:

Object has a specific type:
    C:

        bt_field_class_get_type(fc) == BT_FIELD_CLASS_TYPE_X

    Python:

        type(fc) is bt2._XFieldClassConst

Object is an instance of a conceptual type:
    C:

        bt_field_class_type_is(bt_field_class_get_type(obj),
                               BT_FIELD_CLASS_TYPE_ABC)

    Python:

        isinstance(fc, bt2._AbcFieldClassConst)

Of course it's more natural in Python, but Python was not developed in
1972.

All `BT_FIELD_CLASS_TYPE_*` enumerators are `unsigned long long` values
and the non-API `__BT_FIELD_CLASS_TYPE_BIG_VALUE` is `1ULL << 62`
because we could have more than 32 enumerators eventually so I want to
make sure the `bt_field_class_type` type is a 64-bit integer for ABI
compatibility reasons.

Because the enumerators are not sequential anymore, we can't use them
directly as the indexes of lookup tables. This is why bt_field_create()
and bt_field_destroy() now check the FC type explicitly instead of using
lookup tables. Those functions are not called often anyway as field
objects are always created and destroyed from object pools.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I1b5110f691e1eabc0ce9291c0281755a3c6a755b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2171
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
include/babeltrace2/error-cause-const.h
include/babeltrace2/graph/component-class-const.h
include/babeltrace2/graph/message-const.h
include/babeltrace2/graph/port-const.h
include/babeltrace2/trace-ir/field-class-const.h
include/babeltrace2/trace-ir/field-path-const.h
include/babeltrace2/value-const.h
src/lib/trace-ir/field.c
tests/cli/test_help
This page took 0.025203 seconds and 4 git commands to generate.