Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_RUNTIME_MEMORY_REGION_H__ |
| 25 | #define __ARM_COMPUTE_RUNTIME_MEMORY_REGION_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IMemoryRegion.h" |
| 28 | |
| 29 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 30 | #include "support/ToolchainSupport.h" |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 31 | |
| 32 | #include <cstddef> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | /** Memory region CPU implementation */ |
| 37 | class MemoryRegion final : public IMemoryRegion |
| 38 | { |
| 39 | public: |
| 40 | /** Default constructor |
| 41 | * |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 42 | * @param[in] size Region size |
| 43 | * @param[in] alignment Alignment in bytes of the base pointer. Defaults to 0 |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 44 | */ |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 45 | MemoryRegion(size_t size, size_t alignment = 0) |
| 46 | : IMemoryRegion(size), _mem(nullptr), _alignment(alignment), _offset(0) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 47 | { |
| 48 | if(size != 0) |
| 49 | { |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 50 | // Allocate backing memory |
| 51 | size_t space = size + alignment; |
| 52 | _mem = std::shared_ptr<uint8_t>(new uint8_t[space](), [](uint8_t *ptr) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 53 | { |
| 54 | delete[] ptr; |
| 55 | }); |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 56 | |
| 57 | // Calculate alignment offset |
| 58 | if(alignment != 0) |
| 59 | { |
| 60 | void *aligned_ptr = _mem.get(); |
| 61 | support::cpp11::align(alignment, size, aligned_ptr, space); |
| 62 | _offset = reinterpret_cast<uintptr_t>(aligned_ptr) - reinterpret_cast<uintptr_t>(_mem.get()); |
| 63 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 67 | MemoryRegion(const MemoryRegion &) = delete; |
| 68 | /** Default move constructor */ |
| 69 | MemoryRegion(MemoryRegion &&) = default; |
| 70 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 71 | MemoryRegion &operator=(const MemoryRegion &) = delete; |
| 72 | /** Default move assignment operator */ |
| 73 | MemoryRegion &operator=(MemoryRegion &&) = default; |
| 74 | |
| 75 | // Inherited methods overridden : |
| 76 | void *buffer() final |
| 77 | { |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 78 | return reinterpret_cast<void *>(_mem.get() + _offset); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 79 | } |
| 80 | void *buffer() const final |
| 81 | { |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 82 | // FIXME (COMPMID-1088) : Remove handle() and _offset when done |
| 83 | return reinterpret_cast<void *>(_mem.get() + _offset); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 84 | } |
| 85 | void **handle() final |
| 86 | { |
| 87 | return reinterpret_cast<void **>(&_mem); |
| 88 | } |
| 89 | |
| 90 | protected: |
| 91 | std::shared_ptr<uint8_t> _mem; |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 92 | size_t _alignment; |
| 93 | size_t _offset; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 94 | }; |
| 95 | } // namespace arm_compute |
| 96 | #endif /* __ARM_COMPUTE_RUNTIME_MEMORY_REGION_H__ */ |