From 701d22d935c160255533b58bb835a6c4d93dd724 Mon Sep 17 00:00:00 2001 From: zoltanvb <101990835+zoltanvb@users.noreply.github.com> Date: Mon, 6 Nov 2023 00:33:17 +0100 Subject: [PATCH] PS2 usability fixes (#15861) - Make sure logs are written before frontend deinit - Add memory stats - Add process_args to frontend to fix some cases when salamander cfg was not filled - Add a missing include in case someone wants to compile for PS2 with HAVE_THREADS --- frontend/drivers/platform_ps2.c | 57 +++++++++++++++++++++++++++-- libretro-common/rthreads/rthreads.c | 4 ++ retroarch.c | 6 ++- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/frontend/drivers/platform_ps2.c b/frontend/drivers/platform_ps2.c index 08694367c7..f9815c9c0c 100644 --- a/frontend/drivers/platform_ps2.c +++ b/frontend/drivers/platform_ps2.c @@ -416,6 +416,46 @@ enum frontend_architecture frontend_ps2_get_arch(void) return FRONTEND_ARCH_MIPS; } +static uint64_t frontend_ps2_get_total_mem(void) { return 32*1024*1024; } + +/* Crude try-and-fail approach, in lack of a better solution. */ +static uint64_t frontend_ps2_get_free_mem(void) +{ + uint64_t free_mem; + size_t s0 = 32*1024*1024; + void* p1; + void* p2; + void* p3; + + while (s0 && (p1 = malloc(s0)) == NULL) + s0 >>= 1; + + free_mem = s0; + + s0 = 32*1024*1024; + + while (s0 && (p2 = malloc(s0)) == NULL) + s0 >>= 1; + + free_mem += s0; + + s0 = 32*1024*1024; + + while (s0 && (p3 = malloc(s0)) == NULL) + s0 >>= 1; + + free_mem += s0; + + if (p1) + free(p1); + if (p2) + free(p2); + if (p3) + free(p3); + + return free_mem; +} + static int frontend_ps2_parse_drive_list(void *data, bool load_content) { #ifndef IS_SALAMANDER @@ -474,12 +514,23 @@ static int frontend_ps2_parse_drive_list(void *data, bool load_content) return 0; } +static void frontend_ps2_process_args(int *argc, char *argv[]) +{ +#ifndef IS_SALAMANDER + /* Make sure active core path is set here. */ + char path[PATH_MAX_LENGTH] = {0}; + strlcpy(path, argv[0], sizeof(path)); + if (path_is_valid(path)) + path_set(RARCH_PATH_CORE, path); +#endif +} + frontend_ctx_driver_t frontend_ctx_ps2 = { frontend_ps2_get_env, /* get_env */ frontend_ps2_init, /* init */ frontend_ps2_deinit, /* deinit */ frontend_ps2_exitspawn, /* exitspawn */ - NULL, /* process_args */ + frontend_ps2_process_args, /* process_args */ frontend_ps2_exec, /* exec */ #ifdef IS_SALAMANDER NULL, /* set_fork */ @@ -494,8 +545,8 @@ frontend_ctx_driver_t frontend_ctx_ps2 = { frontend_ps2_get_arch, /* get_architecture */ NULL, /* get_powerstate */ frontend_ps2_parse_drive_list,/* parse_drive_list */ - NULL, /* get_total_mem */ - NULL, /* get_free_mem */ + frontend_ps2_get_total_mem, /* get_total_mem */ + frontend_ps2_get_free_mem, /* get_free_mem */ NULL, /* install_signal_handler */ NULL, /* get_sighandler_state */ NULL, /* set_sighandler_state */ diff --git a/libretro-common/rthreads/rthreads.c b/libretro-common/rthreads/rthreads.c index b923a8e290..c0b69f9195 100644 --- a/libretro-common/rthreads/rthreads.c +++ b/libretro-common/rthreads/rthreads.c @@ -60,6 +60,10 @@ #include #endif +#if defined(PS2) +#include +#endif + #ifdef __MACH__ #include #include diff --git a/retroarch.c b/retroarch.c index da86d7bfb9..b8d37eaa28 100644 --- a/retroarch.c +++ b/retroarch.c @@ -4811,7 +4811,11 @@ void main_exit(void *args) #if defined(HAVE_LOGGER) && !defined(ANDROID) logger_shutdown(); #endif - +#ifdef PS2 + /* PS2 frontend driver deinit also detaches filesystem, + * so make sure logs are written in advance. */ + retro_main_log_file_deinit(); +#endif frontend_driver_deinit(args); frontend_driver_exitspawn( path_get_ptr(RARCH_PATH_CORE),