From baf174e85ddb5399355281cd34d0f459d92124a7 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Fri, 8 Sep 2017 19:47:30 +0100 Subject: COMPMID-485: Memory Manager Change-Id: Ib421b7622838f050038cd81e7426bb1413a7d6e6 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/87376 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- src/runtime/TensorAllocator.cpp | 74 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 9 deletions(-) (limited to 'src/runtime/TensorAllocator.cpp') diff --git a/src/runtime/TensorAllocator.cpp b/src/runtime/TensorAllocator.cpp index 5c719c761a..272b9f5695 100644 --- a/src/runtime/TensorAllocator.cpp +++ b/src/runtime/TensorAllocator.cpp @@ -26,6 +26,7 @@ #include "arm_compute/core/Coordinates.h" #include "arm_compute/core/Error.h" #include "arm_compute/core/TensorInfo.h" +#include "arm_compute/runtime/MemoryGroup.h" #include @@ -63,11 +64,50 @@ bool validate_subtensor_shape(const TensorInfo &parent_info, const TensorInfo &c } } // namespace -TensorAllocator::TensorAllocator() - : _buffer(nullptr) +TensorAllocator::TensorAllocator(Tensor *owner) + : _associated_memory_group(nullptr), _buffer(nullptr), _owner(owner) { } +TensorAllocator::~TensorAllocator() +{ + if((_associated_memory_group == nullptr) && (_buffer != nullptr)) + { + delete[] _buffer; + _buffer = nullptr; + info().set_is_resizable(true); + } +} + +TensorAllocator::TensorAllocator(TensorAllocator &&o) noexcept + : ITensorAllocator(std::move(o)), + _associated_memory_group(o._associated_memory_group), + _buffer(o._buffer), + _owner(o._owner) +{ + o._associated_memory_group = nullptr; + o._buffer = nullptr; + o._owner = nullptr; +} + +TensorAllocator &TensorAllocator::operator=(TensorAllocator &&o) noexcept +{ + if(&o != this) + { + _associated_memory_group = o._associated_memory_group; + o._associated_memory_group = nullptr; + + _buffer = o._buffer; + o._buffer = nullptr; + + _owner = o._owner; + o._owner = nullptr; + + ITensorAllocator::operator=(std::move(o)); + } + return *this; +} + void TensorAllocator::init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo sub_info) { // Get parent info @@ -90,28 +130,44 @@ void TensorAllocator::init(const TensorAllocator &allocator, const Coordinates & uint8_t *TensorAllocator::data() const { - return (_buffer != nullptr) ? _buffer.get()->data() : nullptr; + return _buffer; } void TensorAllocator::allocate() { ARM_COMPUTE_ERROR_ON(_buffer != nullptr); - - _buffer = std::make_shared>(info().total_size()); + if(_associated_memory_group == nullptr) + { + _buffer = new uint8_t[info().total_size()](); + } + else + { + _associated_memory_group->finalize_memory(_owner, reinterpret_cast(&_buffer), info().total_size()); + } info().set_is_resizable(false); } void TensorAllocator::free() { - ARM_COMPUTE_ERROR_ON(_buffer == nullptr); + if((_associated_memory_group == nullptr) && (_buffer != nullptr)) + { + delete[] _buffer; + _buffer = nullptr; + info().set_is_resizable(true); + } +} - _buffer.reset(); - info().set_is_resizable(true); +void TensorAllocator::set_associated_memory_group(MemoryGroup *associated_memory_group) +{ + ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr); + ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr); + ARM_COMPUTE_ERROR_ON(_buffer != nullptr); + _associated_memory_group = associated_memory_group; } uint8_t *TensorAllocator::lock() { - return (_buffer != nullptr) ? _buffer.get()->data() : nullptr; + return _buffer; } void TensorAllocator::unlock() -- cgit v1.2.1