From 8738a040a7933a157d153a5adab52f134be3304a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Sun, 12 Jul 2015 21:56:53 -0400 Subject: [PATCH] Move initialization of components to init functions 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 | 31 +--- include/babeltrace/plugin/component-factory.h | 6 +- .../babeltrace/plugin/component-internal.h | 11 +- .../plugin/notification/iterator-internal.h | 10 +- .../babeltrace/plugin/notification/iterator.h | 3 +- include/babeltrace/plugin/plugin-system.h | 133 +++++++++++++++--- include/babeltrace/plugin/plugin.h | 8 +- include/babeltrace/plugin/sink-internal.h | 9 ++ include/babeltrace/plugin/source-internal.h | 11 +- plugins/component.c | 18 +-- plugins/iterator.c | 36 ++++- plugins/sink.c | 16 +-- plugins/source.c | 17 +-- 13 files changed, 193 insertions(+), 116 deletions(-) diff --git a/include/babeltrace/plugin/component-factory-internal.h b/include/babeltrace/plugin/component-factory-internal.h index 69c16419..0508cd86 100644 --- a/include/babeltrace/plugin/component-factory-internal.h +++ b/include/babeltrace/plugin/component-factory-internal.h @@ -31,33 +31,8 @@ #include #include -/** Component initialization functions */ -/** - * Allocate a source component. - * - * @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_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_init_cb iterator_init_cb); - -/** - * Allocate a sink component. - * - * @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 notification_cb Notification handling callback - * @returns A sink component instance - */ -BT_HIDDEN -extern struct bt_component *bt_component_sink_create(const char *name, - void *private_data, bt_component_destroy_cb destroy_func, - bt_component_sink_handle_notification_cb notification_cb); +struct bt_component_factory { + int a; +}; #endif /* BABELTRACE_PLUGIN_COMPONENT_FACTORY_INTERNAL_H */ diff --git a/include/babeltrace/plugin/component-factory.h b/include/babeltrace/plugin/component-factory.h index 29e2eb5f..350bf72d 100644 --- a/include/babeltrace/plugin/component-factory.h +++ b/include/babeltrace/plugin/component-factory.h @@ -41,13 +41,11 @@ enum bt_component_status bt_component_factory_create(const char *path); enum bt_component_status bt_component_factory_register_source_component_class( struct bt_component_factory *factory, const char *name, - bt_component_init_cb init, bt_component_fini_cb fini, - bt_component_source_iterator_create_cb iterator_create_cb); + bt_component_source_init_cb init); enum bt_component_status bt_component_factory_register_sink_component_class( struct bt_component_factory *factory, const char *name, - bt_component_init_cb init, bt_component_fini_cb fini, - bt_component_sink_handle_notification_cb handle_notification_cb); + bt_component_sink_init_cb init); void bt_component_factory_destroy(struct bt_component_factory *factory); diff --git a/include/babeltrace/plugin/component-internal.h b/include/babeltrace/plugin/component-internal.h index ea6c489d..7f35eaf5 100644 --- a/include/babeltrace/plugin/component-internal.h +++ b/include/babeltrace/plugin/component-internal.h @@ -40,17 +40,16 @@ struct bt_component { enum bt_component_type type; /** No ownership taken */ FILE *error_stream; + /** source, sink or filter destroy */ + bt_component_destroy_cb destroy; void *user_data; - bt_component_destroy_cb user_data_destroy; - bt_component_destroy_cb destroy; + bt_component_destroy_cb user_destroy; }; BT_HIDDEN 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); + const char *name, enum bt_component_type component_type, + bt_component_destroy_cb destroy); #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 6d1676e1..5a473776 100644 --- a/include/babeltrace/plugin/notification/iterator-internal.h +++ b/include/babeltrace/plugin/notification/iterator-internal.h @@ -36,6 +36,7 @@ struct bt_notification_iterator { bt_notification_iterator_get_cb get; bt_notification_iterator_next_cb next; void *user_data; + bt_notification_iterator_destroy_cb user_destroy; }; /** @@ -58,13 +59,4 @@ 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 22807e41..480f50c7 100644 --- a/include/babeltrace/plugin/notification/iterator.h +++ b/include/babeltrace/plugin/notification/iterator.h @@ -116,8 +116,7 @@ bt_notification_iterator_next(struct bt_notification_iterator *iterator); * @see bt_notification_iterator_get_notification() */ extern enum bt_notification_iterator_status *bt_notification_iterator_seek( - struct bt_notification_iterator *iterator, - int whence, + struct bt_notification_iterator *iterator, int whence, int64_t time); /** diff --git a/include/babeltrace/plugin/plugin-system.h b/include/babeltrace/plugin/plugin-system.h index 9a933200..6eee12fb 100644 --- a/include/babeltrace/plugin/plugin-system.h +++ b/include/babeltrace/plugin/plugin-system.h @@ -49,20 +49,92 @@ struct bt_component; typedef void (*bt_component_destroy_cb)(struct bt_component *component); /** - * Iterator creation function type. + * Source component initialization function type. + * + * A source component's iterator initialization callback, private data and + * deinitialization callback must be set by this function. + * + * @param component Component instance + */ +typedef struct bt_component *(*bt_component_source_init_cb)( + struct bt_component *component); + +/** + * Sink component initialization function type. + * + * A sink component's notification handling callback, private data and + * deinitialization callback must be set by this function. * * @param component Component instance */ +typedef struct bt_component *(*bt_component_sink_init_cb)( + struct bt_component *component); + +/** + * Iterator initialization function type. + * + * @param component Component instance + * @param iterator Iterator instance + */ 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); +/** + * Get a component's private data. + * + * @param component Component of which to get the private data + * @returns Component's private data + */ +extern void *bt_component_get_private_data(struct bt_component *component); -typedef struct bt_component *(*bt_component_fini_cb)( - struct bt_component *component); +/** + * Set a component's private data. + * + * @param component Component of which to set the private data + * @param data Component private data + * @returns One of #bt_component_status values + */ +extern enum bt_component_status bt_component_set_private_data( + struct bt_component *component, void *data); + +/** + * Set a component's private data cleanup function. + * + * @param component Component of which to set the private data destruction + * function + * @param data Component private data clean-up function + * @returns One of #bt_component_status values + */ +extern enum bt_component_status bt_component_set_destroy_cb( + struct bt_component *component, + bt_component_destroy_cb destroy); + +/** bt_component_souce */ +/** + * Iterator initialization function type. + * + * A notification iterator's private data, deinitialization, next, and get + * callbacks must be set by this function. + * + * @param component Component instance + * @param iterator Notification iterator instance + */ +typedef enum bt_component_status (*bt_component_source_init_iterator_cb)( + struct bt_component *component, + struct bt_notification_iterator *iterator); +/** + * Set a source component's iterator initialization function. + * + * @param component Component instance + * @param init_iterator Notification iterator initialization callback + */ +extern enum bt_component_status +bt_component_source_set_iterator_init_cb(struct bt_component *component, + bt_component_source_init_iterator_cb init_iterator); + +/** bt_component_sink */ /** * Notification handling function type. * @@ -73,19 +145,43 @@ typedef struct bt_component *(*bt_component_fini_cb)( typedef enum bt_component_status (*bt_component_sink_handle_notification_cb)( struct bt_component *, struct bt_notification *); +/** + * Set a sink component's notification handling callback. + * + * @param component Component instance + * @param handle_notification Notification handling callback + * @returns One of #bt_component_status values + */ +extern enum bt_component_status +bt_component_sink_set_handle_notification_cb(struct bt_component *component, + bt_component_sink_handle_notification_cb handle_notification); + +/** bt_component_notification_iterator */ +/** + * Function returning an iterator's current notification. + * + * @param iterator Notification iterator instance + * @returns A notification instance + */ typedef struct bt_notification *(*bt_notification_iterator_get_cb)( - struct bt_notification_iterator *); + struct bt_notification_iterator *iterator); +/** + * Function advancing an iterator's position. + * + * @param iterator Notification iterator instance + * @returns One of #bt_notification_iterator_status values + */ typedef enum bt_notification_iterator_status (*bt_notification_iterator_next_cb)( - struct bt_notification_iterator *); + struct bt_notification_iterator *iterator); /** - * Get a component's private (implementation) data. + * Function cleaning-up an iterator's private data on destruction. * - * @param component Component of which to get the private data - * @returns Component's private data + * @param iterator Notification iterator instance */ -extern void *bt_component_get_private_data(struct bt_component *component); +typedef void (*bt_notification_iterator_destroy_cb)( + struct bt_notification_iterator *iterator); extern enum bt_notification_iterator_status bt_notification_iterator_set_get_cb(struct bt_notification_iterator *iterator, @@ -95,6 +191,11 @@ 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_destroy_cb( + struct bt_notification_iterator *iterator, + bt_notification_iterator_destroy_cb destroy); + extern enum bt_notification_iterator_status bt_notification_iterator_set_private_data( struct bt_notification_iterator *iterator, void *data); @@ -102,16 +203,6 @@ bt_notification_iterator_set_private_data( extern void *bt_notification_iterator_get_private_data( struct bt_notification_iterator *iterator); -/** - * Set a component's private (implementation) data. - * - * @param component Component of which to set the private data - * @param data Component private data - * @returns One of #bt_component_status values - */ -extern enum bt_component_status bt_component_set_private_data( - struct bt_component *component, void *data); - #ifdef __cplusplus } #endif diff --git a/include/babeltrace/plugin/plugin.h b/include/babeltrace/plugin/plugin.h index ae6dae86..a8e18499 100644 --- a/include/babeltrace/plugin/plugin.h +++ b/include/babeltrace/plugin/plugin.h @@ -41,13 +41,13 @@ struct bt_component_factory *factory)\ { -#define BT_PLUGIN_SOURCE_COMPONENT_CLASS_ENTRY(_name, _init, _fini, _it_cr) \ +#define BT_PLUGIN_SOURCE_COMPONENT_CLASS_ENTRY(_name, _init) \ bt_component_factory_register_source_component_class(factory, \ - _name, _init, _fini, _it_cr); + _name, _init); -#define BT_PLUGIN_SINK_COMPONENT_CLASS_ENTRY(_name, _init, _fini, _hd_notif) \ +#define BT_PLUGIN_SINK_COMPONENT_CLASS_ENTRY(_name, _init) \ bt_component_factory_register_sink_component_class(factory, \ - _name, _init, _fini, _hd_notif); + _name, _init); #define BT_PLUGIN_COMPONENT_CLASSES_END\ \ diff --git a/include/babeltrace/plugin/sink-internal.h b/include/babeltrace/plugin/sink-internal.h index 30617789..b3333387 100644 --- a/include/babeltrace/plugin/sink-internal.h +++ b/include/babeltrace/plugin/sink-internal.h @@ -38,4 +38,13 @@ struct bt_component_sink { bt_component_sink_handle_notification_cb handle_notification; }; +/** + * Allocate a sink component. + * + * @param name Component instance name (will be copied) + * @returns A sink component instance + */ +BT_HIDDEN +extern struct bt_component *bt_component_sink_create(const char *name); + #endif /* BABELTRACE_PLUGIN_SINK_INTERNAL_H */ diff --git a/include/babeltrace/plugin/source-internal.h b/include/babeltrace/plugin/source-internal.h index 03881053..1234339a 100644 --- a/include/babeltrace/plugin/source-internal.h +++ b/include/babeltrace/plugin/source-internal.h @@ -35,7 +35,16 @@ struct bt_component_source { struct bt_component parent; /* Component implementation callbacks */ - bt_component_source_iterator_init_cb init_iterator; + bt_component_source_init_iterator_cb init_iterator; }; +/** + * Allocate a source component. + * + * @param name Component instance name (will be copied) + * @returns A source component instance + */ +BT_HIDDEN +extern struct bt_component *bt_component_source_create(const char *name); + #endif /* BABELTRACE_PLUGIN_SOURCE_INTERNAL_H */ diff --git a/plugins/component.c b/plugins/component.c index f977c837..7dbdaaae 100644 --- a/plugins/component.c +++ b/plugins/component.c @@ -109,30 +109,24 @@ void bt_component_put(struct bt_component *component) BT_HIDDEN enum bt_component_status bt_component_init(struct bt_component *component, - const char *name, void *user_data, - bt_component_destroy_cb user_destroy_func, - enum bt_component_type component_type, - bt_component_destroy_cb component_destroy) + const char *name, enum bt_component_type component_type, + bt_component_destroy_cb destroy) { enum bt_component_status ret = BT_COMPONENT_STATUS_OK; - if (!component || !name || name[0] == '\0' || - !user_destroy_func || !user_data || !component_destroy) { + if (!component || !name || name[0] == '\0' || destroy) { ret = BT_COMPONENT_STATUS_INVAL; goto end; } bt_ctf_ref_init(&component->ref_count); component->type = component_type; - component->user_data = user_data; - component->user_data_destroy = user_destroy_func; - component->destroy = component_destroy; - component->name = g_string_new(name); if (!component->name) { ret = BT_COMPONENT_STATUS_NOMEM; goto end; } + component->destroy = destroy; end: return ret; } @@ -181,8 +175,8 @@ void bt_component_destroy(struct bt_ctf_ref *ref) * User data is destroyed first, followed by the concrete component * instance. */ - assert(!component->user_data || component->user_data_destroy); - component->user_data_destroy(component->user_data); + assert(!component->user_data || component->user_destroy); + component->user_destroy(component->user_data); g_string_free(component->name, TRUE); diff --git a/plugins/iterator.c b/plugins/iterator.c index 66f918c8..094c2ff0 100644 --- a/plugins/iterator.c +++ b/plugins/iterator.c @@ -33,9 +33,19 @@ #include static -void bt_notification_iterator_destroy(struct bt_notification_iterator *iterator) +void bt_notification_iterator_destroy(struct bt_ctf_ref *ref) { - + struct bt_notification_iterator *iterator; + + if (!ref) { + return; + } + + iterator = container_of(ref, struct bt_notification_iterator, + ref_count); + assert(iterator->user_destroy || !iterator->user_data); + iterator->user_destroy(iterator); + g_free(iterator); } BT_HIDDEN @@ -44,7 +54,8 @@ struct bt_notification_iterator *bt_notification_iterator_create( { struct bt_notification_iterator *iterator = NULL; - if (!component || bt_component_get_type(component) != BT_COMPONENT_TYPE_SOURCE) { + if (!component || + bt_component_get_type(component) != BT_COMPONENT_TYPE_SOURCE) { goto end; } @@ -62,7 +73,8 @@ 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; + enum bt_notification_iterator_status ret = + BT_NOTIFICATION_ITERATOR_STATUS_OK; if (!iterator || !iterator->get || !iterator->next) { ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; @@ -89,3 +101,19 @@ void bt_notification_iterator_put(struct bt_notification_iterator *iterator) bt_ctf_ref_put(&iterator->ref_count, bt_notification_iterator_destroy); } + +enum bt_notification_iterator_status bt_notification_iterator_set_get_cb( + struct bt_notification_iterator *iterator, + bt_notification_iterator_get_cb get) +{ + enum bt_notification_iterator_status ret; + + if (!iterator || !get) { + ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; + goto end; + } + + +end: + return ret; +} diff --git a/plugins/sink.c b/plugins/sink.c index fcbc9485..9647f219 100644 --- a/plugins/sink.c +++ b/plugins/sink.c @@ -43,32 +43,24 @@ void bt_component_sink_destroy(struct bt_component *component) g_free(sink); } -struct bt_component *bt_component_sink_create(const char *name, - void *private_data, bt_component_destroy_cb destroy_func, - bt_component_sink_handle_notification_cb notification_cb) +BT_HIDDEN +struct bt_component *bt_component_sink_create(const char *name) { struct bt_component_sink *sink = NULL; enum bt_component_status ret; - if (!notification_cb) { - goto end; - } - sink = g_new0(struct bt_component_sink, 1); if (!sink) { goto end; } - ret = bt_component_init(&sink->parent, name, private_data, - destroy_func, BT_COMPONENT_TYPE_SINK, - bt_component_sink_destroy); + ret = bt_component_init(&sink->parent, name, BT_COMPONENT_TYPE_SINK, + bt_component_sink_destroy); if (ret != BT_COMPONENT_STATUS_OK) { g_free(sink); sink = NULL; goto end; } - - sink->handle_notification = notification_cb; end: return sink ? &sink->parent : NULL; } diff --git a/plugins/source.c b/plugins/source.c index b722059b..094cab24 100644 --- a/plugins/source.c +++ b/plugins/source.c @@ -45,32 +45,23 @@ void bt_component_source_destroy(struct bt_component *component) g_free(source); } -struct bt_component *bt_component_source_create(const char *name, - void *private_data, bt_component_destroy_cb destroy_func, - bt_component_source_iterator_init_cb iterator_init_cb) +BT_HIDDEN +struct bt_component *bt_component_source_create(const char *name) { struct bt_component_source *source = NULL; enum bt_component_status ret; - - if (!iterator_init_cb) { - goto end; - } - source = g_new0(struct bt_component_source, 1); if (!source) { goto end; } - ret = bt_component_init(&source->parent, name, private_data, - destroy_func, BT_COMPONENT_TYPE_SOURCE, - bt_component_source_destroy); + ret = bt_component_init(&source->parent, name, + BT_COMPONENT_TYPE_SOURCE, bt_component_source_destroy); if (ret != BT_COMPONENT_STATUS_OK) { g_free(source); source = NULL; goto end; } - - source->init_iterator = iterator_init_cb; end: return source ? &source->parent : NULL; } -- 2.34.1