Skip to content

Commit 7c2fbf4

Browse files
committed
perf: const where it can be
1 parent 4bfe992 commit 7c2fbf4

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

include/libsharedmemory/libsharedmemory.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ class Memory {
7575

7676
#include <io.h> // CreateFileMappingA, OpenFileMappingA, etc.
7777

78-
Memory::Memory(std::string path, size_t size, bool persist) : _path(path), _size(size), _persist(persist) {};
78+
Memory::Memory(const std::string path, const size_t size, const bool persist) : _path(path), _size(size), _persist(persist) {};
7979

80-
Error Memory::createOrOpen(bool create) {
80+
Error Memory::createOrOpen(const bool create) {
8181
if (create) {
8282
DWORD size_high_order = 0;
8383
DWORD size_low_order = static_cast<DWORD>(size_);
@@ -103,7 +103,7 @@ Error Memory::createOrOpen(bool create) {
103103
}
104104
}
105105

106-
DWORD access = create ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ;
106+
const DWORD access = create ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ;
107107
_data = static_cast<uint8_t *>(MapViewOfFile(_handle, access, 0, 0, _size));
108108

109109
if (!_data) {
@@ -140,24 +140,24 @@ Memory::~Memory() {
140140

141141
#include <stdexcept>
142142

143-
inline Memory::Memory(std::string path, size_t size, bool persist) : _size(size), _persist(persist) {
143+
inline Memory::Memory(const std::string path, const size_t size, const bool persist) : _size(size), _persist(persist) {
144144
_path = "/" + path;
145145
};
146146

147-
inline Error Memory::createOrOpen(bool create) {
147+
inline Error Memory::createOrOpen(const bool create) {
148148
if (create) {
149149
// shm segments persist across runs, and macOS will refuse
150150
// to ftruncate an existing shm segment, so to be on the safe
151151
// side, we unlink it beforehand.
152-
int ret = shm_unlink(_path.c_str());
152+
const int ret = shm_unlink(_path.c_str());
153153
if (ret < 0) {
154154
if (errno != ENOENT) {
155155
return kErrorCreationFailed;
156156
}
157157
}
158158
}
159159

160-
int flags = create ? (O_CREAT | O_RDWR) : O_RDONLY;
160+
const int flags = create ? (O_CREAT | O_RDWR) : O_RDONLY;
161161

162162
_fd = shm_open(_path.c_str(), flags, 0755);
163163
if (_fd < 0) {
@@ -177,7 +177,7 @@ inline Error Memory::createOrOpen(bool create) {
177177
}
178178
}
179179

180-
int prot = create ? (PROT_READ | PROT_WRITE) : PROT_READ;
180+
const int prot = create ? (PROT_READ | PROT_WRITE) : PROT_READ;
181181

182182
void *memory = mmap(nullptr, // addr
183183
_size, // length
@@ -216,7 +216,7 @@ inline Memory::~Memory() {
216216
class SharedMemoryReadStream {
217217
public:
218218

219-
explicit SharedMemoryReadStream(std::string name, uint32_t bufferSize, bool isPersistent):
219+
explicit SharedMemoryReadStream(const std::string name, const uint32_t bufferSize, const bool isPersistent):
220220
_memory(name, bufferSize, isPersistent)/*, _isInOddWriteMode(false)*/ {
221221

222222
if (_memory.open() != kOK) {
@@ -231,7 +231,7 @@ class SharedMemoryReadStream {
231231
std::memcpy(&size, &memory[1], 4 /*uint32 takes 4 byte*/);
232232

233233
// 3) deserialize the buffer vector data
234-
std::string data(reinterpret_cast<const char*>(&memory[5]), size);
234+
const std::string data(reinterpret_cast<const char*>(&memory[5]), size);
235235
return data;
236236
}
237237

@@ -243,7 +243,7 @@ class SharedMemoryReadStream {
243243
class SharedMemoryWriteStream {
244244
public:
245245

246-
explicit SharedMemoryWriteStream(std::string name, uint32_t bufferSize, bool isPersistent):
246+
explicit SharedMemoryWriteStream(const std::string name, const uint32_t bufferSize, const bool isPersistent):
247247
_memory(name, bufferSize, isPersistent), _isInOddWriteMode(false) {
248248

249249
if (_memory.create() != kOK) {
@@ -252,7 +252,7 @@ class SharedMemoryWriteStream {
252252
}
253253

254254
// https://stackoverflow.com/questions/18591924/how-to-use-bitmask
255-
inline uint32_t getWriteFlags(uint8_t type) {
255+
inline uint32_t getWriteFlags(const uint8_t type) {
256256
// flip state
257257
_isInOddWriteMode = !_isInOddWriteMode;
258258
unsigned char flags = type;
@@ -267,15 +267,15 @@ class SharedMemoryWriteStream {
267267
return flags;
268268
}
269269

270-
inline void write(std::string dataString) {
270+
inline void write(const std::string dataString) {
271271
unsigned char* memory = _memory.data();
272272

273273
// 1) copy change flag into buffer for change detection
274274
memory[0] = getWriteFlags(DataType::kMemoryTypeString);
275275

276276
// 2) copy buffer size into buffer (meta data for deserializing)
277277
const char *stringData = dataString.data();
278-
uint32_t bufferSize = dataString.size();
278+
const uint32_t bufferSize = dataString.size();
279279
std::memcpy(&memory[1], &bufferSize, 4 /* uint32_t always takes 4 bytes */);
280280

281281
// 3) copy stringData into memory buffer

0 commit comments

Comments
 (0)