summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/kernel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/kernel/kernel.cpp')
-rw-r--r--src/core/hle/kernel/kernel.cpp85
1 files changed, 1 insertions, 84 deletions
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index b0af5b9b8..7470a97ca 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -2,11 +2,8 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm>
6#include "common/assert.h"
7#include "common/logging/log.h"
8#include "core/hle/config_mem.h" 5#include "core/hle/config_mem.h"
9#include "core/hle/kernel/errors.h" 6#include "core/hle/kernel/handle_table.h"
10#include "core/hle/kernel/kernel.h" 7#include "core/hle/kernel/kernel.h"
11#include "core/hle/kernel/memory.h" 8#include "core/hle/kernel/memory.h"
12#include "core/hle/kernel/process.h" 9#include "core/hle/kernel/process.h"
@@ -18,86 +15,6 @@
18namespace Kernel { 15namespace Kernel {
19 16
20unsigned int Object::next_object_id; 17unsigned int Object::next_object_id;
21HandleTable g_handle_table;
22
23HandleTable::HandleTable() {
24 next_generation = 1;
25 Clear();
26}
27
28ResultVal<Handle> HandleTable::Create(SharedPtr<Object> obj) {
29 DEBUG_ASSERT(obj != nullptr);
30
31 u16 slot = next_free_slot;
32 if (slot >= generations.size()) {
33 LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
34 return ERR_OUT_OF_HANDLES;
35 }
36 next_free_slot = generations[slot];
37
38 u16 generation = next_generation++;
39
40 // Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
41 // CTR-OS doesn't use generation 0, so skip straight to 1.
42 if (next_generation >= (1 << 15))
43 next_generation = 1;
44
45 generations[slot] = generation;
46 objects[slot] = std::move(obj);
47
48 Handle handle = generation | (slot << 15);
49 return MakeResult<Handle>(handle);
50}
51
52ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
53 SharedPtr<Object> object = GetGeneric(handle);
54 if (object == nullptr) {
55 LOG_ERROR(Kernel, "Tried to duplicate invalid handle: %08X", handle);
56 return ERR_INVALID_HANDLE;
57 }
58 return Create(std::move(object));
59}
60
61ResultCode HandleTable::Close(Handle handle) {
62 if (!IsValid(handle))
63 return ERR_INVALID_HANDLE;
64
65 u16 slot = GetSlot(handle);
66
67 objects[slot] = nullptr;
68
69 generations[slot] = next_free_slot;
70 next_free_slot = slot;
71 return RESULT_SUCCESS;
72}
73
74bool HandleTable::IsValid(Handle handle) const {
75 size_t slot = GetSlot(handle);
76 u16 generation = GetGeneration(handle);
77
78 return slot < MAX_COUNT && objects[slot] != nullptr && generations[slot] == generation;
79}
80
81SharedPtr<Object> HandleTable::GetGeneric(Handle handle) const {
82 if (handle == CurrentThread) {
83 return GetCurrentThread();
84 } else if (handle == CurrentProcess) {
85 return g_current_process;
86 }
87
88 if (!IsValid(handle)) {
89 return nullptr;
90 }
91 return objects[GetSlot(handle)];
92}
93
94void HandleTable::Clear() {
95 for (u16 i = 0; i < MAX_COUNT; ++i) {
96 generations[i] = i + 1;
97 objects[i] = nullptr;
98 }
99 next_free_slot = 0;
100}
101 18
102/// Initialize the kernel 19/// Initialize the kernel
103void Init(u32 system_mode) { 20void Init(u32 system_mode) {