2017-07-02 15:18:59 +00:00
|
|
|
#include "memory_blob.h"
|
|
|
|
|
|
|
|
using namespace fnd;
|
|
|
|
|
|
|
|
MemoryBlob::MemoryBlob() :
|
2017-07-06 03:27:25 +00:00
|
|
|
mData(),
|
|
|
|
mSize(0),
|
|
|
|
mVisableSize(0)
|
2017-07-02 15:18:59 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:27:25 +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
|
|
|
{
|
2017-07-06 03:27:25 +00:00
|
|
|
alloc(len);
|
|
|
|
memcpy(getBytes(), bytes, getSize());
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 03:27:25 +00:00
|
|
|
void MemoryBlob::alloc(size_t size)
|
2017-07-02 15:18:59 +00:00
|
|
|
{
|
2017-07-06 03:27:25 +00:00
|
|
|
if (size > mSize)
|
2017-07-02 15:18:59 +00:00
|
|
|
{
|
2017-07-06 03:27:25 +00:00
|
|
|
allocateMemory(size);
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-06 03:27:25 +00:00
|
|
|
mVisableSize = size;
|
|
|
|
clearMemory();
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:27:25 +00:00
|
|
|
void MemoryBlob::extend(size_t new_size)
|
2017-07-02 15:18:59 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-07-06 03:27:25 +00:00
|
|
|
mData.resize(new_size);
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
2017-07-06 03:27:25 +00:00
|
|
|
throw fnd::Exception(kModuleName, "extend() failed to allocate memory");
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 03:27:25 +00:00
|
|
|
void MemoryBlob::allocateMemory(size_t size)
|
2017-07-02 15:18:59 +00:00
|
|
|
{
|
2017-07-06 03:27:25 +00:00
|
|
|
mSize = (size_t)align(size, kAllocBlockSize);
|
|
|
|
mVisableSize = size;
|
|
|
|
extend(mSize);
|
|
|
|
clearMemory();
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 03:27:25 +00:00
|
|
|
void MemoryBlob::clearMemory()
|
2017-07-02 15:18:59 +00:00
|
|
|
{
|
2017-07-06 03:27:25 +00:00
|
|
|
memset(mData.data(), 0, mSize);
|
2017-07-02 15:18:59 +00:00
|
|
|
}
|