aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/runtime/IMemoryGroup.h
blob: 4736acb559e890bc45df039be0ff8d9d583d7d4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (c) 2017-2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef __ARM_COMPUTE_IMEMORYGROUP_H__
#define __ARM_COMPUTE_IMEMORYGROUP_H__

#include "arm_compute/runtime/IMemory.h"
#include "arm_compute/runtime/Types.h"

namespace arm_compute
{
// Forward declarations
class IMemoryGroup;
class IMemoryManageable;

/** Memory group interface */
class IMemoryGroup
{
public:
    /** Default virtual destructor */
    virtual ~IMemoryGroup() = default;
    /** Sets a object to be managed by the given memory group
     *
     * @note Manager must not be finalized
     *
     * @param[in] obj Object to be managed
     */
    virtual void manage(IMemoryManageable *obj) = 0;
    /** Finalizes memory for a given object
     *
     * @note Manager must not be finalized
     *
     * @param[in, out] obj        Object to request memory for
     * @param[in, out] obj_memory Object's memory handling interface which can be used to alter the underlying memory
     *                            that is used by the object.
     * @param[in]      size       Size of memory to allocate
     * @param[in]      alignment  (Optional) Alignment to use
     */
    virtual void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment) = 0;
    /** Acquires backing memory for the whole group */
    virtual void acquire() = 0;
    /** Releases backing memory of the whole group */
    virtual void release() = 0;
    /** Gets the memory mapping of the group */
    virtual MemoryMappings &mappings() = 0;
};

/** Interface of an object than can be memory managed */
class IMemoryManageable
{
public:
    /** Default virtual destructor */
    virtual ~IMemoryManageable() = default;
    /** Associates a memory managable object with the memory group that manages it
     *
     * @param[in] memory_group Memory group that manages the object.
     */
    virtual void associate_memory_group(IMemoryGroup *memory_group) = 0;
};

/** Memory group resources scope handling class */
class MemoryGroupResourceScope
{
public:
    /** Constructor
     *
     * @param[in] memory_group Memory group to handle
     */
    explicit MemoryGroupResourceScope(IMemoryGroup &memory_group)
        : _memory_group(memory_group)
    {
        _memory_group.acquire();
    }
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    MemoryGroupResourceScope(const MemoryGroupResourceScope &) = delete;
    /** Default move constructor */
    MemoryGroupResourceScope(MemoryGroupResourceScope &&) = default;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    MemoryGroupResourceScope &operator=(const MemoryGroupResourceScope &) = delete;
    /** Default move assignment operator */
    MemoryGroupResourceScope &operator=(MemoryGroupResourceScope &&) = default;
    /** Destructor */
    ~MemoryGroupResourceScope()
    {
        _memory_group.release();
    }

private:
    IMemoryGroup &_memory_group;
};
} // arm_compute
#endif /*__ARM_COMPUTE_IMEMORYGROUP_H__ */