summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/hle/kernel/memory/page_linked_list.h93
2 files changed, 94 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 92ed3bdcf..3af325c8e 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -158,6 +158,7 @@ add_library(core STATIC
158 hle/kernel/memory/address_space_info.h 158 hle/kernel/memory/address_space_info.h
159 hle/kernel/memory/memory_block.h 159 hle/kernel/memory/memory_block.h
160 hle/kernel/memory/memory_types.h 160 hle/kernel/memory/memory_types.h
161 hle/kernel/memory/page_linked_list.h
161 hle/kernel/memory/slab_heap.h 162 hle/kernel/memory/slab_heap.h
162 hle/kernel/memory/system_control.cpp 163 hle/kernel/memory/system_control.cpp
163 hle/kernel/memory/system_control.h 164 hle/kernel/memory/system_control.h
diff --git a/src/core/hle/kernel/memory/page_linked_list.h b/src/core/hle/kernel/memory/page_linked_list.h
new file mode 100644
index 000000000..0668d00c6
--- /dev/null
+++ b/src/core/hle/kernel/memory/page_linked_list.h
@@ -0,0 +1,93 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <list>
8
9#include "common/assert.h"
10#include "common/common_funcs.h"
11#include "common/common_types.h"
12#include "core/hle/kernel/memory/memory_types.h"
13#include "core/hle/result.h"
14
15namespace Kernel::Memory {
16
17class PageLinkedList final {
18public:
19 class Node final {
20 public:
21 constexpr Node(u64 addr, std::size_t num_pages) : addr{addr}, num_pages{num_pages} {}
22
23 constexpr u64 GetAddress() const {
24 return addr;
25 }
26
27 constexpr std::size_t GetNumPages() const {
28 return num_pages;
29 }
30
31 private:
32 u64 addr{};
33 std::size_t num_pages{};
34 };
35
36public:
37 PageLinkedList() = default;
38 PageLinkedList(u64 address, u64 num_pages) {
39 ASSERT(AddBlock(address, num_pages).IsSuccess());
40 }
41
42 constexpr std::list<Node>& Nodes() {
43 return nodes;
44 }
45
46 constexpr const std::list<Node>& Nodes() const {
47 return nodes;
48 }
49
50 std::size_t GetNumPages() const {
51 std::size_t num_pages = 0;
52 for (const Node& node : nodes) {
53 num_pages += node.GetNumPages();
54 }
55 return num_pages;
56 }
57
58 bool IsEqual(PageLinkedList& other) const {
59 auto this_node = nodes.begin();
60 auto other_node = other.nodes.begin();
61 while (this_node != nodes.end() && other_node != other.nodes.end()) {
62 if (this_node->GetAddress() != other_node->GetAddress() ||
63 this_node->GetNumPages() != other_node->GetNumPages()) {
64 return false;
65 }
66 this_node = std::next(this_node);
67 other_node = std::next(other_node);
68 }
69
70 return this_node == nodes.end() && other_node == other.nodes.end();
71 }
72
73 ResultCode AddBlock(u64 address, u64 num_pages) {
74 if (!num_pages) {
75 return RESULT_SUCCESS;
76 }
77 if (!nodes.empty()) {
78 const auto node = nodes.back();
79 if (node.GetAddress() + node.GetNumPages() * PageSize == address) {
80 address = node.GetAddress();
81 num_pages += node.GetNumPages();
82 nodes.pop_back();
83 }
84 }
85 nodes.push_back({address, num_pages});
86 return RESULT_SUCCESS;
87 }
88
89private:
90 std::list<Node> nodes;
91};
92
93} // namespace Kernel::Memory