@@ -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
4340const size_t bufferSizeSize = 4 ; // size_t takes 4 bytes
4441const size_t sizeOfOneFloat = 4 ; // float takes 4 bytes
42+ const size_t sizeOfOneChar = 1 ; // char takes 1 byte
4543const size_t sizeOfOneDouble = 8 ; // double takes 8 bytes
4644const 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 =
0 commit comments