File: heap_manager.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 (227 lines) | stat: -rw-r--r-- 8,249 bytes parent folder | download | duplicates (2)
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
/*
* 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_manager.h
//! \brief    The client facing interface for handling heaps.
//!

#ifndef __HEAP_MANAGER_H__
#define __HEAP_MANAGER_H__

#include "memory_block_manager.h"

//! \brief Client accessible manager for heaps.
class HeapManager
{

public:
    //! \brief Expected behavior when space is not available in the heap(s)
    enum Behavior
    {
        wait = 0,           //<! Waits for space to become available.
        extend,             //<! Allocates a new heap of the existing size + the default extend heap size.
        destructiveExtend,  //<! Performs the same action as extend and marks the old heap for delete.
        waitAndExtend,      //<! Waits for a fixed amount of time and then performs an extend if a timeout occurs.
        clientControlled    //<! If space is not available, heap manager returns so client can decide what to do
    };

    HeapManager()
    {
        HEAP_FUNCTION_ENTER;
    }
    virtual ~HeapManager();

    //!
    //! \brief  Acquires space within the heap(s)
    //! \param  [in] params
    //!         Parameters describing the requested space
    //! \param  [out] blocks
    //!         A vector containing the memory blocks allocated
    //! \param  [out] spaceNeeded
    //!         Amount of space that the heap(s) are short of to complete space acquisition
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS AcquireSpace(
        MemoryBlockManager::AcquireParams &params,
        std::vector<MemoryBlock> &blocks,
        uint32_t &spaceNeeded);

    //!
    //! \brief  Indicates that the client is done editing the blocks
    //! \param  [in] blocks
    //!         Blocks to be submitted
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS SubmitBlocks(std::vector<MemoryBlock> &blocks)
    {
        return m_blockManager.SubmitBlocks(blocks);
    }

    //!
    //! \brief   Makes a block available in the heap.
    //! \details Expected to be used only when the behavior selected in HeapManager is client
    //!          controlled--the client wants direct control over what is removed from the heap
    //!          (static blocks would be used in this case).
    //! \param   [in] block
    //!          Block to be removed
    //! \return  MOS_STATUS
    //!          MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS ClearSpace(MemoryBlock &block);

    //!
    //! \brief  Registers the tracker data to be used for determining whether a
    //!         memory block is available.
    //! \param  [in] trackerData
    //!         Must be valid; pointer to tracker data. \see MemoryBlockManager::m_trackerData.
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS RegisterTrackerResource(uint32_t *trackerData);

    //!
    //! \brief  Updates the default behavior of the heap manager
    //! \param  [in] behavior
    //!         Expected behavior of the heap mamanger
    //!
    void SetDefaultBehavior(Behavior behavior)
    { 
        HEAP_FUNCTION_ENTER;
        if (behavior > clientControlled)
        {
            behavior = wait;
        }
        m_behavior = behavior;
    }

    //!
    //! \brief  Updates the initial heap size either for first allocation or for extension
    //! \param  [in] size
    //!         Updates the current heap size, must be non-zero. \see m_currHeapSize
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS SetInitialHeapSize(uint32_t size);

    //!
    //! \brief  Updates the extend heap size when the behavior is not to wait. \see m_behavior \see Behavior::wait
    //! \param  [in] size
    //!         Updates the extend heap size, must be non-zero. \see m_extendHeapSize
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS SetExtendHeapSize(uint32_t size);

    //!
    //! \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  Indicates the total size of all managed heaps
    //! \return The size of all heaps managed \see m_totalSizeOfHeaps
    //!  
    uint32_t GetTotalSize();

    //!
    //! \brief  Indicates the size of heap extensions
    //! \return The size by which heaps are extended by \see m_extendHeapSize
    //!  
    uint32_t GetExtendSize()
    {
        HEAP_FUNCTION_ENTER;
        return m_extendHeapSize;
    }

    //!
    //! \brief   All heaps allocated are locked and kept locked for their lifetimes
    //! \details May only be set before any heaps are allocated.
    //! \return  MOS_STATUS
    //!          MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS LockHeapsOnAllocate()
    {
        HEAP_FUNCTION_ENTER;
        return m_blockManager.LockHeapsOnAllocate();
    }

private:
    //!
    //! \brief  Allocates a heap of requested size
    //! \param  [in] size
    //!         Size of the heap to be allocated
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS AllocateHeap(uint32_t size);

    //!
    //! \brief  Free or attempts to free a heap
    //!
    void FreeHeap();

    //!
    //! \brief  Wait for for space to be available in the heap
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS Wait();

    //!
    //! \brief  If space may not be acquired, behave according to the current specified behavior.
    //!         \see m_behavior \see MemoryBlockManager::AcquireSpace
    //! \return MOS_STATUS
    //!         MOS_STATUS_SUCCESS if success, else fail reason
    //!
    MOS_STATUS BehaveWhenNoSpace();

    //! \brief Alignment used for the heap size during allocation
    static const uint32_t m_heapAlignment = MOS_PAGE_SIZE;
    //! \brief Timeout in milliseconds for wait, currently fixed
    static const uint32_t m_waitTimeout = 100;
    //! \brief Wait increment in milliseconds, currently fixed
    static const uint32_t m_waitIncrement = 10;

    //! \brief Memory block manager for the heap(s)
    MemoryBlockManager m_blockManager;
    //! \brief The default behavior when space is not found in any of the heaps.
    Behavior m_behavior = Behavior::wait;
    //! \brief Current heap ID
    uint32_t m_currHeapId = Heap::m_invalidId;
    //! \brief The current heap size
    uint32_t m_currHeapSize = 0;
    //! \brief The size by which a heap is expected to be extended by when the behavior
    //!        is extend.
    uint32_t m_extendHeapSize = 0;
    //! \brief Stores the IDs for the heaps stored in the memory block manager
    std::list<uint32_t> m_heapIds;
    //! \brief OS interface used for managing graphics resources
    PMOS_INTERFACE m_osInterface = nullptr;
};

#endif // __HEAP_MANAGER_H__