mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-06-19 18:03:56 +02:00
Move memory mapping setup to a function
So we don't need to repeat this twice.
This commit is contained in:
+23
-24
@@ -21,14 +21,7 @@ AudioShmBuffer::AudioShmBuffer(const Config& config)
|
|||||||
shm(boost::interprocess::open_or_create,
|
shm(boost::interprocess::open_or_create,
|
||||||
config.name.c_str(),
|
config.name.c_str(),
|
||||||
boost::interprocess::read_write) {
|
boost::interprocess::read_write) {
|
||||||
// Apparently you get a `Resource temporarily unavailable` when calling
|
setup_mapping();
|
||||||
// `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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioShmBuffer::~AudioShmBuffer() noexcept {
|
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
|
AudioShmBuffer::AudioShmBuffer(AudioShmBuffer&& o) noexcept
|
||||||
: config(std::move(o.config)),
|
: config(std::move(o.config)),
|
||||||
shm(std::move(o.shm)),
|
shm(std::move(o.shm)),
|
||||||
@@ -71,3 +48,25 @@ AudioShmBuffer& AudioShmBuffer::operator=(AudioShmBuffer&& o) noexcept {
|
|||||||
|
|
||||||
return *this;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -181,6 +181,11 @@ class AudioShmBuffer {
|
|||||||
Config config;
|
Config config;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* Resize the shared memory object, and set up the memory mapping.
|
||||||
|
*/
|
||||||
|
void setup_mapping();
|
||||||
|
|
||||||
boost::interprocess::shared_memory_object shm;
|
boost::interprocess::shared_memory_object shm;
|
||||||
boost::interprocess::mapped_region buffer;
|
boost::interprocess::mapped_region buffer;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user