Wayland example window
This commit is contained in:
parent
160b746182
commit
26f54c4581
30 changed files with 825 additions and 22 deletions
68
src/core/memory/SharedMemory.cpp
Normal file
68
src/core/memory/SharedMemory.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "SharedMemory.h"
|
||||
|
||||
#include "RandomUtils.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
void SharedMemory::allocate(const std::string& namePrefix, std::size_t size)
|
||||
{
|
||||
createFile(namePrefix);
|
||||
|
||||
if (!mIsValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int ret{-1};
|
||||
do {
|
||||
ret = ftruncate(mFileDescriptor, size);
|
||||
} while (ret < 0 && errno == EINTR);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
close(mFileDescriptor);
|
||||
mIsValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
int SharedMemory::getFileDescriptor() const
|
||||
{
|
||||
return mFileDescriptor;
|
||||
}
|
||||
|
||||
bool SharedMemory::isValid() const
|
||||
{
|
||||
return mIsValid;
|
||||
}
|
||||
|
||||
void SharedMemory::createFile(const std::string& namePrefix)
|
||||
{
|
||||
unsigned retries = 100;
|
||||
do {
|
||||
const auto name = getRandomName(namePrefix);
|
||||
--retries;
|
||||
|
||||
const int fd = shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
|
||||
if (fd >= 0)
|
||||
{
|
||||
shm_unlink(name.c_str());
|
||||
mFileDescriptor = fd;
|
||||
mIsValid = true;
|
||||
break;
|
||||
}
|
||||
} while (retries > 0 && errno == EEXIST);
|
||||
}
|
||||
|
||||
std::string SharedMemory::getRandomName(const std::string& namePrefix) const
|
||||
{
|
||||
std::string randomSuffix;
|
||||
for (const auto entry : RandomUtils::getRandomVecUnsigned(6))
|
||||
{
|
||||
randomSuffix += std::to_string(entry);
|
||||
}
|
||||
return namePrefix + randomSuffix;
|
||||
}
|
22
src/core/memory/SharedMemory.h
Normal file
22
src/core/memory/SharedMemory.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class SharedMemory
|
||||
{
|
||||
public:
|
||||
void allocate(const std::string& namePrefix, std::size_t size);
|
||||
|
||||
int getFileDescriptor() const;
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
private:
|
||||
|
||||
void createFile(const std::string& namePrefix);
|
||||
|
||||
std::string getRandomName(const std::string& namePrefix) const;
|
||||
|
||||
int mFileDescriptor{0};
|
||||
bool mIsValid{false};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue