lib: add component descriptor set API
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 10 Aug 2019 18:52:21 +0000 (14:52 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 13 Aug 2019 00:28:02 +0000 (20:28 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: I337d4c596bd988b20849700daf9747a3f188450d
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1868
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
include/Makefile.am
include/babeltrace2/babeltrace.h
include/babeltrace2/graph/component-descriptor-set-const.h [new file with mode: 0644]
include/babeltrace2/graph/component-descriptor-set.h [new file with mode: 0644]
include/babeltrace2/types.h
src/lib/graph/Makefile.am
src/lib/graph/component-descriptor-set.c [new file with mode: 0644]
src/lib/graph/component-descriptor-set.h [new file with mode: 0644]

index f2ff631be4b1728daa3a0250cc34845c78677de8..4f7c406f82cddb1ed7f2ee2c802feaaf15d234d4 100644 (file)
@@ -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 \
index 92599faacdb8f95006bbf02c19bfd28213c48cc7..743ca78d146c43a9b91a367390f1749470575518 100644 (file)
 #include <babeltrace2/graph/message-stream-end.h>
 
 /* Graph API */
+#include <babeltrace2/graph/component-descriptor-set-const.h>
+#include <babeltrace2/graph/component-descriptor-set.h>
 #include <babeltrace2/graph/connection-const.h>
 #include <babeltrace2/graph/graph-const.h>
 #include <babeltrace2/graph/graph.h>
diff --git a/include/babeltrace2/graph/component-descriptor-set-const.h b/include/babeltrace2/graph/component-descriptor-set-const.h
new file mode 100644 (file)
index 0000000..4fdf49a
--- /dev/null
@@ -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 <babeltrace2/babeltrace.h> instead."
+#endif
+
+#include <babeltrace2/types.h>
+
+#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 (file)
index 0000000..44b5ec7
--- /dev/null
@@ -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 <babeltrace2/babeltrace.h> instead."
+#endif
+
+#include <babeltrace2/types.h>
+#include <babeltrace2/logging.h>
+
+#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 */
index 54bfc4853ffc7814143dc0534d827ae75137746b..55fba0edfbefaf7eba286b89321baa619ed3640d 100644 (file)
@@ -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;
index 16e0a1e29a411d2a7fb9726f54a2f99c46e0dd7a..9c5527682f471c39c786f2797e27c30e68769766 100644 (file)
@@ -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 (file)
index 0000000..34ae249
--- /dev/null
@@ -0,0 +1,231 @@
+/*
+ * Copyright 2017-2019 Philippe Proulx <pproulx@efficios.com>
+ * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace2/types.h>
+#include <babeltrace2/value.h>
+#include <babeltrace2/value-const.h>
+#include <unistd.h>
+#include <glib.h>
+
+#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 (file)
index 0000000..e275d77
--- /dev/null
@@ -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 <pproulx@efficios.com>
+ * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace2/graph/graph.h>
+#include <babeltrace2/graph/component-descriptor-set.h>
+#include "common/macros.h"
+#include "lib/object.h"
+#include "common/assert.h"
+#include "common/common.h"
+#include <stdlib.h>
+#include <glib.h>
+
+#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 */
This page took 0.031726 seconds and 4 git commands to generate.