|
| 1 | + |
| 2 | +#ifndef INCLUDE_KYR0_LIBSHAREDMEMORY_HPP_ |
| 3 | +#define INCLUDE_KYR0_LIBSHAREDMEMORY_HPP_ |
| 4 | + |
| 5 | +#define KYR0_LIBSHAREDMEMORY_VERSION_MAJOR 1 |
| 6 | +#define KYR0_LIBSHAREDMEMORY_VERSION_MINOR 0 |
| 7 | +#define KYR0_LIBSHAREDMEMORY_VERSION_PATCH 0 |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <cstdint> |
| 11 | +#include <cstring> |
| 12 | +#include <string> |
| 13 | +#include <cstddef> // nullptr_t, ptrdiff_t, size_t |
| 14 | + |
| 15 | +#if defined(_WIN32) |
| 16 | +#define WIN32_LEAN_AND_MEAN |
| 17 | +#include <windows.h> |
| 18 | +#undef WIN32_LEAN_AND_MEAN |
| 19 | +#endif |
| 20 | + |
| 21 | +namespace lsm { |
| 22 | + |
| 23 | +enum Error { |
| 24 | + kOK = 0, |
| 25 | + kErrorCreationFailed = 100, |
| 26 | + kErrorMappingFailed = 110, |
| 27 | + kErrorOpeningFailed = 120, |
| 28 | +}; |
| 29 | + |
| 30 | +enum DataType { |
| 31 | + kMemoryChanged = 1, |
| 32 | + kMemoryTypeString = 2, |
| 33 | + kMemoryFloat32Vector = 4, |
| 34 | + kMemoryUInt8Vector = 8, |
| 35 | +}; |
| 36 | + |
| 37 | +class Memory { |
| 38 | +public: |
| 39 | + // path should only contain alpha-numeric characters, and is normalized |
| 40 | + // on linux/macOS. |
| 41 | + explicit Memory(std::string path, size_t size, bool persist); |
| 42 | + |
| 43 | + // create a shared memory area and open it for writing |
| 44 | + inline Error create() { return createOrOpen(true); }; |
| 45 | + |
| 46 | + // open an existing shared memory for reading |
| 47 | + inline Error open() { return createOrOpen(false); }; |
| 48 | + |
| 49 | + inline size_t size() { return _size; }; |
| 50 | + |
| 51 | + inline const std::string &path() { return _path; } |
| 52 | + |
| 53 | + inline uint8_t *data() { return _data; } |
| 54 | + |
| 55 | + void destroy(); |
| 56 | + |
| 57 | + ~Memory(); |
| 58 | + |
| 59 | +private: |
| 60 | + Error createOrOpen(bool create); |
| 61 | + |
| 62 | + std::string _path; |
| 63 | + uint8_t *_data = nullptr; |
| 64 | + size_t _size = 0; |
| 65 | + bool _persist = true; |
| 66 | +#if defined(_WIN32) |
| 67 | + HANDLE _handle; |
| 68 | +#else |
| 69 | + int _fd = -1; |
| 70 | +#endif |
| 71 | +}; |
| 72 | + |
| 73 | +// Windows shared memory implementation |
| 74 | +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) |
| 75 | + |
| 76 | +#include <io.h> // CreateFileMappingA, OpenFileMappingA, etc. |
| 77 | + |
| 78 | +Memory::Memory(std::string path, size_t size, bool persist) : _path(path), _size(size), _persist(persist) {}; |
| 79 | + |
| 80 | +Error Memory::createOrOpen(bool create) { |
| 81 | + if (create) { |
| 82 | + DWORD size_high_order = 0; |
| 83 | + DWORD size_low_order = static_cast<DWORD>(size_); |
| 84 | + |
| 85 | + _handle = CreateFileMappingA(INVALID_HANDLE_VALUE, // use paging file |
| 86 | + NULL, // default security |
| 87 | + PAGE_READWRITE, // read/write access |
| 88 | + size_high_order, size_low_order, |
| 89 | + _path.c_str() // name of mapping object |
| 90 | + ); |
| 91 | + |
| 92 | + if (!_handle) { |
| 93 | + return kErrorCreationFailed; |
| 94 | + } |
| 95 | + } else { |
| 96 | + _handle = OpenFileMappingA(FILE_MAP_READ, // read access |
| 97 | + FALSE, // do not inherit the name |
| 98 | + _path.c_str() // name of mapping object |
| 99 | + ); |
| 100 | + |
| 101 | + if (!_handle) { |
| 102 | + return kErrorOpeningFailed; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + DWORD access = create ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ; |
| 107 | + _data = static_cast<uint8_t *>(MapViewOfFile(_handle, access, 0, 0, _size)); |
| 108 | + |
| 109 | + if (!_data) { |
| 110 | + return kErrorMappingFailed; |
| 111 | + } |
| 112 | + return kOK; |
| 113 | +} |
| 114 | + |
| 115 | +void Memory::destroy() { |
| 116 | + if (_data) { |
| 117 | + UnmapViewOfFile(_data); |
| 118 | + _data = nullptr; |
| 119 | + } |
| 120 | + CloseHandle(_handle); |
| 121 | +} |
| 122 | + |
| 123 | +Memory::~Memory() { |
| 124 | + destroy() |
| 125 | +} |
| 126 | +#endif // defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) |
| 127 | + |
| 128 | +#if defined(__APPLE__) || defined(__linux__) || defined(__unix__) || defined(_POSIX_VERSION) || defined(__ANDROID__) |
| 129 | + |
| 130 | +#include <fcntl.h> // for O_* constants |
| 131 | +#include <sys/mman.h> // mmap, munmap |
| 132 | +#include <sys/stat.h> // for mode constants |
| 133 | +#include <unistd.h> // unlink |
| 134 | + |
| 135 | +#if defined(__APPLE__) |
| 136 | + |
| 137 | +#include <errno.h> |
| 138 | + |
| 139 | +#endif // __APPLE__ |
| 140 | + |
| 141 | +#include <stdexcept> |
| 142 | + |
| 143 | +inline Memory::Memory(std::string path, size_t size, bool persist) : _size(size), _persist(persist) { |
| 144 | + _path = "/" + path; |
| 145 | +}; |
| 146 | + |
| 147 | +inline Error Memory::createOrOpen(bool create) { |
| 148 | + if (create) { |
| 149 | + // shm segments persist across runs, and macOS will refuse |
| 150 | + // to ftruncate an existing shm segment, so to be on the safe |
| 151 | + // side, we unlink it beforehand. |
| 152 | + int ret = shm_unlink(_path.c_str()); |
| 153 | + if (ret < 0) { |
| 154 | + if (errno != ENOENT) { |
| 155 | + return kErrorCreationFailed; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + int flags = create ? (O_CREAT | O_RDWR) : O_RDONLY; |
| 161 | + |
| 162 | + _fd = shm_open(_path.c_str(), flags, 0755); |
| 163 | + if (_fd < 0) { |
| 164 | + if (create) { |
| 165 | + return kErrorCreationFailed; |
| 166 | + } else { |
| 167 | + return kErrorOpeningFailed; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (create) { |
| 172 | + // this is the only way to specify the size of a |
| 173 | + // newly-created POSIX shared memory object |
| 174 | + int ret = ftruncate(_fd, _size); |
| 175 | + if (ret != 0) { |
| 176 | + return kErrorCreationFailed; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + int prot = create ? (PROT_READ | PROT_WRITE) : PROT_READ; |
| 181 | + |
| 182 | + void *memory = mmap(nullptr, // addr |
| 183 | + _size, // length |
| 184 | + prot, // prot |
| 185 | + MAP_SHARED, // flags |
| 186 | + _fd, // fd |
| 187 | + 0 // offset |
| 188 | + ); |
| 189 | + |
| 190 | + if (memory == MAP_FAILED) { |
| 191 | + return kErrorMappingFailed; |
| 192 | + } |
| 193 | + |
| 194 | + _data = static_cast<uint8_t *>(memory); |
| 195 | + |
| 196 | + if (!_data) { |
| 197 | + return kErrorMappingFailed; |
| 198 | + } |
| 199 | + return kOK; |
| 200 | +} |
| 201 | + |
| 202 | +inline void Memory::destroy() { |
| 203 | + munmap(_data, _size); |
| 204 | + close(_fd); |
| 205 | + shm_unlink(_path.c_str()); |
| 206 | +} |
| 207 | + |
| 208 | +inline Memory::~Memory() { |
| 209 | + if (!_persist) { |
| 210 | + destroy(); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +#endif // defined(__APPLE__) || defined(__linux__) || defined(__unix__) || defined(_POSIX_VERSION) || defined(__ANDROID__) |
| 215 | + |
| 216 | +class SharedMemoryReadStream { |
| 217 | +public: |
| 218 | + |
| 219 | + explicit SharedMemoryReadStream(std::string name, uint32_t bufferSize, bool isPersistent): |
| 220 | + _memory(name, bufferSize, isPersistent)/*, _isInOddWriteMode(false)*/ { |
| 221 | + |
| 222 | + if (_memory.open() != kOK) { |
| 223 | + throw "Shared memory segment could not be opened."; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + inline std::string read() { |
| 228 | + unsigned char* memory = _memory.data(); |
| 229 | + |
| 230 | + uint32_t size; |
| 231 | + std::memcpy(&size, &memory[1], 4 /*uint32 takes 4 byte*/); |
| 232 | + |
| 233 | + // 3) deserialize the buffer vector data |
| 234 | + std::string data(reinterpret_cast<const char*>(&memory[5]), size); |
| 235 | + return data; |
| 236 | + } |
| 237 | + |
| 238 | +private: |
| 239 | + Memory _memory; |
| 240 | + //bool _isInOddWriteMode; |
| 241 | +}; |
| 242 | + |
| 243 | +class SharedMemoryWriteStream { |
| 244 | +public: |
| 245 | + |
| 246 | + explicit SharedMemoryWriteStream(std::string name, uint32_t bufferSize, bool isPersistent): |
| 247 | + _memory(name, bufferSize, isPersistent), _isInOddWriteMode(false) { |
| 248 | + |
| 249 | + if (_memory.create() != kOK) { |
| 250 | + throw "Shared memory segment could not be created."; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + // https://stackoverflow.com/questions/18591924/how-to-use-bitmask |
| 255 | + inline uint32_t getWriteFlags(uint8_t type) { |
| 256 | + // flip state |
| 257 | + _isInOddWriteMode = !_isInOddWriteMode; |
| 258 | + unsigned char flags = type; |
| 259 | + |
| 260 | + if (_isInOddWriteMode) { |
| 261 | + // enable flag, leave rest untouched |
| 262 | + flags ^= DataType::kMemoryChanged; |
| 263 | + } else { |
| 264 | + // disable flag, leave rest untouched |
| 265 | + flags &= ~DataType::kMemoryChanged; |
| 266 | + } |
| 267 | + return flags; |
| 268 | + } |
| 269 | + |
| 270 | + inline void write(std::string dataString) { |
| 271 | + unsigned char* memory = _memory.data(); |
| 272 | + |
| 273 | + // 1) copy change flag into buffer for change detection |
| 274 | + memory[0] = getWriteFlags(DataType::kMemoryTypeString); |
| 275 | + |
| 276 | + // 2) copy buffer size into buffer (meta data for deserializing) |
| 277 | + const char *stringDataVector = dataString.data(); |
| 278 | + uint32_t bufferSize = dataString.size(); |
| 279 | + std::memcpy(&memory[1], &bufferSize, sizeof(bufferSize) /* should be always 4 */); |
| 280 | + |
| 281 | + // 3) copy data vector into memory buffer |
| 282 | + std::memcpy(&memory[5 /* 1b status; 4b buffer size */], stringDataVector, bufferSize); |
| 283 | + } |
| 284 | + |
| 285 | + inline void destroy() { |
| 286 | + _memory.destroy(); |
| 287 | + } |
| 288 | + |
| 289 | +private: |
| 290 | + Memory _memory; |
| 291 | + bool _isInOddWriteMode; |
| 292 | +}; |
| 293 | + |
| 294 | + |
| 295 | +}; // namespace lsm |
| 296 | + |
0 commit comments