From 52c66b47664d47154b2c8368e32beef27d4b2d03 Mon Sep 17 00:00:00 2001 From: Tanu Kaskinen Date: Thu, 3 Dec 2009 13:22:05 +0200 Subject: [PATCH 31/31] libpulse: Store pa_stream pointers to hashmaps instead of dynarrays. Since the stream identifiers (channels) are monotonically growing integer, it isn't a good idea to use them as index to a dynamic array, because the array will grow all the time. This is not a problem with client connections that don't create many streams, but, for example, long-running clients that use libcanberra for playing event sounds, this means that the client connection effectively leaks memory. --- src/pulse/context.c | 12 ++++++------ src/pulse/internal.h | 3 +-- src/pulse/stream.c | 20 ++++++++++---------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/pulse/context.c b/src/pulse/context.c index c83230d..91f4817 100644 --- a/src/pulse/context.c +++ b/src/pulse/context.c @@ -63,7 +63,7 @@ #include #include #include -#include +#include #include #include #include @@ -157,8 +157,8 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char * c->system_bus = c->session_bus = NULL; #endif c->mainloop = mainloop; - c->playback_streams = pa_dynarray_new(); - c->record_streams = pa_dynarray_new(); + c->playback_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); + c->record_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); c->client_index = PA_INVALID_INDEX; c->use_rtclock = pa_mainloop_is_our_api(mainloop); @@ -252,9 +252,9 @@ static void context_free(pa_context *c) { #endif if (c->record_streams) - pa_dynarray_free(c->record_streams, NULL, NULL); + pa_hashmap_free(c->record_streams, NULL, NULL); if (c->playback_streams) - pa_dynarray_free(c->playback_streams, NULL, NULL); + pa_hashmap_free(c->playback_streams, NULL, NULL); if (c->mempool) pa_mempool_free(c->mempool); @@ -361,7 +361,7 @@ static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t o pa_context_ref(c); - if ((s = pa_dynarray_get(c->record_streams, channel))) { + if ((s = pa_hashmap_get(c->record_streams, PA_UINT32_TO_PTR(channel)))) { if (chunk->memblock) { pa_memblockq_seek(s->record_memblockq, offset, seek, TRUE); diff --git a/src/pulse/internal.h b/src/pulse/internal.h index c3ebf74..ab702b9 100644 --- a/src/pulse/internal.h +++ b/src/pulse/internal.h @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include @@ -66,7 +65,7 @@ struct pa_context { pa_pstream *pstream; pa_pdispatch *pdispatch; - pa_dynarray *record_streams, *playback_streams; + pa_hashmap *record_streams, *playback_streams; PA_LLIST_HEAD(pa_stream, streams); PA_LLIST_HEAD(pa_operation, operations); diff --git a/src/pulse/stream.c b/src/pulse/stream.c index daeb53a..ab8f8f4 100644 --- a/src/pulse/stream.c +++ b/src/pulse/stream.c @@ -201,7 +201,7 @@ static void stream_unlink(pa_stream *s) { pa_pdispatch_unregister_reply(s->context->pdispatch, s); if (s->channel_valid) { - pa_dynarray_put((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, s->channel, NULL); + pa_hashmap_remove((s->direction == PA_STREAM_PLAYBACK) ? s->context->playback_streams : s->context->record_streams, PA_UINT32_TO_PTR(s->channel)); s->channel = 0; s->channel_valid = FALSE; } @@ -356,7 +356,7 @@ void pa_command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, goto finish; } - if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, channel))) + if (!(s = pa_hashmap_get(command == PA_COMMAND_PLAYBACK_STREAM_KILLED ? c->playback_streams : c->record_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -476,7 +476,7 @@ void pa_command_stream_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, p goto finish; } - if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, channel))) + if (!(s = pa_hashmap_get(command == PA_COMMAND_PLAYBACK_STREAM_MOVED ? c->playback_streams : c->record_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -559,7 +559,7 @@ void pa_command_stream_buffer_attr(pa_pdispatch *pd, uint32_t command, uint32_t goto finish; } - if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED ? c->playback_streams : c->record_streams, channel))) + if (!(s = pa_hashmap_get(command == PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED ? c->playback_streams : c->record_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -611,7 +611,7 @@ void pa_command_stream_suspended(pa_pdispatch *pd, uint32_t command, uint32_t ta goto finish; } - if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, channel))) + if (!(s = pa_hashmap_get(command == PA_COMMAND_PLAYBACK_STREAM_SUSPENDED ? c->playback_streams : c->record_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -653,7 +653,7 @@ void pa_command_stream_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, goto finish; } - if (!(s = pa_dynarray_get(c->playback_streams, channel))) + if (!(s = pa_hashmap_get(c->playback_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -699,7 +699,7 @@ void pa_command_stream_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, p goto finish; } - if (!(s = pa_dynarray_get(command == PA_COMMAND_PLAYBACK_STREAM_EVENT ? c->playback_streams : c->record_streams, channel))) + if (!(s = pa_hashmap_get(command == PA_COMMAND_PLAYBACK_STREAM_EVENT ? c->playback_streams : c->record_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -735,7 +735,7 @@ void pa_command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tag goto finish; } - if (!(s = pa_dynarray_get(c->playback_streams, channel))) + if (!(s = pa_hashmap_get(c->playback_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -771,7 +771,7 @@ void pa_command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32 goto finish; } - if (!(s = pa_dynarray_get(c->playback_streams, channel))) + if (!(s = pa_hashmap_get(c->playback_streams, PA_UINT32_TO_PTR(channel)))) goto finish; if (s->state != PA_STREAM_READY) @@ -1019,7 +1019,7 @@ void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, } s->channel_valid = TRUE; - pa_dynarray_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, s->channel, s); + pa_hashmap_put((s->direction == PA_STREAM_RECORD) ? s->context->record_streams : s->context->playback_streams, PA_UINT32_TO_PTR(s->channel), s); create_stream_complete(s); -- 1.6.6