diff --git a/src/common/audio-shm.cpp b/src/common/audio-shm.cpp index bc4acd40..7cbf8a9b 100644 --- a/src/common/audio-shm.cpp +++ b/src/common/audio-shm.cpp @@ -21,14 +21,7 @@ AudioShmBuffer::AudioShmBuffer(const Config& config) shm(boost::interprocess::open_or_create, config.name.c_str(), boost::interprocess::read_write) { - // Apparently you get a `Resource temporarily unavailable` when calling - // `ftruncate()` with a size of 0 on shared memory - if (config.size > 0) { - shm.truncate(config.size); - buffer = boost::interprocess::mapped_region( - shm, boost::interprocess::read_write, 0, config.size, nullptr, - MAP_LOCKED); - } + setup_mapping(); } AudioShmBuffer::~AudioShmBuffer() noexcept { @@ -40,22 +33,6 @@ AudioShmBuffer::~AudioShmBuffer() noexcept { } } -void AudioShmBuffer::resize(const Config& new_config) { - if (new_config.name != config.name) { - throw std::invalid_argument("Expected buffer configuration for \"" + - config.name + "\", got \"" + - new_config.name + "\""); - } - - config = new_config; - if (config.size > 0) { - shm.truncate(config.size); - buffer = boost::interprocess::mapped_region( - shm, boost::interprocess::read_write, 0, config.size, nullptr, - MAP_LOCKED); - } -} - AudioShmBuffer::AudioShmBuffer(AudioShmBuffer&& o) noexcept : config(std::move(o.config)), shm(std::move(o.shm)), @@ -71,3 +48,25 @@ AudioShmBuffer& AudioShmBuffer::operator=(AudioShmBuffer&& o) noexcept { return *this; } + +void AudioShmBuffer::resize(const Config& new_config) { + if (new_config.name != config.name) { + throw std::invalid_argument("Expected buffer configuration for \"" + + config.name + "\", got \"" + + new_config.name + "\""); + } + + config = new_config; + setup_mapping(); +} + +void AudioShmBuffer::setup_mapping() { + // Apparently you get a `Resource temporarily unavailable` when calling + // `ftruncate()` with a size of 0 on shared memory + if (config.size > 0) { + shm.truncate(config.size); + buffer = boost::interprocess::mapped_region( + shm, boost::interprocess::read_write, 0, config.size, nullptr, + MAP_LOCKED); + } +} diff --git a/src/common/audio-shm.h b/src/common/audio-shm.h index 73bb7c71..7a6ec8c8 100644 --- a/src/common/audio-shm.h +++ b/src/common/audio-shm.h @@ -181,6 +181,11 @@ class AudioShmBuffer { Config config; private: + /** + * Resize the shared memory object, and set up the memory mapping. + */ + void setup_mapping(); + boost::interprocess::shared_memory_object shm; boost::interprocess::mapped_region buffer;