File: memory_block.cpp

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 (502 lines) | stat: -rw-r--r-- 13,356 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* 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     memory_block.cpp
//! \brief    Implements functionalities pertaining to memory blocks
//!

#include "memory_block.h"
#include "heap.h"

MOS_STATUS MemoryBlockInternal::AddData(
    void* data,
    uint32_t dataOffset,
    uint32_t dataSize,
    bool zeroBlock)
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    if (m_state != allocated)
    {
        HEAP_ASSERTMESSAGE("Memory blocks may only have data added while in allocated state");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (data == nullptr && !zeroBlock)
    {
        HEAP_ASSERTMESSAGE("No data was passed in to be added");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (m_offset + dataOffset + dataSize > m_heap->GetSize() ||
        dataOffset + dataSize > m_size)
    {
        HEAP_ASSERTMESSAGE("Data will not fit within the this memory block or state heap");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    uint8_t *lockedResource = m_heap->Lock();
    HEAP_CHK_NULL(lockedResource);
    lockedResource += m_offset + dataOffset;

    if (zeroBlock)
    {
        memset(lockedResource, 0, m_size);
    }
    else
    {
        MOS_SecureMemcpy(
            lockedResource,
            m_size - dataOffset,
            data,
            dataSize);
    }

    m_heap->Unlock();

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::ReadData(
    void* data,
    uint32_t dataOffset,
    uint32_t dataSize)
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    if (data == nullptr)
    {
        HEAP_ASSERTMESSAGE("Pointer for data to be read back must be valid");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (m_offset + dataOffset + dataSize > m_heap->GetSize() ||
        dataOffset + dataSize > m_size)
    {
        HEAP_ASSERTMESSAGE("Data attempting to read is outside this memory block or state heap");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    uint8_t *lockedResource = m_heap->Lock();
    HEAP_CHK_NULL(lockedResource);
    lockedResource += m_offset + dataOffset;

    MOS_SecureMemcpy(
        data,
        dataSize,
        lockedResource,
        dataSize);

    m_heap->Unlock();

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Dump(
    std::string filename,
    uint32_t offset,
    uint32_t size,
    bool dumpInBinary)
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    if (m_state != allocated && m_state != submitted)
    {
        HEAP_ASSERTMESSAGE("Memory blocks not in allocated or submitted state do not containe valid data");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    size = size ? size : m_size;

    if (m_offset + offset + size > m_heap->GetSize() ||
        offset + size > m_size)
    {
        HEAP_ASSERTMESSAGE("Data will not fit within the this memory block or state heap");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    uint8_t *lockedResource = m_heap->Lock();
    HEAP_CHK_NULL(lockedResource);
    lockedResource += offset + m_offset;
    if (dumpInBinary)
    {
        HEAP_CHK_STATUS(MOS_WriteFileFromPtr(
            filename.c_str(),
            lockedResource,
            size));
    }
    else
    {
        uint32_t *lockedResourceIn4Bytes = (uint32_t*)lockedResource;
        uint32_t sizeIn32BitHex = size / sizeof(uint32_t);
        uint32_t sizeOfLastHex = size % sizeof(uint32_t);
        std::string formattedData = "";
        char dataInHex[10] = {0};

        for (uint32_t i = 0; i < sizeIn32BitHex; i++)
        {
            MOS_SecureStringPrint(
                dataInHex,
                sizeof(dataInHex),
                sizeof(dataInHex),
                "%.8x ",
                lockedResourceIn4Bytes[i]);
            formattedData += dataInHex;
            if (i % 4 == 3)
            {
                formattedData += "\r\n";
            }
        }

        if (sizeOfLastHex > 0) // one last incomplete dword to print out
        {
            uint32_t lastHex = lockedResourceIn4Bytes[sizeIn32BitHex];
            lastHex = lastHex & ((4 - sizeOfLastHex) * 8);

            MOS_SecureStringPrint(dataInHex, sizeof(dataInHex), sizeof(dataInHex), "%.8x ", lastHex);
            formattedData += dataInHex;
        }

        HEAP_CHK_STATUS(MOS_WriteFileFromPtr(
            filename.c_str(),
            (void*)formattedData.c_str(),
            formattedData.size()));
    }

    m_heap->Unlock();

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Create(
    Heap *heap,
    State requestedState,
    MemoryBlockInternal *prev,
    uint32_t offset,
    uint32_t size,
    uint32_t trackerId)
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    HEAP_CHK_NULL(prev);

    if (m_state == State::deleted)
    {
        HEAP_ASSERTMESSAGE("Deleted blocks may not be re-used.");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (heap == nullptr)
    {
        HEAP_ASSERTMESSAGE("Heap pointer is not valid");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    else if (!heap->IsValid())
    {
        HEAP_ASSERTMESSAGE("Heap is not valid");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (offset + size > heap->GetSize())
    {
        HEAP_ASSERTMESSAGE("Data will not fit within the state heap");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    m_heap = heap;
    m_offset = offset;
    m_size = size;

    if (requestedState == State::free)
    {
        HEAP_CHK_STATUS(Free());
    }
    else if (requestedState == State::allocated)
    {
        HEAP_CHK_STATUS(Allocate(trackerId));
    }
    else
    {
        HEAP_ASSERTMESSAGE("Only free and allocated blocks can be created");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    m_prev = prev;
    m_next = prev->m_next;
    prev->m_next = this;
    auto next = GetNext();
    if (next)
    {
        next->m_prev = this;
    }

    // expected to be inserted into a sorted list by MemoryBlockManager
    m_statePrev = m_stateNext = nullptr;

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Combine(MemoryBlockInternal *block)
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    HEAP_CHK_NULL(block);

    if (block->m_state != State::free || m_state != State::free)
    {
        HEAP_ASSERTMESSAGE("Only free blocks may be combined");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_static)
    {
        HEAP_ASSERTMESSAGE("Static blocks may not be modified");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    auto currPrev = GetPrev();
    auto currNext = GetNext();
    if (currPrev == block)
    {
        m_offset = block->m_offset;
        m_prev = block->m_prev;
        currPrev = GetPrev();
        if (currPrev)
        {
            currPrev->m_next = this;
        }
    }
    else if (currNext == block)
    {
        m_next = block->m_next;
        currNext = GetNext();
        if (currNext)
        {
            currNext->m_prev = this;
        }
    }
    else
    {
        HEAP_ASSERTMESSAGE("Only adjacent blocks may be combined");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    m_size += block->m_size;
    block->Pool();

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Split(MemoryBlockInternal *block, uint32_t size)
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    HEAP_CHK_NULL(block);

    if (size == 0 || m_size == size)
    {
        HEAP_ASSERTMESSAGE("It is not possible to split a block if the new block has no size");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (m_state != State::free)
    {
        HEAP_ASSERTMESSAGE("Only free blocks may be resized");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_static)
    {
        HEAP_ASSERTMESSAGE("Static blocks may not be modified");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    uint32_t newBlockSize = m_size - size;
    HEAP_CHK_STATUS(block->Create(
        m_heap,
        State::free,
        this,
        m_offset + size,
        newBlockSize,
        m_invalidTrackerId));

    m_size = size;

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Pool()
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    if (m_state == State::allocated || m_state == State::submitted)
    {
        HEAP_ASSERTMESSAGE("Allocated and submitted blocks may not be added to the pool");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_stateListType != State::stateCount)
    {
        HEAP_ASSERTMESSAGE("Blocks must be removed from sorted list before changing state");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_static)
    {
        HEAP_ASSERTMESSAGE("Static blocks may not be added to the pool");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    m_state = State::pool;
    m_heap = nullptr;
    m_offset = 0;
    m_size = 0;
    m_static = false;
    m_trackerId = m_invalidTrackerId;
    m_prev = m_next = nullptr;
    m_statePrev = m_stateNext = nullptr;
    m_stateListType = State::stateCount;

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Free()
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    if (m_state == State::deleted)
    {
        HEAP_ASSERTMESSAGE("Deleted blocks may not be moved to any state but pool");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_stateListType != State::stateCount)
    {
        HEAP_ASSERTMESSAGE("Blocks must be removed from sorted list before changing state");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_static)
    {
        HEAP_ASSERTMESSAGE("Static blocks may not be freed");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (m_state != State::pool &&
        m_state != State::free)
    {
        HEAP_CHK_STATUS(m_heap->AdjustFreeSpace(m_size));
    }

    m_state = State::free;
    m_trackerId = m_invalidTrackerId;

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Allocate(uint32_t trackerId)
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    if (m_state != State::free)
    {
        HEAP_ASSERTMESSAGE("Only free blocks may be moved to allocated");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_stateListType != State::stateCount)
    {
        HEAP_ASSERTMESSAGE("Blocks must be removed from sorted list before changing state");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (trackerId == m_invalidTrackerId && !m_static)
    {
        HEAP_ASSERTMESSAGE("Only static blocks may have invalid tracker IDs");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    // Reflect the change in state from free space to used state.
    HEAP_CHK_STATUS(m_heap->AdjustUsedSpace(m_size));

    m_state = State::allocated;
    m_trackerId = trackerId;

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Submit()
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    if (m_state != State::allocated)
    {
        HEAP_ASSERTMESSAGE("Only allocated blocks may be submitted");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_static)
    {
        HEAP_ASSERTMESSAGE("Static blocks may not enter submitted state");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_stateListType != State::stateCount)
    {
        HEAP_ASSERTMESSAGE("Blocks must be removed from sorted list before changing state");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    m_state = State::submitted;

    return MOS_STATUS_SUCCESS;
}

MOS_STATUS MemoryBlockInternal::Delete()
{
    HEAP_FUNCTION_ENTER_VERBOSE;

    MOS_STATUS eStatus = MOS_STATUS_SUCCESS;

    if (m_state == State::pool)
    {
        HEAP_ASSERTMESSAGE("Pool blocks should not be deleted");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_stateListType != State::stateCount)
    {
        HEAP_ASSERTMESSAGE("Blocks must be removed from sorted list before changing state");
        return MOS_STATUS_INVALID_PARAMETER;
    }
    if (m_static)
    {
        HEAP_ASSERTMESSAGE("Static blocks may not be deleted");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (!m_heap->IsFreeInProgress())
    {
        HEAP_ASSERTMESSAGE("Blocks should not be deleted unless the heap is being freed");
        return MOS_STATUS_INVALID_PARAMETER;
    }

    if (m_state != State::free && m_state != State::deleted)
    {
        HEAP_CHK_STATUS(m_heap->AdjustFreeSpace(m_size));
    }

    m_state = State::deleted;
    m_trackerId = m_invalidTrackerId;

    return MOS_STATUS_SUCCESS;
}