From 99588ababe6cd5904eb98cebe11e0739bec74787 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 16 Jun 2025 10:08:13 +0200 Subject: [PATCH] remove module.deleter --- src/audio/audio.cpp | 8 +++++--- src/audio/playback/mixer.cpp | 4 ++-- src/host.cpp | 8 ++++---- src/host.h | 3 ++- src/main.cpp | 1 + src/module.c | 39 ++++++++++++++---------------------- src/module.h | 7 ++----- src/rtp/rtp.c | 2 +- src/transmit.cpp | 18 ++++++++--------- src/transmit.h | 3 ++- src/video_rxtx/rtp.cpp | 4 ++-- 11 files changed, 44 insertions(+), 53 deletions(-) diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 1c186434a..1dc922b07 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -9,7 +9,7 @@ * Martin Pulec * Ian Wesley-Smith * - * Copyright (c) 2005-2024 CESNET z.s.p.o. + * Copyright (c) 2005-2025 CESNET * * Redistribution and use in source and binary forms, with or without * modification, is permitted provided that the following conditions @@ -457,7 +457,7 @@ int audio_init(struct state_audio **ret, error: if(s->tx_session) - module_done(CAST_MODULE(s->tx_session)); + tx_done(s->tx_session); if(s->audio_participants) { pdb_destroy(&s->audio_participants); } @@ -534,7 +534,9 @@ void audio_done(struct state_audio *s) free_message(msg, r); } - module_done(CAST_MODULE(s->tx_session)); + if (s->tx_session) { + tx_done(s->tx_session); + } if(s->audio_network_device) rtp_done(s->audio_network_device); if(s->audio_participants) { diff --git a/src/audio/playback/mixer.cpp b/src/audio/playback/mixer.cpp index ebd9b8963..2c2435238 100644 --- a/src/audio/playback/mixer.cpp +++ b/src/audio/playback/mixer.cpp @@ -3,7 +3,7 @@ * @author Martin Pulec */ /* - * Copyright (c) 2016-2024 CESNET + * Copyright (c) 2016-2025 CESNET * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -129,7 +129,7 @@ struct am_participant { } ~am_participant() { if (m_tx_session) { - module_done(CAST_MODULE(m_tx_session)); + tx_done(m_tx_session); } if (m_network_device) { rtp_done(m_network_device); diff --git a/src/host.cpp b/src/host.cpp index 7fe671277..940723394 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -550,9 +550,6 @@ struct state_root { platform_pipe_close(should_exit_pipe[0]); } } - static void deleter(struct module *m) { - delete (state_root*) m->priv_data; - } static void should_exit_watcher(state_root *s) { set_thread_name(__func__); @@ -608,11 +605,14 @@ void init_root_module(struct module *root_mod) { root_mod->cls = MODULE_CLASS_ROOT; root_mod->new_message = nullptr; // note that the root mod messages // processes also the reflector - root_mod->deleter = state_root::deleter; state_root_static = new state_root(); root_mod->priv_data = state_root_static; } +void destroy_root_module(struct module *root_mod) { + delete (state_root *) root_mod->priv_data; +} + /** * Exit function that sets return value and brodcasts registered modules should_exit. * diff --git a/src/host.h b/src/host.h index d4f3acd24..707bd8c23 100644 --- a/src/host.h +++ b/src/host.h @@ -12,7 +12,7 @@ * This file contains common (global) variables and functions. */ /* - * Copyright (c) 2005-2024, CESNET + * Copyright (c) 2005-2025, CESNET * * Redistribution and use in source and binary forms, with or without * modification, is permitted provided that the following conditions @@ -132,6 +132,7 @@ void common_cleanup(struct init_data *init_data); // root module management void init_root_module(struct module *root_mod); +void destroy_root_module(struct module *root_mod); void register_should_exit_callback(struct module *mod, void (*callback)(void *), void *udata); void unregister_should_exit_callback(struct module *mod, void (*callback)(void *), void *udata); diff --git a/src/main.cpp b/src/main.cpp index e7284dd87..6209e8efc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -149,6 +149,7 @@ struct state_uv { } ~state_uv() { module_done(&root_module); + destroy_root_module(&root_module); } struct vidcap *capture_device{}; diff --git a/src/module.c b/src/module.c index fd63a8fb9..7ee653ead 100644 --- a/src/module.c +++ b/src/module.c @@ -3,7 +3,7 @@ * @author Martin Pulec */ /* - * Copyright (c) 2013-2024 CESNET + * Copyright (c) 2013-2025 CESNET * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -108,53 +108,44 @@ void module_done(struct module *module_data) module_mutex_unlock(&module_data->parent->lock); } - // we assume that deleter may dealloc space where are structure stored - module_mutex_lock(&module_data->lock); - struct module tmp; - memcpy(&tmp, module_data, sizeof(struct module)); - module_mutex_unlock(&module_data->lock); - module_data->cls = MODULE_CLASS_NONE; - if(module_data->deleter) - module_data->deleter(module_data); - - if(simple_linked_list_size(tmp.children) > 0) { + if(simple_linked_list_size(module_data->children) > 0) { log_msg(LOG_LEVEL_WARNING, "Warning: Child database not empty! Remaining:\n"); - dump_tree(&tmp, 0); - module_mutex_lock(&tmp.lock); + dump_tree(module_data, 0); + module_mutex_lock(&module_data->lock); for(void *it = simple_linked_list_it_init(module_data->children); it != NULL; ) { struct module *child = simple_linked_list_it_next(&it); module_mutex_lock(&child->lock); child->parent = NULL; module_mutex_unlock(&child->lock); } - module_mutex_unlock(&tmp.lock); + module_mutex_unlock(&module_data->lock); } - simple_linked_list_destroy(tmp.children); + simple_linked_list_destroy(module_data->children); - if(simple_linked_list_size(tmp.msg_queue) > 0) { + if(simple_linked_list_size(module_data->msg_queue) > 0) { fprintf(stderr, "Warning: Message queue not empty!\n"); if (log_level >= LOG_LEVEL_VERBOSE) { printf("Path: "); - dump_parents(&tmp); + dump_parents(module_data); } struct message *m; - while ((m = check_message(&tmp))) { + while ((m = check_message(module_data))) { free_message(m, NULL); } } - simple_linked_list_destroy(tmp.msg_queue); + simple_linked_list_destroy(module_data->msg_queue); - while (simple_linked_list_size(tmp.msg_queue_children) > 0) { - struct message *m = simple_linked_list_pop(tmp.msg_queue_children); + while (simple_linked_list_size(module_data->msg_queue_children) > 0) { + struct message *m = simple_linked_list_pop(module_data->msg_queue_children); free_message_for_child(m, NULL); } - simple_linked_list_destroy(tmp.msg_queue_children); + simple_linked_list_destroy(module_data->msg_queue_children); - pthread_mutex_destroy(&tmp.lock); + pthread_mutex_destroy(&module_data->lock); - free(tmp.name); + free(module_data->name); } static const char *module_class_name_pairs[] = { diff --git a/src/module.h b/src/module.h index 78372efed..6b894e600 100644 --- a/src/module.h +++ b/src/module.h @@ -4,7 +4,7 @@ * @ingroup module */ /* - * Copyright (c) 2013-2024 CESNET + * Copyright (c) 2013-2025 CESNET * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -51,7 +51,6 @@ * module_init_default(&s->mod); * s->mod.cls = MODULE_CLASS_; // always needed * s->mod.priv_data = s; // optional - * s->mod.deleter = deleter; // only used for generic modules, see a note below * module_register(&s->mod, s->parent); * ``` * @@ -100,12 +99,11 @@ enum module_class { struct module; struct simple_linked_list; -typedef void (*module_deleter_t)(struct module *); typedef void (*notify_t)(struct module *); /** * @struct module - * Only members cls, deleter, priv_data and msg_queue may be directly touched + * Only members cls, priv_data and msg_queue may be directly touched * by user. The others should be considered private. */ struct module { @@ -115,7 +113,6 @@ struct module { enum module_class cls; struct module *parent; struct simple_linked_list *children; - module_deleter_t deleter; notify_t new_message; ///< if set, notifies module that new message is in queue, receiver lock is hold during the call pthread_mutex_t msg_queue_lock; // protects msg_queue diff --git a/src/rtp/rtp.c b/src/rtp/rtp.c index e94c8c466..2d8b2ba1d 100644 --- a/src/rtp/rtp.c +++ b/src/rtp/rtp.c @@ -23,7 +23,7 @@ * derived from the algorithms published in that specification. * * Copyright (c) 2005-2010 Fundació i2CAT, Internet I Innovació Digital a Catalunya - * Copyright (c) 2005-2023 CESNET z.s.p.o. + * Copyright (c) 2005-2053 CESNET * Copyright (c) 2001-2004 University of Southern California * Copyright (c) 2003-2004 University of Glasgow * Copyright (c) 1998-2001 University College London diff --git a/src/transmit.cpp b/src/transmit.cpp index e552cdaee..7895f4634 100644 --- a/src/transmit.cpp +++ b/src/transmit.cpp @@ -17,7 +17,7 @@ * * Copyright (c) 2005-2010 Fundació i2CAT, Internet I Innovació Digital a Catalunya * Copyright (c) 2001-2004 University of Southern California - * Copyright (c) 2005-2023 CESNET z.s.p.o. + * Copyright (c) 2005-2025 CESNET * * Redistribution and use in source and binary forms, with or without * modification, is permitted provided that the following conditions @@ -116,7 +116,6 @@ using std::array; using std::vector; static void tx_update(struct tx *tx, struct video_frame *frame, int substream); -static void tx_done(struct module *tx); static uint32_t format_interl_fps_hdr_row(enum interlacing_t interlacing, double input_fps); static void @@ -230,7 +229,6 @@ struct tx *tx_init(struct module *parent, unsigned mtu, enum tx_media_type media module_init_default(&tx->mod); tx->mod.cls = MODULE_CLASS_TX; tx->mod.priv_data = tx; - tx->mod.deleter = tx_done; module_register(&tx->mod, parent); tx->magic = TRANSMIT_MAGIC; @@ -244,7 +242,7 @@ struct tx *tx_init(struct module *parent, unsigned mtu, enum tx_media_type media tx->last_frame_fragment_id = -1; if (fec) { if(!set_fec(tx, fec)) { - module_done(&tx->mod); + tx_done(tx); return NULL; } } @@ -253,13 +251,13 @@ struct tx *tx_init(struct module *parent, unsigned mtu, enum tx_media_type media LIBRARY_CLASS_UNDEFINED, OPENSSL_ENCRYPT_ABI_VERSION)); if (!tx->enc_funcs) { fprintf(stderr, "UltraGrid was build without OpenSSL support!\n"); - module_done(&tx->mod); + tx_done(tx); return NULL; } if (tx->enc_funcs->init(&tx->encryption, encryption) != 0) { log_msg(LOG_LEVEL_ERROR, MOD_NAME "Unable to initialize encryption\n"); - module_done(&tx->mod); + tx_done(tx); return NULL; } } @@ -405,11 +403,11 @@ static void fec_check_messages(struct tx *tx) } } -static void tx_done(struct module *mod) +void tx_done(struct tx *tx_session) { - struct tx *tx = (struct tx *) mod->priv_data; - assert(tx->magic == TRANSMIT_MAGIC); - free(tx); + assert(tx_session->magic == TRANSMIT_MAGIC); + module_done(&tx_session->mod); + free(tx_session); } /* diff --git a/src/transmit.h b/src/transmit.h index 08a1098b8..2d178d9d4 100644 --- a/src/transmit.h +++ b/src/transmit.h @@ -15,7 +15,7 @@ * * Copyright (c) 2005-2010 Fundació i2CAT, Internet I Innovació Digital a Catalunya * Copyright (c) 2001-2002 University of Southern California - * Copyright (c) 2005-2023 CESNET z.s.p.o. + * Copyright (c) 2005-2025 CESNET * * Redistribution and use in source and binary forms, with or without * modification, is permitted provided that the following conditions @@ -78,6 +78,7 @@ struct tx *tx_init(struct module *parent, unsigned mtu, enum tx_media_type media void tx_send(struct tx *tx_session, struct video_frame *frame, struct rtp *rtp_session); void format_video_header(struct video_frame *frame, int tile_idx, int buffer_idx, uint32_t *hdr); +void tx_done(struct tx *tx_session); void tx_send_h264(struct tx *tx_session, struct video_frame *frame, struct rtp *rtp_session); void tx_send_h265(struct tx *tx_session, struct video_frame *frame, struct rtp *rtp_session); diff --git a/src/video_rxtx/rtp.cpp b/src/video_rxtx/rtp.cpp index cb44260d6..1f71d1e96 100644 --- a/src/video_rxtx/rtp.cpp +++ b/src/video_rxtx/rtp.cpp @@ -3,7 +3,7 @@ * @author Martin Pulec */ /* - * Copyright (c) 2013-2024 CESNET z.s.p.o. + * Copyright (c) 2013-2025 CESNET * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -227,7 +227,7 @@ rtp_video_rxtx::rtp_video_rxtx(map const ¶ms) : rtp_video_rxtx::~rtp_video_rxtx() { if (m_tx) { - module_done(CAST_MODULE(m_tx)); + tx_done(m_tx); } m_network_devices_lock.lock();