Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_RUNTIME_MEMORY_REGION_H |
| 25 | #define ARM_COMPUTE_RUNTIME_MEMORY_REGION_H |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 26 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/IMemoryRegion.h" |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 29 | |
| 30 | #include <cstddef> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | /** Memory region CPU implementation */ |
| 35 | class MemoryRegion final : public IMemoryRegion |
| 36 | { |
| 37 | public: |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 38 | /** Constructor |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 39 | * |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 40 | * @param[in] size Region size |
| 41 | * @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] | 42 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 43 | MemoryRegion(size_t size, size_t alignment = 0) : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 44 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | if (size != 0) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 46 | { |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 47 | // Allocate backing memory |
| 48 | size_t space = size + alignment; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | _mem = std::shared_ptr<uint8_t>(new uint8_t[space](), [](uint8_t *ptr) { delete[] ptr; }); |
| 50 | _ptr = _mem.get(); |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 51 | |
| 52 | // Calculate alignment offset |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | if (alignment != 0) |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 54 | { |
| 55 | void *aligned_ptr = _mem.get(); |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 56 | std::align(alignment, size, aligned_ptr, space); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 57 | _ptr = aligned_ptr; |
Georgios Pinitas | 17b1230 | 2018-06-18 18:13:51 +0100 | [diff] [blame] | 58 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 61 | MemoryRegion(void *ptr, size_t size) : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 62 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | if (size != 0) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 64 | { |
| 65 | _ptr = ptr; |
| 66 | } |
| 67 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 68 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 69 | MemoryRegion(const MemoryRegion &) = delete; |
| 70 | /** Default move constructor */ |
| 71 | MemoryRegion(MemoryRegion &&) = default; |
| 72 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 73 | MemoryRegion &operator=(const MemoryRegion &) = delete; |
| 74 | /** Default move assignment operator */ |
| 75 | MemoryRegion &operator=(MemoryRegion &&) = default; |
| 76 | |
| 77 | // Inherited methods overridden : |
| 78 | void *buffer() final |
| 79 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 80 | return _ptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 81 | } |
Georgios Pinitas | 74ef1db | 2020-01-20 17:01:15 +0000 | [diff] [blame] | 82 | const void *buffer() const final |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 83 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 84 | return _ptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 85 | } |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 86 | 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] | 87 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | if (_ptr != nullptr && (offset < _size) && (_size - offset >= size)) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 89 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 90 | return std::make_unique<MemoryRegion>(static_cast<uint8_t *>(_ptr) + offset, size); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
| 94 | return nullptr; |
| 95 | } |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | protected: |
| 99 | std::shared_ptr<uint8_t> _mem; |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 100 | void *_ptr; |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 101 | }; |
| 102 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 103 | #endif /* ARM_COMPUTE_RUNTIME_MEMORY_REGION_H */ |