File: heap.h

package info (click to toggle)
intel-media-driver 18.4.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 77,144 kB
  • sloc: cpp: 784,288; ansic: 95,944; asm: 42,125; python: 353; sh: 156; makefile: 15
file content (238 lines) | stat: -rw-r--r-- 8,501 bytes parent folder | download | duplicates (4)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright (c) 2017, Intel Corporation
*
* 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.
*/
//!
//! \file     heap.h
//! \brief    A heap is a buffer in graphics memory in which the driver may store information
//! \details  Heaps are created for the client via the heap manager, through which the client
//!           is expected to claim blocks of memory for data storage. The client has no direct
//!           access to the heap.
//!

#ifndef __HEAP_H__
#define __HEAP_H__

#include "mos_os.h"
#include "mos_util_debug.h"

//! Evaluates the status and returns if failure
#define HEAP_CHK_STATUS(_stmt)                                               \
    MOS_CHK_STATUS_RETURN(MOS_COMPONENT_HW, MOS_HW_SUBCOMP_ALL, _stmt)
//! Checks the pointer and returns if nullptr
#define HEAP_CHK_NULL(_ptr)                                                  \
    MOS_CHK_NULL_RETURN(MOS_COMPONENT_HW, MOS_HW_SUBCOMP_ALL, _ptr)
//! Asserts and prints \a _message at critical message level
#define HEAP_ASSERTMESSAGE(_message, ...)                                                 \
    MOS_ASSERTMESSAGE(MOS_COMPONENT_HW, MOS_SUBCOMP_SELF, _message, ##__VA_ARGS__)
//! Prints \a _message at normal message level
#define HEAP_NORMALMESSAGE(_message, ...)                                                 \
    MOS_NORMALMESSAGE(MOS_COMPONENT_HW, MOS_SUBCOMP_SELF, _message, ##__VA_ARGS__)
//! Prints \a _message at verbose message level
#define HEAP_VERBOSEMESSAGE(_message, ...)                                                \
    MOS_VERBOSEMESSAGE(MOS_COMPONENT_HW, MOS_SUBCOMP_SELF, _message, ##__VA_ARGS__)
//! Prints the function name at function enter message level
#define HEAP_FUNCTION_ENTER                                                               \
    MOS_FUNCTION_ENTER(MOS_COMPONENT_HW, MOS_SUBCOMP_SELF)
//! Prints the function name at verbose function enter message level
#define HEAP_FUNCTION_ENTER_VERBOSE                                                       \
    MOS_FUNCTION_ENTER_VERBOSE(MOS_COMPONENT_HW, MOS_SUBCOMP_SELF)

//! \brief Generic heap for storing client written data in graphics memory
class Heap
{
    friend class MemoryBlockInternal;
    friend class MemoryBlockManager;

public:
    //!
    //! \brief  Default constructor
    //!         Identifier for the heap
    //!  
    Heap();

    //!
    //! \brief    Copy constructor
    //!
    Heap(const Heap&) = delete;

    //!
    //! \brief    Copy assignment operator
    //!
    Heap& operator=(const Heap&) = delete;

    //!
    //! \brief  Constructor
    //! \param  [in] id
    //!         Identifier for the heap
    //!  
    Heap(uint32_t id);
    virtual ~Heap();

    //!  
    //! \brief  Dumps the contents of the heap
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!  
    MOS_STATUS Dump();

    //!
    //! \brief  Checks heap for validity
    //! \return bool
    //!         true if the heap is valid and false if not
    //!
    bool IsValid() { return (m_size > 0); }

    //!
    //! \brief  Gets the graphics resource for the heap
    //! \param  [out] resource
    //!         Container for the output graphics resource
    //! \return MOS_RESOURCE*
    //!         Valid pointer if success, nullptr for any fail
    //!
    MOS_RESOURCE* GetResource()
    {
        HEAP_FUNCTION_ENTER_VERBOSE;
        if (m_size && !Mos_ResourceIsNull(m_resource))
        {
            return m_resource;
        }
        HEAP_ASSERTMESSAGE("Resource is not valid");
        return nullptr;
    }

    //!
    //! \brief  Gets the size of the heap
    //! \return The size of the heap \see m_size
    //!
    uint32_t GetSize() { return m_size; }

    //!
    //! \brief  Gets the used size of the heap
    //! \return The amount of space used in the heaps \see m_usedSize
    //!
    uint32_t GetUsedSize() { return m_usedSpace; }

    //!
    //! \brief  Gets the ID for the heap
    //! \return The Heap ID \see m_id
    //!
    uint32_t GetId() { return m_id; }

    //!
    //! \brief  Whether or not the heap has been marked for freeing.
    //! \return If the heap is being freed \see m_freeInProgress
    //!  
    bool IsFreeInProgress() { return m_freeInProgress; }

    //! \brief Invalid heap ID.
    static const uint8_t m_invalidId = 0;

protected:
    //!
    //! \brief  Stores the provided OS interface in the heap
    //! \param  [in] osInterface
    //!         Must be valid
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!  
    MOS_STATUS RegisterOsInterface(PMOS_INTERFACE osInterface);

    //!
    //! \brief  Allocates a graphics resource of size \a heapSize
    //! \param  [in] heapSize
    //!         The size of the state heap, must be non-zero
    //! \param  [in] keepLocked
    //!         Locks the heap when allocated and does not unlock it until it is destroyed
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!  
    MOS_STATUS Allocate(uint32_t heapSize, bool keepLocked);

    //!
    //! \brief  Locks the heap asynchonously
    //! \return uint8_t*
    //!         If successful, pointer to data in the heap resource; if failed, nullptr \see m_resource
    //!  
    uint8_t* Lock();

    //!
    //! \brief  Unlocks the heap
    //!  
    void Unlock()
    {
        HEAP_FUNCTION_ENTER_VERBOSE;
        if (!m_keepLocked)
        {
            if (m_osInterface == nullptr)
            {
                HEAP_ASSERTMESSAGE("Invalid m_osInterface(nullptr)");
                return;
            }

            m_osInterface->pfnUnlockResource(m_osInterface, m_resource);
        }
    }

    //!
    //! \brief  Prepares the heap for being freed which will occur when all blocks are
    //!         no longer in use.
    //!  
    void PrepareForFree()
    {
        HEAP_FUNCTION_ENTER;
        m_freeInProgress = true;
    }

    //!
    //! \brief  Adjusts the free space in the heap. 
    //! \param  [in] addedSpace
    //!         Space to be added and subtracted from the free and used space
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //! 
    MOS_STATUS AdjustFreeSpace(uint32_t addedSpace);

    //!
    //! \brief  Adjusts the used space in the heap. 
    //! \param  [in] addedSpace
    //!         Space to be added and subtracted from the free and used space
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //! 
    MOS_STATUS AdjustUsedSpace(uint32_t addedSpace);

private:
    //! \brief Indicates that the heap is in the process of being freed
    bool m_freeInProgress = false;
    bool m_keepLocked = false;          //!< Indictaes that heap is kept locked until freed.
    PMOS_RESOURCE m_resource = nullptr; //!< Graphics resource for the heap.
    uint32_t m_size = 0;                //!< The size of the heap.
    //! \brief   Pointer to the locked heap data.
    //! \details This pointer is only valid if the heap is locked always
    uint8_t *m_lockedHeap = nullptr;
    uint32_t m_freeSpace = 0;           //!< The amount of space available in the heap
    uint32_t m_usedSpace = 0;           //!< The amount of used space within the heap
    //! \brief OS interface used for managing graphics resources
    PMOS_INTERFACE m_osInterface = nullptr;
    //! \brief unique identifier for the heap within the heap manager
    uint32_t m_id = m_invalidId;
};
#endif // __HEAP_H__