Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a bitsery buffer to a C API #71

Closed
VelocityRa opened this issue Oct 30, 2020 · 4 comments
Closed

Passing a bitsery buffer to a C API #71

VelocityRa opened this issue Oct 30, 2020 · 4 comments

Comments

@VelocityRa
Copy link
Contributor

VelocityRa commented Oct 30, 2020

I want to pass some serialized state to a C API, but I don't really see a way of doing that without an additional copy.
By "additional copy" I mean, currently I am using a std::vector BufferWriter. The std::vector will be destroyed after my C API call returns because of RAII. I can't stop that. So I have to use malloc + copy that data somewhere else, which is wasteful.

To be clear, I am the developer of that C API. I want to expose "load" and "save" functions for some state.
So perhaps I could use an opaque pointer that holds a newed std::vector? But that sounds hacky.
Maybe there's a solution where OutputBufferAdapter can be implemented to use malloc + realloc.

Excuse my quick + maybe unclear problem description.
Also if this is not currently feasible, I understand.

Thanks.

@fraillt
Copy link
Owner

fraillt commented Oct 31, 2020

If I understood your problem correctly, you can use C-style array or even pointer+size as a buffer. Here is specializations provided for both cases. Also you could look at working example here

@VelocityRa
Copy link
Contributor Author

VelocityRa commented Nov 1, 2020

Right, but that example is for a C static array and for the other one, I don't know the total size of the serialized data that will be saved ahead of time.
I have lots of objects serialized recursively and some are dynamic.
I don't see a specialization there that would allow for that use-case I think.

For example, a possible solution would be to give bitsery a malloced buffer of some initial size, and then it would realloc as it needs space (just as it does .push_back for std::vector OutputBufferAdapters). Or just give it a ** and it would malloc too and put the pointer there.

Hopefully I explained the problem more clearly now.

@fraillt
Copy link
Owner

fraillt commented Nov 1, 2020

You left with only one solution then. You will need to provide a custom buffer implementation and specialize it for ContainerTraits and BufferAdapterTraits. Hope that helped:)

@VelocityRa
Copy link
Contributor Author

Ok I see, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants