blob: ef8c83572778c02e13dd44bb1737498b900be055 [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
Georgios Pinitasda953f22019-04-02 17:27:03 +01002 * Copyright (c) 2017-2019 ARM Limited.
Georgios Pinitasbaf174e2017-09-08 19:47:30 +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_IMEMORYGROUP_H
25#define ARM_COMPUTE_IMEMORYGROUP_H
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010026
Georgios Pinitas26014cf2019-09-09 19:00:57 +010027#include "arm_compute/runtime/IMemory.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010028#include "arm_compute/runtime/Types.h"
29
30namespace arm_compute
31{
Georgios Pinitas26014cf2019-09-09 19:00:57 +010032// Forward declarations
33class IMemoryGroup;
34class IMemoryManageable;
35
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010036/** Memory group interface */
37class IMemoryGroup
38{
39public:
40 /** Default virtual destructor */
41 virtual ~IMemoryGroup() = default;
Georgios Pinitas26014cf2019-09-09 19:00:57 +010042 /** Sets a object to be managed by the given memory group
43 *
44 * @note Manager must not be finalized
45 *
46 * @param[in] obj Object to be managed
47 */
48 virtual void manage(IMemoryManageable *obj) = 0;
49 /** Finalizes memory for a given object
50 *
51 * @note Manager must not be finalized
52 *
53 * @param[in, out] obj Object to request memory for
54 * @param[in, out] obj_memory Object's memory handling interface which can be used to alter the underlying memory
55 * that is used by the object.
56 * @param[in] size Size of memory to allocate
57 * @param[in] alignment (Optional) Alignment to use
58 */
59 virtual void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment) = 0;
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010060 /** Acquires backing memory for the whole group */
61 virtual void acquire() = 0;
62 /** Releases backing memory of the whole group */
63 virtual void release() = 0;
64 /** Gets the memory mapping of the group */
65 virtual MemoryMappings &mappings() = 0;
66};
Georgios Pinitasda953f22019-04-02 17:27:03 +010067
Georgios Pinitas26014cf2019-09-09 19:00:57 +010068/** Interface of an object than can be memory managed */
69class IMemoryManageable
70{
71public:
72 /** Default virtual destructor */
73 virtual ~IMemoryManageable() = default;
74 /** Associates a memory managable object with the memory group that manages it
75 *
76 * @param[in] memory_group Memory group that manages the object.
77 */
78 virtual void associate_memory_group(IMemoryGroup *memory_group) = 0;
79};
80
Georgios Pinitasda953f22019-04-02 17:27:03 +010081/** Memory group resources scope handling class */
82class MemoryGroupResourceScope
83{
84public:
85 /** Constructor
86 *
87 * @param[in] memory_group Memory group to handle
88 */
89 explicit MemoryGroupResourceScope(IMemoryGroup &memory_group)
90 : _memory_group(memory_group)
91 {
92 _memory_group.acquire();
93 }
Georgios Pinitasda953f22019-04-02 17:27:03 +010094 /** Destructor */
95 ~MemoryGroupResourceScope()
96 {
97 _memory_group.release();
98 }
99
100private:
101 IMemoryGroup &_memory_group;
102};
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100103} // arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000104#endif /*ARM_COMPUTE_IMEMORYGROUP_H */