From 634d474be8d4262f5a3e549cdb6891de239f4fe4 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Fri, 17 Feb 2012 14:39:27 -0500 Subject: [PATCH] API fix: Move callbacks to CTF plugin Signed-off-by: Mathieu Desnoyers --- formats/ctf/Makefile.am | 3 +- {lib => formats/ctf}/callbacks.c | 11 +- formats/ctf/events.c | 32 +++++- include/Makefile.am | 5 +- include/babeltrace/babeltrace.h | 72 ------------- .../babeltrace/{ => ctf}/callbacks-internal.h | 3 +- include/babeltrace/ctf/callbacks.h | 100 ++++++++++++++++++ include/babeltrace/ctf/events-internal.h | 20 ++++ include/babeltrace/iterator-internal.h | 18 ---- lib/Makefile.am | 1 - lib/iterator.c | 30 ------ 11 files changed, 162 insertions(+), 133 deletions(-) rename {lib => formats/ctf}/callbacks.c (95%) rename include/babeltrace/{ => ctf}/callbacks-internal.h (95%) create mode 100644 include/babeltrace/ctf/callbacks.h diff --git a/formats/ctf/Makefile.am b/formats/ctf/Makefile.am index cf01be4b..f9468fcb 100644 --- a/formats/ctf/Makefile.am +++ b/formats/ctf/Makefile.am @@ -6,7 +6,8 @@ lib_LTLIBRARIES = libctf.la libctf_la_SOURCES = \ ctf.c \ - events.c + events.c \ + callbacks.c libctf_la_LIBADD = \ types/libctf-types.la \ diff --git a/lib/callbacks.c b/formats/ctf/callbacks.c similarity index 95% rename from lib/callbacks.c rename to formats/ctf/callbacks.c index fa212c91..f4e0a145 100644 --- a/lib/callbacks.c +++ b/formats/ctf/callbacks.c @@ -20,12 +20,13 @@ #include #include -#include #include #include #include #include #include +#include +#include #include static @@ -59,9 +60,9 @@ struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...) } /* - * bt_iter_add_callback: Add a callback to iterator. + * bt_ctf_iter_add_callback: Add a callback to CTF iterator. */ -int bt_iter_add_callback(struct bt_iter *iter, +int bt_ctf_iter_add_callback(struct bt_ctf_iter *iter, bt_intern_str event, void *private_data, int flags, enum bt_cb_ret (*callback)(struct bt_ctf_event *ctf_data, void *private_data), @@ -72,7 +73,7 @@ int bt_iter_add_callback(struct bt_iter *iter, int i, stream_id; gpointer *event_id_ptr; unsigned long event_id; - struct trace_collection *tc = iter->ctx->tc; + struct trace_collection *tc = iter->parent.ctx->tc; for (i = 0; i < tc->array->len; i++) { struct ctf_trace *tin; @@ -170,7 +171,7 @@ struct ctf_stream_event *extract_ctf_stream_event(struct ctf_stream *stream) return event; } -void process_callbacks(struct bt_iter *iter, +void process_callbacks(struct bt_ctf_iter *iter, struct ctf_stream *stream) { struct bt_stream_callbacks *bt_stream_cb; diff --git a/formats/ctf/events.c b/formats/ctf/events.c index 27643fcb..8ecb7c48 100644 --- a/formats/ctf/events.c +++ b/formats/ctf/events.c @@ -49,11 +49,39 @@ struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx, g_free(iter); return NULL; } + iter->callbacks = g_array_new(0, 1, sizeof(struct bt_stream_callbacks)); + iter->recalculate_dep_graph = 0; + iter->main_callbacks.callback = NULL; + iter->dep_gc = g_ptr_array_new(); return iter; } void bt_ctf_iter_destroy(struct bt_ctf_iter *iter) { + struct bt_stream_callbacks *bt_stream_cb; + struct bt_callback_chain *bt_chain; + int i, j; + + /* free all events callbacks */ + if (iter->main_callbacks.callback) + g_array_free(iter->main_callbacks.callback, TRUE); + + /* free per-event callbacks */ + for (i = 0; i < iter->callbacks->len; i++) { + bt_stream_cb = &g_array_index(iter->callbacks, + struct bt_stream_callbacks, i); + if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks) + continue; + for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) { + bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks, + struct bt_callback_chain, j); + if (bt_chain->callback) { + g_array_free(bt_chain->callback, TRUE); + } + } + g_array_free(bt_stream_cb->per_id_callbacks, TRUE); + } + bt_iter_fini(&iter->parent); g_free(iter); } @@ -77,10 +105,10 @@ struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter) ret->event = g_ptr_array_index(ret->stream->events_by_id, ret->stream->event_id); - if (ret->stream->stream_id > iter->parent.callbacks->len) + if (ret->stream->stream_id > iter->callbacks->len) goto end; - process_callbacks(&iter->parent, ret->stream); + process_callbacks(iter, ret->stream); end: return ret; diff --git a/include/Makefile.am b/include/Makefile.am index f4e410d5..e675f9e3 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -8,13 +8,13 @@ babeltraceinclude_HEADERS = \ babeltrace/list.h babeltracectfinclude_HEADERS = \ - babeltrace/ctf/events.h + babeltrace/ctf/events.h \ + babeltrace/ctf/callbacks.h noinst_HEADERS = \ babeltrace/align.h \ babeltrace/babeltrace-internal.h \ babeltrace/bitfield.h \ - babeltrace/callbacks-internal.h \ babeltrace/compiler.h \ babeltrace/context-internal.h \ babeltrace/iterator-internal.h \ @@ -25,4 +25,5 @@ noinst_HEADERS = \ babeltrace/ctf/metadata.h \ babeltrace/ctf-text/types.h \ babeltrace/ctf/types.h \ + babeltrace/ctf/callbacks-internal.h \ babeltrace/trace-handle-internal.h diff --git a/include/babeltrace/babeltrace.h b/include/babeltrace/babeltrace.h index e533397c..83917c24 100644 --- a/include/babeltrace/babeltrace.h +++ b/include/babeltrace/babeltrace.h @@ -24,76 +24,4 @@ #include #include -/* Forward declarations */ -struct bt_iter; -struct bt_dependencies; - -enum bt_cb_ret { - BT_CB_OK = 0, - BT_CB_OK_STOP = 1, - BT_CB_ERROR_STOP = 2, - BT_CB_ERROR_CONTINUE = 3, -}; - -/* - * Receives a variable number of strings as parameter, ended with NULL. - */ -struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...); - -/* - * struct bt_dependencies must be destroyed explicitly if not passed as - * parameter to a bt_iter_add_callback(). - */ -void babeltrace_dependencies_destroy(struct bt_dependencies *dep); - -/* - * bt_iter_add_callback: Add a callback to iterator. - * - * @iter: trace collection iterator (input) - * @event: event to target. 0 for all events. - * @private_data: private data pointer to pass to the callback - * @flags: specific flags controlling the behavior of this callback - * (or'd). - * - * @callback: function pointer to call - * @depends: struct bt_dependency detailing the required computation results. - * Ends with 0. - * @weak_depends: struct bt_dependency detailing the optional computation - * results that can be optionally consumed by this - * callback. - * @provides: struct bt_dependency detailing the computation results - * provided by this callback. - * Ends with 0. - * - * "depends", "weak_depends" and "provides" memory is handled by the - * babeltrace library after this call succeeds or fails. These objects - * can still be used by the caller until the babeltrace iterator is - * destroyed, but they belong to the babeltrace library. - * - * (note to implementor: we need to keep a gptrarray of struct - * bt_dependencies to "garbage collect" in struct bt_iter, and - * dependencies need to have a refcount to handle the case where they - * would be passed to more than one iterator. Upon iterator detroy, we - * iterate on all the gc ptrarray and decrement the refcounts, freeing - * if we reach 0.) - * (note to implementor: we calculate the dependency graph when - * bt_iter_read_event() is executed after a - * bt_iter_add_callback(). Beware that it is valid to create/add - * callbacks/read/add more callbacks/read some more.) - */ -int bt_iter_add_callback(struct bt_iter *iter, - bt_intern_str event, void *private_data, int flags, - enum bt_cb_ret (*callback)(struct bt_ctf_event *ctf_data, - void *caller_data), - struct bt_dependencies *depends, - struct bt_dependencies *weak_depends, - struct bt_dependencies *provides); - -/* - * For flags parameter above. - */ -enum { - BT_FLAGS_FREE_PRIVATE_DATA = (1 << 0), -}; - #endif /* _BABELTRACE_H */ diff --git a/include/babeltrace/callbacks-internal.h b/include/babeltrace/ctf/callbacks-internal.h similarity index 95% rename from include/babeltrace/callbacks-internal.h rename to include/babeltrace/ctf/callbacks-internal.h index c1257065..611851ae 100644 --- a/include/babeltrace/callbacks-internal.h +++ b/include/babeltrace/ctf/callbacks-internal.h @@ -52,7 +52,6 @@ struct bt_dependencies { int refcount; /* free when decremented to 0 */ }; -void process_callbacks(struct bt_iter *iter, - struct ctf_stream *stream); +void process_callbacks(struct bt_ctf_iter *iter, struct ctf_stream *stream); #endif /* _BABELTRACE_CALLBACKS_INTERNAL_H */ diff --git a/include/babeltrace/ctf/callbacks.h b/include/babeltrace/ctf/callbacks.h new file mode 100644 index 00000000..c0986f9e --- /dev/null +++ b/include/babeltrace/ctf/callbacks.h @@ -0,0 +1,100 @@ +#ifndef _BABELTRACE_CTF_CALLBACKS_H +#define _BABELTRACE_CTF_CALLBACKS_H + +/* + * BabelTrace + * + * CTF events API + * + * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation + * + * Author: Mathieu Desnoyers + * Julien Desfossez + * + * 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. + */ + +#include + +/* Forward declarations */ +struct bt_ctf_iter; +struct bt_dependencies; + +enum bt_cb_ret { + BT_CB_OK = 0, + BT_CB_OK_STOP = 1, + BT_CB_ERROR_STOP = 2, + BT_CB_ERROR_CONTINUE = 3, +}; + +/* + * Receives a variable number of strings as parameter, ended with NULL. + */ +struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...); + +/* + * struct bt_dependencies must be destroyed explicitly if not passed as + * parameter to a bt_iter_add_callback(). + */ +void babeltrace_dependencies_destroy(struct bt_dependencies *dep); + +/* + * bt_iter_add_callback: Add a callback to iterator. + * + * @iter: trace collection iterator (input) + * @event: event to target. 0 for all events. + * @private_data: private data pointer to pass to the callback + * @flags: specific flags controlling the behavior of this callback + * (or'd). + * + * @callback: function pointer to call + * @depends: struct bt_dependency detailing the required computation results. + * Ends with 0. + * @weak_depends: struct bt_dependency detailing the optional computation + * results that can be optionally consumed by this + * callback. + * @provides: struct bt_dependency detailing the computation results + * provided by this callback. + * Ends with 0. + * + * "depends", "weak_depends" and "provides" memory is handled by the + * babeltrace library after this call succeeds or fails. These objects + * can still be used by the caller until the babeltrace iterator is + * destroyed, but they belong to the babeltrace library. + * + * (note to implementor: we need to keep a gptrarray of struct + * bt_dependencies to "garbage collect" in struct bt_iter, and + * dependencies need to have a refcount to handle the case where they + * would be passed to more than one iterator. Upon iterator detroy, we + * iterate on all the gc ptrarray and decrement the refcounts, freeing + * if we reach 0.) + * (note to implementor: we calculate the dependency graph when + * bt_iter_read_event() is executed after a + * bt_iter_add_callback(). Beware that it is valid to create/add + * callbacks/read/add more callbacks/read some more.) + */ +int bt_ctf_iter_add_callback(struct bt_ctf_iter *iter, + bt_intern_str event, void *private_data, int flags, + enum bt_cb_ret (*callback)(struct bt_ctf_event *ctf_data, + void *caller_data), + struct bt_dependencies *depends, + struct bt_dependencies *weak_depends, + struct bt_dependencies *provides); + +/* + * For flags parameter above. + */ +enum { + BT_FLAGS_FREE_PRIVATE_DATA = (1 << 0), +}; + +#endif /*_BABELTRACE_CTF_CALLBACKS_H */ diff --git a/include/babeltrace/ctf/events-internal.h b/include/babeltrace/ctf/events-internal.h index deff1058..c78470ec 100644 --- a/include/babeltrace/ctf/events-internal.h +++ b/include/babeltrace/ctf/events-internal.h @@ -24,10 +24,30 @@ */ #include +#include +#include +#include struct bt_ctf_iter { struct bt_iter parent; struct bt_ctf_event current_ctf_event; /* last read event */ + GArray *callbacks; /* Array of struct bt_stream_callbacks */ + struct bt_callback_chain main_callbacks; /* For all events */ + /* + * Flag indicating if dependency graph needs to be recalculated. + * Set by bt_iter_add_callback(), and checked (and + * cleared) by upon entry into bt_iter_read_event(). + * bt_iter_read_event() is responsible for calling dep + * graph calculation if it sees this flag set. + */ + int recalculate_dep_graph; + /* + * Array of pointers to struct bt_dependencies, for garbage + * collection. We're not using a linked list here because each + * struct bt_dependencies can belong to more than one + * bt_iter. + */ + GPtrArray *dep_gc; }; #endif /*_BABELTRACE_CTF_EVENTS_INTERNAL_H */ diff --git a/include/babeltrace/iterator-internal.h b/include/babeltrace/iterator-internal.h index c9b5d607..626a7a00 100644 --- a/include/babeltrace/iterator-internal.h +++ b/include/babeltrace/iterator-internal.h @@ -21,7 +21,6 @@ * all copies or substantial portions of the Software. */ -#include #include /* @@ -32,23 +31,6 @@ struct bt_iter { struct ptr_heap *stream_heap; struct bt_context *ctx; struct bt_iter_pos *end_pos; - GArray *callbacks; /* Array of struct bt_stream_callbacks */ - struct bt_callback_chain main_callbacks; /* For all events */ - /* - * Flag indicating if dependency graph needs to be recalculated. - * Set by bt_iter_add_callback(), and checked (and - * cleared) by upon entry into bt_iter_read_event(). - * bt_iter_read_event() is responsible for calling dep - * graph calculation if it sees this flag set. - */ - int recalculate_dep_graph; - /* - * Array of pointers to struct bt_dependencies, for garbage - * collection. We're not using a linked list here because each - * struct bt_dependencies can belong to more than one - * bt_iter. - */ - GPtrArray *dep_gc; }; int bt_iter_init(struct bt_iter *iter, diff --git a/lib/Makefile.am b/lib/Makefile.am index 5d069a96..db6db398 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -5,7 +5,6 @@ AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include lib_LTLIBRARIES = libbabeltrace.la libbabeltrace_la_SOURCES = babeltrace.c \ - callbacks.c \ iterator.c \ context.c \ trace-handle.c \ diff --git a/lib/iterator.c b/lib/iterator.c index bf87193a..3dbe8788 100644 --- a/lib/iterator.c +++ b/lib/iterator.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -445,10 +444,6 @@ int bt_iter_init(struct bt_iter *iter, iter->stream_heap = g_new(struct ptr_heap, 1); iter->end_pos = end_pos; - iter->callbacks = g_array_new(0, 1, sizeof(struct bt_stream_callbacks)); - iter->recalculate_dep_graph = 0; - iter->main_callbacks.callback = NULL; - iter->dep_gc = g_ptr_array_new(); bt_context_get(ctx); iter->ctx = ctx; @@ -524,35 +519,10 @@ struct bt_iter *bt_iter_create(struct bt_context *ctx, void bt_iter_fini(struct bt_iter *iter) { - struct bt_stream_callbacks *bt_stream_cb; - struct bt_callback_chain *bt_chain; - int i, j; - if (iter->stream_heap) { heap_free(iter->stream_heap); g_free(iter->stream_heap); } - - /* free all events callbacks */ - if (iter->main_callbacks.callback) - g_array_free(iter->main_callbacks.callback, TRUE); - - /* free per-event callbacks */ - for (i = 0; i < iter->callbacks->len; i++) { - bt_stream_cb = &g_array_index(iter->callbacks, - struct bt_stream_callbacks, i); - if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks) - continue; - for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) { - bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks, - struct bt_callback_chain, j); - if (bt_chain->callback) { - g_array_free(bt_chain->callback, TRUE); - } - } - g_array_free(bt_stream_cb->per_id_callbacks, TRUE); - } - bt_context_put(iter->ctx); } -- 2.34.1