From 6e019795bff589ef0a867772975e34da78fffefb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 14 Sep 2004 20:53:25 +0000 Subject: add refernce counting for sinks, sources, sink-inputs and source-outputs git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@200 fefdeb5f-60dc-0310-8127-8f9354f1896f --- polyp/source.c | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'polyp/source.c') diff --git a/polyp/source.c b/polyp/source.c index 5cdfdb55..2c0caca0 100644 --- a/polyp/source.c +++ b/polyp/source.c @@ -48,6 +48,9 @@ struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail return NULL; } + s->ref = 1; + s->state = PA_SOURCE_RUNNING; + s->name = pa_xstrdup(name); s->description = NULL; @@ -71,9 +74,9 @@ struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail return s; } -void pa_source_free(struct pa_source *s) { +void pa_source_disconnect(struct pa_source *s) { struct pa_source_output *o, *j = NULL; - assert(s); + assert(s && s->state == PA_SOURCE_RUNNING); pa_namereg_unregister(s->core, s->name); @@ -82,21 +85,45 @@ void pa_source_free(struct pa_source *s) { pa_source_output_kill(o); j = o; } - pa_idxset_free(s->outputs, NULL, NULL); - + pa_idxset_remove_by_data(s->core->sources, s, NULL); + pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index); - pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name); + s->notify = NULL; + + s->state = PA_SOURCE_DISCONNECTED; +} - pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index); +static void source_free(struct pa_source *s) { + assert(s && !s->ref); + + if (s->state != PA_SOURCE_DISCONNECTED) + pa_source_disconnect(s); + pa_log(__FILE__": freed %u \"%s\"\n", s->index, s->name); + + pa_idxset_free(s->outputs, NULL, NULL); + pa_xfree(s->name); pa_xfree(s->description); pa_xfree(s); } +void pa_source_unref(struct pa_source *s) { + assert(s && s->ref >= 1); + + if (!(--s->ref)) + source_free(s); +} + +struct pa_source* pa_source_ref(struct pa_source *s) { + assert(s && s->ref >= 1); + s->ref++; + return s; +} + void pa_source_notify(struct pa_source*s) { - assert(s); + assert(s && s->ref >= 1); if (s->notify) s->notify(s); @@ -112,9 +139,11 @@ static int do_post(void *p, uint32_t index, int *del, void*userdata) { } void pa_source_post(struct pa_source*s, struct pa_memchunk *chunk) { - assert(s && chunk); + assert(s && s->ref >= 1 && chunk); + pa_source_ref(s); pa_idxset_foreach(s->outputs, do_post, chunk); + pa_source_unref(s); } void pa_source_set_owner(struct pa_source *s, struct pa_module *m) { -- cgit