blob: 1b645be4a2bc71603648847e0debd156d038f07a [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
Georgios Pinitas74ef1db2020-01-20 17:01:15 +00002 * Copyright (c) 2018-2020 ARM Limited.
Georgios Pinitas99d40952018-04-23 16:26:46 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_RUNTIME_MEMORY_REGION_H
25#define ARM_COMPUTE_RUNTIME_MEMORY_REGION_H
Georgios Pinitas99d40952018-04-23 16:26:46 +010026
27#include "arm_compute/runtime/IMemoryRegion.h"
28
29#include "arm_compute/core/Error.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000030#include "support/MemorySupport.h"
Georgios Pinitas99d40952018-04-23 16:26:46 +010031
32#include <cstddef>
33
34namespace arm_compute
35{
36/** Memory region CPU implementation */
37class MemoryRegion final : public IMemoryRegion
38{
39public:
Georgios Pinitasdf310362018-11-14 13:16:56 +000040 /** Constructor
Georgios Pinitas99d40952018-04-23 16:26:46 +010041 *
Georgios Pinitas17b12302018-06-18 18:13:51 +010042 * @param[in] size Region size
43 * @param[in] alignment Alignment in bytes of the base pointer. Defaults to 0
Georgios Pinitas99d40952018-04-23 16:26:46 +010044 */
Georgios Pinitas17b12302018-06-18 18:13:51 +010045 MemoryRegion(size_t size, size_t alignment = 0)
Georgios Pinitasdf310362018-11-14 13:16:56 +000046 : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr)
Georgios Pinitas99d40952018-04-23 16:26:46 +010047 {
48 if(size != 0)
49 {
Georgios Pinitas17b12302018-06-18 18:13:51 +010050 // Allocate backing memory
51 size_t space = size + alignment;
52 _mem = std::shared_ptr<uint8_t>(new uint8_t[space](), [](uint8_t *ptr)
Georgios Pinitas99d40952018-04-23 16:26:46 +010053 {
54 delete[] ptr;
55 });
Georgios Pinitasdf310362018-11-14 13:16:56 +000056 _ptr = _mem.get();
Georgios Pinitas17b12302018-06-18 18:13:51 +010057
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 Pinitasdf310362018-11-14 13:16:56 +000063 _ptr = aligned_ptr;
Georgios Pinitas17b12302018-06-18 18:13:51 +010064 }
Georgios Pinitas99d40952018-04-23 16:26:46 +010065 }
66 }
Georgios Pinitasdf310362018-11-14 13:16:56 +000067 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 Pinitas99d40952018-04-23 16:26:46 +010075 /** 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 Pinitasdf310362018-11-14 13:16:56 +000087 return _ptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +010088 }
Georgios Pinitas74ef1db2020-01-20 17:01:15 +000089 const void *buffer() const final
Georgios Pinitas99d40952018-04-23 16:26:46 +010090 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000091 return _ptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +010092 }
Georgios Pinitasdf310362018-11-14 13:16:56 +000093 std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) final
Georgios Pinitas99d40952018-04-23 16:26:46 +010094 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000095 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 Pinitas99d40952018-04-23 16:26:46 +0100103 }
104
105protected:
106 std::shared_ptr<uint8_t> _mem;
Georgios Pinitasdf310362018-11-14 13:16:56 +0000107 void *_ptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100108};
109} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000110#endif /* ARM_COMPUTE_RUNTIME_MEMORY_REGION_H */