blob: 6408deceaa06ea9781998eb23b6e0e6573a08ae6 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
30
31#include <cstddef>
32
33namespace arm_compute
34{
35/** Memory region CPU implementation */
36class MemoryRegion final : public IMemoryRegion
37{
38public:
Georgios Pinitasdf310362018-11-14 13:16:56 +000039 /** Constructor
Georgios Pinitas99d40952018-04-23 16:26:46 +010040 *
Georgios Pinitas17b12302018-06-18 18:13:51 +010041 * @param[in] size Region size
42 * @param[in] alignment Alignment in bytes of the base pointer. Defaults to 0
Georgios Pinitas99d40952018-04-23 16:26:46 +010043 */
Georgios Pinitas17b12302018-06-18 18:13:51 +010044 MemoryRegion(size_t size, size_t alignment = 0)
Georgios Pinitasdf310362018-11-14 13:16:56 +000045 : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr)
Georgios Pinitas99d40952018-04-23 16:26:46 +010046 {
47 if(size != 0)
48 {
Georgios Pinitas17b12302018-06-18 18:13:51 +010049 // Allocate backing memory
50 size_t space = size + alignment;
51 _mem = std::shared_ptr<uint8_t>(new uint8_t[space](), [](uint8_t *ptr)
Georgios Pinitas99d40952018-04-23 16:26:46 +010052 {
53 delete[] ptr;
54 });
Georgios Pinitasdf310362018-11-14 13:16:56 +000055 _ptr = _mem.get();
Georgios Pinitas17b12302018-06-18 18:13:51 +010056
57 // Calculate alignment offset
58 if(alignment != 0)
59 {
60 void *aligned_ptr = _mem.get();
Georgios Pinitas40f51a62020-11-21 03:04:18 +000061 std::align(alignment, size, aligned_ptr, space);
Georgios Pinitasdf310362018-11-14 13:16:56 +000062 _ptr = aligned_ptr;
Georgios Pinitas17b12302018-06-18 18:13:51 +010063 }
Georgios Pinitas99d40952018-04-23 16:26:46 +010064 }
65 }
Georgios Pinitasdf310362018-11-14 13:16:56 +000066 MemoryRegion(void *ptr, size_t size)
67 : IMemoryRegion(size), _mem(nullptr), _ptr(nullptr)
68 {
69 if(size != 0)
70 {
71 _ptr = ptr;
72 }
73 }
Georgios Pinitas99d40952018-04-23 16:26:46 +010074 /** Prevent instances of this class from being copied (As this class contains pointers) */
75 MemoryRegion(const MemoryRegion &) = delete;
76 /** Default move constructor */
77 MemoryRegion(MemoryRegion &&) = default;
78 /** Prevent instances of this class from being copied (As this class contains pointers) */
79 MemoryRegion &operator=(const MemoryRegion &) = delete;
80 /** Default move assignment operator */
81 MemoryRegion &operator=(MemoryRegion &&) = default;
82
83 // Inherited methods overridden :
84 void *buffer() final
85 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000086 return _ptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +010087 }
Georgios Pinitas74ef1db2020-01-20 17:01:15 +000088 const void *buffer() const final
Georgios Pinitas99d40952018-04-23 16:26:46 +010089 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000090 return _ptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +010091 }
Georgios Pinitasdf310362018-11-14 13:16:56 +000092 std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) final
Georgios Pinitas99d40952018-04-23 16:26:46 +010093 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000094 if(_ptr != nullptr && (offset < _size) && (_size - offset >= size))
95 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000096 return std::make_unique<MemoryRegion>(static_cast<uint8_t *>(_ptr) + offset, size);
Georgios Pinitasdf310362018-11-14 13:16:56 +000097 }
98 else
99 {
100 return nullptr;
101 }
Georgios Pinitas99d40952018-04-23 16:26:46 +0100102 }
103
104protected:
105 std::shared_ptr<uint8_t> _mem;
Georgios Pinitasdf310362018-11-14 13:16:56 +0000106 void *_ptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +0100107};
108} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000109#endif /* ARM_COMPUTE_RUNTIME_MEMORY_REGION_H */