]> code.delx.au - pulseaudio/blobdiff - polyp/module.c
introduce pa_xmalloc() and friends
[pulseaudio] / polyp / module.c
index 5c6f0fb61d7d9d3aa570603a6c2fbd8be92d3892..8c5e318f1939c83dddf9299783664d676b4e5322 100644 (file)
 #include <errno.h>
 
 #include "module.h"
+#include "xmalloc.h"
+
+#define UNLOAD_POLL_TIME 10
+
+static void timeout_callback(struct pa_mainloop_api *m, void *id, const struct timeval *tv, void *userdata) {
+    struct pa_core *c = userdata;
+    struct timeval ntv;
+    assert(c && c->mainloop == m && c->auto_unload_mainloop_source == id);
+
+    pa_module_unload_unused(c);
+
+    gettimeofday(&ntv, NULL);
+    ntv.tv_sec += UNLOAD_POLL_TIME;
+    m->enable_time(m, id, &ntv);
+}
 
 struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char *argument) {
     struct pa_module *m = NULL;
@@ -38,11 +53,10 @@ struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char
     
     assert(c && name);
 
-    m = malloc(sizeof(struct pa_module));
-    assert(m);
+    m = pa_xmalloc(sizeof(struct pa_module));
 
-    m->name = strdup(name);
-    m->argument = argument ? strdup(argument) : NULL;
+    m->name = pa_xstrdup(name);
+    m->argument = pa_xstrdup(argument);
     
     if (!(m->dl = lt_dlopenext(name)))
         goto fail;
@@ -55,6 +69,8 @@ struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char
     
     m->userdata = NULL;
     m->core = c;
+    m->n_used = -1;
+    m->auto_unload = 0;
 
     assert(m->init);
     if (m->init(c, m) < 0)
@@ -62,6 +78,14 @@ struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char
 
     if (!c->modules)
         c->modules = pa_idxset_new(NULL, NULL);
+
+    if (!c->auto_unload_mainloop_source) {
+        struct timeval ntv;
+        gettimeofday(&ntv, NULL);
+        ntv.tv_sec += UNLOAD_POLL_TIME;
+        c->auto_unload_mainloop_source = c->mainloop->source_time(c->mainloop, &ntv, timeout_callback, c);
+    }
+    assert(c->auto_unload_mainloop_source);
     
     assert(c->modules);
     r = pa_idxset_put(c->modules, m, &m->index);
@@ -73,13 +97,13 @@ struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char
     
 fail:
     if (m) {
-        free(m->argument);
-        free(m->name);
+        pa_xfree(m->argument);
+        pa_xfree(m->name);
         
         if (m->dl)
             lt_dlclose(m->dl);
 
-        free(m);
+        pa_xfree(m);
     }
 
     return NULL;
@@ -93,9 +117,9 @@ static void pa_module_free(struct pa_module *m) {
     
     fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
 
-    free(m->name);
-    free(m->argument);
-    free(m);
+    pa_xfree(m->name);
+    pa_xfree(m->argument);
+    pa_xfree(m);
 }
 
 
@@ -134,6 +158,34 @@ void pa_module_unload_all(struct pa_core *c) {
 
     pa_idxset_free(c->modules, free_callback, NULL);
     c->modules = NULL;
+
+    if (c->auto_unload_mainloop_source)
+        c->mainloop->cancel_time(c->mainloop, c->auto_unload_mainloop_source);
+    c->auto_unload_mainloop_source = NULL;
+}
+
+static int unused_callback(void *p, uint32_t index, int *del, void *userdata) {
+    struct pa_module *m = p;
+    time_t *now = userdata;
+    assert(p && del && now);
+    
+    if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->auto_unload_time <= *now) {
+        pa_module_free(m);
+        *del = 1;
+    }
+
+    return 0;
+}
+
+void pa_module_unload_unused(struct pa_core *c) {
+    time_t now;
+    assert(c);
+
+    if (!c->modules)
+        return;
+    
+    time(&now);
+    pa_idxset_foreach(c->modules, unused_callback, &now);
 }
 
 struct once_info {
@@ -141,21 +193,29 @@ struct once_info {
     uint32_t index;
 };
     
-
 static void module_unload_once_callback(void *userdata) {
     struct once_info *i = userdata;
     assert(i);
     pa_module_unload_by_index(i->core, i->index);
-    free(i);
+    pa_xfree(i);
 }
 
 void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
     struct once_info *i;
     assert(c && m);
 
-    i = malloc(sizeof(struct once_info));
-    assert(i);
+    i = pa_xmalloc(sizeof(struct once_info));
     i->core = c;
     i->index = m->index;
     pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
 }
+
+void pa_module_set_used(struct pa_module*m, int used) {
+    assert(m);
+
+    if (m->n_used != used && used == 0)
+        time(&m->last_used_time);
+
+    m->n_used = used;
+}
+