aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dynarmic/backend/riscv64/code_block.h
blob: 786030c0e0859ffc00091a756f19026b28b6f8a6 (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
/* This file is part of the dynarmic project.
 * Copyright (c) 2024 MerryMage
 * SPDX-License-Identifier: 0BSD
 */

#pragma once

#include <cstdint>
#include <new>

#include <biscuit/assembler.hpp>
#include <sys/mman.h>

namespace Dynarmic::Backend::RV64 {

class CodeBlock {
public:
    explicit CodeBlock(std::size_t size)
            : memsize(size) {
        mem = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);

        if (mem == nullptr)
            throw std::bad_alloc{};
    }

    ~CodeBlock() {
        if (mem == nullptr)
            return;

        munmap(mem, memsize);
    }

    std::uint32_t* ptr() const {
        return mem;
    }

protected:
    std::uint32_t* mem;
    std::size_t memsize = 0;
    biscuit::Assembler as;
};
}  // namespace Dynarmic::Backend::RV64