module: Add hook dynarray

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@canonical.com>
This commit is contained in:
David Henningsson 2015-03-27 11:20:11 +01:00
parent f0c9321b53
commit 127e8a1519
2 changed files with 20 additions and 0 deletions

View file

@ -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);

View file

@ -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