SPSC Ring Buffer variable length string support
p4w4n opened this issue · comments
Hey,
Can support be added to the Ring Buffer to support variable length string data? Maybe add a length of the string as the metadata at the start followed by the data?
Thanks
Hello @p4w4n , I am thinking about a special data structure with framing but i'm not yet sure it's worth adding as you can do this yourself by simply placing the length of the data structure before the data structure in a byte-based Ring Buffer, then reading only the size of the length, and based on that reading out the variable length data structure itself.
If you want the ability to resynchronize if you accidentally read more or less, you can create a framing protocol yourself with a magic value at the start/end.
Thanks @DNedic, this is what I did to make it compatible with my use case. I have defined a metadata structure with a size parameter.
struct ElementMetadata {
size_t size; // Size of the element data, excluding this metadata
};
and then updated Read, Write and Peek functions:
Read:
ElementMetadata metadata;
// Assuming _data is a char pointer to the buffer
memcpy(&metadata, &_data[r], sizeof(ElementMetadata));
size_t cnt = metadata.size;
r = (r + sizeof(ElementMetadata)) % size; // Advance past metadata
Write:
ElementMetadata metadata{cnt};
memcpy(_data + w, &metadata, sizeof(ElementMetadata));
w += sizeof(ElementMetadata);
This code won't be compatible with the generic implementation though.
I don't really see this applicable for anything but a byte buffer, and since lockfree
is templated by type stored for all data structures so far, I don't think it would be a great fit.
Also, the byte buffer itself would require a reinterpret cast before passing the pointer to the trivial type the user wants to store, making for an ugly API.
I might create a C library for this in another repo as I see it a better fit there.
I've thought about this problem some more, and here's what you can do: use a byte BipartiteBuf
, store the size of the string or your data structure, then memcpy the data structure itself. When reading, acquire a read, read the length and then if there is enough bytes available left over, memcpy out that number of bytes into your data structure. Of course, you need to use a trivially copy-able type.