From 74665de68e4ca432debe181c6cdfad4b482eacdc Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 31 Mar 2020 17:52:22 +0200 Subject: [PATCH] alsa-seq: keep track of last port Keep track of the last port and only iterate until that port. --- spa/plugins/alsa/alsa-seq-source.c | 17 +++++++++++++---- spa/plugins/alsa/alsa-seq.c | 10 +++++----- spa/plugins/alsa/alsa-seq.h | 1 + 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/spa/plugins/alsa/alsa-seq-source.c b/spa/plugins/alsa/alsa-seq-source.c index 9d09f09ef..26ebccd08 100644 --- a/spa/plugins/alsa/alsa-seq-source.c +++ b/spa/plugins/alsa/alsa-seq-source.c @@ -350,7 +350,7 @@ static struct seq_port *find_port(struct seq_state *state, struct seq_stream *stream, const snd_seq_addr_t *addr) { uint32_t i; - for (i = 0; i < MAX_PORTS; i++) { + for (i = 0; i < stream->last_port; i++) { struct seq_port *port = &stream->ports[i]; if (port->valid && port->addr.client == addr->client && @@ -369,14 +369,23 @@ static struct seq_port *alloc_port(struct seq_state *state, struct seq_stream *s port->id = i; port->direction = stream->direction; port->valid = true; + if (stream->last_port < i + 1) + stream->last_port = i + 1; return port; } } return NULL; } -static void free_port(struct seq_state *state, struct seq_port *port) +static void free_port(struct seq_state *state, struct seq_stream *stream, struct seq_port *port) { + if (port->id + 1 == stream->last_port) { + int i; + for (i = stream->last_port - 1; i >= 0; i--) + if (!stream->ports[i].valid) + break; + stream->last_port = i + 1; + } spa_node_emit_port_info(&state->hooks, port->direction, port->id, NULL); spa_zero(*port); @@ -417,7 +426,7 @@ static void update_stream_port(struct seq_state *state, struct seq_stream *strea if (info == NULL) { spa_log_debug(state->log, "free port %d.%d", addr->client, addr->port); if (port) - free_port(state, port); + free_port(state, stream, port); } else { if (port == NULL && (caps & stream->caps) == stream->caps) { spa_log_debug(state->log, "new port %d.%d", addr->client, addr->port); @@ -428,7 +437,7 @@ static void update_stream_port(struct seq_state *state, struct seq_stream *strea } else if (port != NULL) { if ((caps & stream->caps) != stream->caps) { spa_log_debug(state->log, "free port %d.%d", addr->client, addr->port); - free_port(state, port); + free_port(state, stream, port); } else { spa_log_debug(state->log, "update port %d.%d", addr->client, addr->port); diff --git a/spa/plugins/alsa/alsa-seq.c b/spa/plugins/alsa/alsa-seq.c index 58639a4f5..6a1621ea6 100644 --- a/spa/plugins/alsa/alsa-seq.c +++ b/spa/plugins/alsa/alsa-seq.c @@ -366,7 +366,7 @@ static struct seq_port *find_port(struct seq_state *state, struct seq_stream *stream, const snd_seq_addr_t *addr) { uint32_t i; - for (i = 0; i < MAX_PORTS; i++) { + for (i = 0; i < stream->last_port; i++) { struct seq_port *port = &stream->ports[i]; if (port->valid && port->addr.client == addr->client && @@ -460,7 +460,7 @@ static int process_recycle(struct seq_state *state) struct seq_stream *stream = &state->streams[SPA_DIRECTION_OUTPUT]; uint32_t i; - for (i = 0; i < MAX_PORTS; i++) { + for (i = 0; i < stream->last_port; i++) { struct seq_port *port = &stream->ports[i]; struct spa_io_buffers *io = port->io; @@ -547,7 +547,7 @@ static int process_read(struct seq_state *state) /* prepare a buffer on each port, some ports might have their * buffer filled above */ res = 0; - for (i = 0; i < MAX_PORTS; i++) { + for (i = 0; i < stream->last_port; i++) { struct seq_port *port = &stream->ports[i]; struct spa_io_buffers *io = port->io; @@ -599,7 +599,7 @@ static int process_write(struct seq_state *state) uint32_t i; int res = 0; - for (i = 0; i < MAX_PORTS; i++) { + for (i = 0; i < stream->last_port; i++) { struct seq_port *port = &stream->ports[i]; struct spa_io_buffers *io = port->io; struct buffer *buffer; @@ -819,7 +819,7 @@ static void reset_buffers(struct seq_state *this, struct seq_port *port) static void reset_stream(struct seq_state *this, struct seq_stream *stream, bool active) { uint32_t i; - for (i = 0; i < MAX_PORTS; i++) { + for (i = 0; i < stream->last_port; i++) { struct seq_port *port = &stream->ports[i]; if (port->valid) { reset_buffers(this, port); diff --git a/spa/plugins/alsa/alsa-seq.h b/spa/plugins/alsa/alsa-seq.h index aca7daaca..32e9b4697 100644 --- a/spa/plugins/alsa/alsa-seq.h +++ b/spa/plugins/alsa/alsa-seq.h @@ -94,6 +94,7 @@ struct seq_stream { unsigned int caps; snd_midi_event_t *codec; struct seq_port ports[MAX_PORTS]; + uint32_t last_port; }; struct seq_conn {