From 46091a9237f17f4295dca7140d8d70b4fce8b357 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 4 Aug 2004 16:39:30 +0000 Subject: introduce pa_xmalloc() and friends implement module auto loading git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@103 fefdeb5f-60dc-0310-8127-8f9354f1896f --- doc/todo | 3 +- polyp/Makefile.am | 7 +++- polyp/alsa-util.c | 13 +++---- polyp/cli-command.c | 96 ++++++++++++++++++++++++++++++++-------------- polyp/cli.c | 8 ++-- polyp/client.c | 14 +++---- polyp/clitext.c | 49 +++++++++++++++++------ polyp/clitext.h | 1 + polyp/cmdline.c | 8 ++-- polyp/core.c | 19 ++++++--- polyp/core.h | 8 ++-- polyp/dynarray.c | 10 ++--- polyp/hashmap.c | 18 ++++----- polyp/idxset.c | 28 ++++++-------- polyp/iochannel.c | 5 ++- polyp/ioline.c | 18 ++++----- polyp/main.c | 3 +- polyp/mainloop-api.c | 7 ++-- polyp/mainloop-signal.c | 8 ++-- polyp/mainloop.c | 24 +++++------- polyp/memblock.c | 14 +++---- polyp/memblockq.c | 15 ++++---- polyp/memchunk.c | 11 +++--- polyp/modargs.c | 62 +++++++----------------------- polyp/modargs.h | 3 -- polyp/module-alsa-sink.c | 52 +++++++++++++++---------- polyp/module-alsa-source.c | 47 ++++++++++++++--------- polyp/module-oss-mmap.c | 35 ++++++++++------- polyp/module-oss.c | 27 +++++++++---- polyp/module-pipe-sink.c | 18 +++++---- polyp/module-x11-bell.c | 38 ++++++------------ polyp/module.c | 88 +++++++++++++++++++++++++++++++++++------- polyp/module.h | 8 ++++ polyp/namereg.c | 78 +++++++++++++++++++++++++++++++------ polyp/namereg.h | 3 +- polyp/packet.c | 13 +++---- polyp/pdispatch.c | 11 +++--- polyp/polypaudio.pa | 10 ++++- polyp/polyplib-simple.c | 16 ++++---- polyp/polyplib.c | 27 ++++++------- polyp/protocol-cli.c | 6 +-- polyp/protocol-esound.c | 85 ++++++++++++---------------------------- polyp/protocol-native.c | 55 +++++++++++--------------- polyp/protocol-simple.c | 48 +++++++++-------------- polyp/pstream.c | 14 +++---- polyp/queue.c | 13 +++---- polyp/resampler.c | 16 ++++---- polyp/scache.c | 11 +++--- polyp/sink-input.c | 10 ++--- polyp/sink.c | 29 ++++---------- polyp/sink.h | 2 - polyp/socket-client.c | 6 +-- polyp/socket-server.c | 14 +++---- polyp/socket-util.c | 11 +++--- polyp/source-output.c | 10 ++--- polyp/source.c | 28 ++++---------- polyp/source.h | 2 - polyp/strbuf.c | 17 ++++---- polyp/tagstruct.c | 13 +++---- polyp/tokenizer.c | 14 +++---- polyp/util.c | 4 +- 61 files changed, 700 insertions(+), 631 deletions(-) diff --git a/doc/todo b/doc/todo index 3f04bfca..7cf36b08 100644 --- a/doc/todo +++ b/doc/todo @@ -3,12 +3,11 @@ *** 0.2 *** - future cancellation -- autoloading/autounloading - make mcalign merge chunks - doxygen -- scache.[ch]/module-x11-bell.c/debug.h copyright +- several files: copyright and config.h - enable searchdir - autoscan - rename clitext.[ch] to cli-text.[ch] diff --git a/polyp/Makefile.am b/polyp/Makefile.am index 48b984f8..0af99110 100644 --- a/polyp/Makefile.am +++ b/polyp/Makefile.am @@ -109,7 +109,9 @@ polypaudio_SOURCES = idxset.c idxset.h \ dynarray.c dynarray.h \ scache.c scache.h \ sound-file.c sound-file.h \ - play-memchunk.c play-memchunk.h + play-memchunk.c play-memchunk.h \ + autoload.c autoload.h \ + xmalloc.c xmalloc.h polypaudio_CFLAGS = $(AM_CFLAGS) $(LIBSAMPLERATE_CFLAGS) $(LIBSNDFILE_CFLAGS) polypaudio_INCLUDES = $(INCLTDL) @@ -273,7 +275,8 @@ libpolyp_la_SOURCES = polyplib.c polyplib.h \ authkey.c authkey.h \ socket-util.c socket-util.h \ native-common.h \ - sample.c sample.h + sample.c sample.h \ + xmalloc.c xmalloc.h libpolyp_la_CFLAGS = $(AM_CFLAGS) libpolyp_mainloop_la_SOURCES = mainloop-api.h mainloop-api.c \ diff --git a/polyp/alsa-util.c b/polyp/alsa-util.c index 7f266df5..43562378 100644 --- a/polyp/alsa-util.c +++ b/polyp/alsa-util.c @@ -27,6 +27,7 @@ #include "alsa-util.h" #include "sample.h" +#include "xmalloc.h" int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, struct pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *buffer_size) { int ret = 0; @@ -66,15 +67,13 @@ int pa_create_io_sources(snd_pcm_t *pcm_handle, struct pa_mainloop_api* m, void *n_io_sources = snd_pcm_poll_descriptors_count(pcm_handle); - pfds = malloc(sizeof(struct pollfd) * *n_io_sources); - assert(pfds); + pfds = pa_xmalloc(sizeof(struct pollfd) * *n_io_sources); if (snd_pcm_poll_descriptors(pcm_handle, pfds, *n_io_sources) < 0) { - free(pfds); + pa_xfree(pfds); return -1; } - *io_sources = malloc(sizeof(void*) * *n_io_sources); - assert(io_sources); + *io_sources = pa_xmalloc(sizeof(void*) * *n_io_sources); for (i = 0, ios = *io_sources, ppfd = pfds; i < *n_io_sources; i++, ios++, ppfd++) { *ios = m->source_io(m, ppfd->fd, @@ -83,7 +82,7 @@ int pa_create_io_sources(snd_pcm_t *pcm_handle, struct pa_mainloop_api* m, void assert(*ios); } - free(pfds); + pa_xfree(pfds); return 0; } @@ -94,5 +93,5 @@ void pa_free_io_sources(struct pa_mainloop_api* m, void **io_sources, unsigned n for (ios = io_sources, i = 0; i < n_io_sources; i++, ios++) m->cancel_io(m, *ios); - free(io_sources); + pa_xfree(io_sources); } diff --git a/polyp/cli-command.c b/polyp/cli-command.c index 826789ce..6386d4e4 100644 --- a/polyp/cli-command.c +++ b/polyp/cli-command.c @@ -44,6 +44,8 @@ #include "sample-util.h" #include "sound-file.h" #include "play-memchunk.h" +#include "autoload.h" +#include "xmalloc.h" struct command { const char *name; @@ -76,6 +78,9 @@ static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer * static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose); static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose); static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose); +static int pa_cli_command_autoload_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose); +static int pa_cli_command_autoload_add(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose); +static int pa_cli_command_autoload_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose); static const struct command commands[] = { { "exit", pa_cli_command_exit, "Terminate the daemon", 1 }, @@ -99,11 +104,16 @@ static const struct command commands[] = { { "kill_client", pa_cli_command_kill_client, "Kill a client (args: index)", 2}, { "kill_sink_input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2}, { "kill_source_output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2}, - { "scache_list", pa_cli_command_scache_list, "List all entries in the sample cache", 2}, + { "scache_list", pa_cli_command_scache_list, "List all entries in the sample cache", 1}, { "scache_play", pa_cli_command_scache_play, "Play a sample from the sample cache (args: name, sink|index)", 3}, { "scache_remove", pa_cli_command_scache_remove, "Remove a sample from the sample cache (args: name)", 2}, { "scache_load", pa_cli_command_scache_load, "Load a sound file into the sample cache (args: filename,name)", 3}, { "play_file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3}, + { "autoload_list", pa_cli_command_autoload_list, "List autoload entries", 1}, + { "autoload_sink_add", pa_cli_command_autoload_add, "Add autoload entry for a sink (args: sink, name, arguments)", 4}, + { "autoload_source_add", pa_cli_command_autoload_add, "Add autoload entry for a source (args: source, name, arguments)", 4}, + { "autoload_sink_remove", pa_cli_command_autoload_remove, "Remove autoload entry for a sink (args: sink)", 2}, + { "autoload_source_remove", pa_cli_command_autoload_remove, "Remove autoload entry for a source (args: source)", 2}, { NULL, NULL, NULL, 0 } }; @@ -144,7 +154,7 @@ static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, str s = pa_module_list_to_string(c); assert(s); pa_strbuf_puts(buf, s); - free(s); + pa_xfree(s); return 0; } @@ -154,7 +164,7 @@ static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, str s = pa_client_list_to_string(c); assert(s); pa_strbuf_puts(buf, s); - free(s); + pa_xfree(s); return 0; } @@ -164,7 +174,7 @@ static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struc s = pa_sink_list_to_string(c); assert(s); pa_strbuf_puts(buf, s); - free(s); + pa_xfree(s); return 0; } @@ -174,7 +184,7 @@ static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, str s = pa_source_list_to_string(c); assert(s); pa_strbuf_puts(buf, s); - free(s); + pa_xfree(s); return 0; } @@ -184,7 +194,7 @@ static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, s = pa_sink_input_list_to_string(c); assert(s); pa_strbuf_puts(buf, s); - free(s); + pa_xfree(s); return 0; } @@ -194,7 +204,7 @@ static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer s = pa_source_output_list_to_string(c); assert(s); pa_strbuf_puts(buf, s); - free(s); + pa_xfree(s); return 0; } @@ -214,6 +224,7 @@ static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_cli_command_sink_inputs(c, t, buf, fail, verbose); pa_cli_command_source_outputs(c, t, buf, fail, verbose); pa_cli_command_scache_list(c, t, buf, fail, verbose); + pa_cli_command_autoload_list(c, t, buf, fail, verbose); return 0; } @@ -284,7 +295,7 @@ static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, return -1; } - if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) { + if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) { pa_strbuf_puts(buf, "No sink found by this name or index.\n"); return -1; } @@ -333,7 +344,6 @@ static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokeniz static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) { const char *n; - struct pa_sink *sink; assert(c && t); if (!(n = pa_tokenizer_get(t, 1))) { @@ -341,18 +351,12 @@ static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t return -1; } - if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) { - pa_strbuf_puts(buf, "No sink found by this name or index.\n"); - return -1; - } - - c->default_sink_index = sink->index; + pa_namereg_set_default(c, n, PA_NAMEREG_SINK); return 0; } static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) { const char *n; - struct pa_source *source; assert(c && t); if (!(n = pa_tokenizer_get(t, 1))) { @@ -360,12 +364,7 @@ static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer return -1; } - if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE))) { - pa_strbuf_puts(buf, "No source found by this name or index.\n"); - return -1; - } - - c->default_source_index = source->index; + pa_namereg_set_default(c, n, PA_NAMEREG_SOURCE); return 0; } @@ -450,7 +449,7 @@ static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, s = pa_scache_list_to_string(c); assert(s); pa_strbuf_puts(buf, s); - free(s); + pa_xfree(s); return 0; } @@ -464,7 +463,7 @@ static int pa_cli_command_scache_play(struct pa_core *c, struct pa_tokenizer *t, return -1; } - if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK))) { + if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) { pa_strbuf_puts(buf, "No sink by that name.\n"); return -1; } @@ -528,7 +527,7 @@ static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, s return -1; } - if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK))) { + if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) { pa_strbuf_puts(buf, "No sink by that name.\n"); return -1; } @@ -543,6 +542,46 @@ static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, s return ret; } +static int pa_cli_command_autoload_add(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) { + const char *devname, *module; + assert(c && t && buf && fail && verbose); + + if (!(devname = pa_tokenizer_get(t, 1)) || !(module = pa_tokenizer_get(t, 2))) { + pa_strbuf_puts(buf, "You need to specify a device name, a module name and optionally module arguments\n"); + return -1; + } + + pa_autoload_add(c, devname, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, pa_tokenizer_get(t, 3)); + return 0; +} + +static int pa_cli_command_autoload_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) { + const char *devname; + assert(c && t && buf && fail && verbose); + + if (!(devname = pa_tokenizer_get(t, 1))) { + pa_strbuf_puts(buf, "You need to specify a device name\n"); + return -1; + } + + if (pa_autoload_remove(c, devname, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) { + pa_strbuf_puts(buf, "Failed to remove autload entry\n"); + return -1; + } + + return 0; +} + +static int pa_cli_command_autoload_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) { + char *s; + assert(c && t); + s = pa_autoload_list_to_string(c); + assert(s); + pa_strbuf_puts(buf, s); + pa_xfree(s); + return 0; +} + int pa_cli_command_execute_line(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) { const char *cs; @@ -654,14 +693,13 @@ int pa_cli_command_execute(struct pa_core *c, const char *s, struct pa_strbuf *b p = s; while (*p) { size_t l = strcspn(p, linebreak); - char *line = strndup(p, l); - assert(line); + char *line = pa_xstrndup(p, l); if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0&& *fail) { - free(line); + pa_xfree(line); return -1; } - free(line); + pa_xfree(line); p += l; p += strspn(p, linebreak); diff --git a/polyp/cli.c b/polyp/cli.c index f2aa5a65..f0ad4830 100644 --- a/polyp/cli.c +++ b/polyp/cli.c @@ -41,6 +41,7 @@ #include "namereg.h" #include "clitext.h" #include "cli-command.h" +#include "xmalloc.h" struct pa_cli { struct pa_core *core; @@ -65,8 +66,7 @@ struct pa_cli* pa_cli_new(struct pa_core *core, struct pa_iochannel *io, struct struct pa_cli *c; assert(io); - c = malloc(sizeof(struct pa_cli)); - assert(c); + c = pa_xmalloc(sizeof(struct pa_cli)); c->core = core; c->line = pa_ioline_new(io); assert(c->line); @@ -95,7 +95,7 @@ void pa_cli_free(struct pa_cli *c) { assert(c); pa_ioline_free(c->line); pa_client_free(c->client); - free(c); + pa_xfree(c); } static void client_kill(struct pa_client *client) { @@ -132,7 +132,7 @@ static void line_callback(struct pa_ioline *line, const char *s, void *userdata) pa_cli_command_execute_line(c->core, s, buf, &c->fail, &c->verbose); c->defer_kill--; pa_ioline_puts(line, p = pa_strbuf_tostring_free(buf)); - free(p); + pa_xfree(p); if (c->kill_requested) { if (c->eof_callback) diff --git a/polyp/client.c b/polyp/client.c index 0294c9e2..83a6264d 100644 --- a/polyp/client.c +++ b/polyp/client.c @@ -29,15 +29,15 @@ #include #include "client.h" +#include "xmalloc.h" struct pa_client *pa_client_new(struct pa_core *core, const char *protocol_name, char *name) { struct pa_client *c; int r; assert(core); - c = malloc(sizeof(struct pa_client)); - assert(c); - c->name = name ? strdup(name) : NULL; + c = pa_xmalloc(sizeof(struct pa_client)); + c->name = pa_xstrdup(name); c->owner = NULL; c->core = core; c->protocol_name = protocol_name; @@ -58,8 +58,8 @@ void pa_client_free(struct pa_client *c) { pa_idxset_remove_by_data(c->core->clients, c, NULL); fprintf(stderr, "client: freed %u \"%s\"\n", c->index, c->name); - free(c->name); - free(c); + pa_xfree(c->name); + pa_xfree(c); } void pa_client_kill(struct pa_client *c) { @@ -74,6 +74,6 @@ void pa_client_kill(struct pa_client *c) { void pa_client_rename(struct pa_client *c, const char *name) { assert(c); - free(c->name); - c->name = name ? strdup(name) : NULL; + pa_xfree(c->name); + c->name = pa_xstrdup(name); } diff --git a/polyp/clitext.c b/polyp/clitext.c index fbce2223..6d2d6253 100644 --- a/polyp/clitext.c +++ b/polyp/clitext.c @@ -36,6 +36,7 @@ #include "strbuf.h" #include "sample-util.h" #include "scache.h" +#include "autoload.h" char *pa_module_list_to_string(struct pa_core *c) { struct pa_strbuf *s; @@ -49,7 +50,7 @@ char *pa_module_list_to_string(struct pa_core *c) { pa_strbuf_printf(s, "%u module(s) loaded.\n", pa_idxset_ncontents(c->modules)); for (m = pa_idxset_first(c->modules, &index); m; m = pa_idxset_next(c->modules, &index)) - pa_strbuf_printf(s, " index: %u\n\tname: <%s>\n\targument: <%s>\n", m->index, m->name, m->argument); + pa_strbuf_printf(s, " index: %u\n\tname: <%s>\n\targument: <%s>\n\tused: %i\n\tauto unload: %s\n", m->index, m->name, m->argument, m->n_used, m->auto_unload ? "yes" : "no"); return pa_strbuf_tostring_free(s); } @@ -77,7 +78,7 @@ char *pa_client_list_to_string(struct pa_core *c) { char *pa_sink_list_to_string(struct pa_core *c) { struct pa_strbuf *s; - struct pa_sink *sink, *default_sink; + struct pa_sink *sink; uint32_t index = PA_IDXSET_INVALID; assert(c); @@ -86,8 +87,6 @@ char *pa_sink_list_to_string(struct pa_core *c) { pa_strbuf_printf(s, "%u sink(s) available.\n", pa_idxset_ncontents(c->sinks)); - default_sink = pa_sink_get_default(c); - for (sink = pa_idxset_first(c->sinks, &index); sink; sink = pa_idxset_next(c->sinks, &index)) { char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH]; pa_sample_snprint(ss, sizeof(ss), &sink->sample_spec); @@ -95,7 +94,7 @@ char *pa_sink_list_to_string(struct pa_core *c) { pa_strbuf_printf( s, " %c index: %u\n\tname: <%s>\n\tvolume: <0x%04x>\n\tlatency: <%u usec>\n\tmonitor_source: <%u>\n\tsample_spec: <%s>\n", - sink == default_sink ? '*' : ' ', + !strcmp(sink->name, c->default_sink_name) ? '*' : ' ', sink->index, sink->name, (unsigned) sink->volume, pa_sink_get_latency(sink), @@ -113,7 +112,7 @@ char *pa_sink_list_to_string(struct pa_core *c) { char *pa_source_list_to_string(struct pa_core *c) { struct pa_strbuf *s; - struct pa_source *source, *default_source; + struct pa_source *source; uint32_t index = PA_IDXSET_INVALID; assert(c); @@ -122,12 +121,14 @@ char *pa_source_list_to_string(struct pa_core *c) { pa_strbuf_printf(s, "%u source(s) available.\n", pa_idxset_ncontents(c->sources)); - default_source = pa_source_get_default(c); - for (source = pa_idxset_first(c->sources, &index); source; source = pa_idxset_next(c->sources, &index)) { char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH]; pa_sample_snprint(ss, sizeof(ss), &source->sample_spec); - pa_strbuf_printf(s, " %c index: %u\n\tname: <%s>\n\tsample_spec: <%s>\n", source == default_source ? '*' : ' ', source->index, source->name, ss); + pa_strbuf_printf(s, " %c index: %u\n\tname: <%s>\n\tsample_spec: <%s>\n", + !strcmp(source->name, c->default_source_name) ? '*' : ' ', + source->index, + source->name, + ss); if (source->monitor_of) pa_strbuf_printf(s, "\tmonitor_of: <%u>\n", source->monitor_of->index); @@ -205,8 +206,6 @@ char *pa_sink_input_list_to_string(struct pa_core *c) { } char *pa_scache_list_to_string(struct pa_core *c) { - struct pa_scache_entry *e; - void *state = NULL; struct pa_strbuf *s; assert(c); @@ -216,6 +215,8 @@ char *pa_scache_list_to_string(struct pa_core *c) { pa_strbuf_printf(s, "%u cache entries available.\n", c->scache_hashmap ? pa_hashmap_ncontents(c->scache_hashmap) : 0); if (c->scache_hashmap) { + struct pa_scache_entry *e; + void *state = NULL; while ((e = pa_hashmap_iterate(c->scache_hashmap, &state))) { double l; @@ -237,3 +238,29 @@ char *pa_scache_list_to_string(struct pa_core *c) { return pa_strbuf_tostring_free(s); } + +char *pa_autoload_list_to_string(struct pa_core *c) { + struct pa_strbuf *s; + assert(c); + + s = pa_strbuf_new(); + assert(s); + + pa_strbuf_printf(s, "%u autoload entries available.\n", c->autoload_hashmap ? pa_hashmap_ncontents(c->autoload_hashmap) : 0); + + if (c->autoload_hashmap) { + struct pa_autoload_entry *e; + void *state = NULL; + + while ((e = pa_hashmap_iterate(c->autoload_hashmap, &state))) { + pa_strbuf_printf( + s, " name: <%s>\n\ttype: <%s>\n\tmodule_name: <%s>\n\targuments: <%s>\n", + e->name, + e->type == PA_NAMEREG_SOURCE ? "source" : "sink", + e->module, + e->argument); + } + } + + return pa_strbuf_tostring_free(s); +} diff --git a/polyp/clitext.h b/polyp/clitext.h index 4e5252fe..121f2a5d 100644 --- a/polyp/clitext.h +++ b/polyp/clitext.h @@ -31,6 +31,7 @@ char *pa_source_list_to_string(struct pa_core *c); char *pa_client_list_to_string(struct pa_core *c); char *pa_module_list_to_string(struct pa_core *c); char *pa_scache_list_to_string(struct pa_core *c); +char *pa_autoload_list_to_string(struct pa_core *c); #endif diff --git a/polyp/cmdline.c b/polyp/cmdline.c index a3330145..8acdde6f 100644 --- a/polyp/cmdline.c +++ b/polyp/cmdline.c @@ -32,6 +32,7 @@ #include "cmdline.h" #include "util.h" #include "strbuf.h" +#include "xmalloc.h" void pa_cmdline_help(const char *argv0) { const char *e; @@ -57,8 +58,7 @@ struct pa_cmdline* pa_cmdline_parse(int argc, char * const argv []) { struct pa_strbuf *buf = NULL; assert(argc && argv); - cmdline = malloc(sizeof(struct pa_cmdline)); - assert(cmdline); + cmdline = pa_xmalloc(sizeof(struct pa_cmdline)); cmdline->daemonize = cmdline->help = cmdline->verbose = 0; cmdline->fail = 1; @@ -106,6 +106,6 @@ fail: void pa_cmdline_free(struct pa_cmdline *cmd) { assert(cmd); - free(cmd->cli_commands); - free(cmd); + pa_xfree(cmd->cli_commands); + pa_xfree(cmd); } diff --git a/polyp/core.c b/polyp/core.c index 1c69f914..ffc11cec 100644 --- a/polyp/core.c +++ b/polyp/core.c @@ -34,11 +34,12 @@ #include "namereg.h" #include "util.h" #include "scache.h" +#include "autoload.h" +#include "xmalloc.h" struct pa_core* pa_core_new(struct pa_mainloop_api *m) { struct pa_core* c; - c = malloc(sizeof(struct pa_core)); - assert(c); + c = pa_xmalloc(sizeof(struct pa_core)); c->mainloop = m; c->clients = pa_idxset_new(NULL, NULL); @@ -47,16 +48,20 @@ struct pa_core* pa_core_new(struct pa_mainloop_api *m) { c->source_outputs = pa_idxset_new(NULL, NULL); c->sink_inputs = pa_idxset_new(NULL, NULL); - c->default_source_index = c->default_sink_index = PA_IDXSET_INVALID; + c->default_source_name = c->default_sink_name = NULL; c->modules = NULL; c->namereg = NULL; c->scache_idxset = NULL; c->scache_hashmap = NULL; + c->autoload_hashmap = NULL; + c->default_sample_spec.format = PA_SAMPLE_S16NE; c->default_sample_spec.rate = 44100; c->default_sample_spec.channels = 2; + + c->auto_unload_time = 20; pa_check_for_sigpipe(); @@ -68,7 +73,7 @@ void pa_core_free(struct pa_core *c) { pa_module_unload_all(c); assert(!c->modules); - + assert(pa_idxset_isempty(c->clients)); pa_idxset_free(c->clients, NULL, NULL); @@ -86,7 +91,11 @@ void pa_core_free(struct pa_core *c) { pa_namereg_free(c); pa_scache_free(c); + pa_autoload_free(c); + + pa_xfree(c->default_source_name); + pa_xfree(c->default_sink_name); - free(c); + pa_xfree(c); }; diff --git a/polyp/core.h b/polyp/core.h index 03b8671a..b125083d 100644 --- a/polyp/core.h +++ b/polyp/core.h @@ -32,11 +32,13 @@ struct pa_core { struct pa_idxset *clients, *sinks, *sources, *sink_inputs, *source_outputs, *modules, *scache_idxset; - struct pa_hashmap *namereg, *scache_hashmap; - - uint32_t default_source_index, default_sink_index; + struct pa_hashmap *namereg, *scache_hashmap, *autoload_hashmap; + + char *default_source_name, *default_sink_name; struct pa_sample_spec default_sample_spec; + int auto_unload_time; + void *auto_unload_mainloop_source; }; struct pa_core* pa_core_new(struct pa_mainloop_api *m); diff --git a/polyp/dynarray.c b/polyp/dynarray.c index 24306964..23adb581 100644 --- a/polyp/dynarray.c +++ b/polyp/dynarray.c @@ -28,6 +28,7 @@ #include #include "dynarray.h" +#include "xmalloc.h" struct pa_dynarray { void **data; @@ -36,8 +37,7 @@ struct pa_dynarray { struct pa_dynarray* pa_dynarray_new(void) { struct pa_dynarray *a; - a = malloc(sizeof(struct pa_dynarray)); - assert(a); + a = pa_xmalloc(sizeof(struct pa_dynarray)); a->data = NULL; a->n_entries = 0; a->n_allocated = 0; @@ -53,8 +53,8 @@ void pa_dynarray_free(struct pa_dynarray* a, void (*func)(void *p, void *userdat if (a->data[i]) func(a->data[i], userdata); - free(a->data); - free(a); + pa_xfree(a->data); + pa_xfree(a); } void pa_dynarray_put(struct pa_dynarray*a, unsigned i, void *p) { @@ -67,7 +67,7 @@ void pa_dynarray_put(struct pa_dynarray*a, unsigned i, void *p) { return; n = i+100; - a->data = realloc(a->data, sizeof(void*)*n); + a->data = pa_xrealloc(a->data, sizeof(void*)*n); memset(a->data+a->n_allocated, 0, sizeof(void*)*(n-a->n_allocated)); a->n_allocated = n; } diff --git a/polyp/hashmap.c b/polyp/hashmap.c index 51e3879b..2b9550fd 100644 --- a/polyp/hashmap.c +++ b/polyp/hashmap.c @@ -29,6 +29,7 @@ #include "hashmap.h" #include "idxset.h" +#include "xmalloc.h" struct hashmap_entry { struct hashmap_entry *next, *previous, *bucket_next, *bucket_previous; @@ -49,11 +50,8 @@ struct pa_hashmap { struct pa_hashmap *pa_hashmap_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b)) { struct pa_hashmap *h; - h = malloc(sizeof(struct pa_hashmap)); - assert(h); - h->data = malloc(sizeof(struct hashmap_entry*)*(h->size = 1023)); - assert(h->data); - memset(h->data, 0, sizeof(struct hashmap_entry*)*(h->size = 1023)); + h = pa_xmalloc(sizeof(struct pa_hashmap)); + h->data = pa_xmalloc0(sizeof(struct hashmap_entry*)*(h->size = 1023)); h->first_entry = NULL; h->n_entries = 0; h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func; @@ -78,7 +76,7 @@ static void remove(struct pa_hashmap *h, struct hashmap_entry *e) { else h->data[e->hash] = e->bucket_next; - free(e); + pa_xfree(e); h->n_entries--; } @@ -91,8 +89,8 @@ void pa_hashmap_free(struct pa_hashmap*h, void (*free_func)(void *p, void *userd remove(h, h->first_entry); } - free(h->data); - free(h); + pa_xfree(h->data); + pa_xfree(h); } static struct hashmap_entry *get(struct pa_hashmap *h, unsigned hash, const void *key) { @@ -115,9 +113,7 @@ int pa_hashmap_put(struct pa_hashmap *h, const void *key, void *value) { if ((e = get(h, hash, key))) return -1; - e = malloc(sizeof(struct hashmap_entry)); - assert(e); - + e = pa_xmalloc(sizeof(struct hashmap_entry)); e->hash = hash; e->key = key; e->value = value; diff --git a/polyp/idxset.c b/polyp/idxset.c index 0072e3cd..83565193 100644 --- a/polyp/idxset.c +++ b/polyp/idxset.c @@ -29,6 +29,7 @@ #include #include "idxset.h" +#include "xmalloc.h" struct idxset_entry { void *data; @@ -73,14 +74,11 @@ int pa_idxset_trivial_compare_func(const void *a, const void *b) { struct pa_idxset* pa_idxset_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b)) { struct pa_idxset *s; - s = malloc(sizeof(struct pa_idxset)); - assert(s); + s = pa_xmalloc(sizeof(struct pa_idxset)); s->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func; s->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func; s->hash_table_size = 1023; - s->hash_table = malloc(sizeof(struct idxset_entry*)*s->hash_table_size); - assert(s->hash_table); - memset(s->hash_table, 0, sizeof(struct idxset_entry*)*s->hash_table_size); + s->hash_table = pa_xmalloc0(sizeof(struct idxset_entry*)*s->hash_table_size); s->array = NULL; s->array_size = 0; s->index = 0; @@ -101,12 +99,12 @@ void pa_idxset_free(struct pa_idxset *s, void (*free_func) (void *p, void *userd if (free_func) free_func(e->data, userdata); - free(e); + pa_xfree(e); } - free(s->hash_table); - free(s->array); - free(s); + pa_xfree(s->hash_table); + pa_xfree(s->array); + pa_xfree(s); } static struct idxset_entry* hash_scan(struct pa_idxset *s, struct idxset_entry* e, void *p) { @@ -133,14 +131,12 @@ static void extend_array(struct pa_idxset *s, uint32_t index) { break; l = index - s->start_index - i + 100; - n = malloc(sizeof(struct hash_table_entry*)*l); - assert(n); - memset(n, 0, sizeof(struct hash_table_entry*)*l); + n = pa_xmalloc0(sizeof(struct hash_table_entry*)*l); for (j = 0; j < s->array_size-i; j++) n[j] = s->array[i+j]; - free(s->array); + pa_xfree(s->array); s->array = n; s->array_size = l; @@ -173,9 +169,7 @@ int pa_idxset_put(struct pa_idxset*s, void *p, uint32_t *index) { return -1; } - e = malloc(sizeof(struct idxset_entry)); - assert(e); - + e = pa_xmalloc(sizeof(struct idxset_entry)); e->data = p; e->index = s->index++; e->hash_value = h; @@ -274,7 +268,7 @@ static void remove_entry(struct pa_idxset *s, struct idxset_entry *e) { else s->hash_table[e->hash_value] = e->hash_next; - free(e); + pa_xfree(e); assert(s->n_entries >= 1); s->n_entries--; diff --git a/polyp/iochannel.c b/polyp/iochannel.c index 69d381f4..77f8fb08 100644 --- a/polyp/iochannel.c +++ b/polyp/iochannel.c @@ -31,6 +31,7 @@ #include "iochannel.h" #include "util.h" #include "socket-util.h" +#include "xmalloc.h" struct pa_iochannel { int ifd, ofd; @@ -103,7 +104,7 @@ struct pa_iochannel* pa_iochannel_new(struct pa_mainloop_api*m, int ifd, int ofd struct pa_iochannel *io; assert(m && (ifd >= 0 || ofd >= 0)); - io = malloc(sizeof(struct pa_iochannel)); + io = pa_xmalloc(sizeof(struct pa_iochannel)); io->ifd = ifd; io->ofd = ofd; io->mainloop = m; @@ -152,7 +153,7 @@ void pa_iochannel_free(struct pa_iochannel*io) { if (io->output_source && (io->output_source != io->input_source)) io->mainloop->cancel_io(io->mainloop, io->output_source); - free(io); + pa_xfree(io); } int pa_iochannel_is_readable(struct pa_iochannel*io) { diff --git a/polyp/ioline.c b/polyp/ioline.c index ff9a03c2..dfa92ab7 100644 --- a/polyp/ioline.c +++ b/polyp/ioline.c @@ -30,6 +30,7 @@ #include #include "ioline.h" +#include "xmalloc.h" #define BUFFER_LIMIT (64*1024) #define READ_SIZE (1024) @@ -55,8 +56,7 @@ struct pa_ioline* pa_ioline_new(struct pa_iochannel *io) { struct pa_ioline *l; assert(io); - l = malloc(sizeof(struct pa_ioline)); - assert(l); + l = pa_xmalloc(sizeof(struct pa_ioline)); l->io = io; l->dead = 0; @@ -77,9 +77,9 @@ struct pa_ioline* pa_ioline_new(struct pa_iochannel *io) { void pa_ioline_free(struct pa_ioline *l) { assert(l); pa_iochannel_free(l->io); - free(l->wbuf); - free(l->rbuf); - free(l); + pa_xfree(l->wbuf); + pa_xfree(l->rbuf); + pa_xfree(l); } void pa_ioline_puts(struct pa_ioline *l, const char *c) { @@ -95,10 +95,10 @@ void pa_ioline_puts(struct pa_ioline *l, const char *c) { if (len > l->wbuf_length - l->wbuf_valid_length) { size_t n = l->wbuf_valid_length+len; - char *new = malloc(n); + char *new = pa_xmalloc(n); if (l->wbuf) { memcpy(new, l->wbuf+l->wbuf_index, l->wbuf_valid_length); - free(l->wbuf); + pa_xfree(l->wbuf); } l->wbuf = new; l->wbuf_length = n; @@ -141,10 +141,10 @@ static int do_read(struct pa_ioline *l) { if (l->rbuf_valid_length) memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length); } else { - char *new = malloc(n); + char *new = pa_xmalloc(n); if (l->rbuf_valid_length) memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length); - free(l->rbuf); + pa_xfree(l->rbuf); l->rbuf = new; l->rbuf_length = n; } diff --git a/polyp/main.c b/polyp/main.c index 0c6104a4..de66f1c8 100644 --- a/polyp/main.c +++ b/polyp/main.c @@ -42,6 +42,7 @@ #include "cli-command.h" #include "util.h" #include "sioman.h" +#include "xmalloc.h" static struct pa_mainloop *mainloop; @@ -147,7 +148,7 @@ int main(int argc, char *argv[]) { assert(buf); r = pa_cli_command_execute(c, cmdline->cli_commands, buf, &cmdline->fail, &cmdline->verbose); fprintf(stderr, s = pa_strbuf_tostring_free(buf)); - free(s); + pa_xfree(s); if (r < 0 && cmdline->fail) { fprintf(stderr, __FILE__": failed to initialize daemon.\n"); diff --git a/polyp/mainloop-api.c b/polyp/mainloop-api.c index cce49c06..8b4e09ac 100644 --- a/polyp/mainloop-api.c +++ b/polyp/mainloop-api.c @@ -25,7 +25,9 @@ #include #include + #include "mainloop-api.h" +#include "xmalloc.h" struct once_info { void (*callback)(void *userdata); @@ -38,7 +40,7 @@ static void once_callback(struct pa_mainloop_api *api, void *id, void *userdata) i->callback(i->userdata); assert(api->cancel_fixed); api->cancel_fixed(api, id); - free(i); + pa_xfree(i); } void pa_mainloop_api_once(struct pa_mainloop_api* api, void (*callback)(void *userdata), void *userdata) { @@ -46,8 +48,7 @@ void pa_mainloop_api_once(struct pa_mainloop_api* api, void (*callback)(void *us void *id; assert(api && callback); - i = malloc(sizeof(struct once_info)); - assert(i); + i = pa_xmalloc(sizeof(struct once_info)); i->callback = callback; i->userdata = userdata; diff --git a/polyp/mainloop-signal.c b/polyp/mainloop-signal.c index 642ca5e0..6e79767a 100644 --- a/polyp/mainloop-signal.c +++ b/polyp/mainloop-signal.c @@ -33,6 +33,7 @@ #include "mainloop-signal.h" #include "util.h" +#include "xmalloc.h" struct signal_info { int sig; @@ -122,8 +123,7 @@ void* pa_signal_register(int sig, void (*callback) (void *id, int signal, void * if (s->sig == sig) goto fail; - s = malloc(sizeof(struct signal_info)); - assert(s); + s = pa_xmalloc(sizeof(struct signal_info)); s->sig = sig; s->callback = callback; s->userdata = userdata; @@ -143,7 +143,7 @@ void* pa_signal_register(int sig, void (*callback) (void *id, int signal, void * return s; fail: if (s) - free(s); + pa_xfree(s); return NULL; } @@ -159,5 +159,5 @@ void pa_signal_unregister(void *id) { signals = s->next; sigaction(s->sig, &s->saved_sigaction, NULL); - free(s); + pa_xfree(s); } diff --git a/polyp/mainloop.c b/polyp/mainloop.c index b9eee86d..f3d8e781 100644 --- a/polyp/mainloop.c +++ b/polyp/mainloop.c @@ -36,6 +36,7 @@ #include "mainloop.h" #include "util.h" #include "idxset.h" +#include "xmalloc.h" struct mainloop_source_header { struct pa_mainloop *mainloop; @@ -87,8 +88,7 @@ static void setup_api(struct pa_mainloop *m); struct pa_mainloop *pa_mainloop_new(void) { struct pa_mainloop *m; - m = malloc(sizeof(struct pa_mainloop)); - assert(m); + m = pa_xmalloc(sizeof(struct pa_mainloop)); m->io_sources = pa_idxset_new(NULL, NULL); m->fixed_sources = pa_idxset_new(NULL, NULL); @@ -115,7 +115,7 @@ static int foreach(void *p, uint32_t index, int *del, void*userdata) { assert(p && del && all); if (*all || h->dead) { - free(h); + pa_xfree(h); *del = 1; } @@ -135,8 +135,8 @@ void pa_mainloop_free(struct pa_mainloop* m) { pa_idxset_free(m->idle_sources, NULL, NULL); pa_idxset_free(m->time_sources, NULL, NULL); - free(m->pollfds); - free(m); + pa_xfree(m->pollfds); + pa_xfree(m); } static void scan_dead(struct pa_mainloop *m) { @@ -160,7 +160,7 @@ static void rebuild_pollfds(struct pa_mainloop *m) { l = pa_idxset_ncontents(m->io_sources); if (m->max_pollfds < l) { - m->pollfds = realloc(m->pollfds, sizeof(struct pollfd)*l); + m->pollfds = pa_xrealloc(m->pollfds, sizeof(struct pollfd)*l); m->max_pollfds = l; } @@ -349,8 +349,7 @@ static void* mainloop_source_io(struct pa_mainloop_api*a, int fd, enum pa_mainlo m = a->userdata; assert(a == &m->api); - s = malloc(sizeof(struct mainloop_source_io)); - assert(s); + s = pa_xmalloc(sizeof(struct mainloop_source_io)); s->header.mainloop = m; s->header.dead = 0; @@ -397,8 +396,7 @@ static void* mainloop_source_fixed(struct pa_mainloop_api*a, void (*callback) (s m = a->userdata; assert(a == &m->api); - s = malloc(sizeof(struct mainloop_source_fixed_or_idle)); - assert(s); + s = pa_xmalloc(sizeof(struct mainloop_source_fixed_or_idle)); s->header.mainloop = m; s->header.dead = 0; @@ -439,8 +437,7 @@ static void* mainloop_source_idle(struct pa_mainloop_api*a, void (*callback) (st m = a->userdata; assert(a == &m->api); - s = malloc(sizeof(struct mainloop_source_fixed_or_idle)); - assert(s); + s = pa_xmalloc(sizeof(struct mainloop_source_fixed_or_idle)); s->header.mainloop = m; s->header.dead = 0; @@ -471,8 +468,7 @@ static void* mainloop_source_time(struct pa_mainloop_api*a, const struct timeval m = a->userdata; assert(a == &m->api); - s = malloc(sizeof(struct mainloop_source_time)); - assert(s); + s = pa_xmalloc(sizeof(struct mainloop_source_time)); s->header.mainloop = m; s->header.dead = 0; diff --git a/polyp/memblock.c b/polyp/memblock.c index 8f24ff22..0571f5da 100644 --- a/polyp/memblock.c +++ b/polyp/memblock.c @@ -29,11 +29,12 @@ #include #include "memblock.h" +#include "xmalloc.h" static unsigned memblock_count = 0, memblock_total = 0; struct pa_memblock *pa_memblock_new(size_t length) { - struct pa_memblock *b = malloc(sizeof(struct pa_memblock)+length); + struct pa_memblock *b = pa_xmalloc(sizeof(struct pa_memblock)+length); b->type = PA_MEMBLOCK_APPENDED; b->ref = 1; b->length = length; @@ -44,7 +45,7 @@ struct pa_memblock *pa_memblock_new(size_t length) { } struct pa_memblock *pa_memblock_new_fixed(void *d, size_t length) { - struct pa_memblock *b = malloc(sizeof(struct pa_memblock)); + struct pa_memblock *b = pa_xmalloc(sizeof(struct pa_memblock)); b->type = PA_MEMBLOCK_FIXED; b->ref = 1; b->length = length; @@ -55,7 +56,7 @@ struct pa_memblock *pa_memblock_new_fixed(void *d, size_t length) { } struct pa_memblock *pa_memblock_new_dynamic(void *d, size_t length) { - struct pa_memblock *b = malloc(sizeof(struct pa_memblock)); + struct pa_memblock *b = pa_xmalloc(sizeof(struct pa_memblock)); b->type = PA_MEMBLOCK_DYNAMIC; b->ref = 1; b->length = length; @@ -77,12 +78,12 @@ void pa_memblock_unref(struct pa_memblock*b) { if (b->ref == 0) { if (b->type == PA_MEMBLOCK_DYNAMIC) - free(b->data); + pa_xfree(b->data); memblock_count--; memblock_total -= b->length; - free(b); + pa_xfree(b); } } @@ -95,8 +96,7 @@ void pa_memblock_unref_fixed(struct pa_memblock *b) { pa_memblock_unref(b); return; } else { - d = malloc(b->length); - assert(d); + d = pa_xmalloc(b->length); memcpy(d, b->data, b->length); b->data = d; b->type = PA_MEMBLOCK_DYNAMIC; diff --git a/polyp/memblockq.c b/polyp/memblockq.c index eff923b9..4019c893 100644 --- a/polyp/memblockq.c +++ b/polyp/memblockq.c @@ -30,6 +30,7 @@ #include #include "memblockq.h" +#include "xmalloc.h" struct memblock_list { struct memblock_list *next; @@ -50,8 +51,7 @@ struct pa_memblockq* pa_memblockq_new(size_t maxlength, size_t tlength, size_t b struct pa_memblockq* bq; assert(maxlength && base && maxlength); - bq = malloc(sizeof(struct pa_memblockq)); - assert(bq); + bq = pa_xmalloc(sizeof(struct pa_memblockq)); bq->blocks = bq->blocks_tail = 0; bq->n_blocks = 0; @@ -97,10 +97,10 @@ void pa_memblockq_free(struct pa_memblockq* bq) { while ((l = bq->blocks)) { bq->blocks = l->next; pa_memblock_unref(l->chunk.memblock); - free(l); + pa_xfree(l); } - free(bq); + pa_xfree(bq); } void pa_memblockq_push(struct pa_memblockq* bq, const struct pa_memchunk *chunk, size_t delta) { @@ -119,8 +119,7 @@ void pa_memblockq_push(struct pa_memblockq* bq, const struct pa_memchunk *chunk, } } - q = malloc(sizeof(struct memblock_list)); - assert(q); + q = pa_xmalloc(sizeof(struct memblock_list)); if (bq->measure_delay) gettimeofday(&q->stamp, NULL); @@ -181,7 +180,7 @@ int memblockq_pop(struct memblockq* bq, struct pa_memchunk *chunk) { bq->n_blocks--; bq->current_length -= chunk->length; - free(q); + pa_xfree(q); return 0; } */ @@ -231,7 +230,7 @@ void pa_memblockq_drop(struct pa_memblockq *bq, size_t length) { if (bq->blocks == NULL) bq->blocks_tail = NULL; pa_memblock_unref(q->chunk.memblock); - free(q); + pa_xfree(q); bq->n_blocks--; } diff --git a/polyp/memchunk.c b/polyp/memchunk.c index d27ca61a..77e1b7dd 100644 --- a/polyp/memchunk.c +++ b/polyp/memchunk.c @@ -29,6 +29,7 @@ #include #include "memchunk.h" +#include "xmalloc.h" void pa_memchunk_make_writable(struct pa_memchunk *c) { struct pa_memblock *n; @@ -57,8 +58,7 @@ struct pa_mcalign *pa_mcalign_new(size_t base) { struct pa_mcalign *m; assert(base); - m = malloc(sizeof(struct pa_mcalign)); - assert(m); + m = pa_xmalloc(sizeof(struct pa_mcalign)); m->base = base; m->chunk.memblock = NULL; m->chunk.length = m->chunk.index = 0; @@ -70,12 +70,12 @@ struct pa_mcalign *pa_mcalign_new(size_t base) { void pa_mcalign_free(struct pa_mcalign *m) { assert(m); - free(m->buffer); + pa_xfree(m->buffer); if (m->chunk.memblock) pa_memblock_unref(m->chunk.memblock); - free(m); + pa_xfree(m); } void pa_mcalign_push(struct pa_mcalign *m, const struct pa_memchunk *c) { @@ -128,8 +128,7 @@ int pa_mcalign_pop(struct pa_mcalign *m, struct pa_memchunk *c) { if (m->buffer_fill) { assert(!m->buffer); - m->buffer = malloc(m->base); - assert(m->buffer); + m->buffer = pa_xmalloc(m->base); m->chunk.length -= m->buffer_fill; memcpy(m->buffer, m->chunk.memblock->data + m->chunk.index + m->chunk.length, m->buffer_fill); } diff --git a/polyp/modargs.c b/polyp/modargs.c index 3841a9ec..87d99ad2 100644 --- a/polyp/modargs.c +++ b/polyp/modargs.c @@ -35,8 +35,7 @@ #include "namereg.h" #include "sink.h" #include "source.h" - -#include "debug.h" +#include "xmalloc.h" struct pa_modargs; @@ -55,14 +54,13 @@ static int add_key_value(struct pa_hashmap *map, char *key, char *value, const c break; if (!*v) { - free(key); - free(value); + pa_xfree(key); + pa_xfree(value); return -1; } } - e = malloc(sizeof(struct entry)); - assert(e); + e = pa_xmalloc(sizeof(struct entry)); e->key = key; e->value = value; pa_hashmap_put(map, key, e); @@ -109,7 +107,7 @@ struct pa_modargs *pa_modargs_new(const char *args, const char* const* valid_key value = p+1; value_len = 0; } else if (isspace(*p)) { - if (add_key_value(map, strndup(key, key_len), strdup(""), valid_keys) < 0) + if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0) goto fail; state = WHITESPACE; } else { @@ -120,7 +118,7 @@ struct pa_modargs *pa_modargs_new(const char *args, const char* const* valid_key break; case VALUE_SIMPLE: if (isspace(*p)) { - if (add_key_value(map, strndup(key, key_len), strndup(value, value_len), valid_keys) < 0) + if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0) goto fail; state = WHITESPACE; } else @@ -128,7 +126,7 @@ struct pa_modargs *pa_modargs_new(const char *args, const char* const* valid_key break; case VALUE_DOUBLE_QUOTES: if (*p == '"') { - if (add_key_value(map, strndup(key, key_len), strndup(value, value_len), valid_keys) < 0) + if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0) goto fail; state = WHITESPACE; } else @@ -136,7 +134,7 @@ struct pa_modargs *pa_modargs_new(const char *args, const char* const* valid_key break; case VALUE_TICKS: if (*p == '\'') { - if (add_key_value(map, strndup(key, key_len), strndup(value, value_len), valid_keys) < 0) + if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrndup(value, value_len), valid_keys) < 0) goto fail; state = WHITESPACE; } else @@ -146,10 +144,10 @@ struct pa_modargs *pa_modargs_new(const char *args, const char* const* valid_key } if (state == VALUE_START) { - if (add_key_value(map, strndup(key, key_len), strdup(""), valid_keys) < 0) + if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(""), valid_keys) < 0) goto fail; } else if (state == VALUE_SIMPLE) { - if (add_key_value(map, strndup(key, key_len), strdup(value), valid_keys) < 0) + if (add_key_value(map, pa_xstrndup(key, key_len), pa_xstrdup(value), valid_keys) < 0) goto fail; } else if (state != WHITESPACE) goto fail; @@ -169,9 +167,9 @@ fail: static void free_func(void *p, void*userdata) { struct entry *e = p; assert(e); - free(e->key); - free(e->value); - free(e); + pa_xfree(e->key); + pa_xfree(e->value); + pa_xfree(e); } void pa_modargs_free(struct pa_modargs*ma) { @@ -256,37 +254,3 @@ int pa_modargs_get_sample_spec(struct pa_modargs *ma, struct pa_sample_spec *rss return 0; } - -int pa_modargs_get_source_index(struct pa_modargs *ma, struct pa_core *c, uint32_t *index) { - const char *t; - assert(ma && index); - - if (!(t = pa_modargs_get_value(ma, "source", NULL))) - *index = PA_IDXSET_INVALID; - else { - struct pa_source *source; - if (!(source = pa_namereg_get(c, t, PA_NAMEREG_SOURCE))) - return -1; - - *index = source->index; - } - - return 0; -} - -int pa_modargs_get_sink_index(struct pa_modargs *ma, struct pa_core *c, uint32_t *index) { - const char *t; - assert(ma && index); - - if (!(t = pa_modargs_get_value(ma, "sink", NULL))) - *index = PA_IDXSET_INVALID; - else { - struct pa_sink *sink; - if (!(sink = pa_namereg_get(c, t, PA_NAMEREG_SINK))) - return -1; - - *index = sink->index; - } - - return 0; -} diff --git a/polyp/modargs.h b/polyp/modargs.h index 301dc297..872fb14f 100644 --- a/polyp/modargs.h +++ b/polyp/modargs.h @@ -36,7 +36,4 @@ int pa_modargs_get_value_u32(struct pa_modargs *ma, const char *key, uint32_t *v int pa_modargs_get_sample_spec(struct pa_modargs *ma, struct pa_sample_spec *ss); -int pa_modargs_get_source_index(struct pa_modargs *ma, struct pa_core *c, uint32_t *index); -int pa_modargs_get_sink_index(struct pa_modargs *ma, struct pa_core *c, uint32_t *index); - #endif diff --git a/polyp/module-alsa-sink.c b/polyp/module-alsa-sink.c index c1958227..a0fa52db 100644 --- a/polyp/module-alsa-sink.c +++ b/polyp/module-alsa-sink.c @@ -37,6 +37,7 @@ #include "util.h" #include "sample-util.h" #include "alsa-util.h" +#include "xmalloc.h" struct userdata { snd_pcm_t *pcm_handle; @@ -46,6 +47,7 @@ struct userdata { size_t frame_size, fragment_size; struct pa_memchunk memchunk, silence; + struct pa_module *module; }; static const char* const valid_modargs[] = { @@ -62,6 +64,12 @@ static const char* const valid_modargs[] = { #define DEFAULT_SINK_NAME "alsa_output" #define DEFAULT_DEVICE "plughw:0,0" +static void update_usage(struct userdata *u) { + pa_module_set_used(u->module, + (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) + + (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0)); +} + static void xrun_recovery(struct userdata *u) { assert(u); @@ -74,6 +82,8 @@ static void xrun_recovery(struct userdata *u) { static void do_write(struct userdata *u) { assert(u); + update_usage(u); + for (;;) { struct pa_memchunk *memchunk = NULL; snd_pcm_sframes_t frames; @@ -175,10 +185,9 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { } buffer_size = fragsize/frame_size*periods; - u = malloc(sizeof(struct userdata)); - assert(u); - memset(u, 0, sizeof(struct userdata)); + u = pa_xmalloc0(sizeof(struct userdata)); m->userdata = u; + u->module = m; if (snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) { fprintf(stderr, __FILE__": Error opening PCM device %s\n", dev); @@ -236,24 +245,25 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { struct userdata *u; assert(c && m); - if ((u = m->userdata)) { - if (u->sink) - pa_sink_free(u->sink); - - if (u->io_sources) - pa_free_io_sources(c->mainloop, u->io_sources, u->n_io_sources); - - if (u->pcm_handle) { - snd_pcm_drop(u->pcm_handle); - snd_pcm_close(u->pcm_handle); - } - - if (u->memchunk.memblock) - pa_memblock_unref(u->memchunk.memblock); - if (u->silence.memblock) - pa_memblock_unref(u->silence.memblock); - - free(u); + if (!(u = m->userdata)) + return; + + if (u->sink) + pa_sink_free(u->sink); + + if (u->io_sources) + pa_free_io_sources(c->mainloop, u->io_sources, u->n_io_sources); + + if (u->pcm_handle) { + snd_pcm_drop(u->pcm_handle); + snd_pcm_close(u->pcm_handle); } + + if (u->memchunk.memblock) + pa_memblock_unref(u->memchunk.memblock); + if (u->silence.memblock) + pa_memblock_unref(u->silence.memblock); + + pa_xfree(u); } diff --git a/polyp/module-alsa-source.c b/polyp/module-alsa-source.c index a453944e..8207d462 100644 --- a/polyp/module-alsa-source.c +++ b/polyp/module-alsa-source.c @@ -37,6 +37,7 @@ #include "util.h" #include "sample-util.h" #include "alsa-util.h" +#include "xmalloc.h" struct userdata { snd_pcm_t *pcm_handle; @@ -46,6 +47,7 @@ struct userdata { size_t frame_size, fragment_size; struct pa_memchunk memchunk; + struct pa_module *module; }; static const char* const valid_modargs[] = { @@ -62,6 +64,11 @@ static const char* const valid_modargs[] = { #define DEFAULT_SOURCE_NAME "alsa_input" #define DEFAULT_DEVICE "hw:0,0" +static void update_usage(struct userdata *u) { + pa_module_set_used(u->module, + (u->source ? pa_idxset_ncontents(u->source->outputs) : 0)); +} + static void xrun_recovery(struct userdata *u) { assert(u); @@ -74,6 +81,8 @@ static void xrun_recovery(struct userdata *u) { static void do_read(struct userdata *u) { assert(u); + update_usage(u); + for (;;) { struct pa_memchunk post_memchunk; snd_pcm_sframes_t frames; @@ -159,10 +168,9 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { } buffer_size = fragsize/frame_size*periods; - u = malloc(sizeof(struct userdata)); - assert(u); - memset(u, 0, sizeof(struct userdata)); + u = pa_xmalloc0(sizeof(struct userdata)); m->userdata = u; + u->module = m; if (snd_pcm_open(&u->pcm_handle, dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) < 0) { fprintf(stderr, __FILE__": Error opening PCM device %s\n", dev); @@ -216,22 +224,23 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { struct userdata *u; assert(c && m); - if ((u = m->userdata)) { - if (u->source) - pa_source_free(u->source); - - if (u->io_sources) - pa_free_io_sources(c->mainloop, u->io_sources, u->n_io_sources); - - if (u->pcm_handle) { - snd_pcm_drop(u->pcm_handle); - snd_pcm_close(u->pcm_handle); - } - - if (u->memchunk.memblock) - pa_memblock_unref(u->memchunk.memblock); - - free(u); + if (!(u = m->userdata)) + return; + + if (u->source) + pa_source_free(u->source); + + if (u->io_sources) + pa_free_io_sources(c->mainloop, u->io_sources, u->n_io_sources); + + if (u->pcm_handle) { + snd_pcm_drop(u->pcm_handle); + snd_pcm_close(u->pcm_handle); } + + if (u->memchunk.memblock) + pa_memblock_unref(u->memchunk.memblock); + + pa_xfree(u); } diff --git a/polyp/module-oss-mmap.c b/polyp/module-oss-mmap.c index 30ff3ce6..37710fc5 100644 --- a/polyp/module-oss-mmap.c +++ b/polyp/module-oss-mmap.c @@ -44,6 +44,7 @@ #include "sample-util.h" #include "util.h" #include "modargs.h" +#include "xmalloc.h" struct userdata { struct pa_sink *sink; @@ -62,6 +63,7 @@ struct userdata { struct pa_memblock **in_memblocks, **out_memblocks; unsigned out_current, in_current; + struct pa_module *module; }; static const char* const valid_modargs[] = { @@ -82,6 +84,13 @@ static const char* const valid_modargs[] = { #define DEFAULT_SOURCE_NAME "oss_input" #define DEFAULT_DEVICE "/dev/dsp" +static void update_usage(struct userdata *u) { + pa_module_set_used(u->module, + (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) + + (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) + + (u->source ? pa_idxset_ncontents(u->source->outputs) : 0)); +} + static void out_fill_memblocks(struct userdata *u, unsigned n) { assert(u && u->out_memblocks); @@ -110,6 +119,8 @@ static void do_write(struct userdata *u) { struct count_info info; assert(u && u->sink); + update_usage(u); + if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) { fprintf(stderr, "SNDCTL_DSP_GETOPTR: %s\n", strerror(errno)); return; @@ -170,6 +181,8 @@ static void do_read(struct userdata *u) { struct count_info info; assert(u && u->source); + update_usage(u); + if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) { fprintf(stderr, "SNDCTL_DSP_GETIPTR: %s\n", strerror(errno)); return; @@ -212,9 +225,8 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { struct pa_modargs *ma = NULL; assert(c && m); - m->userdata = u = malloc(sizeof(struct userdata)); - assert(u); - memset(u, 0, sizeof(struct userdata)); + m->userdata = u = pa_xmalloc0(sizeof(struct userdata)); + u->module = m; u->fd = -1; u->core = c; @@ -288,9 +300,7 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { pa_source_set_owner(u->source, m); u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p); - - u->in_memblocks = malloc(sizeof(struct pa_memblock *)*u->in_fragments); - memset(u->in_memblocks, 0, sizeof(struct pa_memblock *)*u->in_fragments); + u->in_memblocks = pa_xmalloc0(sizeof(struct pa_memblock *)*u->in_fragments); enable_bits |= PCM_ENABLE_INPUT; } @@ -323,8 +333,7 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { pa_sink_set_owner(u->sink, m); u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p); - u->out_memblocks = malloc(sizeof(struct memblock *)*u->out_fragments); - memset(u->out_memblocks, 0, sizeof(struct pa_memblock *)*u->out_fragments); + u->out_memblocks = pa_xmalloc0(sizeof(struct memblock *)*u->out_fragments); enable_bits |= PCM_ENABLE_OUTPUT; } @@ -363,15 +372,15 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { struct userdata *u; assert(c && m); - u = m->userdata; - assert(u); + if (!(u = m->userdata)) + return; if (u->out_memblocks) { unsigned i; for (i = 0; i < u->out_fragments; i++) if (u->out_memblocks[i]) pa_memblock_unref_fixed(u->out_memblocks[i]); - free(u->out_memblocks); + pa_xfree(u->out_memblocks); } if (u->in_memblocks) { @@ -379,7 +388,7 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { for (i = 0; i < u->in_fragments; i++) if (u->in_memblocks[i]) pa_memblock_unref_fixed(u->in_memblocks[i]); - free(u->in_memblocks); + pa_xfree(u->in_memblocks); } if (u->in_mmap && u->in_mmap != MAP_FAILED) @@ -400,5 +409,5 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { if (u->fd >= 0) close(u->fd); - free(u); + pa_xfree(u); } diff --git a/polyp/module-oss.c b/polyp/module-oss.c index 51585ca9..a8db4a33 100644 --- a/polyp/module-oss.c +++ b/polyp/module-oss.c @@ -43,6 +43,7 @@ #include "sample-util.h" #include "util.h" #include "modargs.h" +#include "xmalloc.h" struct userdata { struct pa_sink *sink; @@ -55,6 +56,7 @@ struct userdata { uint32_t in_fragment_size, out_fragment_size, sample_size; int fd; + struct pa_module *module; }; static const char* const valid_modargs[] = { @@ -75,6 +77,13 @@ static const char* const valid_modargs[] = { #define DEFAULT_SOURCE_NAME "oss_input" #define DEFAULT_DEVICE "/dev/dsp" +static void update_usage(struct userdata *u) { + pa_module_set_used(u->module, + (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) + + (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) + + (u->source ? pa_idxset_ncontents(u->source->outputs) : 0)); +} + static void do_write(struct userdata *u) { struct pa_memchunk *memchunk; ssize_t r; @@ -83,6 +92,8 @@ static void do_write(struct userdata *u) { if (!u->sink || !pa_iochannel_is_writable(u->io)) return; + update_usage(u); + if (!u->memchunk.length) { if (pa_sink_render(u->sink, u->out_fragment_size, &u->memchunk) < 0) memchunk = &u->silence; @@ -118,6 +129,8 @@ static void do_read(struct userdata *u) { if (!u->source || !pa_iochannel_is_readable(u->io)) return; + update_usage(u); + memchunk.memblock = pa_memblock_new(u->in_fragment_size); assert(memchunk.memblock); if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) { @@ -225,9 +238,7 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { out_frag_size = info.fragsize; } - u = malloc(sizeof(struct userdata)); - assert(u); - + u = pa_xmalloc(sizeof(struct userdata)); u->core = c; if (mode != O_WRONLY) { @@ -266,7 +277,8 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { assert(u->silence.memblock); pa_silence_memblock(u->silence.memblock, &ss); u->silence.index = 0; - + + u->module = m; m->userdata = u; pa_modargs_free(ma); @@ -287,8 +299,8 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { struct userdata *u; assert(c && m); - u = m->userdata; - assert(u); + if (!(u = m->userdata)) + return; if (u->memchunk.memblock) pa_memblock_unref(u->memchunk.memblock); @@ -299,6 +311,7 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { pa_sink_free(u->sink); if (u->source) pa_source_free(u->source); + pa_iochannel_free(u->io); - free(u); + pa_xfree(u); } diff --git a/polyp/module-pipe-sink.c b/polyp/module-pipe-sink.c index df34f73a..dc2bc633 100644 --- a/polyp/module-pipe-sink.c +++ b/polyp/module-pipe-sink.c @@ -38,6 +38,7 @@ #include "module.h" #include "util.h" #include "modargs.h" +#include "xmalloc.h" #define DEFAULT_FIFO_NAME "/tmp/musicfifo" #define DEFAULT_SINK_NAME "fifo_output" @@ -52,6 +53,7 @@ struct userdata { void *mainloop_source; struct pa_memchunk memchunk; + struct pa_module *module; }; static const char* const valid_modargs[] = { @@ -72,6 +74,8 @@ static void do_write(struct userdata *u) { if (!pa_iochannel_is_writable(u->io)) return; + pa_module_set_used(u->module, pa_idxset_ncontents(u->sink->inputs) + pa_idxset_ncontents(u->sink->monitor_source->outputs)); + if (!u->memchunk.length) if (pa_sink_render(u->sink, PIPE_BUF, &u->memchunk) < 0) return; @@ -149,12 +153,9 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { goto fail; } - u = malloc(sizeof(struct userdata)); - assert(u); - memset(u, 0, sizeof(struct userdata)); + u = pa_xmalloc0(sizeof(struct userdata)); - u->filename = strdup(p); - assert(u->filename); + u->filename = pa_xstrdup(p); u->core = c; if (!(u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss))) { @@ -177,7 +178,8 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { u->mainloop_source = c->mainloop->source_fixed(c->mainloop, fixed_callback, u); assert(u->mainloop_source); c->mainloop->enable_fixed(c->mainloop, u->mainloop_source, 0); - + + u->module = m; m->userdata = u; pa_modargs_free(ma); @@ -212,7 +214,7 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { assert(u->filename); unlink(u->filename); - free(u->filename); + pa_xfree(u->filename); - free(u); + pa_xfree(u); } diff --git a/polyp/module-x11-bell.c b/polyp/module-x11-bell.c index 4da3c880..2cf76099 100644 --- a/polyp/module-x11-bell.c +++ b/polyp/module-x11-bell.c @@ -10,6 +10,8 @@ #include "sink.h" #include "scache.h" #include "modargs.h" +#include "xmalloc.h" +#include "namereg.h" struct x11_source { void *io_source; @@ -22,7 +24,7 @@ struct userdata { struct x11_source *x11_sources; int xkb_event_base; - int sink_index; + char *sink_name; char *scache_item; }; @@ -33,22 +35,11 @@ static const char* const valid_modargs[] = { NULL }; -static struct pa_sink* get_output_sink(struct userdata *u) { - struct pa_sink *s; - assert(u); - - if (!(s = pa_idxset_get_by_index(u->core->sinks, u->sink_index))) - s = pa_sink_get_default(u->core); - - u->sink_index = s ? s->index : PA_IDXSET_INVALID; - return s; -} - static int ring_bell(struct userdata *u, int percent) { struct pa_sink *s; assert(u); - if (!(s = get_output_sink(u))) { + if (!(s = pa_namereg_get(u->core, u->sink_name, PA_NAMEREG_SINK, 1))) { fprintf(stderr, __FILE__": Invalid sink\n"); return -1; } @@ -85,8 +76,7 @@ static void io_callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_main static void new_io_source(struct userdata *u, int fd) { struct x11_source *s; - s = malloc(sizeof(struct x11_source)); - assert(s); + s = pa_xmalloc(sizeof(struct x11_source)); s->io_source = u->core->mainloop->source_io(u->core->mainloop, fd, PA_MAINLOOP_API_IO_EVENT_INPUT, io_callback, u); assert(s->io_source); s->next = u->x11_sources; @@ -105,18 +95,12 @@ int pa_module_init(struct pa_core *c, struct pa_module*m) { goto fail; } - m->userdata = u = malloc(sizeof(struct userdata)); - assert(u); + m->userdata = u = pa_xmalloc(sizeof(struct userdata)); u->core = c; u->display = NULL; u->x11_sources = NULL; - u->scache_item = strdup(pa_modargs_get_value(ma, "sample", "x11-bell")); - assert(u->scache_item); - - if (pa_modargs_get_sink_index(ma, c, &u->sink_index) < 0) { - fprintf(stderr, __FILE__": Invalid sink specified\n"); - goto fail; - } + u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "x11-bell")); + u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL)); if (!(u->display = XOpenDisplay(pa_modargs_get_value(ma, "display", NULL)))) { fprintf(stderr, __FILE__": XOpenDisplay() failed\n"); @@ -166,12 +150,12 @@ void pa_module_done(struct pa_core *c, struct pa_module*m) { struct x11_source *s = u->x11_sources; u->x11_sources = u->x11_sources->next; c->mainloop->cancel_io(c->mainloop, s->io_source); - free(s); + pa_xfree(s); } - free(u->scache_item); + pa_xfree(u->scache_item); if (u->display) XCloseDisplay(u->display); - free(u); + pa_xfree(u); } diff --git a/polyp/module.c b/polyp/module.c index 5c6f0fb6..8c5e318f 100644 --- a/polyp/module.c +++ b/polyp/module.c @@ -31,6 +31,21 @@ #include #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; +} + diff --git a/polyp/module.h b/polyp/module.h index af2d8552..de195a4b 100644 --- a/polyp/module.h +++ b/polyp/module.h @@ -38,6 +38,10 @@ struct pa_module { void (*done)(struct pa_core *c, struct pa_module*m); void *userdata; + + int n_used; + int auto_unload; + time_t last_used_time; }; struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char*argument); @@ -45,6 +49,7 @@ void pa_module_unload(struct pa_core *c, struct pa_module *m); void pa_module_unload_by_index(struct pa_core *c, uint32_t index); void pa_module_unload_all(struct pa_core *c); +void pa_module_unload_unused(struct pa_core *c); void pa_module_unload_request(struct pa_core *c, struct pa_module *m); @@ -52,4 +57,7 @@ void pa_module_unload_request(struct pa_core *c, struct pa_module *m); int pa_module_init(struct pa_core *c, struct pa_module*m); void pa_module_done(struct pa_core *c, struct pa_module*m); +void pa_module_set_used(struct pa_module*m, int used); + + #endif diff --git a/polyp/namereg.c b/polyp/namereg.c index 2349436f..b6a8c23f 100644 --- a/polyp/namereg.c +++ b/polyp/namereg.c @@ -31,6 +31,10 @@ #include #include "namereg.h" +#include "autoload.h" +#include "source.h" +#include "sink.h" +#include "xmalloc.h" struct namereg_entry { enum pa_namereg_type type; @@ -62,12 +66,11 @@ const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_nam return NULL; if (!e) - n = strdup(name); + n = pa_xstrdup(name); else { unsigned i; size_t l = strlen(name); - n = malloc(l+3); - assert(n); + n = pa_xmalloc(l+3); for (i = 1; i <= 99; i++) { snprintf(n, l+2, "%s%u", name, i); @@ -77,14 +80,13 @@ const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_nam } if (e) { - free(n); + pa_xfree(n); return NULL; } } assert(n); - e = malloc(sizeof(struct namereg_entry)); - assert(e); + e = pa_xmalloc(sizeof(struct namereg_entry)); e->type = type; e->name = n; e->data = data; @@ -107,25 +109,66 @@ void pa_namereg_unregister(struct pa_core *c, const char *name) { r = pa_hashmap_remove(c->namereg, name); assert(r >= 0); - free(e->name); - free(e); + pa_xfree(e->name); + pa_xfree(e); } -void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type) { +void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type, int autoload) { struct namereg_entry *e; uint32_t index; char *x = NULL; void *d = NULL; - assert(c && name); + assert(c); + + if (!name) { + if (type == PA_NAMEREG_SOURCE) { + if (!c->default_source_name) { + struct pa_source *s; + + for (s = pa_idxset_first(c->sources, &index); s; s = pa_idxset_next(c->sources, &index)) + if (!s->monitor_of) { + pa_namereg_set_default(c, s->name, PA_NAMEREG_SOURCE); + break; + } + } + + name = c->default_source_name; + + } else { + assert(type == PA_NAMEREG_SINK); + + if (!c->default_sink_name) { + struct pa_sink *s; + + if ((s = pa_idxset_first(c->sinks, NULL))) + pa_namereg_set_default(c, s->name, PA_NAMEREG_SINK); + } + + name = c->default_sink_name; + } + } - if ((e = pa_hashmap_get(c->namereg, name))) + if (!name) + return NULL; + + if (c->namereg && (e = pa_hashmap_get(c->namereg, name))) if (e->type == e->type) return e->data; index = (uint32_t) strtol(name, &x, 0); - if (!x || *x != 0) + if (!x || *x != 0) { + + if (autoload) { + pa_autoload_request(c, name, type); + + if (c->namereg && (e = pa_hashmap_get(c->namereg, name))) + if (e->type == e->type) + return e->data; + } + return NULL; + } if (type == PA_NAMEREG_SINK) d = pa_idxset_get_by_index(c->sinks, index); @@ -134,3 +177,14 @@ void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type t return d; } + +void pa_namereg_set_default(struct pa_core*c, const char *name, enum pa_namereg_type type) { + char **s; + assert(c); + + s = type == PA_NAMEREG_SINK ? &c->default_sink_name : &c->default_source_name; + assert(s); + + pa_xfree(*s); + *s = pa_xstrdup(name); +} diff --git a/polyp/namereg.h b/polyp/namereg.h index 0af83cd8..5bc92693 100644 --- a/polyp/namereg.h +++ b/polyp/namereg.h @@ -33,6 +33,7 @@ void pa_namereg_free(struct pa_core *c); const char *pa_namereg_register(struct pa_core *c, const char *name, enum pa_namereg_type type, void *data, int fail); void pa_namereg_unregister(struct pa_core *c, const char *name); -void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type); +void* pa_namereg_get(struct pa_core *c, const char *name, enum pa_namereg_type type, int autoload); +void pa_namereg_set_default(struct pa_core*c, const char *name, enum pa_namereg_type type); #endif diff --git a/polyp/packet.c b/polyp/packet.c index e94df057..955feeb1 100644 --- a/polyp/packet.c +++ b/polyp/packet.c @@ -27,13 +27,12 @@ #include #include "packet.h" +#include "xmalloc.h" struct pa_packet* pa_packet_new(size_t length) { struct pa_packet *p; assert(length); - p = malloc(sizeof(struct pa_packet)+length); - assert(p); - + p = pa_xmalloc(sizeof(struct pa_packet)+length); p->ref = 1; p->length = length; p->data = (uint8_t*) (p+1); @@ -44,9 +43,7 @@ struct pa_packet* pa_packet_new(size_t length) { struct pa_packet* pa_packet_new_dynamic(uint8_t* data, size_t length) { struct pa_packet *p; assert(data && length); - p = malloc(sizeof(struct pa_packet)); - assert(p); - + p = pa_xmalloc(sizeof(struct pa_packet)); p->ref = 1; p->length = length; p->data = data; @@ -66,7 +63,7 @@ void pa_packet_unref(struct pa_packet *p) { if (p->ref == 0) { if (p->type == PA_PACKET_DYNAMIC) - free(p->data); - free(p); + pa_xfree(p->data); + pa_xfree(p); } } diff --git a/polyp/pdispatch.c b/polyp/pdispatch.c index 2ab98b52..c34c6e0a 100644 --- a/polyp/pdispatch.c +++ b/polyp/pdispatch.c @@ -29,6 +29,7 @@ #include "pdispatch.h" #include "native-common.h" +#include "xmalloc.h" /*#define DEBUG_OPCODES*/ @@ -96,7 +97,7 @@ static void reply_info_free(struct reply_info *r) { if (r->next) r->next->previous = r->previous; - free(r); + pa_xfree(r); } struct pa_pdispatch* pa_pdispatch_new(struct pa_mainloop_api *mainloop, const struct pa_pdispatch_command*table, unsigned entries) { @@ -105,8 +106,7 @@ struct pa_pdispatch* pa_pdispatch_new(struct pa_mainloop_api *mainloop, const st assert((entries && table) || (!entries && !table)); - pd = malloc(sizeof(struct pa_pdispatch)); - assert(pd); + pd = pa_xmalloc(sizeof(struct pa_pdispatch)); pd->mainloop = mainloop; pd->command_table = table; pd->n_commands = entries; @@ -129,7 +129,7 @@ void pa_pdispatch_free(struct pa_pdispatch *pd) { while (pd->replies) reply_info_free(pd->replies); - free(pd); + pa_xfree(pd); } int pa_pdispatch_run(struct pa_pdispatch *pd, struct pa_packet*packet, void *userdata) { @@ -207,8 +207,7 @@ void pa_pdispatch_register_reply(struct pa_pdispatch *pd, uint32_t tag, int time struct timeval tv; assert(pd && cb); - r = malloc(sizeof(struct reply_info)); - assert(r); + r = pa_xmalloc(sizeof(struct reply_info)); r->pdispatch = pd; r->callback = cb; r->userdata = userdata; diff --git a/polyp/polypaudio.pa b/polyp/polypaudio.pa index 79ca83ea..0989a78d 100755 --- a/polyp/polypaudio.pa +++ b/polyp/polypaudio.pa @@ -21,7 +21,7 @@ # Load audio drivers #load module-alsa-sink #load module-alsa-source device=plughw:1,0 -load module-oss device="/dev/dsp" +#load module-oss device="/dev/dsp" #load module-oss-mmap device="/dev/dsp" # Load several protocols @@ -36,9 +36,15 @@ load module-x11-bell # Load the CLI module load module-cli -.nofail +autoload_sink_add oss_output module-oss device="/dev/dsp" sink_name=oss_output source_name=oss_input +autoload_source_add oss_input module-oss device="/dev/dsp" sink_name=oss_output source_name=oss_input # Make some devices default sink_default oss_output source_default oss_input +.nofail + +# Load something to the sample cache +scache_load /usr/share/sounds/KDE_Notify.wav x11-bell +scache_play x11-bell oss_output diff --git a/polyp/polyplib-simple.c b/polyp/polyplib-simple.c index 024cb18a..1f5ea553 100644 --- a/polyp/polyplib-simple.c +++ b/polyp/polyplib-simple.c @@ -32,7 +32,7 @@ #include "polyplib.h" #include "mainloop.h" #include "native-common.h" -/*#include "polyp-error.h"*/ +#include "xmalloc.h" struct pa_simple { struct pa_mainloop *mainloop; @@ -98,8 +98,7 @@ struct pa_simple* pa_simple_new( int error = PA_ERROR_INTERNAL; assert(ss); - p = malloc(sizeof(struct pa_simple)); - assert(p); + p = pa_xmalloc(sizeof(struct pa_simple)); p->context = NULL; p->stream = NULL; p->mainloop = pa_mainloop_new(); @@ -146,7 +145,7 @@ fail: void pa_simple_free(struct pa_simple *s) { assert(s); - free(s->read_data); + pa_xfree(s->read_data); if (s->stream) pa_stream_free(s->stream); @@ -157,7 +156,7 @@ void pa_simple_free(struct pa_simple *s) { if (s->mainloop) pa_mainloop_free(s->mainloop); - free(s); + pa_xfree(s); } int pa_simple_write(struct pa_simple *p, const void*data, size_t length, int *perror) { @@ -191,11 +190,10 @@ static void read_callback(struct pa_stream *s, const void*data, size_t length, v if (p->read_data) { fprintf(stderr, __FILE__": Buffer overflow, dropping incoming memory blocks.\n"); - free(p->read_data); + pa_xfree(p->read_data); } - p->read_data = malloc(p->read_length = length); - assert(p->read_data); + p->read_data = pa_xmalloc(p->read_length = length); memcpy(p->read_data, data, length); p->read_index = 0; } @@ -219,7 +217,7 @@ int pa_simple_read(struct pa_simple *p, void*data, size_t length, int *perror) { p->read_length -= l; if (!p->read_length) { - free(p->read_data); + pa_xfree(p->read_data); p->read_data = NULL; p->read_index = 0; } diff --git a/polyp/polyplib.c b/polyp/polyplib.c index b1052a8d..4d87fdb5 100644 --- a/polyp/polyplib.c +++ b/polyp/polyplib.c @@ -40,6 +40,7 @@ #include "pstream-util.h" #include "authkey.h" #include "util.h" +#include "xmalloc.h" #define DEFAULT_MAXLENGTH 204800 #define DEFAULT_TLENGTH 10240 @@ -148,9 +149,8 @@ struct pa_context *pa_context_new(struct pa_mainloop_api *mainloop, const char * struct pa_context *c; assert(mainloop && name); - c = malloc(sizeof(struct pa_context)); - assert(c); - c->name = strdup(name); + c = pa_xmalloc(sizeof(struct pa_context)); + c->name = pa_xstrdup(name); c->mainloop = mainloop; c->client = NULL; c->pstream = NULL; @@ -203,8 +203,8 @@ void pa_context_free(struct pa_context *c) { if (c->playback_streams) pa_dynarray_free(c->playback_streams, NULL, NULL); - free(c->name); - free(c); + pa_xfree(c->name); + pa_xfree(c); } static void stream_dead(struct pa_stream *s) { @@ -391,14 +391,12 @@ static struct sockaddr *resolve_server(const char *server, size_t *len) { return NULL; assert(result); - sa = malloc(*len = result->ai_addrlen); - assert(sa); + sa = pa_xmalloc(*len = result->ai_addrlen); memcpy(sa, result->ai_addr, *len); freeaddrinfo(result); return sa; - } int pa_context_connect(struct pa_context *c, const char *server, void (*complete) (struct pa_context*c, int success, void *userdata), void *userdata) { @@ -430,7 +428,7 @@ int pa_context_connect(struct pa_context *c, const char *server, void (*complete } c->client = pa_socket_client_new_sockaddr(c->mainloop, sa, sa_len); - free(sa); + pa_xfree(sa); if (!c->client) { c->error = PA_ERROR_CONNECTIONREFUSED; @@ -578,8 +576,7 @@ static void create_stream(struct pa_stream *s, const char *dev) { static struct pa_stream *internal_stream_new(struct pa_context *c) { struct pa_stream *s; - s = malloc(sizeof(struct pa_stream)); - assert(s); + s = pa_xmalloc(sizeof(struct pa_stream)); s->context = c; s->read_callback = NULL; @@ -632,7 +629,7 @@ struct pa_stream* pa_stream_new( s->create_complete_callback = complete; s->create_complete_userdata = userdata; - s->name = strdup(name); + s->name = pa_xstrdup(name); s->state = STREAM_CREATING; s->direction = dir; s->sample_spec = *ss; @@ -657,7 +654,7 @@ void pa_stream_free(struct pa_stream *s) { if (s->context->pdispatch) pa_pdispatch_unregister_reply(s->context->pdispatch, s); - free(s->name); + pa_xfree(s->name); if (s->channel_valid && s->context->state == CONTEXT_READY) { struct pa_tagstruct *t = pa_tagstruct_new(NULL, 0); @@ -680,7 +677,7 @@ void pa_stream_free(struct pa_stream *s) { else s->context->first_stream = s->next; - free(s); + pa_xfree(s); } void pa_stream_set_write_callback(struct pa_stream *s, void (*cb)(struct pa_stream *p, size_t length, void *userdata), void *userdata) { @@ -968,7 +965,7 @@ struct pa_stream* pa_context_upload_sample(struct pa_context *c, const char *nam s->create_complete_callback = cb; s->create_complete_userdata = userdata; - s->name = strdup(name); + s->name = pa_xstrdup(name); s->state = STREAM_CREATING; s->direction = PA_STREAM_UPLOAD; s->sample_spec = *ss; diff --git a/polyp/protocol-cli.c b/polyp/protocol-cli.c index d6e69b54..11aeabe0 100644 --- a/polyp/protocol-cli.c +++ b/polyp/protocol-cli.c @@ -28,6 +28,7 @@ #include "protocol-cli.h" #include "cli.h" +#include "xmalloc.h" struct pa_protocol_cli { struct pa_module *module; @@ -59,8 +60,7 @@ struct pa_protocol_cli* pa_protocol_cli_new(struct pa_core *core, struct pa_sock struct pa_protocol_cli* p; assert(core && server); - p = malloc(sizeof(struct pa_protocol_cli)); - assert(p); + p = pa_xmalloc(sizeof(struct pa_protocol_cli)); p->module = m; p->core = core; p->server = server; @@ -81,5 +81,5 @@ void pa_protocol_cli_free(struct pa_protocol_cli *p) { pa_idxset_free(p->connections, free_connection, NULL); pa_socket_server_free(p->server); - free(p); + pa_xfree(p); } diff --git a/polyp/protocol-esound.c b/polyp/protocol-esound.c index d80445de..5f6f02fa 100644 --- a/polyp/protocol-esound.c +++ b/polyp/protocol-esound.c @@ -43,6 +43,8 @@ #include "sample-util.h" #include "authkey.h" #include "debug.h" +#include "namereg.h" +#include "xmalloc.h" #define DEFAULT_COOKIE_FILE ".esd_auth" @@ -78,7 +80,6 @@ struct connection { size_t memblock_index, fragment_size; } playback; - struct pa_memchunk scache_memchunk; char *scache_name; struct pa_sample_spec scache_sample_spec; @@ -90,7 +91,7 @@ struct pa_protocol_esound { struct pa_core *core; struct pa_socket_server *server; struct pa_idxset *connections; - uint32_t sink_index, source_index; + char *sink_name, *source_name; unsigned n_player; uint8_t esd_key[ESD_KEY_LEN]; }; @@ -177,8 +178,8 @@ static void connection_free(struct connection *c) { if (c->playback.current_memblock) pa_memblock_unref(c->playback.current_memblock); - free(c->read_data); - free(c->write_data); + pa_xfree(c->read_data); + pa_xfree(c->write_data); pa_iochannel_free(c->io); @@ -187,31 +188,9 @@ static void connection_free(struct connection *c) { if (c->scache_memchunk.memblock) pa_memblock_unref(c->scache_memchunk.memblock); - free(c->scache_name); + pa_xfree(c->scache_name); - free(c); -} - -static struct pa_sink* get_output_sink(struct pa_protocol_esound *p) { - struct pa_sink *s; - assert(p); - - if (!(s = pa_idxset_get_by_index(p->core->sinks, p->sink_index))) - s = pa_sink_get_default(p->core); - - p->sink_index = s ? s->index : PA_IDXSET_INVALID; - return s; -} - -static struct pa_source* get_input_source(struct pa_protocol_esound *p) { - struct pa_source *s; - assert(p); - - if (!(s = pa_idxset_get_by_index(p->core->sources, p->sink_index))) - s = pa_source_get_default(p->core); - - p->source_index = s ? s->index : PA_IDXSET_INVALID; - return s; + pa_xfree(c); } static void* connection_write(struct connection *c, size_t length) { @@ -224,7 +203,7 @@ static void* connection_write(struct connection *c, size_t length) { t = c->write_data_length+length; if (c->write_data_alloc < t) - c->write_data = realloc(c->write_data, c->write_data_alloc = t); + c->write_data = pa_xrealloc(c->write_data, c->write_data_alloc = t); assert(c->write_data); @@ -299,7 +278,7 @@ static int esd_proto_stream_play(struct connection *c, esd_proto_t request, cons if (!pa_sample_spec_valid(&ss)) return -1; - if (!(sink = get_output_sink(c->protocol))) { + if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) { fprintf(stderr, __FILE__": No output sink\n"); return -1; } @@ -356,7 +335,7 @@ static int esd_proto_stream_record(struct connection *c, esd_proto_t request, co if (request == ESD_PROTO_STREAM_MON) { struct pa_sink* sink; - if (!(sink = get_output_sink(c->protocol))) + if (!(sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) return -1; if (!(source = sink->monitor_source)) @@ -364,7 +343,7 @@ static int esd_proto_stream_record(struct connection *c, esd_proto_t request, co } else { assert(request == ESD_PROTO_STREAM_REC); - if (!(source = get_input_source(c->protocol))) + if (!(source = pa_namereg_get(c->protocol->core, c->protocol->source_name, PA_NAMEREG_SOURCE, 1))) return -1; } @@ -402,7 +381,7 @@ static int esd_proto_get_latency(struct connection *c, esd_proto_t request, cons int latency, *lag; assert(c && !data && length == 0); - if (!(sink = get_output_sink(c->protocol))) + if (!(sink = pa_namereg(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) latency = 0; else { float usec = pa_sink_get_latency(sink); @@ -422,7 +401,7 @@ static int esd_proto_server_info(struct connection *c, esd_proto_t request, cons struct pa_sink *sink; assert(c && data && length == sizeof(int)); - if ((sink = get_output_sink(c->protocol))) { + if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) { rate = sink->sample_spec.rate; format = format_native2esd(&sink->sample_spec); } @@ -600,8 +579,7 @@ static int esd_proto_sample_cache(struct connection *c, esd_proto_t request, con c->scache_memchunk.length = sc_length; c->scache_sample_spec = ss; assert(!c->scache_name); - c->scache_name = strdup(name); - assert(c->scache_name); + c->scache_name = pa_xstrdup(name); c->state = ESD_CACHING_SAMPLE; @@ -653,7 +631,7 @@ static int esd_proto_sample_free_or_play(struct connection *c, esd_proto_t reque if (request == ESD_PROTO_SAMPLE_PLAY) { struct pa_sink *sink; - if ((sink = get_output_sink(c->protocol))) + if ((sink = pa_namereg_get(c->protocol->core, c->protocol->sink_name, PA_NAMEREG_SINK, 1))) if (pa_scache_play_item(c->protocol->core, name, sink, PA_VOLUME_NORM) >= 0) *ok = (int) index+1; } else { @@ -714,7 +692,7 @@ static int do_read(struct connection *c) { } else { if (c->read_data_alloc < handler->data_length) - c->read_data = realloc(c->read_data, c->read_data_alloc = handler->data_length); + c->read_data = pa_xrealloc(c->read_data, c->read_data_alloc = handler->data_length); assert(c->read_data); c->state = ESD_NEEDS_REQDATA; @@ -769,7 +747,7 @@ static int do_read(struct connection *c) { c->scache_memchunk.memblock = NULL; c->scache_memchunk.index = c->scache_memchunk.length = 0; - free(c->scache_name); + pa_xfree(c->scache_name); c->scache_name = NULL; c->state = ESD_NEXT_REQUEST; @@ -964,8 +942,7 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo char cname[256]; assert(s && io && userdata); - c = malloc(sizeof(struct connection)); - assert(c); + c = pa_xmalloc(sizeof(struct connection)); c->protocol = userdata; c->io = io; pa_iochannel_set_callback(c->io, io_callback, c); @@ -982,8 +959,7 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo c->swap_byte_order = 0; c->read_data_length = 0; - c->read_data = malloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length); - assert(c->read_data); + c->read_data = pa_xmalloc(c->read_data_alloc = proto_map[ESD_PROTO_CONNECT].data_length); c->write_data_length = c->write_data_index = c->write_data_alloc = 0; c->write_data = NULL; @@ -1006,7 +982,6 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo c->scache_name = NULL; c->fixed_source = c->protocol->core->mainloop->source_fixed(c->protocol->core->mainloop, fixed_callback, c); - assert(c->fixed_source); c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0); pa_idxset_put(c->protocol->connections, c, &c->index); @@ -1015,24 +990,13 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo /*** entry points ***/ struct pa_protocol_esound* pa_protocol_esound_new(struct pa_core*core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) { - uint32_t source_index, sink_index; struct pa_protocol_esound *p; assert(core && server && ma); - if (pa_modargs_get_source_index(ma, core, &source_index) < 0) { - fprintf(stderr, __FILE__": source does not exist.\n"); - return NULL; - } - - if (pa_modargs_get_sink_index(ma, core, &sink_index) < 0) { - fprintf(stderr, __FILE__": sink does not exist.\n"); - return NULL; - } - p = malloc(sizeof(struct pa_protocol_esound)); - assert(p); + p = pa_xmalloc(sizeof(struct pa_protocol_esound)); if (pa_authkey_load_auto(pa_modargs_get_value(ma, "cookie", DEFAULT_COOKIE_FILE), p->esd_key, sizeof(p->esd_key)) < 0) { - free(p); + pa_xfree(p); return NULL; } @@ -1043,8 +1007,9 @@ struct pa_protocol_esound* pa_protocol_esound_new(struct pa_core*core, struct pa p->core = core; p->connections = pa_idxset_new(NULL, NULL); assert(p->connections); - p->sink_index = sink_index; - p->source_index = source_index; + + p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL)); + p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL)); p->n_player = 0; return p; @@ -1059,5 +1024,5 @@ void pa_protocol_esound_free(struct pa_protocol_esound *p) { pa_idxset_free(p->connections, NULL, NULL); pa_socket_server_free(p->server); - free(p); + pa_xfree(p); } diff --git a/polyp/protocol-native.c b/polyp/protocol-native.c index a2332a12..c0aef180 100644 --- a/polyp/protocol-native.c +++ b/polyp/protocol-native.c @@ -41,6 +41,7 @@ #include "authkey.h" #include "namereg.h" #include "scache.h" +#include "xmalloc.h" struct connection; struct pa_protocol_native; @@ -157,13 +158,11 @@ static struct upload_stream* upload_stream_new(struct connection *c, const struc struct upload_stream *s; assert(c && ss && name && length); - s = malloc(sizeof(struct upload_stream)); - assert (s); + s = pa_xmalloc(sizeof(struct upload_stream)); s->type = UPLOAD_STREAM; s->connection = c; s->sample_spec = *ss; - s->name = strdup(name); - assert(s->name); + s->name = pa_xstrdup(name); s->memchunk.memblock = NULL; s->memchunk.index = 0; @@ -180,12 +179,12 @@ static void upload_stream_free(struct upload_stream *o) { pa_idxset_remove_by_data(o->connection->output_streams, o, NULL); - free(o->name); + pa_xfree(o->name); if (o->memchunk.memblock) pa_memblock_unref(o->memchunk.memblock); - free(o); + pa_xfree(o); } static struct record_stream* record_stream_new(struct connection *c, struct pa_source *source, const struct pa_sample_spec *ss, const char *name, size_t maxlength, size_t fragment_size) { @@ -197,8 +196,7 @@ static struct record_stream* record_stream_new(struct connection *c, struct pa_s if (!(source_output = pa_source_output_new(source, name, ss))) return NULL; - s = malloc(sizeof(struct record_stream)); - assert(s); + s = pa_xmalloc(sizeof(struct record_stream)); s->connection = c; s->source_output = source_output; s->source_output->push = source_output_push_cb; @@ -224,7 +222,7 @@ static void record_stream_free(struct record_stream* r) { pa_idxset_remove_by_data(r->connection->record_streams, r, NULL); pa_source_output_free(r->source_output); pa_memblockq_free(r->memblockq); - free(r); + pa_xfree(r); } static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name, @@ -239,8 +237,7 @@ static struct playback_stream* playback_stream_new(struct connection *c, struct if (!(sink_input = pa_sink_input_new(sink, name, ss))) return NULL; - s = malloc(sizeof(struct playback_stream)); - assert (s); + s = pa_xmalloc(sizeof(struct playback_stream)); s->type = PLAYBACK_STREAM; s->connection = c; s->sink_input = sink_input; @@ -272,7 +269,7 @@ static void playback_stream_free(struct playback_stream* p) { pa_idxset_remove_by_data(p->connection->output_streams, p, NULL); pa_sink_input_free(p->sink_input); pa_memblockq_free(p->memblockq); - free(p); + pa_xfree(p); } static void connection_free(struct connection *c) { @@ -295,7 +292,7 @@ static void connection_free(struct connection *c) { pa_pdispatch_free(c->pdispatch); pa_pstream_free(c->pstream); pa_client_free(c->client); - free(c); + pa_xfree(c); } static void request_bytes(struct playback_stream *s) { @@ -476,12 +473,10 @@ static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t com return; } - if (!*sink_name || sink_index == (uint32_t) -1) - sink = pa_sink_get_default(c->protocol->core); - else if (sink_index != (uint32_t) -1) + if (sink_index != (uint32_t) -1) sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index); else - sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK); + sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1); if (!sink) { pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY); @@ -577,12 +572,10 @@ static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t comma return; } - if (!*source_name || source_index == (uint32_t) -1) - source = pa_source_get_default(c->protocol->core); - else if (source_index != (uint32_t) -1) + if (source_index != (uint32_t) -1) source = pa_idxset_get_by_index(c->protocol->core->sources, source_index); else - source = pa_namereg_get(c->protocol->core, source_name, PA_NAMEREG_SOURCE); + source = pa_namereg_get(c->protocol->core, *source_name ? source_name : NULL, PA_NAMEREG_SOURCE, 1); if (!source) { pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY); @@ -681,12 +674,12 @@ static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t t if (command == PA_COMMAND_LOOKUP_SINK) { struct pa_sink *sink; - if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK))) + if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1))) index = sink->index; } else { struct pa_source *source; assert(command == PA_COMMAND_LOOKUP_SOURCE); - if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE))) + if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1))) index = source->index; } @@ -888,12 +881,10 @@ static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint3 return; } - if (!*sink_name && sink_index == (uint32_t) -1) - sink = pa_sink_get_default(c->protocol->core); - else if (sink_index != (uint32_t) -1) + if (sink_index != (uint32_t) -1) sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index); else - sink = pa_namereg_get(c->protocol->core, sink_name, PA_NAMEREG_SINK); + sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1); if (!sink) { pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY); @@ -1027,8 +1018,7 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo struct connection *c; assert(s && io && p); - c = malloc(sizeof(struct connection)); - assert(c); + c = pa_xmalloc(sizeof(struct connection)); c->authorized = p->public; c->protocol = p; assert(p->core); @@ -1070,11 +1060,10 @@ struct pa_protocol_native* pa_protocol_native_new(struct pa_core *core, struct p return NULL; } - p = malloc(sizeof(struct pa_protocol_native)); - assert(p); + p = pa_xmalloc(sizeof(struct pa_protocol_native)); if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) { - free(p); + pa_xfree(p); return NULL; } @@ -1098,5 +1087,5 @@ void pa_protocol_native_free(struct pa_protocol_native *p) { connection_free(c); pa_idxset_free(p->connections, NULL, NULL); pa_socket_server_free(p->server); - free(p); + pa_xfree(p); } diff --git a/polyp/protocol-simple.c b/polyp/protocol-simple.c index 037d4f9a..4b3b1513 100644 --- a/polyp/protocol-simple.c +++ b/polyp/protocol-simple.c @@ -36,6 +36,7 @@ #include "client.h" #include "sample-util.h" #include "namereg.h" +#include "xmalloc.h" struct connection { struct pa_protocol_simple *protocol; @@ -63,7 +64,7 @@ struct pa_protocol_simple { DUPLEX = 3 } mode; struct pa_sample_spec sample_spec; - uint32_t sink_index, source_index; + char *source_name, *sink_name; }; #define PLAYBACK_BUFFER_SECONDS (.5) @@ -92,7 +93,7 @@ static void connection_free(struct connection *c) { pa_memblockq_free(c->output_memblockq); if (c->fixed_source) c->protocol->core->mainloop->cancel_fixed(c->protocol->core->mainloop, c->fixed_source); - free(c); + pa_xfree(c); } static int do_read(struct connection *c) { @@ -275,8 +276,7 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo char cname[256]; assert(s && io && p); - c = malloc(sizeof(struct connection)); - assert(c); + c = pa_xmalloc(sizeof(struct connection)); c->io = io; c->sink_input = NULL; c->source_output = NULL; @@ -298,17 +298,16 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo struct pa_sink *sink; size_t l; - if (!(sink = pa_idxset_get_by_index(p->core->sinks, p->sink_index))) - if (!(sink = pa_sink_get_default(p->core))) { - fprintf(stderr, "Failed to get sink.\n"); - goto fail; - } + if (!(sink = pa_namereg_get(p->core, p->sink_name, PA_NAMEREG_SINK, 1))) { + fprintf(stderr, "Failed to get sink.\n"); + goto fail; + } - c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec); - if (!c->sink_input) { + if (!(c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec))) { fprintf(stderr, "Failed to create sink input.\n"); goto fail; } + c->sink_input->owner = p->module; c->sink_input->client = c->client; @@ -329,11 +328,10 @@ static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, vo struct pa_source *source; size_t l; - if (!(source = pa_idxset_get_by_index(p->core->sources, p->source_index))) - if (!(source = pa_source_get_default(p->core))) { - fprintf(stderr, "Failed to get source.\n"); - goto fail; - } + if (!(source = pa_namereg_get(p->core, p->source_name, PA_NAMEREG_SOURCE, 1))) { + fprintf(stderr, "Failed to get source.\n"); + goto fail; + } c->source_output = pa_source_output_new(source, c->client->name, &p->sample_spec); if (!c->source_output) { @@ -371,10 +369,7 @@ struct pa_protocol_simple* pa_protocol_simple_new(struct pa_core *core, struct p uint32_t enable; assert(core && server && ma); - p = malloc(sizeof(struct pa_protocol_simple)); - assert(p); - memset(p, 0, sizeof(struct pa_protocol_simple)); - + p = pa_xmalloc0(sizeof(struct pa_protocol_simple)); p->module = m; p->core = core; p->server = server; @@ -386,15 +381,8 @@ struct pa_protocol_simple* pa_protocol_simple_new(struct pa_core *core, struct p goto fail; } - if (pa_modargs_get_source_index(ma, core, &p->source_index) < 0) { - fprintf(stderr, __FILE__": source does not exist.\n"); - goto fail; - } - - if (pa_modargs_get_sink_index(ma, core, &p->sink_index) < 0) { - fprintf(stderr, __FILE__": sink does not exist.\n"); - goto fail; - } + p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL)); + p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL)); enable = 0; if (pa_modargs_get_value_u32(ma, "record", &enable) < 0) { @@ -439,6 +427,6 @@ void pa_protocol_simple_free(struct pa_protocol_simple *p) { if (p->server) pa_socket_server_free(p->server); - free(p); + pa_xfree(p); } diff --git a/polyp/pstream.c b/polyp/pstream.c index 7d576a16..e7441b24 100644 --- a/polyp/pstream.c +++ b/polyp/pstream.c @@ -30,6 +30,7 @@ #include "pstream.h" #include "queue.h" +#include "xmalloc.h" enum pa_pstream_descriptor_index { PA_PSTREAM_DESCRIPTOR_LENGTH, @@ -148,8 +149,7 @@ struct pa_pstream *pa_pstream_new(struct pa_mainloop_api *m, struct pa_iochannel struct pa_pstream *p; assert(io); - p = malloc(sizeof(struct pa_pstream)); - assert(p); + p = pa_xmalloc(sizeof(struct pa_pstream)); p->io = io; pa_iochannel_set_callback(io, io_callback, p); @@ -199,7 +199,7 @@ static void item_free(void *item, void *p) { pa_packet_unref(i->packet); } - free(i); + pa_xfree(i); } void pa_pstream_free(struct pa_pstream *p) { @@ -224,15 +224,14 @@ void pa_pstream_free(struct pa_pstream *p) { pa_packet_unref(p->read.packet); p->mainloop->cancel_fixed(p->mainloop, p->mainloop_source); - free(p); + pa_xfree(p); } void pa_pstream_send_packet(struct pa_pstream*p, struct pa_packet *packet) { struct item_info *i; assert(p && packet); - i = malloc(sizeof(struct item_info)); - assert(i); + i = pa_xmalloc(sizeof(struct item_info)); i->type = PA_PSTREAM_ITEM_PACKET; i->packet = pa_packet_ref(packet); @@ -244,8 +243,7 @@ void pa_pstream_send_memblock(struct pa_pstream*p, uint32_t channel, int32_t del struct item_info *i; assert(p && channel != (uint32_t) -1 && chunk); - i = malloc(sizeof(struct item_info)); - assert(i); + i = pa_xmalloc(sizeof(struct item_info)); i->type = PA_PSTREAM_ITEM_MEMBLOCK; i->chunk = *chunk; i->channel = channel; diff --git a/polyp/queue.c b/polyp/queue.c index 9befd475..9488f433 100644 --- a/polyp/queue.c +++ b/polyp/queue.c @@ -27,6 +27,7 @@ #include #include "queue.h" +#include "xmalloc.h" struct queue_entry { struct queue_entry *next; @@ -39,8 +40,7 @@ struct pa_queue { }; struct pa_queue* pa_queue_new(void) { - struct pa_queue *q = malloc(sizeof(struct pa_queue)); - assert(q); + struct pa_queue *q = pa_xmalloc(sizeof(struct pa_queue)); q->front = q->back = NULL; q->length = 0; return q; @@ -57,18 +57,17 @@ void pa_queue_free(struct pa_queue* q, void (*destroy)(void *p, void *userdata), if (destroy) destroy(e->data, userdata); - free(e); + pa_xfree(e); e = n; } - free(q); + pa_xfree(q); } void pa_queue_push(struct pa_queue *q, void *p) { struct queue_entry *e; - e = malloc(sizeof(struct queue_entry)); - + e = pa_xmalloc(sizeof(struct queue_entry)); e->data = p; e->next = NULL; @@ -96,7 +95,7 @@ void* pa_queue_pop(struct pa_queue *q) { q->back = NULL; p = e->data; - free(e); + pa_xfree(e); q->length--; diff --git a/polyp/resampler.c b/polyp/resampler.c index adf08e80..241f97c4 100644 --- a/polyp/resampler.c +++ b/polyp/resampler.c @@ -30,6 +30,7 @@ #include "resampler.h" #include "sconv.h" +#include "xmalloc.h" struct pa_resampler { struct pa_sample_spec i_ss, o_ss; @@ -55,8 +56,7 @@ struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const stru if (a->format == PA_SAMPLE_ALAW || a->format == PA_SAMPLE_ULAW || b->format == PA_SAMPLE_ALAW || b->format == PA_SAMPLE_ULAW) goto fail; - r = malloc(sizeof(struct pa_resampler)); - assert(r); + r = pa_xmalloc(sizeof(struct pa_resampler)); r->channels = a->channels; if (b->channels < r->channels) @@ -87,7 +87,7 @@ struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const stru fail: if (r) - free(r); + pa_xfree(r); return NULL; } @@ -96,9 +96,9 @@ void pa_resampler_free(struct pa_resampler *r) { assert(r); if (r->src_state) src_delete(r->src_state); - free(r->i_buf); - free(r->o_buf); - free(r); + pa_xfree(r->i_buf); + pa_xfree(r->o_buf); + pa_xfree(r); } size_t pa_resampler_request(struct pa_resampler *r, size_t out_length) { @@ -139,7 +139,7 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru assert(out->memblock); if (r->i_alloc < eff_ins) - r->i_buf = realloc(r->i_buf, sizeof(float) * (r->i_alloc = eff_ins)); + r->i_buf = pa_xrealloc(r->i_buf, sizeof(float) * (r->i_alloc = eff_ins)); assert(r->i_buf); r->to_float32_func(eff_ins, in->memblock->data+in->index, i_nchannels, r->i_buf); @@ -149,7 +149,7 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru SRC_DATA data; if (r->o_alloc < eff_ons) - r->o_buf = realloc(r->o_buf, sizeof(float) * (r->o_alloc = eff_ons)); + r->o_buf = pa_xrealloc(r->o_buf, sizeof(float) * (r->o_alloc = eff_ons)); assert(r->o_buf); data.data_in = r->i_buf; diff --git a/polyp/scache.c b/polyp/scache.c index 21af0e22..fd7b74e5 100644 --- a/polyp/scache.c +++ b/polyp/scache.c @@ -8,13 +8,14 @@ #include "mainloop.h" #include "sample-util.h" #include "play-memchunk.h" +#include "xmalloc.h" static void free_entry(struct pa_scache_entry *e) { assert(e); - free(e->name); + pa_xfree(e->name); if (e->memchunk.memblock) pa_memblock_unref(e->memchunk.memblock); - free(e); + pa_xfree(e); } void pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_spec *ss, struct pa_memchunk *chunk, uint32_t *index) { @@ -28,10 +29,8 @@ void pa_scache_add_item(struct pa_core *c, const char *name, struct pa_sample_sp pa_memblock_unref(e->memchunk.memblock); } else { put = 1; - e = malloc(sizeof(struct pa_scache_entry)); - assert(e); - e->name = strdup(name); - assert(e->name); + e = pa_xmalloc(sizeof(struct pa_scache_entry)); + e->name = pa_xstrdup(name); } e->volume = 0x100; diff --git a/polyp/sink-input.c b/polyp/sink-input.c index 25d8022f..04a2f020 100644 --- a/polyp/sink-input.c +++ b/polyp/sink-input.c @@ -30,6 +30,7 @@ #include "sink-input.h" #include "sample-util.h" +#include "xmalloc.h" #define CONVERT_BUFFER_LENGTH 4096 @@ -44,9 +45,8 @@ struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, con if (!(resampler = pa_resampler_new(spec, &s->sample_spec))) return NULL; - i = malloc(sizeof(struct pa_sink_input)); - assert(i); - i->name = name ? strdup(name) : NULL; + i = pa_xmalloc(sizeof(struct pa_sink_input)); + i->name = pa_xstrdup(name); i->client = NULL; i->owner = NULL; i->sink = s; @@ -88,8 +88,8 @@ void pa_sink_input_free(struct pa_sink_input* i) { if (i->resampler) pa_resampler_free(i->resampler); - free(i->name); - free(i); + pa_xfree(i->name); + pa_xfree(i); } void pa_sink_input_kill(struct pa_sink_input*i) { diff --git a/polyp/sink.c b/polyp/sink.c index c2c873c6..6df92e76 100644 --- a/polyp/sink.c +++ b/polyp/sink.c @@ -33,6 +33,7 @@ #include "namereg.h" #include "util.h" #include "sample-util.h" +#include "xmalloc.h" #define MAX_MIX_CHANNELS 32 @@ -43,15 +44,14 @@ struct pa_sink* pa_sink_new(struct pa_core *core, const char *name, int fail, co int r; assert(core && name && *name && spec); - s = malloc(sizeof(struct pa_sink)); - assert(s); + s = pa_xmalloc(sizeof(struct pa_sink)); if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SINK, s, fail))) { - free(s); + pa_xfree(s); return NULL; } - s->name = strdup(name); + s->name = pa_xstrdup(name); s->description = NULL; s->owner = NULL; @@ -62,7 +62,7 @@ struct pa_sink* pa_sink_new(struct pa_core *core, const char *name, int fail, co n = pa_sprintf_malloc("%s_monitor", name); s->monitor_source = pa_source_new(core, n, 0, spec); assert(s->monitor_source); - free(n); + pa_xfree(n); s->monitor_source->monitor_of = s; s->volume = PA_VOLUME_NORM; @@ -98,9 +98,9 @@ void pa_sink_free(struct pa_sink *s) { fprintf(stderr, "sink: freed %u \"%s\"\n", s->index, s->name); - free(s->name); - free(s->description); - free(s); + pa_xfree(s->name); + pa_xfree(s->description); + pa_xfree(s); } void pa_sink_notify(struct pa_sink*s) { @@ -270,19 +270,6 @@ uint32_t pa_sink_get_latency(struct pa_sink *s) { return s->get_latency(s); } -struct pa_sink* pa_sink_get_default(struct pa_core *c) { - struct pa_sink *sink; - assert(c); - - if ((sink = pa_idxset_get_by_index(c->sinks, c->default_sink_index))) - return sink; - - if (!(sink = pa_idxset_first(c->sinks, &c->default_sink_index))) - return NULL; - - fprintf(stderr, "core: default sink vanished, setting to %u.\n", sink->index); - return sink; -} void pa_sink_set_owner(struct pa_sink *sink, struct pa_module *m) { sink->owner = m; diff --git a/polyp/sink.h b/polyp/sink.h index 2b5d9495..400d5d04 100644 --- a/polyp/sink.h +++ b/polyp/sink.h @@ -60,8 +60,6 @@ uint32_t pa_sink_get_latency(struct pa_sink *s); void pa_sink_notify(struct pa_sink*s); -struct pa_sink* pa_sink_get_default(struct pa_core *c); - void pa_sink_set_owner(struct pa_sink *sink, struct pa_module *m); #endif diff --git a/polyp/socket-client.c b/polyp/socket-client.c index a2187e6a..c0c355de 100644 --- a/polyp/socket-client.c +++ b/polyp/socket-client.c @@ -36,6 +36,7 @@ #include "socket-client.h" #include "socket-util.h" #include "util.h" +#include "xmalloc.h" struct pa_socket_client { struct pa_mainloop_api *mainloop; @@ -50,8 +51,7 @@ static struct pa_socket_client*pa_socket_client_new(struct pa_mainloop_api *m) { struct pa_socket_client *c; assert(m); - c = malloc(sizeof(struct pa_socket_client)); - assert(c); + c = pa_xmalloc(sizeof(struct pa_socket_client)); c->mainloop = m; c->fd = -1; c->io_source = c->fixed_source = NULL; @@ -226,7 +226,7 @@ void pa_socket_client_free(struct pa_socket_client *c) { c->mainloop->cancel_fixed(c->mainloop, c->fixed_source); if (c->fd >= 0) close(c->fd); - free(c); + pa_xfree(c); } void pa_socket_client_set_callback(struct pa_socket_client *c, void (*on_connection)(struct pa_socket_client *c, struct pa_iochannel*io, void *userdata), void *userdata) { diff --git a/polyp/socket-server.c b/polyp/socket-server.c index 0f497377..5f332f0c 100644 --- a/polyp/socket-server.c +++ b/polyp/socket-server.c @@ -37,6 +37,7 @@ #include "socket-server.h" #include "socket-util.h" +#include "xmalloc.h" struct pa_socket_server { int fd; @@ -81,8 +82,7 @@ struct pa_socket_server* pa_socket_server_new(struct pa_mainloop_api *m, int fd) struct pa_socket_server *s; assert(m && fd >= 0); - s = malloc(sizeof(struct pa_socket_server)); - assert(s); + s = pa_xmalloc(sizeof(struct pa_socket_server)); s->fd = fd; s->filename = NULL; s->on_connection = NULL; @@ -128,9 +128,7 @@ struct pa_socket_server* pa_socket_server_new_unix(struct pa_mainloop_api *m, co s = pa_socket_server_new(m, fd); assert(s); - s->filename = strdup(filename); - assert(s->filename); - + s->filename = pa_xstrdup(filename); s->type = SOCKET_SERVER_UNIX; return s; @@ -192,13 +190,11 @@ void pa_socket_server_free(struct pa_socket_server*s) { if (s->filename) { unlink(s->filename); - free(s->filename); + pa_xfree(s->filename); } - s->mainloop->cancel_io(s->mainloop, s->mainloop_source); - - free(s); + pa_xfree(s); } void pa_socket_server_set_callback(struct pa_socket_server*s, void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata), void *userdata) { diff --git a/polyp/socket-util.c b/polyp/socket-util.c index e0a3c28d..904381b7 100644 --- a/polyp/socket-util.c +++ b/polyp/socket-util.c @@ -40,6 +40,7 @@ #include "socket-util.h" #include "util.h" +#include "xmalloc.h" void pa_socket_peer_to_string(int fd, char *c, size_t l) { struct stat st; @@ -179,8 +180,7 @@ int pa_unix_socket_remove_stale(const char *fn) { int pa_unix_socket_make_secure_dir(const char *fn) { int ret = -1; - char *slash, *dir = strdup(fn); - assert(dir); + char *slash, *dir = pa_xstrdup(fn); if (!(slash = strrchr(dir, '/'))) goto finish; @@ -192,14 +192,13 @@ int pa_unix_socket_make_secure_dir(const char *fn) { ret = 0; finish: - free(dir); + pa_xfree(dir); return ret; } int pa_unix_socket_remove_secure_dir(const char *fn) { int ret = -1; - char *slash, *dir = strdup(fn); - assert(dir); + char *slash, *dir = pa_xstrdup(fn); if (!(slash = strrchr(dir, '/'))) goto finish; @@ -211,6 +210,6 @@ int pa_unix_socket_remove_secure_dir(const char *fn) { ret = 0; finish: - free(dir); + pa_xfree(dir); return ret; } diff --git a/polyp/source-output.c b/polyp/source-output.c index 2705fdb3..07901fa8 100644 --- a/polyp/source-output.c +++ b/polyp/source-output.c @@ -28,6 +28,7 @@ #include #include "source-output.h" +#include "xmalloc.h" struct pa_source_output* pa_source_output_new(struct pa_source *s, const char *name, const struct pa_sample_spec *spec) { struct pa_source_output *o; @@ -39,9 +40,8 @@ struct pa_source_output* pa_source_output_new(struct pa_source *s, const char *n if (!(resampler = pa_resampler_new(&s->sample_spec, spec))) return NULL; - o = malloc(sizeof(struct pa_source_output)); - assert(o); - o->name = name ? strdup(name) : NULL; + o = pa_xmalloc(sizeof(struct pa_source_output)); + o->name = pa_xstrdup(name); o->client = NULL; o->owner = NULL; o->source = s; @@ -71,8 +71,8 @@ void pa_source_output_free(struct pa_source_output* o) { if (o->resampler) pa_resampler_free(o->resampler); - free(o->name); - free(o); + pa_xfree(o->name); + pa_xfree(o); } void pa_source_output_kill(struct pa_source_output*i) { diff --git a/polyp/source.c b/polyp/source.c index 13635414..65c90e9a 100644 --- a/polyp/source.c +++ b/polyp/source.c @@ -31,6 +31,7 @@ #include "source.h" #include "source-output.h" #include "namereg.h" +#include "xmalloc.h" struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail, const struct pa_sample_spec *spec) { struct pa_source *s; @@ -38,15 +39,14 @@ struct pa_source* pa_source_new(struct pa_core *core, const char *name, int fail int r; assert(core && spec && name && *name); - s = malloc(sizeof(struct pa_source)); - assert(s); + s = pa_xmalloc(sizeof(struct pa_source)); if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) { - free(s); + pa_xfree(s); return NULL; } - s->name = strdup(name); + s->name = pa_xstrdup(name); s->description = NULL; s->owner = NULL; @@ -84,9 +84,9 @@ void pa_source_free(struct pa_source *s) { fprintf(stderr, "source: freed %u \"%s\"\n", s->index, s->name); - free(s->name); - free(s->description); - free(s); + pa_xfree(s->name); + pa_xfree(s->description); + pa_xfree(s); } void pa_source_notify(struct pa_source*s) { @@ -111,20 +111,6 @@ void pa_source_post(struct pa_source*s, struct pa_memchunk *chunk) { pa_idxset_foreach(s->outputs, do_post, chunk); } -struct pa_source* pa_source_get_default(struct pa_core *c) { - struct pa_source *source; - assert(c); - - if ((source = pa_idxset_get_by_index(c->sources, c->default_source_index))) - return source; - - if (!(source = pa_idxset_first(c->sources, &c->default_source_index))) - return NULL; - - fprintf(stderr, "core: default source vanished, setting to %u.\n", source->index); - return source; -} - void pa_source_set_owner(struct pa_source *s, struct pa_module *m) { assert(s); s->owner = m; diff --git a/polyp/source.h b/polyp/source.h index 9c584a6d..32ef14e6 100644 --- a/polyp/source.h +++ b/polyp/source.h @@ -54,8 +54,6 @@ void pa_source_post(struct pa_source*s, struct pa_memchunk *b); void pa_source_notify(struct pa_source *s); -struct pa_source* pa_source_get_default(struct pa_core *c); - void pa_source_set_owner(struct pa_source *s, struct pa_module *m); #endif diff --git a/polyp/strbuf.c b/polyp/strbuf.c index c6a3772d..169604e8 100644 --- a/polyp/strbuf.c +++ b/polyp/strbuf.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "strbuf.h" @@ -44,8 +45,7 @@ struct pa_strbuf { }; struct pa_strbuf *pa_strbuf_new(void) { - struct pa_strbuf *sb = malloc(sizeof(struct pa_strbuf)); - assert(sb); + struct pa_strbuf *sb = pa_xmalloc(sizeof(struct pa_strbuf)); sb->length = 0; sb->head = sb->tail = NULL; return sb; @@ -56,10 +56,10 @@ void pa_strbuf_free(struct pa_strbuf *sb) { while (sb->head) { struct chunk *c = sb->head; sb->head = sb->head->next; - free(c); + pa_xfree(c); } - free(sb); + pa_xfree(sb); } char *pa_strbuf_tostring(struct pa_strbuf *sb) { @@ -67,8 +67,7 @@ char *pa_strbuf_tostring(struct pa_strbuf *sb) { struct chunk *c; assert(sb); - t = malloc(sb->length+1); - assert(t); + t = pa_xmalloc(sb->length+1); e = t; for (c = sb->head; c; c = c->next) { @@ -101,8 +100,7 @@ void pa_strbuf_putsn(struct pa_strbuf *sb, const char *t, size_t l) { if (!l) return; - c = malloc(sizeof(struct chunk)+l); - assert(c); + c = pa_xmalloc(sizeof(struct chunk)+l); c->next = NULL; c->length = l; @@ -131,8 +129,7 @@ int pa_strbuf_printf(struct pa_strbuf *sb, const char *format, ...) { for(;;) { va_list ap; - c = realloc(c, sizeof(struct chunk)+size); - assert(c); + c = pa_xrealloc(c, sizeof(struct chunk)+size); va_start(ap, format); r = vsnprintf(c->text, size, format, ap); diff --git a/polyp/tagstruct.c b/polyp/tagstruct.c index 0d93c1e9..cb93a9c4 100644 --- a/polyp/tagstruct.c +++ b/polyp/tagstruct.c @@ -29,6 +29,7 @@ #include #include "tagstruct.h" +#include "xmalloc.h" enum tags { TAG_STRING = 't', @@ -55,8 +56,7 @@ struct pa_tagstruct *pa_tagstruct_new(const uint8_t* data, size_t length) { assert(!data || (data && length)); - t = malloc(sizeof(struct pa_tagstruct)); - assert(t); + t = pa_xmalloc(sizeof(struct pa_tagstruct)); t->data = (uint8_t*) data; t->allocated = t->length = data ? length : 0; t->rindex = 0; @@ -67,8 +67,8 @@ struct pa_tagstruct *pa_tagstruct_new(const uint8_t* data, size_t length) { void pa_tagstruct_free(struct pa_tagstruct*t) { assert(t); if (t->dynamic) - free(t->data); - free(t); + pa_xfree(t->data); + pa_xfree(t); } uint8_t* pa_tagstruct_free_data(struct pa_tagstruct*t, size_t *l) { @@ -76,7 +76,7 @@ uint8_t* pa_tagstruct_free_data(struct pa_tagstruct*t, size_t *l) { assert(t && t->dynamic && l); p = t->data; *l = t->length; - free(t); + pa_xfree(t); return p; } @@ -86,8 +86,7 @@ static void extend(struct pa_tagstruct*t, size_t l) { if (l <= t->allocated) return; - t->data = realloc(t->data, t->allocated = l+100); - assert(t->data); + t->data = pa_xrealloc(t->data, t->allocated = l+100); } void pa_tagstruct_puts(struct pa_tagstruct*t, const char *s) { diff --git a/polyp/tokenizer.c b/polyp/tokenizer.c index c7f18d26..43d695bd 100644 --- a/polyp/tokenizer.c +++ b/polyp/tokenizer.c @@ -29,13 +29,14 @@ #include "tokenizer.h" #include "dynarray.h" +#include "xmalloc.h" struct pa_tokenizer { struct pa_dynarray *dynarray; }; static void token_free(void *p, void *userdata) { - free(p); + pa_xfree(p); } static void parse(struct pa_dynarray*a, const char *s, unsigned args) { @@ -50,8 +51,7 @@ static void parse(struct pa_dynarray*a, const char *s, unsigned args) { p = s+strspn(s, delimiter); while (*p && (infty || args >= 2)) { size_t l = strcspn(p, delimiter); - char *n = strndup(p, l); - assert(n); + char *n = pa_xstrndup(p, l); pa_dynarray_append(a, n); p += l; p += strspn(p, delimiter); @@ -59,8 +59,7 @@ static void parse(struct pa_dynarray*a, const char *s, unsigned args) { } if (args && *p) { - char *n = strdup(p); - assert(n); + char *n = pa_xstrdup(p); pa_dynarray_append(a, n); } } @@ -68,8 +67,7 @@ static void parse(struct pa_dynarray*a, const char *s, unsigned args) { struct pa_tokenizer* pa_tokenizer_new(const char *s, unsigned args) { struct pa_tokenizer *t; - t = malloc(sizeof(struct pa_tokenizer)); - assert(t); + t = pa_xmalloc(sizeof(struct pa_tokenizer)); t->dynarray = pa_dynarray_new(); assert(t->dynarray); @@ -80,7 +78,7 @@ struct pa_tokenizer* pa_tokenizer_new(const char *s, unsigned args) { void pa_tokenizer_free(struct pa_tokenizer *t) { assert(t); pa_dynarray_free(t->dynarray, token_free, NULL); - free(t); + pa_xfree(t); } const char *pa_tokenizer_get(struct pa_tokenizer *t, unsigned i) { diff --git a/polyp/util.c b/polyp/util.c index 6e75c240..98d91075 100644 --- a/polyp/util.c +++ b/polyp/util.c @@ -35,6 +35,7 @@ #include #include "util.h" +#include "xmalloc.h" void pa_make_nonblock_fd(int fd) { int v; @@ -129,8 +130,7 @@ char *pa_sprintf_malloc(const char *format, ...) { int r; va_list ap; - c = realloc(c, size); - assert(c); + c = pa_xrealloc(c, size); va_start(ap, format); r = vsnprintf(c, size, format, ap); -- cgit