SharedMemory: Make key optional, default to shorter UID on macOS (#7681)

* SharedMemory: Make key optional, shorter on macOS
* Add getters for shared memory size
* macOS: Fix linking regression for RemoteZynAddSubFx introduced with #7252 

---------

Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>
This commit is contained in:
Tres Finocchiaro
2025-02-03 14:18:02 -05:00
committed by GitHub
parent 36c1deae42
commit f38c649923
6 changed files with 73 additions and 12 deletions

View File

@@ -71,7 +71,6 @@
#include <QProcess>
#include <QThread>
#include <QString>
#include <QUuid>
#ifndef SYNC_WITH_SHM_FIFO
#include <poll.h>
@@ -125,7 +124,7 @@ public:
m_master( true ),
m_lockDepth( 0 )
{
m_data.create(QUuid::createUuid().toString().toStdString());
m_data.create();
m_data->startPtr = m_data->endPtr = 0;
static int k = 0;
m_data->dataSem.semKey = ( getpid()<<10 ) + ++k;

View File

@@ -44,6 +44,7 @@ public:
SharedMemoryData() noexcept;
SharedMemoryData(std::string&& key, bool readOnly);
SharedMemoryData(std::string&& key, std::size_t size, bool readOnly);
SharedMemoryData(std::size_t size, bool readOnly);
~SharedMemoryData();
SharedMemoryData(SharedMemoryData&& other) noexcept;
@@ -64,6 +65,7 @@ public:
const std::string& key() const noexcept { return m_key; }
void* get() const noexcept { return m_ptr; }
std::size_t size_bytes() const noexcept;
private:
std::string m_key;
@@ -95,6 +97,11 @@ public:
m_data = detail::SharedMemoryData{std::move(key), sizeof(T), std::is_const_v<T>};
}
void create()
{
m_data = detail::SharedMemoryData{sizeof(T), std::is_const_v<T>};
}
void detach() noexcept
{
m_data = detail::SharedMemoryData{};
@@ -103,6 +110,9 @@ public:
const std::string& key() const noexcept { return m_data.key(); }
T* get() const noexcept { return static_cast<T*>(m_data.get()); }
std::size_t size() const noexcept { return get() ? 1 : 0; }
std::size_t size_bytes() const noexcept { return get() ? sizeof(T) : 0; }
T* operator->() const noexcept { return get(); }
T& operator*() const noexcept { return *get(); }
explicit operator bool() const noexcept { return get() != nullptr; }
@@ -132,6 +142,11 @@ public:
m_data = detail::SharedMemoryData{std::move(key), size * sizeof(T), std::is_const_v<T>};
}
void create(std::size_t size)
{
m_data = detail::SharedMemoryData{size * sizeof(T), std::is_const_v<T>};
}
void detach() noexcept
{
m_data = detail::SharedMemoryData{};
@@ -140,6 +155,9 @@ public:
const std::string& key() const noexcept { return m_data.key(); }
T* get() const noexcept { return static_cast<T*>(m_data.get()); }
std::size_t size() const noexcept { return m_data.size_bytes() / sizeof(T); }
std::size_t size_bytes() const noexcept { return m_data.size_bytes(); }
T& operator[](std::size_t index) const noexcept { return get()[index]; }
explicit operator bool() const noexcept { return get() != nullptr; }