blob: ee234795cf3aff7624bbecb3111f4fdee8698107 [file] [log] [blame]
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00001/*
2 * Copyright (c) 2021 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 SRC_COMMON_ICONTEXT_H
25#define SRC_COMMON_ICONTEXT_H
26
27#include "src/common/Types.h"
28#include "src/common/utils/Log.h"
29#include "src/common/utils/Object.h"
30
31#include <atomic>
32
33struct AclContext_
34{
35 arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Context, nullptr };
36
37protected:
38 AclContext_() = default;
39 ~AclContext_() = default;
40};
41
42namespace arm_compute
43{
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000044// Forward declarations
45class ITensorV2;
46
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000047/**< Context interface */
48class IContext : public AclContext_
49{
50public:
51 IContext(Target target)
52 : AclContext_(), _target(target), _refcount(0)
53 {
54 }
55 /** Virtual Destructor */
56 virtual ~IContext()
57 {
58 header.type = detail::ObjectType::Invalid;
59 };
60 /** Target type accessor
61 *
62 * @return Target that the context is associated with
63 */
64 Target type() const
65 {
66 return _target;
67 }
68 /** Increment context refcount */
69 void inc_ref() const
70 {
71 ++_refcount;
72 }
73 /** Decrement context refcount */
74 void dec_ref() const
75 {
76 --_refcount;
77 }
78 /** Reference counter accessor
79 *
80 * @return The number of references pointing to this object
81 */
82 int refcount() const
83 {
84 return _refcount;
85 }
86 /** Checks if an object is valid
87 *
88 * @return True if sucessful otherwise false
89 */
90 bool is_valid() const
91 {
92 return header.type == detail::ObjectType::Context;
93 }
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000094 /** Create a tensor object
95 *
96 * @param[in] desc Descriptor to use
97 * @param[in] allocate Flag to allocate tensor
98 *
99 * @return A pointer to the created tensor object
100 */
101 virtual ITensorV2 *create_tensor(const AclTensorDescriptor &desc, bool allocate) = 0;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000102
103private:
104 Target _target; /**< Target type of context */
105 mutable std::atomic<int> _refcount; /**< Reference counter */
106};
107
108/** Extract internal representation of a Context
109 *
110 * @param[in] ctx Opaque context pointer
111 *
112 * @return The internal representation as an IContext
113 */
114inline IContext *get_internal(AclContext ctx)
115{
116 return static_cast<IContext *>(ctx);
117}
118
119namespace detail
120{
121/** Check if an internal context is valid
122 *
123 * @param[in] ctx Internal context to check
124 *
125 * @return A status code
126 */
127inline StatusCode validate_internal_context(const IContext *ctx)
128{
129 if(ctx == nullptr || !ctx->is_valid())
130 {
131 ARM_COMPUTE_LOG_ERROR_ACL("Invalid context object");
132 return StatusCode::InvalidArgument;
133 }
134 return StatusCode::Success;
135}
136} // namespace detail
137} // namespace arm_compute
138#endif /* SRC_COMMON_ICONTEXT_H */