Skip to content

Reset CDR state #128

@Kofemolka

Description

@Kofemolka

Hello!

We are using CDR and FastBuffer inside our language bindings: Python, Golang, Java. For that we have a C-wrapper, consisting of struct to hold CDR and FastBuffer, and set of methods to serialize/deserialize different types.

Since we can't allocate this on stack, every time we need to serialize or deserialize some data model a new instance of this wrapping struct is allocated in the heap and deleted afterwards.
For performance reasons, we would like to create this struct and subsequently CDR and FastBuffer only once per our data model, and "reset" it with pointer to raw buffer during each serialize/deserialize sequence.

FastBuffer has perfectly legal way of re-init, since implements move ctor and assignment operator.
CDR has method reset() which also does what we need, except re-evaluation of m_lastPosition, which should point to the end of a FastBuffer.

Here is a sample usage:

struct hel_t::Pimpl
{
    Pimpl(unsigned char* buffer, unsigned int max_size, hel_mode mode)
        : fastbuffer(reinterpret_cast<char*>(buffer), max_size)
        , serializer(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR)
    {
        init(mode);
    }

    void init(hel_mode mode)
    {
        switch (mode)
        {
        case kHelModeSer:
            serializer.serialize_encapsulation();
            break;
        case kHelModeDes:
            serializer.read_encapsulation();
            break;
        default:
            throw std::invalid_argument("Unknown HEL mode value");
        }
    }

    eprosima::fastcdr::FastBuffer fastbuffer;
    eprosima::fastcdr::Cdr serializer;
};

void hel_reset(hel handle, unsigned char* buffer, uint32_t max_size, hel_mode mode)
{
    assert(handle != nullptr);

    handle->pimpl_->fastbuffer = eprosima::fastcdr::FastBuffer(reinterpret_cast<char*>(buffer), max_size);
    handle->pimpl_->serializer.reset();
    handle->pimpl_->init(mode);

    handle->status_ = kHelStatusOk;
}

After calling serializer.reset() it's m_lastPosition will point to the end of a previous buffer, not current.

Attaching one-liner patch that solves this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions