mirror of
https://git.suyu.dev/suyu/suyu
synced 2024-11-05 14:57:53 +00:00
0cbcd6ec9a
As means to pave the way for getting rid of global state within core, This eliminates kernel global state by removing all globals. Instead this introduces a KernelCore class which acts as a kernel instance. This instance lives in the System class, which keeps its lifetime contained to the lifetime of the System class. This also forces the kernel types to actually interact with the main kernel instance itself instead of having transient kernel state placed all over several translation units, keeping everything together. It also has a nice consequence of making dependencies much more explicit. This also makes our initialization a tad bit more correct. Previously we were creating a kernel process before the actual kernel was initialized, which doesn't really make much sense. The KernelCore class itself follows the PImpl idiom, which allows keeping all the implementation details sealed away from everything else, which forces the use of the exposed API and allows us to avoid any unnecessary inclusions within the main kernel header.
95 lines
2.5 KiB
C++
95 lines
2.5 KiB
C++
// Copyright 2014 Citra Emulator Project
|
|
// Licensed under GPLv2 or any later version
|
|
// Refer to the license.txt file included.
|
|
|
|
#include <utility>
|
|
#include "common/assert.h"
|
|
#include "common/logging/log.h"
|
|
#include "core/core.h"
|
|
#include "core/hle/kernel/errors.h"
|
|
#include "core/hle/kernel/handle_table.h"
|
|
#include "core/hle/kernel/process.h"
|
|
#include "core/hle/kernel/thread.h"
|
|
|
|
namespace Kernel {
|
|
|
|
HandleTable::HandleTable() {
|
|
next_generation = 1;
|
|
Clear();
|
|
}
|
|
|
|
ResultVal<Handle> HandleTable::Create(SharedPtr<Object> obj) {
|
|
DEBUG_ASSERT(obj != nullptr);
|
|
|
|
u16 slot = next_free_slot;
|
|
if (slot >= generations.size()) {
|
|
LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
|
|
return ERR_HANDLE_TABLE_FULL;
|
|
}
|
|
next_free_slot = generations[slot];
|
|
|
|
u16 generation = next_generation++;
|
|
|
|
// Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
|
|
// CTR-OS doesn't use generation 0, so skip straight to 1.
|
|
if (next_generation >= (1 << 15))
|
|
next_generation = 1;
|
|
|
|
generations[slot] = generation;
|
|
objects[slot] = std::move(obj);
|
|
|
|
Handle handle = generation | (slot << 15);
|
|
return MakeResult<Handle>(handle);
|
|
}
|
|
|
|
ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
|
|
SharedPtr<Object> object = GetGeneric(handle);
|
|
if (object == nullptr) {
|
|
LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle);
|
|
return ERR_INVALID_HANDLE;
|
|
}
|
|
return Create(std::move(object));
|
|
}
|
|
|
|
ResultCode HandleTable::Close(Handle handle) {
|
|
if (!IsValid(handle))
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
u16 slot = GetSlot(handle);
|
|
|
|
objects[slot] = nullptr;
|
|
|
|
generations[slot] = next_free_slot;
|
|
next_free_slot = slot;
|
|
return RESULT_SUCCESS;
|
|
}
|
|
|
|
bool HandleTable::IsValid(Handle handle) const {
|
|
size_t slot = GetSlot(handle);
|
|
u16 generation = GetGeneration(handle);
|
|
|
|
return slot < MAX_COUNT && objects[slot] != nullptr && generations[slot] == generation;
|
|
}
|
|
|
|
SharedPtr<Object> HandleTable::GetGeneric(Handle handle) const {
|
|
if (handle == CurrentThread) {
|
|
return GetCurrentThread();
|
|
} else if (handle == CurrentProcess) {
|
|
return Core::CurrentProcess();
|
|
}
|
|
|
|
if (!IsValid(handle)) {
|
|
return nullptr;
|
|
}
|
|
return objects[GetSlot(handle)];
|
|
}
|
|
|
|
void HandleTable::Clear() {
|
|
for (u16 i = 0; i < MAX_COUNT; ++i) {
|
|
generations[i] = i + 1;
|
|
objects[i] = nullptr;
|
|
}
|
|
next_free_slot = 0;
|
|
}
|
|
|
|
} // namespace Kernel
|