From 90dfa437e024a0967557e31ccc4ff452a15cf691 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 11 Jul 2024 21:41:33 +0200 Subject: [PATCH] lib/alsa/NonBlock: use a persistent pollfd array This implements the semantic API change introduced by commit https://github.com/alsa-project/alsa-lib/commit/cd04da2bcfe147b08b8db2930b2294aead0a8968 --- NEWS | 1 + src/lib/alsa/NonBlock.cxx | 34 ++++++++++++++-------------------- src/lib/alsa/NonBlock.hxx | 10 +++++----- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/NEWS b/NEWS index 433328caf..8eb412698 100644 --- a/NEWS +++ b/NEWS @@ -31,6 +31,7 @@ ver 0.24 (not yet released) * input - alsa: limit ALSA buffer time to 2 seconds - alsa: set up a channel map + - alsa: support the alsa-lib 1.2.11 API - curl: add "connect_timeout" configuration * decoder - ffmpeg: require FFmpeg 4.0 or later diff --git a/src/lib/alsa/NonBlock.cxx b/src/lib/alsa/NonBlock.cxx index a20255bcc..3e7c5f86c 100644 --- a/src/lib/alsa/NonBlock.cxx +++ b/src/lib/alsa/NonBlock.cxx @@ -8,21 +8,23 @@ namespace Alsa { std::span -NonBlock::CopyReturnedEvents(MultiSocketMonitor &m, std::size_t n) noexcept +NonBlock::CopyReturnedEvents(MultiSocketMonitor &m) noexcept { - const auto pfds = buffer.Get(n), end = pfds + n; + const std::span pfds = buffer; - auto *i = pfds; - m.ForEachReturnedEvent([&i, end](SocketDescriptor s, unsigned events){ - if (i >= end) - return; + for (auto &i : pfds) + i.revents = 0; - i->fd = s.Get(); - i->events = i->revents = events; - ++i; + m.ForEachReturnedEvent([pfds](SocketDescriptor s, unsigned events){ + for (auto &i : pfds) { + if (i.fd == s.Get()) { + i.revents = events; + return; + } + } }); - return {pfds, static_cast(i - pfds)}; + return pfds; } @@ -54,11 +56,7 @@ NonBlockPcm::PrepareSockets(MultiSocketMonitor &m, snd_pcm_t *pcm) void NonBlockPcm::DispatchSockets(MultiSocketMonitor &m, snd_pcm_t *pcm) { - int count = snd_pcm_poll_descriptors_count(pcm); - if (count <= 0) - return; - - const auto pfds = base.CopyReturnedEvents(m, count); + const auto pfds = base.CopyReturnedEvents(m); unsigned short dummy; int err = snd_pcm_poll_descriptors_revents(pcm, pfds.data(), pfds.size(), &dummy); @@ -88,11 +86,7 @@ NonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcep void NonBlockMixer::DispatchSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcept { - int count = snd_mixer_poll_descriptors_count(mixer); - if (count <= 0) - return; - - const auto pfds = base.CopyReturnedEvents(m, count); + const auto pfds = base.CopyReturnedEvents(m); unsigned short dummy; snd_mixer_poll_descriptors_revents(mixer, pfds.data(), pfds.size(), &dummy); diff --git a/src/lib/alsa/NonBlock.hxx b/src/lib/alsa/NonBlock.hxx index 16cfb3102..0b523f166 100644 --- a/src/lib/alsa/NonBlock.hxx +++ b/src/lib/alsa/NonBlock.hxx @@ -4,7 +4,7 @@ #pragma once #include "event/Chrono.hxx" -#include "util/ReusableArray.hxx" +#include "util/AllocatedArray.hxx" #include @@ -15,15 +15,15 @@ class MultiSocketMonitor; namespace Alsa { class NonBlock { - ReusableArray buffer; + AllocatedArray buffer; public: std::span Allocate(std::size_t n) noexcept { - return {buffer.Get(n), n}; + buffer.ResizeDiscard(n); + return buffer; } - std::span CopyReturnedEvents(MultiSocketMonitor &m, - std::size_t n) noexcept; + std::span CopyReturnedEvents(MultiSocketMonitor &m) noexcept; }; /**