This small helper will simplify code in many modules. The hooks added through pa_module_hook_connect will be freed just before pa__done is called (so trying to add hooks during pa__done will result in assertion failure). Signed-off-by: David Henningsson <david.henningsson at canonical.com> --- src/pulsecore/module.c | 16 ++++++++++++++++ src/pulsecore/module.h | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/src/pulsecore/module.c b/src/pulsecore/module.c index 1081ad3..256d4de 100644 --- a/src/pulsecore/module.c +++ b/src/pulsecore/module.c @@ -100,6 +100,13 @@ bool pa_module_exists(const char *name) { return false; } +void pa_module_hook_connect(pa_module *m, pa_hook *hook, pa_hook_priority_t prio, pa_hook_cb_t cb, void *data) { + pa_assert(m); + pa_assert(hook); + pa_assert(m->hooks); + pa_dynarray_append(m->hooks, pa_hook_connect(hook, prio, cb, data)); +} + pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) { pa_module *m = NULL; bool (*load_once)(void); @@ -117,6 +124,7 @@ pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) { m->argument = pa_xstrdup(argument); m->load_once = false; m->proplist = pa_proplist_new(); + m->hooks = pa_dynarray_new((pa_free_cb_t) pa_hook_slot_free); m->index = PA_IDXSET_INVALID; if (!(m->dl = lt_dlopenext(name))) { @@ -200,6 +208,9 @@ fail: if (m->index != PA_IDXSET_INVALID) pa_idxset_remove_by_index(c->modules, m->index); + if (m->hooks) + pa_dynarray_free(m->hooks); + if (m->proplist) pa_proplist_free(m->proplist); @@ -221,6 +232,11 @@ static void pa_module_free(pa_module *m) { pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index); + if (m->hooks) { + pa_dynarray_free(m->hooks); + m->hooks = NULL; + } + if (m->done) m->done(m); diff --git a/src/pulsecore/module.h b/src/pulsecore/module.h index 96a6315..3889156 100644 --- a/src/pulsecore/module.h +++ b/src/pulsecore/module.h @@ -26,6 +26,7 @@ typedef struct pa_module pa_module; #include <pulse/proplist.h> +#include <pulsecore/dynarray.h> #include <pulsecore/core.h> @@ -46,6 +47,7 @@ struct pa_module { bool unload_requested:1; pa_proplist *proplist; + pa_dynarray *hooks; }; bool pa_module_exists(const char *name); @@ -64,6 +66,8 @@ int pa_module_get_n_used(pa_module*m); void pa_module_update_proplist(pa_module *m, pa_update_mode_t mode, pa_proplist *p); +void pa_module_hook_connect(pa_module *m, pa_hook *hook, pa_hook_priority_t prio, pa_hook_cb_t cb, void *data); + #define PA_MODULE_AUTHOR(s) \ const char *pa__get_author(void) { return s; } \ struct __stupid_useless_struct_to_allow_trailing_semicolon -- 1.9.1