From 7e9c30606d6c2dd0956ac5ca0f851d3e6ef6f89c Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 1 Oct 2024 19:40:59 +0300 Subject: [PATCH] try to clean up various mmap func failure return value confusion such a mess --- frontend/libretro.c | 32 ++++++++++++++++++++------------ frontend/plat_pollux.c | 6 +++--- frontend/plugin_lib.c | 2 +- libpcsxcore/psxmem.c | 4 ++-- plugins/gpu_neon/psx_gpu_if.c | 3 ++- plugins/gpu_unai/gpulib_if.cpp | 3 ++- plugins/gpulib/gpu.c | 5 +---- 7 files changed, 31 insertions(+), 24 deletions(-) diff --git a/frontend/libretro.c b/frontend/libretro.c index dd45d168..bcd3c615 100644 --- a/frontend/libretro.c +++ b/frontend/libretro.c @@ -8,6 +8,7 @@ #define _GNU_SOURCE 1 // strcasestr #include #include +#include #include #include #ifdef __MACH__ @@ -51,6 +52,10 @@ #include "3ds/3ds_utils.h" #endif +#ifndef MAP_FAILED +#define MAP_FAILED ((void *)(intptr_t)-1) +#endif + #define PORTS_NUMBER 8 #ifndef MIN @@ -391,11 +396,12 @@ static u32 mapped_addrs[8]; static u32 mapped_ram, mapped_ram_src; // http://3dbrew.org/wiki/Memory_layout#ARM11_User-land_memory_regions -void *pl_3ds_mmap(unsigned long addr, size_t size, +static void *pl_3ds_mmap(unsigned long addr, size_t size, enum psxMapTag tag, int *can_retry_addr) { - (void)addr; + void *ret = MAP_FAILED; *can_retry_addr = 0; + (void)addr; if (__ctr_svchax) do { @@ -404,7 +410,6 @@ void *pl_3ds_mmap(unsigned long addr, size_t size, u32 found_addr = 0; MemInfo mem_info; PageInfo page_info; - void *ret = NULL; size_t i; int r; @@ -460,10 +465,11 @@ void *pl_3ds_mmap(unsigned long addr, size_t size, } while (0); - return calloc(size, 1); + ret = calloc(size, 1); + return ret ? ret : MAP_FAILED; } -void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag) +static void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag) { (void)tag; @@ -503,7 +509,7 @@ typedef struct static void *addr = NULL; -psx_map_t custom_psx_maps[] = { +static psx_map_t custom_psx_maps[] = { { NULL, 0x800000, MAP_TAG_LUTS }, { NULL, 0x080000, MAP_TAG_OTHER }, { NULL, 0x010000, MAP_TAG_OTHER }, @@ -512,7 +518,7 @@ psx_map_t custom_psx_maps[] = { { NULL, 0x210000, MAP_TAG_RAM }, }; -int init_vita_mmap() +static int init_vita_mmap() { int n; void *tmpaddr; @@ -535,7 +541,7 @@ int init_vita_mmap() return 0; } -void deinit_vita_mmap() +static void deinit_vita_mmap() { size_t i; for (i = 0; i < sizeof(custom_psx_maps) / sizeof(custom_psx_maps[0]); i++) { @@ -545,9 +551,10 @@ void deinit_vita_mmap() free(addr); } -void *pl_vita_mmap(unsigned long addr, size_t size, +static void *pl_vita_mmap(unsigned long addr, size_t size, enum psxMapTag tag, int *can_retry_addr) { + void *ret; (void)addr; *can_retry_addr = 0; @@ -562,10 +569,11 @@ void *pl_vita_mmap(unsigned long addr, size_t size, } } - return calloc(size, 1); + ret = calloc(size, 1); + return ret ? ret : MAP_FAILED; } -void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag) +static void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag) { (void)tag; @@ -3822,7 +3830,7 @@ void retro_init(void) if (vout_buf == NULL) { LogErr("OOM for vout_buf.\n"); - exit(1); + // may be able to continue if we get retro_framebuffer access } vout_buf_ptr = vout_buf; diff --git a/frontend/plat_pollux.c b/frontend/plat_pollux.c index f349cad1..a27b410d 100644 --- a/frontend/plat_pollux.c +++ b/frontend/plat_pollux.c @@ -475,12 +475,12 @@ static void *pl_emu_mmap(unsigned long addr, size_t size, } basic_map: - retval = plat_mmap(addr, size, 0, is_fixed); + retval = plat_mmap(addr, size, 0, 0); out: - if (tag == MAP_TAG_VRAM) + if (tag == MAP_TAG_VRAM && retval) psx_vram = retval; - return retval; + return retval ? retval : MAP_FAILED; } static void pl_emu_munmap(void *ptr, size_t size, enum psxMapTag tag) diff --git a/frontend/plugin_lib.c b/frontend/plugin_lib.c index 21d6863d..c8a6fed4 100644 --- a/frontend/plugin_lib.c +++ b/frontend/plugin_lib.c @@ -877,7 +877,7 @@ static void *pl_emu_mmap(unsigned long addr, size_t size, enum psxMapTag tag, int *can_retry_addr) { *can_retry_addr = 1; - return plat_mmap(addr, size, 0, is_fixed); + return plat_mmap(addr, size, 0, 0); } static void pl_emu_munmap(void *ptr, size_t size, enum psxMapTag tag) diff --git a/libpcsxcore/psxmem.c b/libpcsxcore/psxmem.c index a8dfaa83..ad472592 100644 --- a/libpcsxcore/psxmem.c +++ b/libpcsxcore/psxmem.c @@ -97,11 +97,11 @@ void *psxMap(unsigned long addr, size_t size, int is_fixed, if (ret != MAP_FAILED) psxUnmap(ret, size, tag); ret = psxMapHook(addr, size, tag, &can_retry_addr); - if (ret == NULL) + if (ret == MAP_FAILED) return MAP_FAILED; if (addr != 0 && ret != (void *)(uintptr_t)addr) { - SysMessage("psxMap: warning: wanted to map @%08x, got %p\n", + SysMessage("psxMap: tried to map @%08x, got %p\n", addr, ret); if (is_fixed) { psxUnmap(ret, size, tag); diff --git a/plugins/gpu_neon/psx_gpu_if.c b/plugins/gpu_neon/psx_gpu_if.c index b2e89999..3f43e431 100644 --- a/plugins/gpu_neon/psx_gpu_if.c +++ b/plugins/gpu_neon/psx_gpu_if.c @@ -87,8 +87,9 @@ static void map_enhancement_buffer(void) // to be able to reuse 1024-width code better (triangle setup, // dithering phase, lines). egpu.enhancement_buf_ptr = gpu.mmap(ENHANCEMENT_BUF_SIZE); - if (egpu.enhancement_buf_ptr == NULL) { + if (egpu.enhancement_buf_ptr == NULL || egpu.enhancement_buf_ptr == (void *)(intptr_t)-1) { fprintf(stderr, "failed to map enhancement buffer\n"); + egpu.enhancement_buf_ptr = NULL; gpu.get_enhancement_bufer = NULL; } else { diff --git a/plugins/gpu_unai/gpulib_if.cpp b/plugins/gpu_unai/gpulib_if.cpp index 6816e2bd..5cc7792b 100644 --- a/plugins/gpu_unai/gpulib_if.cpp +++ b/plugins/gpu_unai/gpulib_if.cpp @@ -201,8 +201,9 @@ static void map_downscale_buffer(void) gpu_unai.downscale_vram = (le16_t*)gpu.mmap(DOWNSCALE_VRAM_SIZE); - if (gpu_unai.downscale_vram == NULL) { + if (gpu_unai.downscale_vram == NULL || gpu_unai.downscale_vram == (le16_t *)(intptr_t)-1) { fprintf(stderr, "failed to map downscale buffer\n"); + gpu_unai.downscale_vram = NULL; gpu.get_downscale_buffer = NULL; } else { diff --git a/plugins/gpulib/gpu.c b/plugins/gpulib/gpu.c index f6340e11..70f21293 100644 --- a/plugins/gpulib/gpu.c +++ b/plugins/gpulib/gpu.c @@ -267,6 +267,7 @@ static int map_vram(void) } else { fprintf(stderr, "could not map vram, expect crashes\n"); + gpu.vram = NULL; return -1; } } @@ -285,10 +286,6 @@ long GPUinit(void) gpu.cmd_len = 0; do_reset(); - /*if (gpu.mmap != NULL) { - if (map_vram() != 0) - ret = -1; - }*/ return ret; } -- 2.39.5