From 1c9577a1acc7e845053c43bf496f29e30f755c8a Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Mon, 11 Jan 2016 00:29:14 -0800 Subject: [PATCH] Simplify blob encoding --- src/ripple/nodestore/impl/EncodedBlob.cpp | 29 ++++++++--------------- src/ripple/nodestore/impl/EncodedBlob.h | 23 +++++++++++++----- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/ripple/nodestore/impl/EncodedBlob.cpp b/src/ripple/nodestore/impl/EncodedBlob.cpp index 5b811751e..abf1327b1 100644 --- a/src/ripple/nodestore/impl/EncodedBlob.cpp +++ b/src/ripple/nodestore/impl/EncodedBlob.cpp @@ -19,36 +19,27 @@ #include #include -#include +#include namespace ripple { namespace NodeStore { void -EncodedBlob::prepare (std::shared_ptr const& object) +EncodedBlob::prepare ( + std::shared_ptr const& object) { m_key = object->getHash().begin (); - // This is how many bytes we need in the flat data - m_size = object->getData ().size () + 9; + auto ret = m_data.alloc(object->getData ().size () + 9); - m_data.ensureSize(m_size); + // the first 8 bytes are unused + memset (ret, 0, 8); - { - // these 8 bytes are unused - std::uint64_t* buf = static_cast < - std::uint64_t*>(m_data.getData ()); - *buf = 0; - } + ret[8] = static_cast (object->getType ()); - { - unsigned char* buf = static_cast < - unsigned char*> (m_data.getData ()); - buf [8] = static_cast < - unsigned char> (object->getType ()); - memcpy (&buf [9], object->getData ().data(), - object->getData ().size()); - } + memcpy (ret + 9, + object->getData ().data(), + object->getData ().size()); } } diff --git a/src/ripple/nodestore/impl/EncodedBlob.h b/src/ripple/nodestore/impl/EncodedBlob.h index 67911917e..849a80ad2 100644 --- a/src/ripple/nodestore/impl/EncodedBlob.h +++ b/src/ripple/nodestore/impl/EncodedBlob.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED #define RIPPLE_NODESTORE_ENCODEDBLOB_H_INCLUDED +#include #include -#include #include namespace ripple { @@ -35,14 +35,25 @@ struct EncodedBlob { public: void prepare (std::shared_ptr const& object); - void const* getKey () const noexcept { return m_key; } - std::size_t getSize () const noexcept { return m_size; } - void const* getData () const noexcept { return m_data.getData (); } + + void const* getKey () const noexcept + { + return m_key; + } + + std::size_t getSize () const noexcept + { + return m_data.size(); + } + + void const* getData () const noexcept + { + return reinterpret_cast(m_data.data()); + } private: void const* m_key; - beast::MemoryBlock m_data; - std::size_t m_size; + Buffer m_data; }; }