Only call ftruncate() when size > 0

Either Boost or Linux really doesn't like it if you ftruncate() shared
memory down to 0 bytes.
This commit is contained in:
Robbert van der Helm
2021-07-17 22:00:52 +02:00
parent f43e9c2153
commit 5f5a7bbdd2
2 changed files with 18 additions and 10 deletions
+14 -8
View File
@@ -21,10 +21,14 @@ AudioShmBuffer::AudioShmBuffer(const Config& config)
shm(boost::interprocess::open_or_create,
config.name.c_str(),
boost::interprocess::read_write) {
shm.truncate(config.size);
buffer =
boost::interprocess::mapped_region(shm, boost::interprocess::read_write,
0, config.size, nullptr, MAP_LOCKED);
// 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);
}
}
AudioShmBuffer::~AudioShmBuffer() noexcept {
@@ -44,10 +48,12 @@ void AudioShmBuffer::resize(const Config& new_config) {
}
config = new_config;
shm.truncate(config.size);
buffer =
boost::interprocess::mapped_region(shm, boost::interprocess::read_write,
0, config.size, nullptr, MAP_LOCKED);
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