diff options
| author | 2014-05-09 22:11:18 -0400 | |
|---|---|---|
| committer | 2014-05-09 22:11:18 -0400 | |
| commit | 6b264518a50ce21cb1be55ff3eac4e1c85582cfe (patch) | |
| tree | 036ae3be74768165fbb4ee24db4cdba4ffadec2e /src/core/hle/kernel/kernel.h | |
| parent | added kernel logger to common (diff) | |
| download | yuzu-6b264518a50ce21cb1be55ff3eac4e1c85582cfe.tar.gz yuzu-6b264518a50ce21cb1be55ff3eac4e1c85582cfe.tar.xz yuzu-6b264518a50ce21cb1be55ff3eac4e1c85582cfe.zip | |
added initial kernel/thread modules
Diffstat (limited to 'src/core/hle/kernel/kernel.h')
| -rw-r--r-- | src/core/hle/kernel/kernel.h | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h new file mode 100644 index 000000000..2381ca7f7 --- /dev/null +++ b/src/core/hle/kernel/kernel.h | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project / PPSSPP Project | ||
| 2 | // Licensed under GPLv2 | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/common_types.h" | ||
| 8 | |||
| 9 | typedef u32 UID; | ||
| 10 | |||
| 11 | class KernelObjectPool; | ||
| 12 | |||
| 13 | class KernelObject { | ||
| 14 | friend class KernelObjectPool; | ||
| 15 | u32 uid; | ||
| 16 | public: | ||
| 17 | virtual ~KernelObject() {} | ||
| 18 | UID GetUID() const { return uid; } | ||
| 19 | virtual const char *GetTypeName() { return "[BAD KERNEL OBJECT TYPE]"; } | ||
| 20 | virtual const char *GetName() { return "[UNKNOWN KERNEL OBJECT]"; } | ||
| 21 | virtual int GetIDType() const = 0; | ||
| 22 | //virtual void GetQuickInfo(char *ptr, int size); | ||
| 23 | }; | ||
| 24 | |||
| 25 | class KernelObjectPool { | ||
| 26 | public: | ||
| 27 | KernelObjectPool(); | ||
| 28 | ~KernelObjectPool() {} | ||
| 29 | |||
| 30 | // Allocates a UID within the range and inserts the object into the map. | ||
| 31 | UID Create(KernelObject *obj, int range_bottom=INITIAL_NEXT_ID, int range_top=0x7FFFFFFF); | ||
| 32 | |||
| 33 | static KernelObject *CreateByIDType(int type); | ||
| 34 | |||
| 35 | template <class T> | ||
| 36 | u32 Destroy(UID handle) { | ||
| 37 | u32 error; | ||
| 38 | if (Get<T>(handle, error)) { | ||
| 39 | occupied[handle - handleOffset] = false; | ||
| 40 | delete pool[handle - handleOffset]; | ||
| 41 | } | ||
| 42 | return error; | ||
| 43 | }; | ||
| 44 | |||
| 45 | bool IsValid(UID handle); | ||
| 46 | |||
| 47 | template <class T> | ||
| 48 | T* Get(UID handle, u32& outError) { | ||
| 49 | if (handle < handleOffset || handle >= handleOffset + maxCount || !occupied[handle - handleOffset]) { | ||
| 50 | // Tekken 6 spams 0x80020001 gets wrong with no ill effects, also on the real PSP | ||
| 51 | if (handle != 0 && (u32)handle != 0x80020001) { | ||
| 52 | WARN_LOG(SCEKERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle); | ||
| 53 | } | ||
| 54 | outError = T::GetMissingErrorCode(); | ||
| 55 | return 0; | ||
| 56 | } else { | ||
| 57 | // Previously we had a dynamic_cast here, but since RTTI was disabled traditionally, | ||
| 58 | // it just acted as a static case and everything worked. This means that we will never | ||
| 59 | // see the Wrong type object error below, but we'll just have to live with that danger. | ||
| 60 | T* t = static_cast<T*>(pool[handle - handleOffset]); | ||
| 61 | if (t == 0 || t->GetIDType() != T::GetStaticIDType()) { | ||
| 62 | WARN_LOG(SCEKERNEL, "Kernel: Wrong object type for %i (%08x)", handle, handle); | ||
| 63 | outError = T::GetMissingErrorCode(); | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | outError = SCE_KERNEL_ERROR_OK; | ||
| 67 | return t; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | // ONLY use this when you know the handle is valid. | ||
| 72 | template <class T> | ||
| 73 | T *GetFast(UID handle) { | ||
| 74 | const UID realHandle = handle - handleOffset; | ||
| 75 | _dbg_assert_(SCEKERNEL, realHandle >= 0 && realHandle < maxCount && occupied[realHandle]); | ||
| 76 | return static_cast<T *>(pool[realHandle]); | ||
| 77 | } | ||
| 78 | |||
| 79 | template <class T, typename ArgT> | ||
| 80 | void Iterate(bool func(T *, ArgT), ArgT arg) { | ||
| 81 | int type = T::GetStaticIDType(); | ||
| 82 | for (int i = 0; i < maxCount; i++) | ||
| 83 | { | ||
| 84 | if (!occupied[i]) | ||
| 85 | continue; | ||
| 86 | T *t = static_cast<T *>(pool[i]); | ||
| 87 | if (t->GetIDType() == type) { | ||
| 88 | if (!func(t, arg)) | ||
| 89 | break; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | bool GetIDType(UID handle, int *type) const { | ||
| 95 | if ((handle < HANDLE_OFFSET) || (handle >= HANDLE_OFFSET + MAX_COUNT) || | ||
| 96 | !occupied[handle - HANDLE_OFFSET]) { | ||
| 97 | ERROR_LOG(KERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle); | ||
| 98 | return false; | ||
| 99 | } | ||
| 100 | KernelObject *t = pool[handle - HANDLE_OFFSET]; | ||
| 101 | *type = t->GetIDType(); | ||
| 102 | return true; | ||
| 103 | } | ||
| 104 | |||
| 105 | KernelObject *&operator [](UID handle); | ||
| 106 | void List(); | ||
| 107 | void Clear(); | ||
| 108 | int GetCount(); | ||
| 109 | |||
| 110 | private: | ||
| 111 | enum { | ||
| 112 | MAX_COUNT = 0x1000, | ||
| 113 | HANDLE_OFFSET = 0x100, | ||
| 114 | INITIAL_NEXT_ID = 0x10, | ||
| 115 | }; | ||
| 116 | KernelObject *pool[MAX_COUNT]; | ||
| 117 | bool occupied[MAX_COUNT]; | ||
| 118 | int next_id; | ||
| 119 | }; | ||
| 120 | |||
| 121 | extern KernelObjectPool g_kernel_objects; | ||