Skip to content

Commit

Permalink
use a std::vector as the internal storage for MemIOCallback
Browse files Browse the repository at this point in the history
It allows simple reallocation (resizing). It should be more efficient when adding data little by little.
  • Loading branch information
robUx4 committed Dec 31, 2023
1 parent 702d15c commit 6855edd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
8 changes: 5 additions & 3 deletions ebml/MemIOCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
#define stringstream strstream
#endif

#include <vector>

namespace libebml {

class EBML_DLL_API MemIOCallback : public IOCallback
{
public:
explicit MemIOCallback(std::uint64_t DefaultSize = 128);
~MemIOCallback() override;
~MemIOCallback() override = default;

MemIOCallback(const MemIOCallback&) = delete;
MemIOCallback& operator=(const MemIOCallback&) = delete;
Expand Down Expand Up @@ -61,7 +63,7 @@ class EBML_DLL_API MemIOCallback : public IOCallback
*/
void close() override {}

const binary *GetDataBuffer() const {return dataBuffer;}
const binary *GetDataBuffer() const {return dataBuffer.data();}
std::uint64_t GetDataBufferSize() const {return dataBufferTotalSize;}
void SetDataBufferSize(std::uint64_t newDataBufferSize) {dataBufferTotalSize = newDataBufferSize;}
/*!
Expand All @@ -75,7 +77,7 @@ class EBML_DLL_API MemIOCallback : public IOCallback
bool mOk;
std::string mLastErrorStr;

binary *dataBuffer;
std::vector<binary> dataBuffer;
/*!
Postion where we start 'writing' to the dataBuffer
*/
Expand Down
23 changes: 9 additions & 14 deletions src/MemIOCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace libebml {
MemIOCallback::MemIOCallback(std::uint64_t DefaultSize)
{
//The default size of the buffer is 128 bytes
dataBuffer = static_cast<binary *>(malloc(DefaultSize));
if (dataBuffer == nullptr) {
try {
dataBuffer.resize(DefaultSize);
} catch (...) {
mOk = false;
std::stringstream Msg;
Msg << "Failed to alloc memory block of size ";
Expand All @@ -30,12 +31,6 @@ MemIOCallback::MemIOCallback(std::uint64_t DefaultSize)
mOk = true;
}

MemIOCallback::~MemIOCallback()
{
if (dataBuffer != nullptr)
free(dataBuffer);
}

std::size_t MemIOCallback::read(void *Buffer, std::size_t Size)
{
if (Buffer == nullptr || Size < 1)
Expand All @@ -44,14 +39,14 @@ std::size_t MemIOCallback::read(void *Buffer, std::size_t Size)
if (Size + dataBufferPos < Size || // overflow, reading too much
Size + dataBufferPos > dataBufferTotalSize) {
//We will only return the remaining data
memcpy(Buffer, dataBuffer + dataBufferPos, dataBufferTotalSize - dataBufferPos);
memcpy(Buffer, dataBuffer.data() + dataBufferPos, dataBufferTotalSize - dataBufferPos);
const std::uint64_t oldDataPos = dataBufferPos;
dataBufferPos = dataBufferTotalSize;
return dataBufferTotalSize - oldDataPos;
}

//Well... We made it here, so do a quick and simple copy
memcpy(Buffer, dataBuffer+dataBufferPos, Size);
memcpy(Buffer, dataBuffer.data()+dataBufferPos, Size);
dataBufferPos += Size;

return Size;
Expand All @@ -73,10 +68,10 @@ std::size_t MemIOCallback::write(const void *Buffer, std::size_t Size)
return 0;
if (dataBufferMemorySize < dataBufferPos + Size) {
//We need more memory!
dataBuffer = static_cast<binary *>(realloc(static_cast<void *>(dataBuffer), dataBufferPos + Size));
dataBuffer.resize(dataBufferPos + Size);
dataBufferMemorySize = dataBufferPos + Size;
}
memcpy(dataBuffer+dataBufferPos, Buffer, Size);
memcpy(dataBuffer.data()+dataBufferPos, Buffer, Size);
dataBufferPos += Size;
if (dataBufferPos > dataBufferTotalSize)
dataBufferTotalSize = dataBufferPos;
Expand All @@ -90,10 +85,10 @@ std::uint32_t MemIOCallback::write(IOCallback & IOToRead, std::size_t Size)
return 0;
if (dataBufferMemorySize < dataBufferPos + Size) {
//We need more memory!
dataBuffer = static_cast<binary *>(realloc(static_cast<void *>(dataBuffer), dataBufferPos + Size));
dataBuffer.resize(dataBufferPos + Size);
dataBufferMemorySize = dataBufferPos + Size;
}
IOToRead.readFully(&dataBuffer[dataBufferPos], Size);
IOToRead.readFully(&dataBuffer.data()[dataBufferPos], Size);
dataBufferTotalSize = Size;
return Size;
}
Expand Down

0 comments on commit 6855edd

Please sign in to comment.