From 398454ed067b95215c7affbe265fd36edab931ee Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 3 Dec 2018 16:43:05 -0500 Subject: [PATCH] lib: bt_object_{get,put}_ref(): accept a `const` parameter This is the first step to make the whole API const-correct. bt_object_get_ref() and bt_object_put_ref() are not considered to logically modify the object: they change the reference count, but the object's readable properties remain unchanged. Considering this, it is simpler to have a single version of each of them accepting a const object so as to be able to get and put const objects (eventually). bt_object_put_ref() can have the effect of destroying/freeing the object, just like Linux's kfree() does while accepting a `const void *` parameter. It is safe to cast away `const` in this library because the user only passes opaque handles to functions (`struct bt_X *` types) pointing to non-const objects defined by the library itself in writable memory (usually through g_new0()). In C++11, the equivalent of having a `const struct bt_X *` object on which you can increment and decrement the reference count would be having an `std::shared_ptr` object: the wrapper is mutable, but the contained object is const. Signed-off-by: Philippe Proulx --- cli/babeltrace-cfg-cli-args-connect.c | 3 +- cli/babeltrace-cfg-cli-args.c | 9 ++++-- cli/babeltrace-cfg.h | 5 ++- cli/babeltrace.c | 19 +++++++----- include/babeltrace/object-internal.h | 31 +++++++++++++------ include/babeltrace/object.h | 4 +-- include/babeltrace/plugin/plugin-internal.h | 6 ++-- lib/graph/component-class-sink-colander.c | 5 ++- lib/graph/component.c | 6 ++-- lib/graph/iterator.c | 9 ++++-- lib/graph/notification/stream.c | 6 ++-- lib/object.c | 10 +++--- lib/plugin/plugin-so.c | 6 ++-- lib/plugin/plugin.c | 6 ++-- lib/trace-ir/clock-value.c | 6 ++-- lib/trace-ir/event-class.c | 6 ++-- lib/trace-ir/field-classes.c | 12 ++++--- lib/trace-ir/fields.c | 3 +- lib/trace-ir/packet.c | 3 +- lib/trace-ir/stream-class.c | 12 ++++--- lib/trace-ir/trace.c | 3 +- .../ctf-meta-update-default-clock-classes.c | 3 +- plugins/ctf/common/metadata/ctf-meta.h | 3 +- .../ctf/common/metadata/visitor-generate-ir.c | 19 +++++++----- plugins/ctf/common/notif-iter/notif-iter.c | 5 +-- plugins/ctf/fs-src/data-stream-file.c | 3 +- plugins/ctf/fs-src/fs.c | 3 +- plugins/utils/muxer/muxer.c | 3 +- tests/lib/test_trace_ir_ref.c | 21 +++++++------ 29 files changed, 146 insertions(+), 84 deletions(-) diff --git a/cli/babeltrace-cfg-cli-args-connect.c b/cli/babeltrace-cfg-cli-args-connect.c index c22fe6ad..7b496165 100644 --- a/cli/babeltrace-cfg-cli-args-connect.c +++ b/cli/babeltrace-cfg-cli-args-connect.c @@ -296,7 +296,8 @@ static struct bt_config_component *find_component_in_array(GPtrArray *comps, struct bt_config_component *comp = g_ptr_array_index(comps, i); if (strcmp(name, comp->instance_name->str) == 0) { - found_comp = bt_object_get_ref(comp); + found_comp = comp; + bt_object_get_ref(found_comp); goto end; } } diff --git a/cli/babeltrace-cfg-cli-args.c b/cli/babeltrace-cfg-cli-args.c index 80802fbe..784cd5fd 100644 --- a/cli/babeltrace-cfg-cli-args.c +++ b/cli/babeltrace-cfg-cli-args.c @@ -1516,7 +1516,8 @@ struct bt_config *bt_config_base_create(enum bt_config_command command, cfg->command_needs_plugins = needs_plugins; if (initial_plugin_paths) { - cfg->plugin_paths = bt_object_get_ref(initial_plugin_paths); + cfg->plugin_paths = initial_plugin_paths; + bt_object_get_ref(cfg->plugin_paths); } else { cfg->plugin_paths = bt_private_value_array_create(); if (!cfg->plugin_paths) { @@ -3630,13 +3631,15 @@ struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], struct implicit_component_args implicit_debug_info_args = { 0 }; struct implicit_component_args implicit_muxer_args = { 0 }; struct implicit_component_args implicit_trimmer_args = { 0 }; - struct bt_private_value *plugin_paths = - bt_object_get_ref(initial_plugin_paths); + struct bt_private_value *plugin_paths; char error_buf[256] = { 0 }; size_t i; struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 }; char *output = NULL; + plugin_paths = initial_plugin_paths; + bt_object_get_ref(plugin_paths); + *retcode = 0; if (argc <= 1) { diff --git a/cli/babeltrace-cfg.h b/cli/babeltrace-cfg.h index b63c6c5c..d72348d3 100644 --- a/cli/babeltrace-cfg.h +++ b/cli/babeltrace-cfg.h @@ -129,7 +129,10 @@ static inline struct bt_config_component *bt_config_get_component(GPtrArray *array, size_t index) { - return bt_object_get_ref(g_ptr_array_index(array, index)); + struct bt_config_component *comp = g_ptr_array_index(array, index); + + bt_object_get_ref(comp); + return comp; } int bt_config_append_plugin_paths(struct bt_private_value *plugin_paths, diff --git a/cli/babeltrace.c b/cli/babeltrace.c index 8a917a79..71e5baf9 100644 --- a/cli/babeltrace.c +++ b/cli/babeltrace.c @@ -138,7 +138,8 @@ void set_signal_handler(void) static void init_static_data(void) { - loaded_plugins = g_ptr_array_new_with_free_func(bt_object_put_ref); + loaded_plugins = g_ptr_array_new_with_free_func( + (GDestroyNotify) bt_object_put_ref); } static @@ -287,7 +288,8 @@ struct bt_plugin *find_plugin(const char *name) } } - return bt_object_get_ref(plugin); + bt_object_get_ref(plugin); + return plugin; } typedef void *(*plugin_borrow_comp_cls_func_t)(struct bt_plugin *, @@ -309,8 +311,8 @@ void *find_component_class_from_plugin(const char *plugin_name, goto end; } - comp_class = bt_object_get_ref( - plugin_borrow_comp_cls_func(plugin, comp_class_name)); + comp_class = plugin_borrow_comp_cls_func(plugin, comp_class_name); + bt_object_get_ref(comp_class); BT_OBJECT_PUT_REF_AND_RESET(plugin); end: @@ -787,7 +789,8 @@ void add_to_loaded_plugins(struct bt_plugin_set *plugin_set) /* Add to global array. */ BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"", bt_plugin_get_name(plugin)); - g_ptr_array_add(loaded_plugins, bt_object_get_ref(plugin)); + bt_object_get_ref(plugin); + g_ptr_array_add(loaded_plugins, plugin); } } } @@ -2085,19 +2088,19 @@ int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg) ctx->cfg = cfg; ctx->connect_ports = false; ctx->src_components = g_hash_table_new_full(g_direct_hash, - g_direct_equal, NULL, bt_object_put_ref); + g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref); if (!ctx->src_components) { goto error; } ctx->flt_components = g_hash_table_new_full(g_direct_hash, - g_direct_equal, NULL, bt_object_put_ref); + g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref); if (!ctx->flt_components) { goto error; } ctx->sink_components = g_hash_table_new_full(g_direct_hash, - g_direct_equal, NULL, bt_object_put_ref); + g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref); if (!ctx->sink_components) { goto error; } diff --git a/include/babeltrace/object-internal.h b/include/babeltrace/object-internal.h index ff11e907..b5a7a1f8 100644 --- a/include/babeltrace/object-internal.h +++ b/include/babeltrace/object-internal.h @@ -36,10 +36,10 @@ typedef void (*bt_object_parent_is_owner_listener_func)( struct bt_object *); static inline -void bt_object_get_no_null_check(struct bt_object *obj); +void bt_object_get_no_null_check(const void *obj); static inline -void bt_object_put_no_null_check(struct bt_object *obj); +void bt_object_put_no_null_check(const void *obj); /* * Babeltrace object base. @@ -89,24 +89,29 @@ struct bt_object { }; static inline -unsigned long long bt_object_get_ref_count(struct bt_object *obj) +unsigned long long bt_object_get_ref_count(const struct bt_object *c_obj) { + struct bt_object *obj = (void *) c_obj; + BT_ASSERT(obj); BT_ASSERT(obj->is_shared); return obj->ref_count; } static inline -struct bt_object *bt_object_borrow_parent(struct bt_object *obj) +struct bt_object *bt_object_borrow_parent(const struct bt_object *c_obj) { + struct bt_object *obj = (void *) c_obj; + BT_ASSERT(obj); BT_ASSERT(obj->is_shared); return obj->parent; } static inline -struct bt_object *bt_object_get_parent(struct bt_object *obj) +struct bt_object *bt_object_get_parent(const struct bt_object *c_obj) { + struct bt_object *obj = (void *) c_obj; struct bt_object *parent = bt_object_borrow_parent(obj); if (parent) { @@ -241,8 +246,10 @@ void bt_object_set_parent_is_owner_listener_func(struct bt_object *obj, } static inline -void bt_object_inc_ref_count(struct bt_object *obj) +void bt_object_inc_ref_count(const struct bt_object *c_obj) { + struct bt_object *obj = (void *) c_obj; + BT_ASSERT(obj); BT_ASSERT(obj->is_shared); obj->ref_count++; @@ -250,8 +257,10 @@ void bt_object_inc_ref_count(struct bt_object *obj) } static inline -void bt_object_get_no_null_check_no_parent_check(struct bt_object *obj) +void bt_object_get_no_null_check_no_parent_check(const struct bt_object *c_obj) { + struct bt_object *obj = (void *) c_obj; + BT_ASSERT(obj); BT_ASSERT(obj->is_shared); @@ -266,8 +275,10 @@ void bt_object_get_no_null_check_no_parent_check(struct bt_object *obj) } static inline -void bt_object_get_no_null_check(struct bt_object *obj) +void bt_object_get_no_null_check(const void *c_obj) { + struct bt_object *obj = (void *) c_obj; + BT_ASSERT(obj); BT_ASSERT(obj->is_shared); @@ -291,8 +302,10 @@ void bt_object_get_no_null_check(struct bt_object *obj) } static inline -void bt_object_put_no_null_check(struct bt_object *obj) +void bt_object_put_no_null_check(const void *c_obj) { + struct bt_object *obj = (void *) c_obj; + BT_ASSERT(obj); BT_ASSERT(obj->is_shared); BT_ASSERT(obj->ref_count > 0); diff --git a/include/babeltrace/object.h b/include/babeltrace/object.h index e72614fb..10f94c44 100644 --- a/include/babeltrace/object.h +++ b/include/babeltrace/object.h @@ -178,7 +178,7 @@ would destroy the object and leave a dangling pointer in \p _var_dst. @sa bt_object_put_ref(): Decrements the reference count of a Babeltrace object. */ -void *bt_object_get_ref(void *obj); +void bt_object_get_ref(const void *obj); /** @brief Decrements the reference count of the Babeltrace object @@ -202,7 +202,7 @@ former is generally safer. variable to another. @sa bt_object_get_ref(): Increments the reference count of a Babeltrace object. */ -void bt_object_put_ref(void *obj); +void bt_object_put_ref(const void *obj); /** @} diff --git a/include/babeltrace/plugin/plugin-internal.h b/include/babeltrace/plugin/plugin-internal.h index de1035c7..7ca57ba9 100644 --- a/include/babeltrace/plugin/plugin-internal.h +++ b/include/babeltrace/plugin/plugin-internal.h @@ -362,7 +362,8 @@ enum bt_plugin_status bt_plugin_add_component_class( } /* Add new component class */ - g_ptr_array_add(comp_classes, bt_object_get_ref(comp_class)); + bt_object_get_ref(comp_class); + g_ptr_array_add(comp_classes, comp_class); /* Special case for a shared object plugin */ if (plugin->type == BT_PLUGIN_TYPE_SO) { @@ -426,7 +427,8 @@ void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set, { BT_ASSERT(plugin_set); BT_ASSERT(plugin); - g_ptr_array_add(plugin_set->plugins, bt_object_get_ref(plugin)); + bt_object_get_ref(plugin); + g_ptr_array_add(plugin_set->plugins, plugin); BT_LIB_LOGV("Added plugin to plugin set: " "plugin-set-addr=%p, %![plugin-]+l", plugin_set, plugin); diff --git a/lib/graph/component-class-sink-colander.c b/lib/graph/component-class-sink-colander.c index 712c4731..724d7de1 100644 --- a/lib/graph/component-class-sink-colander.c +++ b/lib/graph/component-class-sink-colander.c @@ -195,9 +195,8 @@ struct bt_component_class_sink *bt_component_class_sink_colander_get(void) colander_comp_cls, colander_input_port_connected); end: - return bt_object_get_ref( - bt_private_component_class_sink_as_component_class_sink( - colander_comp_cls)); + bt_object_get_ref(colander_comp_cls); + return (void *) colander_comp_cls; } __attribute__((destructor)) static diff --git a/lib/graph/component.c b/lib/graph/component.c index 7bca1bfd..84a85010 100644 --- a/lib/graph/component.c +++ b/lib/graph/component.c @@ -227,7 +227,8 @@ struct bt_port *add_port( /* * Notify the graph's creator that a new port was added. */ - graph = bt_object_get_ref(bt_component_borrow_graph(component)); + bt_object_get_ref(bt_component_borrow_graph(component)); + graph = bt_component_borrow_graph(component); if (graph) { bt_graph_notify_port_added(graph, new_port); BT_OBJECT_PUT_REF_AND_RESET(graph); @@ -278,7 +279,8 @@ int bt_component_create(struct bt_component_class *component_class, bt_object_init_shared_with_parent(&component->base, destroy_component); - component->class = bt_object_get_ref(component_class); + component->class = component_class; + bt_object_get_no_null_check(component->class); component->destroy = component_destroy_funcs[type]; component->name = g_string_new(name); if (!component->name) { diff --git a/lib/graph/iterator.c b/lib/graph/iterator.c index fd728ad7..ffd2de5a 100644 --- a/lib/graph/iterator.c +++ b/lib/graph/iterator.c @@ -103,7 +103,8 @@ struct stream_state *create_stream_state(struct bt_stream *stream) /* * We keep a reference to the stream until we know it's ended. */ - stream_state->stream = bt_object_get_ref(stream); + stream_state->stream = stream; + bt_object_get_no_null_check(stream_state->stream); BT_LIB_LOGV("Created stream state: %![stream-]+s, " "stream-state-addr=%p", stream, stream_state); @@ -609,7 +610,8 @@ bool validate_notification( goto end; } stream_state->expected_notif_seq_num++; - stream_state->cur_packet = bt_object_get_ref(packet); + stream_state->cur_packet = packet; + bt_object_get_no_null_check(stream_state->cur_packet); goto end; case BT_NOTIFICATION_TYPE_PACKET_END: if (!stream_state->cur_packet) { @@ -956,7 +958,8 @@ bt_port_output_notification_iterator_create( goto error; } - iterator->graph = bt_object_get_ref(graph); + iterator->graph = graph; + bt_object_get_no_null_check(iterator->graph); colander_data.notifs = (void *) iterator->base.notifs->pdata; colander_data.count_addr = &iterator->count; diff --git a/lib/graph/notification/stream.c b/lib/graph/notification/stream.c index f24194d8..11af10b8 100644 --- a/lib/graph/notification/stream.c +++ b/lib/graph/notification/stream.c @@ -78,7 +78,8 @@ struct bt_private_notification *bt_private_notification_stream_end_create( bt_notification_init(¬ification->parent, BT_NOTIFICATION_TYPE_STREAM_END, bt_notification_stream_end_destroy, NULL); - notification->stream = bt_object_get_ref(stream); + notification->stream = stream; + bt_object_get_no_null_check(notification->stream); BT_LIB_LOGD("Created stream end notification object: " "%![notif-]+n, %![stream-]+s, %![sc-]+S", notification, stream, stream_class); @@ -183,7 +184,8 @@ struct bt_private_notification *bt_private_notification_stream_begin_create( bt_notification_init(¬ification->parent, BT_NOTIFICATION_TYPE_STREAM_BEGIN, bt_notification_stream_begin_destroy, NULL); - notification->stream = bt_object_get_ref(stream); + notification->stream = stream; + bt_object_get_no_null_check(notification->stream); BT_LIB_LOGD("Created stream beginning notification object: " "%![notif-]+n, %![stream-]+s, %![sc-]+S", notification, stream, stream_class); diff --git a/lib/object.c b/lib/object.c index 60036544..5194dd88 100644 --- a/lib/object.c +++ b/lib/object.c @@ -26,9 +26,9 @@ #include #include -void *bt_object_get_ref(void *ptr) +void bt_object_get_ref(const void *ptr) { - struct bt_object *obj = ptr; + struct bt_object *obj = (void *) ptr; if (unlikely(!obj)) { goto end; @@ -38,12 +38,12 @@ void *bt_object_get_ref(void *ptr) bt_object_get_no_null_check(obj); end: - return ptr; + return; } -void bt_object_put_ref(void *ptr) +void bt_object_put_ref(const void *ptr) { - struct bt_object *obj = ptr; + struct bt_object *obj = (void *) ptr; if (unlikely(!obj)) { return; diff --git a/lib/plugin/plugin-so.c b/lib/plugin/plugin-so.c index 9208dc53..1c3e348a 100644 --- a/lib/plugin/plugin-so.c +++ b/lib/plugin/plugin-so.c @@ -1130,7 +1130,8 @@ struct bt_plugin *bt_plugin_so_create_empty( } spec = plugin->spec_data; - spec->shared_lib_handle = bt_object_get_ref(shared_lib_handle); + spec->shared_lib_handle = shared_lib_handle; + bt_object_get_no_null_check(spec->shared_lib_handle); goto end; error: @@ -1498,7 +1499,8 @@ void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin, BT_ASSERT(plugin->type == BT_PLUGIN_TYPE_SO); bt_list_add(&comp_class->node, &component_class_list); - comp_class->so_handle = bt_object_get_ref(spec->shared_lib_handle); + comp_class->so_handle = spec->shared_lib_handle; + bt_object_get_no_null_check(comp_class->so_handle); /* Add our custom destroy listener */ bt_component_class_add_destroy_listener(comp_class, diff --git a/lib/plugin/plugin.c b/lib/plugin/plugin.c index f70f5c31..70e99e4f 100644 --- a/lib/plugin/plugin.c +++ b/lib/plugin/plugin.c @@ -271,7 +271,8 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name) plugin_name) == 0) { BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"", plugin_name, dir->str); - plugin = bt_object_get_ref(candidate_plugin); + plugin = candidate_plugin; + bt_object_get_no_null_check(plugin); goto end; } } @@ -291,7 +292,8 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name) plugin_name) == 0) { BT_LOGD("Plugin found in built-in plugins: " "name=\"%s\"", plugin_name); - plugin = bt_object_get_ref(candidate_plugin); + plugin = candidate_plugin; + bt_object_get_no_null_check(plugin); goto end; } } diff --git a/lib/trace-ir/clock-value.c b/lib/trace-ir/clock-value.c index affc565c..32c4b862 100644 --- a/lib/trace-ir/clock-value.c +++ b/lib/trace-ir/clock-value.c @@ -58,7 +58,8 @@ struct bt_clock_value *bt_clock_value_new(struct bt_clock_class *clock_class) } bt_object_init_unique(&ret->base); - ret->clock_class = bt_object_get_ref(clock_class); + ret->clock_class = clock_class; + bt_object_get_no_null_check(clock_class); bt_clock_class_freeze(clock_class); BT_LIB_LOGD("Created clock value object: %!+k", ret); @@ -80,7 +81,8 @@ struct bt_clock_value *bt_clock_value_create(struct bt_clock_class *clock_class) } if (likely(!clock_value->clock_class)) { - clock_value->clock_class = bt_object_get_ref(clock_class); + clock_value->clock_class = clock_class; + bt_object_get_no_null_check(clock_class); } goto end; diff --git a/lib/trace-ir/event-class.c b/lib/trace-ir/event-class.c index 685c09c3..839e9a99 100644 --- a/lib/trace-ir/event-class.c +++ b/lib/trace-ir/event-class.c @@ -334,7 +334,8 @@ int bt_private_event_class_set_specific_context_field_class( bt_field_class_make_part_of_trace(field_class); bt_object_put_ref(event_class->specific_context_fc); - event_class->specific_context_fc = bt_object_get_ref(field_class); + event_class->specific_context_fc = field_class; + bt_object_get_no_null_check(event_class->specific_context_fc); bt_field_class_freeze(field_class); BT_LIB_LOGV("Set event class's specific context field classe: %!+E", event_class); @@ -399,7 +400,8 @@ int bt_private_event_class_set_payload_field_class( bt_field_class_make_part_of_trace(field_class); bt_object_put_ref(event_class->payload_fc); - event_class->payload_fc = bt_object_get_ref(field_class); + event_class->payload_fc = field_class; + bt_object_get_no_null_check(event_class->payload_fc); bt_field_class_freeze(field_class); BT_LIB_LOGV("Set event class's payload field classe: %!+E", event_class); diff --git a/lib/trace-ir/field-classes.c b/lib/trace-ir/field-classes.c index 2adf0348..325d1810 100644 --- a/lib/trace-ir/field-classes.c +++ b/lib/trace-ir/field-classes.c @@ -762,7 +762,8 @@ int append_named_field_class_to_container_field_class( named_fc = &g_array_index(container_fc->named_fcs, struct bt_named_field_class, container_fc->named_fcs->len - 1); named_fc->name = name_str; - named_fc->fc = bt_object_get_ref(fc); + named_fc->fc = fc; + bt_object_get_no_null_check(fc); g_hash_table_insert(container_fc->name_to_index, named_fc->name->str, GUINT_TO_POINTER(container_fc->named_fcs->len - 1)); bt_field_class_freeze(fc); @@ -924,7 +925,8 @@ int bt_private_field_class_variant_set_selector_field_class( BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); BT_ASSERT_PRE_FC_IS_ENUM(selector_fc, "Selector field class"); BT_ASSERT_PRE_FC_HOT(fc, "Variant field class"); - var_fc->selector_fc = bt_object_get_ref(selector_fc); + var_fc->selector_fc = (void *) selector_fc; + bt_object_get_no_null_check(selector_fc); bt_field_class_freeze((void *) selector_fc); return 0; } @@ -1003,7 +1005,8 @@ void init_array_field_class(struct bt_field_class_array *fc, { BT_ASSERT(element_fc); init_field_class((void *) fc, type, release_func); - fc->element_fc = bt_object_get_ref(element_fc); + fc->element_fc = element_fc; + bt_object_get_no_null_check(element_fc); bt_field_class_freeze(element_fc); } @@ -1136,7 +1139,8 @@ int bt_private_field_class_dynamic_array_set_length_field_class( "Field class"); BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, "Length field class"); BT_ASSERT_PRE_FC_HOT(fc, "Dynamic array field class"); - array_fc->length_fc = bt_object_get_ref(length_fc); + array_fc->length_fc = (void *) length_fc; + bt_object_get_no_null_check(length_fc); bt_field_class_freeze(length_fc); return 0; } diff --git a/lib/trace-ir/fields.c b/lib/trace-ir/fields.c index 8eadc757..c769c8a0 100644 --- a/lib/trace-ir/fields.c +++ b/lib/trace-ir/fields.c @@ -227,7 +227,8 @@ void init_field(struct bt_field *field, struct bt_field_class *fc, BT_ASSERT(fc); bt_object_init_unique(&field->base); field->methods = methods; - field->class = bt_object_get_ref(fc); + field->class = fc; + bt_object_get_no_null_check(fc); } static diff --git a/lib/trace-ir/packet.c b/lib/trace-ir/packet.c index 983887c4..52f462a4 100644 --- a/lib/trace-ir/packet.c +++ b/lib/trace-ir/packet.c @@ -267,7 +267,8 @@ struct bt_packet *bt_packet_new(struct bt_stream *stream) bt_object_init_shared(&packet->base, (bt_object_release_func) bt_packet_recycle); - packet->stream = bt_object_get_ref(stream); + packet->stream = stream; + bt_object_get_no_null_check(stream); trace = bt_stream_class_borrow_trace_inline(stream->class); BT_ASSERT(trace); diff --git a/lib/trace-ir/stream-class.c b/lib/trace-ir/stream-class.c index 9d016844..6b33bca6 100644 --- a/lib/trace-ir/stream-class.c +++ b/lib/trace-ir/stream-class.c @@ -345,7 +345,8 @@ int bt_private_stream_class_set_packet_context_field_class( bt_field_class_make_part_of_trace(field_class); bt_object_put_ref(stream_class->packet_context_fc); - stream_class->packet_context_fc = bt_object_get_ref(field_class); + stream_class->packet_context_fc = field_class; + bt_object_get_no_null_check(stream_class->packet_context_fc); bt_field_class_freeze(field_class); BT_LIB_LOGV("Set stream class's packet context field classe: %!+S", stream_class); @@ -402,7 +403,8 @@ int bt_private_stream_class_set_event_header_field_class( bt_field_class_make_part_of_trace(field_class); bt_object_put_ref(stream_class->event_header_fc); - stream_class->event_header_fc = bt_object_get_ref(field_class); + stream_class->event_header_fc = field_class; + bt_object_get_no_null_check(stream_class->event_header_fc); bt_field_class_freeze(field_class); BT_LIB_LOGV("Set stream class's event header field classe: %!+S", stream_class); @@ -460,7 +462,8 @@ int bt_private_stream_class_set_event_common_context_field_class( bt_field_class_make_part_of_trace(field_class); bt_object_put_ref(stream_class->event_common_context_fc); - stream_class->event_common_context_fc = bt_object_get_ref(field_class); + stream_class->event_common_context_fc = field_class; + bt_object_get_no_null_check(stream_class->event_common_context_fc); bt_field_class_freeze(field_class); BT_LIB_LOGV("Set stream class's event common context field classe: %!+S", stream_class); @@ -488,7 +491,8 @@ int bt_private_stream_class_set_default_clock_class( BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); BT_ASSERT_PRE_STREAM_CLASS_HOT(stream_class); bt_object_put_ref(stream_class->default_clock_class); - stream_class->default_clock_class = bt_object_get_ref(clock_class); + stream_class->default_clock_class = clock_class; + bt_object_get_no_null_check(stream_class->default_clock_class); bt_clock_class_freeze(clock_class); BT_LIB_LOGV("Set stream class's default clock class: %!+S", stream_class); diff --git a/lib/trace-ir/trace.c b/lib/trace-ir/trace.c index be791f36..d33a4dc4 100644 --- a/lib/trace-ir/trace.c +++ b/lib/trace-ir/trace.c @@ -525,7 +525,8 @@ int bt_private_trace_set_packet_header_field_class( bt_field_class_make_part_of_trace(field_class); bt_object_put_ref(trace->packet_header_fc); - trace->packet_header_fc = bt_object_get_ref(field_class); + trace->packet_header_fc = field_class; + bt_object_get_no_null_check(trace->packet_header_fc); bt_field_class_freeze(field_class); BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace); diff --git a/plugins/ctf/common/metadata/ctf-meta-update-default-clock-classes.c b/plugins/ctf/common/metadata/ctf-meta-update-default-clock-classes.c index f2e48aae..01d94946 100644 --- a/plugins/ctf/common/metadata/ctf-meta-update-default-clock-classes.c +++ b/plugins/ctf/common/metadata/ctf-meta-update-default-clock-classes.c @@ -164,7 +164,8 @@ int update_stream_class_default_clock_class( } if (!stream_class->default_clock_class) { - stream_class->default_clock_class = bt_object_get_ref(clock_class); + stream_class->default_clock_class = clock_class; + bt_object_get_ref(stream_class->default_clock_class); } end: diff --git a/plugins/ctf/common/metadata/ctf-meta.h b/plugins/ctf/common/metadata/ctf-meta.h index cb84d778..481a3b6c 100644 --- a/plugins/ctf/common/metadata/ctf-meta.h +++ b/plugins/ctf/common/metadata/ctf-meta.h @@ -1108,7 +1108,8 @@ void ctf_field_class_int_copy_content( dst_fc->is_signed = src_fc->is_signed; dst_fc->disp_base = src_fc->disp_base; dst_fc->encoding = src_fc->encoding; - dst_fc->mapped_clock_class = bt_object_get_ref(src_fc->mapped_clock_class); + dst_fc->mapped_clock_class = src_fc->mapped_clock_class; + bt_object_get_ref(dst_fc->mapped_clock_class); dst_fc->storing_index = src_fc->storing_index; } diff --git a/plugins/ctf/common/metadata/visitor-generate-ir.c b/plugins/ctf/common/metadata/visitor-generate-ir.c index 44c4e927..69149a50 100644 --- a/plugins/ctf/common/metadata/visitor-generate-ir.c +++ b/plugins/ctf/common/metadata/visitor-generate-ir.c @@ -2719,7 +2719,8 @@ int visit_integer_decl(struct ctx *ctx, (*integer_decl)->is_signed = (signedness > 0); (*integer_decl)->disp_base = base; (*integer_decl)->encoding = encoding; - (*integer_decl)->mapped_clock_class = bt_object_get_ref(mapped_clock_class); + (*integer_decl)->mapped_clock_class = mapped_clock_class; + bt_object_get_ref((*integer_decl)->mapped_clock_class); return 0; error: @@ -3612,15 +3613,16 @@ int auto_map_field_to_trace_clock_class(struct ctx *ctx, "default"); BT_ASSERT(ret == 0); g_ptr_array_add(ctx->ctf_tc->clock_classes, - bt_object_get_ref(clock_class_to_map_to)); + clock_class_to_map_to); + bt_object_get_ref(clock_class_to_map_to); break; case 1: /* * Only one clock class exists in the trace at this point: use * this one. */ - clock_class_to_map_to = - bt_object_get_ref(ctx->ctf_tc->clock_classes->pdata[0]); + clock_class_to_map_to = ctx->ctf_tc->clock_classes->pdata[0]; + bt_object_get_ref(clock_class_to_map_to); break; default: /* @@ -3634,7 +3636,8 @@ int auto_map_field_to_trace_clock_class(struct ctx *ctx, } BT_ASSERT(clock_class_to_map_to); - int_fc->mapped_clock_class = bt_object_get_ref(clock_class_to_map_to); + int_fc->mapped_clock_class = clock_class_to_map_to; + bt_object_get_ref(int_fc->mapped_clock_class); end: bt_object_put_ref(clock_class_to_map_to); @@ -4744,7 +4747,8 @@ int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node) bt_private_clock_class_as_clock_class(clock))); bt_private_clock_class_set_offset(clock, offset_seconds, offset_cycles); apply_clock_class_offset(ctx, clock); - g_ptr_array_add(ctx->ctf_tc->clock_classes, bt_object_get_ref(clock)); + g_ptr_array_add(ctx->ctf_tc->clock_classes, clock); + bt_object_get_ref(clock); end: BT_OBJECT_PUT_REF_AND_RESET(clock); @@ -4902,7 +4906,8 @@ struct bt_private_trace *ctf_visitor_generate_ir_get_ir_trace( BT_ASSERT(ctx); BT_ASSERT(ctx->trace); - return bt_object_get_ref(ctx->trace); + bt_object_get_ref(ctx->trace); + return ctx->trace; } BT_HIDDEN diff --git a/plugins/ctf/common/notif-iter/notif-iter.c b/plugins/ctf/common/notif-iter/notif-iter.c index 4ebff2ae..750f4a10 100644 --- a/plugins/ctf/common/notif-iter/notif-iter.c +++ b/plugins/ctf/common/notif-iter/notif-iter.c @@ -805,9 +805,10 @@ enum bt_notif_iter_status set_current_stream(struct bt_notif_iter *notit) "stream-class-addr=%p, stream-class-id=%" PRId64, notit, notit->meta.sc, notit->meta.sc->id); - stream = bt_object_get_ref(notit->medium.medops.borrow_stream( + stream = notit->medium.medops.borrow_stream( notit->meta.sc->ir_sc, notit->cur_data_stream_id, - notit->medium.data)); + notit->medium.data); + bt_object_get_ref(stream); BT_LOGV("User function returned: stream-addr=%p", stream); if (!stream) { BT_LOGW_STR("User function failed to return a stream object " diff --git a/plugins/ctf/fs-src/data-stream-file.c b/plugins/ctf/fs-src/data-stream-file.c index f700b02f..1a95ae09 100644 --- a/plugins/ctf/fs-src/data-stream-file.c +++ b/plugins/ctf/fs-src/data-stream-file.c @@ -642,7 +642,8 @@ struct ctf_fs_ds_file *ctf_fs_ds_file_create( goto error; } - ds_file->stream = bt_object_get_ref(stream); + ds_file->stream = stream; + bt_object_get_ref(ds_file->stream); ds_file->metadata = ctf_fs_trace->metadata; g_string_assign(ds_file->file->path, path); ret = ctf_fs_file_open(ds_file->file, "rb"); diff --git a/plugins/ctf/fs-src/fs.c b/plugins/ctf/fs-src/fs.c index 082d8fa4..24bfa04a 100644 --- a/plugins/ctf/fs-src/fs.c +++ b/plugins/ctf/fs-src/fs.c @@ -536,7 +536,8 @@ struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create( ds_file_group->stream_id = stream_instance_id; BT_ASSERT(stream_class); - ds_file_group->stream_class = bt_object_get_ref(stream_class); + ds_file_group->stream_class = stream_class; + bt_object_get_ref(ds_file_group->stream_class); ds_file_group->ctf_fs_trace = ctf_fs_trace; goto end; diff --git a/plugins/utils/muxer/muxer.c b/plugins/utils/muxer/muxer.c index 8cd83299..275f8e7e 100644 --- a/plugins/utils/muxer/muxer.c +++ b/plugins/utils/muxer/muxer.c @@ -151,7 +151,8 @@ struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter( goto end; } - muxer_upstream_notif_iter->notif_iter = bt_object_get_ref(self_notif_iter); + muxer_upstream_notif_iter->notif_iter = self_notif_iter; + bt_object_get_ref(muxer_upstream_notif_iter->notif_iter); muxer_upstream_notif_iter->notifs = g_queue_new(); if (!muxer_upstream_notif_iter->notifs) { BT_LOGE_STR("Failed to allocate a GQueue."); diff --git a/tests/lib/test_trace_ir_ref.c b/tests/lib/test_trace_ir_ref.c index 380fbe56..d8af80b9 100644 --- a/tests/lib/test_trace_ir_ref.c +++ b/tests/lib/test_trace_ir_ref.c @@ -360,9 +360,9 @@ static void test_example_scenario(void) "TC1 reference count is 1"); /* User A acquires a reference to SC2 from TC1. */ - user_a.sc = bt_object_get_ref( - bt_private_trace_borrow_stream_class_by_index( - user_a.tc, 1)); + user_a.sc = bt_private_trace_borrow_stream_class_by_index( + user_a.tc, 1); + bt_object_get_ref(user_a.sc); ok(user_a.sc, "User A acquires SC2 from TC1"); ok(bt_object_get_ref_count((void *) weak_tc1) == 2, "TC1 reference count is 2"); @@ -370,9 +370,9 @@ static void test_example_scenario(void) "SC2 reference count is 1"); /* User A acquires a reference to EC3 from SC2. */ - user_a.ec = bt_object_get_ref( - bt_private_stream_class_borrow_event_class_by_index( - user_a.sc, 0)); + user_a.ec = bt_private_stream_class_borrow_event_class_by_index( + user_a.sc, 0); + bt_object_get_ref(user_a.ec); ok(user_a.ec, "User A acquires EC3 from SC2"); ok(bt_object_get_ref_count((void *) weak_tc1) == 2, "TC1 reference count is 2"); @@ -411,7 +411,8 @@ static void test_example_scenario(void) /* User B acquires a reference to SC1. */ diag("User B acquires a reference to SC1"); - user_b.sc = bt_object_get_ref(weak_sc1); + user_b.sc = weak_sc1; + bt_object_get_ref(user_b.sc); ok(bt_object_get_ref_count((void *) weak_tc1) == 2, "TC1 reference count is 2"); ok(bt_object_get_ref_count((void *) weak_sc1) == 1, @@ -419,9 +420,9 @@ static void test_example_scenario(void) /* User C acquires a reference to EC1. */ diag("User C acquires a reference to EC1"); - user_c.ec = bt_object_get_ref( - bt_private_stream_class_borrow_event_class_by_index( - user_b.sc, 0)); + user_c.ec = bt_private_stream_class_borrow_event_class_by_index( + user_b.sc, 0); + bt_object_get_ref(user_c.ec); ok(bt_object_get_ref_count((void *) weak_ec1) == 1, "EC1 reference count is 1"); ok(bt_object_get_ref_count((void *) weak_sc1) == 2, -- 2.34.1