From 2d8aaaac5e88a7f14d432f3cd8d2260bb370cf67 Mon Sep 17 00:00:00 2001 From: sum2012 Date: Wed, 2 Nov 2022 15:50:30 +0800 Subject: [PATCH] Change to if (!heap) first in all function As unknown feedback 2 --- Core/HLE/sceKernelHeap.cpp | 90 +++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 50 deletions(-) diff --git a/Core/HLE/sceKernelHeap.cpp b/Core/HLE/sceKernelHeap.cpp index 579232cb5f..a3461d0fa4 100644 --- a/Core/HLE/sceKernelHeap.cpp +++ b/Core/HLE/sceKernelHeap.cpp @@ -73,29 +73,27 @@ static int sceKernelCreateHeap(int partitionId, int size, int flags, const char static int sceKernelAllocHeapMemory(int heapId, int size) { u32 error; KernelHeap *heap = kernelObjects.Get(heapId, error); - if (heap) { - // There's 8 bytes at the end of every block, reserved. - u32 memSize = KERNEL_HEAP_BLOCK_HEADER_SIZE + size; - u32 addr = heap->alloc.Alloc(memSize, true); - return hleLogSuccessInfoX(SCEKERNEL, addr); - } else { - return hleLogError(SCEKERNEL, error, "invalid heapId"); - } + if (!heap) + return hleLogError(SCEKERNEL, error, "sceKernelAllocHeapMemory(%d): invalid heapId", heapId); + + // There's 8 bytes at the end of every block, reserved. + u32 memSize = KERNEL_HEAP_BLOCK_HEADER_SIZE + size; + u32 addr = heap->alloc.Alloc(memSize, true); + return hleLogSuccessInfoX(SCEKERNEL, addr); } static int sceKernelDeleteHeap(int heapId) { u32 error; KernelHeap *heap = kernelObjects.Get(heapId, error); - if (heap) { - // Not using heap->partitionId here for backwards compatibility with old save states. - BlockAllocator *allocator = BlockAllocatorFromAddr(heap->address); - if (allocator) - allocator->Free(heap->address); - kernelObjects.Destroy(heap->uid); - return hleLogSuccessInfoX(SCEKERNEL, 0); - } else { - return hleLogError(SCEKERNEL, error, "invalid heapId"); - } + if (!heap) + return hleLogError(SCEKERNEL, error, "sceKernelDeleteHeap(%d): invalid heapId", heapId); + + // Not using heap->partitionId here for backwards compatibility with old save states. + BlockAllocator *allocator = BlockAllocatorFromAddr(heap->address); + if (allocator) + allocator->Free(heap->address); + kernelObjects.Destroy(heap->uid); + return hleLogSuccessInfoX(SCEKERNEL, 0); } static u32 sceKernelPartitionTotalFreeMemSize(int partitionId) { @@ -123,47 +121,39 @@ static u32 SysMemForKernel_536AD5E1() static int sceKernelFreeHeapMemory(int heapId, u32 block) { u32 error; KernelHeap* heap = kernelObjects.Get(heapId, error); - if (heap) { - if (block == 0) { - return hleLogSuccessInfoI(SCEKERNEL, 0, "sceKernelFreeHeapMemory(%d): heapId,0: block", heapId); - } - if (!heap->alloc.FreeExact(block)) { - ERROR_LOG(SCEKERNEL, "sceKernelFreeHeapMemory(%d): heapId, block Invalid pointer ", heapId, block); - return SCE_KERNEL_ERROR_INVALID_POINTER; - } - return hleLogSuccessInfoI(SCEKERNEL, 0, "sceKernelFreeHeapMemory(%d): heapId, block", heapId, block); - } - else { + if (!heap) return hleLogError(SCEKERNEL, error, "sceKernelFreeHeapMemory(%d): invalid heapId", heapId); + if (block == 0) { + return hleLogSuccessInfoI(SCEKERNEL, 0, "sceKernelFreeHeapMemory(%d): heapId,0: block", heapId); } + if (!heap->alloc.FreeExact(block)) { + ERROR_LOG(SCEKERNEL, "sceKernelFreeHeapMemory(%d): heapId, block Invalid pointer ", heapId, block); + return SCE_KERNEL_ERROR_INVALID_POINTER; + } + return hleLogSuccessInfoI(SCEKERNEL, 0, "sceKernelFreeHeapMemory(%d): heapId, block", heapId, block); + } static int sceKernelAllocHeapMemoryWithOption(int heapId, u32 memSize, u32 paramsPtr) { u32 error; KernelHeap* heap = kernelObjects.Get(heapId, error); - if (heap) { - u32 grain = 4; - // 0 is ignored. - if (paramsPtr != 0) { - u32 size = Memory::Read_U32(paramsPtr); - if (size < 8) { - ERROR_LOG(HLE, "sceKernelAllocHeapMemoryWithOption(%08x, %08x, %08x): invalid param size", heapId, memSize, paramsPtr); - return 0; - } - if (size > 8) { - WARN_LOG(HLE, "sceKernelAllocHeapMemoryWithOption(): unexpected param size %d", size); - } - grain = Memory::Read_U32(paramsPtr + 4); - } - INFO_LOG(HLE, "sceKernelAllocHeapMemoryWithOption(%08x, %08x, %08x)", heapId, memSize, paramsPtr); - // There's 8 bytes at the end of every block, reserved. - memSize += 8; - u32 addr = heap->alloc.AllocAligned(memSize, grain, grain, true); - return addr; - } - else { + if (!heap) return hleLogError(SCEKERNEL, error, "sceKernelFreeHeapMemory(%d): invalid heapId", heapId); + u32 grain = 4; + // 0 is ignored. + if (paramsPtr != 0) { + u32 size = Memory::Read_U32(paramsPtr); + if (size < 8) + return hleLogError(SCEKERNEL, 0, "invalid param size"); + if (size > 8) + WARN_LOG(HLE, "sceKernelAllocHeapMemoryWithOption(): unexpected param size %d", size); + grain = Memory::Read_U32(paramsPtr + 4); } + INFO_LOG(HLE, "sceKernelAllocHeapMemoryWithOption(%08x, %08x, %08x)", heapId, memSize, paramsPtr); + // There's 8 bytes at the end of every block, reserved. + memSize += 8; + u32 addr = heap->alloc.AllocAligned(memSize, grain, grain, true); + return addr; } const HLEFunction SysMemForKernel[] = {