Coder Social home page Coder Social logo

Comments (4)

DNedic avatar DNedic commented on August 29, 2024

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.

from lockfree.

p4w4n avatar p4w4n commented on August 29, 2024

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.

from lockfree.

DNedic avatar DNedic commented on August 29, 2024

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.

from lockfree.

DNedic avatar DNedic commented on August 29, 2024

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.

from lockfree.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.