Simple drawing example.
This commit is contained in:
parent
d7fe11913f
commit
f0091f9e04
27 changed files with 450 additions and 68 deletions
|
@ -12,63 +12,63 @@
|
|||
|
||||
void SharedMemory::allocate(const std::string& namePrefix, std::size_t size)
|
||||
{
|
||||
createFile(namePrefix);
|
||||
createFile(namePrefix);
|
||||
|
||||
if (!mIsValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!mIsValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
int ret{-1};
|
||||
do {
|
||||
ret = ftruncate(mFileDescriptor, size);
|
||||
} while (ret < 0 && errno == EINTR);
|
||||
int ret{-1};
|
||||
do {
|
||||
ret = ftruncate(mFileDescriptor, size);
|
||||
} while (ret < 0 && errno == EINTR);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
close(mFileDescriptor);
|
||||
mIsValid = false;
|
||||
}
|
||||
if (ret < 0)
|
||||
{
|
||||
close(mFileDescriptor);
|
||||
mIsValid = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int SharedMemory::getFileDescriptor() const
|
||||
{
|
||||
return mFileDescriptor;
|
||||
return mFileDescriptor;
|
||||
}
|
||||
|
||||
bool SharedMemory::isValid() const
|
||||
{
|
||||
return mIsValid;
|
||||
return mIsValid;
|
||||
}
|
||||
|
||||
void SharedMemory::createFile(const std::string& namePrefix)
|
||||
{
|
||||
#ifdef __linux__
|
||||
unsigned retries = 100;
|
||||
do {
|
||||
const auto name = getRandomName(namePrefix);
|
||||
--retries;
|
||||
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);
|
||||
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);
|
||||
#endif
|
||||
}
|
||||
|
||||
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;
|
||||
std::string randomSuffix;
|
||||
for (const auto entry : RandomUtils::getRandomVecUnsigned(6))
|
||||
{
|
||||
randomSuffix += std::to_string(entry);
|
||||
}
|
||||
return namePrefix + randomSuffix;
|
||||
}
|
||||
|
|
|
@ -5,18 +5,18 @@
|
|||
class SharedMemory
|
||||
{
|
||||
public:
|
||||
void allocate(const std::string& namePrefix, std::size_t size);
|
||||
void allocate(const std::string& namePrefix, std::size_t size);
|
||||
|
||||
int getFileDescriptor() const;
|
||||
int getFileDescriptor() const;
|
||||
|
||||
bool isValid() const;
|
||||
bool isValid() const;
|
||||
|
||||
private:
|
||||
|
||||
void createFile(const std::string& namePrefix);
|
||||
void createFile(const std::string& namePrefix);
|
||||
|
||||
std::string getRandomName(const std::string& namePrefix) const;
|
||||
std::string getRandomName(const std::string& namePrefix) const;
|
||||
|
||||
int mFileDescriptor{0};
|
||||
bool mIsValid{false};
|
||||
int mFileDescriptor{0};
|
||||
bool mIsValid{false};
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue