nstool/lib/fnd/memory_blob.cpp

57 lines
814 B
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
}
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 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
}