Skip to content

Commit

Permalink
Use PPSSPP's kernel interface to retrieve Heap objects
Browse files Browse the repository at this point in the history
Allows us to get rid of DoState and the map<>.
  • Loading branch information
hrydgard committed Aug 8, 2019
1 parent 47519d1 commit e02d5bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
1 change: 1 addition & 0 deletions Core/HLE/sceKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "sceJpeg.h"
#include "sceKernel.h"
#include "sceKernelAlarm.h"
#include "sceKernelHeap.h"
#include "sceKernelInterrupt.h"
#include "sceKernelThread.h"
#include "sceKernelMemory.h"
Expand Down
71 changes: 26 additions & 45 deletions Core/HLE/sceKernelHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include "Core/HLE/sceKernelMemory.h"
#include "Core/Util/BlockAllocator.h"

struct HeapInformation : public KernelObject {
const u32 HEAP_BLOCK_HEADER_SIZE = 8;
const bool frombottom = false;

struct Heap : public KernelObject {
int uid = 0;
int partitionId = 0;
u32 size = 0;
Expand All @@ -34,31 +37,9 @@ struct HeapInformation : public KernelObject {
}
};

std::map<u32, HeapInformation *> heapInformationList;

void __HeapInformationDoState(PointerWrap &p) {
auto s = p.Section("kernelHeap", 1);
if (!s)
return;

if (s >= 1) {
p.Do(heapInformationList);
}
}

static HeapInformation *getHeap(u32 heapId) {
auto found = heapInformationList.find(heapId);
if (found == heapInformationList.end()) {
return NULL;
}
return found->second;
}

const u32 HEAP_BLOCK_HEADER_SIZE = 8;
const bool frombottom = false;

static int sceKernelCreateHeap(int partitionId, int size, int flags, const char *Name) {
HeapInformation *heap = new HeapInformation;
Heap *heap = new Heap();
SceUID uid = kernelObjects.Create(heap);
heap->partitionId = partitionId;
heap->flags = flags;
heap->name = *Name;
Expand All @@ -72,42 +53,42 @@ static int sceKernelCreateHeap(int partitionId, int size, int flags, const char
}
heap->address = addr;
heap->alloc.Init(heap->address + 128, heap->size - 128);
SceUID uid = kernelObjects.Create(heap);
heap->uid = uid;
heapInformationList[uid] = heap;
return hleLogSuccessInfoX(SCEKERNEL, uid, "");
}

static int sceKernelAllocHeapMemory(int heapId, int size) {
HeapInformation *heap = getHeap(heapId);
if (!heap) {
u32 error;
Heap *heap = kernelObjects.Get<Heap>(heapId, error);
if (heap) {
// There's 8 bytes at the end of every block, reserved.
u32 memSize = HEAP_BLOCK_HEADER_SIZE + size;
u32 addr = heap->alloc.Alloc(memSize, true);
return hleLogSuccessInfoX(SCEKERNEL, addr, "");
} else {
ERROR_LOG(HLE, "sceKernelAllocHeapMemory cannot find heapId", heapId);
return 0;
return error;
}

// There's 8 bytes at the end of every block, reserved.
u32 memSize = HEAP_BLOCK_HEADER_SIZE + size;
u32 addr = heap->alloc.Alloc(memSize, true);
return hleLogSuccessInfoX(SCEKERNEL, addr, "");
}

static int sceKernelDeleteHeap(int heapId) {
HeapInformation *heap = getHeap(heapId);
if (!heap) {
u32 error;
Heap *heap = kernelObjects.Get<Heap>(heapId, error);
if (heap) {
userMemory.Free(heap->address);
kernelObjects.Destroy<Heap>(heap->uid);
return hleLogSuccessInfoX(SCEKERNEL, 0, "");
} else {
ERROR_LOG(HLE, "sceKernelDeleteHeap(%i): invalid heapId", heapId);
return -1;
return error;
}
userMemory.Free(heap->address);
kernelObjects.Destroy<HeapInformation>(heap->uid);
heapInformationList.erase(heapId);
return hleLogSuccessInfoX(SCEKERNEL, 0, "");
}

const HLEFunction SysMemForKernel[] =
{
{ 0X636C953B, &WrapI_II<sceKernelAllocHeapMemory>, "sceKernelAllocHeapMemory", 'I', "ii" },
{ 0XC9805775, &WrapI_I<sceKernelDeleteHeap>, "sceKernelDeleteHeap", 'I', "i" },
{ 0X1C1FBFE7, &WrapI_IIIC<sceKernelCreateHeap>, "sceKernelCreateHeap", 'I', "iiis" },
{ 0X636C953B, &WrapI_II<sceKernelAllocHeapMemory>, "sceKernelAllocHeapMemory", 'I', "ii" },
{ 0XC9805775, &WrapI_I<sceKernelDeleteHeap>, "sceKernelDeleteHeap", 'I', "i" },
{ 0X1C1FBFE7, &WrapI_IIIC<sceKernelCreateHeap>, "sceKernelCreateHeap", 'I', "iiis" },
};

void Register_SysMemForKernel() {
Expand Down
2 changes: 2 additions & 0 deletions Core/HLE/sceKernelHeap.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "Common/ChunkFile.h"

int sceKernelCreateHeap(int partitionId, int size, int flags, const char *Name);
int sceKernelAllocHeapMemory(int heapId, int size);
int sceKernelDeleteHeap(int heapId);

0 comments on commit e02d5bf

Please sign in to comment.