nstool/lib/fnd/memory_blob.cpp

90 lines
1.3 KiB
C++
Raw Normal View History

2017-07-02 15:18:59 +00:00
#include "memory_blob.h"
using namespace fnd;
MemoryBlob::MemoryBlob() :
mData(),
mSize(0),
mVisableSize(0)
2017-07-02 15:18:59 +00:00
{
}
fnd::MemoryBlob::MemoryBlob(const byte_t * bytes, size_t len) :
mData(),
mSize(0),
mVisableSize(0)
2017-07-02 15:18:59 +00:00
{
alloc(len);
memcpy(getBytes(), bytes, getSize());
2017-07-02 15:18:59 +00:00
}
bool fnd::MemoryBlob::operator==(const MemoryBlob & other) const
{
bool isEqual = true;
if (this->getSize() == other.getSize())
{
isEqual = memcmp(this->getBytes(), other.getBytes(), this->getSize()) == 0;
}
else
{
isEqual = false;
}
return isEqual;
}
bool fnd::MemoryBlob::operator!=(const MemoryBlob & other) const
{
return !operator==(other);
}
void fnd::MemoryBlob::operator=(const MemoryBlob & other)
{
alloc(other.getSize());
memcpy(getBytes(), other.getBytes(), getSize());
}
void MemoryBlob::alloc(size_t size)
2017-07-02 15:18:59 +00:00
{
if (size > mSize)
2017-07-02 15:18:59 +00:00
{
allocateMemory(size);
2017-07-02 15:18:59 +00:00
}
else
{
mVisableSize = size;
clearMemory();
2017-07-02 15:18:59 +00:00
}
}
void MemoryBlob::extend(size_t new_size)
2017-07-02 15:18:59 +00:00
{
try {
mData.resize(new_size);
2017-07-02 15:18:59 +00:00
}
catch (...) {
throw fnd::Exception(kModuleName, "extend() failed to allocate memory");
2017-07-02 15:18:59 +00:00
}
}
void fnd::MemoryBlob::clear()
{
mVisableSize = 0;
}
void MemoryBlob::allocateMemory(size_t size)
2017-07-02 15:18:59 +00:00
{
mSize = (size_t)align(size, kAllocBlockSize);
mVisableSize = size;
extend(mSize);
clearMemory();
2017-07-02 15:18:59 +00:00
}
void MemoryBlob::clearMemory()
2017-07-02 15:18:59 +00:00
{
memset(mData.data(), 0, mSize);
2017-07-02 15:18:59 +00:00
}