2017-05-29 19:45:42 -04:00
|
|
|
// 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"
|
2018-03-13 17:49:59 -04:00
|
|
|
#include "core/core.h"
|
2017-05-29 19:45:42 -04:00
|
|
|
#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 {
|
2018-11-21 18:30:58 -05:00
|
|
|
namespace {
|
|
|
|
constexpr u16 GetSlot(Handle handle) {
|
2019-02-25 09:53:18 -05:00
|
|
|
return static_cast<u16>(handle >> 15);
|
2018-11-21 18:30:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr u16 GetGeneration(Handle handle) {
|
2019-02-25 09:53:18 -05:00
|
|
|
return static_cast<u16>(handle & 0x7FFF);
|
2018-11-21 18:30:58 -05:00
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
2017-05-29 19:45:42 -04:00
|
|
|
|
|
|
|
HandleTable::HandleTable() {
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2018-11-21 18:22:24 -05:00
|
|
|
HandleTable::~HandleTable() = default;
|
|
|
|
|
2019-02-25 10:13:52 -05:00
|
|
|
ResultCode HandleTable::SetSize(s32 handle_table_size) {
|
|
|
|
if (static_cast<u32>(handle_table_size) > MAX_COUNT) {
|
2020-04-29 00:53:53 -04:00
|
|
|
LOG_ERROR(Kernel, "Handle table size {} is greater than {}", handle_table_size, MAX_COUNT);
|
2019-02-25 10:13:52 -05:00
|
|
|
return ERR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values less than or equal to zero indicate to use the maximum allowable
|
|
|
|
// size for the handle table in the actual kernel, so we ignore the given
|
|
|
|
// value in that case, since we assume this by default unless this function
|
|
|
|
// is called.
|
|
|
|
if (handle_table_size > 0) {
|
|
|
|
table_size = static_cast<u16>(handle_table_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
ResultVal<Handle> HandleTable::Create(std::shared_ptr<Object> obj) {
|
2017-05-29 19:45:42 -04:00
|
|
|
DEBUG_ASSERT(obj != nullptr);
|
|
|
|
|
2019-02-25 10:13:52 -05:00
|
|
|
const u16 slot = next_free_slot;
|
|
|
|
if (slot >= table_size) {
|
2018-07-02 12:13:26 -04:00
|
|
|
LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
|
2018-08-25 05:44:51 -04:00
|
|
|
return ERR_HANDLE_TABLE_FULL;
|
2017-05-29 19:45:42 -04:00
|
|
|
}
|
|
|
|
next_free_slot = generations[slot];
|
|
|
|
|
2019-02-25 10:13:52 -05:00
|
|
|
const u16 generation = next_generation++;
|
2017-05-29 19:45:42 -04:00
|
|
|
|
|
|
|
// Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
|
2018-12-04 01:40:16 -05:00
|
|
|
// Horizon OS uses zero to represent an invalid handle, so skip to 1.
|
|
|
|
if (next_generation >= (1 << 15)) {
|
2017-05-29 19:45:42 -04:00
|
|
|
next_generation = 1;
|
2018-12-04 01:40:16 -05:00
|
|
|
}
|
2017-05-29 19:45:42 -04:00
|
|
|
|
|
|
|
generations[slot] = generation;
|
|
|
|
objects[slot] = std::move(obj);
|
|
|
|
|
|
|
|
Handle handle = generation | (slot << 15);
|
|
|
|
return MakeResult<Handle>(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<Object> object = GetGeneric(handle);
|
2017-05-29 19:45:42 -04:00
|
|
|
if (object == nullptr) {
|
2018-07-02 12:13:26 -04:00
|
|
|
LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle);
|
2017-05-29 19:45:42 -04:00
|
|
|
return ERR_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
return Create(std::move(object));
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode HandleTable::Close(Handle handle) {
|
2019-02-25 11:06:00 -05:00
|
|
|
if (!IsValid(handle)) {
|
2020-04-29 00:53:53 -04:00
|
|
|
LOG_ERROR(Kernel, "Handle is not valid! handle={:08X}", handle);
|
2017-05-29 19:45:42 -04:00
|
|
|
return ERR_INVALID_HANDLE;
|
2019-02-25 11:06:00 -05:00
|
|
|
}
|
2017-05-29 19:45:42 -04:00
|
|
|
|
2019-02-25 11:06:00 -05:00
|
|
|
const u16 slot = GetSlot(handle);
|
2017-05-29 19:45:42 -04:00
|
|
|
|
|
|
|
objects[slot] = nullptr;
|
|
|
|
|
|
|
|
generations[slot] = next_free_slot;
|
|
|
|
next_free_slot = slot;
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HandleTable::IsValid(Handle handle) const {
|
2019-02-25 11:06:00 -05:00
|
|
|
const std::size_t slot = GetSlot(handle);
|
|
|
|
const u16 generation = GetGeneration(handle);
|
2017-05-29 19:45:42 -04:00
|
|
|
|
2019-02-25 10:13:52 -05:00
|
|
|
return slot < table_size && objects[slot] != nullptr && generations[slot] == generation;
|
2017-05-29 19:45:42 -04:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<Object> HandleTable::GetGeneric(Handle handle) const {
|
2017-05-29 19:45:42 -04:00
|
|
|
if (handle == CurrentThread) {
|
2019-11-24 20:15:51 -05:00
|
|
|
return SharedFrom(GetCurrentThread());
|
2017-05-29 19:45:42 -04:00
|
|
|
} else if (handle == CurrentProcess) {
|
2019-11-24 20:15:51 -05:00
|
|
|
return SharedFrom(Core::System::GetInstance().CurrentProcess());
|
2017-05-29 19:45:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValid(handle)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return objects[GetSlot(handle)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleTable::Clear() {
|
2019-02-25 10:13:52 -05:00
|
|
|
for (u16 i = 0; i < table_size; ++i) {
|
2017-05-29 19:45:42 -04:00
|
|
|
generations[i] = i + 1;
|
|
|
|
objects[i] = nullptr;
|
|
|
|
}
|
|
|
|
next_free_slot = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-20 02:48:02 -05:00
|
|
|
} // namespace Kernel
|