Skip to content

Commit b2d2d61

Browse files
committed
feat: added isolated readSize(), readLength()
1 parent fa72468 commit b2d2d61

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

include/libsharedmemory/libsharedmemory.hpp

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ enum DataType {
3434
kMemoryTypeString = 2,
3535
kMemoryTypeFloat = 4,
3636
kMemoryTypeDouble = 8,
37-
kMemoryTypeInt = 16,
38-
kMemoryTypeLong = 32,
39-
kMemoryTypeChar = 64,
4037
};
4138

4239
// byte sizes of memory layout
4340
const size_t bufferSizeSize = 4; // size_t takes 4 bytes
4441
const size_t sizeOfOneFloat = 4; // float takes 4 bytes
42+
const size_t sizeOfOneChar = 1; // char takes 1 byte
4543
const size_t sizeOfOneDouble = 8; // double takes 8 bytes
4644
const size_t flagSize = 1; // char takes 1 byte
4745

@@ -263,34 +261,54 @@ class SharedMemoryReadStream {
263261
return memory[0];
264262
}
265263

266-
/**
267-
* @brief Returns a float* read from shared memory
268-
* Caller has the obligation to call delete [] on the returning float*.
269-
*
270-
* @return float*
271-
*/
272-
inline float* readFloatArray() {
273-
void *memory = _memory.data();
274-
int* intMemory = (int*) memory;
275-
float* typedMemory = (float*) memory;
264+
inline void close() { _memory.close(); }
265+
276266

277-
// define value to allocate 4 byte for the size_t
267+
inline size_t readSize(char dataType) {
268+
void *memory = _memory.data();
278269
std::size_t size = 0;
279270

280-
// copy size data to size variable
281-
std::memcpy(&size, &intMemory[flagSize], bufferSizeSize);
271+
// TODO(kyr0): should be clarified why we need to use size_t there
272+
// for the size to be received correctly, but in float, we need int
273+
// Might be prone to undefined behaviour; should be tested
274+
// with various compilers; otherwise use memcpy() for the size
275+
// and align the memory with one cast.
282276

283-
// allocating memory on heap (this might leak)
284-
float *data = new float[size / sizeOfOneFloat]();
277+
if (dataType & kMemoryTypeDouble) {
278+
size_t *intMemory = (size_t *)memory;
279+
// copy size data to size variable
280+
std::memcpy(&size, &intMemory[flagSize], bufferSizeSize);
281+
}
285282

286-
// copy to data buffer
287-
std::memcpy(data, &typedMemory[flagSize + bufferSizeSize], size);
288-
289-
return data;
283+
if (dataType & kMemoryTypeFloat) {
284+
int* intMemory = (int*) memory;
285+
// copy size data to size variable
286+
std::memcpy(&size, &intMemory[flagSize], bufferSizeSize);
287+
}
288+
289+
if (dataType & kMemoryTypeString) {
290+
char* charMemory = (char*) memory;
291+
// copy size data to size variable
292+
std::memcpy(&size, &charMemory[flagSize], bufferSizeSize);
293+
}
294+
return size;
290295
}
291296

292-
inline void close() {
293-
_memory.close();
297+
inline size_t readLength(char dataType) {
298+
size_t size = readSize(dataType);
299+
300+
if (dataType & kMemoryTypeString) {
301+
return size / sizeOfOneChar;
302+
}
303+
304+
if (dataType & kMemoryTypeFloat) {
305+
return size / sizeOfOneFloat;
306+
}
307+
308+
if (dataType & kMemoryTypeDouble) {
309+
return size / sizeOfOneDouble;
310+
}
311+
return 0;
294312
}
295313

296314
/**
@@ -302,22 +320,32 @@ class SharedMemoryReadStream {
302320
// TODO: might wanna use templated functions here like: <T> readNumericArray()
303321
inline double* readDoubleArray() {
304322
void *memory = _memory.data();
305-
// TODO(kyr0): should be clarified why we need to use size_t there
306-
// for the size to be received correctly, but in float, we need int
307-
// Might be prone to undefined behaviour; should be tested
308-
// with various compilers; otherwise use memcpy() for the size
309-
// and align the memory with one cast.
310-
size_t* intMemory = (size_t*) memory;
323+
std::size_t size = readSize(kMemoryTypeDouble);
311324
double* typedMemory = (double*) memory;
312325

313-
// define value to allocate 4 byte for the size_t
314-
std::size_t size = 0;
326+
// allocating memory on heap (this might leak)
327+
double *data = new double[size / sizeOfOneDouble]();
315328

316-
// copy size data to size variable
317-
std::memcpy(&size, &intMemory[flagSize], bufferSizeSize);
329+
// copy to data buffer
330+
std::memcpy(data, &typedMemory[flagSize + bufferSizeSize], size);
331+
332+
return data;
333+
}
334+
335+
/**
336+
* @brief Returns a float* read from shared memory
337+
* Caller has the obligation to call delete [] on the returning float*.
338+
*
339+
* @return float*
340+
*/
341+
inline float* readFloatArray() {
342+
void *memory = _memory.data();
343+
float *typedMemory = (float *)memory;
344+
345+
std::size_t size = readSize(kMemoryTypeFloat);
318346

319347
// allocating memory on heap (this might leak)
320-
double *data = new double[size / sizeOfOneDouble]();
348+
float *data = new float[size / sizeOfOneFloat]();
321349

322350
// copy to data buffer
323351
std::memcpy(data, &typedMemory[flagSize + bufferSizeSize], size);
@@ -328,10 +356,7 @@ class SharedMemoryReadStream {
328356
inline std::string readString() {
329357
char* memory = (char*) _memory.data();
330358

331-
std::size_t size = 0;
332-
333-
// copy buffer size
334-
std::memcpy(&size, &memory[flagSize], bufferSizeSize);
359+
std::size_t size = readSize(kMemoryTypeString);
335360

336361
// create a string that copies the data from memory
337362
std::string data =

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"url": "https://github.com/kyr0/libsharedmemory/issues"
3232
},
3333
"homepage": "https://github.com/kyr0/libsharedmemory#readme"
34-
}
34+
}

test/test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ const lest::test specification[] = {
108108

109109
char flagsData = read$.readFlags();
110110

111+
EXPECT(read$.readLength(kMemoryTypeString) == 4);
112+
111113
std::bitset<8> flags(flagsData);
112114

113115
EXPECT((flagsData & kMemoryTypeString));
@@ -151,6 +153,8 @@ const lest::test specification[] = {
151153

152154
write$.write(numbers, 72);
153155

156+
EXPECT(read$.readLength(kMemoryTypeFloat) == 72);
157+
154158
float* numbersReadPtr = read$.readFloatArray();
155159

156160
EXPECT(numbers[0] == numbersReadPtr[0]);
@@ -194,6 +198,8 @@ const lest::test specification[] = {
194198

195199
write$.write(numbers, 72);
196200

201+
EXPECT(read$.readLength(kMemoryTypeDouble) == 72);
202+
197203
double* numbersReadPtr = read$.readDoubleArray();
198204

199205
EXPECT(numbers[0] == numbersReadPtr[0]);

0 commit comments

Comments
 (0)