From 982492712faf8c64a108059ebba74115fc1bc61c Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 25 Aug 2022 02:31:57 +0100 Subject: [PATCH] Alternative approach for systems without MAP_LOCKED (build&runtime) --- source/utils/CarlaShmUtils.hpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/source/utils/CarlaShmUtils.hpp b/source/utils/CarlaShmUtils.hpp index 55792112b..f065706ad 100644 --- a/source/utils/CarlaShmUtils.hpp +++ b/source/utils/CarlaShmUtils.hpp @@ -29,9 +29,6 @@ struct carla_shm_t { HANDLE map; bool isServer; const char* filename; }; # endif # include # include -# ifndef MAP_LOCKED -# define MAP_LOCKED 0x0 -# endif struct carla_shm_t { int fd; const char* filename; std::size_t size; }; # define carla_shm_t_INIT { -1, nullptr, 0 } #endif @@ -219,13 +216,27 @@ void* carla_shm_map(carla_shm_t& shm, const std::size_t size) noexcept CARLA_SAFE_ASSERT_RETURN(ret == 0, nullptr); } - void* const ptr(::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, shm.fd, 0)); - CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, nullptr); + void* ptr; +# ifdef MAP_LOCKED + ptr = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, shm.fd, 0); if (ptr == MAP_FAILED) +# endif { - carla_stderr2("carla_shm_map() - mmap failed: %s", std::strerror(errno)); - return nullptr; + ptr = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_SHARED, shm.fd, 0); + CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, nullptr); + + if (ptr == MAP_FAILED) + { + carla_stderr2("carla_shm_map() - mmap failed: %s", std::strerror(errno)); + return nullptr; + } + +# ifndef MAP_LOCKED + try { + ::mlock(ptr, size); + } CARLA_SAFE_EXCEPTION("carla_shm_map mlock"); +# endif } shm.size = size; @@ -369,8 +380,4 @@ bool carla_shm_map(carla_shm_t& shm, T*& value) noexcept // ----------------------------------------------------------------------- -#ifndef CARLA_OS_LINUX -# undef MAP_LOCKED -#endif - #endif // CARLA_SHM_UTILS_HPP_INCLUDED