COMPMID-1023: Import memory for OpenCL

Change-Id: I201bc00a1261814737e6b6878ecfe9904bae0cc1
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/128212
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/src/runtime/Allocator.cpp b/src/runtime/Allocator.cpp
index 50b0f0e..7f0e374 100644
--- a/src/runtime/Allocator.cpp
+++ b/src/runtime/Allocator.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -22,8 +22,10 @@
  * SOFTWARE.
  */
 #include "arm_compute/runtime/Allocator.h"
+#include "arm_compute/runtime/MemoryRegion.h"
 
 #include "arm_compute/core/Error.h"
+#include "support/ToolchainSupport.h"
 
 #include <cstddef>
 
@@ -39,3 +41,9 @@
 {
     ::operator delete(ptr);
 }
+
+std::unique_ptr<IMemoryRegion> Allocator::make_region(size_t size, size_t alignment)
+{
+    ARM_COMPUTE_UNUSED(alignment);
+    return arm_compute::support::cpp14::make_unique<MemoryRegion>(size);
+}
\ No newline at end of file
diff --git a/src/runtime/CL/CLBufferAllocator.cpp b/src/runtime/CL/CLBufferAllocator.cpp
index 9a5c13a..84789e7 100644
--- a/src/runtime/CL/CLBufferAllocator.cpp
+++ b/src/runtime/CL/CLBufferAllocator.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -22,9 +22,11 @@
  * SOFTWARE.
  */
 #include "arm_compute/runtime/CL/CLBufferAllocator.h"
+#include "arm_compute/runtime/CL/CLMemoryRegion.h"
 
 #include "arm_compute/core/CL/OpenCL.h"
 #include "arm_compute/core/Error.h"
+#include "support/ToolchainSupport.h"
 
 #include <cstddef>
 
@@ -47,3 +49,9 @@
     ARM_COMPUTE_ERROR_ON(ptr == nullptr);
     clReleaseMemObject(static_cast<cl_mem>(ptr));
 }
+
+std::unique_ptr<IMemoryRegion> CLBufferAllocator::make_region(size_t size, size_t alignment)
+{
+    ARM_COMPUTE_UNUSED(alignment);
+    return arm_compute::support::cpp14::make_unique<CLBufferMemoryRegion>(_context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
+}
diff --git a/src/runtime/CL/CLMemory.cpp b/src/runtime/CL/CLMemory.cpp
new file mode 100644
index 0000000..534c4f9
--- /dev/null
+++ b/src/runtime/CL/CLMemory.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "arm_compute/runtime/CL/CLMemory.h"
+
+#include "arm_compute/core/Error.h"
+
+namespace arm_compute
+{
+CLMemory::CLMemory()
+    : _region(nullptr), _region_owned(nullptr)
+{
+    create_empty_region();
+}
+
+CLMemory::CLMemory(std::shared_ptr<ICLMemoryRegion> memory)
+    : _region(nullptr), _region_owned(std::move(memory))
+{
+    if(_region_owned == nullptr)
+    {
+        create_empty_region();
+    }
+    _region = _region_owned.get();
+}
+
+CLMemory::CLMemory(ICLMemoryRegion *memory)
+    : _region(memory), _region_owned(nullptr)
+{
+    _region = memory;
+}
+
+ICLMemoryRegion *CLMemory::region()
+{
+    return _region;
+}
+
+ICLMemoryRegion *CLMemory::region() const
+{
+    return _region;
+}
+
+void CLMemory::create_empty_region()
+{
+    _region_owned = std::make_shared<CLBufferMemoryRegion>(cl::Context::getDefault(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 0);
+    _region       = _region_owned.get();
+}
+} // namespace arm_compute
\ No newline at end of file
diff --git a/src/runtime/CL/CLMemoryRegion.cpp b/src/runtime/CL/CLMemoryRegion.cpp
new file mode 100644
index 0000000..15fd7f3
--- /dev/null
+++ b/src/runtime/CL/CLMemoryRegion.cpp
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "arm_compute/runtime/CL/CLMemoryRegion.h"
+
+#include "arm_compute/core/Error.h"
+#include "arm_compute/runtime/CL/CLScheduler.h"
+
+namespace arm_compute
+{
+ICLMemoryRegion::ICLMemoryRegion(cl::Context ctx, size_t size)
+    : IMemoryRegion(size), _ctx(std::move(ctx)), _mapping(nullptr), _mem()
+{
+}
+
+const cl::Buffer &ICLMemoryRegion::cl_data() const
+{
+    return _mem;
+}
+
+void *ICLMemoryRegion::buffer()
+{
+    return _mapping;
+}
+
+void *ICLMemoryRegion::buffer() const
+{
+    return _mapping;
+}
+
+void **ICLMemoryRegion::handle()
+{
+    return reinterpret_cast<void **>(&_mem);
+}
+
+CLBufferMemoryRegion::CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size)
+    : ICLMemoryRegion(std::move(ctx), size)
+{
+    if(_size != 0)
+    {
+        _mem = cl::Buffer(_ctx, flags, _size);
+    }
+}
+
+void *CLBufferMemoryRegion::ptr()
+{
+    return nullptr;
+}
+
+void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking)
+{
+    ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
+    _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size);
+    return _mapping;
+}
+
+void CLBufferMemoryRegion::unmap(cl::CommandQueue &q)
+{
+    ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
+    q.enqueueUnmapMemObject(_mem, _mapping);
+    _mapping = nullptr;
+}
+
+ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
+    : ICLMemoryRegion(std::move(ctx), size), _ptr(nullptr)
+{
+    if(size != 0)
+    {
+        _ptr = clSVMAlloc(_ctx.get(), flags, size, alignment);
+        if(_ptr != nullptr)
+        {
+            _mem = cl::Buffer(_ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr);
+        }
+    }
+}
+
+ICLSVMMemoryRegion::~ICLSVMMemoryRegion()
+{
+    if(_ptr != nullptr)
+    {
+        clFinish(CLScheduler::get().queue().get());
+        _mem = cl::Buffer();
+        clSVMFree(_ctx.get(), _ptr);
+    }
+}
+
+void *ICLSVMMemoryRegion::ptr()
+{
+    return _ptr;
+}
+
+CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
+    : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment)
+{
+}
+
+void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
+{
+    ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
+    clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr);
+    _mapping = _ptr;
+    return _mapping;
+}
+
+void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q)
+{
+    ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
+    clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr);
+    _mapping = nullptr;
+}
+
+CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
+    : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment)
+{
+}
+
+void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
+{
+    if(blocking)
+    {
+        clFinish(q.get());
+    }
+    _mapping = _ptr;
+    return _mapping;
+}
+
+void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q)
+{
+    ARM_COMPUTE_UNUSED(q);
+    _mapping = nullptr;
+}
+} // namespace arm_compute
\ No newline at end of file
diff --git a/src/runtime/CL/CLTensor.cpp b/src/runtime/CL/CLTensor.cpp
index bc513d1..dd27738 100644
--- a/src/runtime/CL/CLTensor.cpp
+++ b/src/runtime/CL/CLTensor.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2017 ARM Limited.
+ * Copyright (c) 2016-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -47,7 +47,7 @@
     return _allocator.cl_data();
 }
 
-ITensorAllocator *CLTensor::allocator()
+CLTensorAllocator *CLTensor::allocator()
 {
     return &_allocator;
 }
diff --git a/src/runtime/CL/CLTensorAllocator.cpp b/src/runtime/CL/CLTensorAllocator.cpp
index c5524b1..54e7c5b 100644
--- a/src/runtime/CL/CLTensorAllocator.cpp
+++ b/src/runtime/CL/CLTensorAllocator.cpp
@@ -30,67 +30,57 @@
 
 using namespace arm_compute;
 
-CLTensorAllocator::CLTensorAllocator(CLTensor *owner)
-    : _associated_memory_group(nullptr), _buffer(), _mapping(nullptr), _owner(owner), _svm_memory()
+namespace
 {
-}
+std::shared_ptr<arm_compute::ICLMemoryRegion> allocate_region(cl::Context context, size_t size, cl_uint alignment)
+{
+    // Try fine-grain SVM
+    std::shared_ptr<ICLMemoryRegion> region = std::make_shared<CLFineSVMMemoryRegion>(context, CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, size, alignment);
 
-CLTensorAllocator::~CLTensorAllocator()
+    // Try coarse-grain SVM in case of failure
+    if(region != nullptr && region->ptr() == nullptr)
+    {
+        region = std::make_shared<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment);
+    }
+    // Try legacy buffer memory in case of failure
+    if(region != nullptr && region->ptr() == nullptr)
+    {
+        region = std::make_shared<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
+    }
+    return region;
+}
+} // namespace
+
+CLTensorAllocator::CLTensorAllocator(CLTensor *owner)
+    : _associated_memory_group(nullptr), _memory(), _owner(owner)
 {
-    _buffer = cl::Buffer();
 }
 
 uint8_t *CLTensorAllocator::data()
 {
-    return _mapping;
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
 }
 
 const cl::Buffer &CLTensorAllocator::cl_data() const
 {
-    return _buffer;
-}
-
-void *SVMMemory::allocate(cl_context context, size_t size, cl_svm_mem_flags flags, cl_uint alignment)
-{
-    ARM_COMPUTE_ERROR_ON_NULLPTR(context);
-    ARM_COMPUTE_ERROR_ON(size == 0);
-    ARM_COMPUTE_ERROR_ON(_ptr != nullptr);
-    ARM_COMPUTE_ERROR_ON(size > CL_DEVICE_MAX_MEM_ALLOC_SIZE);
-    _ptr = clSVMAlloc(context, flags, size, alignment);
-    if(_ptr != nullptr)
-    {
-        _size       = size;
-        _fine_grain = static_cast<bool>(flags & CL_MEM_SVM_FINE_GRAIN_BUFFER);
-    }
-    return _ptr;
-}
-void *CLTensorAllocator::svm_ptr()
-{
-    return _svm_memory.ptr();
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    return _memory.region()->cl_data();
 }
 
 void CLTensorAllocator::allocate()
 {
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+
     if(_associated_memory_group == nullptr)
     {
-        ARM_COMPUTE_ERROR_ON(_buffer.get() != nullptr);
-        if(_svm_memory.allocate(CLScheduler::get().context()(), CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, info().total_size(), 0) == nullptr)
-        {
-            // try at coarse grain svm memory
-            _svm_memory.allocate(CLScheduler::get().context()(), CL_MEM_READ_WRITE, info().total_size(), 0);
-        }
-        if(_svm_memory.ptr() != nullptr)
-        {
-            _buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, info().total_size(), _svm_memory.ptr());
-        }
-        else
-        {
-            _buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, info().total_size());
-        }
+        ARM_COMPUTE_ERROR_ON(_memory.region()->cl_data().get() != nullptr);
+        _memory = CLMemory(allocate_region(CLScheduler::get().context(), info().total_size(), 0));
     }
     else
     {
-        _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(&_buffer()), info().total_size());
+        _associated_memory_group->finalize_memory(_owner, _memory.region()->handle(), info().total_size());
+        _memory.region()->set_size(info().total_size());
     }
     info().set_is_resizable(false);
 }
@@ -99,80 +89,55 @@
 {
     if(_associated_memory_group == nullptr)
     {
-        _buffer = cl::Buffer();
-        if(_svm_memory.ptr() != nullptr)
-        {
-            clSVMFree(CLScheduler::get().context()(), _svm_memory.ptr());
-        }
+        _memory = CLMemory();
         info().set_is_resizable(true);
     }
 }
 
+arm_compute::Status CLTensorAllocator::import_memory(CLMemory memory)
+{
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    ARM_COMPUTE_RETURN_ERROR_ON(memory.region()->cl_data().get() == nullptr);
+    ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
+    _memory = memory;
+    info().set_is_resizable(false);
+
+    return Status{};
+}
+
 void CLTensorAllocator::set_associated_memory_group(CLMemoryGroup *associated_memory_group)
 {
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
     ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
     ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
-    ARM_COMPUTE_ERROR_ON(_buffer.get() != nullptr);
+    ARM_COMPUTE_ERROR_ON(_memory.region()->cl_data().get() != nullptr);
+    _memory                  = CLMemory(std::make_shared<CLBufferMemoryRegion>(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 0));
     _associated_memory_group = associated_memory_group;
 }
 
 uint8_t *CLTensorAllocator::lock()
 {
-    ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
-    _mapping = map(CLScheduler::get().queue(), true);
-    return _mapping;
+    return map(CLScheduler::get().queue(), true);
 }
 
 void CLTensorAllocator::unlock()
 {
-    ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
-    unmap(CLScheduler::get().queue(), _mapping);
-    _mapping = nullptr;
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer()));
 }
 
 uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking)
 {
-    const bool svm_mem        = _svm_memory.ptr() != nullptr;
-    const bool fine_grain_svm = _svm_memory.fine_grain();
-    if(!svm_mem)
-    {
-        ARM_COMPUTE_ERROR_ON(_buffer.get() == nullptr);
-        return static_cast<uint8_t *>(q.enqueueMapBuffer(_buffer, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, info().total_size()));
-    }
-    else if(!fine_grain_svm)
-    {
-        const cl_int ret = clEnqueueSVMMap(q(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _svm_memory.ptr(), _svm_memory.size(), 0, nullptr, nullptr);
-        ARM_COMPUTE_ERROR_ON(ret != CL_SUCCESS);
-        if(ret == CL_SUCCESS)
-        {
-            return reinterpret_cast<uint8_t *>(_svm_memory.ptr());
-        }
-        else
-        {
-            return nullptr;
-        }
-    }
-    else
-    {
-        if(blocking)
-        {
-            clFinish(q());
-        }
-        return reinterpret_cast<uint8_t *>(_svm_memory.ptr());
-    }
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
+    _memory.region()->map(q, blocking);
+    return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
 }
 
 void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping)
 {
-    const bool svm_mem        = _svm_memory.ptr() != nullptr;
-    const bool fine_grain_svm = _svm_memory.fine_grain();
-    if(!svm_mem)
-    {
-        ARM_COMPUTE_ERROR_ON(_buffer.get() == nullptr);
-        q.enqueueUnmapMemObject(_buffer, mapping);
-    }
-    else if(!fine_grain_svm)
-    {
-        clEnqueueSVMUnmap(q(), _svm_memory.ptr(), 0, nullptr, nullptr);
-    }
+    ARM_COMPUTE_UNUSED(mapping);
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
+    _memory.region()->unmap(q);
 }
diff --git a/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp b/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp
index d8f6867..cdd12c3 100644
--- a/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp
+++ b/src/runtime/GLES_COMPUTE/GCBufferAllocator.cpp
@@ -48,4 +48,10 @@
     auto *gl_buffer = reinterpret_cast<GLBufferWrapper *>(ptr);
     delete gl_buffer;
 }
+
+std::unique_ptr<IMemoryRegion> GCBufferAllocator::make_region(size_t size, size_t alignment)
+{
+    ARM_COMPUTE_UNUSED(size, alignment);
+    return nullptr;
+}
 } // namespace arm_compute
diff --git a/src/runtime/Memory.cpp b/src/runtime/Memory.cpp
index 35d0c82..15bbb17 100644
--- a/src/runtime/Memory.cpp
+++ b/src/runtime/Memory.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -23,40 +23,45 @@
  */
 #include "arm_compute/runtime/Memory.h"
 
-#include "arm_compute/core/Error.h"
+#include "arm_compute/runtime/MemoryRegion.h"
 
-using namespace arm_compute;
-
+namespace arm_compute
+{
 Memory::Memory()
-    : _memory(nullptr), _memory_owned(nullptr)
+    : _region(nullptr), _region_owned(nullptr)
 {
+    create_empty_region();
 }
 
-Memory::Memory(std::shared_ptr<uint8_t> memory)
-    : _memory(nullptr), _memory_owned(std::move(memory))
+Memory::Memory(std::shared_ptr<IMemoryRegion> memory)
+    : _region(nullptr), _region_owned(std::move(memory))
 {
-    ARM_COMPUTE_ERROR_ON(_memory_owned.get() == nullptr);
-    _memory = _memory_owned.get();
+    if(_region_owned == nullptr)
+    {
+        create_empty_region();
+    }
+    _region = _region_owned.get();
 }
 
-Memory::Memory(uint8_t *memory)
-    : _memory(memory), _memory_owned(nullptr)
+Memory::Memory(IMemoryRegion *memory)
+    : _region(memory), _region_owned(nullptr)
 {
-    ARM_COMPUTE_ERROR_ON(memory == nullptr);
+    _region = memory;
 }
 
-uint8_t *Memory::buffer()
+IMemoryRegion *Memory::region()
 {
-    return _memory;
+    return _region;
 }
 
-uint8_t *Memory::buffer() const
+IMemoryRegion *Memory::region() const
 {
-    return _memory;
+    return _region;
 }
 
-uint8_t **Memory::handle()
+void Memory::create_empty_region()
 {
-    ARM_COMPUTE_ERROR_ON(_memory_owned.get() != nullptr);
-    return &_memory;
-}
\ No newline at end of file
+    _region_owned = std::make_shared<MemoryRegion>(0);
+    _region       = _region_owned.get();
+}
+} // namespace arm_compute
diff --git a/src/runtime/TensorAllocator.cpp b/src/runtime/TensorAllocator.cpp
index a0d41b2..993a95b 100644
--- a/src/runtime/TensorAllocator.cpp
+++ b/src/runtime/TensorAllocator.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2017 ARM Limited.
+ * Copyright (c) 2016-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -27,6 +27,7 @@
 #include "arm_compute/core/Error.h"
 #include "arm_compute/core/TensorInfo.h"
 #include "arm_compute/runtime/MemoryGroup.h"
+#include "arm_compute/runtime/MemoryRegion.h"
 #include "support/ToolchainSupport.h"
 
 #include <cstddef>
@@ -114,7 +115,7 @@
     ARM_COMPUTE_UNUSED(validate_subtensor_shape);
 
     // Copy pointer to buffer
-    _memory = Memory(allocator._memory.buffer());
+    _memory = Memory(allocator._memory.region());
 
     // Init tensor info with new dimensions
     size_t total_size = parent_info.offset_element_in_bytes(coords) + sub_info.total_size() - sub_info.offset_first_element_in_bytes();
@@ -126,22 +127,23 @@
 
 uint8_t *TensorAllocator::data() const
 {
-    return _memory.buffer();
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
 }
 
 void TensorAllocator::allocate()
 {
-    ARM_COMPUTE_ERROR_ON(_memory.buffer() != nullptr);
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
+
     if(_associated_memory_group == nullptr)
     {
-        _memory = Memory(std::shared_ptr<uint8_t>(new uint8_t[info().total_size()](), [](uint8_t *ptr)
-        {
-            delete[] ptr;
-        }));
+        _memory = Memory(std::make_shared<MemoryRegion>(info().total_size()));
     }
     else
     {
-        _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(_memory.handle()), info().total_size());
+        _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(_memory.region()->handle()), info().total_size());
+        _memory.region()->set_size(info().total_size());
     }
     info().set_is_resizable(false);
 }
@@ -154,7 +156,8 @@
 
 arm_compute::Status TensorAllocator::import_memory(Memory memory)
 {
-    ARM_COMPUTE_RETURN_ERROR_ON(memory.buffer() == nullptr);
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    ARM_COMPUTE_RETURN_ERROR_ON(memory.region()->buffer() == nullptr);
     ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
     _memory = memory;
     info().set_is_resizable(false);
@@ -164,15 +167,17 @@
 
 void TensorAllocator::set_associated_memory_group(MemoryGroup *associated_memory_group)
 {
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
     ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
     ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
-    ARM_COMPUTE_ERROR_ON(_memory.buffer() != nullptr);
+    ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
     _associated_memory_group = associated_memory_group;
 }
 
 uint8_t *TensorAllocator::lock()
 {
-    return _memory.buffer();
+    ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
+    return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
 }
 
 void TensorAllocator::unlock()