From 5cebbe7fc898bb1d887cbdac1f4dd5f0042443fd Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sun, 11 Aug 2019 11:20:14 -0400 Subject: [PATCH] lib: add boolean field class and field types This patch adds the boolean field class and field to the available library types. They are analogous to the boolean value object which already exists. CTF 2 will very likely have such a type, so Babeltrace 2.0 will be ready. This patch does not adapt existing plugins and the Python bindings to use and wrap the new boolean field class and field; this work is reserved for subsequent patches. Signed-off-by: Philippe Proulx Change-Id: I90874f214757ad67d5eecaad5bd8297b8610c001 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1891 Tested-by: jenkins Reviewed-by: Francis Deslauriers --- .../babeltrace2/trace-ir/field-class-const.h | 1 + include/babeltrace2/trace-ir/field-class.h | 3 + include/babeltrace2/trace-ir/field-const.h | 2 + include/babeltrace2/trace-ir/field.h | 2 + src/lib/lib-logging.c | 7 ++ src/lib/trace-ir/field-class.c | 34 ++++++++++ src/lib/trace-ir/field-class.h | 4 ++ src/lib/trace-ir/field.c | 67 +++++++++++++++++++ src/lib/trace-ir/field.h | 5 ++ 9 files changed, 125 insertions(+) diff --git a/include/babeltrace2/trace-ir/field-class-const.h b/include/babeltrace2/trace-ir/field-class-const.h index 7511d0d4..688c61d1 100644 --- a/include/babeltrace2/trace-ir/field-class-const.h +++ b/include/babeltrace2/trace-ir/field-class-const.h @@ -37,6 +37,7 @@ extern "C" { #endif typedef enum bt_field_class_type { + BT_FIELD_CLASS_TYPE_BOOL, BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER, BT_FIELD_CLASS_TYPE_SIGNED_INTEGER, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, diff --git a/include/babeltrace2/trace-ir/field-class.h b/include/babeltrace2/trace-ir/field-class.h index 9d2cf064..d2f43bec 100644 --- a/include/babeltrace2/trace-ir/field-class.h +++ b/include/babeltrace2/trace-ir/field-class.h @@ -37,6 +37,9 @@ extern "C" { #endif +extern bt_field_class *bt_field_class_bool_create( + bt_trace_class *trace_class); + extern bt_field_class *bt_field_class_integer_unsigned_create( bt_trace_class *trace_class); diff --git a/include/babeltrace2/trace-ir/field-const.h b/include/babeltrace2/trace-ir/field-const.h index 7ba63ee0..f3f1ef8d 100644 --- a/include/babeltrace2/trace-ir/field-const.h +++ b/include/babeltrace2/trace-ir/field-const.h @@ -42,6 +42,8 @@ extern const bt_field_class *bt_field_borrow_class_const( extern bt_field_class_type bt_field_get_class_type( const bt_field *field); +extern bt_bool bt_field_bool_get_value(const bt_field *field); + extern int64_t bt_field_integer_signed_get_value(const bt_field *field); extern uint64_t bt_field_integer_unsigned_get_value( diff --git a/include/babeltrace2/trace-ir/field.h b/include/babeltrace2/trace-ir/field.h index 75d3ca45..311c53a8 100644 --- a/include/babeltrace2/trace-ir/field.h +++ b/include/babeltrace2/trace-ir/field.h @@ -35,6 +35,8 @@ extern "C" { #endif +extern void bt_field_bool_set_value(bt_field *field, bt_bool value); + extern void bt_field_integer_signed_set_value(bt_field *field, int64_t value); diff --git a/src/lib/lib-logging.c b/src/lib/lib-logging.c index 42eef208..8b878655 100644 --- a/src/lib/lib-logging.c +++ b/src/lib/lib-logging.c @@ -352,6 +352,13 @@ static inline void format_field(char **buf_ch, bool extended, } switch (field->class->type) { + case BT_FIELD_CLASS_TYPE_BOOL: + { + const struct bt_field_bool *bool_field = (const void *) field; + + BUF_APPEND(", %svalue=%d", PRFIELD(bool_field->value)); + break; + } case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: diff --git a/src/lib/trace-ir/field-class.c b/src/lib/trace-ir/field-class.c index ffb972f4..6afc32b8 100644 --- a/src/lib/trace-ir/field-class.c +++ b/src/lib/trace-ir/field-class.c @@ -64,6 +64,40 @@ void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type, fc->type = type; } +static +void destroy_bool_field_class(struct bt_object *obj) +{ + BT_ASSERT(obj); + BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj); + g_free(obj); +} + +struct bt_field_class *bt_field_class_bool_create( + bt_trace_class *trace_class) +{ + struct bt_field_class_bool *bool_fc = NULL; + + BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class"); + BT_LOGD("Creating default boolean field class object."); + bool_fc = g_new0(struct bt_field_class_bool, 1); + if (!bool_fc) { + BT_LIB_LOGE_APPEND_CAUSE( + "Failed to allocate one boolean field class."); + goto error; + } + + init_field_class((void *) bool_fc, BT_FIELD_CLASS_TYPE_BOOL, + destroy_bool_field_class); + BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc); + goto end; + +error: + BT_OBJECT_PUT_REF_AND_RESET(bool_fc); + +end: + return (void *) bool_fc; +} + static void init_integer_field_class(struct bt_field_class_integer *fc, enum bt_field_class_type type, diff --git a/src/lib/trace-ir/field-class.h b/src/lib/trace-ir/field-class.h index e35b3965..53041a13 100644 --- a/src/lib/trace-ir/field-class.h +++ b/src/lib/trace-ir/field-class.h @@ -186,6 +186,10 @@ struct bt_field_class { bool part_of_trace_class; }; +struct bt_field_class_bool { + struct bt_field_class common; +}; + struct bt_field_class_integer { struct bt_field_class common; diff --git a/src/lib/trace-ir/field.c b/src/lib/trace-ir/field.c index 26160c20..95a76e49 100644 --- a/src/lib/trace-ir/field.c +++ b/src/lib/trace-ir/field.c @@ -74,6 +74,13 @@ bool structure_field_is_set(const struct bt_field *field); static bool variant_field_is_set(const struct bt_field *field); +static +struct bt_field_methods bool_field_methods = { + .set_is_frozen = set_single_field_is_frozen, + .is_set = single_field_is_set, + .reset = reset_single_field, +}; + static struct bt_field_methods integer_field_methods = { .set_is_frozen = set_single_field_is_frozen, @@ -116,6 +123,9 @@ struct bt_field_methods variant_field_methods = { .reset = reset_variant_field, }; +static +struct bt_field *create_bool_field(struct bt_field_class *); + static struct bt_field *create_integer_field(struct bt_field_class *); @@ -139,6 +149,7 @@ struct bt_field *create_variant_field(struct bt_field_class *); static struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = { + [BT_FIELD_CLASS_TYPE_BOOL] = create_bool_field, [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = create_integer_field, [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = create_integer_field, [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = create_integer_field, @@ -153,6 +164,9 @@ struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = { [BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR] = create_variant_field, }; +static +void destroy_bool_field(struct bt_field *field); + static void destroy_integer_field(struct bt_field *field); @@ -173,6 +187,7 @@ void destroy_variant_field(struct bt_field *field); static void (* const field_destroy_funcs[])(struct bt_field *) = { + [BT_FIELD_CLASS_TYPE_BOOL] = destroy_bool_field, [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = destroy_integer_field, [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = destroy_integer_field, [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = destroy_integer_field, @@ -235,6 +250,26 @@ void init_field(struct bt_field *field, struct bt_field_class *fc, bt_object_get_no_null_check(fc); } +static +struct bt_field *create_bool_field(struct bt_field_class *fc) +{ + struct bt_field_bool *bool_field; + + BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc); + bool_field = g_new0(struct bt_field_bool, 1); + if (!bool_field) { + BT_LIB_LOGE_APPEND_CAUSE( + "Failed to allocate one boolean field."); + goto end; + } + + init_field((void *) bool_field, fc, &bool_field_methods); + BT_LIB_LOGD("Created boolean field object: %!+f", bool_field); + +end: + return (void *) bool_field; +} + static struct bt_field *create_integer_field(struct bt_field_class *fc) { @@ -494,6 +529,29 @@ end: return (void *) array_field; } +bt_bool bt_field_bool_get_value(const struct bt_field *field) +{ + const struct bt_field_bool *bool_field = (const void *) field; + + BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); + BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field"); + BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL, + "Field"); + return (bt_bool) bool_field->value; +} + +void bt_field_bool_set_value(struct bt_field *field, bt_bool value) +{ + struct bt_field_bool *bool_field = (void *) field; + + BT_ASSERT_PRE_DEV_NON_NULL(field, "Field"); + BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL, + "Field"); + BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field"); + bool_field->value = (bool) value; + bt_field_set_single(field, true); +} + int64_t bt_field_integer_signed_get_value(const struct bt_field *field) { const struct bt_field_integer *int_field = (const void *) field; @@ -952,6 +1010,15 @@ void bt_field_finalize(struct bt_field *field) BT_OBJECT_PUT_REF_AND_RESET(field->class); } +static +void destroy_bool_field(struct bt_field *field) +{ + BT_ASSERT(field); + BT_LIB_LOGD("Destroying boolean field object: %!+f", field); + bt_field_finalize(field); + g_free(field); +} + static void destroy_integer_field(struct bt_field *field) { diff --git a/src/lib/trace-ir/field.h b/src/lib/trace-ir/field.h index ce595f97..352514ef 100644 --- a/src/lib/trace-ir/field.h +++ b/src/lib/trace-ir/field.h @@ -105,6 +105,11 @@ struct bt_field { bool frozen; }; +struct bt_field_bool { + struct bt_field common; + bool value; +}; + struct bt_field_integer { struct bt_field common; -- 2.34.1