Max
Max

Reputation: 20004

Serialize into native memory buffer from C# using flatbuffers

Is it possible with flatbuffers in C# to serialize objects to native (unmanaged) memory buffer? So I want to do these steps:

  1. Allocate a native memory buffer from native memory
  2. Create objects in C# and serialize them into the allocated buffer
  3. Send this memory buffer to C++ for deserialization

I'm thinking either of some custom memory buffer allocator in C#, or of some way of transferring ownership of a memory buffer form C# to C++.

In general I want to avoid copying memory when sending data from C# to C++ and vice versa. I want this memory buffer to be shared between C# and C++.

How do I do that?

Upvotes: 2

Views: 1015

Answers (2)

Aardappel
Aardappel

Reputation: 6074

No, the current FlatBuffers implementation is hard-coded to write to a regular byte array. You could copy this array to native memory afterwards, or like @pm100 says, pin it.

All serialization in FlatBuffers goes through an abstraction called the ByteBuffer, so if you made an implementation of that for native memory, it could be used directly relatively easily.

Upvotes: 1

JazzSoft
JazzSoft

Reputation: 426

Yes, if you use C++/CLI. Basic data types such as bool, 32-bit int, short, etc are same. For other types check it out msclr::interop::marshal_as<>.

Similar post: C++/CLI Converting from System::String^ to std::string

Upvotes: 0

Related Questions