blob: 450323b3abc981a8b5b3ab7b12b27f92be24be17 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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_TENSORALLOCATOR_H__
25#define __ARM_COMPUTE_TENSORALLOCATOR_H__
26
27#include "arm_compute/runtime/ITensorAllocator.h"
28
29#include <cstdint>
30#include <memory>
31#include <vector>
32
33namespace arm_compute
34{
35class Coordinates;
36class TensorInfo;
37
38/** Basic implementation of a CPU memory tensor allocator. */
39class TensorAllocator : public ITensorAllocator
40{
41public:
42 /** Default constructor. */
43 TensorAllocator();
44
45 /** Make ITensorAllocator's init methods available */
46 using ITensorAllocator::init;
47
48 /** Shares the same backing memory with another tensor allocator, while the tensor info might be different.
49 * In other words this can be used to create a sub-tensor from another tensor while sharing the same memory.
50 *
51 * @note TensorAllocator have to be of the same specialized type.
52 *
53 * @param[in] allocator The allocator that owns the backing memory to be shared. Ownership becomes shared afterwards.
54 * @param[in] coords The starting coordinates of the new tensor inside the parent tensor.
55 * @param[in] sub_info The new tensor information (e.g. shape etc)
56 */
57 void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo sub_info);
58
59 /** Returns the pointer to the allocated data. */
60 uint8_t *data() const;
61
62 /** Allocate size specified by TensorInfo of CPU memory.
63 *
64 * @note The tensor must not already be allocated when calling this function.
65 *
66 */
67 void allocate() override;
68
69 /** Free allocated CPU memory.
70 *
71 * @note The tensor must have been allocated when calling this function.
72 *
73 */
74 void free() override;
75
76protected:
77 /** No-op for CPU memory
78 *
79 * @return A pointer to the beginning of the tensor's allocation.
80 */
81 uint8_t *lock() override;
82
83 /** No-op for CPU memory. */
84 void unlock() override;
85
86private:
87 std::shared_ptr<std::vector<uint8_t>> _buffer; /**< CPU memory allocation. */
88};
89}
90#endif /* __ARM_COMPUTE_TENSORALLOCATOR_H__ */