Move memory mapping setup to a function

So we don't need to repeat this twice.
This commit is contained in:
Robbert van der Helm
2021-07-18 22:40:04 +02:00
parent a02dcbdb5a
commit 3c14f0391b
2 changed files with 28 additions and 24 deletions
+23 -24
View File
@@ -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);
}
}
+5
View File
@@ -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;