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: |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 40 | /** Constructor |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 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) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 46 | : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr) |
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 | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 56 | _ptr = _mem.get(); |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 57 | |
| 58 | // Calculate alignment offset |
| 59 | if(alignment != 0) |
| 60 | { |
| 61 | void *aligned_ptr = _mem.get(); |
| 62 | support::cpp11::align(alignment, size, aligned_ptr, space); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 63 | _ptr = aligned_ptr; |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 64 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 67 | MemoryRegion(void *ptr, size_t size) |
| 68 | : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr) |
| 69 | { |
| 70 | if(size != 0) |
| 71 | { |
| 72 | _ptr = ptr; |
| 73 | } |
| 74 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 75 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 76 | MemoryRegion(const MemoryRegion &) = delete; |
| 77 | /** Default move constructor */ |
| 78 | MemoryRegion(MemoryRegion &&) = default; |
| 79 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 80 | MemoryRegion &operator=(const MemoryRegion &) = delete; |
| 81 | /** Default move assignment operator */ |
| 82 | MemoryRegion &operator=(MemoryRegion &&) = default; |
| 83 | |
| 84 | // Inherited methods overridden : |
| 85 | void *buffer() final |
| 86 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 87 | return _ptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 88 | } |
| 89 | void *buffer() const final |
| 90 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 91 | return _ptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 92 | } |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 93 | std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) final |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 94 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 95 | if(_ptr != nullptr && (offset < _size) && (_size - offset >= size)) |
| 96 | { |
| 97 | return support::cpp14::make_unique<MemoryRegion>(static_cast<uint8_t *>(_ptr) + offset, size); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | return nullptr; |
| 102 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | protected: |
| 106 | std::shared_ptr<uint8_t> _mem; |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 107 | void *_ptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 108 | }; |
| 109 | } // namespace arm_compute |
| 110 | #endif /* __ARM_COMPUTE_RUNTIME_MEMORY_REGION_H__ */ |