X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=src%2Fbindings%2Fpython%2Fbt2%2Fbt2%2Fnative_bt_component_class.i.h;h=b2c616226e6afc323a1db765aaac56dd262665b5;hp=a80d059d052b7964d16d4f48ea4933053ca1d57a;hb=0235b0db7de5bcacdb3650c92461f2ce5eb2143d;hpb=981f33ddf743a4f38b959afb7afe66083ce0ed76 diff --git a/src/bindings/python/bt2/bt2/native_bt_component_class.i.h b/src/bindings/python/bt2/bt2/native_bt_component_class.i.h index a80d059d..b2c61622 100644 --- a/src/bindings/python/bt2/bt2/native_bt_component_class.i.h +++ b/src/bindings/python/bt2/bt2/native_bt_component_class.i.h @@ -1,25 +1,7 @@ /* - * The MIT License (MIT) + * SPDX-License-Identifier: MIT * * Copyright (c) 2017 Philippe Proulx - * - * 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 "logging/comp-logging.h" @@ -43,6 +25,8 @@ * a BT component object instance. */ +#define BT_FMT_SWIG_ALLOC_FAILED "Failed to create a SWIG pointer object." + static GHashTable *bt_cc_ptr_to_py_cls; static @@ -234,7 +218,7 @@ bt_component_class_initialize_method_status component_class_init( SWIGTYPE_p_bt_value, 0); if (!py_params_ptr) { BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Failed to create a SWIG pointer object."); + BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -242,7 +226,7 @@ bt_component_class_initialize_method_status component_class_init( self_comp_cls_type_swig_type, 0); if (!py_comp_ptr) { BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Failed to create a SWIG pointer object."); + BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -324,7 +308,7 @@ component_class_get_supported_mip_versions( SWIGTYPE_p_bt_value, 0); if (!py_params_ptr) { BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, - "Failed to create a SWIG pointer object."); + BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -524,35 +508,136 @@ end: Py_DECREF(py_comp); } +/* Decref the Python object in the user data associated to `port`. */ + +static +void delete_port_user_data(bt_self_component_port *port) +{ + Py_DECREF(bt_self_component_port_get_data(port)); +} + +static +void delete_port_input_user_data(bt_self_component_port_input *port_input) +{ + bt_self_component_port *port = + bt_self_component_port_input_as_self_component_port(port_input); + + delete_port_user_data(port); +} + +static +void delete_port_output_user_data(bt_self_component_port_output *port_output) +{ + bt_self_component_port *port = + bt_self_component_port_output_as_self_component_port(port_output); + + delete_port_user_data(port); +} + static void component_class_source_finalize(bt_self_component_source *self_component_source) { - bt_self_component *self_component = bt_self_component_source_as_self_component(self_component_source); + uint64_t i; + bt_self_component *self_component; + const bt_component_source *component_source; + + self_component = bt_self_component_source_as_self_component( + self_component_source); + component_source = bt_self_component_source_as_component_source( + self_component_source); + component_class_finalize(self_component); + + /* + * Free the user data Python object attached to the port. The + * corresponding incref was done by the `void *` typemap in + * native_bt_port.i. + */ + for (i = 0; i < bt_component_source_get_output_port_count(component_source); i++) { + bt_self_component_port_output *port_output; + + port_output = bt_self_component_source_borrow_output_port_by_index( + self_component_source, i); + + delete_port_output_user_data(port_output); + } } static void component_class_filter_finalize(bt_self_component_filter *self_component_filter) { - bt_self_component *self_component = bt_self_component_filter_as_self_component(self_component_filter); + uint64_t i; + bt_self_component *self_component; + const bt_component_filter *component_filter; + + self_component = bt_self_component_filter_as_self_component( + self_component_filter); + component_filter = bt_self_component_filter_as_component_filter( + self_component_filter); + component_class_finalize(self_component); + + /* + * Free the user data Python object attached to the port. The + * corresponding incref was done by the `void *` typemap in + * native_bt_port.i. + */ + for (i = 0; i < bt_component_filter_get_input_port_count(component_filter); i++) { + bt_self_component_port_input *port_input; + + port_input = bt_self_component_filter_borrow_input_port_by_index( + self_component_filter, i); + + delete_port_input_user_data(port_input); + } + + for (i = 0; i < bt_component_filter_get_output_port_count(component_filter); i++) { + bt_self_component_port_output *port_output; + + port_output = bt_self_component_filter_borrow_output_port_by_index( + self_component_filter, i); + + delete_port_output_user_data(port_output); + } } static void component_class_sink_finalize(bt_self_component_sink *self_component_sink) { - bt_self_component *self_component = bt_self_component_sink_as_self_component(self_component_sink); + uint64_t i; + bt_self_component *self_component; + const bt_component_sink *component_sink; + + self_component = bt_self_component_sink_as_self_component( + self_component_sink); + component_sink = bt_self_component_sink_as_component_sink( + self_component_sink); + component_class_finalize(self_component); + + /* + * Free the user data Python object attached to the port. The + * corresponding incref was done by the `void *` typemap in + * native_bt_port.i. + */ + for (i = 0; i < bt_component_sink_get_input_port_count(component_sink); i++) { + bt_self_component_port_input *port_input; + + port_input = bt_self_component_sink_borrow_input_port_by_index( + self_component_sink, i); + + delete_port_input_user_data(port_input); + } } static -bt_component_class_message_iterator_can_seek_beginning_method_status +bt_message_iterator_class_can_seek_beginning_method_status component_class_can_seek_beginning( bt_self_message_iterator *self_message_iterator, bt_bool *can_seek) { PyObject *py_iter; PyObject *py_result = NULL; - bt_component_class_message_iterator_can_seek_beginning_method_status status; + bt_message_iterator_class_can_seek_beginning_method_status status; py_iter = bt_self_message_iterator_get_data(self_message_iterator); BT_ASSERT(py_iter); @@ -567,7 +652,7 @@ component_class_can_seek_beginning( BT_ASSERT(PyBool_Check(py_result)); *can_seek = PyObject_IsTrue(py_result); - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; + status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; end: Py_XDECREF(py_result); @@ -576,12 +661,12 @@ end: } static -bt_component_class_message_iterator_seek_beginning_method_status +bt_message_iterator_class_seek_beginning_method_status component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) { PyObject *py_iter; PyObject *py_result; - bt_component_class_message_iterator_seek_beginning_method_status status; + bt_message_iterator_class_seek_beginning_method_status status; py_iter = bt_self_message_iterator_get_data(self_message_iterator); BT_ASSERT(py_iter); @@ -596,7 +681,7 @@ component_class_seek_beginning(bt_self_message_iterator *self_message_iterator) BT_ASSERT(py_result == Py_None); - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK; + status = BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK; end: Py_XDECREF(py_result); @@ -605,14 +690,14 @@ end: } static -bt_component_class_message_iterator_can_seek_ns_from_origin_method_status +bt_message_iterator_class_can_seek_ns_from_origin_method_status component_class_can_seek_ns_from_origin( bt_self_message_iterator *self_message_iterator, int64_t ns_from_origin, bt_bool *can_seek) { PyObject *py_iter; PyObject *py_result = NULL; - bt_component_class_message_iterator_can_seek_ns_from_origin_method_status status; + bt_message_iterator_class_can_seek_ns_from_origin_method_status status; py_iter = bt_self_message_iterator_get_data(self_message_iterator); BT_ASSERT(py_iter); @@ -627,7 +712,7 @@ component_class_can_seek_ns_from_origin( BT_ASSERT(PyBool_Check(py_result)); *can_seek = PyObject_IsTrue(py_result); - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; + status = BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; end: Py_XDECREF(py_result); @@ -636,14 +721,14 @@ end: } static -bt_component_class_message_iterator_seek_ns_from_origin_method_status +bt_message_iterator_class_seek_ns_from_origin_method_status component_class_seek_ns_from_origin( bt_self_message_iterator *self_message_iterator, int64_t ns_from_origin) { PyObject *py_iter; PyObject *py_result; - bt_component_class_message_iterator_seek_ns_from_origin_method_status status; + bt_message_iterator_class_seek_ns_from_origin_method_status status; py_iter = bt_self_message_iterator_get_data(self_message_iterator); BT_ASSERT(py_iter); @@ -658,7 +743,7 @@ component_class_seek_ns_from_origin( BT_ASSERT(py_result == Py_None); - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; + status = BT_MESSAGE_ITERATOR_CLASS_SEEK_NS_FROM_ORIGIN_METHOD_STATUS_OK; end: Py_XDECREF(py_result); @@ -689,7 +774,7 @@ bt_component_class_port_connected_method_status component_class_port_connected( self_component_port_swig_type, 0); if (!py_self_port_ptr) { BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Failed to create a SWIG pointer object."); + BT_FMT_SWIG_ALLOC_FAILED); status = __BT_FUNC_STATUS_MEMORY_ERROR; goto end; } @@ -698,7 +783,7 @@ bt_component_class_port_connected_method_status component_class_port_connected( other_port_swig_type, 0); if (!py_other_port_ptr) { BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "Failed to create a SWIG pointer object."); + BT_FMT_SWIG_ALLOC_FAILED); status = __BT_FUNC_STATUS_MEMORY_ERROR; goto end; } @@ -864,7 +949,7 @@ bt_component_class_query_method_status component_class_query( SWIGTYPE_p_bt_value, 0); if (!py_params_ptr) { BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, - "Failed to create a SWIG pointer object."); + BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -873,7 +958,7 @@ bt_component_class_query_method_status component_class_query( SWIGTYPE_p_bt_private_query_executor, 0); if (!py_priv_query_exec_ptr) { BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG, - "Failed to create a SWIG pointer object."); + BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -978,14 +1063,13 @@ bt_component_class_query_method_status component_class_sink_query( } static -bt_component_class_message_iterator_initialize_method_status +bt_message_iterator_class_initialize_method_status component_class_message_iterator_init( bt_self_message_iterator *self_message_iterator, bt_self_message_iterator_configuration *config, - bt_self_component *self_component, bt_self_component_port_output *self_component_port_output) { - bt_component_class_message_iterator_initialize_method_status status = __BT_FUNC_STATUS_OK; + bt_message_iterator_class_initialize_method_status status = __BT_FUNC_STATUS_OK; PyObject *py_comp_cls = NULL; PyObject *py_iter_cls = NULL; PyObject *py_iter_ptr = NULL; @@ -994,6 +1078,9 @@ component_class_message_iterator_init( PyObject *py_init_method_result = NULL; PyObject *py_iter = NULL; PyObject *py_comp; + bt_self_component *self_component = + bt_self_message_iterator_borrow_component( + self_message_iterator); bt_logging_level log_level = get_self_component_log_level( self_component); @@ -1017,12 +1104,10 @@ component_class_message_iterator_init( py_iter_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(self_message_iterator), SWIGTYPE_p_bt_self_message_iterator, 0); if (!py_iter_ptr) { - const char *err = "Failed to create a SWIG pointer object."; - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "%s", err); + BT_FMT_SWIG_ALLOC_FAILED); BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( - self_message_iterator, err); + self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -1056,12 +1141,10 @@ component_class_message_iterator_init( py_config_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(config), SWIGTYPE_p_bt_self_message_iterator_configuration, 0); if (!py_config_ptr) { - const char *err = "Failed to create a SWIG pointer object"; - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "%s", err); + BT_FMT_SWIG_ALLOC_FAILED); BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( - self_message_iterator, err); + self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -1069,12 +1152,10 @@ component_class_message_iterator_init( SWIG_as_voidptr(self_component_port_output), SWIGTYPE_p_bt_self_component_port_output, 0); if (!py_component_port_output_ptr) { - const char *err = "Failed to create a SWIG pointer object."; - BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_component, - "%s", err); + "%s", BT_FMT_SWIG_ALLOC_FAILED); BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR( - self_message_iterator, err); + self_message_iterator, BT_FMT_SWIG_ALLOC_FAILED); goto error; } @@ -1130,36 +1211,6 @@ end: return status; } -static -bt_component_class_message_iterator_initialize_method_status -component_class_source_message_iterator_init( - bt_self_message_iterator *self_message_iterator, - bt_self_message_iterator_configuration *config, - bt_self_component_source *self_component_source, - bt_self_component_port_output *self_component_port_output) -{ - bt_self_component *self_component = - bt_self_component_source_as_self_component(self_component_source); - - return component_class_message_iterator_init(self_message_iterator, - config, self_component, self_component_port_output); -} - -static -bt_component_class_message_iterator_initialize_method_status -component_class_filter_message_iterator_init( - bt_self_message_iterator *self_message_iterator, - bt_self_message_iterator_configuration *config, - bt_self_component_filter *self_component_filter, - bt_self_component_port_output *self_component_port_output) -{ - bt_self_component *self_component = - bt_self_component_filter_as_self_component(self_component_filter); - - return component_class_message_iterator_init(self_message_iterator, - config, self_component, self_component_port_output); -} - static void component_class_message_iterator_finalize( bt_self_message_iterator *message_iterator) @@ -1198,18 +1249,17 @@ void component_class_message_iterator_finalize( /* Valid for both sources and filters. */ static -bt_component_class_message_iterator_next_method_status +bt_message_iterator_class_next_method_status component_class_message_iterator_next( bt_self_message_iterator *message_iterator, bt_message_array_const msgs, uint64_t capacity, uint64_t *count) { - bt_component_class_message_iterator_next_method_status status; + bt_message_iterator_class_next_method_status status; PyObject *py_message_iter = bt_self_message_iterator_get_data(message_iterator); PyObject *py_method_result = NULL; - BT_ASSERT(py_message_iter); - + BT_ASSERT_DBG(py_message_iter); py_method_result = PyObject_CallMethod(py_message_iter, "_bt_next_from_native", NULL); if (!py_method_result) { @@ -1226,9 +1276,9 @@ component_class_message_iterator_next( *count = 1; /* Overflow errors should never happen. */ - BT_ASSERT(!PyErr_Occurred()); + BT_ASSERT_DBG(!PyErr_Occurred()); - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; end: Py_XDECREF(py_method_result); @@ -1244,7 +1294,7 @@ component_class_sink_consume(bt_self_component_sink *self_component_sink) PyObject *py_method_result = NULL; bt_component_class_sink_consume_method_status status; - BT_ASSERT(py_comp); + BT_ASSERT_DBG(py_comp); py_method_result = PyObject_CallMethod(py_comp, "_user_consume", NULL); @@ -1291,18 +1341,57 @@ end: return ret; } +static +bt_message_iterator_class *create_message_iterator_class(void) +{ + bt_message_iterator_class *message_iterator_class; + bt_message_iterator_class_set_method_status ret; + + message_iterator_class = bt_message_iterator_class_create( + component_class_message_iterator_next); + if (!message_iterator_class) { + BT_LOGE_STR("Cannot create message iterator class."); + goto end; + } + + ret = bt_message_iterator_class_set_seek_beginning_methods( + message_iterator_class, component_class_seek_beginning, + component_class_can_seek_beginning); + BT_ASSERT(ret == 0); + ret = bt_message_iterator_class_set_seek_ns_from_origin_methods( + message_iterator_class, component_class_seek_ns_from_origin, + component_class_can_seek_ns_from_origin); + BT_ASSERT(ret == 0); + ret = bt_message_iterator_class_set_initialize_method( + message_iterator_class, component_class_message_iterator_init); + BT_ASSERT(ret == 0); + ret = bt_message_iterator_class_set_finalize_method( + message_iterator_class, component_class_message_iterator_finalize); + BT_ASSERT(ret == 0); + +end: + return message_iterator_class; +} + static bt_component_class_source *bt_bt2_component_class_source_create( PyObject *py_cls, const char *name, const char *description, const char *help) { - bt_component_class_source *component_class_source; + bt_component_class_source *component_class_source = NULL; + bt_message_iterator_class *message_iterator_class; bt_component_class *component_class; int ret; BT_ASSERT(py_cls); + + message_iterator_class = create_message_iterator_class(); + if (!message_iterator_class) { + goto end; + } + component_class_source = bt_component_class_source_create(name, - component_class_message_iterator_next); + message_iterator_class); if (!component_class_source) { BT_LOGE_STR("Cannot create source component class."); goto end; @@ -1318,12 +1407,6 @@ bt_component_class_source *bt_bt2_component_class_source_create( BT_ASSERT(ret == 0); ret = bt_component_class_source_set_finalize_method(component_class_source, component_class_source_finalize); BT_ASSERT(ret == 0); - ret = bt_component_class_source_set_message_iterator_seek_beginning_methods(component_class_source, - component_class_seek_beginning, component_class_can_seek_beginning); - ret = bt_component_class_source_set_message_iterator_seek_ns_from_origin_methods( - component_class_source, component_class_seek_ns_from_origin, - component_class_can_seek_ns_from_origin); - BT_ASSERT(ret == 0); ret = bt_component_class_source_set_output_port_connected_method(component_class_source, component_class_source_output_port_connected); BT_ASSERT(ret == 0); @@ -1331,15 +1414,10 @@ bt_component_class_source *bt_bt2_component_class_source_create( BT_ASSERT(ret == 0); ret = bt_component_class_source_set_get_supported_mip_versions_method(component_class_source, component_class_source_get_supported_mip_versions); BT_ASSERT(ret == 0); - ret = bt_component_class_source_set_message_iterator_initialize_method( - component_class_source, component_class_source_message_iterator_init); - BT_ASSERT(ret == 0); - ret = bt_component_class_source_set_message_iterator_finalize_method( - component_class_source, component_class_message_iterator_finalize); - BT_ASSERT(ret == 0); register_cc_ptr_to_py_cls(component_class, py_cls); end: + bt_message_iterator_class_put_ref(message_iterator_class); return component_class_source; } @@ -1348,13 +1426,20 @@ bt_component_class_filter *bt_bt2_component_class_filter_create( PyObject *py_cls, const char *name, const char *description, const char *help) { + bt_component_class_filter *component_class_filter = NULL; + bt_message_iterator_class *message_iterator_class; bt_component_class *component_class; - bt_component_class_filter *component_class_filter; int ret; BT_ASSERT(py_cls); + + message_iterator_class = create_message_iterator_class(); + if (!message_iterator_class) { + goto end; + } + component_class_filter = bt_component_class_filter_create(name, - component_class_message_iterator_next); + message_iterator_class); if (!component_class_filter) { BT_LOGE_STR("Cannot create filter component class."); goto end; @@ -1370,12 +1455,6 @@ bt_component_class_filter *bt_bt2_component_class_filter_create( BT_ASSERT(ret == 0); ret = bt_component_class_filter_set_finalize_method (component_class_filter, component_class_filter_finalize); BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_message_iterator_seek_beginning_methods(component_class_filter, - component_class_seek_beginning, component_class_can_seek_beginning); - BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_message_iterator_seek_ns_from_origin_methods( - component_class_filter, component_class_seek_ns_from_origin, - component_class_can_seek_ns_from_origin); ret = bt_component_class_filter_set_input_port_connected_method(component_class_filter, component_class_filter_input_port_connected); BT_ASSERT(ret == 0); @@ -1386,15 +1465,10 @@ bt_component_class_filter *bt_bt2_component_class_filter_create( BT_ASSERT(ret == 0); ret = bt_component_class_filter_set_get_supported_mip_versions_method(component_class_filter, component_class_filter_get_supported_mip_versions); BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_message_iterator_initialize_method( - component_class_filter, component_class_filter_message_iterator_init); - BT_ASSERT(ret == 0); - ret = bt_component_class_filter_set_message_iterator_finalize_method( - component_class_filter, component_class_message_iterator_finalize); - BT_ASSERT(ret == 0); register_cc_ptr_to_py_cls(component_class, py_cls); end: + bt_message_iterator_class_put_ref(message_iterator_class); return component_class_filter; }