[pulseaudio-discuss] [PATCH 1/2] module: Add hook dynarray

David Henningsson david.henningsson at canonical.com
Fri Mar 27 02:20:37 PDT 2015


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 | 9 +++++++++
 src/pulsecore/module.h | 9 +++++++++
 2 files changed, 18 insertions(+)

diff --git a/src/pulsecore/module.c b/src/pulsecore/module.c
index 1081ad3..f8bd27e 100644
--- a/src/pulsecore/module.c
+++ b/src/pulsecore/module.c
@@ -117,6 +117,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 +201,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 +225,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..20d4464 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,13 @@ 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);
 
+static inline 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));
+}
+
 #define PA_MODULE_AUTHOR(s)                                     \
     const char *pa__get_author(void) { return s; }              \
     struct __stupid_useless_struct_to_allow_trailing_semicolon
-- 
1.9.1



More information about the pulseaudio-discuss mailing list