From 47e5a0329d01fa74bdf8b3fdcf75940f6789c93f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Fri, 10 Jul 2015 17:15:17 -0400 Subject: [PATCH] Add iterator and source implementations MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- .../plugin/component-factory-internal.h | 4 +- include/babeltrace/plugin/component-factory.h | 6 -- .../babeltrace/plugin/component-internal.h | 15 +-- .../plugin/notification/iterator-internal.h | 70 ++++++++++++++ .../babeltrace/plugin/notification/iterator.h | 2 +- include/babeltrace/plugin/plugin-system.h | 48 ++++++---- include/babeltrace/plugin/source-internal.h | 2 +- include/babeltrace/plugin/source.h | 10 +- plugins/component.c | 1 + plugins/iterator.c | 91 +++++++++++++++++++ plugins/sink.c | 2 +- plugins/source.c | 47 +++++++++- 12 files changed, 246 insertions(+), 52 deletions(-) diff --git a/include/babeltrace/plugin/component-factory-internal.h b/include/babeltrace/plugin/component-factory-internal.h index 4810e9ca..69c16419 100644 --- a/include/babeltrace/plugin/component-factory-internal.h +++ b/include/babeltrace/plugin/component-factory-internal.h @@ -38,13 +38,13 @@ * @param name Component instance name (will be copied) * @param private_data Private component implementation data * @param destroy_cb Component private data clean-up callback - * @param iterator_create_cb Iterator creation callback + * @param iterator_init_cb Iterator initialization callback * @returns A source component instance */ BT_HIDDEN extern struct bt_component *bt_component_source_create(const char *name, void *private_data, bt_component_destroy_cb destroy_func, - bt_component_source_iterator_create_cb iterator_create_cb); + bt_component_source_iterator_init_cb iterator_init_cb); /** * Allocate a sink component. diff --git a/include/babeltrace/plugin/component-factory.h b/include/babeltrace/plugin/component-factory.h index 06c61379..29e2eb5f 100644 --- a/include/babeltrace/plugin/component-factory.h +++ b/include/babeltrace/plugin/component-factory.h @@ -37,12 +37,6 @@ extern "C" { struct bt_component_factory; -typedef struct bt_component *(*bt_component_init_cb)( - struct bt_component *component); - -typedef struct bt_component *(*bt_component_fini_cb)( - struct bt_component *component); - enum bt_component_status bt_component_factory_create(const char *path); enum bt_component_status bt_component_factory_register_source_component_class( diff --git a/include/babeltrace/plugin/component-internal.h b/include/babeltrace/plugin/component-internal.h index 6c8689aa..ea6c489d 100644 --- a/include/babeltrace/plugin/component-internal.h +++ b/include/babeltrace/plugin/component-internal.h @@ -28,19 +28,12 @@ */ #include -#include -#include #include +#include #include #include #include -#ifdef __cplusplus -extern "C" { -#endif - -struct bt_notification; - struct bt_component { struct bt_ctf_ref ref_count; GString *name; @@ -54,14 +47,10 @@ struct bt_component { }; BT_HIDDEN -enum bt_component_status bt_component_init(struct bt_component *plugin, +enum bt_component_status bt_component_init(struct bt_component *component, const char *name, void *user_data, bt_component_destroy_cb destroy_func, enum bt_component_type component_type, bt_component_destroy_cb component_destroy); -#ifdef __cplusplus -} -#endif - #endif /* BABELTRACE_PLUGIN_COMPONENT_INTERNAL_H */ diff --git a/include/babeltrace/plugin/notification/iterator-internal.h b/include/babeltrace/plugin/notification/iterator-internal.h index e69de29b..6d1676e1 100644 --- a/include/babeltrace/plugin/notification/iterator-internal.h +++ b/include/babeltrace/plugin/notification/iterator-internal.h @@ -0,0 +1,70 @@ +#ifndef BABELTRACE_PLUGIN_ITERATOR_INTERNAL_H +#define BABELTRACE_PLUGIN_ITERATOR_INTERNAL_H + +/* + * BabelTrace - Notification Iterator Internal + * + * Copyright 2015 Jérémie Galarneau + * + * Author: 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 + +struct bt_notification_iterator { + struct bt_ctf_ref ref_count; + bt_notification_iterator_get_cb get; + bt_notification_iterator_next_cb next; + void *user_data; +}; + +/** + * Allocate a notification iterator. + * + * @param component Component instance + * @returns A notification iterator instance + */ +BT_HIDDEN +struct bt_notification_iterator *bt_notification_iterator_create( + struct bt_component *component); + +/** + * Validate a notification iterator. + * + * @param iterator Notification iterator instance + * @returns One of #bt_component_status values + */ +BT_HIDDEN +enum bt_notification_iterator_status bt_notification_iterator_validate( + struct bt_notification_iterator *iterator); + +/** + * Destroy a notification iterator. + * + * @param iterator Notification iterator instance + */ +BT_HIDDEN +void bt_notification_iterator_destroy( + struct bt_notification_iterator *iterator); + +#endif /* BABELTRACE_PLUGIN_ITERATOR_INTERNAL_H */ diff --git a/include/babeltrace/plugin/notification/iterator.h b/include/babeltrace/plugin/notification/iterator.h index 18ee4f62..22807e41 100644 --- a/include/babeltrace/plugin/notification/iterator.h +++ b/include/babeltrace/plugin/notification/iterator.h @@ -42,7 +42,7 @@ struct bt_notification_iterator; enum bt_notification_iterator_status { /** Invalid arguments. */ /* -22 for compatibility with -EINVAL */ - BT_NOTIFICATION_ITERATOR_STATUS_EINVAL = -22, + BT_NOTIFICATION_ITERATOR_STATUS_INVAL = -22, /** End of trace. */ BT_NOTIFICATION_ITERATOR_STATUS_EOT = -3, diff --git a/include/babeltrace/plugin/plugin-system.h b/include/babeltrace/plugin/plugin-system.h index e7ca01a5..9a933200 100644 --- a/include/babeltrace/plugin/plugin-system.h +++ b/include/babeltrace/plugin/plugin-system.h @@ -31,13 +31,15 @@ */ #include -#include +#include #ifdef __cplusplus extern "C" { #endif struct bt_notification; +struct bt_notification_iterator; +struct bt_component; /** * Component private data deallocation function type. @@ -51,8 +53,14 @@ typedef void (*bt_component_destroy_cb)(struct bt_component *component); * * @param component Component instance */ -typedef struct bt_notification_iterator *( - *bt_component_source_iterator_create_cb)( +typedef enum bt_component_status (*bt_component_source_iterator_init_cb)( + struct bt_component *component, + struct bt_notification_iterator *iterator); + +typedef struct bt_component *(*bt_component_init_cb)( + struct bt_component *component); + +typedef struct bt_component *(*bt_component_fini_cb)( struct bt_component *component); /** @@ -65,10 +73,10 @@ typedef struct bt_notification_iterator *( typedef enum bt_component_status (*bt_component_sink_handle_notification_cb)( struct bt_component *, struct bt_notification *); -typedef struct bt_notification *(bt_notification_iterator_get_notification_cb)( +typedef struct bt_notification *(*bt_notification_iterator_get_cb)( struct bt_notification_iterator *); -typedef enum bt_notification_iterator_status (bt_notification_iterator_next_cb)( +typedef enum bt_notification_iterator_status (*bt_notification_iterator_next_cb)( struct bt_notification_iterator *); /** @@ -79,6 +87,21 @@ typedef enum bt_notification_iterator_status (bt_notification_iterator_next_cb)( */ extern void *bt_component_get_private_data(struct bt_component *component); +extern enum bt_notification_iterator_status +bt_notification_iterator_set_get_cb(struct bt_notification_iterator *iterator, + bt_notification_iterator_get_cb get); + +extern enum bt_notification_iterator_status +bt_notification_iterator_set_next_cb(struct bt_notification_iterator *iterator, + bt_notification_iterator_next_cb next); + +extern enum bt_notification_iterator_status +bt_notification_iterator_set_private_data( + struct bt_notification_iterator *iterator, void *data); + +extern void *bt_notification_iterator_get_private_data( + struct bt_notification_iterator *iterator); + /** * Set a component's private (implementation) data. * @@ -89,21 +112,6 @@ extern void *bt_component_get_private_data(struct bt_component *component); extern enum bt_component_status bt_component_set_private_data( struct bt_component *component, void *data); - -/** Notification iterator functions */ -/** - * Allocate a notification iterator. - * - * @param component Component instance - * @param next_cb Callback advancing to the next notification - * @param notification_cb Callback providing the current notification - * @returns A notification iterator instance - */ -extern struct bt_notification_iterator *bt_notification_iterator_create( - struct bt_component *component, - bt_notification_iterator_next_cb next_cb, - bt_notification_iterator_get_notification_cb notification_cb); - #ifdef __cplusplus } #endif diff --git a/include/babeltrace/plugin/source-internal.h b/include/babeltrace/plugin/source-internal.h index 75508dce..03881053 100644 --- a/include/babeltrace/plugin/source-internal.h +++ b/include/babeltrace/plugin/source-internal.h @@ -35,7 +35,7 @@ struct bt_component_source { struct bt_component parent; /* Component implementation callbacks */ - bt_component_source_iterator_create_cb create_iterator; + bt_component_source_iterator_init_cb init_iterator; }; #endif /* BABELTRACE_PLUGIN_SOURCE_INTERNAL_H */ diff --git a/include/babeltrace/plugin/source.h b/include/babeltrace/plugin/source.h index a333e392..13faedd0 100644 --- a/include/babeltrace/plugin/source.h +++ b/include/babeltrace/plugin/source.h @@ -33,17 +33,17 @@ extern "C" { #endif -struct bt_plugin; +struct bt_component; struct bt_notification_iterator; /** - * Create an iterator on a plug-in instance. + * Create an iterator on a component instance. * - * @param plugin Plug-in instance + * @param component Component instance * @returns Notification iterator instance */ -struct bt_notification_iterator *bt_plugin_source_create_iterator( - struct bt_plugin *plugin); +struct bt_notification_iterator *bt_component_source_create_iterator( + struct bt_component *component); #ifdef __cplusplus } diff --git a/plugins/component.c b/plugins/component.c index 5b7a23b3..f977c837 100644 --- a/plugins/component.c +++ b/plugins/component.c @@ -26,6 +26,7 @@ * SOFTWARE. */ +#include #include #include #include diff --git a/plugins/iterator.c b/plugins/iterator.c index e69de29b..66f918c8 100644 --- a/plugins/iterator.c +++ b/plugins/iterator.c @@ -0,0 +1,91 @@ +/* + * iterator.c + * + * Babeltrace Notification Iterator + * + * Copyright 2015 Jérémie Galarneau + * + * Author: 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 +#include +#include + +static +void bt_notification_iterator_destroy(struct bt_notification_iterator *iterator) +{ + +} + +BT_HIDDEN +struct bt_notification_iterator *bt_notification_iterator_create( + struct bt_component *component) +{ + struct bt_notification_iterator *iterator = NULL; + + if (!component || bt_component_get_type(component) != BT_COMPONENT_TYPE_SOURCE) { + goto end; + } + + iterator = g_new0(struct bt_notification_iterator, 1); + if (!iterator) { + goto end; + } + + bt_ctf_ref_init(&iterator->ref_count); +end: + return iterator; +} + +BT_HIDDEN +enum bt_notification_iterator_status bt_notification_iterator_validate( + struct bt_notification_iterator *iterator) +{ + enum bt_notification_iterator_status ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; + + if (!iterator || !iterator->get || !iterator->next) { + ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; + goto end; + } +end: + return ret; +} + +void bt_notification_iterator_get(struct bt_notification_iterator *iterator) +{ + if (!iterator) { + return; + } + + bt_ctf_ref_get(&iterator->ref_count); +} + +void bt_notification_iterator_put(struct bt_notification_iterator *iterator) +{ + if (!iterator) { + return; + } + + bt_ctf_ref_put(&iterator->ref_count, bt_notification_iterator_destroy); +} diff --git a/plugins/sink.c b/plugins/sink.c index ea7d7993..fcbc9485 100644 --- a/plugins/sink.c +++ b/plugins/sink.c @@ -1,7 +1,7 @@ /* * sink.c * - * Babeltrace Source Component + * Babeltrace Sink Component * * Copyright 2015 Jérémie Galarneau * diff --git a/plugins/source.c b/plugins/source.c index d32fa2a7..b722059b 100644 --- a/plugins/source.c +++ b/plugins/source.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include static void bt_component_source_destroy(struct bt_component *component) @@ -45,12 +47,12 @@ void bt_component_source_destroy(struct bt_component *component) struct bt_component *bt_component_source_create(const char *name, void *private_data, bt_component_destroy_cb destroy_func, - bt_component_source_iterator_create_cb iterator_create_cb) + bt_component_source_iterator_init_cb iterator_init_cb) { struct bt_component_source *source = NULL; enum bt_component_status ret; - if (!iterator_create_cb) { + if (!iterator_init_cb) { goto end; } @@ -68,7 +70,46 @@ struct bt_component *bt_component_source_create(const char *name, goto end; } - source->create_iterator = iterator_create_cb; + source->init_iterator = iterator_init_cb; end: return source ? &source->parent : NULL; } + +struct bt_notification_iterator *bt_plugin_source_create_iterator( + struct bt_component *component) +{ + enum bt_component_status ret_component; + enum bt_notification_iterator_status ret_iterator; + struct bt_component_source *source; + struct bt_notification_iterator *iterator = NULL; + + if (!component) { + goto end; + } + + if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SOURCE) { + goto end; + } + + iterator = bt_notification_iterator_create(component); + if (!iterator) { + goto end; + } + + source = container_of(component, struct bt_component_source, parent); + assert(source->init_iterator); + ret_component = source->init_iterator(component, iterator); + if (ret_component != BT_COMPONENT_STATUS_OK) { + goto error; + } + + ret_iterator = bt_notification_iterator_validate(iterator); + if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) { + goto error; + } +end: + return iterator; +error: + bt_notification_iterator_put(iterator); + return NULL; +} -- 2.34.1