From: Philippe Proulx Date: Sat, 10 Aug 2019 18:52:21 +0000 (-0400) Subject: lib: add component descriptor set API X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=55f09f529d02f6f548195ad3087edb10d12c52f6 lib: add component descriptor set API This patch adds an API to the library to create an empty set of component descriptors and to add descriptors to it. A component descriptor contains: * A component class. * Component initialization parameters. * Component initialization custom data. The motivation is to eventually be able to get an effective/compatible message interchange protocol version number from such a set. As of this patch, the API is available, but it's not used anywhere. A component descriptor set is a shared object. You add a component descriptor to a component descriptor set with bt_component_descriptor_set_add_descriptor(). Internally, the descriptor gets appended to one of the source, filter, or sink descriptor array to eventually have bt_component_descriptor_get_*_descriptor_count() and bt_component_descriptor_set_borrow_*_descriptor_by_index_const() functions if needed. Signed-off-by: Philippe Proulx Change-Id: I337d4c596bd988b20849700daf9747a3f188450d Reviewed-on: https://review.lttng.org/c/babeltrace/+/1868 Tested-by: jenkins Reviewed-by: Simon Marchi --- diff --git a/include/Makefile.am b/include/Makefile.am index f2ff631b..4f7c406f 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -81,6 +81,8 @@ babeltrace2graphinclude_HEADERS = \ babeltrace2/graph/component-class-source.h \ babeltrace2/graph/component-class.h \ babeltrace2/graph/component-const.h \ + babeltrace2/graph/component-descriptor-set-const.h \ + babeltrace2/graph/component-descriptor-set.h \ babeltrace2/graph/component-filter-const.h \ babeltrace2/graph/component-sink-const.h \ babeltrace2/graph/component-source-const.h \ diff --git a/include/babeltrace2/babeltrace.h b/include/babeltrace2/babeltrace.h index 92599faa..743ca78d 100644 --- a/include/babeltrace2/babeltrace.h +++ b/include/babeltrace2/babeltrace.h @@ -135,6 +135,8 @@ #include /* Graph API */ +#include +#include #include #include #include diff --git a/include/babeltrace2/graph/component-descriptor-set-const.h b/include/babeltrace2/graph/component-descriptor-set-const.h new file mode 100644 index 00000000..4fdf49a1 --- /dev/null +++ b/include/babeltrace2/graph/component-descriptor-set-const.h @@ -0,0 +1,59 @@ +#ifndef BABELTRACE2_GRAPH_COMPONENT_DESCRIPTOR_SET_CONST_H +#define BABELTRACE2_GRAPH_COMPONENT_DESCRIPTOR_SET_CONST_H + +/* + * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __BT_IN_BABELTRACE_H +# error "Please include instead." +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern void bt_component_descriptor_set_get_ref( + const bt_component_descriptor_set *comp_descriptor_set); + +extern void bt_component_descriptor_set_put_ref( + const bt_component_descriptor_set *comp_descriptor_set); + +#define BT_COMPONENT_DESCRIPTOR_SET_PUT_REF_AND_RESET(_var) \ + do { \ + bt_component_descriptor_set_put_ref(_var); \ + (_var) = NULL; \ + } while (0) + +#define BT_COMPONENT_DESCRIPTOR_SET_MOVE_REF(_var_dst, _var_src) \ + do { \ + bt_component_descriptor_set_put_ref(_var_dst); \ + (_var_dst) = (_var_src); \ + (_var_src) = NULL; \ + } while (0) + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE2_GRAPH_COMPONENT_DESCRIPTOR_SET_CONST_H */ diff --git a/include/babeltrace2/graph/component-descriptor-set.h b/include/babeltrace2/graph/component-descriptor-set.h new file mode 100644 index 00000000..44b5ec76 --- /dev/null +++ b/include/babeltrace2/graph/component-descriptor-set.h @@ -0,0 +1,60 @@ +#ifndef BABELTRACE2_GRAPH_COMPONENT_DESCRIPTOR_SET_H +#define BABELTRACE2_GRAPH_COMPONENT_DESCRIPTOR_SET_H + +/* + * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __BT_IN_BABELTRACE_H +# error "Please include instead." +#endif + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern bt_component_descriptor_set *bt_component_descriptor_set_create(void); + +typedef enum bt_component_descriptor_set_add_descriptor_status { + BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK = __BT_FUNC_STATUS_OK, + BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_MEMORY_ERROR = __BT_FUNC_STATUS_MEMORY_ERROR, +} bt_component_descriptor_set_add_descriptor_status; + +extern bt_component_descriptor_set_add_descriptor_status +bt_component_descriptor_set_add_descriptor( + bt_component_descriptor_set *comp_descriptor_set, + const bt_component_class *component_class, + const bt_value *params); + +extern bt_component_descriptor_set_add_descriptor_status +bt_component_descriptor_set_add_descriptor_with_init_method_data( + bt_component_descriptor_set *comp_descriptor_set, + const bt_component_class *component_class, + const bt_value *params, void *init_method_data); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE2_GRAPH_COMPONENT_DESCRIPTOR_SET_H */ diff --git a/include/babeltrace2/types.h b/include/babeltrace2/types.h index 54bfc485..55fba0ed 100644 --- a/include/babeltrace2/types.h +++ b/include/babeltrace2/types.h @@ -89,6 +89,7 @@ typedef struct bt_component_class bt_component_class; typedef struct bt_component_class_filter bt_component_class_filter; typedef struct bt_component_class_sink bt_component_class_sink; typedef struct bt_component_class_source bt_component_class_source; +typedef struct bt_component_descriptor_set bt_component_descriptor_set; typedef struct bt_component_filter bt_component_filter; typedef struct bt_component_sink bt_component_sink; typedef struct bt_component_source bt_component_source; diff --git a/src/lib/graph/Makefile.am b/src/lib/graph/Makefile.am index 16e0a1e2..9c552768 100644 --- a/src/lib/graph/Makefile.am +++ b/src/lib/graph/Makefile.am @@ -8,6 +8,8 @@ libgraph_la_SOURCES = \ component-class-sink-simple.h \ component-class.c \ component-class.h \ + component-descriptor-set.c \ + component-descriptor-set.h \ component-filter.c \ component-filter.h \ component-sink.c \ diff --git a/src/lib/graph/component-descriptor-set.c b/src/lib/graph/component-descriptor-set.c new file mode 100644 index 00000000..34ae2499 --- /dev/null +++ b/src/lib/graph/component-descriptor-set.c @@ -0,0 +1,231 @@ +/* + * Copyright 2017-2019 Philippe Proulx + * Copyright 2017 Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#define BT_LOG_TAG "LIB/COMP-DESCR-SET" +#include "lib/logging.h" + +#include "common/assert.h" +#include "lib/assert-pre.h" +#include "compat/compiler.h" +#include "common/common.h" +#include +#include +#include +#include +#include + +#include "component-class.h" +#include "component-descriptor-set.h" +#include "component-class-sink-simple.h" +#include "lib/value.h" + +static +void destroy_component_descriptor_set(struct bt_object *obj) +{ + struct bt_component_descriptor_set *comp_descr_set = (void *) obj; + + if (comp_descr_set->sources) { + BT_LOGD_STR("Destroying source component descriptors."); + g_ptr_array_free(comp_descr_set->sources, TRUE); + comp_descr_set->sources = NULL; + } + + if (comp_descr_set->filters) { + BT_LOGD_STR("Destroying filter component descriptors."); + g_ptr_array_free(comp_descr_set->filters, TRUE); + comp_descr_set->filters = NULL; + } + + if (comp_descr_set->sinks) { + BT_LOGD_STR("Destroying sink component descriptors."); + g_ptr_array_free(comp_descr_set->sinks, TRUE); + comp_descr_set->sinks = NULL; + } + + g_free(comp_descr_set); +} + +static +void destroy_component_descriptor_set_entry(gpointer ptr) +{ + struct bt_component_descriptor_set_entry *entry = ptr; + + if (!ptr) { + goto end; + } + + BT_OBJECT_PUT_REF_AND_RESET(entry->comp_cls); + BT_OBJECT_PUT_REF_AND_RESET(entry->params); + g_free(entry); + +end: + return; +} + +struct bt_component_descriptor_set *bt_component_descriptor_set_create(void) +{ + struct bt_component_descriptor_set *comp_descr_set; + + BT_LOGI_STR("Creating component descriptor set object."); + comp_descr_set = g_new0(struct bt_component_descriptor_set, 1); + if (!comp_descr_set) { + BT_LIB_LOGE_APPEND_CAUSE( + "Failed to allocate one component descriptor set."); + goto end; + } + + bt_object_init_shared(&comp_descr_set->base, + destroy_component_descriptor_set); + comp_descr_set->sources = g_ptr_array_new_with_free_func( + destroy_component_descriptor_set_entry); + if (!comp_descr_set->sources) { + BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); + goto error; + } + + comp_descr_set->filters = g_ptr_array_new_with_free_func( + destroy_component_descriptor_set_entry); + if (!comp_descr_set->filters) { + BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); + goto error; + } + + comp_descr_set->sinks = g_ptr_array_new_with_free_func( + destroy_component_descriptor_set_entry); + if (!comp_descr_set->sinks) { + BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); + goto error; + } + + BT_LOGI("Created component descriptor set object: addr=%p", + comp_descr_set); + goto end; + +error: + BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set); + +end: + return comp_descr_set; +} + +enum bt_component_descriptor_set_add_descriptor_status +bt_component_descriptor_set_add_descriptor_with_init_method_data( + struct bt_component_descriptor_set *comp_descr_set, + const struct bt_component_class *comp_cls, + const struct bt_value *params, void *init_method_data) +{ + bt_component_descriptor_set_add_descriptor_status status = + BT_FUNC_STATUS_OK; + struct bt_value *new_params = NULL; + struct bt_component_descriptor_set_entry *entry = NULL; + GPtrArray *comp_descr_array = NULL; + + BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set"); + BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); + BT_ASSERT_PRE(!params || bt_value_is_map(params), + "Parameter value is not a map value: %!+v", params); + BT_LIB_LOGI("Adding component descriptor to set: " + "set-addr=%p, %![cc-]+C, " + "%![params-]+v, init-method-data-addr=%p", + comp_descr_set, comp_cls, params, init_method_data); + + if (!params) { + new_params = bt_value_map_create(); + if (!new_params) { + BT_LIB_LOGE_APPEND_CAUSE( + "Cannot create empty map value object."); + status = BT_FUNC_STATUS_MEMORY_ERROR; + goto error; + } + + params = new_params; + } + + entry = g_new0(struct bt_component_descriptor_set_entry, 1); + if (!entry) { + BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); + status = BT_FUNC_STATUS_MEMORY_ERROR; + goto error; + } + + entry->comp_cls = (void *) comp_cls; + bt_object_get_no_null_check(entry->comp_cls); + bt_component_class_freeze(entry->comp_cls); + entry->params = (void *) params; + bt_object_get_no_null_check(entry->params); + bt_value_freeze(entry->params); + entry->init_method_data = init_method_data; + + /* Move to array */ + switch (comp_cls->type) { + case BT_COMPONENT_CLASS_TYPE_SOURCE: + comp_descr_array = comp_descr_set->sources; + break; + case BT_COMPONENT_CLASS_TYPE_FILTER: + comp_descr_array = comp_descr_set->filters; + break; + case BT_COMPONENT_CLASS_TYPE_SINK: + comp_descr_array = comp_descr_set->sinks; + break; + default: + abort(); + } + + BT_ASSERT(comp_descr_array); + g_ptr_array_add(comp_descr_array, entry); + BT_LIB_LOGI("Added component descriptor to set: " + "set-addr=%p, %![cc-]+C, " + "%![params-]+v, init-method-data-addr=%p", + comp_descr_set, comp_cls, params, init_method_data); + goto end; + +error: + destroy_component_descriptor_set_entry(entry); + entry = NULL; + +end: + bt_object_put_ref(new_params); + return status; +} + +enum bt_component_descriptor_set_add_descriptor_status +bt_component_descriptor_set_add_descriptor( + struct bt_component_descriptor_set *comp_descr_set, + const struct bt_component_class *comp_cls, + const struct bt_value *params) +{ + return bt_component_descriptor_set_add_descriptor_with_init_method_data( + comp_descr_set, comp_cls, params, NULL); +} + +void bt_component_descriptor_set_get_ref( + const struct bt_component_descriptor_set *comp_descr_set) +{ + bt_object_get_ref(comp_descr_set); +} + +void bt_component_descriptor_set_put_ref( + const struct bt_component_descriptor_set *comp_descr_set) +{ + bt_object_put_ref(comp_descr_set); +} diff --git a/src/lib/graph/component-descriptor-set.h b/src/lib/graph/component-descriptor-set.h new file mode 100644 index 00000000..e275d77d --- /dev/null +++ b/src/lib/graph/component-descriptor-set.h @@ -0,0 +1,71 @@ +#ifndef BABELTRACE_GRAPH_COMPONENT_DESCRIPTOR_SET_INTERNAL_H +#define BABELTRACE_GRAPH_COMPONENT_DESCRIPTOR_SET_INTERNAL_H + +/* + * Copyright 2017-2018 Philippe Proulx + * Copyright 2017 Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include "common/macros.h" +#include "lib/object.h" +#include "common/assert.h" +#include "common/common.h" +#include +#include + +#include "component.h" +#include "component-sink.h" +#include "connection.h" +#include "lib/func-status.h" + +struct bt_component_descriptor_source; +struct bt_component_descriptor_filter; +struct bt_component_descriptor_sink; + +/* + * This structure describes an eventual component instance. + */ +struct bt_component_descriptor_set_entry { + /* Owned by this */ + struct bt_component_class *comp_cls; + + /* Owned by this */ + struct bt_value *params; + + void *init_method_data; +}; + +struct bt_component_descriptor_set { + struct bt_object base; + + /* Array of `struct bt_component_descriptor_set_entry *` */ + GPtrArray *sources; + + /* Array of `struct bt_component_descriptor_set_entry *` */ + GPtrArray *filters; + + /* Array of `struct bt_component_descriptor_set_entry *` */ + GPtrArray *sinks; +}; + +#endif /* BABELTRACE_GRAPH_COMPONENT_DESCRIPTOR_SET_INTERNAL_H */