1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*******************************************************************************
Copyright(c) 2022 Ludovic Pollet. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*******************************************************************************/
extern "C" {
// Load shm_open_anon but force it to have static symbol only
static int shm_open_anon(void);
#include "../libs/indicore/shm_open_anon.c"
}
#include <system_error>
#include "SharedBuffer.h"
#ifdef __linux__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <linux/unistd.h>
#endif
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
SharedBuffer::SharedBuffer()
{
fd = -1;
size = 0;
}
SharedBuffer::~SharedBuffer()
{
release();
}
void SharedBuffer::attach(int fd)
{
release();
this->fd = fd;
if (fd == -1)
{
size = 0;
return;
}
struct stat sb;
if (fstat(fd, &sb) == -1)
{
int e = errno;
throw std::system_error(e, std::generic_category(), "Unable to stat buffer");
}
size = sb.st_size;
}
void SharedBuffer::release()
{
if (fd != -1)
{
close(fd);
fd = -1;
}
size = 0;
}
int SharedBuffer::getFd() const
{
if (fd == -1)
{
throw std::runtime_error("SharedBuffer is not allocated");
}
return fd;
}
void SharedBuffer::write(const void * data, ssize_t offset, ssize_t writeSize)
{
if (writeSize == 0)
{
return;
}
if (size < offset + writeSize)
{
throw std::runtime_error("Attempt to write beyond past");
}
void * mmapped = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mmapped == MAP_FAILED)
{
perror("mmap");
throw std::runtime_error("Mmap failed");
}
memcpy((void*)((char*)mmapped + offset), data, writeSize);
if (munmap(mmapped, size) == -1)
{
perror("munmap");
throw std::runtime_error("Munmap failed");
}
}
void SharedBuffer::allocate(ssize_t nsize)
{
release();
fd = shm_open_anon();
if (fd == -1)
{
throw std::system_error(errno, std::generic_category(), "memfd_create");
}
int ret = ftruncate(fd, nsize);
if (ret == -1)
{
throw std::system_error(errno, std::generic_category(), "memfd_create");
}
size = nsize;
}
|