From 17b12307edeaf488cfdf0cc3fa00b8f08293c93e Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Mon, 18 Jun 2018 18:13:51 +0100 Subject: COMPMID-1293: Handle aligned allocations Change-Id: I6e642c8cd968240f883c327464519e57e5d0c3e3 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/136088 Tested-by: Jenkins Reviewed-by: Anthony Barbier --- arm_compute/runtime/MemoryRegion.h | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'arm_compute/runtime/MemoryRegion.h') diff --git a/arm_compute/runtime/MemoryRegion.h b/arm_compute/runtime/MemoryRegion.h index bf4e1719de..481b20d375 100644 --- a/arm_compute/runtime/MemoryRegion.h +++ b/arm_compute/runtime/MemoryRegion.h @@ -27,6 +27,7 @@ #include "arm_compute/runtime/IMemoryRegion.h" #include "arm_compute/core/Error.h" +#include "support/ToolchainSupport.h" #include @@ -38,18 +39,28 @@ class MemoryRegion final : public IMemoryRegion public: /** Default constructor * - * @param[in] size Region size + * @param[in] size Region size + * @param[in] alignment Alignment in bytes of the base pointer. Defaults to 0 */ - MemoryRegion(size_t size) - : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr) + MemoryRegion(size_t size, size_t alignment = 0) + : IMemoryRegion(size), _mem(nullptr), _alignment(alignment), _offset(0) { if(size != 0) { - _mem = std::shared_ptr(new uint8_t[size](), [](uint8_t *ptr) + // Allocate backing memory + size_t space = size + alignment; + _mem = std::shared_ptr(new uint8_t[space](), [](uint8_t *ptr) { delete[] ptr; }); - _ptr = _mem.get(); + + // Calculate alignment offset + if(alignment != 0) + { + void *aligned_ptr = _mem.get(); + support::cpp11::align(alignment, size, aligned_ptr, space); + _offset = reinterpret_cast(aligned_ptr) - reinterpret_cast(_mem.get()); + } } } /** Prevent instances of this class from being copied (As this class contains pointers) */ @@ -64,11 +75,12 @@ public: // Inherited methods overridden : void *buffer() final { - return _mem.get(); + return reinterpret_cast(_mem.get() + _offset); } void *buffer() const final { - return _mem.get(); + // FIXME (COMPMID-1088) : Remove handle() and _offset when done + return reinterpret_cast(_mem.get() + _offset); } void **handle() final { @@ -77,7 +89,8 @@ public: protected: std::shared_ptr _mem; - uint8_t *_ptr; + size_t _alignment; + size_t _offset; }; } // namespace arm_compute #endif /* __ARM_COMPUTE_RUNTIME_MEMORY_REGION_H__ */ -- cgit v1.2.1