Some code reformatting of the kernel object pool

Also deletes an unused [] operator
This commit is contained in:
Henrik Rydgard 2015-11-25 13:00:07 +01:00
parent ee4261cf5c
commit 7dd631b7f2
2 changed files with 45 additions and 90 deletions

View File

@ -394,6 +394,7 @@ int sceKernelDcacheWritebackRange(u32 addr, int size)
hleEatCycles(165);
return 0;
}
int sceKernelDcacheWritebackInvalidateRange(u32 addr, int size)
{
#ifdef LOG_CACHE
@ -408,6 +409,7 @@ int sceKernelDcacheWritebackInvalidateRange(u32 addr, int size)
hleEatCycles(165);
return 0;
}
int sceKernelDcacheWritebackInvalidateAll()
{
#ifdef LOG_CACHE
@ -419,7 +421,6 @@ int sceKernelDcacheWritebackInvalidateAll()
return 0;
}
u32 sceKernelIcacheInvalidateAll()
{
#ifdef LOG_CACHE
@ -430,7 +431,6 @@ u32 sceKernelIcacheInvalidateAll()
return 0;
}
u32 sceKernelIcacheClearAll()
{
#ifdef LOG_CACHE
@ -447,23 +447,19 @@ void KernelObject::GetQuickInfo(char *ptr, int size)
strcpy(ptr, "-");
}
KernelObjectPool::KernelObjectPool()
{
KernelObjectPool::KernelObjectPool() {
memset(occupied, 0, sizeof(bool)*maxCount);
nextID = initialNextID;
}
SceUID KernelObjectPool::Create(KernelObject *obj, int rangeBottom, int rangeTop)
{
SceUID KernelObjectPool::Create(KernelObject *obj, int rangeBottom, int rangeTop) {
if (rangeTop > maxCount)
rangeTop = maxCount;
if (nextID >= rangeBottom && nextID < rangeTop)
rangeBottom = nextID++;
for (int i = rangeBottom; i < rangeTop; i++)
{
if (!occupied[i])
{
for (int i = rangeBottom; i < rangeTop; i++) {
if (!occupied[i]) {
occupied[i] = true;
pool[i] = obj;
pool[i]->uid = i + handleOffset;
@ -475,69 +471,49 @@ SceUID KernelObjectPool::Create(KernelObject *obj, int rangeBottom, int rangeTop
return 0;
}
bool KernelObjectPool::IsValid(SceUID handle) const
{
bool KernelObjectPool::IsValid(SceUID handle) const {
int index = handle - handleOffset;
if (index < 0)
if (index < 0 || index >= maxCount)
return false;
if (index >= maxCount)
return false;
return occupied[index];
else
return occupied[index];
}
void KernelObjectPool::Clear()
{
for (int i=0; i<maxCount; i++)
{
//brutally clear everything, no validation
void KernelObjectPool::Clear() {
for (int i = 0; i < maxCount; i++) {
// brutally clear everything, no validation
if (occupied[i])
delete pool[i];
occupied[i]=false;
pool[i] = nullptr;
occupied[i] = false;
}
memset(pool, 0, sizeof(KernelObject*)*maxCount);
nextID = initialNextID;
}
KernelObject *&KernelObjectPool::operator [](SceUID handle)
{
_dbg_assert_msg_(SCEKERNEL, IsValid(handle), "GRABBING UNALLOCED KERNEL OBJ");
return pool[handle - handleOffset];
}
void KernelObjectPool::List()
{
for (int i = 0; i < maxCount; i++)
{
if (occupied[i])
{
void KernelObjectPool::List() {
for (int i = 0; i < maxCount; i++) {
if (occupied[i]) {
char buffer[256];
if (pool[i])
{
pool[i]->GetQuickInfo(buffer,256);
if (pool[i]) {
pool[i]->GetQuickInfo(buffer, 256);
INFO_LOG(SCEKERNEL, "KO %i: %s \"%s\": %s", i + handleOffset, pool[i]->GetTypeName(), pool[i]->GetName(), buffer);
}
else
{
strcpy(buffer,"WTF? Zero Pointer");
} else {
strcpy(buffer, "WTF? Zero Pointer");
}
}
}
}
int KernelObjectPool::GetCount() const
{
int KernelObjectPool::GetCount() const {
int count = 0;
for (int i=0; i<maxCount; i++)
{
for (int i = 0; i < maxCount; i++) {
if (occupied[i])
count++;
}
return count;
}
void KernelObjectPool::DoState(PointerWrap &p)
{
void KernelObjectPool::DoState(PointerWrap &p) {
auto s = p.Section("KernelObjectPool", 1);
if (!s)
return;
@ -545,40 +521,34 @@ void KernelObjectPool::DoState(PointerWrap &p)
int _maxCount = maxCount;
p.Do(_maxCount);
if (_maxCount != maxCount)
{
if (_maxCount != maxCount) {
p.SetError(p.ERROR_FAILURE);
ERROR_LOG(SCEKERNEL, "Unable to load state: different kernel object storage.");
return;
}
if (p.mode == p.MODE_READ)
{
hleCurrentThreadName = NULL;
if (p.mode == p.MODE_READ) {
hleCurrentThreadName = nullptr;
kernelObjects.Clear();
}
p.Do(nextID);
p.DoArray(occupied, maxCount);
for (int i = 0; i < maxCount; ++i)
{
for (int i = 0; i < maxCount; ++i) {
if (!occupied[i])
continue;
int type;
if (p.mode == p.MODE_READ)
{
if (p.mode == p.MODE_READ) {
p.Do(type);
pool[i] = CreateByIDType(type);
// Already logged an error.
if (pool[i] == NULL)
if (pool[i] == nullptr)
return;
pool[i]->uid = i + handleOffset;
}
else
{
} else {
type = pool[i]->GetIDType();
p.Do(type);
}
@ -588,11 +558,9 @@ void KernelObjectPool::DoState(PointerWrap &p)
}
}
KernelObject *KernelObjectPool::CreateByIDType(int type)
{
KernelObject *KernelObjectPool::CreateByIDType(int type) {
// Used for save states. This is ugly, but what other way is there?
switch (type)
{
switch (type) {
case SCE_KERNEL_TMID_Alarm:
return __KernelAlarmObject();
case SCE_KERNEL_TMID_EventFlag:

View File

@ -423,7 +423,6 @@ public:
}
};
class KernelObjectPool {
public:
KernelObjectPool();
@ -436,11 +435,9 @@ public:
static KernelObject *CreateByIDType(int type);
template <class T>
u32 Destroy(SceUID handle)
{
u32 Destroy(SceUID handle) {
u32 error;
if (Get<T>(handle, error))
{
if (Get<T>(handle, error)) {
occupied[handle-handleOffset] = false;
delete pool[handle-handleOffset];
}
@ -450,26 +447,20 @@ public:
bool IsValid(SceUID handle) const;
template <class T>
T* Get(SceUID handle, u32 &outError)
{
if (handle < handleOffset || handle >= handleOffset+maxCount || !occupied[handle-handleOffset])
{
T* Get(SceUID handle, u32 &outError) {
if (handle < handleOffset || handle >= handleOffset+maxCount || !occupied[handle-handleOffset]) {
// Tekken 6 spams 0x80020001 gets wrong with no ill effects, also on the real PSP
if (handle != 0 && (u32)handle != 0x80020001)
{
if (handle != 0 && (u32)handle != 0x80020001) {
WARN_LOG(SCEKERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle);
}
outError = T::GetMissingErrorCode();
return 0;
}
else
{
} else {
// Previously we had a dynamic_cast here, but since RTTI was disabled traditionally,
// it just acted as a static case and everything worked. This means that we will never
// see the Wrong type object error below, but we'll just have to live with that danger.
T* t = static_cast<T*>(pool[handle - handleOffset]);
if (t == 0 || t->GetIDType() != T::GetStaticIDType())
{
if (t == 0 || t->GetIDType() != T::GetStaticIDType()) {
WARN_LOG(SCEKERNEL, "Kernel: Wrong object type for %i (%08x)", handle, handle);
outError = T::GetMissingErrorCode();
return 0;
@ -479,10 +470,9 @@ public:
}
}
// ONLY use this when you know the handle is valid.
// ONLY use this when you KNOW the handle is valid.
template <class T>
T *GetFast(SceUID handle)
{
T *GetFast(SceUID handle) {
const SceUID realHandle = handle - handleOffset;
_dbg_assert_(SCEKERNEL, realHandle >= 0 && realHandle < maxCount && occupied[realHandle]);
return static_cast<T *>(pool[realHandle]);
@ -518,10 +508,8 @@ public:
return total;
}
bool GetIDType(SceUID handle, int *type) const
{
if (handle < handleOffset || handle >= handleOffset+maxCount || !occupied[handle-handleOffset])
{
bool GetIDType(SceUID handle, int *type) const {
if (handle < handleOffset || handle >= handleOffset+maxCount || !occupied[handle-handleOffset]) {
ERROR_LOG(SCEKERNEL, "Kernel: Bad object handle %i (%08x)", handle, handle);
return false;
}
@ -530,7 +518,6 @@ public:
return true;
}
KernelObject *&operator [](SceUID handle);
void List();
void Clear();
int GetCount() const;