aboutsummaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/handle_table.cpp
blob: 081135368bd3f2843f9311bca12ece54fe3ef7a9 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <utility>
#include <boost/serialization/array.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include "common/archives.h"
#include "common/assert.h"
#include "common/logging/log.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"

SERIALIZE_EXPORT_IMPL(Kernel::HandleTable)

namespace Kernel {
namespace {
constexpr u16 GetSlot(Handle handle) {
    return handle >> 15;
}

constexpr u16 GetGeneration(Handle handle) {
    return handle & 0x7FFF;
}
} // Anonymous namespace

HandleTable::HandleTable(KernelSystem& kernel) : kernel(kernel) {
    next_generation = 1;
    Clear();
}

HandleTable::~HandleTable() = default;

Result HandleTable::Create(Handle* out_handle, std::shared_ptr<Object> obj) {
    DEBUG_ASSERT(obj != nullptr);

    u16 slot = next_free_slot;
    R_UNLESS(slot < generations.size(), ResultOutOfHandles);
    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);

    *out_handle = generation | (slot << 15);
    return ResultSuccess;
}

Result HandleTable::Duplicate(Handle* out_handle, Handle handle) {
    std::shared_ptr<Object> object = GetGeneric(handle);
    R_UNLESS(object, ResultInvalidHandle);
    return Create(out_handle, std::move(object));
}

Result HandleTable::Close(Handle handle) {
    R_UNLESS(IsValid(handle), ResultInvalidHandle);

    const u16 slot = GetSlot(handle);
    objects[slot] = nullptr;

    generations[slot] = next_free_slot;
    next_free_slot = slot;
    return ResultSuccess;
}

bool HandleTable::IsValid(Handle handle) const {
    const u16 slot = GetSlot(handle);
    const u16 generation = GetGeneration(handle);
    return slot < MAX_COUNT && objects[slot] != nullptr && generations[slot] == generation;
}

std::shared_ptr<Object> HandleTable::GetGeneric(Handle handle) const {
    if (handle == CurrentThread) {
        return SharedFrom(kernel.GetCurrentThreadManager().GetCurrentThread());
    } else if (handle == CurrentProcess) {
        return kernel.GetCurrentProcess();
    }

    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;
}

template <class Archive>
void HandleTable::serialize(Archive& ar, const unsigned int) {
    ar& objects;
    ar& generations;
    ar& next_generation;
    ar& next_free_slot;
}
SERIALIZE_IMPL(HandleTable)

} // namespace Kernel