blob: 481b20d3753c8196a58c2f3aa311e2d0c7b9c4f7 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
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 Pinitas17b12302018-06-18 18:13:51 +010030#include "support/ToolchainSupport.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:
40 /** Default constructor
41 *
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)
46 : IMemoryRegion(size), _mem(nullptr), _alignment(alignment), _offset(0)
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 Pinitas17b12302018-06-18 18:13:51 +010056
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 Pinitas99d40952018-04-23 16:26:46 +010064 }
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 Pinitas17b12302018-06-18 18:13:51 +010078 return reinterpret_cast<void *>(_mem.get() + _offset);
Georgios Pinitas99d40952018-04-23 16:26:46 +010079 }
80 void *buffer() const final
81 {
Georgios Pinitas17b12302018-06-18 18:13:51 +010082 // FIXME (COMPMID-1088) : Remove handle() and _offset when done
83 return reinterpret_cast<void *>(_mem.get() + _offset);
Georgios Pinitas99d40952018-04-23 16:26:46 +010084 }
85 void **handle() final
86 {
87 return reinterpret_cast<void **>(&_mem);
88 }
89
90protected:
91 std::shared_ptr<uint8_t> _mem;
Georgios Pinitas17b12302018-06-18 18:13:51 +010092 size_t _alignment;
93 size_t _offset;
Georgios Pinitas99d40952018-04-23 16:26:46 +010094};
95} // namespace arm_compute
96#endif /* __ARM_COMPUTE_RUNTIME_MEMORY_REGION_H__ */