Commit 1b2fd78e authored by Jayaram Bobba's avatar Jayaram Bobba

Make using the Allocator interface optional for AlignedBuffers (for cases where…

Make using the Allocator interface optional for AlignedBuffers (for cases where Allocator lifetimes are not manageable)
parent 5b78536c
......@@ -32,6 +32,22 @@ runtime::AlignedBuffer::AlignedBuffer()
{
}
runtime::AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment)
: m_allocator(nullptr)
, m_byte_size(byte_size)
{
m_byte_size = std::max<size_t>(1, byte_size);
size_t allocation_size = m_byte_size + alignment;
m_allocated_buffer = static_cast<char*>(malloc(allocation_size));
m_aligned_buffer = m_allocated_buffer;
size_t mod = size_t(m_aligned_buffer) % alignment;
if (mod != 0)
{
m_aligned_buffer += (alignment - mod);
}
}
runtime::AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment, Allocator* allocator)
: m_allocator(allocator)
, m_byte_size(byte_size)
......@@ -63,9 +79,16 @@ runtime::AlignedBuffer::AlignedBuffer(AlignedBuffer&& other)
runtime::AlignedBuffer::~AlignedBuffer()
{
if (m_allocated_buffer != nullptr)
{
if (m_allocator)
{
m_allocator->free(m_allocated_buffer);
}
else
{
free(m_allocated_buffer);
}
}
}
runtime::AlignedBuffer& runtime::AlignedBuffer::operator=(AlignedBuffer&& other)
......
......@@ -34,9 +34,12 @@ namespace ngraph
class ngraph::runtime::AlignedBuffer
{
public:
AlignedBuffer(size_t byte_size,
size_t alignment,
Allocator* allocator = get_default_allocator());
AlignedBuffer(size_t byte_size, size_t alignment);
// Allocator objects and the allocation interfaces are owned by the
// creators of AlignedBuffers. They need to ensure that the lifetime of
// allocator exceeds the lifetime of this AlignedBuffer.
AlignedBuffer(size_t byte_size, size_t alignment, Allocator* allocator);
AlignedBuffer();
~AlignedBuffer();
......@@ -50,11 +53,7 @@ private:
AlignedBuffer(const AlignedBuffer&) = delete;
AlignedBuffer& operator=(const AlignedBuffer&) = delete;
// Allocator objects and the allocation interfaces are owned by the
// creators of AlignedBuffers. They need to ensure that the lifetime of
// m_allocator exceeds the lifetime of this AlignedBuffer.
Allocator* m_allocator;
char* m_allocated_buffer;
char* m_aligned_buffer;
size_t m_byte_size;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment