2021-04-01 00:12:33 -04:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_funcs.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/intrusive_red_black_tree.h"
|
|
|
|
#include "core/hle/kernel/k_auto_object.h"
|
|
|
|
#include "core/hle/kernel/k_auto_object_container.h"
|
|
|
|
#include "core/hle/kernel/k_light_lock.h"
|
|
|
|
#include "core/hle/kernel/k_slab_heap.h"
|
2021-04-03 22:11:46 -04:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2021-04-01 00:12:33 -04:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
class KSlabAllocated {
|
|
|
|
public:
|
|
|
|
constexpr KSlabAllocated() = default;
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
size_t GetSlabIndex(KernelCore& kernel) const {
|
|
|
|
return kernel.SlabHeap<Derived>().GetIndex(static_cast<const Derived*>(this));
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-04-10 01:10:14 -04:00
|
|
|
static void InitializeSlabHeap(KernelCore& kernel, void* memory, size_t memory_size) {
|
|
|
|
kernel.SlabHeap<Derived>().Initialize(memory, memory_size);
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
static Derived* Allocate(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().Allocate();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
static void Free(KernelCore& kernel, Derived* obj) {
|
|
|
|
kernel.SlabHeap<Derived>().Free(obj);
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
static size_t GetObjectSize(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetObjectSize();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
|
|
|
|
static size_t GetSlabHeapSize(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetSlabHeapSize();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
|
|
|
|
static size_t GetPeakIndex(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetPeakIndex();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
|
|
|
|
static uintptr_t GetSlabHeapAddress(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetSlabHeapAddress();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
static size_t GetNumRemaining(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetNumRemaining();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Derived, typename Base>
|
|
|
|
class KAutoObjectWithSlabHeapAndContainer : public Base {
|
|
|
|
static_assert(std::is_base_of<KAutoObjectWithList, Base>::value);
|
|
|
|
|
|
|
|
private:
|
2021-04-10 01:10:14 -04:00
|
|
|
static Derived* Allocate(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().AllocateWithKernel(kernel);
|
2021-04-03 22:11:46 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
static void Free(KernelCore& kernel, Derived* obj) {
|
|
|
|
kernel.SlabHeap<Derived>().Free(obj);
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
class ListAccessor : public KAutoObjectWithListContainer::ListAccessor {
|
|
|
|
public:
|
2021-04-10 01:10:14 -04:00
|
|
|
ListAccessor() : KAutoObjectWithListContainer::ListAccessor(kernel.ObjectListContainer()) {}
|
2021-04-01 00:12:33 -04:00
|
|
|
~ListAccessor() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2021-04-10 01:10:14 -04:00
|
|
|
KAutoObjectWithSlabHeapAndContainer(KernelCore& kernel_) : Base(kernel_), kernel(kernel_) {}
|
2021-04-01 00:12:33 -04:00
|
|
|
virtual ~KAutoObjectWithSlabHeapAndContainer() {}
|
|
|
|
|
|
|
|
virtual void Destroy() override {
|
|
|
|
const bool is_initialized = this->IsInitialized();
|
|
|
|
uintptr_t arg = 0;
|
|
|
|
if (is_initialized) {
|
2021-04-10 01:10:14 -04:00
|
|
|
kernel.ObjectListContainer().Unregister(this);
|
2021-04-01 00:12:33 -04:00
|
|
|
arg = this->GetPostDestroyArgument();
|
|
|
|
this->Finalize();
|
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
Free(kernel, static_cast<Derived*>(this));
|
2021-04-01 00:12:33 -04:00
|
|
|
if (is_initialized) {
|
|
|
|
Derived::PostDestroy(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool IsInitialized() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
virtual uintptr_t GetPostDestroyArgument() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetSlabIndex() const {
|
2021-04-10 01:10:14 -04:00
|
|
|
return SlabHeap<Derived>(kernel).GetObjectIndex(static_cast<const Derived*>(this));
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-04-03 22:11:46 -04:00
|
|
|
static void InitializeSlabHeap(KernelCore& kernel, void* memory, size_t memory_size) {
|
2021-04-10 01:10:14 -04:00
|
|
|
kernel.SlabHeap<Derived>().Initialize(memory, memory_size);
|
2021-04-03 22:11:46 -04:00
|
|
|
kernel.ObjectListContainer().Initialize();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:42:23 -04:00
|
|
|
static Derived* Create(KernelCore& kernel) {
|
|
|
|
Derived* obj = Allocate(kernel);
|
2021-04-03 22:11:46 -04:00
|
|
|
if (obj != nullptr) {
|
2021-04-01 00:12:33 -04:00
|
|
|
KAutoObject::Create(obj);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2021-04-03 22:11:46 -04:00
|
|
|
static void Register(KernelCore& kernel, Derived* obj) {
|
|
|
|
return kernel.ObjectListContainer().Register(obj);
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
static size_t GetObjectSize(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetObjectSize();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
|
|
|
|
static size_t GetSlabHeapSize(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetSlabHeapSize();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
|
|
|
|
static size_t GetPeakIndex(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetPeakIndex();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
|
|
|
|
static uintptr_t GetSlabHeapAddress(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetSlabHeapAddress();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 01:10:14 -04:00
|
|
|
static size_t GetNumRemaining(KernelCore& kernel) {
|
|
|
|
return kernel.SlabHeap<Derived>().GetNumRemaining();
|
2021-04-01 00:12:33 -04:00
|
|
|
}
|
2021-04-10 01:10:14 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
KernelCore& kernel;
|
2021-04-01 00:12:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|