File: address_mapper.cpp

package info (click to toggle)
intel-compute-runtime 22.43.24595.41-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,740 kB
  • sloc: cpp: 631,142; lisp: 3,515; sh: 470; makefile: 76; python: 21
file content (67 lines) | stat: -rw-r--r-- 1,565 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
/*
 * Copyright (C) 2018-2021 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/memory_manager/address_mapper.h"

#include "shared/source/helpers/aligned_memory.h"

#include <inttypes.h>
#include <iostream>

namespace NEO {

AddressMapper::AddressMapper() : nextPage(1) {
}
AddressMapper::~AddressMapper() {
    for (auto &m : mapping)
        delete m;
}
uint32_t AddressMapper::map(void *vm, size_t size) {
    void *aligned = alignDown(vm, MemoryConstants::pageSize);
    size_t alignedSize = alignSizeWholePage(vm, size);

    auto it = mapping.begin();
    for (; it != mapping.end(); it++) {
        if ((*it)->vm == aligned) {
            if ((*it)->size == alignedSize) {
                return (*it)->ggtt;
            }
            break;
        }
    }
    if (it != mapping.end()) {
        delete *it;
        mapping.erase(it);
    }
    uint32_t numPages = static_cast<uint32_t>(alignedSize / MemoryConstants::pageSize);
    auto tmp = nextPage.fetch_add(numPages);

    MapInfo *m = new MapInfo;
    m->vm = aligned;
    m->size = alignedSize;
    m->ggtt = static_cast<uint32_t>(tmp * MemoryConstants::pageSize);

    mapping.push_back(m);

    return m->ggtt;
}

void AddressMapper::unmap(void *vm) {
    void *aligned = alignDown(vm, MemoryConstants::pageSize);

    auto it = mapping.begin();
    for (; it != mapping.end(); it++) {
        if ((*it)->vm == aligned) {
            break;
        }
    }
    if (it != mapping.end()) {
        delete *it;
        mapping.erase(it);
    }
}
} // namespace NEO